-
Notifications
You must be signed in to change notification settings - Fork 0
/
boot16.S
170 lines (150 loc) · 3.04 KB
/
boot16.S
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright (c) 2023 Wang Baisheng <[email protected]>, Wang Shenghan. All Rights Reserved.
#define __ASSEMBLY__
#include <asm/boot.h>
#include <asm/bootparam.h>
#define IO_PIC 0x20
#define IRQ_OFFSET 32
.text
.code16
// https://www.kernel.org/doc/html/latest/arch/x86/boot.html
.fill 0x1F1, 1, 0
// setup_sects:
.byte 0x20
// root_flags
.word 0
// syssize
.long 0
// ram_size
.word 0
// vid_mode
.word ASK_VGA
// root_dev
.word 0
// boot_flags
.word 0xAA55
_start:
jmp start
1:
// make sure jmp is 2 bytes
// .byte 0
.org 0x202
// header
.ascii "HdrS"
// version
.word 0x020f
// realmode_swtch
.long 0
// start_sys_seg
.word 0
// kernel_version
.word kernel_version - _start
// type_of_loader
.byte 0
// loadflags
.byte LOADED_HIGH
// setup_move_size
.word 0x8000
// code32_start
.long 0x100000
// ramdisk_image
.long 0
// ramdisk_size
.long 0
// bootsect_kludge
.long 0
// heap_end_ptr
.word 0
// ext_loader_ver
.byte 0
// ext_loader_type
.byte 0
// cmd_line_ptr
.long 0
// initrd_addr_max
.long 0x7ffffff
// kernel_alignment
.long 0x1000
// relocatable_kernel
.byte 0
// min_alignment
.byte 21
// xloadflags
.word XLF_KERNEL_64
// cmdline_size
.long 0
// hardware_subarch
.long 0
// hardware_subarch_data
.quad 0
// payload_offset
.long 0
// payload_length
.long 0
// setup_data
.quad 0
// pref_address
.quad 0
// init_size
.long 0
// handover_offset
.long 0
// kernel_info_offset
.long 0
kernel_version:
.asciz "I am not Linux"
start:
cli
# read e820
mov $0x0534D4150, %edx
mov $e820_entry, %di
xor %ebx, %ebx
e820_rd_entry:
mov $0xe820, %eax
mov $20, %ecx
int $0x15
add $20, %di
incb e820_nr_entry
cmp $0, %ebx
jne e820_rd_entry
# get display mode info
mov $vesa_mode_info, %di
mov $0x4f01, %ax
int $0x10
# Init 8259A
# ICW1
mov $0x13, %al
mov $(IO_PIC), %dx
out %al,%dx
# ICW2
mov $(IRQ_OFFSET), %al
mov $(IO_PIC+1), %dx
out %al, %dx
# ICW4
mov $0x1, %al
mov $(IO_PIC+1), %dx
out %al, %dx
lgdt gdtr
mov %cr0, %eax
or $0x1, %eax
mov %eax, %cr0
ljmpl $0x8, $0x100000
gdtr:
.word gdt_end - gdt
.word gdt, 1
gdt:
.quad 0x0000000000000000
.quad 0x00c09a00000007ff
.quad 0x00c09200000007ff
gdt_end:
# e820map
.org 0x3000
e820_nr_entry:
.long 0
e820_entry:
.fill 512, 1, 0
# vesa mode info
.org 0x4000
vesa_mode_info:
.fill 256, 1, 0
.align 512, 0
.org 0x4200