Skip to content

Commit 2f144ad

Browse files
committed
Add safety comments & fix memory.x
1 parent d7cee28 commit 2f144ad

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

hal/src/peripherals/flash_controller.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,19 @@ impl<'gcr, 'icc> FlashController<'gcr, 'icc> {
299299
Ok(())
300300
}
301301

302-
/// Writes less than 128 bits (16 bytes) of data to flash. Data needs to fit
303-
/// within one flash word (16 bytes).
302+
/// Writes less than 128 bits (16 bytes) of data to flash.
303+
/// Data needs to fit within one flash word (16 bytes).
304+
///
305+
/// SAFETY:
306+
///
307+
/// Writes must not corrupt potentially executable instructions of the program.
308+
/// Callers must ensure that the following condition is met:
309+
/// * If `address` points to a portion of the program's instructions, `data` must
310+
/// contain valid instructions that does not introduce undefined behavior.
311+
///
312+
/// It is very difficult to define what would cause undefined behavior when
313+
/// modifying program instructions. This would almost certainly result
314+
/// in unwanted and likely undefined behavior. Do so at your own risk.
304315
unsafe fn write_lt_128_unaligned(
305316
&self,
306317
address: u32,
@@ -332,6 +343,18 @@ impl<'gcr, 'icc> FlashController<'gcr, 'icc> {
332343
}
333344

334345
/// Writes 128 bits (16 bytes) of data to flash.
346+
/// Address must be 128-bit aligned.
347+
///
348+
/// SAFETY:
349+
///
350+
/// Writes must not corrupt potentially executable instructions of the program.
351+
/// Callers must ensure that the following condition is met:
352+
/// * If `address` points to a portion of the program's instructions, `data` must
353+
/// contain valid instructions that does not introduce undefined behavior.
354+
///
355+
/// It is very difficult to define what would cause undefined behavior when
356+
/// modifying program instructions. This would almost certainly result
357+
/// in unwanted and likely undefined behavior. Do so at your own risk.
335358
unsafe fn write128(
336359
&self,
337360
address: u32,

tests/memory.x

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,9 @@
11
MEMORY
22
{
3-
FLASH (rx) : ORIGIN = 0x1000E000, LENGTH = 222K
4-
SECFLASH (rx) : ORIGIN = ORIGIN(FLASH) + LENGTH(FLASH), LENGTH = 2K
3+
FLASH (rx) : ORIGIN = 0x10000000, LENGTH = 512K
54
STACK (rw) : ORIGIN = 0x20000000, LENGTH = 110K
65
RAM (rw) : ORIGIN = ORIGIN(STACK) + LENGTH(STACK), LENGTH = 128K - LENGTH(STACK)
76
}
87

9-
/*
10-
Add a block of memory for the stack before the RAM block, so that a stack overflow leaks into
11-
reserved space and flash memory, instead of .data and .bss.
12-
*/
13-
ASSERT((LENGTH(SECFLASH) == 2K), "Error: SECFLASH is not 2K. To change the size, update this assert, the size in the MEMORY section, and the assert in the flash layout crate.")
14-
158
_stack_start = ORIGIN(STACK) + LENGTH(STACK);
169
_stack_end = ORIGIN(STACK);
17-
18-
/* Bootloader hard jumps to 0x1000e200 */
19-
_stext = ORIGIN(FLASH) + 0x200;
20-
21-
SECTIONS {
22-
/* Add a section for the secure flash space. */
23-
.secflash ORIGIN(SECFLASH) :
24-
{
25-
KEEP(*(.secflash .secflash.*));
26-
. = ALIGN(4);
27-
} > SECFLASH
28-
}

0 commit comments

Comments
 (0)