1 // SPDX-License-Identifier: GPL-2.0+
5 * R/W (V)FAT 12/16/32 filesystem implementation by Donggeun Kim
13 #include <asm/byteorder.h>
15 #include <asm/cache.h>
16 #include <linux/ctype.h>
18 #include <linux/math64.h>
21 static void uppercase(char *str
, int len
)
25 for (i
= 0; i
< len
; i
++) {
31 static int total_sector
;
32 static int disk_write(__u32 block
, __u32 nr_blocks
, void *buf
)
39 if (cur_part_info
.start
+ block
+ nr_blocks
>
40 cur_part_info
.start
+ total_sector
) {
41 printf("error: overflow occurs\n");
45 ret
= blk_dwrite(cur_dev
, cur_part_info
.start
+ block
, nr_blocks
, buf
);
46 if (nr_blocks
&& ret
== 0)
53 * Set short name in directory entry
55 static void set_name(dir_entry
*dirent
, const char *filename
)
57 char s_name
[VFAT_MAXLEN_BYTES
];
59 int period_location
, len
, i
, ext_num
;
64 len
= strlen(filename
);
68 strcpy(s_name
, filename
);
69 uppercase(s_name
, len
);
71 period
= strchr(s_name
, '.');
73 period_location
= len
;
76 period_location
= period
- s_name
;
77 ext_num
= len
- period_location
- 1;
80 /* Pad spaces when the length of file name is shorter than eight */
81 if (period_location
< 8) {
82 memcpy(dirent
->name
, s_name
, period_location
);
83 for (i
= period_location
; i
< 8; i
++)
84 dirent
->name
[i
] = ' ';
85 } else if (period_location
== 8) {
86 memcpy(dirent
->name
, s_name
, period_location
);
88 memcpy(dirent
->name
, s_name
, 6);
89 dirent
->name
[6] = '~';
90 dirent
->name
[7] = '1';
94 memcpy(dirent
->ext
, s_name
+ period_location
+ 1, ext_num
);
95 for (i
= ext_num
; i
< 3; i
++)
98 memcpy(dirent
->ext
, s_name
+ period_location
+ 1, 3);
100 debug("name : %s\n", dirent
->name
);
101 debug("ext : %s\n", dirent
->ext
);
105 * Write fat buffer into block device
107 static int flush_dirty_fat_buffer(fsdata
*mydata
)
109 int getsize
= FATBUFBLOCKS
;
110 __u32 fatlength
= mydata
->fatlength
;
111 __u8
*bufptr
= mydata
->fatbuf
;
112 __u32 startblock
= mydata
->fatbufnum
* FATBUFBLOCKS
;
114 debug("debug: evicting %d, dirty: %d\n", mydata
->fatbufnum
,
115 (int)mydata
->fat_dirty
);
117 if ((!mydata
->fat_dirty
) || (mydata
->fatbufnum
== -1))
120 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
121 if (startblock
+ getsize
> fatlength
)
122 getsize
= fatlength
- startblock
;
124 startblock
+= mydata
->fat_sect
;
127 if (disk_write(startblock
, getsize
, bufptr
) < 0) {
128 debug("error: writing FAT blocks\n");
132 if (mydata
->fats
== 2) {
133 /* Update corresponding second FAT blocks */
134 startblock
+= mydata
->fatlength
;
135 if (disk_write(startblock
, getsize
, bufptr
) < 0) {
136 debug("error: writing second FAT blocks\n");
140 mydata
->fat_dirty
= 0;
146 * Set the file name information from 'name' into 'slotptr',
148 static int str2slot(dir_slot
*slotptr
, const char *name
, int *idx
)
152 for (j
= 0; j
<= 8; j
+= 2) {
153 if (name
[*idx
] == 0x00) {
154 slotptr
->name0_4
[j
] = 0;
155 slotptr
->name0_4
[j
+ 1] = 0;
159 slotptr
->name0_4
[j
] = name
[*idx
];
163 for (j
= 0; j
<= 10; j
+= 2) {
164 if (name
[*idx
] == 0x00) {
165 slotptr
->name5_10
[j
] = 0;
166 slotptr
->name5_10
[j
+ 1] = 0;
170 slotptr
->name5_10
[j
] = name
[*idx
];
174 for (j
= 0; j
<= 2; j
+= 2) {
175 if (name
[*idx
] == 0x00) {
176 slotptr
->name11_12
[j
] = 0;
177 slotptr
->name11_12
[j
+ 1] = 0;
181 slotptr
->name11_12
[j
] = name
[*idx
];
186 if (name
[*idx
] == 0x00)
190 /* Not used characters are filled with 0xff 0xff */
192 for (; end_idx
< 5; end_idx
++) {
193 slotptr
->name0_4
[end_idx
* 2] = 0xff;
194 slotptr
->name0_4
[end_idx
* 2 + 1] = 0xff;
199 for (; end_idx
< 6; end_idx
++) {
200 slotptr
->name5_10
[end_idx
* 2] = 0xff;
201 slotptr
->name5_10
[end_idx
* 2 + 1] = 0xff;
206 for (; end_idx
< 2; end_idx
++) {
207 slotptr
->name11_12
[end_idx
* 2] = 0xff;
208 slotptr
->name11_12
[end_idx
* 2 + 1] = 0xff;
214 static int new_dir_table(fat_itr
*itr
);
215 static int flush_dir(fat_itr
*itr
);
218 * Fill dir_slot entries with appropriate name, id, and attr
219 * 'itr' will point to a next entry
222 fill_dir_slot(fat_itr
*itr
, const char *l_name
)
224 __u8 temp_dir_slot_buffer
[MAX_LFN_SLOT
* sizeof(dir_slot
)];
225 dir_slot
*slotptr
= (dir_slot
*)temp_dir_slot_buffer
;
226 __u8 counter
= 0, checksum
;
229 /* Get short file name checksum value */
230 checksum
= mkcksum(itr
->dent
->name
, itr
->dent
->ext
);
233 memset(slotptr
, 0x00, sizeof(dir_slot
));
234 ret
= str2slot(slotptr
, l_name
, &idx
);
235 slotptr
->id
= ++counter
;
236 slotptr
->attr
= ATTR_VFAT
;
237 slotptr
->alias_checksum
= checksum
;
242 slotptr
->id
|= LAST_LONG_ENTRY_MASK
;
244 while (counter
>= 1) {
245 memcpy(itr
->dent
, slotptr
, sizeof(dir_slot
));
249 if (itr
->remaining
== 0)
252 /* allocate a cluster for more entries */
253 if (!fat_itr_next(itr
))
255 (!itr
->is_root
|| itr
->fsdata
->fatsize
== 32) &&
264 * Set the entry at index 'entry' in a FAT (12/16/32) table.
266 static int set_fatent_value(fsdata
*mydata
, __u32 entry
, __u32 entry_value
)
268 __u32 bufnum
, offset
, off16
;
271 switch (mydata
->fatsize
) {
273 bufnum
= entry
/ FAT32BUFSIZE
;
274 offset
= entry
- bufnum
* FAT32BUFSIZE
;
277 bufnum
= entry
/ FAT16BUFSIZE
;
278 offset
= entry
- bufnum
* FAT16BUFSIZE
;
281 bufnum
= entry
/ FAT12BUFSIZE
;
282 offset
= entry
- bufnum
* FAT12BUFSIZE
;
285 /* Unsupported FAT size */
289 /* Read a new block of FAT entries into the cache. */
290 if (bufnum
!= mydata
->fatbufnum
) {
291 int getsize
= FATBUFBLOCKS
;
292 __u8
*bufptr
= mydata
->fatbuf
;
293 __u32 fatlength
= mydata
->fatlength
;
294 __u32 startblock
= bufnum
* FATBUFBLOCKS
;
296 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
297 if (startblock
+ getsize
> fatlength
)
298 getsize
= fatlength
- startblock
;
300 if (flush_dirty_fat_buffer(mydata
) < 0)
303 startblock
+= mydata
->fat_sect
;
305 if (disk_read(startblock
, getsize
, bufptr
) < 0) {
306 debug("Error reading FAT blocks\n");
309 mydata
->fatbufnum
= bufnum
;
313 mydata
->fat_dirty
= 1;
315 /* Set the actual entry */
316 switch (mydata
->fatsize
) {
318 ((__u32
*) mydata
->fatbuf
)[offset
] = cpu_to_le32(entry_value
);
321 ((__u16
*) mydata
->fatbuf
)[offset
] = cpu_to_le16(entry_value
);
324 off16
= (offset
* 3) / 4;
326 switch (offset
& 0x3) {
328 val1
= cpu_to_le16(entry_value
) & 0xfff;
329 ((__u16
*)mydata
->fatbuf
)[off16
] &= ~0xfff;
330 ((__u16
*)mydata
->fatbuf
)[off16
] |= val1
;
333 val1
= cpu_to_le16(entry_value
) & 0xf;
334 val2
= (cpu_to_le16(entry_value
) >> 4) & 0xff;
336 ((__u16
*)mydata
->fatbuf
)[off16
] &= ~0xf000;
337 ((__u16
*)mydata
->fatbuf
)[off16
] |= (val1
<< 12);
339 ((__u16
*)mydata
->fatbuf
)[off16
+ 1] &= ~0xff;
340 ((__u16
*)mydata
->fatbuf
)[off16
+ 1] |= val2
;
343 val1
= cpu_to_le16(entry_value
) & 0xff;
344 val2
= (cpu_to_le16(entry_value
) >> 8) & 0xf;
346 ((__u16
*)mydata
->fatbuf
)[off16
] &= ~0xff00;
347 ((__u16
*)mydata
->fatbuf
)[off16
] |= (val1
<< 8);
349 ((__u16
*)mydata
->fatbuf
)[off16
+ 1] &= ~0xf;
350 ((__u16
*)mydata
->fatbuf
)[off16
+ 1] |= val2
;
353 val1
= cpu_to_le16(entry_value
) & 0xfff;
354 ((__u16
*)mydata
->fatbuf
)[off16
] &= ~0xfff0;
355 ((__u16
*)mydata
->fatbuf
)[off16
] |= (val1
<< 4);
370 * Determine the next free cluster after 'entry' in a FAT (12/16/32) table
371 * and link it to 'entry'. EOC marker is not set on returned entry.
373 static __u32
determine_fatent(fsdata
*mydata
, __u32 entry
)
375 __u32 next_fat
, next_entry
= entry
+ 1;
378 next_fat
= get_fatent(mydata
, next_entry
);
380 /* found free entry, link to entry */
381 set_fatent_value(mydata
, entry
, next_entry
);
386 debug("FAT%d: entry: %08x, entry_value: %04x\n",
387 mydata
->fatsize
, entry
, next_entry
);
393 * set_sectors() - write data to sectors
395 * Write 'size' bytes from 'buffer' into the specified sector.
397 * @mydata: data to be written
398 * @startsect: sector to be written to
399 * @buffer: data to be written
400 * @size: bytes to be written (but not more than the size of a cluster)
401 * Return: 0 on success, -1 otherwise
404 set_sectors(fsdata
*mydata
, u32 startsect
, u8
*buffer
, u32 size
)
409 debug("startsect: %d\n", startsect
);
411 if ((unsigned long)buffer
& (ARCH_DMA_MINALIGN
- 1)) {
412 ALLOC_CACHE_ALIGN_BUFFER(__u8
, tmpbuf
, mydata
->sect_size
);
414 debug("FAT: Misaligned buffer address (%p)\n", buffer
);
416 while (size
>= mydata
->sect_size
) {
417 memcpy(tmpbuf
, buffer
, mydata
->sect_size
);
418 ret
= disk_write(startsect
++, 1, tmpbuf
);
420 debug("Error writing data (got %d)\n", ret
);
424 buffer
+= mydata
->sect_size
;
425 size
-= mydata
->sect_size
;
427 } else if (size
>= mydata
->sect_size
) {
428 nsects
= size
/ mydata
->sect_size
;
429 ret
= disk_write(startsect
, nsects
, buffer
);
431 debug("Error writing data (got %d)\n", ret
);
436 buffer
+= nsects
* mydata
->sect_size
;
437 size
-= nsects
* mydata
->sect_size
;
441 ALLOC_CACHE_ALIGN_BUFFER(__u8
, tmpbuf
, mydata
->sect_size
);
442 /* Do not leak content of stack */
443 memset(tmpbuf
, 0, mydata
->sect_size
);
444 memcpy(tmpbuf
, buffer
, size
);
445 ret
= disk_write(startsect
, 1, tmpbuf
);
447 debug("Error writing data (got %d)\n", ret
);
456 * set_cluster() - write data to cluster
458 * Write 'size' bytes from 'buffer' into the specified cluster.
460 * @mydata: data to be written
461 * @clustnum: cluster to be written to
462 * @buffer: data to be written
463 * @size: bytes to be written (but not more than the size of a cluster)
464 * Return: 0 on success, -1 otherwise
467 set_cluster(fsdata
*mydata
, u32 clustnum
, u8
*buffer
, u32 size
)
469 return set_sectors(mydata
, clust_to_sect(mydata
, clustnum
),
474 flush_dir(fat_itr
*itr
)
476 fsdata
*mydata
= itr
->fsdata
;
477 u32 startsect
, sect_offset
, nsects
;
479 if (!itr
->is_root
|| mydata
->fatsize
== 32)
480 return set_cluster(mydata
, itr
->clust
, itr
->block
,
481 mydata
->clust_size
* mydata
->sect_size
);
483 sect_offset
= itr
->clust
* mydata
->clust_size
;
484 startsect
= mydata
->rootdir_sect
+ sect_offset
;
485 /* do not write past the end of rootdir */
486 nsects
= min_t(u32
, mydata
->clust_size
,
487 mydata
->rootdir_size
- sect_offset
);
489 return set_sectors(mydata
, startsect
, itr
->block
,
490 nsects
* mydata
->sect_size
);
493 static __u8 tmpbuf_cluster
[MAX_CLUSTSIZE
] __aligned(ARCH_DMA_MINALIGN
);
496 * Read and modify data on existing and consecutive cluster blocks
499 get_set_cluster(fsdata
*mydata
, __u32 clustnum
, loff_t pos
, __u8
*buffer
,
500 loff_t size
, loff_t
*gotsize
)
502 unsigned int bytesperclust
= mydata
->clust_size
* mydata
->sect_size
;
505 int clustcount
, i
, ret
;
511 assert(pos
< bytesperclust
);
512 startsect
= clust_to_sect(mydata
, clustnum
);
514 debug("clustnum: %d, startsect: %d, pos: %lld\n",
515 clustnum
, startsect
, pos
);
517 /* partial write at beginning */
519 wsize
= min(bytesperclust
- pos
, size
);
520 ret
= disk_read(startsect
, mydata
->clust_size
, tmpbuf_cluster
);
521 if (ret
!= mydata
->clust_size
) {
522 debug("Error reading data (got %d)\n", ret
);
526 memcpy(tmpbuf_cluster
+ pos
, buffer
, wsize
);
527 ret
= disk_write(startsect
, mydata
->clust_size
, tmpbuf_cluster
);
528 if (ret
!= mydata
->clust_size
) {
529 debug("Error writing data (got %d)\n", ret
);
537 startsect
+= mydata
->clust_size
;
543 /* full-cluster write */
544 if (size
>= bytesperclust
) {
545 clustcount
= lldiv(size
, bytesperclust
);
547 if (!((unsigned long)buffer
& (ARCH_DMA_MINALIGN
- 1))) {
548 wsize
= clustcount
* bytesperclust
;
549 ret
= disk_write(startsect
,
550 clustcount
* mydata
->clust_size
,
552 if (ret
!= clustcount
* mydata
->clust_size
) {
553 debug("Error writing data (got %d)\n", ret
);
561 startsect
+= clustcount
* mydata
->clust_size
;
563 for (i
= 0; i
< clustcount
; i
++) {
564 memcpy(tmpbuf_cluster
, buffer
, bytesperclust
);
565 ret
= disk_write(startsect
,
568 if (ret
!= mydata
->clust_size
) {
569 debug("Error writing data (got %d)\n",
574 size
-= bytesperclust
;
575 buffer
+= bytesperclust
;
576 *gotsize
+= bytesperclust
;
578 startsect
+= mydata
->clust_size
;
583 /* partial write at end */
586 ret
= disk_read(startsect
, mydata
->clust_size
, tmpbuf_cluster
);
587 if (ret
!= mydata
->clust_size
) {
588 debug("Error reading data (got %d)\n", ret
);
591 memcpy(tmpbuf_cluster
, buffer
, wsize
);
592 ret
= disk_write(startsect
, mydata
->clust_size
, tmpbuf_cluster
);
593 if (ret
!= mydata
->clust_size
) {
594 debug("Error writing data (got %d)\n", ret
);
609 * Find the first empty cluster
611 static int find_empty_cluster(fsdata
*mydata
)
613 __u32 fat_val
, entry
= 3;
616 fat_val
= get_fatent(mydata
, entry
);
626 * Allocate a cluster for additional directory entries
628 static int new_dir_table(fat_itr
*itr
)
630 fsdata
*mydata
= itr
->fsdata
;
631 int dir_newclust
= 0;
632 unsigned int bytesperclust
= mydata
->clust_size
* mydata
->sect_size
;
634 dir_newclust
= find_empty_cluster(mydata
);
635 set_fatent_value(mydata
, itr
->clust
, dir_newclust
);
636 if (mydata
->fatsize
== 32)
637 set_fatent_value(mydata
, dir_newclust
, 0xffffff8);
638 else if (mydata
->fatsize
== 16)
639 set_fatent_value(mydata
, dir_newclust
, 0xfff8);
640 else if (mydata
->fatsize
== 12)
641 set_fatent_value(mydata
, dir_newclust
, 0xff8);
643 itr
->clust
= dir_newclust
;
644 itr
->next_clust
= dir_newclust
;
646 if (flush_dirty_fat_buffer(mydata
) < 0)
649 memset(itr
->block
, 0x00, bytesperclust
);
651 itr
->dent
= (dir_entry
*)itr
->block
;
652 itr
->last_cluster
= 1;
653 itr
->remaining
= bytesperclust
/ sizeof(dir_entry
) - 1;
659 * Set empty cluster from 'entry' to the end of a file
661 static int clear_fatent(fsdata
*mydata
, __u32 entry
)
665 while (!CHECK_CLUST(entry
, mydata
->fatsize
)) {
666 fat_val
= get_fatent(mydata
, entry
);
668 set_fatent_value(mydata
, entry
, 0);
675 /* Flush fat buffer */
676 if (flush_dirty_fat_buffer(mydata
) < 0)
683 * Set start cluster in directory entry
685 static void set_start_cluster(const fsdata
*mydata
, dir_entry
*dentptr
,
688 if (mydata
->fatsize
== 32)
690 cpu_to_le16((start_cluster
& 0xffff0000) >> 16);
691 dentptr
->start
= cpu_to_le16(start_cluster
& 0xffff);
695 * Check whether adding a file makes the file system to
696 * exceed the size of the block device
697 * Return -1 when overflow occurs, otherwise return 0
699 static int check_overflow(fsdata
*mydata
, __u32 clustnum
, loff_t size
)
701 __u32 startsect
, sect_num
, offset
;
704 startsect
= clust_to_sect(mydata
, clustnum
);
706 startsect
= mydata
->rootdir_sect
;
708 sect_num
= div_u64_rem(size
, mydata
->sect_size
, &offset
);
713 if (startsect
+ sect_num
> total_sector
)
719 * Write at most 'maxsize' bytes from 'buffer' into
720 * the file associated with 'dentptr'
721 * Update the number of bytes written in *gotsize and return 0
722 * or return -1 on fatal errors.
725 set_contents(fsdata
*mydata
, dir_entry
*dentptr
, loff_t pos
, __u8
*buffer
,
726 loff_t maxsize
, loff_t
*gotsize
)
728 unsigned int bytesperclust
= mydata
->clust_size
* mydata
->sect_size
;
729 __u32 curclust
= START(dentptr
);
730 __u32 endclust
= 0, newclust
= 0;
731 u64 cur_pos
, filesize
;
732 loff_t offset
, actsize
, wsize
;
735 filesize
= pos
+ maxsize
;
737 debug("%llu bytes\n", filesize
);
742 if (!CHECK_CLUST(curclust
, mydata
->fatsize
) ||
743 IS_LAST_CLUST(curclust
, mydata
->fatsize
)) {
744 clear_fatent(mydata
, curclust
);
745 set_start_cluster(mydata
, dentptr
, 0);
748 debug("curclust: 0x%x\n", curclust
);
749 debug("Invalid FAT entry\n");
758 /* go to cluster at pos */
759 cur_pos
= bytesperclust
;
763 if (IS_LAST_CLUST(curclust
, mydata
->fatsize
))
766 newclust
= get_fatent(mydata
, curclust
);
767 if (!IS_LAST_CLUST(newclust
, mydata
->fatsize
) &&
768 CHECK_CLUST(newclust
, mydata
->fatsize
)) {
769 debug("curclust: 0x%x\n", curclust
);
770 debug("Invalid FAT entry\n");
774 cur_pos
+= bytesperclust
;
777 if (IS_LAST_CLUST(curclust
, mydata
->fatsize
)) {
778 assert(pos
== cur_pos
);
782 assert(pos
< cur_pos
);
783 cur_pos
-= bytesperclust
;
786 assert(IS_LAST_CLUST(curclust
, mydata
->fatsize
) ||
787 !CHECK_CLUST(curclust
, mydata
->fatsize
));
790 /* search for allocated consecutive clusters */
791 actsize
= bytesperclust
;
794 if (filesize
<= (cur_pos
+ actsize
))
797 newclust
= get_fatent(mydata
, endclust
);
799 if (newclust
!= endclust
+ 1)
801 if (IS_LAST_CLUST(newclust
, mydata
->fatsize
))
803 if (CHECK_CLUST(newclust
, mydata
->fatsize
)) {
804 debug("curclust: 0x%x\n", curclust
);
805 debug("Invalid FAT entry\n");
809 actsize
+= bytesperclust
;
813 /* overwrite to <curclust..endclust> */
817 offset
= pos
- cur_pos
;
818 wsize
= min_t(unsigned long long, actsize
, filesize
- cur_pos
);
821 if (get_set_cluster(mydata
, curclust
, offset
,
822 buffer
, wsize
, &actsize
)) {
823 printf("Error get-and-setting cluster\n");
828 cur_pos
+= offset
+ wsize
;
830 if (filesize
<= cur_pos
)
833 if (IS_LAST_CLUST(newclust
, mydata
->fatsize
))
834 /* no more clusters */
840 if (filesize
<= cur_pos
) {
842 newclust
= get_fatent(mydata
, endclust
);
843 if (!IS_LAST_CLUST(newclust
, mydata
->fatsize
)) {
844 /* truncate the rest */
845 clear_fatent(mydata
, newclust
);
847 /* Mark end of file in FAT */
848 if (mydata
->fatsize
== 12)
850 else if (mydata
->fatsize
== 16)
852 else if (mydata
->fatsize
== 32)
853 newclust
= 0xfffffff;
854 set_fatent_value(mydata
, endclust
, newclust
);
862 assert(!do_div(cur_pos
, bytesperclust
));
865 /* allocate and write */
868 /* Assure that curclust is valid */
870 curclust
= find_empty_cluster(mydata
);
871 set_start_cluster(mydata
, dentptr
, curclust
);
873 newclust
= get_fatent(mydata
, curclust
);
875 if (IS_LAST_CLUST(newclust
, mydata
->fatsize
)) {
876 newclust
= determine_fatent(mydata
, curclust
);
877 set_fatent_value(mydata
, curclust
, newclust
);
880 debug("error: something wrong\n");
885 /* TODO: already partially written */
886 if (check_overflow(mydata
, curclust
, filesize
)) {
887 printf("Error: no space left: %llu\n", filesize
);
891 actsize
= bytesperclust
;
894 /* search for consecutive clusters */
895 while (actsize
< filesize
) {
896 newclust
= determine_fatent(mydata
, endclust
);
898 if ((newclust
- 1) != endclust
)
899 /* write to <curclust..endclust> */
902 if (CHECK_CLUST(newclust
, mydata
->fatsize
)) {
903 debug("newclust: 0x%x\n", newclust
);
904 debug("Invalid FAT entry\n");
908 actsize
+= bytesperclust
;
911 /* set remaining bytes */
913 if (set_cluster(mydata
, curclust
, buffer
, (u32
)actsize
) != 0) {
914 debug("error: writing cluster\n");
919 /* Mark end of file in FAT */
920 if (mydata
->fatsize
== 12)
922 else if (mydata
->fatsize
== 16)
924 else if (mydata
->fatsize
== 32)
925 newclust
= 0xfffffff;
926 set_fatent_value(mydata
, endclust
, newclust
);
930 if (set_cluster(mydata
, curclust
, buffer
, (u32
)actsize
) != 0) {
931 debug("error: writing cluster\n");
938 if (CHECK_CLUST(newclust
, mydata
->fatsize
)) {
939 debug("newclust: 0x%x\n", newclust
);
940 debug("Invalid FAT entry\n");
943 actsize
= bytesperclust
;
944 curclust
= endclust
= newclust
;
953 static void fill_dentry(fsdata
*mydata
, dir_entry
*dentptr
,
954 const char *filename
, __u32 start_cluster
, __u32 size
, __u8 attr
)
956 set_start_cluster(mydata
, dentptr
, start_cluster
);
957 dentptr
->size
= cpu_to_le32(size
);
959 dentptr
->attr
= attr
;
961 set_name(dentptr
, filename
);
965 * Find a directory entry based on filename or start cluster number
966 * If the directory entry is not found,
967 * the new position for writing a directory entry will be returned
969 static dir_entry
*find_directory_entry(fat_itr
*itr
, char *filename
)
973 while (fat_itr_next(itr
)) {
974 /* check both long and short name: */
975 if (!strcasecmp(filename
, itr
->name
))
977 else if (itr
->name
!= itr
->s_name
&&
978 !strcasecmp(filename
, itr
->s_name
))
984 if (itr
->dent
->name
[0] == '\0')
990 /* allocate a cluster for more entries */
992 (!itr
->is_root
|| itr
->fsdata
->fatsize
== 32) &&
994 /* indicate that allocating dent failed */
1000 static int split_filename(char *filename
, char **dirname
, char **basename
)
1002 char *p
, *last_slash
, *last_slash_cont
;
1007 last_slash_cont
= NULL
;
1009 if (ISDIRDELIM(*p
)) {
1011 last_slash_cont
= p
;
1012 /* continuous slashes */
1013 while (ISDIRDELIM(*p
))
1014 last_slash_cont
= p
++;
1022 if (last_slash_cont
== (filename
+ strlen(filename
) - 1)) {
1023 /* remove trailing slashes */
1028 if (last_slash
== filename
) {
1029 /* avoid ""(null) directory */
1033 *dirname
= filename
;
1036 *last_slash_cont
= '\0';
1037 *basename
= last_slash_cont
+ 1;
1039 *dirname
= "/"; /* root by default */
1040 *basename
= filename
;
1047 * normalize_longname() - check long file name and convert to lower case
1049 * We assume here that the FAT file system is using an 8bit code page.
1050 * Linux typically uses CP437, EDK2 assumes CP1250.
1052 * @l_filename: preallocated buffer receiving the normalized name
1053 * @filename: filename to normalize
1054 * Return: 0 on success, -1 on failure
1056 static int normalize_longname(char *l_filename
, const char *filename
)
1058 const char *p
, illegal
[] = "<>:\"/\\|?*";
1060 if (strlen(filename
) >= VFAT_MAXLEN_BYTES
)
1063 for (p
= filename
; *p
; ++p
) {
1064 if ((unsigned char)*p
< 0x20)
1066 if (strchr(illegal
, *p
))
1070 strcpy(l_filename
, filename
);
1071 downcase(l_filename
, VFAT_MAXLEN_BYTES
);
1076 int file_fat_write_at(const char *filename
, loff_t pos
, void *buffer
,
1077 loff_t size
, loff_t
*actwrite
)
1080 fsdata datablock
= { .fatbuf
= NULL
, };
1081 fsdata
*mydata
= &datablock
;
1082 fat_itr
*itr
= NULL
;
1084 char *filename_copy
, *parent
, *basename
;
1085 char l_filename
[VFAT_MAXLEN_BYTES
];
1087 debug("writing %s\n", filename
);
1089 filename_copy
= strdup(filename
);
1093 split_filename(filename_copy
, &parent
, &basename
);
1094 if (!strlen(basename
)) {
1099 filename
= basename
;
1100 if (normalize_longname(l_filename
, filename
)) {
1101 printf("FAT: illegal filename (%s)\n", filename
);
1106 itr
= malloc_cache_aligned(sizeof(fat_itr
));
1112 ret
= fat_itr_root(itr
, &datablock
);
1116 total_sector
= datablock
.total_sect
;
1118 ret
= fat_itr_resolve(itr
, parent
, TYPE_DIR
);
1120 printf("%s: doesn't exist (%d)\n", parent
, ret
);
1124 retdent
= find_directory_entry(itr
, l_filename
);
1127 if (fat_itr_isdir(itr
)) {
1134 /* Append to the end */
1135 pos
= FAT2CPU32(retdent
->size
);
1136 if (pos
> retdent
->size
) {
1137 /* No hole allowed */
1142 /* Update file size in a directory entry */
1143 retdent
->size
= cpu_to_le32(pos
+ size
);
1145 /* Create a new file */
1148 /* root dir cannot have "." or ".." */
1149 if (!strcmp(l_filename
, ".") ||
1150 !strcmp(l_filename
, "..")) {
1157 printf("Error: allocating new dir entry\n");
1163 /* No hole allowed */
1168 memset(itr
->dent
, 0, sizeof(*itr
->dent
));
1170 /* Calculate checksum for short name */
1171 set_name(itr
->dent
, filename
);
1173 /* Set long name entries */
1174 if (fill_dir_slot(itr
, filename
)) {
1179 /* Set short name entry */
1180 fill_dentry(itr
->fsdata
, itr
->dent
, filename
, 0, size
, 0x20);
1182 retdent
= itr
->dent
;
1185 ret
= set_contents(mydata
, retdent
, pos
, buffer
, size
, actwrite
);
1187 printf("Error: writing contents\n");
1191 debug("attempt to write 0x%llx bytes\n", *actwrite
);
1193 /* Flush fat buffer */
1194 ret
= flush_dirty_fat_buffer(mydata
);
1196 printf("Error: flush fat buffer\n");
1201 /* Write directory table to device */
1202 ret
= flush_dir(itr
);
1204 printf("Error: writing directory entry\n");
1209 free(filename_copy
);
1210 free(mydata
->fatbuf
);
1215 int file_fat_write(const char *filename
, void *buffer
, loff_t offset
,
1216 loff_t maxsize
, loff_t
*actwrite
)
1218 return file_fat_write_at(filename
, offset
, buffer
, maxsize
, actwrite
);
1221 static int fat_dir_entries(fat_itr
*itr
)
1224 fsdata fsdata
= { .fatbuf
= NULL
, }, *mydata
= &fsdata
;
1225 /* for FATBUFSIZE */
1228 dirs
= malloc_cache_aligned(sizeof(fat_itr
));
1230 debug("Error: allocating memory\n");
1235 /* duplicate fsdata */
1236 fat_itr_child(dirs
, itr
);
1237 fsdata
= *dirs
->fsdata
;
1239 /* allocate local fat buffer */
1240 fsdata
.fatbuf
= malloc_cache_aligned(FATBUFSIZE
);
1241 if (!fsdata
.fatbuf
) {
1242 debug("Error: allocating memory\n");
1246 fsdata
.fatbufnum
= -1;
1247 dirs
->fsdata
= &fsdata
;
1249 for (count
= 0; fat_itr_next(dirs
); count
++)
1253 free(fsdata
.fatbuf
);
1258 static int delete_dentry(fat_itr
*itr
)
1260 fsdata
*mydata
= itr
->fsdata
;
1261 dir_entry
*dentptr
= itr
->dent
;
1263 /* free cluster blocks */
1264 clear_fatent(mydata
, START(dentptr
));
1265 if (flush_dirty_fat_buffer(mydata
) < 0) {
1266 printf("Error: flush fat buffer\n");
1271 * update a directory entry
1273 * - long file name support
1274 * - find and mark the "new" first invalid entry as name[0]=0x00
1276 memset(dentptr
, 0, sizeof(*dentptr
));
1277 dentptr
->name
[0] = 0xe5;
1279 if (flush_dir(itr
)) {
1280 printf("error: writing directory entry\n");
1287 int fat_unlink(const char *filename
)
1289 fsdata fsdata
= { .fatbuf
= NULL
, };
1290 fat_itr
*itr
= NULL
;
1292 char *filename_copy
, *dirname
, *basename
;
1294 filename_copy
= strdup(filename
);
1295 if (!filename_copy
) {
1296 printf("Error: allocating memory\n");
1300 split_filename(filename_copy
, &dirname
, &basename
);
1302 if (!strcmp(dirname
, "/") && !strcmp(basename
, "")) {
1303 printf("Error: cannot remove root\n");
1308 itr
= malloc_cache_aligned(sizeof(fat_itr
));
1310 printf("Error: allocating memory\n");
1315 ret
= fat_itr_root(itr
, &fsdata
);
1319 total_sector
= fsdata
.total_sect
;
1321 ret
= fat_itr_resolve(itr
, dirname
, TYPE_DIR
);
1323 printf("%s: doesn't exist (%d)\n", dirname
, ret
);
1328 if (!find_directory_entry(itr
, basename
)) {
1329 printf("%s: doesn't exist\n", basename
);
1334 if (fat_itr_isdir(itr
)) {
1335 n_entries
= fat_dir_entries(itr
);
1336 if (n_entries
< 0) {
1340 if (n_entries
> 2) {
1341 printf("Error: directory is not empty: %d\n",
1348 ret
= delete_dentry(itr
);
1351 free(fsdata
.fatbuf
);
1353 free(filename_copy
);
1358 int fat_mkdir(const char *new_dirname
)
1361 fsdata datablock
= { .fatbuf
= NULL
, };
1362 fsdata
*mydata
= &datablock
;
1363 fat_itr
*itr
= NULL
;
1364 char *dirname_copy
, *parent
, *dirname
;
1365 char l_dirname
[VFAT_MAXLEN_BYTES
];
1368 unsigned int bytesperclust
;
1369 dir_entry
*dotdent
= NULL
;
1371 dirname_copy
= strdup(new_dirname
);
1375 split_filename(dirname_copy
, &parent
, &dirname
);
1376 if (!strlen(dirname
)) {
1381 if (normalize_longname(l_dirname
, dirname
)) {
1382 printf("FAT: illegal filename (%s)\n", dirname
);
1387 itr
= malloc_cache_aligned(sizeof(fat_itr
));
1393 ret
= fat_itr_root(itr
, &datablock
);
1397 total_sector
= datablock
.total_sect
;
1399 ret
= fat_itr_resolve(itr
, parent
, TYPE_DIR
);
1401 printf("%s: doesn't exist (%d)\n", parent
, ret
);
1405 retdent
= find_directory_entry(itr
, l_dirname
);
1408 printf("%s: already exists\n", l_dirname
);
1413 /* root dir cannot have "." or ".." */
1414 if (!strcmp(l_dirname
, ".") ||
1415 !strcmp(l_dirname
, "..")) {
1422 printf("Error: allocating new dir entry\n");
1427 memset(itr
->dent
, 0, sizeof(*itr
->dent
));
1429 /* Set short name to set alias checksum field in dir_slot */
1430 set_name(itr
->dent
, dirname
);
1431 fill_dir_slot(itr
, dirname
);
1433 /* Set attribute as archive for regular file */
1434 fill_dentry(itr
->fsdata
, itr
->dent
, dirname
, 0, 0,
1435 ATTR_DIR
| ATTR_ARCH
);
1437 retdent
= itr
->dent
;
1440 /* Default entries */
1441 bytesperclust
= mydata
->clust_size
* mydata
->sect_size
;
1442 dotdent
= malloc_cache_aligned(bytesperclust
);
1447 memset(dotdent
, 0, bytesperclust
);
1449 memcpy(dotdent
[0].name
, ". ", 8);
1450 memcpy(dotdent
[0].ext
, " ", 3);
1451 dotdent
[0].attr
= ATTR_DIR
| ATTR_ARCH
;
1453 memcpy(dotdent
[1].name
, ".. ", 8);
1454 memcpy(dotdent
[1].ext
, " ", 3);
1455 dotdent
[1].attr
= ATTR_DIR
| ATTR_ARCH
;
1456 set_start_cluster(mydata
, &dotdent
[1], itr
->start_clust
);
1458 ret
= set_contents(mydata
, retdent
, 0, (__u8
*)dotdent
,
1459 bytesperclust
, &actwrite
);
1461 printf("Error: writing contents\n");
1464 /* Write twice for "." */
1465 set_start_cluster(mydata
, &dotdent
[0], START(retdent
));
1466 ret
= set_contents(mydata
, retdent
, 0, (__u8
*)dotdent
,
1467 bytesperclust
, &actwrite
);
1469 printf("Error: writing contents\n");
1473 /* Flush fat buffer */
1474 ret
= flush_dirty_fat_buffer(mydata
);
1476 printf("Error: flush fat buffer\n");
1480 /* Write directory table to device */
1481 ret
= flush_dir(itr
);
1483 printf("Error: writing directory entry\n");
1487 free(mydata
->fatbuf
);