blob: 320a8a7a87d4bfb485bbfcf71c357cb7e266d498 [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 Glass336d4612020-02-03 07:36:16 -070012#include <malloc.h>
Donggeun Kimc30a15e2011-10-24 21:15:28 +000013#include <asm/byteorder.h>
14#include <part.h>
Richard Genoudfb7e16c2012-12-13 00:47:36 +000015#include <linux/ctype.h>
Tom Rini9e374e72014-11-24 11:50:46 -050016#include <div64.h>
17#include <linux/math64.h>
Donggeun Kimc30a15e2011-10-24 21:15:28 +000018#include "fat.c"
19
20static void uppercase(char *str, int len)
21{
22 int i;
23
24 for (i = 0; i < len; i++) {
Richard Genoudfb7e16c2012-12-13 00:47:36 +000025 *str = toupper(*str);
Donggeun Kimc30a15e2011-10-24 21:15:28 +000026 str++;
27 }
28}
29
30static int total_sector;
Donggeun Kim079df722012-03-22 04:38:55 +000031static int disk_write(__u32 block, __u32 nr_blocks, void *buf)
Donggeun Kimc30a15e2011-10-24 21:15:28 +000032{
Łukasz Majewski0a04ed82015-09-03 14:21:39 +020033 ulong ret;
34
Simon Glass2a981dc2016-02-29 15:25:52 -070035 if (!cur_dev)
Donggeun Kimc30a15e2011-10-24 21:15:28 +000036 return -1;
37
Donggeun Kim079df722012-03-22 04:38:55 +000038 if (cur_part_info.start + block + nr_blocks >
39 cur_part_info.start + total_sector) {
Donggeun Kimc30a15e2011-10-24 21:15:28 +000040 printf("error: overflow occurs\n");
41 return -1;
42 }
43
Simon Glass2a981dc2016-02-29 15:25:52 -070044 ret = blk_dwrite(cur_dev, cur_part_info.start + block, nr_blocks, buf);
Łukasz Majewski0a04ed82015-09-03 14:21:39 +020045 if (nr_blocks && ret == 0)
46 return -1;
47
48 return ret;
Donggeun Kimc30a15e2011-10-24 21:15:28 +000049}
50
51/*
52 * Set short name in directory entry
53 */
54static void set_name(dir_entry *dirent, const char *filename)
55{
56 char s_name[VFAT_MAXLEN_BYTES];
57 char *period;
58 int period_location, len, i, ext_num;
59
60 if (filename == NULL)
61 return;
62
63 len = strlen(filename);
64 if (len == 0)
65 return;
66
Piotr Wilczek73dc8322013-10-11 15:43:33 +020067 strcpy(s_name, filename);
Donggeun Kimc30a15e2011-10-24 21:15:28 +000068 uppercase(s_name, len);
69
70 period = strchr(s_name, '.');
71 if (period == NULL) {
72 period_location = len;
73 ext_num = 0;
74 } else {
75 period_location = period - s_name;
76 ext_num = len - period_location - 1;
77 }
78
79 /* Pad spaces when the length of file name is shorter than eight */
80 if (period_location < 8) {
81 memcpy(dirent->name, s_name, period_location);
82 for (i = period_location; i < 8; i++)
83 dirent->name[i] = ' ';
84 } else if (period_location == 8) {
85 memcpy(dirent->name, s_name, period_location);
86 } else {
87 memcpy(dirent->name, s_name, 6);
88 dirent->name[6] = '~';
89 dirent->name[7] = '1';
90 }
91
92 if (ext_num < 3) {
93 memcpy(dirent->ext, s_name + period_location + 1, ext_num);
94 for (i = ext_num; i < 3; i++)
95 dirent->ext[i] = ' ';
96 } else
97 memcpy(dirent->ext, s_name + period_location + 1, 3);
98
99 debug("name : %s\n", dirent->name);
100 debug("ext : %s\n", dirent->ext);
101}
102
103/*
104 * Write fat buffer into block device
105 */
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200106static int flush_dirty_fat_buffer(fsdata *mydata)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000107{
108 int getsize = FATBUFBLOCKS;
109 __u32 fatlength = mydata->fatlength;
110 __u8 *bufptr = mydata->fatbuf;
111 __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
112
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200113 debug("debug: evicting %d, dirty: %d\n", mydata->fatbufnum,
114 (int)mydata->fat_dirty);
115
116 if ((!mydata->fat_dirty) || (mydata->fatbufnum == -1))
117 return 0;
118
Stefan Brüns6c1a8082016-12-17 00:27:50 +0100119 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
120 if (startblock + getsize > fatlength)
121 getsize = fatlength - startblock;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000122
Stefan Brüns6c1a8082016-12-17 00:27:50 +0100123 startblock += mydata->fat_sect;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000124
125 /* Write FAT buf */
126 if (disk_write(startblock, getsize, bufptr) < 0) {
127 debug("error: writing FAT blocks\n");
128 return -1;
129 }
130
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900131 if (mydata->fats == 2) {
Donggeun Kim627182e2011-12-20 18:34:27 +0000132 /* Update corresponding second FAT blocks */
133 startblock += mydata->fatlength;
134 if (disk_write(startblock, getsize, bufptr) < 0) {
135 debug("error: writing second FAT blocks\n");
136 return -1;
137 }
138 }
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200139 mydata->fat_dirty = 0;
Donggeun Kim627182e2011-12-20 18:34:27 +0000140
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000141 return 0;
142}
143
144/*
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000145 * Set the file name information from 'name' into 'slotptr',
146 */
147static int str2slot(dir_slot *slotptr, const char *name, int *idx)
148{
149 int j, end_idx = 0;
150
151 for (j = 0; j <= 8; j += 2) {
152 if (name[*idx] == 0x00) {
153 slotptr->name0_4[j] = 0;
154 slotptr->name0_4[j + 1] = 0;
155 end_idx++;
156 goto name0_4;
157 }
158 slotptr->name0_4[j] = name[*idx];
159 (*idx)++;
160 end_idx++;
161 }
162 for (j = 0; j <= 10; j += 2) {
163 if (name[*idx] == 0x00) {
164 slotptr->name5_10[j] = 0;
165 slotptr->name5_10[j + 1] = 0;
166 end_idx++;
167 goto name5_10;
168 }
169 slotptr->name5_10[j] = name[*idx];
170 (*idx)++;
171 end_idx++;
172 }
173 for (j = 0; j <= 2; j += 2) {
174 if (name[*idx] == 0x00) {
175 slotptr->name11_12[j] = 0;
176 slotptr->name11_12[j + 1] = 0;
177 end_idx++;
178 goto name11_12;
179 }
180 slotptr->name11_12[j] = name[*idx];
181 (*idx)++;
182 end_idx++;
183 }
184
185 if (name[*idx] == 0x00)
186 return 1;
187
188 return 0;
189/* Not used characters are filled with 0xff 0xff */
190name0_4:
191 for (; end_idx < 5; end_idx++) {
192 slotptr->name0_4[end_idx * 2] = 0xff;
193 slotptr->name0_4[end_idx * 2 + 1] = 0xff;
194 }
195 end_idx = 5;
196name5_10:
197 end_idx -= 5;
198 for (; end_idx < 6; end_idx++) {
199 slotptr->name5_10[end_idx * 2] = 0xff;
200 slotptr->name5_10[end_idx * 2 + 1] = 0xff;
201 }
202 end_idx = 11;
203name11_12:
204 end_idx -= 11;
205 for (; end_idx < 2; end_idx++) {
206 slotptr->name11_12[end_idx * 2] = 0xff;
207 slotptr->name11_12[end_idx * 2 + 1] = 0xff;
208 }
209
210 return 1;
211}
212
AKASHI Takahiro9c709c72019-05-24 14:10:36 +0900213static int new_dir_table(fat_itr *itr);
214static int flush_dir(fat_itr *itr);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000215
216/*
217 * Fill dir_slot entries with appropriate name, id, and attr
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900218 * 'itr' will point to a next entry
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000219 */
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900220static int
221fill_dir_slot(fat_itr *itr, const char *l_name)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000222{
Tien Fong Chee7aa1a6b2016-07-27 23:08:56 -0700223 __u8 temp_dir_slot_buffer[MAX_LFN_SLOT * sizeof(dir_slot)];
224 dir_slot *slotptr = (dir_slot *)temp_dir_slot_buffer;
Anatolij Gustschin8506eb82011-12-15 03:12:14 +0000225 __u8 counter = 0, checksum;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000226 int idx = 0, ret;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000227
Stefan Brünsed76f912016-09-11 22:51:39 +0200228 /* Get short file name checksum value */
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900229 checksum = mkcksum(itr->dent->name, itr->dent->ext);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000230
231 do {
232 memset(slotptr, 0x00, sizeof(dir_slot));
233 ret = str2slot(slotptr, l_name, &idx);
234 slotptr->id = ++counter;
235 slotptr->attr = ATTR_VFAT;
236 slotptr->alias_checksum = checksum;
237 slotptr++;
238 } while (ret == 0);
239
240 slotptr--;
241 slotptr->id |= LAST_LONG_ENTRY_MASK;
242
243 while (counter >= 1) {
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900244 memcpy(itr->dent, slotptr, sizeof(dir_slot));
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000245 slotptr--;
246 counter--;
AKASHI Takahiro9c709c72019-05-24 14:10:36 +0900247
248 if (itr->remaining == 0)
249 flush_dir(itr);
250
AKASHI Takahirocd2d7272019-05-24 14:10:37 +0900251 /* allocate a cluster for more entries */
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900252 if (!fat_itr_next(itr))
AKASHI Takahirocd2d7272019-05-24 14:10:37 +0900253 if (!itr->dent &&
254 (!itr->is_root || itr->fsdata->fatsize == 32) &&
255 new_dir_table(itr))
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000256 return -1;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000257 }
258
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000259 return 0;
260}
261
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000262/*
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500263 * Set the entry at index 'entry' in a FAT (12/16/32) table.
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000264 */
265static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
266{
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500267 __u32 bufnum, offset, off16;
268 __u16 val1, val2;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000269
270 switch (mydata->fatsize) {
271 case 32:
272 bufnum = entry / FAT32BUFSIZE;
273 offset = entry - bufnum * FAT32BUFSIZE;
274 break;
275 case 16:
276 bufnum = entry / FAT16BUFSIZE;
277 offset = entry - bufnum * FAT16BUFSIZE;
278 break;
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500279 case 12:
280 bufnum = entry / FAT12BUFSIZE;
281 offset = entry - bufnum * FAT12BUFSIZE;
282 break;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000283 default:
284 /* Unsupported FAT size */
285 return -1;
286 }
287
288 /* Read a new block of FAT entries into the cache. */
289 if (bufnum != mydata->fatbufnum) {
290 int getsize = FATBUFBLOCKS;
291 __u8 *bufptr = mydata->fatbuf;
292 __u32 fatlength = mydata->fatlength;
293 __u32 startblock = bufnum * FATBUFBLOCKS;
294
Stefan Brüns6c1a8082016-12-17 00:27:50 +0100295 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
296 if (startblock + getsize > fatlength)
297 getsize = fatlength - startblock;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000298
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200299 if (flush_dirty_fat_buffer(mydata) < 0)
300 return -1;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000301
Stefan Brüns6c1a8082016-12-17 00:27:50 +0100302 startblock += mydata->fat_sect;
303
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000304 if (disk_read(startblock, getsize, bufptr) < 0) {
305 debug("Error reading FAT blocks\n");
306 return -1;
307 }
308 mydata->fatbufnum = bufnum;
309 }
310
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200311 /* Mark as dirty */
312 mydata->fat_dirty = 1;
313
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000314 /* Set the actual entry */
315 switch (mydata->fatsize) {
316 case 32:
317 ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
318 break;
319 case 16:
320 ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
321 break;
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500322 case 12:
323 off16 = (offset * 3) / 4;
324
325 switch (offset & 0x3) {
326 case 0:
327 val1 = cpu_to_le16(entry_value) & 0xfff;
328 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff;
329 ((__u16 *)mydata->fatbuf)[off16] |= val1;
330 break;
331 case 1:
332 val1 = cpu_to_le16(entry_value) & 0xf;
333 val2 = (cpu_to_le16(entry_value) >> 4) & 0xff;
334
335 ((__u16 *)mydata->fatbuf)[off16] &= ~0xf000;
336 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 12);
337
338 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xff;
339 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
340 break;
341 case 2:
342 val1 = cpu_to_le16(entry_value) & 0xff;
343 val2 = (cpu_to_le16(entry_value) >> 8) & 0xf;
344
345 ((__u16 *)mydata->fatbuf)[off16] &= ~0xff00;
346 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 8);
347
348 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xf;
349 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
350 break;
351 case 3:
352 val1 = cpu_to_le16(entry_value) & 0xfff;
353 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff0;
354 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 4);
355 break;
356 default:
357 break;
358 }
359
360 break;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000361 default:
362 return -1;
363 }
364
365 return 0;
366}
367
368/*
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500369 * Determine the next free cluster after 'entry' in a FAT (12/16/32) table
Stefan Brünsae1755b2016-09-11 22:51:41 +0200370 * and link it to 'entry'. EOC marker is not set on returned entry.
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000371 */
372static __u32 determine_fatent(fsdata *mydata, __u32 entry)
373{
374 __u32 next_fat, next_entry = entry + 1;
375
376 while (1) {
Stefan Brünsb8948d22016-12-17 00:27:51 +0100377 next_fat = get_fatent(mydata, next_entry);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000378 if (next_fat == 0) {
Stefan Brünsae1755b2016-09-11 22:51:41 +0200379 /* found free entry, link to entry */
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000380 set_fatent_value(mydata, entry, next_entry);
381 break;
382 }
383 next_entry++;
384 }
385 debug("FAT%d: entry: %08x, entry_value: %04x\n",
386 mydata->fatsize, entry, next_entry);
387
388 return next_entry;
389}
390
Heinrich Schuchardtf105fe72018-10-02 09:30:45 +0200391/**
AKASHI Takahiroa9f67062019-05-24 14:10:35 +0900392 * set_sectors() - write data to sectors
Heinrich Schuchardtf105fe72018-10-02 09:30:45 +0200393 *
AKASHI Takahiroa9f67062019-05-24 14:10:35 +0900394 * Write 'size' bytes from 'buffer' into the specified sector.
Heinrich Schuchardtf105fe72018-10-02 09:30:45 +0200395 *
396 * @mydata: data to be written
AKASHI Takahiroa9f67062019-05-24 14:10:35 +0900397 * @startsect: sector to be written to
Heinrich Schuchardtf105fe72018-10-02 09:30:45 +0200398 * @buffer: data to be written
399 * @size: bytes to be written (but not more than the size of a cluster)
400 * Return: 0 on success, -1 otherwise
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000401 */
402static int
AKASHI Takahiroa9f67062019-05-24 14:10:35 +0900403set_sectors(fsdata *mydata, u32 startsect, u8 *buffer, u32 size)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000404{
AKASHI Takahiroa9f67062019-05-24 14:10:35 +0900405 u32 nsects = 0;
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200406 int ret;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000407
AKASHI Takahiroa9f67062019-05-24 14:10:35 +0900408 debug("startsect: %d\n", startsect);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000409
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200410 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
411 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
412
Heinrich Schuchardt1c381ce2018-09-13 19:42:47 +0200413 debug("FAT: Misaligned buffer address (%p)\n", buffer);
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200414
415 while (size >= mydata->sect_size) {
416 memcpy(tmpbuf, buffer, mydata->sect_size);
417 ret = disk_write(startsect++, 1, tmpbuf);
418 if (ret != 1) {
419 debug("Error writing data (got %d)\n", ret);
420 return -1;
421 }
422
423 buffer += mydata->sect_size;
424 size -= mydata->sect_size;
425 }
426 } else if (size >= mydata->sect_size) {
AKASHI Takahiroa9f67062019-05-24 14:10:35 +0900427 nsects = size / mydata->sect_size;
428 ret = disk_write(startsect, nsects, buffer);
429 if (ret != nsects) {
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200430 debug("Error writing data (got %d)\n", ret);
Wu, Josh6b8f1852013-07-24 17:55:30 +0800431 return -1;
432 }
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200433
AKASHI Takahiroa9f67062019-05-24 14:10:35 +0900434 startsect += nsects;
435 buffer += nsects * mydata->sect_size;
436 size -= nsects * mydata->sect_size;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000437 }
438
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200439 if (size) {
440 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Heinrich Schuchardtf105fe72018-10-02 09:30:45 +0200441 /* Do not leak content of stack */
442 memset(tmpbuf, 0, mydata->sect_size);
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200443 memcpy(tmpbuf, buffer, size);
444 ret = disk_write(startsect, 1, tmpbuf);
445 if (ret != 1) {
446 debug("Error writing data (got %d)\n", ret);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000447 return -1;
448 }
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000449 }
450
451 return 0;
452}
453
AKASHI Takahiroa9f67062019-05-24 14:10:35 +0900454/**
455 * set_cluster() - write data to cluster
456 *
457 * Write 'size' bytes from 'buffer' into the specified cluster.
458 *
459 * @mydata: data to be written
460 * @clustnum: cluster to be written to
461 * @buffer: data to be written
462 * @size: bytes to be written (but not more than the size of a cluster)
463 * Return: 0 on success, -1 otherwise
464 */
465static int
466set_cluster(fsdata *mydata, u32 clustnum, u8 *buffer, u32 size)
467{
468 return set_sectors(mydata, clust_to_sect(mydata, clustnum),
469 buffer, size);
470}
471
472static int
473flush_dir(fat_itr *itr)
474{
475 fsdata *mydata = itr->fsdata;
476 u32 startsect, sect_offset, nsects;
477
478 if (!itr->is_root || mydata->fatsize == 32)
479 return set_cluster(mydata, itr->clust, itr->block,
480 mydata->clust_size * mydata->sect_size);
481
482 sect_offset = itr->clust * mydata->clust_size;
483 startsect = mydata->rootdir_sect + sect_offset;
484 /* do not write past the end of rootdir */
485 nsects = min_t(u32, mydata->clust_size,
486 mydata->rootdir_size - sect_offset);
487
488 return set_sectors(mydata, startsect, itr->block,
489 nsects * mydata->sect_size);
490}
491
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +0900492static __u8 tmpbuf_cluster[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN);
493
494/*
495 * Read and modify data on existing and consecutive cluster blocks
496 */
497static int
498get_set_cluster(fsdata *mydata, __u32 clustnum, loff_t pos, __u8 *buffer,
499 loff_t size, loff_t *gotsize)
500{
501 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
502 __u32 startsect;
503 loff_t wsize;
504 int clustcount, i, ret;
505
506 *gotsize = 0;
507 if (!size)
508 return 0;
509
510 assert(pos < bytesperclust);
511 startsect = clust_to_sect(mydata, clustnum);
512
513 debug("clustnum: %d, startsect: %d, pos: %lld\n",
514 clustnum, startsect, pos);
515
516 /* partial write at beginning */
517 if (pos) {
518 wsize = min(bytesperclust - pos, size);
519 ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster);
520 if (ret != mydata->clust_size) {
521 debug("Error reading data (got %d)\n", ret);
522 return -1;
523 }
524
525 memcpy(tmpbuf_cluster + pos, buffer, wsize);
526 ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster);
527 if (ret != mydata->clust_size) {
528 debug("Error writing data (got %d)\n", ret);
529 return -1;
530 }
531
532 size -= wsize;
533 buffer += wsize;
534 *gotsize += wsize;
535
536 startsect += mydata->clust_size;
537
538 if (!size)
539 return 0;
540 }
541
542 /* full-cluster write */
543 if (size >= bytesperclust) {
544 clustcount = lldiv(size, bytesperclust);
545
546 if (!((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1))) {
547 wsize = clustcount * bytesperclust;
548 ret = disk_write(startsect,
549 clustcount * mydata->clust_size,
550 buffer);
551 if (ret != clustcount * mydata->clust_size) {
552 debug("Error writing data (got %d)\n", ret);
553 return -1;
554 }
555
556 size -= wsize;
557 buffer += wsize;
558 *gotsize += wsize;
559
560 startsect += clustcount * mydata->clust_size;
561 } else {
562 for (i = 0; i < clustcount; i++) {
563 memcpy(tmpbuf_cluster, buffer, bytesperclust);
564 ret = disk_write(startsect,
565 mydata->clust_size,
566 tmpbuf_cluster);
567 if (ret != mydata->clust_size) {
568 debug("Error writing data (got %d)\n",
569 ret);
570 return -1;
571 }
572
573 size -= bytesperclust;
574 buffer += bytesperclust;
575 *gotsize += bytesperclust;
576
577 startsect += mydata->clust_size;
578 }
579 }
580 }
581
582 /* partial write at end */
583 if (size) {
584 wsize = size;
585 ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster);
586 if (ret != mydata->clust_size) {
587 debug("Error reading data (got %d)\n", ret);
588 return -1;
589 }
590 memcpy(tmpbuf_cluster, buffer, wsize);
591 ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster);
592 if (ret != mydata->clust_size) {
593 debug("Error writing data (got %d)\n", ret);
594 return -1;
595 }
596
597 size -= wsize;
598 buffer += wsize;
599 *gotsize += wsize;
600 }
601
602 assert(!size);
603
604 return 0;
605}
606
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000607/*
608 * Find the first empty cluster
609 */
610static int find_empty_cluster(fsdata *mydata)
611{
612 __u32 fat_val, entry = 3;
613
614 while (1) {
Stefan Brünsb8948d22016-12-17 00:27:51 +0100615 fat_val = get_fatent(mydata, entry);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000616 if (fat_val == 0)
617 break;
618 entry++;
619 }
620
621 return entry;
622}
623
624/*
AKASHI Takahiro9c709c72019-05-24 14:10:36 +0900625 * Allocate a cluster for additional directory entries
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000626 */
AKASHI Takahiro9c709c72019-05-24 14:10:36 +0900627static int new_dir_table(fat_itr *itr)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000628{
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900629 fsdata *mydata = itr->fsdata;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000630 int dir_newclust = 0;
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900631 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000632
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000633 dir_newclust = find_empty_cluster(mydata);
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900634 set_fatent_value(mydata, itr->clust, dir_newclust);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000635 if (mydata->fatsize == 32)
636 set_fatent_value(mydata, dir_newclust, 0xffffff8);
637 else if (mydata->fatsize == 16)
638 set_fatent_value(mydata, dir_newclust, 0xfff8);
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500639 else if (mydata->fatsize == 12)
640 set_fatent_value(mydata, dir_newclust, 0xff8);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000641
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900642 itr->clust = dir_newclust;
643 itr->next_clust = dir_newclust;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000644
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200645 if (flush_dirty_fat_buffer(mydata) < 0)
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900646 return -1;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000647
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900648 memset(itr->block, 0x00, bytesperclust);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000649
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900650 itr->dent = (dir_entry *)itr->block;
651 itr->last_cluster = 1;
652 itr->remaining = bytesperclust / sizeof(dir_entry) - 1;
653
654 return 0;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000655}
656
657/*
658 * Set empty cluster from 'entry' to the end of a file
659 */
660static int clear_fatent(fsdata *mydata, __u32 entry)
661{
662 __u32 fat_val;
663
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500664 while (!CHECK_CLUST(entry, mydata->fatsize)) {
Stefan Brünsb8948d22016-12-17 00:27:51 +0100665 fat_val = get_fatent(mydata, entry);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000666 if (fat_val != 0)
667 set_fatent_value(mydata, entry, 0);
668 else
669 break;
670
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000671 entry = fat_val;
672 }
673
674 /* Flush fat buffer */
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200675 if (flush_dirty_fat_buffer(mydata) < 0)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000676 return -1;
677
678 return 0;
679}
680
681/*
AKASHI Takahiro704df6a2018-09-11 15:59:05 +0900682 * Set start cluster in directory entry
683 */
684static void set_start_cluster(const fsdata *mydata, dir_entry *dentptr,
685 __u32 start_cluster)
686{
687 if (mydata->fatsize == 32)
688 dentptr->starthi =
689 cpu_to_le16((start_cluster & 0xffff0000) >> 16);
690 dentptr->start = cpu_to_le16(start_cluster & 0xffff);
691}
692
693/*
694 * Check whether adding a file makes the file system to
695 * exceed the size of the block device
696 * Return -1 when overflow occurs, otherwise return 0
697 */
698static int check_overflow(fsdata *mydata, __u32 clustnum, loff_t size)
699{
700 __u32 startsect, sect_num, offset;
701
702 if (clustnum > 0)
703 startsect = clust_to_sect(mydata, clustnum);
704 else
705 startsect = mydata->rootdir_sect;
706
707 sect_num = div_u64_rem(size, mydata->sect_size, &offset);
708
709 if (offset != 0)
710 sect_num++;
711
712 if (startsect + sect_num > total_sector)
713 return -1;
714 return 0;
715}
716
717/*
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000718 * Write at most 'maxsize' bytes from 'buffer' into
719 * the file associated with 'dentptr'
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800720 * Update the number of bytes written in *gotsize and return 0
721 * or return -1 on fatal errors.
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000722 */
723static int
AKASHI Takahiro704df6a2018-09-11 15:59:05 +0900724set_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, __u8 *buffer,
725 loff_t maxsize, loff_t *gotsize)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000726{
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000727 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
728 __u32 curclust = START(dentptr);
729 __u32 endclust = 0, newclust = 0;
Heinrich Schuchardt7274b762019-02-25 19:42:48 +0100730 u64 cur_pos, filesize;
731 loff_t offset, actsize, wsize;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000732
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800733 *gotsize = 0;
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +0900734 filesize = pos + maxsize;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000735
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800736 debug("%llu bytes\n", filesize);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000737
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +0900738 if (!filesize) {
739 if (!curclust)
740 return 0;
741 if (!CHECK_CLUST(curclust, mydata->fatsize) ||
742 IS_LAST_CLUST(curclust, mydata->fatsize)) {
743 clear_fatent(mydata, curclust);
744 set_start_cluster(mydata, dentptr, 0);
745 return 0;
746 }
747 debug("curclust: 0x%x\n", curclust);
748 debug("Invalid FAT entry\n");
749 return -1;
750 }
751
752 if (!curclust) {
753 assert(pos == 0);
754 goto set_clusters;
755 }
756
757 /* go to cluster at pos */
758 cur_pos = bytesperclust;
759 while (1) {
760 if (pos <= cur_pos)
761 break;
762 if (IS_LAST_CLUST(curclust, mydata->fatsize))
763 break;
764
765 newclust = get_fatent(mydata, curclust);
766 if (!IS_LAST_CLUST(newclust, mydata->fatsize) &&
767 CHECK_CLUST(newclust, mydata->fatsize)) {
768 debug("curclust: 0x%x\n", curclust);
769 debug("Invalid FAT entry\n");
770 return -1;
771 }
772
773 cur_pos += bytesperclust;
774 curclust = newclust;
775 }
776 if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
777 assert(pos == cur_pos);
778 goto set_clusters;
779 }
780
781 assert(pos < cur_pos);
782 cur_pos -= bytesperclust;
783
784 /* overwrite */
785 assert(IS_LAST_CLUST(curclust, mydata->fatsize) ||
786 !CHECK_CLUST(curclust, mydata->fatsize));
787
788 while (1) {
789 /* search for allocated consecutive clusters */
790 actsize = bytesperclust;
791 endclust = curclust;
792 while (1) {
793 if (filesize <= (cur_pos + actsize))
794 break;
795
796 newclust = get_fatent(mydata, endclust);
797
798 if (IS_LAST_CLUST(newclust, mydata->fatsize))
799 break;
800 if (CHECK_CLUST(newclust, mydata->fatsize)) {
801 debug("curclust: 0x%x\n", curclust);
802 debug("Invalid FAT entry\n");
803 return -1;
804 }
805
806 actsize += bytesperclust;
807 endclust = newclust;
808 }
809
810 /* overwrite to <curclust..endclust> */
811 if (pos < cur_pos)
812 offset = 0;
813 else
814 offset = pos - cur_pos;
815 wsize = min(cur_pos + actsize, filesize) - pos;
816 if (get_set_cluster(mydata, curclust, offset,
817 buffer, wsize, &actsize)) {
818 printf("Error get-and-setting cluster\n");
819 return -1;
820 }
821 buffer += wsize;
822 *gotsize += wsize;
823 cur_pos += offset + wsize;
824
825 if (filesize <= cur_pos)
826 break;
827
828 /* CHECK: newclust = get_fatent(mydata, endclust); */
829
830 if (IS_LAST_CLUST(newclust, mydata->fatsize))
831 /* no more clusters */
832 break;
833
834 curclust = newclust;
835 }
836
837 if (filesize <= cur_pos) {
838 /* no more write */
839 newclust = get_fatent(mydata, endclust);
840 if (!IS_LAST_CLUST(newclust, mydata->fatsize)) {
841 /* truncate the rest */
842 clear_fatent(mydata, newclust);
843
844 /* Mark end of file in FAT */
845 if (mydata->fatsize == 12)
846 newclust = 0xfff;
847 else if (mydata->fatsize == 16)
848 newclust = 0xffff;
849 else if (mydata->fatsize == 32)
850 newclust = 0xfffffff;
851 set_fatent_value(mydata, endclust, newclust);
852 }
853
854 return 0;
855 }
856
857 curclust = endclust;
858 filesize -= cur_pos;
Heinrich Schuchardt7274b762019-02-25 19:42:48 +0100859 assert(!do_div(cur_pos, bytesperclust));
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +0900860
861set_clusters:
862 /* allocate and write */
863 assert(!pos);
864
865 /* Assure that curclust is valid */
866 if (!curclust) {
867 curclust = find_empty_cluster(mydata);
868 set_start_cluster(mydata, dentptr, curclust);
869 } else {
870 newclust = get_fatent(mydata, curclust);
871
872 if (IS_LAST_CLUST(newclust, mydata->fatsize)) {
873 newclust = determine_fatent(mydata, curclust);
874 set_fatent_value(mydata, curclust, newclust);
875 curclust = newclust;
876 } else {
877 debug("error: something wrong\n");
Benoît Thébaudeau1254b442015-09-28 15:45:32 +0200878 return -1;
879 }
AKASHI Takahiro704df6a2018-09-11 15:59:05 +0900880 }
881
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +0900882 /* TODO: already partially written */
AKASHI Takahiro704df6a2018-09-11 15:59:05 +0900883 if (check_overflow(mydata, curclust, filesize)) {
884 printf("Error: no space left: %llu\n", filesize);
885 return -1;
Benoît Thébaudeau1254b442015-09-28 15:45:32 +0200886 }
887
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000888 actsize = bytesperclust;
889 endclust = curclust;
890 do {
891 /* search for consecutive clusters */
892 while (actsize < filesize) {
893 newclust = determine_fatent(mydata, endclust);
894
895 if ((newclust - 1) != endclust)
AKASHI Takahiro704df6a2018-09-11 15:59:05 +0900896 /* write to <curclust..endclust> */
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000897 goto getit;
898
899 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Benoît Thébaudeau5e1a8602015-09-28 15:45:30 +0200900 debug("newclust: 0x%x\n", newclust);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000901 debug("Invalid FAT entry\n");
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800902 return 0;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000903 }
904 endclust = newclust;
905 actsize += bytesperclust;
906 }
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000907
908 /* set remaining bytes */
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000909 actsize = filesize;
Heinrich Schuchardtf105fe72018-10-02 09:30:45 +0200910 if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000911 debug("error: writing cluster\n");
912 return -1;
913 }
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800914 *gotsize += actsize;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000915
916 /* Mark end of file in FAT */
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500917 if (mydata->fatsize == 12)
918 newclust = 0xfff;
919 else if (mydata->fatsize == 16)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000920 newclust = 0xffff;
921 else if (mydata->fatsize == 32)
922 newclust = 0xfffffff;
923 set_fatent_value(mydata, endclust, newclust);
924
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800925 return 0;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000926getit:
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 filesize -= actsize;
933 buffer += actsize;
934
Benoît Thébaudeau5e1a8602015-09-28 15:45:30 +0200935 if (CHECK_CLUST(newclust, mydata->fatsize)) {
936 debug("newclust: 0x%x\n", newclust);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000937 debug("Invalid FAT entry\n");
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800938 return 0;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000939 }
940 actsize = bytesperclust;
941 curclust = endclust = newclust;
942 } while (1);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000943
AKASHI Takahiro704df6a2018-09-11 15:59:05 +0900944 return 0;
Benoît Thébaudeau1254b442015-09-28 15:45:32 +0200945}
946
947/*
948 * Fill dir_entry
949 */
950static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
951 const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
952{
953 set_start_cluster(mydata, dentptr, start_cluster);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000954 dentptr->size = cpu_to_le32(size);
955
956 dentptr->attr = attr;
957
958 set_name(dentptr, filename);
959}
960
961/*
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000962 * Find a directory entry based on filename or start cluster number
963 * If the directory entry is not found,
964 * the new position for writing a directory entry will be returned
965 */
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900966static dir_entry *find_directory_entry(fat_itr *itr, char *filename)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000967{
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900968 int match = 0;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000969
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900970 while (fat_itr_next(itr)) {
971 /* check both long and short name: */
972 if (!strcasecmp(filename, itr->name))
973 match = 1;
974 else if (itr->name != itr->s_name &&
975 !strcasecmp(filename, itr->s_name))
976 match = 1;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000977
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900978 if (!match)
979 continue;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000980
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900981 if (itr->dent->name[0] == '\0')
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000982 return NULL;
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900983 else
984 return itr->dent;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000985 }
986
AKASHI Takahirocd2d7272019-05-24 14:10:37 +0900987 /* allocate a cluster for more entries */
988 if (!itr->dent &&
989 (!itr->is_root || itr->fsdata->fatsize == 32) &&
990 new_dir_table(itr))
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900991 /* indicate that allocating dent failed */
992 itr->dent = NULL;
993
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000994 return NULL;
995}
996
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900997static int split_filename(char *filename, char **dirname, char **basename)
998{
999 char *p, *last_slash, *last_slash_cont;
1000
1001again:
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;
1014 }
1015 p++;
1016 }
1017
1018 if (last_slash) {
1019 if (last_slash_cont == (filename + strlen(filename) - 1)) {
1020 /* remove trailing slashes */
1021 *last_slash = '\0';
1022 goto again;
1023 }
1024
1025 if (last_slash == filename) {
1026 /* avoid ""(null) directory */
1027 *dirname = "/";
1028 } else {
1029 *last_slash = '\0';
1030 *dirname = filename;
1031 }
1032
1033 *last_slash_cont = '\0';
1034 *basename = last_slash_cont + 1;
1035 } else {
1036 *dirname = "/"; /* root by default */
1037 *basename = filename;
1038 }
1039
1040 return 0;
1041}
1042
Heinrich Schuchardt7b437802019-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 Takahiro25bb9da2018-09-11 15:59:02 +09001053static int normalize_longname(char *l_filename, const char *filename)
1054{
Heinrich Schuchardt7b437802019-05-12 09:59:18 +02001055 const char *p, illegal[] = "<>:\"/\\|?*";
AKASHI Takahiro25bb9da2018-09-11 15:59:02 +09001056
Heinrich Schuchardt7b437802019-05-12 09:59:18 +02001057 if (strlen(filename) >= VFAT_MAXLEN_BYTES)
AKASHI Takahiro25bb9da2018-09-11 15:59:02 +09001058 return -1;
Heinrich Schuchardt7b437802019-05-12 09:59:18 +02001059
1060 for (p = filename; *p; ++p) {
1061 if ((unsigned char)*p < 0x20)
1062 return -1;
1063 if (strchr(illegal, *p))
1064 return -1;
AKASHI Takahiro25bb9da2018-09-11 15:59:02 +09001065 }
1066
Heinrich Schuchardt7b437802019-05-12 09:59:18 +02001067 strcpy(l_filename, filename);
1068 downcase(l_filename, VFAT_MAXLEN_BYTES);
AKASHI Takahiro25bb9da2018-09-11 15:59:02 +09001069
1070 return 0;
1071}
1072
AKASHI Takahiro704df6a2018-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 Kimc30a15e2011-10-24 21:15:28 +00001075{
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001076 dir_entry *retdent;
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001077 fsdata datablock = { .fatbuf = NULL, };
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001078 fsdata *mydata = &datablock;
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001079 fat_itr *itr = NULL;
AKASHI Takahiro25bb9da2018-09-11 15:59:02 +09001080 int ret = -1;
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001081 char *filename_copy, *parent, *basename;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001082 char l_filename[VFAT_MAXLEN_BYTES];
1083
AKASHI Takahiro704df6a2018-09-11 15:59:05 +09001084 debug("writing %s\n", filename);
1085
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001086 filename_copy = strdup(filename);
1087 if (!filename_copy)
AKASHI Takahirof1149ce2018-09-11 15:59:03 +09001088 return -ENOMEM;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001089
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001090 split_filename(filename_copy, &parent, &basename);
1091 if (!strlen(basename)) {
1092 ret = -EINVAL;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001093 goto exit;
1094 }
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001095
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001096 filename = basename;
AKASHI Takahiro25bb9da2018-09-11 15:59:02 +09001097 if (normalize_longname(l_filename, filename)) {
1098 printf("FAT: illegal filename (%s)\n", filename);
1099 ret = -EINVAL;
1100 goto exit;
1101 }
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001102
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001103 itr = malloc_cache_aligned(sizeof(fat_itr));
1104 if (!itr) {
1105 ret = -ENOMEM;
1106 goto exit;
1107 }
1108
1109 ret = fat_itr_root(itr, &datablock);
1110 if (ret)
1111 goto exit;
1112
1113 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);
1118 goto exit;
1119 }
1120
1121 retdent = find_directory_entry(itr, l_filename);
1122
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001123 if (retdent) {
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001124 if (fat_itr_isdir(itr)) {
1125 ret = -EISDIR;
1126 goto exit;
1127 }
1128
AKASHI Takahirocb8af8a2018-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 Takahiro704df6a2018-09-11 15:59:05 +09001139 /* Update file size in a directory entry */
1140 retdent->size = cpu_to_le32(pos + size);
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001141 } else {
AKASHI Takahiro4ced2032018-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 Takahirocb8af8a2018-09-11 15:59:06 +09001159 if (pos) {
1160 /* No hole allowed */
1161 ret = -EINVAL;
1162 goto exit;
1163 }
1164
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001165 memset(itr->dent, 0, sizeof(*itr->dent));
1166
AKASHI Takahiro9c709c72019-05-24 14:10:36 +09001167 /* Calculate checksum for short name */
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001168 set_name(itr->dent, filename);
AKASHI Takahiro9c709c72019-05-24 14:10:36 +09001169
1170 /* Set long name entries */
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001171 if (fill_dir_slot(itr, filename)) {
1172 ret = -EIO;
1173 goto exit;
1174 }
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001175
AKASHI Takahiro9c709c72019-05-24 14:10:36 +09001176 /* Set short name entry */
AKASHI Takahiro704df6a2018-09-11 15:59:05 +09001177 fill_dentry(itr->fsdata, itr->dent, filename, 0, size, 0x20);
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001178
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001179 retdent = itr->dent;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001180 }
1181
AKASHI Takahiro704df6a2018-09-11 15:59:05 +09001182 ret = set_contents(mydata, retdent, pos, buffer, size, actwrite);
Benoît Thébaudeaue876be42015-09-28 15:45:31 +02001183 if (ret < 0) {
1184 printf("Error: writing contents\n");
AKASHI Takahirof1149ce2018-09-11 15:59:03 +09001185 ret = -EIO;
Benoît Thébaudeaue876be42015-09-28 15:45:31 +02001186 goto exit;
1187 }
1188 debug("attempt to write 0x%llx bytes\n", *actwrite);
1189
1190 /* Flush fat buffer */
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +02001191 ret = flush_dirty_fat_buffer(mydata);
Benoît Thébaudeaue876be42015-09-28 15:45:31 +02001192 if (ret) {
1193 printf("Error: flush fat buffer\n");
AKASHI Takahirof1149ce2018-09-11 15:59:03 +09001194 ret = -EIO;
Benoît Thébaudeaue876be42015-09-28 15:45:31 +02001195 goto exit;
1196 }
1197
1198 /* Write directory table to device */
AKASHI Takahiroa9f67062019-05-24 14:10:35 +09001199 ret = flush_dir(itr);
AKASHI Takahirof1149ce2018-09-11 15:59:03 +09001200 if (ret) {
Benoît Thébaudeaue876be42015-09-28 15:45:31 +02001201 printf("Error: writing directory entry\n");
AKASHI Takahirof1149ce2018-09-11 15:59:03 +09001202 ret = -EIO;
1203 }
Benoît Thébaudeaue876be42015-09-28 15:45:31 +02001204
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001205exit:
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001206 free(filename_copy);
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001207 free(mydata->fatbuf);
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001208 free(itr);
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001209 return ret;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001210}
1211
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001212int file_fat_write(const char *filename, void *buffer, loff_t offset,
1213 loff_t maxsize, loff_t *actwrite)
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001214{
AKASHI Takahiro704df6a2018-09-11 15:59:05 +09001215 return file_fat_write_at(filename, offset, buffer, maxsize, actwrite);
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001216}
AKASHI Takahiro31a18d52018-09-11 15:59:10 +09001217
AKASHI Takahirof8240ce2018-09-11 15:59:14 +09001218static int fat_dir_entries(fat_itr *itr)
1219{
1220 fat_itr *dirs;
1221 fsdata fsdata = { .fatbuf = NULL, }, *mydata = &fsdata;
1222 /* for FATBUFSIZE */
1223 int count;
1224
1225 dirs = malloc_cache_aligned(sizeof(fat_itr));
1226 if (!dirs) {
1227 debug("Error: allocating memory\n");
1228 count = -ENOMEM;
1229 goto exit;
1230 }
1231
1232 /* duplicate fsdata */
1233 fat_itr_child(dirs, itr);
1234 fsdata = *dirs->fsdata;
1235
1236 /* allocate local fat buffer */
1237 fsdata.fatbuf = malloc_cache_aligned(FATBUFSIZE);
1238 if (!fsdata.fatbuf) {
1239 debug("Error: allocating memory\n");
1240 count = -ENOMEM;
1241 goto exit;
1242 }
1243 fsdata.fatbufnum = -1;
1244 dirs->fsdata = &fsdata;
1245
1246 for (count = 0; fat_itr_next(dirs); count++)
1247 ;
1248
1249exit:
1250 free(fsdata.fatbuf);
1251 free(dirs);
1252 return count;
1253}
1254
1255static int delete_dentry(fat_itr *itr)
1256{
1257 fsdata *mydata = itr->fsdata;
1258 dir_entry *dentptr = itr->dent;
1259
1260 /* free cluster blocks */
1261 clear_fatent(mydata, START(dentptr));
1262 if (flush_dirty_fat_buffer(mydata) < 0) {
1263 printf("Error: flush fat buffer\n");
1264 return -EIO;
1265 }
1266
1267 /*
1268 * update a directory entry
1269 * TODO:
1270 * - long file name support
1271 * - find and mark the "new" first invalid entry as name[0]=0x00
1272 */
1273 memset(dentptr, 0, sizeof(*dentptr));
1274 dentptr->name[0] = 0xe5;
1275
AKASHI Takahiroa9f67062019-05-24 14:10:35 +09001276 if (flush_dir(itr)) {
AKASHI Takahirof8240ce2018-09-11 15:59:14 +09001277 printf("error: writing directory entry\n");
1278 return -EIO;
1279 }
1280
1281 return 0;
1282}
1283
1284int fat_unlink(const char *filename)
1285{
1286 fsdata fsdata = { .fatbuf = NULL, };
1287 fat_itr *itr = NULL;
1288 int n_entries, ret;
1289 char *filename_copy, *dirname, *basename;
1290
1291 filename_copy = strdup(filename);
Heinrich Schuchardt0d532e92018-10-02 06:58:00 +02001292 if (!filename_copy) {
1293 printf("Error: allocating memory\n");
1294 ret = -ENOMEM;
1295 goto exit;
1296 }
AKASHI Takahirof8240ce2018-09-11 15:59:14 +09001297 split_filename(filename_copy, &dirname, &basename);
1298
1299 if (!strcmp(dirname, "/") && !strcmp(basename, "")) {
1300 printf("Error: cannot remove root\n");
1301 ret = -EINVAL;
1302 goto exit;
1303 }
1304
1305 itr = malloc_cache_aligned(sizeof(fat_itr));
1306 if (!itr) {
1307 printf("Error: allocating memory\n");
Heinrich Schuchardt0d532e92018-10-02 06:58:00 +02001308 ret = -ENOMEM;
1309 goto exit;
AKASHI Takahirof8240ce2018-09-11 15:59:14 +09001310 }
1311
1312 ret = fat_itr_root(itr, &fsdata);
1313 if (ret)
1314 goto exit;
1315
1316 total_sector = fsdata.total_sect;
1317
1318 ret = fat_itr_resolve(itr, dirname, TYPE_DIR);
1319 if (ret) {
1320 printf("%s: doesn't exist (%d)\n", dirname, ret);
1321 ret = -ENOENT;
1322 goto exit;
1323 }
1324
1325 if (!find_directory_entry(itr, basename)) {
1326 printf("%s: doesn't exist\n", basename);
1327 ret = -ENOENT;
1328 goto exit;
1329 }
1330
1331 if (fat_itr_isdir(itr)) {
1332 n_entries = fat_dir_entries(itr);
1333 if (n_entries < 0) {
1334 ret = n_entries;
1335 goto exit;
1336 }
1337 if (n_entries > 2) {
1338 printf("Error: directory is not empty: %d\n",
1339 n_entries);
1340 ret = -EINVAL;
1341 goto exit;
1342 }
1343 }
1344
1345 ret = delete_dentry(itr);
1346
1347exit:
1348 free(fsdata.fatbuf);
1349 free(itr);
1350 free(filename_copy);
1351
1352 return ret;
1353}
1354
AKASHI Takahiro31a18d52018-09-11 15:59:10 +09001355int fat_mkdir(const char *new_dirname)
1356{
1357 dir_entry *retdent;
1358 fsdata datablock = { .fatbuf = NULL, };
1359 fsdata *mydata = &datablock;
1360 fat_itr *itr = NULL;
1361 char *dirname_copy, *parent, *dirname;
1362 char l_dirname[VFAT_MAXLEN_BYTES];
1363 int ret = -1;
1364 loff_t actwrite;
1365 unsigned int bytesperclust;
1366 dir_entry *dotdent = NULL;
1367
1368 dirname_copy = strdup(new_dirname);
1369 if (!dirname_copy)
1370 goto exit;
1371
1372 split_filename(dirname_copy, &parent, &dirname);
1373 if (!strlen(dirname)) {
1374 ret = -EINVAL;
1375 goto exit;
1376 }
1377
1378 if (normalize_longname(l_dirname, dirname)) {
1379 printf("FAT: illegal filename (%s)\n", dirname);
1380 ret = -EINVAL;
1381 goto exit;
1382 }
1383
1384 itr = malloc_cache_aligned(sizeof(fat_itr));
1385 if (!itr) {
1386 ret = -ENOMEM;
1387 goto exit;
1388 }
1389
1390 ret = fat_itr_root(itr, &datablock);
1391 if (ret)
1392 goto exit;
1393
1394 total_sector = datablock.total_sect;
1395
1396 ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1397 if (ret) {
1398 printf("%s: doesn't exist (%d)\n", parent, ret);
1399 goto exit;
1400 }
1401
1402 retdent = find_directory_entry(itr, l_dirname);
1403
1404 if (retdent) {
1405 printf("%s: already exists\n", l_dirname);
1406 ret = -EEXIST;
1407 goto exit;
1408 } else {
1409 if (itr->is_root) {
1410 /* root dir cannot have "." or ".." */
1411 if (!strcmp(l_dirname, ".") ||
1412 !strcmp(l_dirname, "..")) {
1413 ret = -EINVAL;
1414 goto exit;
1415 }
1416 }
1417
1418 if (!itr->dent) {
1419 printf("Error: allocating new dir entry\n");
1420 ret = -EIO;
1421 goto exit;
1422 }
1423
1424 memset(itr->dent, 0, sizeof(*itr->dent));
1425
1426 /* Set short name to set alias checksum field in dir_slot */
1427 set_name(itr->dent, dirname);
1428 fill_dir_slot(itr, dirname);
1429
1430 /* Set attribute as archive for regular file */
1431 fill_dentry(itr->fsdata, itr->dent, dirname, 0, 0,
1432 ATTR_DIR | ATTR_ARCH);
1433
1434 retdent = itr->dent;
1435 }
1436
1437 /* Default entries */
1438 bytesperclust = mydata->clust_size * mydata->sect_size;
1439 dotdent = malloc_cache_aligned(bytesperclust);
1440 if (!dotdent) {
1441 ret = -ENOMEM;
1442 goto exit;
1443 }
1444 memset(dotdent, 0, bytesperclust);
1445
1446 memcpy(dotdent[0].name, ". ", 8);
1447 memcpy(dotdent[0].ext, " ", 3);
1448 dotdent[0].attr = ATTR_DIR | ATTR_ARCH;
1449
1450 memcpy(dotdent[1].name, ".. ", 8);
1451 memcpy(dotdent[1].ext, " ", 3);
1452 dotdent[1].attr = ATTR_DIR | ATTR_ARCH;
1453 set_start_cluster(mydata, &dotdent[1], itr->start_clust);
1454
1455 ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
1456 bytesperclust, &actwrite);
1457 if (ret < 0) {
1458 printf("Error: writing contents\n");
1459 goto exit;
1460 }
1461 /* Write twice for "." */
1462 set_start_cluster(mydata, &dotdent[0], START(retdent));
1463 ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
1464 bytesperclust, &actwrite);
1465 if (ret < 0) {
1466 printf("Error: writing contents\n");
1467 goto exit;
1468 }
1469
1470 /* Flush fat buffer */
1471 ret = flush_dirty_fat_buffer(mydata);
1472 if (ret) {
1473 printf("Error: flush fat buffer\n");
1474 goto exit;
1475 }
1476
1477 /* Write directory table to device */
AKASHI Takahiroa9f67062019-05-24 14:10:35 +09001478 ret = flush_dir(itr);
AKASHI Takahiro31a18d52018-09-11 15:59:10 +09001479 if (ret)
1480 printf("Error: writing directory entry\n");
1481
1482exit:
1483 free(dirname_copy);
1484 free(mydata->fatbuf);
1485 free(itr);
1486 free(dotdent);
1487 return ret;
1488}