flush rx buffer when bad crc and fix frame payload length
authorFeliks <feliks.montez@gmail.com>
Tue, 24 Mar 2020 03:04:36 +0000 (23:04 -0400)
committerFeliks <feliks.montez@gmail.com>
Tue, 24 Mar 2020 03:04:36 +0000 (23:04 -0400)
litex/soc/software/bios/boot.c
litex/soc/software/bios/sfl.h
litex/tools/litex_term.py

index 41230ec36a8a9edf488b82a6ebab879c2470be9f..62c83e3072e48e394227f1d78587b08ad064c4c3 100644 (file)
@@ -122,6 +122,7 @@ int serialboot(void)
        printf("Booting from serial...\n");
        printf("Press Q or ESC to abort boot completely.\n");
 
+       // send the "magic" request to host for a firmware download
        c = str;
        while(*c) {
                uart_write(*c);
@@ -145,18 +146,22 @@ int serialboot(void)
                int goodcrc;
 
                /* Get one Frame */
-               frame.length = uart_read();
+               frame.payload_length = uart_read();
                frame.crc[0] = uart_read();
                frame.crc[1] = uart_read();
                frame.cmd = uart_read();
-               for(i=0;i<frame.length;i++)
+               for(i=0;i<frame.payload_length;i++)
                        frame.payload[i] = uart_read();
 
                /* Check Frame CRC (if CMD has a CRC) */
                if (frame.cmd != SFL_CMD_LOAD_NO_CRC) {
                        actualcrc = ((int)frame.crc[0] << 8)|(int)frame.crc[1];
-                       goodcrc = crc16(&frame.cmd, frame.length+1);
+                       goodcrc = crc16(&frame.cmd, frame.payload_length+1);
                        if(actualcrc != goodcrc) {
+                               // clear out the RX buffer
+                               while (uart_read_nonblock()) {
+                                       uart_read();
+                               }
                                failed++;
                                if(failed == MAX_FAILED) {
                                        printf("Too many consecutive errors, aborting");
@@ -179,7 +184,7 @@ int serialboot(void)
 
                                failed = 0;
                                writepointer = (char *) get_uint32(&frame.payload[0]);
-                               for(i=4;i<frame.length;i++)
+                               for(i=4;i<frame.payload_length;i++)
                                        *(writepointer++) = frame.payload[i];
                                if (frame.cmd == SFL_CMD_LOAD)
                                        uart_write(SFL_ACK_SUCCESS);
@@ -201,7 +206,7 @@ int serialboot(void)
                                failed = 0;
                                addr = get_uint32(&frame.payload[0]);
 
-                               for (i = 4; i < frame.length; i++) {
+                               for (i = 4; i < frame.payload_length; i++) {
                                        // erase page at sector boundaries before writing
                                        if ((addr & (SPIFLASH_SECTOR_SIZE - 1)) == 0) {
                                                erase_flash_sector(addr);
index cca67f6f1911fbd2efc6c731cc17047e7ec9b408..b09aee485528bf3e80626c3f9fdb8f6077498b25 100644 (file)
@@ -9,7 +9,7 @@
 #define SFL_MAGIC_ACK "z6IHG7cYDID6o\n"
 
 struct sfl_frame {
-       unsigned char length;
+       unsigned char payload_length;
        unsigned char crc[2];
        unsigned char cmd;
        unsigned char payload[255];
index a71245d4cb38aa0936a3e7a8b63468a506bbcca6..107dee6309157a9180a02624ae7d4b1757a40a85 100755 (executable)
@@ -52,7 +52,7 @@ sfl_prompt_ack = b"\x06"
 sfl_magic_req = b"sL5DdSMmkekro\n"
 sfl_magic_ack = b"z6IHG7cYDID6o\n"
 
-sfl_payload_length = 251
+sfl_payload_length = 64#251
 
 # General commands
 sfl_cmd_abort       = b"\x00"
@@ -212,7 +212,7 @@ class LiteXTerm:
                                                     100*position//length))
             sys.stdout.flush()
             frame = SFLFrame()
-            frame_data = f.read(min(remaining, sfl_payload_length))
+            frame_data = f.read(min(remaining, sfl_payload_length-4))
             if self.flash:
                 frame.cmd = sfl_cmd_flash
             else: