8000 wip · rust-osdev/bootloader@0d90f03 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0d90f03

Browse files
committed
wip
1 parent 967a09b commit 0d90f03

File tree

17 files changed

+309
-600
lines changed

17 files changed

+309
-600
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use std::{
2+
process::Command,
3+
path::Path,
4+
};
5+
6+
fn main() {
7+
let mut cmd = Command::new("cargo");
8+
cmd.arg("xbuild").arg("--release");
9+
cmd.arg("--manifest-path").arg("protected_mode/Cargo.toml");
10+
cmd.arg("--target").arg("protected_mode/i686-bootloader.json");
11+
cmd.env("RUSTFLAGS", "");
12+
cmd.status().unwrap();
13+
14+
let out_dir = Path::new("protected_mode/target/i686-bootloader/release").canonicalize().unwrap();
15+
16+
println!("cargo:rustc-link-search=native={}", out_dir.display());
17+
println!("cargo:rustc-link-lib=static=protected_mode");
18+
19+
println!("cargo:rerun-if-changed=protected_mode");
20+
}

linker.ld

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ENTRY(_start)
1+
ENTRY(entry_point)
22

33
SECTIONS {
44
. = 0x500;
@@ -31,14 +31,14 @@ SECTIONS {
3131
*(.boot)
3232

3333
/* second stage */
34-
_second_stage_start_addr = .;
34+
_bootloader_rest_start_addr = .;
3535
*(.second_stage)
3636
*(.context_switch)
3737
*(.text .text.*)
3838
*(.rodata .rodata.*)
3939
*(.data .data.*)
4040
. = ALIGN(512);
41-
_second_stage_end_addr = .;
41+
_bootloader_rest_end_addr = .;
4242
}
4343

4444
_kernel_info_block_start = .;

protected_mode/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target/
2+
**/*.rs.bk

protected_mode/Cargo.lock

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

protected_mode/Cargo.toml

Line A3E2 s changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "protected_mode"
3+
version = "0.1.0"
4+
authors = ["Philipp Oppermann <dev@phil-opp.com>"]
5+
edition = "2018"
6+
7+
[lib]
8+
crate-type = ["staticlib"]
9+
10+
[dependencies]
11+
x86_64 = "0.3"

protected_mode/i686-bootloader.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"arch": "x86",
3+
"data-layout": "e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128",
4+
"llvm-target": "i686-unknown-linux-gnu",
5+
"linker-flavor": "ld.lld",
6+
"linker": "rust-lld",
7+
"target-endian": "little",
8+
"target-pointer-width": "32",
9+
"target-c-int-width": "32",
10+
"os": "none",
11+
"disable-redzone": true,
12+
"panic": "abort",
13+
"executables": true,
14+
"relocation_model": "static"
15+
}

src/memory_map.s renamed to protected_mode/src/e820.s

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.section .second_stage, "awx"
2+
.global do_e820
23
.intel_syntax noprefix
34
.code16
45

protected_mode/src/lib.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#![feature(global_asm)]
2+
#![feature(lang_items)]
3+
4+
#![no_std]
5+
6+
use core::panic::PanicInfo;
7+
8+
global_asm!(include_str!("e820.s"));
9+
10+
pub mod stages;
11+
12+
extern "C" {
13+
pub fn first_stage();
14+
}
15+
16+
#[no_mangle]
17+
pub extern "C" fn hello_world() {
18+
unsafe { first_stage() }
19+
}
20+
21+
#[panic_handler]
22+
#[no_mangle]
23+
pub extern "C" fn panic(_info: &PanicInfo) -> ! {
24+
loop {}
25+
}
26+
27+
#[lang = "eh_personality"]
28+
#[no_mangle]
29+
pub extern "C" fn eh_personality() {
30+
loop {}
31+
}
File renamed without changes.

protected_mode/src/stages/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
global_asm!(include_str!("stage_1.s"));
2+
global_asm!(include_str!("stage_2.s"));
3+
4+
pub mod stage_3;

src/boot.s renamed to protected_mode/src/stages/stage_1.s

Lines changed: 15 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
.section .boot, "awx"
2-
.global _start
2+
.global first_stage
33
.intel_syntax noprefix
44
.code16
55

6-
_start:
6+
# This stage initializes the stack, enables the A20 line, loads the rest of
7+
# the bootloader from disk, and jumps to it.
8+
9+
first_stage:
710
# zero segment registers
811
xor ax, ax
912
mov ds, ax
@@ -16,7 +19,6 @@ _start:
1619
# instructions like lodsb)
1720
cld
1821

19-
2022
# initialize stack
2123
mov sp, 0x7c00
2224

@@ -29,49 +31,15 @@ enable_a20:
2931
or al, 2
3032
out 0x92, al
3133

32-
enter_protected_mode:
33-
# clear interrupts
34-
cli
35-
push ds
36-
push es
37-
38-
lgdt [gdtinfo]
39-
40-
mov eax, cr0
41-
or al, 1 # set protected mode bit
42-
mov cr0, eax
43-
44-
jmp protected_mode # tell 386/486 to not crash
45-
46-
protected_mode:
47-
mov bx, 0x8
48-
mov ds, bx # set data segment
49-
mov es, bx # set extra segment
50-
51-
and al, 0xfe # clear protected mode bit
52-
mov cr0, eax
53-
54-
unreal_mode:
55-
pop es # get back old extra segment
56-
pop ds # get back old data segment
57-
sti
58-
59-
# back to real mode, but internal data segment register is still loaded
60-
# with gdt segment -> we can access the full 4GiB of memory
61-
62-
mov bx, 0x0f01 # attrib/char of smiley
63-
mov eax, 0xb8f00 # note 32 bit offset
64-
mov word ptr ds:[eax], bx
65-
6634
check_int13h_extensions:
6735
mov ah, 0x41
6836
mov bx, 0x55aa
6937
# dl contains drive number
7038
int 0x13
7139
jc no_int13h_extensions
7240

73-
load_second_stage_from_disk:
74-
lea eax, _second_stage_start_addr
41+
load_bootloader_rest_from_disk:
42+
lea eax, _bootloader_rest_start_addr
7543

7644
# start of memory buffer
7745
mov [dap_buffer_addr], ax
@@ -91,14 +59,12 @@ load_second_stage_from_disk:
9159
lea si, dap
9260
mov ah, 0x42
9361
int 0x13
94-
jc second_stage_load_failed
62+
jc bootloader_rest_load_failed
9563

9664
jump_to_second_stage:
97-
lea eax, second_stage
98-
jmp eax
99-
100-
spin:
101-
jmp spin
65+
jmp second_stage
66+
spin_1st_stage:
67+
jmp spin_1st_stage
10268

10369
# print a string and a newline
10470
# IN
@@ -141,71 +107,22 @@ print_char:
141107
int 0x10
142108
ret
143109

144-
# print a number in hex
145-
# IN
146-
# bx: the number
147-
# CLOBBER
148-
# al, cx
149-
print_hex:
150-
mov cx, 4
151-
.lp:
152-
mov al, bh
153-
shr al, 4
154-
155-
cmp al, 0xA
156-
jb .below_0xA
157-
158-
add al, 'A' - 0xA - '0'
159-
.below_0xA:
160-
add al, '0'
161-
162-
call print_char
163-
164-
shl bx, 4
165-
loop .lp
166-
167-
ret
168-
169110
error:
170111
call println
112+
error_spin:
171113
jmp spin
172114

173115
no_int13h_extensions:
174116
lea si, no_int13h_extensions_str
175117
jmp error
176118

177-
second_stage_load_failed:
178-
lea si, second_stage_load_failed_str
179-
jmp error
180-
181-
kernel_load_failed:
182-
lea si, kernel_load_failed_str
119+
bootloader_rest_load_failed:
120+
lea si, bootloader_rest_load_failed_str
183121
jmp error
184122

185123
boot_start_str: .asciz "Booting (first stage)..."
186-
second_stage_start_str: .asciz "Booting (second stage)..."
187-
error_str: .asciz "Error: "
188-
no_cpuid_str: .asciz "No CPUID support"
189124
no_int13h_extensions_str: .asciz "No support for int13h extensions"
190-
second_stage_load_failed_str: .asciz "Failed to load second stage of bootloader"
191-
192-
gdtinfo:
193-
.word gdt_end - gdt - 1 # last byte in table
194-
.word gdt # start of table
195-
196-
gdt:
197-
# entry 0 is always unused
198-
.quad 0
199-
flatdesc:
200-
.byte 0xff
201-
.byte 0xff
202-
.byte 0
203-
.byte 0
204-
.byte 0
205-
.byte 0x92
206-
.byte 0xcf
207-
.byte 0
208-
gdt_end:
125+
bootloader_rest_load_failed_str: .asciz "Failed to load rest of bootloader"
209126

210127
dap: # disk access packet
211128
.byte 0x10 # size of dap

0 commit comments

Comments
 (0)
0