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.
 
 
 
 
 
 

748 lines
19 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath <Dominic.Rath@gmx.de> *
  3. * Copyright (C) 2007,2008 Ø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. * *
  7. * This program is free software; you can redistribute it and/or modify *
  8. * it under the terms of the GNU General Public License as published by *
  9. * the Free Software Foundation; either version 2 of the License, or *
  10. * (at your option) any later version. *
  11. * *
  12. * This program is distributed in the hope that it will be useful, *
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  15. * GNU General Public License for more details. *
  16. * *
  17. * You should have received a copy of the GNU General Public License *
  18. * along with this program; if not, write to the *
  19. * Free Software Foundation, Inc., *
  20. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  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, int first, int last)
  37. {
  38. int retval;
  39. retval = bank->driver->erase(bank, first, last);
  40. if (retval != ERROR_OK)
  41. {
  42. LOG_ERROR("failed erasing sectors %d to %d (%d)", first, last, retval);
  43. }
  44. return retval;
  45. }
  46. int flash_driver_protect(struct flash_bank *bank, int set, int first, int last)
  47. {
  48. int retval;
  49. bool updated = false;
  50. /* NOTE: "first == last" means protect just that sector */
  51. /* callers may not supply illegal parameters ... */
  52. if (first < 0 || first > last || last >= bank->num_sectors)
  53. return ERROR_FAIL;
  54. /* force "set" to 0/1 */
  55. set = !!set;
  56. /*
  57. * Filter out what trivial nonsense we can, so drivers don't have to.
  58. *
  59. * Don't tell drivers to change to the current state... it's needless,
  60. * and reducing the amount of work to be done (potentially to nothing)
  61. * speeds at least some things up.
  62. */
  63. scan:
  64. for (int i = first; i <= last; i++) {
  65. struct flash_sector *sector = bank->sectors + i;
  66. /* Only filter requests to protect the already-protected, or
  67. * to unprotect the already-unprotected. Changing from the
  68. * unknown state (-1) to a known one is unwise but allowed;
  69. * protection status is best checked first.
  70. */
  71. if (sector->is_protected != set)
  72. continue;
  73. /* Shrink this range of sectors from the start; don't overrun
  74. * the end. Also shrink from the end; don't overun the start.
  75. *
  76. * REVISIT we could handle discontiguous regions by issuing
  77. * more than one driver request. How much would that matter?
  78. */
  79. if (i == first) {
  80. updated = true;
  81. first++;
  82. } else if (i == last) {
  83. updated = true;
  84. last--;
  85. }
  86. }
  87. /* updating the range affects the tests in the scan loop above; so
  88. * re-scan, to make sure we didn't miss anything.
  89. */
  90. if (updated) {
  91. updated = false;
  92. goto scan;
  93. }
  94. /* Single sector, already protected? Nothing to do! */
  95. if (first > last)
  96. return ERROR_OK;
  97. retval = bank->driver->protect(bank, set, first, last);
  98. if (retval != ERROR_OK)
  99. {
  100. LOG_ERROR("failed setting protection for areas %d to %d (%d)", first, last, retval);
  101. }
  102. return retval;
  103. }
  104. int flash_driver_write(struct flash_bank *bank,
  105. uint8_t *buffer, uint32_t offset, uint32_t count)
  106. {
  107. int retval;
  108. retval = bank->driver->write(bank, buffer, offset, count);
  109. if (retval != ERROR_OK)
  110. {
  111. LOG_ERROR("error writing to flash at address 0x%08" PRIx32 " at offset 0x%8.8" PRIx32 " (%d)",
  112. bank->base, offset, retval);
  113. }
  114. return retval;
  115. }
  116. void flash_bank_add(struct flash_bank *bank)
  117. {
  118. /* put flash bank in linked list */
  119. unsigned bank_num = 0;
  120. if (flash_banks)
  121. {
  122. /* find last flash bank */
  123. struct flash_bank *p = flash_banks;
  124. while (NULL != p->next)
  125. {
  126. bank_num += 1;
  127. p = p->next;
  128. }
  129. p->next = bank;
  130. bank_num += 1;
  131. }
  132. else
  133. flash_banks = bank;
  134. bank->bank_number = bank_num;
  135. }
  136. struct flash_bank *flash_bank_list(void)
  137. {
  138. return flash_banks;
  139. }
  140. struct flash_bank *get_flash_bank_by_num_noprobe(int num)
  141. {
  142. struct flash_bank *p;
  143. int i = 0;
  144. for (p = flash_banks; p; p = p->next)
  145. {
  146. if (i++ == num)
  147. {
  148. return p;
  149. }
  150. }
  151. LOG_ERROR("flash bank %d does not exist", num);
  152. return NULL;
  153. }
  154. int flash_get_bank_count(void)
  155. {
  156. struct flash_bank *p;
  157. int i = 0;
  158. for (p = flash_banks; p; p = p->next)
  159. {
  160. i++;
  161. }
  162. return i;
  163. }
  164. struct flash_bank *get_flash_bank_by_name(const char *name)
  165. {
  166. unsigned requested = get_flash_name_index(name);
  167. unsigned found = 0;
  168. struct flash_bank *bank;
  169. for (bank = flash_banks; NULL != bank; bank = bank->next)
  170. {
  171. if (strcmp(bank->name, name) == 0)
  172. return bank;
  173. if (!flash_driver_name_matches(bank->driver->name, name))
  174. continue;
  175. if (++found < requested)
  176. continue;
  177. return bank;
  178. }
  179. return NULL;
  180. }
  181. struct flash_bank *get_flash_bank_by_num(int num)
  182. {
  183. struct flash_bank *p = get_flash_bank_by_num_noprobe(num);
  184. int retval;
  185. if (p == NULL)
  186. return NULL;
  187. retval = p->driver->auto_probe(p);
  188. if (retval != ERROR_OK)
  189. {
  190. LOG_ERROR("auto_probe failed %d\n", retval);
  191. return NULL;
  192. }
  193. return p;
  194. }
  195. /* lookup flash bank by address */
  196. struct flash_bank *get_flash_bank_by_addr(struct target *target, uint32_t addr)
  197. {
  198. struct flash_bank *c;
  199. /* cycle through bank list */
  200. for (c = flash_banks; c; c = c->next)
  201. {
  202. int retval;
  203. retval = c->driver->auto_probe(c);
  204. if (retval != ERROR_OK)
  205. {
  206. LOG_ERROR("auto_probe failed %d\n", retval);
  207. return NULL;
  208. }
  209. /* check whether address belongs to this flash bank */
  210. if ((addr >= c->base) && (addr <= c->base + (c->size - 1)) && target == c->target)
  211. return c;
  212. }
  213. LOG_ERROR("No flash at address 0x%08" PRIx32 "\n", addr);
  214. return NULL;
  215. }
  216. int default_flash_mem_blank_check(struct flash_bank *bank)
  217. {
  218. struct target *target = bank->target;
  219. const int buffer_size = 1024;
  220. int i;
  221. uint32_t nBytes;
  222. int retval = ERROR_OK;
  223. if (bank->target->state != TARGET_HALTED)
  224. {
  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. {
  231. uint32_t j;
  232. bank->sectors[i].is_erased = 1;
  233. for (j = 0; j < bank->sectors[i].size; j += buffer_size)
  234. {
  235. uint32_t chunk;
  236. chunk = buffer_size;
  237. if (chunk > (j - bank->sectors[i].size))
  238. {
  239. chunk = (j - bank->sectors[i].size);
  240. }
  241. retval = target_read_memory(target, bank->base + bank->sectors[i].offset + j, 4, chunk/4, buffer);
  242. if (retval != ERROR_OK)
  243. {
  244. goto done;
  245. }
  246. for (nBytes = 0; nBytes < chunk; nBytes++)
  247. {
  248. if (buffer[nBytes] != 0xFF)
  249. {
  250. bank->sectors[i].is_erased = 0;
  251. break;
  252. }
  253. }
  254. }
  255. }
  256. done:
  257. free(buffer);
  258. return retval;
  259. }
  260. int default_flash_blank_check(struct flash_bank *bank)
  261. {
  262. struct target *target = bank->target;
  263. int i;
  264. int retval;
  265. int fast_check = 0;
  266. uint32_t blank;
  267. if (bank->target->state != TARGET_HALTED)
  268. {
  269. LOG_ERROR("Target not halted");
  270. return ERROR_TARGET_NOT_HALTED;
  271. }
  272. for (i = 0; i < bank->num_sectors; i++)
  273. {
  274. uint32_t address = bank->base + bank->sectors[i].offset;
  275. uint32_t size = bank->sectors[i].size;
  276. if ((retval = target_blank_check_memory(target, address, size, &blank)) != ERROR_OK)
  277. {
  278. fast_check = 0;
  279. break;
  280. }
  281. if (blank == 0xFF)
  282. bank->sectors[i].is_erased = 1;
  283. else
  284. bank->sectors[i].is_erased = 0;
  285. fast_check = 1;
  286. }
  287. if (!fast_check)
  288. {
  289. LOG_USER("Running slow fallback erase check - add working memory");
  290. return default_flash_mem_blank_check(bank);
  291. }
  292. return ERROR_OK;
  293. }
  294. /* Manipulate given flash region, selecting the bank according to target
  295. * and address. Maps an address range to a set of sectors, and issues
  296. * the callback() on that set ... e.g. to erase or unprotect its members.
  297. *
  298. * (Note a current bad assumption: that protection operates on the same
  299. * size sectors as erase operations use.)
  300. *
  301. * The "pad_reason" parameter is a kind of boolean: when it's NULL, the
  302. * range must fit those sectors exactly. This is clearly safe; it can't
  303. * erase data which the caller said to leave alone, for example. If it's
  304. * non-NULL, rather than failing, extra data in the first and/or last
  305. * sectors will be added to the range, and that reason string is used when
  306. * warning about those additions.
  307. */
  308. static int flash_iterate_address_range(struct target *target,
  309. char *pad_reason, uint32_t addr, uint32_t length,
  310. int (*callback)(struct flash_bank *bank, int first, int last))
  311. {
  312. struct flash_bank *c;
  313. uint32_t last_addr = addr + length; /* first address AFTER end */
  314. int first = -1;
  315. int last = -1;
  316. int i;
  317. if ((c = get_flash_bank_by_addr(target, addr)) == NULL)
  318. return ERROR_FLASH_DST_OUT_OF_BANK; /* no corresponding bank found */
  319. if (c->size == 0 || c->num_sectors == 0)
  320. {
  321. LOG_ERROR("Bank is invalid");
  322. return ERROR_FLASH_BANK_INVALID;
  323. }
  324. if (length == 0)
  325. {
  326. /* special case, erase whole bank when length is zero */
  327. if (addr != c->base)
  328. {
  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. {
  337. LOG_ERROR("Flash access does not fit into bank.");
  338. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  339. }
  340. /** @todo: handle erasures that cross into adjacent banks */
  341. addr -= c->base;
  342. last_addr -= c->base;
  343. for (i = 0; i < c->num_sectors; i++)
  344. {
  345. struct flash_sector *f = c->sectors + 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. int flash_erase_address_range(struct target *target,
  410. bool pad, uint32_t addr, uint32_t length)
  411. {
  412. return flash_iterate_address_range(target, pad ? "erase" : NULL,
  413. addr, length, &flash_driver_erase);
  414. }
  415. static int flash_driver_unprotect(struct flash_bank *bank, int first, int last)
  416. {
  417. return flash_driver_protect(bank, 0, first, last);
  418. }
  419. static int flash_unlock_address_range(struct target *target, uint32_t addr, uint32_t length)
  420. {
  421. /* By default, pad to sector boundaries ... the real issue here
  422. * is that our (only) caller *permanently* removes protection,
  423. * and doesn't restore it.
  424. */
  425. return flash_iterate_address_range(target, "unprotect",
  426. addr, length, &flash_driver_unprotect);
  427. }
  428. int flash_write_unlock(struct target *target, struct image *image,
  429. uint32_t *written, int erase, bool unlock)
  430. {
  431. int retval = ERROR_OK;
  432. int section;
  433. uint32_t section_offset;
  434. struct flash_bank *c;
  435. int *padding;
  436. /* REVISIT do_pad should perhaps just be another parameter.
  437. * GDB wouldn't ever need it, since it erases separately.
  438. * But "flash write_image" commands might want that option.
  439. */
  440. bool do_pad = false;
  441. section = 0;
  442. section_offset = 0;
  443. if (written)
  444. *written = 0;
  445. if (erase)
  446. {
  447. /* assume all sectors need erasing - stops any problems
  448. * when flash_write is called multiple times */
  449. flash_set_dirty();
  450. }
  451. /* allocate padding array */
  452. padding = calloc(image->num_sections, sizeof(*padding));
  453. /* loop until we reach end of the image */
  454. while (section < image->num_sections)
  455. {
  456. uint32_t buffer_size;
  457. uint8_t *buffer;
  458. int section_first;
  459. int section_last;
  460. uint32_t run_address = image->sections[section].base_address + section_offset;
  461. uint32_t run_size = image->sections[section].size - section_offset;
  462. int pad_bytes = 0;
  463. if (image->sections[section].size == 0)
  464. {
  465. LOG_WARNING("empty section %d", section);
  466. section++;
  467. section_offset = 0;
  468. continue;
  469. }
  470. /* find the corresponding flash bank */
  471. if ((c = get_flash_bank_by_addr(target, run_address)) == NULL)
  472. {
  473. section++; /* and skip it */
  474. section_offset = 0;
  475. continue;
  476. }
  477. /* collect consecutive sections which fall into the same bank */
  478. section_first = section;
  479. section_last = section;
  480. padding[section] = 0;
  481. while ((run_address + run_size - 1 < c->base + c->size - 1)
  482. && (section_last + 1 < image->num_sections))
  483. {
  484. if (image->sections[section_last + 1].base_address < (run_address + run_size))
  485. {
  486. LOG_DEBUG("section %d out of order "
  487. "(surprising, but supported)",
  488. section_last + 1);
  489. /* REVISIT this can break with autoerase ...
  490. * clobbering data after it's written.
  491. */
  492. break;
  493. }
  494. /* FIXME This needlessly touches sectors BETWEEN the
  495. * sections it's writing. Without auto erase, it just
  496. * writes ones. That WILL INVALIDATE data in cases
  497. * like Stellaris Tempest chips, corrupting internal
  498. * ECC codes; and at least FreeScale suggests issues
  499. * with that approach (in HC11 documentation).
  500. *
  501. * With auto erase enabled, data in those sectors will
  502. * be needlessly destroyed; and some of the limited
  503. * number of flash erase cycles will be wasted...
  504. *
  505. * In both cases, the extra writes slow things down.
  506. */
  507. /* if we have multiple sections within our image,
  508. * flash programming could fail due to alignment issues
  509. * attempt to rebuild a consecutive buffer for the flash loader */
  510. pad_bytes = (image->sections[section_last + 1].base_address) - (run_address + run_size);
  511. if ((run_address + run_size + pad_bytes) > (c->base + c->size))
  512. break;
  513. padding[section_last] = pad_bytes;
  514. run_size += image->sections[++section_last].size;
  515. run_size += pad_bytes;
  516. LOG_INFO("Padding image section %d with %d bytes", section_last-1, pad_bytes);
  517. }
  518. /* fit the run into bank constraints */
  519. if (run_address + run_size - 1 > c->base + c->size - 1)
  520. {
  521. /* REVISIT isn't this superfluous, given the while()
  522. * loop conditions above??
  523. */
  524. LOG_WARNING("writing %d bytes only - as image section is %d bytes and bank is only %d bytes", \
  525. (int)(c->base + c->size - run_address), (int)(run_size), (int)(c->size));
  526. run_size = c->base + c->size - run_address;
  527. }
  528. /* If we're applying any sector automagic, then pad this
  529. * (maybe-combined) segment to the end of its last sector.
  530. */
  531. if (unlock || erase) {
  532. int sector;
  533. uint32_t offset_start = run_address - c->base;
  534. uint32_t offset_end = offset_start + run_size;
  535. uint32_t end = offset_end, delta;
  536. for (sector = 0; sector < c->num_sectors; sector++) {
  537. end = c->sectors[sector].offset
  538. + c->sectors[sector].size;
  539. if (offset_end <= end)
  540. break;
  541. }
  542. delta = end - offset_end;
  543. padding[section_last] += delta;
  544. run_size += delta;
  545. }
  546. /* allocate buffer */
  547. buffer = malloc(run_size);
  548. buffer_size = 0;
  549. /* read sections to the buffer */
  550. while (buffer_size < run_size)
  551. {
  552. size_t size_read;
  553. size_read = run_size - buffer_size;
  554. if (size_read > image->sections[section].size - section_offset)
  555. size_read = image->sections[section].size - section_offset;
  556. if ((retval = image_read_section(image, section, section_offset,
  557. size_read, buffer + buffer_size, &size_read)) != ERROR_OK || size_read == 0)
  558. {
  559. free(buffer);
  560. free(padding);
  561. return retval;
  562. }
  563. /* see if we need to pad the section */
  564. while (padding[section]--)
  565. (buffer + buffer_size)[size_read++] = 0xff;
  566. buffer_size += size_read;
  567. section_offset += size_read;
  568. if (section_offset >= image->sections[section].size)
  569. {
  570. section++;
  571. section_offset = 0;
  572. }
  573. }
  574. retval = ERROR_OK;
  575. if (unlock)
  576. {
  577. retval = flash_unlock_address_range(target, run_address, run_size);
  578. }
  579. if (retval == ERROR_OK)
  580. {
  581. if (erase)
  582. {
  583. /* calculate and erase sectors */
  584. retval = flash_erase_address_range(target,
  585. do_pad, run_address, run_size);
  586. }
  587. }
  588. if (retval == ERROR_OK)
  589. {
  590. /* write flash sectors */
  591. retval = flash_driver_write(c, buffer, run_address - c->base, run_size);
  592. }
  593. free(buffer);
  594. if (retval != ERROR_OK)
  595. {
  596. free(padding);
  597. return retval; /* abort operation */
  598. }
  599. if (written != NULL)
  600. *written += run_size; /* add run size to total written counter */
  601. }
  602. free(padding);
  603. return retval;
  604. }
  605. int flash_write(struct target *target, struct image *image,
  606. uint32_t *written, int erase)
  607. {
  608. return flash_write_unlock(target, image, written, erase, false);
  609. }
  610. /**
  611. * Invalidates cached flash state which a target can change as it runs.
  612. *
  613. * @param target The target being resumed
  614. *
  615. * OpenOCD caches some flash state for brief periods. For example, a sector
  616. * that is protected must be unprotected before OpenOCD tries to write it,
  617. * Also, a sector that's not erased must be erased before it's written.
  618. *
  619. * As a rule, OpenOCD and target firmware can both modify the flash, so when
  620. * a target starts running, OpenOCD needs to invalidate its cached state.
  621. */
  622. void nor_resume(struct target *target)
  623. {
  624. struct flash_bank *bank;
  625. for (bank = flash_banks; bank; bank = bank->next) {
  626. int i;
  627. if (bank->target != target)
  628. continue;
  629. for (i = 0; i < bank->num_sectors; i++) {
  630. struct flash_sector *sector = bank->sectors + i;
  631. sector->is_erased = -1;
  632. sector->is_protected = -1;
  633. }
  634. }
  635. }