Skip to content

Commit 9a826e2

Browse files
committed
update
1 parent 83d8daf commit 9a826e2

File tree

6 files changed

+141
-11
lines changed

6 files changed

+141
-11
lines changed

Cargo.lock

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

platforms/axplat-aarch64-dyn/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ version = "0.1.0"
1212
axconfig-macros = "0.2"
1313
axplat = {git = "https://github.com/arceos-org/axplat_crates"}
1414
pie-boot = {version = "0.2.1"}
15+
any-uart = "0.2.11"
16+
spin = "0.10"
17+
axcpu = { workspace = true }
18+
heapless = "0.8"
19+
fdt-parser = "0.4"
Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,80 @@
1+
use core::ptr::NonNull;
2+
3+
use any_uart::{Receiver, Sender};
14
use axplat::console::ConsoleIf;
5+
use fdt_parser::Fdt;
6+
use spin::Mutex;
7+
8+
use crate::boot_info;
9+
10+
static TX: Mutex<Option<Sender>> = Mutex::new(None);
11+
static RX: Mutex<Option<Receiver>> = Mutex::new(None);
12+
13+
pub(crate) fn setup_early() -> Option<()> {
14+
let ptr = boot_info().fdt?;
15+
let fdt = Fdt::from_ptr(ptr).ok()?;
16+
let choson = fdt.chosen()?;
17+
let node = choson.debugcon()?;
18+
fn phys_to_virt(p: usize) -> *mut u8 {
19+
p as _
20+
}
21+
22+
let mut uart = any_uart::Uart::new_by_fdt_node(&node, phys_to_virt)?;
23+
*TX.lock() = uart.tx.take();
24+
*RX.lock() = uart.rx.take();
25+
26+
Some(())
27+
}
228

329
struct ConsoleIfImpl;
430

531
#[impl_plat_interface]
632
impl ConsoleIf for ConsoleIfImpl {
733
/// Writes given bytes to the console.
834
fn write_bytes(bytes: &[u8]) {
9-
todo!()
35+
let mut g = TX.lock();
36+
if let Some(tx) = g.as_mut() {
37+
macro_rules! write_byte {
38+
($b:expr) => {
39+
let _ = any_uart::block!(tx.write($b));
40+
};
41+
}
42+
43+
for &c in bytes {
44+
match c {
45+
b'\n' => {
46+
write_byte!(b'\r');
47+
write_byte!(b'\n');
48+
}
49+
c => {
50+
write_byte!(c);
51+
}
52+
}
53+
}
54+
}
1055
}
1156

1257
/// Reads bytes from the console into the given mutable slice.
1358
///
1459
/// Returns the number of bytes read.
1560
fn read_bytes(bytes: &mut [u8]) -> usize {
16-
todo!()
61+
let mut read_len = 0;
62+
while read_len < bytes.len() {
63+
if let Some(c) = getchar() {
64+
bytes[read_len] = c;
65+
} else {
66+
break;
67+
}
68+
read_len += 1;
69+
}
70+
read_len
71+
}
72+
}
73+
fn getchar() -> Option<u8> {
74+
let mut g = RX.lock();
75+
if let Some(rx) = g.as_mut() {
76+
any_uart::block!(rx.read()).ok()
77+
}else{
78+
None
1779
}
1880
}

platforms/axplat-aarch64-dyn/src/init.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use axplat::init::InitIf;
22

3+
use crate::console;
4+
35
struct InitIfImpl;
46

57
#[impl_plat_interface]
@@ -29,14 +31,15 @@ impl InitIf for InitIfImpl {
2931
/// * Early console is initialized.
3032
/// * Current monotonic time and wall time can be obtained.
3133
fn init_early(cpu_id: usize, arg: usize) {
32-
todo!()
34+
axcpu::init::init_trap();
35+
console::setup_early();
3336
}
3437

3538
/// Initializes the platform at the early stage for secondary cores.
3639
///
3740
/// See [`init_early`] for details.
3841
fn init_early_secondary(cpu_id: usize) {
39-
todo!()
42+
axcpu::init::init_trap();
4043
}
4144

4245
/// Initializes the platform at the later stage for the primary core.

platforms/axplat-aarch64-dyn/src/lib.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
extern crate axplat;
66

77
use pie_boot::BootInfo;
8+
use spin::Once;
89

910
mod console;
1011
mod init;
@@ -17,8 +18,20 @@ mod config {
1718
axconfig_macros::include_configs!(path_env = "AX_CONFIG_PATH", fallback = "axconfig.toml");
1819
}
1920

21+
static BOOT_INFO: Once<BootInfo> = Once::new();
22+
2023
#[pie_boot::entry]
2124
fn main(args: &BootInfo) -> ! {
22-
// TODO: Implement actual bootstrap logic
23-
axplat::call_main(args.cpu_id, args.fdt.map(|p| p.as_ptr() as usize).unwrap_or_default());
25+
BOOT_INFO.call_once(move || args.clone());
26+
27+
mem::setup();
28+
29+
axplat::call_main(
30+
args.cpu_id,
31+
args.fdt.map(|p| p.as_ptr() as usize).unwrap_or_default(),
32+
);
33+
}
34+
35+
fn boot_info() -> &'static BootInfo {
36+
BOOT_INFO.wait()
2437
}
Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,57 @@
11
use axplat::mem::{MemIf, RawRange};
2+
use heapless::Vec;
3+
use pie_boot::MemoryRegionKind;
4+
use spin::{Mutex, Once};
5+
6+
use crate::boot_info;
27

38
struct MemIfImpl;
49

10+
static RAM_LIST: Once<Vec<RawRange, 32>> = Once::new();
11+
static RESERVED_LIST: Once<Vec<RawRange, 32>> = Once::new();
12+
static MMIO: Mutex<Vec<RawRange, 32>> = Mutex::new(Vec::new());
13+
14+
pub fn setup() {
15+
RAM_LIST.call_once(|| {
16+
let mut ram_list = Vec::new();
17+
for region in boot_info()
18+
.memory_regions
19+
.iter()
20+
.filter(|one| matches!(one.kind, MemoryRegionKind::Ram))
21+
.map(|one| (one.start, one.end - one.start))
22+
{
23+
let _ = ram_list.push(region);
24+
}
25+
ram_list
26+
});
27+
28+
RESERVED_LIST.call_once(|| {
29+
let mut ram_list = Vec::new();
30+
for region in boot_info()
31+
.memory_regions
32+
.iter()
33+
.filter(|one| {
34+
matches!(
35+
one.kind,
36+
MemoryRegionKind::Reserved | MemoryRegionKind::Bootloader
37+
)
38+
})
39+
.map(|one| (one.start, one.end - one.start))
40+
{
41+
let _ = ram_list.push(region);
42+
}
43+
ram_list
44+
});
45+
}
46+
547
#[impl_plat_interface]
648
impl MemIf for MemIfImpl {
749
/// Returns all physical memory (RAM) ranges on the platform.
850
///
951
/// All memory ranges except reserved ranges (including the kernel loaded
1052
/// range) are free for allocation.
1153
fn phys_ram_ranges() -> &'static [RawRange] {
12-
todo!()
54+
RAM_LIST.wait()
1355
}
1456

1557
/// Returns all reserved physical memory ranges on the platform.
@@ -20,11 +62,11 @@ impl MemIf for MemIfImpl {
2062
/// Note that the ranges returned should not include the range where the
2163
/// kernel is loaded.
2264
fn reserved_phys_ram_ranges() -> &'static [RawRange] {
23-
todo!()
65+
RESERVED_LIST.wait()
2466
}
2567

2668
/// Returns all device memory (MMIO) ranges on the platform.
2769
fn mmio_ranges() -> &'static [RawRange] {
28-
todo!()
70+
&[]
2971
}
3072
}

0 commit comments

Comments
 (0)