Skip to content

Commit 9f18a72

Browse files
committed
refactor: 重命名 Node::from 方法为 Node::from_raw,简化节点创建逻辑并移除不必要的上下文引用
1 parent f356f8f commit 9f18a72

File tree

9 files changed

+130
-202
lines changed

9 files changed

+130
-202
lines changed

fdt-edit/src/fdt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl Fdt {
7575

7676
for raw_node in raw_fdt.all_nodes() {
7777
let level = raw_node.level();
78-
let node = Node::from(raw_node);
78+
let node = Node::from_raw(raw_node);
7979

8080
// 弹出栈直到达到正确的父级别
8181
// level 0 = 根节点,应该直接放入空栈

fdt-edit/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub use display::FmtLevel;
1515
pub use encode::{FdtData, FdtEncoder, NodeEncode};
1616
pub use fdt::{Fdt, MemoryReservation};
1717
pub use node::{
18-
ClockRef, ClockType, FixedClock, MemoryRegion, Node, NodeChosen, NodeClock, NodeClockRef,
18+
ClockRef, ClockType, FixedClock, MemoryRegion, Node, NodeChosen, NodeClock,
1919
NodeInterruptController, NodeMemory, NodeMut, NodeOp, NodePci, NodeRef, PciRange, PciSpace,
2020
};
2121
pub use prop::{Phandle, Property, RangesEntry, RawProperty, RegInfo, Status};

fdt-edit/src/node/chosen.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
use alloc::string::ToString;
2+
13
use super::{NodeOp, NodeTrait, RawNode};
2-
use crate::prop::PropertyKind;
4+
use crate::{Property, prop::PropertyKind};
35

46
/// Chosen 节点,包含启动参数等信息
57
#[derive(Clone, Debug)]
@@ -22,8 +24,8 @@ impl NodeTrait for NodeChosen {
2224
}
2325

2426
impl NodeChosen {
25-
pub fn new(name: &str) -> Self {
26-
NodeChosen(RawNode::new(name))
27+
pub fn new() -> Self {
28+
NodeChosen(RawNode::new("chosen"))
2729
}
2830

2931
/// 获取 bootargs 属性
@@ -41,6 +43,39 @@ impl NodeChosen {
4143
self.find_property_str("stdin-path")
4244
}
4345

46+
pub fn set_bootargs(&mut self, args: Option<&str>) {
47+
if let Some(args) = args {
48+
self.0.set_property(Property {
49+
name: "bootargs".to_string(),
50+
kind: PropertyKind::Str(args.to_string()),
51+
});
52+
} else {
53+
self.0.remove_property("bootargs");
54+
}
55+
}
56+
57+
pub fn set_stdout_path(&mut self, path: Option<&str>) {
58+
if let Some(path) = path {
59+
self.0.set_property(Property {
60+
name: "stdout-path".to_string(),
61+
kind: PropertyKind::Str(path.to_string()),
62+
});
63+
} else {
64+
self.0.remove_property("stdout-path");
65+
}
66+
}
67+
68+
pub fn set_stdin_path(&mut self, path: Option<&str>) {
69+
if let Some(path) = path {
70+
self.0.set_property(Property {
71+
name: "stdin-path".to_string(),
72+
kind: PropertyKind::Str(path.to_string()),
73+
});
74+
} else {
75+
self.0.remove_property("stdin-path");
76+
}
77+
}
78+
4479
/// 查找字符串属性
4580
fn find_property_str(&self, name: &str) -> Option<&str> {
4681
let prop = self.find_property(name)?;

fdt-edit/src/node/clock.rs

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl NodeClock {
112112
///
113113
/// 通过查找每个 phandle 对应的 clock provider 的 #clock-cells,
114114
/// 正确解析 specifier 的长度。
115-
pub fn clocks_with_context<'a>(&self, ctx: &FdtContext<'a>) -> Vec<ClockRef> {
115+
pub fn clocks<'a>(&self, ctx: &FdtContext<'a>) -> Vec<ClockRef> {
116116
let Some(prop) = self.raw.find_property("clocks") else {
117117
return Vec::new();
118118
};
@@ -252,48 +252,3 @@ impl ClockRef {
252252
}
253253
}
254254
}
255-
256-
/// 带上下文的 NodeClock 引用
257-
///
258-
/// 可以直接调用 `clocks()` 方法解析时钟引用
259-
pub struct NodeClockRef<'a> {
260-
pub clock: &'a NodeClock,
261-
pub ctx: &'a FdtContext<'a>,
262-
}
263-
264-
impl<'a> NodeClockRef<'a> {
265-
/// 创建新的带上下文的 NodeClock 引用
266-
pub fn new(clock: &'a NodeClock, ctx: &'a FdtContext<'a>) -> Self {
267-
Self { clock, ctx }
268-
}
269-
270-
/// 解析 clocks 属性,返回时钟引用列表
271-
pub fn clocks(&self) -> Vec<ClockRef> {
272-
self.clock.clocks_with_context(self.ctx)
273-
}
274-
275-
/// 获取 #clock-cells
276-
pub fn clock_cells(&self) -> u32 {
277-
self.clock.clock_cells
278-
}
279-
280-
/// 获取时钟类型
281-
pub fn kind(&self) -> &ClockType {
282-
&self.clock.kind
283-
}
284-
285-
/// 获取节点名称
286-
pub fn name(&self) -> &str {
287-
self.clock.name()
288-
}
289-
290-
/// 获取时钟输出名称列表
291-
pub fn clock_output_names(&self) -> &[String] {
292-
&self.clock.clock_output_names
293-
}
294-
295-
/// 获取时钟名称列表
296-
pub fn clock_names(&self) -> &[String] {
297-
&self.clock.clock_names
298-
}
299-
}

fdt-edit/src/node/memory.rs

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,36 @@
11
use alloc::vec::Vec;
22

33
use super::{NodeOp, NodeTrait, RawNode};
4-
use crate::prop::PropertyKind;
54
pub use fdt_raw::MemoryRegion;
65

76
/// Memory 节点,描述物理内存布局
87
#[derive(Clone, Debug)]
9-
pub struct NodeMemory(pub(crate) RawNode);
8+
pub struct NodeMemory {
9+
raw: RawNode,
10+
pub regions: Vec<MemoryRegion>,
11+
}
1012

1113
impl NodeOp for NodeMemory {}
1214

1315
impl NodeTrait for NodeMemory {
1416
fn as_raw(&self) -> &RawNode {
15-
&self.0
17+
&self.raw
1618
}
1719

1820
fn as_raw_mut(&mut self) -> &mut RawNode {
19-
&mut self.0
21+
&mut self.raw
2022
}
2123

2224
fn to_raw(self) -> RawNode {
23-
self.0
25+
self.raw
2426
}
2527
}
2628

2729
impl NodeMemory {
2830
pub fn new(name: &str) -> Self {
29-
NodeMemory(RawNode::new(name))
30-
}
31-
32-
/// 获取内存区域列表
33-
///
34-
/// Memory 节点的 reg 属性描述了物理内存的布局
35-
pub fn regions(&self) -> Vec<MemoryRegion> {
36-
let Some(prop) = self.find_property("reg") else {
37-
return Vec::new();
38-
};
39-
40-
let PropertyKind::Reg(entries) = &prop.kind else {
41-
return Vec::new();
42-
};
43-
44-
entries
45-
.iter()
46-
.map(|entry| MemoryRegion {
47-
address: entry.address,
48-
size: entry.size.unwrap_or(0),
49-
})
50-
.collect()
51-
}
52-
53-
/// 计算总内存大小
54-
pub fn total_size(&self) -> u64 {
55-
self.regions().iter().map(|r| r.size).sum()
31+
NodeMemory {
32+
raw: RawNode::new(name),
33+
regions: Vec::new(),
34+
}
5635
}
5736
}

fdt-edit/src/node/mod.rs

Lines changed: 51 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mod pci;
1717
mod r#ref;
1818

1919
pub use chosen::NodeChosen;
20-
pub use clock::{ClockRef, ClockType, FixedClock, NodeClock, NodeClockRef};
20+
pub use clock::{ClockRef, ClockType, FixedClock, NodeClock};
2121
pub use interrupt_controller::{NodeInterruptController, is_interrupt_controller_node};
2222
pub use memory::{MemoryRegion, NodeMemory};
2323
pub 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

Comments
 (0)