Now, I changed the kernel code to a 16-bit code, so I can execute it in real mode and check if the kernel binary is being loaded from disk, below the 1 MB mark. I am trying to load it to 0x3000:0x0. The only thing this does now is print "Kernel Loaded". Then , as before I burnt this to sector 3 of USB with dd.
Sector 1 -> Stage 1 , Sector 2 -> Stage 2 , Sector 3 -> Kernel (or rather just a print routine).
So this is how it
should work:
1. Stage 1 prints "Loading Stage 2", then loads Stage 2 from Sector 2, to physical address 0x500 (0x50:0x0), and then jumps to it.
2. Stage 2 prints "Loading OS", then loads Kernel from Sector 3, to physical address 0x30000 (0x3000:0x0), and then jumps to it. (No copying to 1 MB).
This I am doing just to check if the kernel binary indeed get's loaded from disk at 0x3000:0x0 or not.
3. Kernel prints "Kernel loaded". This is a 16-bit code, at address 0x3000:0x0.
But this is what is happening:
As before, burnt the code (3 sectors) to USB using dd, and booted the Intel PC with it. Again , it prints only the first 2 strings. "Loading Stage 2", followed by "Loading OS".
Now, I had expected this to work, since I cut off all the A20, protected mode parts and just running the kernel in real mode at an address below 1 MB. But this also failed.
So the only possible point of failure I can see is loading sector 3 from the disk (can't expect the jmp instruction to 0x3000:0x0, to go wrong). I took it for granted that sector 3 will get loaded , because sector 2 (stage 2) is being correctly loaded and executed by stage 1. I can't understand why loading sector 3 can fail though. I am using the same method to load sector 3 that I used for sector 2. (CHS based int 0x13).
I'll try doing this by LBA now (int 0x13 extended read). I need a little help with the disk geometry though. I don't know how to calculate the Heads Per Cylinder (HPC) that is required by LBA method. I ran fdisk on my USB device. This is what fdisk has to say about the geometry:
Code: Select all
Disk /dev/sdb: 4005 MB, 4005560320 bytes
124 heads, 62 sectors/track, 1017 cylinders
Units = cylinders of 7688 * 512 = 3936256 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
How do I calculate HPC from this? Could not find any reference to this on the internet. LBA wikipedia page says HPC is typically 16, but can I bank on this?