1 // SPDX-License-Identifier: GPL-2.0+
5 * R/W (V)FAT 12/16/32 filesystem implementation by Donggeun Kim
12 #include <asm/byteorder.h>
14 #include <linux/ctype.h>
16 #include <linux/math64.h>
19 static void uppercase(char *str, int len)
23 for (i = 0; i < len; i++) {
29 static int total_sector;
30 static int disk_write(__u32 block, __u32 nr_blocks, void *buf)
37 if (cur_part_info.start + block + nr_blocks >
38 cur_part_info.start + total_sector) {
39 printf("error: overflow occurs\n");
43 ret = blk_dwrite(cur_dev, cur_part_info.start + block, nr_blocks, buf);
44 if (nr_blocks && ret == 0)
51 * Set short name in directory entry
53 static void set_name(dir_entry *dirent, const char *filename)
55 char s_name[VFAT_MAXLEN_BYTES];
57 int period_location, len, i, ext_num;
62 len = strlen(filename);
66 strcpy(s_name, filename);
67 uppercase(s_name, len);
69 period = strchr(s_name, '.');
71 period_location = len;
74 period_location = period - s_name;
75 ext_num = len - period_location - 1;
78 /* Pad spaces when the length of file name is shorter than eight */
79 if (period_location < 8) {
80 memcpy(dirent->name, s_name, period_location);
81 for (i = period_location; i < 8; i++)
82 dirent->name[i] = ' ';
83 } else if (period_location == 8) {
84 memcpy(dirent->name, s_name, period_location);
86 memcpy(dirent->name, s_name, 6);
87 dirent->name[6] = '~';
88 dirent->name[7] = '1';
92 memcpy(dirent->ext, s_name + period_location + 1, ext_num);
93 for (i = ext_num; i < 3; i++)
96 memcpy(dirent->ext, s_name + period_location + 1, 3);
98 debug("name : %s\n", dirent->name);
99 debug("ext : %s\n", dirent->ext);
102 static __u8 num_of_fats;
104 * Write fat buffer into block device
106 static int flush_dirty_fat_buffer(fsdata *mydata)
108 int getsize = FATBUFBLOCKS;
109 __u32 fatlength = mydata->fatlength;
110 __u8 *bufptr = mydata->fatbuf;
111 __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
113 debug("debug: evicting %d, dirty: %d\n", mydata->fatbufnum,
114 (int)mydata->fat_dirty);
116 if ((!mydata->fat_dirty) || (mydata->fatbufnum == -1))
119 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
120 if (startblock + getsize > fatlength)
121 getsize = fatlength - startblock;
123 startblock += mydata->fat_sect;
126 if (disk_write(startblock, getsize, bufptr) < 0) {
127 debug("error: writing FAT blocks\n");
131 if (num_of_fats == 2) {
132 /* Update corresponding second FAT blocks */
133 startblock += mydata->fatlength;
134 if (disk_write(startblock, getsize, bufptr) < 0) {
135 debug("error: writing second FAT blocks\n");
139 mydata->fat_dirty = 0;
145 * Set the file name information from 'name' into 'slotptr',
147 static int str2slot(dir_slot *slotptr, const char *name, int *idx)
151 for (j = 0; j <= 8; j += 2) {
152 if (name[*idx] == 0x00) {
153 slotptr->name0_4[j] = 0;
154 slotptr->name0_4[j + 1] = 0;
158 slotptr->name0_4[j] = name[*idx];
162 for (j = 0; j <= 10; j += 2) {
163 if (name[*idx] == 0x00) {
164 slotptr->name5_10[j] = 0;
165 slotptr->name5_10[j + 1] = 0;
169 slotptr->name5_10[j] = name[*idx];
173 for (j = 0; j <= 2; j += 2) {
174 if (name[*idx] == 0x00) {
175 slotptr->name11_12[j] = 0;
176 slotptr->name11_12[j + 1] = 0;
180 slotptr->name11_12[j] = name[*idx];
185 if (name[*idx] == 0x00)
189 /* Not used characters are filled with 0xff 0xff */
191 for (; end_idx < 5; end_idx++) {
192 slotptr->name0_4[end_idx * 2] = 0xff;
193 slotptr->name0_4[end_idx * 2 + 1] = 0xff;
198 for (; end_idx < 6; end_idx++) {
199 slotptr->name5_10[end_idx * 2] = 0xff;
200 slotptr->name5_10[end_idx * 2 + 1] = 0xff;
205 for (; end_idx < 2; end_idx++) {
206 slotptr->name11_12[end_idx * 2] = 0xff;
207 slotptr->name11_12[end_idx * 2 + 1] = 0xff;
213 static int is_next_clust(fsdata *mydata, dir_entry *dentptr);
214 static void flush_dir_table(fsdata *mydata, dir_entry **dentptr);
217 * Fill dir_slot entries with appropriate name, id, and attr
218 * The real directory entry is returned by 'dentptr'
221 fill_dir_slot(fsdata *mydata, dir_entry **dentptr, const char *l_name)
223 __u8 temp_dir_slot_buffer[MAX_LFN_SLOT * sizeof(dir_slot)];
224 dir_slot *slotptr = (dir_slot *)temp_dir_slot_buffer;
225 __u8 counter = 0, checksum;
228 /* Get short file name checksum value */
229 checksum = mkcksum((*dentptr)->name, (*dentptr)->ext);
232 memset(slotptr, 0x00, sizeof(dir_slot));
233 ret = str2slot(slotptr, l_name, &idx);
234 slotptr->id = ++counter;
235 slotptr->attr = ATTR_VFAT;
236 slotptr->alias_checksum = checksum;
241 slotptr->id |= LAST_LONG_ENTRY_MASK;
243 while (counter >= 1) {
244 if (is_next_clust(mydata, *dentptr)) {
245 /* A new cluster is allocated for directory table */
246 flush_dir_table(mydata, dentptr);
248 memcpy(*dentptr, slotptr, sizeof(dir_slot));
254 if (is_next_clust(mydata, *dentptr)) {
255 /* A new cluster is allocated for directory table */
256 flush_dir_table(mydata, dentptr);
260 static __u32 dir_curclust;
263 * Extract the full long filename starting at 'retdent' (which is really
264 * a slot) into 'l_name'. If successful also copy the real directory entry
266 * If additional adjacent cluster for directory entries is read into memory,
267 * then 'get_contents_vfatname_block' is copied into 'get_dentfromdir_block' and
268 * the location of the real directory entry is returned by 'retdent'
269 * Return 0 on success, -1 otherwise.
272 get_long_file_name(fsdata *mydata, int curclust, __u8 *cluster,
273 dir_entry **retdent, char *l_name)
276 dir_slot *slotptr = (dir_slot *)(*retdent);
277 dir_slot *slotptr2 = NULL;
278 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
281 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
282 int idx = 0, cur_position = 0;
284 if (counter > VFAT_MAXSEQ) {
285 debug("Error: VFAT name is too long\n");
289 while ((__u8 *)slotptr < buflimit) {
292 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
298 if ((__u8 *)slotptr >= buflimit) {
301 curclust = get_fatent(mydata, dir_curclust);
302 if (CHECK_CLUST(curclust, mydata->fatsize)) {
303 debug("curclust: 0x%x\n", curclust);
304 printf("Invalid FAT entry\n");
308 dir_curclust = curclust;
310 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
311 mydata->clust_size * mydata->sect_size) != 0) {
312 debug("Error: reading directory block\n");
316 slotptr2 = (dir_slot *)get_contents_vfatname_block;
317 while (counter > 0) {
318 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
325 /* Save the real directory entry */
326 realdent = (dir_entry *)slotptr2;
327 while ((__u8 *)slotptr2 > get_contents_vfatname_block) {
329 slot2str(slotptr2, l_name, &idx);
332 /* Save the real directory entry */
333 realdent = (dir_entry *)slotptr;
338 if (slot2str(slotptr, l_name, &idx))
340 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
343 if (*l_name == DELETED_FLAG)
345 else if (*l_name == aRING)
346 *l_name = DELETED_FLAG;
347 downcase(l_name, INT_MAX);
349 /* Return the real directory entry */
353 memcpy(get_dentfromdir_block, get_contents_vfatname_block,
354 mydata->clust_size * mydata->sect_size);
355 cur_position = (__u8 *)realdent - get_contents_vfatname_block;
356 *retdent = (dir_entry *) &get_dentfromdir_block[cur_position];
363 * Set the entry at index 'entry' in a FAT (12/16/32) table.
365 static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
367 __u32 bufnum, offset, off16;
370 switch (mydata->fatsize) {
372 bufnum = entry / FAT32BUFSIZE;
373 offset = entry - bufnum * FAT32BUFSIZE;
376 bufnum = entry / FAT16BUFSIZE;
377 offset = entry - bufnum * FAT16BUFSIZE;
380 bufnum = entry / FAT12BUFSIZE;
381 offset = entry - bufnum * FAT12BUFSIZE;
384 /* Unsupported FAT size */
388 /* Read a new block of FAT entries into the cache. */
389 if (bufnum != mydata->fatbufnum) {
390 int getsize = FATBUFBLOCKS;
391 __u8 *bufptr = mydata->fatbuf;
392 __u32 fatlength = mydata->fatlength;
393 __u32 startblock = bufnum * FATBUFBLOCKS;
395 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
396 if (startblock + getsize > fatlength)
397 getsize = fatlength - startblock;
399 if (flush_dirty_fat_buffer(mydata) < 0)
402 startblock += mydata->fat_sect;
404 if (disk_read(startblock, getsize, bufptr) < 0) {
405 debug("Error reading FAT blocks\n");
408 mydata->fatbufnum = bufnum;
412 mydata->fat_dirty = 1;
414 /* Set the actual entry */
415 switch (mydata->fatsize) {
417 ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
420 ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
423 off16 = (offset * 3) / 4;
425 switch (offset & 0x3) {
427 val1 = cpu_to_le16(entry_value) & 0xfff;
428 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff;
429 ((__u16 *)mydata->fatbuf)[off16] |= val1;
432 val1 = cpu_to_le16(entry_value) & 0xf;
433 val2 = (cpu_to_le16(entry_value) >> 4) & 0xff;
435 ((__u16 *)mydata->fatbuf)[off16] &= ~0xf000;
436 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 12);
438 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xff;
439 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
442 val1 = cpu_to_le16(entry_value) & 0xff;
443 val2 = (cpu_to_le16(entry_value) >> 8) & 0xf;
445 ((__u16 *)mydata->fatbuf)[off16] &= ~0xff00;
446 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 8);
448 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xf;
449 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
452 val1 = cpu_to_le16(entry_value) & 0xfff;
453 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff0;
454 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 4);
469 * Determine the next free cluster after 'entry' in a FAT (12/16/32) table
470 * and link it to 'entry'. EOC marker is not set on returned entry.
472 static __u32 determine_fatent(fsdata *mydata, __u32 entry)
474 __u32 next_fat, next_entry = entry + 1;
477 next_fat = get_fatent(mydata, next_entry);
479 /* found free entry, link to entry */
480 set_fatent_value(mydata, entry, next_entry);
485 debug("FAT%d: entry: %08x, entry_value: %04x\n",
486 mydata->fatsize, entry, next_entry);
492 * Write at most 'size' bytes from 'buffer' into the specified cluster.
493 * Return 0 on success, -1 otherwise.
496 set_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer,
504 startsect = clust_to_sect(mydata, clustnum);
506 startsect = mydata->rootdir_sect;
508 debug("clustnum: %d, startsect: %d\n", clustnum, startsect);
510 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
511 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
513 printf("FAT: Misaligned buffer address (%p)\n", buffer);
515 while (size >= mydata->sect_size) {
516 memcpy(tmpbuf, buffer, mydata->sect_size);
517 ret = disk_write(startsect++, 1, tmpbuf);
519 debug("Error writing data (got %d)\n", ret);
523 buffer += mydata->sect_size;
524 size -= mydata->sect_size;
526 } else if (size >= mydata->sect_size) {
527 idx = size / mydata->sect_size;
528 ret = disk_write(startsect, idx, buffer);
530 debug("Error writing data (got %d)\n", ret);
535 idx *= mydata->sect_size;
541 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
543 memcpy(tmpbuf, buffer, size);
544 ret = disk_write(startsect, 1, tmpbuf);
546 debug("Error writing data (got %d)\n", ret);
555 * Find the first empty cluster
557 static int find_empty_cluster(fsdata *mydata)
559 __u32 fat_val, entry = 3;
562 fat_val = get_fatent(mydata, entry);
572 * Write directory entries in 'get_dentfromdir_block' to block device
574 static void flush_dir_table(fsdata *mydata, dir_entry **dentptr)
576 int dir_newclust = 0;
578 if (set_cluster(mydata, dir_curclust,
579 get_dentfromdir_block,
580 mydata->clust_size * mydata->sect_size) != 0) {
581 printf("error: wrinting directory entry\n");
584 dir_newclust = find_empty_cluster(mydata);
585 set_fatent_value(mydata, dir_curclust, dir_newclust);
586 if (mydata->fatsize == 32)
587 set_fatent_value(mydata, dir_newclust, 0xffffff8);
588 else if (mydata->fatsize == 16)
589 set_fatent_value(mydata, dir_newclust, 0xfff8);
590 else if (mydata->fatsize == 12)
591 set_fatent_value(mydata, dir_newclust, 0xff8);
593 dir_curclust = dir_newclust;
595 if (flush_dirty_fat_buffer(mydata) < 0)
598 memset(get_dentfromdir_block, 0x00,
599 mydata->clust_size * mydata->sect_size);
601 *dentptr = (dir_entry *) get_dentfromdir_block;
605 * Set empty cluster from 'entry' to the end of a file
607 static int clear_fatent(fsdata *mydata, __u32 entry)
611 while (!CHECK_CLUST(entry, mydata->fatsize)) {
612 fat_val = get_fatent(mydata, entry);
614 set_fatent_value(mydata, entry, 0);
621 /* Flush fat buffer */
622 if (flush_dirty_fat_buffer(mydata) < 0)
629 * Write at most 'maxsize' bytes from 'buffer' into
630 * the file associated with 'dentptr'
631 * Update the number of bytes written in *gotsize and return 0
632 * or return -1 on fatal errors.
635 set_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
636 loff_t maxsize, loff_t *gotsize)
638 loff_t filesize = FAT2CPU32(dentptr->size);
639 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
640 __u32 curclust = START(dentptr);
641 __u32 endclust = 0, newclust = 0;
645 debug("Filesize: %llu bytes\n", filesize);
647 if (maxsize > 0 && filesize > maxsize)
650 debug("%llu bytes\n", filesize);
654 debug("error: nonempty clusterless file!\n");
660 actsize = bytesperclust;
663 /* search for consecutive clusters */
664 while (actsize < filesize) {
665 newclust = determine_fatent(mydata, endclust);
667 if ((newclust - 1) != endclust)
670 if (CHECK_CLUST(newclust, mydata->fatsize)) {
671 debug("newclust: 0x%x\n", newclust);
672 debug("Invalid FAT entry\n");
676 actsize += bytesperclust;
679 /* set remaining bytes */
681 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
682 debug("error: writing cluster\n");
687 /* Mark end of file in FAT */
688 if (mydata->fatsize == 12)
690 else if (mydata->fatsize == 16)
692 else if (mydata->fatsize == 32)
693 newclust = 0xfffffff;
694 set_fatent_value(mydata, endclust, newclust);
698 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
699 debug("error: writing cluster\n");
706 if (CHECK_CLUST(newclust, mydata->fatsize)) {
707 debug("newclust: 0x%x\n", newclust);
708 debug("Invalid FAT entry\n");
711 actsize = bytesperclust;
712 curclust = endclust = newclust;
717 * Set start cluster in directory entry
719 static void set_start_cluster(const fsdata *mydata, dir_entry *dentptr,
722 if (mydata->fatsize == 32)
724 cpu_to_le16((start_cluster & 0xffff0000) >> 16);
725 dentptr->start = cpu_to_le16(start_cluster & 0xffff);
731 static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
732 const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
734 set_start_cluster(mydata, dentptr, start_cluster);
735 dentptr->size = cpu_to_le32(size);
737 dentptr->attr = attr;
739 set_name(dentptr, filename);
743 * Check whether adding a file makes the file system to
744 * exceed the size of the block device
745 * Return -1 when overflow occurs, otherwise return 0
747 static int check_overflow(fsdata *mydata, __u32 clustnum, loff_t size)
749 __u32 startsect, sect_num, offset;
752 startsect = clust_to_sect(mydata, clustnum);
754 startsect = mydata->rootdir_sect;
757 sect_num = div_u64_rem(size, mydata->sect_size, &offset);
762 if (startsect + sect_num > total_sector)
768 * Check if adding several entries exceed one cluster boundary
770 static int is_next_clust(fsdata *mydata, dir_entry *dentptr)
774 cur_position = (__u8 *)dentptr - get_dentfromdir_block;
776 if (cur_position >= mydata->clust_size * mydata->sect_size)
782 static dir_entry *empty_dentptr;
784 * Find a directory entry based on filename or start cluster number
785 * If the directory entry is not found,
786 * the new position for writing a directory entry will be returned
788 static dir_entry *find_directory_entry(fsdata *mydata, int startsect,
789 char *filename, dir_entry *retdent, __u32 start)
791 __u32 curclust = sect_to_clust(mydata, startsect);
793 debug("get_dentfromdir: %s\n", filename);
800 if (get_cluster(mydata, curclust, get_dentfromdir_block,
801 mydata->clust_size * mydata->sect_size) != 0) {
802 printf("Error: reading directory block\n");
806 dentptr = (dir_entry *)get_dentfromdir_block;
808 dir_curclust = curclust;
810 for (i = 0; i < DIRENTSPERCLUST; i++) {
811 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
814 if (dentptr->name[0] == DELETED_FLAG) {
816 if (is_next_clust(mydata, dentptr))
820 if ((dentptr->attr & ATTR_VOLUME)) {
821 if ((dentptr->attr & ATTR_VFAT) &&
822 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
823 get_long_file_name(mydata, curclust,
824 get_dentfromdir_block,
826 debug("vfatname: |%s|\n", l_name);
828 /* Volume label or VFAT entry */
830 if (is_next_clust(mydata, dentptr))
835 if (dentptr->name[0] == 0) {
836 debug("Dentname == NULL - %d\n", i);
837 empty_dentptr = dentptr;
841 get_name(dentptr, s_name);
843 if (strncasecmp(filename, s_name, sizeof(s_name)) &&
844 strncasecmp(filename, l_name, sizeof(l_name))) {
845 debug("Mismatch: |%s|%s|\n",
848 if (is_next_clust(mydata, dentptr))
853 memcpy(retdent, dentptr, sizeof(dir_entry));
855 debug("DentName: %s", s_name);
856 debug(", start: 0x%x", START(dentptr));
857 debug(", size: 0x%x %s\n",
858 FAT2CPU32(dentptr->size),
859 (dentptr->attr & ATTR_DIR) ?
866 * In FAT16/12, the root dir is locate before data area, shows
868 * -------------------------------------------------------------
869 * | Boot | FAT1 & 2 | Root dir | Data (start from cluster #2) |
870 * -------------------------------------------------------------
872 * As a result if curclust is in Root dir, it is a negative
876 if (mydata->fatsize != 32 && (int)curclust <= 1) {
877 /* Current clust is in root dir, set to next clust */
879 if ((int)curclust <= 1)
880 continue; /* continue to find */
882 /* Reach the end of root dir */
883 empty_dentptr = dentptr;
887 curclust = get_fatent(mydata, dir_curclust);
888 if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
889 empty_dentptr = dentptr;
892 if (CHECK_CLUST(curclust, mydata->fatsize)) {
893 debug("curclust: 0x%x\n", curclust);
894 debug("Invalid FAT entry\n");
902 static int do_fat_write(const char *filename, void *buffer, loff_t size,
905 dir_entry *dentptr, *retdent;
911 fsdata *mydata = &datablock;
913 int ret = -1, name_len;
914 char l_filename[VFAT_MAXLEN_BYTES];
916 const char illegal[] = "<>:\"/\\|?*";
921 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
922 debug("error: reading boot sector\n");
926 total_sector = bs.total_sect;
927 if (total_sector == 0)
928 total_sector = (int)cur_part_info.size; /* cast of lbaint_t */
930 if (mydata->fatsize == 32)
931 mydata->fatlength = bs.fat32_length;
933 mydata->fatlength = bs.fat_length;
935 mydata->fat_sect = bs.reserved;
937 cursect = mydata->rootdir_sect
938 = mydata->fat_sect + mydata->fatlength * bs.fats;
939 num_of_fats = bs.fats;
941 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
942 mydata->clust_size = bs.cluster_size;
944 if (mydata->fatsize == 32) {
945 mydata->data_begin = mydata->rootdir_sect -
946 (mydata->clust_size * 2);
950 rootdir_size = ((bs.dir_entries[1] * (int)256 +
954 mydata->data_begin = mydata->rootdir_sect +
956 (mydata->clust_size * 2);
959 mydata->fatbufnum = -1;
960 mydata->fat_dirty = 0;
961 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
962 if (mydata->fatbuf == NULL) {
963 debug("Error: allocating memory\n");
967 if (disk_read(cursect,
968 (mydata->fatsize == 32) ?
969 (mydata->clust_size) :
970 PREFETCH_BLOCKS, do_fat_read_at_block) < 0) {
971 debug("Error: reading rootdir block\n");
974 dentptr = (dir_entry *) do_fat_read_at_block;
976 /* Strip leading (back-)slashes */
977 while ISDIRDELIM(*filename)
979 /* Check that the filename is valid */
980 for (i = 0; i < strlen(illegal); ++i) {
982 if (strstr(filename, bad)) {
983 printf("FAT: illegal filename (%s)\n", filename);
988 name_len = strlen(filename);
989 if (name_len >= VFAT_MAXLEN_BYTES)
990 name_len = VFAT_MAXLEN_BYTES - 1;
992 memcpy(l_filename, filename, name_len);
993 l_filename[name_len] = 0; /* terminate the string */
994 downcase(l_filename, INT_MAX);
996 startsect = mydata->rootdir_sect;
997 retdent = find_directory_entry(mydata, startsect,
998 l_filename, dentptr, 0);
1000 /* Update file size and start_cluster in a directory entry */
1001 retdent->size = cpu_to_le32(size);
1002 start_cluster = START(retdent);
1004 if (start_cluster) {
1006 ret = check_overflow(mydata, start_cluster,
1009 printf("Error: %llu overflow\n", size);
1014 ret = clear_fatent(mydata, start_cluster);
1016 printf("Error: clearing FAT entries\n");
1021 set_start_cluster(mydata, retdent, 0);
1023 ret = start_cluster = find_empty_cluster(mydata);
1025 printf("Error: finding empty cluster\n");
1029 ret = check_overflow(mydata, start_cluster, size);
1031 printf("Error: %llu overflow\n", size);
1035 set_start_cluster(mydata, retdent, start_cluster);
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);
1043 ret = start_cluster = find_empty_cluster(mydata);
1045 printf("Error: finding empty cluster\n");
1049 ret = check_overflow(mydata, start_cluster, size);
1051 printf("Error: %llu overflow\n", size);
1058 /* Set attribute as archieve for regular file */
1059 fill_dentry(mydata, empty_dentptr, filename,
1060 start_cluster, size, 0x20);
1062 retdent = empty_dentptr;
1065 ret = set_contents(mydata, retdent, buffer, size, actwrite);
1067 printf("Error: writing contents\n");
1070 debug("attempt to write 0x%llx bytes\n", *actwrite);
1072 /* Flush fat buffer */
1073 ret = flush_dirty_fat_buffer(mydata);
1075 printf("Error: flush fat buffer\n");
1079 /* Write directory table to device */
1080 ret = set_cluster(mydata, dir_curclust, get_dentfromdir_block,
1081 mydata->clust_size * mydata->sect_size);
1083 printf("Error: writing directory entry\n");
1086 free(mydata->fatbuf);
1090 int file_fat_write(const char *filename, void *buffer, loff_t offset,
1091 loff_t maxsize, loff_t *actwrite)
1094 printf("Error: non zero offset is currently not supported.\n");
1098 printf("writing %s\n", filename);
1099 return do_fat_write(filename, buffer, maxsize, actwrite);