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 flush_dir_table(fat_itr *itr);
215 * Fill dir_slot entries with appropriate name, id, and attr
216 * 'itr' will point to a next entry
219 fill_dir_slot(fat_itr *itr, const char *l_name)
221 __u8 temp_dir_slot_buffer[MAX_LFN_SLOT * sizeof(dir_slot)];
222 dir_slot *slotptr = (dir_slot *)temp_dir_slot_buffer;
223 __u8 counter = 0, checksum;
226 /* Get short file name checksum value */
227 checksum = mkcksum(itr->dent->name, itr->dent->ext);
230 memset(slotptr, 0x00, sizeof(dir_slot));
231 ret = str2slot(slotptr, l_name, &idx);
232 slotptr->id = ++counter;
233 slotptr->attr = ATTR_VFAT;
234 slotptr->alias_checksum = checksum;
239 slotptr->id |= LAST_LONG_ENTRY_MASK;
241 while (counter >= 1) {
242 memcpy(itr->dent, slotptr, sizeof(dir_slot));
245 if (!fat_itr_next(itr))
246 if (!itr->dent && !itr->is_root && flush_dir_table(itr))
250 if (!itr->dent && !itr->is_root)
252 * don't care return value here because we have already
253 * finished completing an entry with name, only ending up
256 flush_dir_table(itr);
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 * Write at most 'size' bytes from 'buffer' into the specified cluster.
392 * Return 0 on success, -1 otherwise.
395 set_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer,
403 startsect = clust_to_sect(mydata, clustnum);
405 startsect = mydata->rootdir_sect;
407 debug("clustnum: %d, startsect: %d\n", clustnum, 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 idx = size / mydata->sect_size;
427 ret = disk_write(startsect, idx, buffer);
429 debug("Error writing data (got %d)\n", ret);
434 idx *= mydata->sect_size;
440 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
442 memcpy(tmpbuf, buffer, size);
443 ret = disk_write(startsect, 1, tmpbuf);
445 debug("Error writing data (got %d)\n", ret);
453 static __u8 tmpbuf_cluster[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN);
456 * Read and modify data on existing and consecutive cluster blocks
459 get_set_cluster(fsdata *mydata, __u32 clustnum, loff_t pos, __u8 *buffer,
460 loff_t size, loff_t *gotsize)
462 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
465 int clustcount, i, ret;
471 assert(pos < bytesperclust);
472 startsect = clust_to_sect(mydata, clustnum);
474 debug("clustnum: %d, startsect: %d, pos: %lld\n",
475 clustnum, startsect, pos);
477 /* partial write at beginning */
479 wsize = min(bytesperclust - pos, size);
480 ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster);
481 if (ret != mydata->clust_size) {
482 debug("Error reading data (got %d)\n", ret);
486 memcpy(tmpbuf_cluster + pos, buffer, wsize);
487 ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster);
488 if (ret != mydata->clust_size) {
489 debug("Error writing data (got %d)\n", ret);
497 startsect += mydata->clust_size;
503 /* full-cluster write */
504 if (size >= bytesperclust) {
505 clustcount = lldiv(size, bytesperclust);
507 if (!((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1))) {
508 wsize = clustcount * bytesperclust;
509 ret = disk_write(startsect,
510 clustcount * mydata->clust_size,
512 if (ret != clustcount * mydata->clust_size) {
513 debug("Error writing data (got %d)\n", ret);
521 startsect += clustcount * mydata->clust_size;
523 for (i = 0; i < clustcount; i++) {
524 memcpy(tmpbuf_cluster, buffer, bytesperclust);
525 ret = disk_write(startsect,
528 if (ret != mydata->clust_size) {
529 debug("Error writing data (got %d)\n",
534 size -= bytesperclust;
535 buffer += bytesperclust;
536 *gotsize += bytesperclust;
538 startsect += mydata->clust_size;
543 /* partial write at end */
546 ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster);
547 if (ret != mydata->clust_size) {
548 debug("Error reading data (got %d)\n", ret);
551 memcpy(tmpbuf_cluster, buffer, wsize);
552 ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster);
553 if (ret != mydata->clust_size) {
554 debug("Error writing data (got %d)\n", ret);
569 * Find the first empty cluster
571 static int find_empty_cluster(fsdata *mydata)
573 __u32 fat_val, entry = 3;
576 fat_val = get_fatent(mydata, entry);
586 * Write directory entries in itr's buffer to block device
588 static int flush_dir_table(fat_itr *itr)
590 fsdata *mydata = itr->fsdata;
591 int dir_newclust = 0;
592 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
594 if (set_cluster(mydata, itr->clust, itr->block, bytesperclust) != 0) {
595 printf("error: writing directory entry\n");
598 dir_newclust = find_empty_cluster(mydata);
599 set_fatent_value(mydata, itr->clust, dir_newclust);
600 if (mydata->fatsize == 32)
601 set_fatent_value(mydata, dir_newclust, 0xffffff8);
602 else if (mydata->fatsize == 16)
603 set_fatent_value(mydata, dir_newclust, 0xfff8);
604 else if (mydata->fatsize == 12)
605 set_fatent_value(mydata, dir_newclust, 0xff8);
607 itr->clust = dir_newclust;
608 itr->next_clust = dir_newclust;
610 if (flush_dirty_fat_buffer(mydata) < 0)
613 memset(itr->block, 0x00, bytesperclust);
615 itr->dent = (dir_entry *)itr->block;
616 itr->last_cluster = 1;
617 itr->remaining = bytesperclust / sizeof(dir_entry) - 1;
623 * Set empty cluster from 'entry' to the end of a file
625 static int clear_fatent(fsdata *mydata, __u32 entry)
629 while (!CHECK_CLUST(entry, mydata->fatsize)) {
630 fat_val = get_fatent(mydata, entry);
632 set_fatent_value(mydata, entry, 0);
639 /* Flush fat buffer */
640 if (flush_dirty_fat_buffer(mydata) < 0)
647 * Set start cluster in directory entry
649 static void set_start_cluster(const fsdata *mydata, dir_entry *dentptr,
652 if (mydata->fatsize == 32)
654 cpu_to_le16((start_cluster & 0xffff0000) >> 16);
655 dentptr->start = cpu_to_le16(start_cluster & 0xffff);
659 * Check whether adding a file makes the file system to
660 * exceed the size of the block device
661 * Return -1 when overflow occurs, otherwise return 0
663 static int check_overflow(fsdata *mydata, __u32 clustnum, loff_t size)
665 __u32 startsect, sect_num, offset;
668 startsect = clust_to_sect(mydata, clustnum);
670 startsect = mydata->rootdir_sect;
672 sect_num = div_u64_rem(size, mydata->sect_size, &offset);
677 if (startsect + sect_num > total_sector)
683 * Write at most 'maxsize' bytes from 'buffer' into
684 * the file associated with 'dentptr'
685 * Update the number of bytes written in *gotsize and return 0
686 * or return -1 on fatal errors.
689 set_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, __u8 *buffer,
690 loff_t maxsize, loff_t *gotsize)
693 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
694 __u32 curclust = START(dentptr);
695 __u32 endclust = 0, newclust = 0;
696 loff_t cur_pos, offset, actsize, wsize;
699 filesize = pos + maxsize;
701 debug("%llu bytes\n", filesize);
706 if (!CHECK_CLUST(curclust, mydata->fatsize) ||
707 IS_LAST_CLUST(curclust, mydata->fatsize)) {
708 clear_fatent(mydata, curclust);
709 set_start_cluster(mydata, dentptr, 0);
712 debug("curclust: 0x%x\n", curclust);
713 debug("Invalid FAT entry\n");
722 /* go to cluster at pos */
723 cur_pos = bytesperclust;
727 if (IS_LAST_CLUST(curclust, mydata->fatsize))
730 newclust = get_fatent(mydata, curclust);
731 if (!IS_LAST_CLUST(newclust, mydata->fatsize) &&
732 CHECK_CLUST(newclust, mydata->fatsize)) {
733 debug("curclust: 0x%x\n", curclust);
734 debug("Invalid FAT entry\n");
738 cur_pos += bytesperclust;
741 if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
742 assert(pos == cur_pos);
746 assert(pos < cur_pos);
747 cur_pos -= bytesperclust;
750 assert(IS_LAST_CLUST(curclust, mydata->fatsize) ||
751 !CHECK_CLUST(curclust, mydata->fatsize));
754 /* search for allocated consecutive clusters */
755 actsize = bytesperclust;
758 if (filesize <= (cur_pos + actsize))
761 newclust = get_fatent(mydata, endclust);
763 if (IS_LAST_CLUST(newclust, mydata->fatsize))
765 if (CHECK_CLUST(newclust, mydata->fatsize)) {
766 debug("curclust: 0x%x\n", curclust);
767 debug("Invalid FAT entry\n");
771 actsize += bytesperclust;
775 /* overwrite to <curclust..endclust> */
779 offset = pos - cur_pos;
780 wsize = min(cur_pos + actsize, filesize) - pos;
781 if (get_set_cluster(mydata, curclust, offset,
782 buffer, wsize, &actsize)) {
783 printf("Error get-and-setting cluster\n");
788 cur_pos += offset + wsize;
790 if (filesize <= cur_pos)
793 /* CHECK: newclust = get_fatent(mydata, endclust); */
795 if (IS_LAST_CLUST(newclust, mydata->fatsize))
796 /* no more clusters */
802 if (filesize <= cur_pos) {
804 newclust = get_fatent(mydata, endclust);
805 if (!IS_LAST_CLUST(newclust, mydata->fatsize)) {
806 /* truncate the rest */
807 clear_fatent(mydata, newclust);
809 /* Mark end of file in FAT */
810 if (mydata->fatsize == 12)
812 else if (mydata->fatsize == 16)
814 else if (mydata->fatsize == 32)
815 newclust = 0xfffffff;
816 set_fatent_value(mydata, endclust, newclust);
824 assert(!(cur_pos % bytesperclust));
827 /* allocate and write */
830 /* Assure that curclust is valid */
832 curclust = find_empty_cluster(mydata);
833 set_start_cluster(mydata, dentptr, curclust);
835 newclust = get_fatent(mydata, curclust);
837 if (IS_LAST_CLUST(newclust, mydata->fatsize)) {
838 newclust = determine_fatent(mydata, curclust);
839 set_fatent_value(mydata, curclust, newclust);
842 debug("error: something wrong\n");
847 /* TODO: already partially written */
848 if (check_overflow(mydata, curclust, filesize)) {
849 printf("Error: no space left: %llu\n", filesize);
853 actsize = bytesperclust;
856 /* search for consecutive clusters */
857 while (actsize < filesize) {
858 newclust = determine_fatent(mydata, endclust);
860 if ((newclust - 1) != endclust)
861 /* write to <curclust..endclust> */
864 if (CHECK_CLUST(newclust, mydata->fatsize)) {
865 debug("newclust: 0x%x\n", newclust);
866 debug("Invalid FAT entry\n");
870 actsize += bytesperclust;
873 /* set remaining bytes */
875 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
876 debug("error: writing cluster\n");
881 /* Mark end of file in FAT */
882 if (mydata->fatsize == 12)
884 else if (mydata->fatsize == 16)
886 else if (mydata->fatsize == 32)
887 newclust = 0xfffffff;
888 set_fatent_value(mydata, endclust, newclust);
892 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
893 debug("error: writing cluster\n");
900 if (CHECK_CLUST(newclust, mydata->fatsize)) {
901 debug("newclust: 0x%x\n", newclust);
902 debug("Invalid FAT entry\n");
905 actsize = bytesperclust;
906 curclust = endclust = newclust;
915 static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
916 const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
918 set_start_cluster(mydata, dentptr, start_cluster);
919 dentptr->size = cpu_to_le32(size);
921 dentptr->attr = attr;
923 set_name(dentptr, filename);
927 * Find a directory entry based on filename or start cluster number
928 * If the directory entry is not found,
929 * the new position for writing a directory entry will be returned
931 static dir_entry *find_directory_entry(fat_itr *itr, char *filename)
935 while (fat_itr_next(itr)) {
936 /* check both long and short name: */
937 if (!strcasecmp(filename, itr->name))
939 else if (itr->name != itr->s_name &&
940 !strcasecmp(filename, itr->s_name))
946 if (itr->dent->name[0] == '\0')
952 if (!itr->dent && !itr->is_root && flush_dir_table(itr))
953 /* indicate that allocating dent failed */
959 static int split_filename(char *filename, char **dirname, char **basename)
961 char *p, *last_slash, *last_slash_cont;
966 last_slash_cont = NULL;
968 if (ISDIRDELIM(*p)) {
971 /* continuous slashes */
972 while (ISDIRDELIM(*p))
973 last_slash_cont = p++;
981 if (last_slash_cont == (filename + strlen(filename) - 1)) {
982 /* remove trailing slashes */
987 if (last_slash == filename) {
988 /* avoid ""(null) directory */
995 *last_slash_cont = '\0';
996 *basename = last_slash_cont + 1;
998 *dirname = "/"; /* root by default */
999 *basename = filename;
1005 static int normalize_longname(char *l_filename, const char *filename)
1007 const char *p, legal[] = "!#$%&\'()-.@^`_{}~";
1011 /* Check that the filename is valid */
1012 for (p = filename; p < filename + strlen(filename); p++) {
1015 if (('0' <= c) && (c <= '9'))
1017 if (('A' <= c) && (c <= 'Z'))
1019 if (('a' <= c) && (c <= 'z'))
1021 if (strchr(legal, c))
1024 if ((0x80 <= c) && (c <= 0xff))
1031 name_len = strlen(filename);
1032 if (name_len >= VFAT_MAXLEN_BYTES)
1033 /* should return an error? */
1034 name_len = VFAT_MAXLEN_BYTES - 1;
1036 memcpy(l_filename, filename, name_len);
1037 l_filename[name_len] = 0; /* terminate the string */
1038 downcase(l_filename, INT_MAX);
1043 int file_fat_write_at(const char *filename, loff_t pos, void *buffer,
1044 loff_t size, loff_t *actwrite)
1047 fsdata datablock = { .fatbuf = NULL, };
1048 fsdata *mydata = &datablock;
1049 fat_itr *itr = NULL;
1051 char *filename_copy, *parent, *basename;
1052 char l_filename[VFAT_MAXLEN_BYTES];
1054 debug("writing %s\n", filename);
1056 filename_copy = strdup(filename);
1060 split_filename(filename_copy, &parent, &basename);
1061 if (!strlen(basename)) {
1066 filename = basename;
1067 if (normalize_longname(l_filename, filename)) {
1068 printf("FAT: illegal filename (%s)\n", filename);
1073 itr = malloc_cache_aligned(sizeof(fat_itr));
1079 ret = fat_itr_root(itr, &datablock);
1083 total_sector = datablock.total_sect;
1085 ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1087 printf("%s: doesn't exist (%d)\n", parent, ret);
1091 retdent = find_directory_entry(itr, l_filename);
1094 if (fat_itr_isdir(itr)) {
1101 /* Append to the end */
1102 pos = FAT2CPU32(retdent->size);
1103 if (pos > retdent->size) {
1104 /* No hole allowed */
1109 /* Update file size in a directory entry */
1110 retdent->size = cpu_to_le32(pos + size);
1112 /* Create a new file */
1115 /* root dir cannot have "." or ".." */
1116 if (!strcmp(l_filename, ".") ||
1117 !strcmp(l_filename, "..")) {
1124 printf("Error: allocating new dir entry\n");
1130 /* No hole allowed */
1135 memset(itr->dent, 0, sizeof(*itr->dent));
1137 /* Set short name to set alias checksum field in dir_slot */
1138 set_name(itr->dent, filename);
1139 if (fill_dir_slot(itr, filename)) {
1144 /* Set attribute as archive for regular file */
1145 fill_dentry(itr->fsdata, itr->dent, filename, 0, size, 0x20);
1147 retdent = itr->dent;
1150 ret = set_contents(mydata, retdent, pos, buffer, size, actwrite);
1152 printf("Error: writing contents\n");
1156 debug("attempt to write 0x%llx bytes\n", *actwrite);
1158 /* Flush fat buffer */
1159 ret = flush_dirty_fat_buffer(mydata);
1161 printf("Error: flush fat buffer\n");
1166 /* Write directory table to device */
1167 ret = set_cluster(mydata, itr->clust, itr->block,
1168 mydata->clust_size * mydata->sect_size);
1170 printf("Error: writing directory entry\n");
1175 free(filename_copy);
1176 free(mydata->fatbuf);
1181 int file_fat_write(const char *filename, void *buffer, loff_t offset,
1182 loff_t maxsize, loff_t *actwrite)
1184 return file_fat_write_at(filename, offset, buffer, maxsize, actwrite);
1187 static int fat_dir_entries(fat_itr *itr)
1190 fsdata fsdata = { .fatbuf = NULL, }, *mydata = &fsdata;
1191 /* for FATBUFSIZE */
1194 dirs = malloc_cache_aligned(sizeof(fat_itr));
1196 debug("Error: allocating memory\n");
1201 /* duplicate fsdata */
1202 fat_itr_child(dirs, itr);
1203 fsdata = *dirs->fsdata;
1205 /* allocate local fat buffer */
1206 fsdata.fatbuf = malloc_cache_aligned(FATBUFSIZE);
1207 if (!fsdata.fatbuf) {
1208 debug("Error: allocating memory\n");
1212 fsdata.fatbufnum = -1;
1213 dirs->fsdata = &fsdata;
1215 for (count = 0; fat_itr_next(dirs); count++)
1219 free(fsdata.fatbuf);
1224 static int delete_dentry(fat_itr *itr)
1226 fsdata *mydata = itr->fsdata;
1227 dir_entry *dentptr = itr->dent;
1229 /* free cluster blocks */
1230 clear_fatent(mydata, START(dentptr));
1231 if (flush_dirty_fat_buffer(mydata) < 0) {
1232 printf("Error: flush fat buffer\n");
1237 * update a directory entry
1239 * - long file name support
1240 * - find and mark the "new" first invalid entry as name[0]=0x00
1242 memset(dentptr, 0, sizeof(*dentptr));
1243 dentptr->name[0] = 0xe5;
1245 if (set_cluster(mydata, itr->clust, itr->block,
1246 mydata->clust_size * mydata->sect_size) != 0) {
1247 printf("error: writing directory entry\n");
1254 int fat_unlink(const char *filename)
1256 fsdata fsdata = { .fatbuf = NULL, };
1257 fat_itr *itr = NULL;
1259 char *filename_copy, *dirname, *basename;
1261 filename_copy = strdup(filename);
1262 split_filename(filename_copy, &dirname, &basename);
1264 if (!strcmp(dirname, "/") && !strcmp(basename, "")) {
1265 printf("Error: cannot remove root\n");
1270 itr = malloc_cache_aligned(sizeof(fat_itr));
1272 printf("Error: allocating memory\n");
1276 ret = fat_itr_root(itr, &fsdata);
1280 total_sector = fsdata.total_sect;
1282 ret = fat_itr_resolve(itr, dirname, TYPE_DIR);
1284 printf("%s: doesn't exist (%d)\n", dirname, ret);
1289 if (!find_directory_entry(itr, basename)) {
1290 printf("%s: doesn't exist\n", basename);
1295 if (fat_itr_isdir(itr)) {
1296 n_entries = fat_dir_entries(itr);
1297 if (n_entries < 0) {
1301 if (n_entries > 2) {
1302 printf("Error: directory is not empty: %d\n",
1309 ret = delete_dentry(itr);
1312 free(fsdata.fatbuf);
1314 free(filename_copy);
1319 int fat_mkdir(const char *new_dirname)
1322 fsdata datablock = { .fatbuf = NULL, };
1323 fsdata *mydata = &datablock;
1324 fat_itr *itr = NULL;
1325 char *dirname_copy, *parent, *dirname;
1326 char l_dirname[VFAT_MAXLEN_BYTES];
1329 unsigned int bytesperclust;
1330 dir_entry *dotdent = NULL;
1332 dirname_copy = strdup(new_dirname);
1336 split_filename(dirname_copy, &parent, &dirname);
1337 if (!strlen(dirname)) {
1342 if (normalize_longname(l_dirname, dirname)) {
1343 printf("FAT: illegal filename (%s)\n", dirname);
1348 itr = malloc_cache_aligned(sizeof(fat_itr));
1354 ret = fat_itr_root(itr, &datablock);
1358 total_sector = datablock.total_sect;
1360 ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1362 printf("%s: doesn't exist (%d)\n", parent, ret);
1366 retdent = find_directory_entry(itr, l_dirname);
1369 printf("%s: already exists\n", l_dirname);
1374 /* root dir cannot have "." or ".." */
1375 if (!strcmp(l_dirname, ".") ||
1376 !strcmp(l_dirname, "..")) {
1383 printf("Error: allocating new dir entry\n");
1388 memset(itr->dent, 0, sizeof(*itr->dent));
1390 /* Set short name to set alias checksum field in dir_slot */
1391 set_name(itr->dent, dirname);
1392 fill_dir_slot(itr, dirname);
1394 /* Set attribute as archive for regular file */
1395 fill_dentry(itr->fsdata, itr->dent, dirname, 0, 0,
1396 ATTR_DIR | ATTR_ARCH);
1398 retdent = itr->dent;
1401 /* Default entries */
1402 bytesperclust = mydata->clust_size * mydata->sect_size;
1403 dotdent = malloc_cache_aligned(bytesperclust);
1408 memset(dotdent, 0, bytesperclust);
1410 memcpy(dotdent[0].name, ". ", 8);
1411 memcpy(dotdent[0].ext, " ", 3);
1412 dotdent[0].attr = ATTR_DIR | ATTR_ARCH;
1414 memcpy(dotdent[1].name, ".. ", 8);
1415 memcpy(dotdent[1].ext, " ", 3);
1416 dotdent[1].attr = ATTR_DIR | ATTR_ARCH;
1417 set_start_cluster(mydata, &dotdent[1], itr->start_clust);
1419 ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
1420 bytesperclust, &actwrite);
1422 printf("Error: writing contents\n");
1425 /* Write twice for "." */
1426 set_start_cluster(mydata, &dotdent[0], START(retdent));
1427 ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
1428 bytesperclust, &actwrite);
1430 printf("Error: writing contents\n");
1434 /* Flush fat buffer */
1435 ret = flush_dirty_fat_buffer(mydata);
1437 printf("Error: flush fat buffer\n");
1441 /* Write directory table to device */
1442 ret = set_cluster(mydata, itr->clust, itr->block,
1443 mydata->clust_size * mydata->sect_size);
1445 printf("Error: writing directory entry\n");
1449 free(mydata->fatbuf);