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.
 
 
 
 
 
 

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