Browse Source

dt: parse '/model' node

Signed-off-by: Zhouqi Jiang <luojia@hust.edu.cn>
Zhouqi Jiang 10 tháng trước cách đây
mục cha
commit
4b6bf5cf90
3 tập tin đã thay đổi với 22 bổ sung15 xóa
  1. 5 8
      src/dt.rs
  2. 9 3
      src/fail.rs
  3. 8 4
      src/main.rs

+ 5 - 8
src/device_tree.rs → src/dt.rs

@@ -1,8 +1,9 @@
 use serde::Deserialize;
-use serde_device_tree::{buildin::StrSeq, from_raw_mut, Dtb, DtbPtr};
+use serde_device_tree::{buildin::StrSeq, Dtb, DtbPtr};
 
 #[derive(Deserialize)]
 pub struct Tree<'a> {
+    pub model: Option<StrSeq<'a>>,
     pub chosen: Chosen<'a>,
 }
 
@@ -14,16 +15,12 @@ pub struct Chosen<'a> {
 
 pub enum ParseDeviceTreeError {
     Format,
-    Deserialize,
 }
 
-pub fn parse_device_tree<'a>(opaque: usize) -> Result<Tree<'a>, ParseDeviceTreeError> {
+pub fn parse_device_tree(opaque: usize) -> Result<Dtb, ParseDeviceTreeError> {
     let Ok(ptr) = DtbPtr::from_raw(opaque as *mut _) else {
         return Err(ParseDeviceTreeError::Format);
     };
-    let dtb = Dtb::from(ptr).share();
-    let Ok(tree) = from_raw_mut(&dtb) else {
-        return Err(ParseDeviceTreeError::Deserialize);
-    };
-    Ok(tree)
+    let dtb = Dtb::from(ptr);
+    Ok(dtb)
 }

+ 9 - 3
src/fail.rs

@@ -1,18 +1,24 @@
 use crate::{
-    device_tree::{self, ParseDeviceTreeError, Tree},
+    dt::{self, ParseDeviceTreeError, Tree},
     dynamic, reset,
 };
 use riscv::register::mstatus;
+use serde_device_tree::Dtb;
 
 #[cold]
-pub fn invalid_device_tree<'a>(err: device_tree::ParseDeviceTreeError) -> Tree<'a> {
+pub fn device_tree_format(err: dt::ParseDeviceTreeError) -> Dtb {
     match err {
-        ParseDeviceTreeError::Deserialize => error!("- deserialization failed"),
         ParseDeviceTreeError::Format => error!("- FDT format error"),
     }
     reset::fail()
 }
 
+#[cold]
+pub fn device_tree_deserialize<'a>(err: serde_device_tree::Error) -> Tree<'a> {
+    error!("Device tree deserialization error: {:?}", err);
+    reset::fail()
+}
+
 #[cold]
 pub fn invalid_dynamic_data(err: dynamic::DynamicError) -> (mstatus::MPP, usize) {
     error!("Invalid data in dynamic information:");

+ 8 - 4
src/main.rs

@@ -9,7 +9,7 @@ mod macros;
 
 mod board;
 mod console;
-mod device_tree;
+mod dt;
 mod dynamic;
 mod fail;
 mod reset;
@@ -26,10 +26,14 @@ extern "C" fn main(hart_id: usize, opaque: usize, nonstandard_a2: usize) -> usiz
     rustsbi::LOGO.lines().for_each(|line| info!("{}", line));
     info!("Initializing RustSBI machine-mode environment.");
 
-    let tree = device_tree::parse_device_tree(opaque).unwrap_or_else(fail::invalid_device_tree);
-    for item in tree.chosen.stdout_path.iter() {
-        info!("chosen stdout item: {:?}", item);
+    let dtb = dt::parse_device_tree(opaque).unwrap_or_else(fail::device_tree_format);
+    let dtb = dtb.share();
+    let tree = serde_device_tree::from_raw_mut(&dtb).unwrap_or_else(fail::device_tree_deserialize);
+    if let Some(model) = tree.model {
+        info!("Model: {}", model.iter().next().unwrap_or("<unspecified>"));
     }
+    info!("Chosen stdout item: {}", tree.chosen.stdout_path.iter().next().unwrap_or("<unspecified>"));
+    // TODO handle unspecified by parsing into &'a str
 
     let info = dynamic::read_paddr(nonstandard_a2).unwrap_or_else(fail::no_dynamic_info_available);