All UEFI applications in the MdeModukePkg should be placed in MdeModulePkg/Application
[edk2.git] / DuetPkg / BootSector / efi32.asm
blob0e2d41732f410ab5862653a0b365b7a6e4507bc3
1 ;------------------------------------------------------------------------------
2 ;*
3 ;* Copyright 2006, Intel Corporation
4 ;* All rights reserved. This program and the accompanying materials
5 ;* are licensed and made available under the terms and conditions of the BSD License
6 ;* which accompanies this distribution. The full text of the license may be found at
7 ;* http://opensource.org/licenses/bsd-license.php
8 ;*
9 ;* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 ;* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11 ;*
12 ;* efi32.asm
13 ;*
14 ;* Abstract:
16 ;------------------------------------------------------------------------------
18 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
19 ; Now in 32-bit protected mode.
20 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
22 .486
23 .model flat
24 .stack
25 .code
26 org 21000h
28 DEFAULT_HANDLER_SIZE EQU INT1 - INT0
30 JmpCommonIdtEntry macro
31 ; jmp commonIdtEntry - this must be hand coded to keep the assembler from
32 ; using a 8 bit reletive jump when the entries are
33 ; within 255 bytes of the common entry. This must
34 ; be done to maintain the consistency of the size
35 ; of entry points...
36 db 0e9h ; jmp 16 bit relative
37 dd commonIdtEntry - $ - 4 ; offset to jump to
38 endm
41 Start:
42 mov ds,ax
43 mov es,ax
44 mov fs,ax
45 mov gs,ax
46 mov ss,ax
47 mov esp,0001ffff0h
49 call ClearScreen
51 ; Populate IDT with meaningful offsets for exception handlers...
52 sidt fword ptr [Idtr] ; get fword address of IDT
54 mov eax, offset Halt
55 mov ebx, eax ; use bx to copy 15..0 to descriptors
56 shr eax, 16 ; use ax to copy 31..16 to descriptors
57 mov ecx, 78h ; 78h IDT entries to initialize with unique entry points (exceptions)
58 mov esi, [offset Idtr + 2]
59 mov edi, [esi]
61 @@: ; loop through all IDT entries exception handlers and initialize to default handler
62 mov word ptr [edi], bx ; write bits 15..0 of offset
63 mov word ptr [edi+2], 20h ; SYS_CODE_SEL from GDT
64 mov word ptr [edi+4], 0e00h OR 8000h ; type = 386 interrupt gate, present
65 mov word ptr [edi+6], ax ; write bits 31..16 of offset
66 add edi, 8 ; move up to next descriptor
67 add bx, DEFAULT_HANDLER_SIZE ; move to next entry point
68 loop @b ; loop back through again until all descriptors are initialized
70 ;; at this point edi contains the offset of the descriptor for INT 20
71 ;; and bx contains the low 16 bits of the offset of the default handler
72 ;; so initialize all the rest of the descriptors with these two values...
73 ; mov ecx, 101 ; there are 100 descriptors left (INT 20 (14h) - INT 119 (77h)
74 ;@@: ; loop through all IDT entries exception handlers and initialize to default handler
75 ; mov word ptr [edi], bx ; write bits 15..0 of offset
76 ; mov word ptr [edi+2], 20h ; SYS_CODE_SEL from GDT
77 ; mov word ptr [edi+4], 0e00h OR 8000h ; type = 386 interrupt gate, present
78 ; mov word ptr [edi+6], ax ; write bits 31..16 of offset
79 ; add edi, 8 ; move up to next descriptor
80 ; loop @b ; loop back through again until all descriptors are initialized
83 ;; DUMP location of IDT and several of the descriptors
84 ; mov ecx, 8
85 ; mov eax, [offset Idtr + 2]
86 ; mov eax, [eax]
87 ; mov edi, 0b8000h
88 ; call PrintDword
89 ; mov esi, eax
90 ; mov edi, 0b80a0h
91 ; jmp OuterLoop
93 ;;
94 ;; just for fun, let's do a software interrupt to see if we correctly land in the exception handler...
95 ; mov eax, 011111111h
96 ; mov ebx, 022222222h
97 ; mov ecx, 033333333h
98 ; mov edx, 044444444h
99 ; mov ebp, 055555555h
100 ; mov esi, 066666666h
101 ; mov edi, 077777777h
102 ; push 011111111h
103 ; push 022222222h
104 ; push 033333333h
105 ; int 119
108 mov esi,022000h ; esi = 22000
109 mov eax,[esi+014h] ; eax = [22014]
110 add esi,eax ; esi = 22000 + [22014] = Base of EFILDR.C
111 mov ebp,[esi+03ch] ; ebp = [22000 + [22014] + 3c] = NT Image Header for EFILDR.C
112 add ebp,esi
113 mov edi,[ebp+034h] ; edi = [[22000 + [22014] + 3c] + 30] = ImageBase
114 mov eax,[ebp+028h] ; eax = [[22000 + [22014] + 3c] + 24] = EntryPoint
115 add eax,edi ; eax = ImageBase + EntryPoint
116 mov dword ptr [EfiLdrOffset],eax ; Modify far jump instruction for correct entry point
118 mov bx,word ptr[ebp+6] ; bx = Number of sections
119 xor eax,eax
120 mov ax,word ptr[ebp+014h] ; ax = Optional Header Size
121 add ebp,eax
122 add ebp,018h ; ebp = Start of 1st Section
124 SectionLoop:
125 push esi ; Save Base of EFILDR.C
126 push edi ; Save ImageBase
127 add esi,[ebp+014h] ; esi = Base of EFILDR.C + PointerToRawData
128 add edi,[ebp+00ch] ; edi = ImageBase + VirtualAddress
129 mov ecx,[ebp+010h] ; ecs = SizeOfRawData
132 shr ecx,2
133 rep movsd
135 pop edi ; Restore ImageBase
136 pop esi ; Restore Base of EFILDR.C
138 add bp,028h ; ebp = ebp + 028h = Pointer to next section record
139 dec bx
140 cmp bx,0
141 jne SectionLoop
143 movzx eax, word ptr [Idtr] ; get size of IDT
144 inc eax
145 add eax, dword ptr [Idtr + 2] ; add to base of IDT to get location of memory map...
146 push eax ; push memory map location on stack for call to EFILDR...
148 push eax ; push return address (useless, just for stack balance)
149 db 0b8h
150 EfiLdrOffset:
151 dd 000401000h ; Offset of EFILDR
152 ; mov eax, 401000h
153 push eax
156 ; db "**** DEFAULT IDT ENTRY ***",0
157 align 02h
158 Halt:
159 INT0:
160 push 0h ; push error code place holder on the stack
161 push 0h
162 JmpCommonIdtEntry
163 ; db 0e9h ; jmp 16 bit reletive
164 ; dd commonIdtEntry - $ - 4 ; offset to jump to
166 INT1:
167 push 0h ; push error code place holder on the stack
168 push 1h
169 JmpCommonIdtEntry
171 INT2:
172 push 0h ; push error code place holder on the stack
173 push 2h
174 JmpCommonIdtEntry
176 INT3:
177 push 0h ; push error code place holder on the stack
178 push 3h
179 JmpCommonIdtEntry
181 INT4:
182 push 0h ; push error code place holder on the stack
183 push 4h
184 JmpCommonIdtEntry
186 INT5:
187 push 0h ; push error code place holder on the stack
188 push 5h
189 JmpCommonIdtEntry
191 INT6:
192 push 0h ; push error code place holder on the stack
193 push 6h
194 JmpCommonIdtEntry
196 INT7:
197 push 0h ; push error code place holder on the stack
198 push 7h
199 JmpCommonIdtEntry
201 INT8:
202 ; Double fault causes an error code to be pushed so no phony push necessary
205 push 8h
206 JmpCommonIdtEntry
208 INT9:
209 push 0h ; push error code place holder on the stack
210 push 9h
211 JmpCommonIdtEntry
213 INT10:
214 ; Invalid TSS causes an error code to be pushed so no phony push necessary
217 push 10
218 JmpCommonIdtEntry
220 INT11:
221 ; Segment Not Present causes an error code to be pushed so no phony push necessary
224 push 11
225 JmpCommonIdtEntry
227 INT12:
228 ; Stack fault causes an error code to be pushed so no phony push necessary
231 push 12
232 JmpCommonIdtEntry
234 INT13:
235 ; GP fault causes an error code to be pushed so no phony push necessary
238 push 13
239 JmpCommonIdtEntry
241 INT14:
242 ; Page fault causes an error code to be pushed so no phony push necessary
245 push 14
246 JmpCommonIdtEntry
248 INT15:
249 push 0h ; push error code place holder on the stack
250 push 15
251 JmpCommonIdtEntry
253 INT16:
254 push 0h ; push error code place holder on the stack
255 push 16
256 JmpCommonIdtEntry
258 INT17:
259 ; Alignment check causes an error code to be pushed so no phony push necessary
262 push 17
263 JmpCommonIdtEntry
265 INT18:
266 push 0h ; push error code place holder on the stack
267 push 18
268 JmpCommonIdtEntry
270 INT19:
271 push 0h ; push error code place holder on the stack
272 push 19
273 JmpCommonIdtEntry
275 INTUnknown:
276 REPEAT (78h - 20)
277 push 0h ; push error code place holder on the stack
278 ; push xxh ; push vector number
279 db 06ah
280 db ( $ - INTUnknown - 3 ) / 9 + 20 ; vector number
281 JmpCommonIdtEntry
282 ENDM
284 commonIdtEntry:
285 pushad
286 mov ebp, esp
288 ;; At this point the stack looks like this:
290 ;; eflags
291 ;; Calling CS
292 ;; Calling EIP
293 ;; Error code or 0
294 ;; Int num or 0ffh for unknown int num
295 ;; eax
296 ;; ecx
297 ;; edx
298 ;; ebx
299 ;; esp
300 ;; ebp
301 ;; esi
302 ;; edi <------- ESP, EBP
305 call ClearScreen
306 mov esi, offset String1
307 call PrintString
308 mov eax, [ebp + 32] ;; move Int number into EAX
309 cmp eax, 19
310 ja PrintDefaultString
311 PrintExceptionString:
312 shl eax, 2 ;; multiply by 4 to get offset from StringTable to actual string address
313 add eax, offset StringTable
314 mov esi, [eax]
315 jmp PrintTheString
316 PrintDefaultString:
317 mov esi, offset IntUnknownString
318 ; patch Int number
319 mov edx, eax
320 call A2C
321 mov [esi + 1], al
322 mov eax, edx
323 shr eax, 4
324 call A2C
325 mov [esi], al
326 PrintTheString:
327 call PrintString
328 mov esi, offset String2
329 call PrintString
330 mov eax, [ebp+44] ; CS
331 call PrintDword
332 mov al, ':'
333 mov byte ptr [edi], al
334 add edi, 2
335 mov eax, [ebp+40] ; EIP
336 call PrintDword
337 mov esi, offset String3
338 call PrintString
340 mov edi, 0b8140h
342 mov esi, offset StringEax ; eax
343 call PrintString
344 mov eax, [ebp+28]
345 call PrintDword
347 mov esi, offset StringEbx ; ebx
348 call PrintString
349 mov eax, [ebp+16]
350 call PrintDword
352 mov esi, offset StringEcx ; ecx
353 call PrintString
354 mov eax, [ebp+24]
355 call PrintDword
357 mov esi, offset StringEdx ; edx
358 call PrintString
359 mov eax, [ebp+20]
360 call PrintDword
362 mov esi, offset StringEcode ; error code
363 call PrintString
364 mov eax, [ebp+36]
365 call PrintDword
367 mov edi, 0b81e0h
369 mov esi, offset StringEsp ; esp
370 call PrintString
371 mov eax, [ebp+12]
372 call PrintDword
374 mov esi, offset StringEbp ; ebp
375 call PrintString
376 mov eax, [ebp+8]
377 call PrintDword
379 mov esi, offset StringEsi ; esi
380 call PrintString
381 mov eax, [ebp+4]
382 call PrintDword
384 mov esi, offset StringEdi ; edi
385 call PrintString
386 mov eax, [ebp]
387 call PrintDword
389 mov esi, offset StringEflags ; eflags
390 call PrintString
391 mov eax, [ebp+48]
392 call PrintDword
394 mov edi, 0b8320h
396 mov esi, ebp
397 add esi, 52
398 mov ecx, 8
401 OuterLoop:
402 push ecx
403 mov ecx, 8
404 mov edx, edi
406 InnerLoop:
407 mov eax, [esi]
408 call PrintDword
409 add esi, 4
410 mov al, ' '
411 mov [edi], al
412 add edi, 2
413 loop InnerLoop
415 pop ecx
416 add edx, 0a0h
417 mov edi, edx
418 loop OuterLoop
421 mov edi, 0b8960h
423 mov eax, [ebp+40] ; EIP
424 sub eax, 32 * 4
425 mov esi, eax ; esi = eip - 32 DWORD linear (total 64 DWORD)
427 mov ecx, 8
429 OuterLoop1:
430 push ecx
431 mov ecx, 8
432 mov edx, edi
434 InnerLoop1:
435 mov eax, [esi]
436 call PrintDword
437 add esi, 4
438 mov al, ' '
439 mov [edi], al
440 add edi, 2
441 loop InnerLoop1
443 pop ecx
444 add edx, 0a0h
445 mov edi, edx
446 loop OuterLoop1
450 ; wbinvd ; Ken: this intruction does not support in early than 486 arch
451 @@:
452 jmp @b
454 ; return
456 mov esp, ebp
457 popad
458 add esp, 8 ; error code and INT number
460 iretd
463 PrintString:
464 push eax
466 mov al, byte ptr [esi]
467 cmp al, 0
468 je @f
469 mov byte ptr [edi], al
470 inc esi
471 add edi, 2
472 jmp @b
474 pop eax
477 ;; EAX contains dword to print
478 ;; EDI contains memory location (screen location) to print it to
479 PrintDword:
480 push ecx
481 push ebx
482 push eax
484 mov ecx, 8
485 looptop:
486 rol eax, 4
487 mov bl, al
488 and bl, 0fh
489 add bl, '0'
490 cmp bl, '9'
491 jle @f
492 add bl, 7
494 mov byte ptr [edi], bl
495 add edi, 2
496 loop looptop
497 ;wbinvd
499 pop eax
500 pop ebx
501 pop ecx
504 ClearScreen:
505 push eax
506 push ecx
508 mov al, ' '
509 mov ah, 0ch
510 mov edi, 0b8000h
511 mov ecx, 80 * 24
513 mov word ptr [edi], ax
514 add edi, 2
515 loop @b
516 mov edi, 0b8000h
518 pop ecx
519 pop eax
521 ret
523 A2C:
524 and al, 0fh
525 add al, '0'
526 cmp al, '9'
527 jle @f
528 add al, 7
532 String1 db "*** INT ",0
534 Int0String db "00h Divide by 0 -",0
535 Int1String db "01h Debug exception -",0
536 Int2String db "02h NMI -",0
537 Int3String db "03h Breakpoint -",0
538 Int4String db "04h Overflow -",0
539 Int5String db "05h Bound -",0
540 Int6String db "06h Invalid opcode -",0
541 Int7String db "07h Device not available -",0
542 Int8String db "08h Double fault -",0
543 Int9String db "09h Coprocessor seg overrun (reserved) -",0
544 Int10String db "0Ah Invalid TSS -",0
545 Int11String db "0Bh Segment not present -",0
546 Int12String db "0Ch Stack fault -",0
547 Int13String db "0Dh General protection fault -",0
548 Int14String db "0Eh Page fault -",0
549 Int15String db "0Fh (Intel reserved) -",0
550 Int16String db "10h Floating point error -",0
551 Int17String db "11h Alignment check -",0
552 Int18String db "12h Machine check -",0
553 Int19String db "13h SIMD Floating-Point Exception -",0
554 IntUnknownString db "??h Unknown interrupt -",0
556 StringTable dd offset Int0String, offset Int1String, offset Int2String, offset Int3String,
557 offset Int4String, offset Int5String, offset Int6String, offset Int7String,
558 offset Int8String, offset Int9String, offset Int10String, offset Int11String,
559 offset Int12String, offset Int13String, offset Int14String, offset Int15String,
560 offset Int16String, offset Int17String, offset Int18String, offset Int19String
562 String2 db " HALT!! *** (",0
563 String3 db ")",0
564 StringEax db "EAX=",0
565 StringEbx db " EBX=",0
566 StringEcx db " ECX=",0
567 StringEdx db " EDX=",0
568 StringEcode db " ECODE=",0
569 StringEsp db "ESP=",0
570 StringEbp db " EBP=",0
571 StringEsi db " ESI=",0
572 StringEdi db " EDI=",0
573 StringEflags db " EFLAGS=",0
575 Idtr df 0
577 org 21ffeh
578 BlockSignature:
579 dw 0aa55h