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->dev,
45 cur_part_info.start + block,
47 if (nr_blocks && ret == 0)
54 * Set short name in directory entry
56 static void set_name(dir_entry *dirent, const char *filename)
58 char s_name[VFAT_MAXLEN_BYTES];
60 int period_location, len, i, ext_num;
65 len = strlen(filename);
69 strcpy(s_name, filename);
70 uppercase(s_name, len);
72 period = strchr(s_name, '.');
74 period_location = len;
77 period_location = period - s_name;
78 ext_num = len - period_location - 1;
81 /* Pad spaces when the length of file name is shorter than eight */
82 if (period_location < 8) {
83 memcpy(dirent->name, s_name, period_location);
84 for (i = period_location; i < 8; i++)
85 dirent->name[i] = ' ';
86 } else if (period_location == 8) {
87 memcpy(dirent->name, s_name, period_location);
89 memcpy(dirent->name, s_name, 6);
90 dirent->name[6] = '~';
91 dirent->name[7] = '1';
95 memcpy(dirent->ext, s_name + period_location + 1, ext_num);
96 for (i = ext_num; i < 3; i++)
99 memcpy(dirent->ext, s_name + period_location + 1, 3);
101 debug("name : %s\n", dirent->name);
102 debug("ext : %s\n", dirent->ext);
105 static __u8 num_of_fats;
107 * Write fat buffer into block device
109 static int flush_fat_buffer(fsdata *mydata)
111 int getsize = FATBUFBLOCKS;
112 __u32 fatlength = mydata->fatlength;
113 __u8 *bufptr = mydata->fatbuf;
114 __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
116 startblock += mydata->fat_sect;
118 if (getsize > fatlength)
122 if (disk_write(startblock, getsize, bufptr) < 0) {
123 debug("error: writing FAT blocks\n");
127 if (num_of_fats == 2) {
128 /* Update corresponding second FAT blocks */
129 startblock += mydata->fatlength;
130 if (disk_write(startblock, getsize, bufptr) < 0) {
131 debug("error: writing second FAT blocks\n");
140 * Get the entry at index 'entry' in a FAT (12/16/32) table.
141 * On failure 0x00 is returned.
142 * When bufnum is changed, write back the previous fatbuf to the disk.
144 static __u32 get_fatent_value(fsdata *mydata, __u32 entry)
151 if (CHECK_CLUST(entry, mydata->fatsize)) {
152 printf("Error: Invalid FAT entry: 0x%08x\n", entry);
156 switch (mydata->fatsize) {
158 bufnum = entry / FAT32BUFSIZE;
159 offset = entry - bufnum * FAT32BUFSIZE;
162 bufnum = entry / FAT16BUFSIZE;
163 offset = entry - bufnum * FAT16BUFSIZE;
166 bufnum = entry / FAT12BUFSIZE;
167 offset = entry - bufnum * FAT12BUFSIZE;
171 /* Unsupported FAT size */
175 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
176 mydata->fatsize, entry, entry, offset, offset);
178 /* Read a new block of FAT entries into the cache. */
179 if (bufnum != mydata->fatbufnum) {
180 int getsize = FATBUFBLOCKS;
181 __u8 *bufptr = mydata->fatbuf;
182 __u32 fatlength = mydata->fatlength;
183 __u32 startblock = bufnum * FATBUFBLOCKS;
185 if (getsize > fatlength)
188 fatlength *= mydata->sect_size; /* We want it in bytes now */
189 startblock += mydata->fat_sect; /* Offset from start of disk */
191 /* Write back the fatbuf to the disk */
192 if (mydata->fatbufnum != -1) {
193 if (flush_fat_buffer(mydata) < 0)
197 if (disk_read(startblock, getsize, bufptr) < 0) {
198 debug("Error reading FAT blocks\n");
201 mydata->fatbufnum = bufnum;
204 /* Get the actual entry from the table */
205 switch (mydata->fatsize) {
207 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
210 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
213 off16 = (offset * 3) / 4;
215 switch (offset & 0x3) {
217 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
221 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
223 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
225 ret = (val2 << 4) | (val1 >> 12);
228 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
230 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
232 ret = (val2 << 8) | (val1 >> 8);
235 ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
236 ret = (ret & 0xfff0) >> 4;
243 debug("FAT%d: ret: %08x, entry: %08x, offset: %04x\n",
244 mydata->fatsize, ret, entry, offset);
250 * Set the file name information from 'name' into 'slotptr',
252 static int str2slot(dir_slot *slotptr, const char *name, int *idx)
256 for (j = 0; j <= 8; j += 2) {
257 if (name[*idx] == 0x00) {
258 slotptr->name0_4[j] = 0;
259 slotptr->name0_4[j + 1] = 0;
263 slotptr->name0_4[j] = name[*idx];
267 for (j = 0; j <= 10; j += 2) {
268 if (name[*idx] == 0x00) {
269 slotptr->name5_10[j] = 0;
270 slotptr->name5_10[j + 1] = 0;
274 slotptr->name5_10[j] = name[*idx];
278 for (j = 0; j <= 2; j += 2) {
279 if (name[*idx] == 0x00) {
280 slotptr->name11_12[j] = 0;
281 slotptr->name11_12[j + 1] = 0;
285 slotptr->name11_12[j] = name[*idx];
290 if (name[*idx] == 0x00)
294 /* Not used characters are filled with 0xff 0xff */
296 for (; end_idx < 5; end_idx++) {
297 slotptr->name0_4[end_idx * 2] = 0xff;
298 slotptr->name0_4[end_idx * 2 + 1] = 0xff;
303 for (; end_idx < 6; end_idx++) {
304 slotptr->name5_10[end_idx * 2] = 0xff;
305 slotptr->name5_10[end_idx * 2 + 1] = 0xff;
310 for (; end_idx < 2; end_idx++) {
311 slotptr->name11_12[end_idx * 2] = 0xff;
312 slotptr->name11_12[end_idx * 2 + 1] = 0xff;
318 static int is_next_clust(fsdata *mydata, dir_entry *dentptr);
319 static void flush_dir_table(fsdata *mydata, dir_entry **dentptr);
322 * Fill dir_slot entries with appropriate name, id, and attr
323 * The real directory entry is returned by 'dentptr'
326 fill_dir_slot(fsdata *mydata, dir_entry **dentptr, const char *l_name)
328 dir_slot *slotptr = (dir_slot *)get_contents_vfatname_block;
329 __u8 counter = 0, checksum;
333 /* Get short file name and checksum value */
334 strncpy(s_name, (*dentptr)->name, 16);
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 (mydata->fatbufnum != -1) {
503 if (flush_fat_buffer(mydata) < 0)
507 if (disk_read(startblock, getsize, bufptr) < 0) {
508 debug("Error reading FAT blocks\n");
511 mydata->fatbufnum = bufnum;
514 /* Set the actual entry */
515 switch (mydata->fatsize) {
517 ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
520 ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
530 * Determine the entry value at index 'entry' in a FAT (16/32) table
532 static __u32 determine_fatent(fsdata *mydata, __u32 entry)
534 __u32 next_fat, next_entry = entry + 1;
537 next_fat = get_fatent_value(mydata, next_entry);
539 set_fatent_value(mydata, entry, next_entry);
544 debug("FAT%d: entry: %08x, entry_value: %04x\n",
545 mydata->fatsize, entry, next_entry);
551 * Write at most 'size' bytes from 'buffer' into the specified cluster.
552 * Return 0 on success, -1 otherwise.
555 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 ((size / mydata->sect_size) > 0) {
570 if (disk_write(startsect, size / mydata->sect_size, buffer) < 0) {
571 debug("Error writing data\n");
576 if (size % mydata->sect_size) {
577 __u8 tmpbuf[mydata->sect_size];
579 idx = size / mydata->sect_size;
580 buffer += idx * mydata->sect_size;
581 memcpy(tmpbuf, buffer, size % mydata->sect_size);
583 if (disk_write(startsect + idx, 1, tmpbuf) < 0) {
584 debug("Error writing data\n");
595 * Find the first empty cluster
597 static int find_empty_cluster(fsdata *mydata)
599 __u32 fat_val, entry = 3;
602 fat_val = get_fatent_value(mydata, entry);
612 * Write directory entries in 'get_dentfromdir_block' to block device
614 static void flush_dir_table(fsdata *mydata, dir_entry **dentptr)
616 int dir_newclust = 0;
618 if (set_cluster(mydata, dir_curclust,
619 get_dentfromdir_block,
620 mydata->clust_size * mydata->sect_size) != 0) {
621 printf("error: wrinting directory entry\n");
624 dir_newclust = find_empty_cluster(mydata);
625 set_fatent_value(mydata, dir_curclust, dir_newclust);
626 if (mydata->fatsize == 32)
627 set_fatent_value(mydata, dir_newclust, 0xffffff8);
628 else if (mydata->fatsize == 16)
629 set_fatent_value(mydata, dir_newclust, 0xfff8);
631 dir_curclust = dir_newclust;
633 if (flush_fat_buffer(mydata) < 0)
636 memset(get_dentfromdir_block, 0x00,
637 mydata->clust_size * mydata->sect_size);
639 *dentptr = (dir_entry *) get_dentfromdir_block;
643 * Set empty cluster from 'entry' to the end of a file
645 static int clear_fatent(fsdata *mydata, __u32 entry)
650 fat_val = get_fatent_value(mydata, entry);
652 set_fatent_value(mydata, entry, 0);
656 if (fat_val == 0xfffffff || fat_val == 0xffff)
662 /* Flush fat buffer */
663 if (flush_fat_buffer(mydata) < 0)
670 * Write at most 'maxsize' bytes from 'buffer' into
671 * the file associated with 'dentptr'
672 * Update the number of bytes written in *gotsize and return 0
673 * or return -1 on fatal errors.
676 set_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
677 loff_t maxsize, loff_t *gotsize)
679 loff_t filesize = FAT2CPU32(dentptr->size);
680 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
681 __u32 curclust = START(dentptr);
682 __u32 endclust = 0, newclust = 0;
686 debug("Filesize: %llu bytes\n", filesize);
688 if (maxsize > 0 && filesize > maxsize)
691 debug("%llu bytes\n", filesize);
693 actsize = bytesperclust;
696 /* search for consecutive clusters */
697 while (actsize < filesize) {
698 newclust = determine_fatent(mydata, endclust);
700 if ((newclust - 1) != endclust)
703 if (CHECK_CLUST(newclust, mydata->fatsize)) {
704 debug("curclust: 0x%x\n", newclust);
705 debug("Invalid FAT entry\n");
709 actsize += bytesperclust;
711 /* actsize >= file size */
712 actsize -= bytesperclust;
713 /* set remaining clusters */
714 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
715 debug("error: writing cluster\n");
719 /* set remaining bytes */
725 if (set_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
726 debug("error: writing cluster\n");
731 /* Mark end of file in FAT */
732 if (mydata->fatsize == 16)
734 else if (mydata->fatsize == 32)
735 newclust = 0xfffffff;
736 set_fatent_value(mydata, endclust, newclust);
740 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
741 debug("error: writing cluster\n");
748 if (CHECK_CLUST(curclust, mydata->fatsize)) {
749 debug("curclust: 0x%x\n", curclust);
750 debug("Invalid FAT entry\n");
753 actsize = bytesperclust;
754 curclust = endclust = newclust;
761 static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
762 const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
764 if (mydata->fatsize == 32)
766 cpu_to_le16((start_cluster & 0xffff0000) >> 16);
767 dentptr->start = cpu_to_le16(start_cluster & 0xffff);
768 dentptr->size = cpu_to_le32(size);
770 dentptr->attr = attr;
772 set_name(dentptr, filename);
776 * Check whether adding a file makes the file system to
777 * exceed the size of the block device
778 * Return -1 when overflow occurs, otherwise return 0
780 static int check_overflow(fsdata *mydata, __u32 clustnum, loff_t size)
782 __u32 startsect, sect_num, offset;
785 startsect = mydata->data_begin +
786 clustnum * mydata->clust_size;
788 startsect = mydata->rootdir_sect;
791 sect_num = div_u64_rem(size, mydata->sect_size, &offset);
796 if (startsect + sect_num > cur_part_info.start + total_sector)
802 * Check if adding several entries exceed one cluster boundary
804 static int is_next_clust(fsdata *mydata, dir_entry *dentptr)
808 cur_position = (__u8 *)dentptr - get_dentfromdir_block;
810 if (cur_position >= mydata->clust_size * mydata->sect_size)
816 static dir_entry *empty_dentptr;
818 * Find a directory entry based on filename or start cluster number
819 * If the directory entry is not found,
820 * the new position for writing a directory entry will be returned
822 static dir_entry *find_directory_entry(fsdata *mydata, int startsect,
823 char *filename, dir_entry *retdent, __u32 start)
825 __u32 curclust = (startsect - mydata->data_begin) / mydata->clust_size;
827 debug("get_dentfromdir: %s\n", filename);
834 if (get_cluster(mydata, curclust, get_dentfromdir_block,
835 mydata->clust_size * mydata->sect_size) != 0) {
836 printf("Error: reading directory block\n");
840 dentptr = (dir_entry *)get_dentfromdir_block;
842 dir_curclust = curclust;
844 for (i = 0; i < DIRENTSPERCLUST; i++) {
845 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
848 if (dentptr->name[0] == DELETED_FLAG) {
850 if (is_next_clust(mydata, dentptr))
854 if ((dentptr->attr & ATTR_VOLUME)) {
856 (dentptr->attr & ATTR_VFAT) &&
857 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
858 get_long_file_name(mydata, curclust,
859 get_dentfromdir_block,
861 debug("vfatname: |%s|\n", l_name);
863 /* Volume label or VFAT entry */
865 if (is_next_clust(mydata, dentptr))
870 if (dentptr->name[0] == 0) {
871 debug("Dentname == NULL - %d\n", i);
872 empty_dentptr = dentptr;
876 get_name(dentptr, s_name);
878 if (strcmp(filename, s_name)
879 && strcmp(filename, l_name)) {
880 debug("Mismatch: |%s|%s|\n",
883 if (is_next_clust(mydata, dentptr))
888 memcpy(retdent, dentptr, sizeof(dir_entry));
890 debug("DentName: %s", s_name);
891 debug(", start: 0x%x", START(dentptr));
892 debug(", size: 0x%x %s\n",
893 FAT2CPU32(dentptr->size),
894 (dentptr->attr & ATTR_DIR) ?
901 * In FAT16/12, the root dir is locate before data area, shows
903 * -------------------------------------------------------------
904 * | Boot | FAT1 & 2 | Root dir | Data (start from cluster #2) |
905 * -------------------------------------------------------------
907 * As a result if curclust is in Root dir, it is a negative
911 if (mydata->fatsize != 32 && (int)curclust <= 1) {
912 /* Current clust is in root dir, set to next clust */
914 if ((int)curclust <= 1)
915 continue; /* continue to find */
917 /* Reach the end of root dir */
918 empty_dentptr = dentptr;
922 curclust = get_fatent_value(mydata, dir_curclust);
923 if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
924 empty_dentptr = dentptr;
927 if (CHECK_CLUST(curclust, mydata->fatsize)) {
928 debug("curclust: 0x%x\n", curclust);
929 debug("Invalid FAT entry\n");
937 static int do_fat_write(const char *filename, void *buffer, loff_t size,
940 dir_entry *dentptr, *retdent;
946 fsdata *mydata = &datablock;
948 int ret = -1, name_len;
949 char l_filename[VFAT_MAXLEN_BYTES];
954 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
955 debug("error: reading boot sector\n");
959 total_sector = bs.total_sect;
960 if (total_sector == 0)
961 total_sector = (int)cur_part_info.size; /* cast of lbaint_t */
963 if (mydata->fatsize == 32)
964 mydata->fatlength = bs.fat32_length;
966 mydata->fatlength = bs.fat_length;
968 mydata->fat_sect = bs.reserved;
970 cursect = mydata->rootdir_sect
971 = mydata->fat_sect + mydata->fatlength * bs.fats;
972 num_of_fats = bs.fats;
974 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
975 mydata->clust_size = bs.cluster_size;
977 if (mydata->fatsize == 32) {
978 mydata->data_begin = mydata->rootdir_sect -
979 (mydata->clust_size * 2);
983 rootdir_size = ((bs.dir_entries[1] * (int)256 +
987 mydata->data_begin = mydata->rootdir_sect +
989 (mydata->clust_size * 2);
992 mydata->fatbufnum = -1;
993 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
994 if (mydata->fatbuf == NULL) {
995 debug("Error: allocating memory\n");
999 if (disk_read(cursect,
1000 (mydata->fatsize == 32) ?
1001 (mydata->clust_size) :
1002 PREFETCH_BLOCKS, do_fat_read_at_block) < 0) {
1003 debug("Error: reading rootdir block\n");
1006 dentptr = (dir_entry *) do_fat_read_at_block;
1008 name_len = strlen(filename);
1009 if (name_len >= VFAT_MAXLEN_BYTES)
1010 name_len = VFAT_MAXLEN_BYTES - 1;
1012 memcpy(l_filename, filename, name_len);
1013 l_filename[name_len] = 0; /* terminate the string */
1014 downcase(l_filename);
1016 startsect = mydata->rootdir_sect;
1017 retdent = find_directory_entry(mydata, startsect,
1018 l_filename, dentptr, 0);
1020 /* Update file size and start_cluster in a directory entry */
1021 retdent->size = cpu_to_le32(size);
1022 start_cluster = FAT2CPU16(retdent->start);
1023 if (mydata->fatsize == 32)
1025 (FAT2CPU16(retdent->starthi) << 16);
1027 ret = check_overflow(mydata, start_cluster, size);
1029 printf("Error: %llu overflow\n", size);
1033 ret = clear_fatent(mydata, start_cluster);
1035 printf("Error: clearing FAT entries\n");
1039 ret = set_contents(mydata, retdent, buffer, size, actwrite);
1041 printf("Error: writing contents\n");
1044 debug("attempt to write 0x%llx bytes\n", *actwrite);
1046 /* Flush fat buffer */
1047 ret = flush_fat_buffer(mydata);
1049 printf("Error: flush fat buffer\n");
1053 /* Write directory table to device */
1054 ret = set_cluster(mydata, dir_curclust,
1055 get_dentfromdir_block,
1056 mydata->clust_size * mydata->sect_size);
1058 printf("Error: writing directory entry\n");
1062 /* Set short name to set alias checksum field in dir_slot */
1063 set_name(empty_dentptr, filename);
1064 fill_dir_slot(mydata, &empty_dentptr, filename);
1066 ret = start_cluster = find_empty_cluster(mydata);
1068 printf("Error: finding empty cluster\n");
1072 ret = check_overflow(mydata, start_cluster, size);
1074 printf("Error: %llu overflow\n", size);
1078 /* Set attribute as archieve for regular file */
1079 fill_dentry(mydata, empty_dentptr, filename,
1080 start_cluster, size, 0x20);
1082 ret = set_contents(mydata, empty_dentptr, buffer, size,
1085 printf("Error: writing contents\n");
1088 debug("attempt to write 0x%llx bytes\n", *actwrite);
1090 /* Flush fat buffer */
1091 ret = flush_fat_buffer(mydata);
1093 printf("Error: flush fat buffer\n");
1097 /* Write directory table to device */
1098 ret = set_cluster(mydata, dir_curclust,
1099 get_dentfromdir_block,
1100 mydata->clust_size * mydata->sect_size);
1102 printf("Error: writing directory entry\n");
1108 free(mydata->fatbuf);
1112 int file_fat_write(const char *filename, void *buffer, loff_t offset,
1113 loff_t maxsize, loff_t *actwrite)
1116 printf("Error: non zero offset is currently not suported.\n");
1120 printf("writing %s\n", filename);
1121 return do_fat_write(filename, buffer, maxsize, actwrite);