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);
116 * Write fat buffer into block device
118 static int flush_fat_buffer(fsdata *mydata)
120 int getsize = FATBUFBLOCKS;
121 __u32 fatlength = mydata->fatlength;
122 __u8 *bufptr = mydata->fatbuf;
123 __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
125 fatlength *= mydata->sect_size;
126 startblock += mydata->fat_sect;
128 if (getsize > fatlength)
132 if (disk_write(startblock, getsize, bufptr) < 0) {
133 debug("error: writing FAT blocks\n");
141 * Get the entry at index 'entry' in a FAT (12/16/32) table.
142 * On failure 0x00 is returned.
143 * When bufnum is changed, write back the previous fatbuf to the disk.
145 static __u32 get_fatent_value(fsdata *mydata, __u32 entry)
152 switch (mydata->fatsize) {
154 bufnum = entry / FAT32BUFSIZE;
155 offset = entry - bufnum * FAT32BUFSIZE;
158 bufnum = entry / FAT16BUFSIZE;
159 offset = entry - bufnum * FAT16BUFSIZE;
162 bufnum = entry / FAT12BUFSIZE;
163 offset = entry - bufnum * FAT12BUFSIZE;
167 /* Unsupported FAT size */
171 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
172 mydata->fatsize, entry, entry, offset, offset);
174 /* Read a new block of FAT entries into the cache. */
175 if (bufnum != mydata->fatbufnum) {
176 int getsize = FATBUFBLOCKS;
177 __u8 *bufptr = mydata->fatbuf;
178 __u32 fatlength = mydata->fatlength;
179 __u32 startblock = bufnum * FATBUFBLOCKS;
181 if (getsize > fatlength)
184 fatlength *= mydata->sect_size; /* We want it in bytes now */
185 startblock += mydata->fat_sect; /* Offset from start of disk */
187 /* Write back the fatbuf to the disk */
188 if (mydata->fatbufnum != -1) {
189 if (flush_fat_buffer(mydata) < 0)
193 if (disk_read(startblock, getsize, bufptr) < 0) {
194 debug("Error reading FAT blocks\n");
197 mydata->fatbufnum = bufnum;
200 /* Get the actual entry from the table */
201 switch (mydata->fatsize) {
203 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
206 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
209 off16 = (offset * 3) / 4;
211 switch (offset & 0x3) {
213 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
217 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
219 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
221 ret = (val2 << 4) | (val1 >> 12);
224 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
226 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
228 ret = (val2 << 8) | (val1 >> 8);
231 ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
232 ret = (ret & 0xfff0) >> 4;
239 debug("FAT%d: ret: %08x, entry: %08x, offset: %04x\n",
240 mydata->fatsize, ret, entry, offset);
245 #ifdef CONFIG_SUPPORT_VFAT
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_vfatname_block;
326 __u8 counter, checksum;
330 /* Get short file name and checksum value */
331 strncpy(s_name, (*dentptr)->name, 16);
332 checksum = mkcksum(s_name);
335 memset(slotptr, 0x00, sizeof(dir_slot));
336 ret = str2slot(slotptr, l_name, &idx);
337 slotptr->id = ++counter;
338 slotptr->attr = ATTR_VFAT;
339 slotptr->alias_checksum = checksum;
344 slotptr->id |= LAST_LONG_ENTRY_MASK;
346 while (counter >= 1) {
347 if (is_next_clust(mydata, *dentptr)) {
348 /* A new cluster is allocated for directory table */
349 flush_dir_table(mydata, dentptr);
351 memcpy(*dentptr, slotptr, sizeof(dir_slot));
357 if (is_next_clust(mydata, *dentptr)) {
358 /* A new cluster is allocated for directory table */
359 flush_dir_table(mydata, dentptr);
363 static __u32 dir_curclust;
366 * Extract the full long filename starting at 'retdent' (which is really
367 * a slot) into 'l_name'. If successful also copy the real directory entry
369 * If additional adjacent cluster for directory entries is read into memory,
370 * then 'get_vfatname_block' is copied into 'get_dentfromdir_block' and
371 * the location of the real directory entry is returned by 'retdent'
372 * Return 0 on success, -1 otherwise.
375 get_long_file_name(fsdata *mydata, int curclust, __u8 *cluster,
376 dir_entry **retdent, char *l_name)
379 dir_slot *slotptr = (dir_slot *)(*retdent);
380 dir_slot *slotptr2 = NULL;
381 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
384 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
385 int idx = 0, cur_position = 0;
387 if (counter > VFAT_MAXSEQ) {
388 debug("Error: VFAT name is too long\n");
392 while ((__u8 *)slotptr < buflimit) {
395 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
401 if ((__u8 *)slotptr >= buflimit) {
404 curclust = get_fatent_value(mydata, dir_curclust);
405 if (CHECK_CLUST(curclust, mydata->fatsize)) {
406 debug("curclust: 0x%x\n", curclust);
407 printf("Invalid FAT entry\n");
411 dir_curclust = curclust;
413 if (get_cluster(mydata, curclust, get_vfatname_block,
414 mydata->clust_size * mydata->sect_size) != 0) {
415 debug("Error: reading directory block\n");
419 slotptr2 = (dir_slot *)get_vfatname_block;
420 while (counter > 0) {
421 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
428 /* Save the real directory entry */
429 realdent = (dir_entry *)slotptr2;
430 while ((__u8 *)slotptr2 > get_vfatname_block) {
432 slot2str(slotptr2, l_name, &idx);
435 /* Save the real directory entry */
436 realdent = (dir_entry *)slotptr;
441 if (slot2str(slotptr, l_name, &idx))
443 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
446 if (*l_name == DELETED_FLAG)
448 else if (*l_name == aRING)
449 *l_name = DELETED_FLAG;
452 /* Return the real directory entry */
456 memcpy(get_dentfromdir_block, get_vfatname_block,
457 mydata->clust_size * mydata->sect_size);
458 cur_position = (__u8 *)realdent - get_vfatname_block;
459 *retdent = (dir_entry *) &get_dentfromdir_block[cur_position];
468 * Set the entry at index 'entry' in a FAT (16/32) table.
470 static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
472 __u32 bufnum, offset;
474 switch (mydata->fatsize) {
476 bufnum = entry / FAT32BUFSIZE;
477 offset = entry - bufnum * FAT32BUFSIZE;
480 bufnum = entry / FAT16BUFSIZE;
481 offset = entry - bufnum * FAT16BUFSIZE;
484 /* Unsupported FAT size */
488 /* Read a new block of FAT entries into the cache. */
489 if (bufnum != mydata->fatbufnum) {
490 int getsize = FATBUFBLOCKS;
491 __u8 *bufptr = mydata->fatbuf;
492 __u32 fatlength = mydata->fatlength;
493 __u32 startblock = bufnum * FATBUFBLOCKS;
495 fatlength *= mydata->sect_size;
496 startblock += mydata->fat_sect;
498 if (getsize > fatlength)
501 if (mydata->fatbufnum != -1) {
502 if (flush_fat_buffer(mydata) < 0)
506 if (disk_read(startblock, getsize, bufptr) < 0) {
507 debug("Error reading FAT blocks\n");
510 mydata->fatbufnum = bufnum;
513 /* Set the actual entry */
514 switch (mydata->fatsize) {
516 ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
519 ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
529 * Determine the entry value at index 'entry' in a FAT (16/32) table
531 static __u32 determine_fatent(fsdata *mydata, __u32 entry)
533 __u32 next_fat, next_entry = entry + 1;
536 next_fat = get_fatent_value(mydata, next_entry);
538 set_fatent_value(mydata, entry, next_entry);
543 debug("FAT%d: entry: %08x, entry_value: %04x\n",
544 mydata->fatsize, entry, next_entry);
550 * Write at most 'size' bytes from 'buffer' into the specified cluster.
551 * Return 0 on success, -1 otherwise.
554 set_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer,
561 startsect = mydata->data_begin +
562 clustnum * mydata->clust_size;
564 startsect = mydata->rootdir_sect;
566 debug("clustnum: %d, startsect: %d\n", clustnum, startsect);
568 if (disk_write(startsect, size / mydata->sect_size, buffer) < 0) {
569 debug("Error writing data\n");
573 if (size % mydata->sect_size) {
574 __u8 tmpbuf[mydata->sect_size];
576 idx = size / mydata->sect_size;
577 buffer += idx * mydata->sect_size;
578 memcpy(tmpbuf, buffer, size % mydata->sect_size);
580 if (disk_write(startsect + idx, 1, tmpbuf) < 0) {
581 debug("Error writing data\n");
592 * Find the first empty cluster
594 static int find_empty_cluster(fsdata *mydata)
596 __u32 fat_val, entry = 3;
599 fat_val = get_fatent_value(mydata, entry);
609 * Write directory entries in 'get_dentfromdir_block' to block device
611 static void flush_dir_table(fsdata *mydata, dir_entry **dentptr)
613 int dir_newclust = 0;
615 if (set_cluster(mydata, dir_curclust,
616 get_dentfromdir_block,
617 mydata->clust_size * mydata->sect_size) != 0) {
618 printf("error: wrinting directory entry\n");
621 dir_newclust = find_empty_cluster(mydata);
622 set_fatent_value(mydata, dir_curclust, dir_newclust);
623 if (mydata->fatsize == 32)
624 set_fatent_value(mydata, dir_newclust, 0xffffff8);
625 else if (mydata->fatsize == 16)
626 set_fatent_value(mydata, dir_newclust, 0xfff8);
628 dir_curclust = dir_newclust;
630 if (flush_fat_buffer(mydata) < 0)
633 memset(get_dentfromdir_block, 0x00,
634 mydata->clust_size * mydata->sect_size);
636 *dentptr = (dir_entry *) get_dentfromdir_block;
640 * Set empty cluster from 'entry' to the end of a file
642 static int clear_fatent(fsdata *mydata, __u32 entry)
647 fat_val = get_fatent_value(mydata, entry);
649 set_fatent_value(mydata, entry, 0);
653 if (fat_val == 0xfffffff || fat_val == 0xffff)
659 /* Flush fat buffer */
660 if (flush_fat_buffer(mydata) < 0)
667 * Write at most 'maxsize' bytes from 'buffer' into
668 * the file associated with 'dentptr'
669 * Return the number of bytes read or -1 on fatal errors.
672 set_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
673 unsigned long maxsize)
675 unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
676 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
677 __u32 curclust = START(dentptr);
678 __u32 endclust = 0, newclust = 0;
679 unsigned long actsize;
681 debug("Filesize: %ld bytes\n", filesize);
683 if (maxsize > 0 && filesize > maxsize)
686 debug("%ld bytes\n", filesize);
688 actsize = bytesperclust;
691 /* search for consecutive clusters */
692 while (actsize < filesize) {
693 newclust = determine_fatent(mydata, endclust);
695 if ((newclust - 1) != endclust)
698 if (CHECK_CLUST(newclust, mydata->fatsize)) {
699 debug("curclust: 0x%x\n", newclust);
700 debug("Invalid FAT entry\n");
704 actsize += bytesperclust;
706 /* actsize >= file size */
707 actsize -= bytesperclust;
708 /* set remaining clusters */
709 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
710 debug("error: writing cluster\n");
714 /* set remaining bytes */
715 gotsize += (int)actsize;
720 if (set_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
721 debug("error: writing cluster\n");
726 /* Mark end of file in FAT */
727 if (mydata->fatsize == 16)
729 else if (mydata->fatsize == 32)
730 newclust = 0xfffffff;
731 set_fatent_value(mydata, endclust, newclust);
735 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
736 debug("error: writing cluster\n");
739 gotsize += (int)actsize;
743 if (CHECK_CLUST(curclust, mydata->fatsize)) {
744 debug("curclust: 0x%x\n", curclust);
745 debug("Invalid FAT entry\n");
748 actsize = bytesperclust;
749 curclust = endclust = newclust;
756 static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
757 const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
759 if (mydata->fatsize == 32)
761 cpu_to_le16((start_cluster & 0xffff0000) >> 16);
762 dentptr->start = cpu_to_le16(start_cluster & 0xffff);
763 dentptr->size = cpu_to_le32(size);
765 dentptr->attr = attr;
767 set_name(dentptr, filename);
771 * Check whether adding a file makes the file system to
772 * exceed the size of the block device
773 * Return -1 when overflow occurs, otherwise return 0
775 static int check_overflow(fsdata *mydata, __u32 clustnum, unsigned long size)
777 __u32 startsect, sect_num;
780 startsect = mydata->data_begin +
781 clustnum * mydata->clust_size;
783 startsect = mydata->rootdir_sect;
786 sect_num = size / mydata->sect_size;
787 if (size % mydata->sect_size)
790 if (startsect + sect_num > total_sector)
797 * Check if adding several entries exceed one cluster boundary
799 static int is_next_clust(fsdata *mydata, dir_entry *dentptr)
803 cur_position = (__u8 *)dentptr - get_dentfromdir_block;
805 if (cur_position >= mydata->clust_size * mydata->sect_size)
811 static dir_entry *empty_dentptr;
813 * Find a directory entry based on filename or start cluster number
814 * If the directory entry is not found,
815 * the new position for writing a directory entry will be returned
817 static dir_entry *find_directory_entry(fsdata *mydata, int startsect,
818 char *filename, dir_entry *retdent, __u32 start)
820 __u16 prevcksum = 0xffff;
821 __u32 curclust = (startsect - mydata->data_begin) / mydata->clust_size;
823 debug("get_dentfromdir: %s\n", filename);
830 if (get_cluster(mydata, curclust, get_dentfromdir_block,
831 mydata->clust_size * mydata->sect_size) != 0) {
832 printf("Error: reading directory block\n");
836 dentptr = (dir_entry *)get_dentfromdir_block;
838 dir_curclust = curclust;
840 for (i = 0; i < DIRENTSPERCLUST; i++) {
841 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
844 if (dentptr->name[0] == DELETED_FLAG) {
846 if (is_next_clust(mydata, dentptr))
850 if ((dentptr->attr & ATTR_VOLUME)) {
851 #ifdef CONFIG_SUPPORT_VFAT
852 if ((dentptr->attr & ATTR_VFAT) &&
853 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
855 ((dir_slot *)dentptr)->alias_checksum;
856 get_long_file_name(mydata, curclust,
857 get_dentfromdir_block,
859 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) ?
900 curclust = get_fatent_value(mydata, dir_curclust);
901 if ((curclust >= 0xffffff8) || (curclust >= 0xfff8)) {
902 empty_dentptr = dentptr;
905 if (CHECK_CLUST(curclust, mydata->fatsize)) {
906 debug("curclust: 0x%x\n", curclust);
907 debug("Invalid FAT entry\n");
915 static int do_fat_write(const char *filename, void *buffer,
918 dir_entry *dentptr, *retdent;
925 fsdata *mydata = &datablock;
927 int root_cluster, ret = -1, name_len;
928 char l_filename[VFAT_MAXLEN_BYTES];
932 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
933 debug("error: reading boot sector\n");
937 total_sector = bs.total_sect;
938 if (total_sector == 0)
939 total_sector = part_size;
941 root_cluster = bs.root_cluster;
943 if (mydata->fatsize == 32)
944 mydata->fatlength = bs.fat32_length;
946 mydata->fatlength = bs.fat_length;
948 mydata->fat_sect = bs.reserved;
950 cursect = mydata->rootdir_sect
951 = mydata->fat_sect + mydata->fatlength * bs.fats;
953 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
954 mydata->clust_size = bs.cluster_size;
956 if (mydata->fatsize == 32) {
957 mydata->data_begin = mydata->rootdir_sect -
958 (mydata->clust_size * 2);
962 rootdir_size = ((bs.dir_entries[1] * (int)256 +
966 mydata->data_begin = mydata->rootdir_sect +
968 (mydata->clust_size * 2);
971 mydata->fatbufnum = -1;
972 mydata->fatbuf = malloc(FATBUFSIZE);
973 if (mydata->fatbuf == NULL) {
974 debug("Error: allocating memory\n");
978 if (disk_read(cursect,
979 (mydata->fatsize == 32) ?
980 (mydata->clust_size) :
981 PREFETCH_BLOCKS, do_fat_read_block) < 0) {
982 debug("Error: reading rootdir block\n");
985 dentptr = (dir_entry *) do_fat_read_block;
987 name_len = strlen(filename);
988 memcpy(l_filename, filename, name_len);
989 downcase(l_filename);
991 startsect = mydata->rootdir_sect;
992 retdent = find_directory_entry(mydata, startsect,
993 l_filename, dentptr, 0);
995 /* Update file size and start_cluster in a directory entry */
996 retdent->size = cpu_to_le32(size);
997 start_cluster = FAT2CPU16(retdent->start);
998 if (mydata->fatsize == 32)
1000 (FAT2CPU16(retdent->starthi) << 16);
1002 ret = check_overflow(mydata, start_cluster, size);
1004 printf("Error: %ld overflow\n", size);
1008 ret = clear_fatent(mydata, start_cluster);
1010 printf("Error: clearing FAT entries\n");
1014 ret = set_contents(mydata, retdent, buffer, size);
1016 printf("Error: writing contents\n");
1020 /* Flush fat buffer */
1021 ret = flush_fat_buffer(mydata);
1023 printf("Error: flush fat buffer\n");
1027 /* Write directory table to device */
1028 ret = set_cluster(mydata, dir_curclust,
1029 get_dentfromdir_block,
1030 mydata->clust_size * mydata->sect_size);
1032 printf("Error: wrinting directory entry\n");
1036 slotptr = (dir_slot *)empty_dentptr;
1038 /* Set short name to set alias checksum field in dir_slot */
1039 set_name(empty_dentptr, filename);
1040 fill_dir_slot(mydata, &empty_dentptr, filename);
1042 ret = start_cluster = find_empty_cluster(mydata);
1044 printf("Error: finding empty cluster\n");
1048 ret = check_overflow(mydata, start_cluster, size);
1050 printf("Error: %ld overflow\n", size);
1054 /* Set attribute as archieve for regular file */
1055 fill_dentry(mydata, empty_dentptr, filename,
1056 start_cluster, size, 0x20);
1058 ret = set_contents(mydata, empty_dentptr, buffer, size);
1060 printf("Error: writing contents\n");
1064 /* Flush fat buffer */
1065 ret = flush_fat_buffer(mydata);
1067 printf("Error: flush fat buffer\n");
1071 /* Write directory table to device */
1072 ret = set_cluster(mydata, dir_curclust,
1073 get_dentfromdir_block,
1074 mydata->clust_size * mydata->sect_size);
1076 printf("Error: writing directory entry\n");
1082 free(mydata->fatbuf);
1086 int file_fat_write(const char *filename, void *buffer, unsigned long maxsize)
1088 printf("writing %s\n", filename);
1089 return do_fat_write(filename, buffer, maxsize);