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>
17 #include <linux/math64.h>
20 static void uppercase(char *str
, int len
)
24 for (i
= 0; i
< len
; i
++) {
30 static int total_sector
;
31 static int disk_write(__u32 block
, __u32 nr_blocks
, void *buf
)
33 if (!cur_dev
|| !cur_dev
->block_write
)
36 if (cur_part_info
.start
+ block
+ nr_blocks
>
37 cur_part_info
.start
+ total_sector
) {
38 printf("error: overflow occurs\n");
42 return cur_dev
->block_write(cur_dev
->dev
,
43 cur_part_info
.start
+ block
, nr_blocks
, buf
);
47 * Set short name in directory entry
49 static void set_name(dir_entry
*dirent
, const char *filename
)
51 char s_name
[VFAT_MAXLEN_BYTES
];
53 int period_location
, len
, i
, ext_num
;
58 len
= strlen(filename
);
62 strcpy(s_name
, filename
);
63 uppercase(s_name
, len
);
65 period
= strchr(s_name
, '.');
67 period_location
= len
;
70 period_location
= period
- s_name
;
71 ext_num
= len
- period_location
- 1;
74 /* Pad spaces when the length of file name is shorter than eight */
75 if (period_location
< 8) {
76 memcpy(dirent
->name
, s_name
, period_location
);
77 for (i
= period_location
; i
< 8; i
++)
78 dirent
->name
[i
] = ' ';
79 } else if (period_location
== 8) {
80 memcpy(dirent
->name
, s_name
, period_location
);
82 memcpy(dirent
->name
, s_name
, 6);
83 dirent
->name
[6] = '~';
84 dirent
->name
[7] = '1';
88 memcpy(dirent
->ext
, s_name
+ period_location
+ 1, ext_num
);
89 for (i
= ext_num
; i
< 3; i
++)
92 memcpy(dirent
->ext
, s_name
+ period_location
+ 1, 3);
94 debug("name : %s\n", dirent
->name
);
95 debug("ext : %s\n", dirent
->ext
);
98 static __u8 num_of_fats
;
100 * Write fat buffer into block device
102 static int flush_fat_buffer(fsdata
*mydata
)
104 int getsize
= FATBUFBLOCKS
;
105 __u32 fatlength
= mydata
->fatlength
;
106 __u8
*bufptr
= mydata
->fatbuf
;
107 __u32 startblock
= mydata
->fatbufnum
* FATBUFBLOCKS
;
109 startblock
+= mydata
->fat_sect
;
111 if (getsize
> fatlength
)
115 if (disk_write(startblock
, getsize
, bufptr
) < 0) {
116 debug("error: writing FAT blocks\n");
120 if (num_of_fats
== 2) {
121 /* Update corresponding second FAT blocks */
122 startblock
+= mydata
->fatlength
;
123 if (disk_write(startblock
, getsize
, bufptr
) < 0) {
124 debug("error: writing second FAT blocks\n");
133 * Get the entry at index 'entry' in a FAT (12/16/32) table.
134 * On failure 0x00 is returned.
135 * When bufnum is changed, write back the previous fatbuf to the disk.
137 static __u32
get_fatent_value(fsdata
*mydata
, __u32 entry
)
144 if (CHECK_CLUST(entry
, mydata
->fatsize
)) {
145 printf("Error: Invalid FAT entry: 0x%08x\n", entry
);
149 switch (mydata
->fatsize
) {
151 bufnum
= entry
/ FAT32BUFSIZE
;
152 offset
= entry
- bufnum
* FAT32BUFSIZE
;
155 bufnum
= entry
/ FAT16BUFSIZE
;
156 offset
= entry
- bufnum
* FAT16BUFSIZE
;
159 bufnum
= entry
/ FAT12BUFSIZE
;
160 offset
= entry
- bufnum
* FAT12BUFSIZE
;
164 /* Unsupported FAT size */
168 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
169 mydata
->fatsize
, entry
, entry
, offset
, offset
);
171 /* Read a new block of FAT entries into the cache. */
172 if (bufnum
!= mydata
->fatbufnum
) {
173 int getsize
= FATBUFBLOCKS
;
174 __u8
*bufptr
= mydata
->fatbuf
;
175 __u32 fatlength
= mydata
->fatlength
;
176 __u32 startblock
= bufnum
* FATBUFBLOCKS
;
178 if (getsize
> fatlength
)
181 fatlength
*= mydata
->sect_size
; /* We want it in bytes now */
182 startblock
+= mydata
->fat_sect
; /* Offset from start of disk */
184 /* Write back the fatbuf to the disk */
185 if (mydata
->fatbufnum
!= -1) {
186 if (flush_fat_buffer(mydata
) < 0)
190 if (disk_read(startblock
, getsize
, bufptr
) < 0) {
191 debug("Error reading FAT blocks\n");
194 mydata
->fatbufnum
= bufnum
;
197 /* Get the actual entry from the table */
198 switch (mydata
->fatsize
) {
200 ret
= FAT2CPU32(((__u32
*) mydata
->fatbuf
)[offset
]);
203 ret
= FAT2CPU16(((__u16
*) mydata
->fatbuf
)[offset
]);
206 off16
= (offset
* 3) / 4;
208 switch (offset
& 0x3) {
210 ret
= FAT2CPU16(((__u16
*) mydata
->fatbuf
)[off16
]);
214 val1
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
]);
216 val2
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
+ 1]);
218 ret
= (val2
<< 4) | (val1
>> 12);
221 val1
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
]);
223 val2
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
+ 1]);
225 ret
= (val2
<< 8) | (val1
>> 8);
228 ret
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
]);
229 ret
= (ret
& 0xfff0) >> 4;
236 debug("FAT%d: ret: %08x, entry: %08x, offset: %04x\n",
237 mydata
->fatsize
, ret
, entry
, offset
);
243 * Set the file name information from 'name' into 'slotptr',
245 static int str2slot(dir_slot
*slotptr
, const char *name
, int *idx
)
249 for (j
= 0; j
<= 8; j
+= 2) {
250 if (name
[*idx
] == 0x00) {
251 slotptr
->name0_4
[j
] = 0;
252 slotptr
->name0_4
[j
+ 1] = 0;
256 slotptr
->name0_4
[j
] = name
[*idx
];
260 for (j
= 0; j
<= 10; j
+= 2) {
261 if (name
[*idx
] == 0x00) {
262 slotptr
->name5_10
[j
] = 0;
263 slotptr
->name5_10
[j
+ 1] = 0;
267 slotptr
->name5_10
[j
] = name
[*idx
];
271 for (j
= 0; j
<= 2; j
+= 2) {
272 if (name
[*idx
] == 0x00) {
273 slotptr
->name11_12
[j
] = 0;
274 slotptr
->name11_12
[j
+ 1] = 0;
278 slotptr
->name11_12
[j
] = name
[*idx
];
283 if (name
[*idx
] == 0x00)
287 /* Not used characters are filled with 0xff 0xff */
289 for (; end_idx
< 5; end_idx
++) {
290 slotptr
->name0_4
[end_idx
* 2] = 0xff;
291 slotptr
->name0_4
[end_idx
* 2 + 1] = 0xff;
296 for (; end_idx
< 6; end_idx
++) {
297 slotptr
->name5_10
[end_idx
* 2] = 0xff;
298 slotptr
->name5_10
[end_idx
* 2 + 1] = 0xff;
303 for (; end_idx
< 2; end_idx
++) {
304 slotptr
->name11_12
[end_idx
* 2] = 0xff;
305 slotptr
->name11_12
[end_idx
* 2 + 1] = 0xff;
311 static int is_next_clust(fsdata
*mydata
, dir_entry
*dentptr
);
312 static void flush_dir_table(fsdata
*mydata
, dir_entry
**dentptr
);
315 * Fill dir_slot entries with appropriate name, id, and attr
316 * The real directory entry is returned by 'dentptr'
319 fill_dir_slot(fsdata
*mydata
, dir_entry
**dentptr
, const char *l_name
)
321 dir_slot
*slotptr
= (dir_slot
*)get_contents_vfatname_block
;
322 __u8 counter
= 0, checksum
;
326 /* Get short file name and checksum value */
327 strncpy(s_name
, (*dentptr
)->name
, 16);
328 checksum
= mkcksum((*dentptr
)->name
, (*dentptr
)->ext
);
331 memset(slotptr
, 0x00, sizeof(dir_slot
));
332 ret
= str2slot(slotptr
, l_name
, &idx
);
333 slotptr
->id
= ++counter
;
334 slotptr
->attr
= ATTR_VFAT
;
335 slotptr
->alias_checksum
= checksum
;
340 slotptr
->id
|= LAST_LONG_ENTRY_MASK
;
342 while (counter
>= 1) {
343 if (is_next_clust(mydata
, *dentptr
)) {
344 /* A new cluster is allocated for directory table */
345 flush_dir_table(mydata
, dentptr
);
347 memcpy(*dentptr
, slotptr
, sizeof(dir_slot
));
353 if (is_next_clust(mydata
, *dentptr
)) {
354 /* A new cluster is allocated for directory table */
355 flush_dir_table(mydata
, dentptr
);
359 static __u32 dir_curclust
;
362 * Extract the full long filename starting at 'retdent' (which is really
363 * a slot) into 'l_name'. If successful also copy the real directory entry
365 * If additional adjacent cluster for directory entries is read into memory,
366 * then 'get_contents_vfatname_block' is copied into 'get_dentfromdir_block' and
367 * the location of the real directory entry is returned by 'retdent'
368 * Return 0 on success, -1 otherwise.
371 get_long_file_name(fsdata
*mydata
, int curclust
, __u8
*cluster
,
372 dir_entry
**retdent
, char *l_name
)
375 dir_slot
*slotptr
= (dir_slot
*)(*retdent
);
376 dir_slot
*slotptr2
= NULL
;
377 __u8
*buflimit
= cluster
+ mydata
->sect_size
* ((curclust
== 0) ?
380 __u8 counter
= (slotptr
->id
& ~LAST_LONG_ENTRY_MASK
) & 0xff;
381 int idx
= 0, cur_position
= 0;
383 if (counter
> VFAT_MAXSEQ
) {
384 debug("Error: VFAT name is too long\n");
388 while ((__u8
*)slotptr
< buflimit
) {
391 if (((slotptr
->id
& ~LAST_LONG_ENTRY_MASK
) & 0xff) != counter
)
397 if ((__u8
*)slotptr
>= buflimit
) {
400 curclust
= get_fatent_value(mydata
, dir_curclust
);
401 if (CHECK_CLUST(curclust
, mydata
->fatsize
)) {
402 debug("curclust: 0x%x\n", curclust
);
403 printf("Invalid FAT entry\n");
407 dir_curclust
= curclust
;
409 if (get_cluster(mydata
, curclust
, get_contents_vfatname_block
,
410 mydata
->clust_size
* mydata
->sect_size
) != 0) {
411 debug("Error: reading directory block\n");
415 slotptr2
= (dir_slot
*)get_contents_vfatname_block
;
416 while (counter
> 0) {
417 if (((slotptr2
->id
& ~LAST_LONG_ENTRY_MASK
)
424 /* Save the real directory entry */
425 realdent
= (dir_entry
*)slotptr2
;
426 while ((__u8
*)slotptr2
> get_contents_vfatname_block
) {
428 slot2str(slotptr2
, l_name
, &idx
);
431 /* Save the real directory entry */
432 realdent
= (dir_entry
*)slotptr
;
437 if (slot2str(slotptr
, l_name
, &idx
))
439 } while (!(slotptr
->id
& LAST_LONG_ENTRY_MASK
));
442 if (*l_name
== DELETED_FLAG
)
444 else if (*l_name
== aRING
)
445 *l_name
= DELETED_FLAG
;
448 /* Return the real directory entry */
452 memcpy(get_dentfromdir_block
, get_contents_vfatname_block
,
453 mydata
->clust_size
* mydata
->sect_size
);
454 cur_position
= (__u8
*)realdent
- get_contents_vfatname_block
;
455 *retdent
= (dir_entry
*) &get_dentfromdir_block
[cur_position
];
462 * Set the entry at index 'entry' in a FAT (16/32) table.
464 static int set_fatent_value(fsdata
*mydata
, __u32 entry
, __u32 entry_value
)
466 __u32 bufnum
, offset
;
468 switch (mydata
->fatsize
) {
470 bufnum
= entry
/ FAT32BUFSIZE
;
471 offset
= entry
- bufnum
* FAT32BUFSIZE
;
474 bufnum
= entry
/ FAT16BUFSIZE
;
475 offset
= entry
- bufnum
* FAT16BUFSIZE
;
478 /* Unsupported FAT size */
482 /* Read a new block of FAT entries into the cache. */
483 if (bufnum
!= mydata
->fatbufnum
) {
484 int getsize
= FATBUFBLOCKS
;
485 __u8
*bufptr
= mydata
->fatbuf
;
486 __u32 fatlength
= mydata
->fatlength
;
487 __u32 startblock
= bufnum
* FATBUFBLOCKS
;
489 fatlength
*= mydata
->sect_size
;
490 startblock
+= mydata
->fat_sect
;
492 if (getsize
> fatlength
)
495 if (mydata
->fatbufnum
!= -1) {
496 if (flush_fat_buffer(mydata
) < 0)
500 if (disk_read(startblock
, getsize
, bufptr
) < 0) {
501 debug("Error reading FAT blocks\n");
504 mydata
->fatbufnum
= bufnum
;
507 /* Set the actual entry */
508 switch (mydata
->fatsize
) {
510 ((__u32
*) mydata
->fatbuf
)[offset
] = cpu_to_le32(entry_value
);
513 ((__u16
*) mydata
->fatbuf
)[offset
] = cpu_to_le16(entry_value
);
523 * Determine the entry value at index 'entry' in a FAT (16/32) table
525 static __u32
determine_fatent(fsdata
*mydata
, __u32 entry
)
527 __u32 next_fat
, next_entry
= entry
+ 1;
530 next_fat
= get_fatent_value(mydata
, next_entry
);
532 set_fatent_value(mydata
, entry
, next_entry
);
537 debug("FAT%d: entry: %08x, entry_value: %04x\n",
538 mydata
->fatsize
, entry
, next_entry
);
544 * Write at most 'size' bytes from 'buffer' into the specified cluster.
545 * Return 0 on success, -1 otherwise.
548 set_cluster(fsdata
*mydata
, __u32 clustnum
, __u8
*buffer
,
555 startsect
= mydata
->data_begin
+
556 clustnum
* mydata
->clust_size
;
558 startsect
= mydata
->rootdir_sect
;
560 debug("clustnum: %d, startsect: %d\n", clustnum
, startsect
);
562 if ((size
/ mydata
->sect_size
) > 0) {
563 if (disk_write(startsect
, size
/ mydata
->sect_size
, buffer
) < 0) {
564 debug("Error writing data\n");
569 if (size
% mydata
->sect_size
) {
570 __u8 tmpbuf
[mydata
->sect_size
];
572 idx
= size
/ mydata
->sect_size
;
573 buffer
+= idx
* mydata
->sect_size
;
574 memcpy(tmpbuf
, buffer
, size
% mydata
->sect_size
);
576 if (disk_write(startsect
+ idx
, 1, tmpbuf
) < 0) {
577 debug("Error writing data\n");
588 * Find the first empty cluster
590 static int find_empty_cluster(fsdata
*mydata
)
592 __u32 fat_val
, entry
= 3;
595 fat_val
= get_fatent_value(mydata
, entry
);
605 * Write directory entries in 'get_dentfromdir_block' to block device
607 static void flush_dir_table(fsdata
*mydata
, dir_entry
**dentptr
)
609 int dir_newclust
= 0;
611 if (set_cluster(mydata
, dir_curclust
,
612 get_dentfromdir_block
,
613 mydata
->clust_size
* mydata
->sect_size
) != 0) {
614 printf("error: wrinting directory entry\n");
617 dir_newclust
= find_empty_cluster(mydata
);
618 set_fatent_value(mydata
, dir_curclust
, dir_newclust
);
619 if (mydata
->fatsize
== 32)
620 set_fatent_value(mydata
, dir_newclust
, 0xffffff8);
621 else if (mydata
->fatsize
== 16)
622 set_fatent_value(mydata
, dir_newclust
, 0xfff8);
624 dir_curclust
= dir_newclust
;
626 if (flush_fat_buffer(mydata
) < 0)
629 memset(get_dentfromdir_block
, 0x00,
630 mydata
->clust_size
* mydata
->sect_size
);
632 *dentptr
= (dir_entry
*) get_dentfromdir_block
;
636 * Set empty cluster from 'entry' to the end of a file
638 static int clear_fatent(fsdata
*mydata
, __u32 entry
)
643 fat_val
= get_fatent_value(mydata
, entry
);
645 set_fatent_value(mydata
, entry
, 0);
649 if (fat_val
== 0xfffffff || fat_val
== 0xffff)
655 /* Flush fat buffer */
656 if (flush_fat_buffer(mydata
) < 0)
663 * Write at most 'maxsize' bytes from 'buffer' into
664 * the file associated with 'dentptr'
665 * Update the number of bytes written in *gotsize and return 0
666 * or return -1 on fatal errors.
669 set_contents(fsdata
*mydata
, dir_entry
*dentptr
, __u8
*buffer
,
670 loff_t maxsize
, loff_t
*gotsize
)
672 loff_t filesize
= FAT2CPU32(dentptr
->size
);
673 unsigned int bytesperclust
= mydata
->clust_size
* mydata
->sect_size
;
674 __u32 curclust
= START(dentptr
);
675 __u32 endclust
= 0, newclust
= 0;
679 debug("Filesize: %llu bytes\n", filesize
);
681 if (maxsize
> 0 && filesize
> maxsize
)
684 debug("%llu bytes\n", filesize
);
686 actsize
= bytesperclust
;
689 /* search for consecutive clusters */
690 while (actsize
< filesize
) {
691 newclust
= determine_fatent(mydata
, endclust
);
693 if ((newclust
- 1) != endclust
)
696 if (CHECK_CLUST(newclust
, mydata
->fatsize
)) {
697 debug("curclust: 0x%x\n", newclust
);
698 debug("Invalid FAT entry\n");
702 actsize
+= bytesperclust
;
704 /* actsize >= file size */
705 actsize
-= bytesperclust
;
706 /* set remaining clusters */
707 if (set_cluster(mydata
, curclust
, buffer
, (int)actsize
) != 0) {
708 debug("error: writing cluster\n");
712 /* set remaining bytes */
718 if (set_cluster(mydata
, endclust
, buffer
, (int)actsize
) != 0) {
719 debug("error: writing cluster\n");
724 /* Mark end of file in FAT */
725 if (mydata
->fatsize
== 16)
727 else if (mydata
->fatsize
== 32)
728 newclust
= 0xfffffff;
729 set_fatent_value(mydata
, endclust
, newclust
);
733 if (set_cluster(mydata
, curclust
, buffer
, (int)actsize
) != 0) {
734 debug("error: writing cluster\n");
741 if (CHECK_CLUST(curclust
, mydata
->fatsize
)) {
742 debug("curclust: 0x%x\n", curclust
);
743 debug("Invalid FAT entry\n");
746 actsize
= bytesperclust
;
747 curclust
= endclust
= newclust
;
754 static void fill_dentry(fsdata
*mydata
, dir_entry
*dentptr
,
755 const char *filename
, __u32 start_cluster
, __u32 size
, __u8 attr
)
757 if (mydata
->fatsize
== 32)
759 cpu_to_le16((start_cluster
& 0xffff0000) >> 16);
760 dentptr
->start
= cpu_to_le16(start_cluster
& 0xffff);
761 dentptr
->size
= cpu_to_le32(size
);
763 dentptr
->attr
= attr
;
765 set_name(dentptr
, filename
);
769 * Check whether adding a file makes the file system to
770 * exceed the size of the block device
771 * Return -1 when overflow occurs, otherwise return 0
773 static int check_overflow(fsdata
*mydata
, __u32 clustnum
, loff_t size
)
775 __u32 startsect
, sect_num
, offset
;
778 startsect
= mydata
->data_begin
+
779 clustnum
* mydata
->clust_size
;
781 startsect
= mydata
->rootdir_sect
;
784 sect_num
= div_u64_rem(size
, mydata
->sect_size
, &offset
);
789 if (startsect
+ sect_num
> cur_part_info
.start
+ total_sector
)
795 * Check if adding several entries exceed one cluster boundary
797 static int is_next_clust(fsdata
*mydata
, dir_entry
*dentptr
)
801 cur_position
= (__u8
*)dentptr
- get_dentfromdir_block
;
803 if (cur_position
>= mydata
->clust_size
* mydata
->sect_size
)
809 static dir_entry
*empty_dentptr
;
811 * Find a directory entry based on filename or start cluster number
812 * If the directory entry is not found,
813 * the new position for writing a directory entry will be returned
815 static dir_entry
*find_directory_entry(fsdata
*mydata
, int startsect
,
816 char *filename
, dir_entry
*retdent
, __u32 start
)
818 __u32 curclust
= (startsect
- mydata
->data_begin
) / mydata
->clust_size
;
820 debug("get_dentfromdir: %s\n", filename
);
827 if (get_cluster(mydata
, curclust
, get_dentfromdir_block
,
828 mydata
->clust_size
* mydata
->sect_size
) != 0) {
829 printf("Error: reading directory block\n");
833 dentptr
= (dir_entry
*)get_dentfromdir_block
;
835 dir_curclust
= curclust
;
837 for (i
= 0; i
< DIRENTSPERCLUST
; i
++) {
838 char s_name
[14], l_name
[VFAT_MAXLEN_BYTES
];
841 if (dentptr
->name
[0] == DELETED_FLAG
) {
843 if (is_next_clust(mydata
, dentptr
))
847 if ((dentptr
->attr
& ATTR_VOLUME
)) {
849 (dentptr
->attr
& ATTR_VFAT
) &&
850 (dentptr
->name
[0] & LAST_LONG_ENTRY_MASK
)) {
851 get_long_file_name(mydata
, curclust
,
852 get_dentfromdir_block
,
854 debug("vfatname: |%s|\n", l_name
);
856 /* Volume label or VFAT entry */
858 if (is_next_clust(mydata
, dentptr
))
863 if (dentptr
->name
[0] == 0) {
864 debug("Dentname == NULL - %d\n", i
);
865 empty_dentptr
= dentptr
;
869 get_name(dentptr
, s_name
);
871 if (strcmp(filename
, s_name
)
872 && strcmp(filename
, l_name
)) {
873 debug("Mismatch: |%s|%s|\n",
876 if (is_next_clust(mydata
, dentptr
))
881 memcpy(retdent
, dentptr
, sizeof(dir_entry
));
883 debug("DentName: %s", s_name
);
884 debug(", start: 0x%x", START(dentptr
));
885 debug(", size: 0x%x %s\n",
886 FAT2CPU32(dentptr
->size
),
887 (dentptr
->attr
& ATTR_DIR
) ?
894 * In FAT16/12, the root dir is locate before data area, shows
896 * -------------------------------------------------------------
897 * | Boot | FAT1 & 2 | Root dir | Data (start from cluster #2) |
898 * -------------------------------------------------------------
900 * As a result if curclust is in Root dir, it is a negative
904 if (mydata
->fatsize
!= 32 && (int)curclust
<= 1) {
905 /* Current clust is in root dir, set to next clust */
907 if ((int)curclust
<= 1)
908 continue; /* continue to find */
910 /* Reach the end of root dir */
911 empty_dentptr
= dentptr
;
915 curclust
= get_fatent_value(mydata
, dir_curclust
);
916 if (IS_LAST_CLUST(curclust
, mydata
->fatsize
)) {
917 empty_dentptr
= dentptr
;
920 if (CHECK_CLUST(curclust
, mydata
->fatsize
)) {
921 debug("curclust: 0x%x\n", curclust
);
922 debug("Invalid FAT entry\n");
930 static int do_fat_write(const char *filename
, void *buffer
, loff_t size
,
933 dir_entry
*dentptr
, *retdent
;
939 fsdata
*mydata
= &datablock
;
941 int ret
= -1, name_len
;
942 char l_filename
[VFAT_MAXLEN_BYTES
];
947 if (read_bootsectandvi(&bs
, &volinfo
, &mydata
->fatsize
)) {
948 debug("error: reading boot sector\n");
952 total_sector
= bs
.total_sect
;
953 if (total_sector
== 0)
954 total_sector
= (int)cur_part_info
.size
; /* cast of lbaint_t */
956 if (mydata
->fatsize
== 32)
957 mydata
->fatlength
= bs
.fat32_length
;
959 mydata
->fatlength
= bs
.fat_length
;
961 mydata
->fat_sect
= bs
.reserved
;
963 cursect
= mydata
->rootdir_sect
964 = mydata
->fat_sect
+ mydata
->fatlength
* bs
.fats
;
965 num_of_fats
= bs
.fats
;
967 mydata
->sect_size
= (bs
.sector_size
[1] << 8) + bs
.sector_size
[0];
968 mydata
->clust_size
= bs
.cluster_size
;
970 if (mydata
->fatsize
== 32) {
971 mydata
->data_begin
= mydata
->rootdir_sect
-
972 (mydata
->clust_size
* 2);
976 rootdir_size
= ((bs
.dir_entries
[1] * (int)256 +
980 mydata
->data_begin
= mydata
->rootdir_sect
+
982 (mydata
->clust_size
* 2);
985 mydata
->fatbufnum
= -1;
986 mydata
->fatbuf
= memalign(ARCH_DMA_MINALIGN
, FATBUFSIZE
);
987 if (mydata
->fatbuf
== NULL
) {
988 debug("Error: allocating memory\n");
992 if (disk_read(cursect
,
993 (mydata
->fatsize
== 32) ?
994 (mydata
->clust_size
) :
995 PREFETCH_BLOCKS
, do_fat_read_at_block
) < 0) {
996 debug("Error: reading rootdir block\n");
999 dentptr
= (dir_entry
*) do_fat_read_at_block
;
1001 name_len
= strlen(filename
);
1002 if (name_len
>= VFAT_MAXLEN_BYTES
)
1003 name_len
= VFAT_MAXLEN_BYTES
- 1;
1005 memcpy(l_filename
, filename
, name_len
);
1006 l_filename
[name_len
] = 0; /* terminate the string */
1007 downcase(l_filename
);
1009 startsect
= mydata
->rootdir_sect
;
1010 retdent
= find_directory_entry(mydata
, startsect
,
1011 l_filename
, dentptr
, 0);
1013 /* Update file size and start_cluster in a directory entry */
1014 retdent
->size
= cpu_to_le32(size
);
1015 start_cluster
= FAT2CPU16(retdent
->start
);
1016 if (mydata
->fatsize
== 32)
1018 (FAT2CPU16(retdent
->starthi
) << 16);
1020 ret
= check_overflow(mydata
, start_cluster
, size
);
1022 printf("Error: %llu overflow\n", size
);
1026 ret
= clear_fatent(mydata
, start_cluster
);
1028 printf("Error: clearing FAT entries\n");
1032 ret
= set_contents(mydata
, retdent
, buffer
, size
, actwrite
);
1034 printf("Error: writing contents\n");
1037 debug("attempt to write 0x%llx bytes\n", *actwrite
);
1039 /* Flush fat buffer */
1040 ret
= flush_fat_buffer(mydata
);
1042 printf("Error: flush fat buffer\n");
1046 /* Write directory table to device */
1047 ret
= set_cluster(mydata
, dir_curclust
,
1048 get_dentfromdir_block
,
1049 mydata
->clust_size
* mydata
->sect_size
);
1051 printf("Error: writing directory entry\n");
1055 /* Set short name to set alias checksum field in dir_slot */
1056 set_name(empty_dentptr
, filename
);
1057 fill_dir_slot(mydata
, &empty_dentptr
, filename
);
1059 ret
= start_cluster
= find_empty_cluster(mydata
);
1061 printf("Error: finding empty cluster\n");
1065 ret
= check_overflow(mydata
, start_cluster
, size
);
1067 printf("Error: %llu overflow\n", size
);
1071 /* Set attribute as archieve for regular file */
1072 fill_dentry(mydata
, empty_dentptr
, filename
,
1073 start_cluster
, size
, 0x20);
1075 ret
= set_contents(mydata
, empty_dentptr
, buffer
, size
,
1078 printf("Error: writing contents\n");
1081 debug("attempt to write 0x%llx bytes\n", *actwrite
);
1083 /* Flush fat buffer */
1084 ret
= flush_fat_buffer(mydata
);
1086 printf("Error: flush fat buffer\n");
1090 /* Write directory table to device */
1091 ret
= set_cluster(mydata
, dir_curclust
,
1092 get_dentfromdir_block
,
1093 mydata
->clust_size
* mydata
->sect_size
);
1095 printf("Error: writing directory entry\n");
1101 free(mydata
->fatbuf
);
1105 int file_fat_write(const char *filename
, void *buffer
, loff_t offset
,
1106 loff_t maxsize
, loff_t
*actwrite
)
1109 printf("Error: non zero offset is currently not suported.\n");
1113 printf("writing %s\n", filename
);
1114 return do_fat_write(filename
, buffer
, maxsize
, actwrite
);