1 // SPDX-License-Identifier: GPL-2.0+
5 * R/W (V)FAT 12/16/32 filesystem implementation by Donggeun Kim
13 #include <asm/byteorder.h>
15 #include <linux/ctype.h>
17 #include <linux/math64.h>
20 static void uppercase(char *str, int len)
24 for (i = 0; i < len; i++) {
30 static int total_sector;
31 static int disk_write(__u32 block, __u32 nr_blocks, void *buf)
38 if (cur_part_info.start + block + nr_blocks >
39 cur_part_info.start + total_sector) {
40 printf("error: overflow occurs\n");
44 ret = blk_dwrite(cur_dev, cur_part_info.start + block, nr_blocks, buf);
45 if (nr_blocks && ret == 0)
52 * Set short name in directory entry
54 static void set_name(dir_entry *dirent, const char *filename)
56 char s_name[VFAT_MAXLEN_BYTES];
58 int period_location, len, i, ext_num;
63 len = strlen(filename);
67 strcpy(s_name, filename);
68 uppercase(s_name, len);
70 period = strchr(s_name, '.');
72 period_location = len;
75 period_location = period - s_name;
76 ext_num = len - period_location - 1;
79 /* Pad spaces when the length of file name is shorter than eight */
80 if (period_location < 8) {
81 memcpy(dirent->name, s_name, period_location);
82 for (i = period_location; i < 8; i++)
83 dirent->name[i] = ' ';
84 } else if (period_location == 8) {
85 memcpy(dirent->name, s_name, period_location);
87 memcpy(dirent->name, s_name, 6);
88 dirent->name[6] = '~';
89 dirent->name[7] = '1';
93 memcpy(dirent->ext, s_name + period_location + 1, ext_num);
94 for (i = ext_num; i < 3; i++)
97 memcpy(dirent->ext, s_name + period_location + 1, 3);
99 debug("name : %s\n", dirent->name);
100 debug("ext : %s\n", dirent->ext);
104 * Write fat buffer into block device
106 static int flush_dirty_fat_buffer(fsdata *mydata)
108 int getsize = FATBUFBLOCKS;
109 __u32 fatlength = mydata->fatlength;
110 __u8 *bufptr = mydata->fatbuf;
111 __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
113 debug("debug: evicting %d, dirty: %d\n", mydata->fatbufnum,
114 (int)mydata->fat_dirty);
116 if ((!mydata->fat_dirty) || (mydata->fatbufnum == -1))
119 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
120 if (startblock + getsize > fatlength)
121 getsize = fatlength - startblock;
123 startblock += mydata->fat_sect;
126 if (disk_write(startblock, getsize, bufptr) < 0) {
127 debug("error: writing FAT blocks\n");
131 if (mydata->fats == 2) {
132 /* Update corresponding second FAT blocks */
133 startblock += mydata->fatlength;
134 if (disk_write(startblock, getsize, bufptr) < 0) {
135 debug("error: writing second FAT blocks\n");
139 mydata->fat_dirty = 0;
145 * Set the file name information from 'name' into 'slotptr',
147 static int str2slot(dir_slot *slotptr, const char *name, int *idx)
151 for (j = 0; j <= 8; j += 2) {
152 if (name[*idx] == 0x00) {
153 slotptr->name0_4[j] = 0;
154 slotptr->name0_4[j + 1] = 0;
158 slotptr->name0_4[j] = name[*idx];
162 for (j = 0; j <= 10; j += 2) {
163 if (name[*idx] == 0x00) {
164 slotptr->name5_10[j] = 0;
165 slotptr->name5_10[j + 1] = 0;
169 slotptr->name5_10[j] = name[*idx];
173 for (j = 0; j <= 2; j += 2) {
174 if (name[*idx] == 0x00) {
175 slotptr->name11_12[j] = 0;
176 slotptr->name11_12[j + 1] = 0;
180 slotptr->name11_12[j] = name[*idx];
185 if (name[*idx] == 0x00)
189 /* Not used characters are filled with 0xff 0xff */
191 for (; end_idx < 5; end_idx++) {
192 slotptr->name0_4[end_idx * 2] = 0xff;
193 slotptr->name0_4[end_idx * 2 + 1] = 0xff;
198 for (; end_idx < 6; end_idx++) {
199 slotptr->name5_10[end_idx * 2] = 0xff;
200 slotptr->name5_10[end_idx * 2 + 1] = 0xff;
205 for (; end_idx < 2; end_idx++) {
206 slotptr->name11_12[end_idx * 2] = 0xff;
207 slotptr->name11_12[end_idx * 2 + 1] = 0xff;
213 static int new_dir_table(fat_itr *itr);
214 static int flush_dir(fat_itr *itr);
217 * Fill dir_slot entries with appropriate name, id, and attr
218 * 'itr' will point to a next entry
221 fill_dir_slot(fat_itr *itr, const char *l_name)
223 __u8 temp_dir_slot_buffer[MAX_LFN_SLOT * sizeof(dir_slot)];
224 dir_slot *slotptr = (dir_slot *)temp_dir_slot_buffer;
225 __u8 counter = 0, checksum;
228 /* Get short file name checksum value */
229 checksum = mkcksum(itr->dent->name, itr->dent->ext);
232 memset(slotptr, 0x00, sizeof(dir_slot));
233 ret = str2slot(slotptr, l_name, &idx);
234 slotptr->id = ++counter;
235 slotptr->attr = ATTR_VFAT;
236 slotptr->alias_checksum = checksum;
241 slotptr->id |= LAST_LONG_ENTRY_MASK;
243 while (counter >= 1) {
244 memcpy(itr->dent, slotptr, sizeof(dir_slot));
248 if (itr->remaining == 0)
251 /* allocate a cluster for more entries */
252 if (!fat_itr_next(itr))
254 (!itr->is_root || itr->fsdata->fatsize == 32) &&
263 * Set the entry at index 'entry' in a FAT (12/16/32) table.
265 static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
267 __u32 bufnum, offset, off16;
270 switch (mydata->fatsize) {
272 bufnum = entry / FAT32BUFSIZE;
273 offset = entry - bufnum * FAT32BUFSIZE;
276 bufnum = entry / FAT16BUFSIZE;
277 offset = entry - bufnum * FAT16BUFSIZE;
280 bufnum = entry / FAT12BUFSIZE;
281 offset = entry - bufnum * FAT12BUFSIZE;
284 /* Unsupported FAT size */
288 /* Read a new block of FAT entries into the cache. */
289 if (bufnum != mydata->fatbufnum) {
290 int getsize = FATBUFBLOCKS;
291 __u8 *bufptr = mydata->fatbuf;
292 __u32 fatlength = mydata->fatlength;
293 __u32 startblock = bufnum * FATBUFBLOCKS;
295 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
296 if (startblock + getsize > fatlength)
297 getsize = fatlength - startblock;
299 if (flush_dirty_fat_buffer(mydata) < 0)
302 startblock += mydata->fat_sect;
304 if (disk_read(startblock, getsize, bufptr) < 0) {
305 debug("Error reading FAT blocks\n");
308 mydata->fatbufnum = bufnum;
312 mydata->fat_dirty = 1;
314 /* Set the actual entry */
315 switch (mydata->fatsize) {
317 ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
320 ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
323 off16 = (offset * 3) / 4;
325 switch (offset & 0x3) {
327 val1 = cpu_to_le16(entry_value) & 0xfff;
328 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff;
329 ((__u16 *)mydata->fatbuf)[off16] |= val1;
332 val1 = cpu_to_le16(entry_value) & 0xf;
333 val2 = (cpu_to_le16(entry_value) >> 4) & 0xff;
335 ((__u16 *)mydata->fatbuf)[off16] &= ~0xf000;
336 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 12);
338 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xff;
339 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
342 val1 = cpu_to_le16(entry_value) & 0xff;
343 val2 = (cpu_to_le16(entry_value) >> 8) & 0xf;
345 ((__u16 *)mydata->fatbuf)[off16] &= ~0xff00;
346 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 8);
348 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xf;
349 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
352 val1 = cpu_to_le16(entry_value) & 0xfff;
353 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff0;
354 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 4);
369 * Determine the next free cluster after 'entry' in a FAT (12/16/32) table
370 * and link it to 'entry'. EOC marker is not set on returned entry.
372 static __u32 determine_fatent(fsdata *mydata, __u32 entry)
374 __u32 next_fat, next_entry = entry + 1;
377 next_fat = get_fatent(mydata, next_entry);
379 /* found free entry, link to entry */
380 set_fatent_value(mydata, entry, next_entry);
385 debug("FAT%d: entry: %08x, entry_value: %04x\n",
386 mydata->fatsize, entry, next_entry);
392 * set_sectors() - write data to sectors
394 * Write 'size' bytes from 'buffer' into the specified sector.
396 * @mydata: data to be written
397 * @startsect: sector to be written to
398 * @buffer: data to be written
399 * @size: bytes to be written (but not more than the size of a cluster)
400 * Return: 0 on success, -1 otherwise
403 set_sectors(fsdata *mydata, u32 startsect, u8 *buffer, u32 size)
408 debug("startsect: %d\n", startsect);
410 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
411 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
413 debug("FAT: Misaligned buffer address (%p)\n", buffer);
415 while (size >= mydata->sect_size) {
416 memcpy(tmpbuf, buffer, mydata->sect_size);
417 ret = disk_write(startsect++, 1, tmpbuf);
419 debug("Error writing data (got %d)\n", ret);
423 buffer += mydata->sect_size;
424 size -= mydata->sect_size;
426 } else if (size >= mydata->sect_size) {
427 nsects = size / mydata->sect_size;
428 ret = disk_write(startsect, nsects, buffer);
430 debug("Error writing data (got %d)\n", ret);
435 buffer += nsects * mydata->sect_size;
436 size -= nsects * mydata->sect_size;
440 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
441 /* Do not leak content of stack */
442 memset(tmpbuf, 0, mydata->sect_size);
443 memcpy(tmpbuf, buffer, size);
444 ret = disk_write(startsect, 1, tmpbuf);
446 debug("Error writing data (got %d)\n", ret);
455 * set_cluster() - write data to cluster
457 * Write 'size' bytes from 'buffer' into the specified cluster.
459 * @mydata: data to be written
460 * @clustnum: cluster to be written to
461 * @buffer: data to be written
462 * @size: bytes to be written (but not more than the size of a cluster)
463 * Return: 0 on success, -1 otherwise
466 set_cluster(fsdata *mydata, u32 clustnum, u8 *buffer, u32 size)
468 return set_sectors(mydata, clust_to_sect(mydata, clustnum),
473 flush_dir(fat_itr *itr)
475 fsdata *mydata = itr->fsdata;
476 u32 startsect, sect_offset, nsects;
478 if (!itr->is_root || mydata->fatsize == 32)
479 return set_cluster(mydata, itr->clust, itr->block,
480 mydata->clust_size * mydata->sect_size);
482 sect_offset = itr->clust * mydata->clust_size;
483 startsect = mydata->rootdir_sect + sect_offset;
484 /* do not write past the end of rootdir */
485 nsects = min_t(u32, mydata->clust_size,
486 mydata->rootdir_size - sect_offset);
488 return set_sectors(mydata, startsect, itr->block,
489 nsects * mydata->sect_size);
492 static __u8 tmpbuf_cluster[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN);
495 * Read and modify data on existing and consecutive cluster blocks
498 get_set_cluster(fsdata *mydata, __u32 clustnum, loff_t pos, __u8 *buffer,
499 loff_t size, loff_t *gotsize)
501 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
504 int clustcount, i, ret;
510 assert(pos < bytesperclust);
511 startsect = clust_to_sect(mydata, clustnum);
513 debug("clustnum: %d, startsect: %d, pos: %lld\n",
514 clustnum, startsect, pos);
516 /* partial write at beginning */
518 wsize = min(bytesperclust - pos, size);
519 ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster);
520 if (ret != mydata->clust_size) {
521 debug("Error reading data (got %d)\n", ret);
525 memcpy(tmpbuf_cluster + pos, buffer, wsize);
526 ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster);
527 if (ret != mydata->clust_size) {
528 debug("Error writing data (got %d)\n", ret);
536 startsect += mydata->clust_size;
542 /* full-cluster write */
543 if (size >= bytesperclust) {
544 clustcount = lldiv(size, bytesperclust);
546 if (!((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1))) {
547 wsize = clustcount * bytesperclust;
548 ret = disk_write(startsect,
549 clustcount * mydata->clust_size,
551 if (ret != clustcount * mydata->clust_size) {
552 debug("Error writing data (got %d)\n", ret);
560 startsect += clustcount * mydata->clust_size;
562 for (i = 0; i < clustcount; i++) {
563 memcpy(tmpbuf_cluster, buffer, bytesperclust);
564 ret = disk_write(startsect,
567 if (ret != mydata->clust_size) {
568 debug("Error writing data (got %d)\n",
573 size -= bytesperclust;
574 buffer += bytesperclust;
575 *gotsize += bytesperclust;
577 startsect += mydata->clust_size;
582 /* partial write at end */
585 ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster);
586 if (ret != mydata->clust_size) {
587 debug("Error reading data (got %d)\n", ret);
590 memcpy(tmpbuf_cluster, buffer, wsize);
591 ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster);
592 if (ret != mydata->clust_size) {
593 debug("Error writing data (got %d)\n", ret);
608 * Find the first empty cluster
610 static int find_empty_cluster(fsdata *mydata)
612 __u32 fat_val, entry = 3;
615 fat_val = get_fatent(mydata, entry);
625 * Allocate a cluster for additional directory entries
627 static int new_dir_table(fat_itr *itr)
629 fsdata *mydata = itr->fsdata;
630 int dir_newclust = 0;
631 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
633 dir_newclust = find_empty_cluster(mydata);
634 set_fatent_value(mydata, itr->clust, dir_newclust);
635 if (mydata->fatsize == 32)
636 set_fatent_value(mydata, dir_newclust, 0xffffff8);
637 else if (mydata->fatsize == 16)
638 set_fatent_value(mydata, dir_newclust, 0xfff8);
639 else if (mydata->fatsize == 12)
640 set_fatent_value(mydata, dir_newclust, 0xff8);
642 itr->clust = dir_newclust;
643 itr->next_clust = dir_newclust;
645 if (flush_dirty_fat_buffer(mydata) < 0)
648 memset(itr->block, 0x00, bytesperclust);
650 itr->dent = (dir_entry *)itr->block;
651 itr->last_cluster = 1;
652 itr->remaining = bytesperclust / sizeof(dir_entry) - 1;
658 * Set empty cluster from 'entry' to the end of a file
660 static int clear_fatent(fsdata *mydata, __u32 entry)
664 while (!CHECK_CLUST(entry, mydata->fatsize)) {
665 fat_val = get_fatent(mydata, entry);
667 set_fatent_value(mydata, entry, 0);
674 /* Flush fat buffer */
675 if (flush_dirty_fat_buffer(mydata) < 0)
682 * Set start cluster in directory entry
684 static void set_start_cluster(const fsdata *mydata, dir_entry *dentptr,
687 if (mydata->fatsize == 32)
689 cpu_to_le16((start_cluster & 0xffff0000) >> 16);
690 dentptr->start = cpu_to_le16(start_cluster & 0xffff);
694 * Check whether adding a file makes the file system to
695 * exceed the size of the block device
696 * Return -1 when overflow occurs, otherwise return 0
698 static int check_overflow(fsdata *mydata, __u32 clustnum, loff_t size)
700 __u32 startsect, sect_num, offset;
703 startsect = clust_to_sect(mydata, clustnum);
705 startsect = mydata->rootdir_sect;
707 sect_num = div_u64_rem(size, mydata->sect_size, &offset);
712 if (startsect + sect_num > total_sector)
718 * Write at most 'maxsize' bytes from 'buffer' into
719 * the file associated with 'dentptr'
720 * Update the number of bytes written in *gotsize and return 0
721 * or return -1 on fatal errors.
724 set_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, __u8 *buffer,
725 loff_t maxsize, loff_t *gotsize)
727 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
728 __u32 curclust = START(dentptr);
729 __u32 endclust = 0, newclust = 0;
730 u64 cur_pos, filesize;
731 loff_t offset, actsize, wsize;
734 filesize = pos + maxsize;
736 debug("%llu bytes\n", filesize);
741 if (!CHECK_CLUST(curclust, mydata->fatsize) ||
742 IS_LAST_CLUST(curclust, mydata->fatsize)) {
743 clear_fatent(mydata, curclust);
744 set_start_cluster(mydata, dentptr, 0);
747 debug("curclust: 0x%x\n", curclust);
748 debug("Invalid FAT entry\n");
757 /* go to cluster at pos */
758 cur_pos = bytesperclust;
762 if (IS_LAST_CLUST(curclust, mydata->fatsize))
765 newclust = get_fatent(mydata, curclust);
766 if (!IS_LAST_CLUST(newclust, mydata->fatsize) &&
767 CHECK_CLUST(newclust, mydata->fatsize)) {
768 debug("curclust: 0x%x\n", curclust);
769 debug("Invalid FAT entry\n");
773 cur_pos += bytesperclust;
776 if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
777 assert(pos == cur_pos);
781 assert(pos < cur_pos);
782 cur_pos -= bytesperclust;
785 assert(IS_LAST_CLUST(curclust, mydata->fatsize) ||
786 !CHECK_CLUST(curclust, mydata->fatsize));
789 /* search for allocated consecutive clusters */
790 actsize = bytesperclust;
793 if (filesize <= (cur_pos + actsize))
796 newclust = get_fatent(mydata, endclust);
798 if (IS_LAST_CLUST(newclust, mydata->fatsize))
800 if (CHECK_CLUST(newclust, mydata->fatsize)) {
801 debug("curclust: 0x%x\n", curclust);
802 debug("Invalid FAT entry\n");
806 actsize += bytesperclust;
810 /* overwrite to <curclust..endclust> */
814 offset = pos - cur_pos;
815 wsize = min(cur_pos + actsize, filesize) - pos;
816 if (get_set_cluster(mydata, curclust, offset,
817 buffer, wsize, &actsize)) {
818 printf("Error get-and-setting cluster\n");
823 cur_pos += offset + wsize;
825 if (filesize <= cur_pos)
828 /* CHECK: newclust = get_fatent(mydata, endclust); */
830 if (IS_LAST_CLUST(newclust, mydata->fatsize))
831 /* no more clusters */
837 if (filesize <= cur_pos) {
839 newclust = get_fatent(mydata, endclust);
840 if (!IS_LAST_CLUST(newclust, mydata->fatsize)) {
841 /* truncate the rest */
842 clear_fatent(mydata, newclust);
844 /* Mark end of file in FAT */
845 if (mydata->fatsize == 12)
847 else if (mydata->fatsize == 16)
849 else if (mydata->fatsize == 32)
850 newclust = 0xfffffff;
851 set_fatent_value(mydata, endclust, newclust);
859 assert(!do_div(cur_pos, bytesperclust));
862 /* allocate and write */
865 /* Assure that curclust is valid */
867 curclust = find_empty_cluster(mydata);
868 set_start_cluster(mydata, dentptr, curclust);
870 newclust = get_fatent(mydata, curclust);
872 if (IS_LAST_CLUST(newclust, mydata->fatsize)) {
873 newclust = determine_fatent(mydata, curclust);
874 set_fatent_value(mydata, curclust, newclust);
877 debug("error: something wrong\n");
882 /* TODO: already partially written */
883 if (check_overflow(mydata, curclust, filesize)) {
884 printf("Error: no space left: %llu\n", filesize);
888 actsize = bytesperclust;
891 /* search for consecutive clusters */
892 while (actsize < filesize) {
893 newclust = determine_fatent(mydata, endclust);
895 if ((newclust - 1) != endclust)
896 /* write to <curclust..endclust> */
899 if (CHECK_CLUST(newclust, mydata->fatsize)) {
900 debug("newclust: 0x%x\n", newclust);
901 debug("Invalid FAT entry\n");
905 actsize += bytesperclust;
908 /* set remaining bytes */
910 if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
911 debug("error: writing cluster\n");
916 /* Mark end of file in FAT */
917 if (mydata->fatsize == 12)
919 else if (mydata->fatsize == 16)
921 else if (mydata->fatsize == 32)
922 newclust = 0xfffffff;
923 set_fatent_value(mydata, endclust, newclust);
927 if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
928 debug("error: writing cluster\n");
935 if (CHECK_CLUST(newclust, mydata->fatsize)) {
936 debug("newclust: 0x%x\n", newclust);
937 debug("Invalid FAT entry\n");
940 actsize = bytesperclust;
941 curclust = endclust = newclust;
950 static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
951 const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
953 set_start_cluster(mydata, dentptr, start_cluster);
954 dentptr->size = cpu_to_le32(size);
956 dentptr->attr = attr;
958 set_name(dentptr, filename);
962 * Find a directory entry based on filename or start cluster number
963 * If the directory entry is not found,
964 * the new position for writing a directory entry will be returned
966 static dir_entry *find_directory_entry(fat_itr *itr, char *filename)
970 while (fat_itr_next(itr)) {
971 /* check both long and short name: */
972 if (!strcasecmp(filename, itr->name))
974 else if (itr->name != itr->s_name &&
975 !strcasecmp(filename, itr->s_name))
981 if (itr->dent->name[0] == '\0')
987 /* allocate a cluster for more entries */
989 (!itr->is_root || itr->fsdata->fatsize == 32) &&
991 /* indicate that allocating dent failed */
997 static int split_filename(char *filename, char **dirname, char **basename)
999 char *p, *last_slash, *last_slash_cont;
1004 last_slash_cont = NULL;
1006 if (ISDIRDELIM(*p)) {
1008 last_slash_cont = p;
1009 /* continuous slashes */
1010 while (ISDIRDELIM(*p))
1011 last_slash_cont = p++;
1019 if (last_slash_cont == (filename + strlen(filename) - 1)) {
1020 /* remove trailing slashes */
1025 if (last_slash == filename) {
1026 /* avoid ""(null) directory */
1030 *dirname = filename;
1033 *last_slash_cont = '\0';
1034 *basename = last_slash_cont + 1;
1036 *dirname = "/"; /* root by default */
1037 *basename = filename;
1044 * normalize_longname() - check long file name and convert to lower case
1046 * We assume here that the FAT file system is using an 8bit code page.
1047 * Linux typically uses CP437, EDK2 assumes CP1250.
1049 * @l_filename: preallocated buffer receiving the normalized name
1050 * @filename: filename to normalize
1051 * Return: 0 on success, -1 on failure
1053 static int normalize_longname(char *l_filename, const char *filename)
1055 const char *p, illegal[] = "<>:\"/\\|?*";
1057 if (strlen(filename) >= VFAT_MAXLEN_BYTES)
1060 for (p = filename; *p; ++p) {
1061 if ((unsigned char)*p < 0x20)
1063 if (strchr(illegal, *p))
1067 strcpy(l_filename, filename);
1068 downcase(l_filename, VFAT_MAXLEN_BYTES);
1073 int file_fat_write_at(const char *filename, loff_t pos, void *buffer,
1074 loff_t size, loff_t *actwrite)
1077 fsdata datablock = { .fatbuf = NULL, };
1078 fsdata *mydata = &datablock;
1079 fat_itr *itr = NULL;
1081 char *filename_copy, *parent, *basename;
1082 char l_filename[VFAT_MAXLEN_BYTES];
1084 debug("writing %s\n", filename);
1086 filename_copy = strdup(filename);
1090 split_filename(filename_copy, &parent, &basename);
1091 if (!strlen(basename)) {
1096 filename = basename;
1097 if (normalize_longname(l_filename, filename)) {
1098 printf("FAT: illegal filename (%s)\n", filename);
1103 itr = malloc_cache_aligned(sizeof(fat_itr));
1109 ret = fat_itr_root(itr, &datablock);
1113 total_sector = datablock.total_sect;
1115 ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1117 printf("%s: doesn't exist (%d)\n", parent, ret);
1121 retdent = find_directory_entry(itr, l_filename);
1124 if (fat_itr_isdir(itr)) {
1131 /* Append to the end */
1132 pos = FAT2CPU32(retdent->size);
1133 if (pos > retdent->size) {
1134 /* No hole allowed */
1139 /* Update file size in a directory entry */
1140 retdent->size = cpu_to_le32(pos + size);
1142 /* Create a new file */
1145 /* root dir cannot have "." or ".." */
1146 if (!strcmp(l_filename, ".") ||
1147 !strcmp(l_filename, "..")) {
1154 printf("Error: allocating new dir entry\n");
1160 /* No hole allowed */
1165 memset(itr->dent, 0, sizeof(*itr->dent));
1167 /* Calculate checksum for short name */
1168 set_name(itr->dent, filename);
1170 /* Set long name entries */
1171 if (fill_dir_slot(itr, filename)) {
1176 /* Set short name entry */
1177 fill_dentry(itr->fsdata, itr->dent, filename, 0, size, 0x20);
1179 retdent = itr->dent;
1182 ret = set_contents(mydata, retdent, pos, buffer, size, actwrite);
1184 printf("Error: writing contents\n");
1188 debug("attempt to write 0x%llx bytes\n", *actwrite);
1190 /* Flush fat buffer */
1191 ret = flush_dirty_fat_buffer(mydata);
1193 printf("Error: flush fat buffer\n");
1198 /* Write directory table to device */
1199 ret = flush_dir(itr);
1201 printf("Error: writing directory entry\n");
1206 free(filename_copy);
1207 free(mydata->fatbuf);
1212 int file_fat_write(const char *filename, void *buffer, loff_t offset,
1213 loff_t maxsize, loff_t *actwrite)
1215 return file_fat_write_at(filename, offset, buffer, maxsize, actwrite);
1218 static int fat_dir_entries(fat_itr *itr)
1221 fsdata fsdata = { .fatbuf = NULL, }, *mydata = &fsdata;
1222 /* for FATBUFSIZE */
1225 dirs = malloc_cache_aligned(sizeof(fat_itr));
1227 debug("Error: allocating memory\n");
1232 /* duplicate fsdata */
1233 fat_itr_child(dirs, itr);
1234 fsdata = *dirs->fsdata;
1236 /* allocate local fat buffer */
1237 fsdata.fatbuf = malloc_cache_aligned(FATBUFSIZE);
1238 if (!fsdata.fatbuf) {
1239 debug("Error: allocating memory\n");
1243 fsdata.fatbufnum = -1;
1244 dirs->fsdata = &fsdata;
1246 for (count = 0; fat_itr_next(dirs); count++)
1250 free(fsdata.fatbuf);
1255 static int delete_dentry(fat_itr *itr)
1257 fsdata *mydata = itr->fsdata;
1258 dir_entry *dentptr = itr->dent;
1260 /* free cluster blocks */
1261 clear_fatent(mydata, START(dentptr));
1262 if (flush_dirty_fat_buffer(mydata) < 0) {
1263 printf("Error: flush fat buffer\n");
1268 * update a directory entry
1270 * - long file name support
1271 * - find and mark the "new" first invalid entry as name[0]=0x00
1273 memset(dentptr, 0, sizeof(*dentptr));
1274 dentptr->name[0] = 0xe5;
1276 if (flush_dir(itr)) {
1277 printf("error: writing directory entry\n");
1284 int fat_unlink(const char *filename)
1286 fsdata fsdata = { .fatbuf = NULL, };
1287 fat_itr *itr = NULL;
1289 char *filename_copy, *dirname, *basename;
1291 filename_copy = strdup(filename);
1292 if (!filename_copy) {
1293 printf("Error: allocating memory\n");
1297 split_filename(filename_copy, &dirname, &basename);
1299 if (!strcmp(dirname, "/") && !strcmp(basename, "")) {
1300 printf("Error: cannot remove root\n");
1305 itr = malloc_cache_aligned(sizeof(fat_itr));
1307 printf("Error: allocating memory\n");
1312 ret = fat_itr_root(itr, &fsdata);
1316 total_sector = fsdata.total_sect;
1318 ret = fat_itr_resolve(itr, dirname, TYPE_DIR);
1320 printf("%s: doesn't exist (%d)\n", dirname, ret);
1325 if (!find_directory_entry(itr, basename)) {
1326 printf("%s: doesn't exist\n", basename);
1331 if (fat_itr_isdir(itr)) {
1332 n_entries = fat_dir_entries(itr);
1333 if (n_entries < 0) {
1337 if (n_entries > 2) {
1338 printf("Error: directory is not empty: %d\n",
1345 ret = delete_dentry(itr);
1348 free(fsdata.fatbuf);
1350 free(filename_copy);
1355 int fat_mkdir(const char *new_dirname)
1358 fsdata datablock = { .fatbuf = NULL, };
1359 fsdata *mydata = &datablock;
1360 fat_itr *itr = NULL;
1361 char *dirname_copy, *parent, *dirname;
1362 char l_dirname[VFAT_MAXLEN_BYTES];
1365 unsigned int bytesperclust;
1366 dir_entry *dotdent = NULL;
1368 dirname_copy = strdup(new_dirname);
1372 split_filename(dirname_copy, &parent, &dirname);
1373 if (!strlen(dirname)) {
1378 if (normalize_longname(l_dirname, dirname)) {
1379 printf("FAT: illegal filename (%s)\n", dirname);
1384 itr = malloc_cache_aligned(sizeof(fat_itr));
1390 ret = fat_itr_root(itr, &datablock);
1394 total_sector = datablock.total_sect;
1396 ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1398 printf("%s: doesn't exist (%d)\n", parent, ret);
1402 retdent = find_directory_entry(itr, l_dirname);
1405 printf("%s: already exists\n", l_dirname);
1410 /* root dir cannot have "." or ".." */
1411 if (!strcmp(l_dirname, ".") ||
1412 !strcmp(l_dirname, "..")) {
1419 printf("Error: allocating new dir entry\n");
1424 memset(itr->dent, 0, sizeof(*itr->dent));
1426 /* Set short name to set alias checksum field in dir_slot */
1427 set_name(itr->dent, dirname);
1428 fill_dir_slot(itr, dirname);
1430 /* Set attribute as archive for regular file */
1431 fill_dentry(itr->fsdata, itr->dent, dirname, 0, 0,
1432 ATTR_DIR | ATTR_ARCH);
1434 retdent = itr->dent;
1437 /* Default entries */
1438 bytesperclust = mydata->clust_size * mydata->sect_size;
1439 dotdent = malloc_cache_aligned(bytesperclust);
1444 memset(dotdent, 0, bytesperclust);
1446 memcpy(dotdent[0].name, ". ", 8);
1447 memcpy(dotdent[0].ext, " ", 3);
1448 dotdent[0].attr = ATTR_DIR | ATTR_ARCH;
1450 memcpy(dotdent[1].name, ".. ", 8);
1451 memcpy(dotdent[1].ext, " ", 3);
1452 dotdent[1].attr = ATTR_DIR | ATTR_ARCH;
1453 set_start_cluster(mydata, &dotdent[1], itr->start_clust);
1455 ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
1456 bytesperclust, &actwrite);
1458 printf("Error: writing contents\n");
1461 /* Write twice for "." */
1462 set_start_cluster(mydata, &dotdent[0], START(retdent));
1463 ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
1464 bytesperclust, &actwrite);
1466 printf("Error: writing contents\n");
1470 /* Flush fat buffer */
1471 ret = flush_dirty_fat_buffer(mydata);
1473 printf("Error: flush fat buffer\n");
1477 /* Write directory table to device */
1478 ret = flush_dir(itr);
1480 printf("Error: writing directory entry\n");
1484 free(mydata->fatbuf);