Message board for the users of flat assembler.
> OS Construction > How does bootloader call kernel stub?
Goto page Previous 1, 2
bzt 08 Nov 2022, 23:34
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.
I need the "extrn" label so that it can link to the entry point of my Pascal program.
You should switch back to FASM, it's much better than NASM.
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 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,
|08 Nov 2022, 23:34||
|Goto page Previous 1, 2
< Last Thread | Next Thread >
Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.
Website powered by rwasa.