Onelio wrote:the first stage just has to load the file and enable the a20 to load it into 0x100.000
I'm not sure what that address supposed to be, but if A20 needs to be enabled for it, then I'm pretty sure you can't load there using BIOS. If it's conventional memory, then why not load at the lowest address possible so that you can utilize more of the memory below 640k?
Onelio wrote:I will relay the Protected Mode setup to the second stage
Why don't you enable the A20 there then? Enabling A20 is only relevant for protected mode because you simply can't really address memory above 1M in real mode.
Onello wrote:After a lot of searching and testing I found out that the problem is with the enabling of the A20 line here
Like Octocontrabass said, I'm not sure either that the problem is this function. However it worth nothing that not all PC supports the fast A20 method, but all BIOS should support the enable A20 function (this is specially true for VMs, on a real hardware you'd probably have the fast method anyway). Nonetheless, if fast method is not supported, there should be no "Trying to execute code outside RAM" error, of that I'm sure. I'd suggest to add "-s -S" to qemu's command line and connect gdb to the vm to debug the issue.
Onello wrote:I came here to ask for guidance and maybe advice.
My first advice would be to use
fasm instead of nasm, because it's much much better. But this is somewhat a personal preference thing.
For guidance I can give you these:
imgrecv.asm has a boot sector that loads the kernel over serial (not important for you). What is important, it sets up protected mode, and if the kernel is an ELF64, then long mode too in the boot sector. Everything implemented in a single sector, no 2nd stage. You can replace the code loading the kernel over serial in lines 122-155 with a code using LBA calls. If you're not interested in 64 bit, protected mode is enough for you, then you can remove lines 169-208.
bootboot 1st stage is a boot sector that is capable of loading a 2nd stage using LBA packets. It supports CDROMs too (with 2048 bytes sectors instead of 512 bytes), and RAID mirrors.
bootboot 2nd stage can be loaded in many ways (Grub, BBS ROM, as Linux kernel etc., not relevant) and also by it's 1st stage boot sector (line 258). It properly detects if the CPU can do protected mode (line 300), and it has an example how to load sectors above the 1MB mark (line 882, hint: it uses a temp buffer in low memory which can be accessed by real mode BIOS, and then in protmode it copies the sector to its final position in memory). It also has a proper A20 enabling sequence (line 364). It reads the GPT, locates ESP (line 1034), and then it reads a file from that partition regardless if it's FAT16 or FAT32 (line 1112, about 99% of the code is shared between FAT16 and FAT32).
alexfru's boot16.asm is a pretty neat boot sector that loads the 2nd stage from a FAT16 file (I believe this might very well be exactly what you need).
I hope these might help.
Cheers,
bzt