2 * Copyright © 2013 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 /* Include FAT commands */
27 #define FAT_COMMANDS 1
30 /* Spew FAT tracing */
38 #define DBG(...) printf(__VA_ARGS__)
44 * Basic file system types
47 typedef ao_fat_offset_t offset_t;
48 typedef ao_fat_sector_t sector_t;
49 typedef ao_fat_cluster_t cluster_t;
50 typedef ao_fat_dirent_t dirent_t;
51 typedef ao_fat_cluster_offset_t cluster_offset_t;
54 static uint8_t ao_fat_mutex;
56 /* Partition information, sector numbers */
58 static uint8_t partition_type;
59 static sector_t partition_start, partition_end;
61 #define AO_FAT_BAD_CLUSTER 0xffffff7
62 #define AO_FAT_LAST_CLUSTER 0xfffffff
63 #define AO_FAT_IS_LAST_CLUSTER(c) (((c) & 0xffffff8) == 0xffffff8)
64 #define AO_FAT_IS_LAST_CLUSTER16(c) (((c) & 0xfff8) == 0xfff8)
67 #define SECTOR_SIZE 512
68 #define SECTOR_MASK (SECTOR_SIZE - 1)
69 #define SECTOR_SHIFT 9
71 #define DIRENT_SIZE 32
73 /* File system parameters */
74 static uint8_t sectors_per_cluster;
75 static uint32_t bytes_per_cluster;
76 static sector_t reserved_sector_count;
77 static uint8_t number_fat;
78 static dirent_t root_entries;
79 static sector_t sectors_per_fat;
80 static cluster_t number_cluster;
81 static sector_t fat_start;
82 static sector_t root_start;
83 static sector_t data_start;
84 static cluster_t next_free;
85 static uint8_t filesystem_full;
87 /* FAT32 extra data */
89 static uint8_t fsinfo_dirty;
90 static cluster_t root_cluster;
91 static sector_t fsinfo_sector;
92 static cluster_t free_count;
95 * Deal with LSB FAT data structures
98 get_u32(uint8_t *base)
100 return ((uint32_t) base[0] |
101 ((uint32_t) base[1] << 8) |
102 ((uint32_t) base[2] << 16) |
103 ((uint32_t) base[3] << 24));
107 put_u32(uint8_t *base, uint32_t value)
110 base[1] = value >> 8;
111 base[2] = value >> 16;
112 base[3] = value >> 24;
116 get_u16(uint8_t *base)
118 return ((uint16_t) base[0] | ((uint16_t) base[1] << 8));
122 put_u16(uint8_t *base, uint16_t value)
125 base[1] = value >> 8;
129 _ao_fat_cluster_valid(cluster_t cluster)
131 return (2 <= cluster && cluster < number_cluster);
134 /* Start using a sector */
136 _ao_fat_sector_get(sector_t sector)
138 sector += partition_start;
139 if (sector >= partition_end)
141 return ao_bufio_get(sector);
144 /* Finish using a sector, 'w' is 1 if modified */
145 #define _ao_fat_sector_put(b,w) ao_bufio_put(b,w)
147 /* Get the next cluster entry in the chain */
149 _ao_fat_entry_read(cluster_t cluster)
156 if (!_ao_fat_cluster_valid(cluster))
163 sector = cluster >> (SECTOR_SHIFT);
164 offset = cluster & SECTOR_MASK;
165 buf = _ao_fat_sector_get(fat_start + sector);
170 ret = get_u32(buf + offset);
173 ret = get_u16(buf + offset);
174 if (AO_FAT_IS_LAST_CLUSTER16(ret))
177 _ao_fat_sector_put(buf, 0);
181 /* Replace the referenced cluster entry in the chain with
182 * 'new_value'. Return the previous value.
185 _ao_fat_entry_replace(cluster_t cluster, cluster_t new_value)
188 cluster_offset_t offset;
194 if (!_ao_fat_cluster_valid(cluster))
197 /* Convert from cluster index to byte index */
202 sector = cluster >> SECTOR_SHIFT;
203 offset = cluster & SECTOR_MASK;
205 new_value &= 0xfffffff;
206 for (fat = 0; fat < number_fat; fat++) {
207 buf = _ao_fat_sector_get(fat_start + fat * sectors_per_fat + sector);
211 old_value = get_u32(buf + offset);
212 put_u32(buf + offset, new_value | (old_value & 0xf0000000));
214 ret = old_value & 0xfffffff;
216 /* Track the free count if it wasn't marked
217 * invalid when we mounted the file system
219 if (free_count != 0xffffffff) {
220 if (new_value && !ret) {
223 } else if (!new_value && ret) {
231 ret = get_u16(buf + offset);
232 if (AO_FAT_IS_LAST_CLUSTER16(ret))
235 put_u16(buf + offset, new_value);
237 _ao_fat_sector_put(buf, 1);
244 * Walk a cluster chain and mark
245 * all of them as free
248 _ao_fat_free_cluster_chain(cluster_t cluster)
250 while (_ao_fat_cluster_valid(cluster)) {
251 if (cluster < next_free) {
255 cluster = _ao_fat_entry_replace(cluster, 0x00000000);
260 * _ao_fat_cluster_seek
262 * The basic file system operation -- map a file cluster index to a
263 * partition cluster number. Done by computing the cluster number and
264 * then walking that many clusters from the first cluster. Returns
265 * 0xffff if we walk off the end of the file or the cluster chain
269 _ao_fat_cluster_seek(cluster_t cluster, cluster_t distance)
272 cluster = _ao_fat_entry_read(cluster);
273 if (!_ao_fat_cluster_valid(cluster))
281 * _ao_fat_cluster_set_size
283 * Set the number of clusters in the specified chain,
284 * freeing extra ones or alocating new ones as needed
286 * Returns AO_FAT_BAD_CLUSTER on allocation failure
290 _ao_fat_cluster_set_size(cluster_t first_cluster, cluster_t size)
293 cluster_t last_cluster;
294 cluster_t next_cluster;
296 /* Walk the cluster chain to the
297 * spot where it needs to change. That
298 * will either be the end of the chain (in case it needs to grow),
299 * or after the desired number of clusters, in which case it needs to shrink
301 next_cluster = first_cluster;
304 for (have = 0; have < size; have++) {
305 DBG(" %08x", next_cluster);
306 if (!_ao_fat_cluster_valid(next_cluster))
308 last_cluster = next_cluster;
309 next_cluster = _ao_fat_entry_read(next_cluster);
313 /* At this point, last_cluster points to the last valid
314 * cluster in the file, if any. That's the spot in the FAT
315 * that needs to be rewritten, either to truncate the file by
316 * writing an END marker, or to extend the file by writing
317 * more clusters. next_cluster will contain the value of the
318 * FAT at last_cluster.
320 * If this is at the head of the cluster chain, then
321 * last_cluster will be zero and next_cluster will
322 * be the first cluster in the chain.
325 /* The file is large enough, truncate as needed */
326 if (_ao_fat_cluster_valid(next_cluster)) {
327 DBG("truncate between %08x and %08x\n", last_cluster, next_cluster);
330 * Otherwise, rewrite the last cluster
331 * in the chain with a LAST marker
333 (void) _ao_fat_entry_replace(last_cluster,
334 AO_FAT_LAST_CLUSTER);
337 * If the file is getting erased, then
338 * rewrite the directory entry cluster
343 /* Clear the remaining clusters in the chain */
344 _ao_fat_free_cluster_chain(next_cluster);
346 /* The file system is no longer full (if it was) */
349 DBG("unchanged FAT chain\n");
350 /* The chain is already the right length, don't mess with it */
358 return AO_FAT_BAD_CLUSTER;
360 /* Set next_free if it has wrapped or wasn't set before */
361 if (next_free < 2 || number_cluster <= next_free) {
366 /* See if there are enough free clusters in the file system */
369 #define loop_cluster for (free = next_free; need > 0;)
370 #define next_cluster \
371 if (++free == number_cluster) \
373 if (free == next_free) \
377 if (!_ao_fat_entry_read(free))
382 /* Still need some, tell the user that we've failed */
385 return AO_FAT_BAD_CLUSTER;
388 /* Now go allocate those clusters and
389 * thread them onto the chain
393 if (_ao_fat_entry_read(free) == 0) {
394 next_free = free + 1;
395 if (next_free >= number_cluster)
398 DBG("\tadd cluster. old %08x new %08x\n", last_cluster, free);
400 _ao_fat_entry_replace(last_cluster, free);
402 first_cluster = free;
410 DBG("\tlast cluster %08x\n", last_cluster);
411 /* Mark the new end of the chain */
412 _ao_fat_entry_replace(last_cluster, AO_FAT_LAST_CLUSTER);
415 DBG("\tfirst cluster %08x\n", first_cluster);
416 return first_cluster;
419 /* Start using a root directory entry */
421 _ao_fat_root_get(dirent_t e)
423 offset_t byte = e * DIRENT_SIZE;
424 sector_t sector = byte >> SECTOR_SHIFT;
425 cluster_offset_t offset = byte & SECTOR_MASK;
429 cluster_t cluster_distance = sector / sectors_per_cluster;
430 sector_t sector_index = sector % sectors_per_cluster;
431 cluster_t cluster = _ao_fat_cluster_seek(root_cluster, cluster_distance);
433 if (_ao_fat_cluster_valid(cluster))
434 sector = data_start + (cluster-2) * sectors_per_cluster + sector_index;
438 if (e >= root_entries)
440 sector = root_start + sector;
443 buf = _ao_fat_sector_get(sector);
449 /* Finish using a root directory entry, 'w' is 1 if modified */
451 _ao_fat_root_put(uint8_t *root, dirent_t e, uint8_t write)
453 cluster_offset_t offset = ((e * DIRENT_SIZE) & SECTOR_MASK);
454 uint8_t *buf = root - offset;
456 _ao_fat_sector_put(buf, write);
460 * _ao_fat_root_extend
462 * On FAT32, make the root directory at least 'ents' entries long
465 _ao_fat_root_extend(dirent_t ents)
468 cluster_t cluster_size;
472 byte_size = (ents + 1) * 0x20;
473 cluster_size = (byte_size + bytes_per_cluster - 1) / bytes_per_cluster;
474 if (_ao_fat_cluster_set_size(root_cluster, cluster_size) != AO_FAT_BAD_CLUSTER)
480 * _ao_fat_setup_partition
482 * Load the boot block and find the first partition
485 _ao_fat_setup_partition(void)
489 uint32_t partition_size;
491 mbr = ao_bufio_get(0);
493 return AO_FAT_FILESYSTEM_MBR_READ_FAILURE;
495 /* Check the signature */
496 if (mbr[0x1fe] != 0x55 || mbr[0x1ff] != 0xaa) {
497 DBG ("Invalid MBR signature %02x %02x\n",
498 mbr[0x1fe], mbr[0x1ff]);
499 ao_bufio_put(mbr, 0);
500 return AO_FAT_FILESYSTEM_INVALID_MBR_SIGNATURE;
503 /* Check to see if it's actually a boot block, in which
504 * case it's presumably not a paritioned device
507 if (mbr[0] == 0xeb) {
509 partition_size = get_u16(mbr + 0x13);
510 if (partition_size == 0)
511 partition_size = get_u32(mbr + 0x20);
513 /* Just use the first partition */
514 partition = &mbr[0x1be];
516 partition_type = partition[4];
517 switch (partition_type) {
518 case 4: /* FAT16 up to 32M */
519 case 6: /* FAT16 over 32M */
521 case 0x0b: /* FAT32 up to 2047GB */
522 case 0x0c: /* FAT32 LBA */
525 DBG ("Invalid partition type %02x\n", partition_type);
526 ao_bufio_put(mbr, 0);
527 return AO_FAT_FILESYSTEM_INVALID_PARTITION_TYPE;
530 partition_start = get_u32(partition+8);
531 partition_size = get_u32(partition+12);
532 if (partition_size == 0) {
533 DBG ("Zero-sized partition\n");
534 ao_bufio_put(mbr, 0);
535 return AO_FAT_FILESYSTEM_ZERO_SIZED_PARTITION;
538 partition_end = partition_start + partition_size;
539 ao_bufio_put(mbr, 0);
540 return AO_FAT_FILESYSTEM_SUCCESS;
544 _ao_fat_setup_fs(void)
546 uint8_t *boot = _ao_fat_sector_get(0);
547 uint32_t data_sectors;
550 return AO_FAT_FILESYSTEM_BOOT_READ_FAILURE;
552 /* Check the signature */
553 if (boot[0x1fe] != 0x55 || boot[0x1ff] != 0xaa) {
554 DBG ("Invalid BOOT signature %02x %02x\n",
555 boot[0x1fe], boot[0x1ff]);
556 _ao_fat_sector_put(boot, 0);
557 return AO_FAT_FILESYSTEM_INVALID_BOOT_SIGNATURE;
560 /* Check the sector size */
561 if (get_u16(boot + 0xb) != SECTOR_SIZE) {
562 DBG ("Invalid sector size %d\n",
563 get_u16(boot + 0xb));
564 _ao_fat_sector_put(boot, 0);
565 return AO_FAT_FILESYSTEM_INVALID_SECTOR_SIZE;
568 sectors_per_cluster = boot[0xd];
569 bytes_per_cluster = sectors_per_cluster << SECTOR_SHIFT;
570 reserved_sector_count = get_u16(boot+0xe);
571 number_fat = boot[0x10];
572 root_entries = get_u16(boot + 0x11);
573 sectors_per_fat = get_u16(boot+0x16);
575 if (sectors_per_fat == 0) {
577 sectors_per_fat = get_u32(boot+0x24);
578 root_cluster = get_u32(boot+0x2c);
579 fsinfo_sector = get_u16(boot + 0x30);
581 _ao_fat_sector_put(boot, 0);
583 free_count = 0xffffffff;
585 if (fat32 && fsinfo_sector) {
586 uint8_t *fsinfo = _ao_fat_sector_get(fsinfo_sector);
589 free_count = get_u32(fsinfo + 0x1e8);
590 next_free = get_u32(fsinfo + 0x1ec);
591 _ao_fat_sector_put(fsinfo, 0);
595 fat_start = reserved_sector_count;;
596 root_start = fat_start + number_fat * sectors_per_fat;
597 data_start = root_start + ((root_entries * DIRENT_SIZE + SECTOR_MASK) >> SECTOR_SHIFT);
599 data_sectors = (partition_end - partition_start) - data_start;
601 number_cluster = data_sectors / sectors_per_cluster;
603 return AO_FAT_FILESYSTEM_SUCCESS;
607 * State for an open file
611 struct ao_fat_dirent *dirent;
613 offset_t cluster_offset;
618 #define AO_FAT_NFILE 8
620 static struct ao_fat_dirent ao_file_dirent[AO_FAT_NFILE];
622 static struct ao_fat_dirent *
623 _ao_fat_file_dirent_alloc(struct ao_fat_dirent *want)
626 struct ao_fat_dirent *free = NULL, *dirent;
628 for (d = 0; d < AO_FAT_NFILE; d++) {
630 dirent = &ao_file_dirent[d];
631 /* See if there's another user of this file already */
632 if (want && dirent->name[0] != 0) {
633 if (dirent->entry == want->entry)
648 static struct ao_file ao_file_table[AO_FAT_NFILE];
651 _ao_fat_fd_alloc(struct ao_fat_dirent *dirent)
655 for (fd = 0; fd < AO_FAT_NFILE; fd++)
656 if (!ao_file_table[fd].busy) {
657 ao_file_table[fd].dirent = _ao_fat_file_dirent_alloc(dirent);
658 ao_file_table[fd].busy = 1;
659 ao_file_table[fd].offset = 0;
660 ao_file_table[fd].cluster_offset = 0;
661 ao_file_table[fd].cluster = ao_file_table[fd].dirent->cluster;
665 return -AO_FAT_EMFILE;
669 _ao_fat_fd_free(int8_t fd)
671 struct ao_file *file = &ao_file_table[fd];
672 struct ao_fat_dirent *dirent = file->dirent;
673 memset(&ao_file_table[fd], '\0', sizeof (struct ao_file));
675 /* Check and see if another ao_file references the same dirent */
676 for (fd = 0; fd < AO_FAT_NFILE; fd++)
677 if (ao_file_table[fd].dirent == dirent)
679 memset(dirent, '\0', sizeof (struct ao_fat_dirent));
682 static struct ao_file *
683 _ao_fat_fd_to_file(int8_t fd)
685 struct ao_file *file;
686 if (fd < 0 || AO_FAT_NFILE <= fd)
689 file = &ao_file_table[fd];
695 static uint8_t ao_filesystem_setup;
696 static uint8_t ao_filesystem_status;
701 if (!ao_filesystem_setup) {
703 ao_filesystem_setup = 1;
706 /* Re-initialize all global state; this will help to allow the
707 * file system to get swapped someday
709 partition_type = partition_start = partition_end = 0;
710 sectors_per_cluster = bytes_per_cluster = reserved_sector_count = 0;
711 number_fat = root_entries = sectors_per_fat = 0;
712 number_cluster = fat_start = root_start = data_start = 0;
713 next_free = filesystem_full = 0;
714 fat32 = fsinfo_dirty = root_cluster = fsinfo_sector = free_count = 0;
716 /* Reset open file table */
717 memset(&ao_file_table, '\0', sizeof (ao_file_table));
719 ao_filesystem_status = _ao_fat_setup_partition();
720 if (ao_filesystem_status != AO_FAT_FILESYSTEM_SUCCESS)
721 return ao_filesystem_status;
722 ao_filesystem_status = _ao_fat_setup_fs();
723 if (ao_filesystem_status != AO_FAT_FILESYSTEM_SUCCESS)
724 return ao_filesystem_status;
726 return ao_filesystem_status;
732 ao_filesystem_setup = 0;
736 * Basic file operations
740 _ao_fat_current_sector(struct ao_file *file)
742 cluster_t cluster_offset;
743 uint32_t sector_offset;
744 uint16_t sector_index;
747 DBG("current sector offset %d size %d\n",
748 file->offset, file->dirent->size);
750 if (file->offset > file->dirent->size) {
751 printf ("file offset %d larger than size %d\n",
752 file->offset, file->dirent->size);
756 sector_offset = file->offset >> SECTOR_SHIFT;
758 if (!file->cluster || file->offset < file->cluster_offset) {
759 file->cluster = file->dirent->cluster;
760 file->cluster_offset = 0;
761 DBG("\treset to start of file %08x\n", file->cluster);
764 if (file->cluster_offset + bytes_per_cluster <= file->offset) {
765 cluster_t cluster_distance;
767 cluster_offset = sector_offset / sectors_per_cluster;
769 cluster_distance = cluster_offset - file->cluster_offset / bytes_per_cluster;
771 DBG("\tseek forward %d clusters\n", cluster_distance);
772 cluster = _ao_fat_cluster_seek(file->cluster, cluster_distance);
774 if (!_ao_fat_cluster_valid(cluster)) {
775 printf ("invalid cluster %08x\n", cluster);
778 file->cluster = cluster;
779 file->cluster_offset = cluster_offset * bytes_per_cluster;
782 sector_index = sector_offset % sectors_per_cluster;
783 DBG("current cluster %08x sector_index %d sector %d\n",
784 file->cluster, sector_index,
785 data_start + (uint32_t) (file->cluster-2) * sectors_per_cluster + sector_index);
786 return data_start + (uint32_t) (file->cluster-2) * sectors_per_cluster + sector_index;
790 * _ao_fat_invaldate_cluster_offset
792 * When the file size gets shrunk, invalidate
793 * any file structures referencing clusters beyond that point
797 _ao_fat_invalidate_cluster_offset(struct ao_fat_dirent *dirent)
800 struct ao_file *file;
802 for (fd = 0; fd < AO_FAT_NFILE; fd++) {
803 file = &ao_file_table[fd];
806 if (file->dirent == dirent) {
807 if (file->cluster_offset >= dirent->size) {
808 file->cluster_offset = 0;
809 file->cluster = dirent->cluster;
819 * Set the size of the current file, truncating or extending
820 * the cluster chain as needed
823 _ao_fat_set_size(struct ao_file *file, uint32_t size)
826 cluster_t first_cluster;
827 cluster_t have_clusters, need_clusters;
829 DBG ("Set size %d\n", size);
830 if (size == file->dirent->size) {
831 DBG("\tsize match\n");
832 return AO_FAT_SUCCESS;
835 first_cluster = file->dirent->cluster;
836 have_clusters = (file->dirent->size + bytes_per_cluster - 1) / bytes_per_cluster;
837 need_clusters = (size + bytes_per_cluster - 1) / bytes_per_cluster;
839 DBG ("\tfirst cluster %08x have %d need %d\n", first_cluster, have_clusters, need_clusters);
840 if (have_clusters != need_clusters) {
841 if (file->cluster && size > file->cluster_offset) {
842 cluster_t offset_clusters = (file->cluster_offset + bytes_per_cluster) / bytes_per_cluster;
843 cluster_t extra_clusters = need_clusters - offset_clusters;
844 cluster_t next_cluster;
846 DBG ("\tset size relative offset_clusters %d extra_clusters %d\n",
847 offset_clusters, extra_clusters);
849 /* Need one more to account for file->cluster, which we already have */
850 next_cluster = _ao_fat_cluster_set_size(file->cluster, extra_clusters + 1);
851 if (next_cluster == AO_FAT_BAD_CLUSTER)
852 return -AO_FAT_ENOSPC;
854 DBG ("\tset size absolute need_clusters %d\n", need_clusters);
855 first_cluster = _ao_fat_cluster_set_size(first_cluster, need_clusters);
857 if (first_cluster == AO_FAT_BAD_CLUSTER)
858 return -AO_FAT_ENOSPC;
862 DBG ("\tupdate directory size\n");
863 /* Update the directory entry */
864 dent = _ao_fat_root_get(file->dirent->entry);
866 printf ("dent update failed\n");
869 put_u32(dent + 0x1c, size);
870 put_u16(dent + 0x1a, first_cluster);
872 put_u16(dent + 0x14, first_cluster >> 16);
873 _ao_fat_root_put(dent, file->dirent->entry, 1);
875 file->dirent->size = size;
876 file->dirent->cluster = first_cluster;
877 if (have_clusters > need_clusters)
878 _ao_fat_invalidate_cluster_offset(file->dirent);
879 DBG ("set size done\n");
880 return AO_FAT_SUCCESS;
886 * Initialize a root directory entry
889 _ao_fat_root_init(uint8_t *dent, char name[11], uint8_t attr)
891 memset(dent, '\0', 0x20);
892 memmove(dent, name, 11);
899 put_u16(dent + 0x0e, 0);
901 put_u16(dent + 0x10, 0);
903 put_u16(dent + 0x12, 0);
906 put_u16(dent + 0x16, 0);
908 put_u16(dent + 0x18, 0);
911 /* Low cluster bytes */
912 put_u16(dent + 0x1a, 0);
913 /* FAT32 high cluster bytes */
914 put_u16(dent + 0x14, 0);
917 put_u32(dent + 0x1c, 0);
922 _ao_fat_dirent_init(struct ao_fat_dirent *dirent, uint8_t *dent, uint16_t entry)
924 memcpy(dirent->name, dent + 0x00, 11);
925 dirent->attr = dent[0x0b];
926 dirent->size = get_u32(dent+0x1c);
927 dirent->cluster = get_u16(dent+0x1a);
929 dirent->cluster |= (cluster_t) get_u16(dent + 0x14) << 16;
930 dirent->entry = entry;
934 * _ao_fat_flush_fsinfo
936 * Write out any fsinfo changes to disk
940 _ao_fat_flush_fsinfo(void)
953 fsinfo = _ao_fat_sector_get(fsinfo_sector);
955 put_u32(fsinfo + 0x1e8, free_count);
956 put_u32(fsinfo + 0x1ec, next_free);
957 _ao_fat_sector_put(fsinfo, 1);
968 * Flush any pending I/O to storage
974 if (_ao_fat_setup() != AO_FAT_FILESYSTEM_SUCCESS)
976 _ao_fat_flush_fsinfo();
983 ao_mutex_get(&ao_fat_mutex);
985 ao_mutex_put(&ao_fat_mutex);
991 * Returns TRUE if the filesystem cannot take
998 ao_mutex_get(&ao_fat_mutex);
999 if (_ao_fat_setup() != AO_FAT_FILESYSTEM_SUCCESS) {
1000 ao_mutex_put(&ao_fat_mutex);
1003 ao_mutex_put(&ao_fat_mutex);
1004 return filesystem_full;
1008 _ao_fat_readdir(uint16_t *entry, struct ao_fat_dirent *dirent)
1012 if (_ao_fat_setup() != AO_FAT_FILESYSTEM_SUCCESS)
1016 dent = _ao_fat_root_get(*entry);
1018 return -AO_FAT_EDIREOF;
1020 if (dent[0] == AO_FAT_DENT_END) {
1021 _ao_fat_root_put(dent, *entry, 0);
1022 return -AO_FAT_EDIREOF;
1024 if (dent[0] != AO_FAT_DENT_EMPTY && (dent[0xb] & 0xf) != 0xf) {
1025 _ao_fat_dirent_init(dirent, dent, *entry);
1026 _ao_fat_root_put(dent, *entry, 0);
1028 return AO_FAT_SUCCESS;
1030 _ao_fat_root_put(dent, *entry, 0);
1036 ao_fat_readdir(uint16_t *entry, struct ao_fat_dirent *dirent)
1040 ao_mutex_get(&ao_fat_mutex);
1041 status = _ao_fat_readdir(entry, dirent);
1042 ao_mutex_put(&ao_fat_mutex);
1049 * Open an existing file.
1052 _ao_fat_open(char name[11], uint8_t mode)
1055 static struct ao_fat_dirent dirent;
1058 if (_ao_fat_setup() != AO_FAT_FILESYSTEM_SUCCESS)
1062 status = _ao_fat_readdir(&entry, &dirent);
1064 if (status == -AO_FAT_EDIREOF)
1065 return -AO_FAT_ENOENT;
1068 if (!memcmp(name, dirent.name, 11)) {
1069 if (AO_FAT_IS_DIR(dirent.attr))
1070 return -AO_FAT_EISDIR;
1071 if (!AO_FAT_IS_FILE(dirent.attr))
1072 return -AO_FAT_EPERM;
1073 if (mode > AO_FAT_OPEN_READ && (dirent.attr & AO_FAT_FILE_READ_ONLY))
1074 return -AO_FAT_EACCESS;
1075 return _ao_fat_fd_alloc(&dirent);
1078 return -AO_FAT_ENOENT;
1082 ao_fat_open(char name[11], uint8_t mode)
1086 ao_mutex_get(&ao_fat_mutex);
1087 status = _ao_fat_open(name, mode);
1088 ao_mutex_put(&ao_fat_mutex);
1095 * Close the currently open file
1098 _ao_fat_close(int8_t fd)
1100 struct ao_file *file;
1102 file = _ao_fat_fd_to_file(fd);
1104 return -AO_FAT_EBADF;
1106 _ao_fat_fd_free(fd);
1108 return AO_FAT_SUCCESS;
1112 ao_fat_close(int8_t fd)
1116 ao_mutex_get(&ao_fat_mutex);
1117 status = _ao_fat_close(fd);
1118 ao_mutex_put(&ao_fat_mutex);
1125 * Open and truncate an existing file or
1130 _ao_fat_creat(char name[11])
1136 struct ao_file *file;
1138 if (_ao_fat_setup() != AO_FAT_FILESYSTEM_SUCCESS)
1141 fd = _ao_fat_open(name, AO_FAT_OPEN_WRITE);
1143 file = &ao_file_table[fd];
1144 status = _ao_fat_set_size(file, 0);
1150 if (fd == -AO_FAT_ENOENT) {
1153 dent = _ao_fat_root_get(entry);
1156 if (_ao_fat_root_extend(entry))
1158 fd = -AO_FAT_ENOSPC;
1161 if (dent[0] == AO_FAT_DENT_EMPTY || dent[0] == AO_FAT_DENT_END) {
1162 fd = _ao_fat_fd_alloc(NULL);
1164 _ao_fat_root_put(dent, entry, 0);
1168 file = &ao_file_table[fd];
1169 /* Initialize the dent */
1170 _ao_fat_root_init(dent, name, AO_FAT_FILE_REGULAR);
1172 /* Now initialize the dirent from the dent */
1173 _ao_fat_dirent_init(file->dirent, dent, entry);
1175 /* And write the dent to storage */
1176 _ao_fat_root_put(dent, entry, 1);
1178 status = -AO_FAT_SUCCESS;
1181 _ao_fat_root_put(dent, entry, 0);
1191 ao_fat_creat(char name[11])
1195 ao_mutex_get(&ao_fat_mutex);
1196 status = _ao_fat_creat(name);
1197 ao_mutex_put(&ao_fat_mutex);
1202 * ao_fat_map_current
1204 * Map the sector pointed at by the current file offset
1208 ao_fat_map_current(struct ao_file *file, int len, cluster_offset_t *offsetp, cluster_offset_t *this_time)
1210 cluster_offset_t offset;
1214 offset = file->offset & SECTOR_MASK;
1215 sector = _ao_fat_current_sector(file);
1216 if (sector == 0xffffffff) {
1219 buf = _ao_fat_sector_get(sector);
1222 if (offset + len < SECTOR_SIZE)
1225 *this_time = SECTOR_SIZE - offset;
1233 * Read from the file
1236 ao_fat_read(int8_t fd, void *dst, int len)
1238 uint8_t *dst_b = dst;
1239 cluster_offset_t this_time;
1240 cluster_offset_t offset;
1243 struct ao_file *file;
1245 ao_mutex_get(&ao_fat_mutex);
1246 file = _ao_fat_fd_to_file(fd);
1248 ret = -AO_FAT_EBADF;
1252 if (file->offset + len > file->dirent->size)
1253 len = file->dirent->size - file->offset;
1259 buf = ao_fat_map_current(file, len, &offset, &this_time);
1264 memcpy(dst_b, buf + offset, this_time);
1265 _ao_fat_sector_put(buf, 0);
1270 file->offset = file->offset + this_time;
1273 ao_mutex_put(&ao_fat_mutex);
1280 * Write to the file, extended as necessary
1283 ao_fat_write(int8_t fd, void *src, int len)
1285 uint8_t *src_b = src;
1286 cluster_offset_t this_time;
1287 cluster_offset_t offset;
1290 struct ao_file *file;
1292 ao_mutex_get(&ao_fat_mutex);
1293 file = _ao_fat_fd_to_file(fd);
1295 ret = -AO_FAT_EBADF;
1299 if (file->offset + len > file->dirent->size) {
1300 ret = _ao_fat_set_size(file, file->offset + len);
1306 buf = ao_fat_map_current(file, len, &offset, &this_time);
1311 memcpy(buf + offset, src_b, this_time);
1312 _ao_fat_sector_put(buf, 1);
1317 file->offset = file->offset + this_time;
1320 ao_mutex_put(&ao_fat_mutex);
1327 * Set the position for the next I/O operation
1328 * Note that this doesn't actually change the size
1329 * of the file if the requested position is beyond
1330 * the current file length, that would take a future
1334 ao_fat_seek(int8_t fd, int32_t pos, uint8_t whence)
1336 offset_t new_offset;
1337 struct ao_file *file;
1340 ao_mutex_get(&ao_fat_mutex);
1341 file = _ao_fat_fd_to_file(fd);
1343 ret = -AO_FAT_EBADF;
1347 new_offset = file->offset;
1349 case AO_FAT_SEEK_SET:
1352 case AO_FAT_SEEK_CUR:
1355 case AO_FAT_SEEK_END:
1356 new_offset = file->dirent->size + pos;
1359 ret = file->offset = new_offset;
1361 ao_mutex_put(&ao_fat_mutex);
1368 * Remove a file from the directory, marking
1369 * all clusters as free
1372 ao_fat_unlink(char name[11])
1375 static struct ao_fat_dirent dirent;
1378 ao_mutex_get(&ao_fat_mutex);
1379 if (_ao_fat_setup() != AO_FAT_FILESYSTEM_SUCCESS) {
1384 while (ao_fat_readdir(&entry, &dirent)) {
1385 if (memcmp(name, dirent.name, 11) == 0) {
1390 if (AO_FAT_IS_DIR(dirent.attr)) {
1391 ret = -AO_FAT_EISDIR;
1394 if (!AO_FAT_IS_FILE(dirent.attr)) {
1395 ret = -AO_FAT_EPERM;
1399 _ao_fat_free_cluster_chain(dirent.cluster);
1400 next = _ao_fat_root_get(dirent.entry + 1);
1401 if (next && next[0] != AO_FAT_DENT_END)
1402 delete = AO_FAT_DENT_EMPTY;
1404 delete = AO_FAT_DENT_END;
1406 _ao_fat_root_put(next, dirent.entry + 1, 0);
1407 ent = _ao_fat_root_get(dirent.entry);
1409 memset(ent, '\0', DIRENT_SIZE);
1411 _ao_fat_root_put(ent, dirent.entry, 1);
1414 ret = AO_FAT_SUCCESS;
1418 ret = -AO_FAT_ENOENT;
1420 ao_mutex_put(&ao_fat_mutex);
1425 ao_fat_rename(char old[11], char new[11])
1432 static const char *filesystem_errors[] = {
1433 [AO_FAT_FILESYSTEM_SUCCESS] = "FAT file system operating normally",
1434 [AO_FAT_FILESYSTEM_MBR_READ_FAILURE] = "MBR media read error",
1435 [AO_FAT_FILESYSTEM_INVALID_MBR_SIGNATURE] = "MBR signature invalid",
1436 [AO_FAT_FILESYSTEM_INVALID_PARTITION_TYPE] = "Unsupported paritition type",
1437 [AO_FAT_FILESYSTEM_ZERO_SIZED_PARTITION] = "Partition has zero sectors",
1438 [AO_FAT_FILESYSTEM_BOOT_READ_FAILURE] = "Boot block media read error",
1439 [AO_FAT_FILESYSTEM_INVALID_BOOT_SIGNATURE] = "Boot block signature invalid",
1440 [AO_FAT_FILESYSTEM_INVALID_SECTOR_SIZE] = "Sector size not 512",
1444 ao_fat_mbr_cmd(void)
1448 ao_mutex_get(&ao_fat_mutex);
1449 status = _ao_fat_setup();
1450 if (status == AO_FAT_FILESYSTEM_SUCCESS) {
1451 printf ("partition type: %02x\n", partition_type);
1452 printf ("partition start: %08x\n", partition_start);
1454 printf ("partition end: %08x\n", partition_end);
1456 printf ("fat32: %d\n", fat32);
1457 printf ("sectors per cluster %d\n", sectors_per_cluster);
1458 printf ("reserved sectors %d\n", reserved_sector_count);
1459 printf ("number of FATs %d\n", number_fat);
1460 printf ("root entries %d\n", root_entries);
1461 printf ("sectors per fat %d\n", sectors_per_fat);
1463 printf ("fat start %d\n", fat_start);
1464 printf ("root start %d\n", root_start);
1465 printf ("data start %d\n", data_start);
1467 printf ("FAT filesystem not available: %s\n", filesystem_errors[status]);
1469 ao_mutex_put(&ao_fat_mutex);
1472 struct ao_fat_attr {
1477 static const struct ao_fat_attr ao_fat_attr[] = {
1478 { .bit = AO_FAT_FILE_READ_ONLY, .label = 'R' },
1479 { .bit = AO_FAT_FILE_HIDDEN, .label = 'H' },
1480 { .bit = AO_FAT_FILE_SYSTEM, .label = 'S' },
1481 { .bit = AO_FAT_FILE_VOLUME_LABEL, .label = 'V' },
1482 { .bit = AO_FAT_FILE_DIRECTORY, .label = 'D' },
1483 { .bit = AO_FAT_FILE_ARCHIVE, .label = 'A' },
1486 #define NUM_FAT_ATTR (sizeof (ao_fat_attr) / sizeof (ao_fat_attr[0]))
1489 ao_fat_list_cmd(void)
1492 static struct ao_fat_dirent dirent;
1496 while ((status = ao_fat_readdir(&entry, &dirent)) == AO_FAT_SUCCESS) {
1497 for (i = 0; i < 8; i++)
1498 putchar(dirent.name[i]);
1501 putchar(dirent.name[i]);
1502 for (i = 0; i < NUM_FAT_ATTR; i++)
1503 putchar (dirent.attr & ao_fat_attr[i].bit ? ao_fat_attr[i].label : ' ');
1504 printf (" @%08x %d\n", dirent.cluster, dirent.size);
1506 if (status != -AO_FAT_EDIREOF)
1507 printf ("readdir failed: %d\n", status);
1511 ao_fat_parse_name(char name[11])
1518 while (ao_cmd_lex_c != '\n') {
1519 if (ao_cmd_lex_c == '.') {
1524 name[c++] = ao_cmd_lex_c;
1533 ao_fat_dump_cmd(void)
1535 static char name[11];
1538 static char buf[32];
1540 ao_fat_parse_name(name);
1541 if (name[0] == '\0') {
1542 ao_cmd_status = ao_cmd_syntax_error;
1546 fd = ao_fat_open(name, AO_FAT_OPEN_READ);
1548 printf ("Open failed: %d\n", fd);
1551 while ((cnt = ao_fat_read(fd, buf, sizeof(buf))) > 0) {
1552 for (i = 0; i < cnt; i++)
1559 ao_fat_write_cmd(void)
1561 static char name[11];
1564 static char buf[64];
1568 ao_fat_parse_name(name);
1569 if (name[0] == '\0') {
1570 ao_cmd_status = ao_cmd_syntax_error;
1574 fd = ao_fat_creat(name);
1576 printf ("Open failed: %d\n", fd);
1580 while ((c = getchar()) != 4) {
1581 if (c == '\r') c = '\n';
1583 if (c == '\n') putchar ('\r');
1584 putchar(c); flush();
1586 status = ao_fat_write(fd, &c, 1);
1588 printf ("Write failure %d\n", status);
1598 ao_cmd_put16(a >> 16);
1603 ao_fat_hexdump_cmd(void)
1611 ao_fat_parse_name(name);
1612 if (name[0] == '\0') {
1613 ao_cmd_status = ao_cmd_syntax_error;
1617 fd = ao_fat_open(name, AO_FAT_OPEN_READ);
1619 printf ("Open failed: %d\n", fd);
1623 while ((cnt = ao_fat_read(fd, buf, sizeof(buf))) > 0) {
1625 for (i = 0; i < cnt; i++) {
1627 ao_cmd_put8(buf[i]);
1635 static const struct ao_cmds ao_fat_cmds[] = {
1636 { ao_fat_mbr_cmd, "M\0Show FAT MBR and other info" },
1637 { ao_fat_list_cmd, "F\0List FAT directory" },
1638 { ao_fat_dump_cmd, "D <name>\0Dump FAT file" },
1639 { ao_fat_write_cmd, "W <name>\0Write FAT file (end with ^D)" },
1640 { ao_fat_hexdump_cmd, "H <name>\0HEX dump FAT file" },
1650 ao_cmd_register(&ao_fat_cmds[0]);