2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2000, 2001 Free Software Foundation, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
46 int current_cluster_num;
50 /* pointer(s) into filesystem info buffer for DOS stuff */
51 #define FAT_SUPER ( (struct fat_superblock *) \
52 ( FSYS_BUF + 32256) )/* 512 bytes long */
53 #define FAT_BUF ( FSYS_BUF + 30208 ) /* 4 sector FAT buffer */
54 #define NAME_BUF ( FSYS_BUF + 29184 ) /* Filename buffer (833 bytes) */
56 #define FAT_CACHE_SIZE 2048
62 __u32 magic, first_fat;
64 /* Check partition type for harddisk */
65 if (((current_drive & 0x80) || (current_slice != 0))
66 && ! IS_PC_SLICE_TYPE_FAT (current_slice)
67 && (! IS_PC_SLICE_TYPE_BSD_WITH_FS (current_slice, FS_MSDOS)))
71 if (! devread (0, 0, sizeof (bpb), (char *) &bpb))
74 /* Check if the number of sectors per cluster is zero here, to avoid
76 if (bpb.sects_per_clust == 0)
79 FAT_SUPER->sectsize_bits = log2 (bpb.bytes_per_sect);
80 FAT_SUPER->clustsize_bits
81 = FAT_SUPER->sectsize_bits + log2 (bpb.sects_per_clust);
83 /* Fill in info about super block */
84 FAT_SUPER->num_sectors = bpb.short_sectors
85 ? bpb.short_sectors : bpb.long_sectors;
87 /* FAT offset and length */
88 FAT_SUPER->fat_offset = bpb.reserved_sects;
89 FAT_SUPER->fat_length =
90 bpb.fat_length ? bpb.fat_length : bpb.fat32_length;
92 /* Rootdir offset and length for FAT12/16 */
93 FAT_SUPER->root_offset =
94 FAT_SUPER->fat_offset + bpb.num_fats * FAT_SUPER->fat_length;
95 FAT_SUPER->root_max = FAT_DIRENTRY_LENGTH * bpb.dir_entries;
97 /* Data offset and number of clusters */
98 FAT_SUPER->data_offset =
99 FAT_SUPER->root_offset
100 + ((FAT_SUPER->root_max - 1) >> FAT_SUPER->sectsize_bits) + 1;
101 FAT_SUPER->num_clust =
102 2 + ((FAT_SUPER->num_sectors - FAT_SUPER->data_offset)
103 / bpb.sects_per_clust);
104 FAT_SUPER->sects_per_clust = bpb.sects_per_clust;
108 /* This is a FAT32 */
112 if (bpb.flags & 0x0080)
114 /* FAT mirroring is disabled, get active FAT */
115 int active_fat = bpb.flags & 0x000f;
116 if (active_fat >= bpb.num_fats)
118 FAT_SUPER->fat_offset += active_fat * FAT_SUPER->fat_length;
121 FAT_SUPER->fat_size = 8;
122 FAT_SUPER->root_cluster = bpb.root_cluster;
124 /* Yes the following is correct. FAT32 should be called FAT28 :) */
125 FAT_SUPER->clust_eof_marker = 0xffffff8;
129 if (!FAT_SUPER->root_max)
132 FAT_SUPER->root_cluster = -1;
133 if (FAT_SUPER->num_clust > FAT_MAX_12BIT_CLUST)
135 FAT_SUPER->fat_size = 4;
136 FAT_SUPER->clust_eof_marker = 0xfff8;
140 FAT_SUPER->fat_size = 3;
141 FAT_SUPER->clust_eof_marker = 0xff8;
146 /* Now do some sanity checks */
148 if (bpb.bytes_per_sect != (1 << FAT_SUPER->sectsize_bits)
149 || bpb.bytes_per_sect != SECTOR_SIZE
150 || bpb.sects_per_clust != (1 << (FAT_SUPER->clustsize_bits
151 - FAT_SUPER->sectsize_bits))
152 || FAT_SUPER->num_clust <= 2
153 || (FAT_SUPER->fat_size * FAT_SUPER->num_clust / (2 * SECTOR_SIZE)
154 > FAT_SUPER->fat_length))
157 /* kbs: Media check on first FAT entry [ported from PUPA] */
159 if (!devread(FAT_SUPER->fat_offset, 0,
160 sizeof(first_fat), (char *)&first_fat))
163 if (FAT_SUPER->fat_size == 8)
165 first_fat &= 0x0fffffff;
168 else if (FAT_SUPER->fat_size == 4)
170 first_fat &= 0x0000ffff;
175 first_fat &= 0x00000fff;
179 if (first_fat != (magic | bpb.media))
182 FAT_SUPER->cached_fat = - 2 * FAT_CACHE_SIZE;
187 fat_read (char *buf, int len)
194 if (FAT_SUPER->file_cluster < 0)
196 /* root directory for fat16 */
197 size = FAT_SUPER->root_max - filepos;
200 if (!devread(FAT_SUPER->root_offset, filepos, size, buf))
206 logical_clust = filepos >> FAT_SUPER->clustsize_bits;
207 offset = (filepos & ((1 << FAT_SUPER->clustsize_bits) - 1));
208 if (logical_clust < FAT_SUPER->current_cluster_num)
210 FAT_SUPER->current_cluster_num = 0;
211 FAT_SUPER->current_cluster = FAT_SUPER->file_cluster;
217 while (logical_clust > FAT_SUPER->current_cluster_num)
219 /* calculate next cluster */
221 FAT_SUPER->current_cluster * FAT_SUPER->fat_size;
223 int cached_pos = (fat_entry - FAT_SUPER->cached_fat);
225 if (cached_pos < 0 ||
226 (cached_pos + FAT_SUPER->fat_size) > 2*FAT_CACHE_SIZE)
228 FAT_SUPER->cached_fat = (fat_entry & ~(2*SECTOR_SIZE - 1));
229 cached_pos = (fat_entry - FAT_SUPER->cached_fat);
230 sector = FAT_SUPER->fat_offset
231 + FAT_SUPER->cached_fat / (2*SECTOR_SIZE);
232 if (!devread (sector, 0, FAT_CACHE_SIZE, (char*) FAT_BUF))
235 next_cluster = * (unsigned long *) (FAT_BUF + (cached_pos >> 1));
236 if (FAT_SUPER->fat_size == 3)
240 next_cluster &= 0xFFF;
242 else if (FAT_SUPER->fat_size == 4)
243 next_cluster &= 0xFFFF;
245 if (next_cluster >= FAT_SUPER->clust_eof_marker)
247 if (next_cluster < 2 || next_cluster >= FAT_SUPER->num_clust)
249 errnum = ERR_FSYS_CORRUPT;
253 FAT_SUPER->current_cluster = next_cluster;
254 FAT_SUPER->current_cluster_num++;
257 sector = FAT_SUPER->data_offset +
258 ((FAT_SUPER->current_cluster - 2) << (FAT_SUPER->clustsize_bits
259 - FAT_SUPER->sectsize_bits));
260 size = (1 << FAT_SUPER->clustsize_bits) - offset;
264 disk_read_func = disk_read_hook;
266 devread(sector, offset, size, buf);
268 disk_read_func = NULL;
277 return errnum ? 0 : ret;
281 fat_dir (char *dirname)
283 char *rest, ch, dir_buf[FAT_DIRENTRY_LENGTH];
284 char *filename = (char *) NAME_BUF;
285 int attrib = FAT_ATTRIB_DIR;
287 int do_possibilities = 0;
291 * the positions 2,4,6 etc are high bytes of a 16 bit unicode char
293 static unsigned char longdir_pos[] =
294 { 1, 3, 5, 7, 9, 14, 16, 18, 20, 22, 24, 28, 30 };
296 int alias_checksum = -1;
298 FAT_SUPER->file_cluster = FAT_SUPER->root_cluster;
300 FAT_SUPER->current_cluster_num = MAXINT;
302 /* main loop to find desired directory entry */
305 /* if we have a real file (and we're not just printing possibilities),
306 then this is where we want to exit */
308 if (!*dirname || isspace (*dirname))
310 if (attrib & FAT_ATTRIB_DIR)
312 errnum = ERR_BAD_FILETYPE;
319 /* continue with the file/directory name interpretation */
321 while (*dirname == '/')
324 if (!(attrib & FAT_ATTRIB_DIR))
326 errnum = ERR_BAD_FILETYPE;
329 /* Directories don't have a file size */
332 for (rest = dirname; (ch = *rest) && !isspace (ch) && ch != '/'; rest++);
337 if (print_possibilities && ch != '/')
338 do_possibilities = 1;
343 if (fat_read (dir_buf, FAT_DIRENTRY_LENGTH) != FAT_DIRENTRY_LENGTH
349 if (print_possibilities < 0)
356 # endif /* STAGE1_5 */
358 errnum = ERR_FILE_NOT_FOUND;
365 if (FAT_DIRENTRY_ATTRIB (dir_buf) == FAT_ATTRIB_LONGNAME)
367 /* This is a long filename. The filename is build from back
368 * to front and may span multiple entries. To bind these
369 * entries together they all contain the same checksum over
372 * The id field tells if this is the first entry (the last
373 * part) of the long filename, and also at which offset this
376 * We just write the part of the long filename this entry
377 * describes and continue with the next dir entry.
380 unsigned char id = FAT_LONGDIR_ID(dir_buf);
386 filename[slot * 13] = 0;
387 alias_checksum = FAT_LONGDIR_ALIASCHECKSUM(dir_buf);
390 if (id != slot || slot == 0
391 || alias_checksum != FAT_LONGDIR_ALIASCHECKSUM(dir_buf))
400 for (i=0; i < 13; i++)
401 filename[offset+i] = dir_buf[longdir_pos[i]];
405 if (!FAT_DIRENTRY_VALID (dir_buf))
408 if (alias_checksum != -1 && slot == 0)
414 for (sum = 0, i = 0; i< 11; i++)
415 sum = ((sum >> 1) | (sum << 7)) + dir_buf[i];
417 if (sum == alias_checksum)
420 if (do_possibilities)
422 # endif /* STAGE1_5 */
424 if (substring (dirname, filename) == 0)
429 /* XXX convert to 8.3 filename format here */
433 for (i = 0; i < 8 && (c = filename[i] = tolower (dir_buf[i]))
434 && !isspace (c); i++);
438 for (j = 0; j < 3 && (c = filename[i + j] = tolower (dir_buf[8 + j]))
439 && !isspace (c); j++);
448 if (do_possibilities)
451 if (substring (dirname, filename) <= 0)
453 if (print_possibilities > 0)
454 print_possibilities = -print_possibilities;
455 print_a_completion (filename);
459 # endif /* STAGE1_5 */
461 if (substring (dirname, filename) == 0)
465 *(dirname = rest) = ch;
467 attrib = FAT_DIRENTRY_ATTRIB (dir_buf);
468 filemax = FAT_DIRENTRY_FILELENGTH (dir_buf);
470 FAT_SUPER->file_cluster = FAT_DIRENTRY_FIRST_CLUSTER (dir_buf);
471 FAT_SUPER->current_cluster_num = MAXINT;
473 /* go back to main loop at top of function */
477 #endif /* FSYS_FAT */