flat assembler
Message board for the users of flat assembler.

Index > OS Construction > How does bootloader call kernel stub?

Goto page Previous  1, 2
Thread Post new topic Reply to topic

Joined: 09 Nov 2018
Posts: 57
bzt 08 Nov 2022, 23:34
FlierMate wrote:
I need the "extrn" label so that it can link to the entry point of my Pascal program.
That's a very bad idea. This means you have to recompile and reinstall the boot loader if your kernel changes. Instead I recommend to parse the kernel file and find the entry point address dynamically.

FlierMate wrote:
Since FASM generates binary image directly, I end up using NASM temporarily for this project, and it has been done, a "hello world" OS kernel written in Pascal!
You should switch back to FASM, it's much better than NASM.

You can also use my boot loader, BOOTBOOT, which is written with FASM and can load Pascal kernels out-of-the-box (not just on BIOS systems, but on UEFI as well, and works as a GRUB module too. There's also an example "Hello World" kernel in Pascal that you can use as a skeleton, tested with the FreePascal Compiler). This loader has several advantages, like setting up long mode, paging, graphical screen for you, able to load your kernel from a FAT partition, initializes multicore and runs your kernel on all cores in parallel etc., but this might be a bit overkill for you.

If you want to write your own boot sector, then I'd strongly recommend to take a look at imgrecv, which is also written in FASM, but much much simpler (one sector, 512 bytes only). I wrote it for a special use case, that's why it loads the kernel from the RS/232 port instead of disk (line 126 to 158, replace that with a disk read BIOS call), but otherwise it might be very useful to you.

This imgrecv boot sector loads the kernel at address LOAD_ADDR (which is defined as 1M), then it sets up protected mode, paging tables, enables A20, disables PIC, etc. and checks the loaded kernel's header. If it's a raw image, then the code simply jumps to the first byte of the kernel, at 1M. But if it's an ELF executable, then it checks if it's 64-bit, and if so, it sets up long mode too. In both cases (ELF32 and ELF64) it parses the ELF header to get the entry point (line 165, so there's no need for "extrn", as the code gets the address from the ELF header dynamically) and jumps to that. The advantage here is, you can compile your kernel in pure protmode or longmode, there's no need for 16-bit trampoline code, as the loader dynamically sets up the environment for your kernel.

If your Pascal compiler outputs PE (Windows' default), then the method is the same, it's just you can find the entry address at a different offset. Example PE header parser in FASM.

The point is, do not use "extrn" and link your loader with your kernel, instead write your loader in a way that parses the kernel's header (either ELF or PE) in run-time. This way you can replace the kernel without touching the loader.

Hope this helps,
Post 08 Nov 2022, 23:34
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum

Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.