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)
35 if (!cur_dev || !cur_dev->block_write)
38 if (cur_part_info.start + block + nr_blocks >
39 cur_part_info.start + total_sector) {
40 printf("error: overflow occurs\n");
44 ret = cur_dev->block_write(cur_dev, cur_part_info.start + block,
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);
104 static __u8 num_of_fats;
106 * Write fat buffer into block device
108 static int flush_fat_buffer(fsdata *mydata)
110 int getsize = FATBUFBLOCKS;
111 __u32 fatlength = mydata->fatlength;
112 __u8 *bufptr = mydata->fatbuf;
113 __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
115 startblock += mydata->fat_sect;
117 if (getsize > fatlength)
121 if (disk_write(startblock, getsize, bufptr) < 0) {
122 debug("error: writing FAT blocks\n");
126 if (num_of_fats == 2) {
127 /* Update corresponding second FAT blocks */
128 startblock += mydata->fatlength;
129 if (disk_write(startblock, getsize, bufptr) < 0) {
130 debug("error: writing second FAT blocks\n");
139 * Get the entry at index 'entry' in a FAT (12/16/32) table.
140 * On failure 0x00 is returned.
141 * When bufnum is changed, write back the previous fatbuf to the disk.
143 static __u32 get_fatent_value(fsdata *mydata, __u32 entry)
150 if (CHECK_CLUST(entry, mydata->fatsize)) {
151 printf("Error: Invalid FAT entry: 0x%08x\n", entry);
155 switch (mydata->fatsize) {
157 bufnum = entry / FAT32BUFSIZE;
158 offset = entry - bufnum * FAT32BUFSIZE;
161 bufnum = entry / FAT16BUFSIZE;
162 offset = entry - bufnum * FAT16BUFSIZE;
165 bufnum = entry / FAT12BUFSIZE;
166 offset = entry - bufnum * FAT12BUFSIZE;
170 /* Unsupported FAT size */
174 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
175 mydata->fatsize, entry, entry, offset, offset);
177 /* Read a new block of FAT entries into the cache. */
178 if (bufnum != mydata->fatbufnum) {
179 int getsize = FATBUFBLOCKS;
180 __u8 *bufptr = mydata->fatbuf;
181 __u32 fatlength = mydata->fatlength;
182 __u32 startblock = bufnum * FATBUFBLOCKS;
184 if (getsize > fatlength)
187 fatlength *= mydata->sect_size; /* We want it in bytes now */
188 startblock += mydata->fat_sect; /* Offset from start of disk */
190 /* Write back the fatbuf to the disk */
191 if (mydata->fatbufnum != -1) {
192 if (flush_fat_buffer(mydata) < 0)
196 if (disk_read(startblock, getsize, bufptr) < 0) {
197 debug("Error reading FAT blocks\n");
200 mydata->fatbufnum = bufnum;
203 /* Get the actual entry from the table */
204 switch (mydata->fatsize) {
206 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
209 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
212 off16 = (offset * 3) / 4;
214 switch (offset & 0x3) {
216 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
220 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
222 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
224 ret = (val2 << 4) | (val1 >> 12);
227 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
229 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
231 ret = (val2 << 8) | (val1 >> 8);
234 ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
235 ret = (ret & 0xfff0) >> 4;
242 debug("FAT%d: ret: %08x, entry: %08x, offset: %04x\n",
243 mydata->fatsize, ret, entry, offset);
249 * Set the file name information from 'name' into 'slotptr',
251 static int str2slot(dir_slot *slotptr, const char *name, int *idx)
255 for (j = 0; j <= 8; j += 2) {
256 if (name[*idx] == 0x00) {
257 slotptr->name0_4[j] = 0;
258 slotptr->name0_4[j + 1] = 0;
262 slotptr->name0_4[j] = name[*idx];
266 for (j = 0; j <= 10; j += 2) {
267 if (name[*idx] == 0x00) {
268 slotptr->name5_10[j] = 0;
269 slotptr->name5_10[j + 1] = 0;
273 slotptr->name5_10[j] = name[*idx];
277 for (j = 0; j <= 2; j += 2) {
278 if (name[*idx] == 0x00) {
279 slotptr->name11_12[j] = 0;
280 slotptr->name11_12[j + 1] = 0;
284 slotptr->name11_12[j] = name[*idx];
289 if (name[*idx] == 0x00)
293 /* Not used characters are filled with 0xff 0xff */
295 for (; end_idx < 5; end_idx++) {
296 slotptr->name0_4[end_idx * 2] = 0xff;
297 slotptr->name0_4[end_idx * 2 + 1] = 0xff;
302 for (; end_idx < 6; end_idx++) {
303 slotptr->name5_10[end_idx * 2] = 0xff;
304 slotptr->name5_10[end_idx * 2 + 1] = 0xff;
309 for (; end_idx < 2; end_idx++) {
310 slotptr->name11_12[end_idx * 2] = 0xff;
311 slotptr->name11_12[end_idx * 2 + 1] = 0xff;
317 static int is_next_clust(fsdata *mydata, dir_entry *dentptr);
318 static void flush_dir_table(fsdata *mydata, dir_entry **dentptr);
321 * Fill dir_slot entries with appropriate name, id, and attr
322 * The real directory entry is returned by 'dentptr'
325 fill_dir_slot(fsdata *mydata, dir_entry **dentptr, const char *l_name)
327 dir_slot *slotptr = (dir_slot *)get_contents_vfatname_block;
328 __u8 counter = 0, checksum;
332 /* Get short file name and checksum value */
333 strncpy(s_name, (*dentptr)->name, 16);
334 checksum = mkcksum((*dentptr)->name, (*dentptr)->ext);
337 memset(slotptr, 0x00, sizeof(dir_slot));
338 ret = str2slot(slotptr, l_name, &idx);
339 slotptr->id = ++counter;
340 slotptr->attr = ATTR_VFAT;
341 slotptr->alias_checksum = checksum;
346 slotptr->id |= LAST_LONG_ENTRY_MASK;
348 while (counter >= 1) {
349 if (is_next_clust(mydata, *dentptr)) {
350 /* A new cluster is allocated for directory table */
351 flush_dir_table(mydata, dentptr);
353 memcpy(*dentptr, slotptr, sizeof(dir_slot));
359 if (is_next_clust(mydata, *dentptr)) {
360 /* A new cluster is allocated for directory table */
361 flush_dir_table(mydata, dentptr);
365 static __u32 dir_curclust;
368 * Extract the full long filename starting at 'retdent' (which is really
369 * a slot) into 'l_name'. If successful also copy the real directory entry
371 * If additional adjacent cluster for directory entries is read into memory,
372 * then 'get_contents_vfatname_block' is copied into 'get_dentfromdir_block' and
373 * the location of the real directory entry is returned by 'retdent'
374 * Return 0 on success, -1 otherwise.
377 get_long_file_name(fsdata *mydata, int curclust, __u8 *cluster,
378 dir_entry **retdent, char *l_name)
381 dir_slot *slotptr = (dir_slot *)(*retdent);
382 dir_slot *slotptr2 = NULL;
383 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
386 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
387 int idx = 0, cur_position = 0;
389 if (counter > VFAT_MAXSEQ) {
390 debug("Error: VFAT name is too long\n");
394 while ((__u8 *)slotptr < buflimit) {
397 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
403 if ((__u8 *)slotptr >= buflimit) {
406 curclust = get_fatent_value(mydata, dir_curclust);
407 if (CHECK_CLUST(curclust, mydata->fatsize)) {
408 debug("curclust: 0x%x\n", curclust);
409 printf("Invalid FAT entry\n");
413 dir_curclust = curclust;
415 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
416 mydata->clust_size * mydata->sect_size) != 0) {
417 debug("Error: reading directory block\n");
421 slotptr2 = (dir_slot *)get_contents_vfatname_block;
422 while (counter > 0) {
423 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
430 /* Save the real directory entry */
431 realdent = (dir_entry *)slotptr2;
432 while ((__u8 *)slotptr2 > get_contents_vfatname_block) {
434 slot2str(slotptr2, l_name, &idx);
437 /* Save the real directory entry */
438 realdent = (dir_entry *)slotptr;
443 if (slot2str(slotptr, l_name, &idx))
445 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
448 if (*l_name == DELETED_FLAG)
450 else if (*l_name == aRING)
451 *l_name = DELETED_FLAG;
454 /* Return the real directory entry */
458 memcpy(get_dentfromdir_block, get_contents_vfatname_block,
459 mydata->clust_size * mydata->sect_size);
460 cur_position = (__u8 *)realdent - get_contents_vfatname_block;
461 *retdent = (dir_entry *) &get_dentfromdir_block[cur_position];
468 * Set the entry at index 'entry' in a FAT (16/32) table.
470 static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
472 __u32 bufnum, offset;
474 switch (mydata->fatsize) {
476 bufnum = entry / FAT32BUFSIZE;
477 offset = entry - bufnum * FAT32BUFSIZE;
480 bufnum = entry / FAT16BUFSIZE;
481 offset = entry - bufnum * FAT16BUFSIZE;
484 /* Unsupported FAT size */
488 /* Read a new block of FAT entries into the cache. */
489 if (bufnum != mydata->fatbufnum) {
490 int getsize = FATBUFBLOCKS;
491 __u8 *bufptr = mydata->fatbuf;
492 __u32 fatlength = mydata->fatlength;
493 __u32 startblock = bufnum * FATBUFBLOCKS;
495 fatlength *= mydata->sect_size;
496 startblock += mydata->fat_sect;
498 if (getsize > fatlength)
501 if (mydata->fatbufnum != -1) {
502 if (flush_fat_buffer(mydata) < 0)
506 if (disk_read(startblock, getsize, bufptr) < 0) {
507 debug("Error reading FAT blocks\n");
510 mydata->fatbufnum = bufnum;
513 /* Set the actual entry */
514 switch (mydata->fatsize) {
516 ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
519 ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
529 * Determine the entry value at index 'entry' in a FAT (16/32) table
531 static __u32 determine_fatent(fsdata *mydata, __u32 entry)
533 __u32 next_fat, next_entry = entry + 1;
536 next_fat = get_fatent_value(mydata, next_entry);
538 set_fatent_value(mydata, entry, next_entry);
543 debug("FAT%d: entry: %08x, entry_value: %04x\n",
544 mydata->fatsize, entry, next_entry);
550 * Write at most 'size' bytes from 'buffer' into the specified cluster.
551 * Return 0 on success, -1 otherwise.
554 set_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer,
562 startsect = mydata->data_begin +
563 clustnum * mydata->clust_size;
565 startsect = mydata->rootdir_sect;
567 debug("clustnum: %d, startsect: %d\n", clustnum, startsect);
569 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
570 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
572 printf("FAT: Misaligned buffer address (%p)\n", buffer);
574 while (size >= mydata->sect_size) {
575 memcpy(tmpbuf, buffer, mydata->sect_size);
576 ret = disk_write(startsect++, 1, tmpbuf);
578 debug("Error writing data (got %d)\n", ret);
582 buffer += mydata->sect_size;
583 size -= mydata->sect_size;
585 } else if (size >= mydata->sect_size) {
586 idx = size / mydata->sect_size;
587 ret = disk_write(startsect, idx, buffer);
589 debug("Error writing data (got %d)\n", ret);
594 idx *= mydata->sect_size;
600 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
602 memcpy(tmpbuf, buffer, size);
603 ret = disk_write(startsect, 1, tmpbuf);
605 debug("Error writing data (got %d)\n", ret);
614 * Find the first empty cluster
616 static int find_empty_cluster(fsdata *mydata)
618 __u32 fat_val, entry = 3;
621 fat_val = get_fatent_value(mydata, entry);
631 * Write directory entries in 'get_dentfromdir_block' to block device
633 static void flush_dir_table(fsdata *mydata, dir_entry **dentptr)
635 int dir_newclust = 0;
637 if (set_cluster(mydata, dir_curclust,
638 get_dentfromdir_block,
639 mydata->clust_size * mydata->sect_size) != 0) {
640 printf("error: wrinting directory entry\n");
643 dir_newclust = find_empty_cluster(mydata);
644 set_fatent_value(mydata, dir_curclust, dir_newclust);
645 if (mydata->fatsize == 32)
646 set_fatent_value(mydata, dir_newclust, 0xffffff8);
647 else if (mydata->fatsize == 16)
648 set_fatent_value(mydata, dir_newclust, 0xfff8);
650 dir_curclust = dir_newclust;
652 if (flush_fat_buffer(mydata) < 0)
655 memset(get_dentfromdir_block, 0x00,
656 mydata->clust_size * mydata->sect_size);
658 *dentptr = (dir_entry *) get_dentfromdir_block;
662 * Set empty cluster from 'entry' to the end of a file
664 static int clear_fatent(fsdata *mydata, __u32 entry)
669 fat_val = get_fatent_value(mydata, entry);
671 set_fatent_value(mydata, entry, 0);
675 if (fat_val == 0xfffffff || fat_val == 0xffff)
681 /* Flush fat buffer */
682 if (flush_fat_buffer(mydata) < 0)
689 * Write at most 'maxsize' bytes from 'buffer' into
690 * the file associated with 'dentptr'
691 * Update the number of bytes written in *gotsize and return 0
692 * or return -1 on fatal errors.
695 set_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
696 loff_t maxsize, loff_t *gotsize)
698 loff_t filesize = FAT2CPU32(dentptr->size);
699 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
700 __u32 curclust = START(dentptr);
701 __u32 endclust = 0, newclust = 0;
705 debug("Filesize: %llu bytes\n", filesize);
707 if (maxsize > 0 && filesize > maxsize)
710 debug("%llu bytes\n", filesize);
714 debug("error: nonempty clusterless file!\n");
720 actsize = bytesperclust;
723 /* search for consecutive clusters */
724 while (actsize < filesize) {
725 newclust = determine_fatent(mydata, endclust);
727 if ((newclust - 1) != endclust)
730 if (CHECK_CLUST(newclust, mydata->fatsize)) {
731 debug("newclust: 0x%x\n", newclust);
732 debug("Invalid FAT entry\n");
736 actsize += bytesperclust;
739 /* set remaining bytes */
741 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
742 debug("error: writing cluster\n");
747 /* Mark end of file in FAT */
748 if (mydata->fatsize == 16)
750 else if (mydata->fatsize == 32)
751 newclust = 0xfffffff;
752 set_fatent_value(mydata, endclust, newclust);
756 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
757 debug("error: writing cluster\n");
764 if (CHECK_CLUST(newclust, mydata->fatsize)) {
765 debug("newclust: 0x%x\n", newclust);
766 debug("Invalid FAT entry\n");
769 actsize = bytesperclust;
770 curclust = endclust = newclust;
775 * Set start cluster in directory entry
777 static void set_start_cluster(const fsdata *mydata, dir_entry *dentptr,
780 if (mydata->fatsize == 32)
782 cpu_to_le16((start_cluster & 0xffff0000) >> 16);
783 dentptr->start = cpu_to_le16(start_cluster & 0xffff);
789 static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
790 const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
792 set_start_cluster(mydata, dentptr, start_cluster);
793 dentptr->size = cpu_to_le32(size);
795 dentptr->attr = attr;
797 set_name(dentptr, filename);
801 * Check whether adding a file makes the file system to
802 * exceed the size of the block device
803 * Return -1 when overflow occurs, otherwise return 0
805 static int check_overflow(fsdata *mydata, __u32 clustnum, loff_t size)
807 __u32 startsect, sect_num, offset;
810 startsect = mydata->data_begin +
811 clustnum * mydata->clust_size;
813 startsect = mydata->rootdir_sect;
816 sect_num = div_u64_rem(size, mydata->sect_size, &offset);
821 if (startsect + sect_num > cur_part_info.start + total_sector)
827 * Check if adding several entries exceed one cluster boundary
829 static int is_next_clust(fsdata *mydata, dir_entry *dentptr)
833 cur_position = (__u8 *)dentptr - get_dentfromdir_block;
835 if (cur_position >= mydata->clust_size * mydata->sect_size)
841 static dir_entry *empty_dentptr;
843 * Find a directory entry based on filename or start cluster number
844 * If the directory entry is not found,
845 * the new position for writing a directory entry will be returned
847 static dir_entry *find_directory_entry(fsdata *mydata, int startsect,
848 char *filename, dir_entry *retdent, __u32 start)
850 __u32 curclust = (startsect - mydata->data_begin) / mydata->clust_size;
852 debug("get_dentfromdir: %s\n", filename);
859 if (get_cluster(mydata, curclust, get_dentfromdir_block,
860 mydata->clust_size * mydata->sect_size) != 0) {
861 printf("Error: reading directory block\n");
865 dentptr = (dir_entry *)get_dentfromdir_block;
867 dir_curclust = curclust;
869 for (i = 0; i < DIRENTSPERCLUST; i++) {
870 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
873 if (dentptr->name[0] == DELETED_FLAG) {
875 if (is_next_clust(mydata, dentptr))
879 if ((dentptr->attr & ATTR_VOLUME)) {
881 (dentptr->attr & ATTR_VFAT) &&
882 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
883 get_long_file_name(mydata, curclust,
884 get_dentfromdir_block,
886 debug("vfatname: |%s|\n", l_name);
888 /* Volume label or VFAT entry */
890 if (is_next_clust(mydata, dentptr))
895 if (dentptr->name[0] == 0) {
896 debug("Dentname == NULL - %d\n", i);
897 empty_dentptr = dentptr;
901 get_name(dentptr, s_name);
903 if (strcmp(filename, s_name)
904 && strcmp(filename, l_name)) {
905 debug("Mismatch: |%s|%s|\n",
908 if (is_next_clust(mydata, dentptr))
913 memcpy(retdent, dentptr, sizeof(dir_entry));
915 debug("DentName: %s", s_name);
916 debug(", start: 0x%x", START(dentptr));
917 debug(", size: 0x%x %s\n",
918 FAT2CPU32(dentptr->size),
919 (dentptr->attr & ATTR_DIR) ?
926 * In FAT16/12, the root dir is locate before data area, shows
928 * -------------------------------------------------------------
929 * | Boot | FAT1 & 2 | Root dir | Data (start from cluster #2) |
930 * -------------------------------------------------------------
932 * As a result if curclust is in Root dir, it is a negative
936 if (mydata->fatsize != 32 && (int)curclust <= 1) {
937 /* Current clust is in root dir, set to next clust */
939 if ((int)curclust <= 1)
940 continue; /* continue to find */
942 /* Reach the end of root dir */
943 empty_dentptr = dentptr;
947 curclust = get_fatent_value(mydata, dir_curclust);
948 if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
949 empty_dentptr = dentptr;
952 if (CHECK_CLUST(curclust, mydata->fatsize)) {
953 debug("curclust: 0x%x\n", curclust);
954 debug("Invalid FAT entry\n");
962 static int do_fat_write(const char *filename, void *buffer, loff_t size,
965 dir_entry *dentptr, *retdent;
971 fsdata *mydata = &datablock;
973 int ret = -1, name_len;
974 char l_filename[VFAT_MAXLEN_BYTES];
979 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
980 debug("error: reading boot sector\n");
984 total_sector = bs.total_sect;
985 if (total_sector == 0)
986 total_sector = (int)cur_part_info.size; /* cast of lbaint_t */
988 if (mydata->fatsize == 32)
989 mydata->fatlength = bs.fat32_length;
991 mydata->fatlength = bs.fat_length;
993 mydata->fat_sect = bs.reserved;
995 cursect = mydata->rootdir_sect
996 = mydata->fat_sect + mydata->fatlength * bs.fats;
997 num_of_fats = bs.fats;
999 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
1000 mydata->clust_size = bs.cluster_size;
1002 if (mydata->fatsize == 32) {
1003 mydata->data_begin = mydata->rootdir_sect -
1004 (mydata->clust_size * 2);
1008 rootdir_size = ((bs.dir_entries[1] * (int)256 +
1009 bs.dir_entries[0]) *
1010 sizeof(dir_entry)) /
1012 mydata->data_begin = mydata->rootdir_sect +
1014 (mydata->clust_size * 2);
1017 mydata->fatbufnum = -1;
1018 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
1019 if (mydata->fatbuf == NULL) {
1020 debug("Error: allocating memory\n");
1024 if (disk_read(cursect,
1025 (mydata->fatsize == 32) ?
1026 (mydata->clust_size) :
1027 PREFETCH_BLOCKS, do_fat_read_at_block) < 0) {
1028 debug("Error: reading rootdir block\n");
1031 dentptr = (dir_entry *) do_fat_read_at_block;
1033 name_len = strlen(filename);
1034 if (name_len >= VFAT_MAXLEN_BYTES)
1035 name_len = VFAT_MAXLEN_BYTES - 1;
1037 memcpy(l_filename, filename, name_len);
1038 l_filename[name_len] = 0; /* terminate the string */
1039 downcase(l_filename);
1041 startsect = mydata->rootdir_sect;
1042 retdent = find_directory_entry(mydata, startsect,
1043 l_filename, dentptr, 0);
1045 /* Update file size and start_cluster in a directory entry */
1046 retdent->size = cpu_to_le32(size);
1047 start_cluster = START(retdent);
1049 if (start_cluster) {
1051 ret = check_overflow(mydata, start_cluster,
1054 printf("Error: %llu overflow\n", size);
1059 ret = clear_fatent(mydata, start_cluster);
1061 printf("Error: clearing FAT entries\n");
1066 set_start_cluster(mydata, retdent, 0);
1068 ret = start_cluster = find_empty_cluster(mydata);
1070 printf("Error: finding empty cluster\n");
1074 ret = check_overflow(mydata, start_cluster, size);
1076 printf("Error: %llu overflow\n", size);
1080 set_start_cluster(mydata, retdent, start_cluster);
1083 /* Set short name to set alias checksum field in dir_slot */
1084 set_name(empty_dentptr, filename);
1085 fill_dir_slot(mydata, &empty_dentptr, filename);
1088 ret = start_cluster = find_empty_cluster(mydata);
1090 printf("Error: finding empty cluster\n");
1094 ret = check_overflow(mydata, start_cluster, size);
1096 printf("Error: %llu overflow\n", size);
1103 /* Set attribute as archieve for regular file */
1104 fill_dentry(mydata, empty_dentptr, filename,
1105 start_cluster, size, 0x20);
1107 retdent = empty_dentptr;
1110 ret = set_contents(mydata, retdent, buffer, size, actwrite);
1112 printf("Error: writing contents\n");
1115 debug("attempt to write 0x%llx bytes\n", *actwrite);
1117 /* Flush fat buffer */
1118 ret = flush_fat_buffer(mydata);
1120 printf("Error: flush fat buffer\n");
1124 /* Write directory table to device */
1125 ret = set_cluster(mydata, dir_curclust, get_dentfromdir_block,
1126 mydata->clust_size * mydata->sect_size);
1128 printf("Error: writing directory entry\n");
1131 free(mydata->fatbuf);
1135 int file_fat_write(const char *filename, void *buffer, loff_t offset,
1136 loff_t maxsize, loff_t *actwrite)
1139 printf("Error: non zero offset is currently not suported.\n");
1143 printf("writing %s\n", filename);
1144 return do_fat_write(filename, buffer, maxsize, actwrite);