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)
38 if (cur_part_info.start + block + nr_blocks >
39 cur_part_info.start + total_sector) {
40 printf("error: overflow occurs\n");
44 ret = blk_dwrite(cur_dev, cur_part_info.start + block, nr_blocks, buf);
45 if (nr_blocks && ret == 0)
52 * Set short name in directory entry
54 static void set_name(dir_entry *dirent, const char *filename)
56 char s_name[VFAT_MAXLEN_BYTES];
58 int period_location, len, i, ext_num;
63 len = strlen(filename);
67 strcpy(s_name, filename);
68 uppercase(s_name, len);
70 period = strchr(s_name, '.');
72 period_location = len;
75 period_location = period - s_name;
76 ext_num = len - period_location - 1;
79 /* Pad spaces when the length of file name is shorter than eight */
80 if (period_location < 8) {
81 memcpy(dirent->name, s_name, period_location);
82 for (i = period_location; i < 8; i++)
83 dirent->name[i] = ' ';
84 } else if (period_location == 8) {
85 memcpy(dirent->name, s_name, period_location);
87 memcpy(dirent->name, s_name, 6);
88 dirent->name[6] = '~';
89 dirent->name[7] = '1';
93 memcpy(dirent->ext, s_name + period_location + 1, ext_num);
94 for (i = ext_num; i < 3; i++)
97 memcpy(dirent->ext, s_name + period_location + 1, 3);
99 debug("name : %s\n", dirent->name);
100 debug("ext : %s\n", dirent->ext);
103 static __u8 num_of_fats;
105 * Write fat buffer into block device
107 static int flush_fat_buffer(fsdata *mydata)
109 int getsize = FATBUFBLOCKS;
110 __u32 fatlength = mydata->fatlength;
111 __u8 *bufptr = mydata->fatbuf;
112 __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
114 startblock += mydata->fat_sect;
116 if (getsize > fatlength)
120 if (disk_write(startblock, getsize, bufptr) < 0) {
121 debug("error: writing FAT blocks\n");
125 if (num_of_fats == 2) {
126 /* Update corresponding second FAT blocks */
127 startblock += mydata->fatlength;
128 if (disk_write(startblock, getsize, bufptr) < 0) {
129 debug("error: writing second FAT blocks\n");
138 * Get the entry at index 'entry' in a FAT (12/16/32) table.
139 * On failure 0x00 is returned.
140 * When bufnum is changed, write back the previous fatbuf to the disk.
142 static __u32 get_fatent_value(fsdata *mydata, __u32 entry)
149 if (CHECK_CLUST(entry, mydata->fatsize)) {
150 printf("Error: Invalid FAT entry: 0x%08x\n", entry);
154 switch (mydata->fatsize) {
156 bufnum = entry / FAT32BUFSIZE;
157 offset = entry - bufnum * FAT32BUFSIZE;
160 bufnum = entry / FAT16BUFSIZE;
161 offset = entry - bufnum * FAT16BUFSIZE;
164 bufnum = entry / FAT12BUFSIZE;
165 offset = entry - bufnum * FAT12BUFSIZE;
169 /* Unsupported FAT size */
173 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
174 mydata->fatsize, entry, entry, offset, offset);
176 /* Read a new block of FAT entries into the cache. */
177 if (bufnum != mydata->fatbufnum) {
178 int getsize = FATBUFBLOCKS;
179 __u8 *bufptr = mydata->fatbuf;
180 __u32 fatlength = mydata->fatlength;
181 __u32 startblock = bufnum * FATBUFBLOCKS;
183 if (getsize > fatlength)
186 fatlength *= mydata->sect_size; /* We want it in bytes now */
187 startblock += mydata->fat_sect; /* Offset from start of disk */
189 /* Write back the fatbuf to the disk */
190 if (mydata->fatbufnum != -1) {
191 if (flush_fat_buffer(mydata) < 0)
195 if (disk_read(startblock, getsize, bufptr) < 0) {
196 debug("Error reading FAT blocks\n");
199 mydata->fatbufnum = bufnum;
202 /* Get the actual entry from the table */
203 switch (mydata->fatsize) {
205 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
208 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
211 off16 = (offset * 3) / 4;
213 switch (offset & 0x3) {
215 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
219 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
221 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
223 ret = (val2 << 4) | (val1 >> 12);
226 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
228 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
230 ret = (val2 << 8) | (val1 >> 8);
233 ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
234 ret = (ret & 0xfff0) >> 4;
241 debug("FAT%d: ret: %08x, entry: %08x, offset: %04x\n",
242 mydata->fatsize, ret, entry, offset);
248 * Set the file name information from 'name' into 'slotptr',
250 static int str2slot(dir_slot *slotptr, const char *name, int *idx)
254 for (j = 0; j <= 8; j += 2) {
255 if (name[*idx] == 0x00) {
256 slotptr->name0_4[j] = 0;
257 slotptr->name0_4[j + 1] = 0;
261 slotptr->name0_4[j] = name[*idx];
265 for (j = 0; j <= 10; j += 2) {
266 if (name[*idx] == 0x00) {
267 slotptr->name5_10[j] = 0;
268 slotptr->name5_10[j + 1] = 0;
272 slotptr->name5_10[j] = name[*idx];
276 for (j = 0; j <= 2; j += 2) {
277 if (name[*idx] == 0x00) {
278 slotptr->name11_12[j] = 0;
279 slotptr->name11_12[j + 1] = 0;
283 slotptr->name11_12[j] = name[*idx];
288 if (name[*idx] == 0x00)
292 /* Not used characters are filled with 0xff 0xff */
294 for (; end_idx < 5; end_idx++) {
295 slotptr->name0_4[end_idx * 2] = 0xff;
296 slotptr->name0_4[end_idx * 2 + 1] = 0xff;
301 for (; end_idx < 6; end_idx++) {
302 slotptr->name5_10[end_idx * 2] = 0xff;
303 slotptr->name5_10[end_idx * 2 + 1] = 0xff;
308 for (; end_idx < 2; end_idx++) {
309 slotptr->name11_12[end_idx * 2] = 0xff;
310 slotptr->name11_12[end_idx * 2 + 1] = 0xff;
316 static int is_next_clust(fsdata *mydata, dir_entry *dentptr);
317 static void flush_dir_table(fsdata *mydata, dir_entry **dentptr);
320 * Fill dir_slot entries with appropriate name, id, and attr
321 * The real directory entry is returned by 'dentptr'
324 fill_dir_slot(fsdata *mydata, dir_entry **dentptr, const char *l_name)
326 dir_slot *slotptr = (dir_slot *)get_contents_vfatname_block;
327 __u8 counter = 0, checksum;
331 /* Get short file name and checksum value */
332 strncpy(s_name, (*dentptr)->name, 16);
333 checksum = mkcksum((*dentptr)->name, (*dentptr)->ext);
336 memset(slotptr, 0x00, sizeof(dir_slot));
337 ret = str2slot(slotptr, l_name, &idx);
338 slotptr->id = ++counter;
339 slotptr->attr = ATTR_VFAT;
340 slotptr->alias_checksum = checksum;
345 slotptr->id |= LAST_LONG_ENTRY_MASK;
347 while (counter >= 1) {
348 if (is_next_clust(mydata, *dentptr)) {
349 /* A new cluster is allocated for directory table */
350 flush_dir_table(mydata, dentptr);
352 memcpy(*dentptr, slotptr, sizeof(dir_slot));
358 if (is_next_clust(mydata, *dentptr)) {
359 /* A new cluster is allocated for directory table */
360 flush_dir_table(mydata, dentptr);
364 static __u32 dir_curclust;
367 * Extract the full long filename starting at 'retdent' (which is really
368 * a slot) into 'l_name'. If successful also copy the real directory entry
370 * If additional adjacent cluster for directory entries is read into memory,
371 * then 'get_contents_vfatname_block' is copied into 'get_dentfromdir_block' and
372 * the location of the real directory entry is returned by 'retdent'
373 * Return 0 on success, -1 otherwise.
376 get_long_file_name(fsdata *mydata, int curclust, __u8 *cluster,
377 dir_entry **retdent, char *l_name)
380 dir_slot *slotptr = (dir_slot *)(*retdent);
381 dir_slot *slotptr2 = NULL;
382 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
385 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
386 int idx = 0, cur_position = 0;
388 if (counter > VFAT_MAXSEQ) {
389 debug("Error: VFAT name is too long\n");
393 while ((__u8 *)slotptr < buflimit) {
396 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
402 if ((__u8 *)slotptr >= buflimit) {
405 curclust = get_fatent_value(mydata, dir_curclust);
406 if (CHECK_CLUST(curclust, mydata->fatsize)) {
407 debug("curclust: 0x%x\n", curclust);
408 printf("Invalid FAT entry\n");
412 dir_curclust = curclust;
414 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
415 mydata->clust_size * mydata->sect_size) != 0) {
416 debug("Error: reading directory block\n");
420 slotptr2 = (dir_slot *)get_contents_vfatname_block;
421 while (counter > 0) {
422 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
429 /* Save the real directory entry */
430 realdent = (dir_entry *)slotptr2;
431 while ((__u8 *)slotptr2 > get_contents_vfatname_block) {
433 slot2str(slotptr2, l_name, &idx);
436 /* Save the real directory entry */
437 realdent = (dir_entry *)slotptr;
442 if (slot2str(slotptr, l_name, &idx))
444 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
447 if (*l_name == DELETED_FLAG)
449 else if (*l_name == aRING)
450 *l_name = DELETED_FLAG;
453 /* Return the real directory entry */
457 memcpy(get_dentfromdir_block, get_contents_vfatname_block,
458 mydata->clust_size * mydata->sect_size);
459 cur_position = (__u8 *)realdent - get_contents_vfatname_block;
460 *retdent = (dir_entry *) &get_dentfromdir_block[cur_position];
467 * Set the entry at index 'entry' in a FAT (16/32) table.
469 static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
471 __u32 bufnum, offset;
473 switch (mydata->fatsize) {
475 bufnum = entry / FAT32BUFSIZE;
476 offset = entry - bufnum * FAT32BUFSIZE;
479 bufnum = entry / FAT16BUFSIZE;
480 offset = entry - bufnum * FAT16BUFSIZE;
483 /* Unsupported FAT size */
487 /* Read a new block of FAT entries into the cache. */
488 if (bufnum != mydata->fatbufnum) {
489 int getsize = FATBUFBLOCKS;
490 __u8 *bufptr = mydata->fatbuf;
491 __u32 fatlength = mydata->fatlength;
492 __u32 startblock = bufnum * FATBUFBLOCKS;
494 fatlength *= mydata->sect_size;
495 startblock += mydata->fat_sect;
497 if (getsize > fatlength)
500 if (mydata->fatbufnum != -1) {
501 if (flush_fat_buffer(mydata) < 0)
505 if (disk_read(startblock, getsize, bufptr) < 0) {
506 debug("Error reading FAT blocks\n");
509 mydata->fatbufnum = bufnum;
512 /* Set the actual entry */
513 switch (mydata->fatsize) {
515 ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
518 ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
528 * Determine the entry value at index 'entry' in a FAT (16/32) table
530 static __u32 determine_fatent(fsdata *mydata, __u32 entry)
532 __u32 next_fat, next_entry = entry + 1;
535 next_fat = get_fatent_value(mydata, next_entry);
537 set_fatent_value(mydata, entry, next_entry);
542 debug("FAT%d: entry: %08x, entry_value: %04x\n",
543 mydata->fatsize, entry, next_entry);
549 * Write at most 'size' bytes from 'buffer' into the specified cluster.
550 * Return 0 on success, -1 otherwise.
553 set_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer,
561 startsect = mydata->data_begin +
562 clustnum * mydata->clust_size;
564 startsect = mydata->rootdir_sect;
566 debug("clustnum: %d, startsect: %d\n", clustnum, startsect);
568 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
569 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
571 printf("FAT: Misaligned buffer address (%p)\n", buffer);
573 while (size >= mydata->sect_size) {
574 memcpy(tmpbuf, buffer, mydata->sect_size);
575 ret = disk_write(startsect++, 1, tmpbuf);
577 debug("Error writing data (got %d)\n", ret);
581 buffer += mydata->sect_size;
582 size -= mydata->sect_size;
584 } else if (size >= mydata->sect_size) {
585 idx = size / mydata->sect_size;
586 ret = disk_write(startsect, idx, buffer);
588 debug("Error writing data (got %d)\n", ret);
593 idx *= mydata->sect_size;
599 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
601 memcpy(tmpbuf, buffer, size);
602 ret = disk_write(startsect, 1, tmpbuf);
604 debug("Error writing data (got %d)\n", ret);
613 * Find the first empty cluster
615 static int find_empty_cluster(fsdata *mydata)
617 __u32 fat_val, entry = 3;
620 fat_val = get_fatent_value(mydata, entry);
630 * Write directory entries in 'get_dentfromdir_block' to block device
632 static void flush_dir_table(fsdata *mydata, dir_entry **dentptr)
634 int dir_newclust = 0;
636 if (set_cluster(mydata, dir_curclust,
637 get_dentfromdir_block,
638 mydata->clust_size * mydata->sect_size) != 0) {
639 printf("error: wrinting directory entry\n");
642 dir_newclust = find_empty_cluster(mydata);
643 set_fatent_value(mydata, dir_curclust, dir_newclust);
644 if (mydata->fatsize == 32)
645 set_fatent_value(mydata, dir_newclust, 0xffffff8);
646 else if (mydata->fatsize == 16)
647 set_fatent_value(mydata, dir_newclust, 0xfff8);
649 dir_curclust = dir_newclust;
651 if (flush_fat_buffer(mydata) < 0)
654 memset(get_dentfromdir_block, 0x00,
655 mydata->clust_size * mydata->sect_size);
657 *dentptr = (dir_entry *) get_dentfromdir_block;
661 * Set empty cluster from 'entry' to the end of a file
663 static int clear_fatent(fsdata *mydata, __u32 entry)
668 fat_val = get_fatent_value(mydata, entry);
670 set_fatent_value(mydata, entry, 0);
674 if (fat_val == 0xfffffff || fat_val == 0xffff)
680 /* Flush fat buffer */
681 if (flush_fat_buffer(mydata) < 0)
688 * Write at most 'maxsize' bytes from 'buffer' into
689 * the file associated with 'dentptr'
690 * Update the number of bytes written in *gotsize and return 0
691 * or return -1 on fatal errors.
694 set_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
695 loff_t maxsize, loff_t *gotsize)
697 loff_t filesize = FAT2CPU32(dentptr->size);
698 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
699 __u32 curclust = START(dentptr);
700 __u32 endclust = 0, newclust = 0;
704 debug("Filesize: %llu bytes\n", filesize);
706 if (maxsize > 0 && filesize > maxsize)
709 debug("%llu bytes\n", filesize);
713 debug("error: nonempty clusterless file!\n");
719 actsize = bytesperclust;
722 /* search for consecutive clusters */
723 while (actsize < filesize) {
724 newclust = determine_fatent(mydata, endclust);
726 if ((newclust - 1) != endclust)
729 if (CHECK_CLUST(newclust, mydata->fatsize)) {
730 debug("newclust: 0x%x\n", newclust);
731 debug("Invalid FAT entry\n");
735 actsize += bytesperclust;
738 /* set remaining bytes */
740 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
741 debug("error: writing cluster\n");
746 /* Mark end of file in FAT */
747 if (mydata->fatsize == 16)
749 else if (mydata->fatsize == 32)
750 newclust = 0xfffffff;
751 set_fatent_value(mydata, endclust, newclust);
755 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
756 debug("error: writing cluster\n");
763 if (CHECK_CLUST(newclust, mydata->fatsize)) {
764 debug("newclust: 0x%x\n", newclust);
765 debug("Invalid FAT entry\n");
768 actsize = bytesperclust;
769 curclust = endclust = newclust;
774 * Set start cluster in directory entry
776 static void set_start_cluster(const fsdata *mydata, dir_entry *dentptr,
779 if (mydata->fatsize == 32)
781 cpu_to_le16((start_cluster & 0xffff0000) >> 16);
782 dentptr->start = cpu_to_le16(start_cluster & 0xffff);
788 static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
789 const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
791 set_start_cluster(mydata, dentptr, start_cluster);
792 dentptr->size = cpu_to_le32(size);
794 dentptr->attr = attr;
796 set_name(dentptr, filename);
800 * Check whether adding a file makes the file system to
801 * exceed the size of the block device
802 * Return -1 when overflow occurs, otherwise return 0
804 static int check_overflow(fsdata *mydata, __u32 clustnum, loff_t size)
806 __u32 startsect, sect_num, offset;
809 startsect = mydata->data_begin +
810 clustnum * mydata->clust_size;
812 startsect = mydata->rootdir_sect;
815 sect_num = div_u64_rem(size, mydata->sect_size, &offset);
820 if (startsect + sect_num > cur_part_info.start + total_sector)
826 * Check if adding several entries exceed one cluster boundary
828 static int is_next_clust(fsdata *mydata, dir_entry *dentptr)
832 cur_position = (__u8 *)dentptr - get_dentfromdir_block;
834 if (cur_position >= mydata->clust_size * mydata->sect_size)
840 static dir_entry *empty_dentptr;
842 * Find a directory entry based on filename or start cluster number
843 * If the directory entry is not found,
844 * the new position for writing a directory entry will be returned
846 static dir_entry *find_directory_entry(fsdata *mydata, int startsect,
847 char *filename, dir_entry *retdent, __u32 start)
849 __u32 curclust = (startsect - mydata->data_begin) / mydata->clust_size;
851 debug("get_dentfromdir: %s\n", filename);
858 if (get_cluster(mydata, curclust, get_dentfromdir_block,
859 mydata->clust_size * mydata->sect_size) != 0) {
860 printf("Error: reading directory block\n");
864 dentptr = (dir_entry *)get_dentfromdir_block;
866 dir_curclust = curclust;
868 for (i = 0; i < DIRENTSPERCLUST; i++) {
869 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
872 if (dentptr->name[0] == DELETED_FLAG) {
874 if (is_next_clust(mydata, dentptr))
878 if ((dentptr->attr & ATTR_VOLUME)) {
880 (dentptr->attr & ATTR_VFAT) &&
881 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
882 get_long_file_name(mydata, curclust,
883 get_dentfromdir_block,
885 debug("vfatname: |%s|\n", l_name);
887 /* Volume label or VFAT entry */
889 if (is_next_clust(mydata, dentptr))
894 if (dentptr->name[0] == 0) {
895 debug("Dentname == NULL - %d\n", i);
896 empty_dentptr = dentptr;
900 get_name(dentptr, s_name);
902 if (strcmp(filename, s_name)
903 && strcmp(filename, l_name)) {
904 debug("Mismatch: |%s|%s|\n",
907 if (is_next_clust(mydata, dentptr))
912 memcpy(retdent, dentptr, sizeof(dir_entry));
914 debug("DentName: %s", s_name);
915 debug(", start: 0x%x", START(dentptr));
916 debug(", size: 0x%x %s\n",
917 FAT2CPU32(dentptr->size),
918 (dentptr->attr & ATTR_DIR) ?
925 * In FAT16/12, the root dir is locate before data area, shows
927 * -------------------------------------------------------------
928 * | Boot | FAT1 & 2 | Root dir | Data (start from cluster #2) |
929 * -------------------------------------------------------------
931 * As a result if curclust is in Root dir, it is a negative
935 if (mydata->fatsize != 32 && (int)curclust <= 1) {
936 /* Current clust is in root dir, set to next clust */
938 if ((int)curclust <= 1)
939 continue; /* continue to find */
941 /* Reach the end of root dir */
942 empty_dentptr = dentptr;
946 curclust = get_fatent_value(mydata, dir_curclust);
947 if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
948 empty_dentptr = dentptr;
951 if (CHECK_CLUST(curclust, mydata->fatsize)) {
952 debug("curclust: 0x%x\n", curclust);
953 debug("Invalid FAT entry\n");
961 static int do_fat_write(const char *filename, void *buffer, loff_t size,
964 dir_entry *dentptr, *retdent;
970 fsdata *mydata = &datablock;
972 int ret = -1, name_len;
973 char l_filename[VFAT_MAXLEN_BYTES];
978 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
979 debug("error: reading boot sector\n");
983 total_sector = bs.total_sect;
984 if (total_sector == 0)
985 total_sector = (int)cur_part_info.size; /* cast of lbaint_t */
987 if (mydata->fatsize == 32)
988 mydata->fatlength = bs.fat32_length;
990 mydata->fatlength = bs.fat_length;
992 mydata->fat_sect = bs.reserved;
994 cursect = mydata->rootdir_sect
995 = mydata->fat_sect + mydata->fatlength * bs.fats;
996 num_of_fats = bs.fats;
998 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
999 mydata->clust_size = bs.cluster_size;
1001 if (mydata->fatsize == 32) {
1002 mydata->data_begin = mydata->rootdir_sect -
1003 (mydata->clust_size * 2);
1007 rootdir_size = ((bs.dir_entries[1] * (int)256 +
1008 bs.dir_entries[0]) *
1009 sizeof(dir_entry)) /
1011 mydata->data_begin = mydata->rootdir_sect +
1013 (mydata->clust_size * 2);
1016 mydata->fatbufnum = -1;
1017 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
1018 if (mydata->fatbuf == NULL) {
1019 debug("Error: allocating memory\n");
1023 if (disk_read(cursect,
1024 (mydata->fatsize == 32) ?
1025 (mydata->clust_size) :
1026 PREFETCH_BLOCKS, do_fat_read_at_block) < 0) {
1027 debug("Error: reading rootdir block\n");
1030 dentptr = (dir_entry *) do_fat_read_at_block;
1032 name_len = strlen(filename);
1033 if (name_len >= VFAT_MAXLEN_BYTES)
1034 name_len = VFAT_MAXLEN_BYTES - 1;
1036 memcpy(l_filename, filename, name_len);
1037 l_filename[name_len] = 0; /* terminate the string */
1038 downcase(l_filename);
1040 startsect = mydata->rootdir_sect;
1041 retdent = find_directory_entry(mydata, startsect,
1042 l_filename, dentptr, 0);
1044 /* Update file size and start_cluster in a directory entry */
1045 retdent->size = cpu_to_le32(size);
1046 start_cluster = START(retdent);
1048 if (start_cluster) {
1050 ret = check_overflow(mydata, start_cluster,
1053 printf("Error: %llu overflow\n", size);
1058 ret = clear_fatent(mydata, start_cluster);
1060 printf("Error: clearing FAT entries\n");
1065 set_start_cluster(mydata, retdent, 0);
1067 ret = start_cluster = find_empty_cluster(mydata);
1069 printf("Error: finding empty cluster\n");
1073 ret = check_overflow(mydata, start_cluster, size);
1075 printf("Error: %llu overflow\n", size);
1079 set_start_cluster(mydata, retdent, start_cluster);
1082 /* Set short name to set alias checksum field in dir_slot */
1083 set_name(empty_dentptr, filename);
1084 fill_dir_slot(mydata, &empty_dentptr, filename);
1087 ret = start_cluster = find_empty_cluster(mydata);
1089 printf("Error: finding empty cluster\n");
1093 ret = check_overflow(mydata, start_cluster, size);
1095 printf("Error: %llu overflow\n", size);
1102 /* Set attribute as archieve for regular file */
1103 fill_dentry(mydata, empty_dentptr, filename,
1104 start_cluster, size, 0x20);
1106 retdent = empty_dentptr;
1109 ret = set_contents(mydata, retdent, buffer, size, actwrite);
1111 printf("Error: writing contents\n");
1114 debug("attempt to write 0x%llx bytes\n", *actwrite);
1116 /* Flush fat buffer */
1117 ret = flush_fat_buffer(mydata);
1119 printf("Error: flush fat buffer\n");
1123 /* Write directory table to device */
1124 ret = set_cluster(mydata, dir_curclust, get_dentfromdir_block,
1125 mydata->clust_size * mydata->sect_size);
1127 printf("Error: writing directory entry\n");
1130 free(mydata->fatbuf);
1134 int file_fat_write(const char *filename, void *buffer, loff_t offset,
1135 loff_t maxsize, loff_t *actwrite)
1138 printf("Error: non zero offset is currently not suported.\n");
1142 printf("writing %s\n", filename);
1143 return do_fat_write(filename, buffer, maxsize, actwrite);