1 // SPDX-License-Identifier: GPL-2.0+
5 * R/W (V)FAT 12/16/32 filesystem implementation by Donggeun Kim
14 #include <asm/byteorder.h>
16 #include <asm/cache.h>
17 #include <linux/ctype.h>
19 #include <linux/math64.h>
22 static void uppercase(char *str, int len)
26 for (i = 0; i < len; i++) {
32 static int total_sector;
33 static int disk_write(__u32 block, __u32 nr_blocks, void *buf)
40 if (cur_part_info.start + block + nr_blocks >
41 cur_part_info.start + total_sector) {
42 printf("error: overflow occurs\n");
46 ret = blk_dwrite(cur_dev, cur_part_info.start + block, nr_blocks, buf);
47 if (nr_blocks && ret == 0)
54 * Set short name in directory entry
56 static void set_name(dir_entry *dirent, const char *filename)
58 char s_name[VFAT_MAXLEN_BYTES];
60 int period_location, len, i, ext_num;
65 len = strlen(filename);
69 strcpy(s_name, filename);
70 uppercase(s_name, len);
72 period = strchr(s_name, '.');
74 period_location = len;
77 period_location = period - s_name;
78 ext_num = len - period_location - 1;
81 /* Pad spaces when the length of file name is shorter than eight */
82 if (period_location < 8) {
83 memcpy(dirent->name, s_name, period_location);
84 for (i = period_location; i < 8; i++)
85 dirent->name[i] = ' ';
86 } else if (period_location == 8) {
87 memcpy(dirent->name, s_name, period_location);
89 memcpy(dirent->name, s_name, 6);
90 dirent->name[6] = '~';
91 dirent->name[7] = '1';
95 memcpy(dirent->ext, s_name + period_location + 1, ext_num);
96 for (i = ext_num; i < 3; i++)
99 memcpy(dirent->ext, s_name + period_location + 1, 3);
101 debug("name : %s\n", dirent->name);
102 debug("ext : %s\n", dirent->ext);
106 * Write fat buffer into block device
108 static int flush_dirty_fat_buffer(fsdata *mydata)
110 int getsize = FATBUFBLOCKS;
111 __u32 fatlength = mydata->fatlength;
112 __u8 *bufptr = mydata->fatbuf;
113 __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
115 debug("debug: evicting %d, dirty: %d\n", mydata->fatbufnum,
116 (int)mydata->fat_dirty);
118 if ((!mydata->fat_dirty) || (mydata->fatbufnum == -1))
121 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
122 if (startblock + getsize > fatlength)
123 getsize = fatlength - startblock;
125 startblock += mydata->fat_sect;
128 if (disk_write(startblock, getsize, bufptr) < 0) {
129 debug("error: writing FAT blocks\n");
133 if (mydata->fats == 2) {
134 /* Update corresponding second FAT blocks */
135 startblock += mydata->fatlength;
136 if (disk_write(startblock, getsize, bufptr) < 0) {
137 debug("error: writing second FAT blocks\n");
141 mydata->fat_dirty = 0;
147 * Set the file name information from 'name' into 'slotptr',
149 static int str2slot(dir_slot *slotptr, const char *name, int *idx)
153 for (j = 0; j <= 8; j += 2) {
154 if (name[*idx] == 0x00) {
155 slotptr->name0_4[j] = 0;
156 slotptr->name0_4[j + 1] = 0;
160 slotptr->name0_4[j] = name[*idx];
164 for (j = 0; j <= 10; j += 2) {
165 if (name[*idx] == 0x00) {
166 slotptr->name5_10[j] = 0;
167 slotptr->name5_10[j + 1] = 0;
171 slotptr->name5_10[j] = name[*idx];
175 for (j = 0; j <= 2; j += 2) {
176 if (name[*idx] == 0x00) {
177 slotptr->name11_12[j] = 0;
178 slotptr->name11_12[j + 1] = 0;
182 slotptr->name11_12[j] = name[*idx];
187 if (name[*idx] == 0x00)
191 /* Not used characters are filled with 0xff 0xff */
193 for (; end_idx < 5; end_idx++) {
194 slotptr->name0_4[end_idx * 2] = 0xff;
195 slotptr->name0_4[end_idx * 2 + 1] = 0xff;
200 for (; end_idx < 6; end_idx++) {
201 slotptr->name5_10[end_idx * 2] = 0xff;
202 slotptr->name5_10[end_idx * 2 + 1] = 0xff;
207 for (; end_idx < 2; end_idx++) {
208 slotptr->name11_12[end_idx * 2] = 0xff;
209 slotptr->name11_12[end_idx * 2 + 1] = 0xff;
215 static int new_dir_table(fat_itr *itr);
216 static int flush_dir(fat_itr *itr);
219 * Fill dir_slot entries with appropriate name, id, and attr
220 * 'itr' will point to a next entry
223 fill_dir_slot(fat_itr *itr, const char *l_name)
225 __u8 temp_dir_slot_buffer[MAX_LFN_SLOT * sizeof(dir_slot)];
226 dir_slot *slotptr = (dir_slot *)temp_dir_slot_buffer;
227 __u8 counter = 0, checksum;
230 /* Get short file name checksum value */
231 checksum = mkcksum(itr->dent->name, itr->dent->ext);
234 memset(slotptr, 0x00, sizeof(dir_slot));
235 ret = str2slot(slotptr, l_name, &idx);
236 slotptr->id = ++counter;
237 slotptr->attr = ATTR_VFAT;
238 slotptr->alias_checksum = checksum;
243 slotptr->id |= LAST_LONG_ENTRY_MASK;
245 while (counter >= 1) {
246 memcpy(itr->dent, slotptr, sizeof(dir_slot));
250 if (itr->remaining == 0)
253 /* allocate a cluster for more entries */
254 if (!fat_itr_next(itr))
256 (!itr->is_root || itr->fsdata->fatsize == 32) &&
265 * Set the entry at index 'entry' in a FAT (12/16/32) table.
267 static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
269 __u32 bufnum, offset, off16;
272 switch (mydata->fatsize) {
274 bufnum = entry / FAT32BUFSIZE;
275 offset = entry - bufnum * FAT32BUFSIZE;
278 bufnum = entry / FAT16BUFSIZE;
279 offset = entry - bufnum * FAT16BUFSIZE;
282 bufnum = entry / FAT12BUFSIZE;
283 offset = entry - bufnum * FAT12BUFSIZE;
286 /* Unsupported FAT size */
290 /* Read a new block of FAT entries into the cache. */
291 if (bufnum != mydata->fatbufnum) {
292 int getsize = FATBUFBLOCKS;
293 __u8 *bufptr = mydata->fatbuf;
294 __u32 fatlength = mydata->fatlength;
295 __u32 startblock = bufnum * FATBUFBLOCKS;
297 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
298 if (startblock + getsize > fatlength)
299 getsize = fatlength - startblock;
301 if (flush_dirty_fat_buffer(mydata) < 0)
304 startblock += mydata->fat_sect;
306 if (disk_read(startblock, getsize, bufptr) < 0) {
307 debug("Error reading FAT blocks\n");
310 mydata->fatbufnum = bufnum;
314 mydata->fat_dirty = 1;
316 /* Set the actual entry */
317 switch (mydata->fatsize) {
319 ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
322 ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
325 off16 = (offset * 3) / 4;
327 switch (offset & 0x3) {
329 val1 = cpu_to_le16(entry_value) & 0xfff;
330 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff;
331 ((__u16 *)mydata->fatbuf)[off16] |= val1;
334 val1 = cpu_to_le16(entry_value) & 0xf;
335 val2 = (cpu_to_le16(entry_value) >> 4) & 0xff;
337 ((__u16 *)mydata->fatbuf)[off16] &= ~0xf000;
338 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 12);
340 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xff;
341 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
344 val1 = cpu_to_le16(entry_value) & 0xff;
345 val2 = (cpu_to_le16(entry_value) >> 8) & 0xf;
347 ((__u16 *)mydata->fatbuf)[off16] &= ~0xff00;
348 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 8);
350 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xf;
351 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
354 val1 = cpu_to_le16(entry_value) & 0xfff;
355 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff0;
356 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 4);
371 * Determine the next free cluster after 'entry' in a FAT (12/16/32) table
372 * and link it to 'entry'. EOC marker is not set on returned entry.
374 static __u32 determine_fatent(fsdata *mydata, __u32 entry)
376 __u32 next_fat, next_entry = entry + 1;
379 next_fat = get_fatent(mydata, next_entry);
381 /* found free entry, link to entry */
382 set_fatent_value(mydata, entry, next_entry);
387 debug("FAT%d: entry: %08x, entry_value: %04x\n",
388 mydata->fatsize, entry, next_entry);
394 * set_sectors() - write data to sectors
396 * Write 'size' bytes from 'buffer' into the specified sector.
398 * @mydata: data to be written
399 * @startsect: sector to be written to
400 * @buffer: data to be written
401 * @size: bytes to be written (but not more than the size of a cluster)
402 * Return: 0 on success, -1 otherwise
405 set_sectors(fsdata *mydata, u32 startsect, u8 *buffer, u32 size)
410 debug("startsect: %d\n", startsect);
412 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
413 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
415 debug("FAT: Misaligned buffer address (%p)\n", buffer);
417 while (size >= mydata->sect_size) {
418 memcpy(tmpbuf, buffer, mydata->sect_size);
419 ret = disk_write(startsect++, 1, tmpbuf);
421 debug("Error writing data (got %d)\n", ret);
425 buffer += mydata->sect_size;
426 size -= mydata->sect_size;
428 } else if (size >= mydata->sect_size) {
429 nsects = size / mydata->sect_size;
430 ret = disk_write(startsect, nsects, buffer);
432 debug("Error writing data (got %d)\n", ret);
437 buffer += nsects * mydata->sect_size;
438 size -= nsects * mydata->sect_size;
442 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
443 /* Do not leak content of stack */
444 memset(tmpbuf, 0, mydata->sect_size);
445 memcpy(tmpbuf, buffer, size);
446 ret = disk_write(startsect, 1, tmpbuf);
448 debug("Error writing data (got %d)\n", ret);
457 * set_cluster() - write data to cluster
459 * Write 'size' bytes from 'buffer' into the specified cluster.
461 * @mydata: data to be written
462 * @clustnum: cluster to be written to
463 * @buffer: data to be written
464 * @size: bytes to be written (but not more than the size of a cluster)
465 * Return: 0 on success, -1 otherwise
468 set_cluster(fsdata *mydata, u32 clustnum, u8 *buffer, u32 size)
470 return set_sectors(mydata, clust_to_sect(mydata, clustnum),
475 flush_dir(fat_itr *itr)
477 fsdata *mydata = itr->fsdata;
478 u32 startsect, sect_offset, nsects;
480 if (!itr->is_root || mydata->fatsize == 32)
481 return set_cluster(mydata, itr->clust, itr->block,
482 mydata->clust_size * mydata->sect_size);
484 sect_offset = itr->clust * mydata->clust_size;
485 startsect = mydata->rootdir_sect + sect_offset;
486 /* do not write past the end of rootdir */
487 nsects = min_t(u32, mydata->clust_size,
488 mydata->rootdir_size - sect_offset);
490 return set_sectors(mydata, startsect, itr->block,
491 nsects * mydata->sect_size);
494 static __u8 tmpbuf_cluster[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN);
497 * Read and modify data on existing and consecutive cluster blocks
500 get_set_cluster(fsdata *mydata, __u32 clustnum, loff_t pos, __u8 *buffer,
501 loff_t size, loff_t *gotsize)
503 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
506 int clustcount, i, ret;
512 assert(pos < bytesperclust);
513 startsect = clust_to_sect(mydata, clustnum);
515 debug("clustnum: %d, startsect: %d, pos: %lld\n",
516 clustnum, startsect, pos);
518 /* partial write at beginning */
520 wsize = min(bytesperclust - pos, size);
521 ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster);
522 if (ret != mydata->clust_size) {
523 debug("Error reading data (got %d)\n", ret);
527 memcpy(tmpbuf_cluster + pos, buffer, wsize);
528 ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster);
529 if (ret != mydata->clust_size) {
530 debug("Error writing data (got %d)\n", ret);
538 startsect += mydata->clust_size;
544 /* full-cluster write */
545 if (size >= bytesperclust) {
546 clustcount = lldiv(size, bytesperclust);
548 if (!((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1))) {
549 wsize = clustcount * bytesperclust;
550 ret = disk_write(startsect,
551 clustcount * mydata->clust_size,
553 if (ret != clustcount * mydata->clust_size) {
554 debug("Error writing data (got %d)\n", ret);
562 startsect += clustcount * mydata->clust_size;
564 for (i = 0; i < clustcount; i++) {
565 memcpy(tmpbuf_cluster, buffer, bytesperclust);
566 ret = disk_write(startsect,
569 if (ret != mydata->clust_size) {
570 debug("Error writing data (got %d)\n",
575 size -= bytesperclust;
576 buffer += bytesperclust;
577 *gotsize += bytesperclust;
579 startsect += mydata->clust_size;
584 /* partial write at end */
587 ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster);
588 if (ret != mydata->clust_size) {
589 debug("Error reading data (got %d)\n", ret);
592 memcpy(tmpbuf_cluster, buffer, wsize);
593 ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster);
594 if (ret != mydata->clust_size) {
595 debug("Error writing data (got %d)\n", ret);
610 * Find the first empty cluster
612 static int find_empty_cluster(fsdata *mydata)
614 __u32 fat_val, entry = 3;
617 fat_val = get_fatent(mydata, entry);
627 * Allocate a cluster for additional directory entries
629 static int new_dir_table(fat_itr *itr)
631 fsdata *mydata = itr->fsdata;
632 int dir_newclust = 0;
633 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
635 dir_newclust = find_empty_cluster(mydata);
636 set_fatent_value(mydata, itr->clust, dir_newclust);
637 if (mydata->fatsize == 32)
638 set_fatent_value(mydata, dir_newclust, 0xffffff8);
639 else if (mydata->fatsize == 16)
640 set_fatent_value(mydata, dir_newclust, 0xfff8);
641 else if (mydata->fatsize == 12)
642 set_fatent_value(mydata, dir_newclust, 0xff8);
644 itr->clust = dir_newclust;
645 itr->next_clust = dir_newclust;
647 if (flush_dirty_fat_buffer(mydata) < 0)
650 memset(itr->block, 0x00, bytesperclust);
652 itr->dent = (dir_entry *)itr->block;
653 itr->last_cluster = 1;
654 itr->remaining = bytesperclust / sizeof(dir_entry) - 1;
660 * Set empty cluster from 'entry' to the end of a file
662 static int clear_fatent(fsdata *mydata, __u32 entry)
666 while (!CHECK_CLUST(entry, mydata->fatsize)) {
667 fat_val = get_fatent(mydata, entry);
669 set_fatent_value(mydata, entry, 0);
676 /* Flush fat buffer */
677 if (flush_dirty_fat_buffer(mydata) < 0)
684 * Set start cluster in directory entry
686 static void set_start_cluster(const fsdata *mydata, dir_entry *dentptr,
689 if (mydata->fatsize == 32)
691 cpu_to_le16((start_cluster & 0xffff0000) >> 16);
692 dentptr->start = cpu_to_le16(start_cluster & 0xffff);
696 * Check whether adding a file makes the file system to
697 * exceed the size of the block device
698 * Return -1 when overflow occurs, otherwise return 0
700 static int check_overflow(fsdata *mydata, __u32 clustnum, loff_t size)
702 __u32 startsect, sect_num, offset;
705 startsect = clust_to_sect(mydata, clustnum);
707 startsect = mydata->rootdir_sect;
709 sect_num = div_u64_rem(size, mydata->sect_size, &offset);
714 if (startsect + sect_num > total_sector)
720 * Write at most 'maxsize' bytes from 'buffer' into
721 * the file associated with 'dentptr'
722 * Update the number of bytes written in *gotsize and return 0
723 * or return -1 on fatal errors.
726 set_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, __u8 *buffer,
727 loff_t maxsize, loff_t *gotsize)
729 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
730 __u32 curclust = START(dentptr);
731 __u32 endclust = 0, newclust = 0;
732 u64 cur_pos, filesize;
733 loff_t offset, actsize, wsize;
736 filesize = pos + maxsize;
738 debug("%llu bytes\n", filesize);
743 if (!CHECK_CLUST(curclust, mydata->fatsize) ||
744 IS_LAST_CLUST(curclust, mydata->fatsize)) {
745 clear_fatent(mydata, curclust);
746 set_start_cluster(mydata, dentptr, 0);
749 debug("curclust: 0x%x\n", curclust);
750 debug("Invalid FAT entry\n");
759 /* go to cluster at pos */
760 cur_pos = bytesperclust;
764 if (IS_LAST_CLUST(curclust, mydata->fatsize))
767 newclust = get_fatent(mydata, curclust);
768 if (!IS_LAST_CLUST(newclust, mydata->fatsize) &&
769 CHECK_CLUST(newclust, mydata->fatsize)) {
770 debug("curclust: 0x%x\n", curclust);
771 debug("Invalid FAT entry\n");
775 cur_pos += bytesperclust;
778 if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
779 assert(pos == cur_pos);
783 assert(pos < cur_pos);
784 cur_pos -= bytesperclust;
787 assert(IS_LAST_CLUST(curclust, mydata->fatsize) ||
788 !CHECK_CLUST(curclust, mydata->fatsize));
791 /* search for allocated consecutive clusters */
792 actsize = bytesperclust;
795 if (filesize <= (cur_pos + actsize))
798 newclust = get_fatent(mydata, endclust);
800 if (newclust != endclust + 1)
802 if (IS_LAST_CLUST(newclust, mydata->fatsize))
804 if (CHECK_CLUST(newclust, mydata->fatsize)) {
805 debug("curclust: 0x%x\n", curclust);
806 debug("Invalid FAT entry\n");
810 actsize += bytesperclust;
814 /* overwrite to <curclust..endclust> */
818 offset = pos - cur_pos;
819 wsize = min_t(unsigned long long, actsize, filesize - cur_pos);
822 if (get_set_cluster(mydata, curclust, offset,
823 buffer, wsize, &actsize)) {
824 printf("Error get-and-setting cluster\n");
829 cur_pos += offset + wsize;
831 if (filesize <= cur_pos)
834 if (IS_LAST_CLUST(newclust, mydata->fatsize))
835 /* no more clusters */
841 if (filesize <= cur_pos) {
843 newclust = get_fatent(mydata, endclust);
844 if (!IS_LAST_CLUST(newclust, mydata->fatsize)) {
845 /* truncate the rest */
846 clear_fatent(mydata, newclust);
848 /* Mark end of file in FAT */
849 if (mydata->fatsize == 12)
851 else if (mydata->fatsize == 16)
853 else if (mydata->fatsize == 32)
854 newclust = 0xfffffff;
855 set_fatent_value(mydata, endclust, newclust);
863 assert(!do_div(cur_pos, bytesperclust));
866 /* allocate and write */
869 /* Assure that curclust is valid */
871 curclust = find_empty_cluster(mydata);
872 set_start_cluster(mydata, dentptr, curclust);
874 newclust = get_fatent(mydata, curclust);
876 if (IS_LAST_CLUST(newclust, mydata->fatsize)) {
877 newclust = determine_fatent(mydata, curclust);
878 set_fatent_value(mydata, curclust, newclust);
881 debug("error: something wrong\n");
886 /* TODO: already partially written */
887 if (check_overflow(mydata, curclust, filesize)) {
888 printf("Error: no space left: %llu\n", filesize);
892 actsize = bytesperclust;
895 /* search for consecutive clusters */
896 while (actsize < filesize) {
897 newclust = determine_fatent(mydata, endclust);
899 if ((newclust - 1) != endclust)
900 /* write to <curclust..endclust> */
903 if (CHECK_CLUST(newclust, mydata->fatsize)) {
904 debug("newclust: 0x%x\n", newclust);
905 debug("Invalid FAT entry\n");
909 actsize += bytesperclust;
912 /* set remaining bytes */
914 if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
915 debug("error: writing cluster\n");
920 /* Mark end of file in FAT */
921 if (mydata->fatsize == 12)
923 else if (mydata->fatsize == 16)
925 else if (mydata->fatsize == 32)
926 newclust = 0xfffffff;
927 set_fatent_value(mydata, endclust, newclust);
931 if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
932 debug("error: writing cluster\n");
939 if (CHECK_CLUST(newclust, mydata->fatsize)) {
940 debug("newclust: 0x%x\n", newclust);
941 debug("Invalid FAT entry\n");
944 actsize = bytesperclust;
945 curclust = endclust = newclust;
954 static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
955 const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
957 set_start_cluster(mydata, dentptr, start_cluster);
958 dentptr->size = cpu_to_le32(size);
960 dentptr->attr = attr;
962 set_name(dentptr, filename);
966 * Find a directory entry based on filename or start cluster number
967 * If the directory entry is not found,
968 * the new position for writing a directory entry will be returned
970 static dir_entry *find_directory_entry(fat_itr *itr, char *filename)
974 while (fat_itr_next(itr)) {
975 /* check both long and short name: */
976 if (!strcasecmp(filename, itr->name))
978 else if (itr->name != itr->s_name &&
979 !strcasecmp(filename, itr->s_name))
985 if (itr->dent->name[0] == '\0')
991 /* allocate a cluster for more entries */
993 (!itr->is_root || itr->fsdata->fatsize == 32) &&
995 /* indicate that allocating dent failed */
1001 static int split_filename(char *filename, char **dirname, char **basename)
1003 char *p, *last_slash, *last_slash_cont;
1008 last_slash_cont = NULL;
1010 if (ISDIRDELIM(*p)) {
1012 last_slash_cont = p;
1013 /* continuous slashes */
1014 while (ISDIRDELIM(*p))
1015 last_slash_cont = p++;
1023 if (last_slash_cont == (filename + strlen(filename) - 1)) {
1024 /* remove trailing slashes */
1029 if (last_slash == filename) {
1030 /* avoid ""(null) directory */
1034 *dirname = filename;
1037 *last_slash_cont = '\0';
1038 *basename = last_slash_cont + 1;
1040 *dirname = "/"; /* root by default */
1041 *basename = filename;
1048 * normalize_longname() - check long file name and convert to lower case
1050 * We assume here that the FAT file system is using an 8bit code page.
1051 * Linux typically uses CP437, EDK2 assumes CP1250.
1053 * @l_filename: preallocated buffer receiving the normalized name
1054 * @filename: filename to normalize
1055 * Return: 0 on success, -1 on failure
1057 static int normalize_longname(char *l_filename, const char *filename)
1059 const char *p, illegal[] = "<>:\"/\\|?*";
1061 if (strlen(filename) >= VFAT_MAXLEN_BYTES)
1064 for (p = filename; *p; ++p) {
1065 if ((unsigned char)*p < 0x20)
1067 if (strchr(illegal, *p))
1071 strcpy(l_filename, filename);
1072 downcase(l_filename, VFAT_MAXLEN_BYTES);
1077 int file_fat_write_at(const char *filename, loff_t pos, void *buffer,
1078 loff_t size, loff_t *actwrite)
1081 fsdata datablock = { .fatbuf = NULL, };
1082 fsdata *mydata = &datablock;
1083 fat_itr *itr = NULL;
1085 char *filename_copy, *parent, *basename;
1086 char l_filename[VFAT_MAXLEN_BYTES];
1088 debug("writing %s\n", filename);
1090 filename_copy = strdup(filename);
1094 split_filename(filename_copy, &parent, &basename);
1095 if (!strlen(basename)) {
1100 filename = basename;
1101 if (normalize_longname(l_filename, filename)) {
1102 printf("FAT: illegal filename (%s)\n", filename);
1107 itr = malloc_cache_aligned(sizeof(fat_itr));
1113 ret = fat_itr_root(itr, &datablock);
1117 total_sector = datablock.total_sect;
1119 ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1121 printf("%s: doesn't exist (%d)\n", parent, ret);
1125 retdent = find_directory_entry(itr, l_filename);
1128 if (fat_itr_isdir(itr)) {
1135 /* Append to the end */
1136 pos = FAT2CPU32(retdent->size);
1137 if (pos > retdent->size) {
1138 /* No hole allowed */
1143 /* Update file size in a directory entry */
1144 retdent->size = cpu_to_le32(pos + size);
1146 /* Create a new file */
1149 /* root dir cannot have "." or ".." */
1150 if (!strcmp(l_filename, ".") ||
1151 !strcmp(l_filename, "..")) {
1158 printf("Error: allocating new dir entry\n");
1164 /* No hole allowed */
1169 memset(itr->dent, 0, sizeof(*itr->dent));
1171 /* Calculate checksum for short name */
1172 set_name(itr->dent, filename);
1174 /* Set long name entries */
1175 if (fill_dir_slot(itr, filename)) {
1180 /* Set short name entry */
1181 fill_dentry(itr->fsdata, itr->dent, filename, 0, size, 0x20);
1183 retdent = itr->dent;
1186 ret = set_contents(mydata, retdent, pos, buffer, size, actwrite);
1188 printf("Error: writing contents\n");
1192 debug("attempt to write 0x%llx bytes\n", *actwrite);
1194 /* Flush fat buffer */
1195 ret = flush_dirty_fat_buffer(mydata);
1197 printf("Error: flush fat buffer\n");
1202 /* Write directory table to device */
1203 ret = flush_dir(itr);
1205 printf("Error: writing directory entry\n");
1210 free(filename_copy);
1211 free(mydata->fatbuf);
1216 int file_fat_write(const char *filename, void *buffer, loff_t offset,
1217 loff_t maxsize, loff_t *actwrite)
1219 return file_fat_write_at(filename, offset, buffer, maxsize, actwrite);
1222 static int fat_dir_entries(fat_itr *itr)
1225 fsdata fsdata = { .fatbuf = NULL, }, *mydata = &fsdata;
1226 /* for FATBUFSIZE */
1229 dirs = malloc_cache_aligned(sizeof(fat_itr));
1231 debug("Error: allocating memory\n");
1236 /* duplicate fsdata */
1237 fat_itr_child(dirs, itr);
1238 fsdata = *dirs->fsdata;
1240 /* allocate local fat buffer */
1241 fsdata.fatbuf = malloc_cache_aligned(FATBUFSIZE);
1242 if (!fsdata.fatbuf) {
1243 debug("Error: allocating memory\n");
1247 fsdata.fatbufnum = -1;
1248 dirs->fsdata = &fsdata;
1250 for (count = 0; fat_itr_next(dirs); count++)
1254 free(fsdata.fatbuf);
1259 static int delete_dentry(fat_itr *itr)
1261 fsdata *mydata = itr->fsdata;
1262 dir_entry *dentptr = itr->dent;
1264 /* free cluster blocks */
1265 clear_fatent(mydata, START(dentptr));
1266 if (flush_dirty_fat_buffer(mydata) < 0) {
1267 printf("Error: flush fat buffer\n");
1272 * update a directory entry
1274 * - long file name support
1275 * - find and mark the "new" first invalid entry as name[0]=0x00
1277 memset(dentptr, 0, sizeof(*dentptr));
1278 dentptr->name[0] = 0xe5;
1280 if (flush_dir(itr)) {
1281 printf("error: writing directory entry\n");
1288 int fat_unlink(const char *filename)
1290 fsdata fsdata = { .fatbuf = NULL, };
1291 fat_itr *itr = NULL;
1293 char *filename_copy, *dirname, *basename;
1295 filename_copy = strdup(filename);
1296 if (!filename_copy) {
1297 printf("Error: allocating memory\n");
1301 split_filename(filename_copy, &dirname, &basename);
1303 if (!strcmp(dirname, "/") && !strcmp(basename, "")) {
1304 printf("Error: cannot remove root\n");
1309 itr = malloc_cache_aligned(sizeof(fat_itr));
1311 printf("Error: allocating memory\n");
1316 ret = fat_itr_root(itr, &fsdata);
1320 total_sector = fsdata.total_sect;
1322 ret = fat_itr_resolve(itr, dirname, TYPE_DIR);
1324 printf("%s: doesn't exist (%d)\n", dirname, ret);
1329 if (!find_directory_entry(itr, basename)) {
1330 printf("%s: doesn't exist\n", basename);
1335 if (fat_itr_isdir(itr)) {
1336 n_entries = fat_dir_entries(itr);
1337 if (n_entries < 0) {
1341 if (n_entries > 2) {
1342 printf("Error: directory is not empty: %d\n",
1349 ret = delete_dentry(itr);
1352 free(fsdata.fatbuf);
1354 free(filename_copy);
1359 int fat_mkdir(const char *new_dirname)
1362 fsdata datablock = { .fatbuf = NULL, };
1363 fsdata *mydata = &datablock;
1364 fat_itr *itr = NULL;
1365 char *dirname_copy, *parent, *dirname;
1366 char l_dirname[VFAT_MAXLEN_BYTES];
1369 unsigned int bytesperclust;
1370 dir_entry *dotdent = NULL;
1372 dirname_copy = strdup(new_dirname);
1376 split_filename(dirname_copy, &parent, &dirname);
1377 if (!strlen(dirname)) {
1382 if (normalize_longname(l_dirname, dirname)) {
1383 printf("FAT: illegal filename (%s)\n", dirname);
1388 itr = malloc_cache_aligned(sizeof(fat_itr));
1394 ret = fat_itr_root(itr, &datablock);
1398 total_sector = datablock.total_sect;
1400 ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1402 printf("%s: doesn't exist (%d)\n", parent, ret);
1406 retdent = find_directory_entry(itr, l_dirname);
1409 printf("%s: already exists\n", l_dirname);
1414 /* root dir cannot have "." or ".." */
1415 if (!strcmp(l_dirname, ".") ||
1416 !strcmp(l_dirname, "..")) {
1423 printf("Error: allocating new dir entry\n");
1428 memset(itr->dent, 0, sizeof(*itr->dent));
1430 /* Set short name to set alias checksum field in dir_slot */
1431 set_name(itr->dent, dirname);
1432 fill_dir_slot(itr, dirname);
1434 /* Set attribute as archive for regular file */
1435 fill_dentry(itr->fsdata, itr->dent, dirname, 0, 0,
1436 ATTR_DIR | ATTR_ARCH);
1438 retdent = itr->dent;
1441 /* Default entries */
1442 bytesperclust = mydata->clust_size * mydata->sect_size;
1443 dotdent = malloc_cache_aligned(bytesperclust);
1448 memset(dotdent, 0, bytesperclust);
1450 memcpy(dotdent[0].name, ". ", 8);
1451 memcpy(dotdent[0].ext, " ", 3);
1452 dotdent[0].attr = ATTR_DIR | ATTR_ARCH;
1454 memcpy(dotdent[1].name, ".. ", 8);
1455 memcpy(dotdent[1].ext, " ", 3);
1456 dotdent[1].attr = ATTR_DIR | ATTR_ARCH;
1457 set_start_cluster(mydata, &dotdent[1], itr->start_clust);
1459 ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
1460 bytesperclust, &actwrite);
1462 printf("Error: writing contents\n");
1465 /* Write twice for "." */
1466 set_start_cluster(mydata, &dotdent[0], START(retdent));
1467 ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
1468 bytesperclust, &actwrite);
1470 printf("Error: writing contents\n");
1474 /* Flush fat buffer */
1475 ret = flush_dirty_fat_buffer(mydata);
1477 printf("Error: flush fat buffer\n");
1481 /* Write directory table to device */
1482 ret = flush_dir(itr);
1484 printf("Error: writing directory entry\n");
1488 free(mydata->fatbuf);