24
ENGI 3655 Lab Sessions 1 Richard Khoury 2. FAT

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

Embed Size (px)

Citation preview

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

ENGI 3655 Lab Sessions

1Richard Khoury

2. FAT

Page 2: ENGI 3655 Lab Sessions 1Richard Khoury.  Linked Allocation ◦ Section 11.4.2 Richard Khoury2

Linked Allocation◦ Section 11.4.2

Richard Khoury 2

Textbook Readings

Page 3: ENGI 3655 Lab Sessions 1Richard Khoury.  Linked Allocation ◦ Section 11.4.2 Richard Khoury2

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

What we did so far

Richard Khoury 3

Page 4: ENGI 3655 Lab Sessions 1Richard Khoury.  Linked Allocation ◦ Section 11.4.2 Richard Khoury2

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

FAT

Richard Khoury 4

Page 5: ENGI 3655 Lab Sessions 1Richard Khoury.  Linked Allocation ◦ Section 11.4.2 Richard Khoury2

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

Linked Allocation

Richard Khoury 5

Page 6: ENGI 3655 Lab Sessions 1Richard Khoury.  Linked Allocation ◦ Section 11.4.2 Richard Khoury2

FAT Disk Structure

Richard Khoury 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

SectorsFAT #1 FAT #2

Root Director

y

Data Region

Page 7: ENGI 3655 Lab Sessions 1Richard Khoury.  Linked Allocation ◦ Section 11.4.2 Richard Khoury2

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

FAT History

Richard Khoury 7

Page 8: ENGI 3655 Lab Sessions 1Richard Khoury.  Linked Allocation ◦ Section 11.4.2 Richard Khoury2

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)

FAT12

Richard Khoury 8

Page 9: ENGI 3655 Lab Sessions 1Richard Khoury.  Linked Allocation ◦ Section 11.4.2 Richard Khoury2

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

FAT BIOS Parameter Block

Richard Khoury 9

Page 10: ENGI 3655 Lab Sessions 1Richard Khoury.  Linked Allocation ◦ Section 11.4.2 Richard Khoury2

JMP mainbpbOEM db "ENGI3655"bpbBytesPerSector: DW 512bpbSectorsPerCluster: DB 1bpbReservedSectors: DW 1bpbNumberOfFATs: DB 2bpbRootEntries: DW 224bpbTotalSectors: DW 2880bpbMedia: DB 0xf0bpbSectorsPerFAT: DW 9bpbSectorsPerTrack: DW 18bpbHeadsPerCylinder: DW 2bpbHiddenSectors: DD 0bpbTotalSectorsBig: DD 0bsDriveNumber: DB 0bsUnused: DB 0bsExtBootSignature: DB 0x29bsSerialNumber:DD 0xa0a1a2a3bsVolumeLabel: DB "OUR FLOPPY "bsFileSystem: DB "FAT12 "

FAT12 BIOS Parameter Block

Richard Khoury 10

Page 11: ENGI 3655 Lab Sessions 1Richard Khoury.  Linked Allocation ◦ Section 11.4.2 Richard Khoury2

1. Load the Root Directory Table into memory2. Search for desired file name and get the

first sector address3. Load FAT into memory4. 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

Using FAT12

Richard Khoury 11

Page 12: ENGI 3655 Lab Sessions 1Richard Khoury.  Linked Allocation ◦ Section 11.4.2 Richard Khoury2

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

Using FAT12: Step 1

Richard Khoury 12

Page 13: ENGI 3655 Lab Sessions 1Richard Khoury.  Linked Allocation ◦ Section 11.4.2 Richard Khoury2

Find the root directory tablemov al, BYTE [bpbNumberOfFATs]mul WORD [bpbSectorsPerFAT]add ax, WORD [bpbReservedSectors]

Read root directory table into memorymov es, WORD [ROOT_SEGMENT]mov bx, WORD [ROOT_OFFSET]call ReadSectors

ReadSectors is an int 13h function like the one we made last week

Using FAT12: Step 1

Richard Khoury 13

Page 14: ENGI 3655 Lab Sessions 1Richard Khoury.  Linked Allocation ◦ Section 11.4.2 Richard Khoury2

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

Using FAT12: Step 2

Richard Khoury 14

DIR_Name DBDIR_Attr DBDIR_NTRes DBDIR_CrtTimeTenth DBDIR_CrtTime DWBPB_CrtDate DWDIR_LstAccDate DWDIR_FstClusHI DWDIR_WrtTime DWDIR_WrtDate DWDIR_FstClusLO DWDIR_FileSize DQ

Page 15: ENGI 3655 Lab Sessions 1Richard Khoury.  Linked Allocation ◦ Section 11.4.2 Richard Khoury2

Using FAT12: Step 2

Richard Khoury 15

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

Page 16: ENGI 3655 Lab Sessions 1Richard Khoury.  Linked Allocation ◦ Section 11.4.2 Richard Khoury2

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

Using FAT12: Step 3

Richard Khoury 16

Page 17: ENGI 3655 Lab Sessions 1Richard Khoury.  Linked Allocation ◦ Section 11.4.2 Richard Khoury2

We have the first sector of the file and the FAT Now what?

◦ Entries in FAT correspond to sectors in data region in the sameorder

◦ Current FAT entry number containsnumber 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 Khoury 17

Using FAT12: Step 4

Page 18: ENGI 3655 Lab Sessions 1Richard Khoury.  Linked Allocation ◦ Section 11.4.2 Richard Khoury2

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

Using FAT12: Step 4

Richard Khoury 18

Page 19: ENGI 3655 Lab Sessions 1Richard Khoury.  Linked Allocation ◦ Section 11.4.2 Richard Khoury2

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

Using FAT12: Step 4

Richard Khoury 19

Page 20: ENGI 3655 Lab Sessions 1Richard Khoury.  Linked Allocation ◦ Section 11.4.2 Richard Khoury2

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

Using FAT12: Step 4

Richard Khoury 20

Page 21: ENGI 3655 Lab Sessions 1Richard Khoury.  Linked Allocation ◦ Section 11.4.2 Richard Khoury2

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

Using FAT12: Step 4

Richard Khoury 21

Page 22: ENGI 3655 Lab Sessions 1Richard Khoury.  Linked Allocation ◦ Section 11.4.2 Richard Khoury2

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

Using FAT12: Step 4

Richard Khoury 22

Page 23: ENGI 3655 Lab Sessions 1Richard Khoury.  Linked Allocation ◦ Section 11.4.2 Richard Khoury2

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

Using FAT12: Step 4

Richard Khoury 23

Page 24: ENGI 3655 Lab Sessions 1Richard Khoury.  Linked Allocation ◦ Section 11.4.2 Richard Khoury2

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

Lab Assignment

Richard Khoury 24