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 startblock += mydata->fat_sect; /* Offset from start of disk */
188 /* Write back the fatbuf to the disk */
189 if (mydata->fatbufnum != -1) {
190 if (flush_fat_buffer(mydata) < 0)
194 if (disk_read(startblock, getsize, bufptr) < 0) {
195 debug("Error reading FAT blocks\n");
198 mydata->fatbufnum = bufnum;
201 /* Get the actual entry from the table */
202 switch (mydata->fatsize) {
204 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
207 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
210 off16 = (offset * 3) / 4;
212 switch (offset & 0x3) {
214 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
218 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
220 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
222 ret = (val2 << 4) | (val1 >> 12);
225 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
227 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
229 ret = (val2 << 8) | (val1 >> 8);
232 ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
233 ret = (ret & 0xfff0) >> 4;
240 debug("FAT%d: ret: %08x, entry: %08x, offset: %04x\n",
241 mydata->fatsize, ret, entry, offset);
247 * Set the file name information from 'name' into 'slotptr',
249 static int str2slot(dir_slot *slotptr, const char *name, int *idx)
253 for (j = 0; j <= 8; j += 2) {
254 if (name[*idx] == 0x00) {
255 slotptr->name0_4[j] = 0;
256 slotptr->name0_4[j + 1] = 0;
260 slotptr->name0_4[j] = name[*idx];
264 for (j = 0; j <= 10; j += 2) {
265 if (name[*idx] == 0x00) {
266 slotptr->name5_10[j] = 0;
267 slotptr->name5_10[j + 1] = 0;
271 slotptr->name5_10[j] = name[*idx];
275 for (j = 0; j <= 2; j += 2) {
276 if (name[*idx] == 0x00) {
277 slotptr->name11_12[j] = 0;
278 slotptr->name11_12[j + 1] = 0;
282 slotptr->name11_12[j] = name[*idx];
287 if (name[*idx] == 0x00)
291 /* Not used characters are filled with 0xff 0xff */
293 for (; end_idx < 5; end_idx++) {
294 slotptr->name0_4[end_idx * 2] = 0xff;
295 slotptr->name0_4[end_idx * 2 + 1] = 0xff;
300 for (; end_idx < 6; end_idx++) {
301 slotptr->name5_10[end_idx * 2] = 0xff;
302 slotptr->name5_10[end_idx * 2 + 1] = 0xff;
307 for (; end_idx < 2; end_idx++) {
308 slotptr->name11_12[end_idx * 2] = 0xff;
309 slotptr->name11_12[end_idx * 2 + 1] = 0xff;
315 static int is_next_clust(fsdata *mydata, dir_entry *dentptr);
316 static void flush_dir_table(fsdata *mydata, dir_entry **dentptr);
319 * Fill dir_slot entries with appropriate name, id, and attr
320 * The real directory entry is returned by 'dentptr'
323 fill_dir_slot(fsdata *mydata, dir_entry **dentptr, const char *l_name)
325 dir_slot *slotptr = (dir_slot *)get_contents_vfatname_block;
326 __u8 counter = 0, checksum;
329 /* Get short file name checksum value */
330 checksum = mkcksum((*dentptr)->name, (*dentptr)->ext);
333 memset(slotptr, 0x00, sizeof(dir_slot));
334 ret = str2slot(slotptr, l_name, &idx);
335 slotptr->id = ++counter;
336 slotptr->attr = ATTR_VFAT;
337 slotptr->alias_checksum = checksum;
342 slotptr->id |= LAST_LONG_ENTRY_MASK;
344 while (counter >= 1) {
345 if (is_next_clust(mydata, *dentptr)) {
346 /* A new cluster is allocated for directory table */
347 flush_dir_table(mydata, dentptr);
349 memcpy(*dentptr, slotptr, sizeof(dir_slot));
355 if (is_next_clust(mydata, *dentptr)) {
356 /* A new cluster is allocated for directory table */
357 flush_dir_table(mydata, dentptr);
361 static __u32 dir_curclust;
364 * Extract the full long filename starting at 'retdent' (which is really
365 * a slot) into 'l_name'. If successful also copy the real directory entry
367 * If additional adjacent cluster for directory entries is read into memory,
368 * then 'get_contents_vfatname_block' is copied into 'get_dentfromdir_block' and
369 * the location of the real directory entry is returned by 'retdent'
370 * Return 0 on success, -1 otherwise.
373 get_long_file_name(fsdata *mydata, int curclust, __u8 *cluster,
374 dir_entry **retdent, char *l_name)
377 dir_slot *slotptr = (dir_slot *)(*retdent);
378 dir_slot *slotptr2 = NULL;
379 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
382 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
383 int idx = 0, cur_position = 0;
385 if (counter > VFAT_MAXSEQ) {
386 debug("Error: VFAT name is too long\n");
390 while ((__u8 *)slotptr < buflimit) {
393 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
399 if ((__u8 *)slotptr >= buflimit) {
402 curclust = get_fatent_value(mydata, dir_curclust);
403 if (CHECK_CLUST(curclust, mydata->fatsize)) {
404 debug("curclust: 0x%x\n", curclust);
405 printf("Invalid FAT entry\n");
409 dir_curclust = curclust;
411 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
412 mydata->clust_size * mydata->sect_size) != 0) {
413 debug("Error: reading directory block\n");
417 slotptr2 = (dir_slot *)get_contents_vfatname_block;
418 while (counter > 0) {
419 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
426 /* Save the real directory entry */
427 realdent = (dir_entry *)slotptr2;
428 while ((__u8 *)slotptr2 > get_contents_vfatname_block) {
430 slot2str(slotptr2, l_name, &idx);
433 /* Save the real directory entry */
434 realdent = (dir_entry *)slotptr;
439 if (slot2str(slotptr, l_name, &idx))
441 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
444 if (*l_name == DELETED_FLAG)
446 else if (*l_name == aRING)
447 *l_name = DELETED_FLAG;
450 /* Return the real directory entry */
454 memcpy(get_dentfromdir_block, get_contents_vfatname_block,
455 mydata->clust_size * mydata->sect_size);
456 cur_position = (__u8 *)realdent - get_contents_vfatname_block;
457 *retdent = (dir_entry *) &get_dentfromdir_block[cur_position];
464 * Set the entry at index 'entry' in a FAT (16/32) table.
466 static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
468 __u32 bufnum, offset;
470 switch (mydata->fatsize) {
472 bufnum = entry / FAT32BUFSIZE;
473 offset = entry - bufnum * FAT32BUFSIZE;
476 bufnum = entry / FAT16BUFSIZE;
477 offset = entry - bufnum * FAT16BUFSIZE;
480 /* Unsupported FAT size */
484 /* Read a new block of FAT entries into the cache. */
485 if (bufnum != mydata->fatbufnum) {
486 int getsize = FATBUFBLOCKS;
487 __u8 *bufptr = mydata->fatbuf;
488 __u32 fatlength = mydata->fatlength;
489 __u32 startblock = bufnum * FATBUFBLOCKS;
491 fatlength *= mydata->sect_size;
492 startblock += mydata->fat_sect;
494 if (getsize > fatlength)
497 if (mydata->fatbufnum != -1) {
498 if (flush_fat_buffer(mydata) < 0)
502 if (disk_read(startblock, getsize, bufptr) < 0) {
503 debug("Error reading FAT blocks\n");
506 mydata->fatbufnum = bufnum;
509 /* Set the actual entry */
510 switch (mydata->fatsize) {
512 ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
515 ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
525 * Determine the entry value at index 'entry' in a FAT (16/32) table
527 static __u32 determine_fatent(fsdata *mydata, __u32 entry)
529 __u32 next_fat, next_entry = entry + 1;
532 next_fat = get_fatent_value(mydata, next_entry);
534 set_fatent_value(mydata, entry, next_entry);
539 debug("FAT%d: entry: %08x, entry_value: %04x\n",
540 mydata->fatsize, entry, next_entry);
546 * Write at most 'size' bytes from 'buffer' into the specified cluster.
547 * Return 0 on success, -1 otherwise.
550 set_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer,
558 startsect = mydata->data_begin +
559 clustnum * mydata->clust_size;
561 startsect = mydata->rootdir_sect;
563 debug("clustnum: %d, startsect: %d\n", clustnum, startsect);
565 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
566 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
568 printf("FAT: Misaligned buffer address (%p)\n", buffer);
570 while (size >= mydata->sect_size) {
571 memcpy(tmpbuf, buffer, mydata->sect_size);
572 ret = disk_write(startsect++, 1, tmpbuf);
574 debug("Error writing data (got %d)\n", ret);
578 buffer += mydata->sect_size;
579 size -= mydata->sect_size;
581 } else if (size >= mydata->sect_size) {
582 idx = size / mydata->sect_size;
583 ret = disk_write(startsect, idx, buffer);
585 debug("Error writing data (got %d)\n", ret);
590 idx *= mydata->sect_size;
596 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
598 memcpy(tmpbuf, buffer, size);
599 ret = disk_write(startsect, 1, tmpbuf);
601 debug("Error writing data (got %d)\n", ret);
610 * Find the first empty cluster
612 static int find_empty_cluster(fsdata *mydata)
614 __u32 fat_val, entry = 3;
617 fat_val = get_fatent_value(mydata, entry);
627 * Write directory entries in 'get_dentfromdir_block' to block device
629 static void flush_dir_table(fsdata *mydata, dir_entry **dentptr)
631 int dir_newclust = 0;
633 if (set_cluster(mydata, dir_curclust,
634 get_dentfromdir_block,
635 mydata->clust_size * mydata->sect_size) != 0) {
636 printf("error: wrinting directory entry\n");
639 dir_newclust = find_empty_cluster(mydata);
640 set_fatent_value(mydata, dir_curclust, dir_newclust);
641 if (mydata->fatsize == 32)
642 set_fatent_value(mydata, dir_newclust, 0xffffff8);
643 else if (mydata->fatsize == 16)
644 set_fatent_value(mydata, dir_newclust, 0xfff8);
646 dir_curclust = dir_newclust;
648 if (flush_fat_buffer(mydata) < 0)
651 memset(get_dentfromdir_block, 0x00,
652 mydata->clust_size * mydata->sect_size);
654 *dentptr = (dir_entry *) get_dentfromdir_block;
658 * Set empty cluster from 'entry' to the end of a file
660 static int clear_fatent(fsdata *mydata, __u32 entry)
665 fat_val = get_fatent_value(mydata, entry);
667 set_fatent_value(mydata, entry, 0);
671 if (fat_val == 0xfffffff || fat_val == 0xffff)
677 /* Flush fat buffer */
678 if (flush_fat_buffer(mydata) < 0)
685 * Write at most 'maxsize' bytes from 'buffer' into
686 * the file associated with 'dentptr'
687 * Update the number of bytes written in *gotsize and return 0
688 * or return -1 on fatal errors.
691 set_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
692 loff_t maxsize, loff_t *gotsize)
694 loff_t filesize = FAT2CPU32(dentptr->size);
695 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
696 __u32 curclust = START(dentptr);
697 __u32 endclust = 0, newclust = 0;
701 debug("Filesize: %llu bytes\n", filesize);
703 if (maxsize > 0 && filesize > maxsize)
706 debug("%llu bytes\n", filesize);
710 debug("error: nonempty clusterless file!\n");
716 actsize = bytesperclust;
719 /* search for consecutive clusters */
720 while (actsize < filesize) {
721 newclust = determine_fatent(mydata, endclust);
723 if ((newclust - 1) != endclust)
726 if (CHECK_CLUST(newclust, mydata->fatsize)) {
727 debug("newclust: 0x%x\n", newclust);
728 debug("Invalid FAT entry\n");
732 actsize += bytesperclust;
735 /* set remaining bytes */
737 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
738 debug("error: writing cluster\n");
743 /* Mark end of file in FAT */
744 if (mydata->fatsize == 16)
746 else if (mydata->fatsize == 32)
747 newclust = 0xfffffff;
748 set_fatent_value(mydata, endclust, newclust);
752 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
753 debug("error: writing cluster\n");
760 if (CHECK_CLUST(newclust, mydata->fatsize)) {
761 debug("newclust: 0x%x\n", newclust);
762 debug("Invalid FAT entry\n");
765 actsize = bytesperclust;
766 curclust = endclust = newclust;
771 * Set start cluster in directory entry
773 static void set_start_cluster(const fsdata *mydata, dir_entry *dentptr,
776 if (mydata->fatsize == 32)
778 cpu_to_le16((start_cluster & 0xffff0000) >> 16);
779 dentptr->start = cpu_to_le16(start_cluster & 0xffff);
785 static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
786 const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
788 set_start_cluster(mydata, dentptr, start_cluster);
789 dentptr->size = cpu_to_le32(size);
791 dentptr->attr = attr;
793 set_name(dentptr, filename);
797 * Check whether adding a file makes the file system to
798 * exceed the size of the block device
799 * Return -1 when overflow occurs, otherwise return 0
801 static int check_overflow(fsdata *mydata, __u32 clustnum, loff_t size)
803 __u32 startsect, sect_num, offset;
806 startsect = mydata->data_begin +
807 clustnum * mydata->clust_size;
809 startsect = mydata->rootdir_sect;
812 sect_num = div_u64_rem(size, mydata->sect_size, &offset);
817 if (startsect + sect_num > cur_part_info.start + total_sector)
823 * Check if adding several entries exceed one cluster boundary
825 static int is_next_clust(fsdata *mydata, dir_entry *dentptr)
829 cur_position = (__u8 *)dentptr - get_dentfromdir_block;
831 if (cur_position >= mydata->clust_size * mydata->sect_size)
837 static dir_entry *empty_dentptr;
839 * Find a directory entry based on filename or start cluster number
840 * If the directory entry is not found,
841 * the new position for writing a directory entry will be returned
843 static dir_entry *find_directory_entry(fsdata *mydata, int startsect,
844 char *filename, dir_entry *retdent, __u32 start)
846 __u32 curclust = (startsect - mydata->data_begin) / mydata->clust_size;
848 debug("get_dentfromdir: %s\n", filename);
855 if (get_cluster(mydata, curclust, get_dentfromdir_block,
856 mydata->clust_size * mydata->sect_size) != 0) {
857 printf("Error: reading directory block\n");
861 dentptr = (dir_entry *)get_dentfromdir_block;
863 dir_curclust = curclust;
865 for (i = 0; i < DIRENTSPERCLUST; i++) {
866 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
869 if (dentptr->name[0] == DELETED_FLAG) {
871 if (is_next_clust(mydata, dentptr))
875 if ((dentptr->attr & ATTR_VOLUME)) {
877 (dentptr->attr & ATTR_VFAT) &&
878 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
879 get_long_file_name(mydata, curclust,
880 get_dentfromdir_block,
882 debug("vfatname: |%s|\n", l_name);
884 /* Volume label or VFAT entry */
886 if (is_next_clust(mydata, dentptr))
891 if (dentptr->name[0] == 0) {
892 debug("Dentname == NULL - %d\n", i);
893 empty_dentptr = dentptr;
897 get_name(dentptr, s_name);
899 if (strcmp(filename, s_name)
900 && strcmp(filename, l_name)) {
901 debug("Mismatch: |%s|%s|\n",
904 if (is_next_clust(mydata, dentptr))
909 memcpy(retdent, dentptr, sizeof(dir_entry));
911 debug("DentName: %s", s_name);
912 debug(", start: 0x%x", START(dentptr));
913 debug(", size: 0x%x %s\n",
914 FAT2CPU32(dentptr->size),
915 (dentptr->attr & ATTR_DIR) ?
922 * In FAT16/12, the root dir is locate before data area, shows
924 * -------------------------------------------------------------
925 * | Boot | FAT1 & 2 | Root dir | Data (start from cluster #2) |
926 * -------------------------------------------------------------
928 * As a result if curclust is in Root dir, it is a negative
932 if (mydata->fatsize != 32 && (int)curclust <= 1) {
933 /* Current clust is in root dir, set to next clust */
935 if ((int)curclust <= 1)
936 continue; /* continue to find */
938 /* Reach the end of root dir */
939 empty_dentptr = dentptr;
943 curclust = get_fatent_value(mydata, dir_curclust);
944 if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
945 empty_dentptr = dentptr;
948 if (CHECK_CLUST(curclust, mydata->fatsize)) {
949 debug("curclust: 0x%x\n", curclust);
950 debug("Invalid FAT entry\n");
958 static int do_fat_write(const char *filename, void *buffer, loff_t size,
961 dir_entry *dentptr, *retdent;
967 fsdata *mydata = &datablock;
969 int ret = -1, name_len;
970 char l_filename[VFAT_MAXLEN_BYTES];
975 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
976 debug("error: reading boot sector\n");
980 total_sector = bs.total_sect;
981 if (total_sector == 0)
982 total_sector = (int)cur_part_info.size; /* cast of lbaint_t */
984 if (mydata->fatsize == 32)
985 mydata->fatlength = bs.fat32_length;
987 mydata->fatlength = bs.fat_length;
989 mydata->fat_sect = bs.reserved;
991 cursect = mydata->rootdir_sect
992 = mydata->fat_sect + mydata->fatlength * bs.fats;
993 num_of_fats = bs.fats;
995 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
996 mydata->clust_size = bs.cluster_size;
998 if (mydata->fatsize == 32) {
999 mydata->data_begin = mydata->rootdir_sect -
1000 (mydata->clust_size * 2);
1004 rootdir_size = ((bs.dir_entries[1] * (int)256 +
1005 bs.dir_entries[0]) *
1006 sizeof(dir_entry)) /
1008 mydata->data_begin = mydata->rootdir_sect +
1010 (mydata->clust_size * 2);
1013 mydata->fatbufnum = -1;
1014 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
1015 if (mydata->fatbuf == NULL) {
1016 debug("Error: allocating memory\n");
1020 if (disk_read(cursect,
1021 (mydata->fatsize == 32) ?
1022 (mydata->clust_size) :
1023 PREFETCH_BLOCKS, do_fat_read_at_block) < 0) {
1024 debug("Error: reading rootdir block\n");
1027 dentptr = (dir_entry *) do_fat_read_at_block;
1029 name_len = strlen(filename);
1030 if (name_len >= VFAT_MAXLEN_BYTES)
1031 name_len = VFAT_MAXLEN_BYTES - 1;
1033 memcpy(l_filename, filename, name_len);
1034 l_filename[name_len] = 0; /* terminate the string */
1035 downcase(l_filename);
1037 startsect = mydata->rootdir_sect;
1038 retdent = find_directory_entry(mydata, startsect,
1039 l_filename, dentptr, 0);
1041 /* Update file size and start_cluster in a directory entry */
1042 retdent->size = cpu_to_le32(size);
1043 start_cluster = START(retdent);
1045 if (start_cluster) {
1047 ret = check_overflow(mydata, start_cluster,
1050 printf("Error: %llu overflow\n", size);
1055 ret = clear_fatent(mydata, start_cluster);
1057 printf("Error: clearing FAT entries\n");
1062 set_start_cluster(mydata, retdent, 0);
1064 ret = start_cluster = find_empty_cluster(mydata);
1066 printf("Error: finding empty cluster\n");
1070 ret = check_overflow(mydata, start_cluster, size);
1072 printf("Error: %llu overflow\n", size);
1076 set_start_cluster(mydata, retdent, start_cluster);
1079 /* Set short name to set alias checksum field in dir_slot */
1080 set_name(empty_dentptr, filename);
1081 fill_dir_slot(mydata, &empty_dentptr, filename);
1084 ret = start_cluster = find_empty_cluster(mydata);
1086 printf("Error: finding empty cluster\n");
1090 ret = check_overflow(mydata, start_cluster, size);
1092 printf("Error: %llu overflow\n", size);
1099 /* Set attribute as archieve for regular file */
1100 fill_dentry(mydata, empty_dentptr, filename,
1101 start_cluster, size, 0x20);
1103 retdent = empty_dentptr;
1106 ret = set_contents(mydata, retdent, buffer, size, actwrite);
1108 printf("Error: writing contents\n");
1111 debug("attempt to write 0x%llx bytes\n", *actwrite);
1113 /* Flush fat buffer */
1114 ret = flush_fat_buffer(mydata);
1116 printf("Error: flush fat buffer\n");
1120 /* Write directory table to device */
1121 ret = set_cluster(mydata, dir_curclust, get_dentfromdir_block,
1122 mydata->clust_size * mydata->sect_size);
1124 printf("Error: writing directory entry\n");
1127 free(mydata->fatbuf);
1131 int file_fat_write(const char *filename, void *buffer, loff_t offset,
1132 loff_t maxsize, loff_t *actwrite)
1135 printf("Error: non zero offset is currently not supported.\n");
1139 printf("writing %s\n", filename);
1140 return do_fat_write(filename, buffer, maxsize, actwrite);