Implementing bootloader #3

Started by ggnfs000, January 10, 2017, 12:02:14 PM

Previous topic - Next topic

ggnfs000

Implementing bootloader #3

Had a major milestone done with the bootloader. Here is the enhancement I made
- Purpose of enhancement:
* Previously, it was just 1 stage bootloader where bootloader code in MBR loads the subsequent payloads in raw format from BIOS int 13h service. Enhancement made such that it is able to load the payload from FAT32 file system. This way payload (kernel) does not have to be written to disk in rawformat. It needs to be present in c:\ root directory.

- Implementation details regarding enhancement:
* Traversing of FAT32 file system code will not fit into MBR therefore 2nd stage of bootloader is introduced. After much discussion, inspection, the second bootloader code will reside in 1st partition's 10th sector and so on. 1st stage of bootloader will load this from disk. 2nd stage bootloader will search for payload from FAT32 file system and loads.

boot.asm - generates boot.bin
00000h - dos header
00200h - bootloader 1st stage code (must be less than 512-64)
01000h - bootloader 2nd stage code (should be less than 4k, currently less than 1K)

copyraw.asm - generates copyraw.exe.
This is "bootloader installer" executable, which will load boot.bin and writes the 0200-0400h into MBR and writes 1000h-1400h to Partition 1 sector 10, 11.

copyraw.exe and boot.bin is loaded into DOS-bootable vFDD (00h). It also has target vHDD (80h).
Launch the copyraw.exe from DOS on target vMachine and it will load boot.bin and installs it into vHDD.

This is all tested to be working correctly. For the next phase, I booted that same vMachine after installing the bootloader and this time booted from vHDD.

From screen, I am able to see the 1st stage bootloader messages only. This means something has gone wrong with 2nd stage bootloader loading. Time to inspect.

7.13.2016.
After finding several bugs, in the copyraw.asm and boot.asm finally, I was able to get 2nd stage of bootloader loaded and running.


Overall layout:


The bugs I fixed were, the jump statement changed to 0000:8000h for jumping to 2nd stage of bootloader. 
db eah, 00, 80h, 00h, 00h.
There were couple of coding errors for obtaining the 10th sector No. of partition 1 where the 2nd stage of bootlaoder is written using copyraw.exe. 

Second stage of bootloader code in the boot.asm is offset at 1000h from beginning of code segment with following statement:
org 1000h-200h (the minus 200h accounts for DOS header).

If there is any printing of string within 2nd stage bootloader code, then the string had to be better moved to 1st stage of bootloader code. This way, I can correcly load offset of the string into SI register same way as in 1st stage of bootloader. If the string is located in the 2nd stage of bootloader the LEA instruction seem to load SI in respect to org statement. In my case, it was SI=0e00h. 

Eventually, I have defined macro for printing any string within the bootloader code:



7.16.2016. 
Before testing the rest of the 2nd stage bootloader, i decided to review the code again and found many bugs which I fixed. This apparently introduced some error as I can no longer see the very first string printed upon jumping to 2nd stage bootloader code. It turns out that the MBR size overblew as 1st stage bootloader coded exceeded 512-64 size. Once fixed that issue here is the resulting screen (below). Apparently, the 2nd stage bootloader code did not find the expmini.exe file it was searching from root directory. So time to debug! 


Here are the some information extracted from target boot HDD using the same method I did before:
1. From MBR, the 1st partition's 1st sector No.: 80h (I call it P1S1 from now on). Partition size 1fe800 which translates to 1GB.
2. From P1S1:
- Reserved sector count: 101ah.
- FAT size: 2000h.
- First data sector = Reserved sector count + 2 FAT size -> 2000h.
But remember this is relative to beginning of partition, to get absolute partition No. we also add 1st sector No. of partition 1:
2000h + 80h: 2080h
3. Once I dump the sector 2080h, here the resulting, root directory content dump:




















Source: Implementing bootloader #3