1: #include <stdio.h>
2: #include "msdos.h"
3:
4: int fat_len; /* length of FAT table (in sectors) */
5: unsigned int end_fat; /* the end-of-chain marker */
6: unsigned int last_fat; /* the last in a chain marker */
7: unsigned char *fat_buf; /* the File Allocation Table */
8:
9: extern int fat_bits;
10:
11: /*
12: * Get and decode a FAT (file allocation table) entry. Returns the cluster
13: * number on success or 1 on failure.
14: */
15:
16: unsigned int
17: fat_decode(num)
18: unsigned int num;
19: {
20: unsigned int fat, fat_hi, fat_low, byte_1, byte_2;
21: int start;
22:
23: if (fat_bits == 12) {
24: /*
25: * | byte n | byte n+1 | byte n+2 |
26: * |7|6|5|4|3|2|1|0|7|6|5|4|3|2|1|0|7|6|5|4|3|2|1|0|
27: * | | | | | | | | | | | | | | | | | | | | | | | | |
28: * | n+0.0 | n+0.5 | n+1.0 | n+1.5 | n+2.0 | n+2.5 |
29: * \_____ \____ \______/________/_____ /
30: * ____\______\________/ _____/ ____\_/
31: * / \ \ / / \
32: * | n+1.5 | n+0.0 | n+0.5 | n+2.0 | n+2.5 | n+1.0 |
33: * | FAT entry k | FAT entry k+1 |
34: */
35: /* which bytes contain the entry */
36: start = num * 3 / 2;
37: if (start <= 2 || start + 1 > (fat_len * MSECTOR_SIZE))
38: return(1);
39:
40: byte_1 = *(fat_buf + start);
41: byte_2 = *(fat_buf + start + 1);
42: /* (odd) not on byte boundary */
43: if (num % 2) {
44: fat_hi = (byte_2 & 0xff) << 4;
45: fat_low = (byte_1 & 0xf0) >> 4;
46: }
47: /* (even) on byte boundary */
48: else {
49: fat_hi = (byte_2 & 0xf) << 8;
50: fat_low = byte_1 & 0xff;
51: }
52: fat = (fat_hi + fat_low) & 0xfff;
53: }
54: else {
55: /*
56: * | byte n | byte n+1 |
57: * |7|6|5|4|3|2|1|0|7|6|5|4|3|2|1|0|
58: * | | | | | | | | | | | | | | | | |
59: * | FAT entry k |
60: */
61: /* which bytes contain the entry */
62: start = num * 2;
63: if (start <= 3 || start + 1 > (fat_len * MSECTOR_SIZE))
64: return(1);
65:
66: fat = (*(fat_buf + start + 1) * 0x100) + *(fat_buf + start);
67: }
68: return(fat);
69: }
70:
71: /*
72: * Read the entire FAT table into memory.
73: */
74:
75: void
76: fat_read(start)
77: int start;
78: {
79: int buflen;
80: char *malloc();
81: void perror(), exit(), disk_read();
82: /* only the first copy of the FAT */
83: buflen = fat_len * MSECTOR_SIZE;
84: fat_buf = (unsigned char *) malloc((unsigned int) buflen);
85: if (fat_buf == NULL) {
86: perror("fat_read: malloc");
87: exit(1);
88: }
89: /* read the FAT sectors */
90: disk_read((long) start, fat_buf, buflen);
91:
92: /* the encoding scheme */
93: if (fat_bits == 12) {
94: end_fat = 0xfff;
95: last_fat = 0xff8;
96: }
97: else {
98: end_fat = 0xffff;
99: last_fat = 0xfff8;
100: }
101: return;
102: }
Defined functions
Defined variables
fat_buf
defined in line
7; used 18 times
fat_len
defined in line
4; used 14 times