os/
loader.rs

1//! Loading user applications into memory
2
3/// Get the total number of applications.
4use alloc::vec::Vec;
5use lazy_static::*;
6///get app number
7pub fn get_num_app() -> usize {
8    unsafe extern "C" {
9        safe fn _num_app();
10    }
11    unsafe { (_num_app as usize as *const usize).read_volatile() }
12}
13/// get applications data
14pub fn get_app_data(app_id: usize) -> &'static [u8] {
15    unsafe extern "C" {
16        safe fn _num_app();
17    }
18    let num_app_ptr = _num_app as usize as *const usize;
19    let num_app = get_num_app();
20    let app_start = unsafe { core::slice::from_raw_parts(num_app_ptr.add(1), num_app + 1) };
21    assert!(app_id < num_app);
22    unsafe {
23        core::slice::from_raw_parts(
24            app_start[app_id] as *const u8,
25            app_start[app_id + 1] - app_start[app_id],
26        )
27    }
28}
29
30lazy_static! {
31    ///All of app's name
32    static ref APP_NAMES: Vec<&'static str> = {
33        let num_app = get_num_app();
34        unsafe extern "C" {
35            safe fn _app_names();
36        }
37        let mut start = _app_names as usize as *const u8;
38        let mut v = Vec::new();
39        unsafe {
40            for _ in 0..num_app {
41                let mut end = start;
42                while end.read_volatile() != b'\0' {
43                    end = end.add(1);
44                }
45                let slice = core::slice::from_raw_parts(start, end as usize - start as usize);
46                let str = core::str::from_utf8(slice).unwrap();
47                v.push(str);
48                start = end.add(1);
49            }
50        }
51        v
52    };
53}
54
55#[allow(unused)]
56///get app data from name
57pub fn get_app_data_by_name(name: &str) -> Option<&'static [u8]> {
58    let num_app = get_num_app();
59    (0..num_app)
60        .find(|&i| APP_NAMES[i] == name)
61        .map(get_app_data)
62}
63///list all apps
64pub fn list_apps() {
65    println!("/**** APPS ****");
66    for app in APP_NAMES.iter() {
67        println!("{}", app);
68    }
69    println!("**************/");
70}