flashesp.pl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. #!/usr/bin/perl
  2. use strict;
  3. use integer;
  4. use PerlIO::gzip;
  5. use File::Temp;
  6. use File::Spec;
  7. use Time::HiRes qw(usleep tv_interval);
  8. use Digest::CRC qw(crc32);
  9. my $esptool = ($^O eq 'MSWin32') ? 'esptool.exe' : 'esptool.py';
  10. $esptool = $ENV{'ESPTOOL'} || $esptool;
  11. my $FW_MAGIC = 0x7a07fbd6;
  12. my %datatypes = (
  13. 'end' => 0, # End of data
  14. 'data' => 1, # FPGA flash data
  15. 'target' => 2, # Firmware target string
  16. 'note' => 3, # Informative string
  17. 'espota' => 4, # ESP32 OTA image
  18. 'fpgainit' => 5, # FPGA bypass (transient) image during update
  19. 'esppart' => 6, # ESP32 partition table
  20. 'espsys' => 7, # ESP32 boot loader, OTA control partition...
  21. 'esptool' => 8 # esptool.py options for flashing
  22. );
  23. my @type;
  24. foreach my $t (keys(%datatypes)) {
  25. $type[$datatypes{$t}] = $t;
  26. }
  27. my $FDF_OPTIONAL = 0x0001;
  28. my $STRING_MAX_LEN = 4095;
  29. # For debug; DC1-4 replaced with functional names
  30. my @ascii = qw(NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI
  31. DLE XON WRST XOFF WGO NAK SYN ETB CAN EM SUB ESC FS GS RS US);
  32. foreach my $i (9, 10, 13, 32..126) {
  33. $ascii[$i] = chr($i);
  34. }
  35. $ascii[127] = 'DEL';
  36. for (my $i = 128; $i < 256; $i++) {
  37. $ascii[$i] = sprintf("%02X", $i);
  38. }
  39. my @a = map { length($_) == 1 ? $_ : '<'.$_.'>' } @ascii;
  40. # Simple base64 encode using 3F-7E, bytewise bigendian
  41. sub mybaseencode {
  42. my $nbits = 0;
  43. my $bitbuf = 0;
  44. my $out = '';
  45. foreach my $s (@_) {
  46. foreach my $c (unpack('C*', $s)) {
  47. $nbits += 8;
  48. $bitbuf = ($bitbuf << 8) + $c;
  49. while ($nbits >= 6) {
  50. $nbits -= 6;
  51. $out .= pack('C', 63 + (($bitbuf >> $nbits) & 63));
  52. }
  53. }
  54. }
  55. if ($nbits) {
  56. $out .= pack('C', 63 + ($bitbuf & ((1 << $nbits) - 1)));
  57. }
  58. return $out;
  59. }
  60. sub getint($) {
  61. my($s) = @_;
  62. return undef
  63. unless ($s =~ /^(([1-9][0-9]+)|(0(x[0-9a-f]+|[0-7]*)))([kmgtpe]?)$/i);
  64. my $o = oct($3) + $2;
  65. my $p = lc($5);
  66. if ($p eq 'k') {
  67. $o <<= 10;
  68. } elsif ($p eq 'm') {
  69. $o <<= 20;
  70. } elsif ($p eq 'g') {
  71. $o <<= 30;
  72. } elsif ($p eq 't') {
  73. $o <<= 40;
  74. } elsif ($p eq 'p') {
  75. $o <<= 50;
  76. } elsif ($p eq 'e') {
  77. $o <<= 60;
  78. }
  79. return $o;
  80. }
  81. sub filelen($) {
  82. my($f) = @_;
  83. my @s = stat($f);
  84. return $s[7];
  85. }
  86. sub unquote_cmd($) {
  87. my($s) = @_;
  88. my @a;
  89. $s =~ s/[\r\n]+/ /g;
  90. while ($s =~ /^\s*(?:\"((?:[^\"]+|\"\")*)\"|(\S+))(\s.*)?$/) {
  91. push(@a, $1.$2);
  92. $s = $3;
  93. }
  94. return @a;
  95. }
  96. my @args = @ARGV;
  97. my $esponly = 0;
  98. my $file;
  99. while (1) {
  100. $file = shift(@args);
  101. last if ($file !~ /^\-/);
  102. if ($file eq '--esponly') {
  103. $esponly = 1;
  104. } elsif ($file eq '--') {
  105. $file = shift(@args);
  106. last;
  107. } else {
  108. undef $file; # Invalid argument, print usage
  109. last;
  110. }
  111. }
  112. my $port = shift(@args);
  113. if (!defined($port)) {
  114. die "Usage: $0 file.fw port [esptool options...]\n";
  115. }
  116. if (!File::Spec->file_name_is_absolute($port)) {
  117. if (-c "/dev/$port") {
  118. $port = "/dev/$port";
  119. } elsif (-c "/dev/tty$port") {
  120. $port = "/dev/tty$port";
  121. } elsif ($^O eq 'MSWin32') {
  122. $port = "\\\\.\\$port";
  123. } else {
  124. die "$0: no such serial port: $port\n";
  125. }
  126. }
  127. print STDERR "Using serial port device $port\n";
  128. open(my $fw, '<:gzip', $file)
  129. or die "$0: $file: $!\n";
  130. my @chunks = ();
  131. my $err = 0;
  132. my $hdr;
  133. while (read($fw, $hdr, 16) == 16) {
  134. # magic type flags data_len addr
  135. my @h = unpack('VvvVV', $hdr);
  136. my $c = { 'hdr' => $hdr, 'magic' => $h[0], 'type' => $h[1],
  137. 'flags' => $h[2], 'len' => $h[3], 'addr' => $h[4] };
  138. if ($c->{'magic'} != $FW_MAGIC) {
  139. print STDERR "$0: $file: bad chunk magic\n";
  140. $err = 1;
  141. last;
  142. }
  143. my $t = $type[$c->{'type'}];
  144. my $d;
  145. if (read($fw, $d, $c->{'len'}) != $c->{'len'}) {
  146. print STDERR "$0: $file: short chunk read\n";
  147. $err = 1;
  148. last;
  149. }
  150. $c->{'data'} = $d;
  151. push(@chunks, $c);
  152. last if ($t eq 'end'); # End of stream
  153. }
  154. close($fw);
  155. exit $err if ($err);
  156. my $td = File::Temp->newdir(CLEANUP => 1);
  157. my @espfiles = ();
  158. my %espopt = ('before' => 'default_reset', 'after' => 'hard_reset',
  159. 'baud' => 115200, 'port' => undef, 'chip' => undef,
  160. 'flash_mode' => undef, 'flash_freq' => undef,
  161. 'flash_size' => undef);
  162. # Create a compressed data buffer without the ESP32 chunks
  163. my $fpgadata;
  164. open(my $fpgafh, '>:gzip', \$fpgadata) or die;
  165. my $nc = 0;
  166. foreach my $c ( @chunks ) {
  167. my $t = $type[$c->{'type'}];
  168. if ($t eq 'esptool') {
  169. my $s = $c->{'data'};
  170. $s =~ s/[\r\n]+/ /g;
  171. while ($s =~ /^\s*(\S+)\s+(\S+)(.*)/) {
  172. $espopt{$1} = $2;
  173. $s = $3;
  174. }
  175. } elsif ($t =~ /^esp/ && $c->{'addr'}) {
  176. my $addr = sprintf('%x', $c->{'addr'});
  177. my $tff = File::Spec->catfile($td, $t.$nc.'_'.$addr.'.bin');
  178. open(my $tf, '>', $tff) or die "$0: $tff: $!\n";
  179. print $tf $c->{'data'};
  180. close($tf);
  181. push(@espfiles, '0x'.$addr, $tff);
  182. } else {
  183. print $fpgafh $c->{'hdr'};
  184. print $fpgafh $c->{'data'};
  185. }
  186. $nc++;
  187. }
  188. close($fpgafh);
  189. foreach my $e (keys %espopt) {
  190. my $ev = $ENV{"ESP\U$e"};
  191. if (defined($ev)) {
  192. $espopt{$e} = $ev;
  193. }
  194. }
  195. $espopt{'port'} = $port;
  196. my @espcmd = unquote_cmd($esptool);
  197. foreach my $o (sort grep (!/^flash_/, keys %espopt)) {
  198. if (defined($espopt{$o})) {
  199. push(@espcmd, "--$o", $espopt{$o});
  200. }
  201. }
  202. push(@espcmd, 'write_flash', '-z');
  203. foreach my $o (sort grep (/^flash_/, keys %espopt)) {
  204. if (defined($espopt{$o})) {
  205. push(@espcmd, "--$o", $espopt{$o});
  206. }
  207. }
  208. push(@espcmd, @espfiles);
  209. print STDERR join(' ', @espcmd), "\n";
  210. $err = system(@espcmd);
  211. if ($err == -1) {
  212. print STDERR "$0: $espcmd[0]: $!\n";
  213. } elsif ($err) {
  214. print STDERR "$0: $espcmd[0]: exit $err\n";
  215. }
  216. exit $err if ($err || $esponly);
  217. my $SerialPort = eval {
  218. require Device::SerialPort;
  219. Device::SerialPort->import(qw(:PARAM));
  220. 'Device::SerialPort';
  221. } || eval {
  222. require Win32::SerialPort;
  223. Win32::SerialPort->import(qw(:STAT));
  224. 'Win32::SerialPort';
  225. } || die "$0: need Device::SerialPort or Win32::SerialPort\n";
  226. print STDERR "Waiting for reinit...\n";
  227. usleep(4000000);
  228. my $tty = $SerialPort->new($port);
  229. die "$0: $port: $!\n" if (!$tty);
  230. $tty->buffers($tty->buffer_max);
  231. $tty->reset_error;
  232. $tty->user_msg(1);
  233. $tty->error_msg(1);
  234. $tty->handshake('none');
  235. $tty->databits(8);
  236. $tty->baudrate(115200);
  237. $tty->parity('none');
  238. $tty->stopbits(1);
  239. $tty->datatype('raw');
  240. $tty->stty_ignbrk(0);
  241. #$tty->stty_brkint(0);
  242. $tty->stty_parmrk(0);
  243. $tty->stty_istrip(0);
  244. $tty->stty_inlcr(0);
  245. $tty->stty_igncr(0);
  246. $tty->stty_icrnl(0);
  247. $tty->stty_opost(0);
  248. $tty->stty_echo(0);
  249. $tty->stty_echonl(0);
  250. $tty->stty_icanon(0);
  251. $tty->stty_isig(0);
  252. #$tty->stty_iexten(0);
  253. $tty->read_char_time(0);
  254. $tty->write_settings;
  255. # In case DTR and/or RTS was asserted on the physical serial port.
  256. # Note that DTR needs to be deasserted before RTS!
  257. # However, deasserting DTR on the ACM port prevents it from working,
  258. # so only do this if we don't see CTS (which is always enabled on ACM)...
  259. if ($tty->can_modemlines && !($tty->modemlines & $tty->MS_CTS_ON)) {
  260. usleep(100000);
  261. $tty->dtr_active(0);
  262. usleep(100000);
  263. $tty->rts_active(0);
  264. usleep(100000);
  265. }
  266. sub tty_read {
  267. my($tty,$bufref,$timeout) = @_;
  268. my $d = $$bufref;
  269. if ($d eq '') {
  270. my $c;
  271. $tty->read_const_time($timeout);
  272. ($c,$d) = $tty->read(256);
  273. return '' if (!$c);
  274. }
  275. my $r = substr($d,0,1);
  276. $$bufref = substr($d,1);
  277. return $r;
  278. }
  279. my $ttybuf = '';
  280. sub tty_write($$) {
  281. my($tty,$data) = @_;
  282. my $bytes = length($data);
  283. my $offs = 0;
  284. while ($bytes) {
  285. my $cnt = $tty->write(substr($data,$offs,$bytes));
  286. usleep(10000) unless ($cnt);
  287. $offs += $cnt;
  288. $bytes -= $cnt;
  289. }
  290. return $offs;
  291. }
  292. my $found = 0;
  293. my $tt = time();
  294. my $start_enq = $tt;
  295. tty_write($tty, "\005"); # ENQ
  296. while (($tt = time()) - $start_enq < 30) {
  297. my $d = tty_read($tty, \$ttybuf, 1000);
  298. if ($d eq '') {
  299. tty_write($tty, "\005"); # ENQ
  300. } else {
  301. my $ix = index("\026\004\027", $d);
  302. if ($ix < 0) {
  303. print STDERR $d;
  304. next;
  305. }
  306. $found = $found == $ix ? $found+1 : 0;
  307. last if ($found == 3);
  308. }
  309. }
  310. if ($found < 3) {
  311. die "$0: $port: no MAX80 ESP32 detected\n";
  312. }
  313. $start_enq = $tt;
  314. my $winspc;
  315. while (!defined($winspc)) {
  316. $tt = time();
  317. if ($tt - $start_enq >= 10) {
  318. die "$0: $port: failed to start FPGA firmware upload\n";
  319. }
  320. tty_write($tty, "\034\001: /// MAX80 FW UPLOAD \~\@\~ \$\r\n\035");
  321. my $d;
  322. while (1) {
  323. $d = tty_read($tty, \$ttybuf, 1000);
  324. last if ($d eq '');
  325. my $dc = unpack('C', $d);
  326. if ($dc == 036) {
  327. $winspc = 0;
  328. last;
  329. } else {
  330. print STDERR $a[$dc];
  331. }
  332. }
  333. }
  334. my $bytes = length($fpgadata);
  335. my $offset = 0;
  336. my $maxchunk = 64;
  337. my $maxahead = 256;
  338. my $last_ack = 0;
  339. my @pktends = ();
  340. print STDERR "\nStarting packet transmit...\n";
  341. while ($last_ack < $bytes) {
  342. my $chunk;
  343. while (1) {
  344. $chunk = $bytes - $offset;
  345. $chunk = $winspc if ($chunk > $winspc);
  346. if ($offset + $chunk > $last_ack + $maxahead) {
  347. $chunk = $last_ack + $maxahead - $offset;
  348. }
  349. $chunk = 0 if ($chunk <= 0);
  350. $chunk = $maxchunk if ($chunk > $maxchunk);
  351. my $d = tty_read($tty, \$ttybuf, $chunk ? 0 : 1000);
  352. last if ($d eq '');
  353. my $dc = unpack('C', $d);
  354. if ($dc == 022) {
  355. $winspc = 0;
  356. print STDERR "WRST, window: $winspc\n";
  357. } elsif ($dc == 024) {
  358. if (defined($winspc)) {
  359. $winspc += 256;
  360. print STDERR "WGO, window: $winspc\n";
  361. }
  362. } elsif ($dc == 006) {
  363. $last_ack = shift(@pktends) || $last_ack;
  364. print STDERR "ACK to $last_ack\n";
  365. } else {
  366. print STDERR $a[$dc];
  367. if ($dc == 025 || $dc == 031 || $dc == 037) {
  368. print STDERR "\n$0: $port: resetting upload to $last_ack\n";
  369. $offset = $last_ack;
  370. @pktends = ();
  371. undef $winspc; # Wait for WRST before resuming
  372. } elsif ($dc == 030) {
  373. print STDERR "\n";
  374. die "$0: $port: upload aborted by target system\n";
  375. }
  376. }
  377. }
  378. print STDERR "Send offset: $offset, window: $winspc, chunk: $chunk\n";
  379. if (!$chunk) {
  380. if ($bytes > $offset) {
  381. tty_write($tty, "\026"); # Request to resynchronize credits
  382. }
  383. next;
  384. }
  385. my $data = substr($fpgadata, $offset, $chunk);
  386. my $hdr = pack("CvVV", $chunk-1, 0, $offset, crc32($data));
  387. tty_write($tty, "\002".mybaseencode($hdr, $data));
  388. push(@pktends, $offset + $chunk);
  389. $offset += $chunk;
  390. $winspc -= $chunk;
  391. }
  392. tty_write($tty, "\004"); # EOT
  393. $tty->close;
  394. print STDERR "$0: $port: firmware upload complete\n";
  395. exit 0;