lpc_common: restore RAM and registers after IAP call

Restore the RAM and registers which are clobbered by an LPC IAP call.
This does not restore any additional RAM which might be clobbered
by a *particular* IAP call. (For example, flash programming always
clobbers the last page of RAM.)
This commit is contained in:
Alexander Zhang 2019-05-24 10:02:29 -04:00 committed by David Lawrence
parent d3979a57b6
commit 880613d641
1 changed files with 12 additions and 0 deletions

View File

@ -66,6 +66,14 @@ enum iap_status lpc_iap_call(struct lpc_flash *f, void *result, enum iap_cmd cmd
if (f->wdt_kick)
f->wdt_kick(t);
/* save IAP RAM to restore after IAP call */
struct flash_param backup_param;
target_mem_read(t, &backup_param, f->iap_ram, sizeof(backup_param));
/* save registers to restore after IAP call */
uint32_t backup_regs[t->regs_size / sizeof(uint32_t)];
target_regs_read(t, backup_regs);
/* fill out the remainder of the parameters */
va_list ap;
va_start(ap, cmd);
@ -93,6 +101,10 @@ enum iap_status lpc_iap_call(struct lpc_flash *f, void *result, enum iap_cmd cmd
/* copy back just the parameters structure */
target_mem_read(t, &param, f->iap_ram, sizeof(param));
/* restore the original data in RAM and registers */
target_mem_write(t, f->iap_ram, &backup_param, sizeof(param));
target_regs_write(t, backup_regs);
/* if the user expected a result, set the result (16 bytes). */
if (result != NULL)
memcpy(result, param.result, sizeof(param.result));