Bootloader - Read FAT

Programming, for all ages and all languages.
Post Reply
User avatar
mark3094
Member
Member
Posts: 164
Joined: Mon Feb 14, 2011 10:32 pm
Location: Australia
Contact:

Bootloader - Read FAT

Post by mark3094 »

Hi,

I am still tinkering with my FAT32 bootloader. It works well, but it makes the assumption that when it loads stage 2, it will find it in the first cluster of the root directory. I am trying to remove this assumption, so if the RD is longer than one cluster, it can lookup the FAT, find the next cluster, load it and continue.

I'm having problems reading from the FAT. I am using INT 13, AH 0x42 to read sectors. During startup, I set these values:

Code: Select all

[ORG 0x0000]
MOV		AX, 0x07C0
MOV		DS, AX
MOV		ES, AX
	
MOV		AX, 0x0000
MOV		SS, AX
MOV		SP, 0xFFFF
My FAT starts at sector 0x20 (relative to the start of the partition), and the partition starts at 0x3F.
DAP is setup like this:

Code: Select all

.Size		DB 0x10
.NULL		DB 0x00
.Sectors	DW 0x01
.Offset		DW 0x8E00
.Segment	DW 0x00
.LBA		DD 0x5F
.LBA48		DD 0x00
I have tried several different ways to read from 07C0:1200, all of which are wrong. What's the best way to do this?

The RD cluster is located at the second entry of the FAT, so I think I need to read a DWORD from 07C0:1202. Is this correct, or do I have a misunderstanding?


Thankyou for your help
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: Bootloader - Read FAT

Post by Combuster »

for one, sizeof(DWORD) != 2. - it's 4 bytes
"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
mark3094
Member
Member
Posts: 164
Joined: Mon Feb 14, 2011 10:32 pm
Location: Australia
Contact:

Re: Bootloader - Read FAT

Post by mark3094 »

Combuster wrote:sizeof(DWORD) != 2. - it's 4 bytes
Yes, I was aware of that. But as you have pointed out, I was adding 2 for the number of entries, not the size of two entries.

Works now. Thank you
User avatar
Yoda
Member
Member
Posts: 255
Joined: Tue Mar 09, 2010 8:57 am
Location: Moscow, Russia

Re: Bootloader - Read FAT

Post by Yoda »

My OS boot tools have FAT32 boot sector which loads kernel file from any location and file doesn't need to be in the first cluster of root directory. Will that help you?
Yet Other Developer of Architecture.
OS Boot Tools.
Russian national OSDev forum.
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: Bootloader - Read FAT

Post by egos »

Also boot loader should not process a directory infinitely long :wink: MS recommends to use limit 2 mb for this.
If you have seen bad English in my words, tell me what's wrong, please.
User avatar
bubach
Member
Member
Posts: 1223
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re: Bootloader - Read FAT

Post by bubach »

Doesn't FAT32 actually respect the reserved sectors count so you could use that for second stage? As opposed to FAT12 for example where support for it is dodgy at best.
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
User avatar
Yoda
Member
Member
Posts: 255
Joined: Tue Mar 09, 2010 8:57 am
Location: Moscow, Russia

Re: Bootloader - Read FAT

Post by Yoda »

bubach wrote:Doesn't FAT32 actually respect the reserved sectors count so you could use that for second stage?
Since that reserved sector count is hardly predicted and may be any value from 2 to 2^16 it is unfair to rely on that space. If you intend to place second stage loader there and there are no enough reserved sectors you need to reformat file system. Also what would you do for FAT16? I think that the better solution is to place second stage loader into file.
Yet Other Developer of Architecture.
OS Boot Tools.
Russian national OSDev forum.
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: Bootloader - Read FAT

Post by egos »

Yes, but as I said earlier, in FAT32 ReservedSectors value almost always greater than 2. Usually at least 12 sectors are available. So we can use 5 sectors for boot loader without any problems: 12/2 (2 copies) - 1 (FSInfo) = 5. My FAT32 boot loader size is 1 kb. My FAT1x boot loader size is same but its second sector usually is located in file or in reserved cluster because MS recommends to use ReservedSectors value 1 for FAT1x.
If you have seen bad English in my words, tell me what's wrong, please.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Bootloader - Read FAT

Post by bluemoon »

I have just upgrade my boot loader to support dual booting ELF32 or ELF64 kernel with CPU detection. If anyone interested I may just share it here. it's about 600 lines of mixed 16/32/64 bits assembly.

Code: Select all


; This loader does:
; 1. Enable A20
; 2. Get BIOS memory map and store at Boot Data Block
; 3. Detect CPU
; 4. Load system files and machine initialization
;    32 BIT CPU:
;        - Load kernel32.bin and initrd32.bin
;        - Setup Protected mode, no paging
;    64 BIT CPU:
;        - Load kernel64.bin and initrd64.bin
;        - Setup Long Mode, identity mapped PML4T
;    * Loaded offset and size is record in Boot Data Block
; 5. Parse ELF32/ELF64 executable and JMP to entry point.
; PS. BSS is not cleared. Kernel need to clear it after setup logical address.
Tested on qemu-system-i386, qemu-system-x86_64(westmere host CPU) and macbook1,1 (oldest dual core)
User avatar
mark3094
Member
Member
Posts: 164
Joined: Mon Feb 14, 2011 10:32 pm
Location: Australia
Contact:

Re: Bootloader - Read FAT

Post by mark3094 »

Yoda wrote:My OS boot tools have FAT32 boot sector which loads kernel file from any location and file doesn't need to be in the first cluster of root directory. Will that help you?
egos wrote:Also boot loader should not process a directory infinitely long :wink: MS recommends to use limit 2 mb for this.
I'm trying to do similar things. Currently, if stage2.bin is in the first or second cluster of the root directory, it can be loaded without any problems. However, if I dump 150 files on the disk first, and then put in stage2.bin, I can't get it to load.

I think I need a better way of figuring out which sector of the FAT to load into memory. My current method is to get the RD entry, divide the cluster number by 128 (FAT entries per FAT sector). The quotient is the FAT sector number, the remainder is the entry in that sector.

eg, if the RD cluster is 0x2, 2/128 = FAT sector 0, entry 2
if the RD cluster is 0x83, 131/128 = FAT sector 1, entry 3.
Last edited by mark3094 on Tue Apr 24, 2012 5:39 am, edited 1 time in total.
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: Bootloader - Read FAT

Post by egos »

I work with root dir by the rules. I'm reading root dir cluster by cluster, each cluster sector by sector to find required dir entries. Your method is right to get a next cluster link/EOF mark. Use a cache for FAT sector and this will be all right.
If you have seen bad English in my words, tell me what's wrong, please.
Post Reply