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