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.
 
 
 
 
 
 

764 lines
20 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. 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. int retval;
  199. retval = c->driver->auto_probe(c);
  200. if (retval != ERROR_OK) {
  201. LOG_ERROR("auto_probe failed");
  202. return retval;
  203. }
  204. /* check whether address belongs to this flash bank */
  205. if ((addr >= c->base) && (addr <= c->base + (c->size - 1)) && target == c->target) {
  206. *result_bank = c;
  207. return ERROR_OK;
  208. }
  209. }
  210. *result_bank = NULL;
  211. if (check) {
  212. LOG_ERROR("No flash at address 0x%08" PRIx32, addr);
  213. return ERROR_FAIL;
  214. }
  215. return ERROR_OK;
  216. }
  217. static int default_flash_mem_blank_check(struct flash_bank *bank)
  218. {
  219. struct target *target = bank->target;
  220. const int buffer_size = 1024;
  221. int i;
  222. uint32_t nBytes;
  223. int retval = ERROR_OK;
  224. if (bank->target->state != TARGET_HALTED) {
  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. uint32_t j;
  231. bank->sectors[i].is_erased = 1;
  232. for (j = 0; j < bank->sectors[i].size; j += buffer_size) {
  233. uint32_t chunk;
  234. chunk = buffer_size;
  235. if (chunk > (j - bank->sectors[i].size))
  236. chunk = (j - bank->sectors[i].size);
  237. retval = target_read_memory(target,
  238. bank->base + bank->sectors[i].offset + j,
  239. 4,
  240. chunk/4,
  241. buffer);
  242. if (retval != ERROR_OK)
  243. goto done;
  244. for (nBytes = 0; nBytes < chunk; nBytes++) {
  245. if (buffer[nBytes] != 0xFF) {
  246. bank->sectors[i].is_erased = 0;
  247. break;
  248. }
  249. }
  250. }
  251. }
  252. done:
  253. free(buffer);
  254. return retval;
  255. }
  256. int default_flash_blank_check(struct flash_bank *bank)
  257. {
  258. struct target *target = bank->target;
  259. int i;
  260. int retval;
  261. int fast_check = 0;
  262. uint32_t blank;
  263. if (bank->target->state != TARGET_HALTED) {
  264. LOG_ERROR("Target not halted");
  265. return ERROR_TARGET_NOT_HALTED;
  266. }
  267. for (i = 0; i < bank->num_sectors; i++) {
  268. uint32_t address = bank->base + bank->sectors[i].offset;
  269. uint32_t size = bank->sectors[i].size;
  270. retval = target_blank_check_memory(target, address, size, &blank);
  271. if (retval != ERROR_OK) {
  272. fast_check = 0;
  273. break;
  274. }
  275. if (blank == 0xFF)
  276. bank->sectors[i].is_erased = 1;
  277. else
  278. bank->sectors[i].is_erased = 0;
  279. fast_check = 1;
  280. }
  281. if (!fast_check) {
  282. LOG_USER("Running slow fallback erase check - add working memory");
  283. return default_flash_mem_blank_check(bank);
  284. }
  285. return ERROR_OK;
  286. }
  287. /* Manipulate given flash region, selecting the bank according to target
  288. * and address. Maps an address range to a set of sectors, and issues
  289. * the callback() on that set ... e.g. to erase or unprotect its members.
  290. *
  291. * (Note a current bad assumption: that protection operates on the same
  292. * size sectors as erase operations use.)
  293. *
  294. * The "pad_reason" parameter is a kind of boolean: when it's NULL, the
  295. * range must fit those sectors exactly. This is clearly safe; it can't
  296. * erase data which the caller said to leave alone, for example. If it's
  297. * non-NULL, rather than failing, extra data in the first and/or last
  298. * sectors will be added to the range, and that reason string is used when
  299. * warning about those additions.
  300. */
  301. static int flash_iterate_address_range_inner(struct target *target,
  302. char *pad_reason, uint32_t addr, uint32_t length,
  303. int (*callback)(struct flash_bank *bank, int first, int last))
  304. {
  305. struct flash_bank *c;
  306. uint32_t last_addr = addr + length; /* first address AFTER end */
  307. int first = -1;
  308. int last = -1;
  309. int i;
  310. int retval = get_flash_bank_by_addr(target, addr, true, &c);
  311. if (retval != ERROR_OK)
  312. return retval;
  313. if (c->size == 0 || c->num_sectors == 0) {
  314. LOG_ERROR("Bank is invalid");
  315. return ERROR_FLASH_BANK_INVALID;
  316. }
  317. if (length == 0) {
  318. /* special case, erase whole bank when length is zero */
  319. if (addr != c->base) {
  320. LOG_ERROR("Whole bank access must start at beginning of bank.");
  321. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  322. }
  323. return callback(c, 0, c->num_sectors - 1);
  324. }
  325. /* check whether it all fits in this bank */
  326. if (addr + length - 1 > c->base + c->size - 1) {
  327. LOG_ERROR("Flash access does not fit into bank.");
  328. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  329. }
  330. /** @todo: handle erasures that cross into adjacent banks */
  331. addr -= c->base;
  332. last_addr -= c->base;
  333. for (i = 0; i < c->num_sectors; i++) {
  334. struct flash_sector *f = c->sectors + i;
  335. uint32_t end = f->offset + f->size;
  336. /* start only on a sector boundary */
  337. if (first < 0) {
  338. /* scanned past the first sector? */
  339. if (addr < f->offset)
  340. break;
  341. /* is this the first sector? */
  342. if (addr == f->offset)
  343. first = i;
  344. /* Does this need head-padding? If so, pad and warn;
  345. * or else force an error.
  346. *
  347. * Such padding can make trouble, since *WE* can't
  348. * ever know if that data was in use. The warning
  349. * should help users sort out messes later.
  350. */
  351. else if (addr < end && pad_reason) {
  352. /* FIXME say how many bytes (e.g. 80 KB) */
  353. LOG_WARNING("Adding extra %s range, "
  354. "%#8.8x to %#8.8x",
  355. pad_reason,
  356. (unsigned) f->offset,
  357. (unsigned) addr - 1);
  358. first = i;
  359. } else
  360. continue;
  361. }
  362. /* is this (also?) the last sector? */
  363. if (last_addr == end) {
  364. last = i;
  365. break;
  366. }
  367. /* Does this need tail-padding? If so, pad and warn;
  368. * or else force an error.
  369. */
  370. if (last_addr < end && pad_reason) {
  371. /* FIXME say how many bytes (e.g. 80 KB) */
  372. LOG_WARNING("Adding extra %s range, "
  373. "%#8.8x to %#8.8x",
  374. pad_reason,
  375. (unsigned) last_addr,
  376. (unsigned) end - 1);
  377. last = i;
  378. break;
  379. }
  380. /* MUST finish on a sector boundary */
  381. if (last_addr <= f->offset)
  382. break;
  383. }
  384. /* invalid start or end address? */
  385. if (first == -1 || last == -1) {
  386. LOG_ERROR("address range 0x%8.8x .. 0x%8.8x "
  387. "is not sector-aligned",
  388. (unsigned) (c->base + addr),
  389. (unsigned) (c->base + last_addr - 1));
  390. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  391. }
  392. /* The NOR driver may trim this range down, based on what
  393. * sectors are already erased/unprotected. GDB currently
  394. * blocks such optimizations.
  395. */
  396. return callback(c, first, last);
  397. }
  398. /* The inner fn only handles a single bank, we could be spanning
  399. * multiple chips.
  400. */
  401. static int flash_iterate_address_range(struct target *target,
  402. char *pad_reason, uint32_t addr, uint32_t length,
  403. int (*callback)(struct flash_bank *bank, int first, int last))
  404. {
  405. struct flash_bank *c;
  406. int retval = ERROR_OK;
  407. /* Danger! zero-length iterations means entire bank! */
  408. do {
  409. retval = get_flash_bank_by_addr(target, addr, true, &c);
  410. if (retval != ERROR_OK)
  411. return retval;
  412. uint32_t cur_length = length;
  413. /* check whether it all fits in this bank */
  414. if (addr + length - 1 > c->base + c->size - 1) {
  415. LOG_DEBUG("iterating over more than one flash bank.");
  416. cur_length = c->base + c->size - addr;
  417. }
  418. retval = flash_iterate_address_range_inner(target,
  419. pad_reason, addr, cur_length,
  420. callback);
  421. if (retval != ERROR_OK)
  422. break;
  423. length -= cur_length;
  424. addr += cur_length;
  425. } while (length > 0);
  426. return retval;
  427. }
  428. int flash_erase_address_range(struct target *target,
  429. bool pad, uint32_t addr, uint32_t length)
  430. {
  431. return flash_iterate_address_range(target, pad ? "erase" : NULL,
  432. addr, length, &flash_driver_erase);
  433. }
  434. static int flash_driver_unprotect(struct flash_bank *bank, int first, int last)
  435. {
  436. return flash_driver_protect(bank, 0, first, last);
  437. }
  438. int flash_unlock_address_range(struct target *target, uint32_t addr, uint32_t length)
  439. {
  440. /* By default, pad to sector boundaries ... the real issue here
  441. * is that our (only) caller *permanently* removes protection,
  442. * and doesn't restore it.
  443. */
  444. return flash_iterate_address_range(target, "unprotect",
  445. addr, length, &flash_driver_unprotect);
  446. }
  447. static int compare_section(const void *a, const void *b)
  448. {
  449. struct imagesection *b1, *b2;
  450. b1 = *((struct imagesection **)a);
  451. b2 = *((struct imagesection **)b);
  452. if (b1->base_address == b2->base_address)
  453. return 0;
  454. else if (b1->base_address > b2->base_address)
  455. return 1;
  456. else
  457. return -1;
  458. }
  459. int flash_write_unlock(struct target *target, struct image *image,
  460. uint32_t *written, int erase, bool unlock)
  461. {
  462. int retval = ERROR_OK;
  463. int section;
  464. uint32_t section_offset;
  465. struct flash_bank *c;
  466. int *padding;
  467. section = 0;
  468. section_offset = 0;
  469. if (written)
  470. *written = 0;
  471. if (erase) {
  472. /* assume all sectors need erasing - stops any problems
  473. * when flash_write is called multiple times */
  474. flash_set_dirty();
  475. }
  476. /* allocate padding array */
  477. padding = calloc(image->num_sections, sizeof(*padding));
  478. /* This fn requires all sections to be in ascending order of addresses,
  479. * whereas an image can have sections out of order. */
  480. struct imagesection **sections = malloc(sizeof(struct imagesection *) *
  481. image->num_sections);
  482. int i;
  483. for (i = 0; i < image->num_sections; i++)
  484. sections[i] = &image->sections[i];
  485. qsort(sections, image->num_sections, sizeof(struct imagesection *),
  486. compare_section);
  487. /* loop until we reach end of the image */
  488. while (section < image->num_sections) {
  489. uint32_t buffer_size;
  490. uint8_t *buffer;
  491. int section_last;
  492. uint32_t run_address = sections[section]->base_address + section_offset;
  493. uint32_t run_size = sections[section]->size - section_offset;
  494. int pad_bytes = 0;
  495. if (sections[section]->size == 0) {
  496. LOG_WARNING("empty section %d", section);
  497. section++;
  498. section_offset = 0;
  499. continue;
  500. }
  501. /* find the corresponding flash bank */
  502. retval = get_flash_bank_by_addr(target, run_address, false, &c);
  503. if (retval != ERROR_OK)
  504. goto done;
  505. if (c == NULL) {
  506. LOG_WARNING("no flash bank found for address %x", run_address);
  507. section++; /* and skip it */
  508. section_offset = 0;
  509. continue;
  510. }
  511. /* collect consecutive sections which fall into the same bank */
  512. section_last = section;
  513. padding[section] = 0;
  514. while ((run_address + run_size - 1 < c->base + c->size - 1) &&
  515. (section_last + 1 < image->num_sections)) {
  516. /* sections are sorted */
  517. assert(sections[section_last + 1]->base_address >= c->base);
  518. if (sections[section_last + 1]->base_address >= (c->base + c->size)) {
  519. /* Done with this bank */
  520. break;
  521. }
  522. /* FIXME This needlessly touches sectors BETWEEN the
  523. * sections it's writing. Without auto erase, it just
  524. * writes ones. That WILL INVALIDATE data in cases
  525. * like Stellaris Tempest chips, corrupting internal
  526. * ECC codes; and at least FreeScale suggests issues
  527. * with that approach (in HC11 documentation).
  528. *
  529. * With auto erase enabled, data in those sectors will
  530. * be needlessly destroyed; and some of the limited
  531. * number of flash erase cycles will be wasted...
  532. *
  533. * In both cases, the extra writes slow things down.
  534. */
  535. /* if we have multiple sections within our image,
  536. * flash programming could fail due to alignment issues
  537. * attempt to rebuild a consecutive buffer for the flash loader */
  538. pad_bytes = (sections[section_last + 1]->base_address) - (run_address + run_size);
  539. padding[section_last] = pad_bytes;
  540. run_size += sections[++section_last]->size;
  541. run_size += pad_bytes;
  542. if (pad_bytes > 0)
  543. LOG_INFO("Padding image section %d with %d bytes",
  544. section_last-1,
  545. pad_bytes);
  546. }
  547. if (run_address + run_size - 1 > c->base + c->size - 1) {
  548. /* If we have more than one flash chip back to back, then we limit
  549. * the current write operation to the current chip.
  550. */
  551. LOG_DEBUG("Truncate flash run size to the current flash chip.");
  552. run_size = c->base + c->size - run_address;
  553. assert(run_size > 0);
  554. }
  555. /* If we're applying any sector automagic, then pad this
  556. * (maybe-combined) segment to the end of its last sector.
  557. */
  558. if (unlock || erase) {
  559. int sector;
  560. uint32_t offset_start = run_address - c->base;
  561. uint32_t offset_end = offset_start + run_size;
  562. uint32_t end = offset_end, delta;
  563. for (sector = 0; sector < c->num_sectors; sector++) {
  564. end = c->sectors[sector].offset
  565. + c->sectors[sector].size;
  566. if (offset_end <= end)
  567. break;
  568. }
  569. delta = end - offset_end;
  570. padding[section_last] += delta;
  571. run_size += delta;
  572. }
  573. /* allocate buffer */
  574. buffer = malloc(run_size);
  575. if (buffer == NULL) {
  576. LOG_ERROR("Out of memory for flash bank buffer");
  577. retval = ERROR_FAIL;
  578. goto done;
  579. }
  580. buffer_size = 0;
  581. /* read sections to the buffer */
  582. while (buffer_size < run_size) {
  583. size_t size_read;
  584. size_read = run_size - buffer_size;
  585. if (size_read > sections[section]->size - section_offset)
  586. size_read = sections[section]->size - section_offset;
  587. /* KLUDGE!
  588. *
  589. * #¤%#"%¤% we have to figure out the section # from the sorted
  590. * list of pointers to sections to invoke image_read_section()...
  591. */
  592. intptr_t diff = (intptr_t)sections[section] - (intptr_t)image->sections;
  593. int t_section_num = diff / sizeof(struct imagesection);
  594. LOG_DEBUG("image_read_section: section = %d, t_section_num = %d, "
  595. "section_offset = %d, buffer_size = %d, size_read = %d",
  596. (int)section, (int)t_section_num, (int)section_offset,
  597. (int)buffer_size, (int)size_read);
  598. retval = image_read_section(image, t_section_num, section_offset,
  599. size_read, buffer + buffer_size, &size_read);
  600. if (retval != ERROR_OK || size_read == 0) {
  601. free(buffer);
  602. goto done;
  603. }
  604. /* see if we need to pad the section */
  605. while (padding[section]--)
  606. (buffer + buffer_size)[size_read++] = 0xff;
  607. buffer_size += size_read;
  608. section_offset += size_read;
  609. if (section_offset >= sections[section]->size) {
  610. section++;
  611. section_offset = 0;
  612. }
  613. }
  614. retval = ERROR_OK;
  615. if (unlock)
  616. retval = flash_unlock_address_range(target, run_address, run_size);
  617. if (retval == ERROR_OK) {
  618. if (erase) {
  619. /* calculate and erase sectors */
  620. retval = flash_erase_address_range(target,
  621. true, run_address, run_size);
  622. }
  623. }
  624. if (retval == ERROR_OK) {
  625. /* write flash sectors */
  626. retval = flash_driver_write(c, buffer, run_address - c->base, run_size);
  627. }
  628. free(buffer);
  629. if (retval != ERROR_OK) {
  630. /* abort operation */
  631. goto done;
  632. }
  633. if (written != NULL)
  634. *written += run_size; /* add run size to total written counter */
  635. }
  636. done:
  637. free(sections);
  638. free(padding);
  639. return retval;
  640. }
  641. int flash_write(struct target *target, struct image *image,
  642. uint32_t *written, int erase)
  643. {
  644. return flash_write_unlock(target, image, written, erase, false);
  645. }