OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 11:17 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 14 posts ] 
Author Message
 Post subject: What do i need to do to find file on fat12 in bootsect
PostPosted: Tue Nov 22, 2022 6:58 pm 
Offline

Joined: Tue Nov 22, 2022 6:55 pm
Posts: 2
What do i need to do to find a file with a specified name on a fat12 partitioned disk in the boot sector


Top
 Profile  
 
 Post subject: Re: What do i need to do to find file on fat12 in bootsect
PostPosted: Sun Nov 27, 2022 8:07 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
Read the root directory into memory and search for the file with the correct name. If you want to load that file, follow the cluster chain in the FAT.

Microsoft published a specification for FAT12 (and FAT16 and FAT32) as part of the EFI specification. Here's a copy of that specification converted to PDF. You can download the original Word document directly from Microsoft.

I've written code to load a file from a FAT12 partition on a hard disk. You can find it here if you're interested. (I've made a few updates recently that haven't yet made it onto Github... nothing important, though; just changing the load address.)


Top
 Profile  
 
 Post subject: Re: What do i need to do to find file on fat12 in bootsect
PostPosted: Tue Nov 29, 2022 8:15 am 
Offline
Member
Member

Joined: Wed Oct 01, 2008 1:55 pm
Posts: 3191
I don't think that is possible within the limited space of the boot sector. You need to load a second stage loader (from fixed sectors) in the boot sector, and then load the file in the second stage loader.


Top
 Profile  
 
 Post subject: Re: What do i need to do to find file on fat12 in bootsect
PostPosted: Tue Nov 29, 2022 9:30 am 
Offline
Member
Member

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 1071
rdos wrote:
I don't think that is possible within the limited space of the boot sector.

Sure it is! See http://www.brokenthorn.com/Resources/OSDev6.html

_________________
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg


Top
 Profile  
 
 Post subject: Re: What do i need to do to find file on fat12 in bootsect
PostPosted: Tue Nov 29, 2022 9:53 am 
Offline
Member
Member

Joined: Wed Oct 01, 2008 1:55 pm
Posts: 3191
nexos wrote:
rdos wrote:
I don't think that is possible within the limited space of the boot sector.

Sure it is! See http://www.brokenthorn.com/Resources/OSDev6.html


OK, it might be possible but it is still not a good idea. By keeping stage2 in the root directory you risk it getting deleted or corrupted, so a better idea is to place it in reserved sectors directly after the boot sector. Additionally, you might decide to format your boot partition as FAT32, EXT or NTFS, and then you need a new boot sector to load the second stage, and it might not be possible to handle reading the root directory of EXT or NTFS from the boot sector.


Top
 Profile  
 
 Post subject: Re: What do i need to do to find file on fat12 in bootsect
PostPosted: Tue Nov 29, 2022 11:37 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
rdos wrote:
By keeping stage2 in the root directory you risk it getting deleted or corrupted,

Everything is in the root directory! By that logic, you shouldn't store any part of your OS as files on the disk because that risks it getting deleted or corrupted.

rdos wrote:
it might not be possible to handle reading the root directory of EXT or NTFS from the boot sector.

That's why EXT2 and NTFS have more than one sector for the boot "sector".


Top
 Profile  
 
 Post subject: Re: What do i need to do to find file on fat12 in bootsect
PostPosted: Tue Nov 29, 2022 1:01 pm 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1593
rdos wrote:
it might not be possible to handle reading the root directory of EXT or NTFS from the boot sector.

That's why EXT2 and NTFS have more than one sector for the boot "sector".[/quote]
Well, you say that, but my previous attempt to write a real-mode ext2 file loader finally clocked in at 2048 bytes. Unfortunately, ext2 only gives me 1024 bytes of space (irrespective of block size, by the way).

However, those 1024 bytes are enough to load i-node 5 (which is the ext2 reserved i-node for the boot loader). So if I had gone that route, I would have had routines to load sectors in the first 512 bytes, routines to load i-nodes in the second 512 bytes, then skip 1024 bytes for superblock and block group descriptors, then routines to traverse directories in however much memory was needed.

But I never finished it. My main goal was to only use the first 1024 bytes. But it seems like that is just not enough space for
  • enabling A20-gate
  • testing if the CPU is acceptable
  • loading second bootloader block and superblock
  • traversing directories to find a programmable path name
  • finally loading that one to the 1MB line
  • mapping it as an ELF file
  • doing the rest of stuff my kernel needs
  • and finally transitioning to 64-bit mode

_________________
Carpe diem!


Top
 Profile  
 
 Post subject: Re: What do i need to do to find file on fat12 in bootsect
PostPosted: Tue Nov 29, 2022 1:30 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
nullplan wrote:
But it seems like that is just not enough space for
  • enabling A20-gate
  • testing if the CPU is acceptable
  • loading second bootloader block and superblock
  • traversing directories to find a programmable path name
  • finally loading that one to the 1MB line
  • mapping it as an ELF file
  • doing the rest of stuff my kernel needs
  • and finally transitioning to 64-bit mode

I don't think anyone is suggesting that you do all of those things within the EXT2 boot block; just finding and loading the second stage is enough.


Top
 Profile  
 
 Post subject: Re: What do i need to do to find file on fat12 in bootsect
PostPosted: Tue Nov 29, 2022 2:28 pm 
Offline
Member
Member

Joined: Wed Oct 01, 2008 1:55 pm
Posts: 3191
Octocontrabass wrote:
rdos wrote:
By keeping stage2 in the root directory you risk it getting deleted or corrupted,

Everything is in the root directory! By that logic, you shouldn't store any part of your OS as files on the disk because that risks it getting deleted or corrupted.


Not in my design. I can have several bootable images in the root directory, and if one is deleted or corrupt I can always boot the other. That's why I call the second one for "safe". However, the second stage loader only exists in a single instance, and it is never modified, so it's better placed in reserved sectors.

For UEFI boot, things are a bit more safe since the loaders & images are on it's own UEFI system partition.


Top
 Profile  
 
 Post subject: Re: What do i need to do to find file on fat12 in bootsect
PostPosted: Tue Nov 29, 2022 2:46 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
You can have a separate boot partition on legacy BIOS too.


Top
 Profile  
 
 Post subject: Re: What do i need to do to find file on fat12 in bootsect
PostPosted: Fri Dec 23, 2022 2:49 pm 
Offline
Member
Member
User avatar

Joined: Sun Aug 25, 2013 5:47 pm
Posts: 86
Location: Nebraska, USA
Late to the party, but here is also a fat32 implementation which loads a file in the "stage1": https://bitbucket.org/AlexOSAdventurer/alo_chainloader/src/master/src/boot_stage1/ . This is if you're wanting something that will work with a large fat system. I heavily commented it to death too so it should be straightforward to understand the process.

_________________
"Procrastination is the art of keeping up with yesterday."


Top
 Profile  
 
 Post subject: Re: What do i need to do to find file on fat12 in bootsect
PostPosted: Mon Feb 06, 2023 4:01 am 
Offline
Member
Member

Joined: Wed Oct 01, 2008 1:55 pm
Posts: 3191
ScropTheOSAdventurer wrote:
Late to the party, but here is also a fat32 implementation which loads a file in the "stage1": https://bitbucket.org/AlexOSAdventurer/alo_chainloader/src/master/src/boot_stage1/ . This is if you're wanting something that will work with a large fat system. I heavily commented it to death too so it should be straightforward to understand the process.


Well, it has no Bios Parameter Block (BPB), which can cause problems in non-Linux environments. I prefer to use the BPB and set hidden sectors so I can write the 2:nd stage loader directly after the boot sector instead of trying to push everything into a single sector.

My second stage loader includes a menu to select a boot file, and works with FAT12, FAT16 and FAT32. It also knows how to switch to protected mode, enable A20, and find the kernel device driver in the boot file so it can jump to it. It also knows how to scan for ACPI and to create a memory map from BIOS. My second stage loader is 7220 bytes (15 sectors), and there is no way to fit that into the boot sector.

I have no plans to support booting from EXT or NTFS, but I do have plans to support EXT as a filesystem.

The use of the BPB and hidden sectors is particularly useful for USB discs, as I can write the boot sector & loader, and format the boot partition with my OS, and then use my ordinary Windows PC to place boot images on them. This will not work with EXT hacks.


Top
 Profile  
 
 Post subject: Re: What do i need to do to find file on fat12 in bootsect
PostPosted: Tue Feb 07, 2023 9:59 pm 
Offline
Member
Member
User avatar

Joined: Sun Feb 18, 2007 7:28 pm
Posts: 1564
Hi,

Can just reference our fat32 boot code here which does have a BPB. It can be done, it just requires a lot of optimization to minimize the code as much as possible. Is it worth it? Honestly, probably not. It took a lot of effort to minimize the code to fit within the 512 bytes. Did learn a few new tricks though. Its been some time but my 2nd stage is easily >30k. Dont think anyone is suggesting to put that in 512 bytes.

_________________
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}


Top
 Profile  
 
 Post subject: Re: What do i need to do to find file on fat12 in bootsect
PostPosted: Sat Apr 15, 2023 6:49 pm 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 1108
I just made a major update to BootProg.
It may help with FAT-based booting (BootProg supports both CHS and LBA, FAT1x and FAT32).


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 29 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group