forked from PAWPAW-Mirror/lib_xua
DFU tidy
This commit is contained in:
@@ -9,16 +9,11 @@
|
||||
#ifdef ARCH_G
|
||||
#define FLASH_MAX_UPGRADE_SIZE 128 * 1024 // 128K default
|
||||
#else
|
||||
//#define FLASH_MAX_UPGRADE_SIZE 64 * 1024 // 64K default
|
||||
#define FLASH_MAX_UPGRADE_SIZE 128 * 1024 // 64K default
|
||||
#define FLASH_MAX_UPGRADE_SIZE 128 * 1024 // 128K default
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if 1
|
||||
#define FLASH_ERROR() do { printstr("Error: line: "); printintln(__LINE__); __builtin_trap(); } while(0)
|
||||
#else
|
||||
#define FLASH_ERROR() do {} while(0)
|
||||
#endif
|
||||
|
||||
static int flash_device_open = 0;
|
||||
static fl_BootImageInfo factory_image;
|
||||
@@ -50,7 +45,6 @@ void DFUCustomFlashDisable()
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
int flash_cmd_init(void)
|
||||
{
|
||||
fl_BootImageInfo image;
|
||||
@@ -93,130 +87,143 @@ int flash_cmd_deinit(void)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int flash_cmd_read_page(unsigned char *data) {
|
||||
if (!upgrade_image_valid) {
|
||||
*(unsigned int *)data = 1;
|
||||
int flash_cmd_read_page(unsigned char *data)
|
||||
{
|
||||
if (!upgrade_image_valid)
|
||||
{
|
||||
*(unsigned int *)data = 1;
|
||||
return 4;
|
||||
}
|
||||
|
||||
if (*(unsigned int *)data == 0)
|
||||
{
|
||||
fl_startImageRead(&upgrade_image);
|
||||
}
|
||||
|
||||
current_flash_subpage_index = 0;
|
||||
|
||||
if (fl_readImageRead(current_flash_page_data) == 0)
|
||||
{
|
||||
*(unsigned int *)data = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
*(unsigned int *)data = 1;
|
||||
}
|
||||
return 4;
|
||||
}
|
||||
if (*(unsigned int *)data == 0) {
|
||||
fl_startImageRead(&upgrade_image);
|
||||
}
|
||||
current_flash_subpage_index = 0;
|
||||
if (fl_readImageRead(current_flash_page_data) == 0) {
|
||||
*(unsigned int *)data = 0;
|
||||
} else {
|
||||
*(unsigned int *)data = 1;
|
||||
}
|
||||
return 4;
|
||||
}
|
||||
|
||||
int flash_cmd_read_page_data(unsigned char *data) {
|
||||
unsigned char *page_data_ptr = ¤t_flash_page_data[current_flash_subpage_index * 64];
|
||||
memcpy(data, page_data_ptr, 64);
|
||||
int flash_cmd_read_page_data(unsigned char *data)
|
||||
{
|
||||
unsigned char *page_data_ptr = ¤t_flash_page_data[current_flash_subpage_index * 64];
|
||||
memcpy(data, page_data_ptr, 64);
|
||||
|
||||
current_flash_subpage_index++;
|
||||
current_flash_subpage_index++;
|
||||
|
||||
return 64;
|
||||
return 64;
|
||||
}
|
||||
|
||||
static void begin_write()
|
||||
{
|
||||
int result;
|
||||
// TODO this will take a long time. To minimise the amount of time spent
|
||||
// paused on this operation it would be preferable to move to this to a
|
||||
// seperate command, e.g. start_write.
|
||||
do {
|
||||
result = fl_startImageAdd(&factory_image, FLASH_MAX_UPGRADE_SIZE, 0);
|
||||
} while (result > 0);
|
||||
if (result < 0)
|
||||
FLASH_ERROR();
|
||||
int result;
|
||||
// TODO this will take a long time. To minimise the amount of time spent
|
||||
// paused on this operation it would be preferable to move to this to a
|
||||
// seperate command, e.g. start_write.
|
||||
do
|
||||
{
|
||||
result = fl_startImageAdd(&factory_image, FLASH_MAX_UPGRADE_SIZE, 0);
|
||||
} while (result > 0);
|
||||
|
||||
if (result < 0)
|
||||
FLASH_ERROR();
|
||||
}
|
||||
|
||||
static int pages_written = 0;
|
||||
|
||||
int flash_cmd_write_page(unsigned char *data) {
|
||||
unsigned int flag = *(unsigned int *)data;
|
||||
//printstr("flash_cmd_write_page(");
|
||||
//printint(flag);
|
||||
//printstr(")\n");
|
||||
int flash_cmd_write_page(unsigned char *data)
|
||||
{
|
||||
unsigned int flag = *(unsigned int *)data;
|
||||
|
||||
if (upgrade_image_valid)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
switch (flag)
|
||||
{
|
||||
case 0:
|
||||
// First page.
|
||||
begin_write();
|
||||
pages_written = 0;
|
||||
// fallthrough
|
||||
case 1:
|
||||
// Do nothing.
|
||||
break;
|
||||
case 2:
|
||||
// Termination.
|
||||
if (fl_endWriteImage() != 0)
|
||||
FLASH_ERROR();
|
||||
|
||||
// Sanity check
|
||||
fl_BootImageInfo image = factory_image;
|
||||
if (fl_getNextBootImage(&image) != 0)
|
||||
FLASH_ERROR();
|
||||
break;
|
||||
}
|
||||
current_flash_subpage_index = 0;
|
||||
|
||||
if (upgrade_image_valid) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
switch (flag) {
|
||||
case 0:
|
||||
// First page.
|
||||
begin_write();
|
||||
pages_written = 0;
|
||||
// fallthrough
|
||||
case 1:
|
||||
// Do nothing.
|
||||
break;
|
||||
case 2:
|
||||
// Termination.
|
||||
if (fl_endWriteImage() != 0)
|
||||
FLASH_ERROR();
|
||||
// Sanity check
|
||||
fl_BootImageInfo image = factory_image;
|
||||
if (fl_getNextBootImage(&image) != 0)
|
||||
FLASH_ERROR();
|
||||
break;
|
||||
}
|
||||
current_flash_subpage_index = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int isAllOnes(unsigned char page[256])
|
||||
{
|
||||
unsigned i;
|
||||
for (i = 0; i < 256; i++) {
|
||||
if (page[i] != 0xff)
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
unsigned i;
|
||||
for (i = 0; i < 256; i++)
|
||||
{
|
||||
if (page[i] != 0xff)
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int flash_cmd_write_page_data(unsigned char *data) {
|
||||
unsigned char *page_data_ptr = ¤t_flash_page_data[current_flash_subpage_index * 64];
|
||||
//printstr("flash_cmd_write_page_data()\n");
|
||||
int flash_cmd_write_page_data(unsigned char *data)
|
||||
{
|
||||
unsigned char *page_data_ptr = ¤t_flash_page_data[current_flash_subpage_index * 64];
|
||||
|
||||
if (upgrade_image_valid)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (current_flash_subpage_index >= 4)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
memcpy(page_data_ptr, data, 64);
|
||||
|
||||
current_flash_subpage_index++;
|
||||
|
||||
if (current_flash_subpage_index == 4)
|
||||
{
|
||||
if (isAllOnes(data))
|
||||
FLASH_ERROR();
|
||||
if (fl_writeImagePage(current_flash_page_data) != 0)
|
||||
FLASH_ERROR();
|
||||
pages_written++;
|
||||
}
|
||||
|
||||
if (upgrade_image_valid) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (current_flash_subpage_index >= 4) {
|
||||
int flash_cmd_erase_all(void)
|
||||
{
|
||||
if (upgrade_image_valid)
|
||||
{
|
||||
if (fl_deleteImage(&upgrade_image) != 0)
|
||||
FLASH_ERROR();
|
||||
upgrade_image_valid = 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
memcpy(page_data_ptr, data, 64);
|
||||
|
||||
current_flash_subpage_index++;
|
||||
|
||||
if (current_flash_subpage_index == 4) {
|
||||
if (isAllOnes(data))
|
||||
FLASH_ERROR();
|
||||
if (fl_writeImagePage(current_flash_page_data) != 0)
|
||||
FLASH_ERROR();
|
||||
pages_written++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int flash_cmd_erase_all(void) {
|
||||
if (upgrade_image_valid) {
|
||||
if (fl_deleteImage(&upgrade_image) != 0)
|
||||
FLASH_ERROR();
|
||||
upgrade_image_valid = 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int flash_cmd_reboot(void) {
|
||||
//unsigned int pllVal;
|
||||
//read_sswitch_reg(0, 6, &pllVal);
|
||||
//write_sswitch_reg(0, 6, pllVal);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user