1 Submission Turn in your well-formatted and commented source code and a copy of the output as two separate files. Be re

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

1 Submission Turn in your well-formatted and commented source code and a copy of the output as two separate files. Be re

Post by answerhappygod »

1 Submission Turn In Your Well Formatted And Commented Source Code And A Copy Of The Output As Two Separate Files Be Re 1
1 Submission Turn In Your Well Formatted And Commented Source Code And A Copy Of The Output As Two Separate Files Be Re 1 (50.99 KiB) Viewed 24 times
1 Submission Turn In Your Well Formatted And Commented Source Code And A Copy Of The Output As Two Separate Files Be Re 2
1 Submission Turn In Your Well Formatted And Commented Source Code And A Copy Of The Output As Two Separate Files Be Re 2 (54.22 KiB) Viewed 24 times
1 Submission Turn In Your Well Formatted And Commented Source Code And A Copy Of The Output As Two Separate Files Be Re 3
1 Submission Turn In Your Well Formatted And Commented Source Code And A Copy Of The Output As Two Separate Files Be Re 3 (49.04 KiB) Viewed 24 times
1 Submission Turn In Your Well Formatted And Commented Source Code And A Copy Of The Output As Two Separate Files Be Re 4
1 Submission Turn In Your Well Formatted And Commented Source Code And A Copy Of The Output As Two Separate Files Be Re 4 (52.52 KiB) Viewed 24 times
1 Submission Turn in your well-formatted and commented source code and a copy of the output as two separate files. Be ready to discuss your work with me. 2 Introduction In this lab, you will gain hands-on experience reading a FAT-12 file system. Using information we provide, you will decode the boot sector by hand. You will then write a program to do this automatically. 2.1 Terms Sector the smallest unit of transfer; It's also called a block. There are 512 bytes / sector on a floppy disk. Boot sector stores vital information about the file system. The boot sector is laid out in the following way, starting at the beginning of the disk (logical sector 0, byte 0): All values are stored as unsigned little-endian numbers unless otherwise specified. Contents Offset Length 0x00 3 0x03 Display Format hex Binary offset of boot loader 8 Volume Label (ASCII, null padded) Bytes / sector ASCII OxOB decimal OxOD Sectors / cluster decimal Reserved sectors decimal OxOE 0x10 Number of FATs (generally 2) decimal decimal 0x11 0x13 0x15 Number of Root Directory entries Number of logical sectors decimal hex decimal 0x16 0x18 0x1A Medium descriptor Sectors per FAT Sectors per track Number of heads Number of hidden sectors decimal decimal 0x1C decimal Table 1: The Layout of FAT-12 Boot Sector 2.2 Useful system calls int open (const char path, int flags) Opens a file at path and returns a file descriptor. For example, open(/tmp/myfile", O_RDONLY) opens an existing file called myfile in the tmp directory in read-only mode. It returns a file descriptor that can be used like a handle to the open file. 1 ∞0 22HNNNNNN 1 1 1 2
ssize_t read(int fd, void buf, size_t count) Reads count bytes from the file descriptor fd into the memory location starting at buf. It starts reading from the current offset into the file. The offset is set to zero if the file has just been opened. off_t 1seek(int fd, off.t offset, int whence) Sets the offset of the file descriptor fd to offset, depending on whence. If whence is SEEK_SET, then the offset is figured from the start of the file. If whence is SEEK CUR, then the offset is relative to the current offset. 2.3 Inspecting binary files You should have a floppy disk image (found in compressed file) and a program for dumping out the boot sector of the disk. In Unix, devices are treated as files. For example, the floppy drive is normally found at a device file such as /dev/140. We can use a file image as a substitute for an actual floppy disk. Your floppy image contains a real FAT-12 file system: if you know how the bits are laid out, then you can decode the structures that describe the file system and work with the files. To help get you started, we're going to decode a few fields by hand. A program to read from the floppy disk is in the compressed file. Given a filename and an offset (in dec imal notation), it will print out 32 bytes of bex and ASCII values. For example, ./bytedump image 1536 will produce the following output with this particular floppy image: Note that you have to give the offset to bytedump in the decimal format, not in Hex $./bytedump image 1536 addr value ascii 0x0600 0x31 1 0x0601 0x36 6 0x0602 0x53 S 0x0603 0x45 E 0x0604 0x43 c 0x061e 0x00 0x0611 0x00 The first column is the address (in hexadecimal), and the second is the data at that address (also in hexadecimal). The third column is the ASCII value of the data, but only if it was fit for printing. We can see that Ox31 is the ASCII character '1', and Ox00 is not printable. Note that data is stored in the little endian format. This means that the most significant byte comes last. For example, if we had the output $./bytedump image 120 addr value ascii 0x0078 0x20 0x0079 0x50 P We would know that Ox20 is stored at Ox0078, and Ox50 is stored at Ox0079. If we interpret this as a short value (2 bytes) starting at address 0x0078, then the value is stored as 0x2050, but it represents Ox5020. Similarly, if we were storing an int (4 bytes), the bytes would be stored in reverse order, from the least significant byte to the most significant byte. When reading data from the floppy disk into memory, we should be careful to understand the little endian format. For example, in reading a short value into memory, we should read two bytes from the file, and then reverse their order, before storing them into memory as a short value. A character is only a single byte, and hence there is no need for reversing the byte order if we read a single character from the file. Alternately, there is a utility in Unix called hexdump that will show the binary contents of a file. This is useful to inspect a file containing binary data. If you use hexdump -C on the supplied image, the output will look something like this (use hexdump -C image I less to more easily read it)
00000000 eb 3c 90 6d 6b 64 6f 73 66 73 00 00 10 01 00 1.<.nkdosfs. 00000010 02 e0 00 40 0b f0 01 00 12 00 02 00 00 00 00 00 0 00000020 00 00 00 00 00 00 29 79 d6 c9 3d 20 20 20 20 20 1.)y... The first column is the hexadecimal offset into the file. Since each line contains 16 bytes, this will always end in a 0. The middle 16 columns are the hexadecimal bytes of the file. The third column contains the ASCII representation of the bytes, or a." if the byte is not a printable character. For example, the second byte, having offset 0x01 has value Ox3c corresponding to the character <, while the third byte 0x90 is not printable, so is rendered as a dot. 3 Exercises 3.1 Decoding the boot sector by hand (1)(4 pt) Using the supplied program and the offsets in the tables above, find and decode the values for each of the following fields in the boot sector (remember that it starts at offset 0): Hex Decimal Bytes/sector Sectors/cluster Root directory entries Sectors/FAT 3.2 Decoding the boot sector (2)(6 pt) Create a program to read and then print information from the boot sector. The starting offset and size of each field can be found in Table 1. All of the information is (of course) stored as binary on disk. When you print the values, use the format specified in Table 1. Use printf to print out values correctly after converting any little endian data. Call your new program bedump (see the C program template badunp-template.c). Your program should take the name of the file to read, and then decode and print out all of the values in the boot sector (you may skip the jump instruction). 4 Some tips to help maximize your grade • Write separate functions to perform the following tasks: - A function for converting shorts from little to big endian. - A function for decoding the boot sector and storing the values in a struct. - A function for printing the boot sector from the struct. • Use Bitwise operators: << left shift >> right shift & and I or • Your conversion function should take advantage of bit shifting to reverse the bytes. One sugges- tion is to take two chars and return a short. Restrict your solution to the bitwise operators (no multiplication or addition). • Do not write a conversion function for binary to hex or hex to decimal. Let the format string in printf handle the dirty work. Format strings are described in "man 3 printf". 3
. Do not attempt to generalize the decoding process simply write a function to decode a FAT12 boot sector one field after another. You shouldn't use any loops (except possibly for printing the name). . Printing tips: - When you print out the values, make the values line up in a column (it's OK to hardcode spaces). For example, the following would be acceptable: Sectors/cluster 37 # of FATS 2 Media descriptor OxAB - Use "ox" to denote a hexadecimal value. - You can pad variables with zeros see bytedump. Be careful when printing the name it can be 8 characters without a binary zero at the end, so you have to ensure that you never print more than 8. Your variables will probably have to be declared "unsigned to print correctly. • A comment every 3-4 lines on average should be fine for algorithms. For declarations, please put a comment by each that explains what the variable or structure is used for. You might look at bytedump.c for an example of the style we are looking for, Give your variables meaningful names. Indexes are usually i, j, and k. but other variables should have more descriptive names. • Avoid using global variables unless you really need them. Global variables can be modified anywhere in a program, making code difficult to read and to debug. When printing out your code, ensure that the lines do not wrap around. Format your source so it fits within the limits of the printer. Not doing so makes it hard to read which defeats the purpose of indenting. 4.1 Packed structures The simplest way to read in the data from disk is to interpret it one byte at a time, and store the data in a structure. However, since structures in C are simply a definition of some binary data in memory, it is possible to define a structure that describes the FAT12 header. The file stdint.h defines a number of useful types such as uint16_t, which is an unsigned integer. Integers are always stored in memory in the byte order of the system, so you do not need to convert between little and big endian if you use this. Because several of the fields in the FAT12 header are not aligned to an even or odd byte bound- ary, the definition of a C structure may include padding to allow faster memory access. Since this is undesirable, you will need to add attribute__(__packed__)) after the structure definition. See http://sig.com/articles/gcc-packed-structures or a C manual for more information. There is no uint24 t type: the simplest way to represent a 3-byte field is by using an array of 3 uint8_t values. Alternatively, you can use a bitfield of length 24- for an introduction to the bitfield feature of C structures, see http://publications.gbdirect.co.uk/c.bo ... ields.html. Bitfields are rarely used in data structures as the slow access times due to compiler-generated bitmasking will outweigh the memory savings. You may choose to either use a packed structure or to read the data byte by byte: you do not need to do both.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply