Adding upstream version 4.01+dfsg.
[syslinux-debian/hramrach.git] / core / fs / fat / fat.c
blob5902f4813a0b6ce7e786a6449dc5550cd11e6491
1 #include <dprintf.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <sys/dirent.h>
5 #include <cache.h>
6 #include <core.h>
7 #include <disk.h>
8 #include <fs.h>
9 #include <ilog2.h>
10 #include <klibc/compiler.h>
11 #include "codepage.h"
12 #include "fat_fs.h"
14 static struct inode * new_fat_inode(struct fs_info *fs)
16 struct inode *inode = alloc_inode(fs, 0, sizeof(struct fat_pvt_inode));
17 if (!inode)
18 malloc_error("inode structure");
20 return inode;
24 * Check for a particular sector in the FAT cache
26 static const void *get_fat_sector(struct fs_info *fs, sector_t sector)
28 return get_cache(fs->fs_dev, FAT_SB(fs)->fat + sector);
31 static uint32_t get_next_cluster(struct fs_info *fs, uint32_t clust_num)
33 uint32_t next_cluster = 0;
34 sector_t fat_sector;
35 uint32_t offset;
36 uint32_t sector_mask = SECTOR_SIZE(fs) - 1;
37 const uint8_t *data;
39 switch(FAT_SB(fs)->fat_type) {
40 case FAT12:
41 offset = clust_num + (clust_num >> 1);
42 fat_sector = offset >> SECTOR_SHIFT(fs);
43 offset &= sector_mask;
44 data = get_fat_sector(fs, fat_sector);
45 if (offset == sector_mask) {
47 * we got the end of the one fat sector,
48 * but we have just one byte and we need two,
49 * so store the low part, then read the next fat
50 * sector, read the high part, then combine it.
52 next_cluster = data[offset];
53 data = get_fat_sector(fs, fat_sector + 1);
54 next_cluster += data[0] << 8;
55 } else {
56 next_cluster = *(const uint16_t *)(data + offset);
59 if (clust_num & 0x0001)
60 next_cluster >>= 4; /* cluster number is ODD */
61 else
62 next_cluster &= 0x0fff; /* cluster number is EVEN */
63 break;
65 case FAT16:
66 offset = clust_num << 1;
67 fat_sector = offset >> SECTOR_SHIFT(fs);
68 offset &= sector_mask;
69 data = get_fat_sector(fs, fat_sector);
70 next_cluster = *(const uint16_t *)(data + offset);
71 break;
73 case FAT32:
74 offset = clust_num << 2;
75 fat_sector = offset >> SECTOR_SHIFT(fs);
76 offset &= sector_mask;
77 data = get_fat_sector(fs, fat_sector);
78 next_cluster = *(const uint32_t *)(data + offset);
79 next_cluster &= 0x0fffffff;
80 break;
83 return next_cluster;
86 static int fat_next_extent(struct inode *inode, uint32_t lstart)
88 struct fs_info *fs = inode->fs;
89 struct fat_sb_info *sbi = FAT_SB(fs);
90 uint32_t mcluster = lstart >> sbi->clust_shift;
91 uint32_t lcluster;
92 uint32_t pcluster;
93 uint32_t tcluster;
94 uint32_t xcluster;
95 const uint32_t cluster_bytes = UINT32_C(1) << sbi->clust_byte_shift;
96 const uint32_t cluster_secs = UINT32_C(1) << sbi->clust_shift;
97 sector_t data_area = sbi->data;
99 tcluster = (inode->size + cluster_bytes - 1) >> sbi->clust_byte_shift;
100 if (mcluster >= tcluster)
101 goto err; /* Requested cluster beyond end of file */
103 lcluster = PVT(inode)->offset >> sbi->clust_shift;
104 pcluster = ((PVT(inode)->here - data_area) >> sbi->clust_shift) + 2;
106 if (lcluster > mcluster || PVT(inode)->here < data_area) {
107 lcluster = 0;
108 pcluster = PVT(inode)->start_cluster;
111 for (;;) {
112 if (pcluster-2 >= sbi->clusters) {
113 inode->size = lcluster << sbi->clust_shift;
114 goto err;
117 if (lcluster >= mcluster)
118 break;
120 lcluster++;
121 pcluster = get_next_cluster(fs, pcluster);
124 inode->next_extent.pstart =
125 ((sector_t)(pcluster-2) << sbi->clust_shift) + data_area;
126 inode->next_extent.len = cluster_secs;
127 xcluster = 0; /* Nonsense */
129 while (++lcluster < tcluster) {
130 xcluster = get_next_cluster(fs, pcluster);
131 if (xcluster != ++pcluster)
132 break; /* Not contiguous */
133 inode->next_extent.len += cluster_secs;
136 /* Note: ->here is bogus if ->offset >= EOF, but that's okay */
137 PVT(inode)->offset = lcluster << sbi->clust_shift;
138 PVT(inode)->here = ((xcluster-2) << sbi->clust_shift) + data_area;
140 return 0;
142 err:
143 return -1;
146 static sector_t get_next_sector(struct fs_info* fs, uint32_t sector)
148 struct fat_sb_info *sbi = FAT_SB(fs);
149 sector_t data_area = sbi->data;
150 sector_t data_sector;
151 uint32_t cluster;
152 int clust_shift = sbi->clust_shift;
154 if (sector < data_area) {
155 /* Root directory sector... */
156 sector++;
157 if (sector >= data_area)
158 sector = 0; /* Ran out of root directory, return EOF */
159 return sector;
162 data_sector = sector - data_area;
163 if ((data_sector + 1) & sbi->clust_mask) /* Still in the same cluster */
164 return sector + 1; /* Next sector inside cluster */
166 /* get a new cluster */
167 cluster = data_sector >> clust_shift;
168 cluster = get_next_cluster(fs, cluster + 2) - 2;
170 if (cluster >= sbi->clusters)
171 return 0;
173 /* return the start of the new cluster */
174 sector = (cluster << clust_shift) + data_area;
175 return sector;
179 * The FAT is a single-linked list. We remember the last place we
180 * were, so for a forward seek we can move forward from there, but
181 * for a reverse seek we have to start over...
183 static sector_t get_the_right_sector(struct file *file)
185 struct inode *inode = file->inode;
186 uint32_t sector_pos = file->offset >> SECTOR_SHIFT(file->fs);
187 uint32_t where;
188 sector_t sector;
190 if (sector_pos < PVT(inode)->offset) {
191 /* Reverse seek */
192 where = 0;
193 sector = PVT(inode)->start;
194 } else {
195 where = PVT(inode)->offset;
196 sector = PVT(inode)->here;
199 while (where < sector_pos) {
200 sector = get_next_sector(file->fs, sector);
201 where++;
204 PVT(inode)->offset = sector_pos;
205 PVT(inode)->here = sector;
207 return sector;
211 * Get the next sector in sequence
213 static sector_t next_sector(struct file *file)
215 struct inode *inode = file->inode;
216 sector_t sector = get_next_sector(file->fs, PVT(inode)->here);
217 PVT(inode)->offset++;
218 PVT(inode)->here = sector;
220 return sector;
224 * Mangle a filename pointed to by src into a buffer pointed to by dst;
225 * ends on encountering any whitespace.
228 static void vfat_mangle_name(char *dst, const char *src)
230 char *p = dst;
231 char c;
232 int i = FILENAME_MAX -1;
235 * Copy the filename, converting backslash to slash and
236 * collapsing duplicate separators.
238 while (not_whitespace(c = *src)) {
239 if (c == '\\')
240 c = '/';
242 if (c == '/') {
243 if (src[1] == '/' || src[1] == '\\') {
244 src++;
245 i--;
246 continue;
249 i--;
250 *dst++ = *src++;
253 /* Strip terminal slashes or whitespace */
254 while (1) {
255 if (dst == p)
256 break;
257 if (*(dst-1) == '/' && dst-1 == p) /* it's the '/' case */
258 break;
259 if ((*(dst-1) != '/') && (*(dst-1) != '.'))
260 break;
262 dst--;
263 i++;
266 i++;
267 for (; i > 0; i --)
268 *dst++ = '\0';
272 * Mangle a normal style string to DOS style string.
274 static void mangle_dos_name(char *mangle_buf, const char *src)
276 int i;
277 unsigned char c;
279 if (src[0] == '.' && (!src[1] || (src[1] == '.' && !src[2]))) {
280 /* . and .. mangle to their respective zero-padded version */
281 i = stpcpy(mangle_buf, src) - mangle_buf;
282 } else {
283 i = 0;
284 while (i < 11) {
285 c = *src++;
287 if ((c <= ' ') || (c == '/'))
288 break;
290 if (c == '.') {
291 while (i < 8)
292 mangle_buf[i++] = ' ';
293 i = 8;
294 continue;
297 c = codepage.upper[c];
298 if (i == 0 && c == 0xe5)
299 c = 0x05; /* Special hack for the first byte only! */
301 mangle_buf[i++] = c;
305 while (i < 11)
306 mangle_buf[i++] = ' ';
308 mangle_buf[i] = '\0';
312 * Match a string name against a longname. "len" is the number of
313 * codepoints in the input; including padding.
315 * Returns true on match.
317 static bool vfat_match_longname(const char *str, const uint16_t *match,
318 int len)
320 unsigned char c = -1; /* Nonzero: we have not yet seen NUL */
321 uint16_t cp;
323 dprintf("Matching: %s\n", str);
325 while (len) {
326 cp = *match++;
327 len--;
328 if (!cp)
329 break;
330 c = *str++;
331 if (cp != codepage.uni[0][c] && cp != codepage.uni[1][c])
332 return false; /* Also handles c == '\0' */
335 /* This should have been the end of the matching string */
336 if (*str)
337 return false;
339 /* Any padding entries must be FFFF */
340 while (len--)
341 if (*match++ != 0xffff)
342 return false;
344 return true;
348 * Convert an UTF-16 longname to the system codepage; return
349 * the length on success or -1 on failure.
351 static int vfat_cvt_longname(char *entry_name, const uint16_t *long_name)
353 struct unicache {
354 uint16_t utf16;
355 uint8_t cp;
357 static struct unicache unicache[256];
358 struct unicache *uc;
359 uint16_t cp;
360 unsigned int c;
361 char *p = entry_name;
363 do {
364 cp = *long_name++;
365 uc = &unicache[cp % 256];
367 if (__likely(uc->utf16 == cp)) {
368 *p++ = uc->cp;
369 } else {
370 for (c = 0; c < 512; c++) {
371 /* This is a bit hacky... */
372 if (codepage.uni[0][c] == cp) {
373 uc->utf16 = cp;
374 *p++ = uc->cp = (uint8_t)c;
375 goto found;
378 return -1; /* Impossible character */
379 found:
382 } while (cp);
384 return (p-entry_name)-1;
387 static void copy_long_chunk(uint16_t *buf, const struct fat_dir_entry *de)
389 const struct fat_long_name_entry *le =
390 (const struct fat_long_name_entry *)de;
392 memcpy(buf, le->name1, 5 * 2);
393 memcpy(buf + 5, le->name2, 6 * 2);
394 memcpy(buf + 11, le->name3, 2 * 2);
397 static uint8_t get_checksum(const char *dir_name)
399 int i;
400 uint8_t sum = 0;
402 for (i = 11; i; i--)
403 sum = ((sum & 1) << 7) + (sum >> 1) + (uint8_t)*dir_name++;
404 return sum;
408 /* compute the first sector number of one dir where the data stores */
409 static inline sector_t first_sector(struct fs_info *fs,
410 const struct fat_dir_entry *dir)
412 const struct fat_sb_info *sbi = FAT_SB(fs);
413 sector_t first_clust;
414 sector_t sector;
416 first_clust = (dir->first_cluster_high << 16) + dir->first_cluster_low;
417 if (first_clust == 0)
418 sector = sbi->root; /* first_clust == 0 means root directory */
419 else
420 sector = ((first_clust - 2) << sbi->clust_shift) + sbi->data;
422 return sector;
425 static inline enum dirent_type get_inode_mode(uint8_t attr)
427 return (attr & FAT_ATTR_DIRECTORY) ? DT_DIR : DT_REG;
431 static struct inode *vfat_find_entry(const char *dname, struct inode *dir)
433 struct fs_info *fs = dir->fs;
434 struct inode *inode;
435 const struct fat_dir_entry *de;
436 struct fat_long_name_entry *long_de;
438 char mangled_name[12];
439 uint16_t long_name[260]; /* == 20*13 */
440 int long_len;
442 sector_t dir_sector = PVT(dir)->start;
443 uint8_t vfat_init, vfat_next, vfat_csum = 0;
444 uint8_t id;
445 int slots;
446 int entries;
447 int checksum;
448 int long_match = 0;
450 slots = (strlen(dname) + 12) / 13;
451 if (slots > 20)
452 return NULL; /* Name too long */
454 slots |= 0x40;
455 vfat_init = vfat_next = slots;
456 long_len = slots*13;
458 /* Produce the shortname version, in case we need it. */
459 mangle_dos_name(mangled_name, dname);
461 while (dir_sector) {
462 de = get_cache(fs->fs_dev, dir_sector);
463 entries = 1 << (fs->sector_shift - 5);
465 while (entries--) {
466 if (de->name[0] == 0)
467 return NULL;
469 if (de->attr == 0x0f) {
471 * It's a long name entry.
473 long_de = (struct fat_long_name_entry *)de;
474 id = long_de->id;
475 if (id != vfat_next)
476 goto not_match;
478 if (id & 0x40) {
479 /* get the initial checksum value */
480 vfat_csum = long_de->checksum;
481 id &= 0x3f;
482 long_len = id * 13;
484 /* ZERO the long_name buffer */
485 memset(long_name, 0, sizeof long_name);
486 } else {
487 if (long_de->checksum != vfat_csum)
488 goto not_match;
491 vfat_next = --id;
493 /* got the long entry name */
494 copy_long_chunk(long_name + id*13, de);
497 * If we got the last entry, check it.
498 * Or, go on with the next entry.
500 if (id == 0) {
501 if (!vfat_match_longname(dname, long_name, long_len))
502 goto not_match;
503 long_match = 1;
505 de++;
506 continue; /* Try the next entry */
507 } else {
509 * It's a short entry
511 if (de->attr & 0x08) /* ignore volume labels */
512 goto not_match;
514 if (long_match) {
516 * We already have a VFAT long name match. However, the
517 * match is only valid if the checksum matches.
519 checksum = get_checksum(de->name);
520 if (checksum == vfat_csum)
521 goto found; /* Got it */
522 } else {
523 if (!memcmp(mangled_name, de->name, 11))
524 goto found;
528 not_match:
529 vfat_next = vfat_init;
530 long_match = 0;
532 de++;
535 /* Try with the next sector */
536 dir_sector = get_next_sector(fs, dir_sector);
538 return NULL; /* Nothing found... */
540 found:
541 inode = new_fat_inode(fs);
542 inode->size = de->file_size;
543 PVT(inode)->start_cluster =
544 (de->first_cluster_high << 16) + de->first_cluster_low;
545 if (PVT(inode)->start_cluster == 0) {
546 /* Root directory */
547 int root_size = FAT_SB(fs)->root_size;
549 PVT(inode)->start_cluster = FAT_SB(fs)->root_cluster;
550 inode->size = root_size ? root_size << fs->sector_shift : ~0;
551 PVT(inode)->start = PVT(inode)->here = FAT_SB(fs)->root;
552 } else {
553 PVT(inode)->start = PVT(inode)->here = first_sector(fs, de);
555 inode->mode = get_inode_mode(de->attr);
557 return inode;
560 static struct inode *vfat_iget_root(struct fs_info *fs)
562 struct inode *inode = new_fat_inode(fs);
563 int root_size = FAT_SB(fs)->root_size;
566 * For FAT32, the only way to get the root directory size is to
567 * follow the entire FAT chain to the end... which seems pointless.
569 PVT(inode)->start_cluster = FAT_SB(fs)->root_cluster;
570 inode->size = root_size ? root_size << fs->sector_shift : ~0;
571 PVT(inode)->start = PVT(inode)->here = FAT_SB(fs)->root;
572 inode->mode = DT_DIR;
574 return inode;
577 static struct inode *vfat_iget(const char *dname, struct inode *parent)
579 return vfat_find_entry(dname, parent);
582 static int vfat_readdir(struct file *file, struct dirent *dirent)
584 struct fs_info *fs = file->fs;
585 const struct fat_dir_entry *de;
586 const char *data;
587 const struct fat_long_name_entry *long_de;
589 sector_t sector = get_the_right_sector(file);
591 uint16_t long_name[261]; /* == 20*13 + 1 (to guarantee null) */
592 char filename[261];
593 int name_len = 0;
595 uint8_t vfat_init, vfat_next, vfat_csum;
596 uint8_t id;
597 int entries_left;
598 bool long_entry = false;
599 int sec_off = file->offset & ((1 << fs->sector_shift) - 1);
601 data = get_cache(fs->fs_dev, sector);
602 de = (const struct fat_dir_entry *)(data + sec_off);
603 entries_left = ((1 << fs->sector_shift) - sec_off) >> 5;
605 vfat_next = vfat_csum = 0xff;
607 while (1) {
608 while (entries_left--) {
609 if (de->name[0] == 0)
610 return -1; /* End of directory */
611 if ((uint8_t)de->name[0] == 0xe5)
612 goto invalid;
614 if (de->attr == 0x0f) {
616 * It's a long name entry.
618 long_de = (struct fat_long_name_entry *)de;
619 id = long_de->id;
621 if (id & 0x40) {
622 /* init vfat_csum and vfat_init */
623 vfat_csum = long_de->checksum;
624 id &= 0x3f;
625 if (id >= 20)
626 goto invalid; /* Too long! */
628 vfat_init = id;
630 /* ZERO the long_name buffer */
631 memset(long_name, 0, sizeof long_name);
632 } else {
633 if (long_de->checksum != vfat_csum || id != vfat_next)
634 goto invalid;
637 vfat_next = --id;
639 /* got the long entry name */
640 copy_long_chunk(long_name + id*13, de);
642 if (id == 0) {
643 name_len = vfat_cvt_longname(filename, long_name);
644 if (name_len > 0 && name_len < sizeof(dirent->d_name))
645 long_entry = true;
648 goto next;
649 } else {
651 * It's a short entry
653 if (de->attr & 0x08) /* ignore volume labels */
654 goto invalid;
656 if (long_entry && get_checksum(de->name) == vfat_csum) {
657 /* Got a long entry */
658 } else {
659 /* Use the shortname */
660 int i;
661 uint8_t c;
662 char *p = filename;
664 for (i = 0; i < 8; i++) {
665 c = de->name[i];
666 if (c == ' ')
667 break;
668 if (de->lcase & LCASE_BASE)
669 c = codepage.lower[c];
670 *p++ = c;
672 if (de->name[8] != ' ') {
673 *p++ = '.';
674 for (i = 8; i < 11; i++) {
675 c = de->name[i];
676 if (c == ' ')
677 break;
678 if (de->lcase & LCASE_EXT)
679 c = codepage.lower[c];
680 *p++ = c;
683 *p = '\0';
684 name_len = p - filename;
686 goto got; /* Got something one way or the other */
689 invalid:
690 long_entry = false;
691 next:
692 de++;
693 file->offset += sizeof(struct fat_dir_entry);
696 /* Try with the next sector */
697 sector = next_sector(file);
698 if (!sector)
699 return -1;
700 de = get_cache(fs->fs_dev, sector);
701 entries_left = 1 << (fs->sector_shift - 5);
704 got:
705 name_len++; /* Include final null */
706 dirent->d_ino = de->first_cluster_low | (de->first_cluster_high << 16);
707 dirent->d_off = file->offset;
708 dirent->d_reclen = offsetof(struct dirent, d_name) + name_len;
709 dirent->d_type = get_inode_mode(de->attr);
710 memcpy(dirent->d_name, filename, name_len);
712 file->offset += sizeof(*de); /* Update for next reading */
714 return 0;
717 /* init. the fs meta data, return the block size in bits */
718 static int vfat_fs_init(struct fs_info *fs)
720 struct fat_bpb fat;
721 struct fat_sb_info *sbi;
722 struct disk *disk = fs->fs_dev->disk;
723 int sectors_per_fat;
724 uint32_t clusters;
725 sector_t total_sectors;
727 fs->sector_shift = fs->block_shift = disk->sector_shift;
728 fs->sector_size = 1 << fs->sector_shift;
729 fs->block_size = 1 << fs->block_shift;
731 disk->rdwr_sectors(disk, &fat, 0, 1, 0);
733 /* XXX: Find better sanity checks... */
734 if (!fat.bxResSectors || !fat.bxFATs)
735 return -1;
736 sbi = malloc(sizeof(*sbi));
737 if (!sbi)
738 malloc_error("fat_sb_info structure");
739 fs->fs_info = sbi;
741 sectors_per_fat = fat.bxFATsecs ? : fat.fat32.bxFATsecs_32;
742 total_sectors = fat.bxSectors ? : fat.bsHugeSectors;
744 sbi->fat = fat.bxResSectors;
745 sbi->root = sbi->fat + sectors_per_fat * fat.bxFATs;
746 sbi->root_size = root_dir_size(fs, &fat);
747 sbi->data = sbi->root + sbi->root_size;
749 sbi->clust_shift = ilog2(fat.bxSecPerClust);
750 sbi->clust_byte_shift = sbi->clust_shift + fs->sector_shift;
751 sbi->clust_mask = fat.bxSecPerClust - 1;
752 sbi->clust_size = fat.bxSecPerClust << fs->sector_shift;
754 clusters = (total_sectors - sbi->data) >> sbi->clust_shift;
755 if (clusters <= 0xff4) {
756 sbi->fat_type = FAT12;
757 } else if (clusters <= 0xfff4) {
758 sbi->fat_type = FAT16;
759 } else {
760 sbi->fat_type = FAT32;
762 if (clusters > 0x0ffffff4)
763 clusters = 0x0ffffff4; /* Maximum possible */
765 if (fat.fat32.extended_flags & 0x80) {
766 /* Non-mirrored FATs, we need to read the active one */
767 sbi->fat += (fat.fat32.extended_flags & 0x0f) * sectors_per_fat;
770 /* FAT32: root directory is a cluster chain */
771 sbi->root = sbi->data
772 + ((fat.fat32.root_cluster-2) << sbi->clust_shift);
774 sbi->clusters = clusters;
776 /* Initialize the cache */
777 cache_init(fs->fs_dev, fs->block_shift);
779 return fs->block_shift;
782 const struct fs_ops vfat_fs_ops = {
783 .fs_name = "vfat",
784 .fs_flags = FS_USEMEM | FS_THISIND,
785 .fs_init = vfat_fs_init,
786 .searchdir = NULL,
787 .getfssec = generic_getfssec,
788 .close_file = generic_close_file,
789 .mangle_name = vfat_mangle_name,
790 .load_config = generic_load_config,
791 .readdir = vfat_readdir,
792 .iget_root = vfat_iget_root,
793 .iget = vfat_iget,
794 .next_extent = fat_next_extent,