blob: 37ef0564eb5ebc87fe2324962b9f8a37780b52a8 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Donggeun Kim8f814002011-10-24 21:15:28 +00002/*
3 * fat_write.c
4 *
5 * R/W (V)FAT 12/16/32 filesystem implementation by Donggeun Kim
Donggeun Kim8f814002011-10-24 21:15:28 +00006 */
7
8#include <common.h>
9#include <command.h>
10#include <config.h>
11#include <fat.h>
12#include <asm/byteorder.h>
13#include <part.h>
Richard Genoud2e372022012-12-13 00:47:36 +000014#include <linux/ctype.h>
Tom Rinia17b7bc2014-11-24 11:50:46 -050015#include <div64.h>
16#include <linux/math64.h>
Donggeun Kim8f814002011-10-24 21:15:28 +000017#include "fat.c"
18
19static void uppercase(char *str, int len)
20{
21 int i;
22
23 for (i = 0; i < len; i++) {
Richard Genoud2e372022012-12-13 00:47:36 +000024 *str = toupper(*str);
Donggeun Kim8f814002011-10-24 21:15:28 +000025 str++;
26 }
27}
28
29static int total_sector;
Donggeun Kimcb5f3bc2012-03-22 04:38:55 +000030static int disk_write(__u32 block, __u32 nr_blocks, void *buf)
Donggeun Kim8f814002011-10-24 21:15:28 +000031{
Łukasz Majewskid6f23d82015-09-03 14:21:39 +020032 ulong ret;
33
Simon Glass2ee8ada2016-02-29 15:25:52 -070034 if (!cur_dev)
Donggeun Kim8f814002011-10-24 21:15:28 +000035 return -1;
36
Donggeun Kimcb5f3bc2012-03-22 04:38:55 +000037 if (cur_part_info.start + block + nr_blocks >
38 cur_part_info.start + total_sector) {
Donggeun Kim8f814002011-10-24 21:15:28 +000039 printf("error: overflow occurs\n");
40 return -1;
41 }
42
Simon Glass2ee8ada2016-02-29 15:25:52 -070043 ret = blk_dwrite(cur_dev, cur_part_info.start + block, nr_blocks, buf);
Łukasz Majewskid6f23d82015-09-03 14:21:39 +020044 if (nr_blocks && ret == 0)
45 return -1;
46
47 return ret;
Donggeun Kim8f814002011-10-24 21:15:28 +000048}
49
50/*
51 * Set short name in directory entry
52 */
53static void set_name(dir_entry *dirent, const char *filename)
54{
55 char s_name[VFAT_MAXLEN_BYTES];
56 char *period;
57 int period_location, len, i, ext_num;
58
59 if (filename == NULL)
60 return;
61
62 len = strlen(filename);
63 if (len == 0)
64 return;
65
Piotr Wilczek65a43e32013-10-11 15:43:33 +020066 strcpy(s_name, filename);
Donggeun Kim8f814002011-10-24 21:15:28 +000067 uppercase(s_name, len);
68
69 period = strchr(s_name, '.');
70 if (period == NULL) {
71 period_location = len;
72 ext_num = 0;
73 } else {
74 period_location = period - s_name;
75 ext_num = len - period_location - 1;
76 }
77
78 /* Pad spaces when the length of file name is shorter than eight */
79 if (period_location < 8) {
80 memcpy(dirent->name, s_name, period_location);
81 for (i = period_location; i < 8; i++)
82 dirent->name[i] = ' ';
83 } else if (period_location == 8) {
84 memcpy(dirent->name, s_name, period_location);
85 } else {
86 memcpy(dirent->name, s_name, 6);
87 dirent->name[6] = '~';
88 dirent->name[7] = '1';
89 }
90
91 if (ext_num < 3) {
92 memcpy(dirent->ext, s_name + period_location + 1, ext_num);
93 for (i = ext_num; i < 3; i++)
94 dirent->ext[i] = ' ';
95 } else
96 memcpy(dirent->ext, s_name + period_location + 1, 3);
97
98 debug("name : %s\n", dirent->name);
99 debug("ext : %s\n", dirent->ext);
100}
101
102/*
103 * Write fat buffer into block device
104 */
Stefan Brüns751b31d2016-09-11 22:51:40 +0200105static int flush_dirty_fat_buffer(fsdata *mydata)
Donggeun Kim8f814002011-10-24 21:15:28 +0000106{
107 int getsize = FATBUFBLOCKS;
108 __u32 fatlength = mydata->fatlength;
109 __u8 *bufptr = mydata->fatbuf;
110 __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
111
Stefan Brüns751b31d2016-09-11 22:51:40 +0200112 debug("debug: evicting %d, dirty: %d\n", mydata->fatbufnum,
113 (int)mydata->fat_dirty);
114
115 if ((!mydata->fat_dirty) || (mydata->fatbufnum == -1))
116 return 0;
117
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100118 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
119 if (startblock + getsize > fatlength)
120 getsize = fatlength - startblock;
Donggeun Kim8f814002011-10-24 21:15:28 +0000121
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100122 startblock += mydata->fat_sect;
Donggeun Kim8f814002011-10-24 21:15:28 +0000123
124 /* Write FAT buf */
125 if (disk_write(startblock, getsize, bufptr) < 0) {
126 debug("error: writing FAT blocks\n");
127 return -1;
128 }
129
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900130 if (mydata->fats == 2) {
Donggeun Kimaf57ce22011-12-20 18:34:27 +0000131 /* Update corresponding second FAT blocks */
132 startblock += mydata->fatlength;
133 if (disk_write(startblock, getsize, bufptr) < 0) {
134 debug("error: writing second FAT blocks\n");
135 return -1;
136 }
137 }
Stefan Brüns751b31d2016-09-11 22:51:40 +0200138 mydata->fat_dirty = 0;
Donggeun Kimaf57ce22011-12-20 18:34:27 +0000139
Donggeun Kim8f814002011-10-24 21:15:28 +0000140 return 0;
141}
142
143/*
Donggeun Kim8f814002011-10-24 21:15:28 +0000144 * Set the file name information from 'name' into 'slotptr',
145 */
146static int str2slot(dir_slot *slotptr, const char *name, int *idx)
147{
148 int j, end_idx = 0;
149
150 for (j = 0; j <= 8; j += 2) {
151 if (name[*idx] == 0x00) {
152 slotptr->name0_4[j] = 0;
153 slotptr->name0_4[j + 1] = 0;
154 end_idx++;
155 goto name0_4;
156 }
157 slotptr->name0_4[j] = name[*idx];
158 (*idx)++;
159 end_idx++;
160 }
161 for (j = 0; j <= 10; j += 2) {
162 if (name[*idx] == 0x00) {
163 slotptr->name5_10[j] = 0;
164 slotptr->name5_10[j + 1] = 0;
165 end_idx++;
166 goto name5_10;
167 }
168 slotptr->name5_10[j] = name[*idx];
169 (*idx)++;
170 end_idx++;
171 }
172 for (j = 0; j <= 2; j += 2) {
173 if (name[*idx] == 0x00) {
174 slotptr->name11_12[j] = 0;
175 slotptr->name11_12[j + 1] = 0;
176 end_idx++;
177 goto name11_12;
178 }
179 slotptr->name11_12[j] = name[*idx];
180 (*idx)++;
181 end_idx++;
182 }
183
184 if (name[*idx] == 0x00)
185 return 1;
186
187 return 0;
188/* Not used characters are filled with 0xff 0xff */
189name0_4:
190 for (; end_idx < 5; end_idx++) {
191 slotptr->name0_4[end_idx * 2] = 0xff;
192 slotptr->name0_4[end_idx * 2 + 1] = 0xff;
193 }
194 end_idx = 5;
195name5_10:
196 end_idx -= 5;
197 for (; end_idx < 6; end_idx++) {
198 slotptr->name5_10[end_idx * 2] = 0xff;
199 slotptr->name5_10[end_idx * 2 + 1] = 0xff;
200 }
201 end_idx = 11;
202name11_12:
203 end_idx -= 11;
204 for (; end_idx < 2; end_idx++) {
205 slotptr->name11_12[end_idx * 2] = 0xff;
206 slotptr->name11_12[end_idx * 2 + 1] = 0xff;
207 }
208
209 return 1;
210}
211
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900212static int flush_dir_table(fat_itr *itr);
Donggeun Kim8f814002011-10-24 21:15:28 +0000213
214/*
215 * Fill dir_slot entries with appropriate name, id, and attr
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900216 * 'itr' will point to a next entry
Donggeun Kim8f814002011-10-24 21:15:28 +0000217 */
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900218static int
219fill_dir_slot(fat_itr *itr, const char *l_name)
Donggeun Kim8f814002011-10-24 21:15:28 +0000220{
Tien Fong Chee0117dbe2016-07-27 23:08:56 -0700221 __u8 temp_dir_slot_buffer[MAX_LFN_SLOT * sizeof(dir_slot)];
222 dir_slot *slotptr = (dir_slot *)temp_dir_slot_buffer;
Anatolij Gustschinf4f9d5d2011-12-15 03:12:14 +0000223 __u8 counter = 0, checksum;
Donggeun Kim8f814002011-10-24 21:15:28 +0000224 int idx = 0, ret;
Donggeun Kim8f814002011-10-24 21:15:28 +0000225
Stefan Brüns6c617d62016-09-11 22:51:39 +0200226 /* Get short file name checksum value */
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900227 checksum = mkcksum(itr->dent->name, itr->dent->ext);
Donggeun Kim8f814002011-10-24 21:15:28 +0000228
229 do {
230 memset(slotptr, 0x00, sizeof(dir_slot));
231 ret = str2slot(slotptr, l_name, &idx);
232 slotptr->id = ++counter;
233 slotptr->attr = ATTR_VFAT;
234 slotptr->alias_checksum = checksum;
235 slotptr++;
236 } while (ret == 0);
237
238 slotptr--;
239 slotptr->id |= LAST_LONG_ENTRY_MASK;
240
241 while (counter >= 1) {
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900242 memcpy(itr->dent, slotptr, sizeof(dir_slot));
Donggeun Kim8f814002011-10-24 21:15:28 +0000243 slotptr--;
244 counter--;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900245 if (!fat_itr_next(itr))
246 if (!itr->dent && !itr->is_root && flush_dir_table(itr))
Donggeun Kim8f814002011-10-24 21:15:28 +0000247 return -1;
Donggeun Kim8f814002011-10-24 21:15:28 +0000248 }
249
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900250 if (!itr->dent && !itr->is_root)
251 /*
252 * don't care return value here because we have already
253 * finished completing an entry with name, only ending up
254 * no more entry left
255 */
256 flush_dir_table(itr);
Donggeun Kim8f814002011-10-24 21:15:28 +0000257
258 return 0;
259}
260
Donggeun Kim8f814002011-10-24 21:15:28 +0000261/*
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500262 * Set the entry at index 'entry' in a FAT (12/16/32) table.
Donggeun Kim8f814002011-10-24 21:15:28 +0000263 */
264static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
265{
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500266 __u32 bufnum, offset, off16;
267 __u16 val1, val2;
Donggeun Kim8f814002011-10-24 21:15:28 +0000268
269 switch (mydata->fatsize) {
270 case 32:
271 bufnum = entry / FAT32BUFSIZE;
272 offset = entry - bufnum * FAT32BUFSIZE;
273 break;
274 case 16:
275 bufnum = entry / FAT16BUFSIZE;
276 offset = entry - bufnum * FAT16BUFSIZE;
277 break;
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500278 case 12:
279 bufnum = entry / FAT12BUFSIZE;
280 offset = entry - bufnum * FAT12BUFSIZE;
281 break;
Donggeun Kim8f814002011-10-24 21:15:28 +0000282 default:
283 /* Unsupported FAT size */
284 return -1;
285 }
286
287 /* Read a new block of FAT entries into the cache. */
288 if (bufnum != mydata->fatbufnum) {
289 int getsize = FATBUFBLOCKS;
290 __u8 *bufptr = mydata->fatbuf;
291 __u32 fatlength = mydata->fatlength;
292 __u32 startblock = bufnum * FATBUFBLOCKS;
293
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100294 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
295 if (startblock + getsize > fatlength)
296 getsize = fatlength - startblock;
Donggeun Kim8f814002011-10-24 21:15:28 +0000297
Stefan Brüns751b31d2016-09-11 22:51:40 +0200298 if (flush_dirty_fat_buffer(mydata) < 0)
299 return -1;
Donggeun Kim8f814002011-10-24 21:15:28 +0000300
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100301 startblock += mydata->fat_sect;
302
Donggeun Kim8f814002011-10-24 21:15:28 +0000303 if (disk_read(startblock, getsize, bufptr) < 0) {
304 debug("Error reading FAT blocks\n");
305 return -1;
306 }
307 mydata->fatbufnum = bufnum;
308 }
309
Stefan Brüns751b31d2016-09-11 22:51:40 +0200310 /* Mark as dirty */
311 mydata->fat_dirty = 1;
312
Donggeun Kim8f814002011-10-24 21:15:28 +0000313 /* Set the actual entry */
314 switch (mydata->fatsize) {
315 case 32:
316 ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
317 break;
318 case 16:
319 ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
320 break;
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500321 case 12:
322 off16 = (offset * 3) / 4;
323
324 switch (offset & 0x3) {
325 case 0:
326 val1 = cpu_to_le16(entry_value) & 0xfff;
327 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff;
328 ((__u16 *)mydata->fatbuf)[off16] |= val1;
329 break;
330 case 1:
331 val1 = cpu_to_le16(entry_value) & 0xf;
332 val2 = (cpu_to_le16(entry_value) >> 4) & 0xff;
333
334 ((__u16 *)mydata->fatbuf)[off16] &= ~0xf000;
335 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 12);
336
337 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xff;
338 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
339 break;
340 case 2:
341 val1 = cpu_to_le16(entry_value) & 0xff;
342 val2 = (cpu_to_le16(entry_value) >> 8) & 0xf;
343
344 ((__u16 *)mydata->fatbuf)[off16] &= ~0xff00;
345 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 8);
346
347 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xf;
348 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
349 break;
350 case 3:
351 val1 = cpu_to_le16(entry_value) & 0xfff;
352 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff0;
353 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 4);
354 break;
355 default:
356 break;
357 }
358
359 break;
Donggeun Kim8f814002011-10-24 21:15:28 +0000360 default:
361 return -1;
362 }
363
364 return 0;
365}
366
367/*
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500368 * Determine the next free cluster after 'entry' in a FAT (12/16/32) table
Stefan Brüns14999402016-09-11 22:51:41 +0200369 * and link it to 'entry'. EOC marker is not set on returned entry.
Donggeun Kim8f814002011-10-24 21:15:28 +0000370 */
371static __u32 determine_fatent(fsdata *mydata, __u32 entry)
372{
373 __u32 next_fat, next_entry = entry + 1;
374
375 while (1) {
Stefan Brüns9f95d812016-12-17 00:27:51 +0100376 next_fat = get_fatent(mydata, next_entry);
Donggeun Kim8f814002011-10-24 21:15:28 +0000377 if (next_fat == 0) {
Stefan Brüns14999402016-09-11 22:51:41 +0200378 /* found free entry, link to entry */
Donggeun Kim8f814002011-10-24 21:15:28 +0000379 set_fatent_value(mydata, entry, next_entry);
380 break;
381 }
382 next_entry++;
383 }
384 debug("FAT%d: entry: %08x, entry_value: %04x\n",
385 mydata->fatsize, entry, next_entry);
386
387 return next_entry;
388}
389
390/*
391 * Write at most 'size' bytes from 'buffer' into the specified cluster.
392 * Return 0 on success, -1 otherwise.
393 */
394static int
395set_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer,
396 unsigned long size)
397{
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200398 __u32 idx = 0;
Donggeun Kim8f814002011-10-24 21:15:28 +0000399 __u32 startsect;
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200400 int ret;
Donggeun Kim8f814002011-10-24 21:15:28 +0000401
402 if (clustnum > 0)
Rob Clarkc3ab0b82017-09-09 13:16:00 -0400403 startsect = clust_to_sect(mydata, clustnum);
Donggeun Kim8f814002011-10-24 21:15:28 +0000404 else
405 startsect = mydata->rootdir_sect;
406
407 debug("clustnum: %d, startsect: %d\n", clustnum, startsect);
408
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200409 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
410 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
411
412 printf("FAT: Misaligned buffer address (%p)\n", buffer);
413
414 while (size >= mydata->sect_size) {
415 memcpy(tmpbuf, buffer, mydata->sect_size);
416 ret = disk_write(startsect++, 1, tmpbuf);
417 if (ret != 1) {
418 debug("Error writing data (got %d)\n", ret);
419 return -1;
420 }
421
422 buffer += mydata->sect_size;
423 size -= mydata->sect_size;
424 }
425 } else if (size >= mydata->sect_size) {
426 idx = size / mydata->sect_size;
427 ret = disk_write(startsect, idx, buffer);
428 if (ret != idx) {
429 debug("Error writing data (got %d)\n", ret);
Wu, Joshb0e38dd2013-07-24 17:55:30 +0800430 return -1;
431 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000432
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200433 startsect += idx;
434 idx *= mydata->sect_size;
435 buffer += idx;
436 size -= idx;
437 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000438
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200439 if (size) {
440 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Donggeun Kim8f814002011-10-24 21:15:28 +0000441
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200442 memcpy(tmpbuf, buffer, size);
443 ret = disk_write(startsect, 1, tmpbuf);
444 if (ret != 1) {
445 debug("Error writing data (got %d)\n", ret);
Donggeun Kim8f814002011-10-24 21:15:28 +0000446 return -1;
447 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000448 }
449
450 return 0;
451}
452
453/*
454 * Find the first empty cluster
455 */
456static int find_empty_cluster(fsdata *mydata)
457{
458 __u32 fat_val, entry = 3;
459
460 while (1) {
Stefan Brüns9f95d812016-12-17 00:27:51 +0100461 fat_val = get_fatent(mydata, entry);
Donggeun Kim8f814002011-10-24 21:15:28 +0000462 if (fat_val == 0)
463 break;
464 entry++;
465 }
466
467 return entry;
468}
469
470/*
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900471 * Write directory entries in itr's buffer to block device
Donggeun Kim8f814002011-10-24 21:15:28 +0000472 */
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900473static int flush_dir_table(fat_itr *itr)
Donggeun Kim8f814002011-10-24 21:15:28 +0000474{
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900475 fsdata *mydata = itr->fsdata;
Donggeun Kim8f814002011-10-24 21:15:28 +0000476 int dir_newclust = 0;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900477 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
Donggeun Kim8f814002011-10-24 21:15:28 +0000478
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900479 if (set_cluster(mydata, itr->clust, itr->block, bytesperclust) != 0) {
480 printf("error: writing directory entry\n");
481 return -1;
Donggeun Kim8f814002011-10-24 21:15:28 +0000482 }
483 dir_newclust = find_empty_cluster(mydata);
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900484 set_fatent_value(mydata, itr->clust, dir_newclust);
Donggeun Kim8f814002011-10-24 21:15:28 +0000485 if (mydata->fatsize == 32)
486 set_fatent_value(mydata, dir_newclust, 0xffffff8);
487 else if (mydata->fatsize == 16)
488 set_fatent_value(mydata, dir_newclust, 0xfff8);
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500489 else if (mydata->fatsize == 12)
490 set_fatent_value(mydata, dir_newclust, 0xff8);
Donggeun Kim8f814002011-10-24 21:15:28 +0000491
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900492 itr->clust = dir_newclust;
493 itr->next_clust = dir_newclust;
Donggeun Kim8f814002011-10-24 21:15:28 +0000494
Stefan Brüns751b31d2016-09-11 22:51:40 +0200495 if (flush_dirty_fat_buffer(mydata) < 0)
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900496 return -1;
497
498 memset(itr->block, 0x00, bytesperclust);
Donggeun Kim8f814002011-10-24 21:15:28 +0000499
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900500 itr->dent = (dir_entry *)itr->block;
501 itr->last_cluster = 1;
502 itr->remaining = bytesperclust / sizeof(dir_entry) - 1;
Donggeun Kim8f814002011-10-24 21:15:28 +0000503
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900504 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +0000505}
506
507/*
508 * Set empty cluster from 'entry' to the end of a file
509 */
510static int clear_fatent(fsdata *mydata, __u32 entry)
511{
512 __u32 fat_val;
513
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500514 while (!CHECK_CLUST(entry, mydata->fatsize)) {
Stefan Brüns9f95d812016-12-17 00:27:51 +0100515 fat_val = get_fatent(mydata, entry);
Donggeun Kim8f814002011-10-24 21:15:28 +0000516 if (fat_val != 0)
517 set_fatent_value(mydata, entry, 0);
518 else
519 break;
520
Donggeun Kim8f814002011-10-24 21:15:28 +0000521 entry = fat_val;
522 }
523
524 /* Flush fat buffer */
Stefan Brüns751b31d2016-09-11 22:51:40 +0200525 if (flush_dirty_fat_buffer(mydata) < 0)
Donggeun Kim8f814002011-10-24 21:15:28 +0000526 return -1;
527
528 return 0;
529}
530
531/*
532 * Write at most 'maxsize' bytes from 'buffer' into
533 * the file associated with 'dentptr'
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800534 * Update the number of bytes written in *gotsize and return 0
535 * or return -1 on fatal errors.
Donggeun Kim8f814002011-10-24 21:15:28 +0000536 */
537static int
538set_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800539 loff_t maxsize, loff_t *gotsize)
Donggeun Kim8f814002011-10-24 21:15:28 +0000540{
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800541 loff_t filesize = FAT2CPU32(dentptr->size);
Donggeun Kim8f814002011-10-24 21:15:28 +0000542 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
543 __u32 curclust = START(dentptr);
544 __u32 endclust = 0, newclust = 0;
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800545 loff_t actsize;
Donggeun Kim8f814002011-10-24 21:15:28 +0000546
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800547 *gotsize = 0;
548 debug("Filesize: %llu bytes\n", filesize);
Donggeun Kim8f814002011-10-24 21:15:28 +0000549
550 if (maxsize > 0 && filesize > maxsize)
551 filesize = maxsize;
552
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800553 debug("%llu bytes\n", filesize);
Donggeun Kim8f814002011-10-24 21:15:28 +0000554
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +0200555 if (!curclust) {
556 if (filesize) {
557 debug("error: nonempty clusterless file!\n");
558 return -1;
559 }
560 return 0;
561 }
562
Donggeun Kim8f814002011-10-24 21:15:28 +0000563 actsize = bytesperclust;
564 endclust = curclust;
565 do {
566 /* search for consecutive clusters */
567 while (actsize < filesize) {
568 newclust = determine_fatent(mydata, endclust);
569
570 if ((newclust - 1) != endclust)
571 goto getit;
572
573 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Benoît Thébaudeaue0b86942015-09-28 15:45:30 +0200574 debug("newclust: 0x%x\n", newclust);
Donggeun Kim8f814002011-10-24 21:15:28 +0000575 debug("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800576 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +0000577 }
578 endclust = newclust;
579 actsize += bytesperclust;
580 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000581
582 /* set remaining bytes */
Donggeun Kim8f814002011-10-24 21:15:28 +0000583 actsize = filesize;
Benoît Thébaudeau39659ce2015-09-28 15:45:29 +0200584 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Donggeun Kim8f814002011-10-24 21:15:28 +0000585 debug("error: writing cluster\n");
586 return -1;
587 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800588 *gotsize += actsize;
Donggeun Kim8f814002011-10-24 21:15:28 +0000589
590 /* Mark end of file in FAT */
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500591 if (mydata->fatsize == 12)
592 newclust = 0xfff;
593 else if (mydata->fatsize == 16)
Donggeun Kim8f814002011-10-24 21:15:28 +0000594 newclust = 0xffff;
595 else if (mydata->fatsize == 32)
596 newclust = 0xfffffff;
597 set_fatent_value(mydata, endclust, newclust);
598
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800599 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +0000600getit:
601 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
602 debug("error: writing cluster\n");
603 return -1;
604 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800605 *gotsize += actsize;
Donggeun Kim8f814002011-10-24 21:15:28 +0000606 filesize -= actsize;
607 buffer += actsize;
608
Benoît Thébaudeaue0b86942015-09-28 15:45:30 +0200609 if (CHECK_CLUST(newclust, mydata->fatsize)) {
610 debug("newclust: 0x%x\n", newclust);
Donggeun Kim8f814002011-10-24 21:15:28 +0000611 debug("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800612 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +0000613 }
614 actsize = bytesperclust;
615 curclust = endclust = newclust;
616 } while (1);
617}
618
619/*
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +0200620 * Set start cluster in directory entry
Donggeun Kim8f814002011-10-24 21:15:28 +0000621 */
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +0200622static void set_start_cluster(const fsdata *mydata, dir_entry *dentptr,
623 __u32 start_cluster)
Donggeun Kim8f814002011-10-24 21:15:28 +0000624{
625 if (mydata->fatsize == 32)
626 dentptr->starthi =
627 cpu_to_le16((start_cluster & 0xffff0000) >> 16);
628 dentptr->start = cpu_to_le16(start_cluster & 0xffff);
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +0200629}
630
631/*
632 * Fill dir_entry
633 */
634static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
635 const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
636{
637 set_start_cluster(mydata, dentptr, start_cluster);
Donggeun Kim8f814002011-10-24 21:15:28 +0000638 dentptr->size = cpu_to_le32(size);
639
640 dentptr->attr = attr;
641
642 set_name(dentptr, filename);
643}
644
645/*
646 * Check whether adding a file makes the file system to
647 * exceed the size of the block device
648 * Return -1 when overflow occurs, otherwise return 0
649 */
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800650static int check_overflow(fsdata *mydata, __u32 clustnum, loff_t size)
Donggeun Kim8f814002011-10-24 21:15:28 +0000651{
Tom Rinia17b7bc2014-11-24 11:50:46 -0500652 __u32 startsect, sect_num, offset;
Donggeun Kim8f814002011-10-24 21:15:28 +0000653
654 if (clustnum > 0) {
Rob Clarkc3ab0b82017-09-09 13:16:00 -0400655 startsect = clust_to_sect(mydata, clustnum);
Donggeun Kim8f814002011-10-24 21:15:28 +0000656 } else {
657 startsect = mydata->rootdir_sect;
658 }
659
Tom Rinia17b7bc2014-11-24 11:50:46 -0500660 sect_num = div_u64_rem(size, mydata->sect_size, &offset);
661
662 if (offset != 0)
Donggeun Kim8f814002011-10-24 21:15:28 +0000663 sect_num++;
664
Reno Farnesic22435f2017-08-13 15:16:17 -0400665 if (startsect + sect_num > total_sector)
Donggeun Kim8f814002011-10-24 21:15:28 +0000666 return -1;
Donggeun Kim8f814002011-10-24 21:15:28 +0000667 return 0;
668}
669
670/*
Donggeun Kim8f814002011-10-24 21:15:28 +0000671 * Find a directory entry based on filename or start cluster number
672 * If the directory entry is not found,
673 * the new position for writing a directory entry will be returned
674 */
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900675static dir_entry *find_directory_entry(fat_itr *itr, char *filename)
Donggeun Kim8f814002011-10-24 21:15:28 +0000676{
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900677 int match = 0;
Donggeun Kim8f814002011-10-24 21:15:28 +0000678
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900679 while (fat_itr_next(itr)) {
680 /* check both long and short name: */
681 if (!strcasecmp(filename, itr->name))
682 match = 1;
683 else if (itr->name != itr->s_name &&
684 !strcasecmp(filename, itr->s_name))
685 match = 1;
Donggeun Kim8f814002011-10-24 21:15:28 +0000686
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900687 if (!match)
688 continue;
Donggeun Kim8f814002011-10-24 21:15:28 +0000689
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900690 if (itr->dent->name[0] == '\0')
Donggeun Kim8f814002011-10-24 21:15:28 +0000691 return NULL;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900692 else
693 return itr->dent;
694 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000695
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900696 if (!itr->dent && !itr->is_root && flush_dir_table(itr))
697 /* indicate that allocating dent failed */
698 itr->dent = NULL;
Donggeun Kim8f814002011-10-24 21:15:28 +0000699
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900700 return NULL;
701}
Donggeun Kim8f814002011-10-24 21:15:28 +0000702
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900703static int split_filename(char *filename, char **dirname, char **basename)
704{
705 char *p, *last_slash, *last_slash_cont;
Donggeun Kim8f814002011-10-24 21:15:28 +0000706
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900707again:
708 p = filename;
709 last_slash = NULL;
710 last_slash_cont = NULL;
711 while (*p) {
712 if (ISDIRDELIM(*p)) {
713 last_slash = p;
714 last_slash_cont = p;
715 /* continuous slashes */
716 while (ISDIRDELIM(*p))
717 last_slash_cont = p++;
718 if (!*p)
719 break;
Donggeun Kim8f814002011-10-24 21:15:28 +0000720 }
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900721 p++;
722 }
Wu, Josh675b23c2014-05-08 16:14:07 +0800723
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900724 if (last_slash) {
725 if (last_slash_cont == (filename + strlen(filename) - 1)) {
726 /* remove trailing slashes */
727 *last_slash = '\0';
728 goto again;
Wu, Josh675b23c2014-05-08 16:14:07 +0800729 }
730
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900731 if (last_slash == filename) {
732 /* avoid ""(null) directory */
733 *dirname = "/";
734 } else {
735 *last_slash = '\0';
736 *dirname = filename;
Donggeun Kim8f814002011-10-24 21:15:28 +0000737 }
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900738
739 *last_slash_cont = '\0';
740 *basename = last_slash_cont + 1;
741 } else {
742 *dirname = "/"; /* root by default */
743 *basename = filename;
Donggeun Kim8f814002011-10-24 21:15:28 +0000744 }
745
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900746 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +0000747}
748
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +0900749static int normalize_longname(char *l_filename, const char *filename)
750{
751 const char *p, legal[] = "!#$%&\'()-.@^`_{}~";
752 char c;
753 int name_len;
754
755 /* Check that the filename is valid */
756 for (p = filename; p < filename + strlen(filename); p++) {
757 c = *p;
758
759 if (('0' <= c) && (c <= '9'))
760 continue;
761 if (('A' <= c) && (c <= 'Z'))
762 continue;
763 if (('a' <= c) && (c <= 'z'))
764 continue;
765 if (strchr(legal, c))
766 continue;
767 /* extended code */
768 if ((0x80 <= c) && (c <= 0xff))
769 continue;
770
771 return -1;
772 }
773
774 /* Normalize it */
775 name_len = strlen(filename);
776 if (name_len >= VFAT_MAXLEN_BYTES)
777 /* should return an error? */
778 name_len = VFAT_MAXLEN_BYTES - 1;
779
780 memcpy(l_filename, filename, name_len);
781 l_filename[name_len] = 0; /* terminate the string */
782 downcase(l_filename, INT_MAX);
783
784 return 0;
785}
786
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800787static int do_fat_write(const char *filename, void *buffer, loff_t size,
788 loff_t *actwrite)
Donggeun Kim8f814002011-10-24 21:15:28 +0000789{
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900790 dir_entry *retdent;
Donggeun Kim8f814002011-10-24 21:15:28 +0000791 __u32 start_cluster;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900792 fsdata datablock = { .fatbuf = NULL, };
Donggeun Kim8f814002011-10-24 21:15:28 +0000793 fsdata *mydata = &datablock;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900794 fat_itr *itr = NULL;
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +0900795 int ret = -1;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900796 char *filename_copy, *parent, *basename;
Donggeun Kim8f814002011-10-24 21:15:28 +0000797 char l_filename[VFAT_MAXLEN_BYTES];
798
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900799 filename_copy = strdup(filename);
800 if (!filename_copy)
801 return -ENOMEM;
Donggeun Kim8f814002011-10-24 21:15:28 +0000802
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900803 split_filename(filename_copy, &parent, &basename);
804 if (!strlen(basename)) {
805 ret = -EINVAL;
806 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +0000807 }
808
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900809 filename = basename;
810 if (normalize_longname(l_filename, filename)) {
811 printf("FAT: illegal filename (%s)\n", filename);
812 ret = -EINVAL;
813 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +0000814 }
815
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900816 itr = malloc_cache_aligned(sizeof(fat_itr));
817 if (!itr) {
818 ret = -ENOMEM;
819 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +0000820 }
821
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900822 ret = fat_itr_root(itr, &datablock);
823 if (ret)
Donggeun Kim8f814002011-10-24 21:15:28 +0000824 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +0000825
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900826 total_sector = datablock.total_sect;
827
828 ret = fat_itr_resolve(itr, parent, TYPE_DIR);
829 if (ret) {
830 printf("%s: doesn't exist (%d)\n", parent, ret);
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +0900831 goto exit;
832 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000833
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900834 retdent = find_directory_entry(itr, l_filename);
835
Donggeun Kim8f814002011-10-24 21:15:28 +0000836 if (retdent) {
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900837 if (fat_itr_isdir(itr)) {
838 ret = -EISDIR;
839 goto exit;
840 }
841
Donggeun Kim8f814002011-10-24 21:15:28 +0000842 /* Update file size and start_cluster in a directory entry */
843 retdent->size = cpu_to_le32(size);
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +0200844 start_cluster = START(retdent);
Donggeun Kim8f814002011-10-24 21:15:28 +0000845
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +0200846 if (start_cluster) {
847 if (size) {
848 ret = check_overflow(mydata, start_cluster,
849 size);
850 if (ret) {
851 printf("Error: %llu overflow\n", size);
AKASHI Takahiro6aa77692018-09-11 15:59:03 +0900852 ret = -ENOSPC;
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +0200853 goto exit;
854 }
855 }
856
857 ret = clear_fatent(mydata, start_cluster);
858 if (ret) {
859 printf("Error: clearing FAT entries\n");
AKASHI Takahiro6aa77692018-09-11 15:59:03 +0900860 ret = -EIO;
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +0200861 goto exit;
862 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000863
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +0200864 if (!size)
865 set_start_cluster(mydata, retdent, 0);
866 } else if (size) {
867 ret = start_cluster = find_empty_cluster(mydata);
868 if (ret < 0) {
869 printf("Error: finding empty cluster\n");
AKASHI Takahiro6aa77692018-09-11 15:59:03 +0900870 ret = -ENOSPC;
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +0200871 goto exit;
872 }
873
874 ret = check_overflow(mydata, start_cluster, size);
875 if (ret) {
876 printf("Error: %llu overflow\n", size);
AKASHI Takahiro6aa77692018-09-11 15:59:03 +0900877 ret = -ENOSPC;
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +0200878 goto exit;
879 }
880
881 set_start_cluster(mydata, retdent, start_cluster);
Donggeun Kim8f814002011-10-24 21:15:28 +0000882 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000883 } else {
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900884 /* Create a new file */
885
886 if (itr->is_root) {
887 /* root dir cannot have "." or ".." */
888 if (!strcmp(l_filename, ".") ||
889 !strcmp(l_filename, "..")) {
890 ret = -EINVAL;
891 goto exit;
892 }
893 }
894
895 if (!itr->dent) {
896 printf("Error: allocating new dir entry\n");
897 ret = -EIO;
898 goto exit;
899 }
900
901 memset(itr->dent, 0, sizeof(*itr->dent));
902
Donggeun Kim8f814002011-10-24 21:15:28 +0000903 /* Set short name to set alias checksum field in dir_slot */
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900904 set_name(itr->dent, filename);
905 if (fill_dir_slot(itr, filename)) {
906 ret = -EIO;
907 goto exit;
908 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000909
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +0200910 if (size) {
911 ret = start_cluster = find_empty_cluster(mydata);
912 if (ret < 0) {
913 printf("Error: finding empty cluster\n");
AKASHI Takahiro6aa77692018-09-11 15:59:03 +0900914 ret = -ENOSPC;
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +0200915 goto exit;
916 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000917
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +0200918 ret = check_overflow(mydata, start_cluster, size);
919 if (ret) {
920 printf("Error: %llu overflow\n", size);
AKASHI Takahiro6aa77692018-09-11 15:59:03 +0900921 ret = -ENOSPC;
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +0200922 goto exit;
923 }
924 } else {
925 start_cluster = 0;
Donggeun Kim8f814002011-10-24 21:15:28 +0000926 }
927
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900928 /* Set attribute as archive for regular file */
929 fill_dentry(itr->fsdata, itr->dent, filename,
930 start_cluster, size, 0x20);
Donggeun Kim8f814002011-10-24 21:15:28 +0000931
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900932 retdent = itr->dent;
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +0200933 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000934
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +0200935 ret = set_contents(mydata, retdent, buffer, size, actwrite);
936 if (ret < 0) {
937 printf("Error: writing contents\n");
AKASHI Takahiro6aa77692018-09-11 15:59:03 +0900938 ret = -EIO;
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +0200939 goto exit;
940 }
941 debug("attempt to write 0x%llx bytes\n", *actwrite);
Donggeun Kim8f814002011-10-24 21:15:28 +0000942
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +0200943 /* Flush fat buffer */
Stefan Brüns751b31d2016-09-11 22:51:40 +0200944 ret = flush_dirty_fat_buffer(mydata);
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +0200945 if (ret) {
946 printf("Error: flush fat buffer\n");
AKASHI Takahiro6aa77692018-09-11 15:59:03 +0900947 ret = -EIO;
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +0200948 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +0000949 }
950
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +0200951 /* Write directory table to device */
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900952 ret = set_cluster(mydata, itr->clust, itr->block,
953 mydata->clust_size * mydata->sect_size);
AKASHI Takahiro6aa77692018-09-11 15:59:03 +0900954 if (ret) {
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +0200955 printf("Error: writing directory entry\n");
AKASHI Takahiro6aa77692018-09-11 15:59:03 +0900956 ret = -EIO;
957 }
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +0200958
Donggeun Kim8f814002011-10-24 21:15:28 +0000959exit:
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900960 free(filename_copy);
Donggeun Kim8f814002011-10-24 21:15:28 +0000961 free(mydata->fatbuf);
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900962 free(itr);
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800963 return ret;
Donggeun Kim8f814002011-10-24 21:15:28 +0000964}
965
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800966int file_fat_write(const char *filename, void *buffer, loff_t offset,
967 loff_t maxsize, loff_t *actwrite)
Donggeun Kim8f814002011-10-24 21:15:28 +0000968{
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800969 if (offset != 0) {
Vagrant Cascadiane1643ed2016-03-15 12:11:13 -0700970 printf("Error: non zero offset is currently not supported.\n");
AKASHI Takahiro6aa77692018-09-11 15:59:03 +0900971 return -EINVAL;
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800972 }
973
Donggeun Kim8f814002011-10-24 21:15:28 +0000974 printf("writing %s\n", filename);
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800975 return do_fat_write(filename, buffer, maxsize, actwrite);
Donggeun Kim8f814002011-10-24 21:15:28 +0000976}