Page 1 of 1
Bootsector Not Working
Posted: Sun Feb 17, 2013 3:35 pm
by gabemaiberger
My bootsector does not jump to the bootloader code that I want it to jump to. I am really not sure though if I am loading it in the right place. I want to be able to load the first sector after the bootsector into memory and jump to that. Can somebody help me? Here is my code:
Code: Select all
BITS 16
ORG 0x7C00
CPU 386
jmp START
OEM_ID db "PARADIGM"
BytesPerSector dw 0x0200
SectorsPerCluster db 0x09
ReservedSectors dw 0x0001
TotalFATs db 0x01
MaxRootEntries dw 0x0000
TotalSectors dw 0x0AF0
MediaDescriptor db 0xF0
SectorsPerFAT dd 0x00000009
SectorsPerTrack dw 0x0003
NumHeads dw 0x0002
HiddenSectors dd 0x00000000
TotalSectorsLarge dd 0x00000000
DriveNumber db 0x80
Signature db 0x29
VolumeID dd 0xFFFFFFFF
VolumeLabel db "PARADIGM-OS"
SystemID db "FAT12 "
START:
cli
mov ax, 0x0000
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ax, 0x0000
mov ss, ax
mov sp, 0xFFFF
sti
mov si, msgLoading ;display loading message
call DisplayMessage
mov cx, WORD [SectorsPerCluster] ;read 9 sectors
mov ax, 0x0001
mov bx, 0x0200
call ClusterLBA
call ReadSectors
jmp DONE
DONE:
mov si, msgSuccess
call DisplayMessage
jmp end
FAILURE:
mov si, msgFailure
call DisplayMessage
hlt
DisplayMessage:
lodsb
or al, al
jz .DONE
mov ah, 0x0E
mov bh, 0x00
mov bl, 0x07
int 0x10
jmp DisplayMessage
.DONE:
ret
;Read 'cx' sectors starting from memory location 'ax' into memory location 'bx'
ReadSectors:
.MAIN:
mov di, 5 ;5 tries total
.SECTORLOOP:
push ax
push bx
push cx
call LBACHS
mov ah, 0x02
mov al, 0x01
mov ch, BYTE [absoluteTrack]
mov cl, BYTE [absoluteSector]
mov dh, BYTE [absoluteHead]
mov dl, BYTE [DriveNumber]
int 0x13
jnc .SUCCESS
xor ah, ah
int 0x13
dec di
pop cx
pop bx
pop ax
jnz .SECTORLOOP
int 0x18
.SUCCESS:
mov si, msgProgress
call DisplayMessage
pop cx
pop bx
pop ax
add bx, WORD [BytesPerSector]
inc ax
loop .MAIN
ret
ClusterLBA:
sub ax, 0x0002
xor cx, cx
mov cl, BYTE [SectorsPerCluster]
mul cx
add ax, WORD [datasector]
ret
LBACHS:
xor dx, dx
div WORD [SectorsPerTrack]
inc dl
mov BYTE [absoluteSector], dl
xor dx, dx
div WORD [NumHeads]
mov BYTE [absoluteHead], dl
mov BYTE [absoluteTrack], al
ret
absoluteSector db 0x00
absoluteHead db 0x00
absoluteTrack db 0x00
datasector dw 0x0000
cluster dw 0x0000
msgLoading db "Loading Boot Image", 0x0D, 0x0A, 0x00
msgCRLF db 0x0D, 0x0A, 0x00
msgProgress db ".", 0x00
msgFailure db 0x0D, 0x0A, "ERROR: Press Any Key to Reboot", 0x00
msgSuccess db "DONE"
times 510-($-$$) db 0
dw 0xAA55
end:
Re: Bootsector Not Working
Posted: Sun Feb 17, 2013 5:29 pm
by DLBuunk
First of all, the bpb starts 3 bytes into the bootsector, and i wouldn't be surprised if your "jmp START" would be encoded as a short jump (2 bytes), not a near one (3 bytes), you might want to check that (and probably insert a nop).
Then there are a lot of labels that don't end in a colon, while your assembler may happen to accept it, it is very bad practice.
The number of sectors/cluster is 9, which is so nonstandard that i would be surprised if any existing driver understands it (a quick test reveals that at least linux doesn't).
Your number of hidden sectors is 1, which means that you are trying to load/run the FAT.
Maxrootentries of zero means that there cannot be any files on the disk. Try the minimum of 16.
Total sectors of 0xAF0 is 80 short of the normal value (for a 3.5" 1.44M floppy).
Sectors/FAT should be a word, not a dword.
3 sectors/track and drive number 0x80, wait is this a hard drive with a bizarre geometry?
You don't reset cs to 0x0000.
Setting sp to an odd number is a recipe for disaster.
I'll leave it to someone else to point out the myriad other errors, in the meantime i suggest you read the wiki articles on bootloaders and FAT and find a good tutorial on real mode segentation.
Good luck.
Re: Bootsector Not Working
Posted: Sun Feb 17, 2013 7:08 pm
by gabemaiberger
Still does not work. Maybe it is the drive number? What is the drive number for a floppy?
Re: Bootsector Not Working
Posted: Sun Feb 17, 2013 11:00 pm
by egos
DLBuunk wrote:Your number of hidden sectors is 1, which means that you are trying to load/run the FAT.
HiddenSectors value is 0, ReservedSectors value is 1. It's correct.
You don't reset cs to 0x0000.
It's not necessary.
gabemaiberger wrote:Still does not work. Maybe it is the drive number? What is the drive number for a floppy?
0?
Re: Bootsector Not Working
Posted: Mon Feb 18, 2013 3:38 am
by DLBuunk
gabemaiberger wrote:Still does not work. Maybe it is the drive number? What is the drive number for a floppy?
Of course it still doesn't work, did i tell you i told you all bugs i could see?
gabemaiberger wrote: I want to be able to load the first sector after the bootsector into memory and jump to that.
egos wrote:HiddenSectors value is 0, ReservedSectors value is 1. It's correct.
With those (otherwise correct) values, the sector after the bootsector is the FAT, and i cannot imagine you want to jump into that.
egos wrote:0?
That's correct, though you might want to consider using the drive number the BIOS passes you in dl.
I suggest you single-step this piece of code in bochs (or another emulator) and check if the registers contain what you think they should after each step, you'll find a few errors that way.
Oh, and int 0x18 tries to run the (long obsolete) ROM-BASIC, i take you mean int 0x13?
Re: Bootsector Not Working
Posted: Mon Feb 18, 2013 5:35 am
by egos
DLBuunk wrote:With those (otherwise correct) values, the sector after the bootsector is the FAT, and i cannot imagine you want to jump into that.
TS tries to read cluster 1 (that's very strange!), not sector 1. But you are right, this code has too many bugs that it's working correctly. "datasector" is used but was not initialized correctly and so on.
Oh, and int 0x18 tries to run the (long obsolete) ROM-BASIC, i take you mean int 0x13?
This service was described in BIOS Boot Spec. But traditionally in stage 1 (especially on floppies) is used "int 19h". "int 18h" is used in stage 0 (MBR) boot loaders to reference next device in BIOS boot sequence when no active boot partition.
Re: Bootsector Not Working
Posted: Mon Feb 18, 2013 10:09 am
by gabemaiberger
I fixed all I saw but it's like it reads only one sector then when it tries to read the next it fails after 5 tries which it is supposed to do but I do not know what is causing it to fail reading sectors. I did everything you guys pointed out. My goal over all is to read sectors that are a part of the bootloader and put them right after the bootcode. When that is done I want it to jump to the bootloader right after the bootsector. I am not asking for any code. I just would like some aid in finding all the bugs in my code so I can get it to work. I have been trying for months with no luck and I am getting depressed. However I now am turning to this forum for help and aid for my bugs in my code. Thanks. My code is in the file I uploaded.
Re: Bootsector Not Working
Posted: Mon Feb 18, 2013 11:19 am
by Combuster
15 seconds later:
Code: Select all
00000000 E95600 jmp word 0x59
00000003 90 nop
Makes four bytes. That's obviously not fixed, even though it has been explicitly pointed out what it should have been...
I have been trying for months
This is not a matter of trial and error, it's science. And science is all about knowing what you are doing. As long as you keep guessing, you'll indeed spend lots of time trying all sorts of combinations of which the working one is often not among them. Do yourself a favour, get the sourcecode to bochs and build your own with the integrated debugger enabled.
Re: Bootsector Not Working
Posted: Mon Feb 18, 2013 12:03 pm
by gabemaiberger
Can I just enable bochs debugging without compiling? I am running ubuntu and turned the rpm from the site into a deb then installed that.
Re: Bootsector Not Working
Posted: Mon Feb 18, 2013 2:27 pm
by Combuster
Have you seen the following line in the forum rules? It's pretty blunt about those "can you do X with Y" questions:
Eric S. Raymond wrote:Try it and see. If you did that, you'd (a) learn the answer, and (b) stop wasting my time.
Like I pointed out in the previous post, you have some issues with the needed problem-solving mentality to be good at this. Sadly, it doesn't grow on you in a week, or even a month's time.
For instance, google's first hit provides the answer to your question. It would have been more efficient if you looked it up yourself rather than waiting for me to write a post on where to find the answer.
Re: Bootsector Not Working
Posted: Mon Feb 18, 2013 4:27 pm
by egos
gabemaiberger wrote:I fixed all I saw but it's like it reads only one sector then when it tries to read the next it fails after 5 tries which it is supposed to do but I do not know what is causing it to fail reading sectors. I did everything you guys pointed out. My goal over all is to read sectors that are a part of the bootloader and put them right after the bootcode. When that is done I want it to jump to the bootloader right after the bootsector. I am not asking for any code. I just would like some aid in finding all the bugs in my code so I can get it to work. I have been trying for months with no luck and I am getting depressed. However I now am turning to this forum for help and aid for my bugs in my code. Thanks. My code is in the file I uploaded.
Good approach. I use same one in general FAT12/16 boot loader (not for floppies): I'm reading extender from 1st sector of 1st cluster of the file "bootex.bin". But Combuster is right. Your problem is in other sphere. Think about every thing that you write. Programming in general and especially programming in asm requires to be very attentive. And excuse me for criticism.
Some advices: Take usual values for BPB+ fields for floppies 2x80x18. Try to write your code from scratch, step by step.
Some tips:
1) FAT32 - What is it? New FS, same bugs. In FAT32 RootEntries field should hold 0, and so on. Read the FAT Spec.
2) mov ax, 0x7A00/mov ss, ax/mov sp, 0x7C00 - very interesting! Read about addressing in real mode.
3) That's enough.