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_name() - set short name in directory entry
56 * @dirent: directory entry
57 * @filename: long file name
59 static void set_name(dir_entry *dirent, const char *filename)
61 char s_name[VFAT_MAXLEN_BYTES];
63 int period_location, len, i, ext_num;
68 len = strlen(filename);
72 strncpy(s_name, filename, VFAT_MAXLEN_BYTES - 1);
73 s_name[VFAT_MAXLEN_BYTES - 1] = '\0';
74 uppercase(s_name, len);
76 period = strchr(s_name, '.');
78 period_location = len;
81 period_location = period - s_name;
82 ext_num = len - period_location - 1;
85 /* Pad spaces when the length of file name is shorter than eight */
86 if (period_location < 8) {
87 memcpy(dirent->name, s_name, period_location);
88 for (i = period_location; i < 8; i++)
89 dirent->name[i] = ' ';
90 } else if (period_location == 8) {
91 memcpy(dirent->name, s_name, period_location);
93 memcpy(dirent->name, s_name, 6);
95 * TODO: Translating two long names with the same first six
96 * characters to the same short name is utterly wrong.
97 * Short names must be unique.
99 dirent->name[6] = '~';
100 dirent->name[7] = '1';
104 memcpy(dirent->ext, s_name + period_location + 1, ext_num);
105 for (i = ext_num; i < 3; i++)
106 dirent->ext[i] = ' ';
108 memcpy(dirent->ext, s_name + period_location + 1, 3);
110 debug("name : %s\n", dirent->name);
111 debug("ext : %s\n", dirent->ext);
115 * Write fat buffer into block device
117 static int flush_dirty_fat_buffer(fsdata *mydata)
119 int getsize = FATBUFBLOCKS;
120 __u32 fatlength = mydata->fatlength;
121 __u8 *bufptr = mydata->fatbuf;
122 __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
124 debug("debug: evicting %d, dirty: %d\n", mydata->fatbufnum,
125 (int)mydata->fat_dirty);
127 if ((!mydata->fat_dirty) || (mydata->fatbufnum == -1))
130 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
131 if (startblock + getsize > fatlength)
132 getsize = fatlength - startblock;
134 startblock += mydata->fat_sect;
137 if (disk_write(startblock, getsize, bufptr) < 0) {
138 debug("error: writing FAT blocks\n");
142 if (mydata->fats == 2) {
143 /* Update corresponding second FAT blocks */
144 startblock += mydata->fatlength;
145 if (disk_write(startblock, getsize, bufptr) < 0) {
146 debug("error: writing second FAT blocks\n");
150 mydata->fat_dirty = 0;
156 * Set the file name information from 'name' into 'slotptr',
158 static int str2slot(dir_slot *slotptr, const char *name, int *idx)
162 for (j = 0; j <= 8; j += 2) {
163 if (name[*idx] == 0x00) {
164 slotptr->name0_4[j] = 0;
165 slotptr->name0_4[j + 1] = 0;
169 slotptr->name0_4[j] = name[*idx];
173 for (j = 0; j <= 10; j += 2) {
174 if (name[*idx] == 0x00) {
175 slotptr->name5_10[j] = 0;
176 slotptr->name5_10[j + 1] = 0;
180 slotptr->name5_10[j] = name[*idx];
184 for (j = 0; j <= 2; j += 2) {
185 if (name[*idx] == 0x00) {
186 slotptr->name11_12[j] = 0;
187 slotptr->name11_12[j + 1] = 0;
191 slotptr->name11_12[j] = name[*idx];
196 if (name[*idx] == 0x00)
200 /* Not used characters are filled with 0xff 0xff */
202 for (; end_idx < 5; end_idx++) {
203 slotptr->name0_4[end_idx * 2] = 0xff;
204 slotptr->name0_4[end_idx * 2 + 1] = 0xff;
209 for (; end_idx < 6; end_idx++) {
210 slotptr->name5_10[end_idx * 2] = 0xff;
211 slotptr->name5_10[end_idx * 2 + 1] = 0xff;
216 for (; end_idx < 2; end_idx++) {
217 slotptr->name11_12[end_idx * 2] = 0xff;
218 slotptr->name11_12[end_idx * 2 + 1] = 0xff;
224 static int new_dir_table(fat_itr *itr);
225 static int flush_dir(fat_itr *itr);
228 * Fill dir_slot entries with appropriate name, id, and attr
229 * 'itr' will point to a next entry
232 fill_dir_slot(fat_itr *itr, const char *l_name)
234 __u8 temp_dir_slot_buffer[MAX_LFN_SLOT * sizeof(dir_slot)];
235 dir_slot *slotptr = (dir_slot *)temp_dir_slot_buffer;
236 __u8 counter = 0, checksum;
239 /* Get short file name checksum value */
240 checksum = mkcksum(itr->dent->name, itr->dent->ext);
243 memset(slotptr, 0x00, sizeof(dir_slot));
244 ret = str2slot(slotptr, l_name, &idx);
245 slotptr->id = ++counter;
246 slotptr->attr = ATTR_VFAT;
247 slotptr->alias_checksum = checksum;
252 slotptr->id |= LAST_LONG_ENTRY_MASK;
254 while (counter >= 1) {
255 memcpy(itr->dent, slotptr, sizeof(dir_slot));
259 if (itr->remaining == 0)
262 /* allocate a cluster for more entries */
263 if (!fat_itr_next(itr) && !itr->dent)
264 if ((itr->is_root && itr->fsdata->fatsize != 32) ||
273 * Set the entry at index 'entry' in a FAT (12/16/32) table.
275 static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
277 __u32 bufnum, offset, off16;
280 switch (mydata->fatsize) {
282 bufnum = entry / FAT32BUFSIZE;
283 offset = entry - bufnum * FAT32BUFSIZE;
286 bufnum = entry / FAT16BUFSIZE;
287 offset = entry - bufnum * FAT16BUFSIZE;
290 bufnum = entry / FAT12BUFSIZE;
291 offset = entry - bufnum * FAT12BUFSIZE;
294 /* Unsupported FAT size */
298 /* Read a new block of FAT entries into the cache. */
299 if (bufnum != mydata->fatbufnum) {
300 int getsize = FATBUFBLOCKS;
301 __u8 *bufptr = mydata->fatbuf;
302 __u32 fatlength = mydata->fatlength;
303 __u32 startblock = bufnum * FATBUFBLOCKS;
305 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
306 if (startblock + getsize > fatlength)
307 getsize = fatlength - startblock;
309 if (flush_dirty_fat_buffer(mydata) < 0)
312 startblock += mydata->fat_sect;
314 if (disk_read(startblock, getsize, bufptr) < 0) {
315 debug("Error reading FAT blocks\n");
318 mydata->fatbufnum = bufnum;
322 mydata->fat_dirty = 1;
324 /* Set the actual entry */
325 switch (mydata->fatsize) {
327 ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
330 ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
333 off16 = (offset * 3) / 4;
335 switch (offset & 0x3) {
337 val1 = cpu_to_le16(entry_value) & 0xfff;
338 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff;
339 ((__u16 *)mydata->fatbuf)[off16] |= val1;
342 val1 = cpu_to_le16(entry_value) & 0xf;
343 val2 = (cpu_to_le16(entry_value) >> 4) & 0xff;
345 ((__u16 *)mydata->fatbuf)[off16] &= ~0xf000;
346 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 12);
348 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xff;
349 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
352 val1 = cpu_to_le16(entry_value) & 0xff;
353 val2 = (cpu_to_le16(entry_value) >> 8) & 0xf;
355 ((__u16 *)mydata->fatbuf)[off16] &= ~0xff00;
356 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 8);
358 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xf;
359 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
362 val1 = cpu_to_le16(entry_value) & 0xfff;
363 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff0;
364 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 4);
379 * Determine the next free cluster after 'entry' in a FAT (12/16/32) table
380 * and link it to 'entry'. EOC marker is not set on returned entry.
382 static __u32 determine_fatent(fsdata *mydata, __u32 entry)
384 __u32 next_fat, next_entry = entry + 1;
387 next_fat = get_fatent(mydata, next_entry);
389 /* found free entry, link to entry */
390 set_fatent_value(mydata, entry, next_entry);
395 debug("FAT%d: entry: %08x, entry_value: %04x\n",
396 mydata->fatsize, entry, next_entry);
402 * set_sectors() - write data to sectors
404 * Write 'size' bytes from 'buffer' into the specified sector.
406 * @mydata: data to be written
407 * @startsect: sector to be written to
408 * @buffer: data to be written
409 * @size: bytes to be written (but not more than the size of a cluster)
410 * Return: 0 on success, -1 otherwise
413 set_sectors(fsdata *mydata, u32 startsect, u8 *buffer, u32 size)
418 debug("startsect: %d\n", startsect);
420 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
421 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
423 debug("FAT: Misaligned buffer address (%p)\n", buffer);
425 while (size >= mydata->sect_size) {
426 memcpy(tmpbuf, buffer, mydata->sect_size);
427 ret = disk_write(startsect++, 1, tmpbuf);
429 debug("Error writing data (got %d)\n", ret);
433 buffer += mydata->sect_size;
434 size -= mydata->sect_size;
436 } else if (size >= mydata->sect_size) {
437 nsects = size / mydata->sect_size;
438 ret = disk_write(startsect, nsects, buffer);
440 debug("Error writing data (got %d)\n", ret);
445 buffer += nsects * mydata->sect_size;
446 size -= nsects * mydata->sect_size;
450 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
451 /* Do not leak content of stack */
452 memset(tmpbuf, 0, mydata->sect_size);
453 memcpy(tmpbuf, buffer, size);
454 ret = disk_write(startsect, 1, tmpbuf);
456 debug("Error writing data (got %d)\n", ret);
465 * set_cluster() - write data to cluster
467 * Write 'size' bytes from 'buffer' into the specified cluster.
469 * @mydata: data to be written
470 * @clustnum: cluster to be written to
471 * @buffer: data to be written
472 * @size: bytes to be written (but not more than the size of a cluster)
473 * Return: 0 on success, -1 otherwise
476 set_cluster(fsdata *mydata, u32 clustnum, u8 *buffer, u32 size)
478 return set_sectors(mydata, clust_to_sect(mydata, clustnum),
483 flush_dir(fat_itr *itr)
485 fsdata *mydata = itr->fsdata;
486 u32 startsect, sect_offset, nsects;
488 if (!itr->is_root || mydata->fatsize == 32)
489 return set_cluster(mydata, itr->clust, itr->block,
490 mydata->clust_size * mydata->sect_size);
492 sect_offset = itr->clust * mydata->clust_size;
493 startsect = mydata->rootdir_sect + sect_offset;
494 /* do not write past the end of rootdir */
495 nsects = min_t(u32, mydata->clust_size,
496 mydata->rootdir_size - sect_offset);
498 return set_sectors(mydata, startsect, itr->block,
499 nsects * mydata->sect_size);
503 * Read and modify data on existing and consecutive cluster blocks
506 get_set_cluster(fsdata *mydata, __u32 clustnum, loff_t pos, __u8 *buffer,
507 loff_t size, loff_t *gotsize)
509 static u8 *tmpbuf_cluster;
510 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
513 int clustcount, i, ret;
519 if (!tmpbuf_cluster) {
520 tmpbuf_cluster = memalign(ARCH_DMA_MINALIGN, MAX_CLUSTSIZE);
525 assert(pos < bytesperclust);
526 startsect = clust_to_sect(mydata, clustnum);
528 debug("clustnum: %d, startsect: %d, pos: %lld\n",
529 clustnum, startsect, pos);
531 /* partial write at beginning */
533 wsize = min(bytesperclust - pos, size);
534 ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster);
535 if (ret != mydata->clust_size) {
536 debug("Error reading data (got %d)\n", ret);
540 memcpy(tmpbuf_cluster + pos, buffer, wsize);
541 ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster);
542 if (ret != mydata->clust_size) {
543 debug("Error writing data (got %d)\n", ret);
551 startsect += mydata->clust_size;
557 /* full-cluster write */
558 if (size >= bytesperclust) {
559 clustcount = lldiv(size, bytesperclust);
561 if (!((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1))) {
562 wsize = clustcount * bytesperclust;
563 ret = disk_write(startsect,
564 clustcount * mydata->clust_size,
566 if (ret != clustcount * mydata->clust_size) {
567 debug("Error writing data (got %d)\n", ret);
575 startsect += clustcount * mydata->clust_size;
577 for (i = 0; i < clustcount; i++) {
578 memcpy(tmpbuf_cluster, buffer, bytesperclust);
579 ret = disk_write(startsect,
582 if (ret != mydata->clust_size) {
583 debug("Error writing data (got %d)\n",
588 size -= bytesperclust;
589 buffer += bytesperclust;
590 *gotsize += bytesperclust;
592 startsect += mydata->clust_size;
597 /* partial write at end */
600 ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster);
601 if (ret != mydata->clust_size) {
602 debug("Error reading data (got %d)\n", ret);
605 memcpy(tmpbuf_cluster, buffer, wsize);
606 ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster);
607 if (ret != mydata->clust_size) {
608 debug("Error writing data (got %d)\n", ret);
623 * Find the first empty cluster
625 static int find_empty_cluster(fsdata *mydata)
627 __u32 fat_val, entry = 3;
630 fat_val = get_fatent(mydata, entry);
640 * Allocate a cluster for additional directory entries
642 static int new_dir_table(fat_itr *itr)
644 fsdata *mydata = itr->fsdata;
645 int dir_newclust = 0;
646 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
648 dir_newclust = find_empty_cluster(mydata);
649 set_fatent_value(mydata, itr->clust, dir_newclust);
650 if (mydata->fatsize == 32)
651 set_fatent_value(mydata, dir_newclust, 0xffffff8);
652 else if (mydata->fatsize == 16)
653 set_fatent_value(mydata, dir_newclust, 0xfff8);
654 else if (mydata->fatsize == 12)
655 set_fatent_value(mydata, dir_newclust, 0xff8);
657 itr->clust = dir_newclust;
658 itr->next_clust = dir_newclust;
660 if (flush_dirty_fat_buffer(mydata) < 0)
663 memset(itr->block, 0x00, bytesperclust);
665 itr->dent = (dir_entry *)itr->block;
666 itr->last_cluster = 1;
667 itr->remaining = bytesperclust / sizeof(dir_entry) - 1;
673 * Set empty cluster from 'entry' to the end of a file
675 static int clear_fatent(fsdata *mydata, __u32 entry)
679 while (!CHECK_CLUST(entry, mydata->fatsize)) {
680 fat_val = get_fatent(mydata, entry);
682 set_fatent_value(mydata, entry, 0);
689 /* Flush fat buffer */
690 if (flush_dirty_fat_buffer(mydata) < 0)
697 * Set start cluster in directory entry
699 static void set_start_cluster(const fsdata *mydata, dir_entry *dentptr,
702 if (mydata->fatsize == 32)
704 cpu_to_le16((start_cluster & 0xffff0000) >> 16);
705 dentptr->start = cpu_to_le16(start_cluster & 0xffff);
709 * Check whether adding a file makes the file system to
710 * exceed the size of the block device
711 * Return -1 when overflow occurs, otherwise return 0
713 static int check_overflow(fsdata *mydata, __u32 clustnum, loff_t size)
715 __u32 startsect, sect_num, offset;
718 startsect = clust_to_sect(mydata, clustnum);
720 startsect = mydata->rootdir_sect;
722 sect_num = div_u64_rem(size, mydata->sect_size, &offset);
727 if (startsect + sect_num > total_sector)
733 * Write at most 'maxsize' bytes from 'buffer' into
734 * the file associated with 'dentptr'
735 * Update the number of bytes written in *gotsize and return 0
736 * or return -1 on fatal errors.
739 set_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, __u8 *buffer,
740 loff_t maxsize, loff_t *gotsize)
742 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
743 __u32 curclust = START(dentptr);
744 __u32 endclust = 0, newclust = 0;
745 u64 cur_pos, filesize;
746 loff_t offset, actsize, wsize;
749 filesize = pos + maxsize;
751 debug("%llu bytes\n", filesize);
756 if (!CHECK_CLUST(curclust, mydata->fatsize) ||
757 IS_LAST_CLUST(curclust, mydata->fatsize)) {
758 clear_fatent(mydata, curclust);
759 set_start_cluster(mydata, dentptr, 0);
762 debug("curclust: 0x%x\n", curclust);
763 debug("Invalid FAT entry\n");
772 /* go to cluster at pos */
773 cur_pos = bytesperclust;
777 if (IS_LAST_CLUST(curclust, mydata->fatsize))
780 newclust = get_fatent(mydata, curclust);
781 if (!IS_LAST_CLUST(newclust, mydata->fatsize) &&
782 CHECK_CLUST(newclust, mydata->fatsize)) {
783 debug("curclust: 0x%x\n", curclust);
784 debug("Invalid FAT entry\n");
788 cur_pos += bytesperclust;
791 if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
792 assert(pos == cur_pos);
796 assert(pos < cur_pos);
797 cur_pos -= bytesperclust;
800 assert(IS_LAST_CLUST(curclust, mydata->fatsize) ||
801 !CHECK_CLUST(curclust, mydata->fatsize));
804 /* search for allocated consecutive clusters */
805 actsize = bytesperclust;
808 if (filesize <= (cur_pos + actsize))
811 newclust = get_fatent(mydata, endclust);
813 if (newclust != endclust + 1)
815 if (IS_LAST_CLUST(newclust, mydata->fatsize))
817 if (CHECK_CLUST(newclust, mydata->fatsize)) {
818 debug("curclust: 0x%x\n", curclust);
819 debug("Invalid FAT entry\n");
823 actsize += bytesperclust;
827 /* overwrite to <curclust..endclust> */
831 offset = pos - cur_pos;
832 wsize = min_t(unsigned long long, actsize, filesize - cur_pos);
835 if (get_set_cluster(mydata, curclust, offset,
836 buffer, wsize, &actsize)) {
837 printf("Error get-and-setting cluster\n");
842 cur_pos += offset + wsize;
844 if (filesize <= cur_pos)
847 if (IS_LAST_CLUST(newclust, mydata->fatsize))
848 /* no more clusters */
854 if (filesize <= cur_pos) {
856 newclust = get_fatent(mydata, endclust);
857 if (!IS_LAST_CLUST(newclust, mydata->fatsize)) {
858 /* truncate the rest */
859 clear_fatent(mydata, newclust);
861 /* Mark end of file in FAT */
862 if (mydata->fatsize == 12)
864 else if (mydata->fatsize == 16)
866 else if (mydata->fatsize == 32)
867 newclust = 0xfffffff;
868 set_fatent_value(mydata, endclust, newclust);
876 assert(!do_div(cur_pos, bytesperclust));
879 /* allocate and write */
882 /* Assure that curclust is valid */
884 curclust = find_empty_cluster(mydata);
885 set_start_cluster(mydata, dentptr, curclust);
887 newclust = get_fatent(mydata, curclust);
889 if (IS_LAST_CLUST(newclust, mydata->fatsize)) {
890 newclust = determine_fatent(mydata, curclust);
891 set_fatent_value(mydata, curclust, newclust);
894 debug("error: something wrong\n");
899 /* TODO: already partially written */
900 if (check_overflow(mydata, curclust, filesize)) {
901 printf("Error: no space left: %llu\n", filesize);
905 actsize = bytesperclust;
908 /* search for consecutive clusters */
909 while (actsize < filesize) {
910 newclust = determine_fatent(mydata, endclust);
912 if ((newclust - 1) != endclust)
913 /* write to <curclust..endclust> */
916 if (CHECK_CLUST(newclust, mydata->fatsize)) {
917 debug("newclust: 0x%x\n", newclust);
918 debug("Invalid FAT entry\n");
922 actsize += bytesperclust;
925 /* set remaining bytes */
927 if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
928 debug("error: writing cluster\n");
933 /* Mark end of file in FAT */
934 if (mydata->fatsize == 12)
936 else if (mydata->fatsize == 16)
938 else if (mydata->fatsize == 32)
939 newclust = 0xfffffff;
940 set_fatent_value(mydata, endclust, newclust);
944 if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
945 debug("error: writing cluster\n");
952 if (CHECK_CLUST(newclust, mydata->fatsize)) {
953 debug("newclust: 0x%x\n", newclust);
954 debug("Invalid FAT entry\n");
957 actsize = bytesperclust;
958 curclust = endclust = newclust;
967 static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
968 const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
970 set_start_cluster(mydata, dentptr, start_cluster);
971 dentptr->size = cpu_to_le32(size);
973 dentptr->attr = attr;
975 set_name(dentptr, filename);
979 * Find a directory entry based on filename or start cluster number
980 * If the directory entry is not found,
981 * the new position for writing a directory entry will be returned
983 static dir_entry *find_directory_entry(fat_itr *itr, char *filename)
987 while (fat_itr_next(itr)) {
988 /* check both long and short name: */
989 if (!strcasecmp(filename, itr->name))
991 else if (itr->name != itr->s_name &&
992 !strcasecmp(filename, itr->s_name))
998 if (itr->dent->name[0] == '\0')
1004 /* allocate a cluster for more entries */
1006 (!itr->is_root || itr->fsdata->fatsize == 32) &&
1008 /* indicate that allocating dent failed */
1014 static int split_filename(char *filename, char **dirname, char **basename)
1016 char *p, *last_slash, *last_slash_cont;
1021 last_slash_cont = NULL;
1023 if (ISDIRDELIM(*p)) {
1025 last_slash_cont = p;
1026 /* continuous slashes */
1027 while (ISDIRDELIM(*p))
1028 last_slash_cont = p++;
1036 if (last_slash_cont == (filename + strlen(filename) - 1)) {
1037 /* remove trailing slashes */
1042 if (last_slash == filename) {
1043 /* avoid ""(null) directory */
1047 *dirname = filename;
1050 *last_slash_cont = '\0';
1051 *basename = last_slash_cont + 1;
1053 *dirname = "/"; /* root by default */
1054 *basename = filename;
1061 * normalize_longname() - check long file name and convert to lower case
1063 * We assume here that the FAT file system is using an 8bit code page.
1064 * Linux typically uses CP437, EDK2 assumes CP1250.
1066 * @l_filename: preallocated buffer receiving the normalized name
1067 * @filename: filename to normalize
1068 * Return: 0 on success, -1 on failure
1070 static int normalize_longname(char *l_filename, const char *filename)
1072 const char *p, illegal[] = "<>:\"/\\|?*";
1074 if (strlen(filename) >= VFAT_MAXLEN_BYTES)
1077 for (p = filename; *p; ++p) {
1078 if ((unsigned char)*p < 0x20)
1080 if (strchr(illegal, *p))
1084 strcpy(l_filename, filename);
1085 downcase(l_filename, VFAT_MAXLEN_BYTES);
1090 int file_fat_write_at(const char *filename, loff_t pos, void *buffer,
1091 loff_t size, loff_t *actwrite)
1094 fsdata datablock = { .fatbuf = NULL, };
1095 fsdata *mydata = &datablock;
1096 fat_itr *itr = NULL;
1098 char *filename_copy, *parent, *basename;
1099 char l_filename[VFAT_MAXLEN_BYTES];
1101 debug("writing %s\n", filename);
1103 filename_copy = strdup(filename);
1107 split_filename(filename_copy, &parent, &basename);
1108 if (!strlen(basename)) {
1113 filename = basename;
1114 if (normalize_longname(l_filename, filename)) {
1115 printf("FAT: illegal filename (%s)\n", filename);
1120 itr = malloc_cache_aligned(sizeof(fat_itr));
1126 ret = fat_itr_root(itr, &datablock);
1130 total_sector = datablock.total_sect;
1132 ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1134 printf("%s: doesn't exist (%d)\n", parent, ret);
1138 retdent = find_directory_entry(itr, l_filename);
1141 if (fat_itr_isdir(itr)) {
1148 /* Append to the end */
1149 pos = FAT2CPU32(retdent->size);
1150 if (pos > retdent->size) {
1151 /* No hole allowed */
1156 /* Update file size in a directory entry */
1157 retdent->size = cpu_to_le32(pos + size);
1159 /* Create a new file */
1162 /* root dir cannot have "." or ".." */
1163 if (!strcmp(l_filename, ".") ||
1164 !strcmp(l_filename, "..")) {
1171 printf("Error: allocating new dir entry\n");
1177 /* No hole allowed */
1182 memset(itr->dent, 0, sizeof(*itr->dent));
1184 /* Calculate checksum for short name */
1185 set_name(itr->dent, filename);
1187 /* Set long name entries */
1188 if (fill_dir_slot(itr, filename)) {
1193 /* Set short name entry */
1194 fill_dentry(itr->fsdata, itr->dent, filename, 0, size,
1197 retdent = itr->dent;
1200 ret = set_contents(mydata, retdent, pos, buffer, size, actwrite);
1202 printf("Error: writing contents\n");
1206 debug("attempt to write 0x%llx bytes\n", *actwrite);
1208 /* Flush fat buffer */
1209 ret = flush_dirty_fat_buffer(mydata);
1211 printf("Error: flush fat buffer\n");
1216 /* Write directory table to device */
1217 ret = flush_dir(itr);
1219 printf("Error: writing directory entry\n");
1224 free(filename_copy);
1225 free(mydata->fatbuf);
1230 int file_fat_write(const char *filename, void *buffer, loff_t offset,
1231 loff_t maxsize, loff_t *actwrite)
1233 return file_fat_write_at(filename, offset, buffer, maxsize, actwrite);
1236 static int fat_dir_entries(fat_itr *itr)
1239 fsdata fsdata = { .fatbuf = NULL, }, *mydata = &fsdata;
1240 /* for FATBUFSIZE */
1243 dirs = malloc_cache_aligned(sizeof(fat_itr));
1245 debug("Error: allocating memory\n");
1250 /* duplicate fsdata */
1251 fat_itr_child(dirs, itr);
1252 fsdata = *dirs->fsdata;
1254 /* allocate local fat buffer */
1255 fsdata.fatbuf = malloc_cache_aligned(FATBUFSIZE);
1256 if (!fsdata.fatbuf) {
1257 debug("Error: allocating memory\n");
1261 fsdata.fatbufnum = -1;
1262 dirs->fsdata = &fsdata;
1264 for (count = 0; fat_itr_next(dirs); count++)
1268 free(fsdata.fatbuf);
1273 static int delete_dentry(fat_itr *itr)
1275 fsdata *mydata = itr->fsdata;
1276 dir_entry *dentptr = itr->dent;
1278 /* free cluster blocks */
1279 clear_fatent(mydata, START(dentptr));
1280 if (flush_dirty_fat_buffer(mydata) < 0) {
1281 printf("Error: flush fat buffer\n");
1286 * update a directory entry
1288 * - long file name support
1289 * - find and mark the "new" first invalid entry as name[0]=0x00
1291 memset(dentptr, 0, sizeof(*dentptr));
1292 dentptr->name[0] = 0xe5;
1294 if (flush_dir(itr)) {
1295 printf("error: writing directory entry\n");
1302 int fat_unlink(const char *filename)
1304 fsdata fsdata = { .fatbuf = NULL, };
1305 fat_itr *itr = NULL;
1307 char *filename_copy, *dirname, *basename;
1309 filename_copy = strdup(filename);
1310 if (!filename_copy) {
1311 printf("Error: allocating memory\n");
1315 split_filename(filename_copy, &dirname, &basename);
1317 if (!strcmp(dirname, "/") && !strcmp(basename, "")) {
1318 printf("Error: cannot remove root\n");
1323 itr = malloc_cache_aligned(sizeof(fat_itr));
1325 printf("Error: allocating memory\n");
1330 ret = fat_itr_root(itr, &fsdata);
1334 total_sector = fsdata.total_sect;
1336 ret = fat_itr_resolve(itr, dirname, TYPE_DIR);
1338 printf("%s: doesn't exist (%d)\n", dirname, ret);
1343 if (!find_directory_entry(itr, basename)) {
1344 printf("%s: doesn't exist\n", basename);
1349 if (fat_itr_isdir(itr)) {
1350 n_entries = fat_dir_entries(itr);
1351 if (n_entries < 0) {
1355 if (n_entries > 2) {
1356 printf("Error: directory is not empty: %d\n",
1363 ret = delete_dentry(itr);
1366 free(fsdata.fatbuf);
1368 free(filename_copy);
1373 int fat_mkdir(const char *new_dirname)
1376 fsdata datablock = { .fatbuf = NULL, };
1377 fsdata *mydata = &datablock;
1378 fat_itr *itr = NULL;
1379 char *dirname_copy, *parent, *dirname;
1380 char l_dirname[VFAT_MAXLEN_BYTES];
1383 unsigned int bytesperclust;
1384 dir_entry *dotdent = NULL;
1386 dirname_copy = strdup(new_dirname);
1390 split_filename(dirname_copy, &parent, &dirname);
1391 if (!strlen(dirname)) {
1396 if (normalize_longname(l_dirname, dirname)) {
1397 printf("FAT: illegal filename (%s)\n", dirname);
1402 itr = malloc_cache_aligned(sizeof(fat_itr));
1408 ret = fat_itr_root(itr, &datablock);
1412 total_sector = datablock.total_sect;
1414 ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1416 printf("%s: doesn't exist (%d)\n", parent, ret);
1420 retdent = find_directory_entry(itr, l_dirname);
1423 printf("%s: already exists\n", l_dirname);
1428 /* root dir cannot have "." or ".." */
1429 if (!strcmp(l_dirname, ".") ||
1430 !strcmp(l_dirname, "..")) {
1437 printf("Error: allocating new dir entry\n");
1442 memset(itr->dent, 0, sizeof(*itr->dent));
1444 /* Set short name to set alias checksum field in dir_slot */
1445 set_name(itr->dent, dirname);
1446 fill_dir_slot(itr, dirname);
1448 /* Set attribute as archive for regular file */
1449 fill_dentry(itr->fsdata, itr->dent, dirname, 0, 0,
1450 ATTR_DIR | ATTR_ARCH);
1452 retdent = itr->dent;
1455 /* Default entries */
1456 bytesperclust = mydata->clust_size * mydata->sect_size;
1457 dotdent = malloc_cache_aligned(bytesperclust);
1462 memset(dotdent, 0, bytesperclust);
1464 memcpy(dotdent[0].name, ". ", 8);
1465 memcpy(dotdent[0].ext, " ", 3);
1466 dotdent[0].attr = ATTR_DIR | ATTR_ARCH;
1468 memcpy(dotdent[1].name, ".. ", 8);
1469 memcpy(dotdent[1].ext, " ", 3);
1470 dotdent[1].attr = ATTR_DIR | ATTR_ARCH;
1471 set_start_cluster(mydata, &dotdent[1], itr->start_clust);
1473 ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
1474 bytesperclust, &actwrite);
1476 printf("Error: writing contents\n");
1479 /* Write twice for "." */
1480 set_start_cluster(mydata, &dotdent[0], START(retdent));
1481 ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
1482 bytesperclust, &actwrite);
1484 printf("Error: writing contents\n");
1488 /* Flush fat buffer */
1489 ret = flush_dirty_fat_buffer(mydata);
1491 printf("Error: flush fat buffer\n");
1495 /* Write directory table to device */
1496 ret = flush_dir(itr);
1498 printf("Error: writing directory entry\n");
1502 free(mydata->fatbuf);