4 * R/W (V)FAT 12/16/32 filesystem implementation by Donggeun Kim
6 * SPDX-License-Identifier: GPL-2.0+
13 #include <asm/byteorder.h>
15 #include <linux/ctype.h>
18 static void uppercase(char *str
, int len
)
22 for (i
= 0; i
< len
; i
++) {
28 static int total_sector
;
29 static int disk_write(__u32 block
, __u32 nr_blocks
, void *buf
)
31 if (!cur_dev
|| !cur_dev
->block_write
)
34 if (cur_part_info
.start
+ block
+ nr_blocks
>
35 cur_part_info
.start
+ total_sector
) {
36 printf("error: overflow occurs\n");
40 return cur_dev
->block_write(cur_dev
->dev
,
41 cur_part_info
.start
+ block
, nr_blocks
, buf
);
45 * Set short name in directory entry
47 static void set_name(dir_entry
*dirent
, const char *filename
)
49 char s_name
[VFAT_MAXLEN_BYTES
];
51 int period_location
, len
, i
, ext_num
;
56 len
= strlen(filename
);
60 strcpy(s_name
, filename
);
61 uppercase(s_name
, len
);
63 period
= strchr(s_name
, '.');
65 period_location
= len
;
68 period_location
= period
- s_name
;
69 ext_num
= len
- period_location
- 1;
72 /* Pad spaces when the length of file name is shorter than eight */
73 if (period_location
< 8) {
74 memcpy(dirent
->name
, s_name
, period_location
);
75 for (i
= period_location
; i
< 8; i
++)
76 dirent
->name
[i
] = ' ';
77 } else if (period_location
== 8) {
78 memcpy(dirent
->name
, s_name
, period_location
);
80 memcpy(dirent
->name
, s_name
, 6);
81 dirent
->name
[6] = '~';
82 dirent
->name
[7] = '1';
86 memcpy(dirent
->ext
, s_name
+ period_location
+ 1, ext_num
);
87 for (i
= ext_num
; i
< 3; i
++)
90 memcpy(dirent
->ext
, s_name
+ period_location
+ 1, 3);
92 debug("name : %s\n", dirent
->name
);
93 debug("ext : %s\n", dirent
->ext
);
96 static __u8 num_of_fats
;
98 * Write fat buffer into block device
100 static int flush_fat_buffer(fsdata
*mydata
)
102 int getsize
= FATBUFBLOCKS
;
103 __u32 fatlength
= mydata
->fatlength
;
104 __u8
*bufptr
= mydata
->fatbuf
;
105 __u32 startblock
= mydata
->fatbufnum
* FATBUFBLOCKS
;
107 startblock
+= mydata
->fat_sect
;
109 if (getsize
> fatlength
)
113 if (disk_write(startblock
, getsize
, bufptr
) < 0) {
114 debug("error: writing FAT blocks\n");
118 if (num_of_fats
== 2) {
119 /* Update corresponding second FAT blocks */
120 startblock
+= mydata
->fatlength
;
121 if (disk_write(startblock
, getsize
, bufptr
) < 0) {
122 debug("error: writing second FAT blocks\n");
131 * Get the entry at index 'entry' in a FAT (12/16/32) table.
132 * On failure 0x00 is returned.
133 * When bufnum is changed, write back the previous fatbuf to the disk.
135 static __u32
get_fatent_value(fsdata
*mydata
, __u32 entry
)
142 switch (mydata
->fatsize
) {
144 bufnum
= entry
/ FAT32BUFSIZE
;
145 offset
= entry
- bufnum
* FAT32BUFSIZE
;
148 bufnum
= entry
/ FAT16BUFSIZE
;
149 offset
= entry
- bufnum
* FAT16BUFSIZE
;
152 bufnum
= entry
/ FAT12BUFSIZE
;
153 offset
= entry
- bufnum
* FAT12BUFSIZE
;
157 /* Unsupported FAT size */
161 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
162 mydata
->fatsize
, entry
, entry
, offset
, offset
);
164 /* Read a new block of FAT entries into the cache. */
165 if (bufnum
!= mydata
->fatbufnum
) {
166 int getsize
= FATBUFBLOCKS
;
167 __u8
*bufptr
= mydata
->fatbuf
;
168 __u32 fatlength
= mydata
->fatlength
;
169 __u32 startblock
= bufnum
* FATBUFBLOCKS
;
171 if (getsize
> fatlength
)
174 fatlength
*= mydata
->sect_size
; /* We want it in bytes now */
175 startblock
+= mydata
->fat_sect
; /* Offset from start of disk */
177 /* Write back the fatbuf to the disk */
178 if (mydata
->fatbufnum
!= -1) {
179 if (flush_fat_buffer(mydata
) < 0)
183 if (disk_read(startblock
, getsize
, bufptr
) < 0) {
184 debug("Error reading FAT blocks\n");
187 mydata
->fatbufnum
= bufnum
;
190 /* Get the actual entry from the table */
191 switch (mydata
->fatsize
) {
193 ret
= FAT2CPU32(((__u32
*) mydata
->fatbuf
)[offset
]);
196 ret
= FAT2CPU16(((__u16
*) mydata
->fatbuf
)[offset
]);
199 off16
= (offset
* 3) / 4;
201 switch (offset
& 0x3) {
203 ret
= FAT2CPU16(((__u16
*) mydata
->fatbuf
)[off16
]);
207 val1
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
]);
209 val2
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
+ 1]);
211 ret
= (val2
<< 4) | (val1
>> 12);
214 val1
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
]);
216 val2
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
+ 1]);
218 ret
= (val2
<< 8) | (val1
>> 8);
221 ret
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
]);
222 ret
= (ret
& 0xfff0) >> 4;
229 debug("FAT%d: ret: %08x, entry: %08x, offset: %04x\n",
230 mydata
->fatsize
, ret
, entry
, offset
);
236 * Set the file name information from 'name' into 'slotptr',
238 static int str2slot(dir_slot
*slotptr
, const char *name
, int *idx
)
242 for (j
= 0; j
<= 8; j
+= 2) {
243 if (name
[*idx
] == 0x00) {
244 slotptr
->name0_4
[j
] = 0;
245 slotptr
->name0_4
[j
+ 1] = 0;
249 slotptr
->name0_4
[j
] = name
[*idx
];
253 for (j
= 0; j
<= 10; j
+= 2) {
254 if (name
[*idx
] == 0x00) {
255 slotptr
->name5_10
[j
] = 0;
256 slotptr
->name5_10
[j
+ 1] = 0;
260 slotptr
->name5_10
[j
] = name
[*idx
];
264 for (j
= 0; j
<= 2; j
+= 2) {
265 if (name
[*idx
] == 0x00) {
266 slotptr
->name11_12
[j
] = 0;
267 slotptr
->name11_12
[j
+ 1] = 0;
271 slotptr
->name11_12
[j
] = name
[*idx
];
276 if (name
[*idx
] == 0x00)
280 /* Not used characters are filled with 0xff 0xff */
282 for (; end_idx
< 5; end_idx
++) {
283 slotptr
->name0_4
[end_idx
* 2] = 0xff;
284 slotptr
->name0_4
[end_idx
* 2 + 1] = 0xff;
289 for (; end_idx
< 6; end_idx
++) {
290 slotptr
->name5_10
[end_idx
* 2] = 0xff;
291 slotptr
->name5_10
[end_idx
* 2 + 1] = 0xff;
296 for (; end_idx
< 2; end_idx
++) {
297 slotptr
->name11_12
[end_idx
* 2] = 0xff;
298 slotptr
->name11_12
[end_idx
* 2 + 1] = 0xff;
304 static int is_next_clust(fsdata
*mydata
, dir_entry
*dentptr
);
305 static void flush_dir_table(fsdata
*mydata
, dir_entry
**dentptr
);
308 * Fill dir_slot entries with appropriate name, id, and attr
309 * The real directory entry is returned by 'dentptr'
312 fill_dir_slot(fsdata
*mydata
, dir_entry
**dentptr
, const char *l_name
)
314 dir_slot
*slotptr
= (dir_slot
*)get_contents_vfatname_block
;
315 __u8 counter
= 0, checksum
;
319 /* Get short file name and checksum value */
320 strncpy(s_name
, (*dentptr
)->name
, 16);
321 checksum
= mkcksum((*dentptr
)->name
, (*dentptr
)->ext
);
324 memset(slotptr
, 0x00, sizeof(dir_slot
));
325 ret
= str2slot(slotptr
, l_name
, &idx
);
326 slotptr
->id
= ++counter
;
327 slotptr
->attr
= ATTR_VFAT
;
328 slotptr
->alias_checksum
= checksum
;
333 slotptr
->id
|= LAST_LONG_ENTRY_MASK
;
335 while (counter
>= 1) {
336 if (is_next_clust(mydata
, *dentptr
)) {
337 /* A new cluster is allocated for directory table */
338 flush_dir_table(mydata
, dentptr
);
340 memcpy(*dentptr
, slotptr
, sizeof(dir_slot
));
346 if (is_next_clust(mydata
, *dentptr
)) {
347 /* A new cluster is allocated for directory table */
348 flush_dir_table(mydata
, dentptr
);
352 static __u32 dir_curclust
;
355 * Extract the full long filename starting at 'retdent' (which is really
356 * a slot) into 'l_name'. If successful also copy the real directory entry
358 * If additional adjacent cluster for directory entries is read into memory,
359 * then 'get_contents_vfatname_block' is copied into 'get_dentfromdir_block' and
360 * the location of the real directory entry is returned by 'retdent'
361 * Return 0 on success, -1 otherwise.
364 get_long_file_name(fsdata
*mydata
, int curclust
, __u8
*cluster
,
365 dir_entry
**retdent
, char *l_name
)
368 dir_slot
*slotptr
= (dir_slot
*)(*retdent
);
369 dir_slot
*slotptr2
= NULL
;
370 __u8
*buflimit
= cluster
+ mydata
->sect_size
* ((curclust
== 0) ?
373 __u8 counter
= (slotptr
->id
& ~LAST_LONG_ENTRY_MASK
) & 0xff;
374 int idx
= 0, cur_position
= 0;
376 if (counter
> VFAT_MAXSEQ
) {
377 debug("Error: VFAT name is too long\n");
381 while ((__u8
*)slotptr
< buflimit
) {
384 if (((slotptr
->id
& ~LAST_LONG_ENTRY_MASK
) & 0xff) != counter
)
390 if ((__u8
*)slotptr
>= buflimit
) {
393 curclust
= get_fatent_value(mydata
, dir_curclust
);
394 if (CHECK_CLUST(curclust
, mydata
->fatsize
)) {
395 debug("curclust: 0x%x\n", curclust
);
396 printf("Invalid FAT entry\n");
400 dir_curclust
= curclust
;
402 if (get_cluster(mydata
, curclust
, get_contents_vfatname_block
,
403 mydata
->clust_size
* mydata
->sect_size
) != 0) {
404 debug("Error: reading directory block\n");
408 slotptr2
= (dir_slot
*)get_contents_vfatname_block
;
409 while (counter
> 0) {
410 if (((slotptr2
->id
& ~LAST_LONG_ENTRY_MASK
)
417 /* Save the real directory entry */
418 realdent
= (dir_entry
*)slotptr2
;
419 while ((__u8
*)slotptr2
> get_contents_vfatname_block
) {
421 slot2str(slotptr2
, l_name
, &idx
);
424 /* Save the real directory entry */
425 realdent
= (dir_entry
*)slotptr
;
430 if (slot2str(slotptr
, l_name
, &idx
))
432 } while (!(slotptr
->id
& LAST_LONG_ENTRY_MASK
));
435 if (*l_name
== DELETED_FLAG
)
437 else if (*l_name
== aRING
)
438 *l_name
= DELETED_FLAG
;
441 /* Return the real directory entry */
445 memcpy(get_dentfromdir_block
, get_contents_vfatname_block
,
446 mydata
->clust_size
* mydata
->sect_size
);
447 cur_position
= (__u8
*)realdent
- get_contents_vfatname_block
;
448 *retdent
= (dir_entry
*) &get_dentfromdir_block
[cur_position
];
455 * Set the entry at index 'entry' in a FAT (16/32) table.
457 static int set_fatent_value(fsdata
*mydata
, __u32 entry
, __u32 entry_value
)
459 __u32 bufnum
, offset
;
461 switch (mydata
->fatsize
) {
463 bufnum
= entry
/ FAT32BUFSIZE
;
464 offset
= entry
- bufnum
* FAT32BUFSIZE
;
467 bufnum
= entry
/ FAT16BUFSIZE
;
468 offset
= entry
- bufnum
* FAT16BUFSIZE
;
471 /* Unsupported FAT size */
475 /* Read a new block of FAT entries into the cache. */
476 if (bufnum
!= mydata
->fatbufnum
) {
477 int getsize
= FATBUFBLOCKS
;
478 __u8
*bufptr
= mydata
->fatbuf
;
479 __u32 fatlength
= mydata
->fatlength
;
480 __u32 startblock
= bufnum
* FATBUFBLOCKS
;
482 fatlength
*= mydata
->sect_size
;
483 startblock
+= mydata
->fat_sect
;
485 if (getsize
> fatlength
)
488 if (mydata
->fatbufnum
!= -1) {
489 if (flush_fat_buffer(mydata
) < 0)
493 if (disk_read(startblock
, getsize
, bufptr
) < 0) {
494 debug("Error reading FAT blocks\n");
497 mydata
->fatbufnum
= bufnum
;
500 /* Set the actual entry */
501 switch (mydata
->fatsize
) {
503 ((__u32
*) mydata
->fatbuf
)[offset
] = cpu_to_le32(entry_value
);
506 ((__u16
*) mydata
->fatbuf
)[offset
] = cpu_to_le16(entry_value
);
516 * Determine the entry value at index 'entry' in a FAT (16/32) table
518 static __u32
determine_fatent(fsdata
*mydata
, __u32 entry
)
520 __u32 next_fat
, next_entry
= entry
+ 1;
523 next_fat
= get_fatent_value(mydata
, next_entry
);
525 set_fatent_value(mydata
, entry
, next_entry
);
530 debug("FAT%d: entry: %08x, entry_value: %04x\n",
531 mydata
->fatsize
, entry
, next_entry
);
537 * Write at most 'size' bytes from 'buffer' into the specified cluster.
538 * Return 0 on success, -1 otherwise.
541 set_cluster(fsdata
*mydata
, __u32 clustnum
, __u8
*buffer
,
548 startsect
= mydata
->data_begin
+
549 clustnum
* mydata
->clust_size
;
551 startsect
= mydata
->rootdir_sect
;
553 debug("clustnum: %d, startsect: %d\n", clustnum
, startsect
);
555 if ((size
/ mydata
->sect_size
) > 0) {
556 if (disk_write(startsect
, size
/ mydata
->sect_size
, buffer
) < 0) {
557 debug("Error writing data\n");
562 if (size
% mydata
->sect_size
) {
563 __u8 tmpbuf
[mydata
->sect_size
];
565 idx
= size
/ mydata
->sect_size
;
566 buffer
+= idx
* mydata
->sect_size
;
567 memcpy(tmpbuf
, buffer
, size
% mydata
->sect_size
);
569 if (disk_write(startsect
+ idx
, 1, tmpbuf
) < 0) {
570 debug("Error writing data\n");
581 * Find the first empty cluster
583 static int find_empty_cluster(fsdata
*mydata
)
585 __u32 fat_val
, entry
= 3;
588 fat_val
= get_fatent_value(mydata
, entry
);
598 * Write directory entries in 'get_dentfromdir_block' to block device
600 static void flush_dir_table(fsdata
*mydata
, dir_entry
**dentptr
)
602 int dir_newclust
= 0;
604 if (set_cluster(mydata
, dir_curclust
,
605 get_dentfromdir_block
,
606 mydata
->clust_size
* mydata
->sect_size
) != 0) {
607 printf("error: wrinting directory entry\n");
610 dir_newclust
= find_empty_cluster(mydata
);
611 set_fatent_value(mydata
, dir_curclust
, dir_newclust
);
612 if (mydata
->fatsize
== 32)
613 set_fatent_value(mydata
, dir_newclust
, 0xffffff8);
614 else if (mydata
->fatsize
== 16)
615 set_fatent_value(mydata
, dir_newclust
, 0xfff8);
617 dir_curclust
= dir_newclust
;
619 if (flush_fat_buffer(mydata
) < 0)
622 memset(get_dentfromdir_block
, 0x00,
623 mydata
->clust_size
* mydata
->sect_size
);
625 *dentptr
= (dir_entry
*) get_dentfromdir_block
;
629 * Set empty cluster from 'entry' to the end of a file
631 static int clear_fatent(fsdata
*mydata
, __u32 entry
)
636 fat_val
= get_fatent_value(mydata
, entry
);
638 set_fatent_value(mydata
, entry
, 0);
642 if (fat_val
== 0xfffffff || fat_val
== 0xffff)
648 /* Flush fat buffer */
649 if (flush_fat_buffer(mydata
) < 0)
656 * Write at most 'maxsize' bytes from 'buffer' into
657 * the file associated with 'dentptr'
658 * Return the number of bytes read or -1 on fatal errors.
661 set_contents(fsdata
*mydata
, dir_entry
*dentptr
, __u8
*buffer
,
662 unsigned long maxsize
)
664 unsigned long filesize
= FAT2CPU32(dentptr
->size
), gotsize
= 0;
665 unsigned int bytesperclust
= mydata
->clust_size
* mydata
->sect_size
;
666 __u32 curclust
= START(dentptr
);
667 __u32 endclust
= 0, newclust
= 0;
668 unsigned long actsize
;
670 debug("Filesize: %ld bytes\n", filesize
);
672 if (maxsize
> 0 && filesize
> maxsize
)
675 debug("%ld bytes\n", filesize
);
677 actsize
= bytesperclust
;
680 /* search for consecutive clusters */
681 while (actsize
< filesize
) {
682 newclust
= determine_fatent(mydata
, endclust
);
684 if ((newclust
- 1) != endclust
)
687 if (CHECK_CLUST(newclust
, mydata
->fatsize
)) {
688 debug("curclust: 0x%x\n", newclust
);
689 debug("Invalid FAT entry\n");
693 actsize
+= bytesperclust
;
695 /* actsize >= file size */
696 actsize
-= bytesperclust
;
697 /* set remaining clusters */
698 if (set_cluster(mydata
, curclust
, buffer
, (int)actsize
) != 0) {
699 debug("error: writing cluster\n");
703 /* set remaining bytes */
704 gotsize
+= (int)actsize
;
709 if (set_cluster(mydata
, endclust
, buffer
, (int)actsize
) != 0) {
710 debug("error: writing cluster\n");
715 /* Mark end of file in FAT */
716 if (mydata
->fatsize
== 16)
718 else if (mydata
->fatsize
== 32)
719 newclust
= 0xfffffff;
720 set_fatent_value(mydata
, endclust
, newclust
);
724 if (set_cluster(mydata
, curclust
, buffer
, (int)actsize
) != 0) {
725 debug("error: writing cluster\n");
728 gotsize
+= (int)actsize
;
732 if (CHECK_CLUST(curclust
, mydata
->fatsize
)) {
733 debug("curclust: 0x%x\n", curclust
);
734 debug("Invalid FAT entry\n");
737 actsize
= bytesperclust
;
738 curclust
= endclust
= newclust
;
745 static void fill_dentry(fsdata
*mydata
, dir_entry
*dentptr
,
746 const char *filename
, __u32 start_cluster
, __u32 size
, __u8 attr
)
748 if (mydata
->fatsize
== 32)
750 cpu_to_le16((start_cluster
& 0xffff0000) >> 16);
751 dentptr
->start
= cpu_to_le16(start_cluster
& 0xffff);
752 dentptr
->size
= cpu_to_le32(size
);
754 dentptr
->attr
= attr
;
756 set_name(dentptr
, filename
);
760 * Check whether adding a file makes the file system to
761 * exceed the size of the block device
762 * Return -1 when overflow occurs, otherwise return 0
764 static int check_overflow(fsdata
*mydata
, __u32 clustnum
, unsigned long size
)
766 __u32 startsect
, sect_num
;
769 startsect
= mydata
->data_begin
+
770 clustnum
* mydata
->clust_size
;
772 startsect
= mydata
->rootdir_sect
;
775 sect_num
= size
/ mydata
->sect_size
;
776 if (size
% mydata
->sect_size
)
779 if (startsect
+ sect_num
> cur_part_info
.start
+ total_sector
)
786 * Check if adding several entries exceed one cluster boundary
788 static int is_next_clust(fsdata
*mydata
, dir_entry
*dentptr
)
792 cur_position
= (__u8
*)dentptr
- get_dentfromdir_block
;
794 if (cur_position
>= mydata
->clust_size
* mydata
->sect_size
)
800 static dir_entry
*empty_dentptr
;
802 * Find a directory entry based on filename or start cluster number
803 * If the directory entry is not found,
804 * the new position for writing a directory entry will be returned
806 static dir_entry
*find_directory_entry(fsdata
*mydata
, int startsect
,
807 char *filename
, dir_entry
*retdent
, __u32 start
)
809 __u32 curclust
= (startsect
- mydata
->data_begin
) / mydata
->clust_size
;
811 debug("get_dentfromdir: %s\n", filename
);
818 if (get_cluster(mydata
, curclust
, get_dentfromdir_block
,
819 mydata
->clust_size
* mydata
->sect_size
) != 0) {
820 printf("Error: reading directory block\n");
824 dentptr
= (dir_entry
*)get_dentfromdir_block
;
826 dir_curclust
= curclust
;
828 for (i
= 0; i
< DIRENTSPERCLUST
; i
++) {
829 char s_name
[14], l_name
[VFAT_MAXLEN_BYTES
];
832 if (dentptr
->name
[0] == DELETED_FLAG
) {
834 if (is_next_clust(mydata
, dentptr
))
838 if ((dentptr
->attr
& ATTR_VOLUME
)) {
840 (dentptr
->attr
& ATTR_VFAT
) &&
841 (dentptr
->name
[0] & LAST_LONG_ENTRY_MASK
)) {
842 get_long_file_name(mydata
, curclust
,
843 get_dentfromdir_block
,
845 debug("vfatname: |%s|\n", l_name
);
847 /* Volume label or VFAT entry */
849 if (is_next_clust(mydata
, dentptr
))
854 if (dentptr
->name
[0] == 0) {
855 debug("Dentname == NULL - %d\n", i
);
856 empty_dentptr
= dentptr
;
860 get_name(dentptr
, s_name
);
862 if (strcmp(filename
, s_name
)
863 && strcmp(filename
, l_name
)) {
864 debug("Mismatch: |%s|%s|\n",
867 if (is_next_clust(mydata
, dentptr
))
872 memcpy(retdent
, dentptr
, sizeof(dir_entry
));
874 debug("DentName: %s", s_name
);
875 debug(", start: 0x%x", START(dentptr
));
876 debug(", size: 0x%x %s\n",
877 FAT2CPU32(dentptr
->size
),
878 (dentptr
->attr
& ATTR_DIR
) ?
884 curclust
= get_fatent_value(mydata
, dir_curclust
);
885 if ((curclust
>= 0xffffff8) || (curclust
>= 0xfff8)) {
886 empty_dentptr
= dentptr
;
889 if (CHECK_CLUST(curclust
, mydata
->fatsize
)) {
890 debug("curclust: 0x%x\n", curclust
);
891 debug("Invalid FAT entry\n");
899 static int do_fat_write(const char *filename
, void *buffer
,
902 dir_entry
*dentptr
, *retdent
;
908 fsdata
*mydata
= &datablock
;
910 int ret
= -1, name_len
;
911 char l_filename
[VFAT_MAXLEN_BYTES
];
912 int write_size
= size
;
916 if (read_bootsectandvi(&bs
, &volinfo
, &mydata
->fatsize
)) {
917 debug("error: reading boot sector\n");
921 total_sector
= bs
.total_sect
;
922 if (total_sector
== 0)
923 total_sector
= cur_part_info
.size
;
925 if (mydata
->fatsize
== 32)
926 mydata
->fatlength
= bs
.fat32_length
;
928 mydata
->fatlength
= bs
.fat_length
;
930 mydata
->fat_sect
= bs
.reserved
;
932 cursect
= mydata
->rootdir_sect
933 = mydata
->fat_sect
+ mydata
->fatlength
* bs
.fats
;
934 num_of_fats
= bs
.fats
;
936 mydata
->sect_size
= (bs
.sector_size
[1] << 8) + bs
.sector_size
[0];
937 mydata
->clust_size
= bs
.cluster_size
;
939 if (mydata
->fatsize
== 32) {
940 mydata
->data_begin
= mydata
->rootdir_sect
-
941 (mydata
->clust_size
* 2);
945 rootdir_size
= ((bs
.dir_entries
[1] * (int)256 +
949 mydata
->data_begin
= mydata
->rootdir_sect
+
951 (mydata
->clust_size
* 2);
954 mydata
->fatbufnum
= -1;
955 mydata
->fatbuf
= malloc(FATBUFSIZE
);
956 if (mydata
->fatbuf
== NULL
) {
957 debug("Error: allocating memory\n");
961 if (disk_read(cursect
,
962 (mydata
->fatsize
== 32) ?
963 (mydata
->clust_size
) :
964 PREFETCH_BLOCKS
, do_fat_read_at_block
) < 0) {
965 debug("Error: reading rootdir block\n");
968 dentptr
= (dir_entry
*) do_fat_read_at_block
;
970 name_len
= strlen(filename
);
971 if (name_len
>= VFAT_MAXLEN_BYTES
)
972 name_len
= VFAT_MAXLEN_BYTES
- 1;
974 memcpy(l_filename
, filename
, name_len
);
975 l_filename
[name_len
] = 0; /* terminate the string */
976 downcase(l_filename
);
978 startsect
= mydata
->rootdir_sect
;
979 retdent
= find_directory_entry(mydata
, startsect
,
980 l_filename
, dentptr
, 0);
982 /* Update file size and start_cluster in a directory entry */
983 retdent
->size
= cpu_to_le32(size
);
984 start_cluster
= FAT2CPU16(retdent
->start
);
985 if (mydata
->fatsize
== 32)
987 (FAT2CPU16(retdent
->starthi
) << 16);
989 ret
= check_overflow(mydata
, start_cluster
, size
);
991 printf("Error: %ld overflow\n", size
);
995 ret
= clear_fatent(mydata
, start_cluster
);
997 printf("Error: clearing FAT entries\n");
1001 ret
= set_contents(mydata
, retdent
, buffer
, size
);
1003 printf("Error: writing contents\n");
1007 debug("attempt to write 0x%x bytes\n", write_size
);
1009 /* Flush fat buffer */
1010 ret
= flush_fat_buffer(mydata
);
1012 printf("Error: flush fat buffer\n");
1016 /* Write directory table to device */
1017 ret
= set_cluster(mydata
, dir_curclust
,
1018 get_dentfromdir_block
,
1019 mydata
->clust_size
* mydata
->sect_size
);
1021 printf("Error: writing directory entry\n");
1025 /* Set short name to set alias checksum field in dir_slot */
1026 set_name(empty_dentptr
, filename
);
1027 fill_dir_slot(mydata
, &empty_dentptr
, filename
);
1029 ret
= start_cluster
= find_empty_cluster(mydata
);
1031 printf("Error: finding empty cluster\n");
1035 ret
= check_overflow(mydata
, start_cluster
, size
);
1037 printf("Error: %ld overflow\n", size
);
1041 /* Set attribute as archieve for regular file */
1042 fill_dentry(mydata
, empty_dentptr
, filename
,
1043 start_cluster
, size
, 0x20);
1045 ret
= set_contents(mydata
, empty_dentptr
, buffer
, size
);
1047 printf("Error: writing contents\n");
1051 debug("attempt to write 0x%x bytes\n", write_size
);
1053 /* Flush fat buffer */
1054 ret
= flush_fat_buffer(mydata
);
1056 printf("Error: flush fat buffer\n");
1060 /* Write directory table to device */
1061 ret
= set_cluster(mydata
, dir_curclust
,
1062 get_dentfromdir_block
,
1063 mydata
->clust_size
* mydata
->sect_size
);
1065 printf("Error: writing directory entry\n");
1071 free(mydata
->fatbuf
);
1072 return ret
< 0 ? ret
: write_size
;
1075 int file_fat_write(const char *filename
, void *buffer
, unsigned long maxsize
)
1077 printf("writing %s\n", filename
);
1078 return do_fat_write(filename
, buffer
, maxsize
);