blob: fc932df953c72ed75da5a3dc1cca54398a5b7928 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Donggeun Kimc30a15e2011-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 Kimc30a15e2011-10-24 21:15:28 +00006 */
7
8#include <common.h>
9#include <command.h>
10#include <config.h>
11#include <fat.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070013#include <malloc.h>
Donggeun Kimc30a15e2011-10-24 21:15:28 +000014#include <asm/byteorder.h>
15#include <part.h>
Simon Glass90526e92020-05-10 11:39:56 -060016#include <asm/cache.h>
Richard Genoudfb7e16c2012-12-13 00:47:36 +000017#include <linux/ctype.h>
Tom Rini9e374e72014-11-24 11:50:46 -050018#include <div64.h>
19#include <linux/math64.h>
Donggeun Kimc30a15e2011-10-24 21:15:28 +000020#include "fat.c"
21
22static void uppercase(char *str, int len)
23{
24 int i;
25
26 for (i = 0; i < len; i++) {
Richard Genoudfb7e16c2012-12-13 00:47:36 +000027 *str = toupper(*str);
Donggeun Kimc30a15e2011-10-24 21:15:28 +000028 str++;
29 }
30}
31
32static int total_sector;
Donggeun Kim079df722012-03-22 04:38:55 +000033static int disk_write(__u32 block, __u32 nr_blocks, void *buf)
Donggeun Kimc30a15e2011-10-24 21:15:28 +000034{
Łukasz Majewski0a04ed82015-09-03 14:21:39 +020035 ulong ret;
36
Simon Glass2a981dc2016-02-29 15:25:52 -070037 if (!cur_dev)
Donggeun Kimc30a15e2011-10-24 21:15:28 +000038 return -1;
39
Donggeun Kim079df722012-03-22 04:38:55 +000040 if (cur_part_info.start + block + nr_blocks >
41 cur_part_info.start + total_sector) {
Donggeun Kimc30a15e2011-10-24 21:15:28 +000042 printf("error: overflow occurs\n");
43 return -1;
44 }
45
Simon Glass2a981dc2016-02-29 15:25:52 -070046 ret = blk_dwrite(cur_dev, cur_part_info.start + block, nr_blocks, buf);
Łukasz Majewski0a04ed82015-09-03 14:21:39 +020047 if (nr_blocks && ret == 0)
48 return -1;
49
50 return ret;
Donggeun Kimc30a15e2011-10-24 21:15:28 +000051}
52
Heinrich Schuchardta20f0c82020-05-26 21:06:50 +020053/**
54 * set_name() - set short name in directory entry
55 *
56 * @dirent: directory entry
57 * @filename: long file name
Donggeun Kimc30a15e2011-10-24 21:15:28 +000058 */
59static void set_name(dir_entry *dirent, const char *filename)
60{
61 char s_name[VFAT_MAXLEN_BYTES];
62 char *period;
63 int period_location, len, i, ext_num;
64
65 if (filename == NULL)
66 return;
67
68 len = strlen(filename);
69 if (len == 0)
70 return;
71
Heinrich Schuchardta20f0c82020-05-26 21:06:50 +020072 strncpy(s_name, filename, VFAT_MAXLEN_BYTES - 1);
73 s_name[VFAT_MAXLEN_BYTES - 1] = '\0';
Donggeun Kimc30a15e2011-10-24 21:15:28 +000074 uppercase(s_name, len);
75
76 period = strchr(s_name, '.');
77 if (period == NULL) {
78 period_location = len;
79 ext_num = 0;
80 } else {
81 period_location = period - s_name;
82 ext_num = len - period_location - 1;
83 }
84
85 /* Pad spaces when the length of file name is shorter than eight */
86 if (period_location < 8) {
87 memcpy(dirent->name, s_name, period_location);
88 for (i = period_location; i < 8; i++)
89 dirent->name[i] = ' ';
90 } else if (period_location == 8) {
91 memcpy(dirent->name, s_name, period_location);
92 } else {
93 memcpy(dirent->name, s_name, 6);
Heinrich Schuchardta20f0c82020-05-26 21:06:50 +020094 /*
95 * TODO: Translating two long names with the same first six
96 * characters to the same short name is utterly wrong.
97 * Short names must be unique.
98 */
Donggeun Kimc30a15e2011-10-24 21:15:28 +000099 dirent->name[6] = '~';
100 dirent->name[7] = '1';
101 }
102
103 if (ext_num < 3) {
104 memcpy(dirent->ext, s_name + period_location + 1, ext_num);
105 for (i = ext_num; i < 3; i++)
106 dirent->ext[i] = ' ';
107 } else
108 memcpy(dirent->ext, s_name + period_location + 1, 3);
109
110 debug("name : %s\n", dirent->name);
111 debug("ext : %s\n", dirent->ext);
112}
113
114/*
115 * Write fat buffer into block device
116 */
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200117static int flush_dirty_fat_buffer(fsdata *mydata)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000118{
119 int getsize = FATBUFBLOCKS;
120 __u32 fatlength = mydata->fatlength;
121 __u8 *bufptr = mydata->fatbuf;
122 __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
123
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200124 debug("debug: evicting %d, dirty: %d\n", mydata->fatbufnum,
125 (int)mydata->fat_dirty);
126
127 if ((!mydata->fat_dirty) || (mydata->fatbufnum == -1))
128 return 0;
129
Stefan Brüns6c1a8082016-12-17 00:27:50 +0100130 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
131 if (startblock + getsize > fatlength)
132 getsize = fatlength - startblock;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000133
Stefan Brüns6c1a8082016-12-17 00:27:50 +0100134 startblock += mydata->fat_sect;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000135
136 /* Write FAT buf */
137 if (disk_write(startblock, getsize, bufptr) < 0) {
138 debug("error: writing FAT blocks\n");
139 return -1;
140 }
141
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900142 if (mydata->fats == 2) {
Donggeun Kim627182e2011-12-20 18:34:27 +0000143 /* Update corresponding second FAT blocks */
144 startblock += mydata->fatlength;
145 if (disk_write(startblock, getsize, bufptr) < 0) {
146 debug("error: writing second FAT blocks\n");
147 return -1;
148 }
149 }
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200150 mydata->fat_dirty = 0;
Donggeun Kim627182e2011-12-20 18:34:27 +0000151
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000152 return 0;
153}
154
155/*
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000156 * Set the file name information from 'name' into 'slotptr',
157 */
158static int str2slot(dir_slot *slotptr, const char *name, int *idx)
159{
160 int j, end_idx = 0;
161
162 for (j = 0; j <= 8; j += 2) {
163 if (name[*idx] == 0x00) {
164 slotptr->name0_4[j] = 0;
165 slotptr->name0_4[j + 1] = 0;
166 end_idx++;
167 goto name0_4;
168 }
169 slotptr->name0_4[j] = name[*idx];
170 (*idx)++;
171 end_idx++;
172 }
173 for (j = 0; j <= 10; j += 2) {
174 if (name[*idx] == 0x00) {
175 slotptr->name5_10[j] = 0;
176 slotptr->name5_10[j + 1] = 0;
177 end_idx++;
178 goto name5_10;
179 }
180 slotptr->name5_10[j] = name[*idx];
181 (*idx)++;
182 end_idx++;
183 }
184 for (j = 0; j <= 2; j += 2) {
185 if (name[*idx] == 0x00) {
186 slotptr->name11_12[j] = 0;
187 slotptr->name11_12[j + 1] = 0;
188 end_idx++;
189 goto name11_12;
190 }
191 slotptr->name11_12[j] = name[*idx];
192 (*idx)++;
193 end_idx++;
194 }
195
196 if (name[*idx] == 0x00)
197 return 1;
198
199 return 0;
200/* Not used characters are filled with 0xff 0xff */
201name0_4:
202 for (; end_idx < 5; end_idx++) {
203 slotptr->name0_4[end_idx * 2] = 0xff;
204 slotptr->name0_4[end_idx * 2 + 1] = 0xff;
205 }
206 end_idx = 5;
207name5_10:
208 end_idx -= 5;
209 for (; end_idx < 6; end_idx++) {
210 slotptr->name5_10[end_idx * 2] = 0xff;
211 slotptr->name5_10[end_idx * 2 + 1] = 0xff;
212 }
213 end_idx = 11;
214name11_12:
215 end_idx -= 11;
216 for (; end_idx < 2; end_idx++) {
217 slotptr->name11_12[end_idx * 2] = 0xff;
218 slotptr->name11_12[end_idx * 2 + 1] = 0xff;
219 }
220
221 return 1;
222}
223
AKASHI Takahiro9c709c72019-05-24 14:10:36 +0900224static int new_dir_table(fat_itr *itr);
225static int flush_dir(fat_itr *itr);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000226
227/*
228 * Fill dir_slot entries with appropriate name, id, and attr
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900229 * 'itr' will point to a next entry
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000230 */
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900231static int
232fill_dir_slot(fat_itr *itr, const char *l_name)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000233{
Tien Fong Chee7aa1a6b2016-07-27 23:08:56 -0700234 __u8 temp_dir_slot_buffer[MAX_LFN_SLOT * sizeof(dir_slot)];
235 dir_slot *slotptr = (dir_slot *)temp_dir_slot_buffer;
Anatolij Gustschin8506eb82011-12-15 03:12:14 +0000236 __u8 counter = 0, checksum;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000237 int idx = 0, ret;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000238
Stefan Brünsed76f912016-09-11 22:51:39 +0200239 /* Get short file name checksum value */
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900240 checksum = mkcksum(itr->dent->name, itr->dent->ext);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000241
242 do {
243 memset(slotptr, 0x00, sizeof(dir_slot));
244 ret = str2slot(slotptr, l_name, &idx);
245 slotptr->id = ++counter;
246 slotptr->attr = ATTR_VFAT;
247 slotptr->alias_checksum = checksum;
248 slotptr++;
249 } while (ret == 0);
250
251 slotptr--;
252 slotptr->id |= LAST_LONG_ENTRY_MASK;
253
254 while (counter >= 1) {
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900255 memcpy(itr->dent, slotptr, sizeof(dir_slot));
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000256 slotptr--;
257 counter--;
AKASHI Takahiro9c709c72019-05-24 14:10:36 +0900258
259 if (itr->remaining == 0)
260 flush_dir(itr);
261
AKASHI Takahirocd2d7272019-05-24 14:10:37 +0900262 /* allocate a cluster for more entries */
Heinrich Schuchardt661d2232020-11-19 12:24:44 +0100263 if (!fat_itr_next(itr) && !itr->dent)
264 if ((itr->is_root && itr->fsdata->fatsize != 32) ||
AKASHI Takahirocd2d7272019-05-24 14:10:37 +0900265 new_dir_table(itr))
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000266 return -1;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000267 }
268
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000269 return 0;
270}
271
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000272/*
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500273 * Set the entry at index 'entry' in a FAT (12/16/32) table.
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000274 */
275static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
276{
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500277 __u32 bufnum, offset, off16;
278 __u16 val1, val2;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000279
280 switch (mydata->fatsize) {
281 case 32:
282 bufnum = entry / FAT32BUFSIZE;
283 offset = entry - bufnum * FAT32BUFSIZE;
284 break;
285 case 16:
286 bufnum = entry / FAT16BUFSIZE;
287 offset = entry - bufnum * FAT16BUFSIZE;
288 break;
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500289 case 12:
290 bufnum = entry / FAT12BUFSIZE;
291 offset = entry - bufnum * FAT12BUFSIZE;
292 break;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000293 default:
294 /* Unsupported FAT size */
295 return -1;
296 }
297
298 /* Read a new block of FAT entries into the cache. */
299 if (bufnum != mydata->fatbufnum) {
300 int getsize = FATBUFBLOCKS;
301 __u8 *bufptr = mydata->fatbuf;
302 __u32 fatlength = mydata->fatlength;
303 __u32 startblock = bufnum * FATBUFBLOCKS;
304
Stefan Brüns6c1a8082016-12-17 00:27:50 +0100305 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
306 if (startblock + getsize > fatlength)
307 getsize = fatlength - startblock;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000308
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200309 if (flush_dirty_fat_buffer(mydata) < 0)
310 return -1;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000311
Stefan Brüns6c1a8082016-12-17 00:27:50 +0100312 startblock += mydata->fat_sect;
313
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000314 if (disk_read(startblock, getsize, bufptr) < 0) {
315 debug("Error reading FAT blocks\n");
316 return -1;
317 }
318 mydata->fatbufnum = bufnum;
319 }
320
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200321 /* Mark as dirty */
322 mydata->fat_dirty = 1;
323
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000324 /* Set the actual entry */
325 switch (mydata->fatsize) {
326 case 32:
327 ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
328 break;
329 case 16:
330 ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
331 break;
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500332 case 12:
333 off16 = (offset * 3) / 4;
334
335 switch (offset & 0x3) {
336 case 0:
337 val1 = cpu_to_le16(entry_value) & 0xfff;
338 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff;
339 ((__u16 *)mydata->fatbuf)[off16] |= val1;
340 break;
341 case 1:
342 val1 = cpu_to_le16(entry_value) & 0xf;
343 val2 = (cpu_to_le16(entry_value) >> 4) & 0xff;
344
345 ((__u16 *)mydata->fatbuf)[off16] &= ~0xf000;
346 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 12);
347
348 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xff;
349 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
350 break;
351 case 2:
352 val1 = cpu_to_le16(entry_value) & 0xff;
353 val2 = (cpu_to_le16(entry_value) >> 8) & 0xf;
354
355 ((__u16 *)mydata->fatbuf)[off16] &= ~0xff00;
356 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 8);
357
358 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xf;
359 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
360 break;
361 case 3:
362 val1 = cpu_to_le16(entry_value) & 0xfff;
363 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff0;
364 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 4);
365 break;
366 default:
367 break;
368 }
369
370 break;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000371 default:
372 return -1;
373 }
374
375 return 0;
376}
377
378/*
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500379 * Determine the next free cluster after 'entry' in a FAT (12/16/32) table
Stefan Brünsae1755b2016-09-11 22:51:41 +0200380 * and link it to 'entry'. EOC marker is not set on returned entry.
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000381 */
382static __u32 determine_fatent(fsdata *mydata, __u32 entry)
383{
384 __u32 next_fat, next_entry = entry + 1;
385
386 while (1) {
Stefan Brünsb8948d22016-12-17 00:27:51 +0100387 next_fat = get_fatent(mydata, next_entry);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000388 if (next_fat == 0) {
Stefan Brünsae1755b2016-09-11 22:51:41 +0200389 /* found free entry, link to entry */
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000390 set_fatent_value(mydata, entry, next_entry);
391 break;
392 }
393 next_entry++;
394 }
395 debug("FAT%d: entry: %08x, entry_value: %04x\n",
396 mydata->fatsize, entry, next_entry);
397
398 return next_entry;
399}
400
Heinrich Schuchardtf105fe72018-10-02 09:30:45 +0200401/**
AKASHI Takahiroa9f67062019-05-24 14:10:35 +0900402 * set_sectors() - write data to sectors
Heinrich Schuchardtf105fe72018-10-02 09:30:45 +0200403 *
AKASHI Takahiroa9f67062019-05-24 14:10:35 +0900404 * Write 'size' bytes from 'buffer' into the specified sector.
Heinrich Schuchardtf105fe72018-10-02 09:30:45 +0200405 *
406 * @mydata: data to be written
AKASHI Takahiroa9f67062019-05-24 14:10:35 +0900407 * @startsect: sector to be written to
Heinrich Schuchardtf105fe72018-10-02 09:30:45 +0200408 * @buffer: data to be written
409 * @size: bytes to be written (but not more than the size of a cluster)
410 * Return: 0 on success, -1 otherwise
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000411 */
412static int
AKASHI Takahiroa9f67062019-05-24 14:10:35 +0900413set_sectors(fsdata *mydata, u32 startsect, u8 *buffer, u32 size)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000414{
AKASHI Takahiroa9f67062019-05-24 14:10:35 +0900415 u32 nsects = 0;
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200416 int ret;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000417
AKASHI Takahiroa9f67062019-05-24 14:10:35 +0900418 debug("startsect: %d\n", startsect);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000419
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200420 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
421 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
422
Heinrich Schuchardt1c381ce2018-09-13 19:42:47 +0200423 debug("FAT: Misaligned buffer address (%p)\n", buffer);
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200424
425 while (size >= mydata->sect_size) {
426 memcpy(tmpbuf, buffer, mydata->sect_size);
427 ret = disk_write(startsect++, 1, tmpbuf);
428 if (ret != 1) {
429 debug("Error writing data (got %d)\n", ret);
430 return -1;
431 }
432
433 buffer += mydata->sect_size;
434 size -= mydata->sect_size;
435 }
436 } else if (size >= mydata->sect_size) {
AKASHI Takahiroa9f67062019-05-24 14:10:35 +0900437 nsects = size / mydata->sect_size;
438 ret = disk_write(startsect, nsects, buffer);
439 if (ret != nsects) {
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200440 debug("Error writing data (got %d)\n", ret);
Wu, Josh6b8f1852013-07-24 17:55:30 +0800441 return -1;
442 }
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200443
AKASHI Takahiroa9f67062019-05-24 14:10:35 +0900444 startsect += nsects;
445 buffer += nsects * mydata->sect_size;
446 size -= nsects * mydata->sect_size;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000447 }
448
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200449 if (size) {
450 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Heinrich Schuchardtf105fe72018-10-02 09:30:45 +0200451 /* Do not leak content of stack */
452 memset(tmpbuf, 0, mydata->sect_size);
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200453 memcpy(tmpbuf, buffer, size);
454 ret = disk_write(startsect, 1, tmpbuf);
455 if (ret != 1) {
456 debug("Error writing data (got %d)\n", ret);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000457 return -1;
458 }
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000459 }
460
461 return 0;
462}
463
AKASHI Takahiroa9f67062019-05-24 14:10:35 +0900464/**
465 * set_cluster() - write data to cluster
466 *
467 * Write 'size' bytes from 'buffer' into the specified cluster.
468 *
469 * @mydata: data to be written
470 * @clustnum: cluster to be written to
471 * @buffer: data to be written
472 * @size: bytes to be written (but not more than the size of a cluster)
473 * Return: 0 on success, -1 otherwise
474 */
475static int
476set_cluster(fsdata *mydata, u32 clustnum, u8 *buffer, u32 size)
477{
478 return set_sectors(mydata, clust_to_sect(mydata, clustnum),
479 buffer, size);
480}
481
482static int
483flush_dir(fat_itr *itr)
484{
485 fsdata *mydata = itr->fsdata;
486 u32 startsect, sect_offset, nsects;
487
488 if (!itr->is_root || mydata->fatsize == 32)
489 return set_cluster(mydata, itr->clust, itr->block,
490 mydata->clust_size * mydata->sect_size);
491
492 sect_offset = itr->clust * mydata->clust_size;
493 startsect = mydata->rootdir_sect + sect_offset;
494 /* do not write past the end of rootdir */
495 nsects = min_t(u32, mydata->clust_size,
496 mydata->rootdir_size - sect_offset);
497
498 return set_sectors(mydata, startsect, itr->block,
499 nsects * mydata->sect_size);
500}
501
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +0900502/*
503 * Read and modify data on existing and consecutive cluster blocks
504 */
505static int
506get_set_cluster(fsdata *mydata, __u32 clustnum, loff_t pos, __u8 *buffer,
507 loff_t size, loff_t *gotsize)
508{
Heinrich Schuchardt5a8d1f62020-07-06 07:48:14 +0200509 static u8 *tmpbuf_cluster;
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +0900510 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
511 __u32 startsect;
512 loff_t wsize;
513 int clustcount, i, ret;
514
515 *gotsize = 0;
516 if (!size)
517 return 0;
518
Heinrich Schuchardt5a8d1f62020-07-06 07:48:14 +0200519 if (!tmpbuf_cluster) {
520 tmpbuf_cluster = memalign(ARCH_DMA_MINALIGN, MAX_CLUSTSIZE);
521 if (!tmpbuf_cluster)
522 return -1;
523 }
524
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +0900525 assert(pos < bytesperclust);
526 startsect = clust_to_sect(mydata, clustnum);
527
528 debug("clustnum: %d, startsect: %d, pos: %lld\n",
529 clustnum, startsect, pos);
530
531 /* partial write at beginning */
532 if (pos) {
533 wsize = min(bytesperclust - pos, size);
534 ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster);
535 if (ret != mydata->clust_size) {
536 debug("Error reading data (got %d)\n", ret);
537 return -1;
538 }
539
540 memcpy(tmpbuf_cluster + pos, buffer, wsize);
541 ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster);
542 if (ret != mydata->clust_size) {
543 debug("Error writing data (got %d)\n", ret);
544 return -1;
545 }
546
547 size -= wsize;
548 buffer += wsize;
549 *gotsize += wsize;
550
551 startsect += mydata->clust_size;
552
553 if (!size)
554 return 0;
555 }
556
557 /* full-cluster write */
558 if (size >= bytesperclust) {
559 clustcount = lldiv(size, bytesperclust);
560
561 if (!((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1))) {
562 wsize = clustcount * bytesperclust;
563 ret = disk_write(startsect,
564 clustcount * mydata->clust_size,
565 buffer);
566 if (ret != clustcount * mydata->clust_size) {
567 debug("Error writing data (got %d)\n", ret);
568 return -1;
569 }
570
571 size -= wsize;
572 buffer += wsize;
573 *gotsize += wsize;
574
575 startsect += clustcount * mydata->clust_size;
576 } else {
577 for (i = 0; i < clustcount; i++) {
578 memcpy(tmpbuf_cluster, buffer, bytesperclust);
579 ret = disk_write(startsect,
580 mydata->clust_size,
581 tmpbuf_cluster);
582 if (ret != mydata->clust_size) {
583 debug("Error writing data (got %d)\n",
584 ret);
585 return -1;
586 }
587
588 size -= bytesperclust;
589 buffer += bytesperclust;
590 *gotsize += bytesperclust;
591
592 startsect += mydata->clust_size;
593 }
594 }
595 }
596
597 /* partial write at end */
598 if (size) {
599 wsize = size;
600 ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster);
601 if (ret != mydata->clust_size) {
602 debug("Error reading data (got %d)\n", ret);
603 return -1;
604 }
605 memcpy(tmpbuf_cluster, buffer, wsize);
606 ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster);
607 if (ret != mydata->clust_size) {
608 debug("Error writing data (got %d)\n", ret);
609 return -1;
610 }
611
612 size -= wsize;
613 buffer += wsize;
614 *gotsize += wsize;
615 }
616
617 assert(!size);
618
619 return 0;
620}
621
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000622/*
623 * Find the first empty cluster
624 */
625static int find_empty_cluster(fsdata *mydata)
626{
627 __u32 fat_val, entry = 3;
628
629 while (1) {
Stefan Brünsb8948d22016-12-17 00:27:51 +0100630 fat_val = get_fatent(mydata, entry);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000631 if (fat_val == 0)
632 break;
633 entry++;
634 }
635
636 return entry;
637}
638
639/*
AKASHI Takahiro9c709c72019-05-24 14:10:36 +0900640 * Allocate a cluster for additional directory entries
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000641 */
AKASHI Takahiro9c709c72019-05-24 14:10:36 +0900642static int new_dir_table(fat_itr *itr)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000643{
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900644 fsdata *mydata = itr->fsdata;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000645 int dir_newclust = 0;
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900646 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000647
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000648 dir_newclust = find_empty_cluster(mydata);
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900649 set_fatent_value(mydata, itr->clust, dir_newclust);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000650 if (mydata->fatsize == 32)
651 set_fatent_value(mydata, dir_newclust, 0xffffff8);
652 else if (mydata->fatsize == 16)
653 set_fatent_value(mydata, dir_newclust, 0xfff8);
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500654 else if (mydata->fatsize == 12)
655 set_fatent_value(mydata, dir_newclust, 0xff8);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000656
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900657 itr->clust = dir_newclust;
658 itr->next_clust = dir_newclust;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000659
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200660 if (flush_dirty_fat_buffer(mydata) < 0)
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900661 return -1;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000662
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900663 memset(itr->block, 0x00, bytesperclust);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000664
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900665 itr->dent = (dir_entry *)itr->block;
666 itr->last_cluster = 1;
667 itr->remaining = bytesperclust / sizeof(dir_entry) - 1;
668
669 return 0;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000670}
671
672/*
673 * Set empty cluster from 'entry' to the end of a file
674 */
675static int clear_fatent(fsdata *mydata, __u32 entry)
676{
677 __u32 fat_val;
678
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500679 while (!CHECK_CLUST(entry, mydata->fatsize)) {
Stefan Brünsb8948d22016-12-17 00:27:51 +0100680 fat_val = get_fatent(mydata, entry);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000681 if (fat_val != 0)
682 set_fatent_value(mydata, entry, 0);
683 else
684 break;
685
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000686 entry = fat_val;
687 }
688
689 /* Flush fat buffer */
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200690 if (flush_dirty_fat_buffer(mydata) < 0)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000691 return -1;
692
693 return 0;
694}
695
696/*
AKASHI Takahiro704df6a2018-09-11 15:59:05 +0900697 * Set start cluster in directory entry
698 */
699static void set_start_cluster(const fsdata *mydata, dir_entry *dentptr,
700 __u32 start_cluster)
701{
702 if (mydata->fatsize == 32)
703 dentptr->starthi =
704 cpu_to_le16((start_cluster & 0xffff0000) >> 16);
705 dentptr->start = cpu_to_le16(start_cluster & 0xffff);
706}
707
708/*
709 * Check whether adding a file makes the file system to
710 * exceed the size of the block device
711 * Return -1 when overflow occurs, otherwise return 0
712 */
713static int check_overflow(fsdata *mydata, __u32 clustnum, loff_t size)
714{
715 __u32 startsect, sect_num, offset;
716
717 if (clustnum > 0)
718 startsect = clust_to_sect(mydata, clustnum);
719 else
720 startsect = mydata->rootdir_sect;
721
722 sect_num = div_u64_rem(size, mydata->sect_size, &offset);
723
724 if (offset != 0)
725 sect_num++;
726
727 if (startsect + sect_num > total_sector)
728 return -1;
729 return 0;
730}
731
732/*
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000733 * Write at most 'maxsize' bytes from 'buffer' into
734 * the file associated with 'dentptr'
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800735 * Update the number of bytes written in *gotsize and return 0
736 * or return -1 on fatal errors.
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000737 */
738static int
AKASHI Takahiro704df6a2018-09-11 15:59:05 +0900739set_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, __u8 *buffer,
740 loff_t maxsize, loff_t *gotsize)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000741{
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000742 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
743 __u32 curclust = START(dentptr);
744 __u32 endclust = 0, newclust = 0;
Heinrich Schuchardt7274b762019-02-25 19:42:48 +0100745 u64 cur_pos, filesize;
746 loff_t offset, actsize, wsize;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000747
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800748 *gotsize = 0;
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +0900749 filesize = pos + maxsize;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000750
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800751 debug("%llu bytes\n", filesize);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000752
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +0900753 if (!filesize) {
754 if (!curclust)
755 return 0;
756 if (!CHECK_CLUST(curclust, mydata->fatsize) ||
757 IS_LAST_CLUST(curclust, mydata->fatsize)) {
758 clear_fatent(mydata, curclust);
759 set_start_cluster(mydata, dentptr, 0);
760 return 0;
761 }
762 debug("curclust: 0x%x\n", curclust);
763 debug("Invalid FAT entry\n");
764 return -1;
765 }
766
767 if (!curclust) {
768 assert(pos == 0);
769 goto set_clusters;
770 }
771
772 /* go to cluster at pos */
773 cur_pos = bytesperclust;
774 while (1) {
775 if (pos <= cur_pos)
776 break;
777 if (IS_LAST_CLUST(curclust, mydata->fatsize))
778 break;
779
780 newclust = get_fatent(mydata, curclust);
781 if (!IS_LAST_CLUST(newclust, mydata->fatsize) &&
782 CHECK_CLUST(newclust, mydata->fatsize)) {
783 debug("curclust: 0x%x\n", curclust);
784 debug("Invalid FAT entry\n");
785 return -1;
786 }
787
788 cur_pos += bytesperclust;
789 curclust = newclust;
790 }
791 if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
792 assert(pos == cur_pos);
793 goto set_clusters;
794 }
795
796 assert(pos < cur_pos);
797 cur_pos -= bytesperclust;
798
799 /* overwrite */
800 assert(IS_LAST_CLUST(curclust, mydata->fatsize) ||
801 !CHECK_CLUST(curclust, mydata->fatsize));
802
803 while (1) {
804 /* search for allocated consecutive clusters */
805 actsize = bytesperclust;
806 endclust = curclust;
807 while (1) {
808 if (filesize <= (cur_pos + actsize))
809 break;
810
811 newclust = get_fatent(mydata, endclust);
812
Marek Szyprowski5e615b72019-12-02 12:11:13 +0100813 if (newclust != endclust + 1)
814 break;
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +0900815 if (IS_LAST_CLUST(newclust, mydata->fatsize))
816 break;
817 if (CHECK_CLUST(newclust, mydata->fatsize)) {
818 debug("curclust: 0x%x\n", curclust);
819 debug("Invalid FAT entry\n");
820 return -1;
821 }
822
823 actsize += bytesperclust;
824 endclust = newclust;
825 }
826
827 /* overwrite to <curclust..endclust> */
828 if (pos < cur_pos)
829 offset = 0;
830 else
831 offset = pos - cur_pos;
Marek Szyprowskia54ece42019-12-02 12:11:14 +0100832 wsize = min_t(unsigned long long, actsize, filesize - cur_pos);
833 wsize -= offset;
834
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +0900835 if (get_set_cluster(mydata, curclust, offset,
836 buffer, wsize, &actsize)) {
837 printf("Error get-and-setting cluster\n");
838 return -1;
839 }
840 buffer += wsize;
841 *gotsize += wsize;
842 cur_pos += offset + wsize;
843
844 if (filesize <= cur_pos)
845 break;
846
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +0900847 if (IS_LAST_CLUST(newclust, mydata->fatsize))
848 /* no more clusters */
849 break;
850
851 curclust = newclust;
852 }
853
854 if (filesize <= cur_pos) {
855 /* no more write */
856 newclust = get_fatent(mydata, endclust);
857 if (!IS_LAST_CLUST(newclust, mydata->fatsize)) {
858 /* truncate the rest */
859 clear_fatent(mydata, newclust);
860
861 /* Mark end of file in FAT */
862 if (mydata->fatsize == 12)
863 newclust = 0xfff;
864 else if (mydata->fatsize == 16)
865 newclust = 0xffff;
866 else if (mydata->fatsize == 32)
867 newclust = 0xfffffff;
868 set_fatent_value(mydata, endclust, newclust);
869 }
870
871 return 0;
872 }
873
874 curclust = endclust;
875 filesize -= cur_pos;
Heinrich Schuchardt7274b762019-02-25 19:42:48 +0100876 assert(!do_div(cur_pos, bytesperclust));
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +0900877
878set_clusters:
879 /* allocate and write */
880 assert(!pos);
881
882 /* Assure that curclust is valid */
883 if (!curclust) {
884 curclust = find_empty_cluster(mydata);
885 set_start_cluster(mydata, dentptr, curclust);
886 } else {
887 newclust = get_fatent(mydata, curclust);
888
889 if (IS_LAST_CLUST(newclust, mydata->fatsize)) {
890 newclust = determine_fatent(mydata, curclust);
891 set_fatent_value(mydata, curclust, newclust);
892 curclust = newclust;
893 } else {
894 debug("error: something wrong\n");
Benoît Thébaudeau1254b442015-09-28 15:45:32 +0200895 return -1;
896 }
AKASHI Takahiro704df6a2018-09-11 15:59:05 +0900897 }
898
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +0900899 /* TODO: already partially written */
AKASHI Takahiro704df6a2018-09-11 15:59:05 +0900900 if (check_overflow(mydata, curclust, filesize)) {
901 printf("Error: no space left: %llu\n", filesize);
902 return -1;
Benoît Thébaudeau1254b442015-09-28 15:45:32 +0200903 }
904
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000905 actsize = bytesperclust;
906 endclust = curclust;
907 do {
908 /* search for consecutive clusters */
909 while (actsize < filesize) {
910 newclust = determine_fatent(mydata, endclust);
911
912 if ((newclust - 1) != endclust)
AKASHI Takahiro704df6a2018-09-11 15:59:05 +0900913 /* write to <curclust..endclust> */
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000914 goto getit;
915
916 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Benoît Thébaudeau5e1a8602015-09-28 15:45:30 +0200917 debug("newclust: 0x%x\n", newclust);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000918 debug("Invalid FAT entry\n");
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800919 return 0;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000920 }
921 endclust = newclust;
922 actsize += bytesperclust;
923 }
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000924
925 /* set remaining bytes */
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000926 actsize = filesize;
Heinrich Schuchardtf105fe72018-10-02 09:30:45 +0200927 if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000928 debug("error: writing cluster\n");
929 return -1;
930 }
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800931 *gotsize += actsize;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000932
933 /* Mark end of file in FAT */
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500934 if (mydata->fatsize == 12)
935 newclust = 0xfff;
936 else if (mydata->fatsize == 16)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000937 newclust = 0xffff;
938 else if (mydata->fatsize == 32)
939 newclust = 0xfffffff;
940 set_fatent_value(mydata, endclust, newclust);
941
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800942 return 0;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000943getit:
Heinrich Schuchardtf105fe72018-10-02 09:30:45 +0200944 if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000945 debug("error: writing cluster\n");
946 return -1;
947 }
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800948 *gotsize += actsize;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000949 filesize -= actsize;
950 buffer += actsize;
951
Benoît Thébaudeau5e1a8602015-09-28 15:45:30 +0200952 if (CHECK_CLUST(newclust, mydata->fatsize)) {
953 debug("newclust: 0x%x\n", newclust);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000954 debug("Invalid FAT entry\n");
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800955 return 0;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000956 }
957 actsize = bytesperclust;
958 curclust = endclust = newclust;
959 } while (1);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000960
AKASHI Takahiro704df6a2018-09-11 15:59:05 +0900961 return 0;
Benoît Thébaudeau1254b442015-09-28 15:45:32 +0200962}
963
964/*
965 * Fill dir_entry
966 */
967static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
968 const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
969{
970 set_start_cluster(mydata, dentptr, start_cluster);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000971 dentptr->size = cpu_to_le32(size);
972
973 dentptr->attr = attr;
974
975 set_name(dentptr, filename);
976}
977
978/*
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000979 * Find a directory entry based on filename or start cluster number
980 * If the directory entry is not found,
981 * the new position for writing a directory entry will be returned
982 */
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900983static dir_entry *find_directory_entry(fat_itr *itr, char *filename)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000984{
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900985 int match = 0;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000986
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900987 while (fat_itr_next(itr)) {
988 /* check both long and short name: */
989 if (!strcasecmp(filename, itr->name))
990 match = 1;
991 else if (itr->name != itr->s_name &&
992 !strcasecmp(filename, itr->s_name))
993 match = 1;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000994
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900995 if (!match)
996 continue;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000997
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900998 if (itr->dent->name[0] == '\0')
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000999 return NULL;
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001000 else
1001 return itr->dent;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001002 }
1003
AKASHI Takahirocd2d7272019-05-24 14:10:37 +09001004 /* allocate a cluster for more entries */
1005 if (!itr->dent &&
1006 (!itr->is_root || itr->fsdata->fatsize == 32) &&
1007 new_dir_table(itr))
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001008 /* indicate that allocating dent failed */
1009 itr->dent = NULL;
1010
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001011 return NULL;
1012}
1013
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001014static int split_filename(char *filename, char **dirname, char **basename)
1015{
1016 char *p, *last_slash, *last_slash_cont;
1017
1018again:
1019 p = filename;
1020 last_slash = NULL;
1021 last_slash_cont = NULL;
1022 while (*p) {
1023 if (ISDIRDELIM(*p)) {
1024 last_slash = p;
1025 last_slash_cont = p;
1026 /* continuous slashes */
1027 while (ISDIRDELIM(*p))
1028 last_slash_cont = p++;
1029 if (!*p)
1030 break;
1031 }
1032 p++;
1033 }
1034
1035 if (last_slash) {
1036 if (last_slash_cont == (filename + strlen(filename) - 1)) {
1037 /* remove trailing slashes */
1038 *last_slash = '\0';
1039 goto again;
1040 }
1041
1042 if (last_slash == filename) {
1043 /* avoid ""(null) directory */
1044 *dirname = "/";
1045 } else {
1046 *last_slash = '\0';
1047 *dirname = filename;
1048 }
1049
1050 *last_slash_cont = '\0';
1051 *basename = last_slash_cont + 1;
1052 } else {
1053 *dirname = "/"; /* root by default */
1054 *basename = filename;
1055 }
1056
1057 return 0;
1058}
1059
Heinrich Schuchardt7b437802019-05-12 09:59:18 +02001060/**
1061 * normalize_longname() - check long file name and convert to lower case
1062 *
1063 * We assume here that the FAT file system is using an 8bit code page.
1064 * Linux typically uses CP437, EDK2 assumes CP1250.
1065 *
1066 * @l_filename: preallocated buffer receiving the normalized name
1067 * @filename: filename to normalize
1068 * Return: 0 on success, -1 on failure
1069 */
AKASHI Takahiro25bb9da2018-09-11 15:59:02 +09001070static int normalize_longname(char *l_filename, const char *filename)
1071{
Heinrich Schuchardt7b437802019-05-12 09:59:18 +02001072 const char *p, illegal[] = "<>:\"/\\|?*";
AKASHI Takahiro25bb9da2018-09-11 15:59:02 +09001073
Heinrich Schuchardt7b437802019-05-12 09:59:18 +02001074 if (strlen(filename) >= VFAT_MAXLEN_BYTES)
AKASHI Takahiro25bb9da2018-09-11 15:59:02 +09001075 return -1;
Heinrich Schuchardt7b437802019-05-12 09:59:18 +02001076
1077 for (p = filename; *p; ++p) {
1078 if ((unsigned char)*p < 0x20)
1079 return -1;
1080 if (strchr(illegal, *p))
1081 return -1;
AKASHI Takahiro25bb9da2018-09-11 15:59:02 +09001082 }
1083
Heinrich Schuchardt7b437802019-05-12 09:59:18 +02001084 strcpy(l_filename, filename);
1085 downcase(l_filename, VFAT_MAXLEN_BYTES);
AKASHI Takahiro25bb9da2018-09-11 15:59:02 +09001086
1087 return 0;
1088}
1089
AKASHI Takahiro704df6a2018-09-11 15:59:05 +09001090int file_fat_write_at(const char *filename, loff_t pos, void *buffer,
1091 loff_t size, loff_t *actwrite)
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001092{
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001093 dir_entry *retdent;
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001094 fsdata datablock = { .fatbuf = NULL, };
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001095 fsdata *mydata = &datablock;
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001096 fat_itr *itr = NULL;
AKASHI Takahiro25bb9da2018-09-11 15:59:02 +09001097 int ret = -1;
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001098 char *filename_copy, *parent, *basename;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001099 char l_filename[VFAT_MAXLEN_BYTES];
1100
AKASHI Takahiro704df6a2018-09-11 15:59:05 +09001101 debug("writing %s\n", filename);
1102
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001103 filename_copy = strdup(filename);
1104 if (!filename_copy)
AKASHI Takahirof1149ce2018-09-11 15:59:03 +09001105 return -ENOMEM;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001106
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001107 split_filename(filename_copy, &parent, &basename);
1108 if (!strlen(basename)) {
1109 ret = -EINVAL;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001110 goto exit;
1111 }
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001112
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001113 filename = basename;
AKASHI Takahiro25bb9da2018-09-11 15:59:02 +09001114 if (normalize_longname(l_filename, filename)) {
1115 printf("FAT: illegal filename (%s)\n", filename);
1116 ret = -EINVAL;
1117 goto exit;
1118 }
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001119
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001120 itr = malloc_cache_aligned(sizeof(fat_itr));
1121 if (!itr) {
1122 ret = -ENOMEM;
1123 goto exit;
1124 }
1125
1126 ret = fat_itr_root(itr, &datablock);
1127 if (ret)
1128 goto exit;
1129
1130 total_sector = datablock.total_sect;
1131
1132 ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1133 if (ret) {
1134 printf("%s: doesn't exist (%d)\n", parent, ret);
1135 goto exit;
1136 }
1137
1138 retdent = find_directory_entry(itr, l_filename);
1139
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001140 if (retdent) {
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001141 if (fat_itr_isdir(itr)) {
1142 ret = -EISDIR;
1143 goto exit;
1144 }
1145
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +09001146 /* A file exists */
1147 if (pos == -1)
1148 /* Append to the end */
1149 pos = FAT2CPU32(retdent->size);
1150 if (pos > retdent->size) {
1151 /* No hole allowed */
1152 ret = -EINVAL;
1153 goto exit;
1154 }
1155
AKASHI Takahiro704df6a2018-09-11 15:59:05 +09001156 /* Update file size in a directory entry */
1157 retdent->size = cpu_to_le32(pos + size);
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001158 } else {
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001159 /* Create a new file */
1160
1161 if (itr->is_root) {
1162 /* root dir cannot have "." or ".." */
1163 if (!strcmp(l_filename, ".") ||
1164 !strcmp(l_filename, "..")) {
1165 ret = -EINVAL;
1166 goto exit;
1167 }
1168 }
1169
1170 if (!itr->dent) {
1171 printf("Error: allocating new dir entry\n");
1172 ret = -EIO;
1173 goto exit;
1174 }
1175
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +09001176 if (pos) {
1177 /* No hole allowed */
1178 ret = -EINVAL;
1179 goto exit;
1180 }
1181
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001182 memset(itr->dent, 0, sizeof(*itr->dent));
1183
AKASHI Takahiro9c709c72019-05-24 14:10:36 +09001184 /* Calculate checksum for short name */
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001185 set_name(itr->dent, filename);
AKASHI Takahiro9c709c72019-05-24 14:10:36 +09001186
1187 /* Set long name entries */
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001188 if (fill_dir_slot(itr, filename)) {
1189 ret = -EIO;
1190 goto exit;
1191 }
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001192
AKASHI Takahiro9c709c72019-05-24 14:10:36 +09001193 /* Set short name entry */
AKASHI Takahiro704df6a2018-09-11 15:59:05 +09001194 fill_dentry(itr->fsdata, itr->dent, filename, 0, size, 0x20);
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001195
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001196 retdent = itr->dent;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001197 }
1198
AKASHI Takahiro704df6a2018-09-11 15:59:05 +09001199 ret = set_contents(mydata, retdent, pos, buffer, size, actwrite);
Benoît Thébaudeaue876be42015-09-28 15:45:31 +02001200 if (ret < 0) {
1201 printf("Error: writing contents\n");
AKASHI Takahirof1149ce2018-09-11 15:59:03 +09001202 ret = -EIO;
Benoît Thébaudeaue876be42015-09-28 15:45:31 +02001203 goto exit;
1204 }
1205 debug("attempt to write 0x%llx bytes\n", *actwrite);
1206
1207 /* Flush fat buffer */
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +02001208 ret = flush_dirty_fat_buffer(mydata);
Benoît Thébaudeaue876be42015-09-28 15:45:31 +02001209 if (ret) {
1210 printf("Error: flush fat buffer\n");
AKASHI Takahirof1149ce2018-09-11 15:59:03 +09001211 ret = -EIO;
Benoît Thébaudeaue876be42015-09-28 15:45:31 +02001212 goto exit;
1213 }
1214
1215 /* Write directory table to device */
AKASHI Takahiroa9f67062019-05-24 14:10:35 +09001216 ret = flush_dir(itr);
AKASHI Takahirof1149ce2018-09-11 15:59:03 +09001217 if (ret) {
Benoît Thébaudeaue876be42015-09-28 15:45:31 +02001218 printf("Error: writing directory entry\n");
AKASHI Takahirof1149ce2018-09-11 15:59:03 +09001219 ret = -EIO;
1220 }
Benoît Thébaudeaue876be42015-09-28 15:45:31 +02001221
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001222exit:
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001223 free(filename_copy);
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001224 free(mydata->fatbuf);
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001225 free(itr);
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001226 return ret;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001227}
1228
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001229int file_fat_write(const char *filename, void *buffer, loff_t offset,
1230 loff_t maxsize, loff_t *actwrite)
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001231{
AKASHI Takahiro704df6a2018-09-11 15:59:05 +09001232 return file_fat_write_at(filename, offset, buffer, maxsize, actwrite);
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001233}
AKASHI Takahiro31a18d52018-09-11 15:59:10 +09001234
AKASHI Takahirof8240ce2018-09-11 15:59:14 +09001235static int fat_dir_entries(fat_itr *itr)
1236{
1237 fat_itr *dirs;
1238 fsdata fsdata = { .fatbuf = NULL, }, *mydata = &fsdata;
1239 /* for FATBUFSIZE */
1240 int count;
1241
1242 dirs = malloc_cache_aligned(sizeof(fat_itr));
1243 if (!dirs) {
1244 debug("Error: allocating memory\n");
1245 count = -ENOMEM;
1246 goto exit;
1247 }
1248
1249 /* duplicate fsdata */
1250 fat_itr_child(dirs, itr);
1251 fsdata = *dirs->fsdata;
1252
1253 /* allocate local fat buffer */
1254 fsdata.fatbuf = malloc_cache_aligned(FATBUFSIZE);
1255 if (!fsdata.fatbuf) {
1256 debug("Error: allocating memory\n");
1257 count = -ENOMEM;
1258 goto exit;
1259 }
1260 fsdata.fatbufnum = -1;
1261 dirs->fsdata = &fsdata;
1262
1263 for (count = 0; fat_itr_next(dirs); count++)
1264 ;
1265
1266exit:
1267 free(fsdata.fatbuf);
1268 free(dirs);
1269 return count;
1270}
1271
1272static int delete_dentry(fat_itr *itr)
1273{
1274 fsdata *mydata = itr->fsdata;
1275 dir_entry *dentptr = itr->dent;
1276
1277 /* free cluster blocks */
1278 clear_fatent(mydata, START(dentptr));
1279 if (flush_dirty_fat_buffer(mydata) < 0) {
1280 printf("Error: flush fat buffer\n");
1281 return -EIO;
1282 }
1283
1284 /*
1285 * update a directory entry
1286 * TODO:
1287 * - long file name support
1288 * - find and mark the "new" first invalid entry as name[0]=0x00
1289 */
1290 memset(dentptr, 0, sizeof(*dentptr));
1291 dentptr->name[0] = 0xe5;
1292
AKASHI Takahiroa9f67062019-05-24 14:10:35 +09001293 if (flush_dir(itr)) {
AKASHI Takahirof8240ce2018-09-11 15:59:14 +09001294 printf("error: writing directory entry\n");
1295 return -EIO;
1296 }
1297
1298 return 0;
1299}
1300
1301int fat_unlink(const char *filename)
1302{
1303 fsdata fsdata = { .fatbuf = NULL, };
1304 fat_itr *itr = NULL;
1305 int n_entries, ret;
1306 char *filename_copy, *dirname, *basename;
1307
1308 filename_copy = strdup(filename);
Heinrich Schuchardt0d532e92018-10-02 06:58:00 +02001309 if (!filename_copy) {
1310 printf("Error: allocating memory\n");
1311 ret = -ENOMEM;
1312 goto exit;
1313 }
AKASHI Takahirof8240ce2018-09-11 15:59:14 +09001314 split_filename(filename_copy, &dirname, &basename);
1315
1316 if (!strcmp(dirname, "/") && !strcmp(basename, "")) {
1317 printf("Error: cannot remove root\n");
1318 ret = -EINVAL;
1319 goto exit;
1320 }
1321
1322 itr = malloc_cache_aligned(sizeof(fat_itr));
1323 if (!itr) {
1324 printf("Error: allocating memory\n");
Heinrich Schuchardt0d532e92018-10-02 06:58:00 +02001325 ret = -ENOMEM;
1326 goto exit;
AKASHI Takahirof8240ce2018-09-11 15:59:14 +09001327 }
1328
1329 ret = fat_itr_root(itr, &fsdata);
1330 if (ret)
1331 goto exit;
1332
1333 total_sector = fsdata.total_sect;
1334
1335 ret = fat_itr_resolve(itr, dirname, TYPE_DIR);
1336 if (ret) {
1337 printf("%s: doesn't exist (%d)\n", dirname, ret);
1338 ret = -ENOENT;
1339 goto exit;
1340 }
1341
1342 if (!find_directory_entry(itr, basename)) {
1343 printf("%s: doesn't exist\n", basename);
1344 ret = -ENOENT;
1345 goto exit;
1346 }
1347
1348 if (fat_itr_isdir(itr)) {
1349 n_entries = fat_dir_entries(itr);
1350 if (n_entries < 0) {
1351 ret = n_entries;
1352 goto exit;
1353 }
1354 if (n_entries > 2) {
1355 printf("Error: directory is not empty: %d\n",
1356 n_entries);
1357 ret = -EINVAL;
1358 goto exit;
1359 }
1360 }
1361
1362 ret = delete_dentry(itr);
1363
1364exit:
1365 free(fsdata.fatbuf);
1366 free(itr);
1367 free(filename_copy);
1368
1369 return ret;
1370}
1371
AKASHI Takahiro31a18d52018-09-11 15:59:10 +09001372int fat_mkdir(const char *new_dirname)
1373{
1374 dir_entry *retdent;
1375 fsdata datablock = { .fatbuf = NULL, };
1376 fsdata *mydata = &datablock;
1377 fat_itr *itr = NULL;
1378 char *dirname_copy, *parent, *dirname;
1379 char l_dirname[VFAT_MAXLEN_BYTES];
1380 int ret = -1;
1381 loff_t actwrite;
1382 unsigned int bytesperclust;
1383 dir_entry *dotdent = NULL;
1384
1385 dirname_copy = strdup(new_dirname);
1386 if (!dirname_copy)
1387 goto exit;
1388
1389 split_filename(dirname_copy, &parent, &dirname);
1390 if (!strlen(dirname)) {
1391 ret = -EINVAL;
1392 goto exit;
1393 }
1394
1395 if (normalize_longname(l_dirname, dirname)) {
1396 printf("FAT: illegal filename (%s)\n", dirname);
1397 ret = -EINVAL;
1398 goto exit;
1399 }
1400
1401 itr = malloc_cache_aligned(sizeof(fat_itr));
1402 if (!itr) {
1403 ret = -ENOMEM;
1404 goto exit;
1405 }
1406
1407 ret = fat_itr_root(itr, &datablock);
1408 if (ret)
1409 goto exit;
1410
1411 total_sector = datablock.total_sect;
1412
1413 ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1414 if (ret) {
1415 printf("%s: doesn't exist (%d)\n", parent, ret);
1416 goto exit;
1417 }
1418
1419 retdent = find_directory_entry(itr, l_dirname);
1420
1421 if (retdent) {
1422 printf("%s: already exists\n", l_dirname);
1423 ret = -EEXIST;
1424 goto exit;
1425 } else {
1426 if (itr->is_root) {
1427 /* root dir cannot have "." or ".." */
1428 if (!strcmp(l_dirname, ".") ||
1429 !strcmp(l_dirname, "..")) {
1430 ret = -EINVAL;
1431 goto exit;
1432 }
1433 }
1434
1435 if (!itr->dent) {
1436 printf("Error: allocating new dir entry\n");
1437 ret = -EIO;
1438 goto exit;
1439 }
1440
1441 memset(itr->dent, 0, sizeof(*itr->dent));
1442
1443 /* Set short name to set alias checksum field in dir_slot */
1444 set_name(itr->dent, dirname);
1445 fill_dir_slot(itr, dirname);
1446
1447 /* Set attribute as archive for regular file */
1448 fill_dentry(itr->fsdata, itr->dent, dirname, 0, 0,
1449 ATTR_DIR | ATTR_ARCH);
1450
1451 retdent = itr->dent;
1452 }
1453
1454 /* Default entries */
1455 bytesperclust = mydata->clust_size * mydata->sect_size;
1456 dotdent = malloc_cache_aligned(bytesperclust);
1457 if (!dotdent) {
1458 ret = -ENOMEM;
1459 goto exit;
1460 }
1461 memset(dotdent, 0, bytesperclust);
1462
1463 memcpy(dotdent[0].name, ". ", 8);
1464 memcpy(dotdent[0].ext, " ", 3);
1465 dotdent[0].attr = ATTR_DIR | ATTR_ARCH;
1466
1467 memcpy(dotdent[1].name, ".. ", 8);
1468 memcpy(dotdent[1].ext, " ", 3);
1469 dotdent[1].attr = ATTR_DIR | ATTR_ARCH;
1470 set_start_cluster(mydata, &dotdent[1], itr->start_clust);
1471
1472 ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
1473 bytesperclust, &actwrite);
1474 if (ret < 0) {
1475 printf("Error: writing contents\n");
1476 goto exit;
1477 }
1478 /* Write twice for "." */
1479 set_start_cluster(mydata, &dotdent[0], START(retdent));
1480 ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
1481 bytesperclust, &actwrite);
1482 if (ret < 0) {
1483 printf("Error: writing contents\n");
1484 goto exit;
1485 }
1486
1487 /* Flush fat buffer */
1488 ret = flush_dirty_fat_buffer(mydata);
1489 if (ret) {
1490 printf("Error: flush fat buffer\n");
1491 goto exit;
1492 }
1493
1494 /* Write directory table to device */
AKASHI Takahiroa9f67062019-05-24 14:10:35 +09001495 ret = flush_dir(itr);
AKASHI Takahiro31a18d52018-09-11 15:59:10 +09001496 if (ret)
1497 printf("Error: writing directory entry\n");
1498
1499exit:
1500 free(dirname_copy);
1501 free(mydata->fatbuf);
1502 free(itr);
1503 free(dotdent);
1504 return ret;
1505}