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.
 
 
 
 
 
 

1246 lines
33 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2008 by Spencer Oliver *
  6. * spen@spen-soft.co.uk *
  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 "stm32x.h"
  27. #include "armv7m.h"
  28. #include "binarybuffer.h"
  29. static int stm32x_register_commands(struct command_context_s *cmd_ctx);
  30. static int stm32x_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
  31. static int stm32x_erase(struct flash_bank_s *bank, int first, int last);
  32. static int stm32x_protect(struct flash_bank_s *bank, int set, int first, int last);
  33. static int stm32x_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count);
  34. static int stm32x_probe(struct flash_bank_s *bank);
  35. static int stm32x_auto_probe(struct flash_bank_s *bank);
  36. //static int stm32x_handle_part_id_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  37. static int stm32x_protect_check(struct flash_bank_s *bank);
  38. static int stm32x_info(struct flash_bank_s *bank, char *buf, int buf_size);
  39. static int stm32x_handle_lock_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  40. static int stm32x_handle_unlock_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  41. static int stm32x_handle_options_read_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  42. static int stm32x_handle_options_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  43. static int stm32x_handle_mass_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  44. static int stm32x_mass_erase(struct flash_bank_s *bank);
  45. flash_driver_t stm32x_flash =
  46. {
  47. .name = "stm32x",
  48. .register_commands = stm32x_register_commands,
  49. .flash_bank_command = stm32x_flash_bank_command,
  50. .erase = stm32x_erase,
  51. .protect = stm32x_protect,
  52. .write = stm32x_write,
  53. .probe = stm32x_probe,
  54. .auto_probe = stm32x_auto_probe,
  55. .erase_check = default_flash_mem_blank_check,
  56. .protect_check = stm32x_protect_check,
  57. .info = stm32x_info
  58. };
  59. static int stm32x_register_commands(struct command_context_s *cmd_ctx)
  60. {
  61. command_t *stm32x_cmd = register_command(cmd_ctx, NULL, "stm32x", NULL, COMMAND_ANY, "stm32x flash specific commands");
  62. register_command(cmd_ctx, stm32x_cmd, "lock", stm32x_handle_lock_command, COMMAND_EXEC,
  63. "lock device");
  64. register_command(cmd_ctx, stm32x_cmd, "unlock", stm32x_handle_unlock_command, COMMAND_EXEC,
  65. "unlock protected device");
  66. register_command(cmd_ctx, stm32x_cmd, "mass_erase", stm32x_handle_mass_erase_command, COMMAND_EXEC,
  67. "mass erase device");
  68. register_command(cmd_ctx, stm32x_cmd, "options_read", stm32x_handle_options_read_command, COMMAND_EXEC,
  69. "read device option bytes");
  70. register_command(cmd_ctx, stm32x_cmd, "options_write", stm32x_handle_options_write_command, COMMAND_EXEC,
  71. "write device option bytes");
  72. return ERROR_OK;
  73. }
  74. /* flash bank stm32x <base> <size> 0 0 <target#>
  75. */
  76. static int stm32x_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank)
  77. {
  78. stm32x_flash_bank_t *stm32x_info;
  79. if (argc < 6)
  80. {
  81. LOG_WARNING("incomplete flash_bank stm32x configuration");
  82. return ERROR_FLASH_BANK_INVALID;
  83. }
  84. stm32x_info = malloc(sizeof(stm32x_flash_bank_t));
  85. bank->driver_priv = stm32x_info;
  86. stm32x_info->write_algorithm = NULL;
  87. stm32x_info->probed = 0;
  88. return ERROR_OK;
  89. }
  90. static uint32_t stm32x_get_flash_status(flash_bank_t *bank)
  91. {
  92. target_t *target = bank->target;
  93. uint32_t status;
  94. target_read_u32(target, STM32_FLASH_SR, &status);
  95. return status;
  96. }
  97. static uint32_t stm32x_wait_status_busy(flash_bank_t *bank, int timeout)
  98. {
  99. target_t *target = bank->target;
  100. uint32_t status;
  101. /* wait for busy to clear */
  102. while (((status = stm32x_get_flash_status(bank)) & FLASH_BSY) && (timeout-- > 0))
  103. {
  104. LOG_DEBUG("status: 0x%" PRIx32 "", status);
  105. alive_sleep(1);
  106. }
  107. /* Clear but report errors */
  108. if (status & (FLASH_WRPRTERR | FLASH_PGERR))
  109. {
  110. target_write_u32(target, STM32_FLASH_SR, FLASH_WRPRTERR | FLASH_PGERR);
  111. }
  112. return status;
  113. }
  114. static int stm32x_read_options(struct flash_bank_s *bank)
  115. {
  116. uint32_t optiondata;
  117. stm32x_flash_bank_t *stm32x_info = NULL;
  118. target_t *target = bank->target;
  119. stm32x_info = bank->driver_priv;
  120. /* read current option bytes */
  121. target_read_u32(target, STM32_FLASH_OBR, &optiondata);
  122. stm32x_info->option_bytes.user_options = (uint16_t)0xFFF8 | ((optiondata >> 2) & 0x07);
  123. stm32x_info->option_bytes.RDP = (optiondata & (1 << OPT_READOUT)) ? 0xFFFF : 0x5AA5;
  124. if (optiondata & (1 << OPT_READOUT))
  125. LOG_INFO("Device Security Bit Set");
  126. /* each bit refers to a 4bank protection */
  127. target_read_u32(target, STM32_FLASH_WRPR, &optiondata);
  128. stm32x_info->option_bytes.protection[0] = (uint16_t)optiondata;
  129. stm32x_info->option_bytes.protection[1] = (uint16_t)(optiondata >> 8);
  130. stm32x_info->option_bytes.protection[2] = (uint16_t)(optiondata >> 16);
  131. stm32x_info->option_bytes.protection[3] = (uint16_t)(optiondata >> 24);
  132. return ERROR_OK;
  133. }
  134. static int stm32x_erase_options(struct flash_bank_s *bank)
  135. {
  136. stm32x_flash_bank_t *stm32x_info = NULL;
  137. target_t *target = bank->target;
  138. uint32_t status;
  139. stm32x_info = bank->driver_priv;
  140. /* read current options */
  141. stm32x_read_options(bank);
  142. /* unlock flash registers */
  143. target_write_u32(target, STM32_FLASH_KEYR, KEY1);
  144. target_write_u32(target, STM32_FLASH_KEYR, KEY2);
  145. /* unlock option flash registers */
  146. target_write_u32(target, STM32_FLASH_OPTKEYR, KEY1);
  147. target_write_u32(target, STM32_FLASH_OPTKEYR, KEY2);
  148. /* erase option bytes */
  149. target_write_u32(target, STM32_FLASH_CR, FLASH_OPTER | FLASH_OPTWRE);
  150. target_write_u32(target, STM32_FLASH_CR, FLASH_OPTER | FLASH_STRT | FLASH_OPTWRE);
  151. status = stm32x_wait_status_busy(bank, 10);
  152. if (status & FLASH_WRPRTERR)
  153. return ERROR_FLASH_OPERATION_FAILED;
  154. if (status & FLASH_PGERR)
  155. return ERROR_FLASH_OPERATION_FAILED;
  156. /* clear readout protection and complementary option bytes
  157. * this will also force a device unlock if set */
  158. stm32x_info->option_bytes.RDP = 0x5AA5;
  159. return ERROR_OK;
  160. }
  161. static int stm32x_write_options(struct flash_bank_s *bank)
  162. {
  163. stm32x_flash_bank_t *stm32x_info = NULL;
  164. target_t *target = bank->target;
  165. uint32_t status;
  166. stm32x_info = bank->driver_priv;
  167. /* unlock flash registers */
  168. target_write_u32(target, STM32_FLASH_KEYR, KEY1);
  169. target_write_u32(target, STM32_FLASH_KEYR, KEY2);
  170. /* unlock option flash registers */
  171. target_write_u32(target, STM32_FLASH_OPTKEYR, KEY1);
  172. target_write_u32(target, STM32_FLASH_OPTKEYR, KEY2);
  173. /* program option bytes */
  174. target_write_u32(target, STM32_FLASH_CR, FLASH_OPTPG | FLASH_OPTWRE);
  175. /* write user option byte */
  176. target_write_u16(target, STM32_OB_USER, stm32x_info->option_bytes.user_options);
  177. status = stm32x_wait_status_busy(bank, 10);
  178. if (status & FLASH_WRPRTERR)
  179. return ERROR_FLASH_OPERATION_FAILED;
  180. if (status & FLASH_PGERR)
  181. return ERROR_FLASH_OPERATION_FAILED;
  182. /* write protection byte 1 */
  183. target_write_u16(target, STM32_OB_WRP0, stm32x_info->option_bytes.protection[0]);
  184. status = stm32x_wait_status_busy(bank, 10);
  185. if (status & FLASH_WRPRTERR)
  186. return ERROR_FLASH_OPERATION_FAILED;
  187. if (status & FLASH_PGERR)
  188. return ERROR_FLASH_OPERATION_FAILED;
  189. /* write protection byte 2 */
  190. target_write_u16(target, STM32_OB_WRP1, stm32x_info->option_bytes.protection[1]);
  191. status = stm32x_wait_status_busy(bank, 10);
  192. if (status & FLASH_WRPRTERR)
  193. return ERROR_FLASH_OPERATION_FAILED;
  194. if (status & FLASH_PGERR)
  195. return ERROR_FLASH_OPERATION_FAILED;
  196. /* write protection byte 3 */
  197. target_write_u16(target, STM32_OB_WRP2, stm32x_info->option_bytes.protection[2]);
  198. status = stm32x_wait_status_busy(bank, 10);
  199. if (status & FLASH_WRPRTERR)
  200. return ERROR_FLASH_OPERATION_FAILED;
  201. if (status & FLASH_PGERR)
  202. return ERROR_FLASH_OPERATION_FAILED;
  203. /* write protection byte 4 */
  204. target_write_u16(target, STM32_OB_WRP3, stm32x_info->option_bytes.protection[3]);
  205. status = stm32x_wait_status_busy(bank, 10);
  206. if (status & FLASH_WRPRTERR)
  207. return ERROR_FLASH_OPERATION_FAILED;
  208. if (status & FLASH_PGERR)
  209. return ERROR_FLASH_OPERATION_FAILED;
  210. /* write readout protection bit */
  211. target_write_u16(target, STM32_OB_RDP, stm32x_info->option_bytes.RDP);
  212. status = stm32x_wait_status_busy(bank, 10);
  213. if (status & FLASH_WRPRTERR)
  214. return ERROR_FLASH_OPERATION_FAILED;
  215. if (status & FLASH_PGERR)
  216. return ERROR_FLASH_OPERATION_FAILED;
  217. target_write_u32(target, STM32_FLASH_CR, FLASH_LOCK);
  218. return ERROR_OK;
  219. }
  220. static int stm32x_protect_check(struct flash_bank_s *bank)
  221. {
  222. target_t *target = bank->target;
  223. stm32x_flash_bank_t *stm32x_info = bank->driver_priv;
  224. uint32_t protection;
  225. int i, s;
  226. int num_bits;
  227. int set;
  228. if (target->state != TARGET_HALTED)
  229. {
  230. LOG_ERROR("Target not halted");
  231. return ERROR_TARGET_NOT_HALTED;
  232. }
  233. /* medium density - each bit refers to a 4bank protection
  234. * high density - each bit refers to a 2bank protection */
  235. target_read_u32(target, STM32_FLASH_WRPR, &protection);
  236. /* medium density - each protection bit is for 4 * 1K pages
  237. * high density - each protection bit is for 2 * 2K pages */
  238. num_bits = (bank->num_sectors / stm32x_info->ppage_size);
  239. if (stm32x_info->ppage_size == 2)
  240. {
  241. /* high density flash/connectivity line protection */
  242. set = 1;
  243. if (protection & (1 << 31))
  244. set = 0;
  245. /* bit 31 controls sector 62 - 255 protection for high density
  246. * bit 31 controls sector 62 - 127 protection for connectivity line */
  247. for (s = 62; s < bank->num_sectors; s++)
  248. {
  249. bank->sectors[s].is_protected = set;
  250. }
  251. if (bank->num_sectors > 61)
  252. num_bits = 31;
  253. for (i = 0; i < num_bits; i++)
  254. {
  255. set = 1;
  256. if (protection & (1 << i))
  257. set = 0;
  258. for (s = 0; s < stm32x_info->ppage_size; s++)
  259. bank->sectors[(i * stm32x_info->ppage_size) + s].is_protected = set;
  260. }
  261. }
  262. else
  263. {
  264. /* low/medium density flash protection */
  265. for (i = 0; i < num_bits; i++)
  266. {
  267. set = 1;
  268. if (protection & (1 << i))
  269. set = 0;
  270. for (s = 0; s < stm32x_info->ppage_size; s++)
  271. bank->sectors[(i * stm32x_info->ppage_size) + s].is_protected = set;
  272. }
  273. }
  274. return ERROR_OK;
  275. }
  276. static int stm32x_erase(struct flash_bank_s *bank, int first, int last)
  277. {
  278. target_t *target = bank->target;
  279. int i;
  280. uint32_t status;
  281. if (bank->target->state != TARGET_HALTED)
  282. {
  283. LOG_ERROR("Target not halted");
  284. return ERROR_TARGET_NOT_HALTED;
  285. }
  286. if ((first == 0) && (last == (bank->num_sectors - 1)))
  287. {
  288. return stm32x_mass_erase(bank);
  289. }
  290. /* unlock flash registers */
  291. target_write_u32(target, STM32_FLASH_KEYR, KEY1);
  292. target_write_u32(target, STM32_FLASH_KEYR, KEY2);
  293. for (i = first; i <= last; i++)
  294. {
  295. target_write_u32(target, STM32_FLASH_CR, FLASH_PER);
  296. target_write_u32(target, STM32_FLASH_AR, bank->base + bank->sectors[i].offset);
  297. target_write_u32(target, STM32_FLASH_CR, FLASH_PER | FLASH_STRT);
  298. status = stm32x_wait_status_busy(bank, 10);
  299. if (status & FLASH_WRPRTERR)
  300. return ERROR_FLASH_OPERATION_FAILED;
  301. if (status & FLASH_PGERR)
  302. return ERROR_FLASH_OPERATION_FAILED;
  303. bank->sectors[i].is_erased = 1;
  304. }
  305. target_write_u32(target, STM32_FLASH_CR, FLASH_LOCK);
  306. return ERROR_OK;
  307. }
  308. static int stm32x_protect(struct flash_bank_s *bank, int set, int first, int last)
  309. {
  310. stm32x_flash_bank_t *stm32x_info = NULL;
  311. target_t *target = bank->target;
  312. uint16_t prot_reg[4] = {0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF};
  313. int i, reg, bit;
  314. int status;
  315. uint32_t protection;
  316. stm32x_info = bank->driver_priv;
  317. if (target->state != TARGET_HALTED)
  318. {
  319. LOG_ERROR("Target not halted");
  320. return ERROR_TARGET_NOT_HALTED;
  321. }
  322. if ((first && (first % stm32x_info->ppage_size)) || ((last + 1) && (last + 1) % stm32x_info->ppage_size))
  323. {
  324. LOG_WARNING("Error: start and end sectors must be on a %d sector boundary", stm32x_info->ppage_size);
  325. return ERROR_FLASH_SECTOR_INVALID;
  326. }
  327. /* medium density - each bit refers to a 4bank protection
  328. * high density - each bit refers to a 2bank protection */
  329. target_read_u32(target, STM32_FLASH_WRPR, &protection);
  330. prot_reg[0] = (uint16_t)protection;
  331. prot_reg[1] = (uint16_t)(protection >> 8);
  332. prot_reg[2] = (uint16_t)(protection >> 16);
  333. prot_reg[3] = (uint16_t)(protection >> 24);
  334. if (stm32x_info->ppage_size == 2)
  335. {
  336. /* high density flash */
  337. /* bit 7 controls sector 62 - 255 protection */
  338. if (last > 61)
  339. {
  340. if (set)
  341. prot_reg[3] &= ~(1 << 7);
  342. else
  343. prot_reg[3] |= (1 << 7);
  344. }
  345. if (first > 61)
  346. first = 62;
  347. if (last > 61)
  348. last = 61;
  349. for (i = first; i <= last; i++)
  350. {
  351. reg = (i / stm32x_info->ppage_size) / 8;
  352. bit = (i / stm32x_info->ppage_size) - (reg * 8);
  353. if (set)
  354. prot_reg[reg] &= ~(1 << bit);
  355. else
  356. prot_reg[reg] |= (1 << bit);
  357. }
  358. }
  359. else
  360. {
  361. /* medium density flash */
  362. for (i = first; i <= last; i++)
  363. {
  364. reg = (i / stm32x_info->ppage_size) / 8;
  365. bit = (i / stm32x_info->ppage_size) - (reg * 8);
  366. if (set)
  367. prot_reg[reg] &= ~(1 << bit);
  368. else
  369. prot_reg[reg] |= (1 << bit);
  370. }
  371. }
  372. if ((status = stm32x_erase_options(bank)) != ERROR_OK)
  373. return status;
  374. stm32x_info->option_bytes.protection[0] = prot_reg[0];
  375. stm32x_info->option_bytes.protection[1] = prot_reg[1];
  376. stm32x_info->option_bytes.protection[2] = prot_reg[2];
  377. stm32x_info->option_bytes.protection[3] = prot_reg[3];
  378. return stm32x_write_options(bank);
  379. }
  380. static int stm32x_write_block(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
  381. {
  382. stm32x_flash_bank_t *stm32x_info = bank->driver_priv;
  383. target_t *target = bank->target;
  384. uint32_t buffer_size = 16384;
  385. working_area_t *source;
  386. uint32_t address = bank->base + offset;
  387. reg_param_t reg_params[4];
  388. armv7m_algorithm_t armv7m_info;
  389. int retval = ERROR_OK;
  390. uint8_t stm32x_flash_write_code[] = {
  391. /* write: */
  392. 0xDF, 0xF8, 0x24, 0x40, /* ldr r4, STM32_FLASH_CR */
  393. 0x09, 0x4D, /* ldr r5, STM32_FLASH_SR */
  394. 0x4F, 0xF0, 0x01, 0x03, /* mov r3, #1 */
  395. 0x23, 0x60, /* str r3, [r4, #0] */
  396. 0x30, 0xF8, 0x02, 0x3B, /* ldrh r3, [r0], #2 */
  397. 0x21, 0xF8, 0x02, 0x3B, /* strh r3, [r1], #2 */
  398. /* busy: */
  399. 0x2B, 0x68, /* ldr r3, [r5, #0] */
  400. 0x13, 0xF0, 0x01, 0x0F, /* tst r3, #0x01 */
  401. 0xFB, 0xD0, /* beq busy */
  402. 0x13, 0xF0, 0x14, 0x0F, /* tst r3, #0x14 */
  403. 0x01, 0xD1, /* bne exit */
  404. 0x01, 0x3A, /* subs r2, r2, #1 */
  405. 0xED, 0xD1, /* bne write */
  406. /* exit: */
  407. 0xFE, 0xE7, /* b exit */
  408. 0x10, 0x20, 0x02, 0x40, /* STM32_FLASH_CR: .word 0x40022010 */
  409. 0x0C, 0x20, 0x02, 0x40 /* STM32_FLASH_SR: .word 0x4002200C */
  410. };
  411. /* flash write code */
  412. if (target_alloc_working_area(target, sizeof(stm32x_flash_write_code), &stm32x_info->write_algorithm) != ERROR_OK)
  413. {
  414. LOG_WARNING("no working area available, can't do block memory writes");
  415. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  416. };
  417. if ((retval = target_write_buffer(target, stm32x_info->write_algorithm->address, sizeof(stm32x_flash_write_code), stm32x_flash_write_code)) != ERROR_OK)
  418. return retval;
  419. /* memory buffer */
  420. while (target_alloc_working_area(target, buffer_size, &source) != ERROR_OK)
  421. {
  422. buffer_size /= 2;
  423. if (buffer_size <= 256)
  424. {
  425. /* if we already allocated the writing code, but failed to get a buffer, free the algorithm */
  426. if (stm32x_info->write_algorithm)
  427. target_free_working_area(target, stm32x_info->write_algorithm);
  428. LOG_WARNING("no large enough working area available, can't do block memory writes");
  429. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  430. }
  431. };
  432. armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
  433. armv7m_info.core_mode = ARMV7M_MODE_ANY;
  434. init_reg_param(&reg_params[0], "r0", 32, PARAM_OUT);
  435. init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);
  436. init_reg_param(&reg_params[2], "r2", 32, PARAM_OUT);
  437. init_reg_param(&reg_params[3], "r3", 32, PARAM_IN);
  438. while (count > 0)
  439. {
  440. uint32_t thisrun_count = (count > (buffer_size / 2)) ? (buffer_size / 2) : count;
  441. if ((retval = target_write_buffer(target, source->address, thisrun_count * 2, buffer)) != ERROR_OK)
  442. break;
  443. buf_set_u32(reg_params[0].value, 0, 32, source->address);
  444. buf_set_u32(reg_params[1].value, 0, 32, address);
  445. buf_set_u32(reg_params[2].value, 0, 32, thisrun_count);
  446. if ((retval = target_run_algorithm(target, 0, NULL, 4, reg_params, stm32x_info->write_algorithm->address, \
  447. stm32x_info->write_algorithm->address + (sizeof(stm32x_flash_write_code) - 10), 10000, &armv7m_info)) != ERROR_OK)
  448. {
  449. LOG_ERROR("error executing stm32x flash write algorithm");
  450. retval = ERROR_FLASH_OPERATION_FAILED;
  451. break;
  452. }
  453. if (buf_get_u32(reg_params[3].value, 0, 32) & FLASH_PGERR)
  454. {
  455. LOG_ERROR("flash memory not erased before writing");
  456. /* Clear but report errors */
  457. target_write_u32(target, STM32_FLASH_SR, FLASH_PGERR);
  458. retval = ERROR_FLASH_OPERATION_FAILED;
  459. break;
  460. }
  461. if (buf_get_u32(reg_params[3].value, 0, 32) & FLASH_WRPRTERR)
  462. {
  463. LOG_ERROR("flash memory write protected");
  464. /* Clear but report errors */
  465. target_write_u32(target, STM32_FLASH_SR, FLASH_WRPRTERR);
  466. retval = ERROR_FLASH_OPERATION_FAILED;
  467. break;
  468. }
  469. buffer += thisrun_count * 2;
  470. address += thisrun_count * 2;
  471. count -= thisrun_count;
  472. }
  473. target_free_working_area(target, source);
  474. target_free_working_area(target, stm32x_info->write_algorithm);
  475. destroy_reg_param(&reg_params[0]);
  476. destroy_reg_param(&reg_params[1]);
  477. destroy_reg_param(&reg_params[2]);
  478. destroy_reg_param(&reg_params[3]);
  479. return retval;
  480. }
  481. static int stm32x_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
  482. {
  483. target_t *target = bank->target;
  484. uint32_t words_remaining = (count / 2);
  485. uint32_t bytes_remaining = (count & 0x00000001);
  486. uint32_t address = bank->base + offset;
  487. uint32_t bytes_written = 0;
  488. uint8_t status;
  489. int retval;
  490. if (bank->target->state != TARGET_HALTED)
  491. {
  492. LOG_ERROR("Target not halted");
  493. return ERROR_TARGET_NOT_HALTED;
  494. }
  495. if (offset & 0x1)
  496. {
  497. LOG_WARNING("offset 0x%" PRIx32 " breaks required 2-byte alignment", offset);
  498. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  499. }
  500. /* unlock flash registers */
  501. target_write_u32(target, STM32_FLASH_KEYR, KEY1);
  502. target_write_u32(target, STM32_FLASH_KEYR, KEY2);
  503. /* multiple half words (2-byte) to be programmed? */
  504. if (words_remaining > 0)
  505. {
  506. /* try using a block write */
  507. if ((retval = stm32x_write_block(bank, buffer, offset, words_remaining)) != ERROR_OK)
  508. {
  509. if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
  510. {
  511. /* if block write failed (no sufficient working area),
  512. * we use normal (slow) single dword accesses */
  513. LOG_WARNING("couldn't use block writes, falling back to single memory accesses");
  514. }
  515. else if (retval == ERROR_FLASH_OPERATION_FAILED)
  516. {
  517. LOG_ERROR("flash writing failed with error code: 0x%x", retval);
  518. return ERROR_FLASH_OPERATION_FAILED;
  519. }
  520. }
  521. else
  522. {
  523. buffer += words_remaining * 2;
  524. address += words_remaining * 2;
  525. words_remaining = 0;
  526. }
  527. }
  528. while (words_remaining > 0)
  529. {
  530. uint16_t value;
  531. memcpy(&value, buffer + bytes_written, sizeof(uint16_t));
  532. target_write_u32(target, STM32_FLASH_CR, FLASH_PG);
  533. target_write_u16(target, address, value);
  534. status = stm32x_wait_status_busy(bank, 5);
  535. if (status & FLASH_WRPRTERR)
  536. {
  537. LOG_ERROR("flash memory not erased before writing");
  538. return ERROR_FLASH_OPERATION_FAILED;
  539. }
  540. if (status & FLASH_PGERR)
  541. {
  542. LOG_ERROR("flash memory write protected");
  543. return ERROR_FLASH_OPERATION_FAILED;
  544. }
  545. bytes_written += 2;
  546. words_remaining--;
  547. address += 2;
  548. }
  549. if (bytes_remaining)
  550. {
  551. uint16_t value = 0xffff;
  552. memcpy(&value, buffer + bytes_written, bytes_remaining);
  553. target_write_u32(target, STM32_FLASH_CR, FLASH_PG);
  554. target_write_u16(target, address, value);
  555. status = stm32x_wait_status_busy(bank, 5);
  556. if (status & FLASH_WRPRTERR)
  557. {
  558. LOG_ERROR("flash memory not erased before writing");
  559. return ERROR_FLASH_OPERATION_FAILED;
  560. }
  561. if (status & FLASH_PGERR)
  562. {
  563. LOG_ERROR("flash memory write protected");
  564. return ERROR_FLASH_OPERATION_FAILED;
  565. }
  566. }
  567. target_write_u32(target, STM32_FLASH_CR, FLASH_LOCK);
  568. return ERROR_OK;
  569. }
  570. static int stm32x_probe(struct flash_bank_s *bank)
  571. {
  572. target_t *target = bank->target;
  573. stm32x_flash_bank_t *stm32x_info = bank->driver_priv;
  574. int i;
  575. uint16_t num_pages;
  576. uint32_t device_id;
  577. int page_size;
  578. if (bank->target->state != TARGET_HALTED)
  579. {
  580. LOG_ERROR("Target not halted");
  581. return ERROR_TARGET_NOT_HALTED;
  582. }
  583. stm32x_info->probed = 0;
  584. /* read stm32 device id register */
  585. target_read_u32(target, 0xE0042000, &device_id);
  586. LOG_INFO("device id = 0x%08" PRIx32 "", device_id);
  587. /* get flash size from target */
  588. if (target_read_u16(target, 0x1FFFF7E0, &num_pages) != ERROR_OK)
  589. {
  590. /* failed reading flash size, default to max target family */
  591. num_pages = 0xffff;
  592. }
  593. if ((device_id & 0x7ff) == 0x410)
  594. {
  595. /* medium density - we have 1k pages
  596. * 4 pages for a protection area */
  597. page_size = 1024;
  598. stm32x_info->ppage_size = 4;
  599. /* check for early silicon */
  600. if (num_pages == 0xffff)
  601. {
  602. /* number of sectors incorrect on revA */
  603. LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 128k flash");
  604. num_pages = 128;
  605. }
  606. }
  607. else if ((device_id & 0x7ff) == 0x412)
  608. {
  609. /* low density - we have 1k pages
  610. * 4 pages for a protection area */
  611. page_size = 1024;
  612. stm32x_info->ppage_size = 4;
  613. /* check for early silicon */
  614. if (num_pages == 0xffff)
  615. {
  616. /* number of sectors incorrect on revA */
  617. LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 32k flash");
  618. num_pages = 32;
  619. }
  620. }
  621. else if ((device_id & 0x7ff) == 0x414)
  622. {
  623. /* high density - we have 2k pages
  624. * 2 pages for a protection area */
  625. page_size = 2048;
  626. stm32x_info->ppage_size = 2;
  627. /* check for early silicon */
  628. if (num_pages == 0xffff)
  629. {
  630. /* number of sectors incorrect on revZ */
  631. LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 512k flash");
  632. num_pages = 512;
  633. }
  634. }
  635. else if ((device_id & 0x7ff) == 0x418)
  636. {
  637. /* connectivity line density - we have 2k pages
  638. * 2 pages for a protection area */
  639. page_size = 2048;
  640. stm32x_info->ppage_size = 2;
  641. /* check for early silicon */
  642. if (num_pages == 0xffff)
  643. {
  644. /* number of sectors incorrect on revZ */
  645. LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 256k flash");
  646. num_pages = 256;
  647. }
  648. }
  649. else
  650. {
  651. LOG_WARNING("Cannot identify target as a STM32 family.");
  652. return ERROR_FLASH_OPERATION_FAILED;
  653. }
  654. LOG_INFO("flash size = %dkbytes", num_pages);
  655. /* calculate numbers of pages */
  656. num_pages /= (page_size / 1024);
  657. bank->base = 0x08000000;
  658. bank->size = (num_pages * page_size);
  659. bank->num_sectors = num_pages;
  660. bank->sectors = malloc(sizeof(flash_sector_t) * num_pages);
  661. for (i = 0; i < num_pages; i++)
  662. {
  663. bank->sectors[i].offset = i * page_size;
  664. bank->sectors[i].size = page_size;
  665. bank->sectors[i].is_erased = -1;
  666. bank->sectors[i].is_protected = 1;
  667. }
  668. stm32x_info->probed = 1;
  669. return ERROR_OK;
  670. }
  671. static int stm32x_auto_probe(struct flash_bank_s *bank)
  672. {
  673. stm32x_flash_bank_t *stm32x_info = bank->driver_priv;
  674. if (stm32x_info->probed)
  675. return ERROR_OK;
  676. return stm32x_probe(bank);
  677. }
  678. #if 0
  679. static int stm32x_handle_part_id_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  680. {
  681. return ERROR_OK;
  682. }
  683. #endif
  684. static int stm32x_info(struct flash_bank_s *bank, char *buf, int buf_size)
  685. {
  686. target_t *target = bank->target;
  687. uint32_t device_id;
  688. int printed;
  689. /* read stm32 device id register */
  690. target_read_u32(target, 0xE0042000, &device_id);
  691. if ((device_id & 0x7ff) == 0x410)
  692. {
  693. printed = snprintf(buf, buf_size, "stm32x (Medium Density) - Rev: ");
  694. buf += printed;
  695. buf_size -= printed;
  696. switch (device_id >> 16)
  697. {
  698. case 0x0000:
  699. snprintf(buf, buf_size, "A");
  700. break;
  701. case 0x2000:
  702. snprintf(buf, buf_size, "B");
  703. break;
  704. case 0x2001:
  705. snprintf(buf, buf_size, "Z");
  706. break;
  707. case 0x2003:
  708. snprintf(buf, buf_size, "Y");
  709. break;
  710. default:
  711. snprintf(buf, buf_size, "unknown");
  712. break;
  713. }
  714. }
  715. else if ((device_id & 0x7ff) == 0x412)
  716. {
  717. printed = snprintf(buf, buf_size, "stm32x (Low Density) - Rev: ");
  718. buf += printed;
  719. buf_size -= printed;
  720. switch (device_id >> 16)
  721. {
  722. case 0x1000:
  723. snprintf(buf, buf_size, "A");
  724. break;
  725. default:
  726. snprintf(buf, buf_size, "unknown");
  727. break;
  728. }
  729. }
  730. else if ((device_id & 0x7ff) == 0x414)
  731. {
  732. printed = snprintf(buf, buf_size, "stm32x (High Density) - Rev: ");
  733. buf += printed;
  734. buf_size -= printed;
  735. switch (device_id >> 16)
  736. {
  737. case 0x1000:
  738. snprintf(buf, buf_size, "A");
  739. break;
  740. case 0x1001:
  741. snprintf(buf, buf_size, "Z");
  742. break;
  743. default:
  744. snprintf(buf, buf_size, "unknown");
  745. break;
  746. }
  747. }
  748. else if ((device_id & 0x7ff) == 0x418)
  749. {
  750. printed = snprintf(buf, buf_size, "stm32x (Connectivity) - Rev: ");
  751. buf += printed;
  752. buf_size -= printed;
  753. switch (device_id >> 16)
  754. {
  755. case 0x1000:
  756. snprintf(buf, buf_size, "A");
  757. break;
  758. case 0x1001:
  759. snprintf(buf, buf_size, "Z");
  760. break;
  761. default:
  762. snprintf(buf, buf_size, "unknown");
  763. break;
  764. }
  765. }
  766. else
  767. {
  768. snprintf(buf, buf_size, "Cannot identify target as a stm32x\n");
  769. return ERROR_FLASH_OPERATION_FAILED;
  770. }
  771. return ERROR_OK;
  772. }
  773. static int stm32x_handle_lock_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  774. {
  775. flash_bank_t *bank;
  776. target_t *target = NULL;
  777. stm32x_flash_bank_t *stm32x_info = NULL;
  778. if (argc < 1)
  779. {
  780. command_print(cmd_ctx, "stm32x lock <bank>");
  781. return ERROR_OK;
  782. }
  783. bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
  784. if (!bank)
  785. {
  786. command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
  787. return ERROR_OK;
  788. }
  789. stm32x_info = bank->driver_priv;
  790. target = bank->target;
  791. if (target->state != TARGET_HALTED)
  792. {
  793. LOG_ERROR("Target not halted");
  794. return ERROR_TARGET_NOT_HALTED;
  795. }
  796. if (stm32x_erase_options(bank) != ERROR_OK)
  797. {
  798. command_print(cmd_ctx, "stm32x failed to erase options");
  799. return ERROR_OK;
  800. }
  801. /* set readout protection */
  802. stm32x_info->option_bytes.RDP = 0;
  803. if (stm32x_write_options(bank) != ERROR_OK)
  804. {
  805. command_print(cmd_ctx, "stm32x failed to lock device");
  806. return ERROR_OK;
  807. }
  808. command_print(cmd_ctx, "stm32x locked");
  809. return ERROR_OK;
  810. }
  811. static int stm32x_handle_unlock_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  812. {
  813. flash_bank_t *bank;
  814. target_t *target = NULL;
  815. stm32x_flash_bank_t *stm32x_info = NULL;
  816. if (argc < 1)
  817. {
  818. command_print(cmd_ctx, "stm32x unlock <bank>");
  819. return ERROR_OK;
  820. }
  821. bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
  822. if (!bank)
  823. {
  824. command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
  825. return ERROR_OK;
  826. }
  827. stm32x_info = bank->driver_priv;
  828. target = bank->target;
  829. if (target->state != TARGET_HALTED)
  830. {
  831. LOG_ERROR("Target not halted");
  832. return ERROR_TARGET_NOT_HALTED;
  833. }
  834. if (stm32x_erase_options(bank) != ERROR_OK)
  835. {
  836. command_print(cmd_ctx, "stm32x failed to unlock device");
  837. return ERROR_OK;
  838. }
  839. if (stm32x_write_options(bank) != ERROR_OK)
  840. {
  841. command_print(cmd_ctx, "stm32x failed to lock device");
  842. return ERROR_OK;
  843. }
  844. command_print(cmd_ctx, "stm32x unlocked");
  845. return ERROR_OK;
  846. }
  847. static int stm32x_handle_options_read_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  848. {
  849. flash_bank_t *bank;
  850. uint32_t optionbyte;
  851. target_t *target = NULL;
  852. stm32x_flash_bank_t *stm32x_info = NULL;
  853. if (argc < 1)
  854. {
  855. command_print(cmd_ctx, "stm32x options_read <bank>");
  856. return ERROR_OK;
  857. }
  858. bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
  859. if (!bank)
  860. {
  861. command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
  862. return ERROR_OK;
  863. }
  864. stm32x_info = bank->driver_priv;
  865. target = bank->target;
  866. if (target->state != TARGET_HALTED)
  867. {
  868. LOG_ERROR("Target not halted");
  869. return ERROR_TARGET_NOT_HALTED;
  870. }
  871. target_read_u32(target, STM32_FLASH_OBR, &optionbyte);
  872. command_print(cmd_ctx, "Option Byte: 0x%" PRIx32 "", optionbyte);
  873. if (buf_get_u32((uint8_t*)&optionbyte, OPT_ERROR, 1))
  874. command_print(cmd_ctx, "Option Byte Complement Error");
  875. if (buf_get_u32((uint8_t*)&optionbyte, OPT_READOUT, 1))
  876. command_print(cmd_ctx, "Readout Protection On");
  877. else
  878. command_print(cmd_ctx, "Readout Protection Off");
  879. if (buf_get_u32((uint8_t*)&optionbyte, OPT_RDWDGSW, 1))
  880. command_print(cmd_ctx, "Software Watchdog");
  881. else
  882. command_print(cmd_ctx, "Hardware Watchdog");
  883. if (buf_get_u32((uint8_t*)&optionbyte, OPT_RDRSTSTOP, 1))
  884. command_print(cmd_ctx, "Stop: No reset generated");
  885. else
  886. command_print(cmd_ctx, "Stop: Reset generated");
  887. if (buf_get_u32((uint8_t*)&optionbyte, OPT_RDRSTSTDBY, 1))
  888. command_print(cmd_ctx, "Standby: No reset generated");
  889. else
  890. command_print(cmd_ctx, "Standby: Reset generated");
  891. return ERROR_OK;
  892. }
  893. static int stm32x_handle_options_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  894. {
  895. flash_bank_t *bank;
  896. target_t *target = NULL;
  897. stm32x_flash_bank_t *stm32x_info = NULL;
  898. uint16_t optionbyte = 0xF8;
  899. if (argc < 4)
  900. {
  901. command_print(cmd_ctx, "stm32x options_write <bank> <SWWDG | HWWDG> <RSTSTNDBY | NORSTSTNDBY> <RSTSTOP | NORSTSTOP>");
  902. return ERROR_OK;
  903. }
  904. bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
  905. if (!bank)
  906. {
  907. command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
  908. return ERROR_OK;
  909. }
  910. stm32x_info = bank->driver_priv;
  911. target = bank->target;
  912. if (target->state != TARGET_HALTED)
  913. {
  914. LOG_ERROR("Target not halted");
  915. return ERROR_TARGET_NOT_HALTED;
  916. }
  917. if (strcmp(args[1], "SWWDG") == 0)
  918. {
  919. optionbyte |= (1 << 0);
  920. }
  921. else
  922. {
  923. optionbyte &= ~(1 << 0);
  924. }
  925. if (strcmp(args[2], "NORSTSTNDBY") == 0)
  926. {
  927. optionbyte |= (1 << 1);
  928. }
  929. else
  930. {
  931. optionbyte &= ~(1 << 1);
  932. }
  933. if (strcmp(args[3], "NORSTSTOP") == 0)
  934. {
  935. optionbyte |= (1 << 2);
  936. }
  937. else
  938. {
  939. optionbyte &= ~(1 << 2);
  940. }
  941. if (stm32x_erase_options(bank) != ERROR_OK)
  942. {
  943. command_print(cmd_ctx, "stm32x failed to erase options");
  944. return ERROR_OK;
  945. }
  946. stm32x_info->option_bytes.user_options = optionbyte;
  947. if (stm32x_write_options(bank) != ERROR_OK)
  948. {
  949. command_print(cmd_ctx, "stm32x failed to write options");
  950. return ERROR_OK;
  951. }
  952. command_print(cmd_ctx, "stm32x write options complete");
  953. return ERROR_OK;
  954. }
  955. static int stm32x_mass_erase(struct flash_bank_s *bank)
  956. {
  957. target_t *target = bank->target;
  958. uint32_t status;
  959. if (target->state != TARGET_HALTED)
  960. {
  961. LOG_ERROR("Target not halted");
  962. return ERROR_TARGET_NOT_HALTED;
  963. }
  964. /* unlock option flash registers */
  965. target_write_u32(target, STM32_FLASH_KEYR, KEY1);
  966. target_write_u32(target, STM32_FLASH_KEYR, KEY2);
  967. /* mass erase flash memory */
  968. target_write_u32(target, STM32_FLASH_CR, FLASH_MER);
  969. target_write_u32(target, STM32_FLASH_CR, FLASH_MER | FLASH_STRT);
  970. status = stm32x_wait_status_busy(bank, 10);
  971. target_write_u32(target, STM32_FLASH_CR, FLASH_LOCK);
  972. if (status & FLASH_WRPRTERR)
  973. {
  974. LOG_ERROR("stm32x device protected");
  975. return ERROR_OK;
  976. }
  977. if (status & FLASH_PGERR)
  978. {
  979. LOG_ERROR("stm32x device programming failed");
  980. return ERROR_OK;
  981. }
  982. return ERROR_OK;
  983. }
  984. static int stm32x_handle_mass_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  985. {
  986. flash_bank_t *bank;
  987. int i;
  988. if (argc < 1)
  989. {
  990. command_print(cmd_ctx, "stm32x mass_erase <bank>");
  991. return ERROR_OK;
  992. }
  993. bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
  994. if (!bank)
  995. {
  996. command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
  997. return ERROR_OK;
  998. }
  999. if (stm32x_mass_erase(bank) == ERROR_OK)
  1000. {
  1001. /* set all sectors as erased */
  1002. for (i = 0; i < bank->num_sectors; i++)
  1003. {
  1004. bank->sectors[i].is_erased = 1;
  1005. }
  1006. command_print(cmd_ctx, "stm32x mass erase complete");
  1007. }
  1008. else
  1009. {
  1010. command_print(cmd_ctx, "stm32x mass erase failed");
  1011. }
  1012. return ERROR_OK;
  1013. }