Presentation is loading. Please wait.

Presentation is loading. Please wait.

ENGI 3655 Lab Sessions 1Richard Khoury.  Linked Allocation ◦ Section 11.4.2 Richard Khoury2.

Similar presentations


Presentation on theme: "ENGI 3655 Lab Sessions 1Richard Khoury.  Linked Allocation ◦ Section 11.4.2 Richard Khoury2."— Presentation transcript:

1 ENGI 3655 Lab Sessions 1Richard Khoury

2  Linked Allocation ◦ Section 11.4.2 Richard Khoury2

3  Last week we built a multi-stage bootloader ◦ Eventually, the second stage will load the kernel, which will load the entire OS  We did this by writing the stage-2 loader in head 0 cylinder 0 sector 2, right after the stage-1 boot sector ◦ Necessary to know where it was so we could tell Int13h to read the correct sector ◦ Clearly, this is not ideal when we have a lot of files on the disk Richard Khoury3

4  We will add a file system to our bootloader ◦ This will then enable us to search for files, such as our second-stage loader  File Allocation Table (FAT) ◦ Common file system ◦ Named after the FAT that keeps track of sector allocation Richard Khoury4

5  A file can be larger than one sector ◦ Has to be broken up and written in several sectors ◦ Sectors might not be contiguous on disk  FAT keeps track of sector & order for file ◦ File directory entry only knows first sector ◦ Lookup in FAT for the rest Richard Khoury5

6 6  Boot Sector should be no mystery to you now  Reserved Sectors are optional  FAT #1 is the file allocation table  FAT #2 is a redundant file allocation table  Root Directory (FAT12 and FAT16 only) is a table containing information about files and directories ◦ FAT32 marks a root directory cluster in the data region  Data Region is where your data is stored Boot Sector Reserved Sectors FAT #1FAT #2 Root Directory Data Region

7  FAT12 (1980) ◦ First and simplest version, basis for later versions ◦ Maximum number of clusters: 4,077 – 4,084 ◦ Maximum volume size: 32MB ◦ No support for hierarchical directory structure (circumvented by OS since MS-DOS 2.0) ◦ Standard FAT for floppy disks  FAT16 (1987) ◦ Maximum number of clusters: 65,517 ◦ Maximum volume size: 2GB  FAT32 (1996) ◦ Maximum number of clusters: 268,435,437 ◦ Maximum volume size: 2TB ◦ Standard FAT for flash memory devices Richard Khoury7

8  We will build the simplest one, FAT12 ◦ All the basic concepts and algorithms of FAT32, but easier to use and program ◦ Downside: we’ll pretend our USB is a 3.5 inch floppy, 1.44MB ◦ File names fixed to 8.3 format (padded with spaces when necessary) Richard Khoury8

9  The Boot Sector needs to define the BIOS Parameter Block  Set of constants that describe parameter values of the file system and disk  Must be located exactly at the beginning of the block  Values are written automatically when disk is formatted ◦ And we included them in our stage-1 bootloader Richard Khoury9

10 JMP main bpbOEMdb "ENGI3655" bpbBytesPerSector: DW 512 bpbSectorsPerCluster: DB 1 bpbReservedSectors: DW 1 bpbNumberOfFATs: DB 2 bpbRootEntries: DW 224 bpbTotalSectors: DW 2880 bpbMedia: DB 0xf0 bpbSectorsPerFAT: DW 9 bpbSectorsPerTrack: DW 18 bpbHeadsPerCylinder: DW 2 bpbHiddenSectors: DD 0 bpbTotalSectorsBig: DD 0 bsDriveNumber: DB 0 bsUnused: DB 0 bsExtBootSignature: DB 0x29 bsSerialNumber:DD 0xa0a1a2a3 bsVolumeLabel: DB "OUR FLOPPY " bsFileSystem: DB "FAT12 " Richard Khoury10

11 1. Load the Root Directory Table into memory 2. Search for desired file name and get the first sector address 3. Load FAT into memory 4. For each sector address a.Load content of sector from data region into memory b.Load position of next sector from current address position in FAT Richard Khoury11

12  A directory table is a data structure that represents a directory (folder) ◦ Each file in the directory is represented as a 32- byte entry in the table  Root Directory Table occupies a special location on the disk ◦ 1 (boot sector) + # of hidden sectors + # of FATs x # of sectors per FAT ◦ “Root Directory Region” ◦ Other directory tables are in the Data Region  That’s what we’ll use to handle files Richard Khoury12

13  Find the root directory table mov al, BYTE [bpbNumberOfFATs] mul WORD [bpbSectorsPerFAT] add ax, WORD [bpbReservedSectors]  Read root directory table into memory mov es, WORD [ROOT_SEGMENT] mov bx, WORD [ROOT_OFFSET] call ReadSectors  ReadSectors is an int 13h function like the one we made last week Richard Khoury13

14  Each file in the RTD is represented as a 32- byte entry ◦ File name is first 11 bytes ◦ First cluster is in two parts at offset 20 (14h) and 26 (1Ah)  Search for desired file name to get the first sector address Richard Khoury14 DIR_Name DB DIR_Attr DB DIR_NTRes DB DIR_CrtTimeTenthDB DIR_CrtTime DW BPB_CrtDate DW DIR_LstAccDate DW DIR_FstClusHI DW DIR_WrtTime DW DIR_WrtDate DW DIR_FstClusLO DW DIR_FileSize DQ

15 Richard Khoury15  Set loop counter to the number of entries in the RDT  Start at the beginning of the RDT  We will use the cmpsb function ◦ Compares two registers (which ones?)  We compare the target file name  With the current RDT entry file name  Compare the first 11 bytes to the file name  If they don’t match, skip 32 bytes to next directory entry  If we’re out of entries, the file doesn’t exist

16  Load FAT into memory  We need to locate the FAT on disk ◦ Easy: It’s at the beginning, after the reserved region! mov ax, WORD [bpbReservedSectors] call ReadSectors Richard Khoury16

17  We have the first sector of the file and the FAT  Now what? ◦ Entries in FAT correspond to sectors in data region in the same order ◦ Current FAT entry number contains number of next FAT entry number  Do until entire file read ◦ Convert the entry number to disk address and read to memory ◦ Get the next entry/sector from the current entry Richard Khoury17

18  In FAT12, each address is 12 bits ◦ But we can only read bytes (8 bits) or words (16 bits) ◦ We cannot read exactly one address  We will have to ignore part of what we read ◦ Two consecutive addresses A and B will cover three bytes AAAAAAAA AAAABBBB BBBBBBBB ◦ For address A we will keep the first word and discard the last four bits ◦ For address B we will keep the second word and discard the first four bits Richard Khoury18

19  Assuming we know the cluster number in the FAT and the cluster address word it contains  We alternate between two kinds of numbers, A and B, or odd and even with A being odd ◦ test function allows us to check if the address is odd (in binary: ends with 1) ◦ When the number is even, we keep the lower twelve bits ◦ When the number is odd, we keep the upper twelve bits  Store the address of the next cluster  After we stored the next cluster, we check if it is the end-of-file cluster ◦ Contains the special marker address (what is it?) ◦ If not, we have to keep loading clusters Richard Khoury19

20  How did we know the cluster number in the FAT and the cluster address word it contains? ◦ Getting the cluster number is easy: we know the initial cluster and we load the next cluster in each loop ◦ Computing the location of the cluster in memory  1.5 times the current cluster number, since each cluster takes 1.5 bytes (12 bits)  Add to that the starting address of the FAT Richard Khoury20

21  So now we can read the FAT and get all the clusters of our file  But we’ll also need to read these clusters from the disk into memory!  Problem is, we have a cluster number, and Int 13h needs a head/track/sector number Richard Khoury21

22  Clusters represent groups of sectors  Convert the cluster number to logical sector number ◦ Recall bpbSectorsPerCluster in BPB ◦ Clusters are simply numbered sequentially starting at 0, and cover the data region ◦ LogicalSector = (cluster – 2) * SectorsPerCluster + start of data region  The LogicalSector can then be converted to head/track/sector number ◦ Head Number = (LogicalSector / SectorPerTrack) mod NumberOfHeads ◦ Track Number = (LogicalSector / SectorPerTrack) / NumberOfHeads) ◦ Sector Number = (LogicalSector mod SectorPerTrack) + 1 Richard Khoury22

23  And where does the data region start? ◦ Right at the end of our Root Directory Table! ◦ We could have computed that back in Step 1 and saved it...  Now we can read the file, sector by sector, using our Int 13h routine Richard Khoury23

24  I’ve given you the FAT12 functions  I want you to read them, understand them, and prove it by commenting them ◦ The functions to comment are: ◦ ClusterLBA, LBACHS, LOAD_ROOT, LOAD_FAT, FindFile, LoadFile ◦ The comments are in these slides  To get a sense of FAT32 as well, go over the FAT12 BPB and mark (in comments) which entries are no longer used (read the documentation!)  Also, compile the new Stage-1 bootloader ◦ Place onto USB stick using DD ◦ Then copy the stage-2 onto USB stick using a copy command Richard Khoury24


Download ppt "ENGI 3655 Lab Sessions 1Richard Khoury.  Linked Allocation ◦ Section 11.4.2 Richard Khoury2."

Similar presentations


Ads by Google