blob: 3272412ca9f3084a50f2ada8f01efa449c1930ee [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Donggeun Kim8f814002011-10-24 21:15:28 +00002/*
3 * fat_write.c
4 *
5 * R/W (V)FAT 12/16/32 filesystem implementation by Donggeun Kim
Donggeun Kim8f814002011-10-24 21:15:28 +00006 */
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>
Richard Genoud2e372022012-12-13 00:47:36 +000014#include <linux/ctype.h>
Tom Rinia17b7bc2014-11-24 11:50:46 -050015#include <div64.h>
16#include <linux/math64.h>
Donggeun Kim8f814002011-10-24 21:15:28 +000017#include "fat.c"
18
19static void uppercase(char *str, int len)
20{
21 int i;
22
23 for (i = 0; i < len; i++) {
Richard Genoud2e372022012-12-13 00:47:36 +000024 *str = toupper(*str);
Donggeun Kim8f814002011-10-24 21:15:28 +000025 str++;
26 }
27}
28
29static int total_sector;
Donggeun Kimcb5f3bc2012-03-22 04:38:55 +000030static int disk_write(__u32 block, __u32 nr_blocks, void *buf)
Donggeun Kim8f814002011-10-24 21:15:28 +000031{
Łukasz Majewskid6f23d82015-09-03 14:21:39 +020032 ulong ret;
33
Simon Glass2ee8ada2016-02-29 15:25:52 -070034 if (!cur_dev)
Donggeun Kim8f814002011-10-24 21:15:28 +000035 return -1;
36
Donggeun Kimcb5f3bc2012-03-22 04:38:55 +000037 if (cur_part_info.start + block + nr_blocks >
38 cur_part_info.start + total_sector) {
Donggeun Kim8f814002011-10-24 21:15:28 +000039 printf("error: overflow occurs\n");
40 return -1;
41 }
42
Simon Glass2ee8ada2016-02-29 15:25:52 -070043 ret = blk_dwrite(cur_dev, cur_part_info.start + block, nr_blocks, buf);
Łukasz Majewskid6f23d82015-09-03 14:21:39 +020044 if (nr_blocks && ret == 0)
45 return -1;
46
47 return ret;
Donggeun Kim8f814002011-10-24 21:15:28 +000048}
49
50/*
51 * Set short name in directory entry
52 */
53static 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
Piotr Wilczek65a43e32013-10-11 15:43:33 +020066 strcpy(s_name, filename);
Donggeun Kim8f814002011-10-24 21:15:28 +000067 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 */
Stefan Brüns751b31d2016-09-11 22:51:40 +0200105static int flush_dirty_fat_buffer(fsdata *mydata)
Donggeun Kim8f814002011-10-24 21:15:28 +0000106{
107 int getsize = FATBUFBLOCKS;
108 __u32 fatlength = mydata->fatlength;
109 __u8 *bufptr = mydata->fatbuf;
110 __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
111
Stefan Brüns751b31d2016-09-11 22:51:40 +0200112 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
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100118 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
119 if (startblock + getsize > fatlength)
120 getsize = fatlength - startblock;
Donggeun Kim8f814002011-10-24 21:15:28 +0000121
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100122 startblock += mydata->fat_sect;
Donggeun Kim8f814002011-10-24 21:15:28 +0000123
124 /* Write FAT buf */
125 if (disk_write(startblock, getsize, bufptr) < 0) {
126 debug("error: writing FAT blocks\n");
127 return -1;
128 }
129
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900130 if (mydata->fats == 2) {
Donggeun Kimaf57ce22011-12-20 18:34:27 +0000131 /* 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 }
Stefan Brüns751b31d2016-09-11 22:51:40 +0200138 mydata->fat_dirty = 0;
Donggeun Kimaf57ce22011-12-20 18:34:27 +0000139
Donggeun Kim8f814002011-10-24 21:15:28 +0000140 return 0;
141}
142
143/*
Donggeun Kim8f814002011-10-24 21:15:28 +0000144 * Set the file name information from 'name' into 'slotptr',
145 */
146static 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 */
189name0_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;
195name5_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;
202name11_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
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900212static int flush_dir_table(fat_itr *itr);
Donggeun Kim8f814002011-10-24 21:15:28 +0000213
214/*
215 * Fill dir_slot entries with appropriate name, id, and attr
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900216 * 'itr' will point to a next entry
Donggeun Kim8f814002011-10-24 21:15:28 +0000217 */
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900218static int
219fill_dir_slot(fat_itr *itr, const char *l_name)
Donggeun Kim8f814002011-10-24 21:15:28 +0000220{
Tien Fong Chee0117dbe2016-07-27 23:08:56 -0700221 __u8 temp_dir_slot_buffer[MAX_LFN_SLOT * sizeof(dir_slot)];
222 dir_slot *slotptr = (dir_slot *)temp_dir_slot_buffer;
Anatolij Gustschinf4f9d5d2011-12-15 03:12:14 +0000223 __u8 counter = 0, checksum;
Donggeun Kim8f814002011-10-24 21:15:28 +0000224 int idx = 0, ret;
Donggeun Kim8f814002011-10-24 21:15:28 +0000225
Stefan Brüns6c617d62016-09-11 22:51:39 +0200226 /* Get short file name checksum value */
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900227 checksum = mkcksum(itr->dent->name, itr->dent->ext);
Donggeun Kim8f814002011-10-24 21:15:28 +0000228
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) {
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900242 memcpy(itr->dent, slotptr, sizeof(dir_slot));
Donggeun Kim8f814002011-10-24 21:15:28 +0000243 slotptr--;
244 counter--;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900245 if (!fat_itr_next(itr))
246 if (!itr->dent && !itr->is_root && flush_dir_table(itr))
Donggeun Kim8f814002011-10-24 21:15:28 +0000247 return -1;
Donggeun Kim8f814002011-10-24 21:15:28 +0000248 }
249
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900250 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);
Donggeun Kim8f814002011-10-24 21:15:28 +0000257
258 return 0;
259}
260
Donggeun Kim8f814002011-10-24 21:15:28 +0000261/*
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500262 * Set the entry at index 'entry' in a FAT (12/16/32) table.
Donggeun Kim8f814002011-10-24 21:15:28 +0000263 */
264static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
265{
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500266 __u32 bufnum, offset, off16;
267 __u16 val1, val2;
Donggeun Kim8f814002011-10-24 21:15:28 +0000268
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;
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500278 case 12:
279 bufnum = entry / FAT12BUFSIZE;
280 offset = entry - bufnum * FAT12BUFSIZE;
281 break;
Donggeun Kim8f814002011-10-24 21:15:28 +0000282 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
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100294 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
295 if (startblock + getsize > fatlength)
296 getsize = fatlength - startblock;
Donggeun Kim8f814002011-10-24 21:15:28 +0000297
Stefan Brüns751b31d2016-09-11 22:51:40 +0200298 if (flush_dirty_fat_buffer(mydata) < 0)
299 return -1;
Donggeun Kim8f814002011-10-24 21:15:28 +0000300
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100301 startblock += mydata->fat_sect;
302
Donggeun Kim8f814002011-10-24 21:15:28 +0000303 if (disk_read(startblock, getsize, bufptr) < 0) {
304 debug("Error reading FAT blocks\n");
305 return -1;
306 }
307 mydata->fatbufnum = bufnum;
308 }
309
Stefan Brüns751b31d2016-09-11 22:51:40 +0200310 /* Mark as dirty */
311 mydata->fat_dirty = 1;
312
Donggeun Kim8f814002011-10-24 21:15:28 +0000313 /* 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;
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500321 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;
Donggeun Kim8f814002011-10-24 21:15:28 +0000360 default:
361 return -1;
362 }
363
364 return 0;
365}
366
367/*
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500368 * Determine the next free cluster after 'entry' in a FAT (12/16/32) table
Stefan Brüns14999402016-09-11 22:51:41 +0200369 * and link it to 'entry'. EOC marker is not set on returned entry.
Donggeun Kim8f814002011-10-24 21:15:28 +0000370 */
371static __u32 determine_fatent(fsdata *mydata, __u32 entry)
372{
373 __u32 next_fat, next_entry = entry + 1;
374
375 while (1) {
Stefan Brüns9f95d812016-12-17 00:27:51 +0100376 next_fat = get_fatent(mydata, next_entry);
Donggeun Kim8f814002011-10-24 21:15:28 +0000377 if (next_fat == 0) {
Stefan Brüns14999402016-09-11 22:51:41 +0200378 /* found free entry, link to entry */
Donggeun Kim8f814002011-10-24 21:15:28 +0000379 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
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200390/**
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
Donggeun Kim8f814002011-10-24 21:15:28 +0000400 */
401static int
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200402set_cluster(fsdata *mydata, u32 clustnum, u8 *buffer, u32 size)
Donggeun Kim8f814002011-10-24 21:15:28 +0000403{
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200404 u32 idx = 0;
405 u32 startsect;
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200406 int ret;
Donggeun Kim8f814002011-10-24 21:15:28 +0000407
408 if (clustnum > 0)
Rob Clarkc3ab0b82017-09-09 13:16:00 -0400409 startsect = clust_to_sect(mydata, clustnum);
Donggeun Kim8f814002011-10-24 21:15:28 +0000410 else
411 startsect = mydata->rootdir_sect;
412
413 debug("clustnum: %d, startsect: %d\n", clustnum, startsect);
414
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200415 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
416 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
417
Heinrich Schuchardtaa9d6ba2018-09-13 19:42:47 +0200418 debug("FAT: Misaligned buffer address (%p)\n", buffer);
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200419
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);
Wu, Joshb0e38dd2013-07-24 17:55:30 +0800436 return -1;
437 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000438
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200439 startsect += idx;
440 idx *= mydata->sect_size;
441 buffer += idx;
442 size -= idx;
443 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000444
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200445 if (size) {
446 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200447 /* Do not leak content of stack */
448 memset(tmpbuf, 0, mydata->sect_size);
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200449 memcpy(tmpbuf, buffer, size);
450 ret = disk_write(startsect, 1, tmpbuf);
451 if (ret != 1) {
452 debug("Error writing data (got %d)\n", ret);
Donggeun Kim8f814002011-10-24 21:15:28 +0000453 return -1;
454 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000455 }
456
457 return 0;
458}
459
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900460static __u8 tmpbuf_cluster[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN);
461
462/*
463 * Read and modify data on existing and consecutive cluster blocks
464 */
465static int
466get_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
Donggeun Kim8f814002011-10-24 21:15:28 +0000575/*
576 * Find the first empty cluster
577 */
578static int find_empty_cluster(fsdata *mydata)
579{
580 __u32 fat_val, entry = 3;
581
582 while (1) {
Stefan Brüns9f95d812016-12-17 00:27:51 +0100583 fat_val = get_fatent(mydata, entry);
Donggeun Kim8f814002011-10-24 21:15:28 +0000584 if (fat_val == 0)
585 break;
586 entry++;
587 }
588
589 return entry;
590}
591
592/*
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900593 * Write directory entries in itr's buffer to block device
Donggeun Kim8f814002011-10-24 21:15:28 +0000594 */
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900595static int flush_dir_table(fat_itr *itr)
Donggeun Kim8f814002011-10-24 21:15:28 +0000596{
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900597 fsdata *mydata = itr->fsdata;
Donggeun Kim8f814002011-10-24 21:15:28 +0000598 int dir_newclust = 0;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900599 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
Donggeun Kim8f814002011-10-24 21:15:28 +0000600
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900601 if (set_cluster(mydata, itr->clust, itr->block, bytesperclust) != 0) {
602 printf("error: writing directory entry\n");
603 return -1;
Donggeun Kim8f814002011-10-24 21:15:28 +0000604 }
605 dir_newclust = find_empty_cluster(mydata);
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900606 set_fatent_value(mydata, itr->clust, dir_newclust);
Donggeun Kim8f814002011-10-24 21:15:28 +0000607 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);
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500611 else if (mydata->fatsize == 12)
612 set_fatent_value(mydata, dir_newclust, 0xff8);
Donggeun Kim8f814002011-10-24 21:15:28 +0000613
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900614 itr->clust = dir_newclust;
615 itr->next_clust = dir_newclust;
Donggeun Kim8f814002011-10-24 21:15:28 +0000616
Stefan Brüns751b31d2016-09-11 22:51:40 +0200617 if (flush_dirty_fat_buffer(mydata) < 0)
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900618 return -1;
619
620 memset(itr->block, 0x00, bytesperclust);
Donggeun Kim8f814002011-10-24 21:15:28 +0000621
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900622 itr->dent = (dir_entry *)itr->block;
623 itr->last_cluster = 1;
624 itr->remaining = bytesperclust / sizeof(dir_entry) - 1;
Donggeun Kim8f814002011-10-24 21:15:28 +0000625
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900626 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +0000627}
628
629/*
630 * Set empty cluster from 'entry' to the end of a file
631 */
632static int clear_fatent(fsdata *mydata, __u32 entry)
633{
634 __u32 fat_val;
635
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500636 while (!CHECK_CLUST(entry, mydata->fatsize)) {
Stefan Brüns9f95d812016-12-17 00:27:51 +0100637 fat_val = get_fatent(mydata, entry);
Donggeun Kim8f814002011-10-24 21:15:28 +0000638 if (fat_val != 0)
639 set_fatent_value(mydata, entry, 0);
640 else
641 break;
642
Donggeun Kim8f814002011-10-24 21:15:28 +0000643 entry = fat_val;
644 }
645
646 /* Flush fat buffer */
Stefan Brüns751b31d2016-09-11 22:51:40 +0200647 if (flush_dirty_fat_buffer(mydata) < 0)
Donggeun Kim8f814002011-10-24 21:15:28 +0000648 return -1;
649
650 return 0;
651}
652
653/*
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +0900654 * Set start cluster in directory entry
655 */
656static 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 */
670static 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/*
Donggeun Kim8f814002011-10-24 21:15:28 +0000690 * Write at most 'maxsize' bytes from 'buffer' into
691 * the file associated with 'dentptr'
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800692 * Update the number of bytes written in *gotsize and return 0
693 * or return -1 on fatal errors.
Donggeun Kim8f814002011-10-24 21:15:28 +0000694 */
695static int
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +0900696set_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, __u8 *buffer,
697 loff_t maxsize, loff_t *gotsize)
Donggeun Kim8f814002011-10-24 21:15:28 +0000698{
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +0900699 loff_t filesize;
Donggeun Kim8f814002011-10-24 21:15:28 +0000700 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
701 __u32 curclust = START(dentptr);
702 __u32 endclust = 0, newclust = 0;
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900703 loff_t cur_pos, offset, actsize, wsize;
Donggeun Kim8f814002011-10-24 21:15:28 +0000704
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800705 *gotsize = 0;
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900706 filesize = pos + maxsize;
Donggeun Kim8f814002011-10-24 21:15:28 +0000707
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800708 debug("%llu bytes\n", filesize);
Donggeun Kim8f814002011-10-24 21:15:28 +0000709
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900710 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");
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +0200742 return -1;
743 }
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900744
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;
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +0900827 }
828
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900829 curclust = endclust;
830 filesize -= cur_pos;
831 assert(!(cur_pos % bytesperclust));
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +0900832
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900833set_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 */
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +0900855 if (check_overflow(mydata, curclust, filesize)) {
856 printf("Error: no space left: %llu\n", filesize);
857 return -1;
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +0200858 }
859
Donggeun Kim8f814002011-10-24 21:15:28 +0000860 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)
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +0900868 /* write to <curclust..endclust> */
Donggeun Kim8f814002011-10-24 21:15:28 +0000869 goto getit;
870
871 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Benoît Thébaudeaue0b86942015-09-28 15:45:30 +0200872 debug("newclust: 0x%x\n", newclust);
Donggeun Kim8f814002011-10-24 21:15:28 +0000873 debug("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800874 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +0000875 }
876 endclust = newclust;
877 actsize += bytesperclust;
878 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000879
880 /* set remaining bytes */
Donggeun Kim8f814002011-10-24 21:15:28 +0000881 actsize = filesize;
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200882 if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
Donggeun Kim8f814002011-10-24 21:15:28 +0000883 debug("error: writing cluster\n");
884 return -1;
885 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800886 *gotsize += actsize;
Donggeun Kim8f814002011-10-24 21:15:28 +0000887
888 /* Mark end of file in FAT */
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500889 if (mydata->fatsize == 12)
890 newclust = 0xfff;
891 else if (mydata->fatsize == 16)
Donggeun Kim8f814002011-10-24 21:15:28 +0000892 newclust = 0xffff;
893 else if (mydata->fatsize == 32)
894 newclust = 0xfffffff;
895 set_fatent_value(mydata, endclust, newclust);
896
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800897 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +0000898getit:
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200899 if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
Donggeun Kim8f814002011-10-24 21:15:28 +0000900 debug("error: writing cluster\n");
901 return -1;
902 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800903 *gotsize += actsize;
Donggeun Kim8f814002011-10-24 21:15:28 +0000904 filesize -= actsize;
905 buffer += actsize;
906
Benoît Thébaudeaue0b86942015-09-28 15:45:30 +0200907 if (CHECK_CLUST(newclust, mydata->fatsize)) {
908 debug("newclust: 0x%x\n", newclust);
Donggeun Kim8f814002011-10-24 21:15:28 +0000909 debug("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800910 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +0000911 }
912 actsize = bytesperclust;
913 curclust = endclust = newclust;
914 } while (1);
Donggeun Kim8f814002011-10-24 21:15:28 +0000915
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +0900916 return 0;
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +0200917}
918
919/*
920 * Fill dir_entry
921 */
922static 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);
Donggeun Kim8f814002011-10-24 21:15:28 +0000926 dentptr->size = cpu_to_le32(size);
927
928 dentptr->attr = attr;
929
930 set_name(dentptr, filename);
931}
932
933/*
Donggeun Kim8f814002011-10-24 21:15:28 +0000934 * 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 */
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900938static dir_entry *find_directory_entry(fat_itr *itr, char *filename)
Donggeun Kim8f814002011-10-24 21:15:28 +0000939{
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900940 int match = 0;
Donggeun Kim8f814002011-10-24 21:15:28 +0000941
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900942 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;
Donggeun Kim8f814002011-10-24 21:15:28 +0000949
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900950 if (!match)
951 continue;
Donggeun Kim8f814002011-10-24 21:15:28 +0000952
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900953 if (itr->dent->name[0] == '\0')
Donggeun Kim8f814002011-10-24 21:15:28 +0000954 return NULL;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900955 else
956 return itr->dent;
957 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000958
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900959 if (!itr->dent && !itr->is_root && flush_dir_table(itr))
960 /* indicate that allocating dent failed */
961 itr->dent = NULL;
Donggeun Kim8f814002011-10-24 21:15:28 +0000962
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900963 return NULL;
964}
Donggeun Kim8f814002011-10-24 21:15:28 +0000965
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900966static int split_filename(char *filename, char **dirname, char **basename)
967{
968 char *p, *last_slash, *last_slash_cont;
Donggeun Kim8f814002011-10-24 21:15:28 +0000969
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900970again:
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;
Donggeun Kim8f814002011-10-24 21:15:28 +0000983 }
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900984 p++;
985 }
Wu, Josh675b23c2014-05-08 16:14:07 +0800986
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900987 if (last_slash) {
988 if (last_slash_cont == (filename + strlen(filename) - 1)) {
989 /* remove trailing slashes */
990 *last_slash = '\0';
991 goto again;
Wu, Josh675b23c2014-05-08 16:14:07 +0800992 }
993
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900994 if (last_slash == filename) {
995 /* avoid ""(null) directory */
996 *dirname = "/";
997 } else {
998 *last_slash = '\0';
999 *dirname = filename;
Donggeun Kim8f814002011-10-24 21:15:28 +00001000 }
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001001
1002 *last_slash_cont = '\0';
1003 *basename = last_slash_cont + 1;
1004 } else {
1005 *dirname = "/"; /* root by default */
1006 *basename = filename;
Donggeun Kim8f814002011-10-24 21:15:28 +00001007 }
1008
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001009 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +00001010}
1011
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001012static int normalize_longname(char *l_filename, const char *filename)
1013{
1014 const char *p, legal[] = "!#$%&\'()-.@^`_{}~";
Tom Rini0d01bdb2018-09-30 10:33:42 -04001015 unsigned char c;
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001016 int name_len;
1017
1018 /* Check that the filename is valid */
1019 for (p = filename; p < filename + strlen(filename); p++) {
1020 c = *p;
1021
1022 if (('0' <= c) && (c <= '9'))
1023 continue;
1024 if (('A' <= c) && (c <= 'Z'))
1025 continue;
1026 if (('a' <= c) && (c <= 'z'))
1027 continue;
1028 if (strchr(legal, c))
1029 continue;
1030 /* extended code */
1031 if ((0x80 <= c) && (c <= 0xff))
1032 continue;
1033
1034 return -1;
1035 }
1036
1037 /* Normalize it */
1038 name_len = strlen(filename);
1039 if (name_len >= VFAT_MAXLEN_BYTES)
1040 /* should return an error? */
1041 name_len = VFAT_MAXLEN_BYTES - 1;
1042
1043 memcpy(l_filename, filename, name_len);
1044 l_filename[name_len] = 0; /* terminate the string */
1045 downcase(l_filename, INT_MAX);
1046
1047 return 0;
1048}
1049
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001050int file_fat_write_at(const char *filename, loff_t pos, void *buffer,
1051 loff_t size, loff_t *actwrite)
Donggeun Kim8f814002011-10-24 21:15:28 +00001052{
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001053 dir_entry *retdent;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001054 fsdata datablock = { .fatbuf = NULL, };
Donggeun Kim8f814002011-10-24 21:15:28 +00001055 fsdata *mydata = &datablock;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001056 fat_itr *itr = NULL;
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001057 int ret = -1;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001058 char *filename_copy, *parent, *basename;
Donggeun Kim8f814002011-10-24 21:15:28 +00001059 char l_filename[VFAT_MAXLEN_BYTES];
1060
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001061 debug("writing %s\n", filename);
1062
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001063 filename_copy = strdup(filename);
1064 if (!filename_copy)
1065 return -ENOMEM;
Donggeun Kim8f814002011-10-24 21:15:28 +00001066
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001067 split_filename(filename_copy, &parent, &basename);
1068 if (!strlen(basename)) {
1069 ret = -EINVAL;
1070 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001071 }
1072
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001073 filename = basename;
1074 if (normalize_longname(l_filename, filename)) {
1075 printf("FAT: illegal filename (%s)\n", filename);
1076 ret = -EINVAL;
1077 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001078 }
1079
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001080 itr = malloc_cache_aligned(sizeof(fat_itr));
1081 if (!itr) {
1082 ret = -ENOMEM;
1083 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001084 }
1085
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001086 ret = fat_itr_root(itr, &datablock);
1087 if (ret)
Donggeun Kim8f814002011-10-24 21:15:28 +00001088 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001089
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001090 total_sector = datablock.total_sect;
1091
1092 ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1093 if (ret) {
1094 printf("%s: doesn't exist (%d)\n", parent, ret);
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001095 goto exit;
1096 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001097
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001098 retdent = find_directory_entry(itr, l_filename);
1099
Donggeun Kim8f814002011-10-24 21:15:28 +00001100 if (retdent) {
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001101 if (fat_itr_isdir(itr)) {
1102 ret = -EISDIR;
1103 goto exit;
1104 }
1105
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001106 /* A file exists */
1107 if (pos == -1)
1108 /* Append to the end */
1109 pos = FAT2CPU32(retdent->size);
1110 if (pos > retdent->size) {
1111 /* No hole allowed */
1112 ret = -EINVAL;
1113 goto exit;
1114 }
1115
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001116 /* Update file size in a directory entry */
1117 retdent->size = cpu_to_le32(pos + size);
Donggeun Kim8f814002011-10-24 21:15:28 +00001118 } else {
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001119 /* Create a new file */
1120
1121 if (itr->is_root) {
1122 /* root dir cannot have "." or ".." */
1123 if (!strcmp(l_filename, ".") ||
1124 !strcmp(l_filename, "..")) {
1125 ret = -EINVAL;
1126 goto exit;
1127 }
1128 }
1129
1130 if (!itr->dent) {
1131 printf("Error: allocating new dir entry\n");
1132 ret = -EIO;
1133 goto exit;
1134 }
1135
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001136 if (pos) {
1137 /* No hole allowed */
1138 ret = -EINVAL;
1139 goto exit;
1140 }
1141
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001142 memset(itr->dent, 0, sizeof(*itr->dent));
1143
Donggeun Kim8f814002011-10-24 21:15:28 +00001144 /* Set short name to set alias checksum field in dir_slot */
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001145 set_name(itr->dent, filename);
1146 if (fill_dir_slot(itr, filename)) {
1147 ret = -EIO;
1148 goto exit;
1149 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001150
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001151 /* Set attribute as archive for regular file */
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001152 fill_dentry(itr->fsdata, itr->dent, filename, 0, size, 0x20);
Donggeun Kim8f814002011-10-24 21:15:28 +00001153
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001154 retdent = itr->dent;
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001155 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001156
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001157 ret = set_contents(mydata, retdent, pos, buffer, size, actwrite);
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001158 if (ret < 0) {
1159 printf("Error: writing contents\n");
AKASHI Takahiro6aa77692018-09-11 15:59:03 +09001160 ret = -EIO;
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001161 goto exit;
1162 }
1163 debug("attempt to write 0x%llx bytes\n", *actwrite);
Donggeun Kim8f814002011-10-24 21:15:28 +00001164
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001165 /* Flush fat buffer */
Stefan Brüns751b31d2016-09-11 22:51:40 +02001166 ret = flush_dirty_fat_buffer(mydata);
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001167 if (ret) {
1168 printf("Error: flush fat buffer\n");
AKASHI Takahiro6aa77692018-09-11 15:59:03 +09001169 ret = -EIO;
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001170 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001171 }
1172
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001173 /* Write directory table to device */
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001174 ret = set_cluster(mydata, itr->clust, itr->block,
1175 mydata->clust_size * mydata->sect_size);
AKASHI Takahiro6aa77692018-09-11 15:59:03 +09001176 if (ret) {
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001177 printf("Error: writing directory entry\n");
AKASHI Takahiro6aa77692018-09-11 15:59:03 +09001178 ret = -EIO;
1179 }
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001180
Donggeun Kim8f814002011-10-24 21:15:28 +00001181exit:
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001182 free(filename_copy);
Donggeun Kim8f814002011-10-24 21:15:28 +00001183 free(mydata->fatbuf);
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001184 free(itr);
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001185 return ret;
Donggeun Kim8f814002011-10-24 21:15:28 +00001186}
1187
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001188int file_fat_write(const char *filename, void *buffer, loff_t offset,
1189 loff_t maxsize, loff_t *actwrite)
Donggeun Kim8f814002011-10-24 21:15:28 +00001190{
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001191 return file_fat_write_at(filename, offset, buffer, maxsize, actwrite);
Donggeun Kim8f814002011-10-24 21:15:28 +00001192}
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001193
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001194static int fat_dir_entries(fat_itr *itr)
1195{
1196 fat_itr *dirs;
1197 fsdata fsdata = { .fatbuf = NULL, }, *mydata = &fsdata;
1198 /* for FATBUFSIZE */
1199 int count;
1200
1201 dirs = malloc_cache_aligned(sizeof(fat_itr));
1202 if (!dirs) {
1203 debug("Error: allocating memory\n");
1204 count = -ENOMEM;
1205 goto exit;
1206 }
1207
1208 /* duplicate fsdata */
1209 fat_itr_child(dirs, itr);
1210 fsdata = *dirs->fsdata;
1211
1212 /* allocate local fat buffer */
1213 fsdata.fatbuf = malloc_cache_aligned(FATBUFSIZE);
1214 if (!fsdata.fatbuf) {
1215 debug("Error: allocating memory\n");
1216 count = -ENOMEM;
1217 goto exit;
1218 }
1219 fsdata.fatbufnum = -1;
1220 dirs->fsdata = &fsdata;
1221
1222 for (count = 0; fat_itr_next(dirs); count++)
1223 ;
1224
1225exit:
1226 free(fsdata.fatbuf);
1227 free(dirs);
1228 return count;
1229}
1230
1231static int delete_dentry(fat_itr *itr)
1232{
1233 fsdata *mydata = itr->fsdata;
1234 dir_entry *dentptr = itr->dent;
1235
1236 /* free cluster blocks */
1237 clear_fatent(mydata, START(dentptr));
1238 if (flush_dirty_fat_buffer(mydata) < 0) {
1239 printf("Error: flush fat buffer\n");
1240 return -EIO;
1241 }
1242
1243 /*
1244 * update a directory entry
1245 * TODO:
1246 * - long file name support
1247 * - find and mark the "new" first invalid entry as name[0]=0x00
1248 */
1249 memset(dentptr, 0, sizeof(*dentptr));
1250 dentptr->name[0] = 0xe5;
1251
1252 if (set_cluster(mydata, itr->clust, itr->block,
1253 mydata->clust_size * mydata->sect_size) != 0) {
1254 printf("error: writing directory entry\n");
1255 return -EIO;
1256 }
1257
1258 return 0;
1259}
1260
1261int fat_unlink(const char *filename)
1262{
1263 fsdata fsdata = { .fatbuf = NULL, };
1264 fat_itr *itr = NULL;
1265 int n_entries, ret;
1266 char *filename_copy, *dirname, *basename;
1267
1268 filename_copy = strdup(filename);
Heinrich Schuchardtb2242f62018-10-02 06:58:00 +02001269 if (!filename_copy) {
1270 printf("Error: allocating memory\n");
1271 ret = -ENOMEM;
1272 goto exit;
1273 }
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001274 split_filename(filename_copy, &dirname, &basename);
1275
1276 if (!strcmp(dirname, "/") && !strcmp(basename, "")) {
1277 printf("Error: cannot remove root\n");
1278 ret = -EINVAL;
1279 goto exit;
1280 }
1281
1282 itr = malloc_cache_aligned(sizeof(fat_itr));
1283 if (!itr) {
1284 printf("Error: allocating memory\n");
Heinrich Schuchardtb2242f62018-10-02 06:58:00 +02001285 ret = -ENOMEM;
1286 goto exit;
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001287 }
1288
1289 ret = fat_itr_root(itr, &fsdata);
1290 if (ret)
1291 goto exit;
1292
1293 total_sector = fsdata.total_sect;
1294
1295 ret = fat_itr_resolve(itr, dirname, TYPE_DIR);
1296 if (ret) {
1297 printf("%s: doesn't exist (%d)\n", dirname, ret);
1298 ret = -ENOENT;
1299 goto exit;
1300 }
1301
1302 if (!find_directory_entry(itr, basename)) {
1303 printf("%s: doesn't exist\n", basename);
1304 ret = -ENOENT;
1305 goto exit;
1306 }
1307
1308 if (fat_itr_isdir(itr)) {
1309 n_entries = fat_dir_entries(itr);
1310 if (n_entries < 0) {
1311 ret = n_entries;
1312 goto exit;
1313 }
1314 if (n_entries > 2) {
1315 printf("Error: directory is not empty: %d\n",
1316 n_entries);
1317 ret = -EINVAL;
1318 goto exit;
1319 }
1320 }
1321
1322 ret = delete_dentry(itr);
1323
1324exit:
1325 free(fsdata.fatbuf);
1326 free(itr);
1327 free(filename_copy);
1328
1329 return ret;
1330}
1331
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001332int fat_mkdir(const char *new_dirname)
1333{
1334 dir_entry *retdent;
1335 fsdata datablock = { .fatbuf = NULL, };
1336 fsdata *mydata = &datablock;
1337 fat_itr *itr = NULL;
1338 char *dirname_copy, *parent, *dirname;
1339 char l_dirname[VFAT_MAXLEN_BYTES];
1340 int ret = -1;
1341 loff_t actwrite;
1342 unsigned int bytesperclust;
1343 dir_entry *dotdent = NULL;
1344
1345 dirname_copy = strdup(new_dirname);
1346 if (!dirname_copy)
1347 goto exit;
1348
1349 split_filename(dirname_copy, &parent, &dirname);
1350 if (!strlen(dirname)) {
1351 ret = -EINVAL;
1352 goto exit;
1353 }
1354
1355 if (normalize_longname(l_dirname, dirname)) {
1356 printf("FAT: illegal filename (%s)\n", dirname);
1357 ret = -EINVAL;
1358 goto exit;
1359 }
1360
1361 itr = malloc_cache_aligned(sizeof(fat_itr));
1362 if (!itr) {
1363 ret = -ENOMEM;
1364 goto exit;
1365 }
1366
1367 ret = fat_itr_root(itr, &datablock);
1368 if (ret)
1369 goto exit;
1370
1371 total_sector = datablock.total_sect;
1372
1373 ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1374 if (ret) {
1375 printf("%s: doesn't exist (%d)\n", parent, ret);
1376 goto exit;
1377 }
1378
1379 retdent = find_directory_entry(itr, l_dirname);
1380
1381 if (retdent) {
1382 printf("%s: already exists\n", l_dirname);
1383 ret = -EEXIST;
1384 goto exit;
1385 } else {
1386 if (itr->is_root) {
1387 /* root dir cannot have "." or ".." */
1388 if (!strcmp(l_dirname, ".") ||
1389 !strcmp(l_dirname, "..")) {
1390 ret = -EINVAL;
1391 goto exit;
1392 }
1393 }
1394
1395 if (!itr->dent) {
1396 printf("Error: allocating new dir entry\n");
1397 ret = -EIO;
1398 goto exit;
1399 }
1400
1401 memset(itr->dent, 0, sizeof(*itr->dent));
1402
1403 /* Set short name to set alias checksum field in dir_slot */
1404 set_name(itr->dent, dirname);
1405 fill_dir_slot(itr, dirname);
1406
1407 /* Set attribute as archive for regular file */
1408 fill_dentry(itr->fsdata, itr->dent, dirname, 0, 0,
1409 ATTR_DIR | ATTR_ARCH);
1410
1411 retdent = itr->dent;
1412 }
1413
1414 /* Default entries */
1415 bytesperclust = mydata->clust_size * mydata->sect_size;
1416 dotdent = malloc_cache_aligned(bytesperclust);
1417 if (!dotdent) {
1418 ret = -ENOMEM;
1419 goto exit;
1420 }
1421 memset(dotdent, 0, bytesperclust);
1422
1423 memcpy(dotdent[0].name, ". ", 8);
1424 memcpy(dotdent[0].ext, " ", 3);
1425 dotdent[0].attr = ATTR_DIR | ATTR_ARCH;
1426
1427 memcpy(dotdent[1].name, ".. ", 8);
1428 memcpy(dotdent[1].ext, " ", 3);
1429 dotdent[1].attr = ATTR_DIR | ATTR_ARCH;
1430 set_start_cluster(mydata, &dotdent[1], itr->start_clust);
1431
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 /* Write twice for "." */
1439 set_start_cluster(mydata, &dotdent[0], START(retdent));
1440 ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
1441 bytesperclust, &actwrite);
1442 if (ret < 0) {
1443 printf("Error: writing contents\n");
1444 goto exit;
1445 }
1446
1447 /* Flush fat buffer */
1448 ret = flush_dirty_fat_buffer(mydata);
1449 if (ret) {
1450 printf("Error: flush fat buffer\n");
1451 goto exit;
1452 }
1453
1454 /* Write directory table to device */
1455 ret = set_cluster(mydata, itr->clust, itr->block,
1456 mydata->clust_size * mydata->sect_size);
1457 if (ret)
1458 printf("Error: writing directory entry\n");
1459
1460exit:
1461 free(dirname_copy);
1462 free(mydata->fatbuf);
1463 free(itr);
1464 free(dotdent);
1465 return ret;
1466}