Skip to content

Commit 270b2f1

Browse files
authored
steami: Fix READ_SECTOR command argument length and address. (#1)
* steami: Fix READ_SECTOR command argument length and address. * fix: address PR review comments - Use STEAMI_FLASH_SECTOR constant instead of hardcoded 256 for TX data length - Use STEAMI_FLASH_FILE_ADDR base offset in steami_flash_read_sector for consistency * steami: Fix WRITE_DATA page boundary corruption. * fix: restrict READ_SECTOR to file area only Limit sector_number bound check to STEAMI_FLASH_FILE_SIZE / STEAMI_FLASH_SECTOR (32752) instead of STEAMI_FLASH_NB_SECTOR (32768) to prevent reading into the reserved filename sector.
1 parent 9513ae0 commit 270b2f1

2 files changed

Lines changed: 19 additions & 9 deletions

File tree

source/board/steami/steami32.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ static char buffer_filename[STEAMI_FLASH_NAME_SIZE] = {'\0'};
121121
static uint8_t buffer_sector[STEAMI_FLASH_SECTOR] = {0};
122122
static uint8_t task_rx[STEAMI_I2C_RX_BUFFER_SIZE] = {'\0'};
123123
static uint16_t task_rx_len = 0;
124+
static uint16_t task_rx_offset = 0;
124125
static uint8_t status_error = 0;
125126
static bool is_user_delete_file_flash = false;
126127

@@ -188,6 +189,7 @@ static void on_I2C_receive_command(steami_i2c_command cmd, uint8_t* rx, uint16_t
188189

189190
current_task = TASK_WRITE_DATA_COUNT;
190191
task_rx_len = rx[0];
192+
task_rx_offset = 0;
191193
memcpy(task_rx, rx + 1, task_rx_len);
192194
break;
193195

@@ -349,34 +351,42 @@ void process_task()
349351

350352
if( task_rx_len == 0){
351353
steami_uart_write_string("No more data in rx_len to write to flash.\n");
354+
task_rx_offset = 0;
352355
current_task = TASK_NONE;
353356
break;
354357
}
355358

356-
uint32_t bytes_wrote = 0;
357-
int16_t bytes_sent = steami_flash_append_file(task_rx + bytes_wrote, task_rx_len);
359+
int16_t bytes_sent = steami_flash_append_file(task_rx + task_rx_offset, task_rx_len);
358360

359361
if( bytes_sent == -1 ){
360362
error_status_set_last_command_fail(&status_error);
361363
steami_uart_write_string("ERROR Unable to write data to flash\n");
364+
task_rx_offset = 0;
362365
current_task = TASK_NONE;
363366
}
364367
else{
365368
task_rx_len -= bytes_sent;
366-
bytes_wrote += bytes_sent;
369+
task_rx_offset += bytes_sent;
367370
current_task = TASK_WRITE_DATA_WRITE;
368371
}
369372
break;
370373
}
371374

372375
case TASK_READ_SECTOR:{
373376

374-
if( task_rx_len == 1 && steami_flash_read_sector(task_rx[0], buffer_sector) ){
375-
steami_i2c_set_tx_data(buffer_sector, 256);
377+
if( task_rx_len == 2 ){
378+
uint16_t sector = ((uint16_t)task_rx[0] << 8) | task_rx[1];
379+
if( steami_flash_read_sector(sector, buffer_sector) ){
380+
steami_i2c_set_tx_data(buffer_sector, STEAMI_FLASH_SECTOR);
381+
}
382+
else{
383+
error_status_set_last_command_fail(&status_error);
384+
steami_uart_write_string("ERROR Unable to read sector (bad sector parameter ?)\n");
385+
}
376386
}
377387
else{
378-
error_status_set_last_command_fail(&status_error);
379-
steami_uart_write_string("ERROR Unable to read sector (bad sector parameter ?)\n");
388+
error_status_bad_parameter(&status_error);
389+
steami_uart_write_string("ERROR READ_SECTOR expects 2 bytes (sector number)\n");
380390
}
381391

382392
current_task = TASK_WAIT_FLASH_BUSY;

source/board/steami/steami_flash.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,11 @@ int16_t steami_flash_append_file(uint8_t* data, uint16_t data_len){
190190

191191
bool steami_flash_read_sector(uint32_t sector_number, uint8_t* data){
192192

193-
if( sector_number >= STEAMI_FLASH_NB_SECTOR ){
193+
if( sector_number >= STEAMI_FLASH_FILE_SIZE / STEAMI_FLASH_SECTOR ){
194194
return false;
195195
}
196196

197-
return w25q64_read_data(data, 0, 256);
197+
return w25q64_read_data(data, STEAMI_FLASH_FILE_ADDR + (sector_number * STEAMI_FLASH_SECTOR), STEAMI_FLASH_SECTOR);
198198
}
199199

200200
uint16_t steami_flash_read_file(uint8_t* data, uint16_t data_len, uint32_t offset){

0 commit comments

Comments
 (0)