-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboot_sec.lst
More file actions
329 lines (329 loc) · 20.2 KB
/
Copy pathboot_sec.lst
File metadata and controls
329 lines (329 loc) · 20.2 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ; Simple Bootloader for x86 systems
3 ; - intial boot set up
4 ; - switch into 32 bit protected mode
5 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
6
7 [org 0x7c00]
8 KERNEL_OFFSET equ 0x1000 ; Memory offset for Kernel
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
10 ; Start up in 16 bit real mode
11 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
12
13 00000000 8816[6F01] mov [BOOT_DRIVE], dl ; Save boot drive
14
15 00000004 BD0090 mov bp, 0x9000 ; Set up stack pointer
16 00000007 89EC mov sp, bp
17
18 00000009 BE[4601] mov si, MSG_REAL_MODE ; Print REAl MODE message
19 0000000C E80800 call printString
20
21 0000000F E82101 call load_kernel
22
23 00000012 E8EF00 call switch_to_pm ; Initiate the switch to Protected mode
24
25 00000015 EBFE jmp $ ; Infinite loop - Should never get here
26
27 %include "print16.asm"
1 <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 <1> ; Prints null terminated string
3 <1> ; Put first letter into SI
4 <1> ; Set BX to number of bytes to use
5 <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
6 <1> printString:
7 00000017 AC <1> lodsb ; Load the byte at address in SI to AL and Inc SI
8 00000018 3C00 <1> cmp al,0 ; check for end of line
9 0000001A 7405 <1> je printStringEnd
10 0000001C E80300 <1> call printChar
11 0000001F EBF6 <1> jmp printString
12 <1> printStringEnd:
13 00000021 C3 <1> ret
14 <1>
15 <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
16 <1> ; Prints a single character to the BIOS
17 <1> ; Teletype function
18 <1> ; Put char in AL
19 <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
20 <1> printChar:
21 <1> ; Call BIOS Routing to print
22 00000022 50 <1> push ax
23 00000023 53 <1> push bx
24 00000024 B40E <1> mov ah, 0x0e ; BIOS Teletype function
25 00000026 B700 <1> mov bh,0 ; Page 0
26 00000028 B347 <1> mov bl, 0x47 ; Text Attribute (light grey on black)
27 0000002A CD10 <1> int 0x10 ; call BIOS intterupt 0x10
28 0000002C 5B <1> pop bx
29 0000002D 58 <1> pop ax
30 0000002E C3 <1> ret
31 <1>
32 <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
33 <1> ; Prints a byte or word in hex format
34 <1> ; Assumes value is in DX and can be 32 bit value
35 <1> ; Set BX to number of bytes to use
36 <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
37 <1> printHex:
38 0000002F 6651 <1> push ecx ; save cl
39 00000031 B004 <1> mov al,4 ; load number of bytes for full 32 bit number
40 00000033 28C8 <1> sub al,cl ; get number of byte to shift out for alignment
41 00000035 B108 <1> mov cl,8 ; set bit multiplier
42 00000037 F6E1 <1> mul cl ; multiply by byute count - we now have numebr of bits to shift right
43 00000039 88C1 <1> mov cl,al
44 0000003B 66D3CA <1> ror edx,cl
45 0000003E 6659 <1> pop ecx ; restore cl
46 00000040 D0E1 <1> shl cl,1 ; multiply by 2 for nibbles
47 <1> ; todo: could put check in for a value greater than 4
48 <1> hexLoop:
49 00000042 80F900 <1> cmp cl,0 ; if bx = 0 then exit
50 00000045 7416 <1> jz hexFin
51 00000047 66C1C204 <1> rol edx,4 ; Rotate the word to get first byte ready
52 0000004B 49 <1> dec cx
53 <1> ; Swap
54 0000004C 88D0 <1> mov al,dl ; copy byte into AL
55 0000004E 240F <1> and al,0x0f ; clear high nibble
56 <1>
57 <1> ; Handle al first
58 00000050 3C0A <1> cmp al,0x0a
59 00000052 7202 <1> jb nibLess
60 00000054 0407 <1> add al,0x07 ; add offset for Hex letters
61 <1> nibLess:
62 00000056 0430 <1> add al,0x30
63 00000058 E8C7FF <1> call printChar
64 0000005B EBE5 <1> jmp hexLoop
65 <1> hexFin:
66 0000005D C3 <1> ret
67 <1>
68 <1>
69 0000005E 0A0D00 <1> NEW_LINE: db 0xa, 0xd,0
28 %include "disk.asm"
1 <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 <1> ; Function to read from the disk
3 <1> ;load DHsectors to ES:BX from drive DL
4 <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5 <1>
6 <1> diskLoad:
7 00000061 52 <1> push dx
8 <1>
9 00000062 B402 <1> mov ah,0x02 ; BIOS read sector function
10 00000064 88F0 <1> mov al,dh ; Read DH sectors
11 00000066 B500 <1> mov ch,0x00 ; Select cylinder 0
12 00000068 B600 <1> mov dh,0x00
13 0000006A B102 <1> mov cl,0x02 ; Start reading from second sector - ie after boot sector
14 <1>
15 0000006C CD13 <1> int 0x13 ; BIOS interrupt
16 <1>
17 0000006E 7206 <1> jc disk_error_BIOS
18 <1>
19 00000070 5A <1> pop dx
20 00000071 38C6 <1> cmp dh,al ; if AL (sectors read) != DH (sectors ecpected)
21 00000073 7509 <1> jne disk_error_SECT
22 00000075 C3 <1> ret
23 <1>
24 <1> disk_error_BIOS:
25 00000076 BE[8600] <1> mov si, DISK_ERROR_BIOS
26 00000079 E89BFF <1> call printString
27 0000007C EBFE <1> jmp $
28 <1>
29 <1> disk_error_SECT:
30 0000007E BE[9900] <1> mov si, DISK_ERROR_SECT
31 00000081 E893FF <1> call printString
32 00000084 EBFE <1> jmp $
33 <1>
34 <1> ; Variables
35 00000086 4469736B2042494F53- <1> DISK_ERROR_BIOS: db "Disk BIOS error!", 0xa, 0xd, 0
35 0000008F 206572726F72210A0D- <1>
35 00000098 00 <1>
36 00000099 4469736B2073656374- <1> DISK_ERROR_SECT: db "Disk sector error!", 0xa, 0xd, 0
36 000000A2 6F72206572726F7221- <1>
36 000000AB 0A0D00 <1>
29 %include "gdt.asm"
1 <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 <1> ; GDT - Global Discriptor table
3 <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4 <1>
5 <1> gdt_start:
6 <1>
7 <1> gdt_null: ;the madatory null descriptor
8 000000AE 00000000 <1> dd 0x0
9 000000B2 00000000 <1> dd 0x0
10 <1>
11 <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
12 <1> ; CODE SEGMENT descriptor
13 <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
14 <1>
15 <1> gdt_code:
16 <1>
17 000000B6 FFFF <1> dw 0xffff ; Limite (bits 0-15)
18 000000B8 0000 <1> dw 0x0 ; Base (bits 0-15)
19 000000BA 00 <1> db 0x0 ; Base (bits 16-23)
20 000000BB 9A <1> db 10011010b ; 1st flags, type flags
21 000000BC CF <1> db 11001111b ; 2nd flags, limit (bits 16-19)
22 000000BD 00 <1> db 0x0 ; Base (bits 24-31)
23 <1>
24 <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 <1> ; DATA SEGMENT descriptor
26 <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 <1>
28 <1> gdt_data: ; The data segment desciptor
29 <1>
30 000000BE FFFF <1> dw 0xffff ; Limit (bits 0-15)
31 000000C0 0000 <1> dw 0x0 ; Base (bits 0-15)
32 000000C2 00 <1> db 0x0 ; Base (bits 16-23)
33 000000C3 92 <1> db 10010010b ; 1st flags, type flags
34 000000C4 CF <1> db 11001111b ; 2nd flags. Limit (bits 16-19)
35 000000C5 00 <1> db 0x0 ; Base (bits 24-31)
36 <1>
37 <1> gdt_end:
38 <1>
39 <1>
40 <1> ; GDT Descriptor
41 <1> gdt_descriptor:
42 000000C6 1700 <1> dw gdt_end - gdt_start -1
43 000000C8 [AE000000] <1> dd gdt_start
44 <1>
45 <1>
46 <1> CODE_SEG equ gdt_code - gdt_start
47 <1> DATA_SEG equ gdt_data - gdt_start
48 <1>
49 <1>
30 %include "print32.asm"
1 <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 <1> ; Print routines for 32 bit protected mode
3 <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4 <1>
5 <1> [bits 32]
6 <1>
7 <1> VIDEO_MEMORY equ 0xb8000
8 <1> VIDEO_MEMORY_SIZE equ 0xfa0
9 <1> WHITE_ON_BLACK equ 0x0f
10 <1> WHITE_ON_BLUE equ 0x1f
11 <1> BLUE_ON_GRAY equ 0x0A
12 <1>
13 <1>
14 <1>
15 <1> COLOR_BLACK equ 0x000000
16 <1> COLOR_BLUE equ 0x0000AA
17 <1> COLOR_GREEN equ 0x00AA00
18 <1> COLOR_CYAN equ 0x00AAAA
19 <1> COLOR_RED equ 0xAA0000
20 <1> COLOR_PURPLE equ 0xAA00AA
21 <1> COLOR_BROWN equ 0xAA5500
22 <1> COLOR_GRAY equ 0xAAAAAA
23 <1> COLOR_LT_BLUE equ 0x5555FF
24 <1>
25 <1> cls:
26 000000CC 60 <1> pusha
27 000000CD B89F0F0000 <1> mov eax, VIDEO_MEMORY_SIZE-1
28 000000D2 BA00800B00 <1> mov edx, VIDEO_MEMORY
29 000000D7 B020 <1> mov al,0x20 ; Space character
30 000000D9 B41F <1> mov ah,WHITE_ON_BLUE
31 <1> clsloop:
32 000000DB 668902 <1> mov [edx],ax
33 000000DE 4B <1> dec ebx
34 000000DF 83C202 <1> add edx, 2
35 000000E2 83FB00 <1> cmp ebx,0
36 000000E5 75F4 <1> jne clsloop
37 000000E7 61 <1> popa
38 000000E8 C3 <1> ret
39 <1>
40 <1> print_string_pm:
41 000000E9 60 <1> pusha
42 000000EA BA00800B00 <1> mov edx, VIDEO_MEMORY
43 <1>
44 <1> print_string_pm_loop:
45 000000EF 8A03 <1> mov al, [ebx]
46 000000F1 B41F <1> mov ah, WHITE_ON_BLUE
47 <1>
48 000000F3 3C00 <1> cmp al, 0
49 000000F5 740B <1> je print_string_pm_done
50 <1>
51 000000F7 668902 <1> mov [edx], ax
52 <1>
53 000000FA 83C301 <1> add ebx, 1
54 000000FD 83C202 <1> add edx, 2
55 <1>
56 00000100 EBED <1> jmp print_string_pm_loop
57 <1>
58 <1> print_string_pm_done:
59 00000102 61 <1> popa
60 00000103 C3 <1> ret
31 %include "switchPM.asm"
1 <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 <1> ; Routine to switch from 16 Real Mode into
3 <1> ; 32 bit protected mode
4 <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5 <1>
6 <1> [bits 16]
7 <1> switch_to_pm:
8 00000104 FA <1> cli ; Clear interrupt flag
9 00000105 0F0116[C600] <1> lgdt [gdt_descriptor] ; Load the GDT descriptor
10 <1>
11 0000010A 0F20C0 <1> mov eax,cr0 ; Set the proteced mode flag
12 0000010D 6683C801 <1> or eax, 0x1
13 00000111 0F22C0 <1> mov cr0,eax
14 <1>
15 00000114 EA[1901]0800 <1> jmp CODE_SEG:init_pm ; Jump to the new CODE segment as per the GDT
16 <1>
17 <1>
18 <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
19 <1> ; 32 MODE Code
20 <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
21 <1>
22 <1> [bits 32]
23 <1>
24 <1> ; Initialise registers and the stack once in PM
25 <1> init_pm:
26 00000119 66B81000 <1> mov ax, DATA_SEG ; Move old Segments to DATA_SEG defined in GDT
27 0000011D 8ED8 <1> mov ds, ax
28 0000011F 8ED0 <1> mov ss, ax
29 00000121 8EC0 <1> mov es, ax
30 00000123 8EE0 <1> mov fs, ax
31 00000125 8EE8 <1> mov gs, ax
32 <1>
33 00000127 BD00000900 <1> mov ebp, 0x90000 ; Update stack point to top of free space
34 0000012C 89EC <1> mov esp, ebp
35 <1>
36 0000012E E82B000000 <1> call BEGIN_PM ; Call the begining of main code
37 <1>
32
33 [bits 16]
34
35 load_kernel:
36 00000133 BE[9B01] mov si, MSG_LOAD_KERNEL
37 00000136 E8DEFE call printString
38
39 00000139 BB0010 mov bx, KERNEL_OFFSET
40 0000013C B60F mov dh, 15
41 0000013E 8A16[6F01] mov dl,[BOOT_DRIVE]
42 00000142 E81CFF call diskLoad
43
44 00000145 C3 ret
45
46 00000146 5374617274696E6720- MSG_REAL_MODE db "Starting in 16-Bit Mode",0
46 0000014F 696E2031362D426974-
46 00000158 204D6F646500
47
48 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
49 ; Protected Mode code
50 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
51
52 BEGIN_PM:
53 ; TODO: clear screen (direct write to memory
54 0000015E E86BFF call cls
55 00000161 66BB[70010000] mov ebx, MSG_PROT_MODE ; Print Message
56 00000167 E87FFF call print_string_pm
57
58 0000016A E8(0010) call KERNEL_OFFSET
59
60 0000016D EBFE jmp $
61
62 0000016F 00 BOOT_DRIVE db 0
63 00000170 537563636573732120- MSG_PROT_MODE db "Success! We are in 32 bit protected mode",0xa,0xd, 0
63 00000179 57652061726520696E-
63 00000182 203332206269742070-
63 0000018B 726F74656374656420-
63 00000194 6D6F64650A0D00
64 0000019B 4C6F6164696E67204B- MSG_LOAD_KERNEL db "Loading Kernel",0xa,0xd,0
64 000001A4 65726E656C0A0D00
65
66 000001AC 00<rep 52h> times 510-($-$$) db 0
67 000001FE 55AA dw 0xaa55