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.

core.c 20 KiB

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