blob: 11b85d35eded4a55b55b475d3a77f09d5a639a97 [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/**
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900391 * set_sectors() - write data to sectors
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200392 *
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900393 * Write 'size' bytes from 'buffer' into the specified sector.
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200394 *
395 * @mydata: data to be written
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900396 * @startsect: sector to be written to
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200397 * @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
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900402set_sectors(fsdata *mydata, u32 startsect, u8 *buffer, u32 size)
Donggeun Kim8f814002011-10-24 21:15:28 +0000403{
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900404 u32 nsects = 0;
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200405 int ret;
Donggeun Kim8f814002011-10-24 21:15:28 +0000406
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900407 debug("startsect: %d\n", startsect);
Donggeun Kim8f814002011-10-24 21:15:28 +0000408
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200409 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
410 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
411
Heinrich Schuchardtaa9d6ba2018-09-13 19:42:47 +0200412 debug("FAT: Misaligned buffer address (%p)\n", buffer);
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200413
414 while (size >= mydata->sect_size) {
415 memcpy(tmpbuf, buffer, mydata->sect_size);
416 ret = disk_write(startsect++, 1, tmpbuf);
417 if (ret != 1) {
418 debug("Error writing data (got %d)\n", ret);
419 return -1;
420 }
421
422 buffer += mydata->sect_size;
423 size -= mydata->sect_size;
424 }
425 } else if (size >= mydata->sect_size) {
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900426 nsects = size / mydata->sect_size;
427 ret = disk_write(startsect, nsects, buffer);
428 if (ret != nsects) {
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200429 debug("Error writing data (got %d)\n", ret);
Wu, Joshb0e38dd2013-07-24 17:55:30 +0800430 return -1;
431 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000432
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900433 startsect += nsects;
434 buffer += nsects * mydata->sect_size;
435 size -= nsects * mydata->sect_size;
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200436 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000437
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200438 if (size) {
439 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200440 /* Do not leak content of stack */
441 memset(tmpbuf, 0, mydata->sect_size);
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200442 memcpy(tmpbuf, buffer, size);
443 ret = disk_write(startsect, 1, tmpbuf);
444 if (ret != 1) {
445 debug("Error writing data (got %d)\n", ret);
Donggeun Kim8f814002011-10-24 21:15:28 +0000446 return -1;
447 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000448 }
449
450 return 0;
451}
452
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900453/**
454 * set_cluster() - write data to cluster
455 *
456 * Write 'size' bytes from 'buffer' into the specified cluster.
457 *
458 * @mydata: data to be written
459 * @clustnum: cluster to be written to
460 * @buffer: data to be written
461 * @size: bytes to be written (but not more than the size of a cluster)
462 * Return: 0 on success, -1 otherwise
463 */
464static int
465set_cluster(fsdata *mydata, u32 clustnum, u8 *buffer, u32 size)
466{
467 return set_sectors(mydata, clust_to_sect(mydata, clustnum),
468 buffer, size);
469}
470
471static int
472flush_dir(fat_itr *itr)
473{
474 fsdata *mydata = itr->fsdata;
475 u32 startsect, sect_offset, nsects;
476
477 if (!itr->is_root || mydata->fatsize == 32)
478 return set_cluster(mydata, itr->clust, itr->block,
479 mydata->clust_size * mydata->sect_size);
480
481 sect_offset = itr->clust * mydata->clust_size;
482 startsect = mydata->rootdir_sect + sect_offset;
483 /* do not write past the end of rootdir */
484 nsects = min_t(u32, mydata->clust_size,
485 mydata->rootdir_size - sect_offset);
486
487 return set_sectors(mydata, startsect, itr->block,
488 nsects * mydata->sect_size);
489}
490
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900491static __u8 tmpbuf_cluster[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN);
492
493/*
494 * Read and modify data on existing and consecutive cluster blocks
495 */
496static int
497get_set_cluster(fsdata *mydata, __u32 clustnum, loff_t pos, __u8 *buffer,
498 loff_t size, loff_t *gotsize)
499{
500 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
501 __u32 startsect;
502 loff_t wsize;
503 int clustcount, i, ret;
504
505 *gotsize = 0;
506 if (!size)
507 return 0;
508
509 assert(pos < bytesperclust);
510 startsect = clust_to_sect(mydata, clustnum);
511
512 debug("clustnum: %d, startsect: %d, pos: %lld\n",
513 clustnum, startsect, pos);
514
515 /* partial write at beginning */
516 if (pos) {
517 wsize = min(bytesperclust - pos, size);
518 ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster);
519 if (ret != mydata->clust_size) {
520 debug("Error reading data (got %d)\n", ret);
521 return -1;
522 }
523
524 memcpy(tmpbuf_cluster + pos, buffer, wsize);
525 ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster);
526 if (ret != mydata->clust_size) {
527 debug("Error writing data (got %d)\n", ret);
528 return -1;
529 }
530
531 size -= wsize;
532 buffer += wsize;
533 *gotsize += wsize;
534
535 startsect += mydata->clust_size;
536
537 if (!size)
538 return 0;
539 }
540
541 /* full-cluster write */
542 if (size >= bytesperclust) {
543 clustcount = lldiv(size, bytesperclust);
544
545 if (!((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1))) {
546 wsize = clustcount * bytesperclust;
547 ret = disk_write(startsect,
548 clustcount * mydata->clust_size,
549 buffer);
550 if (ret != clustcount * mydata->clust_size) {
551 debug("Error writing data (got %d)\n", ret);
552 return -1;
553 }
554
555 size -= wsize;
556 buffer += wsize;
557 *gotsize += wsize;
558
559 startsect += clustcount * mydata->clust_size;
560 } else {
561 for (i = 0; i < clustcount; i++) {
562 memcpy(tmpbuf_cluster, buffer, bytesperclust);
563 ret = disk_write(startsect,
564 mydata->clust_size,
565 tmpbuf_cluster);
566 if (ret != mydata->clust_size) {
567 debug("Error writing data (got %d)\n",
568 ret);
569 return -1;
570 }
571
572 size -= bytesperclust;
573 buffer += bytesperclust;
574 *gotsize += bytesperclust;
575
576 startsect += mydata->clust_size;
577 }
578 }
579 }
580
581 /* partial write at end */
582 if (size) {
583 wsize = size;
584 ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster);
585 if (ret != mydata->clust_size) {
586 debug("Error reading data (got %d)\n", ret);
587 return -1;
588 }
589 memcpy(tmpbuf_cluster, buffer, wsize);
590 ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster);
591 if (ret != mydata->clust_size) {
592 debug("Error writing data (got %d)\n", ret);
593 return -1;
594 }
595
596 size -= wsize;
597 buffer += wsize;
598 *gotsize += wsize;
599 }
600
601 assert(!size);
602
603 return 0;
604}
605
Donggeun Kim8f814002011-10-24 21:15:28 +0000606/*
607 * Find the first empty cluster
608 */
609static int find_empty_cluster(fsdata *mydata)
610{
611 __u32 fat_val, entry = 3;
612
613 while (1) {
Stefan Brüns9f95d812016-12-17 00:27:51 +0100614 fat_val = get_fatent(mydata, entry);
Donggeun Kim8f814002011-10-24 21:15:28 +0000615 if (fat_val == 0)
616 break;
617 entry++;
618 }
619
620 return entry;
621}
622
623/*
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900624 * Write directory entries in itr's buffer to block device
Donggeun Kim8f814002011-10-24 21:15:28 +0000625 */
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900626static int flush_dir_table(fat_itr *itr)
Donggeun Kim8f814002011-10-24 21:15:28 +0000627{
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900628 fsdata *mydata = itr->fsdata;
Donggeun Kim8f814002011-10-24 21:15:28 +0000629 int dir_newclust = 0;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900630 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
Donggeun Kim8f814002011-10-24 21:15:28 +0000631
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900632 if (set_cluster(mydata, itr->clust, itr->block, bytesperclust) != 0) {
633 printf("error: writing directory entry\n");
634 return -1;
Donggeun Kim8f814002011-10-24 21:15:28 +0000635 }
636 dir_newclust = find_empty_cluster(mydata);
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900637 set_fatent_value(mydata, itr->clust, dir_newclust);
Donggeun Kim8f814002011-10-24 21:15:28 +0000638 if (mydata->fatsize == 32)
639 set_fatent_value(mydata, dir_newclust, 0xffffff8);
640 else if (mydata->fatsize == 16)
641 set_fatent_value(mydata, dir_newclust, 0xfff8);
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500642 else if (mydata->fatsize == 12)
643 set_fatent_value(mydata, dir_newclust, 0xff8);
Donggeun Kim8f814002011-10-24 21:15:28 +0000644
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900645 itr->clust = dir_newclust;
646 itr->next_clust = dir_newclust;
Donggeun Kim8f814002011-10-24 21:15:28 +0000647
Stefan Brüns751b31d2016-09-11 22:51:40 +0200648 if (flush_dirty_fat_buffer(mydata) < 0)
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900649 return -1;
650
651 memset(itr->block, 0x00, bytesperclust);
Donggeun Kim8f814002011-10-24 21:15:28 +0000652
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900653 itr->dent = (dir_entry *)itr->block;
654 itr->last_cluster = 1;
655 itr->remaining = bytesperclust / sizeof(dir_entry) - 1;
Donggeun Kim8f814002011-10-24 21:15:28 +0000656
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900657 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +0000658}
659
660/*
661 * Set empty cluster from 'entry' to the end of a file
662 */
663static int clear_fatent(fsdata *mydata, __u32 entry)
664{
665 __u32 fat_val;
666
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500667 while (!CHECK_CLUST(entry, mydata->fatsize)) {
Stefan Brüns9f95d812016-12-17 00:27:51 +0100668 fat_val = get_fatent(mydata, entry);
Donggeun Kim8f814002011-10-24 21:15:28 +0000669 if (fat_val != 0)
670 set_fatent_value(mydata, entry, 0);
671 else
672 break;
673
Donggeun Kim8f814002011-10-24 21:15:28 +0000674 entry = fat_val;
675 }
676
677 /* Flush fat buffer */
Stefan Brüns751b31d2016-09-11 22:51:40 +0200678 if (flush_dirty_fat_buffer(mydata) < 0)
Donggeun Kim8f814002011-10-24 21:15:28 +0000679 return -1;
680
681 return 0;
682}
683
684/*
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +0900685 * Set start cluster in directory entry
686 */
687static void set_start_cluster(const fsdata *mydata, dir_entry *dentptr,
688 __u32 start_cluster)
689{
690 if (mydata->fatsize == 32)
691 dentptr->starthi =
692 cpu_to_le16((start_cluster & 0xffff0000) >> 16);
693 dentptr->start = cpu_to_le16(start_cluster & 0xffff);
694}
695
696/*
697 * Check whether adding a file makes the file system to
698 * exceed the size of the block device
699 * Return -1 when overflow occurs, otherwise return 0
700 */
701static int check_overflow(fsdata *mydata, __u32 clustnum, loff_t size)
702{
703 __u32 startsect, sect_num, offset;
704
705 if (clustnum > 0)
706 startsect = clust_to_sect(mydata, clustnum);
707 else
708 startsect = mydata->rootdir_sect;
709
710 sect_num = div_u64_rem(size, mydata->sect_size, &offset);
711
712 if (offset != 0)
713 sect_num++;
714
715 if (startsect + sect_num > total_sector)
716 return -1;
717 return 0;
718}
719
720/*
Donggeun Kim8f814002011-10-24 21:15:28 +0000721 * Write at most 'maxsize' bytes from 'buffer' into
722 * the file associated with 'dentptr'
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800723 * Update the number of bytes written in *gotsize and return 0
724 * or return -1 on fatal errors.
Donggeun Kim8f814002011-10-24 21:15:28 +0000725 */
726static int
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +0900727set_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, __u8 *buffer,
728 loff_t maxsize, loff_t *gotsize)
Donggeun Kim8f814002011-10-24 21:15:28 +0000729{
Donggeun Kim8f814002011-10-24 21:15:28 +0000730 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
731 __u32 curclust = START(dentptr);
732 __u32 endclust = 0, newclust = 0;
Heinrich Schuchardtdb538a92019-02-25 19:42:48 +0100733 u64 cur_pos, filesize;
734 loff_t offset, actsize, wsize;
Donggeun Kim8f814002011-10-24 21:15:28 +0000735
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800736 *gotsize = 0;
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900737 filesize = pos + maxsize;
Donggeun Kim8f814002011-10-24 21:15:28 +0000738
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800739 debug("%llu bytes\n", filesize);
Donggeun Kim8f814002011-10-24 21:15:28 +0000740
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900741 if (!filesize) {
742 if (!curclust)
743 return 0;
744 if (!CHECK_CLUST(curclust, mydata->fatsize) ||
745 IS_LAST_CLUST(curclust, mydata->fatsize)) {
746 clear_fatent(mydata, curclust);
747 set_start_cluster(mydata, dentptr, 0);
748 return 0;
749 }
750 debug("curclust: 0x%x\n", curclust);
751 debug("Invalid FAT entry\n");
752 return -1;
753 }
754
755 if (!curclust) {
756 assert(pos == 0);
757 goto set_clusters;
758 }
759
760 /* go to cluster at pos */
761 cur_pos = bytesperclust;
762 while (1) {
763 if (pos <= cur_pos)
764 break;
765 if (IS_LAST_CLUST(curclust, mydata->fatsize))
766 break;
767
768 newclust = get_fatent(mydata, curclust);
769 if (!IS_LAST_CLUST(newclust, mydata->fatsize) &&
770 CHECK_CLUST(newclust, mydata->fatsize)) {
771 debug("curclust: 0x%x\n", curclust);
772 debug("Invalid FAT entry\n");
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +0200773 return -1;
774 }
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900775
776 cur_pos += bytesperclust;
777 curclust = newclust;
778 }
779 if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
780 assert(pos == cur_pos);
781 goto set_clusters;
782 }
783
784 assert(pos < cur_pos);
785 cur_pos -= bytesperclust;
786
787 /* overwrite */
788 assert(IS_LAST_CLUST(curclust, mydata->fatsize) ||
789 !CHECK_CLUST(curclust, mydata->fatsize));
790
791 while (1) {
792 /* search for allocated consecutive clusters */
793 actsize = bytesperclust;
794 endclust = curclust;
795 while (1) {
796 if (filesize <= (cur_pos + actsize))
797 break;
798
799 newclust = get_fatent(mydata, endclust);
800
801 if (IS_LAST_CLUST(newclust, mydata->fatsize))
802 break;
803 if (CHECK_CLUST(newclust, mydata->fatsize)) {
804 debug("curclust: 0x%x\n", curclust);
805 debug("Invalid FAT entry\n");
806 return -1;
807 }
808
809 actsize += bytesperclust;
810 endclust = newclust;
811 }
812
813 /* overwrite to <curclust..endclust> */
814 if (pos < cur_pos)
815 offset = 0;
816 else
817 offset = pos - cur_pos;
818 wsize = min(cur_pos + actsize, filesize) - pos;
819 if (get_set_cluster(mydata, curclust, offset,
820 buffer, wsize, &actsize)) {
821 printf("Error get-and-setting cluster\n");
822 return -1;
823 }
824 buffer += wsize;
825 *gotsize += wsize;
826 cur_pos += offset + wsize;
827
828 if (filesize <= cur_pos)
829 break;
830
831 /* CHECK: newclust = get_fatent(mydata, endclust); */
832
833 if (IS_LAST_CLUST(newclust, mydata->fatsize))
834 /* no more clusters */
835 break;
836
837 curclust = newclust;
838 }
839
840 if (filesize <= cur_pos) {
841 /* no more write */
842 newclust = get_fatent(mydata, endclust);
843 if (!IS_LAST_CLUST(newclust, mydata->fatsize)) {
844 /* truncate the rest */
845 clear_fatent(mydata, newclust);
846
847 /* Mark end of file in FAT */
848 if (mydata->fatsize == 12)
849 newclust = 0xfff;
850 else if (mydata->fatsize == 16)
851 newclust = 0xffff;
852 else if (mydata->fatsize == 32)
853 newclust = 0xfffffff;
854 set_fatent_value(mydata, endclust, newclust);
855 }
856
857 return 0;
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +0900858 }
859
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900860 curclust = endclust;
861 filesize -= cur_pos;
Heinrich Schuchardtdb538a92019-02-25 19:42:48 +0100862 assert(!do_div(cur_pos, bytesperclust));
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +0900863
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900864set_clusters:
865 /* allocate and write */
866 assert(!pos);
867
868 /* Assure that curclust is valid */
869 if (!curclust) {
870 curclust = find_empty_cluster(mydata);
871 set_start_cluster(mydata, dentptr, curclust);
872 } else {
873 newclust = get_fatent(mydata, curclust);
874
875 if (IS_LAST_CLUST(newclust, mydata->fatsize)) {
876 newclust = determine_fatent(mydata, curclust);
877 set_fatent_value(mydata, curclust, newclust);
878 curclust = newclust;
879 } else {
880 debug("error: something wrong\n");
881 return -1;
882 }
883 }
884
885 /* TODO: already partially written */
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +0900886 if (check_overflow(mydata, curclust, filesize)) {
887 printf("Error: no space left: %llu\n", filesize);
888 return -1;
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +0200889 }
890
Donggeun Kim8f814002011-10-24 21:15:28 +0000891 actsize = bytesperclust;
892 endclust = curclust;
893 do {
894 /* search for consecutive clusters */
895 while (actsize < filesize) {
896 newclust = determine_fatent(mydata, endclust);
897
898 if ((newclust - 1) != endclust)
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +0900899 /* write to <curclust..endclust> */
Donggeun Kim8f814002011-10-24 21:15:28 +0000900 goto getit;
901
902 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Benoît Thébaudeaue0b86942015-09-28 15:45:30 +0200903 debug("newclust: 0x%x\n", newclust);
Donggeun Kim8f814002011-10-24 21:15:28 +0000904 debug("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800905 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +0000906 }
907 endclust = newclust;
908 actsize += bytesperclust;
909 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000910
911 /* set remaining bytes */
Donggeun Kim8f814002011-10-24 21:15:28 +0000912 actsize = filesize;
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200913 if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
Donggeun Kim8f814002011-10-24 21:15:28 +0000914 debug("error: writing cluster\n");
915 return -1;
916 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800917 *gotsize += actsize;
Donggeun Kim8f814002011-10-24 21:15:28 +0000918
919 /* Mark end of file in FAT */
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500920 if (mydata->fatsize == 12)
921 newclust = 0xfff;
922 else if (mydata->fatsize == 16)
Donggeun Kim8f814002011-10-24 21:15:28 +0000923 newclust = 0xffff;
924 else if (mydata->fatsize == 32)
925 newclust = 0xfffffff;
926 set_fatent_value(mydata, endclust, newclust);
927
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800928 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +0000929getit:
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200930 if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
Donggeun Kim8f814002011-10-24 21:15:28 +0000931 debug("error: writing cluster\n");
932 return -1;
933 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800934 *gotsize += actsize;
Donggeun Kim8f814002011-10-24 21:15:28 +0000935 filesize -= actsize;
936 buffer += actsize;
937
Benoît Thébaudeaue0b86942015-09-28 15:45:30 +0200938 if (CHECK_CLUST(newclust, mydata->fatsize)) {
939 debug("newclust: 0x%x\n", newclust);
Donggeun Kim8f814002011-10-24 21:15:28 +0000940 debug("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800941 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +0000942 }
943 actsize = bytesperclust;
944 curclust = endclust = newclust;
945 } while (1);
Donggeun Kim8f814002011-10-24 21:15:28 +0000946
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +0900947 return 0;
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +0200948}
949
950/*
951 * Fill dir_entry
952 */
953static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
954 const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
955{
956 set_start_cluster(mydata, dentptr, start_cluster);
Donggeun Kim8f814002011-10-24 21:15:28 +0000957 dentptr->size = cpu_to_le32(size);
958
959 dentptr->attr = attr;
960
961 set_name(dentptr, filename);
962}
963
964/*
Donggeun Kim8f814002011-10-24 21:15:28 +0000965 * Find a directory entry based on filename or start cluster number
966 * If the directory entry is not found,
967 * the new position for writing a directory entry will be returned
968 */
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900969static dir_entry *find_directory_entry(fat_itr *itr, char *filename)
Donggeun Kim8f814002011-10-24 21:15:28 +0000970{
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900971 int match = 0;
Donggeun Kim8f814002011-10-24 21:15:28 +0000972
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900973 while (fat_itr_next(itr)) {
974 /* check both long and short name: */
975 if (!strcasecmp(filename, itr->name))
976 match = 1;
977 else if (itr->name != itr->s_name &&
978 !strcasecmp(filename, itr->s_name))
979 match = 1;
Donggeun Kim8f814002011-10-24 21:15:28 +0000980
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900981 if (!match)
982 continue;
Donggeun Kim8f814002011-10-24 21:15:28 +0000983
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900984 if (itr->dent->name[0] == '\0')
Donggeun Kim8f814002011-10-24 21:15:28 +0000985 return NULL;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900986 else
987 return itr->dent;
988 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000989
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900990 if (!itr->dent && !itr->is_root && flush_dir_table(itr))
991 /* indicate that allocating dent failed */
992 itr->dent = NULL;
Donggeun Kim8f814002011-10-24 21:15:28 +0000993
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900994 return NULL;
995}
Donggeun Kim8f814002011-10-24 21:15:28 +0000996
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900997static int split_filename(char *filename, char **dirname, char **basename)
998{
999 char *p, *last_slash, *last_slash_cont;
Donggeun Kim8f814002011-10-24 21:15:28 +00001000
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001001again:
1002 p = filename;
1003 last_slash = NULL;
1004 last_slash_cont = NULL;
1005 while (*p) {
1006 if (ISDIRDELIM(*p)) {
1007 last_slash = p;
1008 last_slash_cont = p;
1009 /* continuous slashes */
1010 while (ISDIRDELIM(*p))
1011 last_slash_cont = p++;
1012 if (!*p)
1013 break;
Donggeun Kim8f814002011-10-24 21:15:28 +00001014 }
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001015 p++;
1016 }
Wu, Josh675b23c2014-05-08 16:14:07 +08001017
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001018 if (last_slash) {
1019 if (last_slash_cont == (filename + strlen(filename) - 1)) {
1020 /* remove trailing slashes */
1021 *last_slash = '\0';
1022 goto again;
Wu, Josh675b23c2014-05-08 16:14:07 +08001023 }
1024
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001025 if (last_slash == filename) {
1026 /* avoid ""(null) directory */
1027 *dirname = "/";
1028 } else {
1029 *last_slash = '\0';
1030 *dirname = filename;
Donggeun Kim8f814002011-10-24 21:15:28 +00001031 }
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001032
1033 *last_slash_cont = '\0';
1034 *basename = last_slash_cont + 1;
1035 } else {
1036 *dirname = "/"; /* root by default */
1037 *basename = filename;
Donggeun Kim8f814002011-10-24 21:15:28 +00001038 }
1039
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001040 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +00001041}
1042
Heinrich Schuchardt58587212019-05-12 09:59:18 +02001043/**
1044 * normalize_longname() - check long file name and convert to lower case
1045 *
1046 * We assume here that the FAT file system is using an 8bit code page.
1047 * Linux typically uses CP437, EDK2 assumes CP1250.
1048 *
1049 * @l_filename: preallocated buffer receiving the normalized name
1050 * @filename: filename to normalize
1051 * Return: 0 on success, -1 on failure
1052 */
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001053static int normalize_longname(char *l_filename, const char *filename)
1054{
Heinrich Schuchardt58587212019-05-12 09:59:18 +02001055 const char *p, illegal[] = "<>:\"/\\|?*";
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001056
Heinrich Schuchardt58587212019-05-12 09:59:18 +02001057 if (strlen(filename) >= VFAT_MAXLEN_BYTES)
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001058 return -1;
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001059
Heinrich Schuchardt58587212019-05-12 09:59:18 +02001060 for (p = filename; *p; ++p) {
1061 if ((unsigned char)*p < 0x20)
1062 return -1;
1063 if (strchr(illegal, *p))
1064 return -1;
1065 }
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001066
Heinrich Schuchardt58587212019-05-12 09:59:18 +02001067 strcpy(l_filename, filename);
1068 downcase(l_filename, VFAT_MAXLEN_BYTES);
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001069
1070 return 0;
1071}
1072
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001073int file_fat_write_at(const char *filename, loff_t pos, void *buffer,
1074 loff_t size, loff_t *actwrite)
Donggeun Kim8f814002011-10-24 21:15:28 +00001075{
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001076 dir_entry *retdent;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001077 fsdata datablock = { .fatbuf = NULL, };
Donggeun Kim8f814002011-10-24 21:15:28 +00001078 fsdata *mydata = &datablock;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001079 fat_itr *itr = NULL;
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001080 int ret = -1;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001081 char *filename_copy, *parent, *basename;
Donggeun Kim8f814002011-10-24 21:15:28 +00001082 char l_filename[VFAT_MAXLEN_BYTES];
1083
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001084 debug("writing %s\n", filename);
1085
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001086 filename_copy = strdup(filename);
1087 if (!filename_copy)
1088 return -ENOMEM;
Donggeun Kim8f814002011-10-24 21:15:28 +00001089
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001090 split_filename(filename_copy, &parent, &basename);
1091 if (!strlen(basename)) {
1092 ret = -EINVAL;
1093 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001094 }
1095
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001096 filename = basename;
1097 if (normalize_longname(l_filename, filename)) {
1098 printf("FAT: illegal filename (%s)\n", filename);
1099 ret = -EINVAL;
1100 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001101 }
1102
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001103 itr = malloc_cache_aligned(sizeof(fat_itr));
1104 if (!itr) {
1105 ret = -ENOMEM;
1106 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001107 }
1108
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001109 ret = fat_itr_root(itr, &datablock);
1110 if (ret)
Donggeun Kim8f814002011-10-24 21:15:28 +00001111 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001112
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001113 total_sector = datablock.total_sect;
1114
1115 ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1116 if (ret) {
1117 printf("%s: doesn't exist (%d)\n", parent, ret);
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001118 goto exit;
1119 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001120
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001121 retdent = find_directory_entry(itr, l_filename);
1122
Donggeun Kim8f814002011-10-24 21:15:28 +00001123 if (retdent) {
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001124 if (fat_itr_isdir(itr)) {
1125 ret = -EISDIR;
1126 goto exit;
1127 }
1128
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001129 /* A file exists */
1130 if (pos == -1)
1131 /* Append to the end */
1132 pos = FAT2CPU32(retdent->size);
1133 if (pos > retdent->size) {
1134 /* No hole allowed */
1135 ret = -EINVAL;
1136 goto exit;
1137 }
1138
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001139 /* Update file size in a directory entry */
1140 retdent->size = cpu_to_le32(pos + size);
Donggeun Kim8f814002011-10-24 21:15:28 +00001141 } else {
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001142 /* Create a new file */
1143
1144 if (itr->is_root) {
1145 /* root dir cannot have "." or ".." */
1146 if (!strcmp(l_filename, ".") ||
1147 !strcmp(l_filename, "..")) {
1148 ret = -EINVAL;
1149 goto exit;
1150 }
1151 }
1152
1153 if (!itr->dent) {
1154 printf("Error: allocating new dir entry\n");
1155 ret = -EIO;
1156 goto exit;
1157 }
1158
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001159 if (pos) {
1160 /* No hole allowed */
1161 ret = -EINVAL;
1162 goto exit;
1163 }
1164
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001165 memset(itr->dent, 0, sizeof(*itr->dent));
1166
Donggeun Kim8f814002011-10-24 21:15:28 +00001167 /* Set short name to set alias checksum field in dir_slot */
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001168 set_name(itr->dent, filename);
1169 if (fill_dir_slot(itr, filename)) {
1170 ret = -EIO;
1171 goto exit;
1172 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001173
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001174 /* Set attribute as archive for regular file */
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001175 fill_dentry(itr->fsdata, itr->dent, filename, 0, size, 0x20);
Donggeun Kim8f814002011-10-24 21:15:28 +00001176
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001177 retdent = itr->dent;
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001178 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001179
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001180 ret = set_contents(mydata, retdent, pos, buffer, size, actwrite);
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001181 if (ret < 0) {
1182 printf("Error: writing contents\n");
AKASHI Takahiro6aa77692018-09-11 15:59:03 +09001183 ret = -EIO;
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001184 goto exit;
1185 }
1186 debug("attempt to write 0x%llx bytes\n", *actwrite);
Donggeun Kim8f814002011-10-24 21:15:28 +00001187
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001188 /* Flush fat buffer */
Stefan Brüns751b31d2016-09-11 22:51:40 +02001189 ret = flush_dirty_fat_buffer(mydata);
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001190 if (ret) {
1191 printf("Error: flush fat buffer\n");
AKASHI Takahiro6aa77692018-09-11 15:59:03 +09001192 ret = -EIO;
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001193 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001194 }
1195
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001196 /* Write directory table to device */
AKASHI Takahirod98c6742019-05-24 14:10:35 +09001197 ret = flush_dir(itr);
AKASHI Takahiro6aa77692018-09-11 15:59:03 +09001198 if (ret) {
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001199 printf("Error: writing directory entry\n");
AKASHI Takahiro6aa77692018-09-11 15:59:03 +09001200 ret = -EIO;
1201 }
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001202
Donggeun Kim8f814002011-10-24 21:15:28 +00001203exit:
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001204 free(filename_copy);
Donggeun Kim8f814002011-10-24 21:15:28 +00001205 free(mydata->fatbuf);
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001206 free(itr);
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001207 return ret;
Donggeun Kim8f814002011-10-24 21:15:28 +00001208}
1209
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001210int file_fat_write(const char *filename, void *buffer, loff_t offset,
1211 loff_t maxsize, loff_t *actwrite)
Donggeun Kim8f814002011-10-24 21:15:28 +00001212{
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001213 return file_fat_write_at(filename, offset, buffer, maxsize, actwrite);
Donggeun Kim8f814002011-10-24 21:15:28 +00001214}
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001215
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001216static int fat_dir_entries(fat_itr *itr)
1217{
1218 fat_itr *dirs;
1219 fsdata fsdata = { .fatbuf = NULL, }, *mydata = &fsdata;
1220 /* for FATBUFSIZE */
1221 int count;
1222
1223 dirs = malloc_cache_aligned(sizeof(fat_itr));
1224 if (!dirs) {
1225 debug("Error: allocating memory\n");
1226 count = -ENOMEM;
1227 goto exit;
1228 }
1229
1230 /* duplicate fsdata */
1231 fat_itr_child(dirs, itr);
1232 fsdata = *dirs->fsdata;
1233
1234 /* allocate local fat buffer */
1235 fsdata.fatbuf = malloc_cache_aligned(FATBUFSIZE);
1236 if (!fsdata.fatbuf) {
1237 debug("Error: allocating memory\n");
1238 count = -ENOMEM;
1239 goto exit;
1240 }
1241 fsdata.fatbufnum = -1;
1242 dirs->fsdata = &fsdata;
1243
1244 for (count = 0; fat_itr_next(dirs); count++)
1245 ;
1246
1247exit:
1248 free(fsdata.fatbuf);
1249 free(dirs);
1250 return count;
1251}
1252
1253static int delete_dentry(fat_itr *itr)
1254{
1255 fsdata *mydata = itr->fsdata;
1256 dir_entry *dentptr = itr->dent;
1257
1258 /* free cluster blocks */
1259 clear_fatent(mydata, START(dentptr));
1260 if (flush_dirty_fat_buffer(mydata) < 0) {
1261 printf("Error: flush fat buffer\n");
1262 return -EIO;
1263 }
1264
1265 /*
1266 * update a directory entry
1267 * TODO:
1268 * - long file name support
1269 * - find and mark the "new" first invalid entry as name[0]=0x00
1270 */
1271 memset(dentptr, 0, sizeof(*dentptr));
1272 dentptr->name[0] = 0xe5;
1273
AKASHI Takahirod98c6742019-05-24 14:10:35 +09001274 if (flush_dir(itr)) {
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001275 printf("error: writing directory entry\n");
1276 return -EIO;
1277 }
1278
1279 return 0;
1280}
1281
1282int fat_unlink(const char *filename)
1283{
1284 fsdata fsdata = { .fatbuf = NULL, };
1285 fat_itr *itr = NULL;
1286 int n_entries, ret;
1287 char *filename_copy, *dirname, *basename;
1288
1289 filename_copy = strdup(filename);
Heinrich Schuchardtb2242f62018-10-02 06:58:00 +02001290 if (!filename_copy) {
1291 printf("Error: allocating memory\n");
1292 ret = -ENOMEM;
1293 goto exit;
1294 }
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001295 split_filename(filename_copy, &dirname, &basename);
1296
1297 if (!strcmp(dirname, "/") && !strcmp(basename, "")) {
1298 printf("Error: cannot remove root\n");
1299 ret = -EINVAL;
1300 goto exit;
1301 }
1302
1303 itr = malloc_cache_aligned(sizeof(fat_itr));
1304 if (!itr) {
1305 printf("Error: allocating memory\n");
Heinrich Schuchardtb2242f62018-10-02 06:58:00 +02001306 ret = -ENOMEM;
1307 goto exit;
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001308 }
1309
1310 ret = fat_itr_root(itr, &fsdata);
1311 if (ret)
1312 goto exit;
1313
1314 total_sector = fsdata.total_sect;
1315
1316 ret = fat_itr_resolve(itr, dirname, TYPE_DIR);
1317 if (ret) {
1318 printf("%s: doesn't exist (%d)\n", dirname, ret);
1319 ret = -ENOENT;
1320 goto exit;
1321 }
1322
1323 if (!find_directory_entry(itr, basename)) {
1324 printf("%s: doesn't exist\n", basename);
1325 ret = -ENOENT;
1326 goto exit;
1327 }
1328
1329 if (fat_itr_isdir(itr)) {
1330 n_entries = fat_dir_entries(itr);
1331 if (n_entries < 0) {
1332 ret = n_entries;
1333 goto exit;
1334 }
1335 if (n_entries > 2) {
1336 printf("Error: directory is not empty: %d\n",
1337 n_entries);
1338 ret = -EINVAL;
1339 goto exit;
1340 }
1341 }
1342
1343 ret = delete_dentry(itr);
1344
1345exit:
1346 free(fsdata.fatbuf);
1347 free(itr);
1348 free(filename_copy);
1349
1350 return ret;
1351}
1352
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001353int fat_mkdir(const char *new_dirname)
1354{
1355 dir_entry *retdent;
1356 fsdata datablock = { .fatbuf = NULL, };
1357 fsdata *mydata = &datablock;
1358 fat_itr *itr = NULL;
1359 char *dirname_copy, *parent, *dirname;
1360 char l_dirname[VFAT_MAXLEN_BYTES];
1361 int ret = -1;
1362 loff_t actwrite;
1363 unsigned int bytesperclust;
1364 dir_entry *dotdent = NULL;
1365
1366 dirname_copy = strdup(new_dirname);
1367 if (!dirname_copy)
1368 goto exit;
1369
1370 split_filename(dirname_copy, &parent, &dirname);
1371 if (!strlen(dirname)) {
1372 ret = -EINVAL;
1373 goto exit;
1374 }
1375
1376 if (normalize_longname(l_dirname, dirname)) {
1377 printf("FAT: illegal filename (%s)\n", dirname);
1378 ret = -EINVAL;
1379 goto exit;
1380 }
1381
1382 itr = malloc_cache_aligned(sizeof(fat_itr));
1383 if (!itr) {
1384 ret = -ENOMEM;
1385 goto exit;
1386 }
1387
1388 ret = fat_itr_root(itr, &datablock);
1389 if (ret)
1390 goto exit;
1391
1392 total_sector = datablock.total_sect;
1393
1394 ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1395 if (ret) {
1396 printf("%s: doesn't exist (%d)\n", parent, ret);
1397 goto exit;
1398 }
1399
1400 retdent = find_directory_entry(itr, l_dirname);
1401
1402 if (retdent) {
1403 printf("%s: already exists\n", l_dirname);
1404 ret = -EEXIST;
1405 goto exit;
1406 } else {
1407 if (itr->is_root) {
1408 /* root dir cannot have "." or ".." */
1409 if (!strcmp(l_dirname, ".") ||
1410 !strcmp(l_dirname, "..")) {
1411 ret = -EINVAL;
1412 goto exit;
1413 }
1414 }
1415
1416 if (!itr->dent) {
1417 printf("Error: allocating new dir entry\n");
1418 ret = -EIO;
1419 goto exit;
1420 }
1421
1422 memset(itr->dent, 0, sizeof(*itr->dent));
1423
1424 /* Set short name to set alias checksum field in dir_slot */
1425 set_name(itr->dent, dirname);
1426 fill_dir_slot(itr, dirname);
1427
1428 /* Set attribute as archive for regular file */
1429 fill_dentry(itr->fsdata, itr->dent, dirname, 0, 0,
1430 ATTR_DIR | ATTR_ARCH);
1431
1432 retdent = itr->dent;
1433 }
1434
1435 /* Default entries */
1436 bytesperclust = mydata->clust_size * mydata->sect_size;
1437 dotdent = malloc_cache_aligned(bytesperclust);
1438 if (!dotdent) {
1439 ret = -ENOMEM;
1440 goto exit;
1441 }
1442 memset(dotdent, 0, bytesperclust);
1443
1444 memcpy(dotdent[0].name, ". ", 8);
1445 memcpy(dotdent[0].ext, " ", 3);
1446 dotdent[0].attr = ATTR_DIR | ATTR_ARCH;
1447
1448 memcpy(dotdent[1].name, ".. ", 8);
1449 memcpy(dotdent[1].ext, " ", 3);
1450 dotdent[1].attr = ATTR_DIR | ATTR_ARCH;
1451 set_start_cluster(mydata, &dotdent[1], itr->start_clust);
1452
1453 ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
1454 bytesperclust, &actwrite);
1455 if (ret < 0) {
1456 printf("Error: writing contents\n");
1457 goto exit;
1458 }
1459 /* Write twice for "." */
1460 set_start_cluster(mydata, &dotdent[0], START(retdent));
1461 ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
1462 bytesperclust, &actwrite);
1463 if (ret < 0) {
1464 printf("Error: writing contents\n");
1465 goto exit;
1466 }
1467
1468 /* Flush fat buffer */
1469 ret = flush_dirty_fat_buffer(mydata);
1470 if (ret) {
1471 printf("Error: flush fat buffer\n");
1472 goto exit;
1473 }
1474
1475 /* Write directory table to device */
AKASHI Takahirod98c6742019-05-24 14:10:35 +09001476 ret = flush_dir(itr);
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001477 if (ret)
1478 printf("Error: writing directory entry\n");
1479
1480exit:
1481 free(dirname_copy);
1482 free(mydata->fatbuf);
1483 free(itr);
1484 free(dotdent);
1485 return ret;
1486}