NASM Forum > Using NASM

Compiling EFI file from ASM code

<< < (2/2)

JD9999:
Thanks Frank!

I found the command from BrianOtto's script that supposedly converts the .obj to .efi (using Visual Studio Link), but it hasn't worked. Instead, it's given me the error output you can see in the attached image file. Interestingly, it is Visual Studio that is unhappy, but NASM seems to be fine.

You're right about the PE header. It could be that there's an offset issue because I don't have the PE header. At the moment I'm looking at these articles to try to make one:
https://wiki.osdev.org/PE <-- particularly helpful
https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#general-concepts
https://tech-zealots.com/malware-analysis/pe-portable-executable-structure-malware-analysis-part-2/
https://blog.kowalczyk.info/articles/pefileformat.html

I'll try it sometime in the next couple of days and let you know the result!

I'm trying to make it so it runs on a real machine (e.g. my 8th gen i7), so I don't want to use an IDE or VHS. once I have an EFI file, I know I can put it in a folder on my USB which *should* make it bootable. That's what I'm aiming for.

Thanks,
Jamie

fredericopissarra:
As for the linker, I prefer to use GCC's ld and objcopy (as in osdev article). On Linux, you can compile with nasm to elf64 format and link in the same format, using objcopy you get rid of unecessary sections and convert your binary to PE+ format using --target=efi-app_x86_64 option:


--- Code: ---; boot.asm
  bits  64
  default rel   ; x86_64 default effective address calculations should be always RIP relative.
 
  section .text

  ; ..start is the default entry point for ld.
  global _start
_start:

  mov   rcx,[rdx+64]    ; Get SysInfo ptr.
  lea   rdx,[hello]     ; Argument for Print.
  call  [rcx+8]         ; Call Service.

  ; Here you should call ExitBoot?!
  ret

  section .rodata

  ; Strings are always in UTF16 for EFI.
hello:
  db __utf16__ `hello, world!\r\n\0`
--- End code ---


--- Code: ---$ nasm -f elf64 -o boot.o boot.asm
$ ld -znocombreloc -shared boot.o -o boot.so
$ objcopy -j .text -j .data -j .rodata -j .bss -j .dynamic -j .dynsym -j .rel -j .rela -j .reloc \
   --target=efi-app-x86_64 -o boot.so bootx64.efi
--- End code ---

Use objdump -x to see all the sections available on your boot.so file (we don't have relocations and .bss on this example, so copy of .bss, .rel, .rela and .reloc are not necessary).

JD9999:
Hey everybody,

I recently found another attempt at a NASM UEFI here: https://github.com/charlesap/nasm-uefi

This one compiles correctly and can run (a little bit) on both QEMU as a .iso and on my USB drive.

From this I have discovered that compiling a .efi is the same method as compiling a .o file.

There are, however, three caveats:

* The program has an error. It displays "here we go" and then it displays "fail". There are six ways the program can fail, and QEMU doesn't show any text when it runs at all. I guess I would need to write more strings to work out where it fails.
* It joins two files together using dd. Even though there is a dd for windows which works properly (I'm using the one at http://www.chrysocome.net/), dd is not a windows tool by default, so I would like to find a Windows way of doing it. The two files combining is also a bit confusing. For example, although the shoe-x64.asm file contains the DOS + PE header, the hexdump of both shoe-x64.efi and toe-x64.efi does not contain this DOS header. It's only when the dd command is run, and the files are combined, that the DOS header suddenly appears. I would love to know why this happens.
* Again, the program does not seem to have a DOS stub at all, until the two files are combined using dd.
I'm much closer to a UEFI OS now, but I don't understand how nasm compiles the efi header without the MZ header present in the hex dump (of the shoe-x64.efi).

If anyone has any pointers or suggestions to help me understand this, I would be very grateful.

Thanks,
Jamie

JD9999:
I've done it (with much learning from other implementations)!

Have a look at my example code here (https://forum.nasm.us/index.php?topic=2786.0) if you're interested.

Thanks,
Jamie

Frank Kotler:
Thank you !

Best,
Frank

Navigation

[0] Message Index

[*] Previous page

Go to full version