build flat x86 rust binary

main
pantonshire 1 year ago
parent 88aa06627c
commit 246f5cfccf

@ -1,6 +0,0 @@
[workspace]
resolver = "2"
members = [
"elf"
]

@ -0,0 +1,7 @@
[build]
target = "target_protected.json"
[unstable]
build-std = [ "core", "compiler_builtins" ]
build-std-features = [ "compiler-builtins-mem" ]

@ -3,5 +3,5 @@
version = 3
[[package]]
name = "elf"
name = "boot1"
version = "0.1.0"

@ -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]

@ -0,0 +1,4 @@
fn main() {
println!("cargo::rustc-link-arg=-Tlink.ld");
}

@ -0,0 +1,9 @@
OUTPUT_FORMAT("binary")
. = 0x9000;
SECTIONS {
.text : { *(.text); *(.text.*) }
.data : { *(.data); *(.data.*) }
.rodata : { *(.rodata); *(.rodata.*) }
}

@ -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");
}
}
}

@ -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"
}
Loading…
Cancel
Save