5 string timed_read(file f, int timeout) {
6 thread reader = fork func() {
9 } catch Thread::signal(int i) {
14 thread killer = fork func() {
17 Thread::send_signal(reader, 1);
18 } catch Thread::signal(int i) {
23 poly v = Thread::join(reader);
24 Thread::send_signal(killer, 1);
31 void flush_input(file f) {
33 string s = timed_read(f, 200);
39 string[*] flash(file f) {
43 fprintf (f, "f\nv\n");
46 string l = timed_read(f, 1000);
48 File::fprintf(stderr, "Read timedout\n");
52 if (String::index(l, "software-version") == 0)
58 string[*] find_flash(string[*] s, string match) {
59 for (int i = 0; i < dim(s); i++)
60 if (String::index(s[i], match) >= 0)
61 return String::wordsplit(s[i], " ");
62 return (string[*]) {};
66 do_flash(file f, int expected_size) {
67 string[*] i = flash(f);
68 string[*] size = find_flash(i, "Storage size:");
69 string[*] erase = find_flash(i, "Storage erase unit:");
71 int actual_size = string_to_integer(size[2]);
73 if (actual_size != expected_size) {
74 printf ("weird flash size %d != %d\n", actual_size, expected_size);
78 int actual_erase = string_to_integer(erase[3]);
80 if (actual_erase != 65536) {
81 printf ("weird erase size %d\n", actual_erase);
85 printf ("flash size %d erase block %d\n", actual_size, actual_erase);
91 string name = argv[1];
92 string size = argv[2];
93 file f = open(name, "r+");
96 if (!do_flash(f, string_to_integer(size)))