Jump to content

Problem reading from disk

- - - - -

  • Please log in to reply
2 replies to this topic

#1
blackjackk

blackjackk

    Newbie

  • Members
  • Pip
  • 1 posts
Hi ! I`m trying to write an OS, but I already have some problems with the bootloader. I`m trying to load some assembly code from hard disk, that will set protected mode and load kernel, but i don`t think it loads what it should load. After it calls the int 13h and jumps to the address specified it does nothing and doesn`t even return.
I am using vmware virtual machine. Can anyone give me some advice how i can verify what it loads into the memory? I also posted some code. Maybe there is some problem with the coding.
I compiled the asm file with: nasm -f aout -o x.bin x.asm ;gcc -c -o c.o c.c ; ld -T link.ld -o pmk.com x.o c.o ; and wrote it to the third sector (x00000002-x000) with Runtime`s DiskExplorer.

Here`s the code that should load it:

[BITS 16]

[ORG 0]


jmp start


............


start:

mov ax, 0x7C0

mov ds, ax


mov [bootdrv], dl


............


read:

mov ax, 0x1000

mov es, ax

xor bx, bx


mov ax, 0x0201

mov ch,	0

mov cl,	3

mov dh,	0

mov dl, bootdrv

int 13h


jc read


jmp 1000h:0000h


.............



#2
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 402 posts
Interrupts are originally from real mode and only simulated for protected mode some what. Following link might be useful

Using Interrupts in Protected Mode
Today is the first day of the rest of my life

#3
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,252 posts
  • Location:C:\Countries\US

Quote

... Can anyone give me some advice how i can verify what it loads into the memory? ...
You can check to make sure it loaded stage 2 by using a "stage 2 identifier," or something like that. I mean, the first two bytes of stage 2 can be an identifier that you check for before jumping to the stage 2 code; but note, though, that you would then have to jump to <stage 2 address> + 2, instead of <stage 2 address>, because of the 2-byte identifier. You could use a different size for the identifier, if you want, but 2 is what I used.

I did something like this, in my boot sector code:
ORG 0x7C00 

USE16 


jmp 0x00:start         ;; The BIOS might either load this sector to 0x07C0:0x0000 or to 0x0000:0x7C00, but we want it to be a particular way, so we force 0x0000:0x7C00. 


%define    STAGE2_OFFSET              0x8000                    ;; Where, in memory, stage 2 is supposed to be. 


%define    STAGE2_LOAD_SEGMENT        0x0800              ;; The segment we load stage 2 to. 

%define    STAGE2_LOAD_OFFSET         0x0000               ;; The offset we load stage 2 to. 


;; The disk geometry for a 1.44 MB (1440 KB) floppy disk: 

%define    FLOPPY_HEADS               2 

%define    FLOPPY_TRACKS              80 

%define    FLOPPY_SECTORS             18 

%define    FLOPPY_BYTES               512 


%define    STAGE2_LOADS               7         ;; This means we want to load 7 tracks, besides the first track, from the boot disk. 


boot_drive: 

dw 0x00          ;; We initialize this to 0; we change this a little later. 


start: 


;; Set things up. 

cli 

xor ax, ax 

mov es, ax 

mov ds, ax 

mov ss, ax 

mov sp, 0x7BE0 

sti 


;; Save the drive number of the boot drive (you don't have to first copy it to AL; it's just I like it better that way). 

mov al, dl 

mov word [boot_drive], ax 


;; Try to enable the A20 line using the BIOS service. 

mov ax, 0x2401 

int 0x15 

jnc .load_stage2 


;; In case the BIOS service does not work, do it using I/O ports. 

in al, 0x92 

or al, 0x02 

out 0x92, al 


;; Check if A20 is enabled. 

call .check_a20 

cmp al, 0 

jz .load_stage2 


.hang: 

call b800 

mov byte [es:bx+00], "h" 

mov byte [es:bx+02], "h" 

hlt 

jmp .hang 


.check_a20: 

	;; The idea, here, is to write to a memory location and check another memory location if the same value is there; if so, A20 is not enabled. 

	;; I mean, if we have only 2^16 (16-bit address) address space, then 0xFFFF:0x0510 would evaluate to physical address 0x0500; if, however, 

	;; the A20 line is enabled, then we would have 2^20 address space, so 0xFFFF:0x0510 would evaluate to some other physical address. 

	;; So if we write a value to 0x0500 and compare that value to the value at 0xFFFF:0x0510, then the result we get should tell us whether 

	;; A20 is enabled or disabled. 

	

	;; Note, though, that we don't use an address that is less than 0x0500, because the first 0x500 bytes of memory are 

	;; used by the BIOS and by the computer for important data that we don't want to overwrite; free memory starts 

	;; at 0x0500. 

	

	;; ... some code that checks to see if A20 is enabled ...  

	

ret 


.load_stage2: 

	

	;; ... some code ...  

	

	mov al, FLOPPY_SECTORS 

	int 0x13 

	jc .load_stage2_err 

	

	ret 

.load_stage2_err: 

	;; Output "2e" (stage-2 load Error) 

	call b800 

	mov byte [es:bx+04], "2" 

	mov byte [es:bx+06], "e" 

	jmp .hang 

.load_stage2_inv: 

	;; Output "IS" (Invalid Stage2) 

	call b800 

	mov byte [es:bx+04], "i" 

	mov byte [es:bx+06], "s" 

	jmp .hang 

.load_stage2_finish: 


;; Here we compare the first two bytes of what we loaded to the stage 2 signature, to make sure that it's really stage 2. 

mov bx, STAGE2_OFFSET 

cmp byte [bx+00], "S" 

jnz .load_stage2_inv 

cmp byte [bx+01], "2" 

jnz .load_stage2_inv 


;; Okay, the stuff we loaded passes the "stage 2 test." 


xor ax, ax 

mov ds, ax 

mov es, ax 


jmp 0x00:STAGE2_OFFSET + 2 


b800: 

mov ax, 0xB800 

mov es, ax 

xor bx, bx 

ret 


TIMES  510 - ($ - $$)  DB 0x00 

DW 0xAA55 

And something like this for the stage 2 code:
ORG 0x8000 

USE16 


db "S2"            ;; Stage 1 will check for this file identifier, so we want to include it, to tell stage 1 that this is a valid stage 2 start. 


start: 


;; .....  and the rest of the code  .....  

EDIT: Later on, you might need to use ATA or ATAPI or ...:
ATA PIO Mode - OSDev Wiki
Category:ATA - OSDev Wiki
ATAPI - OSDev Wiki




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users