loader

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.
lama
Member
Member
Posts: 83
Joined: Thu Apr 16, 2009 8:41 am

loader

Post by lama »

hello,
i have rewritten part of my loader and sometimes it loads file (fat12) ok, sometimes it doesn't.(when filesize is changed) the problem is, that sometimes loader cant recognize the end of cluster. so can anyone please, check my code?
thanks a lot :)

Code: Select all

initmodule3:
mov si, init3note
call print_string
MOV SI, fatload
CALL print_string
XOR AX, AX
xor dl, dl 
INT 0x13
MOV AH, 2
MOV AL, 14
MOV CL, 2
MOV CH, 0
;XOR DX, DX  ; rofl logical 19
MOV DH, 1
XOR DL, DL
push es
mov bx, 0x60
mov es, bx
xor bx, bx
INT 0x13
pop es
JC mountfailure
MOV AH, 2
MOV AL, 9
MOV CL, 2
XOR CH, CH
XOR DX, DX
push es
mov bx, 0x220
mov es, bx
xor bx, bx
INT 0x13
pop es
JC mountfailure
MOV CX, 0xE0
xor si, si
push ds
mov ax, 0x60
mov ds, ax
iter:
ADD SI, 0x20
MOV DI, image
PUSH SI
PUSH CX
MOV CX, 11
CLD
REPE CMPSB
POP CX
POP SI
LOOPNE iter
CMP CX, 0
JE noimage
pop ds
PUSH SI
MOV SI, found
CALL print_string
POP SI
push 0x60
pop ds
ADD SI, 0x1A
MOV AX, [ds:SI]
xor bx, bx
iter1:
PUSH AX
push 0x900
pop ds
CALL clstochs
MOV AH, 2
MOV AL, 1
MOV CH, [ds:fdd_cyld]
MOV CL, [ds:fdd_sect]
MOV DH, [ds:fdd_head]
XOR DL, DL
push 0xa00
pop es
INT 0x13
;xchg bx, bx
JC readfailure
push es
push 0x900
pop es
inc byte [es:counter]
mov al, byte [es:counter]
call printhex
pop es
ADD BX, 0x200
POP AX
PUSH AX
MOV CX, 3
MUL CX
SHR AX, 1
MOV SI, AX
push 0x220
pop ds
MOV CX, [ds:SI]
POP AX
AND AX, 1
CMP AX, 0
JE even
SHR CX, 4
JMP n1
even:
AND CX, 0x0FFF
n1:
MOV AX, CX
CMP AX, 0x0FF7 <=============== there is the problem
xchg bx, bx
JA loadok
JMP iter1

there is the cluster/chs routine:

clstochs:
convclustlba:
;clusterStart=rootStart+(rootEntries*32)/bytesPerSectors
;lba=clusterStart+(cluster-2)*sectPerCluster
PUSH BX
PUSH AX ; cluster
MOV AX, 7168
MOV BX, 0x200
DIV BX
ADD AX, 19
POP BX
PUSH AX ; clusterStart
SUB BX, 2 
MOV AX, 1
MUL BX
MOV BX, AX
POP AX
ADD AX, BX
POP BX
convlbachs:
PUSHA
MOV BX, 18
XOR DX, DX
DIV BX ; 
INC DX
MOV [ds:fdd_sect], DL
MOV BX, 2
XOR DX, DX
DIV BX
MOV [ds:fdd_cyld], AL
MOV [ds:fdd_head], DL
POPA
RET
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Re: loader

Post by bewing »

I think your problem is on the next line -- WTH is xchg bx, bx supposed to do?
nedbrek
Member
Member
Posts: 44
Joined: Tue Dec 15, 2009 6:36 pm

Re: loader

Post by nedbrek »

bewing wrote:I think your problem is on the next line -- WTH is xchg bx, bx supposed to do?
Usually, it is a "magic instruction" - a signal to an emulator to break. At least, in Bochs and Simics it is... real machines will treat it as a nop.
lama
Member
Member
Posts: 83
Joined: Thu Apr 16, 2009 8:41 am

Re: loader

Post by lama »

jj it is a magic instruction for bochs...and cpu will exchange all bytes from bx to bx, so it does nothing but wasted cycles.
but to my question - any ideas?:)
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: loader

Post by Combuster »

You too, young padawan, must learn to format and comment your code. :wink:

Seriously, someone who gets paid would spend a day figuring out what you planned on doing, before even finding out what the bug is, now you are asking this effort of people whom do this in their spare hours.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: loader

Post by qw »

Combuster wrote:You too, young padawan, must learn to format and comment your code. :wink:

Seriously, someone who gets paid would spend a day figuring out what you planned on doing, before even finding out what the bug is, now you are asking this effort of people whom do this in their spare hours.
Agree. All my brain can make of this is a meaningless list of characters.
lama
Member
Member
Posts: 83
Joined: Thu Apr 16, 2009 8:41 am

Re: loader

Post by lama »

ok, sorry for that, i dont comment my code because nobody reads it , except me.
i wanted to load root to 0x600, fat to 0x2200 and my kernel image to 0xa000 . (0x900 is the segment of this loader :) )
the image loading procedure starts at "iter1" .
ax holds the current cluster and the rest should be self-explanatory.
lama
Member
Member
Posts: 83
Joined: Thu Apr 16, 2009 8:41 am

Re: loader

Post by lama »

i discovered, that anything smaller then 47 sectors will load fine. when the file gets bigger - it will result in invalid cluster sequence.
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: loader

Post by Gigasoft »

Where's your stack at? You're probably overwriting it.
lama
Member
Member
Posts: 83
Joined: Thu Apr 16, 2009 8:41 am

Re: loader

Post by lama »

stack is fine..somewhere around 0xf000 i guess. there must be some connection between the kernel's filesize and cluster/chs calculations. it is driving me nuts already :'(
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: loader

Post by Combuster »

You're a very bad debugger. Know, don't guess.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
lama
Member
Member
Posts: 83
Joined: Thu Apr 16, 2009 8:41 am

Re: loader

Post by lama »

i said i know that my stack is ok. any real ideas what could be wrong?
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: loader

Post by neon »

Hello,

I personally would like to help, however your code is unreadable: Magic numbers everywhere, no comments, no structure. I cannot help with this code, and rather not go through it to determine what it is doing.

What I would do is try to force the code to fail. If it "sometimes works" then its most likely a data error that you will need to look into. Check your registers and variables and verify what is correct and not and try to narrow down where its at. ie; debug your code using the Bochs debugger. If you have the capability, it can also be helpful to print out information as well.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
lama
Member
Member
Posts: 83
Joined: Thu Apr 16, 2009 8:41 am

Re: loader

Post by lama »

i dont know what i just did, but it worked i guess..i have rewritted that loading part (iter1) into this:

Code: Select all

push 0x900
push 0x900
pop ds
pop es
inc byte [counter]
push ax
push dx
mov al, byte [counter]
call printhex
pop dx
pop ax
push 0x220
push 0x220
pop ds
pop es
mov ax, dx
mov cx, 3
push dx
mul cx
shr ax, 1
pop dx
mov bx, ax
mov cx, [bx]
and dx, 1
cmp dx, 0
je even
shr cx, 4
jmp n1
even:
and cx, 0x0fff
n1:
mov dx, cx
cmp cx, 0xff0
jb iter1
xchg bx, bx
if anyone cares..
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: loader

Post by Gigasoft »

Well you're loading your kernel at A000. At 47 sectors, you're at FE00. If your stack is here, it's going to be overwritten by the next read.
Post Reply