Skip to content

Commit cafa00b

Browse files
Tamano de section .code de etapa uefi en archivo EFI64 agrandado a 3KiB.
1 parent f7149a1 commit cafa00b

File tree

8 files changed

+100
-39
lines changed

8 files changed

+100
-39
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ $(BOOTLOADER):
1818

1919
$(UEFI):
2020
cd ./asm/boot; nasm uefi.asm -o ./../../build/uefi.sys
21+
cd ./asm/boot; ld -g --oformat elf64-x86-64 --entry 0x400000 ./../../build/uefi.sys -o ./../../build/uefi.elf
2122

2223
clean:
2324
rm -rf *.sys

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Ver /boot/src/boot.cpp
2727

2828
[EFI Specification Version 1.10](https://www.intel.com/content/dam/www/public/us/en/zip/efi-110.zip)
2929

30+
[EFI Specification Version 2.8](https://uefi.org/sites/default/files/resources/UEFI_Spec_2_8_final.pdf}
31+
3032
### Info extra varia
3133

3234
[Introduction to UEFI](http://x86asm.net/articles/introduction-to-uefi/index.html)

asm/boot/uefi.asm

Lines changed: 84 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@
33
;;==============================================================================
44
;; Varios de los comentarios realizados estan basados en la informacion de:
55
;; -- Extensible Firmware Interface Specification Version 1.10 December 1, 2002.
6+
;; -- EFI Specification Version 2.8
67
;; -- Headers: https://learn.microsoft.com/en-us/windows/win32/debug/pe-format
78
;; Otra info:
89
;; Calling convention: https://learn.microsoft.com/en-us/cpp/build/x64-calling-c
910
;; onvention?view=msvc-170
1011
;;
1112
;; La salida de NASM se guarda en /EFI/BOOT/BOOTX64.EFI y se le inyecta el paylo
1213
;; ad (UEFI bootloader + packedKernel.bin) que se requiera. Archivo BOOTX64.EFI,
13-
;; luego de agregado de payload queda:
14+
;; luego de agregado el payload queda:
1415
;; +--------------------------+-------------------------+------------+
1516
;; | binario BOOTX64.EFI | payload | padeo de |
1617
;; | | | | UEFI | packed | 0x00 hasta |
1718
;; | Encabez | Codigo | Datos | bootloader | Kernel.bin | el fin |
1819
;; +---------+--------+-------+------------+------------+------------+
19-
;; |^ |^ |^ |^ |^ ^|
20-
;; 0x0 0x800 0x1000 0x2800 0x40000 0xFFFFF
21-
;; 0 2KiB 4KiB 10KiB 256KiB 1MiB-1
20+
;; |^ |^ |^ |^ |^ |^ ^|
21+
;; 0x0 0x200 0xC00 0x1000 0x2800 0x40000 0xFFFFF
22+
;; 0 512B 3KiB 4KiB 10KiB 256KiB 1MiB-1
2223
;;==============================================================================
2324

2425

@@ -124,10 +125,11 @@ SECTION_CODE:
124125
.number_of_relocations dw 0
125126
.number_of_line_numbers dw 0
126127
.characteristics dd 0x70000020
127-
;;IMAGE_SCN_MEM_SHARED 0x10000000 The section can be shared in memory.
128-
;;IMAGE_SCN_MEM_EXECUTE 0x20000000 The section can be executed as code.
129-
;;IMAGE_SCN_MEM_READ 0x40000000 The section can be read.
130-
;;IMAGE_SCN_CNT_CODE 0x00000020 The section contains executable code.
128+
;; Section flags:
129+
;; IMAGE_SCN_MEM_SHARED 0x10000000 Can be shared in memory.
130+
;; IMAGE_SCN_MEM_EXECUTE 0x20000000 Can be executed as code.
131+
;; IMAGE_SCN_MEM_READ 0x40000000 Can be read.
132+
;; IMAGE_SCN_CNT_CODE 0x00000020 Contains executable code.
131133

132134
SECTION_DATA:
133135
.name db ".data", 0x00, 0x00, 0x00
@@ -140,21 +142,23 @@ SECTION_DATA:
140142
.number_of_relocations dw 0
141143
.number_of_line_numbers dw 0
142144
.characteristics dd 0xD0000040
143-
;;IMAGE_SCN_MEM_SHARED 0x10000000 The section can be shared in memory.
144-
;;IMAGE_SCN_MEM_READ 0x40000000 The section can be read.
145-
;;IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040 The section contains initialized data.
146-
;;IMAGE_SCN_MEM_WRITE 0x80000000 The section can be written to.
147-
145+
;; Section flags:
146+
;; IMAGE_SCN_MEM_SHARED 0x10000000 Can be shared in memory.
147+
;; IMAGE_SCN_MEM_READ 0x40000000 Can be read.
148+
;; IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040 Contains initialized data.
149+
;; IMAGE_SCN_MEM_WRITE 0x80000000 Can be written to.
150+
151+
;; El header ocupo exactamente 0x160 bytes. Lo alineo a 0x200 para que termine o
152+
;; cupando 512 bytes.
148153
HEADER_END:
149-
150-
align 16
154+
align 0x200
151155

152156

153157
;; Entry point prototype:
154158
;; EFI_STATUS main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
155159
;; Argumentos:
156-
;; -- ImageHandle Handle that identifies the loaded image. Type EFI_HANDLE is defin
157-
;; edin the InstallProtocolInterface() function description.
160+
;; -- ImageHandle Handle that identifies the loaded image. Type EFI_HANDLE is de
161+
;; fined in the InstallProtocolInterface() function description.
158162
;; -- SystemTable System Table for this image.
159163
;;
160164
;; Where UEFI ABI specifies:
@@ -170,7 +174,7 @@ align 16
170174
;; Note: EFI, for every supported architecture defines exact ABI.
171175

172176
CODE:
173-
EntryPoint:
177+
EntryPoint: ;; Ubicado en 0x400200 cuando imagen va en 0x400000
174178

175179
;; UEFI entry point args and rerturn address.
176180
mov [EFI_IMAGE_HANDLE], rcx
@@ -202,6 +206,10 @@ EntryPoint:
202206
mov rax, [rax + EFI_SYSTEM_TABLE_CONFIGURATION_TABLE]
203207
mov [CONFIG], rax
204208

209+
mov rax, [EFI_SYSTEM_TABLE]
210+
mov rax, [rax + EFI_SYSTEM_TABLE_CONIN]
211+
mov [TXT_IN_INTERFACE], rax
212+
205213
mov rax, [EFI_SYSTEM_TABLE]
206214
mov rax, [rax + EFI_SYSTEM_TABLE_CONOUT]
207215
mov [TXT_OUT_INTERFACE], rax
@@ -394,7 +402,7 @@ jmp use_GOP
394402

395403

396404
; Set video to desired resolution. By default it is 1024x768 unless EDID was found
397-
use_GOP:
405+
use_GOP: ;; @0x400366
398406

399407

400408

@@ -455,9 +463,9 @@ vid_query:
455463

456464

457465
;; Si llego hasta aqui, he encontrado el modo con resolucion apropiada segun edid
458-
mov rcx, [TXT_OUT_INTERFACE]
459-
lea rdx, [msg_graphics_mode_info_found]
460-
call [rcx + EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_OUTPUTSTRING]
466+
;;mov rcx, [TXT_OUT_INTERFACE]
467+
;;lea rdx, [msg_graphics_mode_info_found]
468+
;;call [rcx + EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_OUTPUTSTRING]
461469

462470

463471

@@ -504,7 +512,7 @@ pop rcx
504512

505513
;; haya econtrado match en un video mode y logrado setearlo, o no, continua (si no pudo, con la resolucion
506514
;; por defecto y posiblemente no este bien configurado el video, podria fallar, pero va a buscar info igual)
507-
get_video:
515+
get_video: ;; @0x40046d
508516

509517

510518

@@ -537,6 +545,31 @@ get_video:
537545

538546

539547

548+
549+
550+
551+
552+
553+
554+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
555+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
556+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
557+
;; mov rcx, [TXT_OUT_INTERFACE]
558+
;; lea rdx, [msg_error]
559+
;; call [rcx + EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_OUTPUTSTRING]
560+
561+
562+
563+
564+
565+
566+
567+
568+
569+
570+
571+
572+
540573
;;;;;;;;;;;;;;;;; imprime info screen en pantalla de el modo seleccionado / valores que quedaron
541574
;; imprime esto:
542575
;; horizResol x vertResol x ppsl x fbSize
@@ -744,7 +777,7 @@ get_memmap:
744777

745778
mov bl, 'U'
746779

747-
jmp 0x8000 ;; Vamos a siguiente loader.
780+
jmp 0x8000 ;; Vamos a siguiente loader. Aprox 0x400702
748781

749782

750783

@@ -858,10 +891,13 @@ num2strWord:
858891
859892
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
860893

861-
times 2048 - $ + $$ db 0
894+
times 3 * 1024 - ($ - $$) db 0
862895
CODE_END:
863896

864-
; Data begins here
897+
;; section .data
898+
;; Cuidado con la posicion de estas tablas, no se pudede cambiar porque por el m
899+
;; omento estan hardcodeadas las posiciones relativas de la misma donde bootload
900+
;; er.asm busca, por ejemplo, ACPI.
865901
DATA:
866902
EFI_IMAGE_HANDLE: dq 0 ; EFI gives this in RCX
867903
EFI_SYSTEM_TABLE: dq 0 ; And this in RDX
@@ -871,6 +907,7 @@ RTS: dq 0 ; Runtime services
871907
CONFIG: dq 0 ; Config Table address
872908
ACPI: dq 0 ; ACPI table address
873909
TXT_OUT_INTERFACE: dq 0 ; Output services
910+
TXT_IN_INTERFACE: dq 0 ; Input services
874911
VIDEO: dq 0 ; Video services
875912
EDID: dq 0
876913
FB: dq 0 ; Frame buffer base address
@@ -944,8 +981,9 @@ PAYLOAD:
944981
align 65536 ; 64KiB para BOOT64.EFI + payload (bootloader + PackedKernel).
945982
RAMDISK:
946983

947-
;; Suficientes 0x00 para obtener un tamano de archivo de 1MB.
948-
times 65535 + 1048576 - $ + $$ db 0
984+
;; Suficientes 0x00 para obtener un tamano de archivo de 1MiB.
985+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;times 65535 + 1048576 + $$ - $ db 0
986+
times 1048576 - ($ - $$) db 0
949987
DATA_END:
950988
END:
951989

@@ -966,6 +1004,24 @@ EFI_NO_MEDIA equ 12
9661004
EFI_MEDIA_CHANGED equ 13
9671005
EFI_NOT_FOUND equ 14
9681006

1007+
;; EFI system table.
1008+
;; typedef struct {
1009+
;; EFI_TABLE_HEADER Hdr; (8 * 3 bytes)
1010+
;; CHAR16 *FirmwareVendor; (8 bytes)
1011+
;; UINT32 FirmwareRevision; (8 bytes)
1012+
;; EFI_HANDLE ConsoleInHandle; (8 bytes)
1013+
;; SIMPLE_INPUT_INTERFACE *ConIn; (8 bytes)
1014+
;; EFI_HANDLE ConsoleOutHandle; (8 bytes)
1015+
;; SIMPLE_TEXT_OUTPUT_INTERFACE *ConOut; (8 bytes)
1016+
;; EFI_HANDLE StandardErrorHandle; (8 bytes)
1017+
;; SIMPLE_TEXT_OUTPUT_INTERFACE *StdErr; (8 bytes)
1018+
;; EFI_RUNTIME_SERVICES *RuntimeServices; (8 bytes)
1019+
;; EFI_BOOT_SERVICES *BootServices; (8 bytes)
1020+
; ; UINTN NumberOfTableEntries; (8 bytes)
1021+
;; EFI_CONFIGURATION_TABLE *ConfigurationTable;
1022+
;; } EFI_SYSTEM_TABLE;
1023+
1024+
EFI_SYSTEM_TABLE_CONIN equ 48
9691025
EFI_SYSTEM_TABLE_CONOUT equ 64
9701026
EFI_SYSTEM_TABLE_RUNTIMESERVICES equ 88
9711027
EFI_SYSTEM_TABLE_BOOTSERVICES equ 96

asm/bootloader.asm

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515

1616
BITS 64
1717
ORG 0x00008000
18-
UEFI_BOOTLOADER_SIZE equ 0x1800 ;; 6KiB
18+
BOOTLOADER_SIZE equ 0x1800 ;; 6KiB
1919

2020
start:
21-
jmp bootmode ; This command will be overwritten with 'NOP's before the AP's are started
21+
jmp bootmode ;; This command will be overwritten with 'NOP's before the AP's are started
2222
nop
23-
;;db 0x36, 0x34 ; '64' marker
2423
db "UEFIBOOT" ;; Simple chequeo de que hay payload.
24+
nop
25+
nop
2526

2627
; =============================================================================
2728
; Code for AP startup
@@ -584,11 +585,11 @@ make_interrupt_gates: ; make gates for the other interrupts
584585

585586
lidt [IDTR64] ; load IDT register
586587

587-
; Patch bootloader AP code ; The AP's will be told to start execution at 0x8000
588+
; Patch bootloader AP code ; The AP's will be told to start execution at 0x8000
588589
mov edi, start ; We need to remove the BSP Jump call to get the AP's
589-
mov eax, 0x90909090 ; to fall through to the AP Init code
590-
stosd
591-
stosd ; Write 8 bytes in total to overwrite the 'far jump' and marker
590+
mov rax, 0x9090909090909090 ; to fall through to the AP Init code
591+
stosq
592+
stosq ; Overwrite the 'far jump' and marker.
592593

593594

594595

@@ -997,11 +998,11 @@ lfb_wc_end:
997998
wbinvd ; Flush Cache
998999

9991000
; Move the trailing binary to its final location
1000-
mov esi, 0x8000+UEFI_BOOTLOADER_SIZE ; Memory offset to end of bootloader.sys
1001+
mov esi, 0x8000+BOOTLOADER_SIZE ; Memory offset to end of bootloader.sys
10011002
10021003
;; esto es la direccion a la cual nuestro kernel se copia, tal como luego comienza ejecutando en _start en 100000
10031004
mov edi, 0x100000 ; Destination address at the 1MiB mark
1004-
mov ecx, ((32768 - UEFI_BOOTLOADER_SIZE) / 8)
1005+
mov ecx, ((32768 - BOOTLOADER_SIZE) / 8)
10051006
rep movsq ; Copy 8 bytes at a time
10061007

10071008
; Visual Debug (4/4)
@@ -1330,7 +1331,7 @@ EOF:
13301331
db 0xDE, 0xAD, 0xC0, 0xDE
13311332

13321333
; Pad to an even KB file
1333-
times UEFI_BOOTLOADER_SIZE-($-$$) db 0x90
1334+
times BOOTLOADER_SIZE-($-$$) db 0x90
13341335

13351336

13361337

asm/init/acpi.asm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ searchingforACPI:
2727

2828
; Find the ACPI RSDP Structure on a UEFI system
2929
foundACPIfromUEFI:
30-
mov rsi, [0x400830] ; TODO This should be passed properly
30+
mov rsi, [0x400000 + 3 * 1024 + 8 * 6] ;; TODO: simbolizar. El 3 es KiB que
31+
;; ocupa seccion de codig o de uefi.
3132
mov rbx, 'RSD PTR ' ; This in the Signature for the ACPI Structure Table (0x2052545020445352)
3233
lodsq ; Signature
3334
cmp rax, rbx ; Verify the Signature

build/bootloader.sys

0 Bytes
Binary file not shown.

build/uefi.elf

1.01 MB
Binary file not shown.

build/uefi.sys

-64 KB
Binary file not shown.

0 commit comments

Comments
 (0)