Page 1 of 1

Bug in bootloader

Posted: Mon Jan 10, 2005 12:00 am
by cipek
I've got this code:

Code: Select all

 

[bits 16]
[org 0]


jmp BootCode

 BPB_OEM               db 'ADDIX   '
 BPB_BytesPerSector    dw 512
 BPB_SectorsPerCluster db 1
 BPB_ReservedSectors   dw 1
 BPB_FatsNum           db 2
 BPB_RootEntries       dw 224
 BPB_TotalSectors      dw 2880
 BPB_MediaType         db 0f0h
 BPB_SectorsPerFat     dw 9
 BPB_SectorsPerTrack   dw 18
 BPB_HeadsNum          dw 2
 BPB_HiddenSectors     dd 0
 BPB_TotelSectors32    dd 0
 BPB_DriveNum          db 0
 BPB_Reserved          db 0
 BPB_BootSignature     db 0
 BPB_VolumeID          dd 0bbbbddddh
 BPB_VolumeLabel       db 'ADDIX      '
 BPB_FileSystem        db 'FAT 12  '


BootCode:
 cli
 mov ax, 7c00h
 mov ds, ax
 mov es, ax
 mov fs, ax
 mov gs, ax

 mov [BPB_DriveNum], dl

 mov ax, 0000h
 mov ss, ax
 mov sp, 0ffffh
 sti

 xor cx, cx
 xor dx, dx
 mov ax, 32
 mul word[BPB_RootEntries]
 div word[BPB_BytesPerSector]
 xchg ax, cx

 mov al, byte[BPB_FatsNum]
 mul word[BPB_SectorsPerFat]
 add ax, [BPB_ReservedSectors]

 mov word[DataSector], ax
 add word[DataSector], cx

 mov bx, 0200h
 call ReadSector


 mov cx, [BPB_RootEntries]
 mov di, 0200h

FindLoop:
 push cx
 
 mov cx, 11
 mov si, FileName
 push di
 rep cmpsb

 pop di
 je LoadFAT
 pop cx
 add di, 32

 loop FindLoop

 jmp Failure


LoadFAT:
 mov dx, word[di+1ah]
 mov word[Cluster], dx

 xor ax, ax
 mov al, byte[BPB_FatsNum]
 mul word[BPB_SectorsPerFat]
 mov cx, ax

 mov ax, word[BPB_ReservedSectors]
 mov bx, 0200h
 call ReadSector

 
 mov ax, 1000h
 mov es, ax
 mov bx, 0000h
 push bx

LoadFile:
 mov ax, word[Cluster]
 pop bx
 call ClusterLBA
 xor cx, cx
 mov cl, byte[BPB_SectorsPerCluster]
 call ReadSector
 push bx
 
 mov ax, word[Cluster]
 mov cx, ax
 mov dx, ax
 shr dx, 0x0001
 add cx, dx
 mov bx, 0x0200
 add bx, cx
 mov dx, word[bx]
 test ax, 0001
 jnz LoadFile2
  
LoadFile1:
 and dx, 0000111111111111b
 jmp LoadFileDone

LoadFile2:
 shr dx, 4

LoadFileDone:
 mov word[Cluster], dx
 cmp dx, 0x0ff0
 jb LoadFile

Done:
 mov ax, 1000h
 mov es, ax
 mov ds, ax
 mov ss, ax
 xor ax, ax
 xor bx, bx
 xor cx, cx
 xor dx, dx
 xor si, si
 xor di, di
 xor sp, sp
 xor bp, bp
 jmp 1000h:0000h

Halt:
 jmp Halt

Failure:
 mov si, MSG_Failure
 call PrintString
 
 mov ax, 0
 int 16h

 int 19h

PrintString:
 lodsb
 cmp al, 0
 je PrintStringEnd

 mov ah, 0Eh
 mov bh, 0
 int 10h		
 jmp PrintString
PrintStringEnd:
 ret

LBA_to_CHS:
 xor dx, dx
 div word[BPB_SectorsPerTrack]
 inc dl
 mov byte[AbsSector], dl

 xor dx, dx
 div word[BPB_HeadsNum]
 mov byte[AbsHead], dl
 mov byte[AbsTrack], al
 ret

ClusterLBA:
 sub ax, 2
 xor cx, cx
 mov cl, byte[BPB_SectorsPerCluster]
 mul cx
 add ax, word[DataSector]

ReadSector:
 push ax
 push bx
 push cx

 call LBA_to_CHS

 mov ah, 02h
 mov al, 01h
 mov ch, byte[AbsTrack]
 mov cl, byte[AbsSector]
 mov dh, byte[AbsHead]
 mov dl, byte[BPB_DriveNum]
 int 13h

 pop cx
 pop bx
 pop ax

 add bx, word[BPB_BytesPerSector]
 inc ax
 loop ReadSector
 ret

 
AbsSector  db 0
AbsHead    db 0
AbsTrack   db 0
DataSector dw 0
Cluster    dw 0

FileName    db 'ADDIX   SYS'
MSG_Welcome db 'AddixOS loader by Adix', 13,10,0
MSG_Loading db 'Ladowanie AddixOS...', 13,10,0
MSG_Failure db 'Blad! Nacisnij jakis klawisz...', 0

times 510-($-$$) db 0
dw 0aa55h
i'm compiling it without any errors in NASM. Rawrating on floppy, copying kernel and trying to boot in bochs. I don't know why it's don't working :/

Re: Bug in bootloader

Posted: Mon Jan 10, 2005 12:00 am
by prajwal
don't u think the ORG should be to 0x7c00.... Just try

Re: Bug in bootloader

Posted: Tue Jan 11, 2005 12:00 am
by smiddy
I can not tell from your post the size of a couple of the BPB items. These all need to be specific to the requirements BPB in order to be used correctly as a FAT file system. Unfortunately I don't have a reference handy here at work, but recheck OEM, Volume, and FileSystem for the correct string length. Also verify that your kernel name is 11 characters long (sorry, I can not tell the stringlength since you placed it in the text area, perhaps use the code markers [ code ] code here [ / code ] [<- remove spaces]).

Also, can you be more specific? What error or lack of errors are you getting. Does the boot code print out a failure message?

Re: Bug in bootloader

Posted: Tue Jan 11, 2005 12:00 am
by JAAman
yes it looks like your OEMID is too short
also looks like your fileName might be too short (use the code tag to make it clearer)
also if your CS is not default to 0x07c90 then your initial jmp over the bpb will be wrong(this could be your problem)

change the

Code: Select all

 jmp BootCode
into

Code: Select all

 jmp short BootCode
NOP