Unable to get FAT16 boot loader working

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.
vbguyny
Member
Member
Posts: 48
Joined: Wed Aug 12, 2015 6:30 pm

Unable to get FAT16 boot loader working

Post by vbguyny »

I am trying to create a custom boot loader based on John Fine's FAT16 boot loader (http://geezer.osdevbrasil.net/johnfine/ and see attachment). However I cannot get it working either in VirtualBox (v5) nor in Bochs (2.6.6).

When I boot into the machine I simply get the following message:
Error Executing FAT16 bootsector
Press any key to reboot
I created the FAT image using bximage for a 20MB flat hard disk image with the following line added to my .bxrc file:

Code: Select all

ata0-master: type=disk, path="D:\Stuff\projs\Nasm\FAT\fat16_6.img", mode=flat, cylinders=40, heads=16, spt=63, model="Generic 1234", biosdetect=auto, translation=auto
I am also mounting it to my Windows 7 Host OS in order to format it using OSFMount (http://www.osforensics.com/tools/mount-disk-images.html). I format it using Windows (FAT + 512 bytes per sector).

After putting some "magic breakpoints" into the boot loader I see that the int 13h call doesn't error but it isn't populating the memory with the sector from the disk. Instead the segment that I expect to be populated contains all zeros. This causes the boot loader to not find my kernel.

Any suggestions?
Attachments
bootr01.zip
(12.4 KiB) Downloaded 72 times
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: Unable to get FAT16 boot loader working

Post by SpyderTL »

Process of elimination.

Hard code the sector/block number right before calling int 13h, and then put a breakpoint and check and see if it is working at all. That will tell you if it is a problem with the int 13h call, or with the parameters that you are sending it.

Best guess, the sector is being loaded, but to the wrong address... Just a guess.
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
vbguyny
Member
Member
Posts: 48
Joined: Wed Aug 12, 2015 6:30 pm

Re: Unable to get FAT16 boot loader working

Post by vbguyny »

@SpyderTL: Thanks for your suggestion. I think that the problem is I don't know what the correct values need to be for int 13h. However I did find a template BPB (see below) that I put into my code after using partcopy reports my image to be 2 GB drive.

However, I still get the same error saying that there was an error executing the FAT16 bootsector. Interestingly, the carry flag isn't being set after calling int 13h which means that the call was successful. As for the return memory goes to 0800h:0000h, which is all zeros according to Bochs.

Any other ideas?
; Skip over the data portion of the "DOS BOOT RECORD". The install method
; must merge the code from this ASM with the data put in the boot record
; by the FAT16 formatter.
;
;times 0x3B db 0

;minimal, default BIOS Parameter Block (BPB)

oem_id: ; 03h not used by this code
db "GEEZER", 0, 0

bytes_per_sector: ; 0Bh
dw 512

sc_p_clu: ; 0Dh
db 64 ; maximum (2 gig partition)

sc_b4_fat:; 0Eh
dw 1

fats: ; 10h
db 2

dir_ent: ; 11h
dw 512

total_sectors: ; 13h not used by this code
dw 0

media_id: ; 15h not used by this code
db 0F8h

sc_p_fat: ; 16h
dw 256 ; maximum (2 gig partition)

sc_p_trk: ; 18h
dw 63 ; maximum (2 gig partition)

heads:; 1Ah
dw 255 ; maximum (2 gig partition)

sc_b4_prt: ; 1Ch
dd 16065 ; partition 1 (CHS=1:0:1)

total_sectors_large: ; 20h not used by this code
dd 4192965 ; CHS=261:255:63 (2 gig partition)

drive: dw 80 ; 0x24
Signiture: db 0 ; 0x26
VolSerial: dd 0 ; 0x27
VolLabel: db "12345678901" ; 0x2B
SysId: db "12345" ; 0x36
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Unable to get FAT16 boot loader working

Post by Brendan »

Hi,
vbguyny wrote:After putting some "magic breakpoints" into the boot loader I see that the int 13h call doesn't error but it isn't populating the memory with the sector from the disk. Instead the segment that I expect to be populated contains all zeros. This causes the boot loader to not find my kernel.
Are you sure it's not loading the sectors correctly (and that the sectors didn't contain all zeros)?

Also; is it FAT16 or FAT12? The code you posted (in "bootr01.zip") says FAT12, but you keep saying FAT16. This makes me wonder if you're having trouble because you're trying to boot from a FAT16 file system using a boot loader designed for FAT12.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
vbguyny
Member
Member
Posts: 48
Joined: Wed Aug 12, 2015 6:30 pm

Re: Unable to get FAT16 boot loader working

Post by vbguyny »

@Brendan: Yes, you are probably correct. I am not sure if there is an issue with the code which I downloaded from John Fine's website or the image file that I am using for FAT16.

Yes, I am using FAT16 boot loader. The zip file contains a FAT12 and FAT16 boot loader. I have tested FAT12 against Virtual Floppy Drive (http://vfd.sourceforge.net/) and that works. However the example in the zip file for FAT16 doesn't work for me.

Maybe I should ask you guys how to correctly create a FAT16 image file.
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Unable to get FAT16 boot loader working

Post by iansjack »

When you are deciding which sector to load you are allowing for the fact that a hard disk contains partitions?
vbguyny
Member
Member
Posts: 48
Joined: Wed Aug 12, 2015 6:30 pm

Re: Unable to get FAT16 boot loader working

Post by vbguyny »

@iansjack: I am not sure. This isn't my code it is literally something that someone else wrote back in 1999. Below is the method that is used to read the sector into memory.
read_32:
;
; Input:
; dx:di = sector within partition
; bl = sector count (max 0x80)
; es = destination segment
;
; The sector number is converted from a partition-relative to a whole-disk
; (LBN) value, and then converted to CHS form, and then the sectors are read
; into ES:0.
;
; Output:
; di = input di + input bx
; bx = 0
; dl = drive
; si, bp and seg regs unchanged
; other registers modified

lea ax, [di+bx] ;Compute di+bx (needed by two caller's
push ax ; of read_16)

add di, [sc_b4_prt] ;Convert to LBN
adc dx, [sc_b4_prt+2]

xchg ax, dx ;AX = (high) LBN
cwd
div word [sc_p_trk] ; / Sectors per track
xchg ax, di ;AX = (low) LBN ;DI = (high) track
div word [sc_p_trk] ;AX = (low) track ;DX = sector-1
inc dx
mov cl, dl ;CL = sector
mov dx, di ;DX = (high) track
div word [heads] ; / Number of heads
mov dh, dl ;DH = head

mov ch, al ;CH = (low) cylinder
ror ah, 1 ;rotate (high) cylinder
ror ah, 1
add cl, ah ;CL = combine sector, (high) cylinder
mov dl, [drive] ;DL = Drive number
xchg ax, bx ;AL = Sector count
mov ah, 2 ;AH = Read command
xor bx, bx ;ES:BX = address
xchg bx, bx ; Magic breakpoint
int 13h ;Do it
jc error.2
pop di ;DI = Input DI + Input BX
ret
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: Unable to get FAT16 boot loader working

Post by SpyderTL »

Where are "sc_p_trk" and "heads" and "sc_b4_prt" defined? If these aren't passed in via a register, or structure, then these have to be hard-coded, and they will only work if they are hard coded to match your specific hard drive.

Eventually, I would recommend replacing this code with your own. You'll never be sure that it's working properly until you fully understand it, and writing it yourself is the best way to do that.
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
vbguyny
Member
Member
Posts: 48
Joined: Wed Aug 12, 2015 6:30 pm

Re: Unable to get FAT16 boot loader working

Post by vbguyny »

@SpyderTL: I would like to write it myself however I am not able to find any good tutorials on this. It seems that the internet favors FAT12 for hobbyist boot loaders. Do you have any suggestions?
Octocontrabass
Member
Member
Posts: 5588
Joined: Mon Mar 25, 2013 7:01 pm

Re: Unable to get FAT16 boot loader working

Post by Octocontrabass »

vbguyny wrote:cylinders=40, heads=16, spt=63,
The bootloaders you're using trust the drive geometry in the BPB instead of asking the BIOS.

You formatted the disk with Windows, which means the BPB contains whatever geometry Windows thinks should be there, and not the geometry you've told Bochs to expect.

As a temporary fix, you can manually update the BPB to contain the geometry you want.

As a permanent fix, find a different bootloader or write your own.
User avatar
BASICFreak
Member
Member
Posts: 284
Joined: Fri Jan 16, 2009 8:34 pm
Location: Louisiana, USA

Re: Unable to get FAT16 boot loader working

Post by BASICFreak »

vbguyny wrote:@SpyderTL: I would like to write it myself however I am not able to find any good tutorials on this. It seems that the internet favors FAT12 for hobbyist boot loaders. Do you have any suggestions?
FAT16 is easier than FAT12

Use the same code just grab a 16-bit value from fat (with no 2/3 offset and no bit shifting) when chasing cluster chains.

Same goes for FAT32 (except the Root Dir is A File aka cluster based)
BOS Source Thanks to GitHub
BOS Expanded Commentary
Both under active development!
Sortie wrote:
  • Don't play the role of an operating systems developer, be one.
  • Be truly afraid of undefined [behavior].
  • Your operating system should be itself, not fight what it is.
vbguyny
Member
Member
Posts: 48
Joined: Wed Aug 12, 2015 6:30 pm

Re: Unable to get FAT16 boot loader working

Post by vbguyny »

@Octocontrabass: Thanks for your suggestions. The problem is that I don't know what the correct values need to be in the BPB in order to get this working. In my previous post I tried to hard code them to 2GB but it didn't make a difference (Although this was copied from something I found on the internet).

Also I tried 4 other boot loaders and none of them work so I think that there is something wrong with the formatting of the drive or the BPB. Does anyone know of a good utility that I can use to create hard drive images? I am using bximage right now.
User avatar
BASICFreak
Member
Member
Posts: 284
Joined: Fri Jan 16, 2009 8:34 pm
Location: Louisiana, USA

Re: Unable to get FAT16 boot loader working

Post by BASICFreak »

vbguyny wrote:@Octocontrabass: Thanks for your suggestions. The problem is that I don't know what the correct values need to be in the BPB in order to get this working. In my previous post I tried to hard code them to 2GB but it didn't make a difference (Although this was copied from something I found on the internet).

Also I tried 4 other boot loaders and none of them work so I think that there is something wrong with the formatting of the drive or the BPB. Does anyone know of a good utility that I can use to create hard drive images? I am using bximage right now.
HINT: Do not overwrite the BPB, copy the first 3 Bytes and the last 448 (might not be right) Bytes. Let the format tool take care of BPB (assuming you are not formatting the disk in your OS)

I'm actually about to attempt this to install my OS on HDD (once I get a user interface implemented, almost there)

Mount the image and format it with your development system (or do what I did and download a copy of windows 98 SE startup disk and format the image in Bochs.)
BOS Source Thanks to GitHub
BOS Expanded Commentary
Both under active development!
Sortie wrote:
  • Don't play the role of an operating systems developer, be one.
  • Be truly afraid of undefined [behavior].
  • Your operating system should be itself, not fight what it is.
vbguyny
Member
Member
Posts: 48
Joined: Wed Aug 12, 2015 6:30 pm

Re: Unable to get FAT16 boot loader working

Post by vbguyny »

BASICFreak: Here are the steps that I am doing now:

1. Use bximage to create new harddrive image (see attachment f1)
2. Add line to bochsrc_dbg.bxrc
ata0-master: type=disk, path="D:\Stuff\projs\Nasm\FAT\fat16_9.img", mode=flat, cylinders=65, heads=16, spt=63, model="Generic 1234", biosdetect=auto, translation=auto
3. Mount drive using OSFMount (see attachment f2) to I:\
4. Format the drive using FAT16 and 512 bytes as sector size (see attachment f3 and f4)
5. Use partcopy to copy over the boot loader:
partcopy boot16.bin 0 3 -ai
partcopy boot16.bin 3e 1c2 -ai 3e
6. Copied TESTLOAD.BIN to LOADER.BIN on the I:\
7. Run Bochs-2.6.6 in debug mode (bochsdbg.exe) against bochsrc_dbg.bxrc
8. Receive error executing FAT16 bootsector (see attachment f5)
Attachments
FAT.zip
(54.67 KiB) Downloaded 29 times
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Unable to get FAT16 boot loader working

Post by neon »

Hello,

We tend to use ImDisk or Gizmo to produce disk images over bximage. They integrate within Explorer and can produce raw disk images for use with Bochs. As noted above, the heads, spt, and cylinders values passed to Bochs should be obtained directly from the BPB installed by the formatting utility. If you are not sure what values you should use, feel free to post a hex dump of your boot sector and we can extract them here. If interested, we might be able to release a copy of our fat16 boot record for you to try - it uses the Extended INT 13h read services rather then legacy services to adopt to large hard disks.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
Post Reply