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 * set_cluster() - write data to cluster
393 * Write 'size' bytes from 'buffer' into the specified cluster.
395 * @mydata: data to be written
396 * @clustnum: cluster 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_cluster(fsdata
*mydata
, u32 clustnum
, u8
*buffer
, u32 size
)
409 startsect
= clust_to_sect(mydata
, clustnum
);
411 startsect
= mydata
->rootdir_sect
;
413 debug("clustnum: %d, startsect: %d\n", clustnum
, startsect
);
415 if ((unsigned long)buffer
& (ARCH_DMA_MINALIGN
- 1)) {
416 ALLOC_CACHE_ALIGN_BUFFER(__u8
, tmpbuf
, mydata
->sect_size
);
418 debug("FAT: Misaligned buffer address (%p)\n", buffer
);
420 while (size
>= mydata
->sect_size
) {
421 memcpy(tmpbuf
, buffer
, mydata
->sect_size
);
422 ret
= disk_write(startsect
++, 1, tmpbuf
);
424 debug("Error writing data (got %d)\n", ret
);
428 buffer
+= mydata
->sect_size
;
429 size
-= mydata
->sect_size
;
431 } else if (size
>= mydata
->sect_size
) {
432 idx
= size
/ mydata
->sect_size
;
433 ret
= disk_write(startsect
, idx
, buffer
);
435 debug("Error writing data (got %d)\n", ret
);
440 idx
*= mydata
->sect_size
;
446 ALLOC_CACHE_ALIGN_BUFFER(__u8
, tmpbuf
, mydata
->sect_size
);
447 /* Do not leak content of stack */
448 memset(tmpbuf
, 0, mydata
->sect_size
);
449 memcpy(tmpbuf
, buffer
, size
);
450 ret
= disk_write(startsect
, 1, tmpbuf
);
452 debug("Error writing data (got %d)\n", ret
);
460 static __u8 tmpbuf_cluster
[MAX_CLUSTSIZE
] __aligned(ARCH_DMA_MINALIGN
);
463 * Read and modify data on existing and consecutive cluster blocks
466 get_set_cluster(fsdata
*mydata
, __u32 clustnum
, loff_t pos
, __u8
*buffer
,
467 loff_t size
, loff_t
*gotsize
)
469 unsigned int bytesperclust
= mydata
->clust_size
* mydata
->sect_size
;
472 int clustcount
, i
, ret
;
478 assert(pos
< bytesperclust
);
479 startsect
= clust_to_sect(mydata
, clustnum
);
481 debug("clustnum: %d, startsect: %d, pos: %lld\n",
482 clustnum
, startsect
, pos
);
484 /* partial write at beginning */
486 wsize
= min(bytesperclust
- pos
, size
);
487 ret
= disk_read(startsect
, mydata
->clust_size
, tmpbuf_cluster
);
488 if (ret
!= mydata
->clust_size
) {
489 debug("Error reading data (got %d)\n", ret
);
493 memcpy(tmpbuf_cluster
+ pos
, buffer
, wsize
);
494 ret
= disk_write(startsect
, mydata
->clust_size
, tmpbuf_cluster
);
495 if (ret
!= mydata
->clust_size
) {
496 debug("Error writing data (got %d)\n", ret
);
504 startsect
+= mydata
->clust_size
;
510 /* full-cluster write */
511 if (size
>= bytesperclust
) {
512 clustcount
= lldiv(size
, bytesperclust
);
514 if (!((unsigned long)buffer
& (ARCH_DMA_MINALIGN
- 1))) {
515 wsize
= clustcount
* bytesperclust
;
516 ret
= disk_write(startsect
,
517 clustcount
* mydata
->clust_size
,
519 if (ret
!= clustcount
* mydata
->clust_size
) {
520 debug("Error writing data (got %d)\n", ret
);
528 startsect
+= clustcount
* mydata
->clust_size
;
530 for (i
= 0; i
< clustcount
; i
++) {
531 memcpy(tmpbuf_cluster
, buffer
, bytesperclust
);
532 ret
= disk_write(startsect
,
535 if (ret
!= mydata
->clust_size
) {
536 debug("Error writing data (got %d)\n",
541 size
-= bytesperclust
;
542 buffer
+= bytesperclust
;
543 *gotsize
+= bytesperclust
;
545 startsect
+= mydata
->clust_size
;
550 /* partial write at end */
553 ret
= disk_read(startsect
, mydata
->clust_size
, tmpbuf_cluster
);
554 if (ret
!= mydata
->clust_size
) {
555 debug("Error reading data (got %d)\n", ret
);
558 memcpy(tmpbuf_cluster
, buffer
, wsize
);
559 ret
= disk_write(startsect
, mydata
->clust_size
, tmpbuf_cluster
);
560 if (ret
!= mydata
->clust_size
) {
561 debug("Error writing data (got %d)\n", ret
);
576 * Find the first empty cluster
578 static int find_empty_cluster(fsdata
*mydata
)
580 __u32 fat_val
, entry
= 3;
583 fat_val
= get_fatent(mydata
, entry
);
593 * Write directory entries in itr's buffer to block device
595 static int flush_dir_table(fat_itr
*itr
)
597 fsdata
*mydata
= itr
->fsdata
;
598 int dir_newclust
= 0;
599 unsigned int bytesperclust
= mydata
->clust_size
* mydata
->sect_size
;
601 if (set_cluster(mydata
, itr
->clust
, itr
->block
, bytesperclust
) != 0) {
602 printf("error: writing directory entry\n");
605 dir_newclust
= find_empty_cluster(mydata
);
606 set_fatent_value(mydata
, itr
->clust
, dir_newclust
);
607 if (mydata
->fatsize
== 32)
608 set_fatent_value(mydata
, dir_newclust
, 0xffffff8);
609 else if (mydata
->fatsize
== 16)
610 set_fatent_value(mydata
, dir_newclust
, 0xfff8);
611 else if (mydata
->fatsize
== 12)
612 set_fatent_value(mydata
, dir_newclust
, 0xff8);
614 itr
->clust
= dir_newclust
;
615 itr
->next_clust
= dir_newclust
;
617 if (flush_dirty_fat_buffer(mydata
) < 0)
620 memset(itr
->block
, 0x00, bytesperclust
);
622 itr
->dent
= (dir_entry
*)itr
->block
;
623 itr
->last_cluster
= 1;
624 itr
->remaining
= bytesperclust
/ sizeof(dir_entry
) - 1;
630 * Set empty cluster from 'entry' to the end of a file
632 static int clear_fatent(fsdata
*mydata
, __u32 entry
)
636 while (!CHECK_CLUST(entry
, mydata
->fatsize
)) {
637 fat_val
= get_fatent(mydata
, entry
);
639 set_fatent_value(mydata
, entry
, 0);
646 /* Flush fat buffer */
647 if (flush_dirty_fat_buffer(mydata
) < 0)
654 * Set start cluster in directory entry
656 static void set_start_cluster(const fsdata
*mydata
, dir_entry
*dentptr
,
659 if (mydata
->fatsize
== 32)
661 cpu_to_le16((start_cluster
& 0xffff0000) >> 16);
662 dentptr
->start
= cpu_to_le16(start_cluster
& 0xffff);
666 * Check whether adding a file makes the file system to
667 * exceed the size of the block device
668 * Return -1 when overflow occurs, otherwise return 0
670 static int check_overflow(fsdata
*mydata
, __u32 clustnum
, loff_t size
)
672 __u32 startsect
, sect_num
, offset
;
675 startsect
= clust_to_sect(mydata
, clustnum
);
677 startsect
= mydata
->rootdir_sect
;
679 sect_num
= div_u64_rem(size
, mydata
->sect_size
, &offset
);
684 if (startsect
+ sect_num
> total_sector
)
690 * Write at most 'maxsize' bytes from 'buffer' into
691 * the file associated with 'dentptr'
692 * Update the number of bytes written in *gotsize and return 0
693 * or return -1 on fatal errors.
696 set_contents(fsdata
*mydata
, dir_entry
*dentptr
, loff_t pos
, __u8
*buffer
,
697 loff_t maxsize
, loff_t
*gotsize
)
699 unsigned int bytesperclust
= mydata
->clust_size
* mydata
->sect_size
;
700 __u32 curclust
= START(dentptr
);
701 __u32 endclust
= 0, newclust
= 0;
702 u64 cur_pos
, filesize
;
703 loff_t offset
, actsize
, wsize
;
706 filesize
= pos
+ maxsize
;
708 debug("%llu bytes\n", filesize
);
713 if (!CHECK_CLUST(curclust
, mydata
->fatsize
) ||
714 IS_LAST_CLUST(curclust
, mydata
->fatsize
)) {
715 clear_fatent(mydata
, curclust
);
716 set_start_cluster(mydata
, dentptr
, 0);
719 debug("curclust: 0x%x\n", curclust
);
720 debug("Invalid FAT entry\n");
729 /* go to cluster at pos */
730 cur_pos
= bytesperclust
;
734 if (IS_LAST_CLUST(curclust
, mydata
->fatsize
))
737 newclust
= get_fatent(mydata
, curclust
);
738 if (!IS_LAST_CLUST(newclust
, mydata
->fatsize
) &&
739 CHECK_CLUST(newclust
, mydata
->fatsize
)) {
740 debug("curclust: 0x%x\n", curclust
);
741 debug("Invalid FAT entry\n");
745 cur_pos
+= bytesperclust
;
748 if (IS_LAST_CLUST(curclust
, mydata
->fatsize
)) {
749 assert(pos
== cur_pos
);
753 assert(pos
< cur_pos
);
754 cur_pos
-= bytesperclust
;
757 assert(IS_LAST_CLUST(curclust
, mydata
->fatsize
) ||
758 !CHECK_CLUST(curclust
, mydata
->fatsize
));
761 /* search for allocated consecutive clusters */
762 actsize
= bytesperclust
;
765 if (filesize
<= (cur_pos
+ actsize
))
768 newclust
= get_fatent(mydata
, endclust
);
770 if (IS_LAST_CLUST(newclust
, mydata
->fatsize
))
772 if (CHECK_CLUST(newclust
, mydata
->fatsize
)) {
773 debug("curclust: 0x%x\n", curclust
);
774 debug("Invalid FAT entry\n");
778 actsize
+= bytesperclust
;
782 /* overwrite to <curclust..endclust> */
786 offset
= pos
- cur_pos
;
787 wsize
= min(cur_pos
+ actsize
, filesize
) - pos
;
788 if (get_set_cluster(mydata
, curclust
, offset
,
789 buffer
, wsize
, &actsize
)) {
790 printf("Error get-and-setting cluster\n");
795 cur_pos
+= offset
+ wsize
;
797 if (filesize
<= cur_pos
)
800 /* CHECK: newclust = get_fatent(mydata, endclust); */
802 if (IS_LAST_CLUST(newclust
, mydata
->fatsize
))
803 /* no more clusters */
809 if (filesize
<= cur_pos
) {
811 newclust
= get_fatent(mydata
, endclust
);
812 if (!IS_LAST_CLUST(newclust
, mydata
->fatsize
)) {
813 /* truncate the rest */
814 clear_fatent(mydata
, newclust
);
816 /* Mark end of file in FAT */
817 if (mydata
->fatsize
== 12)
819 else if (mydata
->fatsize
== 16)
821 else if (mydata
->fatsize
== 32)
822 newclust
= 0xfffffff;
823 set_fatent_value(mydata
, endclust
, newclust
);
831 assert(!do_div(cur_pos
, bytesperclust
));
834 /* allocate and write */
837 /* Assure that curclust is valid */
839 curclust
= find_empty_cluster(mydata
);
840 set_start_cluster(mydata
, dentptr
, curclust
);
842 newclust
= get_fatent(mydata
, curclust
);
844 if (IS_LAST_CLUST(newclust
, mydata
->fatsize
)) {
845 newclust
= determine_fatent(mydata
, curclust
);
846 set_fatent_value(mydata
, curclust
, newclust
);
849 debug("error: something wrong\n");
854 /* TODO: already partially written */
855 if (check_overflow(mydata
, curclust
, filesize
)) {
856 printf("Error: no space left: %llu\n", filesize
);
860 actsize
= bytesperclust
;
863 /* search for consecutive clusters */
864 while (actsize
< filesize
) {
865 newclust
= determine_fatent(mydata
, endclust
);
867 if ((newclust
- 1) != endclust
)
868 /* write to <curclust..endclust> */
871 if (CHECK_CLUST(newclust
, mydata
->fatsize
)) {
872 debug("newclust: 0x%x\n", newclust
);
873 debug("Invalid FAT entry\n");
877 actsize
+= bytesperclust
;
880 /* set remaining bytes */
882 if (set_cluster(mydata
, curclust
, buffer
, (u32
)actsize
) != 0) {
883 debug("error: writing cluster\n");
888 /* Mark end of file in FAT */
889 if (mydata
->fatsize
== 12)
891 else if (mydata
->fatsize
== 16)
893 else if (mydata
->fatsize
== 32)
894 newclust
= 0xfffffff;
895 set_fatent_value(mydata
, endclust
, newclust
);
899 if (set_cluster(mydata
, curclust
, buffer
, (u32
)actsize
) != 0) {
900 debug("error: writing cluster\n");
907 if (CHECK_CLUST(newclust
, mydata
->fatsize
)) {
908 debug("newclust: 0x%x\n", newclust
);
909 debug("Invalid FAT entry\n");
912 actsize
= bytesperclust
;
913 curclust
= endclust
= newclust
;
922 static void fill_dentry(fsdata
*mydata
, dir_entry
*dentptr
,
923 const char *filename
, __u32 start_cluster
, __u32 size
, __u8 attr
)
925 set_start_cluster(mydata
, dentptr
, start_cluster
);
926 dentptr
->size
= cpu_to_le32(size
);
928 dentptr
->attr
= attr
;
930 set_name(dentptr
, filename
);
934 * Find a directory entry based on filename or start cluster number
935 * If the directory entry is not found,
936 * the new position for writing a directory entry will be returned
938 static dir_entry
*find_directory_entry(fat_itr
*itr
, char *filename
)
942 while (fat_itr_next(itr
)) {
943 /* check both long and short name: */
944 if (!strcasecmp(filename
, itr
->name
))
946 else if (itr
->name
!= itr
->s_name
&&
947 !strcasecmp(filename
, itr
->s_name
))
953 if (itr
->dent
->name
[0] == '\0')
959 if (!itr
->dent
&& !itr
->is_root
&& flush_dir_table(itr
))
960 /* indicate that allocating dent failed */
966 static int split_filename(char *filename
, char **dirname
, char **basename
)
968 char *p
, *last_slash
, *last_slash_cont
;
973 last_slash_cont
= NULL
;
975 if (ISDIRDELIM(*p
)) {
978 /* continuous slashes */
979 while (ISDIRDELIM(*p
))
980 last_slash_cont
= p
++;
988 if (last_slash_cont
== (filename
+ strlen(filename
) - 1)) {
989 /* remove trailing slashes */
994 if (last_slash
== filename
) {
995 /* avoid ""(null) directory */
1002 *last_slash_cont
= '\0';
1003 *basename
= last_slash_cont
+ 1;
1005 *dirname
= "/"; /* root by default */
1006 *basename
= filename
;
1012 static int normalize_longname(char *l_filename
, const char *filename
)
1014 const char *p
, legal
[] = "!#$%&\'()-.@^`_{}~";
1018 /* Check that the filename is valid */
1019 for (p
= filename
; p
< filename
+ strlen(filename
); p
++) {
1022 if (('0' <= c
) && (c
<= '9'))
1024 if (('A' <= c
) && (c
<= 'Z'))
1026 if (('a' <= c
) && (c
<= 'z'))
1028 if (strchr(legal
, c
))
1031 if ((0x80 <= c
) && (c
<= 0xff))
1038 name_len
= strlen(filename
);
1039 if (name_len
>= VFAT_MAXLEN_BYTES
)
1040 /* should return an error? */
1041 name_len
= VFAT_MAXLEN_BYTES
- 1;
1043 memcpy(l_filename
, filename
, name_len
);
1044 l_filename
[name_len
] = 0; /* terminate the string */
1045 downcase(l_filename
, INT_MAX
);
1050 int file_fat_write_at(const char *filename
, loff_t pos
, void *buffer
,
1051 loff_t size
, loff_t
*actwrite
)
1054 fsdata datablock
= { .fatbuf
= NULL
, };
1055 fsdata
*mydata
= &datablock
;
1056 fat_itr
*itr
= NULL
;
1058 char *filename_copy
, *parent
, *basename
;
1059 char l_filename
[VFAT_MAXLEN_BYTES
];
1061 debug("writing %s\n", filename
);
1063 filename_copy
= strdup(filename
);
1067 split_filename(filename_copy
, &parent
, &basename
);
1068 if (!strlen(basename
)) {
1073 filename
= basename
;
1074 if (normalize_longname(l_filename
, filename
)) {
1075 printf("FAT: illegal filename (%s)\n", filename
);
1080 itr
= malloc_cache_aligned(sizeof(fat_itr
));
1086 ret
= fat_itr_root(itr
, &datablock
);
1090 total_sector
= datablock
.total_sect
;
1092 ret
= fat_itr_resolve(itr
, parent
, TYPE_DIR
);
1094 printf("%s: doesn't exist (%d)\n", parent
, ret
);
1098 retdent
= find_directory_entry(itr
, l_filename
);
1101 if (fat_itr_isdir(itr
)) {
1108 /* Append to the end */
1109 pos
= FAT2CPU32(retdent
->size
);
1110 if (pos
> retdent
->size
) {
1111 /* No hole allowed */
1116 /* Update file size in a directory entry */
1117 retdent
->size
= cpu_to_le32(pos
+ size
);
1119 /* Create a new file */
1122 /* root dir cannot have "." or ".." */
1123 if (!strcmp(l_filename
, ".") ||
1124 !strcmp(l_filename
, "..")) {
1131 printf("Error: allocating new dir entry\n");
1137 /* No hole allowed */
1142 memset(itr
->dent
, 0, sizeof(*itr
->dent
));
1144 /* Set short name to set alias checksum field in dir_slot */
1145 set_name(itr
->dent
, filename
);
1146 if (fill_dir_slot(itr
, filename
)) {
1151 /* Set attribute as archive for regular file */
1152 fill_dentry(itr
->fsdata
, itr
->dent
, filename
, 0, size
, 0x20);
1154 retdent
= itr
->dent
;
1157 ret
= set_contents(mydata
, retdent
, pos
, buffer
, size
, actwrite
);
1159 printf("Error: writing contents\n");
1163 debug("attempt to write 0x%llx bytes\n", *actwrite
);
1165 /* Flush fat buffer */
1166 ret
= flush_dirty_fat_buffer(mydata
);
1168 printf("Error: flush fat buffer\n");
1173 /* Write directory table to device */
1174 ret
= set_cluster(mydata
, itr
->clust
, itr
->block
,
1175 mydata
->clust_size
* mydata
->sect_size
);
1177 printf("Error: writing directory entry\n");
1182 free(filename_copy
);
1183 free(mydata
->fatbuf
);
1188 int file_fat_write(const char *filename
, void *buffer
, loff_t offset
,
1189 loff_t maxsize
, loff_t
*actwrite
)
1191 return file_fat_write_at(filename
, offset
, buffer
, maxsize
, actwrite
);
1194 static int fat_dir_entries(fat_itr
*itr
)
1197 fsdata fsdata
= { .fatbuf
= NULL
, }, *mydata
= &fsdata
;
1198 /* for FATBUFSIZE */
1201 dirs
= malloc_cache_aligned(sizeof(fat_itr
));
1203 debug("Error: allocating memory\n");
1208 /* duplicate fsdata */
1209 fat_itr_child(dirs
, itr
);
1210 fsdata
= *dirs
->fsdata
;
1212 /* allocate local fat buffer */
1213 fsdata
.fatbuf
= malloc_cache_aligned(FATBUFSIZE
);
1214 if (!fsdata
.fatbuf
) {
1215 debug("Error: allocating memory\n");
1219 fsdata
.fatbufnum
= -1;
1220 dirs
->fsdata
= &fsdata
;
1222 for (count
= 0; fat_itr_next(dirs
); count
++)
1226 free(fsdata
.fatbuf
);
1231 static int delete_dentry(fat_itr
*itr
)
1233 fsdata
*mydata
= itr
->fsdata
;
1234 dir_entry
*dentptr
= itr
->dent
;
1236 /* free cluster blocks */
1237 clear_fatent(mydata
, START(dentptr
));
1238 if (flush_dirty_fat_buffer(mydata
) < 0) {
1239 printf("Error: flush fat buffer\n");
1244 * update a directory entry
1246 * - long file name support
1247 * - find and mark the "new" first invalid entry as name[0]=0x00
1249 memset(dentptr
, 0, sizeof(*dentptr
));
1250 dentptr
->name
[0] = 0xe5;
1252 if (set_cluster(mydata
, itr
->clust
, itr
->block
,
1253 mydata
->clust_size
* mydata
->sect_size
) != 0) {
1254 printf("error: writing directory entry\n");
1261 int fat_unlink(const char *filename
)
1263 fsdata fsdata
= { .fatbuf
= NULL
, };
1264 fat_itr
*itr
= NULL
;
1266 char *filename_copy
, *dirname
, *basename
;
1268 filename_copy
= strdup(filename
);
1269 if (!filename_copy
) {
1270 printf("Error: allocating memory\n");
1274 split_filename(filename_copy
, &dirname
, &basename
);
1276 if (!strcmp(dirname
, "/") && !strcmp(basename
, "")) {
1277 printf("Error: cannot remove root\n");
1282 itr
= malloc_cache_aligned(sizeof(fat_itr
));
1284 printf("Error: allocating memory\n");
1289 ret
= fat_itr_root(itr
, &fsdata
);
1293 total_sector
= fsdata
.total_sect
;
1295 ret
= fat_itr_resolve(itr
, dirname
, TYPE_DIR
);
1297 printf("%s: doesn't exist (%d)\n", dirname
, ret
);
1302 if (!find_directory_entry(itr
, basename
)) {
1303 printf("%s: doesn't exist\n", basename
);
1308 if (fat_itr_isdir(itr
)) {
1309 n_entries
= fat_dir_entries(itr
);
1310 if (n_entries
< 0) {
1314 if (n_entries
> 2) {
1315 printf("Error: directory is not empty: %d\n",
1322 ret
= delete_dentry(itr
);
1325 free(fsdata
.fatbuf
);
1327 free(filename_copy
);
1332 int fat_mkdir(const char *new_dirname
)
1335 fsdata datablock
= { .fatbuf
= NULL
, };
1336 fsdata
*mydata
= &datablock
;
1337 fat_itr
*itr
= NULL
;
1338 char *dirname_copy
, *parent
, *dirname
;
1339 char l_dirname
[VFAT_MAXLEN_BYTES
];
1342 unsigned int bytesperclust
;
1343 dir_entry
*dotdent
= NULL
;
1345 dirname_copy
= strdup(new_dirname
);
1349 split_filename(dirname_copy
, &parent
, &dirname
);
1350 if (!strlen(dirname
)) {
1355 if (normalize_longname(l_dirname
, dirname
)) {
1356 printf("FAT: illegal filename (%s)\n", dirname
);
1361 itr
= malloc_cache_aligned(sizeof(fat_itr
));
1367 ret
= fat_itr_root(itr
, &datablock
);
1371 total_sector
= datablock
.total_sect
;
1373 ret
= fat_itr_resolve(itr
, parent
, TYPE_DIR
);
1375 printf("%s: doesn't exist (%d)\n", parent
, ret
);
1379 retdent
= find_directory_entry(itr
, l_dirname
);
1382 printf("%s: already exists\n", l_dirname
);
1387 /* root dir cannot have "." or ".." */
1388 if (!strcmp(l_dirname
, ".") ||
1389 !strcmp(l_dirname
, "..")) {
1396 printf("Error: allocating new dir entry\n");
1401 memset(itr
->dent
, 0, sizeof(*itr
->dent
));
1403 /* Set short name to set alias checksum field in dir_slot */
1404 set_name(itr
->dent
, dirname
);
1405 fill_dir_slot(itr
, dirname
);
1407 /* Set attribute as archive for regular file */
1408 fill_dentry(itr
->fsdata
, itr
->dent
, dirname
, 0, 0,
1409 ATTR_DIR
| ATTR_ARCH
);
1411 retdent
= itr
->dent
;
1414 /* Default entries */
1415 bytesperclust
= mydata
->clust_size
* mydata
->sect_size
;
1416 dotdent
= malloc_cache_aligned(bytesperclust
);
1421 memset(dotdent
, 0, bytesperclust
);
1423 memcpy(dotdent
[0].name
, ". ", 8);
1424 memcpy(dotdent
[0].ext
, " ", 3);
1425 dotdent
[0].attr
= ATTR_DIR
| ATTR_ARCH
;
1427 memcpy(dotdent
[1].name
, ".. ", 8);
1428 memcpy(dotdent
[1].ext
, " ", 3);
1429 dotdent
[1].attr
= ATTR_DIR
| ATTR_ARCH
;
1430 set_start_cluster(mydata
, &dotdent
[1], itr
->start_clust
);
1432 ret
= set_contents(mydata
, retdent
, 0, (__u8
*)dotdent
,
1433 bytesperclust
, &actwrite
);
1435 printf("Error: writing contents\n");
1438 /* Write twice for "." */
1439 set_start_cluster(mydata
, &dotdent
[0], START(retdent
));
1440 ret
= set_contents(mydata
, retdent
, 0, (__u8
*)dotdent
,
1441 bytesperclust
, &actwrite
);
1443 printf("Error: writing contents\n");
1447 /* Flush fat buffer */
1448 ret
= flush_dirty_fat_buffer(mydata
);
1450 printf("Error: flush fat buffer\n");
1454 /* Write directory table to device */
1455 ret
= set_cluster(mydata
, itr
->clust
, itr
->block
,
1456 mydata
->clust_size
* mydata
->sect_size
);
1458 printf("Error: writing directory entry\n");
1462 free(mydata
->fatbuf
);