Loading/Running Programs | UEFI Dev (in C)

Channel:
Subscribers:
5,840
Published on ● Video Link: https://www.youtube.com/watch?v=HzoX3Fr7a5E



Duration: 2:21:41
1,683 views
59


Running a program instead of just printing text files.
A program or executable file has a format like any other type of file, and in C it's a
relatively simple matter of loading data structures to match the format, loading needed
sections of the program to a new buffer, getting the new entry point, and calling that
entry point to run the program.

A PE file can be slightly easier to load than an ELF file as the PE format has fields for the
total size of the loaded file and its sections, and the entry point and section virtual addresses are already relative and do not need to be computed, for a PIE executable.

Errata:
- It's probably a bug loading the ELF program headers' p_memsz amount of data instead of p_filesz.
It's possible that filesz is non-zero while memsz is 0 or at least less than filesz.
Using filesz instead would more closely match the PE loading and would fix any ambiguity and future bugs. The difference in filesz and memsz is implicitly zero padded anyway from memset-ing the
whole buffer to 0.

- Random output from printf() was usually from taking 4+ bytes from the argument's memory on the stack via va_arg() inside printf, when the argument is actually 8 or 16 bits. My guess is due to compiler optimizations or otherwise not using address/size prefix bytes in the compiled code.
A fix could be to add size or length specifiers in the formatted string to be parsed by printf, e.g. %xbits8 or %xz8 for 8 bits. Or look up how gcc and others work around those issues.

Notes:
- Sorry about any flashbangs, could've used a darker color for drawing to the framebuffer.

- Probably could have used (elf header + sizeof elf_header_var) or (input_buffer + elf_header_phoff) to get to the program headers instead, which may make a little more sense for the ELF layout as the program headers immediately follow the overall Elf header.

- I also kept thinking the default text line length was 25 characters instead of 80.
The senility's kicking in early these days.

Links:
https://uefi.org/specifications (uefi & other specifications)
https://en.wikipedia.org/wiki/Executable_and_Linkable_Format
https://en.wikipedia.org/wiki/Portable_Executable
https://learn.microsoft.com/en-us/windows/win32/debug/pe-format
https://github.com/mentebinaria/readpe (readelf-esque tool for PE files)

UEFI Programming playlist:
https://www.youtube.com/playlist?list=PLT7NbkyNWaqZYHNLtOZ1MNxOt8myP5K0p

Git Repo:
https://github.com/queso-fuego/uefi-dev

Repo state at the start of this video:
git clone --recurse-submodules https://github.com/queso-fuego/uefi-dev
cd uefi-dev
git checkout 20ff7f39ba9ca2aade5ffce975b9ff08ee8e2044

Repo state at the end of this video:
git checkout 1de80a5fba694550cbb8095d415a0d8f087bf6b7

Next video:
- Getting the memory map and exiting boot services, and maybe making an installer option to write the disk image and kernel to another disk. That way you could "install" everything and reboot to use the new OS or loaded program.

Join the Community Discord: https://discord.gg/yKm4T89QFn

Contact:
https://queso_fuego.srht.site/contact.html

- Let me know if there's anything specific you'd like to see!

Questions about setup/software/etc.?
Check the FAQ: https://queso_fuego.srht.site/about.html

0:00:00 intro, add example program to draw to screen
0:14:45 change load file function to load flat binary program
0:28:07 close timer event before running program
0:29:28 "fix" void pointer to function pointer cast
0:31:13 load flat binary from PE obj instead of ELF
0:34:32 build elf & pe from makefile, check header bytes
0:44:17 ELF structs for basic ELF64 format
0:50:30 print info and load ELF file (copy paste my own code)
1:25:18 PE structs and info for basic PE32+ format
1:43:09 print info and load PE file
2:19:15 coming up & outro

Music credits:
"Acid Trumpet" Kevin MacLeod (incompetech.com)
Licensed under Creative Commons: By Attribution 4.0 License
http://creativecommons.org/licenses/by/4.0/

[Chiptune] Medieval: Minstrel Dance by RandomMind
Music: https://www.chosic.com/free-music/all/

#uefidev #cprogramming #PIEhole







Tags:
queso fuego
run programs
load file
load program
elf
pe
elf64
file format
exe
uefi programming
uefi dev
low level
c programming
elf format
pe format
64 bit