X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=ao-bringup%2Ftest-flash;fp=ao-bringup%2Ftest-flash;h=d79de320d565c258586a6ce5aca117475da782b7;hb=3cf030fffffd223c3717011e03aac82346295d71;hp=0000000000000000000000000000000000000000;hpb=607fbb01710be1cb263625337f5be3d0fb48d5e7;p=fw%2Faltos diff --git a/ao-bringup/test-flash b/ao-bringup/test-flash new file mode 100755 index 00000000..d79de320 --- /dev/null +++ b/ao-bringup/test-flash @@ -0,0 +1,101 @@ +#!/usr/bin/nickle + +import File; + +string timed_read(file f, int timeout) { + thread reader = fork func() { + try { + return fgets(f); + } catch Thread::signal(int i) { + return ""; + } + }(); + + thread killer = fork func() { + try { + sleep (timeout); + Thread::send_signal(reader, 1); + } catch Thread::signal(int i) { + return; + } + }(); + + poly v = Thread::join(reader); + Thread::send_signal(killer, 1); + Thread::join(killer); + if (is_string(v)) + return v; + return ""; +} + +void flush_input(file f) { + for (;;) { + string s = timed_read(f, 200); + if (s == "") + break; + } +} + +string[*] flash(file f) { + string[...] x = {}; + + flush_input(f); + fprintf (f, "f\nv\n"); + flush(f); + for (;;) { + string l = timed_read(f, 1000); + if (l == "") { + File::fprintf(stderr, "Read timedout\n"); + exit(1); + } + x[dim(x)] = l; + if (String::index(l, "software-version") == 0) + break; + } + return x; +} + +string[*] find_flash(string[*] s, string match) { + for (int i = 0; i < dim(s); i++) + if (String::index(s[i], match) >= 0) + return String::wordsplit(s[i], " "); + return (string[*]) {}; +} + +bool +do_flash(file f, int expected_size) { + string[*] i = flash(f); + string[*] size = find_flash(i, "Storage size:"); + string[*] erase = find_flash(i, "Storage erase unit:"); + + int actual_size = string_to_integer(size[2]); + + if (actual_size != expected_size) { + printf ("weird flash size %d != %d\n", actual_size, expected_size); + return false; + } + + int actual_erase = string_to_integer(erase[3]); + + if (actual_erase != 65536) { + printf ("weird erase size %d\n", actual_erase); + return false; + } + + printf ("flash size %d erase block %d\n", actual_size, actual_erase); + + return true; +} + +void main () { + string name = argv[1]; + string size = argv[2]; + file f = open(name, "r+"); + bool ret = true; + + if (!do_flash(f, string_to_integer(size))) + ret = false; + exit (ret? 0 : 1); +} + +main();