Skip to content

Commit 88344a0

Browse files
authored
Merge pull request #8 from hanbings/feat-uefi
feat(uefi): supports memory allocation, simple text output and testing
2 parents 6e9a49f + f8f689f commit 88344a0

28 files changed

+740
-24
lines changed

.github/workflows/test.yml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
name: Test
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
tests:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- name: Checkout
11+
uses: actions/checkout@v4
12+
13+
- name: Set up Rust toolchain
14+
uses: actions-rust-lang/setup-rust-toolchain@v1
15+
with:
16+
target: riscv64gc-unknown-none-elf
17+
toolchain: nightly-2025-03-31
18+
19+
- name: Install cargo-binutils and rust-objcopy
20+
run: |
21+
rustup component add llvm-tools-preview
22+
cargo install cargo-binutils
23+
24+
- name: Prepare for Build
25+
run: |
26+
# Fix breaking update in rust dependency.
27+
cargo fetch
28+
find ~/.cargo/git/checkouts/ -type f -name '*.rs' -exec sed -i 's/#\[unsafe(naked)\]/#[naked]/g' {} +
29+
30+
# Correct incorrect build directory name in build script.
31+
sed -i '/^SBI :=/s#riscv64imac-unknown-none-elf#riscv64gc-unknown-none-elf#' Makefile
32+
sed -i '/^SBI :=/s#riscv64imac-unknown-none-elf#riscv64gc-unknown-none-elf#' scripts/make/build.mk
33+
34+
make clone-rustsbi
35+
sed -i -E '/^\s*#\[repr\(align\(16\)\)\]\s*$/d' rustsbi/prototyper/prototyper/src/sbi/early_trap.rs
36+
37+
- name: Build with Makefile
38+
run: |
39+
make defconfig
40+
make
41+
42+
- name: Set up Python
43+
uses: actions/setup-python@v5
44+
with:
45+
python-version: '3.12'
46+
47+
- name: Install dev dependencies
48+
run: |
49+
sudo apt update
50+
sudo apt install -y uuid-dev
51+
52+
- name: Build EDK2
53+
run: sh scripts/test/build_edk2.sh
54+
55+
- name: Generate disk image
56+
run: sh scripts/test/disk.sh
57+
58+
- name: Create EFI System Partition (ESP)
59+
run: sh scripts/test/make_esp.sh
60+
61+
- name: Set up QEMU
62+
run: |
63+
sudo apt update
64+
sudo apt install -y qemu-system-misc
65+
66+
- name: Run QEMU
67+
run: make virtiodisk > qemu.log
68+
69+
- name: Upload QEMU log
70+
uses: actions/upload-artifact@v4
71+
with:
72+
name: log
73+
path: qemu.log
74+
75+
- name: Check QEMU output
76+
run: sh scripts/test/check_hello_test.sh

.gitignore

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ target/
2222

2323
mnt/
2424

25-
arceboot.bin
26-
27-
*.img
28-
25+
# This part is used by the process files, executable files, image files, etc.
26+
# generated by the project itself during the final runtime.
2927
rustsbi/
28+
myramdisk/
3029

3130
/.axconfig.*
3231

32+
*.bin
33+
*.img
3334
*.cpio

Cargo.lock

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

Cargo.toml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,19 @@ ramdisk_cpio = []
2222
axhal = { git = "https://github.com/arceos-org/arceos.git" }
2323
axlog = { git = "https://github.com/arceos-org/arceos.git" }
2424
axconfig = { git = "https://github.com/arceos-org/arceos.git" }
25+
axsync = { git = "https://github.com/arceos-org/arceos.git" }
2526
axalloc = { git = "https://github.com/arceos-org/arceos.git", optional = true }
2627
axmm = { git = "https://github.com/arceos-org/arceos.git", optional = true }
27-
axdriver = { git = "https://github.com/arceos-org/arceos.git", optional = true }
28+
axdriver = { git = "https://github.com/arceos-org/arceos.git", optional = true, features = ["virtio-blk"] }
2829
axfs = { git = "https://github.com/arceos-org/arceos.git", optional = true }
2930
axnet = { git = "https://github.com/arceos-org/arceos.git", optional = true }
3031
axdisplay = { git = "https://github.com/arceos-org/arceos.git", optional = true }
32+
axio = { version = "0.1", features = ["alloc"] }
3133

3234
crate_interface = "0.1"
3335
ctor_bare = "0.2"
34-
cfg-if = "1.0"
35-
36+
cfg-if = "1.0.1"
37+
object = { version = "0.37.1", default-features = false, features = [ "read" ] }
3638
chrono = { version = "0.4.38", default-features = false }
37-
axio = { version = "0.1", features = ["alloc"] }
39+
uefi-raw = "0.11.0"
40+
lazyinit = "0.2.2"

scripts/test/build_edk2.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/bash
2+
set -e
3+
4+
PROJECT_ROOT=$(pwd)
5+
WORKSPACE_DIR="${PROJECT_ROOT}/edk2"
6+
EDK_DIR="$WORKSPACE_DIR/edk2"
7+
8+
mkdir -p "$WORKSPACE_DIR"
9+
10+
echo "[1/3] 准备 EDK2 构建环境..."
11+
cd "$WORKSPACE_DIR"
12+
13+
if [ ! -d "ToolChain/RISCV" ]; then
14+
echo "下载 RISCV 工具链..."
15+
mkdir -p ToolChain/RISCV
16+
wget -q https://github.com/riscv-collab/riscv-gnu-toolchain/releases/download/2025.07.16/riscv64-elf-ubuntu-24.04-gcc-nightly-2025.07.16-nightly.tar.xz
17+
tar -xf riscv64-elf-ubuntu-24.04-gcc-nightly-2025.07.16-nightly.tar.xz -C "${WORKSPACE_DIR}/ToolChain/RISCV"
18+
else
19+
echo "RISCV 工具链已存在,跳过下载。"
20+
fi
21+
22+
echo "[2/3] 克隆 EDK2 仓库..."
23+
if [ ! -d "$EDK_DIR" ]; then
24+
git clone --recurse-submodule https://github.com/tianocore/edk2.git "$EDK_DIR"
25+
else
26+
echo "EDK2 仓库已存在,跳过克隆。"
27+
fi
28+
29+
export PATH="$WORKSPACE_DIR/ToolChain/RISCV/riscv/bin:$PATH"
30+
31+
echo "[3/3] 构建 EDK2..."
32+
cd "$WORKSPACE_DIR"
33+
34+
export WORKSPACE=`pwd`
35+
export GCC5_RISCV64_PREFIX=riscv64-unknown-elf-
36+
export PACKAGES_PATH=$WORKSPACE/edk2
37+
export EDK_TOOLS_PATH=$WORKSPACE/edk2/BaseTools
38+
39+
. "$EDK_DIR/edksetup.sh" --reconfig
40+
make -C edk2/BaseTools
41+
. "$EDK_DIR/edksetup.sh" BaseTools
42+
43+
# mkdir -p "$EDK_DIR/Hello"
44+
# cp -r "$PROJECT_ROOT/tests/edk2-Hello" "$EDK_DIR"
45+
# mv "$EDK_DIR/edk2-Hello"/* "$EDK_DIR/Hello/"
46+
# cp -r "$EDK_DIR/MdeModulePkg/MdeModulePkg.dsc" "$EDK_DIR/Hello/Hello.dsc"
47+
# printf "\n[Components]\n Hello/Hello.inf\n" >> "$EDK_DIR/Hello/Hello.dsc"
48+
# build -a RISCV64 -t GCC5 -p "$EDK_DIR/Hello/Hello.dsc"
49+
cp -r "$PROJECT_ROOT/tests/edk2-HelloRiscv" "$EDK_DIR"
50+
mv "$EDK_DIR/edk2-HelloRiscv" "$EDK_DIR/HelloRiscv/"
51+
build -a RISCV64 -t GCC5 -p "$EDK_DIR/HelloRiscv/HelloRiscv.dsc"
52+
53+
echo "EDK2 构建完成。生成的镜像位于:$WORKSPACE_DIR/Build/DEBUG_GCC5/RISCV64/HelloRiscv.efi"

scripts/test/check_hello_test.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
set -e
3+
4+
LOG_FILE="qemu.log"
5+
TARGET_STRING="EFI Output: Hello, World!"
6+
7+
if [ ! -f "$LOG_FILE" ]; then
8+
echo "$LOG_FILE 不存在"
9+
exit 1
10+
fi
11+
12+
if grep -qF "$TARGET_STRING" "$LOG_FILE"; then
13+
echo "✅ 找到匹配日志行"
14+
exit 0
15+
else
16+
echo "❌ 未找到匹配日志行"
17+
exit 2
18+
fi

scripts/test/disk.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ mkfs.vfat -F 32 disk.img
1616

1717
mkdir temp
1818
sudo mount -o loop disk.img temp
19-
mkdir -p temp/test
20-
touch temp/test/arceboot.txt
21-
echo "This is a test file for Arceboot." > temp/test/arceboot.txt
19+
sudo mkdir -p temp/test
20+
sudo touch temp/test/arceboot.txt
21+
echo "This is a test file for Arceboot." | sudo tee temp/test/arceboot.txt > /dev/null
2222
sudo umount temp
2323
rm -rf temp
2424

scripts/test/make_esp.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
set -e
3+
4+
PROJECT_ROOT=$(pwd)
5+
IMG_NAME="disk.img"
6+
MOUNT_DIR="mnt_fat32"
7+
ESP_DIR="$MOUNT_DIR/EFI/BOOT"
8+
9+
if [ ! -d "$MOUNT_DIR" ]; then
10+
mkdir "$MOUNT_DIR"
11+
fi
12+
13+
echo "[1/3] 挂载 FAT32 镜像..."
14+
sudo mount -o loop "$IMG_NAME" "$MOUNT_DIR"
15+
16+
echo "[2/3] 创建 ESP 目录结构..."
17+
sudo mkdir -p "$ESP_DIR"
18+
19+
echo "[3/3] 复制 efi 文件到 ESP..."
20+
sudo cp "$PROJECT_ROOT/edk2/Build/DEBUG_GCC5/RISCV64/HelloRiscv.efi" "$ESP_DIR/BOOTRISCV64.EFI"
21+
22+
sudo find "$ESP_DIR" -type d | while read -r dir; do
23+
echo "$dir"
24+
done
25+
26+
sudo umount "$MOUNT_DIR"

src/log.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ impl axlog::LogIf for LogIfImpl {
1717
fn current_task_id() -> Option<u64> {
1818
None
1919
}
20-
}
20+
}

src/main.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#![cfg_attr(not(test), no_std)]
22
#![no_main]
3+
#![allow(dead_code)]
34

45
#[macro_use]
56
extern crate axlog;
67

7-
mod panic;
88
mod log;
99
mod medium;
10+
mod panic;
11+
mod runtime;
1012

1113
#[cfg_attr(not(test), unsafe(no_mangle))]
1214
pub extern "C" fn rust_main(_cpu_id: usize, _dtb: usize) -> ! {
@@ -55,6 +57,8 @@ pub extern "C" fn rust_main(_cpu_id: usize, _dtb: usize) -> ! {
5557
info!("current root dir: {}", crate::medium::current_dir().unwrap());
5658
info!("read test file context: {}", crate::medium::read_to_string("/test/arceboot.txt").unwrap());
5759

60+
crate::runtime::efi_runtime_init();
61+
5862
info!("will shut down.");
5963

6064
axhal::misc::terminate();
@@ -86,4 +90,4 @@ fn init_allocator() {
8690
.expect("add heap memory region failed");
8791
}
8892
}
89-
}
93+
}

0 commit comments

Comments
 (0)