X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=src%2Fflash%2Fnor%2Fcore.c;h=9083ed15ef92a95dda02aa4f0d9362922c2b4905;hb=8c730aaee22a505cf66666be3ff28734ac885418;hp=1873deede567e56ef93caf230040cabded7fb40a;hpb=3f18900b19f49a84ba9df56f2e089c255e610011;p=fw%2Fopenocd diff --git a/src/flash/nor/core.c b/src/flash/nor/core.c index 1873deede..9083ed15e 100644 --- a/src/flash/nor/core.c +++ b/src/flash/nor/core.c @@ -29,6 +29,13 @@ #include +/** + * @file + * Upper level of NOR flash framework. + * The lower level interfaces are to drivers. These upper level ones + * primarily support access from Tcl scripts or from GDB. + */ + struct flash_bank *flash_banks; int flash_driver_erase(struct flash_bank *bank, int first, int last) @@ -281,7 +288,8 @@ int default_flash_blank_check(struct flash_bank *bank) } /* erase given flash region, selects proper bank according to target and address */ -static int flash_iterate_address_range(struct target *target, uint32_t addr, uint32_t length, +static int flash_iterate_address_range(struct target *target, + uint32_t addr, uint32_t length, int (*callback)(struct flash_bank *bank, int first, int last)) { struct flash_bank *c; @@ -315,6 +323,7 @@ static int flash_iterate_address_range(struct target *target, uint32_t addr, uin /** @todo: handle erasures that cross into adjacent banks */ addr -= c->base; + last_addr -= c->base; for (i = 0; i < c->num_sectors; i++) { @@ -344,8 +353,8 @@ static int flash_iterate_address_range(struct target *target, uint32_t addr, uin if (first == -1 || last == -1) { 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 + addr), + (unsigned) (last_addr - 1)); return ERROR_FLASH_DST_BREAKS_ALIGNMENT; } @@ -357,7 +366,8 @@ static int flash_iterate_address_range(struct target *target, uint32_t addr, uin return callback(c, first, last); } -int flash_erase_address_range(struct target *target, uint32_t addr, uint32_t length) +int flash_erase_address_range(struct target *target, + uint32_t addr, uint32_t length) { return flash_iterate_address_range(target, addr, length, &flash_driver_erase); @@ -399,7 +409,7 @@ int flash_write_unlock(struct target *target, struct image *image, } /* allocate padding array */ - padding = malloc(image->num_sections * sizeof(padding)); + padding = calloc(image->num_sections, sizeof(*padding)); /* loop until we reach end of the image */ while (section < image->num_sections) @@ -437,9 +447,29 @@ int flash_write_unlock(struct target *target, struct image *image, { if (image->sections[section_last + 1].base_address < (run_address + run_size)) { - LOG_DEBUG("section %d out of order(very slightly surprising, but supported)", section_last + 1); + LOG_DEBUG("section %d out of order " + "(surprising, but supported)", + section_last + 1); + /* REVISIT this can break with autoerase ... + * clobbering data after it's written. + */ break; } + + /* FIXME This needlessly touches sectors BETWEEN the + * sections it's writing. Without auto erase, it just + * writes ones. That WILL INVALIDATE data in cases + * like Stellaris Tempest chips, corrupting internal + * ECC codes; and at least FreeScale suggests issues + * with that approach (in HC11 documentation). + * + * With auto erase enabled, data in those sectors will + * be needlessly destroyed; and some of the limited + * number of flash erase cycles will be wasted... + * + * In both cases, the extra writes slow things down. + */ + /* if we have multiple sections within our image, flash programming could fail due to alignment issues * attempt to rebuild a consecutive buffer for the flash loader */ pad_bytes = (image->sections[section_last + 1].base_address) - (run_address + run_size); @@ -448,7 +478,6 @@ int flash_write_unlock(struct target *target, struct image *image, padding[section_last] = pad_bytes; run_size += image->sections[++section_last].size; run_size += pad_bytes; - padding[section_last] = 0; LOG_INFO("Padding image section %d with %d bytes", section_last-1, pad_bytes); } @@ -456,11 +485,35 @@ int flash_write_unlock(struct target *target, struct image *image, /* fit the run into bank constraints */ if (run_address + run_size - 1 > c->base + c->size - 1) { + /* REVISIT isn't this superfluous, given the while() + * loop conditions above?? + */ LOG_WARNING("writing %d bytes only - as image section is %d bytes and bank is only %d bytes", \ (int)(c->base + c->size - run_address), (int)(run_size), (int)(c->size)); run_size = c->base + c->size - run_address; } + /* If we're applying any sector automagic, then pad this + * (maybe-combined) segment to the end of its last sector. + */ + if (unlock || erase) { + int sector; + uint32_t offset_start = run_address - c->base; + uint32_t offset_end = offset_start + run_size; + uint32_t end = offset_end, delta; + + for (sector = 0; sector < c->num_sectors; sector++) { + end = c->sectors[sector].offset + + c->sectors[sector].size; + if (offset_end <= end) + break; + } + + delta = end - offset_end; + padding[section_last] += delta; + run_size += delta; + } + /* allocate buffer */ buffer = malloc(run_size); buffer_size = 0;