flush rx buffer when bad crc and fix frame payload length

This commit is contained in:
Feliks 2020-03-23 23:04:36 -04:00
parent d62ef38c4b
commit ebdc38fc91
3 changed files with 13 additions and 8 deletions

View File

@ -122,6 +122,7 @@ int serialboot(void)
printf("Booting from serial...\n"); printf("Booting from serial...\n");
printf("Press Q or ESC to abort boot completely.\n"); printf("Press Q or ESC to abort boot completely.\n");
// send the "magic" request to host for a firmware download
c = str; c = str;
while(*c) { while(*c) {
uart_write(*c); uart_write(*c);
@ -145,18 +146,22 @@ int serialboot(void)
int goodcrc; int goodcrc;
/* Get one Frame */ /* Get one Frame */
frame.length = uart_read(); frame.payload_length = uart_read();
frame.crc[0] = uart_read(); frame.crc[0] = uart_read();
frame.crc[1] = uart_read(); frame.crc[1] = uart_read();
frame.cmd = 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(); frame.payload[i] = uart_read();
/* Check Frame CRC (if CMD has a CRC) */ /* Check Frame CRC (if CMD has a CRC) */
if (frame.cmd != SFL_CMD_LOAD_NO_CRC) { if (frame.cmd != SFL_CMD_LOAD_NO_CRC) {
actualcrc = ((int)frame.crc[0] << 8)|(int)frame.crc[1]; 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) { if(actualcrc != goodcrc) {
// clear out the RX buffer
while (uart_read_nonblock()) {
uart_read();
}
failed++; failed++;
if(failed == MAX_FAILED) { if(failed == MAX_FAILED) {
printf("Too many consecutive errors, aborting"); printf("Too many consecutive errors, aborting");
@ -179,7 +184,7 @@ int serialboot(void)
failed = 0; failed = 0;
writepointer = (char *) get_uint32(&frame.payload[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]; *(writepointer++) = frame.payload[i];
if (frame.cmd == SFL_CMD_LOAD) if (frame.cmd == SFL_CMD_LOAD)
uart_write(SFL_ACK_SUCCESS); uart_write(SFL_ACK_SUCCESS);
@ -201,7 +206,7 @@ int serialboot(void)
failed = 0; failed = 0;
addr = get_uint32(&frame.payload[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 // erase page at sector boundaries before writing
if ((addr & (SPIFLASH_SECTOR_SIZE - 1)) == 0) { if ((addr & (SPIFLASH_SECTOR_SIZE - 1)) == 0) {
erase_flash_sector(addr); erase_flash_sector(addr);

View File

@ -9,7 +9,7 @@
#define SFL_MAGIC_ACK "z6IHG7cYDID6o\n" #define SFL_MAGIC_ACK "z6IHG7cYDID6o\n"
struct sfl_frame { struct sfl_frame {
unsigned char length; unsigned char payload_length;
unsigned char crc[2]; unsigned char crc[2];
unsigned char cmd; unsigned char cmd;
unsigned char payload[255]; unsigned char payload[255];

View File

@ -52,7 +52,7 @@ sfl_prompt_ack = b"\x06"
sfl_magic_req = b"sL5DdSMmkekro\n" sfl_magic_req = b"sL5DdSMmkekro\n"
sfl_magic_ack = b"z6IHG7cYDID6o\n" sfl_magic_ack = b"z6IHG7cYDID6o\n"
sfl_payload_length = 251 sfl_payload_length = 64#251
# General commands # General commands
sfl_cmd_abort = b"\x00" sfl_cmd_abort = b"\x00"
@ -212,7 +212,7 @@ class LiteXTerm:
100*position//length)) 100*position//length))
sys.stdout.flush() sys.stdout.flush()
frame = SFLFrame() frame = SFLFrame()
frame_data = f.read(min(remaining, sfl_payload_length)) frame_data = f.read(min(remaining, sfl_payload_length-4))
if self.flash: if self.flash:
frame.cmd = sfl_cmd_flash frame.cmd = sfl_cmd_flash
else: else: