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 block, __u32 nr_blocks, void *buf)
46 if (!cur_dev || !cur_dev->block_write)
49 if (cur_part_info.start + block + nr_blocks >
50 cur_part_info.start + total_sector) {
51 printf("error: overflow occurs\n");
55 return cur_dev->block_write(cur_dev->dev,
56 cur_part_info.start + block, nr_blocks, buf);
60 * Set short name in directory entry
62 static void set_name(dir_entry *dirent, const char *filename)
64 char s_name[VFAT_MAXLEN_BYTES];
66 int period_location, len, i, ext_num;
71 len = strlen(filename);
75 memcpy(s_name, filename, len);
76 uppercase(s_name, len);
78 period = strchr(s_name, '.');
80 period_location = len;
83 period_location = period - s_name;
84 ext_num = len - period_location - 1;
87 /* Pad spaces when the length of file name is shorter than eight */
88 if (period_location < 8) {
89 memcpy(dirent->name, s_name, period_location);
90 for (i = period_location; i < 8; i++)
91 dirent->name[i] = ' ';
92 } else if (period_location == 8) {
93 memcpy(dirent->name, s_name, period_location);
95 memcpy(dirent->name, s_name, 6);
96 dirent->name[6] = '~';
97 dirent->name[7] = '1';
101 memcpy(dirent->ext, s_name + period_location + 1, ext_num);
102 for (i = ext_num; i < 3; i++)
103 dirent->ext[i] = ' ';
105 memcpy(dirent->ext, s_name + period_location + 1, 3);
107 debug("name : %s\n", dirent->name);
108 debug("ext : %s\n", dirent->ext);
111 static __u8 num_of_fats;
113 * Write fat buffer into block device
115 static int flush_fat_buffer(fsdata *mydata)
117 int getsize = FATBUFBLOCKS;
118 __u32 fatlength = mydata->fatlength;
119 __u8 *bufptr = mydata->fatbuf;
120 __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
122 fatlength *= mydata->sect_size;
123 startblock += mydata->fat_sect;
125 if (getsize > fatlength)
129 if (disk_write(startblock, getsize, bufptr) < 0) {
130 debug("error: writing FAT blocks\n");
134 if (num_of_fats == 2) {
135 /* Update corresponding second FAT blocks */
136 startblock += mydata->fatlength;
137 if (disk_write(startblock, getsize, bufptr) < 0) {
138 debug("error: writing second FAT blocks\n");
147 * Get the entry at index 'entry' in a FAT (12/16/32) table.
148 * On failure 0x00 is returned.
149 * When bufnum is changed, write back the previous fatbuf to the disk.
151 static __u32 get_fatent_value(fsdata *mydata, __u32 entry)
158 switch (mydata->fatsize) {
160 bufnum = entry / FAT32BUFSIZE;
161 offset = entry - bufnum * FAT32BUFSIZE;
164 bufnum = entry / FAT16BUFSIZE;
165 offset = entry - bufnum * FAT16BUFSIZE;
168 bufnum = entry / FAT12BUFSIZE;
169 offset = entry - bufnum * FAT12BUFSIZE;
173 /* Unsupported FAT size */
177 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
178 mydata->fatsize, entry, entry, offset, offset);
180 /* Read a new block of FAT entries into the cache. */
181 if (bufnum != mydata->fatbufnum) {
182 int getsize = FATBUFBLOCKS;
183 __u8 *bufptr = mydata->fatbuf;
184 __u32 fatlength = mydata->fatlength;
185 __u32 startblock = bufnum * FATBUFBLOCKS;
187 if (getsize > fatlength)
190 fatlength *= mydata->sect_size; /* We want it in bytes now */
191 startblock += mydata->fat_sect; /* Offset from start of disk */
193 /* Write back the fatbuf to the disk */
194 if (mydata->fatbufnum != -1) {
195 if (flush_fat_buffer(mydata) < 0)
199 if (disk_read(startblock, getsize, bufptr) < 0) {
200 debug("Error reading FAT blocks\n");
203 mydata->fatbufnum = bufnum;
206 /* Get the actual entry from the table */
207 switch (mydata->fatsize) {
209 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
212 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
215 off16 = (offset * 3) / 4;
217 switch (offset & 0x3) {
219 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
223 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
225 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
227 ret = (val2 << 4) | (val1 >> 12);
230 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
232 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
234 ret = (val2 << 8) | (val1 >> 8);
237 ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
238 ret = (ret & 0xfff0) >> 4;
245 debug("FAT%d: ret: %08x, entry: %08x, offset: %04x\n",
246 mydata->fatsize, ret, entry, offset);
251 #ifdef CONFIG_SUPPORT_VFAT
253 * Set the file name information from 'name' into 'slotptr',
255 static int str2slot(dir_slot *slotptr, const char *name, int *idx)
259 for (j = 0; j <= 8; j += 2) {
260 if (name[*idx] == 0x00) {
261 slotptr->name0_4[j] = 0;
262 slotptr->name0_4[j + 1] = 0;
266 slotptr->name0_4[j] = name[*idx];
270 for (j = 0; j <= 10; j += 2) {
271 if (name[*idx] == 0x00) {
272 slotptr->name5_10[j] = 0;
273 slotptr->name5_10[j + 1] = 0;
277 slotptr->name5_10[j] = name[*idx];
281 for (j = 0; j <= 2; j += 2) {
282 if (name[*idx] == 0x00) {
283 slotptr->name11_12[j] = 0;
284 slotptr->name11_12[j + 1] = 0;
288 slotptr->name11_12[j] = name[*idx];
293 if (name[*idx] == 0x00)
297 /* Not used characters are filled with 0xff 0xff */
299 for (; end_idx < 5; end_idx++) {
300 slotptr->name0_4[end_idx * 2] = 0xff;
301 slotptr->name0_4[end_idx * 2 + 1] = 0xff;
306 for (; end_idx < 6; end_idx++) {
307 slotptr->name5_10[end_idx * 2] = 0xff;
308 slotptr->name5_10[end_idx * 2 + 1] = 0xff;
313 for (; end_idx < 2; end_idx++) {
314 slotptr->name11_12[end_idx * 2] = 0xff;
315 slotptr->name11_12[end_idx * 2 + 1] = 0xff;
321 static int is_next_clust(fsdata *mydata, dir_entry *dentptr);
322 static void flush_dir_table(fsdata *mydata, dir_entry **dentptr);
325 * Fill dir_slot entries with appropriate name, id, and attr
326 * The real directory entry is returned by 'dentptr'
329 fill_dir_slot(fsdata *mydata, dir_entry **dentptr, const char *l_name)
331 dir_slot *slotptr = (dir_slot *)get_vfatname_block;
332 __u8 counter = 0, checksum;
336 /* Get short file name and checksum value */
337 strncpy(s_name, (*dentptr)->name, 16);
338 checksum = mkcksum(s_name);
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_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_vfatname_block,
420 mydata->clust_size * mydata->sect_size) != 0) {
421 debug("Error: reading directory block\n");
425 slotptr2 = (dir_slot *)get_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_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_vfatname_block,
463 mydata->clust_size * mydata->sect_size);
464 cur_position = (__u8 *)realdent - get_vfatname_block;
465 *retdent = (dir_entry *) &get_dentfromdir_block[cur_position];
472 delete_long_file_name(fsdata *mydata, int curclust, __u8 *cluster,
476 dir_slot *slotptr = (dir_slot *)(*retdent);
477 dir_slot *slotptr2 = NULL;
478 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
481 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
483 if (counter > VFAT_MAXSEQ) {
484 debug("Error: VFAT name is too long\n");
488 while ((__u8 *)slotptr < buflimit) {
491 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
493 slotptr->id = DELETED_FLAG;
498 if ((__u8 *)slotptr >= buflimit) {
501 curclust = get_fatent_value(mydata, dir_curclust);
502 if (CHECK_CLUST(curclust, mydata->fatsize)) {
503 debug("curclust: 0x%x\n", curclust);
504 printf("Invalid FAT entry\n");
508 dir_curclust = curclust;
510 if (get_cluster(mydata, curclust, get_vfatname_block,
511 mydata->clust_size * mydata->sect_size) != 0) {
512 debug("Error: reading directory block\n");
516 slotptr2 = (dir_slot *)get_vfatname_block;
517 while (counter > 0) {
518 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
521 slotptr2->id = DELETED_FLAG;
526 /* Save the real directory entry */
527 realdent = (dir_entry *)slotptr2;
529 /* Save the real directory entry */
530 realdent = (dir_entry *)slotptr;
532 realdent->name[0] = DELETED_FLAG;
539 * Set the entry at index 'entry' in a FAT (16/32) table.
541 static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
543 __u32 bufnum, offset;
545 switch (mydata->fatsize) {
547 bufnum = entry / FAT32BUFSIZE;
548 offset = entry - bufnum * FAT32BUFSIZE;
551 bufnum = entry / FAT16BUFSIZE;
552 offset = entry - bufnum * FAT16BUFSIZE;
555 /* Unsupported FAT size */
559 /* Read a new block of FAT entries into the cache. */
560 if (bufnum != mydata->fatbufnum) {
561 int getsize = FATBUFBLOCKS;
562 __u8 *bufptr = mydata->fatbuf;
563 __u32 fatlength = mydata->fatlength;
564 __u32 startblock = bufnum * FATBUFBLOCKS;
566 fatlength *= mydata->sect_size;
567 startblock += mydata->fat_sect;
569 if (getsize > fatlength)
572 if (mydata->fatbufnum != -1) {
573 if (flush_fat_buffer(mydata) < 0)
577 if (disk_read(startblock, getsize, bufptr) < 0) {
578 debug("Error reading FAT blocks\n");
581 mydata->fatbufnum = bufnum;
584 /* Set the actual entry */
585 switch (mydata->fatsize) {
587 ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
590 ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
600 * Determine the entry value at index 'entry' in a FAT (16/32) table
602 static __u32 determine_fatent(fsdata *mydata, __u32 entry)
604 __u32 next_fat, next_entry = entry + 1;
607 next_fat = get_fatent_value(mydata, next_entry);
609 set_fatent_value(mydata, entry, next_entry);
614 debug("FAT%d: entry: %08x, entry_value: %04x\n",
615 mydata->fatsize, entry, next_entry);
621 * Write at most 'size' bytes from 'buffer' into the specified cluster.
622 * Return 0 on success, -1 otherwise.
625 set_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer,
632 startsect = mydata->data_begin +
633 clustnum * mydata->clust_size;
635 startsect = mydata->rootdir_sect;
637 debug("clustnum: %d, startsect: %d\n", clustnum, startsect);
639 if (disk_write(startsect, size / mydata->sect_size, buffer) < 0) {
640 debug("Error writing data\n");
644 if (size % mydata->sect_size) {
645 __u8 tmpbuf[mydata->sect_size];
647 idx = size / mydata->sect_size;
648 buffer += idx * mydata->sect_size;
649 memcpy(tmpbuf, buffer, size % mydata->sect_size);
651 if (disk_write(startsect + idx, 1, tmpbuf) < 0) {
652 debug("Error writing data\n");
663 * Find the first empty cluster
665 static int find_empty_cluster(fsdata *mydata)
667 __u32 fat_val, entry = 3;
670 fat_val = get_fatent_value(mydata, entry);
680 * Write directory entries in 'get_dentfromdir_block' to block device
682 static void flush_dir_table(fsdata *mydata, dir_entry **dentptr)
684 int dir_newclust = 0;
686 if (set_cluster(mydata, dir_curclust,
687 get_dentfromdir_block,
688 mydata->clust_size * mydata->sect_size) != 0) {
689 printf("error: wrinting directory entry\n");
692 dir_newclust = find_empty_cluster(mydata);
693 set_fatent_value(mydata, dir_curclust, dir_newclust);
694 if (mydata->fatsize == 32)
695 set_fatent_value(mydata, dir_newclust, 0xffffff8);
696 else if (mydata->fatsize == 16)
697 set_fatent_value(mydata, dir_newclust, 0xfff8);
699 dir_curclust = dir_newclust;
701 if (flush_fat_buffer(mydata) < 0)
704 memset(get_dentfromdir_block, 0x00,
705 mydata->clust_size * mydata->sect_size);
707 *dentptr = (dir_entry *) get_dentfromdir_block;
711 * Set empty cluster from 'entry' to the end of a file
713 static int clear_fatent(fsdata *mydata, __u32 entry)
718 fat_val = get_fatent_value(mydata, entry);
720 set_fatent_value(mydata, entry, 0);
724 if (fat_val == 0xfffffff || fat_val == 0xffff)
730 /* Flush fat buffer */
731 if (flush_fat_buffer(mydata) < 0)
738 * Write at most 'maxsize' bytes from 'buffer' into
739 * the file associated with 'dentptr'
740 * Return the number of bytes read or -1 on fatal errors.
743 set_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
744 unsigned long maxsize)
746 unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
747 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
748 __u32 curclust = START(dentptr);
749 __u32 endclust = 0, newclust = 0;
750 unsigned long actsize;
752 debug("Filesize: %ld bytes\n", filesize);
754 if (maxsize > 0 && filesize > maxsize)
757 debug("%ld bytes\n", filesize);
759 actsize = bytesperclust;
762 /* search for consecutive clusters */
763 while (actsize < filesize) {
764 newclust = determine_fatent(mydata, endclust);
766 if ((newclust - 1) != endclust)
769 if (CHECK_CLUST(newclust, mydata->fatsize)) {
770 debug("curclust: 0x%x\n", newclust);
771 debug("Invalid FAT entry\n");
775 actsize += bytesperclust;
777 /* actsize >= file size */
778 actsize -= bytesperclust;
779 /* set remaining clusters */
780 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
781 debug("error: writing cluster\n");
785 /* set remaining bytes */
786 gotsize += (int)actsize;
791 if (set_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
792 debug("error: writing cluster\n");
797 /* Mark end of file in FAT */
798 if (mydata->fatsize == 16)
800 else if (mydata->fatsize == 32)
801 newclust = 0xfffffff;
802 set_fatent_value(mydata, endclust, newclust);
806 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
807 debug("error: writing cluster\n");
810 gotsize += (int)actsize;
814 if (CHECK_CLUST(curclust, mydata->fatsize)) {
815 debug("curclust: 0x%x\n", curclust);
816 debug("Invalid FAT entry\n");
819 actsize = bytesperclust;
820 curclust = endclust = newclust;
827 static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
828 const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
830 if (mydata->fatsize == 32)
832 cpu_to_le16((start_cluster & 0xffff0000) >> 16);
833 dentptr->start = cpu_to_le16(start_cluster & 0xffff);
834 dentptr->size = cpu_to_le32(size);
836 dentptr->attr = attr;
838 set_name(dentptr, filename);
842 * Check whether adding a file makes the file system to
843 * exceed the size of the block device
844 * Return -1 when overflow occurs, otherwise return 0
846 static int check_overflow(fsdata *mydata, __u32 clustnum, unsigned long size)
848 __u32 startsect, sect_num;
851 startsect = mydata->data_begin +
852 clustnum * mydata->clust_size;
854 startsect = mydata->rootdir_sect;
857 sect_num = size / mydata->sect_size;
858 if (size % mydata->sect_size)
861 if (startsect + sect_num > cur_part_info.start + total_sector)
868 * Check if adding several entries exceed one cluster boundary
870 static int is_next_clust(fsdata *mydata, dir_entry *dentptr)
874 cur_position = (__u8 *)dentptr - get_dentfromdir_block;
876 if (cur_position >= mydata->clust_size * mydata->sect_size)
882 static dir_entry *empty_dentptr;
884 * Find a directory entry based on filename or start cluster number
885 * If the directory entry is not found,
886 * the new position for writing a directory entry will be returned
888 static dir_entry *find_directory_entry(fsdata *mydata, int startsect,
889 char *filename, dir_entry *retdent, __u32 start)
891 __u32 curclust = (startsect - mydata->data_begin) / mydata->clust_size;
893 debug("get_dentfromdir: %s\n", filename);
900 if (get_cluster(mydata, curclust, get_dentfromdir_block,
901 mydata->clust_size * mydata->sect_size) != 0) {
902 printf("Error: reading directory block\n");
906 dentptr = (dir_entry *)get_dentfromdir_block;
908 dir_curclust = curclust;
910 for (i = 0; i < DIRENTSPERCLUST; i++) {
911 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
914 if (dentptr->name[0] == DELETED_FLAG) {
916 if (is_next_clust(mydata, dentptr))
920 if ((dentptr->attr & ATTR_VOLUME)) {
921 #ifdef CONFIG_SUPPORT_VFAT
922 if ((dentptr->attr & ATTR_VFAT) &&
923 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
924 get_long_file_name(mydata, curclust,
925 get_dentfromdir_block,
927 debug("vfatname: |%s|\n", l_name);
931 /* Volume label or VFAT entry */
933 if (is_next_clust(mydata, dentptr))
938 if (dentptr->name[0] == 0) {
939 debug("Dentname == NULL - %d\n", i);
940 empty_dentptr = dentptr;
944 get_name(dentptr, s_name);
946 if (strcmp(filename, s_name)
947 && strcmp(filename, l_name)) {
948 debug("Mismatch: |%s|%s|\n",
951 if (is_next_clust(mydata, dentptr))
956 memcpy(retdent, dentptr, sizeof(dir_entry));
958 debug("DentName: %s", s_name);
959 debug(", start: 0x%x", START(dentptr));
960 debug(", size: 0x%x %s\n",
961 FAT2CPU32(dentptr->size),
962 (dentptr->attr & ATTR_DIR) ?
968 curclust = get_fatent_value(mydata, dir_curclust);
969 if ((curclust >= 0xffffff8) || (curclust >= 0xfff8)) {
970 empty_dentptr = dentptr;
973 if (CHECK_CLUST(curclust, mydata->fatsize)) {
974 debug("curclust: 0x%x\n", curclust);
975 debug("Invalid FAT entry\n");
983 static int do_fat_write(const char *filename, void *buffer,
986 dir_entry *dentptr, *retdent;
992 fsdata *mydata = &datablock;
994 int ret = -1, name_len;
995 char l_filename[VFAT_MAXLEN_BYTES];
996 int write_size = size;
1000 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
1001 debug("error: reading boot sector\n");
1005 total_sector = bs.total_sect;
1006 if (total_sector == 0)
1007 total_sector = cur_part_info.size;
1009 if (mydata->fatsize == 32)
1010 mydata->fatlength = bs.fat32_length;
1012 mydata->fatlength = bs.fat_length;
1014 mydata->fat_sect = bs.reserved;
1016 cursect = mydata->rootdir_sect
1017 = mydata->fat_sect + mydata->fatlength * bs.fats;
1018 num_of_fats = bs.fats;
1020 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
1021 mydata->clust_size = bs.cluster_size;
1023 if (mydata->fatsize == 32) {
1024 mydata->data_begin = mydata->rootdir_sect -
1025 (mydata->clust_size * 2);
1029 rootdir_size = ((bs.dir_entries[1] * (int)256 +
1030 bs.dir_entries[0]) *
1031 sizeof(dir_entry)) /
1033 mydata->data_begin = mydata->rootdir_sect +
1035 (mydata->clust_size * 2);
1038 mydata->fatbufnum = -1;
1039 mydata->fatbuf = malloc(FATBUFSIZE);
1040 if (mydata->fatbuf == NULL) {
1041 debug("Error: allocating memory\n");
1045 if (disk_read(cursect,
1046 (mydata->fatsize == 32) ?
1047 (mydata->clust_size) :
1048 PREFETCH_BLOCKS, do_fat_read_block) < 0) {
1049 debug("Error: reading rootdir block\n");
1052 dentptr = (dir_entry *) do_fat_read_block;
1054 name_len = strlen(filename);
1055 if (name_len >= VFAT_MAXLEN_BYTES)
1056 name_len = VFAT_MAXLEN_BYTES - 1;
1058 memcpy(l_filename, filename, name_len);
1059 l_filename[name_len] = 0; /* terminate the string */
1060 downcase(l_filename);
1062 startsect = mydata->rootdir_sect;
1063 retdent = find_directory_entry(mydata, startsect,
1064 l_filename, dentptr, 0);
1066 /* Update file size and start_cluster in a directory entry */
1067 retdent->size = cpu_to_le32(size);
1068 start_cluster = FAT2CPU16(retdent->start);
1069 if (mydata->fatsize == 32)
1071 (FAT2CPU16(retdent->starthi) << 16);
1073 ret = check_overflow(mydata, start_cluster, size);
1075 printf("Error: %ld overflow\n", size);
1079 ret = clear_fatent(mydata, start_cluster);
1081 printf("Error: clearing FAT entries\n");
1085 ret = set_contents(mydata, retdent, buffer, size);
1087 printf("Error: writing contents\n");
1091 debug("attempt to write 0x%x bytes\n", write_size);
1093 /* Flush fat buffer */
1094 ret = flush_fat_buffer(mydata);
1096 printf("Error: flush fat buffer\n");
1100 /* Write directory table to device */
1101 ret = set_cluster(mydata, dir_curclust,
1102 get_dentfromdir_block,
1103 mydata->clust_size * mydata->sect_size);
1105 printf("Error: writing directory entry\n");
1109 /* Set short name to set alias checksum field in dir_slot */
1110 set_name(empty_dentptr, filename);
1111 fill_dir_slot(mydata, &empty_dentptr, filename);
1113 ret = start_cluster = find_empty_cluster(mydata);
1115 printf("Error: finding empty cluster\n");
1119 ret = check_overflow(mydata, start_cluster, size);
1121 printf("Error: %ld overflow\n", size);
1125 /* Set attribute as archieve for regular file */
1126 fill_dentry(mydata, empty_dentptr, filename,
1127 start_cluster, size, 0x20);
1129 ret = set_contents(mydata, empty_dentptr, buffer, size);
1131 printf("Error: writing contents\n");
1135 debug("attempt to write 0x%x bytes\n", write_size);
1137 /* Flush fat buffer */
1138 ret = flush_fat_buffer(mydata);
1140 printf("Error: flush fat buffer\n");
1144 /* Write directory table to device */
1145 ret = set_cluster(mydata, dir_curclust,
1146 get_dentfromdir_block,
1147 mydata->clust_size * mydata->sect_size);
1149 printf("Error: writing directory entry\n");
1155 free(mydata->fatbuf);
1156 return ret < 0 ? ret : write_size;
1159 int file_fat_write(const char *filename, void *buffer, unsigned long maxsize)
1161 printf("writing %s\n", filename);
1162 return do_fat_write(filename, buffer, maxsize);
1165 static int delete_fatent(fsdata *mydata, __u32 entry)
1170 fat_val = get_fatent_value(mydata, entry);
1172 set_fatent_value(mydata, entry, 0);
1177 if (fat_val == 0xfffffff || fat_val == 0xffff) {
1184 /* Flush fat buffer */
1185 if (flush_fat_buffer(mydata) < 0)
1191 static dir_entry *delete_directory_entry(fsdata *mydata, int startsect,
1192 char *filename, dir_entry *retdent, __u32 start)
1194 __u32 curclust = (startsect - mydata->data_begin) / mydata->clust_size;
1195 __u32 find_curclust = curclust;
1196 #ifdef CONFIG_SUPPORT_VFAT
1197 dir_entry *find_dentptr = NULL;
1200 debug("get_dentfromdir: %s\n", filename);
1207 if (get_cluster(mydata, curclust, get_dentfromdir_block,
1208 mydata->clust_size * mydata->sect_size) != 0) {
1209 printf("Error: reading directory block\n");
1213 dentptr = (dir_entry *)get_dentfromdir_block;
1215 dir_curclust = curclust;
1217 for (i = 0; i < DIRENTSPERCLUST; i++) {
1218 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
1221 if (dentptr->name[0] == DELETED_FLAG) {
1223 if (is_next_clust(mydata, dentptr))
1227 if ((dentptr->attr & ATTR_VOLUME)) {
1228 #ifdef CONFIG_SUPPORT_VFAT
1229 if ((dentptr->attr & ATTR_VFAT) &&
1230 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
1231 find_dentptr = dentptr;
1232 find_curclust = curclust;
1233 get_long_file_name(mydata, curclust,
1234 get_dentfromdir_block,
1236 debug("vfatname: |%s|\n", l_name);
1240 /* Volume label or VFAT entry */
1242 if (is_next_clust(mydata, dentptr))
1247 if (dentptr->name[0] == 0) {
1248 debug("Dentname == NULL - %d\n", i);
1249 empty_dentptr = dentptr;
1253 get_name(dentptr, s_name);
1255 if (strcmp(filename, s_name)
1256 && strcmp(filename, l_name)) {
1257 debug("Mismatch: |%s|%s|\n",
1260 if (is_next_clust(mydata, dentptr))
1265 memcpy(retdent, dentptr, sizeof(dir_entry));
1266 #ifdef CONFIG_SUPPORT_VFAT
1267 delete_long_file_name(mydata, find_curclust,
1268 get_dentfromdir_block,
1271 dentptr->name[0] = DELETED_FLAG;
1273 if (set_cluster(mydata, dir_curclust,
1274 get_dentfromdir_block,
1275 mydata->clust_size * mydata->sect_size) != 0) {
1276 printf("error: wrinting directory entry\n");
1279 debug("DentName: %s", s_name);
1280 debug(", start: 0x%x", START(dentptr));
1281 debug(", size: 0x%x %s\n",
1282 FAT2CPU32(dentptr->size),
1283 (dentptr->attr & ATTR_DIR) ?
1289 curclust = get_fatent_value(mydata, dir_curclust);
1290 if ((curclust >= 0xffffff8) || (curclust >= 0xfff8)) {
1291 empty_dentptr = dentptr;
1294 if (CHECK_CLUST(curclust, mydata->fatsize)) {
1295 debug("curclust: 0x%x\n", curclust);
1296 debug("Invalid FAT entry\n");
1304 static int do_fat_rm(const char *filename)
1306 dir_entry *dentptr, *retdent;
1308 __u32 start_cluster;
1310 volume_info volinfo;
1312 fsdata *mydata = &datablock;
1314 int ret = -1, name_len;
1315 char l_filename[VFAT_MAXLEN_BYTES];
1319 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
1320 debug("error: reading boot sector\n");
1324 total_sector = bs.total_sect;
1325 if (total_sector == 0)
1326 total_sector = cur_part_info.size;
1328 if (mydata->fatsize == 32)
1329 mydata->fatlength = bs.fat32_length;
1331 mydata->fatlength = bs.fat_length;
1333 mydata->fat_sect = bs.reserved;
1335 cursect = mydata->rootdir_sect
1336 = mydata->fat_sect + mydata->fatlength * bs.fats;
1337 num_of_fats = bs.fats;
1339 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
1340 mydata->clust_size = bs.cluster_size;
1342 if (mydata->fatsize == 32) {
1343 mydata->data_begin = mydata->rootdir_sect -
1344 (mydata->clust_size * 2);
1348 rootdir_size = ((bs.dir_entries[1] * (int)256 +
1349 bs.dir_entries[0]) *
1350 sizeof(dir_entry)) /
1352 mydata->data_begin = mydata->rootdir_sect +
1354 (mydata->clust_size * 2);
1357 mydata->fatbufnum = -1;
1358 mydata->fatbuf = malloc(FATBUFSIZE);
1359 if (mydata->fatbuf == NULL) {
1360 debug("Error: allocating memory\n");
1364 if (disk_read(cursect,
1365 (mydata->fatsize == 32) ?
1366 (mydata->clust_size) :
1367 PREFETCH_BLOCKS, do_fat_read_block) < 0) {
1368 debug("Error: reading rootdir block\n");
1371 dentptr = (dir_entry *) do_fat_read_block;
1373 name_len = strlen(filename);
1374 if (name_len >= VFAT_MAXLEN_BYTES)
1375 name_len = VFAT_MAXLEN_BYTES - 1;
1377 memcpy(l_filename, filename, name_len);
1378 l_filename[name_len] = 0; /* terminate the string */
1379 downcase(l_filename);
1381 startsect = mydata->rootdir_sect;
1382 retdent = delete_directory_entry(mydata, startsect,
1383 l_filename, dentptr, 0);
1386 /* Update file size and start_cluster in a directory entry */
1387 start_cluster = FAT2CPU16(retdent->start);
1388 if (mydata->fatsize == 32)
1390 (FAT2CPU16(retdent->starthi) << 16);
1392 ret = delete_fatent(mydata, start_cluster);
1394 printf("Error: clearing FAT entries\n");
1398 /* Write directory table to device */
1399 ret = set_cluster(mydata, dir_curclust,
1400 get_dentfromdir_block,
1401 mydata->clust_size * mydata->sect_size);
1403 printf("Error: writing directory entry\n");
1407 printf("Error: %s not found\n", filename);
1412 free(mydata->fatbuf);
1413 return ret < 0 ? ret : 0;
1416 int file_fat_rm(const char *filename)
1418 printf("deleting %s\n", filename);
1419 return do_fat_rm(filename);