Skip to content

Commit

Permalink
feat(exec2.5): impl multi-application support
Browse files Browse the repository at this point in the history
  • Loading branch information
jackyliu16 committed Nov 19, 2023
1 parent 6cb73d2 commit 724adc8
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 43 deletions.
88 changes: 63 additions & 25 deletions apps/loader/src/main.rs
Expand Up @@ -12,33 +12,37 @@ const LOAD_START: usize = 0xffff_ffc0_8010_0000;
fn main() {
println!("RUN LOADER");
let apps_start = PLASH_START as *const u8;

// Gain NUM
let byte_num = unsafe { core::slice::from_raw_parts(apps_start, size_of::<u8>()) };
let app_num = u8::from_be_bytes([byte_num[0]]);
println!("DETACT {app_num} app");

let byte = unsafe { core::slice::from_raw_parts(apps_start, size_of::<u64>()) };
for b in byte {
println!("{:08b}", b);
// for b in byte { println!("{:08b}", b); }
// Gain Each App Size
let mut apps: [APP; MAX_APP_NUM] = [APP::empty(); MAX_APP_NUM];
let byte_apps_sizes = unsafe {
// NOTE: BC Rust Internal structure autocomplete will fill vacancy, thus u16 rather than u8
core::slice::from_raw_parts(
apps_start.offset(size_of::<u16>() as isize),
app_num as usize * size_of::<u16>(),
)
};
println!("app sizes: {byte_apps_sizes:?}");

let head_offset = size_of::<u8>() + app_num as usize * size_of::<u16>();
for i in 0..app_num {
let i = i as usize;
apps[i] = unsafe {
APP::new(
apps_start.offset(head_offset as isize),
u16::from_be_bytes([byte_apps_sizes[i * 2], byte_apps_sizes[i * 2 + 1]]) as usize,
)
}
}
let app_size_1 = u16::from_be_bytes([byte[0], byte[1]]);
let app_size_2 = u16::from_be_bytes([byte[2], byte[3]]);
println!("size 1: {app_size_1}, size 2: {app_size_2}");
let app_size_1 = u16::from_be_bytes([byte[4], byte[5]]);
let app_size_2 = u16::from_be_bytes([byte[6], byte[7]]);
println!("size 1: {app_size_1}, size 2: {app_size_2}");
let app_size_1 = u32::from_be_bytes([byte[0], byte[1], byte[2], byte[3]]);
let app_size_2 = u32::from_be_bytes([byte[4], byte[5], byte[6], byte[7]]);
println!("size 1: {app_size_1}, size 2: {app_size_2}");

println!("Load payload ...");
let app_size_1 = u16::from_le_bytes([byte[0], byte[1]]);
let app_size_2 = u16::from_le_bytes([byte[2], byte[3]]);
println!("size 1: {app_size_1}, size 2: {app_size_2}");
let app_size_1 = u16::from_le_bytes([byte[4], byte[5]]);
let app_size_2 = u16::from_le_bytes([byte[6], byte[7]]);
println!("size 1: {app_size_1}, size 2: {app_size_2}");
let app_size_1 = u32::from_le_bytes([byte[0], byte[1], byte[2], byte[3]]);
let app_size_2 = u32::from_le_bytes([byte[4], byte[5], byte[6], byte[7]]);
println!("size 1: {app_size_1}, size 2: {app_size_2}");

println!("sizeByte: {byte:?}");

println!("{apps:?}");
// let read_only_app1 = unsafe {
// core::slice::from_raw_parts(
// apps_start.offset(size_of::<u16>() as isize),
Expand Down Expand Up @@ -120,7 +124,7 @@ fn main() {
// run_start = in(reg) jump_location
// )
// }
println!("App 2 Finish");
// println!("App 2 Finish");
}

const SYS_HELLO: usize = 1;
Expand Down Expand Up @@ -149,3 +153,37 @@ fn abi_terminate() -> ! {
println!("[ABI:TERMINATE]: Shutting Down !!!");
arceos_api::sys::ax_terminate();
}

const MAX_APP_NUM: usize = u8::MAX as usize;
#[derive(Clone, Copy)]
struct APP {
start_addr: *const u8,
size: usize,
}

impl APP {
pub fn new(start_addr: *const u8, size: usize) -> Self {
Self { start_addr, size }
}
pub fn empty() -> Self {
Self {
start_addr: 0x0 as *const u8,
size: 0,
}
}
}

impl core::fmt::Debug for APP {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
// 如果 size 为 0,则不显示任何信息
if self.size == 0 {
return Ok(());
}

// 自定义打印的行为
f.debug_struct("APP")
.field("start_addr", &self.start_addr)
.field("size", &self.size)
.finish()
}
}
35 changes: 17 additions & 18 deletions playload/generater.sh
Expand Up @@ -29,7 +29,7 @@ function generateBinary() {
app_names=("hello_nop" "hello_app")
declare -A app_sizes
declare -a link
app_num=0
NUM_OF_IMAGE=0

echo "==================== HEAD OF GEN ==================="

Expand All @@ -38,34 +38,33 @@ for name in "${app_names[@]}"; do
app_size=$(generateBinary $name)
app_sizes["$name"]=$app_size
link+=${app_size}
NUM_OF_IMAGE=$(expr $NUM_OF_IMAGE + 1)
done

echo "app_sizes: ${app_sizes[@]}"
echo "link: ${link}"
echo "NUM_OF_IMAGE": $NUM_OF_IMAGE

echo "==================== TAIL OF GEN ==================="

# PFLASH 32M ]
# PFLASH 32M ] [ NUM_OF_IMAGE ]
# PFLASH 32M ] [ u16:2B ] [ BYTE_LIST:2B*NUM_OF_IMAGE ]
# PFLASH 32M ] [ ] [ ] [ ] [ ]
# PFLASH 32M ] [ u16:2B ] [ BYTE_LIST:4B*NUM_OF_IMAGE ]
# PFLASH 32M ] [ 2B + 4B * NUM_OF_IMAGE ] [ ] [ ] [ ]

cd $BASE_DIR
printf "%02x" $NUM_OF_IMAGE | xxd -r -p >num.bin # NOTE: not allow app > 255
echo -n "${app_sizes[@]}" | xxd -r -p > size.bin
echo "size.bin size: $(stat -c%s "./size.bin")"

dd if=/dev/zero of=./apps.bin bs=1M count=32
dd if=./size.bin of=./apps.bin conv=notrunc
# dd if=./hello_nop/hello_nop.bin of=./apps.bin conv=notrunc bs=1B seek=2
# dd if=./hello_app/hello_app.bin of=./apps.bin conv=notrunc bs=1B seek=$(($hello_nop_size + 2))
# seek=$(($hello_nop_size + 2))



# # Using Zero to fill the block to 32M(32Times, one times for 1M)
# dd if=/dev/zero of=./apps.bin bs=1M count=32
# # Add origin app into the end of the file (not cover)
# dd if=./size.bin of=./apps.bin conv=notrunc bs=1B seek=2
# dd if=./hello_app.bin of=./apps.bin conv=notrunc bs=1B seek=4
# mv $BASE_DIR/hello_app/apps.bin "$BASE_DIR/apps.bin"
dd if=/dev/zero of=./apps.bin bs=1M count=32
dd if=./num.bin of=./apps.bin conv=notrunc
dd if=./size.bin of=./apps.bin conv=notrunc bs=1B seek=2

start_offset=$((2 + 4 * $NUM_OF_IMAGE)) # NUM_OF_IMAGE:2B + IMAGE_SIZE:4B * NUM_OF_IMAGE
echo "start_offset" $start_offset
for ((i=0; i<${#app_names[@]}; i++)); do
app_name=${app_names[i]}
app_size=${app_sizes[i]}
dd if="$BASE_DIR/$app_name/$app_name.bin" of=./apps.bin conv=notrunc bs=1B seek=$start_offset
start_offset=$((start_offset + app_size))
done

0 comments on commit 724adc8

Please sign in to comment.