|
17 | 17 |
|
18 | 18 | #include "flash_stm32.h" |
19 | 19 |
|
| 20 | +LOG_MODULE_REGISTER(flash_stm32f4x, CONFIG_FLASH_LOG_LEVEL); |
| 21 | + |
20 | 22 | bool flash_stm32_valid_range(const struct device *dev, off_t offset, |
21 | 23 | uint32_t len, |
22 | 24 | bool write) |
@@ -270,6 +272,108 @@ int flash_stm32_get_wp_sectors(const struct device *dev, |
270 | 272 | } |
271 | 273 | #endif /* CONFIG_FLASH_STM32_WRITE_PROTECT */ |
272 | 274 |
|
| 275 | +#if defined(CONFIG_FLASH_STM32_READOUT_PROTECTION) |
| 276 | +int flash_stm32_update_rdp(const struct device *dev, bool enable, |
| 277 | + bool permanent) |
| 278 | +{ |
| 279 | + FLASH_TypeDef *regs = FLASH_STM32_REGS(dev); |
| 280 | + uint8_t current_level, target_level; |
| 281 | + |
| 282 | + current_level = |
| 283 | + (regs->OPTCR & FLASH_OPTCR_RDP_Msk) >> FLASH_OPTCR_RDP_Pos; |
| 284 | + target_level = current_level; |
| 285 | + |
| 286 | + /* |
| 287 | + * 0xAA = RDP level 0 (no protection) |
| 288 | + * 0xCC = RDP level 2 (permanent protection) |
| 289 | + * others = RDP level 1 (protection active) |
| 290 | + */ |
| 291 | + switch (current_level) { |
| 292 | + case FLASH_STM32_RDP2: |
| 293 | + if (!enable || !permanent) { |
| 294 | + __ASSERT(false, "RDP level 2 is permanent and can't be " |
| 295 | + "changed!"); |
| 296 | + return -ENOTSUP; |
| 297 | + } |
| 298 | + break; |
| 299 | + case FLASH_STM32_RDP0: |
| 300 | + if (enable) { |
| 301 | + target_level = FLASH_STM32_RDP1; |
| 302 | + if (permanent) { |
| 303 | +#if defined(CONFIG_FLASH_STM32_READOUT_PROTECTION_PERMANENT_ALLOW) |
| 304 | + target_level = FLASH_STM32_RDP2; |
| 305 | +#else |
| 306 | + __ASSERT(false, |
| 307 | + "Permanent readout protection (RDP " |
| 308 | + "level 0 -> 2) not allowed"); |
| 309 | + return -ENOTSUP; |
| 310 | +#endif |
| 311 | + } |
| 312 | + } |
| 313 | + break; |
| 314 | + default: /* FLASH_STM32_RDP1 */ |
| 315 | + if (enable && permanent) { |
| 316 | +#if defined(CONFIG_FLASH_STM32_READOUT_PROTECTION_PERMANENT_ALLOW) |
| 317 | + target_level = FLASH_STM32_RDP2; |
| 318 | +#else |
| 319 | + __ASSERT(false, "Permanent readout protection (RDP " |
| 320 | + "level 1 -> 2) not allowed"); |
| 321 | + return -ENOTSUP; |
| 322 | +#endif |
| 323 | + } |
| 324 | + if (!enable) { |
| 325 | +#if defined(CONFIG_FLASH_STM32_READOUT_PROTECTION_DISABLE_ALLOW) |
| 326 | + target_level = FLASH_STM32_RDP0; |
| 327 | +#else |
| 328 | + __ASSERT(false, "Disabling readout protection (RDP " |
| 329 | + "level 1 -> 0) not allowed"); |
| 330 | + return -EACCES; |
| 331 | +#endif |
| 332 | + } |
| 333 | + } |
| 334 | + |
| 335 | + /* Update RDP level if needed */ |
| 336 | + if (current_level != target_level) { |
| 337 | + LOG_INF("RDP changed from 0x%02x to 0x%02x", current_level, |
| 338 | + target_level); |
| 339 | + |
| 340 | + write_optb(dev, FLASH_OPTCR_RDP_Msk, |
| 341 | + (uint32_t)target_level << FLASH_OPTCR_RDP_Pos); |
| 342 | + } |
| 343 | + return 0; |
| 344 | +} |
| 345 | + |
| 346 | +int flash_stm32_get_rdp(const struct device *dev, bool *enabled, |
| 347 | + bool *permanent) |
| 348 | +{ |
| 349 | + FLASH_TypeDef *regs = FLASH_STM32_REGS(dev); |
| 350 | + uint8_t current_level; |
| 351 | + |
| 352 | + current_level = |
| 353 | + (regs->OPTCR & FLASH_OPTCR_RDP_Msk) >> FLASH_OPTCR_RDP_Pos; |
| 354 | + |
| 355 | + /* |
| 356 | + * 0xAA = RDP level 0 (no protection) |
| 357 | + * 0xCC = RDP level 2 (permanent protection) |
| 358 | + * others = RDP level 1 (protection active) |
| 359 | + */ |
| 360 | + switch (current_level) { |
| 361 | + case FLASH_STM32_RDP2: |
| 362 | + *enabled = true; |
| 363 | + *permanent = true; |
| 364 | + break; |
| 365 | + case FLASH_STM32_RDP0: |
| 366 | + *enabled = false; |
| 367 | + *permanent = false; |
| 368 | + break; |
| 369 | + default: /* FLASH_STM32_RDP1 */ |
| 370 | + *enabled = true; |
| 371 | + *permanent = false; |
| 372 | + } |
| 373 | + return 0; |
| 374 | +} |
| 375 | +#endif /* CONFIG_FLASH_STM32_READOUT_PROTECTION */ |
| 376 | + |
273 | 377 | /* |
274 | 378 | * Different SoC flash layouts are specified in across various |
275 | 379 | * reference manuals, but the flash layout for a given number of |
|
0 commit comments