OpenOCD
stm32f1x.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2005 by Dominic Rath *
5  * Dominic.Rath@gmx.de *
6  * *
7  * Copyright (C) 2008 by Spencer Oliver *
8  * spen@spen-soft.co.uk *
9  * *
10  * Copyright (C) 2011 by Andreas Fritiofson *
11  * andreas.fritiofson@gmail.com *
12  ***************************************************************************/
13 
14 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif
17 
18 #include <string.h>
19 
20 #include "imp.h"
21 #include <helper/binarybuffer.h>
22 #include <target/algorithm.h>
23 #include <target/cortex_m.h>
24 #include <target/riscv/riscv.h>
25 
26 /* stm32x register locations */
27 
28 #define FLASH_REG_BASE_B0 0x40022000
29 #define FLASH_REG_BASE_B1 0x40022040
30 
31 #define STM32_FLASH_ACR 0x00
32 #define STM32_FLASH_KEYR 0x04
33 #define STM32_FLASH_OPTKEYR 0x08
34 #define STM32_FLASH_SR 0x0C
35 #define STM32_FLASH_CR 0x10
36 #define STM32_FLASH_AR 0x14
37 #define STM32_FLASH_OBR 0x1C
38 #define STM32_FLASH_WRPR 0x20
39 
40 /* TODO: Check if code using these really should be hard coded to bank 0.
41  * There are valid cases, on dual flash devices the protection of the
42  * second bank is done on the bank0 reg's. */
43 #define STM32_FLASH_ACR_B0 0x40022000
44 #define STM32_FLASH_KEYR_B0 0x40022004
45 #define STM32_FLASH_OPTKEYR_B0 0x40022008
46 #define STM32_FLASH_SR_B0 0x4002200C
47 #define STM32_FLASH_CR_B0 0x40022010
48 #define STM32_FLASH_AR_B0 0x40022014
49 #define STM32_FLASH_OBR_B0 0x4002201C
50 #define STM32_FLASH_WRPR_B0 0x40022020
51 
52 /* option byte location */
53 
54 #define STM32_OB_RDP 0x1FFFF800
55 #define STM32_OB_USER 0x1FFFF802
56 #define STM32_OB_DATA0 0x1FFFF804
57 #define STM32_OB_DATA1 0x1FFFF806
58 #define STM32_OB_WRP0 0x1FFFF808
59 #define STM32_OB_WRP1 0x1FFFF80A
60 #define STM32_OB_WRP2 0x1FFFF80C
61 #define STM32_OB_WRP3 0x1FFFF80E
62 
63 /* FLASH_CR register bits */
64 
65 #define FLASH_PG (1 << 0)
66 #define FLASH_PER (1 << 1)
67 #define FLASH_MER (1 << 2)
68 #define FLASH_OPTPG (1 << 4)
69 #define FLASH_OPTER (1 << 5)
70 #define FLASH_STRT (1 << 6)
71 #define FLASH_LOCK (1 << 7)
72 #define FLASH_OPTWRE (1 << 9)
73 #define FLASH_OBL_LAUNCH (1 << 13) /* except stm32f1x series */
74 
75 /* FLASH_SR register bits */
76 
77 #define FLASH_BSY (1 << 0)
78 #define FLASH_PGERR (1 << 2)
79 #define FLASH_WRPRTERR (1 << 4)
80 #define FLASH_EOP (1 << 5)
81 
82 /* STM32_FLASH_OBR bit definitions (reading) */
83 
84 #define OPT_ERROR 0
85 #define OPT_READOUT 1
86 #define OPT_RDWDGSW 2
87 #define OPT_RDRSTSTOP 3
88 #define OPT_RDRSTSTDBY 4
89 #define OPT_BFB2 5 /* dual flash bank only */
90 
91 /* register unlock keys */
92 
93 #define KEY1 0x45670123
94 #define KEY2 0xCDEF89AB
95 
96 /* timeout values */
97 
98 #define FLASH_WRITE_TIMEOUT 10
99 #define FLASH_ERASE_TIMEOUT 100
100 
102  uint8_t rdp;
103  uint8_t user;
104  uint16_t data;
105  uint32_t protection;
106 };
107 
111  bool probed;
112 
114  /* used to access dual flash bank stm32xl */
116  uint32_t register_base;
117  uint8_t default_rdp;
120  uint32_t user_bank_size;
121 };
122 
123 static int stm32x_mass_erase(struct flash_bank *bank);
124 static int stm32x_write_block(struct flash_bank *bank, const uint8_t *buffer,
125  uint32_t address, uint32_t hwords_count);
126 
127 /* flash bank stm32x <base> <size> 0 0 <target#>
128  */
129 FLASH_BANK_COMMAND_HANDLER(stm32x_flash_bank_command)
130 {
131  struct stm32x_flash_bank *stm32x_info;
132 
133  if (CMD_ARGC < 6)
135 
136  stm32x_info = malloc(sizeof(struct stm32x_flash_bank));
137 
138  bank->driver_priv = stm32x_info;
139  stm32x_info->probed = false;
140  stm32x_info->has_dual_banks = false;
141  stm32x_info->can_load_options = false;
142  stm32x_info->register_base = FLASH_REG_BASE_B0;
143  stm32x_info->user_bank_size = bank->size;
144 
145  /* The flash write must be aligned to a halfword boundary */
146  bank->write_start_alignment = bank->write_end_alignment = 2;
147 
148  return ERROR_OK;
149 }
150 
151 static inline int stm32x_get_flash_reg(struct flash_bank *bank, uint32_t reg)
152 {
153  struct stm32x_flash_bank *stm32x_info = bank->driver_priv;
154  return reg + stm32x_info->register_base;
155 }
156 
157 static inline int stm32x_get_flash_status(struct flash_bank *bank, uint32_t *status)
158 {
159  struct target *target = bank->target;
161 }
162 
164 {
165  struct target *target = bank->target;
166  uint32_t status;
167  int retval = ERROR_OK;
168 
169  /* wait for busy to clear */
170  for (;;) {
171  retval = stm32x_get_flash_status(bank, &status);
172  if (retval != ERROR_OK)
173  return retval;
174  LOG_DEBUG("status: 0x%" PRIx32, status);
175  if ((status & FLASH_BSY) == 0)
176  break;
177  if (timeout-- <= 0) {
178  LOG_ERROR("timed out waiting for flash");
179  return ERROR_FLASH_BUSY;
180  }
181  alive_sleep(1);
182  }
183 
184  if (status & FLASH_WRPRTERR) {
185  LOG_ERROR("stm32x device protected");
186  retval = ERROR_FLASH_PROTECTED;
187  }
188 
189  if (status & FLASH_PGERR) {
190  LOG_ERROR("stm32x device programming failed / flash not erased");
192  }
193 
194  /* Clear but report errors */
195  if (status & (FLASH_WRPRTERR | FLASH_PGERR)) {
196  /* If this operation fails, we ignore it and report the original
197  * retval
198  */
201  }
202  return retval;
203 }
204 
206 {
207  struct stm32x_flash_bank *stm32x_info = bank->driver_priv;
208 
209  /* if we have a dual flash bank device then
210  * we need to perform option byte stuff on bank0 only */
211  if (stm32x_info->register_base != FLASH_REG_BASE_B0) {
212  LOG_ERROR("Option byte operations must use bank 0");
214  }
215 
216  return ERROR_OK;
217 }
218 
220 {
221  struct stm32x_flash_bank *stm32x_info = bank->driver_priv;
222  struct target *target = bank->target;
223  uint32_t option_bytes;
224  int retval;
225 
226  /* read user and read protection option bytes, user data option bytes */
227  retval = target_read_u32(target, STM32_FLASH_OBR_B0, &option_bytes);
228  if (retval != ERROR_OK)
229  return retval;
230 
231  stm32x_info->option_bytes.rdp = (option_bytes & (1 << OPT_READOUT)) ? 0 : stm32x_info->default_rdp;
232  stm32x_info->option_bytes.user = (option_bytes >> stm32x_info->option_offset >> 2) & 0xff;
233  stm32x_info->option_bytes.data = (option_bytes >> stm32x_info->user_data_offset) & 0xffff;
234 
235  /* read write protection option bytes */
237  if (retval != ERROR_OK)
238  return retval;
239 
240  return ERROR_OK;
241 }
242 
244 {
245  struct stm32x_flash_bank *stm32x_info = bank->driver_priv;
246  struct target *target = bank->target;
247 
248  /* read current options */
250 
251  /* unlock flash registers */
253  if (retval != ERROR_OK)
254  return retval;
256  if (retval != ERROR_OK)
257  goto flash_lock;
258 
259  /* unlock option flash registers */
261  if (retval != ERROR_OK)
262  goto flash_lock;
264  if (retval != ERROR_OK)
265  goto flash_lock;
266 
267  /* erase option bytes */
269  if (retval != ERROR_OK)
270  goto flash_lock;
272  if (retval != ERROR_OK)
273  goto flash_lock;
274 
276  if (retval != ERROR_OK)
277  goto flash_lock;
278 
279  /* clear read protection option byte
280  * this will also force a device unlock if set */
281  stm32x_info->option_bytes.rdp = stm32x_info->default_rdp;
282 
283  return ERROR_OK;
284 
285 flash_lock:
287  return retval;
288 }
289 
291 {
292  struct stm32x_flash_bank *stm32x_info = NULL;
293  struct target *target = bank->target;
294 
295  stm32x_info = bank->driver_priv;
296 
297  /* unlock flash registers */
299  if (retval != ERROR_OK)
300  return retval;
302  if (retval != ERROR_OK)
303  goto flash_lock;
304 
305  /* unlock option flash registers */
307  if (retval != ERROR_OK)
308  goto flash_lock;
310  if (retval != ERROR_OK)
311  goto flash_lock;
312 
313  /* program option bytes */
315  if (retval != ERROR_OK)
316  goto flash_lock;
317 
318  uint8_t opt_bytes[16];
319 
320  target_buffer_set_u16(target, opt_bytes, stm32x_info->option_bytes.rdp);
321  target_buffer_set_u16(target, opt_bytes + 2, stm32x_info->option_bytes.user);
322  target_buffer_set_u16(target, opt_bytes + 4, stm32x_info->option_bytes.data & 0xff);
323  target_buffer_set_u16(target, opt_bytes + 6, (stm32x_info->option_bytes.data >> 8) & 0xff);
324  target_buffer_set_u16(target, opt_bytes + 8, stm32x_info->option_bytes.protection & 0xff);
325  target_buffer_set_u16(target, opt_bytes + 10, (stm32x_info->option_bytes.protection >> 8) & 0xff);
326  target_buffer_set_u16(target, opt_bytes + 12, (stm32x_info->option_bytes.protection >> 16) & 0xff);
327  target_buffer_set_u16(target, opt_bytes + 14, (stm32x_info->option_bytes.protection >> 24) & 0xff);
328 
329  /* Block write is preferred in favour of operation with ancient ST-Link
330  * firmwares without 16-bit memory access. See
331  * 480: flash: stm32f1x: write option bytes using the loader
332  * https://review.openocd.org/c/openocd/+/480
333  */
334  retval = stm32x_write_block(bank, opt_bytes, STM32_OB_RDP, sizeof(opt_bytes) / 2);
335 
336 flash_lock:
337  {
339  if (retval == ERROR_OK)
340  retval = retval2;
341  }
342  return retval;
343 }
344 
346 {
347  struct target *target = bank->target;
348  uint32_t protection;
349 
351  if (retval != ERROR_OK)
352  return retval;
353 
354  /* medium density - each bit refers to a 4 sector protection block
355  * high density - each bit refers to a 2 sector protection block
356  * bit 31 refers to all remaining sectors in a bank */
358  if (retval != ERROR_OK)
359  return retval;
360 
361  for (unsigned int i = 0; i < bank->num_prot_blocks; i++)
362  bank->prot_blocks[i].is_protected = (protection & (1 << i)) ? 0 : 1;
363 
364  return ERROR_OK;
365 }
366 
367 static int stm32x_erase(struct flash_bank *bank, unsigned int first,
368  unsigned int last)
369 {
370  struct target *target = bank->target;
371 
372  if (bank->target->state != TARGET_HALTED) {
373  LOG_ERROR("Target not halted");
375  }
376 
377  if ((first == 0) && (last == (bank->num_sectors - 1)))
378  return stm32x_mass_erase(bank);
379 
380  /* unlock flash registers */
382  if (retval != ERROR_OK)
383  return retval;
385  if (retval != ERROR_OK)
386  goto flash_lock;
387 
388  for (unsigned int i = first; i <= last; i++) {
390  if (retval != ERROR_OK)
391  goto flash_lock;
393  bank->base + bank->sectors[i].offset);
394  if (retval != ERROR_OK)
395  goto flash_lock;
396  retval = target_write_u32(target,
398  if (retval != ERROR_OK)
399  goto flash_lock;
400 
402  if (retval != ERROR_OK)
403  goto flash_lock;
404  }
405 
406 flash_lock:
407  {
409  if (retval == ERROR_OK)
410  retval = retval2;
411  }
412  return retval;
413 }
414 
415 static int stm32x_protect(struct flash_bank *bank, int set, unsigned int first,
416  unsigned int last)
417 {
418  struct target *target = bank->target;
419  struct stm32x_flash_bank *stm32x_info = bank->driver_priv;
420 
421  if (target->state != TARGET_HALTED) {
422  LOG_ERROR("Target not halted");
424  }
425 
427  if (retval != ERROR_OK)
428  return retval;
429 
430  retval = stm32x_erase_options(bank);
431  if (retval != ERROR_OK) {
432  LOG_ERROR("stm32x failed to erase options");
433  return retval;
434  }
435 
436  for (unsigned int i = first; i <= last; i++) {
437  if (set)
438  stm32x_info->option_bytes.protection &= ~(1 << i);
439  else
440  stm32x_info->option_bytes.protection |= (1 << i);
441  }
442 
443  return stm32x_write_options(bank);
444 }
445 
446 static int stm32x_write_block_async(struct flash_bank *bank, const uint8_t *buffer,
447  uint32_t address, uint32_t hwords_count)
448 {
449  struct stm32x_flash_bank *stm32x_info = bank->driver_priv;
450  struct target *target = bank->target;
451  uint32_t buffer_size;
452  struct working_area *write_algorithm;
453  struct working_area *source;
454  struct armv7m_algorithm armv7m_info;
455  int retval;
456 
457  static const uint8_t stm32x_flash_write_code[] = {
458 #include "../../../contrib/loaders/flash/stm32/stm32f1x.inc"
459  };
460 
461  /* flash write code */
462  if (target_alloc_working_area(target, sizeof(stm32x_flash_write_code),
463  &write_algorithm) != ERROR_OK) {
464  LOG_WARNING("no working area available, can't do block memory writes");
466  }
467 
468  retval = target_write_buffer(target, write_algorithm->address,
469  sizeof(stm32x_flash_write_code), stm32x_flash_write_code);
470  if (retval != ERROR_OK) {
471  target_free_working_area(target, write_algorithm);
472  return retval;
473  }
474 
475  /* memory buffer */
477  buffer_size = MIN(hwords_count * 2 + 8, MAX(buffer_size, 256));
478  /* Normally we allocate all available working area.
479  * MIN shrinks buffer_size if the size of the written block is smaller.
480  * MAX prevents using async algo if the available working area is smaller
481  * than 256, the following allocation fails with
482  * ERROR_TARGET_RESOURCE_NOT_AVAILABLE and slow flashing takes place.
483  */
484 
486  /* Allocated size is always 32-bit word aligned */
487  if (retval != ERROR_OK) {
488  target_free_working_area(target, write_algorithm);
489  LOG_WARNING("no large enough working area available, can't do block memory writes");
490  /* target_alloc_working_area() may return ERROR_FAIL if area backup fails:
491  * convert any error to ERROR_TARGET_RESOURCE_NOT_AVAILABLE
492  */
494  }
495 
496  struct reg_param reg_params[5];
497 
498  init_reg_param(&reg_params[0], "r0", 32, PARAM_IN_OUT); /* flash base (in), status (out) */
499  init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT); /* count (halfword-16bit) */
500  init_reg_param(&reg_params[2], "r2", 32, PARAM_OUT); /* buffer start */
501  init_reg_param(&reg_params[3], "r3", 32, PARAM_OUT); /* buffer end */
502  init_reg_param(&reg_params[4], "r4", 32, PARAM_IN_OUT); /* target address */
503 
504  buf_set_u32(reg_params[0].value, 0, 32, stm32x_info->register_base);
505  buf_set_u32(reg_params[1].value, 0, 32, hwords_count);
506  buf_set_u32(reg_params[2].value, 0, 32, source->address);
507  buf_set_u32(reg_params[3].value, 0, 32, source->address + source->size);
508  buf_set_u32(reg_params[4].value, 0, 32, address);
509 
510  armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
511  armv7m_info.core_mode = ARM_MODE_THREAD;
512 
513  retval = target_run_flash_async_algorithm(target, buffer, hwords_count, 2,
514  0, NULL,
515  ARRAY_SIZE(reg_params), reg_params,
516  source->address, source->size,
517  write_algorithm->address, 0,
518  &armv7m_info);
519 
520  if (retval == ERROR_FLASH_OPERATION_FAILED) {
521  /* Actually we just need to check for programming errors
522  * stm32x_wait_status_busy also reports error and clears status bits.
523  *
524  * Target algo returns flash status in r0 only if properly finished.
525  * It is safer to re-read status register.
526  */
527  int retval2 = stm32x_wait_status_busy(bank, 5);
528  if (retval2 != ERROR_OK)
529  retval = retval2;
530 
531  LOG_ERROR("flash write failed just before address 0x%"PRIx32,
532  buf_get_u32(reg_params[4].value, 0, 32));
533  }
534 
535  for (unsigned int i = 0; i < ARRAY_SIZE(reg_params); i++)
536  destroy_reg_param(&reg_params[i]);
537 
539  target_free_working_area(target, write_algorithm);
540 
541  return retval;
542 }
543 
544 static int stm32x_write_block_riscv(struct flash_bank *bank, const uint8_t *buffer,
545  uint32_t address, uint32_t hwords_count)
546 {
547  struct target *target = bank->target;
548  uint32_t buffer_size;
549  struct working_area *write_algorithm;
550  struct working_area *source;
551  static const uint8_t gd32vf103_flash_write_code[] = {
552 #include "../../../contrib/loaders/flash/gd32vf103/gd32vf103.inc"
553  };
554 
555  /* flash write code */
556  if (target_alloc_working_area(target, sizeof(gd32vf103_flash_write_code),
557  &write_algorithm) != ERROR_OK) {
558  LOG_WARNING("no working area available, can't do block memory writes");
560  }
561 
562  int retval = target_write_buffer(target, write_algorithm->address,
563  sizeof(gd32vf103_flash_write_code), gd32vf103_flash_write_code);
564  if (retval != ERROR_OK) {
565  target_free_working_area(target, write_algorithm);
566  return retval;
567  }
568 
569  /* memory buffer */
571  buffer_size = MIN(hwords_count * 2, MAX(buffer_size, 256));
572 
574  /* Allocated size is always word aligned */
575  if (retval != ERROR_OK) {
576  target_free_working_area(target, write_algorithm);
577  LOG_WARNING("no large enough working area available, can't do block memory writes");
578  /* target_alloc_working_area() may return ERROR_FAIL if area backup fails:
579  * convert any error to ERROR_TARGET_RESOURCE_NOT_AVAILABLE
580  */
582  }
583 
584  struct reg_param reg_params[4];
585 
586  init_reg_param(&reg_params[0], "a0", 32, PARAM_OUT); /* poiner to FLASH_SR */
587  init_reg_param(&reg_params[1], "a1", 32, PARAM_OUT); /* count (halfword-16bit) */
588  init_reg_param(&reg_params[2], "a2", 32, PARAM_OUT); /* buffer start */
589  init_reg_param(&reg_params[3], "a3", 32, PARAM_IN_OUT); /* target address */
590 
591  while (hwords_count > 0) {
592  uint32_t thisrun_hwords = source->size / 2;
593 
594  /* Limit to the amount of data we actually want to write */
595  if (thisrun_hwords > hwords_count)
596  thisrun_hwords = hwords_count;
597 
598  /* Write data to buffer */
599  retval = target_write_buffer(target, source->address,
600  thisrun_hwords * 2, buffer);
601  if (retval != ERROR_OK)
602  break;
603 
604  buf_set_u32(reg_params[0].value, 0, 32, stm32x_get_flash_reg(bank, STM32_FLASH_SR));
605  buf_set_u32(reg_params[1].value, 0, 32, thisrun_hwords);
606  buf_set_u32(reg_params[2].value, 0, 32, source->address);
607  buf_set_u32(reg_params[3].value, 0, 32, address);
608 
609  struct riscv_algorithm algorithm_info = {
611  };
612 
613  retval = target_run_algorithm(target,
614  0, NULL,
615  ARRAY_SIZE(reg_params), reg_params,
616  write_algorithm->address,
617  write_algorithm->address + sizeof(gd32vf103_flash_write_code) - 4,
618  10000, &algorithm_info);
619 
620  if (retval != ERROR_OK) {
621  LOG_ERROR("Failed to execute algorithm at 0x%" TARGET_PRIxADDR ": %d",
622  write_algorithm->address, retval);
623  break;
624  }
625 
626  /* Actually we just need to check for programming errors
627  * stm32x_wait_status_busy also reports error and clears status bits
628  */
629  retval = stm32x_wait_status_busy(bank, 5);
630  if (retval != ERROR_OK) {
631  LOG_ERROR("flash write failed at address 0x%"PRIx32,
632  buf_get_u32(reg_params[3].value, 0, 32));
633  break;
634  }
635 
636  /* Update counters */
637  buffer += thisrun_hwords * 2;
638  address += thisrun_hwords * 2;
639  hwords_count -= thisrun_hwords;
640  }
641 
642  for (unsigned int i = 0; i < ARRAY_SIZE(reg_params); i++)
643  destroy_reg_param(&reg_params[i]);
644 
646  target_free_working_area(target, write_algorithm);
647 
648  return retval;
649 }
650 
655 static int stm32x_write_block(struct flash_bank *bank,
656  const uint8_t *buffer, uint32_t address, uint32_t hwords_count)
657 {
658  struct target *target = bank->target;
659 
660  /* The flash write must be aligned to a halfword boundary.
661  * The flash infrastructure ensures it, do just a security check
662  */
663  assert(address % 2 == 0);
664 
665  int retval;
666  struct arm *arm = target_to_arm(target);
667  if (is_arm(arm)) {
668  /* try using a block write - on ARM architecture or... */
669  retval = stm32x_write_block_async(bank, buffer, address, hwords_count);
670  } else {
671  /* ... RISC-V architecture */
672  retval = stm32x_write_block_riscv(bank, buffer, address, hwords_count);
673  }
674 
675  if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE) {
676  /* if block write failed (no sufficient working area),
677  * we use normal (slow) single halfword accesses */
678  LOG_WARNING("couldn't use block writes, falling back to single memory accesses");
679 
680  while (hwords_count > 0) {
681  retval = target_write_memory(target, address, 2, 1, buffer);
682  if (retval != ERROR_OK)
683  return retval;
684 
685  retval = stm32x_wait_status_busy(bank, 5);
686  if (retval != ERROR_OK)
687  return retval;
688 
689  hwords_count--;
690  buffer += 2;
691  address += 2;
692  }
693  }
694  return retval;
695 }
696 
697 static int stm32x_write(struct flash_bank *bank, const uint8_t *buffer,
698  uint32_t offset, uint32_t count)
699 {
700  struct target *target = bank->target;
701 
702  if (bank->target->state != TARGET_HALTED) {
703  LOG_ERROR("Target not halted");
705  }
706 
707  /* The flash write must be aligned to a halfword boundary.
708  * The flash infrastructure ensures it, do just a security check
709  */
710  assert(offset % 2 == 0);
711  assert(count % 2 == 0);
712 
713  int retval, retval2;
714 
715  /* unlock flash registers */
717  if (retval != ERROR_OK)
718  return retval;
720  if (retval != ERROR_OK)
721  goto reset_pg_and_lock;
722 
723  /* enable flash programming */
725  if (retval != ERROR_OK)
726  goto reset_pg_and_lock;
727 
728  /* write to flash */
729  retval = stm32x_write_block(bank, buffer, bank->base + offset, count / 2);
730 
731 reset_pg_and_lock:
733  if (retval == ERROR_OK)
734  retval = retval2;
735 
736  return retval;
737 }
738 
740  uint32_t device_id;
741  uint32_t flash_size;
742 };
743 
745 {
746  if (!target_was_examined(target)) {
747  LOG_ERROR("Target not examined yet");
749  }
750 
751  switch (cortex_m_get_impl_part(target)) {
752  case CORTEX_M0_PARTNO: /* STM32F0x devices */
753  case CORTEX_M0P_PARTNO: /* APM32F0x devices */
754  addr->device_id = 0x40015800;
755  addr->flash_size = 0x1FFFF7CC;
756  return ERROR_OK;
757  case CORTEX_M3_PARTNO: /* STM32F1x devices */
758  addr->device_id = 0xE0042000;
759  addr->flash_size = 0x1FFFF7E0;
760  return ERROR_OK;
761  case CORTEX_M4_PARTNO: /* STM32F3x devices */
762  addr->device_id = 0xE0042000;
763  addr->flash_size = 0x1FFFF7CC;
764  return ERROR_OK;
765  case CORTEX_M23_PARTNO: /* GD32E23x devices */
766  addr->device_id = 0x40015800;
767  addr->flash_size = 0x1FFFF7E0;
768  return ERROR_OK;
770  /* Check for GD32VF103 with RISC-V CPU */
771  if (strcmp(target_type_name(target), "riscv") == 0
772  && target_address_bits(target) == 32) {
773  /* There is nothing like arm common_magic in riscv_info_t
774  * check text name of target and if target is 32-bit
775  */
776  addr->device_id = 0xE0042000;
777  addr->flash_size = 0x1FFFF7E0;
778  return ERROR_OK;
779  }
780  /* fallthrough */
781  default:
782  LOG_ERROR("Cannot identify target as a stm32x");
783  return ERROR_FAIL;
784  }
785 }
786 
787 static int stm32x_get_device_id(struct flash_bank *bank, uint32_t *device_id)
788 {
789  struct target *target = bank->target;
790  struct stm32x_property_addr addr;
791 
792  int retval = stm32x_get_property_addr(target, &addr);
793  if (retval != ERROR_OK)
794  return retval;
795 
796  return target_read_u32(target, addr.device_id, device_id);
797 }
798 
799 static int stm32x_get_flash_size(struct flash_bank *bank, uint16_t *flash_size_in_kb)
800 {
801  struct target *target = bank->target;
802  struct stm32x_property_addr addr;
803 
804  int retval = stm32x_get_property_addr(target, &addr);
805  if (retval != ERROR_OK)
806  return retval;
807 
808  return target_read_u16(target, addr.flash_size, flash_size_in_kb);
809 }
810 
811 static int stm32x_probe(struct flash_bank *bank)
812 {
813  struct stm32x_flash_bank *stm32x_info = bank->driver_priv;
814  uint16_t flash_size_in_kb;
815  uint16_t max_flash_size_in_kb;
816  uint32_t dbgmcu_idcode;
817  int page_size;
818  uint32_t base_address = 0x08000000;
819 
820  stm32x_info->probed = false;
821  stm32x_info->register_base = FLASH_REG_BASE_B0;
822  stm32x_info->user_data_offset = 10;
823  stm32x_info->option_offset = 0;
824 
825  /* default factory read protection level 0 */
826  stm32x_info->default_rdp = 0xA5;
827 
828  /* read stm32 device id register */
829  int retval = stm32x_get_device_id(bank, &dbgmcu_idcode);
830  if (retval != ERROR_OK)
831  return retval;
832 
833  LOG_INFO("device id = 0x%08" PRIx32, dbgmcu_idcode);
834 
835  uint16_t device_id = dbgmcu_idcode & 0xfff;
836  uint16_t rev_id = dbgmcu_idcode >> 16;
837 
838  /* set page size, protection granularity and max flash size depending on family */
839  switch (device_id) {
840  case 0x440: /* stm32f05x */
841  page_size = 1024;
842  stm32x_info->ppage_size = 4;
843  max_flash_size_in_kb = 64;
844  stm32x_info->user_data_offset = 16;
845  stm32x_info->option_offset = 6;
846  stm32x_info->default_rdp = 0xAA;
847  stm32x_info->can_load_options = true;
848  break;
849  case 0x444: /* stm32f03x */
850  case 0x445: /* stm32f04x */
851  page_size = 1024;
852  stm32x_info->ppage_size = 4;
853  max_flash_size_in_kb = 32;
854  stm32x_info->user_data_offset = 16;
855  stm32x_info->option_offset = 6;
856  stm32x_info->default_rdp = 0xAA;
857  stm32x_info->can_load_options = true;
858  break;
859  case 0x448: /* stm32f07x */
860  page_size = 2048;
861  stm32x_info->ppage_size = 4;
862  max_flash_size_in_kb = 128;
863  stm32x_info->user_data_offset = 16;
864  stm32x_info->option_offset = 6;
865  stm32x_info->default_rdp = 0xAA;
866  stm32x_info->can_load_options = true;
867  break;
868  case 0x442: /* stm32f09x */
869  page_size = 2048;
870  stm32x_info->ppage_size = 4;
871  max_flash_size_in_kb = 256;
872  stm32x_info->user_data_offset = 16;
873  stm32x_info->option_offset = 6;
874  stm32x_info->default_rdp = 0xAA;
875  stm32x_info->can_load_options = true;
876  break;
877  case 0x410: /* stm32f1x medium-density */
878  page_size = 1024;
879  stm32x_info->ppage_size = 4;
880  max_flash_size_in_kb = 128;
881  /* GigaDevice GD32F1x0 & GD32F3x0 & GD32E23x series devices
882  share DEV_ID with STM32F101/2/3 medium-density line,
883  however they use a REV_ID different from any STM32 device.
884  The main difference is another offset of user option bits
885  (like WDG_SW, nRST_STOP, nRST_STDBY) in option byte register
886  (FLASH_OBR/FMC_OBSTAT 0x4002201C).
887  This caused problems e.g. during flash block programming
888  because of unexpected active hardware watchog. */
889  switch (rev_id) {
890  case 0x1303: /* gd32f1x0 */
891  stm32x_info->user_data_offset = 16;
892  stm32x_info->option_offset = 6;
893  max_flash_size_in_kb = 64;
894  stm32x_info->can_load_options = true;
895  break;
896  case 0x1704: /* gd32f3x0 */
897  stm32x_info->user_data_offset = 16;
898  stm32x_info->option_offset = 6;
899  stm32x_info->can_load_options = true;
900  break;
901  case 0x1906: /* gd32vf103 */
902  break;
903  case 0x1909: /* gd32e23x */
904  stm32x_info->user_data_offset = 16;
905  stm32x_info->option_offset = 6;
906  max_flash_size_in_kb = 64;
907  stm32x_info->can_load_options = true;
908  break;
909  }
910  break;
911  case 0x412: /* stm32f1x low-density */
912  page_size = 1024;
913  stm32x_info->ppage_size = 4;
914  max_flash_size_in_kb = 32;
915  break;
916  case 0x414: /* stm32f1x high-density */
917  page_size = 2048;
918  stm32x_info->ppage_size = 2;
919  max_flash_size_in_kb = 512;
920  break;
921  case 0x418: /* stm32f1x connectivity */
922  page_size = 2048;
923  stm32x_info->ppage_size = 2;
924  max_flash_size_in_kb = 256;
925  break;
926  case 0x430: /* stm32f1 XL-density (dual flash banks) */
927  page_size = 2048;
928  stm32x_info->ppage_size = 2;
929  max_flash_size_in_kb = 1024;
930  stm32x_info->has_dual_banks = true;
931  break;
932  case 0x420: /* stm32f100xx low- and medium-density value line */
933  page_size = 1024;
934  stm32x_info->ppage_size = 4;
935  max_flash_size_in_kb = 128;
936  break;
937  case 0x428: /* stm32f100xx high-density value line */
938  page_size = 2048;
939  stm32x_info->ppage_size = 4;
940  max_flash_size_in_kb = 512;
941  break;
942  case 0x422: /* stm32f302/3xb/c */
943  page_size = 2048;
944  stm32x_info->ppage_size = 2;
945  max_flash_size_in_kb = 256;
946  stm32x_info->user_data_offset = 16;
947  stm32x_info->option_offset = 6;
948  stm32x_info->default_rdp = 0xAA;
949  stm32x_info->can_load_options = true;
950  break;
951  case 0x446: /* stm32f303xD/E */
952  page_size = 2048;
953  stm32x_info->ppage_size = 2;
954  max_flash_size_in_kb = 512;
955  stm32x_info->user_data_offset = 16;
956  stm32x_info->option_offset = 6;
957  stm32x_info->default_rdp = 0xAA;
958  stm32x_info->can_load_options = true;
959  break;
960  case 0x432: /* stm32f37x */
961  page_size = 2048;
962  stm32x_info->ppage_size = 2;
963  max_flash_size_in_kb = 256;
964  stm32x_info->user_data_offset = 16;
965  stm32x_info->option_offset = 6;
966  stm32x_info->default_rdp = 0xAA;
967  stm32x_info->can_load_options = true;
968  break;
969  case 0x438: /* stm32f33x */
970  case 0x439: /* stm32f302x6/8 */
971  page_size = 2048;
972  stm32x_info->ppage_size = 2;
973  max_flash_size_in_kb = 64;
974  stm32x_info->user_data_offset = 16;
975  stm32x_info->option_offset = 6;
976  stm32x_info->default_rdp = 0xAA;
977  stm32x_info->can_load_options = true;
978  break;
979  default:
980  LOG_WARNING("Cannot identify target as a STM32 family.");
981  return ERROR_FAIL;
982  }
983 
984  /* get flash size from target. */
985  retval = stm32x_get_flash_size(bank, &flash_size_in_kb);
986 
987  /* failed reading flash size or flash size invalid (early silicon),
988  * default to max target family */
989  if (retval != ERROR_OK || flash_size_in_kb == 0xffff || flash_size_in_kb == 0) {
990  LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming %dk flash",
991  max_flash_size_in_kb);
992  flash_size_in_kb = max_flash_size_in_kb;
993  }
994 
995  if (stm32x_info->has_dual_banks) {
996  /* split reported size into matching bank */
997  if (bank->base != 0x08080000) {
998  /* bank 0 will be fixed 512k */
999  flash_size_in_kb = 512;
1000  } else {
1001  flash_size_in_kb -= 512;
1002  /* bank1 also uses a register offset */
1003  stm32x_info->register_base = FLASH_REG_BASE_B1;
1004  base_address = 0x08080000;
1005  }
1006  }
1007 
1008  /* if the user sets the size manually then ignore the probed value
1009  * this allows us to work around devices that have a invalid flash size register value */
1010  if (stm32x_info->user_bank_size) {
1011  LOG_INFO("ignoring flash probed value, using configured bank size");
1012  flash_size_in_kb = stm32x_info->user_bank_size / 1024;
1013  }
1014 
1015  LOG_INFO("flash size = %d KiB", flash_size_in_kb);
1016 
1017  /* did we assign flash size? */
1018  assert(flash_size_in_kb != 0xffff);
1019 
1020  /* calculate numbers of pages */
1021  int num_pages = flash_size_in_kb * 1024 / page_size;
1022 
1023  /* check that calculation result makes sense */
1024  assert(num_pages > 0);
1025 
1026  free(bank->sectors);
1027  bank->sectors = NULL;
1028 
1029  free(bank->prot_blocks);
1030  bank->prot_blocks = NULL;
1031 
1032  bank->base = base_address;
1033  bank->size = (num_pages * page_size);
1034 
1035  bank->num_sectors = num_pages;
1036  bank->sectors = alloc_block_array(0, page_size, num_pages);
1037  if (!bank->sectors)
1038  return ERROR_FAIL;
1039 
1040  /* calculate number of write protection blocks */
1041  int num_prot_blocks = num_pages / stm32x_info->ppage_size;
1042  if (num_prot_blocks > 32)
1043  num_prot_blocks = 32;
1044 
1045  bank->num_prot_blocks = num_prot_blocks;
1046  bank->prot_blocks = alloc_block_array(0, stm32x_info->ppage_size * page_size, num_prot_blocks);
1047  if (!bank->prot_blocks)
1048  return ERROR_FAIL;
1049 
1050  if (num_prot_blocks == 32)
1051  bank->prot_blocks[31].size = (num_pages - (31 * stm32x_info->ppage_size)) * page_size;
1052 
1053  stm32x_info->probed = true;
1054 
1055  return ERROR_OK;
1056 }
1057 
1058 static int stm32x_auto_probe(struct flash_bank *bank)
1059 {
1060  struct stm32x_flash_bank *stm32x_info = bank->driver_priv;
1061  if (stm32x_info->probed)
1062  return ERROR_OK;
1063  return stm32x_probe(bank);
1064 }
1065 
1066 #if 0
1067 COMMAND_HANDLER(stm32x_handle_part_id_command)
1068 {
1069  return ERROR_OK;
1070 }
1071 #endif
1072 
1073 static const char *get_stm32f0_revision(uint16_t rev_id)
1074 {
1075  const char *rev_str = NULL;
1076 
1077  switch (rev_id) {
1078  case 0x1000:
1079  rev_str = "1.0";
1080  break;
1081  case 0x2000:
1082  rev_str = "2.0";
1083  break;
1084  }
1085  return rev_str;
1086 }
1087 
1089 {
1090  uint32_t dbgmcu_idcode;
1091 
1092  /* read stm32 device id register */
1093  int retval = stm32x_get_device_id(bank, &dbgmcu_idcode);
1094  if (retval != ERROR_OK)
1095  return retval;
1096 
1097  uint16_t device_id = dbgmcu_idcode & 0xfff;
1098  uint16_t rev_id = dbgmcu_idcode >> 16;
1099  const char *device_str;
1100  const char *rev_str = NULL;
1101 
1102  switch (device_id) {
1103  case 0x410:
1104  device_str = "STM32F10x (Medium Density)";
1105 
1106  switch (rev_id) {
1107  case 0x0000:
1108  rev_str = "A";
1109  break;
1110 
1111  case 0x1303: /* gd32f1x0 */
1112  device_str = "GD32F1x0";
1113  break;
1114 
1115  case 0x1704: /* gd32f3x0 */
1116  device_str = "GD32F3x0";
1117  break;
1118 
1119  case 0x1906:
1120  device_str = "GD32VF103";
1121  break;
1122 
1123  case 0x1909: /* gd32e23x */
1124  device_str = "GD32E23x";
1125  break;
1126 
1127  case 0x2000:
1128  rev_str = "B";
1129  break;
1130 
1131  case 0x2001:
1132  rev_str = "Z";
1133  break;
1134 
1135  case 0x2003:
1136  rev_str = "Y";
1137  break;
1138  }
1139  break;
1140 
1141  case 0x412:
1142  device_str = "STM32F10x (Low Density)";
1143 
1144  switch (rev_id) {
1145  case 0x1000:
1146  rev_str = "A";
1147  break;
1148  }
1149  break;
1150 
1151  case 0x414:
1152  device_str = "STM32F10x (High Density)";
1153 
1154  switch (rev_id) {
1155  case 0x1000:
1156  rev_str = "A";
1157  break;
1158 
1159  case 0x1001:
1160  rev_str = "Z";
1161  break;
1162 
1163  case 0x1003:
1164  rev_str = "Y";
1165  break;
1166  }
1167  break;
1168 
1169  case 0x418:
1170  device_str = "STM32F10x (Connectivity)";
1171 
1172  switch (rev_id) {
1173  case 0x1000:
1174  rev_str = "A";
1175  break;
1176 
1177  case 0x1001:
1178  rev_str = "Z";
1179  break;
1180  }
1181  break;
1182 
1183  case 0x420:
1184  device_str = "STM32F100 (Low/Medium Density)";
1185 
1186  switch (rev_id) {
1187  case 0x1000:
1188  rev_str = "A";
1189  break;
1190 
1191  case 0x1001:
1192  rev_str = "Z";
1193  break;
1194  }
1195  break;
1196 
1197  case 0x422:
1198  device_str = "STM32F302xB/C";
1199 
1200  switch (rev_id) {
1201  case 0x1000:
1202  rev_str = "A";
1203  break;
1204 
1205  case 0x1001:
1206  rev_str = "Z";
1207  break;
1208 
1209  case 0x1003:
1210  rev_str = "Y";
1211  break;
1212 
1213  case 0x2000:
1214  rev_str = "B";
1215  break;
1216  }
1217  break;
1218 
1219  case 0x428:
1220  device_str = "STM32F100 (High Density)";
1221 
1222  switch (rev_id) {
1223  case 0x1000:
1224  rev_str = "A";
1225  break;
1226 
1227  case 0x1001:
1228  rev_str = "Z";
1229  break;
1230  }
1231  break;
1232 
1233  case 0x430:
1234  device_str = "STM32F10x (XL Density)";
1235 
1236  switch (rev_id) {
1237  case 0x1000:
1238  rev_str = "A";
1239  break;
1240  }
1241  break;
1242 
1243  case 0x432:
1244  device_str = "STM32F37x";
1245 
1246  switch (rev_id) {
1247  case 0x1000:
1248  rev_str = "A";
1249  break;
1250 
1251  case 0x2000:
1252  rev_str = "B";
1253  break;
1254  }
1255  break;
1256 
1257  case 0x438:
1258  device_str = "STM32F33x";
1259 
1260  switch (rev_id) {
1261  case 0x1000:
1262  rev_str = "A";
1263  break;
1264  }
1265  break;
1266 
1267  case 0x439:
1268  device_str = "STM32F302x6/8";
1269 
1270  switch (rev_id) {
1271  case 0x1000:
1272  rev_str = "A";
1273  break;
1274 
1275  case 0x1001:
1276  rev_str = "Z";
1277  break;
1278  }
1279  break;
1280 
1281  case 0x444:
1282  device_str = "STM32F03x";
1283  rev_str = get_stm32f0_revision(rev_id);
1284  break;
1285 
1286  case 0x440:
1287  device_str = "STM32F05x";
1288  rev_str = get_stm32f0_revision(rev_id);
1289  break;
1290 
1291  case 0x445:
1292  device_str = "STM32F04x";
1293  rev_str = get_stm32f0_revision(rev_id);
1294  break;
1295 
1296  case 0x446:
1297  device_str = "STM32F303xD/E";
1298  switch (rev_id) {
1299  case 0x1000:
1300  rev_str = "A";
1301  break;
1302  }
1303  break;
1304 
1305  case 0x448:
1306  device_str = "STM32F07x";
1307  rev_str = get_stm32f0_revision(rev_id);
1308  break;
1309 
1310  case 0x442:
1311  device_str = "STM32F09x";
1312  rev_str = get_stm32f0_revision(rev_id);
1313  break;
1314 
1315  default:
1316  command_print_sameline(cmd, "Cannot identify target as a STM32F0/1/3\n");
1317  return ERROR_FAIL;
1318  }
1319 
1320  if (rev_str)
1321  command_print_sameline(cmd, "%s - Rev: %s", device_str, rev_str);
1322  else
1323  command_print_sameline(cmd, "%s - Rev: unknown (0x%04x)", device_str, rev_id);
1324 
1325  return ERROR_OK;
1326 }
1327 
1328 COMMAND_HANDLER(stm32x_handle_lock_command)
1329 {
1330  struct target *target = NULL;
1331  struct stm32x_flash_bank *stm32x_info = NULL;
1332 
1333  if (CMD_ARGC < 1)
1335 
1336  struct flash_bank *bank;
1337  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
1338  if (retval != ERROR_OK)
1339  return retval;
1340 
1341  stm32x_info = bank->driver_priv;
1342 
1343  target = bank->target;
1344 
1345  if (target->state != TARGET_HALTED) {
1346  LOG_ERROR("Target not halted");
1347  return ERROR_TARGET_NOT_HALTED;
1348  }
1349 
1351  if (retval != ERROR_OK)
1352  return retval;
1353 
1355  command_print(CMD, "stm32x failed to erase options");
1356  return ERROR_OK;
1357  }
1358 
1359  /* set readout protection */
1360  stm32x_info->option_bytes.rdp = 0;
1361 
1363  command_print(CMD, "stm32x failed to lock device");
1364  return ERROR_OK;
1365  }
1366 
1367  command_print(CMD, "stm32x locked");
1368 
1369  return ERROR_OK;
1370 }
1371 
1372 COMMAND_HANDLER(stm32x_handle_unlock_command)
1373 {
1374  struct target *target = NULL;
1375 
1376  if (CMD_ARGC < 1)
1378 
1379  struct flash_bank *bank;
1380  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
1381  if (retval != ERROR_OK)
1382  return retval;
1383 
1384  target = bank->target;
1385 
1386  if (target->state != TARGET_HALTED) {
1387  LOG_ERROR("Target not halted");
1388  return ERROR_TARGET_NOT_HALTED;
1389  }
1390 
1392  if (retval != ERROR_OK)
1393  return retval;
1394 
1396  command_print(CMD, "stm32x failed to erase options");
1397  return ERROR_OK;
1398  }
1399 
1401  command_print(CMD, "stm32x failed to unlock device");
1402  return ERROR_OK;
1403  }
1404 
1405  command_print(CMD, "stm32x unlocked.\n"
1406  "INFO: a reset or power cycle is required "
1407  "for the new settings to take effect.");
1408 
1409  return ERROR_OK;
1410 }
1411 
1412 COMMAND_HANDLER(stm32x_handle_options_read_command)
1413 {
1414  uint32_t optionbyte, protection;
1415  struct target *target = NULL;
1416  struct stm32x_flash_bank *stm32x_info = NULL;
1417 
1418  if (CMD_ARGC < 1)
1420 
1421  struct flash_bank *bank;
1422  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
1423  if (retval != ERROR_OK)
1424  return retval;
1425 
1426  stm32x_info = bank->driver_priv;
1427 
1428  target = bank->target;
1429 
1430  if (target->state != TARGET_HALTED) {
1431  LOG_ERROR("Target not halted");
1432  return ERROR_TARGET_NOT_HALTED;
1433  }
1434 
1436  if (retval != ERROR_OK)
1437  return retval;
1438 
1439  retval = target_read_u32(target, STM32_FLASH_OBR_B0, &optionbyte);
1440  if (retval != ERROR_OK)
1441  return retval;
1442 
1443  uint16_t user_data = optionbyte >> stm32x_info->user_data_offset;
1444 
1446  if (retval != ERROR_OK)
1447  return retval;
1448 
1449  if (optionbyte & (1 << OPT_ERROR))
1450  command_print(CMD, "option byte complement error");
1451 
1452  command_print(CMD, "option byte register = 0x%" PRIx32, optionbyte);
1453  command_print(CMD, "write protection register = 0x%" PRIx32, protection);
1454 
1455  command_print(CMD, "read protection: %s",
1456  (optionbyte & (1 << OPT_READOUT)) ? "on" : "off");
1457 
1458  /* user option bytes are offset depending on variant */
1459  optionbyte >>= stm32x_info->option_offset;
1460 
1461  command_print(CMD, "watchdog: %sware",
1462  (optionbyte & (1 << OPT_RDWDGSW)) ? "soft" : "hard");
1463 
1464  command_print(CMD, "stop mode: %sreset generated upon entry",
1465  (optionbyte & (1 << OPT_RDRSTSTOP)) ? "no " : "");
1466 
1467  command_print(CMD, "standby mode: %sreset generated upon entry",
1468  (optionbyte & (1 << OPT_RDRSTSTDBY)) ? "no " : "");
1469 
1470  if (stm32x_info->has_dual_banks)
1471  command_print(CMD, "boot: bank %d", (optionbyte & (1 << OPT_BFB2)) ? 0 : 1);
1472 
1473  command_print(CMD, "user data = 0x%02" PRIx16, user_data);
1474 
1475  return ERROR_OK;
1476 }
1477 
1478 COMMAND_HANDLER(stm32x_handle_options_write_command)
1479 {
1480  struct target *target = NULL;
1481  struct stm32x_flash_bank *stm32x_info = NULL;
1482  uint8_t optionbyte;
1483  uint16_t useropt;
1484 
1485  if (CMD_ARGC < 2)
1487 
1488  struct flash_bank *bank;
1489  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
1490  if (retval != ERROR_OK)
1491  return retval;
1492 
1493  stm32x_info = bank->driver_priv;
1494 
1495  target = bank->target;
1496 
1497  if (target->state != TARGET_HALTED) {
1498  LOG_ERROR("Target not halted");
1499  return ERROR_TARGET_NOT_HALTED;
1500  }
1501 
1503  if (retval != ERROR_OK)
1504  return retval;
1505 
1506  retval = stm32x_read_options(bank);
1507  if (retval != ERROR_OK)
1508  return retval;
1509 
1510  /* start with current options */
1511  optionbyte = stm32x_info->option_bytes.user;
1512  useropt = stm32x_info->option_bytes.data;
1513 
1514  /* skip over flash bank */
1515  CMD_ARGC--;
1516  CMD_ARGV++;
1517 
1518  while (CMD_ARGC) {
1519  if (strcmp("SWWDG", CMD_ARGV[0]) == 0)
1520  optionbyte |= (1 << 0);
1521  else if (strcmp("HWWDG", CMD_ARGV[0]) == 0)
1522  optionbyte &= ~(1 << 0);
1523  else if (strcmp("NORSTSTOP", CMD_ARGV[0]) == 0)
1524  optionbyte |= (1 << 1);
1525  else if (strcmp("RSTSTOP", CMD_ARGV[0]) == 0)
1526  optionbyte &= ~(1 << 1);
1527  else if (strcmp("NORSTSTNDBY", CMD_ARGV[0]) == 0)
1528  optionbyte |= (1 << 2);
1529  else if (strcmp("RSTSTNDBY", CMD_ARGV[0]) == 0)
1530  optionbyte &= ~(1 << 2);
1531  else if (strcmp("USEROPT", CMD_ARGV[0]) == 0) {
1532  if (CMD_ARGC < 2)
1534  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], useropt);
1535  CMD_ARGC--;
1536  CMD_ARGV++;
1537  } else if (stm32x_info->has_dual_banks) {
1538  if (strcmp("BOOT0", CMD_ARGV[0]) == 0)
1539  optionbyte |= (1 << 3);
1540  else if (strcmp("BOOT1", CMD_ARGV[0]) == 0)
1541  optionbyte &= ~(1 << 3);
1542  else
1544  } else
1546  CMD_ARGC--;
1547  CMD_ARGV++;
1548  }
1549 
1551  command_print(CMD, "stm32x failed to erase options");
1552  return ERROR_OK;
1553  }
1554 
1555  stm32x_info->option_bytes.user = optionbyte;
1556  stm32x_info->option_bytes.data = useropt;
1557 
1559  command_print(CMD, "stm32x failed to write options");
1560  return ERROR_OK;
1561  }
1562 
1563  command_print(CMD, "stm32x write options complete.\n"
1564  "INFO: %spower cycle is required "
1565  "for the new settings to take effect.",
1566  stm32x_info->can_load_options
1567  ? "'stm32f1x options_load' command or " : "");
1568 
1569  return ERROR_OK;
1570 }
1571 
1572 COMMAND_HANDLER(stm32x_handle_options_load_command)
1573 {
1574  if (CMD_ARGC < 1)
1576 
1577  struct flash_bank *bank;
1578  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
1579  if (retval != ERROR_OK)
1580  return retval;
1581 
1582  struct stm32x_flash_bank *stm32x_info = bank->driver_priv;
1583 
1584  if (!stm32x_info->can_load_options) {
1585  LOG_ERROR("Command not applicable to stm32f1x devices - power cycle is "
1586  "required instead.");
1587  return ERROR_FAIL;
1588  }
1589 
1590  struct target *target = bank->target;
1591 
1592  if (target->state != TARGET_HALTED) {
1593  LOG_ERROR("Target not halted");
1594  return ERROR_TARGET_NOT_HALTED;
1595  }
1596 
1598  if (retval != ERROR_OK)
1599  return retval;
1600 
1601  /* unlock option flash registers */
1603  if (retval != ERROR_OK)
1604  return retval;
1606  if (retval != ERROR_OK) {
1608  return retval;
1609  }
1610 
1611  /* force re-load of option bytes - generates software reset */
1613  if (retval != ERROR_OK)
1614  return retval;
1615 
1616  return ERROR_OK;
1617 }
1618 
1619 static int stm32x_mass_erase(struct flash_bank *bank)
1620 {
1621  struct target *target = bank->target;
1622 
1623  if (target->state != TARGET_HALTED) {
1624  LOG_ERROR("Target not halted");
1625  return ERROR_TARGET_NOT_HALTED;
1626  }
1627 
1628  /* unlock option flash registers */
1630  if (retval != ERROR_OK)
1631  return retval;
1633  if (retval != ERROR_OK)
1634  goto flash_lock;
1635 
1636  /* mass erase flash memory */
1638  if (retval != ERROR_OK)
1639  goto flash_lock;
1641  FLASH_MER | FLASH_STRT);
1642  if (retval != ERROR_OK)
1643  goto flash_lock;
1644 
1646 
1647 flash_lock:
1648  {
1650  if (retval == ERROR_OK)
1651  retval = retval2;
1652  }
1653  return retval;
1654 }
1655 
1656 COMMAND_HANDLER(stm32x_handle_mass_erase_command)
1657 {
1658  if (CMD_ARGC < 1)
1660 
1661  struct flash_bank *bank;
1662  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
1663  if (retval != ERROR_OK)
1664  return retval;
1665 
1666  retval = stm32x_mass_erase(bank);
1667  if (retval == ERROR_OK)
1668  command_print(CMD, "stm32x mass erase complete");
1669  else
1670  command_print(CMD, "stm32x mass erase failed");
1671 
1672  return retval;
1673 }
1674 
1675 static const struct command_registration stm32f1x_exec_command_handlers[] = {
1676  {
1677  .name = "lock",
1678  .handler = stm32x_handle_lock_command,
1679  .mode = COMMAND_EXEC,
1680  .usage = "bank_id",
1681  .help = "Lock entire flash device.",
1682  },
1683  {
1684  .name = "unlock",
1685  .handler = stm32x_handle_unlock_command,
1686  .mode = COMMAND_EXEC,
1687  .usage = "bank_id",
1688  .help = "Unlock entire protected flash device.",
1689  },
1690  {
1691  .name = "mass_erase",
1692  .handler = stm32x_handle_mass_erase_command,
1693  .mode = COMMAND_EXEC,
1694  .usage = "bank_id",
1695  .help = "Erase entire flash device.",
1696  },
1697  {
1698  .name = "options_read",
1699  .handler = stm32x_handle_options_read_command,
1700  .mode = COMMAND_EXEC,
1701  .usage = "bank_id",
1702  .help = "Read and display device option bytes.",
1703  },
1704  {
1705  .name = "options_write",
1706  .handler = stm32x_handle_options_write_command,
1707  .mode = COMMAND_EXEC,
1708  .usage = "bank_id ('SWWDG'|'HWWDG') "
1709  "('RSTSTNDBY'|'NORSTSTNDBY') "
1710  "('RSTSTOP'|'NORSTSTOP') ('USEROPT' user_data)",
1711  .help = "Replace bits in device option bytes.",
1712  },
1713  {
1714  .name = "options_load",
1715  .handler = stm32x_handle_options_load_command,
1716  .mode = COMMAND_EXEC,
1717  .usage = "bank_id",
1718  .help = "Force re-load of device option bytes.",
1719  },
1721 };
1722 
1723 static const struct command_registration stm32f1x_command_handlers[] = {
1724  {
1725  .name = "stm32f1x",
1726  .mode = COMMAND_ANY,
1727  .help = "stm32f1x flash command group",
1728  .usage = "",
1730  },
1732 };
1733 
1734 const struct flash_driver stm32f1x_flash = {
1735  .name = "stm32f1x",
1736  .commands = stm32f1x_command_handlers,
1737  .flash_bank_command = stm32x_flash_bank_command,
1738  .erase = stm32x_erase,
1739  .protect = stm32x_protect,
1740  .write = stm32x_write,
1741  .read = default_flash_read,
1742  .probe = stm32x_probe,
1743  .auto_probe = stm32x_auto_probe,
1744  .erase_check = default_flash_blank_check,
1745  .protect_check = stm32x_protect_check,
1746  .info = get_stm32x_info,
1747  .free_driver_priv = default_flash_free_driver_priv,
1748 };
void init_reg_param(struct reg_param *param, const char *reg_name, uint32_t size, enum param_direction direction)
Definition: algorithm.c:29
void destroy_reg_param(struct reg_param *param)
Definition: algorithm.c:38
@ PARAM_OUT
Definition: algorithm.h:16
@ PARAM_IN_OUT
Definition: algorithm.h:17
static bool is_arm(struct arm *arm)
Definition: arm.h:268
@ ARM_MODE_THREAD
Definition: arm.h:94
static struct arm * target_to_arm(const struct target *target)
Convert target handle to generic ARM target state handle.
Definition: arm.h:262
#define ARMV7M_COMMON_MAGIC
Definition: armv7m.h:229
Support functions to access arbitrary bits in a byte array.
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
Definition: binarybuffer.h:104
static void buf_set_u32(uint8_t *_buffer, unsigned int first, unsigned int num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
Definition: binarybuffer.h:34
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
Definition: command.c:378
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:389
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:146
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
Definition: command.h:123
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:161
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:405
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:156
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
Definition: command.h:445
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:256
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
static enum cortex_m_impl_part cortex_m_get_impl_part(struct target *target)
Definition: cortex_m.h:379
@ CORTEX_M4_PARTNO
Definition: cortex_m.h:59
@ CORTEX_M0_PARTNO
Definition: cortex_m.h:56
@ CORTEX_M0P_PARTNO
Definition: cortex_m.h:61
@ CORTEX_M23_PARTNO
Definition: cortex_m.h:62
@ CORTEX_M_PARTNO_INVALID
Definition: cortex_m.h:54
@ CORTEX_M3_PARTNO
Definition: cortex_m.h:58
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
uint32_t page_size
Page size.
Definition: dw-spi-helper.h:3
uint32_t buffer_size
Size of dw_spi_program::buffer.
Definition: dw-spi-helper.h:5
uint32_t address
Starting address. Sector aligned.
Definition: dw-spi-helper.h:0
uint8_t bank
Definition: esirisc.c:135
#define ERROR_FLASH_PROTECTED
Definition: flash/common.h:37
#define ERROR_FLASH_OPERATION_FAILED
Definition: flash/common.h:30
#define ERROR_FLASH_BUSY
Definition: flash/common.h:33
struct flash_sector * alloc_block_array(uint32_t offset, uint32_t size, unsigned int num_blocks)
Allocate and fill an array of sectors or protection blocks.
int default_flash_blank_check(struct flash_bank *bank)
Provides default erased-bank check handling.
int default_flash_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
Provides default read implementation for flash memory.
void default_flash_free_driver_priv(struct flash_bank *bank)
Deallocates bank->driver_priv.
void alive_sleep(uint64_t ms)
Definition: log.c:478
#define LOG_WARNING(expr ...)
Definition: log.h:144
#define ERROR_FAIL
Definition: log.h:188
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define LOG_INFO(expr ...)
Definition: log.h:141
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
uint8_t protection
Definition: qn908x.c:1
#define RISCV_COMMON_MAGIC
Definition: riscv.h:18
target_addr_t addr
Start address to search for the control block.
Definition: rtt/rtt.c:28
struct rtt_source source
Definition: rtt/rtt.c:23
static int stm32x_get_property_addr(struct target *target, struct stm32x_property_addr *addr)
Definition: stm32f1x.c:744
static int stm32x_auto_probe(struct flash_bank *bank)
Definition: stm32f1x.c:1058
static const struct command_registration stm32f1x_exec_command_handlers[]
Definition: stm32f1x.c:1675
#define STM32_FLASH_AR
Definition: stm32f1x.c:36
static int stm32x_probe(struct flash_bank *bank)
Definition: stm32f1x.c:811
#define FLASH_ERASE_TIMEOUT
Definition: stm32f1x.c:99
static const struct command_registration stm32f1x_command_handlers[]
Definition: stm32f1x.c:1723
#define FLASH_OBL_LAUNCH
Definition: stm32f1x.c:73
static int get_stm32x_info(struct flash_bank *bank, struct command_invocation *cmd)
Definition: stm32f1x.c:1088
#define FLASH_PGERR
Definition: stm32f1x.c:78
#define STM32_FLASH_OBR_B0
Definition: stm32f1x.c:49
#define OPT_RDRSTSTOP
Definition: stm32f1x.c:87
#define FLASH_OPTWRE
Definition: stm32f1x.c:72
static int stm32x_get_flash_status(struct flash_bank *bank, uint32_t *status)
Definition: stm32f1x.c:157
#define FLASH_PG
Definition: stm32f1x.c:65
#define FLASH_MER
Definition: stm32f1x.c:67
#define FLASH_OPTER
Definition: stm32f1x.c:69
#define KEY2
Definition: stm32f1x.c:94
static int stm32x_get_flash_reg(struct flash_bank *bank, uint32_t reg)
Definition: stm32f1x.c:151
static int stm32x_check_operation_supported(struct flash_bank *bank)
Definition: stm32f1x.c:205
static int stm32x_get_flash_size(struct flash_bank *bank, uint16_t *flash_size_in_kb)
Definition: stm32f1x.c:799
static int stm32x_erase_options(struct flash_bank *bank)
Definition: stm32f1x.c:243
#define OPT_ERROR
Definition: stm32f1x.c:84
FLASH_BANK_COMMAND_HANDLER(stm32x_flash_bank_command)
Definition: stm32f1x.c:129
static int stm32x_read_options(struct flash_bank *bank)
Definition: stm32f1x.c:219
#define STM32_FLASH_WRPR_B0
Definition: stm32f1x.c:50
#define STM32_FLASH_OPTKEYR_B0
Definition: stm32f1x.c:45
static int stm32x_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: stm32f1x.c:697
#define FLASH_PER
Definition: stm32f1x.c:66
static int stm32x_get_device_id(struct flash_bank *bank, uint32_t *device_id)
Definition: stm32f1x.c:787
#define FLASH_BSY
Definition: stm32f1x.c:77
#define STM32_FLASH_KEYR
Definition: stm32f1x.c:32
#define FLASH_REG_BASE_B0
Definition: stm32f1x.c:28
#define OPT_READOUT
Definition: stm32f1x.c:85
#define STM32_FLASH_KEYR_B0
Definition: stm32f1x.c:44
static int stm32x_write_options(struct flash_bank *bank)
Definition: stm32f1x.c:290
COMMAND_HANDLER(stm32x_handle_lock_command)
Definition: stm32f1x.c:1328
#define STM32_OB_RDP
Definition: stm32f1x.c:54
#define FLASH_LOCK
Definition: stm32f1x.c:71
static int stm32x_write_block_riscv(struct flash_bank *bank, const uint8_t *buffer, uint32_t address, uint32_t hwords_count)
Definition: stm32f1x.c:544
static int stm32x_write_block(struct flash_bank *bank, const uint8_t *buffer, uint32_t address, uint32_t hwords_count)
Writes a block to flash either using target algorithm or use fallback, host controlled halfword-by-ha...
Definition: stm32f1x.c:655
static int stm32x_wait_status_busy(struct flash_bank *bank, int timeout)
Definition: stm32f1x.c:163
#define OPT_RDRSTSTDBY
Definition: stm32f1x.c:88
#define OPT_RDWDGSW
Definition: stm32f1x.c:86
static int stm32x_mass_erase(struct flash_bank *bank)
Definition: stm32f1x.c:1619
#define FLASH_STRT
Definition: stm32f1x.c:70
#define STM32_FLASH_CR
Definition: stm32f1x.c:35
#define OPT_BFB2
Definition: stm32f1x.c:89
static int stm32x_write_block_async(struct flash_bank *bank, const uint8_t *buffer, uint32_t address, uint32_t hwords_count)
Definition: stm32f1x.c:446
static int stm32x_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
Definition: stm32f1x.c:367
#define STM32_FLASH_SR
Definition: stm32f1x.c:34
#define FLASH_OPTPG
Definition: stm32f1x.c:68
static const char * get_stm32f0_revision(uint16_t rev_id)
Definition: stm32f1x.c:1073
static int stm32x_protect_check(struct flash_bank *bank)
Definition: stm32f1x.c:345
const struct flash_driver stm32f1x_flash
Definition: stm32f1x.c:1734
#define FLASH_WRPRTERR
Definition: stm32f1x.c:79
#define FLASH_REG_BASE_B1
Definition: stm32f1x.c:29
static int stm32x_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
Definition: stm32f1x.c:415
#define KEY1
Definition: stm32f1x.c:93
#define STM32_FLASH_CR_B0
Definition: stm32f1x.c:47
Represents a generic ARM core, with standard application registers.
Definition: arm.h:176
unsigned int common_magic
Definition: armv7m.h:306
enum arm_mode core_mode
Definition: armv7m.h:308
When run_command is called, a new instance will be created on the stack, filled with the proper value...
Definition: command.h:76
const char * name
Definition: command.h:239
Provides details of a flash bank, available either on-chip or through a major interface.
Definition: nor/core.h:75
Provides the implementation-independent structure that defines all of the callbacks required by OpenO...
Definition: nor/driver.h:39
const char * name
Gives a human-readable name of this flash driver, This field is used to select and initialize the dri...
Definition: nor/driver.h:44
uint8_t * value
Definition: algorithm.h:30
Definition: register.h:111
Architecture info for RISC-V algorithms.
Definition: riscv.h:182
unsigned int common_magic
Definition: riscv.h:183
uint32_t user_bank_size
Definition: stm32f1x.c:120
uint8_t default_rdp
Definition: stm32f1x.c:117
struct stm32x_options option_bytes
Definition: stm32f1x.c:109
bool can_load_options
Definition: stm32f1x.c:115
uint32_t register_base
Definition: stm32f1x.c:116
uint8_t rdp
Definition: stm32f1x.c:102
uint32_t protection
Definition: stm32f1x.c:105
uint8_t user
Definition: stm32f1x.c:103
uint16_t data
Definition: stm32f1x.c:104
uint32_t device_id
Definition: stm32f1x.c:740
uint32_t flash_size
Definition: stm32f1x.c:741
Definition: target.h:119
enum target_state state
Definition: target.h:167
Definition: psoc6.c:83
target_addr_t address
Definition: target.h:89
void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
Definition: target.c:381
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
Definition: target.c:2423
int target_run_algorithm(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_param, target_addr_t entry_point, target_addr_t exit_point, unsigned int timeout_ms, void *arch_info)
Downloads a target-specific native code algorithm to the target, and executes it.
Definition: target.c:787
unsigned int target_address_bits(struct target *target)
Return the number of address bits this target supports.
Definition: target.c:1496
int target_write_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Write count items of size bytes to the memory of target at the address given.
Definition: target.c:1302
uint32_t target_get_working_area_avail(struct target *target)
Definition: target.c:2233
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:2131
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
Definition: target.c:2689
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
Definition: target.c:2187
int target_read_u16(struct target *target, target_addr_t address, uint16_t *value)
Definition: target.c:2635
int target_run_flash_async_algorithm(struct target *target, const uint8_t *buffer, uint32_t count, int block_size, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_params, uint32_t buffer_start, uint32_t buffer_size, uint32_t entry_point, uint32_t exit_point, void *arch_info)
Streams data to a circular buffer on target intended for consumption by code running asynchronously o...
Definition: target.c:959
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
Definition: target.c:2615
const char * target_type_name(const struct target *target)
Get the target type name.
Definition: target.c:750
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:818
static bool target_was_examined(const struct target *target)
Definition: target.h:444
@ TARGET_HALTED
Definition: target.h:58
#define ERROR_TARGET_NOT_EXAMINED
Definition: target.h:825
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
Definition: target.h:822
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
#define TARGET_PRIxADDR
Definition: types.h:284
#define NULL
Definition: usb.h:16
uint8_t status[4]
Definition: vdebug.c:17
uint8_t cmd
Definition: vdebug.c:1
uint8_t offset[4]
Definition: vdebug.c:9
uint8_t count[4]
Definition: vdebug.c:22