Page 1 of 1
Second stage bootloader filename doesn't work with lowercase
Posted: Wed Jan 29, 2014 12:03 pm
by shahsunny712
I am referring to BrokenThorn's tutorial on OS development and am currently at the chapter on writing the first stage bootloader and loading the second stage.
I wrote the whole code and ran into crashes on running it using qemu. After some debugging, I found out that the problem was with the name of the second stage bootloader, which I'm reading by looping through the root directory entries.
If I use the filename for the second stage as INITKRNL.BIN, everything works fine. But if I use initkrnl.bin (thus the difference being only in the case of the name), it crashes.
I also printed the name being read from the root directory. For the upper case name, it reads INITKRNL.BIN only. For the lower case name, it reads some letters of the name and some random characters.
Why does the case of the file name matter, and what is the reason for this difference ?
Note : I'm using Ubuntu 13.04 as my development environment. I format the floppy image with the VFAT file system using mkfs.vfat.
Re: Second stage bootloader filename doesn't work with lower
Posted: Wed Jan 29, 2014 1:35 pm
by Combuster
FAT has a fair share of legacy. Real file names are always in uppercase 8.3 format, and it would actually be stored as "INITKRNLBIN" (without the period). After a file you can add a legible filename according to the LFN scheme, but it's not mandatory. Since it's also more complicated, you won't see that feature used in bootloaders, which revert to the simple name.
So if you use a filename that doesn't fit the simple scheme, the fat driver will make something up for you that only superfluously resembles the name you think you gave it.
Re: Second stage bootloader filename doesn't work with lower
Posted: Wed Jan 29, 2014 2:44 pm
by Owen
I'll also add:
FAT mandates upper case short file names, so for the name "initkrnl.bin" the short name would have to be "INITKRNLBIN".
However, Linux wants to preserve case, so it stores "initkrnl.bin" as a long name (Windows will do the same).
The difference arises because of patents. Microsoft have a patent covering storing of both short and long names. Pretty much every FAT driver can cope with files which don't have a valid short name (and they must support files which only have a short name), so Linux works around this patent by not storing a short name when it stores a long name, which it will do whenever the short name cannot represent the chosen name.
(There is a flag somewhere to turn on storing both names. If you are outside the US, the patent does not apply to you and you can turn on this flag if you wish)
Re: Second stage bootloader filename doesn't work with lower
Posted: Wed Jan 29, 2014 3:51 pm
by Combuster
I thought the patent in question expired somewhere last year...
Re: Second stage bootloader filename doesn't work with lower
Posted: Wed Jan 29, 2014 9:36 pm
by Octocontrabass
Windows NT and upwards will use two extra bits somewhere in the directory entry to represent the case of a short file name, and only write a long file name if the case can't be represented in those two bits. Linux, by default, will not.
Mounting your disk image with the option "shortname=winnt" should cause the same short name to be written for both "INITKRNL.BIN" and "initkrnl.bin" (and also "initkrnl.BIN" and "INITKRNL.bin").
Re: Second stage bootloader filename doesn't work with lower
Posted: Thu Jan 30, 2014 1:22 am
by wichtounet
Combuster wrote:I thought the patent in question expired somewhere last year...
If I'm not mistaken, there are still two years before the end of this damn patent... (
http://www.google.com/patents/US5758352)
Re: Second stage bootloader filename doesn't work with lower
Posted: Thu Jan 30, 2014 10:58 am
by shahsunny712
Thank you for all the quick replies.
But I think I am even more confused now.
The name I am using is exactly 8.3 characters. So shouldn't it be stored that way ? Where does the long/short name come into play here, and why does simply changing the case make it work ?
Is it something to do with using Linux?
If someone could explain what value is written and why, it would be very helpful.
Re: Second stage bootloader filename doesn't work with lower
Posted: Thu Jan 30, 2014 11:59 am
by Owen
Owen wrote:FAT mandates upper case short file names, so for the name "initkrnl.bin" the short name would have to be "INITKRNLBIN".
Re: Second stage bootloader filename doesn't work with lower
Posted: Thu Jan 30, 2014 12:20 pm
by JAAman
shahsunny712 wrote:Thank you for all the quick replies.
But I think I am even more confused now.
The name I am using is exactly 8.3 characters. So shouldn't it be stored that way ? Where does the long/short name come into play here, and why does simply changing the case make it work ?
Is it something to do with using Linux?
If someone could explain what value is written and why, it would be very helpful.
maybe if i repeat...
IT IS NOT POSSIBLE TO USE LOWER CASE FILENAMES IN FAT
if you create a filename using lower case letters, you will then instead of having the filename in lower case letters, a filename will be created using random numbers and letters (in SOME cases portions of the filename might be the same changed to all caps, but that cannot be relied on)
Re: Second stage bootloader filename doesn't work with lower
Posted: Thu Jan 30, 2014 3:54 pm
by Octocontrabass
shahsunny712 wrote:The name I am using is exactly 8.3 characters. So shouldn't it be stored that way ? Where does the long/short name come into play here, and why does simply changing the case make it work ?
Is it something to do with using Linux?
FAT stores short file names as all-uppercase, but it has some extra data to specify when the name or extension should be lowercase. Windows NT uses that extra data, so that "initkrnl.bin" and "INITKRNL.BIN" will both be written as "INITKRNLBIN", and the only difference between the two will be the extra data.
Linux, on the other hand, will only allow you to store an all-uppercase file name as a short file name. This means "INITKRNL.BIN" will end up as "INITKRNLBIN", but "initkrnl.bin" will use a long file name instead. The short file name cannot be blank, which is why Linux writes the random characters.
You can make Linux do the same thing as Windows NT by mounting your filesystem with the "shortname=winnt" option.
JAAman wrote:IT IS NOT POSSIBLE TO USE LOWER CASE FILENAMES IN FAT
Tell that to Windows NT.
Re: Second stage bootloader filename doesn't work with lower
Posted: Fri Jan 31, 2014 12:07 pm
by shahsunny712
Thanks for your replies guys.
Okay, so here's what I gather:
FAT stores short file names in only upper case. For lower case, you need a long file name which specifies the case. In Windows NT, this is read to determine the case. If I do want to have a lower case support, I would have to store a long file name and read off the extra data.
Linux stores only upper case names as a short name. Others end up as long names, and the short name is given random characters.
Assuming this is right(please correct me if I am wrong), questions:
- Why can't Linux also use a long file name and write the upper case version in the short name value (why does FAT have to be different for Windows and Linux, assuming thats a valid question), and use the extra data to store the case (like in NT) ?
- How can I get the name with the correct case in Linux ?
Re: Second stage bootloader filename doesn't work with lower
Posted: Fri Jan 31, 2014 12:36 pm
by Combuster
shahsunny712 wrote:- Why can't Linux also use a long file name and write the upper case version in the short name value (why does FAT have to be different for Windows and Linux, assuming thats a valid question), and use the extra data to store the case (like in NT) ?
Owen wrote:The difference arises because of patents. (...)
shahsunny712 wrote:- How can I get the name with the correct case in Linux ?
Octocontrabass wrote:You can make Linux do the same thing as Windows NT by mounting your filesystem with the "shortname=winnt" option.
The fact that I could answer both with quotes from this very thread is a really bad indication of your reading skills. Please, take some effort to read things through instead of blindly asking.