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.
 
 
 
 
 
 

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