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>
18 static void uppercase(char *str, int len)
22 for (i = 0; i < len; i++) {
28 static int total_sector;
29 static int disk_write(__u32 block, __u32 nr_blocks, void *buf)
31 if (!cur_dev || !cur_dev->block_write)
34 if (cur_part_info.start + block + nr_blocks >
35 cur_part_info.start + total_sector) {
36 printf("error: overflow occurs\n");
40 return cur_dev->block_write(cur_dev->dev,
41 cur_part_info.start + block, nr_blocks, buf);
45 * Set short name in directory entry
47 static void set_name(dir_entry *dirent, const char *filename)
49 char s_name[VFAT_MAXLEN_BYTES];
51 int period_location, len, i, ext_num;
56 len = strlen(filename);
60 strcpy(s_name, filename);
61 uppercase(s_name, len);
63 period = strchr(s_name, '.');
65 period_location = len;
68 period_location = period - s_name;
69 ext_num = len - period_location - 1;
72 /* Pad spaces when the length of file name is shorter than eight */
73 if (period_location < 8) {
74 memcpy(dirent->name, s_name, period_location);
75 for (i = period_location; i < 8; i++)
76 dirent->name[i] = ' ';
77 } else if (period_location == 8) {
78 memcpy(dirent->name, s_name, period_location);
80 memcpy(dirent->name, s_name, 6);
81 dirent->name[6] = '~';
82 dirent->name[7] = '1';
86 memcpy(dirent->ext, s_name + period_location + 1, ext_num);
87 for (i = ext_num; i < 3; i++)
90 memcpy(dirent->ext, s_name + period_location + 1, 3);
92 debug("name : %s\n", dirent->name);
93 debug("ext : %s\n", dirent->ext);
96 static __u8 num_of_fats;
98 * Write fat buffer into block device
100 static int flush_fat_buffer(fsdata *mydata)
102 int getsize = FATBUFBLOCKS;
103 __u32 fatlength = mydata->fatlength;
104 __u8 *bufptr = mydata->fatbuf;
105 __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
107 startblock += mydata->fat_sect;
109 if (getsize > fatlength)
113 if (disk_write(startblock, getsize, bufptr) < 0) {
114 debug("error: writing FAT blocks\n");
118 if (num_of_fats == 2) {
119 /* Update corresponding second FAT blocks */
120 startblock += mydata->fatlength;
121 if (disk_write(startblock, getsize, bufptr) < 0) {
122 debug("error: writing second FAT blocks\n");
131 * Get the entry at index 'entry' in a FAT (12/16/32) table.
132 * On failure 0x00 is returned.
133 * When bufnum is changed, write back the previous fatbuf to the disk.
135 static __u32 get_fatent_value(fsdata *mydata, __u32 entry)
142 if (CHECK_CLUST(entry, mydata->fatsize)) {
143 printf("Error: Invalid FAT entry: 0x%08x\n", entry);
147 switch (mydata->fatsize) {
149 bufnum = entry / FAT32BUFSIZE;
150 offset = entry - bufnum * FAT32BUFSIZE;
153 bufnum = entry / FAT16BUFSIZE;
154 offset = entry - bufnum * FAT16BUFSIZE;
157 bufnum = entry / FAT12BUFSIZE;
158 offset = entry - bufnum * FAT12BUFSIZE;
162 /* Unsupported FAT size */
166 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
167 mydata->fatsize, entry, entry, offset, offset);
169 /* Read a new block of FAT entries into the cache. */
170 if (bufnum != mydata->fatbufnum) {
171 int getsize = FATBUFBLOCKS;
172 __u8 *bufptr = mydata->fatbuf;
173 __u32 fatlength = mydata->fatlength;
174 __u32 startblock = bufnum * FATBUFBLOCKS;
176 if (getsize > fatlength)
179 fatlength *= mydata->sect_size; /* We want it in bytes now */
180 startblock += mydata->fat_sect; /* Offset from start of disk */
182 /* Write back the fatbuf to the disk */
183 if (mydata->fatbufnum != -1) {
184 if (flush_fat_buffer(mydata) < 0)
188 if (disk_read(startblock, getsize, bufptr) < 0) {
189 debug("Error reading FAT blocks\n");
192 mydata->fatbufnum = bufnum;
195 /* Get the actual entry from the table */
196 switch (mydata->fatsize) {
198 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
201 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
204 off16 = (offset * 3) / 4;
206 switch (offset & 0x3) {
208 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
212 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
214 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
216 ret = (val2 << 4) | (val1 >> 12);
219 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
221 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
223 ret = (val2 << 8) | (val1 >> 8);
226 ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
227 ret = (ret & 0xfff0) >> 4;
234 debug("FAT%d: ret: %08x, entry: %08x, offset: %04x\n",
235 mydata->fatsize, ret, entry, offset);
241 * Set the file name information from 'name' into 'slotptr',
243 static int str2slot(dir_slot *slotptr, const char *name, int *idx)
247 for (j = 0; j <= 8; j += 2) {
248 if (name[*idx] == 0x00) {
249 slotptr->name0_4[j] = 0;
250 slotptr->name0_4[j + 1] = 0;
254 slotptr->name0_4[j] = name[*idx];
258 for (j = 0; j <= 10; j += 2) {
259 if (name[*idx] == 0x00) {
260 slotptr->name5_10[j] = 0;
261 slotptr->name5_10[j + 1] = 0;
265 slotptr->name5_10[j] = name[*idx];
269 for (j = 0; j <= 2; j += 2) {
270 if (name[*idx] == 0x00) {
271 slotptr->name11_12[j] = 0;
272 slotptr->name11_12[j + 1] = 0;
276 slotptr->name11_12[j] = name[*idx];
281 if (name[*idx] == 0x00)
285 /* Not used characters are filled with 0xff 0xff */
287 for (; end_idx < 5; end_idx++) {
288 slotptr->name0_4[end_idx * 2] = 0xff;
289 slotptr->name0_4[end_idx * 2 + 1] = 0xff;
294 for (; end_idx < 6; end_idx++) {
295 slotptr->name5_10[end_idx * 2] = 0xff;
296 slotptr->name5_10[end_idx * 2 + 1] = 0xff;
301 for (; end_idx < 2; end_idx++) {
302 slotptr->name11_12[end_idx * 2] = 0xff;
303 slotptr->name11_12[end_idx * 2 + 1] = 0xff;
309 static int is_next_clust(fsdata *mydata, dir_entry *dentptr);
310 static void flush_dir_table(fsdata *mydata, dir_entry **dentptr);
313 * Fill dir_slot entries with appropriate name, id, and attr
314 * The real directory entry is returned by 'dentptr'
317 fill_dir_slot(fsdata *mydata, dir_entry **dentptr, const char *l_name)
319 dir_slot *slotptr = (dir_slot *)get_contents_vfatname_block;
320 __u8 counter = 0, checksum;
324 /* Get short file name and checksum value */
325 strncpy(s_name, (*dentptr)->name, 16);
326 checksum = mkcksum((*dentptr)->name, (*dentptr)->ext);
329 memset(slotptr, 0x00, sizeof(dir_slot));
330 ret = str2slot(slotptr, l_name, &idx);
331 slotptr->id = ++counter;
332 slotptr->attr = ATTR_VFAT;
333 slotptr->alias_checksum = checksum;
338 slotptr->id |= LAST_LONG_ENTRY_MASK;
340 while (counter >= 1) {
341 if (is_next_clust(mydata, *dentptr)) {
342 /* A new cluster is allocated for directory table */
343 flush_dir_table(mydata, dentptr);
345 memcpy(*dentptr, slotptr, sizeof(dir_slot));
351 if (is_next_clust(mydata, *dentptr)) {
352 /* A new cluster is allocated for directory table */
353 flush_dir_table(mydata, dentptr);
357 static __u32 dir_curclust;
360 * Extract the full long filename starting at 'retdent' (which is really
361 * a slot) into 'l_name'. If successful also copy the real directory entry
363 * If additional adjacent cluster for directory entries is read into memory,
364 * then 'get_contents_vfatname_block' is copied into 'get_dentfromdir_block' and
365 * the location of the real directory entry is returned by 'retdent'
366 * Return 0 on success, -1 otherwise.
369 get_long_file_name(fsdata *mydata, int curclust, __u8 *cluster,
370 dir_entry **retdent, char *l_name)
373 dir_slot *slotptr = (dir_slot *)(*retdent);
374 dir_slot *slotptr2 = NULL;
375 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
378 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
379 int idx = 0, cur_position = 0;
381 if (counter > VFAT_MAXSEQ) {
382 debug("Error: VFAT name is too long\n");
386 while ((__u8 *)slotptr < buflimit) {
389 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
395 if ((__u8 *)slotptr >= buflimit) {
398 curclust = get_fatent_value(mydata, dir_curclust);
399 if (CHECK_CLUST(curclust, mydata->fatsize)) {
400 debug("curclust: 0x%x\n", curclust);
401 printf("Invalid FAT entry\n");
405 dir_curclust = curclust;
407 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
408 mydata->clust_size * mydata->sect_size) != 0) {
409 debug("Error: reading directory block\n");
413 slotptr2 = (dir_slot *)get_contents_vfatname_block;
414 while (counter > 0) {
415 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
422 /* Save the real directory entry */
423 realdent = (dir_entry *)slotptr2;
424 while ((__u8 *)slotptr2 > get_contents_vfatname_block) {
426 slot2str(slotptr2, l_name, &idx);
429 /* Save the real directory entry */
430 realdent = (dir_entry *)slotptr;
435 if (slot2str(slotptr, l_name, &idx))
437 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
440 if (*l_name == DELETED_FLAG)
442 else if (*l_name == aRING)
443 *l_name = DELETED_FLAG;
446 /* Return the real directory entry */
450 memcpy(get_dentfromdir_block, get_contents_vfatname_block,
451 mydata->clust_size * mydata->sect_size);
452 cur_position = (__u8 *)realdent - get_contents_vfatname_block;
453 *retdent = (dir_entry *) &get_dentfromdir_block[cur_position];
460 * Set the entry at index 'entry' in a FAT (16/32) table.
462 static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
464 __u32 bufnum, offset;
466 switch (mydata->fatsize) {
468 bufnum = entry / FAT32BUFSIZE;
469 offset = entry - bufnum * FAT32BUFSIZE;
472 bufnum = entry / FAT16BUFSIZE;
473 offset = entry - bufnum * FAT16BUFSIZE;
476 /* Unsupported FAT size */
480 /* Read a new block of FAT entries into the cache. */
481 if (bufnum != mydata->fatbufnum) {
482 int getsize = FATBUFBLOCKS;
483 __u8 *bufptr = mydata->fatbuf;
484 __u32 fatlength = mydata->fatlength;
485 __u32 startblock = bufnum * FATBUFBLOCKS;
487 fatlength *= mydata->sect_size;
488 startblock += mydata->fat_sect;
490 if (getsize > fatlength)
493 if (mydata->fatbufnum != -1) {
494 if (flush_fat_buffer(mydata) < 0)
498 if (disk_read(startblock, getsize, bufptr) < 0) {
499 debug("Error reading FAT blocks\n");
502 mydata->fatbufnum = bufnum;
505 /* Set the actual entry */
506 switch (mydata->fatsize) {
508 ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
511 ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
521 * Determine the entry value at index 'entry' in a FAT (16/32) table
523 static __u32 determine_fatent(fsdata *mydata, __u32 entry)
525 __u32 next_fat, next_entry = entry + 1;
528 next_fat = get_fatent_value(mydata, next_entry);
530 set_fatent_value(mydata, entry, next_entry);
535 debug("FAT%d: entry: %08x, entry_value: %04x\n",
536 mydata->fatsize, entry, next_entry);
542 * Write at most 'size' bytes from 'buffer' into the specified cluster.
543 * Return 0 on success, -1 otherwise.
546 set_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer,
553 startsect = mydata->data_begin +
554 clustnum * mydata->clust_size;
556 startsect = mydata->rootdir_sect;
558 debug("clustnum: %d, startsect: %d\n", clustnum, startsect);
560 if ((size / mydata->sect_size) > 0) {
561 if (disk_write(startsect, size / mydata->sect_size, buffer) < 0) {
562 debug("Error writing data\n");
567 if (size % mydata->sect_size) {
568 __u8 tmpbuf[mydata->sect_size];
570 idx = size / mydata->sect_size;
571 buffer += idx * mydata->sect_size;
572 memcpy(tmpbuf, buffer, size % mydata->sect_size);
574 if (disk_write(startsect + idx, 1, tmpbuf) < 0) {
575 debug("Error writing data\n");
586 * Find the first empty cluster
588 static int find_empty_cluster(fsdata *mydata)
590 __u32 fat_val, entry = 3;
593 fat_val = get_fatent_value(mydata, entry);
603 * Write directory entries in 'get_dentfromdir_block' to block device
605 static void flush_dir_table(fsdata *mydata, dir_entry **dentptr)
607 int dir_newclust = 0;
609 if (set_cluster(mydata, dir_curclust,
610 get_dentfromdir_block,
611 mydata->clust_size * mydata->sect_size) != 0) {
612 printf("error: wrinting directory entry\n");
615 dir_newclust = find_empty_cluster(mydata);
616 set_fatent_value(mydata, dir_curclust, dir_newclust);
617 if (mydata->fatsize == 32)
618 set_fatent_value(mydata, dir_newclust, 0xffffff8);
619 else if (mydata->fatsize == 16)
620 set_fatent_value(mydata, dir_newclust, 0xfff8);
622 dir_curclust = dir_newclust;
624 if (flush_fat_buffer(mydata) < 0)
627 memset(get_dentfromdir_block, 0x00,
628 mydata->clust_size * mydata->sect_size);
630 *dentptr = (dir_entry *) get_dentfromdir_block;
634 * Set empty cluster from 'entry' to the end of a file
636 static int clear_fatent(fsdata *mydata, __u32 entry)
641 fat_val = get_fatent_value(mydata, entry);
643 set_fatent_value(mydata, entry, 0);
647 if (fat_val == 0xfffffff || fat_val == 0xffff)
653 /* Flush fat buffer */
654 if (flush_fat_buffer(mydata) < 0)
661 * Write at most 'maxsize' bytes from 'buffer' into
662 * the file associated with 'dentptr'
663 * Return the number of bytes read or -1 on fatal errors.
666 set_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
667 unsigned long maxsize)
669 unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
670 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
671 __u32 curclust = START(dentptr);
672 __u32 endclust = 0, newclust = 0;
673 unsigned long actsize;
675 debug("Filesize: %ld bytes\n", filesize);
677 if (maxsize > 0 && filesize > maxsize)
680 debug("%ld bytes\n", filesize);
682 actsize = bytesperclust;
685 /* search for consecutive clusters */
686 while (actsize < filesize) {
687 newclust = determine_fatent(mydata, endclust);
689 if ((newclust - 1) != endclust)
692 if (CHECK_CLUST(newclust, mydata->fatsize)) {
693 debug("curclust: 0x%x\n", newclust);
694 debug("Invalid FAT entry\n");
698 actsize += bytesperclust;
700 /* actsize >= file size */
701 actsize -= bytesperclust;
702 /* set remaining clusters */
703 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
704 debug("error: writing cluster\n");
708 /* set remaining bytes */
709 gotsize += (int)actsize;
714 if (set_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
715 debug("error: writing cluster\n");
720 /* Mark end of file in FAT */
721 if (mydata->fatsize == 16)
723 else if (mydata->fatsize == 32)
724 newclust = 0xfffffff;
725 set_fatent_value(mydata, endclust, newclust);
729 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
730 debug("error: writing cluster\n");
733 gotsize += (int)actsize;
737 if (CHECK_CLUST(curclust, mydata->fatsize)) {
738 debug("curclust: 0x%x\n", curclust);
739 debug("Invalid FAT entry\n");
742 actsize = bytesperclust;
743 curclust = endclust = newclust;
750 static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
751 const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
753 if (mydata->fatsize == 32)
755 cpu_to_le16((start_cluster & 0xffff0000) >> 16);
756 dentptr->start = cpu_to_le16(start_cluster & 0xffff);
757 dentptr->size = cpu_to_le32(size);
759 dentptr->attr = attr;
761 set_name(dentptr, filename);
765 * Check whether adding a file makes the file system to
766 * exceed the size of the block device
767 * Return -1 when overflow occurs, otherwise return 0
769 static int check_overflow(fsdata *mydata, __u32 clustnum, unsigned long size)
771 __u32 startsect, sect_num;
774 startsect = mydata->data_begin +
775 clustnum * mydata->clust_size;
777 startsect = mydata->rootdir_sect;
780 sect_num = size / mydata->sect_size;
781 if (size % mydata->sect_size)
784 if (startsect + sect_num > cur_part_info.start + total_sector)
791 * Check if adding several entries exceed one cluster boundary
793 static int is_next_clust(fsdata *mydata, dir_entry *dentptr)
797 cur_position = (__u8 *)dentptr - get_dentfromdir_block;
799 if (cur_position >= mydata->clust_size * mydata->sect_size)
805 static dir_entry *empty_dentptr;
807 * Find a directory entry based on filename or start cluster number
808 * If the directory entry is not found,
809 * the new position for writing a directory entry will be returned
811 static dir_entry *find_directory_entry(fsdata *mydata, int startsect,
812 char *filename, dir_entry *retdent, __u32 start)
814 __u32 curclust = (startsect - mydata->data_begin) / mydata->clust_size;
816 debug("get_dentfromdir: %s\n", filename);
823 if (get_cluster(mydata, curclust, get_dentfromdir_block,
824 mydata->clust_size * mydata->sect_size) != 0) {
825 printf("Error: reading directory block\n");
829 dentptr = (dir_entry *)get_dentfromdir_block;
831 dir_curclust = curclust;
833 for (i = 0; i < DIRENTSPERCLUST; i++) {
834 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
837 if (dentptr->name[0] == DELETED_FLAG) {
839 if (is_next_clust(mydata, dentptr))
843 if ((dentptr->attr & ATTR_VOLUME)) {
845 (dentptr->attr & ATTR_VFAT) &&
846 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
847 get_long_file_name(mydata, curclust,
848 get_dentfromdir_block,
850 debug("vfatname: |%s|\n", l_name);
852 /* Volume label or VFAT entry */
854 if (is_next_clust(mydata, dentptr))
859 if (dentptr->name[0] == 0) {
860 debug("Dentname == NULL - %d\n", i);
861 empty_dentptr = dentptr;
865 get_name(dentptr, s_name);
867 if (strcmp(filename, s_name)
868 && strcmp(filename, l_name)) {
869 debug("Mismatch: |%s|%s|\n",
872 if (is_next_clust(mydata, dentptr))
877 memcpy(retdent, dentptr, sizeof(dir_entry));
879 debug("DentName: %s", s_name);
880 debug(", start: 0x%x", START(dentptr));
881 debug(", size: 0x%x %s\n",
882 FAT2CPU32(dentptr->size),
883 (dentptr->attr & ATTR_DIR) ?
890 * In FAT16/12, the root dir is locate before data area, shows
892 * -------------------------------------------------------------
893 * | Boot | FAT1 & 2 | Root dir | Data (start from cluster #2) |
894 * -------------------------------------------------------------
896 * As a result if curclust is in Root dir, it is a negative
900 if (mydata->fatsize != 32 && (int)curclust <= 1) {
901 /* Current clust is in root dir, set to next clust */
903 if ((int)curclust <= 1)
904 continue; /* continue to find */
906 /* Reach the end of root dir */
907 empty_dentptr = dentptr;
911 curclust = get_fatent_value(mydata, dir_curclust);
912 if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
913 empty_dentptr = dentptr;
916 if (CHECK_CLUST(curclust, mydata->fatsize)) {
917 debug("curclust: 0x%x\n", curclust);
918 debug("Invalid FAT entry\n");
926 static int do_fat_write(const char *filename, void *buffer,
929 dir_entry *dentptr, *retdent;
935 fsdata *mydata = &datablock;
937 int ret = -1, name_len;
938 char l_filename[VFAT_MAXLEN_BYTES];
939 int write_size = size;
943 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
944 debug("error: reading boot sector\n");
948 total_sector = bs.total_sect;
949 if (total_sector == 0)
950 total_sector = (int)cur_part_info.size; /* cast of lbaint_t */
952 if (mydata->fatsize == 32)
953 mydata->fatlength = bs.fat32_length;
955 mydata->fatlength = bs.fat_length;
957 mydata->fat_sect = bs.reserved;
959 cursect = mydata->rootdir_sect
960 = mydata->fat_sect + mydata->fatlength * bs.fats;
961 num_of_fats = bs.fats;
963 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
964 mydata->clust_size = bs.cluster_size;
966 if (mydata->fatsize == 32) {
967 mydata->data_begin = mydata->rootdir_sect -
968 (mydata->clust_size * 2);
972 rootdir_size = ((bs.dir_entries[1] * (int)256 +
976 mydata->data_begin = mydata->rootdir_sect +
978 (mydata->clust_size * 2);
981 mydata->fatbufnum = -1;
982 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
983 if (mydata->fatbuf == NULL) {
984 debug("Error: allocating memory\n");
988 if (disk_read(cursect,
989 (mydata->fatsize == 32) ?
990 (mydata->clust_size) :
991 PREFETCH_BLOCKS, do_fat_read_at_block) < 0) {
992 debug("Error: reading rootdir block\n");
995 dentptr = (dir_entry *) do_fat_read_at_block;
997 name_len = strlen(filename);
998 if (name_len >= VFAT_MAXLEN_BYTES)
999 name_len = VFAT_MAXLEN_BYTES - 1;
1001 memcpy(l_filename, filename, name_len);
1002 l_filename[name_len] = 0; /* terminate the string */
1003 downcase(l_filename);
1005 startsect = mydata->rootdir_sect;
1006 retdent = find_directory_entry(mydata, startsect,
1007 l_filename, dentptr, 0);
1009 /* Update file size and start_cluster in a directory entry */
1010 retdent->size = cpu_to_le32(size);
1011 start_cluster = FAT2CPU16(retdent->start);
1012 if (mydata->fatsize == 32)
1014 (FAT2CPU16(retdent->starthi) << 16);
1016 ret = check_overflow(mydata, start_cluster, size);
1018 printf("Error: %ld overflow\n", size);
1022 ret = clear_fatent(mydata, start_cluster);
1024 printf("Error: clearing FAT entries\n");
1028 ret = set_contents(mydata, retdent, buffer, size);
1030 printf("Error: writing contents\n");
1034 debug("attempt to write 0x%x bytes\n", write_size);
1036 /* Flush fat buffer */
1037 ret = flush_fat_buffer(mydata);
1039 printf("Error: flush fat buffer\n");
1043 /* Write directory table to device */
1044 ret = set_cluster(mydata, dir_curclust,
1045 get_dentfromdir_block,
1046 mydata->clust_size * mydata->sect_size);
1048 printf("Error: writing directory entry\n");
1052 /* Set short name to set alias checksum field in dir_slot */
1053 set_name(empty_dentptr, filename);
1054 fill_dir_slot(mydata, &empty_dentptr, filename);
1056 ret = start_cluster = find_empty_cluster(mydata);
1058 printf("Error: finding empty cluster\n");
1062 ret = check_overflow(mydata, start_cluster, size);
1064 printf("Error: %ld overflow\n", size);
1068 /* Set attribute as archieve for regular file */
1069 fill_dentry(mydata, empty_dentptr, filename,
1070 start_cluster, size, 0x20);
1072 ret = set_contents(mydata, empty_dentptr, buffer, size);
1074 printf("Error: writing contents\n");
1078 debug("attempt to write 0x%x bytes\n", write_size);
1080 /* Flush fat buffer */
1081 ret = flush_fat_buffer(mydata);
1083 printf("Error: flush fat buffer\n");
1087 /* Write directory table to device */
1088 ret = set_cluster(mydata, dir_curclust,
1089 get_dentfromdir_block,
1090 mydata->clust_size * mydata->sect_size);
1092 printf("Error: writing directory entry\n");
1098 free(mydata->fatbuf);
1099 return ret < 0 ? ret : write_size;
1102 int file_fat_write(const char *filename, void *buffer, unsigned long maxsize)
1104 printf("writing %s\n", filename);
1105 return do_fat_write(filename, buffer, maxsize);