Skip to content

Commit c77ae77

Browse files
committed
add log
1 parent cea816c commit c77ae77

File tree

4 files changed

+57
-34
lines changed

4 files changed

+57
-34
lines changed

modules/axfs/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ cfg-if = "1.0"
2828
crate_interface = {version = "0.1", optional = true}
2929
lazyinit = "0.2"
3030
log = "0.4"
31-
lwext4_rust = {git = "https://github.com/Josen-B/lwext4_rust.git"}
31+
lwext4_rust = {git = "https://github.com/Josen-B/lwext4_rust.git", tag ="v1.0"}
3232

3333
[dependencies.fatfs]
3434
default-features = false

modules/axhal/src/dtb.rs

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,81 @@
1-
use fdt_parser::{Fdt, FdtHeader};
2-
1+
//! DTB (Device Tree Blob) related functionality.
32
use core::fmt::Write;
3+
use fdt_parser::{Fdt, FdtHeader};
44
use lazyinit::LazyInit;
55

6-
static BOOTARGS_STR: LazyInit<heapless::String<256>> = LazyInit::new();
6+
/// Record address of the boot argument (DTB address).
7+
pub static BOOT_ARG: LazyInit<usize> = LazyInit::new();
8+
static CACHED_FDT: LazyInit<Fdt<'static>> = LazyInit::new();
9+
static BOOTARGS_STR: LazyInit<heapless::String<1024>> = LazyInit::new();
10+
11+
/// Returns the boot argument.
12+
/// This is typically the device tree blob address passed from the bootloader.
13+
pub fn get_dtb_address() -> usize {
14+
*BOOT_ARG
15+
}
716

8-
pub fn bootargs_message() -> Option<&'static str> {
9-
let fdt_addr = crate::get_bootarg();
17+
/// Get the cached FDT or initialize it if not already done.
18+
pub fn get_fdt() -> Option<&'static Fdt<'static>> {
19+
// Return cached FDT if available
20+
if let Some(fdt) = CACHED_FDT.get() {
21+
return Some(fdt);
22+
}
1023

24+
// Parse and cache the FDT
25+
let fdt_addr = get_dtb_address();
1126
if fdt_addr == 0 {
1227
return None;
1328
}
1429

1530
let virt_addr = crate::mem::phys_to_virt(crate::mem::PhysAddr::from(fdt_addr)).as_usize();
1631

17-
let fdt_header = unsafe {
32+
let fdt = unsafe {
33+
// First read the header to get the size
1834
let header_size = core::mem::size_of::<FdtHeader>();
19-
let ptr = virt_addr as *const u8;
20-
core::slice::from_raw_parts(ptr, header_size)
21-
};
35+
let header_ptr = virt_addr as *const u8;
36+
let header_slice = core::slice::from_raw_parts(header_ptr, header_size);
2237

23-
let fdt_header = match FdtHeader::from_bytes(fdt_header) {
24-
Ok(header) => header,
25-
Err(_) => return None,
26-
};
38+
let fdt_header = match FdtHeader::from_bytes(header_slice) {
39+
Ok(header) => header,
40+
Err(_) => return None,
41+
};
2742

28-
let fdt_bytes = unsafe {
29-
let ptr = virt_addr as *const u8;
3043
let size = fdt_header.total_size() as usize;
31-
core::slice::from_raw_parts(ptr, size)
32-
};
44+
let fdt_ptr = virt_addr as *const u8;
45+
let fdt_slice = core::slice::from_raw_parts(fdt_ptr, size);
46+
47+
let fdt_slice_static = core::mem::transmute::<&[u8], &'static [u8]>(fdt_slice);
3348

34-
let fdt = match Fdt::from_bytes(fdt_bytes) {
35-
Ok(fdt) => fdt,
36-
Err(_) => return None,
49+
match Fdt::from_bytes(fdt_slice_static) {
50+
Ok(fdt) => fdt,
51+
Err(_) => return None,
52+
}
3753
};
3854

55+
// Store the FDT in the cache
56+
CACHED_FDT.init_once(fdt);
57+
CACHED_FDT.get()
58+
}
59+
60+
/// Get the bootargs chosen from the device tree.
61+
pub fn get_chosen() -> Option<&'static str> {
62+
// If bootargs are already cached, return them
63+
if let Some(bootargs) = BOOTARGS_STR.get() {
64+
return Some(bootargs.as_str());
65+
}
66+
67+
// Get or initialize the cached FDT
68+
let fdt = get_fdt()?;
69+
3970
if let Some(chosen) = fdt.chosen() {
4071
if let Some(bootargs) = chosen.bootargs() {
4172
// Store bootargs in static variable
42-
let mut bootargs_str = heapless::String::<256>::new();
73+
let mut bootargs_str = heapless::String::<1024>::new();
4374
if write!(bootargs_str, "{}", bootargs).is_ok() {
4475
BOOTARGS_STR.init_once(bootargs_str);
4576
return Some(BOOTARGS_STR.as_str());
77+
} else {
78+
warn!("Failed to write bootargs");
4679
}
4780
}
4881
}

modules/axhal/src/lib.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,9 @@ pub fn init_percpu_secondary(cpu_id: usize) {
126126
self::percpu::init_secondary(cpu_id);
127127
}
128128

129-
use lazyinit::LazyInit;
130-
131-
static BOOT_ARG: LazyInit<usize> = LazyInit::new();
132-
133129
/// Initializes the platform and boot argument.
134130
/// This function should be called as early as possible.
135131
pub fn init_early(cpu_id: usize, arg: usize) {
136-
BOOT_ARG.init_once(arg);
132+
dtb::BOOT_ARG.init_once(arg);
137133
axplat::init::init_early(cpu_id, arg);
138134
}
139-
140-
/// Returns the boot argument.
141-
/// This is typically the device tree blob address passed from the bootloader.
142-
pub fn get_bootarg() -> usize {
143-
*BOOT_ARG
144-
}

modules/axruntime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ pub fn rust_main(cpu_id: usize, arg: usize) -> ! {
169169
let all_devices = axdriver::init_drivers();
170170

171171
#[cfg(feature = "fs")]
172-
axfs::init_filesystems(all_devices.block, axhal::dtb::bootargs_message());
172+
axfs::init_filesystems(all_devices.block, axhal::dtb::get_chosen());
173173

174174
#[cfg(feature = "net")]
175175
axnet::init_network(all_devices.net);

0 commit comments

Comments
 (0)