Skip to content

Commit

Permalink
add environ funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
KouweiLee committed Sep 10, 2023
1 parent 6a4b056 commit 1fcabca
Show file tree
Hide file tree
Showing 19 changed files with 509 additions and 39 deletions.
94 changes: 91 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ NET_DEV ?= user
IP ?= 10.0.2.15
GW ?= 10.0.2.2

#args and env
ARGS ?=
ENVS ?=

# App type
ifeq ($(wildcard $(APP)),)
$(error Application path "$(APP)" is not valid)
Expand Down
4 changes: 4 additions & 0 deletions api/arceos_posix_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ extern crate axruntime;
#[cfg(feature = "alloc")]
extern crate alloc;

#[cfg(feature = "alloc")]
pub use axruntime::{environ, OUR_ENVIRON, environ_iter};

#[macro_use]
mod utils;

Expand Down Expand Up @@ -60,3 +63,4 @@ pub use imp::pthread::mutex::{
};
#[cfg(feature = "multitask")]
pub use imp::pthread::{sys_pthread_create, sys_pthread_exit, sys_pthread_join, sys_pthread_self};

2 changes: 2 additions & 0 deletions apps/c/memtest/expect_trace.out
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ allocated addr=0x[0-9a-f]\{16\}
allocated addr=0x[0-9a-f]\{16\}
allocated addr=0x[0-9a-f]\{16\}
Memory tests run OK!
Running environ tests...
Environ tests run OK!
Shutting down...
6 changes: 6 additions & 0 deletions apps/c/memtest/memtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,11 @@ int main()
}
free(p);
puts("Memory tests run OK!");
puts("Running environ tests...");
char *env1 = "env1", *ex1 = "ex1", *ex2 = "ex_2";
if(setenv(env1, ex1, 1) || strcmp(ex1, getenv(env1))) puts("set new env is wrong");
if(setenv(env1, ex2, 1) || strcmp(ex2, getenv(env1))) puts("set old env is wrong");
if(setenv(env1, ex1, 0) || strcmp(ex2, getenv(env1))) puts("override the old env is wrong");
puts("Environ tests run OK!");
return 0;
}
14 changes: 14 additions & 0 deletions crates/dtb/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "dtb"
version = "0.1.0"
edition = "2021"
authors = ["Leping Wang <xuehao14@126.com>"]
description = "Device tree basic operations"
license = "GPL-3.0-or-later OR Apache-2.0"
homepage = "https://github.com/rcore-os/arceos"
repository = "https://github.com/rcore-os/arceos/tree/main/crates/dtb"
documentation = "https://rcore-os.github.io/arceos/dtb/index.html"

[dependencies]
fdt-rs = { version = "0.4.3", default-features = false }
lazy_init = { path = "../../crates/lazy_init" }
114 changes: 114 additions & 0 deletions crates/dtb/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//! Some useful interfaces for device tree.

#![no_std]
use fdt_rs::{
base::{DevTree, DevTreeNode, DevTreeProp},
prelude::{FallibleIterator, PropReader},
};
use lazy_init::LazyInit;

/// The device Tree.
static TREE: LazyInit<DevTree> = LazyInit::new();

/// Init device tree from given address.
/// # Safety
///
/// Callers of this method the must guarantee the following:
///
/// - The passed address is 32-bit aligned.
pub unsafe fn init(dtb: *const u8) {
TREE.init_by(DevTree::from_raw_pointer(dtb).unwrap());
}

/// A node on the device tree.
pub struct DeviceNode<'a>(DevTreeNode<'a, 'static>);

/// A prop of a node.
pub struct DeviceProp<'a>(DevTreeProp<'a, 'static>);

impl<'a> DeviceNode<'a> {
/// Find a node's prop with given name(may not exist).
pub fn find_prop(&'a self, name: &str) -> Option<DeviceProp<'a>> {
self.0
.props()
.filter(|p| p.name().map(|s| s == name))
.next()
.unwrap()
.map(DeviceProp)
}

/// Find a node's prop with given name(must exist).
pub fn prop(&'a self, name: &str) -> DeviceProp<'a> {
self.find_prop(name).unwrap()
}
}

impl<'a> DeviceProp<'a> {
/// Assume the prop is a u32 array. Get an element.
pub fn u32(&self, index: usize) -> u32 {
self.0.u32(index).unwrap()
}

/// Assume the prop is a u64 array. Get an element.
pub fn u64(&self, index: usize) -> u64 {
self.0.u64(index).unwrap()
}

/// Assume the prop is a str. Get the whole str.
pub fn str(&self) -> &'static str {
self.0.str().unwrap()
}
}

/// Find the first node with given compatible(may not exist).
pub fn compatible_node(compatible: &str) -> Option<DeviceNode> {
TREE.compatible_nodes(compatible)
.next()
.unwrap()
.map(DeviceNode)
}

/// Find the first node with given name(may not exist).
pub fn get_node(name: &str) -> Option<DeviceNode> {
TREE.nodes()
.filter(|n| n.name().map(|s| s == name))
.next()
.unwrap()
.map(DeviceNode)
}

/// Do something for all devices with given type.
pub fn devices<F>(device_type: &str, f: F)
where
F: Fn(DeviceNode),
{
TREE.nodes()
.filter_map(|n| {
let n = DeviceNode(n);
Ok(
if n.find_prop("device_type").map(|p| p.str()) == Some(device_type) {
Some(n)
} else {
None
},
)
})
.for_each(|n| {
f(n);
Ok(())
})
.unwrap();
}

/// Do something for all nodes with given compatible.
pub fn compatible_nodes<F>(compatible: &str, mut f: F)
where
F: FnMut(DeviceNode),
{
TREE.compatible_nodes(compatible)
.for_each(|n| {
f(DeviceNode(n));
Ok(())
})
.unwrap();
}
2 changes: 2 additions & 0 deletions modules/axhal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ raw-cpuid = "11.0"
[target.'cfg(any(target_arch = "riscv32", target_arch = "riscv64"))'.dependencies]
riscv = "0.10"
sbi-rt = { version = "0.0.2", features = ["legacy"] }
dtb = {path = "../../crates/dtb" }

[target.'cfg(target_arch = "aarch64")'.dependencies]
aarch64-cpu = "9.3"
tock-registers = "0.8"
arm_gic = { path = "../../crates/arm_gic" }
arm_pl011 = { path = "../../crates/arm_pl011" }
dw_apb_uart = { path = "../../crates/dw_apb_uart" }
dtb = {path = "../../crates/dtb" }

[build-dependencies]
axconfig = { path = "../axconfig" }
2 changes: 2 additions & 0 deletions modules/axhal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,5 @@ pub use self::platform::platform_init;

#[cfg(feature = "smp")]
pub use self::platform::platform_init_secondary;

pub static mut COMLINE_BUF:[u8; 256] = [0; 256];
Loading

0 comments on commit 1fcabca

Please sign in to comment.