FAT32 BPB

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.
Post Reply
Richy
Member
Member
Posts: 41
Joined: Wed Nov 19, 2008 10:03 am

FAT32 BPB

Post by Richy »

So I'm trying to setup a FAT32 system on a custom bootloader, to replace the FAT12 I was running before. FAT12 is simpler, but also outdated and unable to handle larger disks. I'm trying to boot from a Kingston 8GB DataTraveller.

After pouring over the MS documentation and any other resource I can find, this is my best shot for a BPB:

Code: Select all

;FAT 12/16/32
bpbOEM			DB "MSWIN4.1"
bpbBytesPerSector:  	DW 512
bpbSectorsPerCluster: 	DB 1
bpbReservedSectors: 	DW 1
bpbNumberOfFATs: 	DB 2
bpbRootEntries: 	        DW 0
bpbTotalSectors16: 	DW 0
bpbMedia: 	                DB 0xF8
bpbSectorsPerFAT16: 	DW 0
bpbSectorsPerTrack: 	DW 0
bpbHeadsPerCylinder: DW 0
bpbHiddenSectors:      DD 0
bpbTotalSectors32:     DD 15148608

;FAT32
bpbSectorsPerFAT32:	DD 118348
bpbExtFlags:              DW 0
bpbVersion:               DW 0
bpbRootCluster:         DD 2
bpbFSInfoSector:        DW 1
bpbBackupBootSector: DW 6
bpbReserved8:		DQ 0
bpbReserved4:		DD 0

;FAT 12/16/32
bsDriveNumber: 	        DB 0x80
bsUnused: 	        DB 0
bsExtBootSignature: 	DB 0x29
bsSerialNumber:	        DD 0xa0a1a2a3
bsVolumeLabel: 	        DB "BOOT USB   "
bsFileSystem: 	        DB "FAT32   "
When I do this, my computer (Windows 7) stops recognizing the USB stick and tells me it's not formatted. So obviously it's not right. But I can't figure out the mistake. Is anyone here familiar with FAT32 enough to help? Thanks!
User avatar
iansjack
Member
Member
Posts: 4709
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: FAT32 BPB

Post by iansjack »

Why don't you just use Windows to format the stick with FAT32 and then look and see what it has written to the sector?
alexfru
Member
Member
Posts: 1112
Joined: Tue Mar 04, 2014 5:27 am

Re: FAT32 BPB

Post by alexfru »

You might be missing this little detail.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: FAT32 BPB

Post by neon »

Hello,

The Bios Parameter Block provided is incomplete. What you should be doing is reserving space for the BPB and referencing it; making sure to install the boot record in a way as to not overwrite the file system structure. For example, this is the way we do it,

Code: Select all

bits 16
org 0

jmp short main
nop
;
;	bios paramater block area is from offset 0 to 0x5a
;	actual code starts at offset 0x5a. Formatting software
;	installs the BPB in this area
;
times 0x5a db 0

main:
	cli
	push 0x7c0
	push .fix_cs
	retf						; jump to 0x7c0:fix_cs
.fix_cs:
	mov	ax, 0x7c0				; set segments
	mov	es, ax
	mov	ds, ax
	mov	fs, ax
	mov	gs, ax
	mov	ax, 0
	mov	ss, ax
	mov	sp, 0x2000				; ss:sp = stack top at 0x2000
	mov	bp, 0x7c00				; ss:bp = bios param block
The Bios Parameter Block can then be referenced through bp. We reference through bp as the base in order to decrease instruction size and conserve space. It looks like this,

Code: Select all

struc biosParamBlock
	.jump             resb 3             ; jump instruction
	.ident            resb 8             ; oem identifier
   .bps              resw 1             ; bytes per sector
	.spc              resb 1             ; sectors per cluster
	.resSectors       resw 1             ; reserved sector count
	.fatCount         resb 1             ; number of FATs
	.dirEntryCount    resw 1             ; number of directory enteries
	.sectorCount      resw 1             ; number of sectors in logical volume
	.mediaDescr       resb 1             ; media descriptor byte
	.fatSectorCount   resw 1             ; sectors per FAT (fat12/16 only)
	.trackSectorCount resw 1             ; sectors per track
	.headCount        resw 1             ; number of heads
	.hiddenSectCount  resd 1             ; number of hidden sectors
	.sectorCountBig   resd 1             ; only set if sectorCount field is 0 (more then 65535 sectors)
	;
	;	extended boot record for fat32
	;
	.fatSize32        resd 1             ; Sectors per FAT. The size of the FAT in sectors
	.flags            resw 1             ; flags
	.fatVersion       resw 1             ; FAT version number
	.rootCluster      resd 1             ; cluster number of root directory. Often 2
	.fsInfoCluster    resw 1             ; cluster number of FSinfo structure
	.bootCluster      resw 1             ; cluster number of backed up boot sector
	.reserved         resb 12
	.driveNum         resb 1             ; drive number, identical to the values returned by the BIOS
	.winNTflags       resb 1             ; flags in Windows NT
	.signiture        resb 1             ; must be 0x28 or 0x29
	.volumeId         resd 1             ; serial number
	.label            resb 11            ; volume label string padded with spaces
	.systemId         resb 8             ; system idenitifier string always "FAT32   "
endstruc
Installing the boot record using PartCopy is also a little different from FAT12 due to the size of the new structure,

Code: Select all

partcopy bootsect.bin 0 3 C.IMG 0 
partcopy bootsect.bin 5a 1a6 C.IMG 5a
This should provide enough startup code on the proper structure of FAT32 formatted disks and how to install them properly. Note that PartCopy is called twice here as to not overwrite the on disk BPB that the formatting utility installed.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
billcu
Posts: 17
Joined: Tue Aug 26, 2014 9:12 pm

Re: FAT32 BPB

Post by billcu »

I have been looking at fatxx too. And specifically confused concerning BPB_OENName element of the C struct. Now the type is unsigned char BPB_OEMName[8]; and the recommended string is "MSWIN4.1". C wants to add the \0 or null character to the end of every string. Someone told me that memcpy can be used to deal with that terminating NULL character. There's no room in the array for it. The space is 8 bytes at offset 13 with all the FATs I guess.

Bill
Richy
Member
Member
Posts: 41
Joined: Wed Nov 19, 2008 10:03 am

Re: FAT32 BPB

Post by Richy »

iansjack wrote:Why don't you just use Windows to format the stick with FAT32 and then look and see what it has written to the sector?
That's a good idea! I reformatted the drive, opened it with HxD, and indeed some of my values were off. I put the right values in my BPB and now it works fine.

Well, almost fine. Now the BIOS of the computer I'm booting is detecting it as a removable drive, despite the BPB defining the media type as 0xF8 (fixed drive) and the drive number as 0x80 (first hdd). In fact, I even put in code to check the values of the drive number in the BPB and the value of dl right at the start of the bootloader, and both are 0 (first floppy) when I boot. What's happening to my values there?
Octocontrabass
Member
Member
Posts: 5590
Joined: Mon Mar 25, 2013 7:01 pm

Re: FAT32 BPB

Post by Octocontrabass »

Richy wrote:What's happening to my values there?
Most BIOSes will assume you want floppy disk emulation if the first sector contains a BPB.

If you want hard disk emulation, you will need to partition your flash drive as if it were a hard drive, and include a functional FDISK-compatible MBR in the boot sector. (At least one BIOS is known to skip the MBR and run the VBR directly, which will cause problems if your code requires a custom MBR.)
Post Reply