Skip to content

Commit

Permalink
feat(loader): support load kernel and dtb from flash
Browse files Browse the repository at this point in the history
Signed-off-by: tfx2001 <tfx2001@outlook.com>
  • Loading branch information
tfx2001 committed Jul 21, 2024
1 parent 014ddfd commit 791c8e6
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 12 deletions.
55 changes: 55 additions & 0 deletions src/loader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use crate::{DTB_LOAD_ADDRESS, SUPERVISOR_ENTRY};

#[derive(PartialEq)]
enum BlobType {
Kernel,
Dts,
}
struct BlobInfo {
type_: BlobType,
start: usize,
length: usize,
}
/// # Blob Info Table
///
/// | Name | Begin | Length |
/// |--------|------------|--------|
/// | Kernel | 0x80040000 | 2 MB |
/// | DTS | 0x80240000 | 16 KB |
///
const BLOB_TABLE: &'static [BlobInfo] = &[
BlobInfo {
type_: BlobType::Kernel,
// Keep 256 KB for SBI firmware.
start: 0x80040000,
length: 2 * 1024 * 1024,
},
BlobInfo {
type_: BlobType::Dts,
start: 0x80240000,
length: 16 * 1024,
},
];

impl BlobInfo {
unsafe fn load(&self, load_address: *mut u8) {
let src: &[u8] = core::slice::from_raw_parts(self.start as *mut _, self.length);
let dst: &mut [u8] = core::slice::from_raw_parts_mut(load_address, self.length);
dst.copy_from_slice(src);
}
}

pub unsafe fn load_test_kernel() {
let info: &BlobInfo = &BLOB_TABLE[0];
assert!(info.type_ == BlobType::Kernel);
assert!(info.start + info.length <= BLOB_TABLE[1].start);

info.load(SUPERVISOR_ENTRY as *mut u8);
}

pub unsafe fn load_dtb() {
let info: &BlobInfo = &BLOB_TABLE[1];
assert!(info.type_ == BlobType::Dts);

info.load(DTB_LOAD_ADDRESS as *mut u8);
}
24 changes: 12 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@

mod board;
mod extension;
mod loader;
mod pmp;
mod riscv_spec;
mod trap;
mod trap_stack;
mod constants {
/// 特权软件入口。
pub(crate) const SUPERVISOR_ENTRY: usize = 0x0108_0000;
pub(crate) const SUPERVISOR_ENTRY: usize = 0x4000_0000;
/// 设备树加载地址。
pub(crate) const DTB_LOAD_ADDRESS: usize = 0x4020_0000;
/// 每个硬件线程设置 16KiB 栈空间。
pub(crate) const LEN_STACK_PER_HART: usize = 16 * 1024;
}
Expand All @@ -21,21 +24,13 @@ use core::arch::asm;
use constants::*;
use trap_stack::local_hsm;

const TEST_KERNEL: &'static [u8] = include_bytes!("kernel.bin");

/// 特权软件信息。
#[derive(Debug)]
struct Supervisor {
start_addr: usize,
opaque: usize,
}

fn load_test_kernel() {
let dst: &mut [u8] =
unsafe { core::slice::from_raw_parts_mut(SUPERVISOR_ENTRY as *mut u8, TEST_KERNEL.len()) };
dst.copy_from_slice(TEST_KERNEL);
}

#[hpm_rt::entry]
fn main() -> ! {
let hartid = riscv::register::mhartid::read();
Expand Down Expand Up @@ -65,14 +60,19 @@ fn main() -> ! {
pmp::print_pmps();
// 设置陷入栈
trap_stack::prepare_for_trap();
// 加载内核
load_test_kernel();
unsafe {
// 加载内核
loader::load_test_kernel();
// 加载设备树
loader::load_dtb()
};
// 设置内核入口
local_hsm().prepare(Supervisor {
start_addr: SUPERVISOR_ENTRY,
opaque: Default::default(),
opaque: DTB_LOAD_ADDRESS,
});
// 准备启动调度
println!("\nStarting kernel ...\n");
unsafe {
asm!("csrw mideleg, {}", in(reg) !0);
asm!("csrw medeleg, {}", in(reg) !0);
Expand Down

0 comments on commit 791c8e6

Please sign in to comment.