Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 1 | // Copyright 2022, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! High-level FDT functions. |
| 16 | |
| 17 | use core::ffi::CStr; |
| 18 | use core::ops::Range; |
| 19 | |
Pierre-Clément Tosi | c3811b8 | 2022-11-29 11:24:16 +0000 | [diff] [blame] | 20 | /// Extract from /config the address range containing the pre-loaded kernel. |
| 21 | pub fn kernel_range(fdt: &libfdt::Fdt) -> libfdt::Result<Option<Range<usize>>> { |
| 22 | let config = CStr::from_bytes_with_nul(b"/config\0").unwrap(); |
| 23 | let addr = CStr::from_bytes_with_nul(b"kernel-address\0").unwrap(); |
| 24 | let size = CStr::from_bytes_with_nul(b"kernel-size\0").unwrap(); |
| 25 | |
| 26 | if let Some(config) = fdt.node(config)? { |
| 27 | if let (Some(addr), Some(size)) = (config.getprop_u32(addr)?, config.getprop_u32(size)?) { |
| 28 | let addr = addr as usize; |
| 29 | let size = size as usize; |
| 30 | |
| 31 | return Ok(Some(addr..(addr + size))); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | Ok(None) |
| 36 | } |
| 37 | |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 38 | /// Extract from /chosen the address range containing the pre-loaded ramdisk. |
| 39 | pub fn initrd_range(fdt: &libfdt::Fdt) -> libfdt::Result<Option<Range<usize>>> { |
| 40 | let start = CStr::from_bytes_with_nul(b"linux,initrd-start\0").unwrap(); |
| 41 | let end = CStr::from_bytes_with_nul(b"linux,initrd-end\0").unwrap(); |
| 42 | |
| 43 | if let Some(chosen) = fdt.chosen()? { |
| 44 | if let (Some(start), Some(end)) = (chosen.getprop_u32(start)?, chosen.getprop_u32(end)?) { |
| 45 | return Ok(Some((start as usize)..(end as usize))); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | Ok(None) |
| 50 | } |
Pierre-Clément Tosi | db74cb1 | 2022-12-08 13:56:25 +0000 | [diff] [blame^] | 51 | |
| 52 | /// Add a "google,open-dice"-compatible reserved-memory node to the tree. |
| 53 | pub fn add_dice_node(fdt: &mut libfdt::Fdt, addr: usize, size: usize) -> libfdt::Result<()> { |
| 54 | fdt.unpack()?; |
| 55 | |
| 56 | let reserved_memory = CStr::from_bytes_with_nul(b"/reserved-memory\0").unwrap(); |
| 57 | // We reject DTs with missing reserved-memory node as validation should have checked that the |
| 58 | // "swiotlb" subnode (compatible = "restricted-dma-pool") was present. |
| 59 | let mut reserved_memory = fdt.node_mut(reserved_memory)?.ok_or(libfdt::FdtError::NotFound)?; |
| 60 | |
| 61 | let dice = CStr::from_bytes_with_nul(b"dice\0").unwrap(); |
| 62 | let mut dice = reserved_memory.add_subnode(dice)?; |
| 63 | |
| 64 | let compatible = CStr::from_bytes_with_nul(b"compatible\0").unwrap(); |
| 65 | dice.appendprop(compatible, b"google,open-dice\0")?; |
| 66 | |
| 67 | let no_map = CStr::from_bytes_with_nul(b"no-map\0").unwrap(); |
| 68 | dice.appendprop(no_map, &[])?; |
| 69 | |
| 70 | let reg = CStr::from_bytes_with_nul(b"reg\0").unwrap(); |
| 71 | dice.appendprop_addrrange(reg, addr as u64, size as u64)?; |
| 72 | |
| 73 | fdt.pack()?; |
| 74 | |
| 75 | Ok(()) |
| 76 | } |