@@ -331,3 +331,72 @@ int32_t pmp_set_region(pmp_config_t *config, const pmp_region_t *region)
331331
332332 return ERR_OK ;
333333}
334+
335+ int32_t pmp_disable_region (pmp_config_t * config , uint8_t region_idx )
336+ {
337+ if (!config )
338+ return ERR_PMP_INVALID_REGION ;
339+
340+ /* Validate region index is within bounds */
341+ if (region_idx >= PMP_MAX_REGIONS )
342+ return ERR_PMP_INVALID_REGION ;
343+
344+ /* Check if region is already locked */
345+ if (config -> regions [region_idx ].locked )
346+ return ERR_PMP_LOCKED ;
347+
348+ uint8_t pmpcfg_idx , pmpcfg_offset ;
349+ pmp_get_cfg_indices (region_idx , & pmpcfg_idx , & pmpcfg_offset );
350+
351+ /* Read current pmpcfg register to preserve other regions */
352+ uint32_t pmpcfg_val = read_pmpcfg (pmpcfg_idx );
353+
354+ /* Clear the configuration byte for this region (disables it) */
355+ pmpcfg_val &= ~(0xFFU << pmpcfg_offset );
356+
357+ /* Write pmpcfg register with updated configuration */
358+ write_pmpcfg (pmpcfg_idx , pmpcfg_val );
359+
360+ /* Update shadow configuration */
361+ config -> regions [region_idx ].addr_start = 0 ;
362+ config -> regions [region_idx ].addr_end = 0 ;
363+ config -> regions [region_idx ].permissions = 0 ;
364+
365+ return ERR_OK ;
366+ }
367+
368+ int32_t pmp_lock_region (pmp_config_t * config , uint8_t region_idx )
369+ {
370+ if (!config )
371+ return ERR_PMP_INVALID_REGION ;
372+
373+ /* Validate region index is within bounds */
374+ if (region_idx >= PMP_MAX_REGIONS )
375+ return ERR_PMP_INVALID_REGION ;
376+
377+ uint8_t pmpcfg_idx , pmpcfg_offset ;
378+ pmp_get_cfg_indices (region_idx , & pmpcfg_idx , & pmpcfg_offset );
379+
380+ /* Read current pmpcfg register to preserve other regions */
381+ uint32_t pmpcfg_val = read_pmpcfg (pmpcfg_idx );
382+
383+ /* Get current configuration byte for this region */
384+ uint8_t pmpcfg_byte = (pmpcfg_val >> pmpcfg_offset ) & 0xFFU ;
385+
386+ /* Set lock bit */
387+ pmpcfg_byte |= PMPCFG_L ;
388+
389+ /* Clear the configuration byte for this region */
390+ pmpcfg_val &= ~(0xFFU << pmpcfg_offset );
391+
392+ /* Write new configuration byte with lock bit set */
393+ pmpcfg_val |= (pmpcfg_byte << pmpcfg_offset );
394+
395+ /* Write pmpcfg register with updated configuration */
396+ write_pmpcfg (pmpcfg_idx , pmpcfg_val );
397+
398+ /* Update shadow configuration */
399+ config -> regions [region_idx ].locked = 1 ;
400+
401+ return ERR_OK ;
402+ }
0 commit comments