diff options
| author | Caleb Jamison <[email protected]> | 2024-08-26 09:41:52 -0400 |
|---|---|---|
| committer | Caleb Jamison <[email protected]> | 2024-08-26 09:43:08 -0400 |
| commit | 0a33edc9976caddcdebe0943cce78376f9c49789 (patch) | |
| tree | 471ea1dd4c9daccb29c543b1680f85ac81deb8bf | |
| parent | 86ca664655b6f7977da09aeb251663ac88200d41 (diff) | |
Import rom_data for the rp235x, don't use intrinsics on rp235x
Many thanks to @thejpster for his work on the rom_data!
Working around boot2 is currently a bit hacky for the rp235x, that will
improve in upcoming rp235x flash pr.
| -rw-r--r-- | embassy-rp/src/flash.rs | 82 | ||||
| -rw-r--r-- | embassy-rp/src/lib.rs | 1 | ||||
| -rw-r--r-- | embassy-rp/src/rom_data/mod.rs | 33 | ||||
| -rw-r--r-- | embassy-rp/src/rom_data/rp2040.rs (renamed from embassy-rp/src/rom_data.rs) | 4 | ||||
| -rw-r--r-- | embassy-rp/src/rom_data/rp235x.rs | 752 |
5 files changed, 850 insertions, 22 deletions
diff --git a/embassy-rp/src/flash.rs b/embassy-rp/src/flash.rs index a68493932..5f7922f8e 100644 --- a/embassy-rp/src/flash.rs +++ b/embassy-rp/src/flash.rs | |||
| @@ -510,11 +510,19 @@ mod ram_helpers { | |||
| 510 | /// | 510 | /// |
| 511 | /// `addr` and `len` parameters must be valid and are not checked. | 511 | /// `addr` and `len` parameters must be valid and are not checked. |
| 512 | pub unsafe fn flash_range_erase(addr: u32, len: u32) { | 512 | pub unsafe fn flash_range_erase(addr: u32, len: u32) { |
| 513 | #[cfg(feature = "rp2040")] | ||
| 513 | let mut boot2 = [0u32; 256 / 4]; | 514 | let mut boot2 = [0u32; 256 / 4]; |
| 514 | let ptrs = if USE_BOOT2 { | 515 | let ptrs = { |
| 515 | rom_data::memcpy44(&mut boot2 as *mut _, FLASH_BASE, 256); | 516 | #[cfg(feature = "rp2040")] |
| 516 | flash_function_pointers_with_boot2(true, false, &boot2) | 517 | { |
| 517 | } else { | 518 | if USE_BOOT2 { |
| 519 | rom_data::memcpy44(&mut boot2 as *mut _, FLASH_BASE, 256); | ||
| 520 | flash_function_pointers_with_boot2(true, false, &boot2) | ||
| 521 | } else { | ||
| 522 | flash_function_pointers(true, false) | ||
| 523 | } | ||
| 524 | } | ||
| 525 | #[cfg(feature = "_rp235x")] | ||
| 518 | flash_function_pointers(true, false) | 526 | flash_function_pointers(true, false) |
| 519 | }; | 527 | }; |
| 520 | 528 | ||
| @@ -540,11 +548,19 @@ mod ram_helpers { | |||
| 540 | /// | 548 | /// |
| 541 | /// `addr` and `len` parameters must be valid and are not checked. | 549 | /// `addr` and `len` parameters must be valid and are not checked. |
| 542 | pub unsafe fn flash_range_erase_and_program(addr: u32, data: &[u8]) { | 550 | pub unsafe fn flash_range_erase_and_program(addr: u32, data: &[u8]) { |
| 551 | #[cfg(feature = "rp2040")] | ||
| 543 | let mut boot2 = [0u32; 256 / 4]; | 552 | let mut boot2 = [0u32; 256 / 4]; |
| 544 | let ptrs = if USE_BOOT2 { | 553 | let ptrs = { |
| 545 | rom_data::memcpy44(&mut boot2 as *mut _, FLASH_BASE, 256); | 554 | #[cfg(feature = "rp2040")] |
| 546 | flash_function_pointers_with_boot2(true, true, &boot2) | 555 | { |
| 547 | } else { | 556 | if USE_BOOT2 { |
| 557 | rom_data::memcpy44(&mut boot2 as *mut _, FLASH_BASE, 256); | ||
| 558 | flash_function_pointers_with_boot2(true, true, &boot2) | ||
| 559 | } else { | ||
| 560 | flash_function_pointers(true, true) | ||
| 561 | } | ||
| 562 | } | ||
| 563 | #[cfg(feature = "_rp235x")] | ||
| 548 | flash_function_pointers(true, true) | 564 | flash_function_pointers(true, true) |
| 549 | }; | 565 | }; |
| 550 | 566 | ||
| @@ -575,11 +591,19 @@ mod ram_helpers { | |||
| 575 | /// | 591 | /// |
| 576 | /// `addr` and `len` parameters must be valid and are not checked. | 592 | /// `addr` and `len` parameters must be valid and are not checked. |
| 577 | pub unsafe fn flash_range_program(addr: u32, data: &[u8]) { | 593 | pub unsafe fn flash_range_program(addr: u32, data: &[u8]) { |
| 594 | #[cfg(feature = "rp2040")] | ||
| 578 | let mut boot2 = [0u32; 256 / 4]; | 595 | let mut boot2 = [0u32; 256 / 4]; |
| 579 | let ptrs = if USE_BOOT2 { | 596 | let ptrs = { |
| 580 | rom_data::memcpy44(&mut boot2 as *mut _, FLASH_BASE, 256); | 597 | #[cfg(feature = "rp2040")] |
| 581 | flash_function_pointers_with_boot2(false, true, &boot2) | 598 | { |
| 582 | } else { | 599 | if USE_BOOT2 { |
| 600 | rom_data::memcpy44(&mut boot2 as *mut _, FLASH_BASE, 256); | ||
| 601 | flash_function_pointers_with_boot2(false, true, &boot2) | ||
| 602 | } else { | ||
| 603 | flash_function_pointers(false, true) | ||
| 604 | } | ||
| 605 | } | ||
| 606 | #[cfg(feature = "_rp235x")] | ||
| 583 | flash_function_pointers(false, true) | 607 | flash_function_pointers(false, true) |
| 584 | }; | 608 | }; |
| 585 | 609 | ||
| @@ -708,13 +732,22 @@ mod ram_helpers { | |||
| 708 | /// | 732 | /// |
| 709 | /// Credit: taken from `rp2040-flash` (also licensed Apache+MIT) | 733 | /// Credit: taken from `rp2040-flash` (also licensed Apache+MIT) |
| 710 | pub unsafe fn flash_unique_id(out: &mut [u8]) { | 734 | pub unsafe fn flash_unique_id(out: &mut [u8]) { |
| 735 | #[cfg(feature = "rp2040")] | ||
| 711 | let mut boot2 = [0u32; 256 / 4]; | 736 | let mut boot2 = [0u32; 256 / 4]; |
| 712 | let ptrs = if USE_BOOT2 { | 737 | let ptrs = { |
| 713 | rom_data::memcpy44(&mut boot2 as *mut _, FLASH_BASE, 256); | 738 | #[cfg(feature = "rp2040")] |
| 714 | flash_function_pointers_with_boot2(false, false, &boot2) | 739 | { |
| 715 | } else { | 740 | if USE_BOOT2 { |
| 741 | rom_data::memcpy44(&mut boot2 as *mut _, FLASH_BASE, 256); | ||
| 742 | flash_function_pointers_with_boot2(false, false, &boot2) | ||
| 743 | } else { | ||
| 744 | flash_function_pointers(false, false) | ||
| 745 | } | ||
| 746 | } | ||
| 747 | #[cfg(feature = "_rp235x")] | ||
| 716 | flash_function_pointers(false, false) | 748 | flash_function_pointers(false, false) |
| 717 | }; | 749 | }; |
| 750 | |||
| 718 | // 4B - read unique ID | 751 | // 4B - read unique ID |
| 719 | let cmd = [0x4B]; | 752 | let cmd = [0x4B]; |
| 720 | read_flash(&cmd[..], 4, out, &ptrs as *const FlashFunctionPointers); | 753 | read_flash(&cmd[..], 4, out, &ptrs as *const FlashFunctionPointers); |
| @@ -736,13 +769,22 @@ mod ram_helpers { | |||
| 736 | /// | 769 | /// |
| 737 | /// Credit: taken from `rp2040-flash` (also licensed Apache+MIT) | 770 | /// Credit: taken from `rp2040-flash` (also licensed Apache+MIT) |
| 738 | pub unsafe fn flash_jedec_id() -> u32 { | 771 | pub unsafe fn flash_jedec_id() -> u32 { |
| 772 | #[cfg(feature = "rp2040")] | ||
| 739 | let mut boot2 = [0u32; 256 / 4]; | 773 | let mut boot2 = [0u32; 256 / 4]; |
| 740 | let ptrs = if USE_BOOT2 { | 774 | let ptrs = { |
| 741 | rom_data::memcpy44(&mut boot2 as *mut _, FLASH_BASE, 256); | 775 | #[cfg(feature = "rp2040")] |
| 742 | flash_function_pointers_with_boot2(false, false, &boot2) | 776 | { |
| 743 | } else { | 777 | if USE_BOOT2 { |
| 778 | rom_data::memcpy44(&mut boot2 as *mut _, FLASH_BASE, 256); | ||
| 779 | flash_function_pointers_with_boot2(false, false, &boot2) | ||
| 780 | } else { | ||
| 781 | flash_function_pointers(false, false) | ||
| 782 | } | ||
| 783 | } | ||
| 784 | #[cfg(feature = "_rp235x")] | ||
| 744 | flash_function_pointers(false, false) | 785 | flash_function_pointers(false, false) |
| 745 | }; | 786 | }; |
| 787 | |||
| 746 | let mut id = [0u8; 4]; | 788 | let mut id = [0u8; 4]; |
| 747 | // 9F - read JEDEC ID | 789 | // 9F - read JEDEC ID |
| 748 | let cmd = [0x9F]; | 790 | let cmd = [0x9F]; |
diff --git a/embassy-rp/src/lib.rs b/embassy-rp/src/lib.rs index 21f0771de..c2c57eaa0 100644 --- a/embassy-rp/src/lib.rs +++ b/embassy-rp/src/lib.rs | |||
| @@ -15,6 +15,7 @@ pub use rp_binary_info as binary_info; | |||
| 15 | #[cfg(feature = "critical-section-impl")] | 15 | #[cfg(feature = "critical-section-impl")] |
| 16 | mod critical_section_impl; | 16 | mod critical_section_impl; |
| 17 | 17 | ||
| 18 | #[cfg(feature = "rp2040")] | ||
| 18 | mod intrinsics; | 19 | mod intrinsics; |
| 19 | 20 | ||
| 20 | pub mod adc; | 21 | pub mod adc; |
diff --git a/embassy-rp/src/rom_data/mod.rs b/embassy-rp/src/rom_data/mod.rs new file mode 100644 index 000000000..e5fcf8e3c --- /dev/null +++ b/embassy-rp/src/rom_data/mod.rs | |||
| @@ -0,0 +1,33 @@ | |||
| 1 | #, Section 2.8.2.1: | ||
| 7 | //! | ||
| 8 | //! > The Bootrom contains a number of public functions that provide useful | ||
| 9 | //! > RP2040 functionality that might be needed in the absence of any other code | ||
| 10 | //! > on the device, as well as highly optimized versions of certain key | ||
| 11 | //! > functionality that would otherwise have to take up space in most user | ||
| 12 | //! > binaries. | ||
| 13 | " | ||
| 14 | )] | ||
| 15 | # of the | ||
| 21 | //! RP2350 datasheet: | ||
| 22 | //! | ||
| 23 | //! > Whilst some ROM space is dedicated to the implementation of the boot | ||
| 24 | //! > sequence and USB/UART boot interfaces, the bootrom also contains public | ||
| 25 | //! > functions that provide useful RP2350 functionality that may be useful for | ||
| 26 | //! > any code or runtime running on the device | ||
| 27 | " | ||
| 28 | )] | ||
| 29 | |||
| 30 | #[cfg_attr(feature = "rp2040", path = "rp2040.rs")] | ||
| 31 | #[cfg_attr(feature = "_rp235x", path = "rp235x.rs")] | ||
| 32 | mod inner; | ||
| 33 | pub use inner::*; | ||
diff --git a/embassy-rp/src/rom_data.rs b/embassy-rp/src/rom_data/rp2040.rs index baebe5b6c..5a74eddd6 100644 --- a/embassy-rp/src/rom_data.rs +++ b/embassy-rp/src/rom_data/rp2040.rs | |||
| @@ -189,7 +189,7 @@ macro_rules! rom_functions { | |||
| 189 | declare_rom_function! { | 189 | declare_rom_function! { |
| 190 | $(#[$outer])* | 190 | $(#[$outer])* |
| 191 | fn $name( $($argname: $ty),* ) -> $ret { | 191 | fn $name( $($argname: $ty),* ) -> $ret { |
| 192 | $crate::rom_data::rom_table_lookup($crate::rom_data::FUNC_TABLE, *$c) | 192 | $crate::rom_data::inner::rom_table_lookup($crate::rom_data::inner::FUNC_TABLE, *$c) |
| 193 | } | 193 | } |
| 194 | } | 194 | } |
| 195 | 195 | ||
| @@ -205,7 +205,7 @@ macro_rules! rom_functions { | |||
| 205 | declare_rom_function! { | 205 | declare_rom_function! { |
| 206 | $(#[$outer])* | 206 | $(#[$outer])* |
| 207 | unsafe fn $name( $($argname: $ty),* ) -> $ret { | 207 | unsafe fn $name( $($argname: $ty),* ) -> $ret { |
| 208 | $crate::rom_data::rom_table_lookup($crate::rom_data::FUNC_TABLE, *$c) | 208 | $crate::rom_data::inner::rom_table_lookup($crate::rom_data::inner::FUNC_TABLE, *$c) |
| 209 | } | 209 | } |
| 210 | } | 210 | } |
| 211 | 211 | ||
diff --git a/embassy-rp/src/rom_data/rp235x.rs b/embassy-rp/src/rom_data/rp235x.rs new file mode 100644 index 000000000..b16fee8f7 --- /dev/null +++ b/embassy-rp/src/rom_data/rp235x.rs | |||
| @@ -0,0 +1,752 @@ | |||
| 1 | //! Functions and data from the RPI Bootrom. | ||
| 2 | //! | ||
| 3 | //! From [Section 5.4](https://rptl.io/rp2350-datasheet#section_bootrom) of the | ||
| 4 | //! RP2350 datasheet: | ||
| 5 | //! | ||
| 6 | //! > Whilst some ROM space is dedicated to the implementation of the boot | ||
| 7 | //! > sequence and USB/UART boot interfaces, the bootrom also contains public | ||
| 8 | //! > functions that provide useful RP2350 functionality that may be useful for | ||
| 9 | //! > any code or runtime running on the device | ||
| 10 | |||
| 11 | // Credit: taken from `rp-hal` (also licensed Apache+MIT) | ||
| 12 | // https://github.com/rp-rs/rp-hal/blob/main/rp235x-hal/src/rom_data.rs | ||
| 13 | |||
| 14 | /// A bootrom function table code. | ||
| 15 | pub type RomFnTableCode = [u8; 2]; | ||
| 16 | |||
| 17 | /// This function searches for the tag which matches the mask. | ||
| 18 | type RomTableLookupFn = unsafe extern "C" fn(code: u32, mask: u32) -> usize; | ||
| 19 | |||
| 20 | /// Pointer to the value lookup function supplied by the ROM. | ||
| 21 | /// | ||
| 22 | /// This address is described at `5.5.1. Locating the API Functions` | ||
| 23 | #[cfg(all(target_arch = "arm", target_os = "none"))] | ||
| 24 | const ROM_TABLE_LOOKUP_A2: *const u16 = 0x0000_0016 as _; | ||
| 25 | |||
| 26 | /// Pointer to the value lookup function supplied by the ROM. | ||
| 27 | /// | ||
| 28 | /// This address is described at `5.5.1. Locating the API Functions` | ||
| 29 | #[cfg(all(target_arch = "arm", target_os = "none"))] | ||
| 30 | const ROM_TABLE_LOOKUP_A1: *const u32 = 0x0000_0018 as _; | ||
| 31 | |||
| 32 | /// Pointer to the data lookup function supplied by the ROM. | ||
| 33 | /// | ||
| 34 | /// On Arm, the same function is used to look up code and data. | ||
| 35 | #[cfg(all(target_arch = "arm", target_os = "none"))] | ||
| 36 | const ROM_DATA_LOOKUP_A2: *const u16 = ROM_TABLE_LOOKUP_A2; | ||
| 37 | |||
| 38 | /// Pointer to the data lookup function supplied by the ROM. | ||
| 39 | /// | ||
| 40 | /// On Arm, the same function is used to look up code and data. | ||
| 41 | #[cfg(all(target_arch = "arm", target_os = "none"))] | ||
| 42 | const ROM_DATA_LOOKUP_A1: *const u32 = ROM_TABLE_LOOKUP_A1; | ||
| 43 | |||
| 44 | /// Pointer to the value lookup function supplied by the ROM. | ||
| 45 | /// | ||
| 46 | /// This address is described at `5.5.1. Locating the API Functions` | ||
| 47 | #[cfg(not(all(target_arch = "arm", target_os = "none")))] | ||
| 48 | const ROM_TABLE_LOOKUP_A2: *const u16 = 0x0000_7DFA as _; | ||
| 49 | |||
| 50 | /// Pointer to the value lookup function supplied by the ROM. | ||
| 51 | /// | ||
| 52 | /// This address is described at `5.5.1. Locating the API Functions` | ||
| 53 | #[cfg(not(all(target_arch = "arm", target_os = "none")))] | ||
| 54 | const ROM_TABLE_LOOKUP_A1: *const u32 = 0x0000_7DF8 as _; | ||
| 55 | |||
| 56 | /// Pointer to the data lookup function supplied by the ROM. | ||
| 57 | /// | ||
| 58 | /// On RISC-V, a different function is used to look up data. | ||
| 59 | #[cfg(not(all(target_arch = "arm", target_os = "none")))] | ||
| 60 | const ROM_DATA_LOOKUP_A2: *const u16 = 0x0000_7DF8 as _; | ||
| 61 | |||
| 62 | /// Pointer to the data lookup function supplied by the ROM. | ||
| 63 | /// | ||
| 64 | /// On RISC-V, a different function is used to look up data. | ||
| 65 | #[cfg(not(all(target_arch = "arm", target_os = "none")))] | ||
| 66 | const ROM_DATA_LOOKUP_A1: *const u32 = 0x0000_7DF4 as _; | ||
| 67 | |||
| 68 | /// Address of the version number of the ROM. | ||
| 69 | const VERSION_NUMBER: *const u8 = 0x0000_0013 as _; | ||
| 70 | |||
| 71 | #[allow(unused)] | ||
| 72 | mod rt_flags { | ||
| 73 | pub const FUNC_RISCV: u32 = 0x0001; | ||
| 74 | pub const FUNC_RISCV_FAR: u32 = 0x0003; | ||
| 75 | pub const FUNC_ARM_SEC: u32 = 0x0004; | ||
| 76 | // reserved for 32-bit pointer: 0x0008 | ||
| 77 | pub const FUNC_ARM_NONSEC: u32 = 0x0010; | ||
| 78 | // reserved for 32-bit pointer: 0x0020 | ||
| 79 | pub const DATA: u32 = 0x0040; | ||
| 80 | // reserved for 32-bit pointer: 0x0080 | ||
| 81 | #[cfg(all(target_arch = "arm", target_os = "none"))] | ||
| 82 | pub const FUNC_ARM_SEC_RISCV: u32 = FUNC_ARM_SEC; | ||
| 83 | #[cfg(not(all(target_arch = "arm", target_os = "none")))] | ||
| 84 | pub const FUNC_ARM_SEC_RISCV: u32 = FUNC_RISCV; | ||
| 85 | } | ||
| 86 | |||
| 87 | /// Retrieve rom content from a table using a code. | ||
| 88 | pub fn rom_table_lookup(tag: RomFnTableCode, mask: u32) -> usize { | ||
| 89 | let tag = u16::from_le_bytes(tag) as u32; | ||
| 90 | unsafe { | ||
| 91 | let lookup_func = if rom_version_number() == 1 { | ||
| 92 | ROM_TABLE_LOOKUP_A1.read() as usize | ||
| 93 | } else { | ||
| 94 | ROM_TABLE_LOOKUP_A2.read() as usize | ||
| 95 | }; | ||
| 96 | let lookup_func: RomTableLookupFn = core::mem::transmute(lookup_func); | ||
| 97 | lookup_func(tag, mask) | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | /// Retrieve rom data content from a table using a code. | ||
| 102 | pub fn rom_data_lookup(tag: RomFnTableCode, mask: u32) -> usize { | ||
| 103 | let tag = u16::from_le_bytes(tag) as u32; | ||
| 104 | unsafe { | ||
| 105 | let lookup_func = if rom_version_number() == 1 { | ||
| 106 | ROM_DATA_LOOKUP_A1.read() as usize | ||
| 107 | } else { | ||
| 108 | ROM_DATA_LOOKUP_A2.read() as usize | ||
| 109 | }; | ||
| 110 | let lookup_func: RomTableLookupFn = core::mem::transmute(lookup_func); | ||
| 111 | lookup_func(tag, mask) | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | macro_rules! declare_rom_function { | ||
| 116 | ( | ||
| 117 | $(#[$outer:meta])* | ||
| 118 | fn $name:ident( $($argname:ident: $ty:ty),* ) -> $ret:ty | ||
| 119 | $lookup:block | ||
| 120 | ) => { | ||
| 121 | #[doc = r"Additional access for the `"] | ||
| 122 | #[doc = stringify!($name)] | ||
| 123 | #[doc = r"` ROM function."] | ||
| 124 | pub mod $name { | ||
| 125 | /// Retrieve a function pointer. | ||
| 126 | #[cfg(not(feature = "rom-func-cache"))] | ||
| 127 | pub fn ptr() -> extern "C" fn( $($argname: $ty),* ) -> $ret { | ||
| 128 | let p: usize = $lookup; | ||
| 129 | unsafe { | ||
| 130 | let func : extern "C" fn( $($argname: $ty),* ) -> $ret = core::mem::transmute(p); | ||
| 131 | func | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | /// Retrieve a function pointer. | ||
| 136 | #[cfg(feature = "rom-func-cache")] | ||
| 137 | pub fn ptr() -> extern "C" fn( $($argname: $ty),* ) -> $ret { | ||
| 138 | use core::sync::atomic::{AtomicU16, Ordering}; | ||
| 139 | |||
| 140 | // All pointers in the ROM fit in 16 bits, so we don't need a | ||
| 141 | // full width word to store the cached value. | ||
| 142 | static CACHED_PTR: AtomicU16 = AtomicU16::new(0); | ||
| 143 | // This is safe because the lookup will always resolve | ||
| 144 | // to the same value. So even if an interrupt or another | ||
| 145 | // core starts at the same time, it just repeats some | ||
| 146 | // work and eventually writes back the correct value. | ||
| 147 | let p: usize = match CACHED_PTR.load(Ordering::Relaxed) { | ||
| 148 | 0 => { | ||
| 149 | let raw: usize = $lookup; | ||
| 150 | CACHED_PTR.store(raw as u16, Ordering::Relaxed); | ||
| 151 | raw | ||
| 152 | }, | ||
| 153 | val => val as usize, | ||
| 154 | }; | ||
| 155 | unsafe { | ||
| 156 | let func : extern "C" fn( $($argname: $ty),* ) -> $ret = core::mem::transmute(p); | ||
| 157 | func | ||
| 158 | } | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | $(#[$outer])* | ||
| 163 | pub extern "C" fn $name( $($argname: $ty),* ) -> $ret { | ||
| 164 | $name::ptr()($($argname),*) | ||
| 165 | } | ||
| 166 | }; | ||
| 167 | |||
| 168 | ( | ||
| 169 | $(#[$outer:meta])* | ||
| 170 | unsafe fn $name:ident( $($argname:ident: $ty:ty),* ) -> $ret:ty | ||
| 171 | $lookup:block | ||
| 172 | ) => { | ||
| 173 | #[doc = r"Additional access for the `"] | ||
| 174 | #[doc = stringify!($name)] | ||
| 175 | #[doc = r"` ROM function."] | ||
| 176 | pub mod $name { | ||
| 177 | /// Retrieve a function pointer. | ||
| 178 | #[cfg(not(feature = "rom-func-cache"))] | ||
| 179 | pub fn ptr() -> unsafe extern "C" fn( $($argname: $ty),* ) -> $ret { | ||
| 180 | let p: usize = $lookup; | ||
| 181 | unsafe { | ||
| 182 | let func : unsafe extern "C" fn( $($argname: $ty),* ) -> $ret = core::mem::transmute(p); | ||
| 183 | func | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | /// Retrieve a function pointer. | ||
| 188 | #[cfg(feature = "rom-func-cache")] | ||
| 189 | pub fn ptr() -> unsafe extern "C" fn( $($argname: $ty),* ) -> $ret { | ||
| 190 | use core::sync::atomic::{AtomicU16, Ordering}; | ||
| 191 | |||
| 192 | // All pointers in the ROM fit in 16 bits, so we don't need a | ||
| 193 | // full width word to store the cached value. | ||
| 194 | static CACHED_PTR: AtomicU16 = AtomicU16::new(0); | ||
| 195 | // This is safe because the lookup will always resolve | ||
| 196 | // to the same value. So even if an interrupt or another | ||
| 197 | // core starts at the same time, it just repeats some | ||
| 198 | // work and eventually writes back the correct value. | ||
| 199 | let p: usize = match CACHED_PTR.load(Ordering::Relaxed) { | ||
| 200 | 0 => { | ||
| 201 | let raw: usize = $lookup; | ||
| 202 | CACHED_PTR.store(raw as u16, Ordering::Relaxed); | ||
| 203 | raw | ||
| 204 | }, | ||
| 205 | val => val as usize, | ||
| 206 | }; | ||
| 207 | unsafe { | ||
| 208 | let func : unsafe extern "C" fn( $($argname: $ty),* ) -> $ret = core::mem::transmute(p); | ||
| 209 | func | ||
| 210 | } | ||
| 211 | } | ||
| 212 | } | ||
| 213 | |||
| 214 | $(#[$outer])* | ||
| 215 | /// # Safety | ||
| 216 | /// | ||
| 217 | /// This is a low-level C function. It may be difficult to call safely from | ||
| 218 | /// Rust. If in doubt, check the rp235x datasheet for details and do your own | ||
| 219 | /// safety evaluation. | ||
| 220 | pub unsafe extern "C" fn $name( $($argname: $ty),* ) -> $ret { | ||
| 221 | $name::ptr()($($argname),*) | ||
| 222 | } | ||
| 223 | }; | ||
| 224 | } | ||
| 225 | |||
| 226 | // **************** 5.5.7 Low-level Flash Commands **************** | ||
| 227 | |||
| 228 | declare_rom_function! { | ||
| 229 | /// Restore all QSPI pad controls to their default state, and connect the | ||
| 230 | /// QMI peripheral to the QSPI pads. | ||
| 231 | /// | ||
| 232 | /// Supported architectures: ARM-S, RISC-V | ||
| 233 | unsafe fn connect_internal_flash() -> () { | ||
| 234 | crate::rom_data::rom_table_lookup(*b"IF", crate::rom_data::inner::rt_flags::FUNC_ARM_SEC_RISCV) | ||
| 235 | } | ||
| 236 | } | ||
| 237 | |||
| 238 | declare_rom_function! { | ||
| 239 | /// Initialise the QMI for serial operations (direct mode) | ||
| 240 | /// | ||
| 241 | /// Also initialise a basic XIP mode, where the QMI will perform 03h serial | ||
| 242 | /// read commands at low speed (CLKDIV=12) in response to XIP reads. | ||
| 243 | /// | ||
| 244 | /// Then, issue a sequence to the QSPI device on chip select 0, designed to | ||
| 245 | /// return it from continuous read mode ("XIP mode") and/or QPI mode to a | ||
| 246 | /// state where it will accept serial commands. This is necessary after | ||
| 247 | /// system reset to restore the QSPI device to a known state, because | ||
| 248 | /// resetting RP2350 does not reset attached QSPI devices. It is also | ||
| 249 | /// necessary when user code, having already performed some | ||
| 250 | /// continuous-read-mode or QPI-mode accesses, wishes to return the QSPI | ||
| 251 | /// device to a state where it will accept the serial erase and programming | ||
| 252 | /// commands issued by the bootrom’s flash access functions. | ||
| 253 | /// | ||
| 254 | /// If a GPIO for the secondary chip select is configured via FLASH_DEVINFO, | ||
| 255 | /// then the XIP exit sequence is also issued to chip select 1. | ||
| 256 | /// | ||
| 257 | /// The QSPI device should be accessible for XIP reads after calling this | ||
| 258 | /// function; the name flash_exit_xip refers to returning the QSPI device | ||
| 259 | /// from its XIP state to a serial command state. | ||
| 260 | /// | ||
| 261 | /// Supported architectures: ARM-S, RISC-V | ||
| 262 | unsafe fn flash_exit_xip() -> () { | ||
| 263 | crate::rom_data::rom_table_lookup(*b"EX", crate::rom_data::inner::rt_flags::FUNC_ARM_SEC_RISCV) | ||
| 264 | } | ||
| 265 | } | ||
| 266 | |||
| 267 | declare_rom_function! { | ||
| 268 | /// Erase count bytes, starting at addr (offset from start of flash). | ||
| 269 | /// | ||
| 270 | /// Optionally, pass a block erase command e.g. D8h block erase, and the | ||
| 271 | /// size of the block erased by this command — this function will use the | ||
| 272 | /// larger block erase where possible, for much higher erase speed. addr | ||
| 273 | /// must be aligned to a 4096-byte sector, and count must be a multiple of | ||
| 274 | /// 4096 bytes. | ||
| 275 | /// | ||
| 276 | /// This is a low-level flash API, and no validation of the arguments is | ||
| 277 | /// performed. See flash_op() for a higher-level API which checks alignment, | ||
| 278 | /// flash bounds and partition permissions, and can transparently apply a | ||
| 279 | /// runtime-to-storage address translation. | ||
| 280 | /// | ||
| 281 | /// The QSPI device must be in a serial command state before calling this | ||
| 282 | /// API, which can be achieved by calling connect_internal_flash() followed | ||
| 283 | /// by flash_exit_xip(). After the erase, the flash cache should be flushed | ||
| 284 | /// via flash_flush_cache() to ensure the modified flash data is visible to | ||
| 285 | /// cached XIP accesses. | ||
| 286 | /// | ||
| 287 | /// Finally, the original XIP mode should be restored by copying the saved | ||
| 288 | /// XIP setup function from bootram into SRAM, and executing it: the bootrom | ||
| 289 | /// provides a default function which restores the flash mode/clkdiv | ||
| 290 | /// discovered during flash scanning, and user programs can override this | ||
| 291 | /// with their own XIP setup function. | ||
| 292 | /// | ||
| 293 | /// For the duration of the erase operation, QMI is in direct mode (Section | ||
| 294 | /// 12.14.5) and attempting to access XIP from DMA, the debugger or the | ||
| 295 | /// other core will return a bus fault. XIP becomes accessible again once | ||
| 296 | /// the function returns. | ||
| 297 | /// | ||
| 298 | /// Supported architectures: ARM-S, RISC-V | ||
| 299 | unsafe fn flash_range_erase(addr: u32, count: usize, block_size: u32, block_cmd: u8) -> () { | ||
| 300 | crate::rom_data::rom_table_lookup(*b"RE", crate::rom_data::inner::rt_flags::FUNC_ARM_SEC_RISCV) | ||
| 301 | } | ||
| 302 | } | ||
| 303 | |||
| 304 | declare_rom_function! { | ||
| 305 | /// Program data to a range of flash storage addresses starting at addr | ||
| 306 | /// (offset from the start of flash) and count bytes in size. | ||
| 307 | /// | ||
| 308 | /// `addr` must be aligned to a 256-byte boundary, and count must be a | ||
| 309 | /// multiple of 256. | ||
| 310 | /// | ||
| 311 | /// This is a low-level flash API, and no validation of the arguments is | ||
| 312 | /// performed. See flash_op() for a higher-level API which checks alignment, | ||
| 313 | /// flash bounds and partition permissions, and can transparently apply a | ||
| 314 | /// runtime-to-storage address translation. | ||
| 315 | /// | ||
| 316 | /// The QSPI device must be in a serial command state before calling this | ||
| 317 | /// API — see notes on flash_range_erase(). | ||
| 318 | /// | ||
| 319 | /// Supported architectures: ARM-S, RISC-V | ||
| 320 | unsafe fn flash_range_program(addr: u32, data: *const u8, count: usize) -> () { | ||
| 321 | crate::rom_data::rom_table_lookup(*b"RP", crate::rom_data::inner::rt_flags::FUNC_ARM_SEC_RISCV) | ||
| 322 | } | ||
| 323 | } | ||
| 324 | |||
| 325 | declare_rom_function! { | ||
| 326 | /// Flush the entire XIP cache, by issuing an invalidate by set/way | ||
| 327 | /// maintenance operation to every cache line (Section 4.4.1). | ||
| 328 | /// | ||
| 329 | /// This ensures that flash program/erase operations are visible to | ||
| 330 | /// subsequent cached XIP reads. | ||
| 331 | /// | ||
| 332 | /// Note that this unpins pinned cache lines, which may interfere with | ||
| 333 | /// cache-as-SRAM use of the XIP cache. | ||
| 334 | /// | ||
| 335 | /// No other operations are performed. | ||
| 336 | /// | ||
| 337 | /// Supported architectures: ARM-S, RISC-V | ||
| 338 | unsafe fn flash_flush_cache() -> () { | ||
| 339 | crate::rom_data::rom_table_lookup(*b"FC", crate::rom_data::inner::rt_flags::FUNC_ARM_SEC_RISCV) | ||
| 340 | } | ||
| 341 | } | ||
| 342 | |||
| 343 | declare_rom_function! { | ||
| 344 | /// Configure the QMI to generate a standard 03h serial read command, with | ||
| 345 | /// 24 address bits, upon each XIP access. | ||
| 346 | /// | ||
| 347 | /// This is a slow XIP configuration, but is widely supported. CLKDIV is set | ||
| 348 | /// to 12. The debugger may call this function to ensure that flash is | ||
| 349 | /// readable following a program/erase operation. | ||
| 350 | /// | ||
| 351 | /// Note that the same setup is performed by flash_exit_xip(), and the | ||
| 352 | /// RP2350 flash program/erase functions do not leave XIP in an inaccessible | ||
| 353 | /// state, so calls to this function are largely redundant. It is provided | ||
| 354 | /// for compatibility with RP2040. | ||
| 355 | /// | ||
| 356 | /// Supported architectures: ARM-S, RISC-V | ||
| 357 | unsafe fn flash_enter_cmd_xip() -> () { | ||
| 358 | crate::rom_data::rom_table_lookup(*b"CX", crate::rom_data::inner::rt_flags::FUNC_ARM_SEC_RISCV) | ||
| 359 | } | ||
| 360 | } | ||
| 361 | |||
| 362 | declare_rom_function! { | ||
| 363 | /// Configure QMI for one of a small menu of XIP read modes supported by the | ||
| 364 | /// bootrom. This mode is configured for both memory windows (both chip | ||
| 365 | /// selects), and the clock divisor is also applied to direct mode. | ||
| 366 | /// | ||
| 367 | /// The available modes are: | ||
| 368 | /// | ||
| 369 | /// * 0: `03h` serial read: serial address, serial data, no wait cycles | ||
| 370 | /// * 1: `0Bh` serial read: serial address, serial data, 8 wait cycles | ||
| 371 | /// * 2: `BBh` dual-IO read: dual address, dual data, 4 wait cycles | ||
| 372 | /// (including MODE bits, which are driven to 0) | ||
| 373 | /// * 3: `EBh` quad-IO read: quad address, quad data, 6 wait cycles | ||
| 374 | /// (including MODE bits, which are driven to 0) | ||
| 375 | /// | ||
| 376 | /// The XIP write command/format are not configured by this function. When | ||
| 377 | /// booting from flash, the bootrom tries each of these modes in turn, from | ||
| 378 | /// 3 down to 0. The first mode that is found to work is remembered, and a | ||
| 379 | /// default XIP setup function is written into bootram that calls this | ||
| 380 | /// function (flash_select_xip_read_mode) with the parameters discovered | ||
| 381 | /// during flash scanning. This can be called at any time to restore the | ||
| 382 | /// flash parameters discovered during flash boot. | ||
| 383 | /// | ||
| 384 | /// All XIP modes configured by the bootrom have an 8-bit serial command | ||
| 385 | /// prefix, so that the flash can remain in a serial command state, meaning | ||
| 386 | /// XIP accesses can be mixed more freely with program/erase serial | ||
| 387 | /// operations. This has a performance penalty, so users can perform their | ||
| 388 | /// own flash setup after flash boot using continuous read mode or QPI mode | ||
| 389 | /// to avoid or alleviate the command prefix cost. | ||
| 390 | /// | ||
| 391 | /// Supported architectures: ARM-S, RISC-V | ||
| 392 | unsafe fn flash_select_xip_read_mode(bootrom_xip_mode: u8, clkdiv: u8) -> () { | ||
| 393 | crate::rom_data::rom_table_lookup(*b"XM", crate::rom_data::inner::rt_flags::FUNC_ARM_SEC_RISCV) | ||
| 394 | } | ||
| 395 | } | ||
| 396 | |||
| 397 | declare_rom_function! { | ||
| 398 | /// Restore the QMI address translation registers, ATRANS0 through ATRANS7, | ||
| 399 | /// to their reset state. This makes the runtime- to-storage address map an | ||
| 400 | /// identity map, i.e. the mapped and unmapped address are equal, and the | ||
| 401 | /// entire space is fully mapped. | ||
| 402 | /// | ||
| 403 | /// See [Section 12.14.4](https://rptl.io/rp2350-datasheet#section_bootrom) of the RP2350 | ||
| 404 | /// datasheet. | ||
| 405 | /// | ||
| 406 | /// Supported architectures: ARM-S, RISC-V | ||
| 407 | unsafe fn flash_reset_address_trans() -> () { | ||
| 408 | crate::rom_data::rom_table_lookup(*b"RA", crate::rom_data::inner::rt_flags::FUNC_ARM_SEC_RISCV) | ||
| 409 | } | ||
| 410 | } | ||
| 411 | |||
| 412 | // **************** High-level Flash Commands **************** | ||
| 413 | |||
| 414 | declare_rom_function! { | ||
| 415 | /// Applies the address translation currently configured by QMI address | ||
| 416 | /// translation registers, ATRANS0 through ATRANS7. | ||
| 417 | /// | ||
| 418 | /// See [Section 12.14.4](https://rptl.io/rp2350-datasheet#section_bootrom) of the RP2350 | ||
| 419 | /// datasheet. | ||
| 420 | /// | ||
| 421 | /// Translating an address outside of the XIP runtime address window, or | ||
| 422 | /// beyond the bounds of an ATRANSx_SIZE field, returns | ||
| 423 | /// BOOTROM_ERROR_INVALID_ADDRESS, which is not a valid flash storage | ||
| 424 | /// address. Otherwise, return the storage address which QMI would access | ||
| 425 | /// when presented with the runtime address addr. This is effectively a | ||
| 426 | /// virtual-to-physical address translation for QMI. | ||
| 427 | /// | ||
| 428 | /// Supported architectures: ARM-S, RISC-V | ||
| 429 | unsafe fn flash_runtime_to_storage_addr(addr: u32) -> i32 { | ||
| 430 | crate::rom_data::rom_table_lookup(*b"FA", crate::rom_data::inner::rt_flags::FUNC_ARM_SEC_RISCV) | ||
| 431 | } | ||
| 432 | } | ||
| 433 | |||
| 434 | declare_rom_function! { | ||
| 435 | /// Non-secure version of [flash_runtime_to_storage_addr()] | ||
| 436 | /// | ||
| 437 | /// Supported architectures: ARM-NS | ||
| 438 | #[cfg(all(target_arch = "arm", target_os = "none"))] | ||
| 439 | unsafe fn flash_runtime_to_storage_addr_ns(addr: u32) -> i32 { | ||
| 440 | crate::rom_data::rom_table_lookup(*b"FA", crate::rom_data::inner::rt_flags::FUNC_ARM_NONSEC) | ||
| 441 | } | ||
| 442 | } | ||
| 443 | |||
| 444 | declare_rom_function! { | ||
| 445 | /// Perform a flash read, erase, or program operation. | ||
| 446 | /// | ||
| 447 | /// Erase operations must be sector-aligned (4096 bytes) and sector- | ||
| 448 | /// multiple-sized, and program operations must be page-aligned (256 bytes) | ||
| 449 | /// and page-multiple-sized; misaligned erase and program operations will | ||
| 450 | /// return BOOTROM_ERROR_BAD_ALIGNMENT. The operation — erase, read, program | ||
| 451 | /// — is selected by the CFLASH_OP_BITS bitfield of the flags argument. | ||
| 452 | /// | ||
| 453 | /// See datasheet section 5.5.8.2 for more details. | ||
| 454 | /// | ||
| 455 | /// Supported architectures: ARM-S, RISC-V | ||
| 456 | unsafe fn flash_op(flags: u32, addr: u32, size_bytes: u32, buffer: *mut u8) -> i32 { | ||
| 457 | crate::rom_data::rom_table_lookup(*b"FO", crate::rom_data::inner::rt_flags::FUNC_ARM_SEC_RISCV) | ||
| 458 | } | ||
| 459 | } | ||
| 460 | |||
| 461 | declare_rom_function! { | ||
| 462 | /// Non-secure version of [flash_op()] | ||
| 463 | /// | ||
| 464 | /// Supported architectures: ARM-NS | ||
| 465 | #[cfg(all(target_arch = "arm", target_os = "none"))] | ||
| 466 | unsafe fn flash_op_ns(flags: u32, addr: u32, size_bytes: u32, buffer: *mut u8) -> i32 { | ||
| 467 | crate::rom_data::rom_table_lookup(*b"FO", crate::rom_data::inner::rt_flags::FUNC_ARM_NONSEC) | ||
| 468 | } | ||
| 469 | } | ||
| 470 | |||
| 471 | // **************** Security Related Functions **************** | ||
| 472 | |||
| 473 | declare_rom_function! { | ||
| 474 | /// Allow or disallow the specific NS API (note all NS APIs default to | ||
| 475 | /// disabled). | ||
| 476 | /// | ||
| 477 | /// See datasheet section 5.5.9.1 for more details. | ||
| 478 | /// | ||
| 479 | /// Supported architectures: ARM-S | ||
| 480 | #[cfg(all(target_arch = "arm", target_os = "none"))] | ||
| 481 | unsafe fn set_ns_api_permission(ns_api_num: u32, allowed: u8) -> i32 { | ||
| 482 | crate::rom_data::rom_table_lookup(*b"SP", crate::rom_data::inner::rt_flags::FUNC_ARM_SEC) | ||
| 483 | } | ||
| 484 | } | ||
| 485 | |||
| 486 | declare_rom_function! { | ||
| 487 | /// Utility method that can be used by secure ARM code to validate a buffer | ||
| 488 | /// passed to it from Non-secure code. | ||
| 489 | /// | ||
| 490 | /// See datasheet section 5.5.9.2 for more details. | ||
| 491 | /// | ||
| 492 | /// Supported architectures: ARM-S, RISC-V | ||
| 493 | unsafe fn validate_ns_buffer() -> () { | ||
| 494 | crate::rom_data::rom_table_lookup(*b"VB", crate::rom_data::inner::rt_flags::FUNC_ARM_SEC_RISCV) | ||
| 495 | } | ||
| 496 | } | ||
| 497 | |||
| 498 | // **************** Miscellaneous Functions **************** | ||
| 499 | |||
| 500 | declare_rom_function! { | ||
| 501 | /// Resets the RP2350 and uses the watchdog facility to restart. | ||
| 502 | /// | ||
| 503 | /// See datasheet section 5.5.10.1 for more details. | ||
| 504 | /// | ||
| 505 | /// Supported architectures: ARM-S, RISC-V | ||
| 506 | fn reboot(flags: u32, delay_ms: u32, p0: u32, p1: u32) -> i32 { | ||
| 507 | crate::rom_data::rom_table_lookup(*b"RB", crate::rom_data::inner::rt_flags::FUNC_ARM_SEC_RISCV) | ||
| 508 | } | ||
| 509 | } | ||
| 510 | |||
| 511 | declare_rom_function! { | ||
| 512 | /// Non-secure version of [reboot()] | ||
| 513 | /// | ||
| 514 | /// Supported architectures: ARM-NS | ||
| 515 | #[cfg(all(target_arch = "arm", target_os = "none"))] | ||
| 516 | fn reboot_ns(flags: u32, delay_ms: u32, p0: u32, p1: u32) -> i32 { | ||
| 517 | crate::rom_data::rom_table_lookup(*b"RB", crate::rom_data::inner::rt_flags::FUNC_ARM_NONSEC) | ||
| 518 | } | ||
| 519 | } | ||
| 520 | |||
| 521 | declare_rom_function! { | ||
| 522 | /// Resets internal bootrom state. | ||
| 523 | /// | ||
| 524 | /// See datasheet section 5.5.10.2 for more details. | ||
| 525 | /// | ||
| 526 | /// Supported architectures: ARM-S, RISC-V | ||
| 527 | unsafe fn bootrom_state_reset(flags: u32) -> () { | ||
| 528 | crate::rom_data::rom_table_lookup(*b"SR", crate::rom_data::inner::rt_flags::FUNC_ARM_SEC_RISCV) | ||
| 529 | } | ||
| 530 | } | ||
| 531 | |||
| 532 | declare_rom_function! { | ||
| 533 | /// Set a boot ROM callback. | ||
| 534 | /// | ||
| 535 | /// The only supported callback_number is 0 which sets the callback used for | ||
| 536 | /// the secure_call API. | ||
| 537 | /// | ||
| 538 | /// See datasheet section 5.5.10.3 for more details. | ||
| 539 | /// | ||
| 540 | /// Supported architectures: ARM-S, RISC-V | ||
| 541 | unsafe fn set_rom_callback(callback_number: i32, callback_fn: *const ()) -> i32 { | ||
| 542 | crate::rom_data::rom_table_lookup(*b"RC", crate::rom_data::inner::rt_flags::FUNC_ARM_SEC_RISCV) | ||
| 543 | } | ||
| 544 | } | ||
| 545 | |||
| 546 | // **************** System Information Functions **************** | ||
| 547 | |||
| 548 | declare_rom_function! { | ||
| 549 | /// Fills a buffer with various system information. | ||
| 550 | /// | ||
| 551 | /// Note that this API is also used to return information over the PICOBOOT | ||
| 552 | /// interface. | ||
| 553 | /// | ||
| 554 | /// See datasheet section 5.5.11.1 for more details. | ||
| 555 | /// | ||
| 556 | /// Supported architectures: ARM-S, RISC-V | ||
| 557 | unsafe fn get_sys_info(out_buffer: *mut u32, out_buffer_word_size: usize, flags: u32) -> i32 { | ||
| 558 | crate::rom_data::rom_table_lookup(*b"GS", crate::rom_data::inner::rt_flags::FUNC_ARM_SEC_RISCV) | ||
| 559 | } | ||
| 560 | } | ||
| 561 | |||
| 562 | declare_rom_function! { | ||
| 563 | /// Non-secure version of [get_sys_info()] | ||
| 564 | /// | ||
| 565 | /// Supported architectures: ARM-NS | ||
| 566 | #[cfg(all(target_arch = "arm", target_os = "none"))] | ||
| 567 | unsafe fn get_sys_info_ns(out_buffer: *mut u32, out_buffer_word_size: usize, flags: u32) -> i32 { | ||
| 568 | crate::rom_data::rom_table_lookup(*b"GS", crate::rom_data::inner::rt_flags::FUNC_ARM_NONSEC) | ||
| 569 | } | ||
| 570 | } | ||
| 571 | |||
| 572 | declare_rom_function! { | ||
| 573 | /// Fills a buffer with information from the partition table. | ||
| 574 | /// | ||
| 575 | /// Note that this API is also used to return information over the PICOBOOT | ||
| 576 | /// interface. | ||
| 577 | /// | ||
| 578 | /// See datasheet section 5.5.11.2 for more details. | ||
| 579 | /// | ||
| 580 | /// Supported architectures: ARM-S, RISC-V | ||
| 581 | unsafe fn get_partition_table_info(out_buffer: *mut u32, out_buffer_word_size: usize, flags_and_partition: u32) -> i32 { | ||
| 582 | crate::rom_data::rom_table_lookup(*b"GP", crate::rom_data::inner::rt_flags::FUNC_ARM_SEC_RISCV) | ||
| 583 | } | ||
| 584 | } | ||
| 585 | |||
| 586 | declare_rom_function! { | ||
| 587 | /// Non-secure version of [get_partition_table_info()] | ||
| 588 | /// | ||
| 589 | /// Supported architectures: ARM-NS | ||
| 590 | #[cfg(all(target_arch = "arm", target_os = "none"))] | ||
| 591 | unsafe fn get_partition_table_info_ns(out_buffer: *mut u32, out_buffer_word_size: usize, flags_and_partition: u32) -> i32 { | ||
| 592 | crate::rom_data::rom_table_lookup(*b"GP", crate::rom_data::inner::rt_flags::FUNC_ARM_NONSEC) | ||
| 593 | } | ||
| 594 | } | ||
| 595 | |||
| 596 | declare_rom_function! { | ||
| 597 | /// Loads the current partition table from flash, if present. | ||
| 598 | /// | ||
| 599 | /// See datasheet section 5.5.11.3 for more details. | ||
| 600 | /// | ||
| 601 | /// Supported architectures: ARM-S, RISC-V | ||
| 602 | unsafe fn load_partition_table(workarea_base: *mut u8, workarea_size: usize, force_reload: bool) -> i32 { | ||
| 603 | crate::rom_data::rom_table_lookup(*b"LP", crate::rom_data::inner::rt_flags::FUNC_ARM_SEC_RISCV) | ||
| 604 | } | ||
| 605 | } | ||
| 606 | |||
| 607 | declare_rom_function! { | ||
| 608 | /// Writes data from a buffer into OTP, or reads data from OTP into a buffer. | ||
| 609 | /// | ||
| 610 | /// See datasheet section 5.5.11.4 for more details. | ||
| 611 | /// | ||
| 612 | /// Supported architectures: ARM-S, RISC-V | ||
| 613 | unsafe fn otp_access(buf: *mut u8, buf_len: usize, row_and_flags: u32) -> i32 { | ||
| 614 | crate::rom_data::rom_table_lookup(*b"OA", crate::rom_data::inner::rt_flags::FUNC_ARM_SEC_RISCV) | ||
| 615 | } | ||
| 616 | } | ||
| 617 | |||
| 618 | declare_rom_function! { | ||
| 619 | /// Non-secure version of [otp_access()] | ||
| 620 | /// | ||
| 621 | /// Supported architectures: ARM-NS | ||
| 622 | #[cfg(all(target_arch = "arm", target_os = "none"))] | ||
| 623 | unsafe fn otp_access_ns(buf: *mut u8, buf_len: usize, row_and_flags: u32) -> i32 { | ||
| 624 | crate::rom_data::rom_table_lookup(*b"OA", crate::rom_data::inner::rt_flags::FUNC_ARM_NONSEC) | ||
| 625 | } | ||
| 626 | } | ||
| 627 | |||
| 628 | // **************** Boot Related Functions **************** | ||
| 629 | |||
| 630 | declare_rom_function! { | ||
| 631 | /// Determines which of the partitions has the "better" IMAGE_DEF. In the | ||
| 632 | /// case of executable images, this is the one that would be booted. | ||
| 633 | /// | ||
| 634 | /// See datasheet section 5.5.12.1 for more details. | ||
| 635 | /// | ||
| 636 | /// Supported architectures: ARM-S, RISC-V | ||
| 637 | unsafe fn pick_ab_parition(workarea_base: *mut u8, workarea_size: usize, partition_a_num: u32) -> i32 { | ||
| 638 | crate::rom_data::rom_table_lookup(*b"AB", crate::rom_data::inner::rt_flags::FUNC_ARM_SEC_RISCV) | ||
| 639 | } | ||
| 640 | } | ||
| 641 | |||
| 642 | declare_rom_function! { | ||
| 643 | /// Searches a memory region for a launchable image, and executes it if | ||
| 644 | /// possible. | ||
| 645 | /// | ||
| 646 | /// See datasheet section 5.5.12.2 for more details. | ||
| 647 | /// | ||
| 648 | /// Supported architectures: ARM-S, RISC-V | ||
| 649 | unsafe fn chain_image(workarea_base: *mut u8, workarea_size: usize, region_base: i32, region_size: u32) -> i32 { | ||
| 650 | crate::rom_data::rom_table_lookup(*b"CI", crate::rom_data::inner::rt_flags::FUNC_ARM_SEC_RISCV) | ||
| 651 | } | ||
| 652 | } | ||
| 653 | |||
| 654 | declare_rom_function! { | ||
| 655 | /// Perform an "explicit" buy of an executable launched via an IMAGE_DEF | ||
| 656 | /// which was "explicit buy" flagged. | ||
| 657 | /// | ||
| 658 | /// See datasheet section 5.5.12.3 for more details. | ||
| 659 | /// | ||
| 660 | /// Supported architectures: ARM-S, RISC-V | ||
| 661 | unsafe fn explicit_buy(buffer: *mut u8, buffer_size: u32) -> i32 { | ||
| 662 | crate::rom_data::rom_table_lookup(*b"EB", crate::rom_data::inner::rt_flags::FUNC_ARM_SEC_RISCV) | ||
| 663 | } | ||
| 664 | } | ||
| 665 | |||
| 666 | declare_rom_function! { | ||
| 667 | /// Not yet documented. | ||
| 668 | /// | ||
| 669 | /// See datasheet section 5.5.12.4 for more details. | ||
| 670 | /// | ||
| 671 | /// Supported architectures: ARM-S, RISC-V | ||
| 672 | unsafe fn get_uf2_target_partition(workarea_base: *mut u8, workarea_size: usize, family_id: u32, partition_out: *mut u32) -> i32 { | ||
| 673 | crate::rom_data::rom_table_lookup(*b"GU", crate::rom_data::inner::rt_flags::FUNC_ARM_SEC_RISCV) | ||
| 674 | } | ||
| 675 | } | ||
| 676 | |||
| 677 | declare_rom_function! { | ||
| 678 | /// Returns: The index of the B partition of partition A if a partition | ||
| 679 | /// table is present and loaded, and there is a partition A with a B | ||
| 680 | /// partition; otherwise returns BOOTROM_ERROR_NOT_FOUND. | ||
| 681 | /// | ||
| 682 | /// See datasheet section 5.5.12.5 for more details. | ||
| 683 | /// | ||
| 684 | /// Supported architectures: ARM-S, RISC-V | ||
| 685 | unsafe fn get_b_partition(partition_a: u32) -> i32 { | ||
| 686 | crate::rom_data::rom_table_lookup(*b"GB", crate::rom_data::inner::rt_flags::FUNC_ARM_SEC_RISCV) | ||
| 687 | } | ||
| 688 | } | ||
| 689 | |||
| 690 | // **************** Non-secure-specific Functions **************** | ||
| 691 | |||
| 692 | // NB: The "secure_call" function should be here, but it doesn't have a fixed | ||
| 693 | // function signature as it is designed to let you bounce into any secure | ||
| 694 | // function from non-secure mode. | ||
| 695 | |||
| 696 | // **************** RISC-V Functions **************** | ||
| 697 | |||
| 698 | declare_rom_function! { | ||
| 699 | /// Set stack for RISC-V bootrom functions to use. | ||
| 700 | /// | ||
| 701 | /// See datasheet section 5.5.14.1 for more details. | ||
| 702 | /// | ||
| 703 | /// Supported architectures: RISC-V | ||
| 704 | #[cfg(not(all(target_arch = "arm", target_os = "none")))] | ||
| 705 | unsafe fn set_bootrom_stack(base_size: *mut u32) -> i32 { | ||
| 706 | crate::rom_data::rom_table_lookup(*b"SS", crate::rom_data::inner::rt_flags::FUNC_RISCV) | ||
| 707 | } | ||
| 708 | } | ||
| 709 | |||
| 710 | /// The version number of the rom. | ||
| 711 | pub fn rom_version_number() -> u8 { | ||
| 712 | unsafe { *VERSION_NUMBER } | ||
| 713 | } | ||
| 714 | |||
| 715 | /// The 8 most significant hex digits of the Bootrom git revision. | ||
| 716 | pub fn git_revision() -> u32 { | ||
| 717 | let ptr = rom_data_lookup(*b"GR", rt_flags::DATA) as *const u32; | ||
| 718 | unsafe { ptr.read() } | ||
| 719 | } | ||
| 720 | |||
| 721 | /// A pointer to the resident partition table info. | ||
| 722 | /// | ||
| 723 | /// The resident partition table is the subset of the full partition table that | ||
| 724 | /// is kept in memory, and used for flash permissions. | ||
| 725 | pub fn partition_table_pointer() -> *const u32 { | ||
| 726 | let ptr = rom_data_lookup(*b"PT", rt_flags::DATA) as *const *const u32; | ||
| 727 | unsafe { ptr.read() } | ||
| 728 | } | ||
| 729 | |||
| 730 | /// Determine if we are in secure mode | ||
| 731 | /// | ||
| 732 | /// Returns `true` if we are in secure mode and `false` if we are in non-secure | ||
| 733 | /// mode. | ||
| 734 | #[cfg(all(target_arch = "arm", target_os = "none"))] | ||
| 735 | pub fn is_secure_mode() -> bool { | ||
| 736 | // Look at the start of ROM, which is always readable | ||
| 737 | #[allow(clippy::zero_ptr)] | ||
| 738 | let rom_base: *mut u32 = 0x0000_0000 as *mut u32; | ||
| 739 | // Use the 'tt' instruction to check the permissions for that address | ||
| 740 | let tt = cortex_m::asm::tt(rom_base); | ||
| 741 | // Is the secure bit set? => secure mode | ||
| 742 | (tt & (1 << 22)) != 0 | ||
| 743 | } | ||
| 744 | |||
| 745 | /// Determine if we are in secure mode | ||
| 746 | /// | ||
| 747 | /// Always returns `false` on RISC-V as it is impossible to determine if | ||
| 748 | /// you are in Machine Mode or User Mode by design. | ||
| 749 | #[cfg(not(all(target_arch = "arm", target_os = "none")))] | ||
| 750 | pub fn is_secure_mode() -> bool { | ||
| 751 | false | ||
| 752 | } | ||
