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)
38 if (cur_part_info.start + block + nr_blocks >
39 cur_part_info.start + total_sector) {
40 printf("error: overflow occurs\n");
44 ret = blk_dwrite(cur_dev, cur_part_info.start + block, nr_blocks, buf);
45 if (nr_blocks && ret == 0)
52 * Set short name in directory entry
54 static void set_name(dir_entry *dirent, const char *filename)
56 char s_name[VFAT_MAXLEN_BYTES];
58 int period_location, len, i, ext_num;
63 len = strlen(filename);
67 strcpy(s_name, filename);
68 uppercase(s_name, len);
70 period = strchr(s_name, '.');
72 period_location = len;
75 period_location = period - s_name;
76 ext_num = len - period_location - 1;
79 /* Pad spaces when the length of file name is shorter than eight */
80 if (period_location < 8) {
81 memcpy(dirent->name, s_name, period_location);
82 for (i = period_location; i < 8; i++)
83 dirent->name[i] = ' ';
84 } else if (period_location == 8) {
85 memcpy(dirent->name, s_name, period_location);
87 memcpy(dirent->name, s_name, 6);
88 dirent->name[6] = '~';
89 dirent->name[7] = '1';
93 memcpy(dirent->ext, s_name + period_location + 1, ext_num);
94 for (i = ext_num; i < 3; i++)
97 memcpy(dirent->ext, s_name + period_location + 1, 3);
99 debug("name : %s\n", dirent->name);
100 debug("ext : %s\n", dirent->ext);
103 static __u8 num_of_fats;
105 * Write fat buffer into block device
107 static int flush_dirty_fat_buffer(fsdata *mydata)
109 int getsize = FATBUFBLOCKS;
110 __u32 fatlength = mydata->fatlength;
111 __u8 *bufptr = mydata->fatbuf;
112 __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
114 debug("debug: evicting %d, dirty: %d\n", mydata->fatbufnum,
115 (int)mydata->fat_dirty);
117 if ((!mydata->fat_dirty) || (mydata->fatbufnum == -1))
120 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
121 if (startblock + getsize > fatlength)
122 getsize = fatlength - startblock;
124 startblock += mydata->fat_sect;
127 if (disk_write(startblock, getsize, bufptr) < 0) {
128 debug("error: writing FAT blocks\n");
132 if (num_of_fats == 2) {
133 /* Update corresponding second FAT blocks */
134 startblock += mydata->fatlength;
135 if (disk_write(startblock, getsize, bufptr) < 0) {
136 debug("error: writing second FAT blocks\n");
140 mydata->fat_dirty = 0;
146 * Get the entry at index 'entry' in a FAT (12/16/32) table.
147 * On failure 0x00 is returned.
148 * When bufnum is changed, write back the previous fatbuf to the disk.
150 static __u32 get_fatent_value(fsdata *mydata, __u32 entry)
157 if (CHECK_CLUST(entry, mydata->fatsize)) {
158 printf("Error: Invalid FAT entry: 0x%08x\n", 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 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
192 if (startblock + getsize > fatlength)
193 getsize = fatlength - startblock;
195 startblock += mydata->fat_sect; /* Offset from start of disk */
197 /* Write back the fatbuf to the disk */
198 if (flush_dirty_fat_buffer(mydata) < 0)
201 if (disk_read(startblock, getsize, bufptr) < 0) {
202 debug("Error reading FAT blocks\n");
205 mydata->fatbufnum = bufnum;
208 /* Get the actual entry from the table */
209 switch (mydata->fatsize) {
211 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
214 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
217 off16 = (offset * 3) / 4;
219 switch (offset & 0x3) {
221 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
225 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
227 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
229 ret = (val2 << 4) | (val1 >> 12);
232 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
234 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
236 ret = (val2 << 8) | (val1 >> 8);
239 ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
240 ret = (ret & 0xfff0) >> 4;
247 debug("FAT%d: ret: %08x, entry: %08x, offset: %04x\n",
248 mydata->fatsize, ret, entry, offset);
254 * Set the file name information from 'name' into 'slotptr',
256 static int str2slot(dir_slot *slotptr, const char *name, int *idx)
260 for (j = 0; j <= 8; j += 2) {
261 if (name[*idx] == 0x00) {
262 slotptr->name0_4[j] = 0;
263 slotptr->name0_4[j + 1] = 0;
267 slotptr->name0_4[j] = name[*idx];
271 for (j = 0; j <= 10; j += 2) {
272 if (name[*idx] == 0x00) {
273 slotptr->name5_10[j] = 0;
274 slotptr->name5_10[j + 1] = 0;
278 slotptr->name5_10[j] = name[*idx];
282 for (j = 0; j <= 2; j += 2) {
283 if (name[*idx] == 0x00) {
284 slotptr->name11_12[j] = 0;
285 slotptr->name11_12[j + 1] = 0;
289 slotptr->name11_12[j] = name[*idx];
294 if (name[*idx] == 0x00)
298 /* Not used characters are filled with 0xff 0xff */
300 for (; end_idx < 5; end_idx++) {
301 slotptr->name0_4[end_idx * 2] = 0xff;
302 slotptr->name0_4[end_idx * 2 + 1] = 0xff;
307 for (; end_idx < 6; end_idx++) {
308 slotptr->name5_10[end_idx * 2] = 0xff;
309 slotptr->name5_10[end_idx * 2 + 1] = 0xff;
314 for (; end_idx < 2; end_idx++) {
315 slotptr->name11_12[end_idx * 2] = 0xff;
316 slotptr->name11_12[end_idx * 2 + 1] = 0xff;
322 static int is_next_clust(fsdata *mydata, dir_entry *dentptr);
323 static void flush_dir_table(fsdata *mydata, dir_entry **dentptr);
326 * Fill dir_slot entries with appropriate name, id, and attr
327 * The real directory entry is returned by 'dentptr'
330 fill_dir_slot(fsdata *mydata, dir_entry **dentptr, const char *l_name)
332 __u8 temp_dir_slot_buffer[MAX_LFN_SLOT * sizeof(dir_slot)];
333 dir_slot *slotptr = (dir_slot *)temp_dir_slot_buffer;
334 __u8 counter = 0, checksum;
337 /* Get short file name checksum value */
338 checksum = mkcksum((*dentptr)->name, (*dentptr)->ext);
341 memset(slotptr, 0x00, sizeof(dir_slot));
342 ret = str2slot(slotptr, l_name, &idx);
343 slotptr->id = ++counter;
344 slotptr->attr = ATTR_VFAT;
345 slotptr->alias_checksum = checksum;
350 slotptr->id |= LAST_LONG_ENTRY_MASK;
352 while (counter >= 1) {
353 if (is_next_clust(mydata, *dentptr)) {
354 /* A new cluster is allocated for directory table */
355 flush_dir_table(mydata, dentptr);
357 memcpy(*dentptr, slotptr, sizeof(dir_slot));
363 if (is_next_clust(mydata, *dentptr)) {
364 /* A new cluster is allocated for directory table */
365 flush_dir_table(mydata, dentptr);
369 static __u32 dir_curclust;
372 * Extract the full long filename starting at 'retdent' (which is really
373 * a slot) into 'l_name'. If successful also copy the real directory entry
375 * If additional adjacent cluster for directory entries is read into memory,
376 * then 'get_contents_vfatname_block' is copied into 'get_dentfromdir_block' and
377 * the location of the real directory entry is returned by 'retdent'
378 * Return 0 on success, -1 otherwise.
381 get_long_file_name(fsdata *mydata, int curclust, __u8 *cluster,
382 dir_entry **retdent, char *l_name)
385 dir_slot *slotptr = (dir_slot *)(*retdent);
386 dir_slot *slotptr2 = NULL;
387 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
390 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
391 int idx = 0, cur_position = 0;
393 if (counter > VFAT_MAXSEQ) {
394 debug("Error: VFAT name is too long\n");
398 while ((__u8 *)slotptr < buflimit) {
401 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
407 if ((__u8 *)slotptr >= buflimit) {
410 curclust = get_fatent_value(mydata, dir_curclust);
411 if (CHECK_CLUST(curclust, mydata->fatsize)) {
412 debug("curclust: 0x%x\n", curclust);
413 printf("Invalid FAT entry\n");
417 dir_curclust = curclust;
419 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
420 mydata->clust_size * mydata->sect_size) != 0) {
421 debug("Error: reading directory block\n");
425 slotptr2 = (dir_slot *)get_contents_vfatname_block;
426 while (counter > 0) {
427 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
434 /* Save the real directory entry */
435 realdent = (dir_entry *)slotptr2;
436 while ((__u8 *)slotptr2 > get_contents_vfatname_block) {
438 slot2str(slotptr2, l_name, &idx);
441 /* Save the real directory entry */
442 realdent = (dir_entry *)slotptr;
447 if (slot2str(slotptr, l_name, &idx))
449 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
452 if (*l_name == DELETED_FLAG)
454 else if (*l_name == aRING)
455 *l_name = DELETED_FLAG;
458 /* Return the real directory entry */
462 memcpy(get_dentfromdir_block, get_contents_vfatname_block,
463 mydata->clust_size * mydata->sect_size);
464 cur_position = (__u8 *)realdent - get_contents_vfatname_block;
465 *retdent = (dir_entry *) &get_dentfromdir_block[cur_position];
472 * Set the entry at index 'entry' in a FAT (12/16/32) table.
474 static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
476 __u32 bufnum, offset, off16;
479 switch (mydata->fatsize) {
481 bufnum = entry / FAT32BUFSIZE;
482 offset = entry - bufnum * FAT32BUFSIZE;
485 bufnum = entry / FAT16BUFSIZE;
486 offset = entry - bufnum * FAT16BUFSIZE;
489 bufnum = entry / FAT12BUFSIZE;
490 offset = entry - bufnum * FAT12BUFSIZE;
493 /* Unsupported FAT size */
497 /* Read a new block of FAT entries into the cache. */
498 if (bufnum != mydata->fatbufnum) {
499 int getsize = FATBUFBLOCKS;
500 __u8 *bufptr = mydata->fatbuf;
501 __u32 fatlength = mydata->fatlength;
502 __u32 startblock = bufnum * FATBUFBLOCKS;
504 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
505 if (startblock + getsize > fatlength)
506 getsize = fatlength - startblock;
508 if (flush_dirty_fat_buffer(mydata) < 0)
511 startblock += mydata->fat_sect;
513 if (disk_read(startblock, getsize, bufptr) < 0) {
514 debug("Error reading FAT blocks\n");
517 mydata->fatbufnum = bufnum;
521 mydata->fat_dirty = 1;
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);
532 off16 = (offset * 3) / 4;
534 switch (offset & 0x3) {
536 val1 = cpu_to_le16(entry_value) & 0xfff;
537 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff;
538 ((__u16 *)mydata->fatbuf)[off16] |= val1;
541 val1 = cpu_to_le16(entry_value) & 0xf;
542 val2 = (cpu_to_le16(entry_value) >> 4) & 0xff;
544 ((__u16 *)mydata->fatbuf)[off16] &= ~0xf000;
545 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 12);
547 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xff;
548 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
551 val1 = cpu_to_le16(entry_value) & 0xff;
552 val2 = (cpu_to_le16(entry_value) >> 8) & 0xf;
554 ((__u16 *)mydata->fatbuf)[off16] &= ~0xff00;
555 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 8);
557 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xf;
558 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
561 val1 = cpu_to_le16(entry_value) & 0xfff;
562 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff0;
563 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 4);
578 * Determine the next free cluster after 'entry' in a FAT (12/16/32) table
579 * and link it to 'entry'. EOC marker is not set on returned entry.
581 static __u32 determine_fatent(fsdata *mydata, __u32 entry)
583 __u32 next_fat, next_entry = entry + 1;
586 next_fat = get_fatent_value(mydata, next_entry);
588 /* found free entry, link to entry */
589 set_fatent_value(mydata, entry, next_entry);
594 debug("FAT%d: entry: %08x, entry_value: %04x\n",
595 mydata->fatsize, entry, next_entry);
601 * Write at most 'size' bytes from 'buffer' into the specified cluster.
602 * Return 0 on success, -1 otherwise.
605 set_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer,
613 startsect = mydata->data_begin +
614 clustnum * mydata->clust_size;
616 startsect = mydata->rootdir_sect;
618 debug("clustnum: %d, startsect: %d\n", clustnum, startsect);
620 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
621 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
623 printf("FAT: Misaligned buffer address (%p)\n", buffer);
625 while (size >= mydata->sect_size) {
626 memcpy(tmpbuf, buffer, mydata->sect_size);
627 ret = disk_write(startsect++, 1, tmpbuf);
629 debug("Error writing data (got %d)\n", ret);
633 buffer += mydata->sect_size;
634 size -= mydata->sect_size;
636 } else if (size >= mydata->sect_size) {
637 idx = size / mydata->sect_size;
638 ret = disk_write(startsect, idx, buffer);
640 debug("Error writing data (got %d)\n", ret);
645 idx *= mydata->sect_size;
651 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
653 memcpy(tmpbuf, buffer, size);
654 ret = disk_write(startsect, 1, tmpbuf);
656 debug("Error writing data (got %d)\n", ret);
665 * Find the first empty cluster
667 static int find_empty_cluster(fsdata *mydata)
669 __u32 fat_val, entry = 3;
672 fat_val = get_fatent_value(mydata, entry);
682 * Write directory entries in 'get_dentfromdir_block' to block device
684 static void flush_dir_table(fsdata *mydata, dir_entry **dentptr)
686 int dir_newclust = 0;
688 if (set_cluster(mydata, dir_curclust,
689 get_dentfromdir_block,
690 mydata->clust_size * mydata->sect_size) != 0) {
691 printf("error: wrinting directory entry\n");
694 dir_newclust = find_empty_cluster(mydata);
695 set_fatent_value(mydata, dir_curclust, dir_newclust);
696 if (mydata->fatsize == 32)
697 set_fatent_value(mydata, dir_newclust, 0xffffff8);
698 else if (mydata->fatsize == 16)
699 set_fatent_value(mydata, dir_newclust, 0xfff8);
700 else if (mydata->fatsize == 12)
701 set_fatent_value(mydata, dir_newclust, 0xff8);
703 dir_curclust = dir_newclust;
705 if (flush_dirty_fat_buffer(mydata) < 0)
708 memset(get_dentfromdir_block, 0x00,
709 mydata->clust_size * mydata->sect_size);
711 *dentptr = (dir_entry *) get_dentfromdir_block;
715 * Set empty cluster from 'entry' to the end of a file
717 static int clear_fatent(fsdata *mydata, __u32 entry)
721 while (!CHECK_CLUST(entry, mydata->fatsize)) {
722 fat_val = get_fatent_value(mydata, entry);
724 set_fatent_value(mydata, entry, 0);
731 /* Flush fat buffer */
732 if (flush_dirty_fat_buffer(mydata) < 0)
739 * Write at most 'maxsize' bytes from 'buffer' into
740 * the file associated with 'dentptr'
741 * Update the number of bytes written in *gotsize and return 0
742 * or return -1 on fatal errors.
745 set_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
746 loff_t maxsize, loff_t *gotsize)
748 loff_t filesize = FAT2CPU32(dentptr->size);
749 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
750 __u32 curclust = START(dentptr);
751 __u32 endclust = 0, newclust = 0;
755 debug("Filesize: %llu bytes\n", filesize);
757 if (maxsize > 0 && filesize > maxsize)
760 debug("%llu bytes\n", filesize);
764 debug("error: nonempty clusterless file!\n");
770 actsize = bytesperclust;
773 /* search for consecutive clusters */
774 while (actsize < filesize) {
775 newclust = determine_fatent(mydata, endclust);
777 if ((newclust - 1) != endclust)
780 if (CHECK_CLUST(newclust, mydata->fatsize)) {
781 debug("newclust: 0x%x\n", newclust);
782 debug("Invalid FAT entry\n");
786 actsize += bytesperclust;
789 /* set remaining bytes */
791 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
792 debug("error: writing cluster\n");
797 /* Mark end of file in FAT */
798 if (mydata->fatsize == 12)
800 else if (mydata->fatsize == 16)
802 else if (mydata->fatsize == 32)
803 newclust = 0xfffffff;
804 set_fatent_value(mydata, endclust, newclust);
808 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
809 debug("error: writing cluster\n");
816 if (CHECK_CLUST(newclust, mydata->fatsize)) {
817 debug("newclust: 0x%x\n", newclust);
818 debug("Invalid FAT entry\n");
821 actsize = bytesperclust;
822 curclust = endclust = newclust;
827 * Set start cluster in directory entry
829 static void set_start_cluster(const fsdata *mydata, dir_entry *dentptr,
832 if (mydata->fatsize == 32)
834 cpu_to_le16((start_cluster & 0xffff0000) >> 16);
835 dentptr->start = cpu_to_le16(start_cluster & 0xffff);
841 static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
842 const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
844 set_start_cluster(mydata, dentptr, start_cluster);
845 dentptr->size = cpu_to_le32(size);
847 dentptr->attr = attr;
849 set_name(dentptr, filename);
853 * Check whether adding a file makes the file system to
854 * exceed the size of the block device
855 * Return -1 when overflow occurs, otherwise return 0
857 static int check_overflow(fsdata *mydata, __u32 clustnum, loff_t size)
859 __u32 startsect, sect_num, offset;
862 startsect = mydata->data_begin +
863 clustnum * mydata->clust_size;
865 startsect = mydata->rootdir_sect;
868 sect_num = div_u64_rem(size, mydata->sect_size, &offset);
873 if (startsect + sect_num > cur_part_info.start + total_sector)
879 * Check if adding several entries exceed one cluster boundary
881 static int is_next_clust(fsdata *mydata, dir_entry *dentptr)
885 cur_position = (__u8 *)dentptr - get_dentfromdir_block;
887 if (cur_position >= mydata->clust_size * mydata->sect_size)
893 static dir_entry *empty_dentptr;
895 * Find a directory entry based on filename or start cluster number
896 * If the directory entry is not found,
897 * the new position for writing a directory entry will be returned
899 static dir_entry *find_directory_entry(fsdata *mydata, int startsect,
900 char *filename, dir_entry *retdent, __u32 start)
902 __u32 curclust = (startsect - mydata->data_begin) / mydata->clust_size;
904 debug("get_dentfromdir: %s\n", filename);
911 if (get_cluster(mydata, curclust, get_dentfromdir_block,
912 mydata->clust_size * mydata->sect_size) != 0) {
913 printf("Error: reading directory block\n");
917 dentptr = (dir_entry *)get_dentfromdir_block;
919 dir_curclust = curclust;
921 for (i = 0; i < DIRENTSPERCLUST; i++) {
922 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
925 if (dentptr->name[0] == DELETED_FLAG) {
927 if (is_next_clust(mydata, dentptr))
931 if ((dentptr->attr & ATTR_VOLUME)) {
933 (dentptr->attr & ATTR_VFAT) &&
934 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
935 get_long_file_name(mydata, curclust,
936 get_dentfromdir_block,
938 debug("vfatname: |%s|\n", l_name);
940 /* Volume label or VFAT entry */
942 if (is_next_clust(mydata, dentptr))
947 if (dentptr->name[0] == 0) {
948 debug("Dentname == NULL - %d\n", i);
949 empty_dentptr = dentptr;
953 get_name(dentptr, s_name);
955 if (strcmp(filename, s_name)
956 && strcmp(filename, l_name)) {
957 debug("Mismatch: |%s|%s|\n",
960 if (is_next_clust(mydata, dentptr))
965 memcpy(retdent, dentptr, sizeof(dir_entry));
967 debug("DentName: %s", s_name);
968 debug(", start: 0x%x", START(dentptr));
969 debug(", size: 0x%x %s\n",
970 FAT2CPU32(dentptr->size),
971 (dentptr->attr & ATTR_DIR) ?
978 * In FAT16/12, the root dir is locate before data area, shows
980 * -------------------------------------------------------------
981 * | Boot | FAT1 & 2 | Root dir | Data (start from cluster #2) |
982 * -------------------------------------------------------------
984 * As a result if curclust is in Root dir, it is a negative
988 if (mydata->fatsize != 32 && (int)curclust <= 1) {
989 /* Current clust is in root dir, set to next clust */
991 if ((int)curclust <= 1)
992 continue; /* continue to find */
994 /* Reach the end of root dir */
995 empty_dentptr = dentptr;
999 curclust = get_fatent_value(mydata, dir_curclust);
1000 if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
1001 empty_dentptr = dentptr;
1004 if (CHECK_CLUST(curclust, mydata->fatsize)) {
1005 debug("curclust: 0x%x\n", curclust);
1006 debug("Invalid FAT entry\n");
1014 static int do_fat_write(const char *filename, void *buffer, loff_t size,
1017 dir_entry *dentptr, *retdent;
1019 __u32 start_cluster;
1021 volume_info volinfo;
1023 fsdata *mydata = &datablock;
1025 int ret = -1, name_len;
1026 char l_filename[VFAT_MAXLEN_BYTES];
1031 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
1032 debug("error: reading boot sector\n");
1036 total_sector = bs.total_sect;
1037 if (total_sector == 0)
1038 total_sector = (int)cur_part_info.size; /* cast of lbaint_t */
1040 if (mydata->fatsize == 32)
1041 mydata->fatlength = bs.fat32_length;
1043 mydata->fatlength = bs.fat_length;
1045 mydata->fat_sect = bs.reserved;
1047 cursect = mydata->rootdir_sect
1048 = mydata->fat_sect + mydata->fatlength * bs.fats;
1049 num_of_fats = bs.fats;
1051 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
1052 mydata->clust_size = bs.cluster_size;
1054 if (mydata->fatsize == 32) {
1055 mydata->data_begin = mydata->rootdir_sect -
1056 (mydata->clust_size * 2);
1060 rootdir_size = ((bs.dir_entries[1] * (int)256 +
1061 bs.dir_entries[0]) *
1062 sizeof(dir_entry)) /
1064 mydata->data_begin = mydata->rootdir_sect +
1066 (mydata->clust_size * 2);
1069 mydata->fatbufnum = -1;
1070 mydata->fat_dirty = 0;
1071 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
1072 if (mydata->fatbuf == NULL) {
1073 debug("Error: allocating memory\n");
1077 if (disk_read(cursect,
1078 (mydata->fatsize == 32) ?
1079 (mydata->clust_size) :
1080 PREFETCH_BLOCKS, do_fat_read_at_block) < 0) {
1081 debug("Error: reading rootdir block\n");
1084 dentptr = (dir_entry *) do_fat_read_at_block;
1086 name_len = strlen(filename);
1087 if (name_len >= VFAT_MAXLEN_BYTES)
1088 name_len = VFAT_MAXLEN_BYTES - 1;
1090 memcpy(l_filename, filename, name_len);
1091 l_filename[name_len] = 0; /* terminate the string */
1092 downcase(l_filename);
1094 startsect = mydata->rootdir_sect;
1095 retdent = find_directory_entry(mydata, startsect,
1096 l_filename, dentptr, 0);
1098 /* Update file size and start_cluster in a directory entry */
1099 retdent->size = cpu_to_le32(size);
1100 start_cluster = START(retdent);
1102 if (start_cluster) {
1104 ret = check_overflow(mydata, start_cluster,
1107 printf("Error: %llu overflow\n", size);
1112 ret = clear_fatent(mydata, start_cluster);
1114 printf("Error: clearing FAT entries\n");
1119 set_start_cluster(mydata, retdent, 0);
1121 ret = start_cluster = find_empty_cluster(mydata);
1123 printf("Error: finding empty cluster\n");
1127 ret = check_overflow(mydata, start_cluster, size);
1129 printf("Error: %llu overflow\n", size);
1133 set_start_cluster(mydata, retdent, start_cluster);
1136 /* Set short name to set alias checksum field in dir_slot */
1137 set_name(empty_dentptr, filename);
1138 fill_dir_slot(mydata, &empty_dentptr, filename);
1141 ret = start_cluster = find_empty_cluster(mydata);
1143 printf("Error: finding empty cluster\n");
1147 ret = check_overflow(mydata, start_cluster, size);
1149 printf("Error: %llu overflow\n", size);
1156 /* Set attribute as archieve for regular file */
1157 fill_dentry(mydata, empty_dentptr, filename,
1158 start_cluster, size, 0x20);
1160 retdent = empty_dentptr;
1163 ret = set_contents(mydata, retdent, buffer, size, actwrite);
1165 printf("Error: writing contents\n");
1168 debug("attempt to write 0x%llx bytes\n", *actwrite);
1170 /* Flush fat buffer */
1171 ret = flush_dirty_fat_buffer(mydata);
1173 printf("Error: flush fat buffer\n");
1177 /* Write directory table to device */
1178 ret = set_cluster(mydata, dir_curclust, get_dentfromdir_block,
1179 mydata->clust_size * mydata->sect_size);
1181 printf("Error: writing directory entry\n");
1184 free(mydata->fatbuf);
1188 int file_fat_write(const char *filename, void *buffer, loff_t offset,
1189 loff_t maxsize, loff_t *actwrite)
1192 printf("Error: non zero offset is currently not supported.\n");
1196 printf("writing %s\n", filename);
1197 return do_fat_write(filename, buffer, maxsize, actwrite);