mirror of
https://github.com/marqs85/ossc.git
synced 2026-04-20 13:16:50 +00:00
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:
@@ -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;
|
||||
|
||||
@@ -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_ */
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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_ */
|
||||
|
||||
Reference in New Issue
Block a user