build flat x86 rust binary

This commit is contained in:
pantonshire committed 2024-08-11 14:18:27 +01:00
1 parent 88aa06627c
commit 246f5cfccf
8 files changed
+108 -7

No files matched your search

+57
View File
@@ -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");
}
}
}