You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

798 lines
21 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath <Dominic.Rath@gmx.de> *
  3. * Copyright (C) 2007-2010 Øyvind Harboe <oyvind.harboe@zylin.com> *
  4. * Copyright (C) 2008 by Spencer Oliver <spen@spen-soft.co.uk> *
  5. * Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net> *
  6. * Copyright (C) 2010 by Antonio Borneo <borneo.antonio@gmail.com> *
  7. * *
  8. * This program is free software; you can redistribute it and/or modify *
  9. * it under the terms of the GNU General Public License as published by *
  10. * the Free Software Foundation; either version 2 of the License, or *
  11. * (at your option) any later version. *
  12. * *
  13. * This program is distributed in the hope that it will be useful, *
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  16. * GNU General Public License for more details. *
  17. * *
  18. * You should have received a copy of the GNU General Public License *
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  20. ***************************************************************************/
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <flash/common.h>
  25. #include <flash/nor/core.h>
  26. #include <flash/nor/imp.h>
  27. #include <target/image.h>
  28. /**
  29. * @file
  30. * Upper level of NOR flash framework.
  31. * The lower level interfaces are to drivers. These upper level ones
  32. * primarily support access from Tcl scripts or from GDB.
  33. */
  34. static struct flash_bank *flash_banks;
  35. int flash_driver_erase(struct flash_bank *bank, int first, int last)
  36. {
  37. int retval;
  38. retval = bank->driver->erase(bank, first, last);
  39. if (retval != ERROR_OK)
  40. LOG_ERROR("failed erasing sectors %d to %d", first, last);
  41. return retval;
  42. }
  43. int flash_driver_protect(struct flash_bank *bank, int set, int first, int last)
  44. {
  45. int retval;
  46. /* callers may not supply illegal parameters ... */
  47. if (first < 0 || first > last || last >= bank->num_sectors) {
  48. LOG_ERROR("illegal sector range");
  49. return ERROR_FAIL;
  50. }
  51. /* force "set" to 0/1 */
  52. set = !!set;
  53. /* DANGER!
  54. *
  55. * We must not use any cached information about protection state!!!!
  56. *
  57. * There are a million things that could change the protect state:
  58. *
  59. * the target could have reset, power cycled, been hot plugged,
  60. * the application could have run, etc.
  61. *
  62. * Drivers only receive valid sector range.
  63. */
  64. retval = bank->driver->protect(bank, set, first, last);
  65. if (retval != ERROR_OK)
  66. LOG_ERROR("failed setting protection for areas %d to %d", first, last);
  67. return retval;
  68. }
  69. int flash_driver_write(struct flash_bank *bank,
  70. uint8_t *buffer, uint32_t offset, uint32_t count)
  71. {
  72. int retval;
  73. retval = bank->driver->write(bank, buffer, offset, count);
  74. if (retval != ERROR_OK) {
  75. LOG_ERROR(
  76. "error writing to flash at address 0x%08" PRIx32 " at offset 0x%8.8" PRIx32,
  77. bank->base,
  78. offset);
  79. }
  80. return retval;
  81. }
  82. int flash_driver_read(struct flash_bank *bank,
  83. uint8_t *buffer, uint32_t offset, uint32_t count)
  84. {
  85. int retval;
  86. LOG_DEBUG("call flash_driver_read()");
  87. retval = bank->driver->read(bank, buffer, offset, count);
  88. if (retval != ERROR_OK) {
  89. LOG_ERROR(
  90. "error reading to flash at address 0x%08" PRIx32 " at offset 0x%8.8" PRIx32,
  91. bank->base,
  92. offset);
  93. }
  94. return retval;
  95. }
  96. int default_flash_read(struct flash_bank *bank,
  97. uint8_t *buffer, uint32_t offset, uint32_t count)
  98. {
  99. return target_read_buffer(bank->target, offset + bank->base, count, buffer);
  100. }
  101. void flash_bank_add(struct flash_bank *bank)
  102. {
  103. /* put flash bank in linked list */
  104. unsigned bank_num = 0;
  105. if (flash_banks) {
  106. /* find last flash bank */
  107. struct flash_bank *p = flash_banks;
  108. while (NULL != p->next) {
  109. bank_num += 1;
  110. p = p->next;
  111. }
  112. p->next = bank;
  113. bank_num += 1;
  114. } else
  115. flash_banks = bank;
  116. bank->bank_number = bank_num;
  117. }
  118. struct flash_bank *flash_bank_list(void)
  119. {
  120. return flash_banks;
  121. }
  122. struct flash_bank *get_flash_bank_by_num_noprobe(int num)
  123. {
  124. struct flash_bank *p;
  125. int i = 0;
  126. for (p = flash_banks; p; p = p->next) {
  127. if (i++ == num)
  128. return p;
  129. }
  130. LOG_ERROR("flash bank %d does not exist", num);
  131. return NULL;
  132. }
  133. int flash_get_bank_count(void)
  134. {
  135. struct flash_bank *p;
  136. int i = 0;
  137. for (p = flash_banks; p; p = p->next)
  138. i++;
  139. return i;
  140. }
  141. struct flash_bank *get_flash_bank_by_name_noprobe(const char *name)
  142. {
  143. unsigned requested = get_flash_name_index(name);
  144. unsigned found = 0;
  145. struct flash_bank *bank;
  146. for (bank = flash_banks; NULL != bank; bank = bank->next) {
  147. if (strcmp(bank->name, name) == 0)
  148. return bank;
  149. if (!flash_driver_name_matches(bank->driver->name, name))
  150. continue;
  151. if (++found < requested)
  152. continue;
  153. return bank;
  154. }
  155. return NULL;
  156. }
  157. int get_flash_bank_by_name(const char *name, struct flash_bank **bank_result)
  158. {
  159. struct flash_bank *bank;
  160. int retval;
  161. bank = get_flash_bank_by_name_noprobe(name);
  162. if (bank != NULL) {
  163. retval = bank->driver->auto_probe(bank);
  164. if (retval != ERROR_OK) {
  165. LOG_ERROR("auto_probe failed");
  166. return retval;
  167. }
  168. }
  169. *bank_result = bank;
  170. return ERROR_OK;
  171. }
  172. int get_flash_bank_by_num(int num, struct flash_bank **bank)
  173. {
  174. struct flash_bank *p = get_flash_bank_by_num_noprobe(num);
  175. int retval;
  176. if (p == NULL)
  177. return ERROR_FAIL;
  178. retval = p->driver->auto_probe(p);
  179. if (retval != ERROR_OK) {
  180. LOG_ERROR("auto_probe failed");
  181. return retval;
  182. }
  183. *bank = p;
  184. return ERROR_OK;
  185. }
  186. /* lookup flash bank by address, bank not found is success, but
  187. * result_bank is set to NULL. */
  188. int get_flash_bank_by_addr(struct target *target,
  189. uint32_t addr,
  190. bool check,
  191. struct flash_bank **result_bank)
  192. {
  193. struct flash_bank *c;
  194. /* cycle through bank list */
  195. for (c = flash_banks; c; c = c->next) {
  196. if (c->target != target)
  197. continue;
  198. int retval;
  199. retval = c->driver->auto_probe(c);
  200. if (retval != ERROR_OK) {
  201. LOG_ERROR("auto_probe failed");
  202. return retval;
  203. }
  204. /* check whether address belongs to this flash bank */
  205. if ((addr >= c->base) && (addr <= c->base + (c->size - 1))) {
  206. *result_bank = c;
  207. return ERROR_OK;
  208. }
  209. }
  210. *result_bank = NULL;
  211. if (check) {
  212. LOG_ERROR("No flash at address 0x%08" PRIx32, addr);
  213. return ERROR_FAIL;
  214. }
  215. return ERROR_OK;
  216. }
  217. static int default_flash_mem_blank_check(struct flash_bank *bank)
  218. {
  219. struct target *target = bank->target;
  220. const int buffer_size = 1024;
  221. int i;
  222. uint32_t nBytes;
  223. int retval = ERROR_OK;
  224. if (bank->target->state != TARGET_HALTED) {
  225. LOG_ERROR("Target not halted");
  226. return ERROR_TARGET_NOT_HALTED;
  227. }
  228. uint8_t *buffer = malloc(buffer_size);
  229. for (i = 0; i < bank->num_sectors; i++) {
  230. uint32_t j;
  231. bank->sectors[i].is_erased = 1;
  232. for (j = 0; j < bank->sectors[i].size; j += buffer_size) {
  233. uint32_t chunk;
  234. chunk = buffer_size;
  235. if (chunk > (j - bank->sectors[i].size))
  236. chunk = (j - bank->sectors[i].size);
  237. retval = target_read_memory(target,
  238. bank->base + bank->sectors[i].offset + j,
  239. 4,
  240. chunk/4,
  241. buffer);
  242. if (retval != ERROR_OK)
  243. goto done;
  244. for (nBytes = 0; nBytes < chunk; nBytes++) {
  245. if (buffer[nBytes] != 0xFF) {
  246. bank->sectors[i].is_erased = 0;
  247. break;
  248. }
  249. }
  250. }
  251. }
  252. done:
  253. free(buffer);
  254. return retval;
  255. }
  256. int default_flash_blank_check(struct flash_bank *bank)
  257. {
  258. struct target *target = bank->target;
  259. int i;
  260. int retval;
  261. int fast_check = 0;
  262. uint32_t blank;
  263. if (bank->target->state != TARGET_HALTED) {
  264. LOG_ERROR("Target not halted");
  265. return ERROR_TARGET_NOT_HALTED;
  266. }
  267. for (i = 0; i < bank->num_sectors; i++) {
  268. uint32_t address = bank->base + bank->sectors[i].offset;
  269. uint32_t size = bank->sectors[i].size;
  270. retval = target_blank_check_memory(target, address, size, &blank);
  271. if (retval != ERROR_OK) {
  272. fast_check = 0;
  273. break;
  274. }
  275. if (blank == 0xFF)
  276. bank->sectors[i].is_erased = 1;
  277. else
  278. bank->sectors[i].is_erased = 0;
  279. fast_check = 1;
  280. }
  281. if (!fast_check) {
  282. LOG_USER("Running slow fallback erase check - add working memory");
  283. return default_flash_mem_blank_check(bank);
  284. }
  285. return ERROR_OK;
  286. }
  287. /* Manipulate given flash region, selecting the bank according to target
  288. * and address. Maps an address range to a set of sectors, and issues
  289. * the callback() on that set ... e.g. to erase or unprotect its members.
  290. *
  291. * Parameter iterate_protect_blocks switches iteration of protect block
  292. * instead of erase sectors. If there is no protect blocks array, sectors
  293. * are used in iteration, so compatibility for old flash drivers is retained.
  294. *
  295. * The "pad_reason" parameter is a kind of boolean: when it's NULL, the
  296. * range must fit those sectors exactly. This is clearly safe; it can't
  297. * erase data which the caller said to leave alone, for example. If it's
  298. * non-NULL, rather than failing, extra data in the first and/or last
  299. * sectors will be added to the range, and that reason string is used when
  300. * warning about those additions.
  301. */
  302. static int flash_iterate_address_range_inner(struct target *target,
  303. char *pad_reason, uint32_t addr, uint32_t length,
  304. bool iterate_protect_blocks,
  305. int (*callback)(struct flash_bank *bank, int first, int last))
  306. {
  307. struct flash_bank *c;
  308. struct flash_sector *block_array;
  309. uint32_t last_addr = addr + length; /* first address AFTER end */
  310. int first = -1;
  311. int last = -1;
  312. int i;
  313. int num_blocks;
  314. int retval = get_flash_bank_by_addr(target, addr, true, &c);
  315. if (retval != ERROR_OK)
  316. return retval;
  317. if (c->size == 0 || c->num_sectors == 0) {
  318. LOG_ERROR("Bank is invalid");
  319. return ERROR_FLASH_BANK_INVALID;
  320. }
  321. if (length == 0) {
  322. /* special case, erase whole bank when length is zero */
  323. if (addr != c->base) {
  324. LOG_ERROR("Whole bank access must start at beginning of bank.");
  325. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  326. }
  327. return callback(c, 0, c->num_sectors - 1);
  328. }
  329. /* check whether it all fits in this bank */
  330. if (addr + length - 1 > c->base + c->size - 1) {
  331. LOG_ERROR("Flash access does not fit into bank.");
  332. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  333. }
  334. addr -= c->base;
  335. last_addr -= c->base;
  336. if (iterate_protect_blocks && c->prot_blocks && c->num_prot_blocks) {
  337. block_array = c->prot_blocks;
  338. num_blocks = c->num_prot_blocks;
  339. } else {
  340. block_array = c->sectors;
  341. num_blocks = c->num_sectors;
  342. iterate_protect_blocks = false;
  343. }
  344. for (i = 0; i < num_blocks; i++) {
  345. struct flash_sector *f = &block_array[i];
  346. uint32_t end = f->offset + f->size;
  347. /* start only on a sector boundary */
  348. if (first < 0) {
  349. /* scanned past the first sector? */
  350. if (addr < f->offset)
  351. break;
  352. /* is this the first sector? */
  353. if (addr == f->offset)
  354. first = i;
  355. /* Does this need head-padding? If so, pad and warn;
  356. * or else force an error.
  357. *
  358. * Such padding can make trouble, since *WE* can't
  359. * ever know if that data was in use. The warning
  360. * should help users sort out messes later.
  361. */
  362. else if (addr < end && pad_reason) {
  363. /* FIXME say how many bytes (e.g. 80 KB) */
  364. LOG_WARNING("Adding extra %s range, "
  365. "%#8.8x to %#8.8x",
  366. pad_reason,
  367. (unsigned) f->offset,
  368. (unsigned) addr - 1);
  369. first = i;
  370. } else
  371. continue;
  372. }
  373. /* is this (also?) the last sector? */
  374. if (last_addr == end) {
  375. last = i;
  376. break;
  377. }
  378. /* Does this need tail-padding? If so, pad and warn;
  379. * or else force an error.
  380. */
  381. if (last_addr < end && pad_reason) {
  382. /* FIXME say how many bytes (e.g. 80 KB) */
  383. LOG_WARNING("Adding extra %s range, "
  384. "%#8.8x to %#8.8x",
  385. pad_reason,
  386. (unsigned) last_addr,
  387. (unsigned) end - 1);
  388. last = i;
  389. break;
  390. }
  391. /* MUST finish on a sector boundary */
  392. if (last_addr <= f->offset)
  393. break;
  394. }
  395. /* invalid start or end address? */
  396. if (first == -1 || last == -1) {
  397. LOG_ERROR("address range 0x%8.8x .. 0x%8.8x "
  398. "is not sector-aligned",
  399. (unsigned) (c->base + addr),
  400. (unsigned) (c->base + last_addr - 1));
  401. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  402. }
  403. /* The NOR driver may trim this range down, based on what
  404. * sectors are already erased/unprotected. GDB currently
  405. * blocks such optimizations.
  406. */
  407. return callback(c, first, last);
  408. }
  409. /* The inner fn only handles a single bank, we could be spanning
  410. * multiple chips.
  411. */
  412. static int flash_iterate_address_range(struct target *target,
  413. char *pad_reason, uint32_t addr, uint32_t length,
  414. bool iterate_protect_blocks,
  415. int (*callback)(struct flash_bank *bank, int first, int last))
  416. {
  417. struct flash_bank *c;
  418. int retval = ERROR_OK;
  419. /* Danger! zero-length iterations means entire bank! */
  420. do {
  421. retval = get_flash_bank_by_addr(target, addr, true, &c);
  422. if (retval != ERROR_OK)
  423. return retval;
  424. uint32_t cur_length = length;
  425. /* check whether it all fits in this bank */
  426. if (addr + length - 1 > c->base + c->size - 1) {
  427. LOG_DEBUG("iterating over more than one flash bank.");
  428. cur_length = c->base + c->size - addr;
  429. }
  430. retval = flash_iterate_address_range_inner(target,
  431. pad_reason, addr, cur_length,
  432. iterate_protect_blocks,
  433. callback);
  434. if (retval != ERROR_OK)
  435. break;
  436. length -= cur_length;
  437. addr += cur_length;
  438. } while (length > 0);
  439. return retval;
  440. }
  441. int flash_erase_address_range(struct target *target,
  442. bool pad, uint32_t addr, uint32_t length)
  443. {
  444. return flash_iterate_address_range(target, pad ? "erase" : NULL,
  445. addr, length, false, &flash_driver_erase);
  446. }
  447. static int flash_driver_unprotect(struct flash_bank *bank, int first, int last)
  448. {
  449. return flash_driver_protect(bank, 0, first, last);
  450. }
  451. int flash_unlock_address_range(struct target *target, uint32_t addr, uint32_t length)
  452. {
  453. /* By default, pad to sector boundaries ... the real issue here
  454. * is that our (only) caller *permanently* removes protection,
  455. * and doesn't restore it.
  456. */
  457. return flash_iterate_address_range(target, "unprotect",
  458. addr, length, true, &flash_driver_unprotect);
  459. }
  460. static int compare_section(const void *a, const void *b)
  461. {
  462. struct imagesection *b1, *b2;
  463. b1 = *((struct imagesection **)a);
  464. b2 = *((struct imagesection **)b);
  465. if (b1->base_address == b2->base_address)
  466. return 0;
  467. else if (b1->base_address > b2->base_address)
  468. return 1;
  469. else
  470. return -1;
  471. }
  472. int flash_write_unlock(struct target *target, struct image *image,
  473. uint32_t *written, int erase, bool unlock)
  474. {
  475. int retval = ERROR_OK;
  476. int section;
  477. uint32_t section_offset;
  478. struct flash_bank *c;
  479. int *padding;
  480. section = 0;
  481. section_offset = 0;
  482. if (written)
  483. *written = 0;
  484. if (erase) {
  485. /* assume all sectors need erasing - stops any problems
  486. * when flash_write is called multiple times */
  487. flash_set_dirty();
  488. }
  489. /* allocate padding array */
  490. padding = calloc(image->num_sections, sizeof(*padding));
  491. /* This fn requires all sections to be in ascending order of addresses,
  492. * whereas an image can have sections out of order. */
  493. struct imagesection **sections = malloc(sizeof(struct imagesection *) *
  494. image->num_sections);
  495. int i;
  496. for (i = 0; i < image->num_sections; i++)
  497. sections[i] = &image->sections[i];
  498. qsort(sections, image->num_sections, sizeof(struct imagesection *),
  499. compare_section);
  500. /* loop until we reach end of the image */
  501. while (section < image->num_sections) {
  502. uint32_t buffer_size;
  503. uint8_t *buffer;
  504. int section_last;
  505. uint32_t run_address = sections[section]->base_address + section_offset;
  506. uint32_t run_size = sections[section]->size - section_offset;
  507. int pad_bytes = 0;
  508. if (sections[section]->size == 0) {
  509. LOG_WARNING("empty section %d", section);
  510. section++;
  511. section_offset = 0;
  512. continue;
  513. }
  514. /* find the corresponding flash bank */
  515. retval = get_flash_bank_by_addr(target, run_address, false, &c);
  516. if (retval != ERROR_OK)
  517. goto done;
  518. if (c == NULL) {
  519. LOG_WARNING("no flash bank found for address %" PRIx32, run_address);
  520. section++; /* and skip it */
  521. section_offset = 0;
  522. continue;
  523. }
  524. /* collect consecutive sections which fall into the same bank */
  525. section_last = section;
  526. padding[section] = 0;
  527. while ((run_address + run_size - 1 < c->base + c->size - 1) &&
  528. (section_last + 1 < image->num_sections)) {
  529. /* sections are sorted */
  530. assert(sections[section_last + 1]->base_address >= c->base);
  531. if (sections[section_last + 1]->base_address >= (c->base + c->size)) {
  532. /* Done with this bank */
  533. break;
  534. }
  535. /* FIXME This needlessly touches sectors BETWEEN the
  536. * sections it's writing. Without auto erase, it just
  537. * writes ones. That WILL INVALIDATE data in cases
  538. * like Stellaris Tempest chips, corrupting internal
  539. * ECC codes; and at least FreeScale suggests issues
  540. * with that approach (in HC11 documentation).
  541. *
  542. * With auto erase enabled, data in those sectors will
  543. * be needlessly destroyed; and some of the limited
  544. * number of flash erase cycles will be wasted...
  545. *
  546. * In both cases, the extra writes slow things down.
  547. */
  548. /* if we have multiple sections within our image,
  549. * flash programming could fail due to alignment issues
  550. * attempt to rebuild a consecutive buffer for the flash loader */
  551. pad_bytes = (sections[section_last + 1]->base_address) - (run_address + run_size);
  552. padding[section_last] = pad_bytes;
  553. run_size += sections[++section_last]->size;
  554. run_size += pad_bytes;
  555. if (pad_bytes > 0)
  556. LOG_INFO("Padding image section %d with %d bytes",
  557. section_last-1,
  558. pad_bytes);
  559. }
  560. if (run_address + run_size - 1 > c->base + c->size - 1) {
  561. /* If we have more than one flash chip back to back, then we limit
  562. * the current write operation to the current chip.
  563. */
  564. LOG_DEBUG("Truncate flash run size to the current flash chip.");
  565. run_size = c->base + c->size - run_address;
  566. assert(run_size > 0);
  567. }
  568. /* If we're applying any sector automagic, then pad this
  569. * (maybe-combined) segment to the end of its last sector.
  570. */
  571. if (unlock || erase) {
  572. int sector;
  573. uint32_t offset_start = run_address - c->base;
  574. uint32_t offset_end = offset_start + run_size;
  575. uint32_t end = offset_end, delta;
  576. for (sector = 0; sector < c->num_sectors; sector++) {
  577. end = c->sectors[sector].offset
  578. + c->sectors[sector].size;
  579. if (offset_end <= end)
  580. break;
  581. }
  582. delta = end - offset_end;
  583. padding[section_last] += delta;
  584. run_size += delta;
  585. }
  586. /* allocate buffer */
  587. buffer = malloc(run_size);
  588. if (buffer == NULL) {
  589. LOG_ERROR("Out of memory for flash bank buffer");
  590. retval = ERROR_FAIL;
  591. goto done;
  592. }
  593. buffer_size = 0;
  594. /* read sections to the buffer */
  595. while (buffer_size < run_size) {
  596. size_t size_read;
  597. size_read = run_size - buffer_size;
  598. if (size_read > sections[section]->size - section_offset)
  599. size_read = sections[section]->size - section_offset;
  600. /* KLUDGE!
  601. *
  602. * #¤%#"%¤% we have to figure out the section # from the sorted
  603. * list of pointers to sections to invoke image_read_section()...
  604. */
  605. intptr_t diff = (intptr_t)sections[section] - (intptr_t)image->sections;
  606. int t_section_num = diff / sizeof(struct imagesection);
  607. LOG_DEBUG("image_read_section: section = %d, t_section_num = %d, "
  608. "section_offset = %d, buffer_size = %d, size_read = %d",
  609. (int)section, (int)t_section_num, (int)section_offset,
  610. (int)buffer_size, (int)size_read);
  611. retval = image_read_section(image, t_section_num, section_offset,
  612. size_read, buffer + buffer_size, &size_read);
  613. if (retval != ERROR_OK || size_read == 0) {
  614. free(buffer);
  615. goto done;
  616. }
  617. /* see if we need to pad the section */
  618. while (padding[section]--)
  619. (buffer + buffer_size)[size_read++] = c->default_padded_value;
  620. buffer_size += size_read;
  621. section_offset += size_read;
  622. if (section_offset >= sections[section]->size) {
  623. section++;
  624. section_offset = 0;
  625. }
  626. }
  627. retval = ERROR_OK;
  628. if (unlock)
  629. retval = flash_unlock_address_range(target, run_address, run_size);
  630. if (retval == ERROR_OK) {
  631. if (erase) {
  632. /* calculate and erase sectors */
  633. retval = flash_erase_address_range(target,
  634. true, run_address, run_size);
  635. }
  636. }
  637. if (retval == ERROR_OK) {
  638. /* write flash sectors */
  639. retval = flash_driver_write(c, buffer, run_address - c->base, run_size);
  640. }
  641. free(buffer);
  642. if (retval != ERROR_OK) {
  643. /* abort operation */
  644. goto done;
  645. }
  646. if (written != NULL)
  647. *written += run_size; /* add run size to total written counter */
  648. }
  649. done:
  650. free(sections);
  651. free(padding);
  652. return retval;
  653. }
  654. int flash_write(struct target *target, struct image *image,
  655. uint32_t *written, int erase)
  656. {
  657. return flash_write_unlock(target, image, written, erase, false);
  658. }
  659. struct flash_sector *alloc_block_array(uint32_t offset, uint32_t size, int num_blocks)
  660. {
  661. int i;
  662. struct flash_sector *array = calloc(num_blocks, sizeof(struct flash_sector));
  663. if (array == NULL)
  664. return NULL;
  665. for (i = 0; i < num_blocks; i++) {
  666. array[i].offset = offset;
  667. array[i].size = size;
  668. array[i].is_erased = -1;
  669. array[i].is_protected = -1;
  670. offset += size;
  671. }
  672. return array;
  673. }