Read data from disk
By cedrou on Friday, December 26 2008, 11:21 - Boot - Permalink
This post is related to rev.4.
In the previous post, we have created a small program that boots when the computer is switched on. The only thing it does is to write a welcome message on the screen. Remember that this small bootloader was put in memory by the BIOS and that only 512 bytes were read from the boot device. This is a really tiny space to write a full OS ! Therefore, we will have to write some code that reads more data from data and puts it in memory.
To do that, we will invoke the BIOS interrupt 13h with several parameters passed through CPU registers.
You could get the full source here at Google Code.
The first thing to do is to create a new sector of data on the disk. This is easily done by adding some code after the AA55
signature which mark the end of the first sector. We simply move the code that prints the message, along with the infinite loop and the string data that is printed. This new code will be located in the second sector of the boot disk.
We now return to the main procedure, the entry point. We'll modify it in such a way it will be able to read the second sector of the boot device and to place it in memory just after the current code.
We start by reset the drive, in order to put it in a known configuration:
mov ax, 00h ; Reset the floppy drive mov dl, [boot_drive] ; Set the drive int 13h
Then, we ask the BIOS to read the second sector of the boot drive and to place it at address 07C0:new_sector
, just after the current memory sector:
mov ax, cs ; write into 07C0:new_sector mov es, ax ; mov bx, new_sector ; mov ah, 02h ; Read sector from drive mov al, 1 ; Load 1 sector mov ch, 0 ; Cylinder=0 mov cl, 2 ; Sector=2 mov dh, 0 ; Head=0 mov dl, [boot_drive] ; Bootable drive int 13h
Cylinder/Head/Sector is a way to localize data chunks on a drive (see Wikipedia for more information).
The code is now placed in memory just after the current sector. All we have to do is to jump into this new code:
jmp new_sector ; Jump to the program