1 // SPDX-License-Identifier: GPL-2.0+
5 * R/W (V)FAT 12/16/32 filesystem implementation by Donggeun Kim
12 #include <asm/byteorder.h>
14 #include <linux/ctype.h>
16 #include <linux/math64.h>
19 static void uppercase(char *str, int len)
23 for (i = 0; i < len; i++) {
29 static int total_sector;
30 static int disk_write(__u32 block, __u32 nr_blocks, void *buf)
37 if (cur_part_info.start + block + nr_blocks >
38 cur_part_info.start + total_sector) {
39 printf("error: overflow occurs\n");
43 ret = blk_dwrite(cur_dev, cur_part_info.start + block, nr_blocks, buf);
44 if (nr_blocks && ret == 0)
51 * Set short name in directory entry
53 static void set_name(dir_entry *dirent, const char *filename)
55 char s_name[VFAT_MAXLEN_BYTES];
57 int period_location, len, i, ext_num;
62 len = strlen(filename);
66 strcpy(s_name, filename);
67 uppercase(s_name, len);
69 period = strchr(s_name, '.');
71 period_location = len;
74 period_location = period - s_name;
75 ext_num = len - period_location - 1;
78 /* Pad spaces when the length of file name is shorter than eight */
79 if (period_location < 8) {
80 memcpy(dirent->name, s_name, period_location);
81 for (i = period_location; i < 8; i++)
82 dirent->name[i] = ' ';
83 } else if (period_location == 8) {
84 memcpy(dirent->name, s_name, period_location);
86 memcpy(dirent->name, s_name, 6);
87 dirent->name[6] = '~';
88 dirent->name[7] = '1';
92 memcpy(dirent->ext, s_name + period_location + 1, ext_num);
93 for (i = ext_num; i < 3; i++)
96 memcpy(dirent->ext, s_name + period_location + 1, 3);
98 debug("name : %s\n", dirent->name);
99 debug("ext : %s\n", dirent->ext);
103 * Write fat buffer into block device
105 static int flush_dirty_fat_buffer(fsdata *mydata)
107 int getsize = FATBUFBLOCKS;
108 __u32 fatlength = mydata->fatlength;
109 __u8 *bufptr = mydata->fatbuf;
110 __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
112 debug("debug: evicting %d, dirty: %d\n", mydata->fatbufnum,
113 (int)mydata->fat_dirty);
115 if ((!mydata->fat_dirty) || (mydata->fatbufnum == -1))
118 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
119 if (startblock + getsize > fatlength)
120 getsize = fatlength - startblock;
122 startblock += mydata->fat_sect;
125 if (disk_write(startblock, getsize, bufptr) < 0) {
126 debug("error: writing FAT blocks\n");
130 if (mydata->fats == 2) {
131 /* Update corresponding second FAT blocks */
132 startblock += mydata->fatlength;
133 if (disk_write(startblock, getsize, bufptr) < 0) {
134 debug("error: writing second FAT blocks\n");
138 mydata->fat_dirty = 0;
144 * Set the file name information from 'name' into 'slotptr',
146 static int str2slot(dir_slot *slotptr, const char *name, int *idx)
150 for (j = 0; j <= 8; j += 2) {
151 if (name[*idx] == 0x00) {
152 slotptr->name0_4[j] = 0;
153 slotptr->name0_4[j + 1] = 0;
157 slotptr->name0_4[j] = name[*idx];
161 for (j = 0; j <= 10; j += 2) {
162 if (name[*idx] == 0x00) {
163 slotptr->name5_10[j] = 0;
164 slotptr->name5_10[j + 1] = 0;
168 slotptr->name5_10[j] = name[*idx];
172 for (j = 0; j <= 2; j += 2) {
173 if (name[*idx] == 0x00) {
174 slotptr->name11_12[j] = 0;
175 slotptr->name11_12[j + 1] = 0;
179 slotptr->name11_12[j] = name[*idx];
184 if (name[*idx] == 0x00)
188 /* Not used characters are filled with 0xff 0xff */
190 for (; end_idx < 5; end_idx++) {
191 slotptr->name0_4[end_idx * 2] = 0xff;
192 slotptr->name0_4[end_idx * 2 + 1] = 0xff;
197 for (; end_idx < 6; end_idx++) {
198 slotptr->name5_10[end_idx * 2] = 0xff;
199 slotptr->name5_10[end_idx * 2 + 1] = 0xff;
204 for (; end_idx < 2; end_idx++) {
205 slotptr->name11_12[end_idx * 2] = 0xff;
206 slotptr->name11_12[end_idx * 2 + 1] = 0xff;
212 static int new_dir_table(fat_itr *itr);
213 static int flush_dir(fat_itr *itr);
216 * Fill dir_slot entries with appropriate name, id, and attr
217 * 'itr' will point to a next entry
220 fill_dir_slot(fat_itr *itr, const char *l_name)
222 __u8 temp_dir_slot_buffer[MAX_LFN_SLOT * sizeof(dir_slot)];
223 dir_slot *slotptr = (dir_slot *)temp_dir_slot_buffer;
224 __u8 counter = 0, checksum;
227 /* Get short file name checksum value */
228 checksum = mkcksum(itr->dent->name, itr->dent->ext);
231 memset(slotptr, 0x00, sizeof(dir_slot));
232 ret = str2slot(slotptr, l_name, &idx);
233 slotptr->id = ++counter;
234 slotptr->attr = ATTR_VFAT;
235 slotptr->alias_checksum = checksum;
240 slotptr->id |= LAST_LONG_ENTRY_MASK;
242 while (counter >= 1) {
243 memcpy(itr->dent, slotptr, sizeof(dir_slot));
247 if (itr->remaining == 0)
250 /* allocate a cluster for more entries */
251 if (!fat_itr_next(itr))
253 (!itr->is_root || itr->fsdata->fatsize == 32) &&
262 * Set the entry at index 'entry' in a FAT (12/16/32) table.
264 static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
266 __u32 bufnum, offset, off16;
269 switch (mydata->fatsize) {
271 bufnum = entry / FAT32BUFSIZE;
272 offset = entry - bufnum * FAT32BUFSIZE;
275 bufnum = entry / FAT16BUFSIZE;
276 offset = entry - bufnum * FAT16BUFSIZE;
279 bufnum = entry / FAT12BUFSIZE;
280 offset = entry - bufnum * FAT12BUFSIZE;
283 /* Unsupported FAT size */
287 /* Read a new block of FAT entries into the cache. */
288 if (bufnum != mydata->fatbufnum) {
289 int getsize = FATBUFBLOCKS;
290 __u8 *bufptr = mydata->fatbuf;
291 __u32 fatlength = mydata->fatlength;
292 __u32 startblock = bufnum * FATBUFBLOCKS;
294 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
295 if (startblock + getsize > fatlength)
296 getsize = fatlength - startblock;
298 if (flush_dirty_fat_buffer(mydata) < 0)
301 startblock += mydata->fat_sect;
303 if (disk_read(startblock, getsize, bufptr) < 0) {
304 debug("Error reading FAT blocks\n");
307 mydata->fatbufnum = bufnum;
311 mydata->fat_dirty = 1;
313 /* Set the actual entry */
314 switch (mydata->fatsize) {
316 ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
319 ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
322 off16 = (offset * 3) / 4;
324 switch (offset & 0x3) {
326 val1 = cpu_to_le16(entry_value) & 0xfff;
327 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff;
328 ((__u16 *)mydata->fatbuf)[off16] |= val1;
331 val1 = cpu_to_le16(entry_value) & 0xf;
332 val2 = (cpu_to_le16(entry_value) >> 4) & 0xff;
334 ((__u16 *)mydata->fatbuf)[off16] &= ~0xf000;
335 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 12);
337 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xff;
338 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
341 val1 = cpu_to_le16(entry_value) & 0xff;
342 val2 = (cpu_to_le16(entry_value) >> 8) & 0xf;
344 ((__u16 *)mydata->fatbuf)[off16] &= ~0xff00;
345 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 8);
347 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xf;
348 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
351 val1 = cpu_to_le16(entry_value) & 0xfff;
352 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff0;
353 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 4);
368 * Determine the next free cluster after 'entry' in a FAT (12/16/32) table
369 * and link it to 'entry'. EOC marker is not set on returned entry.
371 static __u32 determine_fatent(fsdata *mydata, __u32 entry)
373 __u32 next_fat, next_entry = entry + 1;
376 next_fat = get_fatent(mydata, next_entry);
378 /* found free entry, link to entry */
379 set_fatent_value(mydata, entry, next_entry);
384 debug("FAT%d: entry: %08x, entry_value: %04x\n",
385 mydata->fatsize, entry, next_entry);
391 * set_sectors() - write data to sectors
393 * Write 'size' bytes from 'buffer' into the specified sector.
395 * @mydata: data to be written
396 * @startsect: sector to be written to
397 * @buffer: data to be written
398 * @size: bytes to be written (but not more than the size of a cluster)
399 * Return: 0 on success, -1 otherwise
402 set_sectors(fsdata *mydata, u32 startsect, u8 *buffer, u32 size)
407 debug("startsect: %d\n", startsect);
409 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
410 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
412 debug("FAT: Misaligned buffer address (%p)\n", buffer);
414 while (size >= mydata->sect_size) {
415 memcpy(tmpbuf, buffer, mydata->sect_size);
416 ret = disk_write(startsect++, 1, tmpbuf);
418 debug("Error writing data (got %d)\n", ret);
422 buffer += mydata->sect_size;
423 size -= mydata->sect_size;
425 } else if (size >= mydata->sect_size) {
426 nsects = size / mydata->sect_size;
427 ret = disk_write(startsect, nsects, buffer);
429 debug("Error writing data (got %d)\n", ret);
434 buffer += nsects * mydata->sect_size;
435 size -= nsects * mydata->sect_size;
439 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
440 /* Do not leak content of stack */
441 memset(tmpbuf, 0, mydata->sect_size);
442 memcpy(tmpbuf, buffer, size);
443 ret = disk_write(startsect, 1, tmpbuf);
445 debug("Error writing data (got %d)\n", ret);
454 * set_cluster() - write data to cluster
456 * Write 'size' bytes from 'buffer' into the specified cluster.
458 * @mydata: data to be written
459 * @clustnum: cluster to be written to
460 * @buffer: data to be written
461 * @size: bytes to be written (but not more than the size of a cluster)
462 * Return: 0 on success, -1 otherwise
465 set_cluster(fsdata *mydata, u32 clustnum, u8 *buffer, u32 size)
467 return set_sectors(mydata, clust_to_sect(mydata, clustnum),
472 flush_dir(fat_itr *itr)
474 fsdata *mydata = itr->fsdata;
475 u32 startsect, sect_offset, nsects;
477 if (!itr->is_root || mydata->fatsize == 32)
478 return set_cluster(mydata, itr->clust, itr->block,
479 mydata->clust_size * mydata->sect_size);
481 sect_offset = itr->clust * mydata->clust_size;
482 startsect = mydata->rootdir_sect + sect_offset;
483 /* do not write past the end of rootdir */
484 nsects = min_t(u32, mydata->clust_size,
485 mydata->rootdir_size - sect_offset);
487 return set_sectors(mydata, startsect, itr->block,
488 nsects * mydata->sect_size);
491 static __u8 tmpbuf_cluster[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN);
494 * Read and modify data on existing and consecutive cluster blocks
497 get_set_cluster(fsdata *mydata, __u32 clustnum, loff_t pos, __u8 *buffer,
498 loff_t size, loff_t *gotsize)
500 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
503 int clustcount, i, ret;
509 assert(pos < bytesperclust);
510 startsect = clust_to_sect(mydata, clustnum);
512 debug("clustnum: %d, startsect: %d, pos: %lld\n",
513 clustnum, startsect, pos);
515 /* partial write at beginning */
517 wsize = min(bytesperclust - pos, size);
518 ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster);
519 if (ret != mydata->clust_size) {
520 debug("Error reading data (got %d)\n", ret);
524 memcpy(tmpbuf_cluster + pos, buffer, wsize);
525 ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster);
526 if (ret != mydata->clust_size) {
527 debug("Error writing data (got %d)\n", ret);
535 startsect += mydata->clust_size;
541 /* full-cluster write */
542 if (size >= bytesperclust) {
543 clustcount = lldiv(size, bytesperclust);
545 if (!((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1))) {
546 wsize = clustcount * bytesperclust;
547 ret = disk_write(startsect,
548 clustcount * mydata->clust_size,
550 if (ret != clustcount * mydata->clust_size) {
551 debug("Error writing data (got %d)\n", ret);
559 startsect += clustcount * mydata->clust_size;
561 for (i = 0; i < clustcount; i++) {
562 memcpy(tmpbuf_cluster, buffer, bytesperclust);
563 ret = disk_write(startsect,
566 if (ret != mydata->clust_size) {
567 debug("Error writing data (got %d)\n",
572 size -= bytesperclust;
573 buffer += bytesperclust;
574 *gotsize += bytesperclust;
576 startsect += mydata->clust_size;
581 /* partial write at end */
584 ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster);
585 if (ret != mydata->clust_size) {
586 debug("Error reading data (got %d)\n", ret);
589 memcpy(tmpbuf_cluster, buffer, wsize);
590 ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster);
591 if (ret != mydata->clust_size) {
592 debug("Error writing data (got %d)\n", ret);
607 * Find the first empty cluster
609 static int find_empty_cluster(fsdata *mydata)
611 __u32 fat_val, entry = 3;
614 fat_val = get_fatent(mydata, entry);
624 * Allocate a cluster for additional directory entries
626 static int new_dir_table(fat_itr *itr)
628 fsdata *mydata = itr->fsdata;
629 int dir_newclust = 0;
630 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
632 dir_newclust = find_empty_cluster(mydata);
633 set_fatent_value(mydata, itr->clust, dir_newclust);
634 if (mydata->fatsize == 32)
635 set_fatent_value(mydata, dir_newclust, 0xffffff8);
636 else if (mydata->fatsize == 16)
637 set_fatent_value(mydata, dir_newclust, 0xfff8);
638 else if (mydata->fatsize == 12)
639 set_fatent_value(mydata, dir_newclust, 0xff8);
641 itr->clust = dir_newclust;
642 itr->next_clust = dir_newclust;
644 if (flush_dirty_fat_buffer(mydata) < 0)
647 memset(itr->block, 0x00, bytesperclust);
649 itr->dent = (dir_entry *)itr->block;
650 itr->last_cluster = 1;
651 itr->remaining = bytesperclust / sizeof(dir_entry) - 1;
657 * Set empty cluster from 'entry' to the end of a file
659 static int clear_fatent(fsdata *mydata, __u32 entry)
663 while (!CHECK_CLUST(entry, mydata->fatsize)) {
664 fat_val = get_fatent(mydata, entry);
666 set_fatent_value(mydata, entry, 0);
673 /* Flush fat buffer */
674 if (flush_dirty_fat_buffer(mydata) < 0)
681 * Set start cluster in directory entry
683 static void set_start_cluster(const fsdata *mydata, dir_entry *dentptr,
686 if (mydata->fatsize == 32)
688 cpu_to_le16((start_cluster & 0xffff0000) >> 16);
689 dentptr->start = cpu_to_le16(start_cluster & 0xffff);
693 * Check whether adding a file makes the file system to
694 * exceed the size of the block device
695 * Return -1 when overflow occurs, otherwise return 0
697 static int check_overflow(fsdata *mydata, __u32 clustnum, loff_t size)
699 __u32 startsect, sect_num, offset;
702 startsect = clust_to_sect(mydata, clustnum);
704 startsect = mydata->rootdir_sect;
706 sect_num = div_u64_rem(size, mydata->sect_size, &offset);
711 if (startsect + sect_num > total_sector)
717 * Write at most 'maxsize' bytes from 'buffer' into
718 * the file associated with 'dentptr'
719 * Update the number of bytes written in *gotsize and return 0
720 * or return -1 on fatal errors.
723 set_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, __u8 *buffer,
724 loff_t maxsize, loff_t *gotsize)
726 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
727 __u32 curclust = START(dentptr);
728 __u32 endclust = 0, newclust = 0;
729 u64 cur_pos, filesize;
730 loff_t offset, actsize, wsize;
733 filesize = pos + maxsize;
735 debug("%llu bytes\n", filesize);
740 if (!CHECK_CLUST(curclust, mydata->fatsize) ||
741 IS_LAST_CLUST(curclust, mydata->fatsize)) {
742 clear_fatent(mydata, curclust);
743 set_start_cluster(mydata, dentptr, 0);
746 debug("curclust: 0x%x\n", curclust);
747 debug("Invalid FAT entry\n");
756 /* go to cluster at pos */
757 cur_pos = bytesperclust;
761 if (IS_LAST_CLUST(curclust, mydata->fatsize))
764 newclust = get_fatent(mydata, curclust);
765 if (!IS_LAST_CLUST(newclust, mydata->fatsize) &&
766 CHECK_CLUST(newclust, mydata->fatsize)) {
767 debug("curclust: 0x%x\n", curclust);
768 debug("Invalid FAT entry\n");
772 cur_pos += bytesperclust;
775 if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
776 assert(pos == cur_pos);
780 assert(pos < cur_pos);
781 cur_pos -= bytesperclust;
784 assert(IS_LAST_CLUST(curclust, mydata->fatsize) ||
785 !CHECK_CLUST(curclust, mydata->fatsize));
788 /* search for allocated consecutive clusters */
789 actsize = bytesperclust;
792 if (filesize <= (cur_pos + actsize))
795 newclust = get_fatent(mydata, endclust);
797 if (newclust != endclust + 1)
799 if (IS_LAST_CLUST(newclust, mydata->fatsize))
801 if (CHECK_CLUST(newclust, mydata->fatsize)) {
802 debug("curclust: 0x%x\n", curclust);
803 debug("Invalid FAT entry\n");
807 actsize += bytesperclust;
811 /* overwrite to <curclust..endclust> */
815 offset = pos - cur_pos;
816 wsize = min_t(unsigned long long, actsize, filesize - cur_pos);
819 if (get_set_cluster(mydata, curclust, offset,
820 buffer, wsize, &actsize)) {
821 printf("Error get-and-setting cluster\n");
826 cur_pos += offset + wsize;
828 if (filesize <= cur_pos)
831 if (IS_LAST_CLUST(newclust, mydata->fatsize))
832 /* no more clusters */
838 if (filesize <= cur_pos) {
840 newclust = get_fatent(mydata, endclust);
841 if (!IS_LAST_CLUST(newclust, mydata->fatsize)) {
842 /* truncate the rest */
843 clear_fatent(mydata, newclust);
845 /* Mark end of file in FAT */
846 if (mydata->fatsize == 12)
848 else if (mydata->fatsize == 16)
850 else if (mydata->fatsize == 32)
851 newclust = 0xfffffff;
852 set_fatent_value(mydata, endclust, newclust);
860 assert(!do_div(cur_pos, bytesperclust));
863 /* allocate and write */
866 /* Assure that curclust is valid */
868 curclust = find_empty_cluster(mydata);
869 set_start_cluster(mydata, dentptr, curclust);
871 newclust = get_fatent(mydata, curclust);
873 if (IS_LAST_CLUST(newclust, mydata->fatsize)) {
874 newclust = determine_fatent(mydata, curclust);
875 set_fatent_value(mydata, curclust, newclust);
878 debug("error: something wrong\n");
883 /* TODO: already partially written */
884 if (check_overflow(mydata, curclust, filesize)) {
885 printf("Error: no space left: %llu\n", filesize);
889 actsize = bytesperclust;
892 /* search for consecutive clusters */
893 while (actsize < filesize) {
894 newclust = determine_fatent(mydata, endclust);
896 if ((newclust - 1) != endclust)
897 /* write to <curclust..endclust> */
900 if (CHECK_CLUST(newclust, mydata->fatsize)) {
901 debug("newclust: 0x%x\n", newclust);
902 debug("Invalid FAT entry\n");
906 actsize += bytesperclust;
909 /* set remaining bytes */
911 if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
912 debug("error: writing cluster\n");
917 /* Mark end of file in FAT */
918 if (mydata->fatsize == 12)
920 else if (mydata->fatsize == 16)
922 else if (mydata->fatsize == 32)
923 newclust = 0xfffffff;
924 set_fatent_value(mydata, endclust, newclust);
928 if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
929 debug("error: writing cluster\n");
936 if (CHECK_CLUST(newclust, mydata->fatsize)) {
937 debug("newclust: 0x%x\n", newclust);
938 debug("Invalid FAT entry\n");
941 actsize = bytesperclust;
942 curclust = endclust = newclust;
951 static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
952 const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
954 set_start_cluster(mydata, dentptr, start_cluster);
955 dentptr->size = cpu_to_le32(size);
957 dentptr->attr = attr;
959 set_name(dentptr, filename);
963 * Find a directory entry based on filename or start cluster number
964 * If the directory entry is not found,
965 * the new position for writing a directory entry will be returned
967 static dir_entry *find_directory_entry(fat_itr *itr, char *filename)
971 while (fat_itr_next(itr)) {
972 /* check both long and short name: */
973 if (!strcasecmp(filename, itr->name))
975 else if (itr->name != itr->s_name &&
976 !strcasecmp(filename, itr->s_name))
982 if (itr->dent->name[0] == '\0')
988 /* allocate a cluster for more entries */
990 (!itr->is_root || itr->fsdata->fatsize == 32) &&
992 /* indicate that allocating dent failed */
998 static int split_filename(char *filename, char **dirname, char **basename)
1000 char *p, *last_slash, *last_slash_cont;
1005 last_slash_cont = NULL;
1007 if (ISDIRDELIM(*p)) {
1009 last_slash_cont = p;
1010 /* continuous slashes */
1011 while (ISDIRDELIM(*p))
1012 last_slash_cont = p++;
1020 if (last_slash_cont == (filename + strlen(filename) - 1)) {
1021 /* remove trailing slashes */
1026 if (last_slash == filename) {
1027 /* avoid ""(null) directory */
1031 *dirname = filename;
1034 *last_slash_cont = '\0';
1035 *basename = last_slash_cont + 1;
1037 *dirname = "/"; /* root by default */
1038 *basename = filename;
1045 * normalize_longname() - check long file name and convert to lower case
1047 * We assume here that the FAT file system is using an 8bit code page.
1048 * Linux typically uses CP437, EDK2 assumes CP1250.
1050 * @l_filename: preallocated buffer receiving the normalized name
1051 * @filename: filename to normalize
1052 * Return: 0 on success, -1 on failure
1054 static int normalize_longname(char *l_filename, const char *filename)
1056 const char *p, illegal[] = "<>:\"/\\|?*";
1058 if (strlen(filename) >= VFAT_MAXLEN_BYTES)
1061 for (p = filename; *p; ++p) {
1062 if ((unsigned char)*p < 0x20)
1064 if (strchr(illegal, *p))
1068 strcpy(l_filename, filename);
1069 downcase(l_filename, VFAT_MAXLEN_BYTES);
1074 int file_fat_write_at(const char *filename, loff_t pos, void *buffer,
1075 loff_t size, loff_t *actwrite)
1078 fsdata datablock = { .fatbuf = NULL, };
1079 fsdata *mydata = &datablock;
1080 fat_itr *itr = NULL;
1082 char *filename_copy, *parent, *basename;
1083 char l_filename[VFAT_MAXLEN_BYTES];
1085 debug("writing %s\n", filename);
1087 filename_copy = strdup(filename);
1091 split_filename(filename_copy, &parent, &basename);
1092 if (!strlen(basename)) {
1097 filename = basename;
1098 if (normalize_longname(l_filename, filename)) {
1099 printf("FAT: illegal filename (%s)\n", filename);
1104 itr = malloc_cache_aligned(sizeof(fat_itr));
1110 ret = fat_itr_root(itr, &datablock);
1114 total_sector = datablock.total_sect;
1116 ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1118 printf("%s: doesn't exist (%d)\n", parent, ret);
1122 retdent = find_directory_entry(itr, l_filename);
1125 if (fat_itr_isdir(itr)) {
1132 /* Append to the end */
1133 pos = FAT2CPU32(retdent->size);
1134 if (pos > retdent->size) {
1135 /* No hole allowed */
1140 /* Update file size in a directory entry */
1141 retdent->size = cpu_to_le32(pos + size);
1143 /* Create a new file */
1146 /* root dir cannot have "." or ".." */
1147 if (!strcmp(l_filename, ".") ||
1148 !strcmp(l_filename, "..")) {
1155 printf("Error: allocating new dir entry\n");
1161 /* No hole allowed */
1166 memset(itr->dent, 0, sizeof(*itr->dent));
1168 /* Calculate checksum for short name */
1169 set_name(itr->dent, filename);
1171 /* Set long name entries */
1172 if (fill_dir_slot(itr, filename)) {
1177 /* Set short name entry */
1178 fill_dentry(itr->fsdata, itr->dent, filename, 0, size, 0x20);
1180 retdent = itr->dent;
1183 ret = set_contents(mydata, retdent, pos, buffer, size, actwrite);
1185 printf("Error: writing contents\n");
1189 debug("attempt to write 0x%llx bytes\n", *actwrite);
1191 /* Flush fat buffer */
1192 ret = flush_dirty_fat_buffer(mydata);
1194 printf("Error: flush fat buffer\n");
1199 /* Write directory table to device */
1200 ret = flush_dir(itr);
1202 printf("Error: writing directory entry\n");
1207 free(filename_copy);
1208 free(mydata->fatbuf);
1213 int file_fat_write(const char *filename, void *buffer, loff_t offset,
1214 loff_t maxsize, loff_t *actwrite)
1216 return file_fat_write_at(filename, offset, buffer, maxsize, actwrite);
1219 static int fat_dir_entries(fat_itr *itr)
1222 fsdata fsdata = { .fatbuf = NULL, }, *mydata = &fsdata;
1223 /* for FATBUFSIZE */
1226 dirs = malloc_cache_aligned(sizeof(fat_itr));
1228 debug("Error: allocating memory\n");
1233 /* duplicate fsdata */
1234 fat_itr_child(dirs, itr);
1235 fsdata = *dirs->fsdata;
1237 /* allocate local fat buffer */
1238 fsdata.fatbuf = malloc_cache_aligned(FATBUFSIZE);
1239 if (!fsdata.fatbuf) {
1240 debug("Error: allocating memory\n");
1244 fsdata.fatbufnum = -1;
1245 dirs->fsdata = &fsdata;
1247 for (count = 0; fat_itr_next(dirs); count++)
1251 free(fsdata.fatbuf);
1256 static int delete_dentry(fat_itr *itr)
1258 fsdata *mydata = itr->fsdata;
1259 dir_entry *dentptr = itr->dent;
1261 /* free cluster blocks */
1262 clear_fatent(mydata, START(dentptr));
1263 if (flush_dirty_fat_buffer(mydata) < 0) {
1264 printf("Error: flush fat buffer\n");
1269 * update a directory entry
1271 * - long file name support
1272 * - find and mark the "new" first invalid entry as name[0]=0x00
1274 memset(dentptr, 0, sizeof(*dentptr));
1275 dentptr->name[0] = 0xe5;
1277 if (flush_dir(itr)) {
1278 printf("error: writing directory entry\n");
1285 int fat_unlink(const char *filename)
1287 fsdata fsdata = { .fatbuf = NULL, };
1288 fat_itr *itr = NULL;
1290 char *filename_copy, *dirname, *basename;
1292 filename_copy = strdup(filename);
1293 if (!filename_copy) {
1294 printf("Error: allocating memory\n");
1298 split_filename(filename_copy, &dirname, &basename);
1300 if (!strcmp(dirname, "/") && !strcmp(basename, "")) {
1301 printf("Error: cannot remove root\n");
1306 itr = malloc_cache_aligned(sizeof(fat_itr));
1308 printf("Error: allocating memory\n");
1313 ret = fat_itr_root(itr, &fsdata);
1317 total_sector = fsdata.total_sect;
1319 ret = fat_itr_resolve(itr, dirname, TYPE_DIR);
1321 printf("%s: doesn't exist (%d)\n", dirname, ret);
1326 if (!find_directory_entry(itr, basename)) {
1327 printf("%s: doesn't exist\n", basename);
1332 if (fat_itr_isdir(itr)) {
1333 n_entries = fat_dir_entries(itr);
1334 if (n_entries < 0) {
1338 if (n_entries > 2) {
1339 printf("Error: directory is not empty: %d\n",
1346 ret = delete_dentry(itr);
1349 free(fsdata.fatbuf);
1351 free(filename_copy);
1356 int fat_mkdir(const char *new_dirname)
1359 fsdata datablock = { .fatbuf = NULL, };
1360 fsdata *mydata = &datablock;
1361 fat_itr *itr = NULL;
1362 char *dirname_copy, *parent, *dirname;
1363 char l_dirname[VFAT_MAXLEN_BYTES];
1366 unsigned int bytesperclust;
1367 dir_entry *dotdent = NULL;
1369 dirname_copy = strdup(new_dirname);
1373 split_filename(dirname_copy, &parent, &dirname);
1374 if (!strlen(dirname)) {
1379 if (normalize_longname(l_dirname, dirname)) {
1380 printf("FAT: illegal filename (%s)\n", dirname);
1385 itr = malloc_cache_aligned(sizeof(fat_itr));
1391 ret = fat_itr_root(itr, &datablock);
1395 total_sector = datablock.total_sect;
1397 ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1399 printf("%s: doesn't exist (%d)\n", parent, ret);
1403 retdent = find_directory_entry(itr, l_dirname);
1406 printf("%s: already exists\n", l_dirname);
1411 /* root dir cannot have "." or ".." */
1412 if (!strcmp(l_dirname, ".") ||
1413 !strcmp(l_dirname, "..")) {
1420 printf("Error: allocating new dir entry\n");
1425 memset(itr->dent, 0, sizeof(*itr->dent));
1427 /* Set short name to set alias checksum field in dir_slot */
1428 set_name(itr->dent, dirname);
1429 fill_dir_slot(itr, dirname);
1431 /* Set attribute as archive for regular file */
1432 fill_dentry(itr->fsdata, itr->dent, dirname, 0, 0,
1433 ATTR_DIR | ATTR_ARCH);
1435 retdent = itr->dent;
1438 /* Default entries */
1439 bytesperclust = mydata->clust_size * mydata->sect_size;
1440 dotdent = malloc_cache_aligned(bytesperclust);
1445 memset(dotdent, 0, bytesperclust);
1447 memcpy(dotdent[0].name, ". ", 8);
1448 memcpy(dotdent[0].ext, " ", 3);
1449 dotdent[0].attr = ATTR_DIR | ATTR_ARCH;
1451 memcpy(dotdent[1].name, ".. ", 8);
1452 memcpy(dotdent[1].ext, " ", 3);
1453 dotdent[1].attr = ATTR_DIR | ATTR_ARCH;
1454 set_start_cluster(mydata, &dotdent[1], itr->start_clust);
1456 ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
1457 bytesperclust, &actwrite);
1459 printf("Error: writing contents\n");
1462 /* Write twice for "." */
1463 set_start_cluster(mydata, &dotdent[0], START(retdent));
1464 ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
1465 bytesperclust, &actwrite);
1467 printf("Error: writing contents\n");
1471 /* Flush fat buffer */
1472 ret = flush_dirty_fat_buffer(mydata);
1474 printf("Error: flush fat buffer\n");
1478 /* Write directory table to device */
1479 ret = flush_dir(itr);
1481 printf("Error: writing directory entry\n");
1485 free(mydata->fatbuf);