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.
 
 
 
 
 
 

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