]> git.proxmox.com Git - mirror_edk2.git/blame - DuetPkg/BootSector/efi32.S
Remove exit from batch file
[mirror_edk2.git] / DuetPkg / BootSector / efi32.S
CommitLineData
ccec4c39 1#------------------------------------------------------------------------------\r
2#*\r
3#* Copyright 2006, Intel Corporation \r
4#* All rights reserved. This program and the accompanying materials \r
5#* are licensed and made available under the terms and conditions of the BSD License \r
6#* which accompanies this distribution. The full text of the license may be found at \r
7#* http://opensource.org/licenses/bsd-license.php \r
8#* \r
9#* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
10#* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
11#* \r
12#* efi32.asm\r
13#* \r
14#* Abstract:\r
15#*\r
16#------------------------------------------------------------------------------\r
17\r
18##############################################################################\r
19# Now in 32-bit protected mode.\r
20##############################################################################\r
21\r
ccec4c39 22 .org 0x21000\r
23\r
24.equ DEFAULT_HANDLER_SIZE, INT1 - INT0\r
25\r
26.macro jmpCommonIdtEntry \r
27 # jmp commonIdtEntry - this must be hand coded to keep the assembler from\r
28 # using a 8 bit reletive jump when the entries are\r
29 # within 255 bytes of the common entry. This must\r
30 # be done to maintain the consistency of the size\r
31 # of entry points...\r
32 .byte 0xe9 # jmp 16 bit relative\r
33 .long commonIdtEntry - . - 4 # A problem \r
34.endm\r
35\r
36Start: \r
37 movw %ax, %ds\r
38 movw %ax, %es\r
39 movw %ax, %fs\r
40 movw %ax, %gs\r
41 movw %ax, %ss\r
42 movl $0x001ffff0, %esp\r
43\r
44 call ClearScreen\r
45\r
46 # Populate IDT with meaningful offsets for exception handlers...\r
47 sidt Idtr \r
48\r
49 movl Halt, %eax\r
50 movl %eax, %ebx # use bx to copy 15..0 to descriptors\r
51 shrl $16, %eax # use ax to copy 31..16 to descriptors \r
52 movl $0x78, %ecx # 78h IDT entries to initialize with unique entry points (exceptions)\r
53 movl (Idtr + 2), %esi\r
54 movl (%esi), %edi\r
55\r
56LOOP_1: # loop through all IDT entries exception handlers and initialize to default handler\r
57 movw %bx, (%edi) # write bits 15..0 of offset\r
58 movw $0x20, 2(%edi) # SYS_CODE_SEL from GDT\r
59 movw $(0x0e00 | 0x8000), 4(%edi) # type = 386 interrupt gate, present\r
60 movw %ax, 6(%edi) # write bits 31..16 of offset\r
61 addl $8, %edi # move up to next descriptor\r
62 addw DEFAULT_HANDLER_SIZE, %bx # move to next entry point\r
63 loopl LOOP_1 # loop back through again until all descriptors are initialized\r
64\r
65 ## at this point edi contains the offset of the descriptor for INT 20\r
66 ## and bx contains the low 16 bits of the offset of the default handler\r
67 ## so initialize all the rest of the descriptors with these two values...\r
68# mov ecx, 101 ; there are 100 descriptors left (INT 20 (14h) - INT 119 (77h)\r
69#@@: ; loop through all IDT entries exception handlers and initialize to default handler\r
70# mov word ptr [edi], bx ; write bits 15..0 of offset\r
71# mov word ptr [edi+2], 20h ; SYS_CODE_SEL from GDT\r
72# mov word ptr [edi+4], 0e00h OR 8000h ; type = 386 interrupt gate, present\r
73# mov word ptr [edi+6], ax ; write bits 31..16 of offset\r
74# add edi, 8 ; move up to next descriptor\r
75# loop @b ; loop back through again until all descriptors are initialized\r
76\r
77\r
78## DUMP location of IDT and several of the descriptors\r
79# mov ecx, 8\r
80# mov eax, [offset Idtr + 2]\r
81# mov eax, [eax]\r
82# mov edi, 0b8000h\r
83# call PrintDword\r
84# mov esi, eax\r
85# mov edi, 0b80a0h\r
86# jmp OuterLoop\r
87\r
88## \r
89## just for fun, let's do a software interrupt to see if we correctly land in the exception handler...\r
90# mov eax, 011111111h\r
91# mov ebx, 022222222h\r
92# mov ecx, 033333333h\r
93# mov edx, 044444444h\r
94# mov ebp, 055555555h\r
95# mov esi, 066666666h\r
96# mov edi, 077777777h\r
97# push 011111111h\r
98# push 022222222h\r
99# push 033333333h\r
100# int 119\r
101\r
102\r
103 movl $0x22000, %esi # esi = 22000\r
104 movl 0x14(%esi), %eax # eax = [22014]\r
105 addl %eax, %esi # esi = 22000 + [22014] = Base of EFILDR.C\r
106 movl 0x3c(%esi), %ebp # ebp = [22000 + [22014] + 3c] = NT Image Header for EFILDR.C\r
107 addl %esi, %ebp\r
108 movl 0x34(%ebp), %edi # edi = [[22000 + [22014] + 3c] + 30] = ImageBase\r
109 movl 0x28(%ebp), %eax # eax = [[22000 + [22014] + 3c] + 24] = EntryPoint\r
110 addl %edi, %eax # eax = ImageBase + EntryPoint\r
111 movl %eax, EfiLdrOffset # Modify far jump instruction for correct entry point\r
112\r
113 movw 6(%ebp), %bx # bx = Number of sections\r
114 xorl %eax, %eax\r
115 movw 0x14(%ebp), %ax # ax = Optional Header Size\r
116 addl %eax, %ebp\r
117 addl $0x18, %ebp # ebp = Start of 1st Section\r
118\r
119SectionLoop: \r
120 pushl %esi # Save Base of EFILDR.C\r
121 pushl %edi # Save ImageBase\r
122 addl 0x14(%ebp), %esi # esi = Base of EFILDR.C + PointerToRawData\r
123 addl 0x0c(%ebp), %edi # edi = ImageBase + VirtualAddress\r
124 movl 0x10(%ebp), %ecx # ecs = SizeOfRawData\r
125\r
126 cld\r
127 shrl $2, %ecx\r
128 rep\r
129 movsl\r
130\r
131 popl %edi # Restore ImageBase\r
132 popl %esi # Restore Base of EFILDR.C\r
133\r
134 addw $0x28, %bp # ebp = ebp + 028h = Pointer to next section record\r
135 decw %bx\r
136 cmpw $0, %bx\r
137 jne SectionLoop\r
138\r
7674c40a 139 movzwl (Idtr), %eax # get size of IDT\r
ccec4c39 140 incl %eax\r
49794755 141 addl (Idtr + 2), %eax # add to base of IDT to get location of memory map... \r
ccec4c39 142 pushl %eax # push memory map location on stack for call to EFILDR...\r
143\r
144 pushl %eax # push return address (useless, just for stack balance)\r
145 .byte 0xb8\r
146EfiLdrOffset: \r
147 .long 0x00401000 # Offset of EFILDR\r
148# mov eax, 401000h\r
149 pushl %eax\r
150 ret\r
151\r
152# db "**** DEFAULT IDT ENTRY ***",0\r
153 .align 0x2\r
154Halt: \r
155INT0: \r
156 pushl $0x0 # push error code place holder on the stack\r
157 pushl $0x0\r
158 jmpCommonIdtEntry \r
159# db 0e9h ; jmp 16 bit reletive \r
160# dd commonIdtEntry - $ - 4 ; offset to jump to\r
161\r
162INT1: \r
163 pushl $0x0 # push error code place holder on the stack\r
164 pushl $0x1\r
165 jmpCommonIdtEntry \r
166\r
167INT2: \r
168 pushl $0x0 # push error code place holder on the stack\r
169 pushl $0x2\r
170 jmpCommonIdtEntry \r
171\r
172INT3: \r
173 pushl $0x0 # push error code place holder on the stack\r
174 pushl $0x3\r
175 jmpCommonIdtEntry \r
176\r
177INT4: \r
178 pushl $0x0 # push error code place holder on the stack\r
179 pushl $0x4\r
180 jmpCommonIdtEntry \r
181\r
182INT5: \r
183 pushl $0x0 # push error code place holder on the stack\r
184 pushl $0x5\r
185 jmpCommonIdtEntry \r
186\r
187INT6: \r
188 pushl $0x0 # push error code place holder on the stack\r
189 pushl $0x6\r
190 jmpCommonIdtEntry \r
191\r
192INT7: \r
193 pushl $0x0 # push error code place holder on the stack\r
194 pushl $0x7\r
195 jmpCommonIdtEntry \r
196\r
197INT8: \r
198# Double fault causes an error code to be pushed so no phony push necessary\r
199 nop\r
200 nop\r
201 pushl $0x8\r
202 jmpCommonIdtEntry \r
203\r
204INT9: \r
205 pushl $0x0 # push error code place holder on the stack\r
206 pushl $0x9\r
207 jmpCommonIdtEntry \r
208\r
209INT10: \r
210# Invalid TSS causes an error code to be pushed so no phony push necessary\r
211 nop\r
212 nop\r
213 pushl $10\r
214 jmpCommonIdtEntry \r
215\r
216INT11: \r
217# Segment Not Present causes an error code to be pushed so no phony push necessary\r
218 nop\r
219 nop\r
220 pushl $11\r
221 jmpCommonIdtEntry \r
222\r
223INT12: \r
224# Stack fault causes an error code to be pushed so no phony push necessary\r
225 nop\r
226 nop\r
227 pushl $12\r
228 jmpCommonIdtEntry \r
229\r
230INT13: \r
231# GP fault causes an error code to be pushed so no phony push necessary\r
232 nop\r
233 nop\r
234 pushl $13\r
235 jmpCommonIdtEntry \r
236\r
237INT14: \r
238# Page fault causes an error code to be pushed so no phony push necessary\r
239 nop\r
240 nop\r
241 pushl $14\r
242 jmpCommonIdtEntry \r
243\r
244INT15: \r
245 pushl $0x0 # push error code place holder on the stack\r
246 pushl $15\r
247 jmpCommonIdtEntry \r
248\r
249INT16: \r
250 pushl $0x0 # push error code place holder on the stack\r
251 pushl $16\r
252 jmpCommonIdtEntry \r
253\r
254INT17: \r
255# Alignment check causes an error code to be pushed so no phony push necessary\r
256 nop\r
257 nop\r
258 pushl $17\r
259 jmpCommonIdtEntry \r
260\r
261INT18: \r
262 pushl $0x0 # push error code place holder on the stack\r
263 pushl $18\r
264 jmpCommonIdtEntry \r
265\r
266INT19: \r
267 pushl $0x0 # push error code place holder on the stack\r
268 pushl $19\r
269 jmpCommonIdtEntry \r
270\r
271INTUnknown: \r
272.rept (0x78 - 20)\r
273 pushl $0x0 # push error code place holder on the stack\r
274# push $0xxx # push vector number\r
275 .byte 0x6a\r
276 .long ( . - INTUnknown - 3 ) / 9 + 20 # vector number\r
277 jmpCommonIdtEntry \r
278.endr\r
279\r
280commonIdtEntry: \r
281 pushal\r
282 movl %esp, %ebp\r
283##\r
284## At this point the stack looks like this:\r
285##\r
286## eflags\r
287## Calling CS\r
288## Calling EIP\r
289## Error code or 0\r
290## Int num or 0ffh for unknown int num\r
291## eax\r
292## ecx\r
293## edx\r
294## ebx\r
295## esp\r
296## ebp\r
297## esi\r
298## edi <------- ESP, EBP\r
299## \r
300\r
301 call ClearScreen\r
302 movl String1, %esi\r
303 call PrintString\r
304 movl 32(%ebp), %eax ## move Int number into EAX \r
305 cmpl $19, %eax\r
306 ja PrintDefaultString\r
307PrintExceptionString: \r
308 shll $2, %eax ## multiply by 4 to get offset from StringTable to actual string address\r
309 addl StringTable, %eax\r
310 movl (%eax), %esi\r
311 jmp PrintTheString\r
312PrintDefaultString: \r
313 movl IntUnknownString, %esi\r
314 # patch Int number\r
315 movl %eax, %edx\r
316 call A2C\r
317 movb %al, 1(%esi)\r
318 movl %edx, %eax\r
319 shrl $4, %eax\r
320 call A2C\r
321 movb %al, (%esi)\r
322PrintTheString: \r
323 call PrintString\r
324 movl String2, %esi\r
325 call PrintString\r
326 movl 44(%ebp), %eax # CS\r
327 call PrintDword\r
328 movb ':', %al\r
329 movb %al, (%edi)\r
330 addl $2, %edi\r
331 movl 40(%ebp), %eax # EIP\r
332 call PrintDword\r
333 movl String3, %esi\r
334 call PrintString\r
335\r
336 movl $0xb8140, %edi\r
337\r
338 movl StringEax, %esi # eax\r
339 call PrintString\r
340 movl 28(%ebp), %eax\r
341 call PrintDword\r
342\r
343 movl StringEbx, %esi # ebx\r
344 call PrintString\r
345 movl 16(%ebp), %eax\r
346 call PrintDword\r
347\r
348 movl StringEcx, %esi # ecx\r
349 call PrintString\r
350 movl 24(%ebp), %eax\r
351 call PrintDword\r
352\r
353 movl StringEdx, %esi # edx\r
354 call PrintString\r
355 movl 20(%ebp), %eax\r
356 call PrintDword\r
357\r
358 movl StringEcode, %esi # error code\r
359 call PrintString\r
360 movl 36(%ebp), %eax\r
361 call PrintDword\r
362\r
363 movl $0xb81e0, %edi\r
364\r
365 movl StringEsp, %esi # esp\r
366 call PrintString\r
367 movl 12(%ebp), %eax\r
368 call PrintDword\r
369\r
370 movl StringEbp, %esi # ebp\r
371 call PrintString\r
372 movl 8(%ebp), %eax\r
373 call PrintDword\r
374\r
375 movl StringEsi, %esi # esi\r
376 call PrintString\r
377 movl 4(%ebp), %eax\r
378 call PrintDword\r
379\r
380 movl StringEdi, %esi # edi\r
381 call PrintString\r
382 movl (%ebp), %eax\r
383 call PrintDword\r
384\r
385 movl StringEflags, %esi # eflags\r
386 call PrintString\r
387 movl 48(%ebp), %eax\r
388 call PrintDword\r
389\r
390 movl $0xb8320, %edi\r
391\r
392 movl %ebp, %esi\r
393 addl $52, %esi\r
394 movl $8, %ecx\r
395\r
396\r
397OuterLoop: \r
398 pushl %ecx\r
399 movl $8, %ecx\r
400 movl %edi, %edx\r
401\r
402InnerLoop: \r
403 movl (%esi), %eax\r
404 call PrintDword\r
405 addl $4, %esi\r
406 movb ' ', %al \r
407 movb %al, (%edi)\r
408 addl $2, %edi\r
409 loop InnerLoop\r
410\r
411 popl %ecx\r
412 addl $0xa0, %edx\r
413 movl %edx, %edi\r
414 loop OuterLoop\r
415\r
416\r
417 movl $0xb8960, %edi\r
418\r
419 movl 40(%ebp), %eax # EIP\r
420 subl $32*4, %eax\r
421 movl %eax, %esi # esi = eip - 32 DWORD linear (total 64 DWORD)\r
422\r
423 movl $8, %ecx\r
424\r
425OuterLoop1: \r
426 pushl %ecx\r
427 movl $8, %ecx\r
428 movl %edi, %edx\r
429\r
430InnerLoop1: \r
431 movl (%esi), %eax\r
432 call PrintDword\r
433 addl $4, %esi\r
434 movb ' ', %al \r
435 movb %al, (%edi)\r
436 addl $2, %edi\r
437 loop InnerLoop1\r
438\r
439 popl %ecx\r
440 addl $0xa0, %edx\r
441 movl %edx, %edi\r
442 loop OuterLoop1\r
443\r
444\r
445\r
446# wbinvd ; this intruction does not support in early than 486 arch\r
447LN_C1: \r
448 jmp LN_C1\r
449#\r
450# return\r
451#\r
452 movl %ebp, %esp\r
453 popal\r
454 addl $8, %esp # error code and INT number\r
455\r
456 iretl\r
457\r
458\r
459PrintString: \r
460 pushl %eax\r
461LN_C2: \r
462 movb (%esi), %al\r
463 cmpb $0, %al\r
464 je LN_C3\r
465 movb %al, (%edi)\r
466 incl %esi\r
467 addl $2, %edi\r
468 jmp LN_C2\r
469LN_C3: \r
470 popl %eax\r
471 ret\r
472\r
473## EAX contains dword to print\r
474## EDI contains memory location (screen location) to print it to\r
475PrintDword: \r
476 pushl %ecx\r
477 pushl %ebx\r
478 pushl %eax\r
479\r
480 movl $8, %ecx\r
481looptop: \r
482 roll $4, %eax\r
483 movb %al, %bl\r
484 andb $0xf, %bl\r
485 addb '0', %bl\r
486 cmpb '9', %bl\r
487 jle LN_C4\r
488 addb $7, %bl\r
489LN_C4: \r
490 movb %bl, (%edi)\r
491 addl $2, %edi\r
492 loop looptop\r
493 #wbinvd\r
494\r
495 popl %eax\r
496 popl %ebx\r
497 popl %ecx\r
498 ret\r
499\r
500ClearScreen: \r
501 pushl %eax\r
502 pushl %ecx\r
503\r
d0b0659f 504 movb $0x00, %al\r
ccec4c39 505 movb $0xc, %ah\r
506 movl $0xb8000, %edi\r
507 movl $80*24, %ecx\r
508LN_C5: \r
509 movw %ax, (%edi)\r
510 addl $2, %edi\r
511 loop LN_C5\r
512 movl $0xb8000, %edi\r
513\r
514 popl %ecx\r
515 popl %eax\r
516\r
517 ret\r
518\r
519A2C: \r
520 andb $0xf, %al\r
521 addb '0', %al\r
522 cmpb '9', %al\r
523 jle LN_C6\r
524 addb $7, %al\r
525LN_C6: \r
526 ret\r
527\r
528String1: .asciz "*** INT "\r
529\r
530Int0String: .asciz "00h Divide by 0 -"\r
531Int1String: .asciz "01h Debug exception -"\r
532Int2String: .asciz "02h NMI -"\r
533Int3String: .asciz "03h Breakpoint -"\r
534Int4String: .asciz "04h Overflow -"\r
535Int5String: .asciz "05h Bound -"\r
536Int6String: .asciz "06h Invalid opcode -"\r
537Int7String: .asciz "07h Device not available -"\r
538Int8String: .asciz "08h Double fault -"\r
539Int9String: .asciz "09h Coprocessor seg overrun (reserved) -"\r
540Int10String: .asciz "0Ah Invalid TSS -"\r
541Int11String: .asciz "0Bh Segment not present -"\r
542Int12String: .asciz "0Ch Stack fault -"\r
543Int13String: .asciz "0Dh General protection fault -"\r
544Int14String: .asciz "0Eh Page fault -"\r
545Int15String: .asciz "0Fh (Intel reserved) -"\r
546Int16String: .asciz "10h Floating point error -"\r
547Int17String: .asciz "11h Alignment check -"\r
548Int18String: .asciz "12h Machine check -"\r
549Int19String: .asciz "13h SIMD Floating-Point Exception -"\r
550IntUnknownString: .asciz "??h Unknown interrupt -"\r
551\r
552StringTable: .long Int0String, Int1String, Int2String, Int3String, \\r
553 Int4String, Int5String, Int6String, Int7String, \\r
554 Int8String, Int9String, Int10String, Int11String, \\r
555 Int12String, Int13String, Int14String, Int15String,\\r
556 Int16String, Int17String, Int18String, Int19String\r
557\r
558String2: .asciz " HALT!! *** ("\r
559String3: .asciz ")"\r
560StringEax: .asciz "EAX="\r
561StringEbx: .asciz "EBX="\r
562StringEcx: .asciz "ECX="\r
563StringEdx: .asciz "EDX="\r
564StringEcode: .asciz "ECODE="\r
565StringEsp: .asciz "ESP="\r
566StringEbp: .asciz "EBP="\r
567StringEsi: .asciz "ESI="\r
568StringEdi: .asciz "EDI="\r
569StringEflags: .asciz "EFLAGS="\r
570\r
571Idtr: .float 0\r
572\r
573 .org 0x21ffe\r
574BlockSignature: \r
575 .word 0xaa55\r
576\r
577\r