Skip to content

Commit 3504bca

Browse files
authored
Merge pull request #76 from ReturnInfinity/UEFI-only
Hybrid
2 parents 6e6da55 + 93d4d09 commit 3504bca

12 files changed

+601
-613
lines changed

build.sh

+2-5
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ nasm pure64.asm -o ../bin/pure64.sys -l ../bin/pure64-debug.txt
88

99
cd boot
1010

11-
nasm mbr.asm -o ../../bin/mbr.sys
12-
nasm pxestart.asm -o ../../bin/pxestart.sys
13-
nasm multiboot.asm -o ../../bin/multiboot.sys
14-
nasm multiboot2.asm -o ../../bin/multiboot2.sys
15-
nasm uefi.asm -o ../../bin/uefi.sys
11+
nasm bios.asm -o ../../bin/bios.sys -l ../../bin/bios-debug.txt
12+
nasm uefi.asm -o ../../bin/uefi.sys -l ../../bin/uefi-debug.txt
1613

1714
cd ../..

docs/README.md

+22-21
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,19 @@ OUTPUT_ARCH("i386:x86-64")
7373
7474
SECTIONS
7575
{
76-
. = 0x100000;
77-
.text : {
78-
*(.text)
79-
}
80-
.data : {
81-
*(.data)
82-
}
83-
.rodata : {
84-
*(.rodata)
85-
}
86-
.bss : {
87-
*(.bss)
88-
}
76+
. = 0x100000;
77+
.text : {
78+
*(.text)
79+
}
80+
.data : {
81+
*(.data)
82+
}
83+
.rodata : {
84+
*(.rodata)
85+
}
86+
.bss : {
87+
*(.bss)
88+
}
8989
}
9090
9191
```
@@ -108,7 +108,7 @@ extern int main(void);
108108
109109
void _start(void)
110110
{
111-
main();
111+
main();
112112
}
113113
```
114114
This file would **always** have to be linked in front of everything else. For the above example that would mean the linker command above would have to become:
@@ -231,13 +231,14 @@ MPS INTI flags:
231231
<tr><td>Trigger Mode</td><td>2</td><td>2</td><td>01 Edge-triggered, 11 Level-triggered</td></tr>
232232
</table>
233233

234-
A copy of the E820 System Memory Map is stored at memory address `0x0000000000006000`. Each E820 record is 32 bytes in length and the memory map is terminated by a blank record.
234+
A copy of the UEFI System Memory Map is stored at memory address `0x0000000000006000`. Each UEFI record is 48 bytes in length and the memory map is terminated by a blank record.
235235
<table border="1" cellpadding="2" cellspacing="0">
236236
<tr><th>Variable</th><th>Variable Size</th><th>Description</th></tr>
237-
<tr><td>Starting Address</td><td>64-bit</td><td>The starting address for this record</td></tr>
238-
<tr><td>Length</td><td>64-bit</td><td>The length of memory for this record</td></tr>
239-
<tr><td>Memory Type</td><td>32-bit</td><td>Type 1 is usable memory, Type 2 is not usable</td></tr>
240-
<tr><td>Extended Attributes</td><td>32-bit</td><td>ACPI 3.0 Extended Attributes bitfield</td></tr>
241-
<tr><td>Padding</td><td>64-bit</td><td>Padding for 32-byte alignment</td></tr>
237+
<tr><td>Type</td><td>64-bit</td><td>The type of the memory region</td></tr>
238+
<tr><td>Physical Start</td><td>64-bit</td><td>Physical Address - 4K aligned</td></tr>
239+
<tr><td>Virtual Start</td><td>64-bit</td><td>Virtual Address - 4K aligned</td></tr>
240+
<tr><td>NumberOfPages</td><td>64-bit</td><td>The number of 4K pages in this section</td></tr>
241+
<tr><td>Attribute</td><td>64-bit</td><td>See document linked below</td></tr>
242+
<tr><td>Padding</td><td>64-bit</td><td>Padding</td></tr>
242243
</table>
243-
For more information on the E820 Memory Map: <a href="http://wiki.osdev.org/Detecting_Memory_%28x86%29">OSDev wiki on E820</a>
244+
For more information on the UEFI Memory Map: <a href="https://uefi.org/specs/UEFI/2.9_A/07_Services_Boot_Services.html#efi-boot-services-getmemorymap">UEFI Specs</a>

src/boot/pxestart.asm renamed to src/boot/bios-pxe.asm

+10-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
; kernel64.sys 16384 bytes (or so)
1818
; =============================================================================
1919

20+
; Set the desired screen resolution values below
21+
Horizontal_Resolution equ 800
22+
Vertical_Resolution equ 600
2023

2124
BITS 16
2225
org 0x7C00
@@ -31,10 +34,10 @@ start:
3134
mov sp, 0x7C00
3235
sti ; Enable interrupts
3336

34-
mov ah, 0
35-
mov al, 11100011b ; 9600bps, no parity, 1 stop bit, 8 data bits
36-
mov dx, 0 ; Serial port 0
37-
int 0x14 ; Configure serial port
37+
; mov ah, 0
38+
; mov al, 11100011b ; 9600bps, no parity, 1 stop bit, 8 data bits
39+
; mov dx, 0 ; Serial port 0
40+
; int 0x14 ; Configure serial port
3841

3942
; Get the BIOS E820 Memory Map
4043
; use the INT 0x15, eax= 0xE820 BIOS function to get a memory map
@@ -133,6 +136,8 @@ check_A20:
133136
mov si, msg_OK
134137
call print_string_16
135138

139+
mov bl, 'B' ; 'B' as we booted via BIOS
140+
136141
; At this point we are done with real mode and BIOS interrupts. Jump to 32-bit mode.
137142
cli ; No more interrupts
138143
lgdt [cs:GDTR32] ; Load GDT register
@@ -230,4 +235,4 @@ VBEModeInfoBlock.PhysBasePtr equ VBEModeInfoBlock + 40 ; DD - physical address
230235
VBEModeInfoBlock.Reserved1 equ VBEModeInfoBlock + 44 ; DD - Reserved - always set to 0
231236
VBEModeInfoBlock.Reserved2 equ VBEModeInfoBlock + 48 ; DD - Reserved - always set to 0
232237

233-
; EOF
238+
; EOF

src/boot/mbr.asm renamed to src/boot/bios.asm

+84-49
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,53 @@
1313
; Default location of the second stage boot loader. This loads
1414
; 32 KiB from sector 16 into memory at 0x8000
1515
%define DAP_SECTORS 64
16-
%define DAP_STARTSECTOR 16
16+
%define DAP_STARTSECTOR 262160
1717
%define DAP_ADDRESS 0x8000
1818
%define DAP_SEGMENT 0x0000
1919

20+
; Set the desired screen resolution values below
21+
Horizontal_Resolution equ 800
22+
Vertical_Resolution equ 600
2023

2124
BITS 16
2225
org 0x7C00
2326

2427
entry:
28+
jmp bootcode
29+
nop
30+
31+
; BPB (BIOS Parameter Block)
32+
dq 0 ; OEM identifier
33+
dw 0 ; Bytes per sector
34+
db 0 ; Sectors per cluster
35+
dw 0 ; Reserved sectors
36+
db 0 ; Number of FATs
37+
dw 0 ; Number of root directory entries
38+
dw 0 ; The total sectors in the logical volume
39+
db 0 ; Media descriptor type
40+
dw 0 ; Number of sectors per FAT
41+
dw 0 ; Number of sectors per track
42+
dw 0 ; Number of heads or sides on the storage media
43+
dd 0 ; Number of hidden sectors
44+
dd 0 ; Large sector count
45+
46+
; EBPB (Extended Boot Record)
47+
dd 0 ; Sectors per FAT
48+
dw 0 ; Flags
49+
dw 0 ; FAT version number
50+
dd 0 ; The cluster number of the root directory
51+
dw 0 ; The sector number of the FSInfo structure
52+
dw 0 ; The sector number of the backup boot sector
53+
dq 0 ; Reserved
54+
dd 0 ; Reserved
55+
db 0 ; Drive number
56+
db 0 ; Flags in Windows NT
57+
db 0 ; Signature
58+
dd 0 ; Volume ID 'Serial' number
59+
times 11 db 0 ; Volume label string
60+
dq 0 ; System identifier string. Always "FAT32 "
61+
62+
bootcode:
2563
cli ; Disable interrupts
2664
cld ; Clear direction flag
2765
xor eax, eax
@@ -33,11 +71,6 @@ entry:
3371

3472
mov [DriveNumber], dl ; BIOS passes drive number in DL
3573

36-
mov ah, 0
37-
mov al, 11100011b ; 9600bps, no parity, 1 stop bit, 8 data bits
38-
mov dx, 0 ; Serial port 0
39-
int 0x14 ; Configure serial port
40-
4174
; Get the BIOS E820 Memory Map
4275
; use the INT 0x15, eax= 0xE820 BIOS function to get a memory map
4376
; inputs: es:di -> destination buffer for 24 byte entries
@@ -104,8 +137,8 @@ check_A20:
104137
mov al, 0xDF
105138
out 0x60, al
106139

107-
mov si, msg_Load
108-
call print_string_16
140+
; mov si, msg_Load
141+
; call print_string_16
109142

110143
mov cx, 0x4000 - 1 ; Start looking from here
111144
VBESearch:
@@ -121,9 +154,9 @@ VBESearch:
121154
jne VBESearch ; Try next mode
122155
cmp byte [VBEModeInfoBlock.BitsPerPixel], 32 ; Desired bit depth
123156
jne VBESearch ; If not equal, try next mode
124-
cmp word [VBEModeInfoBlock.XResolution], 800 ; Desired XRes here
157+
cmp word [VBEModeInfoBlock.XResolution], Horizontal_Resolution ; Desired XRes here
125158
jne VBESearch
126-
cmp word [VBEModeInfoBlock.YResolution], 600 ; Desired YRes here
159+
cmp word [VBEModeInfoBlock.YResolution], Vertical_Resolution ; Desired YRes here
127160
jne VBESearch
128161

129162
or bx, 0x4000 ; Use linear/flat frame buffer model (set bit 14)
@@ -140,12 +173,15 @@ VBESearch:
140173
jc read_fail
141174

142175
; Verify that the 2nd stage boot loader was read.
143-
mov ax, [0x8006]
144-
cmp ax, 0x3436 ; Match against the Pure64 binary
145-
jne sig_fail
176+
; mov ax, [0x8006]
177+
; cmp ax, 0x3436 ; Match against the Pure64 binary
178+
; jne sig_fail
179+
180+
mov bl, 'B' ; 'B' as we booted via BIOS
181+
; mov [0x5FFF], al ; Store the boot marker
146182

147-
mov si, msg_OK
148-
call print_string_16
183+
; mov si, msg_OK
184+
; call print_string_16
149185

150186
; At this point we are done with real mode and BIOS interrupts. Jump to 32-bit mode.
151187
cli ; No more interrupts
@@ -156,12 +192,12 @@ VBESearch:
156192
jmp 8:0x8000 ; Jump to 32-bit protected mode
157193

158194
read_fail:
159-
mov si, msg_ReadFail
160-
call print_string_16
161-
jmp halt
195+
; mov si, msg_ReadFail
196+
; call print_string_16
197+
; jmp halt
162198
sig_fail:
163-
mov si, msg_SigFail
164-
call print_string_16
199+
; mov si, msg_SigFail
200+
; call print_string_16
165201
halt:
166202
hlt
167203
jmp halt
@@ -171,19 +207,19 @@ halt:
171207
;------------------------------------------------------------------------------
172208
; 16-bit function to output a string to the serial port
173209
; IN: SI - Address of start of string
174-
print_string_16: ; Output string in SI to screen
175-
pusha
176-
mov dx, 0 ; Port 0
177-
.repeat:
178-
mov ah, 0x01 ; Serial - Write character to port
179-
lodsb ; Get char from string
180-
cmp al, 0
181-
je .done ; If char is zero, end of string
182-
int 0x14 ; Output the character
183-
jmp short .repeat
184-
.done:
185-
popa
186-
ret
210+
;print_string_16: ; Output string in SI to screen
211+
; pusha
212+
; mov dx, 0 ; Port 0
213+
;.repeat:
214+
; mov ah, 0x01 ; Serial - Write character to port
215+
; lodsb ; Get char from string
216+
; cmp al, 0
217+
; je .done ; If char is zero, end of string
218+
; int 0x14 ; Output the character
219+
; jmp short .repeat
220+
;.done:
221+
; popa
222+
; ret
187223
;------------------------------------------------------------------------------
188224

189225
align 16
@@ -198,31 +234,30 @@ dw 0xFFFF, 0x0000, 0x9A00, 0x00CF ; 32-bit code descriptor
198234
dw 0xFFFF, 0x0000, 0x9200, 0x00CF ; 32-bit data descriptor
199235
gdt32_end:
200236

201-
msg_Load db 10, "MBR ", 0
202-
msg_OK db "OK", 0
203-
msg_SigFail db "- Bad Sig!", 0
204-
msg_ReadFail db "Failed to read drive!", 0
237+
align 4
205238

206-
times 446-$+$$ db 0
239+
DAP:
240+
db 0x10
241+
db 0x00
242+
dw DAP_SECTORS
243+
dw DAP_ADDRESS
244+
dw DAP_SEGMENT
245+
dq DAP_STARTSECTOR
207246

208-
; False partition table entry required by some BIOS vendors.
209-
db 0x80, 0x00, 0x01, 0x00, 0xEB, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF
210247
DriveNumber db 0x00
211248

212-
times 476-$+$$ db 0
249+
;msg_Load db 10, "MBR ", 0
250+
;msg_OK db "OK", 0
251+
;msg_SigFail db "- Bad Sig!", 0
252+
;msg_ReadFail db "Failed to read!", 0
213253

214-
align 4
254+
times 446-$+$$ db 0
215255

216-
DAP:
217-
db 0x10
218-
db 0x00
219-
dw DAP_SECTORS
220-
dw DAP_ADDRESS
221-
dw DAP_SEGMENT
222-
dq DAP_STARTSECTOR
256+
; Partition entries (4x 16-bytes)
223257

224258
times 510-$+$$ db 0
225259

260+
; Boot signature
226261
sign dw 0xAA55
227262

228263
VBEModeInfoBlock: equ 0x5F00

0 commit comments

Comments
 (0)