#define OPT_RDWDGSW 2
#define OPT_RDRSTSTOP 3
#define OPT_RDRSTSTDBY 4
+#define OPT_BFB2 5 /* dual flash bank only */
/* register unlock keys */
#define KEY1 0x45670123
#define KEY2 0xCDEF89AB
+/* we use an offset to access the second bank on dual flash devices
+ * strangely the protection of the second bank is done on the bank0 reg's */
+
+#define FLASH_OFFSET_B0 0x00
+#define FLASH_OFFSET_B1 0x40
+
struct stm32x_options
{
uint16_t RDP;
int ppage_size;
int probed;
+ bool has_dual_banks;
/* used to access dual flash bank stm32xl
- * 0x00 will address sector 0 flash
- * 0x40 will address sector 1 flash */
+ * 0x00 will address bank 0 flash
+ * 0x40 will address bank 1 flash */
int register_offset;
};
stm32x_info->write_algorithm = NULL;
stm32x_info->probed = 0;
- stm32x_info->register_offset = 0x00;
+ stm32x_info->has_dual_banks = false;
+ stm32x_info->register_offset = FLASH_OFFSET_B0;
return ERROR_OK;
}
return retval;
}
+int stm32x_check_operation_supported(struct flash_bank *bank)
+{
+ struct stm32x_flash_bank *stm32x_info = bank->driver_priv;
+
+ /* if we have a dual flash bank device then
+ * we need to perform option byte stuff on bank0 only */
+ if (stm32x_info->register_offset != FLASH_OFFSET_B0)
+ {
+ LOG_ERROR("Option Byte Operation's must use bank0");
+ return ERROR_FLASH_OPERATION_FAILED;
+ }
+
+ return ERROR_OK;
+}
+
static int stm32x_read_options(struct flash_bank *bank)
{
uint32_t optiondata;
return ERROR_TARGET_NOT_HALTED;
}
+ int retval = stm32x_check_operation_supported(bank);
+ if (ERROR_OK != retval)
+ return retval;
+
/* medium density - each bit refers to a 4bank protection
* high density - each bit refers to a 2bank protection */
- int retval = target_read_u32(target, STM32_FLASH_WRPR, &protection);
+ retval = target_read_u32(target, STM32_FLASH_WRPR, &protection);
if (retval != ERROR_OK)
return retval;
return ERROR_TARGET_NOT_HALTED;
}
+ int retval = stm32x_check_operation_supported(bank);
+ if (ERROR_OK != retval)
+ return retval;
+
if ((first % stm32x_info->ppage_size) != 0)
{
LOG_WARNING("aligned start protect sector to a %d sector boundary",
/* medium density - each bit refers to a 4bank protection
* high density - each bit refers to a 2bank protection */
- int retval = target_read_u32(target, STM32_FLASH_WRPR, &protection);
+ retval = target_read_u32(target, STM32_FLASH_WRPR, &protection);
if (retval != ERROR_OK)
return retval;
int page_size;
stm32x_info->probed = 0;
- stm32x_info->register_offset = 0x00;
+ stm32x_info->register_offset = FLASH_OFFSET_B0;
/* read stm32 device id register */
int retval = target_read_u32(target, 0xE0042000, &device_id);
* 2 pages for a protection area */
page_size = 2048;
stm32x_info->ppage_size = 2;
+ stm32x_info->has_dual_banks = true;
/* check for early silicon */
if (num_pages == 0xffff)
{
num_pages -= 512;
/* bank1 also uses a register offset */
- stm32x_info->register_offset = 0x40;
+ stm32x_info->register_offset = FLASH_OFFSET_B1;
}
}
else
return ERROR_TARGET_NOT_HALTED;
}
+ retval = stm32x_check_operation_supported(bank);
+ if (ERROR_OK != retval)
+ return retval;
+
if (stm32x_erase_options(bank) != ERROR_OK)
{
command_print(CMD_CTX, "stm32x failed to erase options");
return ERROR_TARGET_NOT_HALTED;
}
+ retval = stm32x_check_operation_supported(bank);
+ if (ERROR_OK != retval)
+ return retval;
+
if (stm32x_erase_options(bank) != ERROR_OK)
{
command_print(CMD_CTX, "stm32x failed to unlock device");
return ERROR_TARGET_NOT_HALTED;
}
+ retval = stm32x_check_operation_supported(bank);
+ if (ERROR_OK != retval)
+ return retval;
+
retval = target_read_u32(target, STM32_FLASH_OBR, &optionbyte);
if (retval != ERROR_OK)
return retval;
else
command_print(CMD_CTX, "Standby: Reset generated");
+ if (stm32x_info->has_dual_banks)
+ {
+ if (buf_get_u32((uint8_t*)&optionbyte, OPT_BFB2, 1))
+ command_print(CMD_CTX, "Boot: Bank 0");
+ else
+ command_print(CMD_CTX, "Boot: Bank 1");
+ }
+
return ERROR_OK;
}
if (CMD_ARGC < 4)
{
- command_print(CMD_CTX, "stm32x options_write <bank> <SWWDG | HWWDG> <RSTSTNDBY | NORSTSTNDBY> <RSTSTOP | NORSTSTOP>");
+ command_print(CMD_CTX, "stm32x options_write <bank> <SWWDG | HWWDG> "
+ "<RSTSTNDBY | NORSTSTNDBY> <RSTSTOP | NORSTSTOP> <BOOT0 | BOOT1>");
return ERROR_OK;
}
return ERROR_TARGET_NOT_HALTED;
}
+ retval = stm32x_check_operation_supported(bank);
+ if (ERROR_OK != retval)
+ return retval;
+
/* REVISIT: ignores some options which we will display...
* and doesn't insist on the specified syntax.
*/
optionbyte &= ~(1 << 2);
}
+ if (CMD_ARGC > 4 && stm32x_info->has_dual_banks)
+ {
+ /* OPT_BFB2 */
+ if (strcmp(CMD_ARGV[4], "BOOT0") == 0)
+ {
+ optionbyte |= (1 << 3);
+ }
+ else
+ {
+ optionbyte &= ~(1 << 3);
+ }
+ }
+
if (stm32x_erase_options(bank) != ERROR_OK)
{
command_print(CMD_CTX, "stm32x failed to erase options");