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_dirty_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 debug("debug: evicting %d, dirty: %d\n", mydata->fatbufnum,
115 (int)mydata->fat_dirty);
117 if ((!mydata->fat_dirty) || (mydata->fatbufnum == -1))
120 startblock += mydata->fat_sect;
122 if (getsize > fatlength)
126 if (disk_write(startblock, getsize, bufptr) < 0) {
127 debug("error: writing FAT blocks\n");
131 if (num_of_fats == 2) {
132 /* Update corresponding second FAT blocks */
133 startblock += mydata->fatlength;
134 if (disk_write(startblock, getsize, bufptr) < 0) {
135 debug("error: writing second FAT blocks\n");
139 mydata->fat_dirty = 0;
145 * Get the entry at index 'entry' in a FAT (12/16/32) table.
146 * On failure 0x00 is returned.
147 * When bufnum is changed, write back the previous fatbuf to the disk.
149 static __u32 get_fatent_value(fsdata *mydata, __u32 entry)
156 if (CHECK_CLUST(entry, mydata->fatsize)) {
157 printf("Error: Invalid FAT entry: 0x%08x\n", entry);
161 switch (mydata->fatsize) {
163 bufnum = entry / FAT32BUFSIZE;
164 offset = entry - bufnum * FAT32BUFSIZE;
167 bufnum = entry / FAT16BUFSIZE;
168 offset = entry - bufnum * FAT16BUFSIZE;
171 bufnum = entry / FAT12BUFSIZE;
172 offset = entry - bufnum * FAT12BUFSIZE;
176 /* Unsupported FAT size */
180 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
181 mydata->fatsize, entry, entry, offset, offset);
183 /* Read a new block of FAT entries into the cache. */
184 if (bufnum != mydata->fatbufnum) {
185 int getsize = FATBUFBLOCKS;
186 __u8 *bufptr = mydata->fatbuf;
187 __u32 fatlength = mydata->fatlength;
188 __u32 startblock = bufnum * FATBUFBLOCKS;
190 if (getsize > fatlength)
193 startblock += mydata->fat_sect; /* Offset from start of disk */
195 /* Write back the fatbuf to the disk */
196 if (flush_dirty_fat_buffer(mydata) < 0)
199 if (disk_read(startblock, getsize, bufptr) < 0) {
200 debug("Error reading FAT blocks\n");
203 mydata->fatbufnum = bufnum;
206 /* Get the actual entry from the table */
207 switch (mydata->fatsize) {
209 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
212 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
215 off16 = (offset * 3) / 4;
217 switch (offset & 0x3) {
219 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
223 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
225 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
227 ret = (val2 << 4) | (val1 >> 12);
230 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
232 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
234 ret = (val2 << 8) | (val1 >> 8);
237 ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
238 ret = (ret & 0xfff0) >> 4;
245 debug("FAT%d: ret: %08x, entry: %08x, offset: %04x\n",
246 mydata->fatsize, ret, entry, offset);
252 * Set the file name information from 'name' into 'slotptr',
254 static int str2slot(dir_slot *slotptr, const char *name, int *idx)
258 for (j = 0; j <= 8; j += 2) {
259 if (name[*idx] == 0x00) {
260 slotptr->name0_4[j] = 0;
261 slotptr->name0_4[j + 1] = 0;
265 slotptr->name0_4[j] = name[*idx];
269 for (j = 0; j <= 10; j += 2) {
270 if (name[*idx] == 0x00) {
271 slotptr->name5_10[j] = 0;
272 slotptr->name5_10[j + 1] = 0;
276 slotptr->name5_10[j] = name[*idx];
280 for (j = 0; j <= 2; j += 2) {
281 if (name[*idx] == 0x00) {
282 slotptr->name11_12[j] = 0;
283 slotptr->name11_12[j + 1] = 0;
287 slotptr->name11_12[j] = name[*idx];
292 if (name[*idx] == 0x00)
296 /* Not used characters are filled with 0xff 0xff */
298 for (; end_idx < 5; end_idx++) {
299 slotptr->name0_4[end_idx * 2] = 0xff;
300 slotptr->name0_4[end_idx * 2 + 1] = 0xff;
305 for (; end_idx < 6; end_idx++) {
306 slotptr->name5_10[end_idx * 2] = 0xff;
307 slotptr->name5_10[end_idx * 2 + 1] = 0xff;
312 for (; end_idx < 2; end_idx++) {
313 slotptr->name11_12[end_idx * 2] = 0xff;
314 slotptr->name11_12[end_idx * 2 + 1] = 0xff;
320 static int is_next_clust(fsdata *mydata, dir_entry *dentptr);
321 static void flush_dir_table(fsdata *mydata, dir_entry **dentptr);
324 * Fill dir_slot entries with appropriate name, id, and attr
325 * The real directory entry is returned by 'dentptr'
328 fill_dir_slot(fsdata *mydata, dir_entry **dentptr, const char *l_name)
330 dir_slot *slotptr = (dir_slot *)get_contents_vfatname_block;
331 __u8 counter = 0, checksum;
334 /* Get short file name checksum value */
335 checksum = mkcksum((*dentptr)->name, (*dentptr)->ext);
338 memset(slotptr, 0x00, sizeof(dir_slot));
339 ret = str2slot(slotptr, l_name, &idx);
340 slotptr->id = ++counter;
341 slotptr->attr = ATTR_VFAT;
342 slotptr->alias_checksum = checksum;
347 slotptr->id |= LAST_LONG_ENTRY_MASK;
349 while (counter >= 1) {
350 if (is_next_clust(mydata, *dentptr)) {
351 /* A new cluster is allocated for directory table */
352 flush_dir_table(mydata, dentptr);
354 memcpy(*dentptr, slotptr, sizeof(dir_slot));
360 if (is_next_clust(mydata, *dentptr)) {
361 /* A new cluster is allocated for directory table */
362 flush_dir_table(mydata, dentptr);
366 static __u32 dir_curclust;
369 * Extract the full long filename starting at 'retdent' (which is really
370 * a slot) into 'l_name'. If successful also copy the real directory entry
372 * If additional adjacent cluster for directory entries is read into memory,
373 * then 'get_contents_vfatname_block' is copied into 'get_dentfromdir_block' and
374 * the location of the real directory entry is returned by 'retdent'
375 * Return 0 on success, -1 otherwise.
378 get_long_file_name(fsdata *mydata, int curclust, __u8 *cluster,
379 dir_entry **retdent, char *l_name)
382 dir_slot *slotptr = (dir_slot *)(*retdent);
383 dir_slot *slotptr2 = NULL;
384 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
387 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
388 int idx = 0, cur_position = 0;
390 if (counter > VFAT_MAXSEQ) {
391 debug("Error: VFAT name is too long\n");
395 while ((__u8 *)slotptr < buflimit) {
398 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
404 if ((__u8 *)slotptr >= buflimit) {
407 curclust = get_fatent_value(mydata, dir_curclust);
408 if (CHECK_CLUST(curclust, mydata->fatsize)) {
409 debug("curclust: 0x%x\n", curclust);
410 printf("Invalid FAT entry\n");
414 dir_curclust = curclust;
416 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
417 mydata->clust_size * mydata->sect_size) != 0) {
418 debug("Error: reading directory block\n");
422 slotptr2 = (dir_slot *)get_contents_vfatname_block;
423 while (counter > 0) {
424 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
431 /* Save the real directory entry */
432 realdent = (dir_entry *)slotptr2;
433 while ((__u8 *)slotptr2 > get_contents_vfatname_block) {
435 slot2str(slotptr2, l_name, &idx);
438 /* Save the real directory entry */
439 realdent = (dir_entry *)slotptr;
444 if (slot2str(slotptr, l_name, &idx))
446 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
449 if (*l_name == DELETED_FLAG)
451 else if (*l_name == aRING)
452 *l_name = DELETED_FLAG;
455 /* Return the real directory entry */
459 memcpy(get_dentfromdir_block, get_contents_vfatname_block,
460 mydata->clust_size * mydata->sect_size);
461 cur_position = (__u8 *)realdent - get_contents_vfatname_block;
462 *retdent = (dir_entry *) &get_dentfromdir_block[cur_position];
469 * Set the entry at index 'entry' in a FAT (16/32) table.
471 static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
473 __u32 bufnum, offset;
475 switch (mydata->fatsize) {
477 bufnum = entry / FAT32BUFSIZE;
478 offset = entry - bufnum * FAT32BUFSIZE;
481 bufnum = entry / FAT16BUFSIZE;
482 offset = entry - bufnum * FAT16BUFSIZE;
485 /* Unsupported FAT size */
489 /* Read a new block of FAT entries into the cache. */
490 if (bufnum != mydata->fatbufnum) {
491 int getsize = FATBUFBLOCKS;
492 __u8 *bufptr = mydata->fatbuf;
493 __u32 fatlength = mydata->fatlength;
494 __u32 startblock = bufnum * FATBUFBLOCKS;
496 fatlength *= mydata->sect_size;
497 startblock += mydata->fat_sect;
499 if (getsize > fatlength)
502 if (flush_dirty_fat_buffer(mydata) < 0)
505 if (disk_read(startblock, getsize, bufptr) < 0) {
506 debug("Error reading FAT blocks\n");
509 mydata->fatbufnum = bufnum;
513 mydata->fat_dirty = 1;
515 /* Set the actual entry */
516 switch (mydata->fatsize) {
518 ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
521 ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
531 * Determine the entry value at index 'entry' in a FAT (16/32) table
533 static __u32 determine_fatent(fsdata *mydata, __u32 entry)
535 __u32 next_fat, next_entry = entry + 1;
538 next_fat = get_fatent_value(mydata, next_entry);
540 set_fatent_value(mydata, entry, next_entry);
545 debug("FAT%d: entry: %08x, entry_value: %04x\n",
546 mydata->fatsize, entry, next_entry);
552 * Write at most 'size' bytes from 'buffer' into the specified cluster.
553 * Return 0 on success, -1 otherwise.
556 set_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer,
564 startsect = mydata->data_begin +
565 clustnum * mydata->clust_size;
567 startsect = mydata->rootdir_sect;
569 debug("clustnum: %d, startsect: %d\n", clustnum, startsect);
571 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
572 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
574 printf("FAT: Misaligned buffer address (%p)\n", buffer);
576 while (size >= mydata->sect_size) {
577 memcpy(tmpbuf, buffer, mydata->sect_size);
578 ret = disk_write(startsect++, 1, tmpbuf);
580 debug("Error writing data (got %d)\n", ret);
584 buffer += mydata->sect_size;
585 size -= mydata->sect_size;
587 } else if (size >= mydata->sect_size) {
588 idx = size / mydata->sect_size;
589 ret = disk_write(startsect, idx, buffer);
591 debug("Error writing data (got %d)\n", ret);
596 idx *= mydata->sect_size;
602 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
604 memcpy(tmpbuf, buffer, size);
605 ret = disk_write(startsect, 1, tmpbuf);
607 debug("Error writing data (got %d)\n", ret);
616 * Find the first empty cluster
618 static int find_empty_cluster(fsdata *mydata)
620 __u32 fat_val, entry = 3;
623 fat_val = get_fatent_value(mydata, entry);
633 * Write directory entries in 'get_dentfromdir_block' to block device
635 static void flush_dir_table(fsdata *mydata, dir_entry **dentptr)
637 int dir_newclust = 0;
639 if (set_cluster(mydata, dir_curclust,
640 get_dentfromdir_block,
641 mydata->clust_size * mydata->sect_size) != 0) {
642 printf("error: wrinting directory entry\n");
645 dir_newclust = find_empty_cluster(mydata);
646 set_fatent_value(mydata, dir_curclust, dir_newclust);
647 if (mydata->fatsize == 32)
648 set_fatent_value(mydata, dir_newclust, 0xffffff8);
649 else if (mydata->fatsize == 16)
650 set_fatent_value(mydata, dir_newclust, 0xfff8);
652 dir_curclust = dir_newclust;
654 if (flush_dirty_fat_buffer(mydata) < 0)
657 memset(get_dentfromdir_block, 0x00,
658 mydata->clust_size * mydata->sect_size);
660 *dentptr = (dir_entry *) get_dentfromdir_block;
664 * Set empty cluster from 'entry' to the end of a file
666 static int clear_fatent(fsdata *mydata, __u32 entry)
671 fat_val = get_fatent_value(mydata, entry);
673 set_fatent_value(mydata, entry, 0);
677 if (fat_val == 0xfffffff || fat_val == 0xffff)
683 /* Flush fat buffer */
684 if (flush_dirty_fat_buffer(mydata) < 0)
691 * Write at most 'maxsize' bytes from 'buffer' into
692 * the file associated with 'dentptr'
693 * Update the number of bytes written in *gotsize and return 0
694 * or return -1 on fatal errors.
697 set_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
698 loff_t maxsize, loff_t *gotsize)
700 loff_t filesize = FAT2CPU32(dentptr->size);
701 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
702 __u32 curclust = START(dentptr);
703 __u32 endclust = 0, newclust = 0;
707 debug("Filesize: %llu bytes\n", filesize);
709 if (maxsize > 0 && filesize > maxsize)
712 debug("%llu bytes\n", filesize);
716 debug("error: nonempty clusterless file!\n");
722 actsize = bytesperclust;
725 /* search for consecutive clusters */
726 while (actsize < filesize) {
727 newclust = determine_fatent(mydata, endclust);
729 if ((newclust - 1) != endclust)
732 if (CHECK_CLUST(newclust, mydata->fatsize)) {
733 debug("newclust: 0x%x\n", newclust);
734 debug("Invalid FAT entry\n");
738 actsize += bytesperclust;
741 /* set remaining bytes */
743 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
744 debug("error: writing cluster\n");
749 /* Mark end of file in FAT */
750 if (mydata->fatsize == 16)
752 else if (mydata->fatsize == 32)
753 newclust = 0xfffffff;
754 set_fatent_value(mydata, endclust, newclust);
758 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
759 debug("error: writing cluster\n");
766 if (CHECK_CLUST(newclust, mydata->fatsize)) {
767 debug("newclust: 0x%x\n", newclust);
768 debug("Invalid FAT entry\n");
771 actsize = bytesperclust;
772 curclust = endclust = newclust;
777 * Set start cluster in directory entry
779 static void set_start_cluster(const fsdata *mydata, dir_entry *dentptr,
782 if (mydata->fatsize == 32)
784 cpu_to_le16((start_cluster & 0xffff0000) >> 16);
785 dentptr->start = cpu_to_le16(start_cluster & 0xffff);
791 static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
792 const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
794 set_start_cluster(mydata, dentptr, start_cluster);
795 dentptr->size = cpu_to_le32(size);
797 dentptr->attr = attr;
799 set_name(dentptr, filename);
803 * Check whether adding a file makes the file system to
804 * exceed the size of the block device
805 * Return -1 when overflow occurs, otherwise return 0
807 static int check_overflow(fsdata *mydata, __u32 clustnum, loff_t size)
809 __u32 startsect, sect_num, offset;
812 startsect = mydata->data_begin +
813 clustnum * mydata->clust_size;
815 startsect = mydata->rootdir_sect;
818 sect_num = div_u64_rem(size, mydata->sect_size, &offset);
823 if (startsect + sect_num > cur_part_info.start + total_sector)
829 * Check if adding several entries exceed one cluster boundary
831 static int is_next_clust(fsdata *mydata, dir_entry *dentptr)
835 cur_position = (__u8 *)dentptr - get_dentfromdir_block;
837 if (cur_position >= mydata->clust_size * mydata->sect_size)
843 static dir_entry *empty_dentptr;
845 * Find a directory entry based on filename or start cluster number
846 * If the directory entry is not found,
847 * the new position for writing a directory entry will be returned
849 static dir_entry *find_directory_entry(fsdata *mydata, int startsect,
850 char *filename, dir_entry *retdent, __u32 start)
852 __u32 curclust = (startsect - mydata->data_begin) / mydata->clust_size;
854 debug("get_dentfromdir: %s\n", filename);
861 if (get_cluster(mydata, curclust, get_dentfromdir_block,
862 mydata->clust_size * mydata->sect_size) != 0) {
863 printf("Error: reading directory block\n");
867 dentptr = (dir_entry *)get_dentfromdir_block;
869 dir_curclust = curclust;
871 for (i = 0; i < DIRENTSPERCLUST; i++) {
872 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
875 if (dentptr->name[0] == DELETED_FLAG) {
877 if (is_next_clust(mydata, dentptr))
881 if ((dentptr->attr & ATTR_VOLUME)) {
883 (dentptr->attr & ATTR_VFAT) &&
884 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
885 get_long_file_name(mydata, curclust,
886 get_dentfromdir_block,
888 debug("vfatname: |%s|\n", l_name);
890 /* Volume label or VFAT entry */
892 if (is_next_clust(mydata, dentptr))
897 if (dentptr->name[0] == 0) {
898 debug("Dentname == NULL - %d\n", i);
899 empty_dentptr = dentptr;
903 get_name(dentptr, s_name);
905 if (strcmp(filename, s_name)
906 && strcmp(filename, l_name)) {
907 debug("Mismatch: |%s|%s|\n",
910 if (is_next_clust(mydata, dentptr))
915 memcpy(retdent, dentptr, sizeof(dir_entry));
917 debug("DentName: %s", s_name);
918 debug(", start: 0x%x", START(dentptr));
919 debug(", size: 0x%x %s\n",
920 FAT2CPU32(dentptr->size),
921 (dentptr->attr & ATTR_DIR) ?
928 * In FAT16/12, the root dir is locate before data area, shows
930 * -------------------------------------------------------------
931 * | Boot | FAT1 & 2 | Root dir | Data (start from cluster #2) |
932 * -------------------------------------------------------------
934 * As a result if curclust is in Root dir, it is a negative
938 if (mydata->fatsize != 32 && (int)curclust <= 1) {
939 /* Current clust is in root dir, set to next clust */
941 if ((int)curclust <= 1)
942 continue; /* continue to find */
944 /* Reach the end of root dir */
945 empty_dentptr = dentptr;
949 curclust = get_fatent_value(mydata, dir_curclust);
950 if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
951 empty_dentptr = dentptr;
954 if (CHECK_CLUST(curclust, mydata->fatsize)) {
955 debug("curclust: 0x%x\n", curclust);
956 debug("Invalid FAT entry\n");
964 static int do_fat_write(const char *filename, void *buffer, loff_t size,
967 dir_entry *dentptr, *retdent;
973 fsdata *mydata = &datablock;
975 int ret = -1, name_len;
976 char l_filename[VFAT_MAXLEN_BYTES];
981 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
982 debug("error: reading boot sector\n");
986 total_sector = bs.total_sect;
987 if (total_sector == 0)
988 total_sector = (int)cur_part_info.size; /* cast of lbaint_t */
990 if (mydata->fatsize == 32)
991 mydata->fatlength = bs.fat32_length;
993 mydata->fatlength = bs.fat_length;
995 mydata->fat_sect = bs.reserved;
997 cursect = mydata->rootdir_sect
998 = mydata->fat_sect + mydata->fatlength * bs.fats;
999 num_of_fats = bs.fats;
1001 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
1002 mydata->clust_size = bs.cluster_size;
1004 if (mydata->fatsize == 32) {
1005 mydata->data_begin = mydata->rootdir_sect -
1006 (mydata->clust_size * 2);
1010 rootdir_size = ((bs.dir_entries[1] * (int)256 +
1011 bs.dir_entries[0]) *
1012 sizeof(dir_entry)) /
1014 mydata->data_begin = mydata->rootdir_sect +
1016 (mydata->clust_size * 2);
1019 mydata->fatbufnum = -1;
1020 mydata->fat_dirty = 0;
1021 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
1022 if (mydata->fatbuf == NULL) {
1023 debug("Error: allocating memory\n");
1027 if (disk_read(cursect,
1028 (mydata->fatsize == 32) ?
1029 (mydata->clust_size) :
1030 PREFETCH_BLOCKS, do_fat_read_at_block) < 0) {
1031 debug("Error: reading rootdir block\n");
1034 dentptr = (dir_entry *) do_fat_read_at_block;
1036 name_len = strlen(filename);
1037 if (name_len >= VFAT_MAXLEN_BYTES)
1038 name_len = VFAT_MAXLEN_BYTES - 1;
1040 memcpy(l_filename, filename, name_len);
1041 l_filename[name_len] = 0; /* terminate the string */
1042 downcase(l_filename);
1044 startsect = mydata->rootdir_sect;
1045 retdent = find_directory_entry(mydata, startsect,
1046 l_filename, dentptr, 0);
1048 /* Update file size and start_cluster in a directory entry */
1049 retdent->size = cpu_to_le32(size);
1050 start_cluster = START(retdent);
1052 if (start_cluster) {
1054 ret = check_overflow(mydata, start_cluster,
1057 printf("Error: %llu overflow\n", size);
1062 ret = clear_fatent(mydata, start_cluster);
1064 printf("Error: clearing FAT entries\n");
1069 set_start_cluster(mydata, retdent, 0);
1071 ret = start_cluster = find_empty_cluster(mydata);
1073 printf("Error: finding empty cluster\n");
1077 ret = check_overflow(mydata, start_cluster, size);
1079 printf("Error: %llu overflow\n", size);
1083 set_start_cluster(mydata, retdent, start_cluster);
1086 /* Set short name to set alias checksum field in dir_slot */
1087 set_name(empty_dentptr, filename);
1088 fill_dir_slot(mydata, &empty_dentptr, filename);
1091 ret = start_cluster = find_empty_cluster(mydata);
1093 printf("Error: finding empty cluster\n");
1097 ret = check_overflow(mydata, start_cluster, size);
1099 printf("Error: %llu overflow\n", size);
1106 /* Set attribute as archieve for regular file */
1107 fill_dentry(mydata, empty_dentptr, filename,
1108 start_cluster, size, 0x20);
1110 retdent = empty_dentptr;
1113 ret = set_contents(mydata, retdent, buffer, size, actwrite);
1115 printf("Error: writing contents\n");
1118 debug("attempt to write 0x%llx bytes\n", *actwrite);
1120 /* Flush fat buffer */
1121 ret = flush_dirty_fat_buffer(mydata);
1123 printf("Error: flush fat buffer\n");
1127 /* Write directory table to device */
1128 ret = set_cluster(mydata, dir_curclust, get_dentfromdir_block,
1129 mydata->clust_size * mydata->sect_size);
1131 printf("Error: writing directory entry\n");
1134 free(mydata->fatbuf);
1138 int file_fat_write(const char *filename, void *buffer, loff_t offset,
1139 loff_t maxsize, loff_t *actwrite)
1142 printf("Error: non zero offset is currently not supported.\n");
1146 printf("writing %s\n", filename);
1147 return do_fat_write(filename, buffer, maxsize, actwrite);