fs: fat: remove build warnings
[profile/mobile/platform/kernel/u-boot-tm1.git] / fs / fat / fat_write.c
1 /*
2  * fat_write.c
3  *
4  * R/W (V)FAT 12/16/32 filesystem implementation by Donggeun Kim
5  *
6  * See file CREDITS for list of people who contributed to this
7  * project.
8  *
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.
13  *
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.
18  *
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,
22  * MA 02111-1307 USA
23  */
24
25 #include <common.h>
26 #include <command.h>
27 #include <config.h>
28 #include <fat.h>
29 #include <asm/byteorder.h>
30 #include <part.h>
31 #include "fat.c"
32
33 static void uppercase(char *str, int len)
34 {
35         int i;
36
37         for (i = 0; i < len; i++) {
38                 TOUPPER(*str);
39                 str++;
40         }
41 }
42
43 static int total_sector;
44 static int disk_write(__u32 block, __u32 nr_blocks, void *buf)
45 {
46         if (!cur_dev || !cur_dev->block_write)
47                 return -1;
48
49         if (cur_part_info.start + block + nr_blocks >
50                 cur_part_info.start + total_sector) {
51                 printf("error: overflow occurs\n");
52                 return -1;
53         }
54
55         return cur_dev->block_write(cur_dev->dev,
56                         cur_part_info.start + block, nr_blocks, buf);
57 }
58
59 /*
60  * Set short name in directory entry
61  */
62 static void set_name(dir_entry *dirent, const char *filename)
63 {
64         char s_name[VFAT_MAXLEN_BYTES];
65         char *period;
66         int period_location, len, i, ext_num;
67
68         if (filename == NULL)
69                 return;
70
71         len = strlen(filename);
72         if (len == 0)
73                 return;
74
75         memcpy(s_name, filename, len);
76         uppercase(s_name, len);
77
78         period = strchr(s_name, '.');
79         if (period == NULL) {
80                 period_location = len;
81                 ext_num = 0;
82         } else {
83                 period_location = period - s_name;
84                 ext_num = len - period_location - 1;
85         }
86
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);
94         } else {
95                 memcpy(dirent->name, s_name, 6);
96                 dirent->name[6] = '~';
97                 dirent->name[7] = '1';
98         }
99
100         if (ext_num < 3) {
101                 memcpy(dirent->ext, s_name + period_location + 1, ext_num);
102                 for (i = ext_num; i < 3; i++)
103                         dirent->ext[i] = ' ';
104         } else
105                 memcpy(dirent->ext, s_name + period_location + 1, 3);
106
107         debug("name : %s\n", dirent->name);
108         debug("ext : %s\n", dirent->ext);
109 }
110
111 static __u8 num_of_fats;
112 /*
113  * Write fat buffer into block device
114  */
115 static int flush_fat_buffer(fsdata *mydata)
116 {
117         int getsize = FATBUFBLOCKS;
118         __u32 fatlength = mydata->fatlength;
119         __u8 *bufptr = mydata->fatbuf;
120         __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
121
122         fatlength *= mydata->sect_size;
123         startblock += mydata->fat_sect;
124
125         if (getsize > fatlength)
126                 getsize = fatlength;
127
128         /* Write FAT buf */
129         if (disk_write(startblock, getsize, bufptr) < 0) {
130                 debug("error: writing FAT blocks\n");
131                 return -1;
132         }
133
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");
139                         return -1;
140                 }
141         }
142
143         return 0;
144 }
145
146 /*
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.
150  */
151 static __u32 get_fatent_value(fsdata *mydata, __u32 entry)
152 {
153         __u32 bufnum;
154         __u32 off16, offset;
155         __u32 ret = 0x00;
156         __u16 val1, val2;
157
158         switch (mydata->fatsize) {
159         case 32:
160                 bufnum = entry / FAT32BUFSIZE;
161                 offset = entry - bufnum * FAT32BUFSIZE;
162                 break;
163         case 16:
164                 bufnum = entry / FAT16BUFSIZE;
165                 offset = entry - bufnum * FAT16BUFSIZE;
166                 break;
167         case 12:
168                 bufnum = entry / FAT12BUFSIZE;
169                 offset = entry - bufnum * FAT12BUFSIZE;
170                 break;
171
172         default:
173                 /* Unsupported FAT size */
174                 return ret;
175         }
176
177         debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
178                mydata->fatsize, entry, entry, offset, offset);
179
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;
186
187                 if (getsize > fatlength)
188                         getsize = fatlength;
189
190                 fatlength *= mydata->sect_size; /* We want it in bytes now */
191                 startblock += mydata->fat_sect; /* Offset from start of disk */
192
193                 /* Write back the fatbuf to the disk */
194                 if (mydata->fatbufnum != -1) {
195                         if (flush_fat_buffer(mydata) < 0)
196                                 return -1;
197                 }
198
199                 if (disk_read(startblock, getsize, bufptr) < 0) {
200                         debug("Error reading FAT blocks\n");
201                         return ret;
202                 }
203                 mydata->fatbufnum = bufnum;
204         }
205
206         /* Get the actual entry from the table */
207         switch (mydata->fatsize) {
208         case 32:
209                 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
210                 break;
211         case 16:
212                 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
213                 break;
214         case 12:
215                 off16 = (offset * 3) / 4;
216
217                 switch (offset & 0x3) {
218                 case 0:
219                         ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
220                         ret &= 0xfff;
221                         break;
222                 case 1:
223                         val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
224                         val1 &= 0xf000;
225                         val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
226                         val2 &= 0x00ff;
227                         ret = (val2 << 4) | (val1 >> 12);
228                         break;
229                 case 2:
230                         val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
231                         val1 &= 0xff00;
232                         val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
233                         val2 &= 0x000f;
234                         ret = (val2 << 8) | (val1 >> 8);
235                         break;
236                 case 3:
237                         ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
238                         ret = (ret & 0xfff0) >> 4;
239                         break;
240                 default:
241                         break;
242                 }
243                 break;
244         }
245         debug("FAT%d: ret: %08x, entry: %08x, offset: %04x\n",
246                mydata->fatsize, ret, entry, offset);
247
248         return ret;
249 }
250
251 #ifdef CONFIG_SUPPORT_VFAT
252 /*
253  * Set the file name information from 'name' into 'slotptr',
254  */
255 static int str2slot(dir_slot *slotptr, const char *name, int *idx)
256 {
257         int j, end_idx = 0;
258
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;
263                         end_idx++;
264                         goto name0_4;
265                 }
266                 slotptr->name0_4[j] = name[*idx];
267                 (*idx)++;
268                 end_idx++;
269         }
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;
274                         end_idx++;
275                         goto name5_10;
276                 }
277                 slotptr->name5_10[j] = name[*idx];
278                 (*idx)++;
279                 end_idx++;
280         }
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;
285                         end_idx++;
286                         goto name11_12;
287                 }
288                 slotptr->name11_12[j] = name[*idx];
289                 (*idx)++;
290                 end_idx++;
291         }
292
293         if (name[*idx] == 0x00)
294                 return 1;
295
296         return 0;
297 /* Not used characters are filled with 0xff 0xff */
298 name0_4:
299         for (; end_idx < 5; end_idx++) {
300                 slotptr->name0_4[end_idx * 2] = 0xff;
301                 slotptr->name0_4[end_idx * 2 + 1] = 0xff;
302         }
303         end_idx = 5;
304 name5_10:
305         end_idx -= 5;
306         for (; end_idx < 6; end_idx++) {
307                 slotptr->name5_10[end_idx * 2] = 0xff;
308                 slotptr->name5_10[end_idx * 2 + 1] = 0xff;
309         }
310         end_idx = 11;
311 name11_12:
312         end_idx -= 11;
313         for (; end_idx < 2; end_idx++) {
314                 slotptr->name11_12[end_idx * 2] = 0xff;
315                 slotptr->name11_12[end_idx * 2 + 1] = 0xff;
316         }
317
318         return 1;
319 }
320
321 static int is_next_clust(fsdata *mydata, dir_entry *dentptr);
322 static void flush_dir_table(fsdata *mydata, dir_entry **dentptr);
323
324 /*
325  * Fill dir_slot entries with appropriate name, id, and attr
326  * The real directory entry is returned by 'dentptr'
327  */
328 static void
329 fill_dir_slot(fsdata *mydata, dir_entry **dentptr, const char *l_name)
330 {
331         dir_slot *slotptr = (dir_slot *)get_vfatname_block;
332         __u8 counter = 0, checksum;
333         int idx = 0, ret;
334         char s_name[16];
335
336         /* Get short file name and checksum value */
337         strncpy(s_name, (*dentptr)->name, 16);
338         checksum = mkcksum(s_name);
339
340         do {
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;
346                 slotptr++;
347         } while (ret == 0);
348
349         slotptr--;
350         slotptr->id |= LAST_LONG_ENTRY_MASK;
351
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);
356                 }
357                 memcpy(*dentptr, slotptr, sizeof(dir_slot));
358                 (*dentptr)++;
359                 slotptr--;
360                 counter--;
361         }
362
363         if (is_next_clust(mydata, *dentptr)) {
364                 /* A new cluster is allocated for directory table */
365                 flush_dir_table(mydata, dentptr);
366         }
367 }
368
369 static __u32 dir_curclust;
370
371 /*
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
374  * into 'retdent'
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.
379  */
380 static int
381 get_long_file_name(fsdata *mydata, int curclust, __u8 *cluster,
382               dir_entry **retdent, char *l_name)
383 {
384         dir_entry *realdent;
385         dir_slot *slotptr = (dir_slot *)(*retdent);
386         dir_slot *slotptr2 = NULL;
387         __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
388                                                         PREFETCH_BLOCKS :
389                                                         mydata->clust_size);
390         __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
391         int idx = 0, cur_position = 0;
392
393         if (counter > VFAT_MAXSEQ) {
394                 debug("Error: VFAT name is too long\n");
395                 return -1;
396         }
397
398         while ((__u8 *)slotptr < buflimit) {
399                 if (counter == 0)
400                         break;
401                 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
402                         return -1;
403                 slotptr++;
404                 counter--;
405         }
406
407         if ((__u8 *)slotptr >= buflimit) {
408                 if (curclust == 0)
409                         return -1;
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");
414                         return -1;
415                 }
416
417                 dir_curclust = curclust;
418
419                 if (get_cluster(mydata, curclust, get_vfatname_block,
420                                 mydata->clust_size * mydata->sect_size) != 0) {
421                         debug("Error: reading directory block\n");
422                         return -1;
423                 }
424
425                 slotptr2 = (dir_slot *)get_vfatname_block;
426                 while (counter > 0) {
427                         if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
428                             & 0xff) != counter)
429                                 return -1;
430                         slotptr2++;
431                         counter--;
432                 }
433
434                 /* Save the real directory entry */
435                 realdent = (dir_entry *)slotptr2;
436                 while ((__u8 *)slotptr2 > get_vfatname_block) {
437                         slotptr2--;
438                         slot2str(slotptr2, l_name, &idx);
439                 }
440         } else {
441                 /* Save the real directory entry */
442                 realdent = (dir_entry *)slotptr;
443         }
444
445         do {
446                 slotptr--;
447                 if (slot2str(slotptr, l_name, &idx))
448                         break;
449         } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
450
451         l_name[idx] = '\0';
452         if (*l_name == DELETED_FLAG)
453                 *l_name = '\0';
454         else if (*l_name == aRING)
455                 *l_name = DELETED_FLAG;
456         downcase(l_name);
457
458         /* Return the real directory entry */
459         *retdent = realdent;
460
461         if (slotptr2) {
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];
466         }
467
468         return 0;
469 }
470
471 static int
472 delete_long_file_name(fsdata *mydata, int curclust, __u8 *cluster,
473               dir_entry **retdent)
474 {
475         dir_entry *realdent;
476         dir_slot *slotptr = (dir_slot *)(*retdent);
477         dir_slot *slotptr2 = NULL;
478         __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
479                                                         PREFETCH_BLOCKS :
480                                                         mydata->clust_size);
481         __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
482
483         if (counter > VFAT_MAXSEQ) {
484                 debug("Error: VFAT name is too long\n");
485                 return -1;
486         }
487
488         while ((__u8 *)slotptr < buflimit) {
489                 if (counter == 0)
490                         break;
491                 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
492                         return -1;
493                 slotptr->id = DELETED_FLAG;
494                 slotptr++;
495                 counter--;
496         }
497
498         if ((__u8 *)slotptr >= buflimit) {
499                 if (curclust == 0)
500                         return -1;
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");
505                         return -1;
506                 }
507
508                 dir_curclust = curclust;
509
510                 if (get_cluster(mydata, curclust, get_vfatname_block,
511                                 mydata->clust_size * mydata->sect_size) != 0) {
512                         debug("Error: reading directory block\n");
513                         return -1;
514                 }
515
516                 slotptr2 = (dir_slot *)get_vfatname_block;
517                 while (counter > 0) {
518                         if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
519                             & 0xff) != counter)
520                                 return -1;
521                         slotptr2->id = DELETED_FLAG;
522                         slotptr2++;
523                         counter--;
524                 }
525
526                 /* Save the real directory entry */
527                 realdent = (dir_entry *)slotptr2;
528         } else {
529                 /* Save the real directory entry */
530                 realdent = (dir_entry *)slotptr;
531         }
532         realdent->name[0] = DELETED_FLAG;
533
534         return 0;
535 }
536 #endif
537
538 /*
539  * Set the entry at index 'entry' in a FAT (16/32) table.
540  */
541 static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
542 {
543         __u32 bufnum, offset;
544
545         switch (mydata->fatsize) {
546         case 32:
547                 bufnum = entry / FAT32BUFSIZE;
548                 offset = entry - bufnum * FAT32BUFSIZE;
549                 break;
550         case 16:
551                 bufnum = entry / FAT16BUFSIZE;
552                 offset = entry - bufnum * FAT16BUFSIZE;
553                 break;
554         default:
555                 /* Unsupported FAT size */
556                 return -1;
557         }
558
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;
565
566                 fatlength *= mydata->sect_size;
567                 startblock += mydata->fat_sect;
568
569                 if (getsize > fatlength)
570                         getsize = fatlength;
571
572                 if (mydata->fatbufnum != -1) {
573                         if (flush_fat_buffer(mydata) < 0)
574                                 return -1;
575                 }
576
577                 if (disk_read(startblock, getsize, bufptr) < 0) {
578                         debug("Error reading FAT blocks\n");
579                         return -1;
580                 }
581                 mydata->fatbufnum = bufnum;
582         }
583
584         /* Set the actual entry */
585         switch (mydata->fatsize) {
586         case 32:
587                 ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
588                 break;
589         case 16:
590                 ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
591                 break;
592         default:
593                 return -1;
594         }
595
596         return 0;
597 }
598
599 /*
600  * Determine the entry value at index 'entry' in a FAT (16/32) table
601  */
602 static __u32 determine_fatent(fsdata *mydata, __u32 entry)
603 {
604         __u32 next_fat, next_entry = entry + 1;
605
606         while (1) {
607                 next_fat = get_fatent_value(mydata, next_entry);
608                 if (next_fat == 0) {
609                         set_fatent_value(mydata, entry, next_entry);
610                         break;
611                 }
612                 next_entry++;
613         }
614         debug("FAT%d: entry: %08x, entry_value: %04x\n",
615                mydata->fatsize, entry, next_entry);
616
617         return next_entry;
618 }
619
620 /*
621  * Write at most 'size' bytes from 'buffer' into the specified cluster.
622  * Return 0 on success, -1 otherwise.
623  */
624 static int
625 set_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer,
626              unsigned long size)
627 {
628         int idx = 0;
629         __u32 startsect;
630
631         if (clustnum > 0)
632                 startsect = mydata->data_begin +
633                                 clustnum * mydata->clust_size;
634         else
635                 startsect = mydata->rootdir_sect;
636
637         debug("clustnum: %d, startsect: %d\n", clustnum, startsect);
638
639         if (disk_write(startsect, size / mydata->sect_size, buffer) < 0) {
640                 debug("Error writing data\n");
641                 return -1;
642         }
643
644         if (size % mydata->sect_size) {
645                 __u8 tmpbuf[mydata->sect_size];
646
647                 idx = size / mydata->sect_size;
648                 buffer += idx * mydata->sect_size;
649                 memcpy(tmpbuf, buffer, size % mydata->sect_size);
650
651                 if (disk_write(startsect + idx, 1, tmpbuf) < 0) {
652                         debug("Error writing data\n");
653                         return -1;
654                 }
655
656                 return 0;
657         }
658
659         return 0;
660 }
661
662 /*
663  * Find the first empty cluster
664  */
665 static int find_empty_cluster(fsdata *mydata)
666 {
667         __u32 fat_val, entry = 3;
668
669         while (1) {
670                 fat_val = get_fatent_value(mydata, entry);
671                 if (fat_val == 0)
672                         break;
673                 entry++;
674         }
675
676         return entry;
677 }
678
679 /*
680  * Write directory entries in 'get_dentfromdir_block' to block device
681  */
682 static void flush_dir_table(fsdata *mydata, dir_entry **dentptr)
683 {
684         int dir_newclust = 0;
685
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");
690                 return;
691         }
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);
698
699         dir_curclust = dir_newclust;
700
701         if (flush_fat_buffer(mydata) < 0)
702                 return;
703
704         memset(get_dentfromdir_block, 0x00,
705                 mydata->clust_size * mydata->sect_size);
706
707         *dentptr = (dir_entry *) get_dentfromdir_block;
708 }
709
710 /*
711  * Set empty cluster from 'entry' to the end of a file
712  */
713 static int clear_fatent(fsdata *mydata, __u32 entry)
714 {
715         __u32 fat_val;
716
717         while (1) {
718                 fat_val = get_fatent_value(mydata, entry);
719                 if (fat_val != 0)
720                         set_fatent_value(mydata, entry, 0);
721                 else
722                         break;
723
724                 if (fat_val == 0xfffffff || fat_val == 0xffff)
725                         break;
726
727                 entry = fat_val;
728         }
729
730         /* Flush fat buffer */
731         if (flush_fat_buffer(mydata) < 0)
732                 return -1;
733
734         return 0;
735 }
736
737 /*
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.
741  */
742 static int
743 set_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
744               unsigned long maxsize)
745 {
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;
751
752         debug("Filesize: %ld bytes\n", filesize);
753
754         if (maxsize > 0 && filesize > maxsize)
755                 filesize = maxsize;
756
757         debug("%ld bytes\n", filesize);
758
759         actsize = bytesperclust;
760         endclust = curclust;
761         do {
762                 /* search for consecutive clusters */
763                 while (actsize < filesize) {
764                         newclust = determine_fatent(mydata, endclust);
765
766                         if ((newclust - 1) != endclust)
767                                 goto getit;
768
769                         if (CHECK_CLUST(newclust, mydata->fatsize)) {
770                                 debug("curclust: 0x%x\n", newclust);
771                                 debug("Invalid FAT entry\n");
772                                 return gotsize;
773                         }
774                         endclust = newclust;
775                         actsize += bytesperclust;
776                 }
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");
782                         return -1;
783                 }
784
785                 /* set remaining bytes */
786                 gotsize += (int)actsize;
787                 filesize -= actsize;
788                 buffer += actsize;
789                 actsize = filesize;
790
791                 if (set_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
792                         debug("error: writing cluster\n");
793                         return -1;
794                 }
795                 gotsize += actsize;
796
797                 /* Mark end of file in FAT */
798                 if (mydata->fatsize == 16)
799                         newclust = 0xffff;
800                 else if (mydata->fatsize == 32)
801                         newclust = 0xfffffff;
802                 set_fatent_value(mydata, endclust, newclust);
803
804                 return gotsize;
805 getit:
806                 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
807                         debug("error: writing cluster\n");
808                         return -1;
809                 }
810                 gotsize += (int)actsize;
811                 filesize -= actsize;
812                 buffer += actsize;
813
814                 if (CHECK_CLUST(curclust, mydata->fatsize)) {
815                         debug("curclust: 0x%x\n", curclust);
816                         debug("Invalid FAT entry\n");
817                         return gotsize;
818                 }
819                 actsize = bytesperclust;
820                 curclust = endclust = newclust;
821         } while (1);
822 }
823
824 /*
825  * Fill dir_entry
826  */
827 static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
828         const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
829 {
830         if (mydata->fatsize == 32)
831                 dentptr->starthi =
832                         cpu_to_le16((start_cluster & 0xffff0000) >> 16);
833         dentptr->start = cpu_to_le16(start_cluster & 0xffff);
834         dentptr->size = cpu_to_le32(size);
835
836         dentptr->attr = attr;
837
838         set_name(dentptr, filename);
839 }
840
841 /*
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
845  */
846 static int check_overflow(fsdata *mydata, __u32 clustnum, unsigned long size)
847 {
848         __u32 startsect, sect_num;
849
850         if (clustnum > 0) {
851                 startsect = mydata->data_begin +
852                                 clustnum * mydata->clust_size;
853         } else {
854                 startsect = mydata->rootdir_sect;
855         }
856
857         sect_num = size / mydata->sect_size;
858         if (size % mydata->sect_size)
859                 sect_num++;
860
861         if (startsect + sect_num > cur_part_info.start + total_sector)
862                 return -1;
863
864         return 0;
865 }
866
867 /*
868  * Check if adding several entries exceed one cluster boundary
869  */
870 static int is_next_clust(fsdata *mydata, dir_entry *dentptr)
871 {
872         int cur_position;
873
874         cur_position = (__u8 *)dentptr - get_dentfromdir_block;
875
876         if (cur_position >= mydata->clust_size * mydata->sect_size)
877                 return 1;
878         else
879                 return 0;
880 }
881
882 static dir_entry *empty_dentptr;
883 /*
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
887  */
888 static dir_entry *find_directory_entry(fsdata *mydata, int startsect,
889         char *filename, dir_entry *retdent, __u32 start)
890 {
891         __u32 curclust = (startsect - mydata->data_begin) / mydata->clust_size;
892
893         debug("get_dentfromdir: %s\n", filename);
894
895         while (1) {
896                 dir_entry *dentptr;
897
898                 int i;
899
900                 if (get_cluster(mydata, curclust, get_dentfromdir_block,
901                             mydata->clust_size * mydata->sect_size) != 0) {
902                         printf("Error: reading directory block\n");
903                         return NULL;
904                 }
905
906                 dentptr = (dir_entry *)get_dentfromdir_block;
907
908                 dir_curclust = curclust;
909
910                 for (i = 0; i < DIRENTSPERCLUST; i++) {
911                         char s_name[14], l_name[VFAT_MAXLEN_BYTES];
912
913                         l_name[0] = '\0';
914                         if (dentptr->name[0] == DELETED_FLAG) {
915                                 dentptr++;
916                                 if (is_next_clust(mydata, dentptr))
917                                         break;
918                                 continue;
919                         }
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,
926                                                      &dentptr, l_name);
927                                         debug("vfatname: |%s|\n", l_name);
928                                 } else
929 #endif
930                                 {
931                                         /* Volume label or VFAT entry */
932                                         dentptr++;
933                                         if (is_next_clust(mydata, dentptr))
934                                                 break;
935                                         continue;
936                                 }
937                         }
938                         if (dentptr->name[0] == 0) {
939                                 debug("Dentname == NULL - %d\n", i);
940                                 empty_dentptr = dentptr;
941                                 return NULL;
942                         }
943
944                         get_name(dentptr, s_name);
945
946                         if (strcmp(filename, s_name)
947                             && strcmp(filename, l_name)) {
948                                 debug("Mismatch: |%s|%s|\n",
949                                         s_name, l_name);
950                                 dentptr++;
951                                 if (is_next_clust(mydata, dentptr))
952                                         break;
953                                 continue;
954                         }
955
956                         memcpy(retdent, dentptr, sizeof(dir_entry));
957
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) ?
963                               "(DIR)" : "");
964
965                         return dentptr;
966                 }
967
968                 curclust = get_fatent_value(mydata, dir_curclust);
969                 if ((curclust >= 0xffffff8) || (curclust >= 0xfff8)) {
970                         empty_dentptr = dentptr;
971                         return NULL;
972                 }
973                 if (CHECK_CLUST(curclust, mydata->fatsize)) {
974                         debug("curclust: 0x%x\n", curclust);
975                         debug("Invalid FAT entry\n");
976                         return NULL;
977                 }
978         }
979
980         return NULL;
981 }
982
983 static int do_fat_write(const char *filename, void *buffer,
984         unsigned long size)
985 {
986         dir_entry *dentptr, *retdent;
987         __u32 startsect;
988         __u32 start_cluster;
989         boot_sector bs;
990         volume_info volinfo;
991         fsdata datablock;
992         fsdata *mydata = &datablock;
993         int cursect;
994         int ret = -1, name_len;
995         char l_filename[VFAT_MAXLEN_BYTES];
996         int write_size = size;
997
998         dir_curclust = 0;
999
1000         if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
1001                 debug("error: reading boot sector\n");
1002                 return -1;
1003         }
1004
1005         total_sector = bs.total_sect;
1006         if (total_sector == 0)
1007                 total_sector = cur_part_info.size;
1008
1009         if (mydata->fatsize == 32)
1010                 mydata->fatlength = bs.fat32_length;
1011         else
1012                 mydata->fatlength = bs.fat_length;
1013
1014         mydata->fat_sect = bs.reserved;
1015
1016         cursect = mydata->rootdir_sect
1017                 = mydata->fat_sect + mydata->fatlength * bs.fats;
1018         num_of_fats = bs.fats;
1019
1020         mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
1021         mydata->clust_size = bs.cluster_size;
1022
1023         if (mydata->fatsize == 32) {
1024                 mydata->data_begin = mydata->rootdir_sect -
1025                                         (mydata->clust_size * 2);
1026         } else {
1027                 int rootdir_size;
1028
1029                 rootdir_size = ((bs.dir_entries[1]  * (int)256 +
1030                                  bs.dir_entries[0]) *
1031                                  sizeof(dir_entry)) /
1032                                  mydata->sect_size;
1033                 mydata->data_begin = mydata->rootdir_sect +
1034                                         rootdir_size -
1035                                         (mydata->clust_size * 2);
1036         }
1037
1038         mydata->fatbufnum = -1;
1039         mydata->fatbuf = malloc(FATBUFSIZE);
1040         if (mydata->fatbuf == NULL) {
1041                 debug("Error: allocating memory\n");
1042                 return -1;
1043         }
1044
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");
1050                 goto exit;
1051         }
1052         dentptr = (dir_entry *) do_fat_read_block;
1053
1054         name_len = strlen(filename);
1055         if (name_len >= VFAT_MAXLEN_BYTES)
1056                 name_len = VFAT_MAXLEN_BYTES - 1;
1057
1058         memcpy(l_filename, filename, name_len);
1059         l_filename[name_len] = 0; /* terminate the string */
1060         downcase(l_filename);
1061
1062         startsect = mydata->rootdir_sect;
1063         retdent = find_directory_entry(mydata, startsect,
1064                                 l_filename, dentptr, 0);
1065         if (retdent) {
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)
1070                         start_cluster |=
1071                                 (FAT2CPU16(retdent->starthi) << 16);
1072
1073                 ret = check_overflow(mydata, start_cluster, size);
1074                 if (ret) {
1075                         printf("Error: %ld overflow\n", size);
1076                         goto exit;
1077                 }
1078
1079                 ret = clear_fatent(mydata, start_cluster);
1080                 if (ret) {
1081                         printf("Error: clearing FAT entries\n");
1082                         goto exit;
1083                 }
1084
1085                 ret = set_contents(mydata, retdent, buffer, size);
1086                 if (ret < 0) {
1087                         printf("Error: writing contents\n");
1088                         goto exit;
1089                 }
1090                 write_size = ret;
1091                 debug("attempt to write 0x%x bytes\n", write_size);
1092
1093                 /* Flush fat buffer */
1094                 ret = flush_fat_buffer(mydata);
1095                 if (ret) {
1096                         printf("Error: flush fat buffer\n");
1097                         goto exit;
1098                 }
1099
1100                 /* Write directory table to device */
1101                 ret = set_cluster(mydata, dir_curclust,
1102                             get_dentfromdir_block,
1103                             mydata->clust_size * mydata->sect_size);
1104                 if (ret) {
1105                         printf("Error: writing directory entry\n");
1106                         goto exit;
1107                 }
1108         } else {
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);
1112
1113                 ret = start_cluster = find_empty_cluster(mydata);
1114                 if (ret < 0) {
1115                         printf("Error: finding empty cluster\n");
1116                         goto exit;
1117                 }
1118
1119                 ret = check_overflow(mydata, start_cluster, size);
1120                 if (ret) {
1121                         printf("Error: %ld overflow\n", size);
1122                         goto exit;
1123                 }
1124
1125                 /* Set attribute as archieve for regular file */
1126                 fill_dentry(mydata, empty_dentptr, filename,
1127                         start_cluster, size, 0x20);
1128
1129                 ret = set_contents(mydata, empty_dentptr, buffer, size);
1130                 if (ret < 0) {
1131                         printf("Error: writing contents\n");
1132                         goto exit;
1133                 }
1134                 write_size = ret;
1135                 debug("attempt to write 0x%x bytes\n", write_size);
1136
1137                 /* Flush fat buffer */
1138                 ret = flush_fat_buffer(mydata);
1139                 if (ret) {
1140                         printf("Error: flush fat buffer\n");
1141                         goto exit;
1142                 }
1143
1144                 /* Write directory table to device */
1145                 ret = set_cluster(mydata, dir_curclust,
1146                             get_dentfromdir_block,
1147                             mydata->clust_size * mydata->sect_size);
1148                 if (ret) {
1149                         printf("Error: writing directory entry\n");
1150                         goto exit;
1151                 }
1152         }
1153
1154 exit:
1155         free(mydata->fatbuf);
1156         return ret < 0 ? ret : write_size;
1157 }
1158
1159 int file_fat_write(const char *filename, void *buffer, unsigned long maxsize)
1160 {
1161         printf("writing %s\n", filename);
1162         return do_fat_write(filename, buffer, maxsize);
1163 }
1164
1165 static int delete_fatent(fsdata *mydata, __u32 entry)
1166 {
1167         __u32 fat_val;
1168
1169         while (1) {
1170                 fat_val = get_fatent_value(mydata, entry);
1171                 if (fat_val != 0) {
1172                         set_fatent_value(mydata, entry, 0);
1173                 }
1174                 else
1175                         break;
1176
1177                 if (fat_val == 0xfffffff || fat_val == 0xffff) {
1178                         break;
1179                 }
1180
1181                 entry = fat_val;
1182         }
1183
1184         /* Flush fat buffer */
1185         if (flush_fat_buffer(mydata) < 0)
1186                 return -1;
1187
1188         return 0;
1189 }
1190
1191 static dir_entry *delete_directory_entry(fsdata *mydata, int startsect,
1192         char *filename, dir_entry *retdent, __u32 start)
1193 {
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;
1198 #endif
1199
1200         debug("get_dentfromdir: %s\n", filename);
1201
1202         while (1) {
1203                 dir_entry *dentptr;
1204
1205                 int i;
1206
1207                 if (get_cluster(mydata, curclust, get_dentfromdir_block,
1208                             mydata->clust_size * mydata->sect_size) != 0) {
1209                         printf("Error: reading directory block\n");
1210                         return NULL;
1211                 }
1212
1213                 dentptr = (dir_entry *)get_dentfromdir_block;
1214
1215                 dir_curclust = curclust;
1216
1217                 for (i = 0; i < DIRENTSPERCLUST; i++) {
1218                         char s_name[14], l_name[VFAT_MAXLEN_BYTES];
1219
1220                         l_name[0] = '\0';
1221                         if (dentptr->name[0] == DELETED_FLAG) {
1222                                 dentptr++;
1223                                 if (is_next_clust(mydata, dentptr))
1224                                         break;
1225                                 continue;
1226                         }
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,
1235                                                      &dentptr, l_name);
1236                                         debug("vfatname: |%s|\n", l_name);
1237                                 } else
1238 #endif
1239                                 {
1240                                         /* Volume label or VFAT entry */
1241                                         dentptr++;
1242                                         if (is_next_clust(mydata, dentptr))
1243                                                 break;
1244                                         continue;
1245                                 }
1246                         }
1247                         if (dentptr->name[0] == 0) {
1248                                 debug("Dentname == NULL - %d\n", i);
1249                                 empty_dentptr = dentptr;
1250                                 return NULL;
1251                         }
1252
1253                         get_name(dentptr, s_name);
1254
1255                         if (strcmp(filename, s_name)
1256                             && strcmp(filename, l_name)) {
1257                                 debug("Mismatch: |%s|%s|\n",
1258                                         s_name, l_name);
1259                                 dentptr++;
1260                                 if (is_next_clust(mydata, dentptr))
1261                                         break;
1262                                 continue;
1263                         }
1264
1265                         memcpy(retdent, dentptr, sizeof(dir_entry));
1266 #ifdef CONFIG_SUPPORT_VFAT
1267                         delete_long_file_name(mydata, find_curclust,
1268                                         get_dentfromdir_block,
1269                                         &find_dentptr);
1270 #else
1271                         dentptr->name[0] = DELETED_FLAG;
1272 #endif
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");
1277                                 return NULL;
1278                         }
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) ?
1284                               "(DIR)" : "");
1285
1286                         return dentptr;
1287                 }
1288
1289                 curclust = get_fatent_value(mydata, dir_curclust);
1290                 if ((curclust >= 0xffffff8) || (curclust >= 0xfff8)) {
1291                         empty_dentptr = dentptr;
1292                         return NULL;
1293                 }
1294                 if (CHECK_CLUST(curclust, mydata->fatsize)) {
1295                         debug("curclust: 0x%x\n", curclust);
1296                         debug("Invalid FAT entry\n");
1297                         return NULL;
1298                 }
1299         }
1300
1301         return NULL;
1302 }
1303
1304 static int do_fat_rm(const char *filename)
1305 {
1306         dir_entry *dentptr, *retdent;
1307         __u32 startsect;
1308         __u32 start_cluster;
1309         boot_sector bs;
1310         volume_info volinfo;
1311         fsdata datablock;
1312         fsdata *mydata = &datablock;
1313         int cursect;
1314         int ret = -1, name_len;
1315         char l_filename[VFAT_MAXLEN_BYTES];
1316
1317         dir_curclust = 0;
1318
1319         if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
1320                 debug("error: reading boot sector\n");
1321                 return -1;
1322         }
1323
1324         total_sector = bs.total_sect;
1325         if (total_sector == 0)
1326                 total_sector = cur_part_info.size;
1327
1328         if (mydata->fatsize == 32)
1329                 mydata->fatlength = bs.fat32_length;
1330         else
1331                 mydata->fatlength = bs.fat_length;
1332
1333         mydata->fat_sect = bs.reserved;
1334
1335         cursect = mydata->rootdir_sect
1336                 = mydata->fat_sect + mydata->fatlength * bs.fats;
1337         num_of_fats = bs.fats;
1338
1339         mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
1340         mydata->clust_size = bs.cluster_size;
1341
1342         if (mydata->fatsize == 32) {
1343                 mydata->data_begin = mydata->rootdir_sect -
1344                                         (mydata->clust_size * 2);
1345         } else {
1346                 int rootdir_size;
1347
1348                 rootdir_size = ((bs.dir_entries[1]  * (int)256 +
1349                                  bs.dir_entries[0]) *
1350                                  sizeof(dir_entry)) /
1351                                  mydata->sect_size;
1352                 mydata->data_begin = mydata->rootdir_sect +
1353                                         rootdir_size -
1354                                         (mydata->clust_size * 2);
1355         }
1356
1357         mydata->fatbufnum = -1;
1358         mydata->fatbuf = malloc(FATBUFSIZE);
1359         if (mydata->fatbuf == NULL) {
1360                 debug("Error: allocating memory\n");
1361                 return -1;
1362         }
1363
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");
1369                 goto exit;
1370         }
1371         dentptr = (dir_entry *) do_fat_read_block;
1372
1373         name_len = strlen(filename);
1374         if (name_len >= VFAT_MAXLEN_BYTES)
1375                 name_len = VFAT_MAXLEN_BYTES - 1;
1376
1377         memcpy(l_filename, filename, name_len);
1378         l_filename[name_len] = 0; /* terminate the string */
1379         downcase(l_filename);
1380
1381         startsect = mydata->rootdir_sect;
1382         retdent = delete_directory_entry(mydata, startsect,
1383                                 l_filename, dentptr, 0);
1384
1385         if (retdent) {
1386                 /* Update file size and start_cluster in a directory entry */
1387                 start_cluster = FAT2CPU16(retdent->start);
1388                 if (mydata->fatsize == 32)
1389                         start_cluster |=
1390                                 (FAT2CPU16(retdent->starthi) << 16);
1391
1392                 ret = delete_fatent(mydata, start_cluster);
1393                 if (ret) {
1394                         printf("Error: clearing FAT entries\n");
1395                         goto exit;
1396                 }
1397
1398                 /* Write directory table to device */
1399                 ret = set_cluster(mydata, dir_curclust,
1400                             get_dentfromdir_block,
1401                             mydata->clust_size * mydata->sect_size);
1402                 if (ret) {
1403                         printf("Error: writing directory entry\n");
1404                         goto exit;
1405                 }
1406         } else {
1407                 printf("Error: %s not found\n", filename);
1408                 goto exit;
1409         }
1410
1411 exit:
1412         free(mydata->fatbuf);
1413         return ret < 0 ? ret : 0;
1414 }
1415
1416 int file_fat_rm(const char *filename)
1417 {
1418         printf("deleting %s\n", filename);
1419         return do_fat_rm(filename);
1420 }