File I/O, Command Line Parameters, Endian-ness Operating Systems CS3430

advertisement
File I/O, Command Line
Parameters, Endian-ness
Operating Systems
CS3430
Sarah Diesburg
1
Outline




File I/O in C
Passing arguments on the command line
Starter C code
More on serialization (hexedit, endian, safe
functions)
2
File I/O in C

Two resources:


A C language file I/O tutorial (link also found on
class Resources page)
Take a look at endian_test.c

Linked on today session webpage
3
Starting our Utility
$>./fat32_reader fat32.img
/]


Fat32_reader is name of our utility
Should return a different prompt (like “/]”) to
symbolize that user is inside utility
4
Alternatively…
$>java fat32_reader fat32.img
/]
$>python3 fat32_reader fat32.img
/]
$>python2 fat32_reader fat32.img
/]
5
Passing in Command Line
Parameters



Can be done with any language
Usually uses an argv[] and argc passed into
main
Let’s look at a C example:

args.c
6
Main Structure of our Utility

Our utility will
1.
2.
3.
Prompt the user for a command
Perform the command (or quit)
Repeat
Need a main loop and a way to compare
input to commands


fat32_reader.c - C starter code
7
Reserved Region

Reserved Region – Includes the boot
sector, the extended boot sector, the file
system information sector, and a few other
reserved sectors
Reserved
Region
Boot Sector
FS Information
Sector
FAT
Region
Additional
Reserved Sectors
(Optional)
Data
Region
Beginning Project 3

Start by reading boot sector fat32 information
Reserved
Region
Boot Sector


FS Information
Sector
FAT
Region
Data
Region
Additional
Reserved Sectors
(Optional)
As you work, it might make sense to first take
a look at the raw file system image
Hexedit to the rescue!
9
Hexedit
To install (run once):


$> sudo apt-get update
$> sudo apt-get install hexedit
To run:
$> hexedit [filename]


View files in hexadecimal or ASCII
Why wouldn’t you want to view the file system
image file in your regular editor?
10
Hexedit
11
Hexedit
Line
numbers in
hex
12
Hexedit
Content in
hex
13
Hexedit
Content in
printable
ASCII
14
Hexadecimal Hints


Hex is base 16 – one hexadecimal can
represent 0-15
It takes 4 binary bits to represent values 0-15


0000 = 0
1111 = 15
15
Hexadecimal Hints

If it takes 4 bits to represent one
hexadecimal number, it takes 8 bits to
represent two hexadecimal numbers


8 bits = 1 byte
Two hex numbers together symbolize one
byte

That’s why hex numbers are in groups of two
16
Endianness

FAT32 is represented in little endian byte
order


Reading left to right, you encounter leastsignificant byte first
What 32-bit number is this? 0x0000040 or
0x40000000?
17
Endianness

Why are characters in order (readable) if
some numbers are not?
18
Endianness

You must account for little endianness
across bytes when reading in numbers of
size larger than one byte


Characters are only one byte, no re-ordering
necessary
Use only safe functions when reading, to make
sure to adjust for the endian-ness of your
architecture

Ex. le8toh(num), le16toh(num), le32toh(num)
19
Important Boot Sector Information

Size of each region






Root directory (first directory in tree)


BPB_BytesPerSec
BPB_SecPerClus
BPB_RsvdSecCnt
BPB_NumFATS
BPB_FATSz32
BPB_RootClus
Warning: this list is not exhaustive!
20
Download