Factor writing to Flash memory and SD->Flash copying into functions

This is in preparation for the feature to import settings from the
SD card.
This commit is contained in:
Ari Sundholm 2017-11-03 17:07:44 +02:00
parent 7914a2ee83
commit a5b329584e
6 changed files with 60 additions and 27 deletions

View File

@ -83,6 +83,32 @@ int write_flash_page(alt_u8 *pagedata, alt_u32 length, alt_u32 pagenum)
return 0;
}
int write_flash(alt_u8 *buf, alt_u32 length, alt_u32 pagenum, alt_u8 *tmpbuf)
{
int retval;
alt_u32 bytes_to_w;
while (length > 0) {
bytes_to_w = (length > PAGESIZE) ? PAGESIZE : length;
// Use a temporary buffer if one was given.
// This is to avoid the original buffer from
// being overwritten by write_flash_page().
if (tmpbuf)
memcpy(tmpbuf, buf, bytes_to_w);
retval = write_flash_page(tmpbuf ? tmpbuf : buf, bytes_to_w, pagenum);
if (retval != 0)
return retval;
buf += bytes_to_w;
length -= bytes_to_w;
++pagenum;
}
return 0;
}
int verify_flash(alt_u32 offset, alt_u32 length, alt_u32 golden_crc, alt_u8 *tmpbuf)
{
alt_u32 crcval=0, i, bytes_to_read;

View File

@ -44,6 +44,8 @@ int read_flash(alt_u32 offset, alt_u32 length, alt_u8 *dstbuf);
int write_flash_page(alt_u8 *pagedata, alt_u32 length, alt_u32 pagenum);
int write_flash(alt_u8 *buf, alt_u32 length, alt_u32 pagenum, alt_u8 *tmpbuf);
int verify_flash(alt_u32 offset, alt_u32 length, alt_u32 golden_crc, alt_u8 *tmpbuf);
#endif /* FLASH_H_ */

View File

@ -19,6 +19,7 @@
#include <io.h>
#include "sdcard.h"
#include "flash.h"
#include "lcd.h"
extern char menu_row1[LCD_ROW_LEN+1], menu_row2[LCD_ROW_LEN+1];
@ -36,3 +37,28 @@ int check_sdcard(alt_u8 *databuf)
return SD_Read(&sdcard_dev, databuf, 0, 0, 512);
}
int copy_sd_to_flash(alt_u32 sd_blknum, alt_u32 flash_pagenum, alt_u32 length, alt_u8 *tmpbuf)
{
int retval;
alt_u32 bytes_to_rw;
while (length > 0) {
bytes_to_rw = (length < SD_BLK_SIZE) ? length : SD_BLK_SIZE;
retval = SD_Read(&sdcard_dev, tmpbuf, sd_blknum, 0, bytes_to_rw);
if (retval != 0) {
printf("Failed to read SD card\n");
return -retval;
}
retval = write_flash(tmpbuf, bytes_to_rw, flash_pagenum, NULL);
if (retval != 0)
return retval;
++sd_blknum;
flash_pagenum += bytes_to_rw/PAGESIZE;
length -= bytes_to_rw;
}
return 0;
}

View File

@ -25,5 +25,6 @@
#include "sd_io.h"
int check_sdcard(alt_u8 *databuf);
int copy_sd_to_flash(alt_u32 sd_offset, alt_u32 flash_offset, alt_u32 length, alt_u8 *tmpbuf);
#endif /* SDCARD_H_ */

View File

@ -163,24 +163,9 @@ update_init:
strncpy(menu_row2, "please wait...", LCD_ROW_LEN+1);
lcd_write_menu();
for (i=0; i<fw_header.data_len; i=i+SD_BLK_SIZE) {
bytes_to_rw = ((fw_header.data_len-i < SD_BLK_SIZE) ? (fw_header.data_len-i) : SD_BLK_SIZE);
retval = SD_Read(&sdcard_dev, databuf, (512+i)/SD_BLK_SIZE, 0, bytes_to_rw);
if (retval != 0) {
retval = -retval; //flag any SD errors critical to trigger update retry
goto failure;
}
retval = write_flash_page(databuf, ((bytes_to_rw < PAGESIZE) ? bytes_to_rw : PAGESIZE), (i/PAGESIZE));
if (retval != 0)
goto failure;
//TODO: support multiple page sizes
if (bytes_to_rw > PAGESIZE) {
retval = write_flash_page(databuf+PAGESIZE, (bytes_to_rw-PAGESIZE), (i/PAGESIZE)+1);
if (retval != 0)
goto failure;
}
}
retval = copy_sd_to_flash(512/SD_BLK_SIZE, 0, fw_header.data_len, databuf);
if (retval != 0)
goto failure;
strncpy(menu_row1, "Verifying flash", LCD_ROW_LEN+1);
strncpy(menu_row2, "please wait...", LCD_ROW_LEN+1);

View File

@ -87,15 +87,8 @@ int write_userdata(alt_u8 entry)
write_flash_page(databuf, PAGESIZE, ((USERDATA_OFFSET+entry*SECTORSIZE)/PAGESIZE));
// then write the rest
pageno = 1;
while (vm_to_write > 0) {
bytes_to_w = (vm_to_write > PAGESIZE) ? PAGESIZE : vm_to_write;
memcpy(databuf, (char*)video_modes+srcoffset, bytes_to_w);
write_flash_page(databuf, bytes_to_w, ((USERDATA_OFFSET+entry*SECTORSIZE)/PAGESIZE) + pageno);
srcoffset += bytes_to_w;
vm_to_write -= bytes_to_w;
++pageno;
}
if (vm_to_write > 0)
write_flash((alt_u8*)video_modes+srcoffset, vm_to_write, ((USERDATA_OFFSET+entry*SECTORSIZE)/PAGESIZE) + 1, databuf);
printf("Profile %u data written (%u bytes)\n", entry, sizeof(avconfig_t)+VIDEO_MODES_SIZE);
break;