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.
 
 
 
 
 
 

1026 lines
28 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. * Copyright (C) 2017-2018 Tomas Vanek <vanekt@fbl.cz> *
  8. * *
  9. * This program is free software; you can redistribute it and/or modify *
  10. * it under the terms of the GNU General Public License as published by *
  11. * the Free Software Foundation; either version 2 of the License, or *
  12. * (at your option) any later version. *
  13. * *
  14. * This program is distributed in the hope that it will be useful, *
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  17. * GNU General Public License for more details. *
  18. * *
  19. * You should have received a copy of the GNU General Public License *
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  21. ***************************************************************************/
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #include <flash/common.h>
  26. #include <flash/nor/core.h>
  27. #include <flash/nor/imp.h>
  28. #include <target/image.h>
  29. /**
  30. * @file
  31. * Upper level of NOR flash framework.
  32. * The lower level interfaces are to drivers. These upper level ones
  33. * primarily support access from Tcl scripts or from GDB.
  34. */
  35. static struct flash_bank *flash_banks;
  36. int flash_driver_erase(struct flash_bank *bank, unsigned int first,
  37. unsigned 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 %u to %u", first, last);
  43. return retval;
  44. }
  45. int flash_driver_protect(struct flash_bank *bank, int set, unsigned int first,
  46. unsigned int last)
  47. {
  48. int retval;
  49. unsigned int num_blocks;
  50. if (bank->num_prot_blocks)
  51. num_blocks = bank->num_prot_blocks;
  52. else
  53. num_blocks = bank->num_sectors;
  54. /* callers may not supply illegal parameters ... */
  55. if (first > last || last >= num_blocks) {
  56. LOG_ERROR("illegal protection block range");
  57. return ERROR_FAIL;
  58. }
  59. /* force "set" to 0/1 */
  60. set = !!set;
  61. if (!bank->driver->protect) {
  62. LOG_ERROR("Flash protection is not supported.");
  63. return ERROR_FLASH_OPER_UNSUPPORTED;
  64. }
  65. /* DANGER!
  66. *
  67. * We must not use any cached information about protection state!!!!
  68. *
  69. * There are a million things that could change the protect state:
  70. *
  71. * the target could have reset, power cycled, been hot plugged,
  72. * the application could have run, etc.
  73. *
  74. * Drivers only receive valid protection block range.
  75. */
  76. retval = bank->driver->protect(bank, set, first, last);
  77. if (retval != ERROR_OK)
  78. LOG_ERROR("failed setting protection for blocks %u to %u", first, last);
  79. return retval;
  80. }
  81. int flash_driver_write(struct flash_bank *bank,
  82. const uint8_t *buffer, uint32_t offset, uint32_t count)
  83. {
  84. int retval;
  85. retval = bank->driver->write(bank, buffer, offset, count);
  86. if (retval != ERROR_OK) {
  87. LOG_ERROR(
  88. "error writing to flash at address " TARGET_ADDR_FMT
  89. " at offset 0x%8.8" PRIx32,
  90. bank->base,
  91. offset);
  92. }
  93. return retval;
  94. }
  95. int flash_driver_read(struct flash_bank *bank,
  96. uint8_t *buffer, uint32_t offset, uint32_t count)
  97. {
  98. int retval;
  99. LOG_DEBUG("call flash_driver_read()");
  100. retval = bank->driver->read(bank, buffer, offset, count);
  101. if (retval != ERROR_OK) {
  102. LOG_ERROR(
  103. "error reading to flash at address " TARGET_ADDR_FMT
  104. " at offset 0x%8.8" PRIx32,
  105. bank->base,
  106. offset);
  107. }
  108. return retval;
  109. }
  110. int default_flash_read(struct flash_bank *bank,
  111. uint8_t *buffer, uint32_t offset, uint32_t count)
  112. {
  113. return target_read_buffer(bank->target, offset + bank->base, count, buffer);
  114. }
  115. int flash_driver_verify(struct flash_bank *bank,
  116. const uint8_t *buffer, uint32_t offset, uint32_t count)
  117. {
  118. int retval;
  119. retval = bank->driver->verify ? bank->driver->verify(bank, buffer, offset, count) :
  120. default_flash_verify(bank, buffer, offset, count);
  121. if (retval != ERROR_OK) {
  122. LOG_ERROR("verify failed in bank at " TARGET_ADDR_FMT " starting at 0x%8.8" PRIx32,
  123. bank->base, offset);
  124. }
  125. return retval;
  126. }
  127. int default_flash_verify(struct flash_bank *bank,
  128. const uint8_t *buffer, uint32_t offset, uint32_t count)
  129. {
  130. uint32_t target_crc, image_crc;
  131. int retval;
  132. retval = image_calculate_checksum(buffer, count, &image_crc);
  133. if (retval != ERROR_OK)
  134. return retval;
  135. retval = target_checksum_memory(bank->target, offset + bank->base, count, &target_crc);
  136. if (retval != ERROR_OK)
  137. return retval;
  138. LOG_DEBUG("addr " TARGET_ADDR_FMT ", len 0x%08" PRIx32 ", crc 0x%08" PRIx32 " 0x%08" PRIx32,
  139. offset + bank->base, count, ~image_crc, ~target_crc);
  140. if (target_crc == image_crc)
  141. return ERROR_OK;
  142. else
  143. return ERROR_FAIL;
  144. }
  145. void flash_bank_add(struct flash_bank *bank)
  146. {
  147. /* put flash bank in linked list */
  148. unsigned bank_num = 0;
  149. if (flash_banks) {
  150. /* find last flash bank */
  151. struct flash_bank *p = flash_banks;
  152. while (p->next) {
  153. bank_num += 1;
  154. p = p->next;
  155. }
  156. p->next = bank;
  157. bank_num += 1;
  158. } else
  159. flash_banks = bank;
  160. bank->bank_number = bank_num;
  161. }
  162. struct flash_bank *flash_bank_list(void)
  163. {
  164. return flash_banks;
  165. }
  166. struct flash_bank *get_flash_bank_by_num_noprobe(unsigned int num)
  167. {
  168. struct flash_bank *p;
  169. unsigned int i = 0;
  170. for (p = flash_banks; p; p = p->next) {
  171. if (i++ == num)
  172. return p;
  173. }
  174. LOG_ERROR("flash bank %d does not exist", num);
  175. return NULL;
  176. }
  177. unsigned int flash_get_bank_count(void)
  178. {
  179. struct flash_bank *p;
  180. unsigned int i = 0;
  181. for (p = flash_banks; p; p = p->next)
  182. i++;
  183. return i;
  184. }
  185. void default_flash_free_driver_priv(struct flash_bank *bank)
  186. {
  187. free(bank->driver_priv);
  188. bank->driver_priv = NULL;
  189. }
  190. void flash_free_all_banks(void)
  191. {
  192. struct flash_bank *bank = flash_banks;
  193. while (bank) {
  194. struct flash_bank *next = bank->next;
  195. if (bank->driver->free_driver_priv)
  196. bank->driver->free_driver_priv(bank);
  197. else
  198. LOG_WARNING("Flash driver of %s does not support free_driver_priv()", bank->name);
  199. /* For 'virtual' flash driver bank->sectors and bank->prot_blocks pointers are copied from
  200. * master flash_bank structure. They point to memory locations allocated by master flash driver
  201. * so master driver is responsible for releasing them.
  202. * Avoid UB caused by double-free memory corruption if flash bank is 'virtual'. */
  203. if (strcmp(bank->driver->name, "virtual") != 0) {
  204. free(bank->sectors);
  205. free(bank->prot_blocks);
  206. }
  207. free(bank->name);
  208. free(bank);
  209. bank = next;
  210. }
  211. flash_banks = NULL;
  212. }
  213. struct flash_bank *get_flash_bank_by_name_noprobe(const char *name)
  214. {
  215. unsigned requested = get_flash_name_index(name);
  216. unsigned found = 0;
  217. struct flash_bank *bank;
  218. for (bank = flash_banks; NULL != bank; bank = bank->next) {
  219. if (strcmp(bank->name, name) == 0)
  220. return bank;
  221. if (!flash_driver_name_matches(bank->driver->name, name))
  222. continue;
  223. if (++found < requested)
  224. continue;
  225. return bank;
  226. }
  227. return NULL;
  228. }
  229. int get_flash_bank_by_name(const char *name, struct flash_bank **bank_result)
  230. {
  231. struct flash_bank *bank;
  232. int retval;
  233. bank = get_flash_bank_by_name_noprobe(name);
  234. if (bank) {
  235. retval = bank->driver->auto_probe(bank);
  236. if (retval != ERROR_OK) {
  237. LOG_ERROR("auto_probe failed");
  238. return retval;
  239. }
  240. }
  241. *bank_result = bank;
  242. return ERROR_OK;
  243. }
  244. int get_flash_bank_by_num(unsigned int num, struct flash_bank **bank)
  245. {
  246. struct flash_bank *p = get_flash_bank_by_num_noprobe(num);
  247. int retval;
  248. if (!p)
  249. return ERROR_FAIL;
  250. retval = p->driver->auto_probe(p);
  251. if (retval != ERROR_OK) {
  252. LOG_ERROR("auto_probe failed");
  253. return retval;
  254. }
  255. *bank = p;
  256. return ERROR_OK;
  257. }
  258. /* lookup flash bank by address, bank not found is success, but
  259. * result_bank is set to NULL. */
  260. int get_flash_bank_by_addr(struct target *target,
  261. target_addr_t addr,
  262. bool check,
  263. struct flash_bank **result_bank)
  264. {
  265. struct flash_bank *c;
  266. /* cycle through bank list */
  267. for (c = flash_banks; c; c = c->next) {
  268. if (c->target != target)
  269. continue;
  270. int retval;
  271. retval = c->driver->auto_probe(c);
  272. if (retval != ERROR_OK) {
  273. LOG_ERROR("auto_probe failed");
  274. return retval;
  275. }
  276. /* check whether address belongs to this flash bank */
  277. if ((addr >= c->base) && (addr <= c->base + (c->size - 1))) {
  278. *result_bank = c;
  279. return ERROR_OK;
  280. }
  281. }
  282. *result_bank = NULL;
  283. if (check) {
  284. LOG_ERROR("No flash at address " TARGET_ADDR_FMT, addr);
  285. return ERROR_FAIL;
  286. }
  287. return ERROR_OK;
  288. }
  289. static int default_flash_mem_blank_check(struct flash_bank *bank)
  290. {
  291. struct target *target = bank->target;
  292. const int buffer_size = 1024;
  293. uint32_t n_bytes;
  294. int retval = ERROR_OK;
  295. if (bank->target->state != TARGET_HALTED) {
  296. LOG_ERROR("Target not halted");
  297. return ERROR_TARGET_NOT_HALTED;
  298. }
  299. uint8_t *buffer = malloc(buffer_size);
  300. for (unsigned int i = 0; i < bank->num_sectors; i++) {
  301. uint32_t j;
  302. bank->sectors[i].is_erased = 1;
  303. for (j = 0; j < bank->sectors[i].size; j += buffer_size) {
  304. uint32_t chunk;
  305. chunk = buffer_size;
  306. if (chunk > (bank->sectors[i].size - j))
  307. chunk = (bank->sectors[i].size - j);
  308. retval = target_read_memory(target,
  309. bank->base + bank->sectors[i].offset + j,
  310. 4,
  311. chunk/4,
  312. buffer);
  313. if (retval != ERROR_OK)
  314. goto done;
  315. for (n_bytes = 0; n_bytes < chunk; n_bytes++) {
  316. if (buffer[n_bytes] != bank->erased_value) {
  317. bank->sectors[i].is_erased = 0;
  318. break;
  319. }
  320. }
  321. }
  322. }
  323. done:
  324. free(buffer);
  325. return retval;
  326. }
  327. int default_flash_blank_check(struct flash_bank *bank)
  328. {
  329. struct target *target = bank->target;
  330. int retval;
  331. if (bank->target->state != TARGET_HALTED) {
  332. LOG_ERROR("Target not halted");
  333. return ERROR_TARGET_NOT_HALTED;
  334. }
  335. struct target_memory_check_block *block_array;
  336. block_array = malloc(bank->num_sectors * sizeof(struct target_memory_check_block));
  337. if (!block_array)
  338. return default_flash_mem_blank_check(bank);
  339. for (unsigned int i = 0; i < bank->num_sectors; i++) {
  340. block_array[i].address = bank->base + bank->sectors[i].offset;
  341. block_array[i].size = bank->sectors[i].size;
  342. block_array[i].result = UINT32_MAX; /* erase state unknown */
  343. }
  344. bool fast_check = true;
  345. for (unsigned int i = 0; i < bank->num_sectors; ) {
  346. retval = target_blank_check_memory(target,
  347. block_array + i, bank->num_sectors - i,
  348. bank->erased_value);
  349. if (retval < 1) {
  350. /* Run slow fallback if the first run gives no result
  351. * otherwise use possibly incomplete results */
  352. if (i == 0)
  353. fast_check = false;
  354. break;
  355. }
  356. i += retval; /* add number of blocks done this round */
  357. }
  358. if (fast_check) {
  359. for (unsigned int i = 0; i < bank->num_sectors; i++)
  360. bank->sectors[i].is_erased = block_array[i].result;
  361. retval = ERROR_OK;
  362. } else {
  363. LOG_USER("Running slow fallback erase check - add working memory");
  364. retval = default_flash_mem_blank_check(bank);
  365. }
  366. free(block_array);
  367. return retval;
  368. }
  369. /* Manipulate given flash region, selecting the bank according to target
  370. * and address. Maps an address range to a set of sectors, and issues
  371. * the callback() on that set ... e.g. to erase or unprotect its members.
  372. *
  373. * Parameter iterate_protect_blocks switches iteration of protect block
  374. * instead of erase sectors. If there is no protect blocks array, sectors
  375. * are used in iteration, so compatibility for old flash drivers is retained.
  376. *
  377. * The "pad_reason" parameter is a kind of boolean: when it's NULL, the
  378. * range must fit those sectors exactly. This is clearly safe; it can't
  379. * erase data which the caller said to leave alone, for example. If it's
  380. * non-NULL, rather than failing, extra data in the first and/or last
  381. * sectors will be added to the range, and that reason string is used when
  382. * warning about those additions.
  383. */
  384. static int flash_iterate_address_range_inner(struct target *target,
  385. char *pad_reason, target_addr_t addr, uint32_t length,
  386. bool iterate_protect_blocks,
  387. int (*callback)(struct flash_bank *bank, unsigned int first,
  388. unsigned int last))
  389. {
  390. struct flash_bank *c;
  391. struct flash_sector *block_array;
  392. target_addr_t last_addr = addr + length - 1; /* the last address of range */
  393. int first = -1;
  394. int last = -1;
  395. int i;
  396. int num_blocks;
  397. int retval = get_flash_bank_by_addr(target, addr, true, &c);
  398. if (retval != ERROR_OK)
  399. return retval;
  400. if (c->size == 0 || c->num_sectors == 0) {
  401. LOG_ERROR("Bank is invalid");
  402. return ERROR_FLASH_BANK_INVALID;
  403. }
  404. if (length == 0) {
  405. /* special case, erase whole bank when length is zero */
  406. if (addr != c->base) {
  407. LOG_ERROR("Whole bank access must start at beginning of bank.");
  408. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  409. }
  410. return callback(c, 0, c->num_sectors - 1);
  411. }
  412. /* check whether it all fits in this bank */
  413. if (last_addr > c->base + c->size - 1) {
  414. LOG_ERROR("Flash access does not fit into bank.");
  415. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  416. }
  417. if (!c->prot_blocks || c->num_prot_blocks == 0) {
  418. /* flash driver does not define protect blocks, use sectors instead */
  419. iterate_protect_blocks = false;
  420. }
  421. if (iterate_protect_blocks) {
  422. block_array = c->prot_blocks;
  423. num_blocks = c->num_prot_blocks;
  424. } else {
  425. block_array = c->sectors;
  426. num_blocks = c->num_sectors;
  427. }
  428. for (i = 0; i < num_blocks; i++) {
  429. struct flash_sector *f = &block_array[i];
  430. target_addr_t sector_addr = c->base + f->offset;
  431. target_addr_t sector_last_addr = sector_addr + f->size - 1;
  432. /* start only on a sector boundary */
  433. if (first < 0) {
  434. /* scanned past the first sector? */
  435. if (addr < sector_addr)
  436. break;
  437. /* is this the first sector? */
  438. if (addr == sector_addr)
  439. first = i;
  440. /* Does this need head-padding? If so, pad and warn;
  441. * or else force an error.
  442. *
  443. * Such padding can make trouble, since *WE* can't
  444. * ever know if that data was in use. The warning
  445. * should help users sort out messes later.
  446. */
  447. else if (addr <= sector_last_addr && pad_reason) {
  448. /* FIXME say how many bytes (e.g. 80 KB) */
  449. LOG_WARNING("Adding extra %s range, "
  450. TARGET_ADDR_FMT " .. " TARGET_ADDR_FMT,
  451. pad_reason,
  452. sector_addr,
  453. addr - 1);
  454. first = i;
  455. } else
  456. continue;
  457. }
  458. /* is this (also?) the last sector? */
  459. if (last_addr == sector_last_addr) {
  460. last = i;
  461. break;
  462. }
  463. /* Does this need tail-padding? If so, pad and warn;
  464. * or else force an error.
  465. */
  466. if (last_addr < sector_last_addr && pad_reason) {
  467. /* FIXME say how many bytes (e.g. 80 KB) */
  468. LOG_WARNING("Adding extra %s range, "
  469. TARGET_ADDR_FMT " .. " TARGET_ADDR_FMT,
  470. pad_reason,
  471. last_addr + 1,
  472. sector_last_addr);
  473. last = i;
  474. break;
  475. }
  476. /* MUST finish on a sector boundary */
  477. if (last_addr < sector_addr)
  478. break;
  479. }
  480. /* invalid start or end address? */
  481. if (first == -1 || last == -1) {
  482. LOG_ERROR("address range " TARGET_ADDR_FMT " .. " TARGET_ADDR_FMT
  483. " is not sector-aligned",
  484. addr,
  485. last_addr);
  486. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  487. }
  488. /* The NOR driver may trim this range down, based on what
  489. * sectors are already erased/unprotected. GDB currently
  490. * blocks such optimizations.
  491. */
  492. return callback(c, first, last);
  493. }
  494. /* The inner fn only handles a single bank, we could be spanning
  495. * multiple chips.
  496. */
  497. static int flash_iterate_address_range(struct target *target,
  498. char *pad_reason, target_addr_t addr, uint32_t length,
  499. bool iterate_protect_blocks,
  500. int (*callback)(struct flash_bank *bank, unsigned int first,
  501. unsigned int last))
  502. {
  503. struct flash_bank *c;
  504. int retval = ERROR_OK;
  505. /* Danger! zero-length iterations means entire bank! */
  506. do {
  507. retval = get_flash_bank_by_addr(target, addr, true, &c);
  508. if (retval != ERROR_OK)
  509. return retval;
  510. uint32_t cur_length = length;
  511. /* check whether it all fits in this bank */
  512. if (addr + length - 1 > c->base + c->size - 1) {
  513. LOG_DEBUG("iterating over more than one flash bank.");
  514. cur_length = c->base + c->size - addr;
  515. }
  516. retval = flash_iterate_address_range_inner(target,
  517. pad_reason, addr, cur_length,
  518. iterate_protect_blocks,
  519. callback);
  520. if (retval != ERROR_OK)
  521. break;
  522. length -= cur_length;
  523. addr += cur_length;
  524. } while (length > 0);
  525. return retval;
  526. }
  527. int flash_erase_address_range(struct target *target,
  528. bool pad, target_addr_t addr, uint32_t length)
  529. {
  530. return flash_iterate_address_range(target, pad ? "erase" : NULL,
  531. addr, length, false, &flash_driver_erase);
  532. }
  533. static int flash_driver_unprotect(struct flash_bank *bank, unsigned int first,
  534. unsigned int last)
  535. {
  536. return flash_driver_protect(bank, 0, first, last);
  537. }
  538. int flash_unlock_address_range(struct target *target, target_addr_t addr,
  539. uint32_t length)
  540. {
  541. /* By default, pad to sector boundaries ... the real issue here
  542. * is that our (only) caller *permanently* removes protection,
  543. * and doesn't restore it.
  544. */
  545. return flash_iterate_address_range(target, "unprotect",
  546. addr, length, true, &flash_driver_unprotect);
  547. }
  548. static int compare_section(const void *a, const void *b)
  549. {
  550. struct imagesection *b1, *b2;
  551. b1 = *((struct imagesection **)a);
  552. b2 = *((struct imagesection **)b);
  553. if (b1->base_address == b2->base_address)
  554. return 0;
  555. else if (b1->base_address > b2->base_address)
  556. return 1;
  557. else
  558. return -1;
  559. }
  560. /**
  561. * Get aligned start address of a flash write region
  562. */
  563. target_addr_t flash_write_align_start(struct flash_bank *bank, target_addr_t addr)
  564. {
  565. if (addr < bank->base || addr >= bank->base + bank->size
  566. || bank->write_start_alignment <= 1)
  567. return addr;
  568. if (bank->write_start_alignment == FLASH_WRITE_ALIGN_SECTOR) {
  569. uint32_t offset = addr - bank->base;
  570. uint32_t aligned = 0;
  571. for (unsigned int sect = 0; sect < bank->num_sectors; sect++) {
  572. if (bank->sectors[sect].offset > offset)
  573. break;
  574. aligned = bank->sectors[sect].offset;
  575. }
  576. return bank->base + aligned;
  577. }
  578. return addr & ~(bank->write_start_alignment - 1);
  579. }
  580. /**
  581. * Get aligned end address of a flash write region
  582. */
  583. target_addr_t flash_write_align_end(struct flash_bank *bank, target_addr_t addr)
  584. {
  585. if (addr < bank->base || addr >= bank->base + bank->size
  586. || bank->write_end_alignment <= 1)
  587. return addr;
  588. if (bank->write_end_alignment == FLASH_WRITE_ALIGN_SECTOR) {
  589. uint32_t offset = addr - bank->base;
  590. uint32_t aligned = 0;
  591. for (unsigned int sect = 0; sect < bank->num_sectors; sect++) {
  592. aligned = bank->sectors[sect].offset + bank->sectors[sect].size - 1;
  593. if (aligned >= offset)
  594. break;
  595. }
  596. return bank->base + aligned;
  597. }
  598. return addr | (bank->write_end_alignment - 1);
  599. }
  600. /**
  601. * Check if gap between sections is bigger than minimum required to discontinue flash write
  602. */
  603. static bool flash_write_check_gap(struct flash_bank *bank,
  604. target_addr_t addr1, target_addr_t addr2)
  605. {
  606. if (bank->minimal_write_gap == FLASH_WRITE_CONTINUOUS
  607. || addr1 < bank->base || addr1 >= bank->base + bank->size
  608. || addr2 < bank->base || addr2 >= bank->base + bank->size)
  609. return false;
  610. if (bank->minimal_write_gap == FLASH_WRITE_GAP_SECTOR) {
  611. unsigned int sect;
  612. uint32_t offset1 = addr1 - bank->base;
  613. /* find the sector following the one containing addr1 */
  614. for (sect = 0; sect < bank->num_sectors; sect++) {
  615. if (bank->sectors[sect].offset > offset1)
  616. break;
  617. }
  618. if (sect >= bank->num_sectors)
  619. return false;
  620. uint32_t offset2 = addr2 - bank->base;
  621. return bank->sectors[sect].offset + bank->sectors[sect].size <= offset2;
  622. }
  623. target_addr_t aligned1 = flash_write_align_end(bank, addr1);
  624. target_addr_t aligned2 = flash_write_align_start(bank, addr2);
  625. return aligned1 + bank->minimal_write_gap < aligned2;
  626. }
  627. int flash_write_unlock_verify(struct target *target, struct image *image,
  628. uint32_t *written, bool erase, bool unlock, bool write, bool verify)
  629. {
  630. int retval = ERROR_OK;
  631. unsigned int section;
  632. uint32_t section_offset;
  633. struct flash_bank *c;
  634. int *padding;
  635. section = 0;
  636. section_offset = 0;
  637. if (written)
  638. *written = 0;
  639. if (erase) {
  640. /* assume all sectors need erasing - stops any problems
  641. * when flash_write is called multiple times */
  642. flash_set_dirty();
  643. }
  644. /* allocate padding array */
  645. padding = calloc(image->num_sections, sizeof(*padding));
  646. /* This fn requires all sections to be in ascending order of addresses,
  647. * whereas an image can have sections out of order. */
  648. struct imagesection **sections = malloc(sizeof(struct imagesection *) *
  649. image->num_sections);
  650. for (unsigned int i = 0; i < image->num_sections; i++)
  651. sections[i] = &image->sections[i];
  652. qsort(sections, image->num_sections, sizeof(struct imagesection *),
  653. compare_section);
  654. /* loop until we reach end of the image */
  655. while (section < image->num_sections) {
  656. uint32_t buffer_idx;
  657. uint8_t *buffer;
  658. unsigned int section_last;
  659. target_addr_t run_address = sections[section]->base_address + section_offset;
  660. uint32_t run_size = sections[section]->size - section_offset;
  661. int pad_bytes = 0;
  662. if (sections[section]->size == 0) {
  663. LOG_WARNING("empty section %d", section);
  664. section++;
  665. section_offset = 0;
  666. continue;
  667. }
  668. /* find the corresponding flash bank */
  669. retval = get_flash_bank_by_addr(target, run_address, false, &c);
  670. if (retval != ERROR_OK)
  671. goto done;
  672. if (!c) {
  673. LOG_WARNING("no flash bank found for address " TARGET_ADDR_FMT, run_address);
  674. section++; /* and skip it */
  675. section_offset = 0;
  676. continue;
  677. }
  678. /* collect consecutive sections which fall into the same bank */
  679. section_last = section;
  680. padding[section] = 0;
  681. while ((run_address + run_size - 1 < c->base + c->size - 1) &&
  682. (section_last + 1 < image->num_sections)) {
  683. /* sections are sorted */
  684. assert(sections[section_last + 1]->base_address >= c->base);
  685. if (sections[section_last + 1]->base_address >= (c->base + c->size)) {
  686. /* Done with this bank */
  687. break;
  688. }
  689. /* if we have multiple sections within our image,
  690. * flash programming could fail due to alignment issues
  691. * attempt to rebuild a consecutive buffer for the flash loader */
  692. target_addr_t run_next_addr = run_address + run_size;
  693. target_addr_t next_section_base = sections[section_last + 1]->base_address;
  694. if (next_section_base < run_next_addr) {
  695. LOG_ERROR("Section at " TARGET_ADDR_FMT
  696. " overlaps section ending at " TARGET_ADDR_FMT,
  697. next_section_base, run_next_addr);
  698. LOG_ERROR("Flash write aborted.");
  699. retval = ERROR_FAIL;
  700. goto done;
  701. }
  702. pad_bytes = next_section_base - run_next_addr;
  703. if (pad_bytes) {
  704. if (flash_write_check_gap(c, run_next_addr - 1, next_section_base)) {
  705. LOG_INFO("Flash write discontinued at " TARGET_ADDR_FMT
  706. ", next section at " TARGET_ADDR_FMT,
  707. run_next_addr, next_section_base);
  708. break;
  709. }
  710. }
  711. if (pad_bytes > 0)
  712. LOG_INFO("Padding image section %d at " TARGET_ADDR_FMT
  713. " with %d bytes",
  714. section_last, run_next_addr, pad_bytes);
  715. padding[section_last] = pad_bytes;
  716. run_size += pad_bytes;
  717. run_size += sections[++section_last]->size;
  718. }
  719. if (run_address + run_size - 1 > c->base + c->size - 1) {
  720. /* If we have more than one flash chip back to back, then we limit
  721. * the current write operation to the current chip.
  722. */
  723. LOG_DEBUG("Truncate flash run size to the current flash chip.");
  724. run_size = c->base + c->size - run_address;
  725. assert(run_size > 0);
  726. }
  727. uint32_t padding_at_start = 0;
  728. if (c->write_start_alignment || c->write_end_alignment) {
  729. /* align write region according to bank requirements */
  730. target_addr_t aligned_start = flash_write_align_start(c, run_address);
  731. padding_at_start = run_address - aligned_start;
  732. if (padding_at_start > 0) {
  733. LOG_WARNING("Section start address " TARGET_ADDR_FMT
  734. " breaks the required alignment of flash bank %s",
  735. run_address, c->name);
  736. LOG_WARNING("Padding %" PRIu32 " bytes from " TARGET_ADDR_FMT,
  737. padding_at_start, aligned_start);
  738. run_address -= padding_at_start;
  739. run_size += padding_at_start;
  740. }
  741. target_addr_t run_end = run_address + run_size - 1;
  742. target_addr_t aligned_end = flash_write_align_end(c, run_end);
  743. pad_bytes = aligned_end - run_end;
  744. if (pad_bytes > 0) {
  745. LOG_INFO("Padding image section %d at " TARGET_ADDR_FMT
  746. " with %d bytes (bank write end alignment)",
  747. section_last, run_end + 1, pad_bytes);
  748. padding[section_last] += pad_bytes;
  749. run_size += pad_bytes;
  750. }
  751. } else if (unlock || erase) {
  752. /* If we're applying any sector automagic, then pad this
  753. * (maybe-combined) segment to the end of its last sector.
  754. */
  755. uint32_t offset_start = run_address - c->base;
  756. uint32_t offset_end = offset_start + run_size;
  757. uint32_t end = offset_end, delta;
  758. for (unsigned int sector = 0; sector < c->num_sectors; sector++) {
  759. end = c->sectors[sector].offset
  760. + c->sectors[sector].size;
  761. if (offset_end <= end)
  762. break;
  763. }
  764. delta = end - offset_end;
  765. padding[section_last] += delta;
  766. run_size += delta;
  767. }
  768. /* allocate buffer */
  769. buffer = malloc(run_size);
  770. if (!buffer) {
  771. LOG_ERROR("Out of memory for flash bank buffer");
  772. retval = ERROR_FAIL;
  773. goto done;
  774. }
  775. if (padding_at_start)
  776. memset(buffer, c->default_padded_value, padding_at_start);
  777. buffer_idx = padding_at_start;
  778. /* read sections to the buffer */
  779. while (buffer_idx < run_size) {
  780. size_t size_read;
  781. size_read = run_size - buffer_idx;
  782. if (size_read > sections[section]->size - section_offset)
  783. size_read = sections[section]->size - section_offset;
  784. /* KLUDGE!
  785. *
  786. * #¤%#"%¤% we have to figure out the section # from the sorted
  787. * list of pointers to sections to invoke image_read_section()...
  788. */
  789. intptr_t diff = (intptr_t)sections[section] - (intptr_t)image->sections;
  790. int t_section_num = diff / sizeof(struct imagesection);
  791. LOG_DEBUG("image_read_section: section = %d, t_section_num = %d, "
  792. "section_offset = %"PRIu32", buffer_idx = %"PRIu32", size_read = %zu",
  793. section, t_section_num, section_offset,
  794. buffer_idx, size_read);
  795. retval = image_read_section(image, t_section_num, section_offset,
  796. size_read, buffer + buffer_idx, &size_read);
  797. if (retval != ERROR_OK || size_read == 0) {
  798. free(buffer);
  799. goto done;
  800. }
  801. buffer_idx += size_read;
  802. section_offset += size_read;
  803. /* see if we need to pad the section */
  804. if (padding[section]) {
  805. memset(buffer + buffer_idx, c->default_padded_value, padding[section]);
  806. buffer_idx += padding[section];
  807. }
  808. if (section_offset >= sections[section]->size) {
  809. section++;
  810. section_offset = 0;
  811. }
  812. }
  813. retval = ERROR_OK;
  814. if (unlock)
  815. retval = flash_unlock_address_range(target, run_address, run_size);
  816. if (retval == ERROR_OK) {
  817. if (erase) {
  818. /* calculate and erase sectors */
  819. retval = flash_erase_address_range(target,
  820. true, run_address, run_size);
  821. }
  822. }
  823. if (retval == ERROR_OK) {
  824. if (write) {
  825. /* write flash sectors */
  826. retval = flash_driver_write(c, buffer, run_address - c->base, run_size);
  827. }
  828. }
  829. if (retval == ERROR_OK) {
  830. if (verify) {
  831. /* verify flash sectors */
  832. retval = flash_driver_verify(c, buffer, run_address - c->base, run_size);
  833. }
  834. }
  835. free(buffer);
  836. if (retval != ERROR_OK) {
  837. /* abort operation */
  838. goto done;
  839. }
  840. if (written)
  841. *written += run_size; /* add run size to total written counter */
  842. }
  843. done:
  844. free(sections);
  845. free(padding);
  846. return retval;
  847. }
  848. int flash_write(struct target *target, struct image *image,
  849. uint32_t *written, bool erase)
  850. {
  851. return flash_write_unlock_verify(target, image, written, erase, false, true, false);
  852. }
  853. struct flash_sector *alloc_block_array(uint32_t offset, uint32_t size,
  854. unsigned int num_blocks)
  855. {
  856. struct flash_sector *array = calloc(num_blocks, sizeof(struct flash_sector));
  857. if (!array)
  858. return NULL;
  859. for (unsigned int i = 0; i < num_blocks; i++) {
  860. array[i].offset = offset;
  861. array[i].size = size;
  862. array[i].is_erased = -1;
  863. array[i].is_protected = -1;
  864. offset += size;
  865. }
  866. return array;
  867. }