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 memcpy(s_name, filename, len);
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 switch (mydata->fatsize) {
144 bufnum = entry / FAT32BUFSIZE;
145 offset = entry - bufnum * FAT32BUFSIZE;
148 bufnum = entry / FAT16BUFSIZE;
149 offset = entry - bufnum * FAT16BUFSIZE;
152 bufnum = entry / FAT12BUFSIZE;
153 offset = entry - bufnum * FAT12BUFSIZE;
157 /* Unsupported FAT size */
161 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
162 mydata->fatsize, entry, entry, offset, offset);
164 /* Read a new block of FAT entries into the cache. */
165 if (bufnum != mydata->fatbufnum) {
166 int getsize = FATBUFBLOCKS;
167 __u8 *bufptr = mydata->fatbuf;
168 __u32 fatlength = mydata->fatlength;
169 __u32 startblock = bufnum * FATBUFBLOCKS;
171 if (getsize > fatlength)
174 fatlength *= mydata->sect_size; /* We want it in bytes now */
175 startblock += mydata->fat_sect; /* Offset from start of disk */
177 /* Write back the fatbuf to the disk */
178 if (mydata->fatbufnum != -1) {
179 if (flush_fat_buffer(mydata) < 0)
183 if (disk_read(startblock, getsize, bufptr) < 0) {
184 debug("Error reading FAT blocks\n");
187 mydata->fatbufnum = bufnum;
190 /* Get the actual entry from the table */
191 switch (mydata->fatsize) {
193 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
196 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
199 off16 = (offset * 3) / 4;
201 switch (offset & 0x3) {
203 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
207 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
209 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
211 ret = (val2 << 4) | (val1 >> 12);
214 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
216 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
218 ret = (val2 << 8) | (val1 >> 8);
221 ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
222 ret = (ret & 0xfff0) >> 4;
229 debug("FAT%d: ret: %08x, entry: %08x, offset: %04x\n",
230 mydata->fatsize, ret, entry, offset);
236 * Set the file name information from 'name' into 'slotptr',
238 static int str2slot(dir_slot *slotptr, const char *name, int *idx)
242 for (j = 0; j <= 8; j += 2) {
243 if (name[*idx] == 0x00) {
244 slotptr->name0_4[j] = 0;
245 slotptr->name0_4[j + 1] = 0;
249 slotptr->name0_4[j] = name[*idx];
253 for (j = 0; j <= 10; j += 2) {
254 if (name[*idx] == 0x00) {
255 slotptr->name5_10[j] = 0;
256 slotptr->name5_10[j + 1] = 0;
260 slotptr->name5_10[j] = name[*idx];
264 for (j = 0; j <= 2; j += 2) {
265 if (name[*idx] == 0x00) {
266 slotptr->name11_12[j] = 0;
267 slotptr->name11_12[j + 1] = 0;
271 slotptr->name11_12[j] = name[*idx];
276 if (name[*idx] == 0x00)
280 /* Not used characters are filled with 0xff 0xff */
282 for (; end_idx < 5; end_idx++) {
283 slotptr->name0_4[end_idx * 2] = 0xff;
284 slotptr->name0_4[end_idx * 2 + 1] = 0xff;
289 for (; end_idx < 6; end_idx++) {
290 slotptr->name5_10[end_idx * 2] = 0xff;
291 slotptr->name5_10[end_idx * 2 + 1] = 0xff;
296 for (; end_idx < 2; end_idx++) {
297 slotptr->name11_12[end_idx * 2] = 0xff;
298 slotptr->name11_12[end_idx * 2 + 1] = 0xff;
304 static int is_next_clust(fsdata *mydata, dir_entry *dentptr);
305 static void flush_dir_table(fsdata *mydata, dir_entry **dentptr);
308 * Fill dir_slot entries with appropriate name, id, and attr
309 * The real directory entry is returned by 'dentptr'
312 fill_dir_slot(fsdata *mydata, dir_entry **dentptr, const char *l_name)
314 dir_slot *slotptr = (dir_slot *)get_contents_vfatname_block;
315 __u8 counter = 0, checksum;
319 /* Get short file name and checksum value */
320 strncpy(s_name, (*dentptr)->name, 16);
321 checksum = mkcksum((*dentptr)->name, (*dentptr)->ext);
324 memset(slotptr, 0x00, sizeof(dir_slot));
325 ret = str2slot(slotptr, l_name, &idx);
326 slotptr->id = ++counter;
327 slotptr->attr = ATTR_VFAT;
328 slotptr->alias_checksum = checksum;
333 slotptr->id |= LAST_LONG_ENTRY_MASK;
335 while (counter >= 1) {
336 if (is_next_clust(mydata, *dentptr)) {
337 /* A new cluster is allocated for directory table */
338 flush_dir_table(mydata, dentptr);
340 memcpy(*dentptr, slotptr, sizeof(dir_slot));
346 if (is_next_clust(mydata, *dentptr)) {
347 /* A new cluster is allocated for directory table */
348 flush_dir_table(mydata, dentptr);
352 static __u32 dir_curclust;
355 * Extract the full long filename starting at 'retdent' (which is really
356 * a slot) into 'l_name'. If successful also copy the real directory entry
358 * If additional adjacent cluster for directory entries is read into memory,
359 * then 'get_contents_vfatname_block' is copied into 'get_dentfromdir_block' and
360 * the location of the real directory entry is returned by 'retdent'
361 * Return 0 on success, -1 otherwise.
364 get_long_file_name(fsdata *mydata, int curclust, __u8 *cluster,
365 dir_entry **retdent, char *l_name)
368 dir_slot *slotptr = (dir_slot *)(*retdent);
369 dir_slot *slotptr2 = NULL;
370 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
373 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
374 int idx = 0, cur_position = 0;
376 if (counter > VFAT_MAXSEQ) {
377 debug("Error: VFAT name is too long\n");
381 while ((__u8 *)slotptr < buflimit) {
384 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
390 if ((__u8 *)slotptr >= buflimit) {
393 curclust = get_fatent_value(mydata, dir_curclust);
394 if (CHECK_CLUST(curclust, mydata->fatsize)) {
395 debug("curclust: 0x%x\n", curclust);
396 printf("Invalid FAT entry\n");
400 dir_curclust = curclust;
402 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
403 mydata->clust_size * mydata->sect_size) != 0) {
404 debug("Error: reading directory block\n");
408 slotptr2 = (dir_slot *)get_contents_vfatname_block;
409 while (counter > 0) {
410 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
417 /* Save the real directory entry */
418 realdent = (dir_entry *)slotptr2;
419 while ((__u8 *)slotptr2 > get_contents_vfatname_block) {
421 slot2str(slotptr2, l_name, &idx);
424 /* Save the real directory entry */
425 realdent = (dir_entry *)slotptr;
430 if (slot2str(slotptr, l_name, &idx))
432 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
435 if (*l_name == DELETED_FLAG)
437 else if (*l_name == aRING)
438 *l_name = DELETED_FLAG;
441 /* Return the real directory entry */
445 memcpy(get_dentfromdir_block, get_contents_vfatname_block,
446 mydata->clust_size * mydata->sect_size);
447 cur_position = (__u8 *)realdent - get_contents_vfatname_block;
448 *retdent = (dir_entry *) &get_dentfromdir_block[cur_position];
455 * Set the entry at index 'entry' in a FAT (16/32) table.
457 static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
459 __u32 bufnum, offset;
461 switch (mydata->fatsize) {
463 bufnum = entry / FAT32BUFSIZE;
464 offset = entry - bufnum * FAT32BUFSIZE;
467 bufnum = entry / FAT16BUFSIZE;
468 offset = entry - bufnum * FAT16BUFSIZE;
471 /* Unsupported FAT size */
475 /* Read a new block of FAT entries into the cache. */
476 if (bufnum != mydata->fatbufnum) {
477 int getsize = FATBUFBLOCKS;
478 __u8 *bufptr = mydata->fatbuf;
479 __u32 fatlength = mydata->fatlength;
480 __u32 startblock = bufnum * FATBUFBLOCKS;
482 fatlength *= mydata->sect_size;
483 startblock += mydata->fat_sect;
485 if (getsize > fatlength)
488 if (mydata->fatbufnum != -1) {
489 if (flush_fat_buffer(mydata) < 0)
493 if (disk_read(startblock, getsize, bufptr) < 0) {
494 debug("Error reading FAT blocks\n");
497 mydata->fatbufnum = bufnum;
500 /* Set the actual entry */
501 switch (mydata->fatsize) {
503 ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
506 ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
516 * Determine the entry value at index 'entry' in a FAT (16/32) table
518 static __u32 determine_fatent(fsdata *mydata, __u32 entry)
520 __u32 next_fat, next_entry = entry + 1;
523 next_fat = get_fatent_value(mydata, next_entry);
525 set_fatent_value(mydata, entry, next_entry);
530 debug("FAT%d: entry: %08x, entry_value: %04x\n",
531 mydata->fatsize, entry, next_entry);
537 * Write at most 'size' bytes from 'buffer' into the specified cluster.
538 * Return 0 on success, -1 otherwise.
541 set_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer,
548 startsect = mydata->data_begin +
549 clustnum * mydata->clust_size;
551 startsect = mydata->rootdir_sect;
553 debug("clustnum: %d, startsect: %d\n", clustnum, startsect);
555 if (disk_write(startsect, size / mydata->sect_size, buffer) < 0) {
556 debug("Error writing data\n");
560 if (size % mydata->sect_size) {
561 __u8 tmpbuf[mydata->sect_size];
563 idx = size / mydata->sect_size;
564 buffer += idx * mydata->sect_size;
565 memcpy(tmpbuf, buffer, size % mydata->sect_size);
567 if (disk_write(startsect + idx, 1, tmpbuf) < 0) {
568 debug("Error writing data\n");
579 * Find the first empty cluster
581 static int find_empty_cluster(fsdata *mydata)
583 __u32 fat_val, entry = 3;
586 fat_val = get_fatent_value(mydata, entry);
596 * Write directory entries in 'get_dentfromdir_block' to block device
598 static void flush_dir_table(fsdata *mydata, dir_entry **dentptr)
600 int dir_newclust = 0;
602 if (set_cluster(mydata, dir_curclust,
603 get_dentfromdir_block,
604 mydata->clust_size * mydata->sect_size) != 0) {
605 printf("error: wrinting directory entry\n");
608 dir_newclust = find_empty_cluster(mydata);
609 set_fatent_value(mydata, dir_curclust, dir_newclust);
610 if (mydata->fatsize == 32)
611 set_fatent_value(mydata, dir_newclust, 0xffffff8);
612 else if (mydata->fatsize == 16)
613 set_fatent_value(mydata, dir_newclust, 0xfff8);
615 dir_curclust = dir_newclust;
617 if (flush_fat_buffer(mydata) < 0)
620 memset(get_dentfromdir_block, 0x00,
621 mydata->clust_size * mydata->sect_size);
623 *dentptr = (dir_entry *) get_dentfromdir_block;
627 * Set empty cluster from 'entry' to the end of a file
629 static int clear_fatent(fsdata *mydata, __u32 entry)
634 fat_val = get_fatent_value(mydata, entry);
636 set_fatent_value(mydata, entry, 0);
640 if (fat_val == 0xfffffff || fat_val == 0xffff)
646 /* Flush fat buffer */
647 if (flush_fat_buffer(mydata) < 0)
654 * Write at most 'maxsize' bytes from 'buffer' into
655 * the file associated with 'dentptr'
656 * Return the number of bytes read or -1 on fatal errors.
659 set_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
660 unsigned long maxsize)
662 unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
663 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
664 __u32 curclust = START(dentptr);
665 __u32 endclust = 0, newclust = 0;
666 unsigned long actsize;
668 debug("Filesize: %ld bytes\n", filesize);
670 if (maxsize > 0 && filesize > maxsize)
673 debug("%ld bytes\n", filesize);
675 actsize = bytesperclust;
678 /* search for consecutive clusters */
679 while (actsize < filesize) {
680 newclust = determine_fatent(mydata, endclust);
682 if ((newclust - 1) != endclust)
685 if (CHECK_CLUST(newclust, mydata->fatsize)) {
686 debug("curclust: 0x%x\n", newclust);
687 debug("Invalid FAT entry\n");
691 actsize += bytesperclust;
693 /* actsize >= file size */
694 actsize -= bytesperclust;
695 /* set remaining clusters */
696 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
697 debug("error: writing cluster\n");
701 /* set remaining bytes */
702 gotsize += (int)actsize;
707 if (set_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
708 debug("error: writing cluster\n");
713 /* Mark end of file in FAT */
714 if (mydata->fatsize == 16)
716 else if (mydata->fatsize == 32)
717 newclust = 0xfffffff;
718 set_fatent_value(mydata, endclust, newclust);
722 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
723 debug("error: writing cluster\n");
726 gotsize += (int)actsize;
730 if (CHECK_CLUST(curclust, mydata->fatsize)) {
731 debug("curclust: 0x%x\n", curclust);
732 debug("Invalid FAT entry\n");
735 actsize = bytesperclust;
736 curclust = endclust = newclust;
743 static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
744 const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
746 if (mydata->fatsize == 32)
748 cpu_to_le16((start_cluster & 0xffff0000) >> 16);
749 dentptr->start = cpu_to_le16(start_cluster & 0xffff);
750 dentptr->size = cpu_to_le32(size);
752 dentptr->attr = attr;
754 set_name(dentptr, filename);
758 * Check whether adding a file makes the file system to
759 * exceed the size of the block device
760 * Return -1 when overflow occurs, otherwise return 0
762 static int check_overflow(fsdata *mydata, __u32 clustnum, unsigned long size)
764 __u32 startsect, sect_num;
767 startsect = mydata->data_begin +
768 clustnum * mydata->clust_size;
770 startsect = mydata->rootdir_sect;
773 sect_num = size / mydata->sect_size;
774 if (size % mydata->sect_size)
777 if (startsect + sect_num > cur_part_info.start + total_sector)
784 * Check if adding several entries exceed one cluster boundary
786 static int is_next_clust(fsdata *mydata, dir_entry *dentptr)
790 cur_position = (__u8 *)dentptr - get_dentfromdir_block;
792 if (cur_position >= mydata->clust_size * mydata->sect_size)
798 static dir_entry *empty_dentptr;
800 * Find a directory entry based on filename or start cluster number
801 * If the directory entry is not found,
802 * the new position for writing a directory entry will be returned
804 static dir_entry *find_directory_entry(fsdata *mydata, int startsect,
805 char *filename, dir_entry *retdent, __u32 start)
807 __u32 curclust = (startsect - mydata->data_begin) / mydata->clust_size;
809 debug("get_dentfromdir: %s\n", filename);
816 if (get_cluster(mydata, curclust, get_dentfromdir_block,
817 mydata->clust_size * mydata->sect_size) != 0) {
818 printf("Error: reading directory block\n");
822 dentptr = (dir_entry *)get_dentfromdir_block;
824 dir_curclust = curclust;
826 for (i = 0; i < DIRENTSPERCLUST; i++) {
827 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
830 if (dentptr->name[0] == DELETED_FLAG) {
832 if (is_next_clust(mydata, dentptr))
836 if ((dentptr->attr & ATTR_VOLUME)) {
838 (dentptr->attr & ATTR_VFAT) &&
839 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
840 get_long_file_name(mydata, curclust,
841 get_dentfromdir_block,
843 debug("vfatname: |%s|\n", l_name);
845 /* Volume label or VFAT entry */
847 if (is_next_clust(mydata, dentptr))
852 if (dentptr->name[0] == 0) {
853 debug("Dentname == NULL - %d\n", i);
854 empty_dentptr = dentptr;
858 get_name(dentptr, s_name);
860 if (strcmp(filename, s_name)
861 && strcmp(filename, l_name)) {
862 debug("Mismatch: |%s|%s|\n",
865 if (is_next_clust(mydata, dentptr))
870 memcpy(retdent, dentptr, sizeof(dir_entry));
872 debug("DentName: %s", s_name);
873 debug(", start: 0x%x", START(dentptr));
874 debug(", size: 0x%x %s\n",
875 FAT2CPU32(dentptr->size),
876 (dentptr->attr & ATTR_DIR) ?
882 curclust = get_fatent_value(mydata, dir_curclust);
883 if ((curclust >= 0xffffff8) || (curclust >= 0xfff8)) {
884 empty_dentptr = dentptr;
887 if (CHECK_CLUST(curclust, mydata->fatsize)) {
888 debug("curclust: 0x%x\n", curclust);
889 debug("Invalid FAT entry\n");
897 static int do_fat_write(const char *filename, void *buffer,
900 dir_entry *dentptr, *retdent;
906 fsdata *mydata = &datablock;
908 int ret = -1, name_len;
909 char l_filename[VFAT_MAXLEN_BYTES];
910 int write_size = size;
914 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
915 debug("error: reading boot sector\n");
919 total_sector = bs.total_sect;
920 if (total_sector == 0)
921 total_sector = cur_part_info.size;
923 if (mydata->fatsize == 32)
924 mydata->fatlength = bs.fat32_length;
926 mydata->fatlength = bs.fat_length;
928 mydata->fat_sect = bs.reserved;
930 cursect = mydata->rootdir_sect
931 = mydata->fat_sect + mydata->fatlength * bs.fats;
932 num_of_fats = bs.fats;
934 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
935 mydata->clust_size = bs.cluster_size;
937 if (mydata->fatsize == 32) {
938 mydata->data_begin = mydata->rootdir_sect -
939 (mydata->clust_size * 2);
943 rootdir_size = ((bs.dir_entries[1] * (int)256 +
947 mydata->data_begin = mydata->rootdir_sect +
949 (mydata->clust_size * 2);
952 mydata->fatbufnum = -1;
953 mydata->fatbuf = malloc(FATBUFSIZE);
954 if (mydata->fatbuf == NULL) {
955 debug("Error: allocating memory\n");
959 if (disk_read(cursect,
960 (mydata->fatsize == 32) ?
961 (mydata->clust_size) :
962 PREFETCH_BLOCKS, do_fat_read_at_block) < 0) {
963 debug("Error: reading rootdir block\n");
966 dentptr = (dir_entry *) do_fat_read_at_block;
968 name_len = strlen(filename);
969 if (name_len >= VFAT_MAXLEN_BYTES)
970 name_len = VFAT_MAXLEN_BYTES - 1;
972 memcpy(l_filename, filename, name_len);
973 l_filename[name_len] = 0; /* terminate the string */
974 downcase(l_filename);
976 startsect = mydata->rootdir_sect;
977 retdent = find_directory_entry(mydata, startsect,
978 l_filename, dentptr, 0);
980 /* Update file size and start_cluster in a directory entry */
981 retdent->size = cpu_to_le32(size);
982 start_cluster = FAT2CPU16(retdent->start);
983 if (mydata->fatsize == 32)
985 (FAT2CPU16(retdent->starthi) << 16);
987 ret = check_overflow(mydata, start_cluster, size);
989 printf("Error: %ld overflow\n", size);
993 ret = clear_fatent(mydata, start_cluster);
995 printf("Error: clearing FAT entries\n");
999 ret = set_contents(mydata, retdent, buffer, size);
1001 printf("Error: writing contents\n");
1005 debug("attempt to write 0x%x bytes\n", write_size);
1007 /* Flush fat buffer */
1008 ret = flush_fat_buffer(mydata);
1010 printf("Error: flush fat buffer\n");
1014 /* Write directory table to device */
1015 ret = set_cluster(mydata, dir_curclust,
1016 get_dentfromdir_block,
1017 mydata->clust_size * mydata->sect_size);
1019 printf("Error: writing directory entry\n");
1023 /* Set short name to set alias checksum field in dir_slot */
1024 set_name(empty_dentptr, filename);
1025 fill_dir_slot(mydata, &empty_dentptr, filename);
1027 ret = start_cluster = find_empty_cluster(mydata);
1029 printf("Error: finding empty cluster\n");
1033 ret = check_overflow(mydata, start_cluster, size);
1035 printf("Error: %ld overflow\n", size);
1039 /* Set attribute as archieve for regular file */
1040 fill_dentry(mydata, empty_dentptr, filename,
1041 start_cluster, size, 0x20);
1043 ret = set_contents(mydata, empty_dentptr, buffer, size);
1045 printf("Error: writing contents\n");
1049 debug("attempt to write 0x%x bytes\n", write_size);
1051 /* Flush fat buffer */
1052 ret = flush_fat_buffer(mydata);
1054 printf("Error: flush fat buffer\n");
1058 /* Write directory table to device */
1059 ret = set_cluster(mydata, dir_curclust,
1060 get_dentfromdir_block,
1061 mydata->clust_size * mydata->sect_size);
1063 printf("Error: writing directory entry\n");
1069 free(mydata->fatbuf);
1070 return ret < 0 ? ret : write_size;
1073 int file_fat_write(const char *filename, void *buffer, unsigned long maxsize)
1075 printf("writing %s\n", filename);
1076 return do_fat_write(filename, buffer, maxsize);