X-Git-Url: https://review.openocd.org/gitweb?p=openocd.git;a=blobdiff_plain;f=src%2Fflash%2Fnor%2Fcore.c;h=18012c650b55d0f03603e24f9094bbe6dd12b6a8;hp=277da38c9fdd9ebcc6cb58ff7231ec988e844f18;hb=300f0f53c54fab5dd994a8bc42edbdc6115ef7b9;hpb=cdcb9b0885cdb2ca2a212536ab68acc2e9bc7fad diff --git a/src/flash/nor/core.c b/src/flash/nor/core.c index 277da38c9f..18012c650b 100644 --- a/src/flash/nor/core.c +++ b/src/flash/nor/core.c @@ -36,7 +36,7 @@ * primarily support access from Tcl scripts or from GDB. */ -struct flash_bank *flash_banks; +static struct flash_bank *flash_banks; int flash_driver_erase(struct flash_bank *bank, int first, int last) { @@ -54,6 +54,63 @@ int flash_driver_erase(struct flash_bank *bank, int first, int last) int flash_driver_protect(struct flash_bank *bank, int set, int first, int last) { int retval; + bool updated = false; + + /* NOTE: "first == last" means protect just that sector */ + + /* callers may not supply illegal parameters ... */ + if (first < 0 || first > last || last >= bank->num_sectors) + return ERROR_FAIL; + + /* force "set" to 0/1 */ + set = !!set; + + /* + * Filter out what trivial nonsense we can, so drivers don't have to. + * + * Don't tell drivers to change to the current state... it's needless, + * and reducing the amount of work to be done (potentially to nothing) + * speeds at least some things up. + */ +scan: + for (int i = first; i <= last; i++) { + struct flash_sector *sector = bank->sectors + i; + + /* Only filter requests to protect the already-protected, or + * to unprotect the already-unprotected. Changing from the + * unknown state (-1) to a known one is unwise but allowed; + * protection status is best checked first. + */ + if (sector->is_protected != set) + continue; + + /* Shrink this range of sectors from the start; don't overrun + * the end. Also shrink from the end; don't overun the start. + * + * REVISIT we could handle discontiguous regions by issuing + * more than one driver request. How much would that matter? + */ + if (i == first) { + updated = true; + first++; + } else if (i == last) { + updated = true; + last--; + } + } + + /* updating the range affects the tests in the scan loop above; so + * re-scan, to make sure we didn't miss anything. + */ + if (updated) { + updated = false; + goto scan; + } + + /* Single sector, already protected? Nothing to do! */ + if (first > last) + return ERROR_OK; + retval = bank->driver->protect(bank, set, first, last); if (retval != ERROR_OK) @@ -408,7 +465,7 @@ static int flash_iterate_address_range(struct target *target, LOG_ERROR("address range 0x%8.8x .. 0x%8.8x " "is not sector-aligned", (unsigned) (c->base + addr), - (unsigned) (last_addr - 1)); + (unsigned) (c->base + last_addr - 1)); return ERROR_FLASH_DST_BREAKS_ALIGNMENT; } @@ -657,3 +714,34 @@ int flash_write(struct target *target, struct image *image, { return flash_write_unlock(target, image, written, erase, false); } + +/** + * Invalidates cached flash state which a target can change as it runs. + * + * @param target The target being resumed + * + * OpenOCD caches some flash state for brief periods. For example, a sector + * that is protected must be unprotected before OpenOCD tries to write it, + * Also, a sector that's not erased must be erased before it's written. + * + * As a rule, OpenOCD and target firmware can both modify the flash, so when + * a target starts running, OpenOCD needs to invalidate its cached state. + */ +void nor_resume(struct target *target) +{ + struct flash_bank *bank; + + for (bank = flash_banks; bank; bank = bank->next) { + int i; + + if (bank->target != target) + continue; + + for (i = 0; i < bank->num_sectors; i++) { + struct flash_sector *sector = bank->sectors + i; + + sector->is_erased = -1; + sector->is_protected = -1; + } + } +}