FAT32 file system learning (1)-understanding of BPB

Source: Internet
Author: User
Tags readfile
Fat 32 file system learning

 

1. Objectives of this Article

This article will read a FAT32 format USB flash drive to learn and understand the format of the FAT32 file system. Although the mainstream File System Format of windwos Is NTFS, FAT32 still has some learning value due to its compatibility. In order to make a form Program provide an intuitive feeling, the code in this article is written in C #, and the corresponding C ++ code is also attached.

2. Contents of this Article

1. Objectives of this Article

2. What is FAT32?

3. Guide Area

2. What is FAT32?

FAT32 is a type of hard disk partition in the windwos system. This format uses a 32-bit File Allocation Table, which greatly enhances the disk management capability and breaks through the limit of 2 GB for a partition with fat16. Although [1] has been replaced by a better NTFS partition format. To put it bluntly, each item in the fat table has a length of 32 bits, so it is called FAT32. As for what each item stores, the following content will be analyzed slowly.

  • FAT32 Composition

The FAT32 file system divides the space of the Logical Disk into three parts: boot, fat, and data. The boot area and File Allocation Table area are also called the system area. In this article, we will learn about the content of the guide area, the file allocation table area and the data area in the next article.

3. Guide Area

Starting from the first sector, the boot area stores the number of bytes for each sector, the number of sectors for a cluster, the starting position of the fat table, the number of fat tables, and the number of sectors for the fat table. There are still several reserved sectors. First, let's take a look at how to read the boot area from a USB flash drive (Here we only read the first sector, and the next read method is the same ). To directly read the logical sector of the disk, we need to use several functions in Windows API, namely createfile (the handle used to create the disk here) and readfile (used to read the disk sector here ). First, let's take a look at the definitions of these two functions:

1 handle winapi createfile (2 _ in _ lpctstr lpfilename, // name of the file to be opened or device name. 3 _ in _ DWORD dwdesiredaccess, // Access Object of the specified type. 4 _ in _ DWORD dw1_mode, // file sharing mode 5 _ in_opt _ lpsecurity_attributes lpsecurityattributes, // defines the file security feature 6 _ in _ DWORD dwcreationdisposition, 7 _ in _ DWORD dwflagsandattributes, 8 _ in_opt _ HANDLE htemplatefile // return handle 9 );

This function can be found on msnd. It should be noted that dw1_mode needs to be specified as file_1__write to read the data in the sector (here is why I am not quite sure why, I hope you can advise me ). Dwcreationdisposition must be set to open_existing, indicating that the file must already exist and requested by the device. If the function is successfully executed, the file handle is returned. Otherwise, the returned handle = invalid_handle_value indicates an error and getlasterror is set. You can query the error code for the specific failure cause.

The second function is readfile, which is defined as follows:

1 bool readfile (2 handle hfile, // file handle 3 lpvoidl pbuffer, // a buffer used to save read data 4 DWORD nnumberofbytestoread, // The number of bytes to be read 5 lpdword lpnumberofbytesread, // pointer to the actual number of bytes read 6 lpoverlapped lpoverlapped7 // If the file_flag_overlapped is specified when the file is opened, it is required, use this parameter to reference a special structure. 8 // This structure defines an asynchronous read operation. Otherwise, set this parameter to null_9 );

This function is used to read files. Here it refers to reading the USB flash drive sector. Set lpoverlapped to null. Note that because the disk reads and writes data in units of sectors, the number of bytes read here must be a multiple of 512! Other parameter annotations are clearly written, so we will not explain them here. The setfilepointer function can be used to read data of specified bytes at the specified starting position.

First, let's take a look at how C ++ reads data from the boot sector.

1 // The author's USB flash drive letter is G 2 wchar szdiscfile [] = _ T ("\\\\. \ G: "); 3 // open the device handle 4 handle hdisc = createfile (szdiscfile, generic_read, file_assist_write, null, open_existing, 0, null ); 5 If (hdisc = invalid_handle_value) 6 {7 // failed to open the device 8 return 0; 9} 10 // read 11 setfilepointer (hdisc, 0, 0, file_begin); 12 // number of bytes to be read 13 DWORD dwnumber2read = 512; 14 // Number of actually read bytes 15 DWORD dwrealnumber; 16 // allocate the buffer 17 char * buffer = new char [512]; 18 bool Bret = readfile (hdisc, buffer, dwnumber2read, & dwrealnumber, null ); 19 20 // run the tail scan to release the buffer and close the handle 21 Delete [] buffer; 22 closehandle (hdisc );

If we can see the buffer data in the memory at each breakpoint in line 21, the author directly saves the 512 bytes of data to the file for convenience, some data is shown in the following figure:

  • BPB Parameters

Okay. Next we will focus on it. First, the data in the first three bytes is the jump command and the empty command, because in the compilation, 0xeb is the jump command, 0x58 is the jump address, and 0x90 is the empty command. As for why I want to add a jump command here, I have to start from the start zone. To save space, I will briefly introduce it: Generally, the first sector is called the start zone, the CPU executes the data in the sector as an instruction. When the instruction EB 58 is read, it jumps to the address 0x58 and continues to read the instruction for execution, the content after the address 0x58 is usually a command loaded into the operating system. If you want to know the details, take a look at the book "30-day self-made operating system". The last part of the first day has a detailed description. In short, FAT32 specifies that the content of each 3 byte must be EB 58 90, just remember it (Laugh ).

From 0x03 ~ The nine bytes of data 0x0a indicate OEM. Here it is "msdos5.0 ".

We call the 79 bytes of data starting from 0x000b as BPB (BiOS paramter block). For details about BPB, see the following table [5]:

BPB parameter information
Offset Bytes Description Value
0x00b 2 Number of words per sector Zero X 0200
0x00d 1 Number of sectors per cluster 0x08
0x00e 2 Number of reserved sectors 0x03f8
0x010 1 Fat count 0x02
0x011 2 Number of root directory items, FAT32 to break through this limit, invalid Zero x 0000
0x013 2 Total number of sectors, less than 32 m Zero x 0000
0x015 1 Negative storage medium description 0x0f8
0x016 2 Number of sectors occupied by each fat table, less than 32 m Zero x 0000
0x018 2 Number of sectors per track in logic 0x003f
0x01a 2 Logical magnetic head count 0x00ff
0x01c 4 Number of hidden sectors in the system Zero X 00000080
0x020 4 Total number of sectors, more than 32 m 0x00784f80
0x024 4 Number of sectors per fat table, larger than 32 m 0x00001e04
0x028 2 Mark Zero x 0000
0x02a 2 Version (usually zero) Zero x 0000
0x02c 4 Root directory start Cluster Zero X 00000002
0x030 2 Number of sectors occupied by boot Zero X 0001
0x032 2 Backup guide sector position Zero X 0006
0x034 14 Retained 0x00 of 14 bytes
0x042 1 Extended boot tag 0x29
0x043 4 Serial number 0x6a9c4125
0x047 10 Scale Convert to a character that is "no name"
0x052 8 File System Converts the string to "FAT32"

 

In order to use the form program to intuitively display each parameter, rewrite the above program to C #. As we all know, most C # Calls to system functions are implemented by indirectly Calling C's dynamic library. Here, createfile and readfile are no exception. Next we will write a filereader class to provide these system API calls. Part of the Code is as follows. Click here to download the complete filereader.

class FileReader{  [System.Runtime.InteropServices.DllImport("kernel32", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = System.Runtime.InteropServices.CharSet.Unicode)]    static extern unsafe System.IntPtr CreateFile    (        string FileName,          // file name        uint DesiredAccess,       // access mode        uint ShareMode,           // share mode        uint SecurityAttributes,  // Security Attributes        uint CreationDisposition, // how to create        uint FlagsAndAttributes,  // file attributes        int hTemplateFile         // handle to template file    );
  public bool Open(string FileName) { // open the existing file for reading handle = CreateFile ( FileName, GENERIC_READ, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0 ); if (handle != System.IntPtr.Zero) { return true; } else { return false; } }

Because the focus of this article is not to learn C # form programming, skip this part and directly call the system API provided by filereader and compile the form program, shows the effect (for details about this project, refer to [3]). If you need a complete project, click here to download it. Open vs2012 or above.

Now, this article is written here. I am not very familiar with FAT32, and I am also the first to learn FAT32. If there is any mistake, I would like to criticize and correct it. At the same time, this is my first essay. If there is something wrong with typographical layout, you are welcome to submit it. Next, we will continue to learn the next fat table area and data area, and continue to update the next learning experience.

 

References:

1, http://baike.baidu.com/view/45233.htm? Fr = Aladdin

2. FAT32 File Format http://blog.csdn.net/shrekmu/article/details/5950414

3, read and write U disk (FAT32) Boot Sector http://blog.csdn.net/zhanglei8893/article/details/5912903

4. F? A? T? 3? 2? Text? ? System? Unified? Ge? ? Details? Solution http://wenku.baidu.com/link? Url = zrGv8nld-bc-7KT_TKbo2vWplaiIHhmJ9_ydRZBZdZ4zy8odQFwS6komz2gz1AHX36T_EN1CKZ_16d19upW9pDauno6zEmpw10wlTSTwcoi

5. Basic? Yu? U? Disk? F? A? T? 3? 2? Text? ? System? Unified? ? Points? Analysis http://wenku.baidu.com/link? Url = cIKgrwV66y4CoyuOEB1-OhjRY9tnXtIAoZuYEwDCjxbyRomSIiJgBAXGxq6LudfwuopUpYhiVd8TjxrBFoVyPs0NX3OqbnoWjyn4ZAx60Wi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.