summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Brownell <dbrownell@users.sourceforge.net>2010-03-03 21:01:16 -0800
committerDavid Brownell <dbrownell@users.sourceforge.net>2010-03-03 21:01:16 -0800
commit5e78ddcea0d8303c316f687c05dfa78af27109d8 (patch)
tree563777a521940811f1d1ac8d8113aa45668bf895
parent5fdf9535cef7e43f6e99081b6d1f6bd682184803 (diff)
downloadopenocd+libswd-5e78ddcea0d8303c316f687c05dfa78af27109d8.tar.gz
openocd+libswd-5e78ddcea0d8303c316f687c05dfa78af27109d8.tar.bz2
openocd+libswd-5e78ddcea0d8303c316f687c05dfa78af27109d8.tar.xz
openocd+libswd-5e78ddcea0d8303c316f687c05dfa78af27109d8.zip
NOR: trim range in flash_driver_protect()
When the beginning or end of the specified range of sectors already has the requested protection status, don't ask the flash driver to change those sectors. This will among other things turn command sequences like this into the NOPs one would expect: flash protect_check 0 flash info 0 ... reports everything as unprotected ... flash protect 0 0 1 off That speeds things up (by whatever work was just avoided). Also, with Stellaris (which can't unprotect flash at page level) this can eliminate some undesirable/false error reports. (And finishes fixing a bug currently listed in our bug database...) Signed-off-by: David Brownell <dbrownell@users.sourceforge.net>
-rw-r--r--src/flash/nor/core.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/flash/nor/core.c b/src/flash/nor/core.c
index fc020a8a..767006d2 100644
--- a/src/flash/nor/core.c
+++ b/src/flash/nor/core.c
@@ -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)