After disk extension check is passed, we load a new file called loader in the memory.
The reason we need a loader file is that the MBR is fixed size which is 512 bytes.
There are spaces reserved for other use such as partition entries
entries which leaves us less than 512 bytes for the boot code.
The tasks that we should do in the boot process includes load kernel file,
get memory map, switch to protected mode and then jump to long mode.
Doing all these tasks requires the boot code larger than 512 bytes.
So here we introduce a new file loader file to do all these things.
The loader file has no 512 bytes limits.
This is the memory map when we load the loader file.
The boot code is loaded by BIOS in the memory address 7c00.
The size of the boot code is 512 bytes which is 200 in hexadecimal.
So here we simply load the loader file into the location right after the boot code
which is at the location 7e00.
Ok back to our project.
As we did before, we define a label load loader.
The disk service we use is interrupt 13.
The function code is
42 saved in ah
which means we want to use disk extension service.
And also, don’t forget to save the drive id to dl register before we call the service.
Notice that the drive id has been saved in value drive id.
The parameter we pass to the service is actually a structure. Let's define the strcuture,
we call it read packet
the size of the structure is 16 bytes.
we mov the address of read packet to si register
Now si holds the address of read packet.
The first word holds the value of structure length.
So we move 16
or 10 in hexadecimal to it, because readpacket is 16 bytes.
The second word is the number of sectors we want to read.
Since the loader in this course is a small file, we simply read 5 sectors which is enough space for the loader.
The next two words specify the memory location into which we want to read our file.
As we have talk about, we load the loader file into the memory address 7e00.
So we save 7e00 in the first word
which is the offset. The second word holds the value of segment part of the address.
Here we simply set it to 0.
So the logical address is 0 7e00 and the physical address it points to is 7e00.
The last two words are the 64-bit logical block address.
The loader file will be written into the second sector of the disk. Therefore, we use lba 1.
Remember logical block address is zero-based address. Meaning that the first sector is sector 0, the second sector is sector 1 and so on.
So we save 1 to the lower half of the 64-bit address.
The higher part is simply set to 0.
Now with all the parameters prepared, we call interrupt 13.
If it fails to read sectors, the carry flag is set. So we use jc instruction
and jump to label read error.
What we are going to do next is change the print message part so that it prints error message on the screen.
Remove the label print message
and place read error and not support.
If disk extension service is not supported or read error occurs, we print read error in boot process.
we change the message to
we have an error in boot process.
Ok we have finished handling the error case.
When we successfully read the loader file into memory, the next thing we are going to do is we are going to jmp to the start of loader
and don’t forget to pass the drive id to the loader, because we need to loader kernel file using drive id.
We simply pass drive id to dl register.
and jump to 0x7e00
which is the address of memory into which we load our loader from disk.
Next thing we are going to do is write loader file. We create a new assembly file.
called loader file.
Just like the boot file, we specify the code is running in 16-bit mode
and the loader is supposed to be running at address 7e00.
Then we define the label start
indicating that this is the start of the loader. As for the end part, we use infinite loop to halt the processor as we did in the boot file.
In this lecture, we only demonstrate how to load loader file. So we just print message on the screen. The print function is the same as the one in boot file. So we copy and paste it here
the message we want to print is loader starts for example .
When the loader is running, it should print loader starts.Now let’s build this project.
Now let’s build this project. We save these two files.
Since we have loader file, we add the file in the build script.
The output file is also binary file
the name of the file is called loader.bin.
Once we generate the loader file, we need to write it into boot image
When we load the loader file using bios serivce, we read 5 sectors of data in memory.
So here we sepcify count to 5
to write 5 sectors of data in boot image.
The first sector of boot image stores boot.bin, and we write loader from the second sector.
So here we use seek
which means skip blocks at the beginning of the output.
So 1 means we skip the first block.
ok let's open the terminal,
and run the script.
The message shows that the two files have been written into the boot image.
With the boot image is ready to load, let's test it in virtual machine first.
As you see, Loader starts is printed.
Here shows the message loader starts on the real machine.
That's it for this lecture, see you in the next video.