Loading more than 54 sectors [SOLVED]

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
yolda
Posts: 13
Joined: Wed May 27, 2009 12:56 pm

Loading more than 54 sectors [SOLVED]

Post by yolda »

Hi all, i know my last post wasnt successful, and now i face a new challenge, i cant seem to load more than 54 sectors with my bootloader :(
the code is simple i tried to read 1 sector at a time, but everytime i get to read sector # 54 it hangs up.
it doesnt fail in the error message control loop, but on the load loop when i load 57 secotors, 52 are ok tho

Code: Select all

[BITS 16]       ; We need 16-bit intructions for Real mode

[ORG 0x7C00]    ; The BIOS loads the boot sector into memory location 0x7C00

	jmp 0x0000:start
start:

	 xor ax, ax
	 mov ds, ax
	 mov es, ax
	 mov ss, ax
	 mov sp, ax

	 mov [bootdrive], dl ; boot drive stored by BIOS in DL.
	 
	 mov di, 0FFh ;tries before an error
reset_drive:
	dec di
	mov ax, di
	cmp ax, 0
	je errorMSG      ;if all tries failed print error
	
	mov ah, 0               ; RESET-command
        int 13h                 ; Call interrupt 13h

	or ah, ah
	jnz reset_drive


        mov ax, 0
        mov es, ax
	push di
	mov di, 0x1000        ;where in memory to load
	mov si, 0
loadLoop:
        mov bx, di          ; Destination address = 0000:1000, increments per sector
        mov ah, 02h             ; READ SECTOR-command
        mov al, 1              ; Number of sectors to read = 1
        mov ch, 0               ; Cylinder = 0
        mov cl, 2             ; Sector = 2
	add cx, si
        mov dh, 0             ; Head = 0
	mov dl, [bootdrive]

        int 13h                 ; Call interrupt 13h
        or ah, ah               ; Check for error code
        jnz reset_drive         ; Try again if ah != 0

	add di, 0x0200       ;next place in memory to load the next sector
	inc si
	cmp si, 57
	jne loadLoop
Last edited by yolda on Sat Jun 20, 2009 4:12 pm, edited 1 time in total.
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

Re: Loading more than 54 sectors

Post by VolTeK »

interesting, nice work
User avatar
kop99
Member
Member
Posts: 120
Joined: Fri May 15, 2009 2:58 am

Re: Loading more than 54 sectors

Post by kop99 »

Code: Select all

loadLoop:
        mov bx, di          ; Destination address = 0000:1000, increments per sector
        mov ah, 02h             ; READ SECTOR-command
        mov al, 1              ; Number of sectors to read = 1
        mov ch, 0               ; Cylinder = 0
        mov cl, 2             ; Sector = 2
   add cx, si
        mov dh, 0             ; Head = 0
   mov dl, [bootdrive]

        int 13h                 ; Call interrupt 13h
        or ah, ah               ; Check for error code
        jnz reset_drive         ; Try again if ah != 0

   add di, 0x0200       ;next place in memory to load the next sector
   inc si
   cmp si, 57
   jne loadLoop
If you are going to read continuous 57 sectors, why don't you change ch, dh, cl register?
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: Loading more than 54 sectors

Post by egos »

(7C00h-1000h)/200h=54

Are you understand me?

Edited
You can use memory below 7C00h for a stack and above the boot loader for loading from a disk. For example, I'm using 8000h as a base address for loading kernel file.
Last edited by egos on Sat Jun 20, 2009 11:59 am, edited 1 time in total.
If you have seen bad English in my words, tell me what's wrong, please.
yolda
Posts: 13
Joined: Wed May 27, 2009 12:56 pm

Re: Loading more than 54 sectors

Post by yolda »

(7C00h-1000h)/200h=54

Are you understand me?
Is 7C00h some kind of limit? So your saying the problem is the memory rather than the loading using sectors and cylinders? oh i get it now! [edited] jaja, i am putting stuff where the loader is! what do i do now? :P lol
If you are going to read continuous 57 sectors, why don't you change ch, dh, cl register?
i do change them cl with si, but i dont know if i should change heads or cylinders too.
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: Loading more than 54 sectors

Post by egos »

yolda wrote:Is 7C00h some kind of limit?
What are you think about this? :)
i do change them cl with si, but i dont know if i should change heads or cylinders too.
Use linear addressing of sectors and recalculate their numbers to CHS at runtime.
If you have seen bad English in my words, tell me what's wrong, please.
User avatar
salil_bhagurkar
Member
Member
Posts: 261
Joined: Mon Feb 19, 2007 10:40 am
Location: India

Re: Loading more than 54 sectors

Post by salil_bhagurkar »

You don't specify the media from which you are loading the sectors. Depending on whether it is an hd or fd, you have to do the loading depending on the lower and upper limits for each paramter (sector, cylinder, head)...
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Loading more than 54 sectors

Post by Troy Martin »

You need to learn how to convert LBA to CHS. Search the wiki. Hell, it's even on freakin' wikipedia!
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
yolda
Posts: 13
Joined: Wed May 27, 2009 12:56 pm

Re: Loading more than 54 sectors

Post by yolda »

Ty you have all been very helpfull, right now i realize that my problem is the size of the data i am loading. i want to use the area from 00007E00 - 0007FFFF for my kernel, but if i tried to load to anything rather than 1000h it fails!
http://wiki.osdev.org/Memory_Map_(x86)

Code: Select all

        mov bx, 007e00h          ; Destination address = 0000:1000
        mov ah, 02h             ; READ SECTOR-command
        mov al, 49           ; Number of sectors to read = 1
        mov ch, 0               ; Cylinder = 0
        mov cl, 02h             ; Sector = 2
        mov dh, 0             ; Head = 0
        mov dl, [bootdrive]

....


        mov esp, 090000h        ; Move the stack pointer to 090000h
        jmp 08h:007e00h          ; Jump to section 08h (code), offset 01000h
madeofstaples
Member
Member
Posts: 204
Joined: Thu Apr 12, 2007 8:15 am
Location: Michigan

Re: Loading more than 54 sectors

Post by madeofstaples »

yolda wrote:Ty you have all been very helpfull, right now i realize that my problem is the size of the data i am loading. i want to use the area from 00007E00 - 0007FFFF for my kernel, but if i tried to load to anything rather than 1000h it fails!
http://wiki.osdev.org/Memory_Map_(x86)

Code: Select all

        mov esp, 090000h        ; Move the stack pointer to 090000h
        jmp 08h:007e00h          ; Jump to section 08h (code), offset 01000h
I think you mean to set ss to 8000h or so, and sp to a reasonable stack size.

Why are you using segment 08h? Your code effectively starts executing instructions at the physical address 07E80h, which I have a feeling is not what you intended.
Some people are offended by the verifiable truth; such people tend to remain blissfully unencumbered by fact.
If you are one of these people, my posts may cause considerable discomfort. Read at your own risk.
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Loading more than 54 sectors

Post by Troy Martin »

madeofstaples: You're kidding, right? He's jumping into protected mode.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
madeofstaples
Member
Member
Posts: 204
Joined: Thu Apr 12, 2007 8:15 am
Location: Michigan

Re: Loading more than 54 sectors

Post by madeofstaples »

If he has entered protected mode, then the jump should immediately follow modification of the CR0 control register, as per the intel manual section 8.9.

Edit: Intel manual volume 3A, that is.
Some people are offended by the verifiable truth; such people tend to remain blissfully unencumbered by fact.
If you are one of these people, my posts may cause considerable discomfort. Read at your own risk.
yolda
Posts: 13
Joined: Wed May 27, 2009 12:56 pm

Re: Loading more than 54 sectors

Post by yolda »

ok perhaps this code can explain it. I do set cr0, but i still cant seem to get my kernel to load in a space in memeory big enough. i cant use anything lower then 1000h or bigger than 7c00h :(

Code: Select all

%define kernelCODE 0x1000 ; default, i wish i could use 600/500 or even 8000 to be over my 512b kernel and have enough space.
        mov ax, 0000h
        mov es, ax

        mov bx, kernelCODE ; Destination address = 0000:1000
        mov ah, 02h             ; READ SECTOR-command
        mov al, 40           ; Number of sectors to read = 1
        mov ch, 0               ; Cylinder = 0
        mov cl, 02h             ; Sector = 2
        mov dh, 0             ; Head = 0
        mov dl, [bootdrive]

        int 13h                 ; Call interrupt 13h
        or ah, ah               ; Check for error code
        jnz reset_drive         ; Try again if ah != 0

        cli                     ; Disable interrupts, we want to be alone

        xor ax, ax
        mov ds, ax              ; Set DS-register to 0 - used by lgdt

        lgdt [gdt_desc]         ; Load the GDT descriptor

        mov eax, cr0            ; Copy the contents of CR0 into EAX
        or eax, 1               ; Set bit 0
        mov cr0, eax            ; Copy the contents of EAX into CR0

        jmp 08h:clear_pipe      ; Jump to code segment, offset clear_pipe




[BITS 32]                       ; We now need 32-bit instructions so we go into protected mode
clear_pipe:
        mov ax, 10h             ; Save data segment identifyer
        mov ds, ax              ; Move a valid data segment into the data segment register
        mov ss, ax              ; Move a valid data segment into the stack segment register
        mov es, ax
        xor eax, eax
        mov fs, ax
        mov gs, ax
        mov esp, 9FB00h        ; Move the stack pointer to 090000h
        jmp 08h:kernelCODE          ; Jump to section 08h (code), offset 01000h
User avatar
kmtdk
Member
Member
Posts: 263
Joined: Sat May 17, 2008 4:05 am
Location: Cyperspace, Denmark
Contact:

Re: Loading more than 54 sectors

Post by kmtdk »

well
it will all help, if we had the full code, insted of thouse picese, witch does not make 100% sense...
and secound : what device is this code designed for ??? floppy disk, harddisk ??? (just to point it out; a harddisk have 16 heads, a floppy have only 2.)
third: 7c00 is not a limit, but the loading will overwrite the current running code.

KMT dk
well, what to say, to much to do in too little space.
when it goes up hill, increase work, when it goes straight, test yourself but when going down, slow down.
yolda
Posts: 13
Joined: Wed May 27, 2009 12:56 pm

Re: Loading more than 54 sectors

Post by yolda »

kmtdk wrote:well
it will all help, if we had the full code, insted of thouse picese, witch does not make 100% sense...
and secound : what device is this code designed for ??? floppy disk, harddisk ??? (just to point it out; a harddisk have 16 heads, a floppy have only 2.)
third: 7c00 is not a limit, but the loading will overwrite the current running code.

KMT dk
TY reading your post made me go thru all my project for references to the loading area, and now i found out why it didnt work! ONCE again the community helped out a lot!

SOLUTION:
my linker was doing binding at compile time, and i had to change the memory area i wanted to load my kernel to! no i change it from
ld" -e _kernel -Ttext 0x1000
to
ld -e _kernel -Ttext 0x8000

so now i am above the bootloader and with a bunch of space!
00007E00 - 0007FFFF size: 7FB00 (481 KiB) RAM (guaranteed free for use)
Post Reply