Page 1 of 2
Loading more than 54 sectors [SOLVED]
Posted: Sat Jun 20, 2009 12:36 am
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
Re: Loading more than 54 sectors
Posted: Sat Jun 20, 2009 1:35 am
by VolTeK
interesting, nice work
Re: Loading more than 54 sectors
Posted: Sat Jun 20, 2009 1:43 am
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?
Re: Loading more than 54 sectors
Posted: Sat Jun 20, 2009 11:33 am
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.
Re: Loading more than 54 sectors
Posted: Sat Jun 20, 2009 11:56 am
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?
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.
Re: Loading more than 54 sectors
Posted: Sat Jun 20, 2009 12:14 pm
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.
Re: Loading more than 54 sectors
Posted: Sat Jun 20, 2009 12:31 pm
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)...
Re: Loading more than 54 sectors
Posted: Sat Jun 20, 2009 1:01 pm
by Troy Martin
You need to learn how to convert LBA to CHS. Search the wiki. Hell, it's even on freakin' wikipedia!
Re: Loading more than 54 sectors
Posted: Sat Jun 20, 2009 1:24 pm
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
Re: Loading more than 54 sectors
Posted: Sat Jun 20, 2009 2:06 pm
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.
Re: Loading more than 54 sectors
Posted: Sat Jun 20, 2009 2:43 pm
by Troy Martin
madeofstaples: You're kidding, right? He's jumping into protected mode.
Re: Loading more than 54 sectors
Posted: Sat Jun 20, 2009 3:22 pm
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.
Re: Loading more than 54 sectors
Posted: Sat Jun 20, 2009 3:31 pm
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
Re: Loading more than 54 sectors
Posted: Sat Jun 20, 2009 3:38 pm
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
Re: Loading more than 54 sectors
Posted: Sat Jun 20, 2009 4:12 pm
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)