Page 1 of 1

Problems with loading files from disk

Posted: Mon Oct 18, 2004 12:45 pm
by Jonathan
Hi!

I am trying to load my kernel from a floppy disk via my bootsector. The problem is I don't have any floppy drive so I would like to use an image created with dd and load it with BOCHS.

I created the floppy image with:
[pre]
dd if=/dev/zero of=fd.img obs=512 count=2880
[/pre]

After that I mount it on loop1 with losetup.
Then I have my bootsector (boot) which is exactly 512 byte large.

If I do:
[pre]
dd if=boot of=/dev/loop1
[/pre]

It runs the bootsector fine I have verifyed that this works. But Now I have my second image I want to be loaded into memory, I call this the stub.
The Stub is exactly 512 bytes large too so it will fit within one sector. I want the stub to be located on sector 1 that is the sector after the bootsector, so how do I get it there?

The bootsector:

Code: Select all

;; Basic Bootloader by Jonathan W ?2004

[BITS 16]
[ORG 0x7C00]   
      
main:
   mov ax,0x0000      ; Set up the registers
   mov ds, ax         ; Set up the registers
   mov si, Status_1   ; Print the first status message
   call printstr
   call read_stub
   mov ax, 0x2000
   mov ds, ax
   jmp 0x2000:0x0000   ; Jump to the loaded stub
   

printstr:
   mov ah, 0x0E      ; This is the basic teletype mode
   mov bh, 0x00      ; Page number
   mov bl, 0x07      ; Text attributes
.START_PRINTSTR
   lodsb
   cmp al, 0
   je .END_PRINTSTR   ; End the loop if EOS
   int 0x10      ; Call BIOS Video Interrupt
   jmp .START_PRINTSTR   ; Loop again
.END_PRINTSTR      
   ret

read_stub:
   mov di, 5       ; We need to try the loop atleast 5 times before failing
   mov bx, 0x2000      ; Segment to load stub into
   mov es, bx
   mov bx, 0x0000
.START_RSTUB
   mov ah, 0x00      ; Reset AH
   mov ah, 0x02      ; We want to read from disk
   mov al, 0x01      ; Read 1 sector
   mov ch, 0x01      ; Track 0
   mov cl, 0x02      ; Sector 1
   mov dh, 0x01      ; Head 1
   mov dl, 0x00      ; Drive 0 This is temporary!
   int 0x13      ; Call the BIOS
   jnc .END_RSTUB      ; If CF = 0 then we are done
   dec di
   cmp di, 0
   je .READ_ERR      ; Show error message
   jmp .START_RSTUB   ; Try again
.READ_ERR
   mov si, Read_error   ; Move error message into SI
   call printstr
.END_RSTUB
   ret

Status_1 db 'Now loading stage 1....Jumping into stub',13,10,0
Read_error db 'Error reading stub, please check your drive',13,10,0

times 510-($-$$) db 0
dw 0xAA55
And my stub looks like this now, this is just a temporary file ;)

Code: Select all

;; This is the stub which will take us into protected mode 
;; and load our kernel of the disk this is limited to 1024kb-512byte
[BITS 16]
[ORG 0x2000]

main:
   mov si, Test_str
   call test_msg      ; Print a test message
   cli         ; Disable interrupts
   hlt         ; Halt the CPU

test_msg:
   mov ah, 0x0E      ; This is the basic teletype mode
   mov bh, 0x00      ; Page number
   mov bl, 0x07      ; Text attribute
.START_TMSG
   lodsb
   cmp al, 0
   je .END_TMSG
   int 0x10
   jmp .START_TMSG
.END_TMSG
   ret


Test_str db 'Loaded!',13,10,0

times 512-($-$$) db 0

Re:Problems with loading files from disk

Posted: Mon Oct 18, 2004 12:56 pm
by Curufir
dd if=/dev/null of=fd.img bs=512 count=2880

dd if=boot of=fd.img bs=512 conv=notrunc

dd if=boot2 of=fd.img bs=512 seek=1 conv=notrunc

Re:Problems with loading files from disk

Posted: Mon Oct 18, 2004 2:10 pm
by Jonathan
Curufir wrote: dd if=/dev/null of=fd.img bs=512 count=2880

dd if=boot of=fd.img bs=512 conv=notrunc

dd if=boot2 of=fd.img bs=512 seek=1 conv=notrunc
Thank you, I tried that now I didn't get my code to work at first but later I fixed som minor errors att changed the [ORG] in the stub program to nothing and then it worked like a charm.