4 * R/W (V)FAT 12/16/32 filesystem implementation by Donggeun Kim
6 * See file CREDITS for list of people who contributed to this
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 #include <asm/byteorder.h>
33 static void uppercase(char *str, int len)
37 for (i = 0; i < len; i++) {
43 static int total_sector;
44 static int disk_write(__u32 startblock, __u32 getsize, __u8 *bufptr)
49 if (startblock + getsize > total_sector) {
50 printf("error: overflow occurs\n");
54 startblock += part_offset;
56 if (cur_dev->block_read) {
57 return cur_dev->block_write(cur_dev->dev, startblock, getsize,
58 (unsigned long *) bufptr);
64 * Set short name in directory entry
66 static void set_name(dir_entry *dirent, const char *filename)
68 char s_name[VFAT_MAXLEN_BYTES];
70 int period_location, len, i, ext_num;
75 len = strlen(filename);
79 memcpy(s_name, filename, len);
80 uppercase(s_name, len);
82 period = strchr(s_name, '.');
84 period_location = len;
87 period_location = period - s_name;
88 ext_num = len - period_location - 1;
91 /* Pad spaces when the length of file name is shorter than eight */
92 if (period_location < 8) {
93 memcpy(dirent->name, s_name, period_location);
94 for (i = period_location; i < 8; i++)
95 dirent->name[i] = ' ';
96 } else if (period_location == 8) {
97 memcpy(dirent->name, s_name, period_location);
99 memcpy(dirent->name, s_name, 6);
100 dirent->name[6] = '~';
101 dirent->name[7] = '1';
105 memcpy(dirent->ext, s_name + period_location + 1, ext_num);
106 for (i = ext_num; i < 3; i++)
107 dirent->ext[i] = ' ';
109 memcpy(dirent->ext, s_name + period_location + 1, 3);
111 debug("name : %s\n", dirent->name);
112 debug("ext : %s\n", dirent->ext);
115 static __u8 num_of_fats;
117 * Write fat buffer into block device
119 static int flush_fat_buffer(fsdata *mydata)
121 int getsize = FATBUFBLOCKS;
122 __u32 fatlength = mydata->fatlength;
123 __u8 *bufptr = mydata->fatbuf;
124 __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
126 fatlength *= mydata->sect_size;
127 startblock += mydata->fat_sect;
129 if (getsize > fatlength)
133 if (disk_write(startblock, getsize, bufptr) < 0) {
134 debug("error: writing FAT blocks\n");
138 if (num_of_fats == 2) {
139 /* Update corresponding second FAT blocks */
140 startblock += mydata->fatlength;
141 if (disk_write(startblock, getsize, bufptr) < 0) {
142 debug("error: writing second FAT blocks\n");
151 * Get the entry at index 'entry' in a FAT (12/16/32) table.
152 * On failure 0x00 is returned.
153 * When bufnum is changed, write back the previous fatbuf to the disk.
155 static __u32 get_fatent_value(fsdata *mydata, __u32 entry)
162 switch (mydata->fatsize) {
164 bufnum = entry / FAT32BUFSIZE;
165 offset = entry - bufnum * FAT32BUFSIZE;
168 bufnum = entry / FAT16BUFSIZE;
169 offset = entry - bufnum * FAT16BUFSIZE;
172 bufnum = entry / FAT12BUFSIZE;
173 offset = entry - bufnum * FAT12BUFSIZE;
177 /* Unsupported FAT size */
181 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
182 mydata->fatsize, entry, entry, offset, offset);
184 /* Read a new block of FAT entries into the cache. */
185 if (bufnum != mydata->fatbufnum) {
186 int getsize = FATBUFBLOCKS;
187 __u8 *bufptr = mydata->fatbuf;
188 __u32 fatlength = mydata->fatlength;
189 __u32 startblock = bufnum * FATBUFBLOCKS;
191 if (getsize > fatlength)
194 fatlength *= mydata->sect_size; /* We want it in bytes now */
195 startblock += mydata->fat_sect; /* Offset from start of disk */
197 /* Write back the fatbuf to the disk */
198 if (mydata->fatbufnum != -1) {
199 if (flush_fat_buffer(mydata) < 0)
203 if (disk_read(startblock, getsize, bufptr) < 0) {
204 debug("Error reading FAT blocks\n");
207 mydata->fatbufnum = bufnum;
210 /* Get the actual entry from the table */
211 switch (mydata->fatsize) {
213 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
216 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
219 off16 = (offset * 3) / 4;
221 switch (offset & 0x3) {
223 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
227 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
229 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
231 ret = (val2 << 4) | (val1 >> 12);
234 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
236 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
238 ret = (val2 << 8) | (val1 >> 8);
241 ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
242 ret = (ret & 0xfff0) >> 4;
249 debug("FAT%d: ret: %08x, entry: %08x, offset: %04x\n",
250 mydata->fatsize, ret, entry, offset);
255 #ifdef CONFIG_SUPPORT_VFAT
257 * Set the file name information from 'name' into 'slotptr',
259 static int str2slot(dir_slot *slotptr, const char *name, int *idx)
263 for (j = 0; j <= 8; j += 2) {
264 if (name[*idx] == 0x00) {
265 slotptr->name0_4[j] = 0;
266 slotptr->name0_4[j + 1] = 0;
270 slotptr->name0_4[j] = name[*idx];
274 for (j = 0; j <= 10; j += 2) {
275 if (name[*idx] == 0x00) {
276 slotptr->name5_10[j] = 0;
277 slotptr->name5_10[j + 1] = 0;
281 slotptr->name5_10[j] = name[*idx];
285 for (j = 0; j <= 2; j += 2) {
286 if (name[*idx] == 0x00) {
287 slotptr->name11_12[j] = 0;
288 slotptr->name11_12[j + 1] = 0;
292 slotptr->name11_12[j] = name[*idx];
297 if (name[*idx] == 0x00)
301 /* Not used characters are filled with 0xff 0xff */
303 for (; end_idx < 5; end_idx++) {
304 slotptr->name0_4[end_idx * 2] = 0xff;
305 slotptr->name0_4[end_idx * 2 + 1] = 0xff;
310 for (; end_idx < 6; end_idx++) {
311 slotptr->name5_10[end_idx * 2] = 0xff;
312 slotptr->name5_10[end_idx * 2 + 1] = 0xff;
317 for (; end_idx < 2; end_idx++) {
318 slotptr->name11_12[end_idx * 2] = 0xff;
319 slotptr->name11_12[end_idx * 2 + 1] = 0xff;
325 static int is_next_clust(fsdata *mydata, dir_entry *dentptr);
326 static void flush_dir_table(fsdata *mydata, dir_entry **dentptr);
329 * Fill dir_slot entries with appropriate name, id, and attr
330 * The real directory entry is returned by 'dentptr'
333 fill_dir_slot(fsdata *mydata, dir_entry **dentptr, const char *l_name)
335 dir_slot *slotptr = (dir_slot *)get_vfatname_block;
336 __u8 counter = 0, checksum;
340 /* Get short file name and checksum value */
341 strncpy(s_name, (*dentptr)->name, 16);
342 checksum = mkcksum(s_name);
345 memset(slotptr, 0x00, sizeof(dir_slot));
346 ret = str2slot(slotptr, l_name, &idx);
347 slotptr->id = ++counter;
348 slotptr->attr = ATTR_VFAT;
349 slotptr->alias_checksum = checksum;
354 slotptr->id |= LAST_LONG_ENTRY_MASK;
356 while (counter >= 1) {
357 if (is_next_clust(mydata, *dentptr)) {
358 /* A new cluster is allocated for directory table */
359 flush_dir_table(mydata, dentptr);
361 memcpy(*dentptr, slotptr, sizeof(dir_slot));
367 if (is_next_clust(mydata, *dentptr)) {
368 /* A new cluster is allocated for directory table */
369 flush_dir_table(mydata, dentptr);
373 static __u32 dir_curclust;
376 * Extract the full long filename starting at 'retdent' (which is really
377 * a slot) into 'l_name'. If successful also copy the real directory entry
379 * If additional adjacent cluster for directory entries is read into memory,
380 * then 'get_vfatname_block' is copied into 'get_dentfromdir_block' and
381 * the location of the real directory entry is returned by 'retdent'
382 * Return 0 on success, -1 otherwise.
385 get_long_file_name(fsdata *mydata, int curclust, __u8 *cluster,
386 dir_entry **retdent, char *l_name)
389 dir_slot *slotptr = (dir_slot *)(*retdent);
390 dir_slot *slotptr2 = NULL;
391 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
394 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
395 int idx = 0, cur_position = 0;
397 if (counter > VFAT_MAXSEQ) {
398 debug("Error: VFAT name is too long\n");
402 while ((__u8 *)slotptr < buflimit) {
405 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
411 if ((__u8 *)slotptr >= buflimit) {
414 curclust = get_fatent_value(mydata, dir_curclust);
415 if (CHECK_CLUST(curclust, mydata->fatsize)) {
416 debug("curclust: 0x%x\n", curclust);
417 printf("Invalid FAT entry\n");
421 dir_curclust = curclust;
423 if (get_cluster(mydata, curclust, get_vfatname_block,
424 mydata->clust_size * mydata->sect_size) != 0) {
425 debug("Error: reading directory block\n");
429 slotptr2 = (dir_slot *)get_vfatname_block;
430 while (counter > 0) {
431 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
438 /* Save the real directory entry */
439 realdent = (dir_entry *)slotptr2;
440 while ((__u8 *)slotptr2 > get_vfatname_block) {
442 slot2str(slotptr2, l_name, &idx);
445 /* Save the real directory entry */
446 realdent = (dir_entry *)slotptr;
451 if (slot2str(slotptr, l_name, &idx))
453 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
456 if (*l_name == DELETED_FLAG)
458 else if (*l_name == aRING)
459 *l_name = DELETED_FLAG;
462 /* Return the real directory entry */
466 memcpy(get_dentfromdir_block, get_vfatname_block,
467 mydata->clust_size * mydata->sect_size);
468 cur_position = (__u8 *)realdent - get_vfatname_block;
469 *retdent = (dir_entry *) &get_dentfromdir_block[cur_position];
478 * Set the entry at index 'entry' in a FAT (16/32) table.
480 static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
482 __u32 bufnum, offset;
484 switch (mydata->fatsize) {
486 bufnum = entry / FAT32BUFSIZE;
487 offset = entry - bufnum * FAT32BUFSIZE;
490 bufnum = entry / FAT16BUFSIZE;
491 offset = entry - bufnum * FAT16BUFSIZE;
494 /* Unsupported FAT size */
498 /* Read a new block of FAT entries into the cache. */
499 if (bufnum != mydata->fatbufnum) {
500 int getsize = FATBUFBLOCKS;
501 __u8 *bufptr = mydata->fatbuf;
502 __u32 fatlength = mydata->fatlength;
503 __u32 startblock = bufnum * FATBUFBLOCKS;
505 fatlength *= mydata->sect_size;
506 startblock += mydata->fat_sect;
508 if (getsize > fatlength)
511 if (mydata->fatbufnum != -1) {
512 if (flush_fat_buffer(mydata) < 0)
516 if (disk_read(startblock, getsize, bufptr) < 0) {
517 debug("Error reading FAT blocks\n");
520 mydata->fatbufnum = bufnum;
523 /* Set the actual entry */
524 switch (mydata->fatsize) {
526 ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
529 ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
539 * Determine the entry value at index 'entry' in a FAT (16/32) table
541 static __u32 determine_fatent(fsdata *mydata, __u32 entry)
543 __u32 next_fat, next_entry = entry + 1;
546 next_fat = get_fatent_value(mydata, next_entry);
548 set_fatent_value(mydata, entry, next_entry);
553 debug("FAT%d: entry: %08x, entry_value: %04x\n",
554 mydata->fatsize, entry, next_entry);
560 * Write at most 'size' bytes from 'buffer' into the specified cluster.
561 * Return 0 on success, -1 otherwise.
564 set_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer,
571 startsect = mydata->data_begin +
572 clustnum * mydata->clust_size;
574 startsect = mydata->rootdir_sect;
576 debug("clustnum: %d, startsect: %d\n", clustnum, startsect);
578 if (disk_write(startsect, size / mydata->sect_size, buffer) < 0) {
579 debug("Error writing data\n");
583 if (size % mydata->sect_size) {
584 __u8 tmpbuf[mydata->sect_size];
586 idx = size / mydata->sect_size;
587 buffer += idx * mydata->sect_size;
588 memcpy(tmpbuf, buffer, size % mydata->sect_size);
590 if (disk_write(startsect + idx, 1, tmpbuf) < 0) {
591 debug("Error writing data\n");
602 * Find the first empty cluster
604 static int find_empty_cluster(fsdata *mydata)
606 __u32 fat_val, entry = 3;
609 fat_val = get_fatent_value(mydata, entry);
619 * Write directory entries in 'get_dentfromdir_block' to block device
621 static void flush_dir_table(fsdata *mydata, dir_entry **dentptr)
623 int dir_newclust = 0;
625 if (set_cluster(mydata, dir_curclust,
626 get_dentfromdir_block,
627 mydata->clust_size * mydata->sect_size) != 0) {
628 printf("error: wrinting directory entry\n");
631 dir_newclust = find_empty_cluster(mydata);
632 set_fatent_value(mydata, dir_curclust, dir_newclust);
633 if (mydata->fatsize == 32)
634 set_fatent_value(mydata, dir_newclust, 0xffffff8);
635 else if (mydata->fatsize == 16)
636 set_fatent_value(mydata, dir_newclust, 0xfff8);
638 dir_curclust = dir_newclust;
640 if (flush_fat_buffer(mydata) < 0)
643 memset(get_dentfromdir_block, 0x00,
644 mydata->clust_size * mydata->sect_size);
646 *dentptr = (dir_entry *) get_dentfromdir_block;
650 * Set empty cluster from 'entry' to the end of a file
652 static int clear_fatent(fsdata *mydata, __u32 entry)
657 fat_val = get_fatent_value(mydata, entry);
659 set_fatent_value(mydata, entry, 0);
663 if (fat_val == 0xfffffff || fat_val == 0xffff)
669 /* Flush fat buffer */
670 if (flush_fat_buffer(mydata) < 0)
677 * Write at most 'maxsize' bytes from 'buffer' into
678 * the file associated with 'dentptr'
679 * Return the number of bytes read or -1 on fatal errors.
682 set_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
683 unsigned long maxsize)
685 unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
686 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
687 __u32 curclust = START(dentptr);
688 __u32 endclust = 0, newclust = 0;
689 unsigned long actsize;
691 debug("Filesize: %ld bytes\n", filesize);
693 if (maxsize > 0 && filesize > maxsize)
696 debug("%ld bytes\n", filesize);
698 actsize = bytesperclust;
701 /* search for consecutive clusters */
702 while (actsize < filesize) {
703 newclust = determine_fatent(mydata, endclust);
705 if ((newclust - 1) != endclust)
708 if (CHECK_CLUST(newclust, mydata->fatsize)) {
709 debug("curclust: 0x%x\n", newclust);
710 debug("Invalid FAT entry\n");
714 actsize += bytesperclust;
716 /* actsize >= file size */
717 actsize -= bytesperclust;
718 /* set remaining clusters */
719 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
720 debug("error: writing cluster\n");
724 /* set remaining bytes */
725 gotsize += (int)actsize;
730 if (set_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
731 debug("error: writing cluster\n");
736 /* Mark end of file in FAT */
737 if (mydata->fatsize == 16)
739 else if (mydata->fatsize == 32)
740 newclust = 0xfffffff;
741 set_fatent_value(mydata, endclust, newclust);
745 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
746 debug("error: writing cluster\n");
749 gotsize += (int)actsize;
753 if (CHECK_CLUST(curclust, mydata->fatsize)) {
754 debug("curclust: 0x%x\n", curclust);
755 debug("Invalid FAT entry\n");
758 actsize = bytesperclust;
759 curclust = endclust = newclust;
766 static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
767 const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
769 if (mydata->fatsize == 32)
771 cpu_to_le16((start_cluster & 0xffff0000) >> 16);
772 dentptr->start = cpu_to_le16(start_cluster & 0xffff);
773 dentptr->size = cpu_to_le32(size);
775 dentptr->attr = attr;
777 set_name(dentptr, filename);
781 * Check whether adding a file makes the file system to
782 * exceed the size of the block device
783 * Return -1 when overflow occurs, otherwise return 0
785 static int check_overflow(fsdata *mydata, __u32 clustnum, unsigned long size)
787 __u32 startsect, sect_num;
790 startsect = mydata->data_begin +
791 clustnum * mydata->clust_size;
793 startsect = mydata->rootdir_sect;
796 sect_num = size / mydata->sect_size;
797 if (size % mydata->sect_size)
800 if (startsect + sect_num > total_sector)
807 * Check if adding several entries exceed one cluster boundary
809 static int is_next_clust(fsdata *mydata, dir_entry *dentptr)
813 cur_position = (__u8 *)dentptr - get_dentfromdir_block;
815 if (cur_position >= mydata->clust_size * mydata->sect_size)
821 static dir_entry *empty_dentptr;
823 * Find a directory entry based on filename or start cluster number
824 * If the directory entry is not found,
825 * the new position for writing a directory entry will be returned
827 static dir_entry *find_directory_entry(fsdata *mydata, int startsect,
828 char *filename, dir_entry *retdent, __u32 start)
830 __u16 prevcksum = 0xffff;
831 __u32 curclust = (startsect - mydata->data_begin) / mydata->clust_size;
833 debug("get_dentfromdir: %s\n", filename);
840 if (get_cluster(mydata, curclust, get_dentfromdir_block,
841 mydata->clust_size * mydata->sect_size) != 0) {
842 printf("Error: reading directory block\n");
846 dentptr = (dir_entry *)get_dentfromdir_block;
848 dir_curclust = curclust;
850 for (i = 0; i < DIRENTSPERCLUST; i++) {
851 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
854 if (dentptr->name[0] == DELETED_FLAG) {
856 if (is_next_clust(mydata, dentptr))
860 if ((dentptr->attr & ATTR_VOLUME)) {
861 #ifdef CONFIG_SUPPORT_VFAT
862 if ((dentptr->attr & ATTR_VFAT) &&
863 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
865 ((dir_slot *)dentptr)->alias_checksum;
866 get_long_file_name(mydata, curclust,
867 get_dentfromdir_block,
869 debug("vfatname: |%s|\n", l_name);
873 /* Volume label or VFAT entry */
875 if (is_next_clust(mydata, dentptr))
880 if (dentptr->name[0] == 0) {
881 debug("Dentname == NULL - %d\n", i);
882 empty_dentptr = dentptr;
886 get_name(dentptr, s_name);
888 if (strcmp(filename, s_name)
889 && strcmp(filename, l_name)) {
890 debug("Mismatch: |%s|%s|\n",
893 if (is_next_clust(mydata, dentptr))
898 memcpy(retdent, dentptr, sizeof(dir_entry));
900 debug("DentName: %s", s_name);
901 debug(", start: 0x%x", START(dentptr));
902 debug(", size: 0x%x %s\n",
903 FAT2CPU32(dentptr->size),
904 (dentptr->attr & ATTR_DIR) ?
910 curclust = get_fatent_value(mydata, dir_curclust);
911 if ((curclust >= 0xffffff8) || (curclust >= 0xfff8)) {
912 empty_dentptr = dentptr;
915 if (CHECK_CLUST(curclust, mydata->fatsize)) {
916 debug("curclust: 0x%x\n", curclust);
917 debug("Invalid FAT entry\n");
925 static int do_fat_write(const char *filename, void *buffer,
928 dir_entry *dentptr, *retdent;
935 fsdata *mydata = &datablock;
937 int root_cluster, 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 = part_size;
952 root_cluster = bs.root_cluster;
954 if (mydata->fatsize == 32)
955 mydata->fatlength = bs.fat32_length;
957 mydata->fatlength = bs.fat_length;
959 mydata->fat_sect = bs.reserved;
961 cursect = mydata->rootdir_sect
962 = mydata->fat_sect + mydata->fatlength * bs.fats;
963 num_of_fats = bs.fats;
965 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
966 mydata->clust_size = bs.cluster_size;
968 if (mydata->fatsize == 32) {
969 mydata->data_begin = mydata->rootdir_sect -
970 (mydata->clust_size * 2);
974 rootdir_size = ((bs.dir_entries[1] * (int)256 +
978 mydata->data_begin = mydata->rootdir_sect +
980 (mydata->clust_size * 2);
983 mydata->fatbufnum = -1;
984 mydata->fatbuf = malloc(FATBUFSIZE);
985 if (mydata->fatbuf == NULL) {
986 debug("Error: allocating memory\n");
990 if (disk_read(cursect,
991 (mydata->fatsize == 32) ?
992 (mydata->clust_size) :
993 PREFETCH_BLOCKS, do_fat_read_block) < 0) {
994 debug("Error: reading rootdir block\n");
997 dentptr = (dir_entry *) do_fat_read_block;
999 name_len = strlen(filename);
1000 if (name_len >= VFAT_MAXLEN_BYTES)
1001 name_len = VFAT_MAXLEN_BYTES - 1;
1003 memcpy(l_filename, filename, name_len);
1004 l_filename[name_len] = 0; /* terminate the string */
1005 downcase(l_filename);
1007 startsect = mydata->rootdir_sect;
1008 retdent = find_directory_entry(mydata, startsect,
1009 l_filename, dentptr, 0);
1011 /* Update file size and start_cluster in a directory entry */
1012 retdent->size = cpu_to_le32(size);
1013 start_cluster = FAT2CPU16(retdent->start);
1014 if (mydata->fatsize == 32)
1016 (FAT2CPU16(retdent->starthi) << 16);
1018 ret = check_overflow(mydata, start_cluster, size);
1020 printf("Error: %ld overflow\n", size);
1024 ret = clear_fatent(mydata, start_cluster);
1026 printf("Error: clearing FAT entries\n");
1030 ret = set_contents(mydata, retdent, buffer, size);
1032 printf("Error: writing contents\n");
1036 debug("attempt to write 0x%x bytes\n", write_size);
1038 /* Flush fat buffer */
1039 ret = flush_fat_buffer(mydata);
1041 printf("Error: flush fat buffer\n");
1045 /* Write directory table to device */
1046 ret = set_cluster(mydata, dir_curclust,
1047 get_dentfromdir_block,
1048 mydata->clust_size * mydata->sect_size);
1050 printf("Error: writing directory entry\n");
1054 slotptr = (dir_slot *)empty_dentptr;
1056 /* Set short name to set alias checksum field in dir_slot */
1057 set_name(empty_dentptr, filename);
1058 fill_dir_slot(mydata, &empty_dentptr, filename);
1060 ret = start_cluster = find_empty_cluster(mydata);
1062 printf("Error: finding empty cluster\n");
1066 ret = check_overflow(mydata, start_cluster, size);
1068 printf("Error: %ld overflow\n", size);
1072 /* Set attribute as archieve for regular file */
1073 fill_dentry(mydata, empty_dentptr, filename,
1074 start_cluster, size, 0x20);
1076 ret = set_contents(mydata, empty_dentptr, buffer, size);
1078 printf("Error: writing contents\n");
1082 debug("attempt to write 0x%x bytes\n", write_size);
1084 /* Flush fat buffer */
1085 ret = flush_fat_buffer(mydata);
1087 printf("Error: flush fat buffer\n");
1091 /* Write directory table to device */
1092 ret = set_cluster(mydata, dir_curclust,
1093 get_dentfromdir_block,
1094 mydata->clust_size * mydata->sect_size);
1096 printf("Error: writing directory entry\n");
1102 free(mydata->fatbuf);
1103 return ret < 0 ? ret : write_size;
1106 int file_fat_write(const char *filename, void *buffer, unsigned long maxsize)
1108 printf("writing %s\n", filename);
1109 return do_fat_write(filename, buffer, maxsize);