From 246f5cfccf2b252950980a92aa5884c8e22f0ca5 Mon Sep 17 00:00:00 2001 From: pantonshire Date: Sun, 11 Aug 2024 12:51:31 +0100 Subject: [PATCH] build flat x86 rust binary --- Cargo.toml | 6 ---- boot1/.cargo/config.toml | 7 +++++ Cargo.lock => boot1/Cargo.lock | 2 +- boot1/Cargo.toml | 14 +++++++++ boot1/build.rs | 4 +++ boot1/link.ld | 9 ++++++ boot1/src/main.rs | 57 ++++++++++++++++++++++++++++++++++ boot1/target_protected.json | 16 ++++++++++ 8 files changed, 108 insertions(+), 7 deletions(-) delete mode 100644 Cargo.toml create mode 100644 boot1/.cargo/config.toml rename Cargo.lock => boot1/Cargo.lock (89%) create mode 100644 boot1/Cargo.toml create mode 100644 boot1/build.rs create mode 100644 boot1/link.ld create mode 100644 boot1/src/main.rs create mode 100644 boot1/target_protected.json diff --git a/Cargo.toml b/Cargo.toml deleted file mode 100644 index 94e426c..0000000 --- a/Cargo.toml +++ /dev/null @@ -1,6 +0,0 @@ -[workspace] -resolver = "2" -members = [ - "elf" -] - diff --git a/boot1/.cargo/config.toml b/boot1/.cargo/config.toml new file mode 100644 index 0000000..8bd474f --- /dev/null +++ b/boot1/.cargo/config.toml @@ -0,0 +1,7 @@ +[build] +target = "target_protected.json" + +[unstable] +build-std = [ "core", "compiler_builtins" ] +build-std-features = [ "compiler-builtins-mem" ] + diff --git a/Cargo.lock b/boot1/Cargo.lock similarity index 89% rename from Cargo.lock rename to boot1/Cargo.lock index b245edb..91f0598 100644 --- a/Cargo.lock +++ b/boot1/Cargo.lock @@ -3,5 +3,5 @@ version = 3 [[package]] -name = "elf" +name = "boot1" version = "0.1.0" diff --git a/boot1/Cargo.toml b/boot1/Cargo.toml new file mode 100644 index 0000000..42337ea --- /dev/null +++ b/boot1/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "boot1" +version = "0.1.0" +edition = "2021" + +[profile.dev] +opt-level = "s" +debug = 0 + +[profile.release] +opt-level = "s" +debug = 0 + +[dependencies] diff --git a/boot1/build.rs b/boot1/build.rs new file mode 100644 index 0000000..3bd445e --- /dev/null +++ b/boot1/build.rs @@ -0,0 +1,4 @@ +fn main() { + println!("cargo::rustc-link-arg=-Tlink.ld"); +} + diff --git a/boot1/link.ld b/boot1/link.ld new file mode 100644 index 0000000..28c7f94 --- /dev/null +++ b/boot1/link.ld @@ -0,0 +1,9 @@ +OUTPUT_FORMAT("binary") +. = 0x9000; +SECTIONS { + .text : { *(.text); *(.text.*) } + .data : { *(.data); *(.data.*) } + .rodata : { *(.rodata); *(.rodata.*) } +} + + diff --git a/boot1/src/main.rs b/boot1/src/main.rs new file mode 100644 index 0000000..b8e0adc --- /dev/null +++ b/boot1/src/main.rs @@ -0,0 +1,57 @@ +#![no_std] +#![no_main] + +use core::{arch::asm, panic::PanicInfo}; + +const VGA_WIDTH: usize = 80; +const VGA_HEIGHT: usize = 25; +const VGA_SIZE: usize = VGA_WIDTH * VGA_HEIGHT; +const VGA_ADDR: usize = 0xb8000; + +const STR: &[u8] = b"the quick brown fox jumps over the lazy dog"; + +#[panic_handler] +fn panic(_info: &PanicInfo) -> ! { + hlt() +} + +// ```asm +// 00000000 E800000000 call 0x5 ; call / pop gadget to load eip into ecx +// 00000005 59 pop ecx +// 00000006 81C15F000000 add ecx,0x5f +// 0000000C 31C0 xor eax,eax +// 0000000E 8D89D4FFFFFF lea ecx,[ecx-0x2c] ; length of string is 0x2b +// 00000014 0FB611 movzx edx,byte [ecx] +// 00000017 81CA001F0000 or edx,0x1f00 +// 0000001D 6689940000800B00 mov [eax+eax+0xb8000],dx +// 00000025 40 inc eax +// 00000026 41 inc ecx +// 00000027 83F82B cmp eax,byte +0x2b +// 0000002A 75E8 jnz 0x14 +// 0000002C 66C70500800B0002 mov word [dword 0xb8000],0x1f02 +// -1F +// 00000035 F4 hlt +// 00000036 EBFD jmp short 0x35 +// ``` +#[no_mangle] +pub extern "C" fn _start() -> ! { + let vga_buf: &'static mut [u16; VGA_SIZE] = unsafe { &mut *(VGA_ADDR as *mut [u16; VGA_SIZE]) }; + + for (i, b) in STR.iter().copied().enumerate() { + vga_buf[i] = 0x1f00 | u16::from(b); + } + + vga_buf[0] = 0x1f02; //smiley + + hlt() +} + +#[inline] +fn hlt() -> ! { + loop { + unsafe { + asm!("hlt"); + } + } +} + diff --git a/boot1/target_protected.json b/boot1/target_protected.json new file mode 100644 index 0000000..68ba8fb --- /dev/null +++ b/boot1/target_protected.json @@ -0,0 +1,16 @@ +{ + "arch": "x86", + "os": "none", + "llvm-target": "i686-unknown-none", + "data-layout": "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-i128:128-f64:32:64-f80:32-n8:16:32-S128", + "target-endian": "little", + "target-pointer-width": "32", + "target-c-int-width": "32", + "executables": true, + "linker-flavor": "ld.lld", + "linker": "rust-lld", + "panic-strategy": "abort", + "disable-redzone": true, + "features": "-mmx,-sse,+soft-float" +} +