@@ -17,7 +17,7 @@ mod pci;
1717mod r#ref;
1818
1919pub use chosen:: NodeChosen ;
20- pub use clock:: { ClockRef , ClockType , FixedClock , NodeClock , NodeClockRef } ;
20+ pub use clock:: { ClockRef , ClockType , FixedClock , NodeClock } ;
2121pub use interrupt_controller:: { NodeInterruptController , is_interrupt_controller_node} ;
2222pub use memory:: { MemoryRegion , NodeMemory } ;
2323pub use pci:: * ;
@@ -54,85 +54,64 @@ impl Node {
5454 Self :: Raw ( RawNode :: new ( "" ) )
5555 }
5656
57- fn new ( raw : RawNode ) -> Self {
58- let name = raw. name . as_str ( ) ;
57+ // fn new(raw: RawNode) -> Self {
58+ // let name = raw.name.as_str();
5959
60- // 根据节点名称或属性判断类型
61- if name == "chosen" {
62- return Self :: Chosen ( NodeChosen ( raw) ) ;
63- }
60+ // // 根据节点名称或属性判断类型
61+ // if name == "chosen" {
62+ // return Self::Chosen(NodeChosen(raw));
63+ // }
6464
65- if name. starts_with ( "memory" ) {
66- return Self :: Memory ( NodeMemory ( raw) ) ;
67- }
65+ // if name.starts_with("memory") {
66+ // return Self::Memory(NodeMemory(raw));
67+ // }
6868
69- // 检查是否是中断控制器
70- if is_interrupt_controller_node ( & raw ) {
71- return Self :: InterruptController ( NodeInterruptController :: new ( raw) ) ;
72- }
69+ // // 检查是否是中断控制器
70+ // if is_interrupt_controller_node(&raw) {
71+ // return Self::InterruptController(NodeInterruptController::new(raw));
72+ // }
7373
74- // 检查是否是时钟提供者
75- if raw. properties . iter ( ) . any ( |p| p. name ( ) == "#clock-cells" ) {
76- return Self :: Clock ( NodeClock :: new ( raw) ) ;
77- }
74+ // // 检查是否是时钟提供者
75+ // if raw.properties.iter().any(|p| p.name() == "#clock-cells") {
76+ // return Self::Clock(NodeClock::new(raw));
77+ // }
7878
79- // 检查 device_type 属性
80- let mut node = Self :: Raw ( raw) ;
81- if let Some ( t) = node. find_property ( "device_type" )
82- && let PropertyKind :: Str ( dt) = & t. kind
83- && dt. as_str ( ) == "pci"
84- {
85- node = Self :: Pci ( NodePci ( node. to_raw ( ) ) ) ;
86- }
87- node
88- }
79+ // // 检查 device_type 属性
80+ // let mut node = Self::Raw(raw);
81+ // if let Some(t) = node.find_property("device_type")
82+ // && let PropertyKind::Str(dt) = &t.kind
83+ // && dt.as_str() == "pci"
84+ // {
85+ // node = Self::Pci(NodePci(node.to_raw()));
86+ // }
87+ // node
88+ // }
8989
9090 pub fn new_raw ( name : & str ) -> Self {
91- Self :: new ( RawNode :: new ( name) )
91+ Self :: Raw ( RawNode :: new ( name) )
9292 }
9393
94- /// 尝试转换为 Chosen 节点
95- pub fn as_chosen ( & self ) -> Option < & NodeChosen > {
96- if let Node :: Chosen ( c) = self {
97- Some ( c)
98- } else {
99- None
100- }
101- }
94+ pub fn from_raw < ' a > ( val : fdt_raw:: Node < ' a > ) -> Self {
95+ match val {
96+ fdt_raw:: Node :: General ( node_base) => {
10297
103- /// 尝试转换为 Memory 节点
104- pub fn as_memory ( & self ) -> Option < & NodeMemory > {
105- if let Node :: Memory ( m) = self {
106- Some ( m)
107- } else {
108- None
109- }
110- }
98+
11199
112- /// 尝试转换为 Pci 节点
113- pub fn as_pci ( & self ) -> Option < & NodePci > {
114- if let Node :: Pci ( p) = self {
115- Some ( p)
116- } else {
117- None
118- }
119- }
120-
121- /// 尝试转换为 Clock 节点
122- pub fn as_clock ( & self ) -> Option < & NodeClock > {
123- if let Node :: Clock ( c) = self {
124- Some ( c)
125- } else {
126- None
127- }
128- }
129-
130- /// 尝试转换为 InterruptController 节点
131- pub fn as_interrupt_controller ( & self ) -> Option < & NodeInterruptController > {
132- if let Node :: InterruptController ( ic) = self {
133- Some ( ic)
134- } else {
135- None
100+ let raw_node = RawNode :: from ( node_base) ;
101+ Self :: Raw ( raw_node)
102+ }
103+ fdt_raw:: Node :: Chosen ( chosen) => {
104+ let mut new_one = NodeChosen :: new ( ) ;
105+ new_one. set_bootargs ( chosen. bootargs ( ) ) ;
106+ new_one. set_stdout_path ( chosen. stdout_path ( ) ) ;
107+ new_one. set_stdin_path ( chosen. stdin_path ( ) ) ;
108+ Self :: Chosen ( new_one)
109+ }
110+ fdt_raw:: Node :: Memory ( memory) => {
111+ let mut raw_node = NodeMemory :: new ( memory. name ( ) ) ;
112+ raw_node. regions = memory. regions ( ) . collect ( ) ;
113+ Self :: Memory ( raw_node)
114+ }
136115 }
137116 }
138117}
@@ -543,33 +522,15 @@ impl RawNode {
543522 }
544523}
545524
546- impl < ' a > From < fdt_raw:: Node < ' a > > for Node {
547- fn from ( raw_node : fdt_raw:: Node < ' a > ) -> Self {
525+ impl < ' a > From < fdt_raw:: NodeBase < ' a > > for RawNode {
526+ fn from ( raw_node : fdt_raw:: NodeBase < ' a > ) -> Self {
548527 let mut node = RawNode :: new ( raw_node. name ( ) ) ;
549528 // 转换属性
550529 for prop in raw_node. properties ( ) {
551- // 特殊处理 reg 属性,需要 context 信息
552- if prop. name ( ) == "reg"
553- && let Some ( reg_iter) = raw_node. reg ( )
554- {
555- let entries = reg_iter
556- . map ( |e| super :: prop:: Reg {
557- address : e. address ,
558- size : e. size ,
559- } )
560- . collect ( ) ;
561- let prop = super :: prop:: Property {
562- name : "reg" . to_string ( ) ,
563- kind : super :: prop:: PropertyKind :: Reg ( entries) ,
564- } ;
565- node. properties . push ( prop) ;
566- continue ;
567- }
568-
569530 // 其他属性使用标准的 From 转换
570531 let raw = super :: prop:: Property :: from ( prop) ;
571532 node. properties . push ( raw) ;
572533 }
573- Self :: new ( node)
534+ node
574535 }
575536}
0 commit comments