blob: dcd17b792537b28cfd61b519e9702fcbe02052d7 [file] [log] [blame]
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +00001// 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
17use core::ffi::CStr;
18use core::ops::Range;
19
Pierre-Clément Tosic3811b82022-11-29 11:24:16 +000020/// Extract from /config the address range containing the pre-loaded kernel.
21pub 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 Tosia0934c12022-11-25 20:54:11 +000038/// Extract from /chosen the address range containing the pre-loaded ramdisk.
39pub 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}