Skip to content

Commit a253b3f

Browse files
committed
Start integrating first-stage executable into bootloader
1 parent 9b887fc commit a253b3f

File tree

15 files changed

+126
-16
lines changed

15 files changed

+126
-16
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ edition = "2021"
1010
[workspace]
1111
members = [
1212
"api",
13+
"bios/first_stage",
1314
"tests/runner",
1415
"tests/test_kernels/default_settings",
1516
"tests/test_kernels/map_phys_mem",
@@ -103,6 +104,15 @@ lto = false
103104
debug = true
104105
overflow-checks = true
105106

107+
[profile.first-stage]
108+
inherits = "release"
109+
opt-level = "s"
110+
lto = true
111+
codegen-units = 1
112+
debug = false
113+
overflow-checks = false
114+
115+
106116
[package.metadata.docs.rs]
107117
default-target = "x86_64-unknown-linux-gnu"
108118

File renamed without changes.
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
[package]
2-
name = "first_stage"
2+
name = "bootloader_first_stage"
33
version = "0.1.0"
44
authors = ["Philipp Oppermann <dev@phil-opp.com>"]
55
edition = "2021"
66

77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10-
11-
[profile.release]
12-
opt-level = "s"
13-
lto = true
14-
codegen-units = 1
15-
16-
# debug = true
File renamed without changes.

bios/first_stage/src/boot unreal.s

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
.section .boot, "awx"
2+
.global _start
3+
.code16
4+
5+
# This stage initializes the stack, enables the A20 line
6+
7+
_start:
8+
# zero segment registers
9+
xor ax, ax
10+
mov ds, ax
11+
mov es, ax
12+
mov ss, ax
13+
mov fs, ax
14+
mov gs, ax
15+
16+
# clear the direction flag (e.g. go forward in memory when using
17+
# instructions like lodsb)
18+
cld
19+
20+
# initialize stack
21+
mov sp, 0x7c00
22+
23+
enable_a20:
24+
# enable A20-Line via IO-Port 92, might not work on all motherboards
25+
in al, 0x92
26+
test al, 2
27+
jnz enable_a20_after
28+
or al, 2
29+
and al, 0xFE
30+
out 0x92, al
31+
enable_a20_after:
32+
33+
enter_protected_mode:
34+
# clear interrupts
35+
cli
36+
push ds
37+
push es
38+
39+
lgdt [gdt32info]
40+
41+
mov eax, cr0
42+
or al, 1 # set protected mode bit
43+
mov cr0, eax
44+
45+
jmp protected_mode # tell 386/486 to not crash
46+
47+
protected_mode:
48+
mov bx, 0x10
49+
mov ds, bx # set data segment
50+
mov es, bx # set extra segment
51+
52+
and al, 0xfe # clear protected mode bit
53+
mov cr0, eax
54+
55+
unreal_mode:
56+
pop es # get back old extra segment
57+
pop ds # get back old data segment
58+
sti
59+
60+
# back to real mode, but internal data segment register is still loaded
61+
# with gdt segment -> we can access the full 4GiB of memory
62+
63+
mov bx, 0x0f02 # attrib/char of smiley
64+
mov eax, 0xb8f00 # note 32 bit offset
65+
mov word ptr ds:[eax], bx
66+
67+
check_int13h_extensions:
68+
push 'y' # error code
69+
mov ah, 0x41
70+
mov bx, 0x55aa
71+
# dl contains drive number
72+
int 0x13
73+
jc fail
74+
pop ax # pop error code again
75+
76+
rust:
77+
# push arguments
78+
push dx # disk number
79+
call first_stage
80+
81+
spin:
82+
hlt
83+
jmp spin
84+
85+
gdt32info:
86+
.word gdt32_end - gdt32 - 1 # last byte in table
87+
.word gdt32 # start of table
88+
89+
gdt32:
90+
# entry 0 is always unused
91+
.quad 0
92+
codedesc:
93+
.byte 0xff
94+
.byte 0xff
95+
.byte 0
96+
.byte 0
97+
.byte 0
98+
.byte 0x9a
99+
.byte 0xcf
100+
.byte 0
101+
datadesc:
102+
.byte 0xff
103+
.byte 0xff
104+
.byte 0
105+
.byte 0
106+
.byte 0
107+
.byte 0x92
108+
.byte 0xcf
109+
.byte 0
110+
gdt32_end:
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

real_mode/first_stage/x86-16bit.json renamed to bios/first_stage/x86-16bit.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"relocation-model": "static",
2020
"pre-link-args": {
2121
"ld.lld": [
22-
"--script=16-bit-linker.ld"
22+
"--script=bios/first_stage/16-bit-linker.ld"
2323
]
2424
}
2525
}

real_mode/first_stage/Cargo.lock

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)