fs: fat: correct file name normalization
[oweals/u-boot.git] / fs / fat / fat_write.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * fat_write.c
4  *
5  * R/W (V)FAT 12/16/32 filesystem implementation by Donggeun Kim
6  */
7
8 #include <common.h>
9 #include <command.h>
10 #include <config.h>
11 #include <fat.h>
12 #include <asm/byteorder.h>
13 #include <part.h>
14 #include <linux/ctype.h>
15 #include <div64.h>
16 #include <linux/math64.h>
17 #include "fat.c"
18
19 static void uppercase(char *str, int len)
20 {
21         int i;
22
23         for (i = 0; i < len; i++) {
24                 *str = toupper(*str);
25                 str++;
26         }
27 }
28
29 static int total_sector;
30 static int disk_write(__u32 block, __u32 nr_blocks, void *buf)
31 {
32         ulong ret;
33
34         if (!cur_dev)
35                 return -1;
36
37         if (cur_part_info.start + block + nr_blocks >
38                 cur_part_info.start + total_sector) {
39                 printf("error: overflow occurs\n");
40                 return -1;
41         }
42
43         ret = blk_dwrite(cur_dev, cur_part_info.start + block, nr_blocks, buf);
44         if (nr_blocks && ret == 0)
45                 return -1;
46
47         return ret;
48 }
49
50 /*
51  * Set short name in directory entry
52  */
53 static void set_name(dir_entry *dirent, const char *filename)
54 {
55         char s_name[VFAT_MAXLEN_BYTES];
56         char *period;
57         int period_location, len, i, ext_num;
58
59         if (filename == NULL)
60                 return;
61
62         len = strlen(filename);
63         if (len == 0)
64                 return;
65
66         strcpy(s_name, filename);
67         uppercase(s_name, len);
68
69         period = strchr(s_name, '.');
70         if (period == NULL) {
71                 period_location = len;
72                 ext_num = 0;
73         } else {
74                 period_location = period - s_name;
75                 ext_num = len - period_location - 1;
76         }
77
78         /* Pad spaces when the length of file name is shorter than eight */
79         if (period_location < 8) {
80                 memcpy(dirent->name, s_name, period_location);
81                 for (i = period_location; i < 8; i++)
82                         dirent->name[i] = ' ';
83         } else if (period_location == 8) {
84                 memcpy(dirent->name, s_name, period_location);
85         } else {
86                 memcpy(dirent->name, s_name, 6);
87                 dirent->name[6] = '~';
88                 dirent->name[7] = '1';
89         }
90
91         if (ext_num < 3) {
92                 memcpy(dirent->ext, s_name + period_location + 1, ext_num);
93                 for (i = ext_num; i < 3; i++)
94                         dirent->ext[i] = ' ';
95         } else
96                 memcpy(dirent->ext, s_name + period_location + 1, 3);
97
98         debug("name : %s\n", dirent->name);
99         debug("ext : %s\n", dirent->ext);
100 }
101
102 /*
103  * Write fat buffer into block device
104  */
105 static int flush_dirty_fat_buffer(fsdata *mydata)
106 {
107         int getsize = FATBUFBLOCKS;
108         __u32 fatlength = mydata->fatlength;
109         __u8 *bufptr = mydata->fatbuf;
110         __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
111
112         debug("debug: evicting %d, dirty: %d\n", mydata->fatbufnum,
113               (int)mydata->fat_dirty);
114
115         if ((!mydata->fat_dirty) || (mydata->fatbufnum == -1))
116                 return 0;
117
118         /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
119         if (startblock + getsize > fatlength)
120                 getsize = fatlength - startblock;
121
122         startblock += mydata->fat_sect;
123
124         /* Write FAT buf */
125         if (disk_write(startblock, getsize, bufptr) < 0) {
126                 debug("error: writing FAT blocks\n");
127                 return -1;
128         }
129
130         if (mydata->fats == 2) {
131                 /* Update corresponding second FAT blocks */
132                 startblock += mydata->fatlength;
133                 if (disk_write(startblock, getsize, bufptr) < 0) {
134                         debug("error: writing second FAT blocks\n");
135                         return -1;
136                 }
137         }
138         mydata->fat_dirty = 0;
139
140         return 0;
141 }
142
143 /*
144  * Set the file name information from 'name' into 'slotptr',
145  */
146 static int str2slot(dir_slot *slotptr, const char *name, int *idx)
147 {
148         int j, end_idx = 0;
149
150         for (j = 0; j <= 8; j += 2) {
151                 if (name[*idx] == 0x00) {
152                         slotptr->name0_4[j] = 0;
153                         slotptr->name0_4[j + 1] = 0;
154                         end_idx++;
155                         goto name0_4;
156                 }
157                 slotptr->name0_4[j] = name[*idx];
158                 (*idx)++;
159                 end_idx++;
160         }
161         for (j = 0; j <= 10; j += 2) {
162                 if (name[*idx] == 0x00) {
163                         slotptr->name5_10[j] = 0;
164                         slotptr->name5_10[j + 1] = 0;
165                         end_idx++;
166                         goto name5_10;
167                 }
168                 slotptr->name5_10[j] = name[*idx];
169                 (*idx)++;
170                 end_idx++;
171         }
172         for (j = 0; j <= 2; j += 2) {
173                 if (name[*idx] == 0x00) {
174                         slotptr->name11_12[j] = 0;
175                         slotptr->name11_12[j + 1] = 0;
176                         end_idx++;
177                         goto name11_12;
178                 }
179                 slotptr->name11_12[j] = name[*idx];
180                 (*idx)++;
181                 end_idx++;
182         }
183
184         if (name[*idx] == 0x00)
185                 return 1;
186
187         return 0;
188 /* Not used characters are filled with 0xff 0xff */
189 name0_4:
190         for (; end_idx < 5; end_idx++) {
191                 slotptr->name0_4[end_idx * 2] = 0xff;
192                 slotptr->name0_4[end_idx * 2 + 1] = 0xff;
193         }
194         end_idx = 5;
195 name5_10:
196         end_idx -= 5;
197         for (; end_idx < 6; end_idx++) {
198                 slotptr->name5_10[end_idx * 2] = 0xff;
199                 slotptr->name5_10[end_idx * 2 + 1] = 0xff;
200         }
201         end_idx = 11;
202 name11_12:
203         end_idx -= 11;
204         for (; end_idx < 2; end_idx++) {
205                 slotptr->name11_12[end_idx * 2] = 0xff;
206                 slotptr->name11_12[end_idx * 2 + 1] = 0xff;
207         }
208
209         return 1;
210 }
211
212 static int flush_dir_table(fat_itr *itr);
213
214 /*
215  * Fill dir_slot entries with appropriate name, id, and attr
216  * 'itr' will point to a next entry
217  */
218 static int
219 fill_dir_slot(fat_itr *itr, const char *l_name)
220 {
221         __u8 temp_dir_slot_buffer[MAX_LFN_SLOT * sizeof(dir_slot)];
222         dir_slot *slotptr = (dir_slot *)temp_dir_slot_buffer;
223         __u8 counter = 0, checksum;
224         int idx = 0, ret;
225
226         /* Get short file name checksum value */
227         checksum = mkcksum(itr->dent->name, itr->dent->ext);
228
229         do {
230                 memset(slotptr, 0x00, sizeof(dir_slot));
231                 ret = str2slot(slotptr, l_name, &idx);
232                 slotptr->id = ++counter;
233                 slotptr->attr = ATTR_VFAT;
234                 slotptr->alias_checksum = checksum;
235                 slotptr++;
236         } while (ret == 0);
237
238         slotptr--;
239         slotptr->id |= LAST_LONG_ENTRY_MASK;
240
241         while (counter >= 1) {
242                 memcpy(itr->dent, slotptr, sizeof(dir_slot));
243                 slotptr--;
244                 counter--;
245                 if (!fat_itr_next(itr))
246                         if (!itr->dent && !itr->is_root && flush_dir_table(itr))
247                                 return -1;
248         }
249
250         if (!itr->dent && !itr->is_root)
251                 /*
252                  * don't care return value here because we have already
253                  * finished completing an entry with name, only ending up
254                  * no more entry left
255                  */
256                 flush_dir_table(itr);
257
258         return 0;
259 }
260
261 /*
262  * Set the entry at index 'entry' in a FAT (12/16/32) table.
263  */
264 static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
265 {
266         __u32 bufnum, offset, off16;
267         __u16 val1, val2;
268
269         switch (mydata->fatsize) {
270         case 32:
271                 bufnum = entry / FAT32BUFSIZE;
272                 offset = entry - bufnum * FAT32BUFSIZE;
273                 break;
274         case 16:
275                 bufnum = entry / FAT16BUFSIZE;
276                 offset = entry - bufnum * FAT16BUFSIZE;
277                 break;
278         case 12:
279                 bufnum = entry / FAT12BUFSIZE;
280                 offset = entry - bufnum * FAT12BUFSIZE;
281                 break;
282         default:
283                 /* Unsupported FAT size */
284                 return -1;
285         }
286
287         /* Read a new block of FAT entries into the cache. */
288         if (bufnum != mydata->fatbufnum) {
289                 int getsize = FATBUFBLOCKS;
290                 __u8 *bufptr = mydata->fatbuf;
291                 __u32 fatlength = mydata->fatlength;
292                 __u32 startblock = bufnum * FATBUFBLOCKS;
293
294                 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
295                 if (startblock + getsize > fatlength)
296                         getsize = fatlength - startblock;
297
298                 if (flush_dirty_fat_buffer(mydata) < 0)
299                         return -1;
300
301                 startblock += mydata->fat_sect;
302
303                 if (disk_read(startblock, getsize, bufptr) < 0) {
304                         debug("Error reading FAT blocks\n");
305                         return -1;
306                 }
307                 mydata->fatbufnum = bufnum;
308         }
309
310         /* Mark as dirty */
311         mydata->fat_dirty = 1;
312
313         /* Set the actual entry */
314         switch (mydata->fatsize) {
315         case 32:
316                 ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
317                 break;
318         case 16:
319                 ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
320                 break;
321         case 12:
322                 off16 = (offset * 3) / 4;
323
324                 switch (offset & 0x3) {
325                 case 0:
326                         val1 = cpu_to_le16(entry_value) & 0xfff;
327                         ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff;
328                         ((__u16 *)mydata->fatbuf)[off16] |= val1;
329                         break;
330                 case 1:
331                         val1 = cpu_to_le16(entry_value) & 0xf;
332                         val2 = (cpu_to_le16(entry_value) >> 4) & 0xff;
333
334                         ((__u16 *)mydata->fatbuf)[off16] &= ~0xf000;
335                         ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 12);
336
337                         ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xff;
338                         ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
339                         break;
340                 case 2:
341                         val1 = cpu_to_le16(entry_value) & 0xff;
342                         val2 = (cpu_to_le16(entry_value) >> 8) & 0xf;
343
344                         ((__u16 *)mydata->fatbuf)[off16] &= ~0xff00;
345                         ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 8);
346
347                         ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xf;
348                         ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
349                         break;
350                 case 3:
351                         val1 = cpu_to_le16(entry_value) & 0xfff;
352                         ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff0;
353                         ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 4);
354                         break;
355                 default:
356                         break;
357                 }
358
359                 break;
360         default:
361                 return -1;
362         }
363
364         return 0;
365 }
366
367 /*
368  * Determine the next free cluster after 'entry' in a FAT (12/16/32) table
369  * and link it to 'entry'. EOC marker is not set on returned entry.
370  */
371 static __u32 determine_fatent(fsdata *mydata, __u32 entry)
372 {
373         __u32 next_fat, next_entry = entry + 1;
374
375         while (1) {
376                 next_fat = get_fatent(mydata, next_entry);
377                 if (next_fat == 0) {
378                         /* found free entry, link to entry */
379                         set_fatent_value(mydata, entry, next_entry);
380                         break;
381                 }
382                 next_entry++;
383         }
384         debug("FAT%d: entry: %08x, entry_value: %04x\n",
385                mydata->fatsize, entry, next_entry);
386
387         return next_entry;
388 }
389
390 /**
391  * set_cluster() - write data to cluster
392  *
393  * Write 'size' bytes from 'buffer' into the specified cluster.
394  *
395  * @mydata:     data to be written
396  * @clustnum:   cluster to be written to
397  * @buffer:     data to be written
398  * @size:       bytes to be written (but not more than the size of a cluster)
399  * Return:      0 on success, -1 otherwise
400  */
401 static int
402 set_cluster(fsdata *mydata, u32 clustnum, u8 *buffer, u32 size)
403 {
404         u32 idx = 0;
405         u32 startsect;
406         int ret;
407
408         if (clustnum > 0)
409                 startsect = clust_to_sect(mydata, clustnum);
410         else
411                 startsect = mydata->rootdir_sect;
412
413         debug("clustnum: %d, startsect: %d\n", clustnum, startsect);
414
415         if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
416                 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
417
418                 debug("FAT: Misaligned buffer address (%p)\n", buffer);
419
420                 while (size >= mydata->sect_size) {
421                         memcpy(tmpbuf, buffer, mydata->sect_size);
422                         ret = disk_write(startsect++, 1, tmpbuf);
423                         if (ret != 1) {
424                                 debug("Error writing data (got %d)\n", ret);
425                                 return -1;
426                         }
427
428                         buffer += mydata->sect_size;
429                         size -= mydata->sect_size;
430                 }
431         } else if (size >= mydata->sect_size) {
432                 idx = size / mydata->sect_size;
433                 ret = disk_write(startsect, idx, buffer);
434                 if (ret != idx) {
435                         debug("Error writing data (got %d)\n", ret);
436                         return -1;
437                 }
438
439                 startsect += idx;
440                 idx *= mydata->sect_size;
441                 buffer += idx;
442                 size -= idx;
443         }
444
445         if (size) {
446                 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
447                 /* Do not leak content of stack */
448                 memset(tmpbuf, 0, mydata->sect_size);
449                 memcpy(tmpbuf, buffer, size);
450                 ret = disk_write(startsect, 1, tmpbuf);
451                 if (ret != 1) {
452                         debug("Error writing data (got %d)\n", ret);
453                         return -1;
454                 }
455         }
456
457         return 0;
458 }
459
460 static __u8 tmpbuf_cluster[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN);
461
462 /*
463  * Read and modify data on existing and consecutive cluster blocks
464  */
465 static int
466 get_set_cluster(fsdata *mydata, __u32 clustnum, loff_t pos, __u8 *buffer,
467                 loff_t size, loff_t *gotsize)
468 {
469         unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
470         __u32 startsect;
471         loff_t wsize;
472         int clustcount, i, ret;
473
474         *gotsize = 0;
475         if (!size)
476                 return 0;
477
478         assert(pos < bytesperclust);
479         startsect = clust_to_sect(mydata, clustnum);
480
481         debug("clustnum: %d, startsect: %d, pos: %lld\n",
482               clustnum, startsect, pos);
483
484         /* partial write at beginning */
485         if (pos) {
486                 wsize = min(bytesperclust - pos, size);
487                 ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster);
488                 if (ret != mydata->clust_size) {
489                         debug("Error reading data (got %d)\n", ret);
490                         return -1;
491                 }
492
493                 memcpy(tmpbuf_cluster + pos, buffer, wsize);
494                 ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster);
495                 if (ret != mydata->clust_size) {
496                         debug("Error writing data (got %d)\n", ret);
497                         return -1;
498                 }
499
500                 size -= wsize;
501                 buffer += wsize;
502                 *gotsize += wsize;
503
504                 startsect += mydata->clust_size;
505
506                 if (!size)
507                         return 0;
508         }
509
510         /* full-cluster write */
511         if (size >= bytesperclust) {
512                 clustcount = lldiv(size, bytesperclust);
513
514                 if (!((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1))) {
515                         wsize = clustcount * bytesperclust;
516                         ret = disk_write(startsect,
517                                          clustcount * mydata->clust_size,
518                                          buffer);
519                         if (ret != clustcount * mydata->clust_size) {
520                                 debug("Error writing data (got %d)\n", ret);
521                                 return -1;
522                         }
523
524                         size -= wsize;
525                         buffer += wsize;
526                         *gotsize += wsize;
527
528                         startsect += clustcount * mydata->clust_size;
529                 } else {
530                         for (i = 0; i < clustcount; i++) {
531                                 memcpy(tmpbuf_cluster, buffer, bytesperclust);
532                                 ret = disk_write(startsect,
533                                                  mydata->clust_size,
534                                                  tmpbuf_cluster);
535                                 if (ret != mydata->clust_size) {
536                                         debug("Error writing data (got %d)\n",
537                                               ret);
538                                         return -1;
539                                 }
540
541                                 size -= bytesperclust;
542                                 buffer += bytesperclust;
543                                 *gotsize += bytesperclust;
544
545                                 startsect += mydata->clust_size;
546                         }
547                 }
548         }
549
550         /* partial write at end */
551         if (size) {
552                 wsize = size;
553                 ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster);
554                 if (ret != mydata->clust_size) {
555                         debug("Error reading data (got %d)\n", ret);
556                         return -1;
557                 }
558                 memcpy(tmpbuf_cluster, buffer, wsize);
559                 ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster);
560                 if (ret != mydata->clust_size) {
561                         debug("Error writing data (got %d)\n", ret);
562                         return -1;
563                 }
564
565                 size -= wsize;
566                 buffer += wsize;
567                 *gotsize += wsize;
568         }
569
570         assert(!size);
571
572         return 0;
573 }
574
575 /*
576  * Find the first empty cluster
577  */
578 static int find_empty_cluster(fsdata *mydata)
579 {
580         __u32 fat_val, entry = 3;
581
582         while (1) {
583                 fat_val = get_fatent(mydata, entry);
584                 if (fat_val == 0)
585                         break;
586                 entry++;
587         }
588
589         return entry;
590 }
591
592 /*
593  * Write directory entries in itr's buffer to block device
594  */
595 static int flush_dir_table(fat_itr *itr)
596 {
597         fsdata *mydata = itr->fsdata;
598         int dir_newclust = 0;
599         unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
600
601         if (set_cluster(mydata, itr->clust, itr->block, bytesperclust) != 0) {
602                 printf("error: writing directory entry\n");
603                 return -1;
604         }
605         dir_newclust = find_empty_cluster(mydata);
606         set_fatent_value(mydata, itr->clust, dir_newclust);
607         if (mydata->fatsize == 32)
608                 set_fatent_value(mydata, dir_newclust, 0xffffff8);
609         else if (mydata->fatsize == 16)
610                 set_fatent_value(mydata, dir_newclust, 0xfff8);
611         else if (mydata->fatsize == 12)
612                 set_fatent_value(mydata, dir_newclust, 0xff8);
613
614         itr->clust = dir_newclust;
615         itr->next_clust = dir_newclust;
616
617         if (flush_dirty_fat_buffer(mydata) < 0)
618                 return -1;
619
620         memset(itr->block, 0x00, bytesperclust);
621
622         itr->dent = (dir_entry *)itr->block;
623         itr->last_cluster = 1;
624         itr->remaining = bytesperclust / sizeof(dir_entry) - 1;
625
626         return 0;
627 }
628
629 /*
630  * Set empty cluster from 'entry' to the end of a file
631  */
632 static int clear_fatent(fsdata *mydata, __u32 entry)
633 {
634         __u32 fat_val;
635
636         while (!CHECK_CLUST(entry, mydata->fatsize)) {
637                 fat_val = get_fatent(mydata, entry);
638                 if (fat_val != 0)
639                         set_fatent_value(mydata, entry, 0);
640                 else
641                         break;
642
643                 entry = fat_val;
644         }
645
646         /* Flush fat buffer */
647         if (flush_dirty_fat_buffer(mydata) < 0)
648                 return -1;
649
650         return 0;
651 }
652
653 /*
654  * Set start cluster in directory entry
655  */
656 static void set_start_cluster(const fsdata *mydata, dir_entry *dentptr,
657                               __u32 start_cluster)
658 {
659         if (mydata->fatsize == 32)
660                 dentptr->starthi =
661                         cpu_to_le16((start_cluster & 0xffff0000) >> 16);
662         dentptr->start = cpu_to_le16(start_cluster & 0xffff);
663 }
664
665 /*
666  * Check whether adding a file makes the file system to
667  * exceed the size of the block device
668  * Return -1 when overflow occurs, otherwise return 0
669  */
670 static int check_overflow(fsdata *mydata, __u32 clustnum, loff_t size)
671 {
672         __u32 startsect, sect_num, offset;
673
674         if (clustnum > 0)
675                 startsect = clust_to_sect(mydata, clustnum);
676         else
677                 startsect = mydata->rootdir_sect;
678
679         sect_num = div_u64_rem(size, mydata->sect_size, &offset);
680
681         if (offset != 0)
682                 sect_num++;
683
684         if (startsect + sect_num > total_sector)
685                 return -1;
686         return 0;
687 }
688
689 /*
690  * Write at most 'maxsize' bytes from 'buffer' into
691  * the file associated with 'dentptr'
692  * Update the number of bytes written in *gotsize and return 0
693  * or return -1 on fatal errors.
694  */
695 static int
696 set_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, __u8 *buffer,
697              loff_t maxsize, loff_t *gotsize)
698 {
699         unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
700         __u32 curclust = START(dentptr);
701         __u32 endclust = 0, newclust = 0;
702         u64 cur_pos, filesize;
703         loff_t offset, actsize, wsize;
704
705         *gotsize = 0;
706         filesize = pos + maxsize;
707
708         debug("%llu bytes\n", filesize);
709
710         if (!filesize) {
711                 if (!curclust)
712                         return 0;
713                 if (!CHECK_CLUST(curclust, mydata->fatsize) ||
714                     IS_LAST_CLUST(curclust, mydata->fatsize)) {
715                         clear_fatent(mydata, curclust);
716                         set_start_cluster(mydata, dentptr, 0);
717                         return 0;
718                 }
719                 debug("curclust: 0x%x\n", curclust);
720                 debug("Invalid FAT entry\n");
721                 return -1;
722         }
723
724         if (!curclust) {
725                 assert(pos == 0);
726                 goto set_clusters;
727         }
728
729         /* go to cluster at pos */
730         cur_pos = bytesperclust;
731         while (1) {
732                 if (pos <= cur_pos)
733                         break;
734                 if (IS_LAST_CLUST(curclust, mydata->fatsize))
735                         break;
736
737                 newclust = get_fatent(mydata, curclust);
738                 if (!IS_LAST_CLUST(newclust, mydata->fatsize) &&
739                     CHECK_CLUST(newclust, mydata->fatsize)) {
740                         debug("curclust: 0x%x\n", curclust);
741                         debug("Invalid FAT entry\n");
742                         return -1;
743                 }
744
745                 cur_pos += bytesperclust;
746                 curclust = newclust;
747         }
748         if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
749                 assert(pos == cur_pos);
750                 goto set_clusters;
751         }
752
753         assert(pos < cur_pos);
754         cur_pos -= bytesperclust;
755
756         /* overwrite */
757         assert(IS_LAST_CLUST(curclust, mydata->fatsize) ||
758                !CHECK_CLUST(curclust, mydata->fatsize));
759
760         while (1) {
761                 /* search for allocated consecutive clusters */
762                 actsize = bytesperclust;
763                 endclust = curclust;
764                 while (1) {
765                         if (filesize <= (cur_pos + actsize))
766                                 break;
767
768                         newclust = get_fatent(mydata, endclust);
769
770                         if (IS_LAST_CLUST(newclust, mydata->fatsize))
771                                 break;
772                         if (CHECK_CLUST(newclust, mydata->fatsize)) {
773                                 debug("curclust: 0x%x\n", curclust);
774                                 debug("Invalid FAT entry\n");
775                                 return -1;
776                         }
777
778                         actsize += bytesperclust;
779                         endclust = newclust;
780                 }
781
782                 /* overwrite to <curclust..endclust> */
783                 if (pos < cur_pos)
784                         offset = 0;
785                 else
786                         offset = pos - cur_pos;
787                 wsize = min(cur_pos + actsize, filesize) - pos;
788                 if (get_set_cluster(mydata, curclust, offset,
789                                     buffer, wsize, &actsize)) {
790                         printf("Error get-and-setting cluster\n");
791                         return -1;
792                 }
793                 buffer += wsize;
794                 *gotsize += wsize;
795                 cur_pos += offset + wsize;
796
797                 if (filesize <= cur_pos)
798                         break;
799
800                 /* CHECK: newclust = get_fatent(mydata, endclust); */
801
802                 if (IS_LAST_CLUST(newclust, mydata->fatsize))
803                         /* no more clusters */
804                         break;
805
806                 curclust = newclust;
807         }
808
809         if (filesize <= cur_pos) {
810                 /* no more write */
811                 newclust = get_fatent(mydata, endclust);
812                 if (!IS_LAST_CLUST(newclust, mydata->fatsize)) {
813                         /* truncate the rest */
814                         clear_fatent(mydata, newclust);
815
816                         /* Mark end of file in FAT */
817                         if (mydata->fatsize == 12)
818                                 newclust = 0xfff;
819                         else if (mydata->fatsize == 16)
820                                 newclust = 0xffff;
821                         else if (mydata->fatsize == 32)
822                                 newclust = 0xfffffff;
823                         set_fatent_value(mydata, endclust, newclust);
824                 }
825
826                 return 0;
827         }
828
829         curclust = endclust;
830         filesize -= cur_pos;
831         assert(!do_div(cur_pos, bytesperclust));
832
833 set_clusters:
834         /* allocate and write */
835         assert(!pos);
836
837         /* Assure that curclust is valid */
838         if (!curclust) {
839                 curclust = find_empty_cluster(mydata);
840                 set_start_cluster(mydata, dentptr, curclust);
841         } else {
842                 newclust = get_fatent(mydata, curclust);
843
844                 if (IS_LAST_CLUST(newclust, mydata->fatsize)) {
845                         newclust = determine_fatent(mydata, curclust);
846                         set_fatent_value(mydata, curclust, newclust);
847                         curclust = newclust;
848                 } else {
849                         debug("error: something wrong\n");
850                         return -1;
851                 }
852         }
853
854         /* TODO: already partially written */
855         if (check_overflow(mydata, curclust, filesize)) {
856                 printf("Error: no space left: %llu\n", filesize);
857                 return -1;
858         }
859
860         actsize = bytesperclust;
861         endclust = curclust;
862         do {
863                 /* search for consecutive clusters */
864                 while (actsize < filesize) {
865                         newclust = determine_fatent(mydata, endclust);
866
867                         if ((newclust - 1) != endclust)
868                                 /* write to <curclust..endclust> */
869                                 goto getit;
870
871                         if (CHECK_CLUST(newclust, mydata->fatsize)) {
872                                 debug("newclust: 0x%x\n", newclust);
873                                 debug("Invalid FAT entry\n");
874                                 return 0;
875                         }
876                         endclust = newclust;
877                         actsize += bytesperclust;
878                 }
879
880                 /* set remaining bytes */
881                 actsize = filesize;
882                 if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
883                         debug("error: writing cluster\n");
884                         return -1;
885                 }
886                 *gotsize += actsize;
887
888                 /* Mark end of file in FAT */
889                 if (mydata->fatsize == 12)
890                         newclust = 0xfff;
891                 else if (mydata->fatsize == 16)
892                         newclust = 0xffff;
893                 else if (mydata->fatsize == 32)
894                         newclust = 0xfffffff;
895                 set_fatent_value(mydata, endclust, newclust);
896
897                 return 0;
898 getit:
899                 if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
900                         debug("error: writing cluster\n");
901                         return -1;
902                 }
903                 *gotsize += actsize;
904                 filesize -= actsize;
905                 buffer += actsize;
906
907                 if (CHECK_CLUST(newclust, mydata->fatsize)) {
908                         debug("newclust: 0x%x\n", newclust);
909                         debug("Invalid FAT entry\n");
910                         return 0;
911                 }
912                 actsize = bytesperclust;
913                 curclust = endclust = newclust;
914         } while (1);
915
916         return 0;
917 }
918
919 /*
920  * Fill dir_entry
921  */
922 static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
923         const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
924 {
925         set_start_cluster(mydata, dentptr, start_cluster);
926         dentptr->size = cpu_to_le32(size);
927
928         dentptr->attr = attr;
929
930         set_name(dentptr, filename);
931 }
932
933 /*
934  * Find a directory entry based on filename or start cluster number
935  * If the directory entry is not found,
936  * the new position for writing a directory entry will be returned
937  */
938 static dir_entry *find_directory_entry(fat_itr *itr, char *filename)
939 {
940         int match = 0;
941
942         while (fat_itr_next(itr)) {
943                 /* check both long and short name: */
944                 if (!strcasecmp(filename, itr->name))
945                         match = 1;
946                 else if (itr->name != itr->s_name &&
947                          !strcasecmp(filename, itr->s_name))
948                         match = 1;
949
950                 if (!match)
951                         continue;
952
953                 if (itr->dent->name[0] == '\0')
954                         return NULL;
955                 else
956                         return itr->dent;
957         }
958
959         if (!itr->dent && !itr->is_root && flush_dir_table(itr))
960                 /* indicate that allocating dent failed */
961                 itr->dent = NULL;
962
963         return NULL;
964 }
965
966 static int split_filename(char *filename, char **dirname, char **basename)
967 {
968         char *p, *last_slash, *last_slash_cont;
969
970 again:
971         p = filename;
972         last_slash = NULL;
973         last_slash_cont = NULL;
974         while (*p) {
975                 if (ISDIRDELIM(*p)) {
976                         last_slash = p;
977                         last_slash_cont = p;
978                         /* continuous slashes */
979                         while (ISDIRDELIM(*p))
980                                 last_slash_cont = p++;
981                         if (!*p)
982                                 break;
983                 }
984                 p++;
985         }
986
987         if (last_slash) {
988                 if (last_slash_cont == (filename + strlen(filename) - 1)) {
989                         /* remove trailing slashes */
990                         *last_slash = '\0';
991                         goto again;
992                 }
993
994                 if (last_slash == filename) {
995                         /* avoid ""(null) directory */
996                         *dirname = "/";
997                 } else {
998                         *last_slash = '\0';
999                         *dirname = filename;
1000                 }
1001
1002                 *last_slash_cont = '\0';
1003                 *basename = last_slash_cont + 1;
1004         } else {
1005                 *dirname = "/"; /* root by default */
1006                 *basename = filename;
1007         }
1008
1009         return 0;
1010 }
1011
1012 /**
1013  * normalize_longname() - check long file name and convert to lower case
1014  *
1015  * We assume here that the FAT file system is using an 8bit code page.
1016  * Linux typically uses CP437, EDK2 assumes CP1250.
1017  *
1018  * @l_filename: preallocated buffer receiving the normalized name
1019  * @filename:   filename to normalize
1020  * Return:      0 on success, -1 on failure
1021  */
1022 static int normalize_longname(char *l_filename, const char *filename)
1023 {
1024         const char *p, illegal[] = "<>:\"/\\|?*";
1025
1026         if (strlen(filename) >= VFAT_MAXLEN_BYTES)
1027                 return -1;
1028
1029         for (p = filename; *p; ++p) {
1030                 if ((unsigned char)*p < 0x20)
1031                         return -1;
1032                 if (strchr(illegal, *p))
1033                         return -1;
1034         }
1035
1036         strcpy(l_filename, filename);
1037         downcase(l_filename, VFAT_MAXLEN_BYTES);
1038
1039         return 0;
1040 }
1041
1042 int file_fat_write_at(const char *filename, loff_t pos, void *buffer,
1043                       loff_t size, loff_t *actwrite)
1044 {
1045         dir_entry *retdent;
1046         fsdata datablock = { .fatbuf = NULL, };
1047         fsdata *mydata = &datablock;
1048         fat_itr *itr = NULL;
1049         int ret = -1;
1050         char *filename_copy, *parent, *basename;
1051         char l_filename[VFAT_MAXLEN_BYTES];
1052
1053         debug("writing %s\n", filename);
1054
1055         filename_copy = strdup(filename);
1056         if (!filename_copy)
1057                 return -ENOMEM;
1058
1059         split_filename(filename_copy, &parent, &basename);
1060         if (!strlen(basename)) {
1061                 ret = -EINVAL;
1062                 goto exit;
1063         }
1064
1065         filename = basename;
1066         if (normalize_longname(l_filename, filename)) {
1067                 printf("FAT: illegal filename (%s)\n", filename);
1068                 ret = -EINVAL;
1069                 goto exit;
1070         }
1071
1072         itr = malloc_cache_aligned(sizeof(fat_itr));
1073         if (!itr) {
1074                 ret = -ENOMEM;
1075                 goto exit;
1076         }
1077
1078         ret = fat_itr_root(itr, &datablock);
1079         if (ret)
1080                 goto exit;
1081
1082         total_sector = datablock.total_sect;
1083
1084         ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1085         if (ret) {
1086                 printf("%s: doesn't exist (%d)\n", parent, ret);
1087                 goto exit;
1088         }
1089
1090         retdent = find_directory_entry(itr, l_filename);
1091
1092         if (retdent) {
1093                 if (fat_itr_isdir(itr)) {
1094                         ret = -EISDIR;
1095                         goto exit;
1096                 }
1097
1098                 /* A file exists */
1099                 if (pos == -1)
1100                         /* Append to the end */
1101                         pos = FAT2CPU32(retdent->size);
1102                 if (pos > retdent->size) {
1103                         /* No hole allowed */
1104                         ret = -EINVAL;
1105                         goto exit;
1106                 }
1107
1108                 /* Update file size in a directory entry */
1109                 retdent->size = cpu_to_le32(pos + size);
1110         } else {
1111                 /* Create a new file */
1112
1113                 if (itr->is_root) {
1114                         /* root dir cannot have "." or ".." */
1115                         if (!strcmp(l_filename, ".") ||
1116                             !strcmp(l_filename, "..")) {
1117                                 ret = -EINVAL;
1118                                 goto exit;
1119                         }
1120                 }
1121
1122                 if (!itr->dent) {
1123                         printf("Error: allocating new dir entry\n");
1124                         ret = -EIO;
1125                         goto exit;
1126                 }
1127
1128                 if (pos) {
1129                         /* No hole allowed */
1130                         ret = -EINVAL;
1131                         goto exit;
1132                 }
1133
1134                 memset(itr->dent, 0, sizeof(*itr->dent));
1135
1136                 /* Set short name to set alias checksum field in dir_slot */
1137                 set_name(itr->dent, filename);
1138                 if (fill_dir_slot(itr, filename)) {
1139                         ret = -EIO;
1140                         goto exit;
1141                 }
1142
1143                 /* Set attribute as archive for regular file */
1144                 fill_dentry(itr->fsdata, itr->dent, filename, 0, size, 0x20);
1145
1146                 retdent = itr->dent;
1147         }
1148
1149         ret = set_contents(mydata, retdent, pos, buffer, size, actwrite);
1150         if (ret < 0) {
1151                 printf("Error: writing contents\n");
1152                 ret = -EIO;
1153                 goto exit;
1154         }
1155         debug("attempt to write 0x%llx bytes\n", *actwrite);
1156
1157         /* Flush fat buffer */
1158         ret = flush_dirty_fat_buffer(mydata);
1159         if (ret) {
1160                 printf("Error: flush fat buffer\n");
1161                 ret = -EIO;
1162                 goto exit;
1163         }
1164
1165         /* Write directory table to device */
1166         ret = set_cluster(mydata, itr->clust, itr->block,
1167                           mydata->clust_size * mydata->sect_size);
1168         if (ret) {
1169                 printf("Error: writing directory entry\n");
1170                 ret = -EIO;
1171         }
1172
1173 exit:
1174         free(filename_copy);
1175         free(mydata->fatbuf);
1176         free(itr);
1177         return ret;
1178 }
1179
1180 int file_fat_write(const char *filename, void *buffer, loff_t offset,
1181                    loff_t maxsize, loff_t *actwrite)
1182 {
1183         return file_fat_write_at(filename, offset, buffer, maxsize, actwrite);
1184 }
1185
1186 static int fat_dir_entries(fat_itr *itr)
1187 {
1188         fat_itr *dirs;
1189         fsdata fsdata = { .fatbuf = NULL, }, *mydata = &fsdata;
1190                                                 /* for FATBUFSIZE */
1191         int count;
1192
1193         dirs = malloc_cache_aligned(sizeof(fat_itr));
1194         if (!dirs) {
1195                 debug("Error: allocating memory\n");
1196                 count = -ENOMEM;
1197                 goto exit;
1198         }
1199
1200         /* duplicate fsdata */
1201         fat_itr_child(dirs, itr);
1202         fsdata = *dirs->fsdata;
1203
1204         /* allocate local fat buffer */
1205         fsdata.fatbuf = malloc_cache_aligned(FATBUFSIZE);
1206         if (!fsdata.fatbuf) {
1207                 debug("Error: allocating memory\n");
1208                 count = -ENOMEM;
1209                 goto exit;
1210         }
1211         fsdata.fatbufnum = -1;
1212         dirs->fsdata = &fsdata;
1213
1214         for (count = 0; fat_itr_next(dirs); count++)
1215                 ;
1216
1217 exit:
1218         free(fsdata.fatbuf);
1219         free(dirs);
1220         return count;
1221 }
1222
1223 static int delete_dentry(fat_itr *itr)
1224 {
1225         fsdata *mydata = itr->fsdata;
1226         dir_entry *dentptr = itr->dent;
1227
1228         /* free cluster blocks */
1229         clear_fatent(mydata, START(dentptr));
1230         if (flush_dirty_fat_buffer(mydata) < 0) {
1231                 printf("Error: flush fat buffer\n");
1232                 return -EIO;
1233         }
1234
1235         /*
1236          * update a directory entry
1237          * TODO:
1238          *  - long file name support
1239          *  - find and mark the "new" first invalid entry as name[0]=0x00
1240          */
1241         memset(dentptr, 0, sizeof(*dentptr));
1242         dentptr->name[0] = 0xe5;
1243
1244         if (set_cluster(mydata, itr->clust, itr->block,
1245                         mydata->clust_size * mydata->sect_size) != 0) {
1246                 printf("error: writing directory entry\n");
1247                 return -EIO;
1248         }
1249
1250         return 0;
1251 }
1252
1253 int fat_unlink(const char *filename)
1254 {
1255         fsdata fsdata = { .fatbuf = NULL, };
1256         fat_itr *itr = NULL;
1257         int n_entries, ret;
1258         char *filename_copy, *dirname, *basename;
1259
1260         filename_copy = strdup(filename);
1261         if (!filename_copy) {
1262                 printf("Error: allocating memory\n");
1263                 ret = -ENOMEM;
1264                 goto exit;
1265         }
1266         split_filename(filename_copy, &dirname, &basename);
1267
1268         if (!strcmp(dirname, "/") && !strcmp(basename, "")) {
1269                 printf("Error: cannot remove root\n");
1270                 ret = -EINVAL;
1271                 goto exit;
1272         }
1273
1274         itr = malloc_cache_aligned(sizeof(fat_itr));
1275         if (!itr) {
1276                 printf("Error: allocating memory\n");
1277                 ret = -ENOMEM;
1278                 goto exit;
1279         }
1280
1281         ret = fat_itr_root(itr, &fsdata);
1282         if (ret)
1283                 goto exit;
1284
1285         total_sector = fsdata.total_sect;
1286
1287         ret = fat_itr_resolve(itr, dirname, TYPE_DIR);
1288         if (ret) {
1289                 printf("%s: doesn't exist (%d)\n", dirname, ret);
1290                 ret = -ENOENT;
1291                 goto exit;
1292         }
1293
1294         if (!find_directory_entry(itr, basename)) {
1295                 printf("%s: doesn't exist\n", basename);
1296                 ret = -ENOENT;
1297                 goto exit;
1298         }
1299
1300         if (fat_itr_isdir(itr)) {
1301                 n_entries = fat_dir_entries(itr);
1302                 if (n_entries < 0) {
1303                         ret = n_entries;
1304                         goto exit;
1305                 }
1306                 if (n_entries > 2) {
1307                         printf("Error: directory is not empty: %d\n",
1308                                n_entries);
1309                         ret = -EINVAL;
1310                         goto exit;
1311                 }
1312         }
1313
1314         ret = delete_dentry(itr);
1315
1316 exit:
1317         free(fsdata.fatbuf);
1318         free(itr);
1319         free(filename_copy);
1320
1321         return ret;
1322 }
1323
1324 int fat_mkdir(const char *new_dirname)
1325 {
1326         dir_entry *retdent;
1327         fsdata datablock = { .fatbuf = NULL, };
1328         fsdata *mydata = &datablock;
1329         fat_itr *itr = NULL;
1330         char *dirname_copy, *parent, *dirname;
1331         char l_dirname[VFAT_MAXLEN_BYTES];
1332         int ret = -1;
1333         loff_t actwrite;
1334         unsigned int bytesperclust;
1335         dir_entry *dotdent = NULL;
1336
1337         dirname_copy = strdup(new_dirname);
1338         if (!dirname_copy)
1339                 goto exit;
1340
1341         split_filename(dirname_copy, &parent, &dirname);
1342         if (!strlen(dirname)) {
1343                 ret = -EINVAL;
1344                 goto exit;
1345         }
1346
1347         if (normalize_longname(l_dirname, dirname)) {
1348                 printf("FAT: illegal filename (%s)\n", dirname);
1349                 ret = -EINVAL;
1350                 goto exit;
1351         }
1352
1353         itr = malloc_cache_aligned(sizeof(fat_itr));
1354         if (!itr) {
1355                 ret = -ENOMEM;
1356                 goto exit;
1357         }
1358
1359         ret = fat_itr_root(itr, &datablock);
1360         if (ret)
1361                 goto exit;
1362
1363         total_sector = datablock.total_sect;
1364
1365         ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1366         if (ret) {
1367                 printf("%s: doesn't exist (%d)\n", parent, ret);
1368                 goto exit;
1369         }
1370
1371         retdent = find_directory_entry(itr, l_dirname);
1372
1373         if (retdent) {
1374                 printf("%s: already exists\n", l_dirname);
1375                 ret = -EEXIST;
1376                 goto exit;
1377         } else {
1378                 if (itr->is_root) {
1379                         /* root dir cannot have "." or ".." */
1380                         if (!strcmp(l_dirname, ".") ||
1381                             !strcmp(l_dirname, "..")) {
1382                                 ret = -EINVAL;
1383                                 goto exit;
1384                         }
1385                 }
1386
1387                 if (!itr->dent) {
1388                         printf("Error: allocating new dir entry\n");
1389                         ret = -EIO;
1390                         goto exit;
1391                 }
1392
1393                 memset(itr->dent, 0, sizeof(*itr->dent));
1394
1395                 /* Set short name to set alias checksum field in dir_slot */
1396                 set_name(itr->dent, dirname);
1397                 fill_dir_slot(itr, dirname);
1398
1399                 /* Set attribute as archive for regular file */
1400                 fill_dentry(itr->fsdata, itr->dent, dirname, 0, 0,
1401                             ATTR_DIR | ATTR_ARCH);
1402
1403                 retdent = itr->dent;
1404         }
1405
1406         /* Default entries */
1407         bytesperclust = mydata->clust_size * mydata->sect_size;
1408         dotdent = malloc_cache_aligned(bytesperclust);
1409         if (!dotdent) {
1410                 ret = -ENOMEM;
1411                 goto exit;
1412         }
1413         memset(dotdent, 0, bytesperclust);
1414
1415         memcpy(dotdent[0].name, ".       ", 8);
1416         memcpy(dotdent[0].ext, "   ", 3);
1417         dotdent[0].attr = ATTR_DIR | ATTR_ARCH;
1418
1419         memcpy(dotdent[1].name, "..      ", 8);
1420         memcpy(dotdent[1].ext, "   ", 3);
1421         dotdent[1].attr = ATTR_DIR | ATTR_ARCH;
1422         set_start_cluster(mydata, &dotdent[1], itr->start_clust);
1423
1424         ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
1425                            bytesperclust, &actwrite);
1426         if (ret < 0) {
1427                 printf("Error: writing contents\n");
1428                 goto exit;
1429         }
1430         /* Write twice for "." */
1431         set_start_cluster(mydata, &dotdent[0], START(retdent));
1432         ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
1433                            bytesperclust, &actwrite);
1434         if (ret < 0) {
1435                 printf("Error: writing contents\n");
1436                 goto exit;
1437         }
1438
1439         /* Flush fat buffer */
1440         ret = flush_dirty_fat_buffer(mydata);
1441         if (ret) {
1442                 printf("Error: flush fat buffer\n");
1443                 goto exit;
1444         }
1445
1446         /* Write directory table to device */
1447         ret = set_cluster(mydata, itr->clust, itr->block,
1448                           mydata->clust_size * mydata->sect_size);
1449         if (ret)
1450                 printf("Error: writing directory entry\n");
1451
1452 exit:
1453         free(dirname_copy);
1454         free(mydata->fatbuf);
1455         free(itr);
1456         free(dotdent);
1457         return ret;
1458 }