Skip to content

Commit 0cae1d8

Browse files
SebastianBoeMaureenHelm
authored andcommitted
drivers: flash: Refactor boundary checking
We will soon need to do more boundary checking to test whether we are reading secure or non-secure memory. Refactor the boundary checking in preparation for this. Signed-off-by: Sebastian Bøe <[email protected]>
1 parent b2ebd98 commit 0cae1d8

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

drivers/flash/soc_flash_nrf.c

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -94,32 +94,23 @@ static inline bool is_aligned_32(uint32_t data)
9494
return (data & 0x3) ? false : true;
9595
}
9696

97-
static inline bool is_regular_addr_valid(off_t addr, size_t len)
97+
static inline bool is_within_bounds(off_t addr, size_t len, off_t boundary_start,
98+
size_t boundary_size)
9899
{
99-
size_t flash_size = nrfx_nvmc_flash_size_get();
100-
101-
if (addr >= flash_size ||
102-
addr < 0 ||
103-
len > flash_size ||
104-
(addr) + len > flash_size) {
105-
return false;
106-
}
107-
108-
return true;
100+
return (addr >= boundary_start &&
101+
(addr < (boundary_start + boundary_size)) &&
102+
(len <= (boundary_start + boundary_size - addr)));
109103
}
110104

105+
static inline bool is_regular_addr_valid(off_t addr, size_t len)
106+
{
107+
return is_within_bounds(addr, len, 0, nrfx_nvmc_flash_size_get());
108+
}
111109

112110
static inline bool is_uicr_addr_valid(off_t addr, size_t len)
113111
{
114112
#ifdef CONFIG_SOC_FLASH_NRF_UICR
115-
if (addr >= (off_t)NRF_UICR + sizeof(*NRF_UICR) ||
116-
addr < (off_t)NRF_UICR ||
117-
len > sizeof(*NRF_UICR) ||
118-
addr + len > (off_t)NRF_UICR + sizeof(*NRF_UICR)) {
119-
return false;
120-
}
121-
122-
return true;
113+
return is_within_bounds(addr, len, (off_t)NRF_UICR, sizeof(*NRF_UICR));
123114
#else
124115
return false;
125116
#endif /* CONFIG_SOC_FLASH_NRF_UICR */

0 commit comments

Comments
 (0)