1 // SPDX-License-Identifier: GPL-2.0+
5 * R/W (V)FAT 12/16/32 filesystem implementation by Donggeun Kim
12 #include <asm/byteorder.h>
14 #include <linux/ctype.h>
16 #include <linux/math64.h>
19 static void uppercase(char *str
, int len
)
23 for (i
= 0; i
< len
; i
++) {
29 static int total_sector
;
30 static int disk_write(__u32 block
, __u32 nr_blocks
, void *buf
)
37 if (cur_part_info
.start
+ block
+ nr_blocks
>
38 cur_part_info
.start
+ total_sector
) {
39 printf("error: overflow occurs\n");
43 ret
= blk_dwrite(cur_dev
, cur_part_info
.start
+ block
, nr_blocks
, buf
);
44 if (nr_blocks
&& ret
== 0)
51 * Set short name in directory entry
53 static void set_name(dir_entry
*dirent
, const char *filename
)
55 char s_name
[VFAT_MAXLEN_BYTES
];
57 int period_location
, len
, i
, ext_num
;
62 len
= strlen(filename
);
66 strcpy(s_name
, filename
);
67 uppercase(s_name
, len
);
69 period
= strchr(s_name
, '.');
71 period_location
= len
;
74 period_location
= period
- s_name
;
75 ext_num
= len
- period_location
- 1;
78 /* Pad spaces when the length of file name is shorter than eight */
79 if (period_location
< 8) {
80 memcpy(dirent
->name
, s_name
, period_location
);
81 for (i
= period_location
; i
< 8; i
++)
82 dirent
->name
[i
] = ' ';
83 } else if (period_location
== 8) {
84 memcpy(dirent
->name
, s_name
, period_location
);
86 memcpy(dirent
->name
, s_name
, 6);
87 dirent
->name
[6] = '~';
88 dirent
->name
[7] = '1';
92 memcpy(dirent
->ext
, s_name
+ period_location
+ 1, ext_num
);
93 for (i
= ext_num
; i
< 3; i
++)
96 memcpy(dirent
->ext
, s_name
+ period_location
+ 1, 3);
98 debug("name : %s\n", dirent
->name
);
99 debug("ext : %s\n", dirent
->ext
);
103 * Write fat buffer into block device
105 static int flush_dirty_fat_buffer(fsdata
*mydata
)
107 int getsize
= FATBUFBLOCKS
;
108 __u32 fatlength
= mydata
->fatlength
;
109 __u8
*bufptr
= mydata
->fatbuf
;
110 __u32 startblock
= mydata
->fatbufnum
* FATBUFBLOCKS
;
112 debug("debug: evicting %d, dirty: %d\n", mydata
->fatbufnum
,
113 (int)mydata
->fat_dirty
);
115 if ((!mydata
->fat_dirty
) || (mydata
->fatbufnum
== -1))
118 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
119 if (startblock
+ getsize
> fatlength
)
120 getsize
= fatlength
- startblock
;
122 startblock
+= mydata
->fat_sect
;
125 if (disk_write(startblock
, getsize
, bufptr
) < 0) {
126 debug("error: writing FAT blocks\n");
130 if (mydata
->fats
== 2) {
131 /* Update corresponding second FAT blocks */
132 startblock
+= mydata
->fatlength
;
133 if (disk_write(startblock
, getsize
, bufptr
) < 0) {
134 debug("error: writing second FAT blocks\n");
138 mydata
->fat_dirty
= 0;
144 * Set the file name information from 'name' into 'slotptr',
146 static int str2slot(dir_slot
*slotptr
, const char *name
, int *idx
)
150 for (j
= 0; j
<= 8; j
+= 2) {
151 if (name
[*idx
] == 0x00) {
152 slotptr
->name0_4
[j
] = 0;
153 slotptr
->name0_4
[j
+ 1] = 0;
157 slotptr
->name0_4
[j
] = name
[*idx
];
161 for (j
= 0; j
<= 10; j
+= 2) {
162 if (name
[*idx
] == 0x00) {
163 slotptr
->name5_10
[j
] = 0;
164 slotptr
->name5_10
[j
+ 1] = 0;
168 slotptr
->name5_10
[j
] = name
[*idx
];
172 for (j
= 0; j
<= 2; j
+= 2) {
173 if (name
[*idx
] == 0x00) {
174 slotptr
->name11_12
[j
] = 0;
175 slotptr
->name11_12
[j
+ 1] = 0;
179 slotptr
->name11_12
[j
] = name
[*idx
];
184 if (name
[*idx
] == 0x00)
188 /* Not used characters are filled with 0xff 0xff */
190 for (; end_idx
< 5; end_idx
++) {
191 slotptr
->name0_4
[end_idx
* 2] = 0xff;
192 slotptr
->name0_4
[end_idx
* 2 + 1] = 0xff;
197 for (; end_idx
< 6; end_idx
++) {
198 slotptr
->name5_10
[end_idx
* 2] = 0xff;
199 slotptr
->name5_10
[end_idx
* 2 + 1] = 0xff;
204 for (; end_idx
< 2; end_idx
++) {
205 slotptr
->name11_12
[end_idx
* 2] = 0xff;
206 slotptr
->name11_12
[end_idx
* 2 + 1] = 0xff;
212 static int new_dir_table(fat_itr
*itr
);
213 static int flush_dir(fat_itr
*itr
);
216 * Fill dir_slot entries with appropriate name, id, and attr
217 * 'itr' will point to a next entry
220 fill_dir_slot(fat_itr
*itr
, const char *l_name
)
222 __u8 temp_dir_slot_buffer
[MAX_LFN_SLOT
* sizeof(dir_slot
)];
223 dir_slot
*slotptr
= (dir_slot
*)temp_dir_slot_buffer
;
224 __u8 counter
= 0, checksum
;
227 /* Get short file name checksum value */
228 checksum
= mkcksum(itr
->dent
->name
, itr
->dent
->ext
);
231 memset(slotptr
, 0x00, sizeof(dir_slot
));
232 ret
= str2slot(slotptr
, l_name
, &idx
);
233 slotptr
->id
= ++counter
;
234 slotptr
->attr
= ATTR_VFAT
;
235 slotptr
->alias_checksum
= checksum
;
240 slotptr
->id
|= LAST_LONG_ENTRY_MASK
;
242 while (counter
>= 1) {
243 memcpy(itr
->dent
, slotptr
, sizeof(dir_slot
));
247 if (itr
->remaining
== 0)
250 if (!fat_itr_next(itr
))
251 if (!itr
->dent
&& !itr
->is_root
&& new_dir_table(itr
))
259 * Set the entry at index 'entry' in a FAT (12/16/32) table.
261 static int set_fatent_value(fsdata
*mydata
, __u32 entry
, __u32 entry_value
)
263 __u32 bufnum
, offset
, off16
;
266 switch (mydata
->fatsize
) {
268 bufnum
= entry
/ FAT32BUFSIZE
;
269 offset
= entry
- bufnum
* FAT32BUFSIZE
;
272 bufnum
= entry
/ FAT16BUFSIZE
;
273 offset
= entry
- bufnum
* FAT16BUFSIZE
;
276 bufnum
= entry
/ FAT12BUFSIZE
;
277 offset
= entry
- bufnum
* FAT12BUFSIZE
;
280 /* Unsupported FAT size */
284 /* Read a new block of FAT entries into the cache. */
285 if (bufnum
!= mydata
->fatbufnum
) {
286 int getsize
= FATBUFBLOCKS
;
287 __u8
*bufptr
= mydata
->fatbuf
;
288 __u32 fatlength
= mydata
->fatlength
;
289 __u32 startblock
= bufnum
* FATBUFBLOCKS
;
291 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
292 if (startblock
+ getsize
> fatlength
)
293 getsize
= fatlength
- startblock
;
295 if (flush_dirty_fat_buffer(mydata
) < 0)
298 startblock
+= mydata
->fat_sect
;
300 if (disk_read(startblock
, getsize
, bufptr
) < 0) {
301 debug("Error reading FAT blocks\n");
304 mydata
->fatbufnum
= bufnum
;
308 mydata
->fat_dirty
= 1;
310 /* Set the actual entry */
311 switch (mydata
->fatsize
) {
313 ((__u32
*) mydata
->fatbuf
)[offset
] = cpu_to_le32(entry_value
);
316 ((__u16
*) mydata
->fatbuf
)[offset
] = cpu_to_le16(entry_value
);
319 off16
= (offset
* 3) / 4;
321 switch (offset
& 0x3) {
323 val1
= cpu_to_le16(entry_value
) & 0xfff;
324 ((__u16
*)mydata
->fatbuf
)[off16
] &= ~0xfff;
325 ((__u16
*)mydata
->fatbuf
)[off16
] |= val1
;
328 val1
= cpu_to_le16(entry_value
) & 0xf;
329 val2
= (cpu_to_le16(entry_value
) >> 4) & 0xff;
331 ((__u16
*)mydata
->fatbuf
)[off16
] &= ~0xf000;
332 ((__u16
*)mydata
->fatbuf
)[off16
] |= (val1
<< 12);
334 ((__u16
*)mydata
->fatbuf
)[off16
+ 1] &= ~0xff;
335 ((__u16
*)mydata
->fatbuf
)[off16
+ 1] |= val2
;
338 val1
= cpu_to_le16(entry_value
) & 0xff;
339 val2
= (cpu_to_le16(entry_value
) >> 8) & 0xf;
341 ((__u16
*)mydata
->fatbuf
)[off16
] &= ~0xff00;
342 ((__u16
*)mydata
->fatbuf
)[off16
] |= (val1
<< 8);
344 ((__u16
*)mydata
->fatbuf
)[off16
+ 1] &= ~0xf;
345 ((__u16
*)mydata
->fatbuf
)[off16
+ 1] |= val2
;
348 val1
= cpu_to_le16(entry_value
) & 0xfff;
349 ((__u16
*)mydata
->fatbuf
)[off16
] &= ~0xfff0;
350 ((__u16
*)mydata
->fatbuf
)[off16
] |= (val1
<< 4);
365 * Determine the next free cluster after 'entry' in a FAT (12/16/32) table
366 * and link it to 'entry'. EOC marker is not set on returned entry.
368 static __u32
determine_fatent(fsdata
*mydata
, __u32 entry
)
370 __u32 next_fat
, next_entry
= entry
+ 1;
373 next_fat
= get_fatent(mydata
, next_entry
);
375 /* found free entry, link to entry */
376 set_fatent_value(mydata
, entry
, next_entry
);
381 debug("FAT%d: entry: %08x, entry_value: %04x\n",
382 mydata
->fatsize
, entry
, next_entry
);
388 * set_sectors() - write data to sectors
390 * Write 'size' bytes from 'buffer' into the specified sector.
392 * @mydata: data to be written
393 * @startsect: sector to be written to
394 * @buffer: data to be written
395 * @size: bytes to be written (but not more than the size of a cluster)
396 * Return: 0 on success, -1 otherwise
399 set_sectors(fsdata
*mydata
, u32 startsect
, u8
*buffer
, u32 size
)
404 debug("startsect: %d\n", startsect
);
406 if ((unsigned long)buffer
& (ARCH_DMA_MINALIGN
- 1)) {
407 ALLOC_CACHE_ALIGN_BUFFER(__u8
, tmpbuf
, mydata
->sect_size
);
409 debug("FAT: Misaligned buffer address (%p)\n", buffer
);
411 while (size
>= mydata
->sect_size
) {
412 memcpy(tmpbuf
, buffer
, mydata
->sect_size
);
413 ret
= disk_write(startsect
++, 1, tmpbuf
);
415 debug("Error writing data (got %d)\n", ret
);
419 buffer
+= mydata
->sect_size
;
420 size
-= mydata
->sect_size
;
422 } else if (size
>= mydata
->sect_size
) {
423 nsects
= size
/ mydata
->sect_size
;
424 ret
= disk_write(startsect
, nsects
, buffer
);
426 debug("Error writing data (got %d)\n", ret
);
431 buffer
+= nsects
* mydata
->sect_size
;
432 size
-= nsects
* mydata
->sect_size
;
436 ALLOC_CACHE_ALIGN_BUFFER(__u8
, tmpbuf
, mydata
->sect_size
);
437 /* Do not leak content of stack */
438 memset(tmpbuf
, 0, mydata
->sect_size
);
439 memcpy(tmpbuf
, buffer
, size
);
440 ret
= disk_write(startsect
, 1, tmpbuf
);
442 debug("Error writing data (got %d)\n", ret
);
451 * set_cluster() - write data to cluster
453 * Write 'size' bytes from 'buffer' into the specified cluster.
455 * @mydata: data to be written
456 * @clustnum: cluster to be written to
457 * @buffer: data to be written
458 * @size: bytes to be written (but not more than the size of a cluster)
459 * Return: 0 on success, -1 otherwise
462 set_cluster(fsdata
*mydata
, u32 clustnum
, u8
*buffer
, u32 size
)
464 return set_sectors(mydata
, clust_to_sect(mydata
, clustnum
),
469 flush_dir(fat_itr
*itr
)
471 fsdata
*mydata
= itr
->fsdata
;
472 u32 startsect
, sect_offset
, nsects
;
474 if (!itr
->is_root
|| mydata
->fatsize
== 32)
475 return set_cluster(mydata
, itr
->clust
, itr
->block
,
476 mydata
->clust_size
* mydata
->sect_size
);
478 sect_offset
= itr
->clust
* mydata
->clust_size
;
479 startsect
= mydata
->rootdir_sect
+ sect_offset
;
480 /* do not write past the end of rootdir */
481 nsects
= min_t(u32
, mydata
->clust_size
,
482 mydata
->rootdir_size
- sect_offset
);
484 return set_sectors(mydata
, startsect
, itr
->block
,
485 nsects
* mydata
->sect_size
);
488 static __u8 tmpbuf_cluster
[MAX_CLUSTSIZE
] __aligned(ARCH_DMA_MINALIGN
);
491 * Read and modify data on existing and consecutive cluster blocks
494 get_set_cluster(fsdata
*mydata
, __u32 clustnum
, loff_t pos
, __u8
*buffer
,
495 loff_t size
, loff_t
*gotsize
)
497 unsigned int bytesperclust
= mydata
->clust_size
* mydata
->sect_size
;
500 int clustcount
, i
, ret
;
506 assert(pos
< bytesperclust
);
507 startsect
= clust_to_sect(mydata
, clustnum
);
509 debug("clustnum: %d, startsect: %d, pos: %lld\n",
510 clustnum
, startsect
, pos
);
512 /* partial write at beginning */
514 wsize
= min(bytesperclust
- pos
, size
);
515 ret
= disk_read(startsect
, mydata
->clust_size
, tmpbuf_cluster
);
516 if (ret
!= mydata
->clust_size
) {
517 debug("Error reading data (got %d)\n", ret
);
521 memcpy(tmpbuf_cluster
+ pos
, buffer
, wsize
);
522 ret
= disk_write(startsect
, mydata
->clust_size
, tmpbuf_cluster
);
523 if (ret
!= mydata
->clust_size
) {
524 debug("Error writing data (got %d)\n", ret
);
532 startsect
+= mydata
->clust_size
;
538 /* full-cluster write */
539 if (size
>= bytesperclust
) {
540 clustcount
= lldiv(size
, bytesperclust
);
542 if (!((unsigned long)buffer
& (ARCH_DMA_MINALIGN
- 1))) {
543 wsize
= clustcount
* bytesperclust
;
544 ret
= disk_write(startsect
,
545 clustcount
* mydata
->clust_size
,
547 if (ret
!= clustcount
* mydata
->clust_size
) {
548 debug("Error writing data (got %d)\n", ret
);
556 startsect
+= clustcount
* mydata
->clust_size
;
558 for (i
= 0; i
< clustcount
; i
++) {
559 memcpy(tmpbuf_cluster
, buffer
, bytesperclust
);
560 ret
= disk_write(startsect
,
563 if (ret
!= mydata
->clust_size
) {
564 debug("Error writing data (got %d)\n",
569 size
-= bytesperclust
;
570 buffer
+= bytesperclust
;
571 *gotsize
+= bytesperclust
;
573 startsect
+= mydata
->clust_size
;
578 /* partial write at end */
581 ret
= disk_read(startsect
, mydata
->clust_size
, tmpbuf_cluster
);
582 if (ret
!= mydata
->clust_size
) {
583 debug("Error reading data (got %d)\n", ret
);
586 memcpy(tmpbuf_cluster
, buffer
, wsize
);
587 ret
= disk_write(startsect
, mydata
->clust_size
, tmpbuf_cluster
);
588 if (ret
!= mydata
->clust_size
) {
589 debug("Error writing data (got %d)\n", ret
);
604 * Find the first empty cluster
606 static int find_empty_cluster(fsdata
*mydata
)
608 __u32 fat_val
, entry
= 3;
611 fat_val
= get_fatent(mydata
, entry
);
621 * Allocate a cluster for additional directory entries
623 static int new_dir_table(fat_itr
*itr
)
625 fsdata
*mydata
= itr
->fsdata
;
626 int dir_newclust
= 0;
627 unsigned int bytesperclust
= mydata
->clust_size
* mydata
->sect_size
;
629 dir_newclust
= find_empty_cluster(mydata
);
630 set_fatent_value(mydata
, itr
->clust
, dir_newclust
);
631 if (mydata
->fatsize
== 32)
632 set_fatent_value(mydata
, dir_newclust
, 0xffffff8);
633 else if (mydata
->fatsize
== 16)
634 set_fatent_value(mydata
, dir_newclust
, 0xfff8);
635 else if (mydata
->fatsize
== 12)
636 set_fatent_value(mydata
, dir_newclust
, 0xff8);
638 itr
->clust
= dir_newclust
;
639 itr
->next_clust
= dir_newclust
;
641 if (flush_dirty_fat_buffer(mydata
) < 0)
644 memset(itr
->block
, 0x00, bytesperclust
);
646 itr
->dent
= (dir_entry
*)itr
->block
;
647 itr
->last_cluster
= 1;
648 itr
->remaining
= bytesperclust
/ sizeof(dir_entry
) - 1;
654 * Set empty cluster from 'entry' to the end of a file
656 static int clear_fatent(fsdata
*mydata
, __u32 entry
)
660 while (!CHECK_CLUST(entry
, mydata
->fatsize
)) {
661 fat_val
= get_fatent(mydata
, entry
);
663 set_fatent_value(mydata
, entry
, 0);
670 /* Flush fat buffer */
671 if (flush_dirty_fat_buffer(mydata
) < 0)
678 * Set start cluster in directory entry
680 static void set_start_cluster(const fsdata
*mydata
, dir_entry
*dentptr
,
683 if (mydata
->fatsize
== 32)
685 cpu_to_le16((start_cluster
& 0xffff0000) >> 16);
686 dentptr
->start
= cpu_to_le16(start_cluster
& 0xffff);
690 * Check whether adding a file makes the file system to
691 * exceed the size of the block device
692 * Return -1 when overflow occurs, otherwise return 0
694 static int check_overflow(fsdata
*mydata
, __u32 clustnum
, loff_t size
)
696 __u32 startsect
, sect_num
, offset
;
699 startsect
= clust_to_sect(mydata
, clustnum
);
701 startsect
= mydata
->rootdir_sect
;
703 sect_num
= div_u64_rem(size
, mydata
->sect_size
, &offset
);
708 if (startsect
+ sect_num
> total_sector
)
714 * Write at most 'maxsize' bytes from 'buffer' into
715 * the file associated with 'dentptr'
716 * Update the number of bytes written in *gotsize and return 0
717 * or return -1 on fatal errors.
720 set_contents(fsdata
*mydata
, dir_entry
*dentptr
, loff_t pos
, __u8
*buffer
,
721 loff_t maxsize
, loff_t
*gotsize
)
723 unsigned int bytesperclust
= mydata
->clust_size
* mydata
->sect_size
;
724 __u32 curclust
= START(dentptr
);
725 __u32 endclust
= 0, newclust
= 0;
726 u64 cur_pos
, filesize
;
727 loff_t offset
, actsize
, wsize
;
730 filesize
= pos
+ maxsize
;
732 debug("%llu bytes\n", filesize
);
737 if (!CHECK_CLUST(curclust
, mydata
->fatsize
) ||
738 IS_LAST_CLUST(curclust
, mydata
->fatsize
)) {
739 clear_fatent(mydata
, curclust
);
740 set_start_cluster(mydata
, dentptr
, 0);
743 debug("curclust: 0x%x\n", curclust
);
744 debug("Invalid FAT entry\n");
753 /* go to cluster at pos */
754 cur_pos
= bytesperclust
;
758 if (IS_LAST_CLUST(curclust
, mydata
->fatsize
))
761 newclust
= get_fatent(mydata
, curclust
);
762 if (!IS_LAST_CLUST(newclust
, mydata
->fatsize
) &&
763 CHECK_CLUST(newclust
, mydata
->fatsize
)) {
764 debug("curclust: 0x%x\n", curclust
);
765 debug("Invalid FAT entry\n");
769 cur_pos
+= bytesperclust
;
772 if (IS_LAST_CLUST(curclust
, mydata
->fatsize
)) {
773 assert(pos
== cur_pos
);
777 assert(pos
< cur_pos
);
778 cur_pos
-= bytesperclust
;
781 assert(IS_LAST_CLUST(curclust
, mydata
->fatsize
) ||
782 !CHECK_CLUST(curclust
, mydata
->fatsize
));
785 /* search for allocated consecutive clusters */
786 actsize
= bytesperclust
;
789 if (filesize
<= (cur_pos
+ actsize
))
792 newclust
= get_fatent(mydata
, endclust
);
794 if (IS_LAST_CLUST(newclust
, mydata
->fatsize
))
796 if (CHECK_CLUST(newclust
, mydata
->fatsize
)) {
797 debug("curclust: 0x%x\n", curclust
);
798 debug("Invalid FAT entry\n");
802 actsize
+= bytesperclust
;
806 /* overwrite to <curclust..endclust> */
810 offset
= pos
- cur_pos
;
811 wsize
= min(cur_pos
+ actsize
, filesize
) - pos
;
812 if (get_set_cluster(mydata
, curclust
, offset
,
813 buffer
, wsize
, &actsize
)) {
814 printf("Error get-and-setting cluster\n");
819 cur_pos
+= offset
+ wsize
;
821 if (filesize
<= cur_pos
)
824 /* CHECK: newclust = get_fatent(mydata, endclust); */
826 if (IS_LAST_CLUST(newclust
, mydata
->fatsize
))
827 /* no more clusters */
833 if (filesize
<= cur_pos
) {
835 newclust
= get_fatent(mydata
, endclust
);
836 if (!IS_LAST_CLUST(newclust
, mydata
->fatsize
)) {
837 /* truncate the rest */
838 clear_fatent(mydata
, newclust
);
840 /* Mark end of file in FAT */
841 if (mydata
->fatsize
== 12)
843 else if (mydata
->fatsize
== 16)
845 else if (mydata
->fatsize
== 32)
846 newclust
= 0xfffffff;
847 set_fatent_value(mydata
, endclust
, newclust
);
855 assert(!do_div(cur_pos
, bytesperclust
));
858 /* allocate and write */
861 /* Assure that curclust is valid */
863 curclust
= find_empty_cluster(mydata
);
864 set_start_cluster(mydata
, dentptr
, curclust
);
866 newclust
= get_fatent(mydata
, curclust
);
868 if (IS_LAST_CLUST(newclust
, mydata
->fatsize
)) {
869 newclust
= determine_fatent(mydata
, curclust
);
870 set_fatent_value(mydata
, curclust
, newclust
);
873 debug("error: something wrong\n");
878 /* TODO: already partially written */
879 if (check_overflow(mydata
, curclust
, filesize
)) {
880 printf("Error: no space left: %llu\n", filesize
);
884 actsize
= bytesperclust
;
887 /* search for consecutive clusters */
888 while (actsize
< filesize
) {
889 newclust
= determine_fatent(mydata
, endclust
);
891 if ((newclust
- 1) != endclust
)
892 /* write to <curclust..endclust> */
895 if (CHECK_CLUST(newclust
, mydata
->fatsize
)) {
896 debug("newclust: 0x%x\n", newclust
);
897 debug("Invalid FAT entry\n");
901 actsize
+= bytesperclust
;
904 /* set remaining bytes */
906 if (set_cluster(mydata
, curclust
, buffer
, (u32
)actsize
) != 0) {
907 debug("error: writing cluster\n");
912 /* Mark end of file in FAT */
913 if (mydata
->fatsize
== 12)
915 else if (mydata
->fatsize
== 16)
917 else if (mydata
->fatsize
== 32)
918 newclust
= 0xfffffff;
919 set_fatent_value(mydata
, endclust
, newclust
);
923 if (set_cluster(mydata
, curclust
, buffer
, (u32
)actsize
) != 0) {
924 debug("error: writing cluster\n");
931 if (CHECK_CLUST(newclust
, mydata
->fatsize
)) {
932 debug("newclust: 0x%x\n", newclust
);
933 debug("Invalid FAT entry\n");
936 actsize
= bytesperclust
;
937 curclust
= endclust
= newclust
;
946 static void fill_dentry(fsdata
*mydata
, dir_entry
*dentptr
,
947 const char *filename
, __u32 start_cluster
, __u32 size
, __u8 attr
)
949 set_start_cluster(mydata
, dentptr
, start_cluster
);
950 dentptr
->size
= cpu_to_le32(size
);
952 dentptr
->attr
= attr
;
954 set_name(dentptr
, filename
);
958 * Find a directory entry based on filename or start cluster number
959 * If the directory entry is not found,
960 * the new position for writing a directory entry will be returned
962 static dir_entry
*find_directory_entry(fat_itr
*itr
, char *filename
)
966 while (fat_itr_next(itr
)) {
967 /* check both long and short name: */
968 if (!strcasecmp(filename
, itr
->name
))
970 else if (itr
->name
!= itr
->s_name
&&
971 !strcasecmp(filename
, itr
->s_name
))
977 if (itr
->dent
->name
[0] == '\0')
983 if (!itr
->dent
&& !itr
->is_root
&& new_dir_table(itr
))
984 /* indicate that allocating dent failed */
990 static int split_filename(char *filename
, char **dirname
, char **basename
)
992 char *p
, *last_slash
, *last_slash_cont
;
997 last_slash_cont
= NULL
;
999 if (ISDIRDELIM(*p
)) {
1001 last_slash_cont
= p
;
1002 /* continuous slashes */
1003 while (ISDIRDELIM(*p
))
1004 last_slash_cont
= p
++;
1012 if (last_slash_cont
== (filename
+ strlen(filename
) - 1)) {
1013 /* remove trailing slashes */
1018 if (last_slash
== filename
) {
1019 /* avoid ""(null) directory */
1023 *dirname
= filename
;
1026 *last_slash_cont
= '\0';
1027 *basename
= last_slash_cont
+ 1;
1029 *dirname
= "/"; /* root by default */
1030 *basename
= filename
;
1037 * normalize_longname() - check long file name and convert to lower case
1039 * We assume here that the FAT file system is using an 8bit code page.
1040 * Linux typically uses CP437, EDK2 assumes CP1250.
1042 * @l_filename: preallocated buffer receiving the normalized name
1043 * @filename: filename to normalize
1044 * Return: 0 on success, -1 on failure
1046 static int normalize_longname(char *l_filename
, const char *filename
)
1048 const char *p
, illegal
[] = "<>:\"/\\|?*";
1050 if (strlen(filename
) >= VFAT_MAXLEN_BYTES
)
1053 for (p
= filename
; *p
; ++p
) {
1054 if ((unsigned char)*p
< 0x20)
1056 if (strchr(illegal
, *p
))
1060 strcpy(l_filename
, filename
);
1061 downcase(l_filename
, VFAT_MAXLEN_BYTES
);
1066 int file_fat_write_at(const char *filename
, loff_t pos
, void *buffer
,
1067 loff_t size
, loff_t
*actwrite
)
1070 fsdata datablock
= { .fatbuf
= NULL
, };
1071 fsdata
*mydata
= &datablock
;
1072 fat_itr
*itr
= NULL
;
1074 char *filename_copy
, *parent
, *basename
;
1075 char l_filename
[VFAT_MAXLEN_BYTES
];
1077 debug("writing %s\n", filename
);
1079 filename_copy
= strdup(filename
);
1083 split_filename(filename_copy
, &parent
, &basename
);
1084 if (!strlen(basename
)) {
1089 filename
= basename
;
1090 if (normalize_longname(l_filename
, filename
)) {
1091 printf("FAT: illegal filename (%s)\n", filename
);
1096 itr
= malloc_cache_aligned(sizeof(fat_itr
));
1102 ret
= fat_itr_root(itr
, &datablock
);
1106 total_sector
= datablock
.total_sect
;
1108 ret
= fat_itr_resolve(itr
, parent
, TYPE_DIR
);
1110 printf("%s: doesn't exist (%d)\n", parent
, ret
);
1114 retdent
= find_directory_entry(itr
, l_filename
);
1117 if (fat_itr_isdir(itr
)) {
1124 /* Append to the end */
1125 pos
= FAT2CPU32(retdent
->size
);
1126 if (pos
> retdent
->size
) {
1127 /* No hole allowed */
1132 /* Update file size in a directory entry */
1133 retdent
->size
= cpu_to_le32(pos
+ size
);
1135 /* Create a new file */
1138 /* root dir cannot have "." or ".." */
1139 if (!strcmp(l_filename
, ".") ||
1140 !strcmp(l_filename
, "..")) {
1147 printf("Error: allocating new dir entry\n");
1153 /* No hole allowed */
1158 memset(itr
->dent
, 0, sizeof(*itr
->dent
));
1160 /* Calculate checksum for short name */
1161 set_name(itr
->dent
, filename
);
1163 /* Set long name entries */
1164 if (fill_dir_slot(itr
, filename
)) {
1169 /* Set short name entry */
1170 fill_dentry(itr
->fsdata
, itr
->dent
, filename
, 0, size
, 0x20);
1172 retdent
= itr
->dent
;
1175 ret
= set_contents(mydata
, retdent
, pos
, buffer
, size
, actwrite
);
1177 printf("Error: writing contents\n");
1181 debug("attempt to write 0x%llx bytes\n", *actwrite
);
1183 /* Flush fat buffer */
1184 ret
= flush_dirty_fat_buffer(mydata
);
1186 printf("Error: flush fat buffer\n");
1191 /* Write directory table to device */
1192 ret
= flush_dir(itr
);
1194 printf("Error: writing directory entry\n");
1199 free(filename_copy
);
1200 free(mydata
->fatbuf
);
1205 int file_fat_write(const char *filename
, void *buffer
, loff_t offset
,
1206 loff_t maxsize
, loff_t
*actwrite
)
1208 return file_fat_write_at(filename
, offset
, buffer
, maxsize
, actwrite
);
1211 static int fat_dir_entries(fat_itr
*itr
)
1214 fsdata fsdata
= { .fatbuf
= NULL
, }, *mydata
= &fsdata
;
1215 /* for FATBUFSIZE */
1218 dirs
= malloc_cache_aligned(sizeof(fat_itr
));
1220 debug("Error: allocating memory\n");
1225 /* duplicate fsdata */
1226 fat_itr_child(dirs
, itr
);
1227 fsdata
= *dirs
->fsdata
;
1229 /* allocate local fat buffer */
1230 fsdata
.fatbuf
= malloc_cache_aligned(FATBUFSIZE
);
1231 if (!fsdata
.fatbuf
) {
1232 debug("Error: allocating memory\n");
1236 fsdata
.fatbufnum
= -1;
1237 dirs
->fsdata
= &fsdata
;
1239 for (count
= 0; fat_itr_next(dirs
); count
++)
1243 free(fsdata
.fatbuf
);
1248 static int delete_dentry(fat_itr
*itr
)
1250 fsdata
*mydata
= itr
->fsdata
;
1251 dir_entry
*dentptr
= itr
->dent
;
1253 /* free cluster blocks */
1254 clear_fatent(mydata
, START(dentptr
));
1255 if (flush_dirty_fat_buffer(mydata
) < 0) {
1256 printf("Error: flush fat buffer\n");
1261 * update a directory entry
1263 * - long file name support
1264 * - find and mark the "new" first invalid entry as name[0]=0x00
1266 memset(dentptr
, 0, sizeof(*dentptr
));
1267 dentptr
->name
[0] = 0xe5;
1269 if (flush_dir(itr
)) {
1270 printf("error: writing directory entry\n");
1277 int fat_unlink(const char *filename
)
1279 fsdata fsdata
= { .fatbuf
= NULL
, };
1280 fat_itr
*itr
= NULL
;
1282 char *filename_copy
, *dirname
, *basename
;
1284 filename_copy
= strdup(filename
);
1285 if (!filename_copy
) {
1286 printf("Error: allocating memory\n");
1290 split_filename(filename_copy
, &dirname
, &basename
);
1292 if (!strcmp(dirname
, "/") && !strcmp(basename
, "")) {
1293 printf("Error: cannot remove root\n");
1298 itr
= malloc_cache_aligned(sizeof(fat_itr
));
1300 printf("Error: allocating memory\n");
1305 ret
= fat_itr_root(itr
, &fsdata
);
1309 total_sector
= fsdata
.total_sect
;
1311 ret
= fat_itr_resolve(itr
, dirname
, TYPE_DIR
);
1313 printf("%s: doesn't exist (%d)\n", dirname
, ret
);
1318 if (!find_directory_entry(itr
, basename
)) {
1319 printf("%s: doesn't exist\n", basename
);
1324 if (fat_itr_isdir(itr
)) {
1325 n_entries
= fat_dir_entries(itr
);
1326 if (n_entries
< 0) {
1330 if (n_entries
> 2) {
1331 printf("Error: directory is not empty: %d\n",
1338 ret
= delete_dentry(itr
);
1341 free(fsdata
.fatbuf
);
1343 free(filename_copy
);
1348 int fat_mkdir(const char *new_dirname
)
1351 fsdata datablock
= { .fatbuf
= NULL
, };
1352 fsdata
*mydata
= &datablock
;
1353 fat_itr
*itr
= NULL
;
1354 char *dirname_copy
, *parent
, *dirname
;
1355 char l_dirname
[VFAT_MAXLEN_BYTES
];
1358 unsigned int bytesperclust
;
1359 dir_entry
*dotdent
= NULL
;
1361 dirname_copy
= strdup(new_dirname
);
1365 split_filename(dirname_copy
, &parent
, &dirname
);
1366 if (!strlen(dirname
)) {
1371 if (normalize_longname(l_dirname
, dirname
)) {
1372 printf("FAT: illegal filename (%s)\n", dirname
);
1377 itr
= malloc_cache_aligned(sizeof(fat_itr
));
1383 ret
= fat_itr_root(itr
, &datablock
);
1387 total_sector
= datablock
.total_sect
;
1389 ret
= fat_itr_resolve(itr
, parent
, TYPE_DIR
);
1391 printf("%s: doesn't exist (%d)\n", parent
, ret
);
1395 retdent
= find_directory_entry(itr
, l_dirname
);
1398 printf("%s: already exists\n", l_dirname
);
1403 /* root dir cannot have "." or ".." */
1404 if (!strcmp(l_dirname
, ".") ||
1405 !strcmp(l_dirname
, "..")) {
1412 printf("Error: allocating new dir entry\n");
1417 memset(itr
->dent
, 0, sizeof(*itr
->dent
));
1419 /* Set short name to set alias checksum field in dir_slot */
1420 set_name(itr
->dent
, dirname
);
1421 fill_dir_slot(itr
, dirname
);
1423 /* Set attribute as archive for regular file */
1424 fill_dentry(itr
->fsdata
, itr
->dent
, dirname
, 0, 0,
1425 ATTR_DIR
| ATTR_ARCH
);
1427 retdent
= itr
->dent
;
1430 /* Default entries */
1431 bytesperclust
= mydata
->clust_size
* mydata
->sect_size
;
1432 dotdent
= malloc_cache_aligned(bytesperclust
);
1437 memset(dotdent
, 0, bytesperclust
);
1439 memcpy(dotdent
[0].name
, ". ", 8);
1440 memcpy(dotdent
[0].ext
, " ", 3);
1441 dotdent
[0].attr
= ATTR_DIR
| ATTR_ARCH
;
1443 memcpy(dotdent
[1].name
, ".. ", 8);
1444 memcpy(dotdent
[1].ext
, " ", 3);
1445 dotdent
[1].attr
= ATTR_DIR
| ATTR_ARCH
;
1446 set_start_cluster(mydata
, &dotdent
[1], itr
->start_clust
);
1448 ret
= set_contents(mydata
, retdent
, 0, (__u8
*)dotdent
,
1449 bytesperclust
, &actwrite
);
1451 printf("Error: writing contents\n");
1454 /* Write twice for "." */
1455 set_start_cluster(mydata
, &dotdent
[0], START(retdent
));
1456 ret
= set_contents(mydata
, retdent
, 0, (__u8
*)dotdent
,
1457 bytesperclust
, &actwrite
);
1459 printf("Error: writing contents\n");
1463 /* Flush fat buffer */
1464 ret
= flush_dirty_fat_buffer(mydata
);
1466 printf("Error: flush fat buffer\n");
1470 /* Write directory table to device */
1471 ret
= flush_dir(itr
);
1473 printf("Error: writing directory entry\n");
1477 free(mydata
->fatbuf
);