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;
482 int idx = 0, cur_position = 0;
484 if (counter > VFAT_MAXSEQ) {
485 debug("Error: VFAT name is too long\n");
489 while ((__u8 *)slotptr < buflimit) {
492 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
494 slotptr->id = DELETED_FLAG;
499 if ((__u8 *)slotptr >= buflimit) {
502 curclust = get_fatent_value(mydata, dir_curclust);
503 if (CHECK_CLUST(curclust, mydata->fatsize)) {
504 debug("curclust: 0x%x\n", curclust);
505 printf("Invalid FAT entry\n");
509 dir_curclust = curclust;
511 if (get_cluster(mydata, curclust, get_vfatname_block,
512 mydata->clust_size * mydata->sect_size) != 0) {
513 debug("Error: reading directory block\n");
517 slotptr2 = (dir_slot *)get_vfatname_block;
518 while (counter > 0) {
519 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
522 slotptr2->id = DELETED_FLAG;
527 /* Save the real directory entry */
528 realdent = (dir_entry *)slotptr2;
530 /* Save the real directory entry */
531 realdent = (dir_entry *)slotptr;
533 realdent->name[0] = DELETED_FLAG;
540 * Set the entry at index 'entry' in a FAT (16/32) table.
542 static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
544 __u32 bufnum, offset;
546 switch (mydata->fatsize) {
548 bufnum = entry / FAT32BUFSIZE;
549 offset = entry - bufnum * FAT32BUFSIZE;
552 bufnum = entry / FAT16BUFSIZE;
553 offset = entry - bufnum * FAT16BUFSIZE;
556 /* Unsupported FAT size */
560 /* Read a new block of FAT entries into the cache. */
561 if (bufnum != mydata->fatbufnum) {
562 int getsize = FATBUFBLOCKS;
563 __u8 *bufptr = mydata->fatbuf;
564 __u32 fatlength = mydata->fatlength;
565 __u32 startblock = bufnum * FATBUFBLOCKS;
567 fatlength *= mydata->sect_size;
568 startblock += mydata->fat_sect;
570 if (getsize > fatlength)
573 if (mydata->fatbufnum != -1) {
574 if (flush_fat_buffer(mydata) < 0)
578 if (disk_read(startblock, getsize, bufptr) < 0) {
579 debug("Error reading FAT blocks\n");
582 mydata->fatbufnum = bufnum;
585 /* Set the actual entry */
586 switch (mydata->fatsize) {
588 ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
591 ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
601 * Determine the entry value at index 'entry' in a FAT (16/32) table
603 static __u32 determine_fatent(fsdata *mydata, __u32 entry)
605 __u32 next_fat, next_entry = entry + 1;
608 next_fat = get_fatent_value(mydata, next_entry);
610 set_fatent_value(mydata, entry, next_entry);
615 debug("FAT%d: entry: %08x, entry_value: %04x\n",
616 mydata->fatsize, entry, next_entry);
622 * Write at most 'size' bytes from 'buffer' into the specified cluster.
623 * Return 0 on success, -1 otherwise.
626 set_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer,
633 startsect = mydata->data_begin +
634 clustnum * mydata->clust_size;
636 startsect = mydata->rootdir_sect;
638 debug("clustnum: %d, startsect: %d\n", clustnum, startsect);
640 if (disk_write(startsect, size / mydata->sect_size, buffer) < 0) {
641 debug("Error writing data\n");
645 if (size % mydata->sect_size) {
646 __u8 tmpbuf[mydata->sect_size];
648 idx = size / mydata->sect_size;
649 buffer += idx * mydata->sect_size;
650 memcpy(tmpbuf, buffer, size % mydata->sect_size);
652 if (disk_write(startsect + idx, 1, tmpbuf) < 0) {
653 debug("Error writing data\n");
664 * Find the first empty cluster
666 static int find_empty_cluster(fsdata *mydata)
668 __u32 fat_val, entry = 3;
671 fat_val = get_fatent_value(mydata, entry);
681 * Write directory entries in 'get_dentfromdir_block' to block device
683 static void flush_dir_table(fsdata *mydata, dir_entry **dentptr)
685 int dir_newclust = 0;
687 if (set_cluster(mydata, dir_curclust,
688 get_dentfromdir_block,
689 mydata->clust_size * mydata->sect_size) != 0) {
690 printf("error: wrinting directory entry\n");
693 dir_newclust = find_empty_cluster(mydata);
694 set_fatent_value(mydata, dir_curclust, dir_newclust);
695 if (mydata->fatsize == 32)
696 set_fatent_value(mydata, dir_newclust, 0xffffff8);
697 else if (mydata->fatsize == 16)
698 set_fatent_value(mydata, dir_newclust, 0xfff8);
700 dir_curclust = dir_newclust;
702 if (flush_fat_buffer(mydata) < 0)
705 memset(get_dentfromdir_block, 0x00,
706 mydata->clust_size * mydata->sect_size);
708 *dentptr = (dir_entry *) get_dentfromdir_block;
712 * Set empty cluster from 'entry' to the end of a file
714 static int clear_fatent(fsdata *mydata, __u32 entry)
719 fat_val = get_fatent_value(mydata, entry);
721 set_fatent_value(mydata, entry, 0);
725 if (fat_val == 0xfffffff || fat_val == 0xffff)
731 /* Flush fat buffer */
732 if (flush_fat_buffer(mydata) < 0)
739 * Write at most 'maxsize' bytes from 'buffer' into
740 * the file associated with 'dentptr'
741 * Return the number of bytes read or -1 on fatal errors.
744 set_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
745 unsigned long maxsize)
747 unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
748 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
749 __u32 curclust = START(dentptr);
750 __u32 endclust = 0, newclust = 0;
751 unsigned long actsize;
753 debug("Filesize: %ld bytes\n", filesize);
755 if (maxsize > 0 && filesize > maxsize)
758 debug("%ld bytes\n", filesize);
760 actsize = bytesperclust;
763 /* search for consecutive clusters */
764 while (actsize < filesize) {
765 newclust = determine_fatent(mydata, endclust);
767 if ((newclust - 1) != endclust)
770 if (CHECK_CLUST(newclust, mydata->fatsize)) {
771 debug("curclust: 0x%x\n", newclust);
772 debug("Invalid FAT entry\n");
776 actsize += bytesperclust;
778 /* actsize >= file size */
779 actsize -= bytesperclust;
780 /* set remaining clusters */
781 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
782 debug("error: writing cluster\n");
786 /* set remaining bytes */
787 gotsize += (int)actsize;
792 if (set_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
793 debug("error: writing cluster\n");
798 /* Mark end of file in FAT */
799 if (mydata->fatsize == 16)
801 else if (mydata->fatsize == 32)
802 newclust = 0xfffffff;
803 set_fatent_value(mydata, endclust, newclust);
807 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
808 debug("error: writing cluster\n");
811 gotsize += (int)actsize;
815 if (CHECK_CLUST(curclust, mydata->fatsize)) {
816 debug("curclust: 0x%x\n", curclust);
817 debug("Invalid FAT entry\n");
820 actsize = bytesperclust;
821 curclust = endclust = newclust;
828 static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
829 const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
831 if (mydata->fatsize == 32)
833 cpu_to_le16((start_cluster & 0xffff0000) >> 16);
834 dentptr->start = cpu_to_le16(start_cluster & 0xffff);
835 dentptr->size = cpu_to_le32(size);
837 dentptr->attr = attr;
839 set_name(dentptr, filename);
843 * Check whether adding a file makes the file system to
844 * exceed the size of the block device
845 * Return -1 when overflow occurs, otherwise return 0
847 static int check_overflow(fsdata *mydata, __u32 clustnum, unsigned long size)
849 __u32 startsect, sect_num;
852 startsect = mydata->data_begin +
853 clustnum * mydata->clust_size;
855 startsect = mydata->rootdir_sect;
858 sect_num = size / mydata->sect_size;
859 if (size % mydata->sect_size)
862 if (startsect + sect_num > cur_part_info.start + total_sector)
869 * Check if adding several entries exceed one cluster boundary
871 static int is_next_clust(fsdata *mydata, dir_entry *dentptr)
875 cur_position = (__u8 *)dentptr - get_dentfromdir_block;
877 if (cur_position >= mydata->clust_size * mydata->sect_size)
883 static dir_entry *empty_dentptr;
885 * Find a directory entry based on filename or start cluster number
886 * If the directory entry is not found,
887 * the new position for writing a directory entry will be returned
889 static dir_entry *find_directory_entry(fsdata *mydata, int startsect,
890 char *filename, dir_entry *retdent, __u32 start)
892 __u32 curclust = (startsect - mydata->data_begin) / mydata->clust_size;
894 debug("get_dentfromdir: %s\n", filename);
901 if (get_cluster(mydata, curclust, get_dentfromdir_block,
902 mydata->clust_size * mydata->sect_size) != 0) {
903 printf("Error: reading directory block\n");
907 dentptr = (dir_entry *)get_dentfromdir_block;
909 dir_curclust = curclust;
911 for (i = 0; i < DIRENTSPERCLUST; i++) {
912 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
915 if (dentptr->name[0] == DELETED_FLAG) {
917 if (is_next_clust(mydata, dentptr))
921 if ((dentptr->attr & ATTR_VOLUME)) {
922 #ifdef CONFIG_SUPPORT_VFAT
923 if ((dentptr->attr & ATTR_VFAT) &&
924 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
925 get_long_file_name(mydata, curclust,
926 get_dentfromdir_block,
928 debug("vfatname: |%s|\n", l_name);
932 /* Volume label or VFAT entry */
934 if (is_next_clust(mydata, dentptr))
939 if (dentptr->name[0] == 0) {
940 debug("Dentname == NULL - %d\n", i);
941 empty_dentptr = dentptr;
945 get_name(dentptr, s_name);
947 if (strcmp(filename, s_name)
948 && strcmp(filename, l_name)) {
949 debug("Mismatch: |%s|%s|\n",
952 if (is_next_clust(mydata, dentptr))
957 memcpy(retdent, dentptr, sizeof(dir_entry));
959 debug("DentName: %s", s_name);
960 debug(", start: 0x%x", START(dentptr));
961 debug(", size: 0x%x %s\n",
962 FAT2CPU32(dentptr->size),
963 (dentptr->attr & ATTR_DIR) ?
969 curclust = get_fatent_value(mydata, dir_curclust);
970 if ((curclust >= 0xffffff8) || (curclust >= 0xfff8)) {
971 empty_dentptr = dentptr;
974 if (CHECK_CLUST(curclust, mydata->fatsize)) {
975 debug("curclust: 0x%x\n", curclust);
976 debug("Invalid FAT entry\n");
984 static int do_fat_write(const char *filename, void *buffer,
987 dir_entry *dentptr, *retdent;
993 fsdata *mydata = &datablock;
995 int ret = -1, name_len;
996 char l_filename[VFAT_MAXLEN_BYTES];
997 int write_size = size;
1001 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
1002 debug("error: reading boot sector\n");
1006 total_sector = bs.total_sect;
1007 if (total_sector == 0)
1008 total_sector = cur_part_info.size;
1010 if (mydata->fatsize == 32)
1011 mydata->fatlength = bs.fat32_length;
1013 mydata->fatlength = bs.fat_length;
1015 mydata->fat_sect = bs.reserved;
1017 cursect = mydata->rootdir_sect
1018 = mydata->fat_sect + mydata->fatlength * bs.fats;
1019 num_of_fats = bs.fats;
1021 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
1022 mydata->clust_size = bs.cluster_size;
1024 if (mydata->fatsize == 32) {
1025 mydata->data_begin = mydata->rootdir_sect -
1026 (mydata->clust_size * 2);
1030 rootdir_size = ((bs.dir_entries[1] * (int)256 +
1031 bs.dir_entries[0]) *
1032 sizeof(dir_entry)) /
1034 mydata->data_begin = mydata->rootdir_sect +
1036 (mydata->clust_size * 2);
1039 mydata->fatbufnum = -1;
1040 mydata->fatbuf = malloc(FATBUFSIZE);
1041 if (mydata->fatbuf == NULL) {
1042 debug("Error: allocating memory\n");
1046 if (disk_read(cursect,
1047 (mydata->fatsize == 32) ?
1048 (mydata->clust_size) :
1049 PREFETCH_BLOCKS, do_fat_read_block) < 0) {
1050 debug("Error: reading rootdir block\n");
1053 dentptr = (dir_entry *) do_fat_read_block;
1055 name_len = strlen(filename);
1056 if (name_len >= VFAT_MAXLEN_BYTES)
1057 name_len = VFAT_MAXLEN_BYTES - 1;
1059 memcpy(l_filename, filename, name_len);
1060 l_filename[name_len] = 0; /* terminate the string */
1061 downcase(l_filename);
1063 startsect = mydata->rootdir_sect;
1064 retdent = find_directory_entry(mydata, startsect,
1065 l_filename, dentptr, 0);
1067 /* Update file size and start_cluster in a directory entry */
1068 retdent->size = cpu_to_le32(size);
1069 start_cluster = FAT2CPU16(retdent->start);
1070 if (mydata->fatsize == 32)
1072 (FAT2CPU16(retdent->starthi) << 16);
1074 ret = check_overflow(mydata, start_cluster, size);
1076 printf("Error: %ld overflow\n", size);
1080 ret = clear_fatent(mydata, start_cluster);
1082 printf("Error: clearing FAT entries\n");
1086 ret = set_contents(mydata, retdent, buffer, size);
1088 printf("Error: writing contents\n");
1092 debug("attempt to write 0x%x bytes\n", write_size);
1094 /* Flush fat buffer */
1095 ret = flush_fat_buffer(mydata);
1097 printf("Error: flush fat buffer\n");
1101 /* Write directory table to device */
1102 ret = set_cluster(mydata, dir_curclust,
1103 get_dentfromdir_block,
1104 mydata->clust_size * mydata->sect_size);
1106 printf("Error: writing directory entry\n");
1110 /* Set short name to set alias checksum field in dir_slot */
1111 set_name(empty_dentptr, filename);
1112 fill_dir_slot(mydata, &empty_dentptr, filename);
1114 ret = start_cluster = find_empty_cluster(mydata);
1116 printf("Error: finding empty cluster\n");
1120 ret = check_overflow(mydata, start_cluster, size);
1122 printf("Error: %ld overflow\n", size);
1126 /* Set attribute as archieve for regular file */
1127 fill_dentry(mydata, empty_dentptr, filename,
1128 start_cluster, size, 0x20);
1130 ret = set_contents(mydata, empty_dentptr, buffer, size);
1132 printf("Error: writing contents\n");
1136 debug("attempt to write 0x%x bytes\n", write_size);
1138 /* Flush fat buffer */
1139 ret = flush_fat_buffer(mydata);
1141 printf("Error: flush fat buffer\n");
1145 /* Write directory table to device */
1146 ret = set_cluster(mydata, dir_curclust,
1147 get_dentfromdir_block,
1148 mydata->clust_size * mydata->sect_size);
1150 printf("Error: writing directory entry\n");
1156 free(mydata->fatbuf);
1157 return ret < 0 ? ret : write_size;
1160 int file_fat_write(const char *filename, void *buffer, unsigned long maxsize)
1162 printf("writing %s\n", filename);
1163 return do_fat_write(filename, buffer, maxsize);
1166 static int delete_fatent(fsdata *mydata, __u32 entry)
1171 fat_val = get_fatent_value(mydata, entry);
1173 set_fatent_value(mydata, entry, 0);
1178 if (fat_val == 0xfffffff || fat_val == 0xffff) {
1185 /* Flush fat buffer */
1186 if (flush_fat_buffer(mydata) < 0)
1192 static dir_entry *delete_directory_entry(fsdata *mydata, int startsect,
1193 char *filename, dir_entry *retdent, __u32 start)
1195 __u32 curclust = (startsect - mydata->data_begin) / mydata->clust_size;
1196 __u32 find_curclust = curclust;
1197 #ifdef CONFIG_SUPPORT_VFAT
1198 dir_entry *find_dentptr = NULL;
1201 debug("get_dentfromdir: %s\n", filename);
1209 if (get_cluster(mydata, curclust, get_dentfromdir_block,
1210 mydata->clust_size * mydata->sect_size) != 0) {
1211 printf("Error: reading directory block\n");
1215 dentptr = (dir_entry *)get_dentfromdir_block;
1217 dir_curclust = curclust;
1219 for (i = 0; i < DIRENTSPERCLUST; i++) {
1220 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
1223 if (dentptr->name[0] == DELETED_FLAG) {
1225 if (is_next_clust(mydata, dentptr))
1229 if ((dentptr->attr & ATTR_VOLUME)) {
1230 #ifdef CONFIG_SUPPORT_VFAT
1231 if ((dentptr->attr & ATTR_VFAT) &&
1232 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
1233 find_dentptr = dentptr;
1234 find_curclust = curclust;
1235 get_long_file_name(mydata, curclust,
1236 get_dentfromdir_block,
1238 debug("vfatname: |%s|\n", l_name);
1242 /* Volume label or VFAT entry */
1244 if (is_next_clust(mydata, dentptr))
1249 if (dentptr->name[0] == 0) {
1250 debug("Dentname == NULL - %d\n", i);
1251 empty_dentptr = dentptr;
1255 get_name(dentptr, s_name);
1257 if (strcmp(filename, s_name)
1258 && strcmp(filename, l_name)) {
1259 debug("Mismatch: |%s|%s|\n",
1262 if (is_next_clust(mydata, dentptr))
1267 memcpy(retdent, dentptr, sizeof(dir_entry));
1268 #ifdef CONFIG_SUPPORT_VFAT
1269 delete_long_file_name(mydata, find_curclust,
1270 get_dentfromdir_block,
1273 dentptr->name[0] = DELETED_FLAG;
1275 if (set_cluster(mydata, dir_curclust,
1276 get_dentfromdir_block,
1277 mydata->clust_size * mydata->sect_size) != 0) {
1278 printf("error: wrinting directory entry\n");
1281 debug("DentName: %s", s_name);
1282 debug(", start: 0x%x", START(dentptr));
1283 debug(", size: 0x%x %s\n",
1284 FAT2CPU32(dentptr->size),
1285 (dentptr->attr & ATTR_DIR) ?
1291 curclust = get_fatent_value(mydata, dir_curclust);
1292 if ((curclust >= 0xffffff8) || (curclust >= 0xfff8)) {
1293 empty_dentptr = dentptr;
1296 if (CHECK_CLUST(curclust, mydata->fatsize)) {
1297 debug("curclust: 0x%x\n", curclust);
1298 debug("Invalid FAT entry\n");
1306 static int do_fat_rm(const char *filename)
1308 dir_entry *dentptr, *retdent;
1310 __u32 start_cluster;
1312 volume_info volinfo;
1314 fsdata *mydata = &datablock;
1316 int ret = -1, name_len;
1317 char l_filename[VFAT_MAXLEN_BYTES];
1321 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
1322 debug("error: reading boot sector\n");
1326 total_sector = bs.total_sect;
1327 if (total_sector == 0)
1328 total_sector = cur_part_info.size;
1330 if (mydata->fatsize == 32)
1331 mydata->fatlength = bs.fat32_length;
1333 mydata->fatlength = bs.fat_length;
1335 mydata->fat_sect = bs.reserved;
1337 cursect = mydata->rootdir_sect
1338 = mydata->fat_sect + mydata->fatlength * bs.fats;
1339 num_of_fats = bs.fats;
1341 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
1342 mydata->clust_size = bs.cluster_size;
1344 if (mydata->fatsize == 32) {
1345 mydata->data_begin = mydata->rootdir_sect -
1346 (mydata->clust_size * 2);
1350 rootdir_size = ((bs.dir_entries[1] * (int)256 +
1351 bs.dir_entries[0]) *
1352 sizeof(dir_entry)) /
1354 mydata->data_begin = mydata->rootdir_sect +
1356 (mydata->clust_size * 2);
1359 mydata->fatbufnum = -1;
1360 mydata->fatbuf = malloc(FATBUFSIZE);
1361 if (mydata->fatbuf == NULL) {
1362 debug("Error: allocating memory\n");
1366 if (disk_read(cursect,
1367 (mydata->fatsize == 32) ?
1368 (mydata->clust_size) :
1369 PREFETCH_BLOCKS, do_fat_read_block) < 0) {
1370 debug("Error: reading rootdir block\n");
1373 dentptr = (dir_entry *) do_fat_read_block;
1375 name_len = strlen(filename);
1376 if (name_len >= VFAT_MAXLEN_BYTES)
1377 name_len = VFAT_MAXLEN_BYTES - 1;
1379 memcpy(l_filename, filename, name_len);
1380 l_filename[name_len] = 0; /* terminate the string */
1381 downcase(l_filename);
1383 startsect = mydata->rootdir_sect;
1384 retdent = delete_directory_entry(mydata, startsect,
1385 l_filename, dentptr, 0);
1388 /* Update file size and start_cluster in a directory entry */
1389 start_cluster = FAT2CPU16(retdent->start);
1390 if (mydata->fatsize == 32)
1392 (FAT2CPU16(retdent->starthi) << 16);
1394 ret = delete_fatent(mydata, start_cluster);
1396 printf("Error: clearing FAT entries\n");
1400 /* Write directory table to device */
1401 ret = set_cluster(mydata, dir_curclust,
1402 get_dentfromdir_block,
1403 mydata->clust_size * mydata->sect_size);
1405 printf("Error: writing directory entry\n");
1409 printf("Error: %s not found\n", filename);
1414 free(mydata->fatbuf);
1415 return ret < 0 ? ret : 0;
1418 int file_fat_rm(const char *filename)
1420 printf("deleting %s\n", filename);
1421 return do_fat_rm(filename);