From e4470bc04c0db9da783c46342efe62d6b6eded1c Mon Sep 17 00:00:00 2001 From: Sainnhe Park Date: Wed, 1 Dec 2021 16:47:28 +0800 Subject: [PATCH] Finish VGA Buffer --- .cargo/config | 2 -- .cargo/config.toml | 6 ++++++ Cargo.lock | 9 +++++++++ Cargo.toml | 3 +++ rust-toolchain.toml | 4 +--- src/main.rs | 14 ++++++++++++++ x86_64-anos.json | 15 +++++++++++++++ 7 files changed, 48 insertions(+), 5 deletions(-) delete mode 100644 .cargo/config create mode 100644 .cargo/config.toml create mode 100644 x86_64-anos.json diff --git a/.cargo/config b/.cargo/config deleted file mode 100644 index 5d10902..0000000 --- a/.cargo/config +++ /dev/null @@ -1,2 +0,0 @@ -[build] -target = "thumbv7em-none-eabihf" diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..098ed3e --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,6 @@ +[build] +target = "x86_64-anos.json" + +[unstable] +build-std-features = ["compiler-builtins-mem"] +build-std = ["core", "compiler_builtins"] diff --git a/Cargo.lock b/Cargo.lock index 9678ac5..addcc1d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,3 +5,12 @@ version = 3 [[package]] name = "anos" version = "0.1.0" +dependencies = [ + "bootloader", +] + +[[package]] +name = "bootloader" +version = "0.9.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7c452074efc3c0bfb241fb7bc87df04741c7c85e926f6a07c05f8fbd6008240" diff --git a/Cargo.toml b/Cargo.toml index 793a53b..b9b47b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,6 @@ panic = "abort" # 禁用 panic 时的栈展开 [profile.release] panic = "abort" # 禁用 panic 时的栈展开 + +[dependencies] +bootloader = "0.9.8" diff --git a/rust-toolchain.toml b/rust-toolchain.toml index 39d1ac5..92e4674 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,6 +1,4 @@ [toolchain] channel = "nightly" -components = [ "rust-analyzer-preview" ] -targets = [ "thumbv7em-none-eabihf" ] +components = [ "llvm-tools-preview" ] profile = "minimal" - diff --git a/src/main.rs b/src/main.rs index 3457e83..7529037 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,11 +3,25 @@ use core::panic::PanicInfo; +// 定义一个 byte string 类型的静态变量 +static HELLO: &[u8] = b"Hello World!"; + #[no_mangle] // 不重整函数名,否则编译器可能会生成名为 _ZN3blog_os4_start7hb173fedf945531caE 的函数 // extern "C" 告诉编译器这个函数应当使用 C 语言的调用约定 // 函数名为 _start 是因为大多数系统默认用这个名字作为入口点名称 // -> ! 代表这是一个发散函数,不允许有任何返回值,因为它不会被任何函数调用,而是将直接被 bootloader 调用 pub extern "C" fn _start() -> ! { + // VGA Buffer 从 0xb8000 开始 + let vga_buffer = 0xb8000 as *mut u8; + + // 迭代 HELLO, 通过裸指针 vga_buffer 来定位待写入的缓冲区地址 + for (i, &byte) in HELLO.iter().enumerate() { + unsafe { + *vga_buffer.offset(i as isize * 2) = byte; // ASCII 码字节 + *vga_buffer.offset(i as isize * 2 + 1) = 0xb; // 颜色字节, 0xb 代表青色 + } + } + loop {} } diff --git a/x86_64-anos.json b/x86_64-anos.json new file mode 100644 index 0000000..23da62b --- /dev/null +++ b/x86_64-anos.json @@ -0,0 +1,15 @@ +{ + "llvm-target": "x86_64-unknown-none", + "data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128", + "arch": "x86_64", + "target-endian": "little", + "target-pointer-width": "64", + "target-c-int-width": "32", + "os": "none", + "executables": true, + "linker-flavor": "ld.lld", + "linker": "rust-lld", + "panic-strategy": "abort", + "disable-redzone": true, + "features": "-mmx,-sse,+soft-float" +}