blob: 1e4f5af9106dd82ddee2785ccabead0ad24a3727 [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>
12#include <asm/byteorder.h>
13#include <part.h>
Richard Genoudfb7e16c2012-12-13 00:47:36 +000014#include <linux/ctype.h>
Tom Rini9e374e72014-11-24 11:50:46 -050015#include <div64.h>
16#include <linux/math64.h>
Donggeun Kimc30a15e2011-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 Genoudfb7e16c2012-12-13 00:47:36 +000024 *str = toupper(*str);
Donggeun Kimc30a15e2011-10-24 21:15:28 +000025 str++;
26 }
27}
28
29static int total_sector;
Donggeun Kim079df722012-03-22 04:38:55 +000030static int disk_write(__u32 block, __u32 nr_blocks, void *buf)
Donggeun Kimc30a15e2011-10-24 21:15:28 +000031{
Łukasz Majewski0a04ed82015-09-03 14:21:39 +020032 ulong ret;
33
Simon Glass2a981dc2016-02-29 15:25:52 -070034 if (!cur_dev)
Donggeun Kimc30a15e2011-10-24 21:15:28 +000035 return -1;
36
Donggeun Kim079df722012-03-22 04:38:55 +000037 if (cur_part_info.start + block + nr_blocks >
38 cur_part_info.start + total_sector) {
Donggeun Kimc30a15e2011-10-24 21:15:28 +000039 printf("error: overflow occurs\n");
40 return -1;
41 }
42
Simon Glass2a981dc2016-02-29 15:25:52 -070043 ret = blk_dwrite(cur_dev, cur_part_info.start + block, nr_blocks, buf);
Łukasz Majewski0a04ed82015-09-03 14:21:39 +020044 if (nr_blocks && ret == 0)
45 return -1;
46
47 return ret;
Donggeun Kimc30a15e2011-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 Wilczek73dc8322013-10-11 15:43:33 +020066 strcpy(s_name, filename);
Donggeun Kimc30a15e2011-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
Donggeun Kim627182e2011-12-20 18:34:27 +0000102static __u8 num_of_fats;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000103/*
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
Donggeun Kim627182e2011-12-20 18:34:27 +0000131 if (num_of_fats == 2) {
132 /* 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
213static int is_next_clust(fsdata *mydata, dir_entry *dentptr);
214static void flush_dir_table(fsdata *mydata, dir_entry **dentptr);
215
216/*
217 * Fill dir_slot entries with appropriate name, id, and attr
218 * The real directory entry is returned by 'dentptr'
219 */
220static void
221fill_dir_slot(fsdata *mydata, dir_entry **dentptr, const char *l_name)
222{
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 */
Marek Vasutff04f6d2012-10-09 07:20:22 +0000229 checksum = mkcksum((*dentptr)->name, (*dentptr)->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) {
244 if (is_next_clust(mydata, *dentptr)) {
245 /* A new cluster is allocated for directory table */
246 flush_dir_table(mydata, dentptr);
247 }
248 memcpy(*dentptr, slotptr, sizeof(dir_slot));
249 (*dentptr)++;
250 slotptr--;
251 counter--;
252 }
253
254 if (is_next_clust(mydata, *dentptr)) {
255 /* A new cluster is allocated for directory table */
256 flush_dir_table(mydata, dentptr);
257 }
258}
259
260static __u32 dir_curclust;
261
262/*
263 * Extract the full long filename starting at 'retdent' (which is really
264 * a slot) into 'l_name'. If successful also copy the real directory entry
265 * into 'retdent'
266 * If additional adjacent cluster for directory entries is read into memory,
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000267 * then 'get_contents_vfatname_block' is copied into 'get_dentfromdir_block' and
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000268 * the location of the real directory entry is returned by 'retdent'
269 * Return 0 on success, -1 otherwise.
270 */
271static int
272get_long_file_name(fsdata *mydata, int curclust, __u8 *cluster,
273 dir_entry **retdent, char *l_name)
274{
275 dir_entry *realdent;
276 dir_slot *slotptr = (dir_slot *)(*retdent);
277 dir_slot *slotptr2 = NULL;
278 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
279 PREFETCH_BLOCKS :
280 mydata->clust_size);
281 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
282 int idx = 0, cur_position = 0;
283
284 if (counter > VFAT_MAXSEQ) {
285 debug("Error: VFAT name is too long\n");
286 return -1;
287 }
288
289 while ((__u8 *)slotptr < buflimit) {
290 if (counter == 0)
291 break;
292 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
293 return -1;
294 slotptr++;
295 counter--;
296 }
297
298 if ((__u8 *)slotptr >= buflimit) {
299 if (curclust == 0)
300 return -1;
Stefan Brünsb8948d22016-12-17 00:27:51 +0100301 curclust = get_fatent(mydata, dir_curclust);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000302 if (CHECK_CLUST(curclust, mydata->fatsize)) {
303 debug("curclust: 0x%x\n", curclust);
304 printf("Invalid FAT entry\n");
305 return -1;
306 }
307
308 dir_curclust = curclust;
309
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000310 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000311 mydata->clust_size * mydata->sect_size) != 0) {
312 debug("Error: reading directory block\n");
313 return -1;
314 }
315
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000316 slotptr2 = (dir_slot *)get_contents_vfatname_block;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000317 while (counter > 0) {
318 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
319 & 0xff) != counter)
320 return -1;
321 slotptr2++;
322 counter--;
323 }
324
325 /* Save the real directory entry */
326 realdent = (dir_entry *)slotptr2;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000327 while ((__u8 *)slotptr2 > get_contents_vfatname_block) {
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000328 slotptr2--;
329 slot2str(slotptr2, l_name, &idx);
330 }
331 } else {
332 /* Save the real directory entry */
333 realdent = (dir_entry *)slotptr;
334 }
335
336 do {
337 slotptr--;
338 if (slot2str(slotptr, l_name, &idx))
339 break;
340 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
341
342 l_name[idx] = '\0';
343 if (*l_name == DELETED_FLAG)
344 *l_name = '\0';
345 else if (*l_name == aRING)
346 *l_name = DELETED_FLAG;
Rob Clark21a24c32017-09-09 13:15:59 -0400347 downcase(l_name, INT_MAX);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000348
349 /* Return the real directory entry */
350 *retdent = realdent;
351
352 if (slotptr2) {
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000353 memcpy(get_dentfromdir_block, get_contents_vfatname_block,
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000354 mydata->clust_size * mydata->sect_size);
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000355 cur_position = (__u8 *)realdent - get_contents_vfatname_block;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000356 *retdent = (dir_entry *) &get_dentfromdir_block[cur_position];
357 }
358
359 return 0;
360}
361
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000362/*
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500363 * Set the entry at index 'entry' in a FAT (12/16/32) table.
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000364 */
365static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
366{
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500367 __u32 bufnum, offset, off16;
368 __u16 val1, val2;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000369
370 switch (mydata->fatsize) {
371 case 32:
372 bufnum = entry / FAT32BUFSIZE;
373 offset = entry - bufnum * FAT32BUFSIZE;
374 break;
375 case 16:
376 bufnum = entry / FAT16BUFSIZE;
377 offset = entry - bufnum * FAT16BUFSIZE;
378 break;
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500379 case 12:
380 bufnum = entry / FAT12BUFSIZE;
381 offset = entry - bufnum * FAT12BUFSIZE;
382 break;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000383 default:
384 /* Unsupported FAT size */
385 return -1;
386 }
387
388 /* Read a new block of FAT entries into the cache. */
389 if (bufnum != mydata->fatbufnum) {
390 int getsize = FATBUFBLOCKS;
391 __u8 *bufptr = mydata->fatbuf;
392 __u32 fatlength = mydata->fatlength;
393 __u32 startblock = bufnum * FATBUFBLOCKS;
394
Stefan Brüns6c1a8082016-12-17 00:27:50 +0100395 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
396 if (startblock + getsize > fatlength)
397 getsize = fatlength - startblock;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000398
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200399 if (flush_dirty_fat_buffer(mydata) < 0)
400 return -1;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000401
Stefan Brüns6c1a8082016-12-17 00:27:50 +0100402 startblock += mydata->fat_sect;
403
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000404 if (disk_read(startblock, getsize, bufptr) < 0) {
405 debug("Error reading FAT blocks\n");
406 return -1;
407 }
408 mydata->fatbufnum = bufnum;
409 }
410
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200411 /* Mark as dirty */
412 mydata->fat_dirty = 1;
413
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000414 /* Set the actual entry */
415 switch (mydata->fatsize) {
416 case 32:
417 ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
418 break;
419 case 16:
420 ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
421 break;
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500422 case 12:
423 off16 = (offset * 3) / 4;
424
425 switch (offset & 0x3) {
426 case 0:
427 val1 = cpu_to_le16(entry_value) & 0xfff;
428 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff;
429 ((__u16 *)mydata->fatbuf)[off16] |= val1;
430 break;
431 case 1:
432 val1 = cpu_to_le16(entry_value) & 0xf;
433 val2 = (cpu_to_le16(entry_value) >> 4) & 0xff;
434
435 ((__u16 *)mydata->fatbuf)[off16] &= ~0xf000;
436 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 12);
437
438 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xff;
439 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
440 break;
441 case 2:
442 val1 = cpu_to_le16(entry_value) & 0xff;
443 val2 = (cpu_to_le16(entry_value) >> 8) & 0xf;
444
445 ((__u16 *)mydata->fatbuf)[off16] &= ~0xff00;
446 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 8);
447
448 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xf;
449 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
450 break;
451 case 3:
452 val1 = cpu_to_le16(entry_value) & 0xfff;
453 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff0;
454 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 4);
455 break;
456 default:
457 break;
458 }
459
460 break;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000461 default:
462 return -1;
463 }
464
465 return 0;
466}
467
468/*
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500469 * Determine the next free cluster after 'entry' in a FAT (12/16/32) table
Stefan Brünsae1755b2016-09-11 22:51:41 +0200470 * and link it to 'entry'. EOC marker is not set on returned entry.
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000471 */
472static __u32 determine_fatent(fsdata *mydata, __u32 entry)
473{
474 __u32 next_fat, next_entry = entry + 1;
475
476 while (1) {
Stefan Brünsb8948d22016-12-17 00:27:51 +0100477 next_fat = get_fatent(mydata, next_entry);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000478 if (next_fat == 0) {
Stefan Brünsae1755b2016-09-11 22:51:41 +0200479 /* found free entry, link to entry */
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000480 set_fatent_value(mydata, entry, next_entry);
481 break;
482 }
483 next_entry++;
484 }
485 debug("FAT%d: entry: %08x, entry_value: %04x\n",
486 mydata->fatsize, entry, next_entry);
487
488 return next_entry;
489}
490
491/*
492 * Write at most 'size' bytes from 'buffer' into the specified cluster.
493 * Return 0 on success, -1 otherwise.
494 */
495static int
496set_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer,
497 unsigned long size)
498{
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200499 __u32 idx = 0;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000500 __u32 startsect;
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200501 int ret;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000502
503 if (clustnum > 0)
Rob Clark265edc02017-09-09 13:16:00 -0400504 startsect = clust_to_sect(mydata, clustnum);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000505 else
506 startsect = mydata->rootdir_sect;
507
508 debug("clustnum: %d, startsect: %d\n", clustnum, startsect);
509
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200510 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
511 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
512
513 printf("FAT: Misaligned buffer address (%p)\n", buffer);
514
515 while (size >= mydata->sect_size) {
516 memcpy(tmpbuf, buffer, mydata->sect_size);
517 ret = disk_write(startsect++, 1, tmpbuf);
518 if (ret != 1) {
519 debug("Error writing data (got %d)\n", ret);
520 return -1;
521 }
522
523 buffer += mydata->sect_size;
524 size -= mydata->sect_size;
525 }
526 } else if (size >= mydata->sect_size) {
527 idx = size / mydata->sect_size;
528 ret = disk_write(startsect, idx, buffer);
529 if (ret != idx) {
530 debug("Error writing data (got %d)\n", ret);
Wu, Josh6b8f1852013-07-24 17:55:30 +0800531 return -1;
532 }
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200533
534 startsect += idx;
535 idx *= mydata->sect_size;
536 buffer += idx;
537 size -= idx;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000538 }
539
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200540 if (size) {
541 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000542
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200543 memcpy(tmpbuf, buffer, size);
544 ret = disk_write(startsect, 1, tmpbuf);
545 if (ret != 1) {
546 debug("Error writing data (got %d)\n", ret);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000547 return -1;
548 }
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000549 }
550
551 return 0;
552}
553
554/*
555 * Find the first empty cluster
556 */
557static int find_empty_cluster(fsdata *mydata)
558{
559 __u32 fat_val, entry = 3;
560
561 while (1) {
Stefan Brünsb8948d22016-12-17 00:27:51 +0100562 fat_val = get_fatent(mydata, entry);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000563 if (fat_val == 0)
564 break;
565 entry++;
566 }
567
568 return entry;
569}
570
571/*
572 * Write directory entries in 'get_dentfromdir_block' to block device
573 */
574static void flush_dir_table(fsdata *mydata, dir_entry **dentptr)
575{
576 int dir_newclust = 0;
577
578 if (set_cluster(mydata, dir_curclust,
579 get_dentfromdir_block,
580 mydata->clust_size * mydata->sect_size) != 0) {
581 printf("error: wrinting directory entry\n");
582 return;
583 }
584 dir_newclust = find_empty_cluster(mydata);
585 set_fatent_value(mydata, dir_curclust, dir_newclust);
586 if (mydata->fatsize == 32)
587 set_fatent_value(mydata, dir_newclust, 0xffffff8);
588 else if (mydata->fatsize == 16)
589 set_fatent_value(mydata, dir_newclust, 0xfff8);
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500590 else if (mydata->fatsize == 12)
591 set_fatent_value(mydata, dir_newclust, 0xff8);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000592
593 dir_curclust = dir_newclust;
594
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200595 if (flush_dirty_fat_buffer(mydata) < 0)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000596 return;
597
598 memset(get_dentfromdir_block, 0x00,
599 mydata->clust_size * mydata->sect_size);
600
601 *dentptr = (dir_entry *) get_dentfromdir_block;
602}
603
604/*
605 * Set empty cluster from 'entry' to the end of a file
606 */
607static int clear_fatent(fsdata *mydata, __u32 entry)
608{
609 __u32 fat_val;
610
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500611 while (!CHECK_CLUST(entry, mydata->fatsize)) {
Stefan Brünsb8948d22016-12-17 00:27:51 +0100612 fat_val = get_fatent(mydata, entry);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000613 if (fat_val != 0)
614 set_fatent_value(mydata, entry, 0);
615 else
616 break;
617
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000618 entry = fat_val;
619 }
620
621 /* Flush fat buffer */
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200622 if (flush_dirty_fat_buffer(mydata) < 0)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000623 return -1;
624
625 return 0;
626}
627
628/*
629 * Write at most 'maxsize' bytes from 'buffer' into
630 * the file associated with 'dentptr'
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800631 * Update the number of bytes written in *gotsize and return 0
632 * or return -1 on fatal errors.
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000633 */
634static int
635set_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800636 loff_t maxsize, loff_t *gotsize)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000637{
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800638 loff_t filesize = FAT2CPU32(dentptr->size);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000639 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
640 __u32 curclust = START(dentptr);
641 __u32 endclust = 0, newclust = 0;
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800642 loff_t actsize;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000643
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800644 *gotsize = 0;
645 debug("Filesize: %llu bytes\n", filesize);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000646
647 if (maxsize > 0 && filesize > maxsize)
648 filesize = maxsize;
649
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800650 debug("%llu bytes\n", filesize);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000651
Benoît Thébaudeau1254b442015-09-28 15:45:32 +0200652 if (!curclust) {
653 if (filesize) {
654 debug("error: nonempty clusterless file!\n");
655 return -1;
656 }
657 return 0;
658 }
659
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000660 actsize = bytesperclust;
661 endclust = curclust;
662 do {
663 /* search for consecutive clusters */
664 while (actsize < filesize) {
665 newclust = determine_fatent(mydata, endclust);
666
667 if ((newclust - 1) != endclust)
668 goto getit;
669
670 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Benoît Thébaudeau5e1a8602015-09-28 15:45:30 +0200671 debug("newclust: 0x%x\n", newclust);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000672 debug("Invalid FAT entry\n");
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800673 return 0;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000674 }
675 endclust = newclust;
676 actsize += bytesperclust;
677 }
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000678
679 /* set remaining bytes */
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000680 actsize = filesize;
Benoît Thébaudeau1d7f2ec2015-09-28 15:45:29 +0200681 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000682 debug("error: writing cluster\n");
683 return -1;
684 }
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800685 *gotsize += actsize;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000686
687 /* Mark end of file in FAT */
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500688 if (mydata->fatsize == 12)
689 newclust = 0xfff;
690 else if (mydata->fatsize == 16)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000691 newclust = 0xffff;
692 else if (mydata->fatsize == 32)
693 newclust = 0xfffffff;
694 set_fatent_value(mydata, endclust, newclust);
695
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800696 return 0;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000697getit:
698 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
699 debug("error: writing cluster\n");
700 return -1;
701 }
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800702 *gotsize += actsize;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000703 filesize -= actsize;
704 buffer += actsize;
705
Benoît Thébaudeau5e1a8602015-09-28 15:45:30 +0200706 if (CHECK_CLUST(newclust, mydata->fatsize)) {
707 debug("newclust: 0x%x\n", newclust);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000708 debug("Invalid FAT entry\n");
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800709 return 0;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000710 }
711 actsize = bytesperclust;
712 curclust = endclust = newclust;
713 } while (1);
714}
715
716/*
Benoît Thébaudeau1254b442015-09-28 15:45:32 +0200717 * Set start cluster in directory entry
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000718 */
Benoît Thébaudeau1254b442015-09-28 15:45:32 +0200719static void set_start_cluster(const fsdata *mydata, dir_entry *dentptr,
720 __u32 start_cluster)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000721{
722 if (mydata->fatsize == 32)
723 dentptr->starthi =
724 cpu_to_le16((start_cluster & 0xffff0000) >> 16);
725 dentptr->start = cpu_to_le16(start_cluster & 0xffff);
Benoît Thébaudeau1254b442015-09-28 15:45:32 +0200726}
727
728/*
729 * Fill dir_entry
730 */
731static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
732 const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
733{
734 set_start_cluster(mydata, dentptr, start_cluster);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000735 dentptr->size = cpu_to_le32(size);
736
737 dentptr->attr = attr;
738
739 set_name(dentptr, filename);
740}
741
742/*
743 * Check whether adding a file makes the file system to
744 * exceed the size of the block device
745 * Return -1 when overflow occurs, otherwise return 0
746 */
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800747static int check_overflow(fsdata *mydata, __u32 clustnum, loff_t size)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000748{
Tom Rini9e374e72014-11-24 11:50:46 -0500749 __u32 startsect, sect_num, offset;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000750
751 if (clustnum > 0) {
Rob Clark265edc02017-09-09 13:16:00 -0400752 startsect = clust_to_sect(mydata, clustnum);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000753 } else {
754 startsect = mydata->rootdir_sect;
755 }
756
Tom Rini9e374e72014-11-24 11:50:46 -0500757 sect_num = div_u64_rem(size, mydata->sect_size, &offset);
758
759 if (offset != 0)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000760 sect_num++;
761
Reno Farnesi76216212017-08-13 15:16:17 -0400762 if (startsect + sect_num > total_sector)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000763 return -1;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000764 return 0;
765}
766
767/*
768 * Check if adding several entries exceed one cluster boundary
769 */
770static int is_next_clust(fsdata *mydata, dir_entry *dentptr)
771{
772 int cur_position;
773
774 cur_position = (__u8 *)dentptr - get_dentfromdir_block;
775
776 if (cur_position >= mydata->clust_size * mydata->sect_size)
777 return 1;
778 else
779 return 0;
780}
781
782static dir_entry *empty_dentptr;
783/*
784 * Find a directory entry based on filename or start cluster number
785 * If the directory entry is not found,
786 * the new position for writing a directory entry will be returned
787 */
788static dir_entry *find_directory_entry(fsdata *mydata, int startsect,
789 char *filename, dir_entry *retdent, __u32 start)
790{
Rob Clark265edc02017-09-09 13:16:00 -0400791 __u32 curclust = sect_to_clust(mydata, startsect);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000792
793 debug("get_dentfromdir: %s\n", filename);
794
795 while (1) {
796 dir_entry *dentptr;
797
798 int i;
799
800 if (get_cluster(mydata, curclust, get_dentfromdir_block,
801 mydata->clust_size * mydata->sect_size) != 0) {
802 printf("Error: reading directory block\n");
803 return NULL;
804 }
805
806 dentptr = (dir_entry *)get_dentfromdir_block;
807
808 dir_curclust = curclust;
809
810 for (i = 0; i < DIRENTSPERCLUST; i++) {
811 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
812
813 l_name[0] = '\0';
814 if (dentptr->name[0] == DELETED_FLAG) {
815 dentptr++;
816 if (is_next_clust(mydata, dentptr))
817 break;
818 continue;
819 }
820 if ((dentptr->attr & ATTR_VOLUME)) {
Tuomas Tynkkynen8bad6cb2018-01-05 02:45:21 +0200821 if ((dentptr->attr & ATTR_VFAT) &&
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000822 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000823 get_long_file_name(mydata, curclust,
824 get_dentfromdir_block,
825 &dentptr, l_name);
826 debug("vfatname: |%s|\n", l_name);
Richard Genoudcb940c72012-12-13 03:30:10 +0000827 } else {
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000828 /* Volume label or VFAT entry */
829 dentptr++;
830 if (is_next_clust(mydata, dentptr))
831 break;
832 continue;
833 }
834 }
835 if (dentptr->name[0] == 0) {
836 debug("Dentname == NULL - %d\n", i);
837 empty_dentptr = dentptr;
838 return NULL;
839 }
840
841 get_name(dentptr, s_name);
842
Jean-Jacques Hiblot2c33b0c2017-12-21 12:49:47 +0100843 if (strncasecmp(filename, s_name, sizeof(s_name)) &&
844 strncasecmp(filename, l_name, sizeof(l_name))) {
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000845 debug("Mismatch: |%s|%s|\n",
846 s_name, l_name);
847 dentptr++;
848 if (is_next_clust(mydata, dentptr))
849 break;
850 continue;
851 }
852
853 memcpy(retdent, dentptr, sizeof(dir_entry));
854
855 debug("DentName: %s", s_name);
856 debug(", start: 0x%x", START(dentptr));
857 debug(", size: 0x%x %s\n",
858 FAT2CPU32(dentptr->size),
859 (dentptr->attr & ATTR_DIR) ?
860 "(DIR)" : "");
861
862 return dentptr;
863 }
864
Wu, Joshdd6d7962014-05-08 16:14:07 +0800865 /*
866 * In FAT16/12, the root dir is locate before data area, shows
867 * in following:
868 * -------------------------------------------------------------
869 * | Boot | FAT1 & 2 | Root dir | Data (start from cluster #2) |
870 * -------------------------------------------------------------
871 *
872 * As a result if curclust is in Root dir, it is a negative
873 * number or 0, 1.
874 *
875 */
876 if (mydata->fatsize != 32 && (int)curclust <= 1) {
877 /* Current clust is in root dir, set to next clust */
878 curclust++;
879 if ((int)curclust <= 1)
880 continue; /* continue to find */
881
882 /* Reach the end of root dir */
883 empty_dentptr = dentptr;
884 return NULL;
885 }
886
Stefan Brünsb8948d22016-12-17 00:27:51 +0100887 curclust = get_fatent(mydata, dir_curclust);
Wu, Josh2e98f702014-05-08 16:14:06 +0800888 if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000889 empty_dentptr = dentptr;
890 return NULL;
891 }
892 if (CHECK_CLUST(curclust, mydata->fatsize)) {
893 debug("curclust: 0x%x\n", curclust);
894 debug("Invalid FAT entry\n");
895 return NULL;
896 }
897 }
898
899 return NULL;
900}
901
AKASHI Takahiro25bb9da2018-09-11 15:59:02 +0900902static int normalize_longname(char *l_filename, const char *filename)
903{
904 const char *p, legal[] = "!#$%&\'()-.@^`_{}~";
905 char c;
906 int name_len;
907
908 /* Check that the filename is valid */
909 for (p = filename; p < filename + strlen(filename); p++) {
910 c = *p;
911
912 if (('0' <= c) && (c <= '9'))
913 continue;
914 if (('A' <= c) && (c <= 'Z'))
915 continue;
916 if (('a' <= c) && (c <= 'z'))
917 continue;
918 if (strchr(legal, c))
919 continue;
920 /* extended code */
921 if ((0x80 <= c) && (c <= 0xff))
922 continue;
923
924 return -1;
925 }
926
927 /* Normalize it */
928 name_len = strlen(filename);
929 if (name_len >= VFAT_MAXLEN_BYTES)
930 /* should return an error? */
931 name_len = VFAT_MAXLEN_BYTES - 1;
932
933 memcpy(l_filename, filename, name_len);
934 l_filename[name_len] = 0; /* terminate the string */
935 downcase(l_filename, INT_MAX);
936
937 return 0;
938}
939
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800940static int do_fat_write(const char *filename, void *buffer, loff_t size,
941 loff_t *actwrite)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000942{
943 dir_entry *dentptr, *retdent;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000944 __u32 startsect;
945 __u32 start_cluster;
946 boot_sector bs;
947 volume_info volinfo;
948 fsdata datablock;
949 fsdata *mydata = &datablock;
AKASHI Takahiro881042e2018-09-11 15:59:01 +0900950 int cursect;
AKASHI Takahiro25bb9da2018-09-11 15:59:02 +0900951 int ret = -1;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000952 char l_filename[VFAT_MAXLEN_BYTES];
953
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800954 *actwrite = size;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000955 dir_curclust = 0;
956
957 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
958 debug("error: reading boot sector\n");
AKASHI Takahirof1149ce2018-09-11 15:59:03 +0900959 return -EIO;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000960 }
961
962 total_sector = bs.total_sect;
963 if (total_sector == 0)
Steve Raee04350d2014-05-26 11:52:23 -0700964 total_sector = (int)cur_part_info.size; /* cast of lbaint_t */
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000965
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000966 if (mydata->fatsize == 32)
967 mydata->fatlength = bs.fat32_length;
968 else
969 mydata->fatlength = bs.fat_length;
970
971 mydata->fat_sect = bs.reserved;
972
973 cursect = mydata->rootdir_sect
974 = mydata->fat_sect + mydata->fatlength * bs.fats;
Donggeun Kim627182e2011-12-20 18:34:27 +0000975 num_of_fats = bs.fats;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000976
977 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
978 mydata->clust_size = bs.cluster_size;
979
980 if (mydata->fatsize == 32) {
981 mydata->data_begin = mydata->rootdir_sect -
982 (mydata->clust_size * 2);
983 } else {
984 int rootdir_size;
985
986 rootdir_size = ((bs.dir_entries[1] * (int)256 +
987 bs.dir_entries[0]) *
988 sizeof(dir_entry)) /
989 mydata->sect_size;
990 mydata->data_begin = mydata->rootdir_sect +
991 rootdir_size -
992 (mydata->clust_size * 2);
993 }
994
995 mydata->fatbufnum = -1;
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200996 mydata->fat_dirty = 0;
Nobuhiro Iwamatsu8abd0532014-04-08 11:12:46 +0900997 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000998 if (mydata->fatbuf == NULL) {
999 debug("Error: allocating memory\n");
AKASHI Takahirof1149ce2018-09-11 15:59:03 +09001000 return -ENOMEM;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001001 }
1002
1003 if (disk_read(cursect,
1004 (mydata->fatsize == 32) ?
1005 (mydata->clust_size) :
Benoît Thébaudeau1170e632012-09-18 08:14:56 +00001006 PREFETCH_BLOCKS, do_fat_read_at_block) < 0) {
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001007 debug("Error: reading rootdir block\n");
AKASHI Takahirof1149ce2018-09-11 15:59:03 +09001008 ret = -EIO;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001009 goto exit;
1010 }
Benoît Thébaudeau1170e632012-09-18 08:14:56 +00001011 dentptr = (dir_entry *) do_fat_read_at_block;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001012
AKASHI Takahiro25bb9da2018-09-11 15:59:02 +09001013 if (normalize_longname(l_filename, filename)) {
1014 printf("FAT: illegal filename (%s)\n", filename);
1015 ret = -EINVAL;
1016 goto exit;
1017 }
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001018
1019 startsect = mydata->rootdir_sect;
1020 retdent = find_directory_entry(mydata, startsect,
1021 l_filename, dentptr, 0);
1022 if (retdent) {
1023 /* Update file size and start_cluster in a directory entry */
1024 retdent->size = cpu_to_le32(size);
Benoît Thébaudeaue876be42015-09-28 15:45:31 +02001025 start_cluster = START(retdent);
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001026
Benoît Thébaudeau1254b442015-09-28 15:45:32 +02001027 if (start_cluster) {
1028 if (size) {
1029 ret = check_overflow(mydata, start_cluster,
1030 size);
1031 if (ret) {
1032 printf("Error: %llu overflow\n", size);
AKASHI Takahirof1149ce2018-09-11 15:59:03 +09001033 ret = -ENOSPC;
Benoît Thébaudeau1254b442015-09-28 15:45:32 +02001034 goto exit;
1035 }
1036 }
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001037
Benoît Thébaudeau1254b442015-09-28 15:45:32 +02001038 ret = clear_fatent(mydata, start_cluster);
1039 if (ret) {
1040 printf("Error: clearing FAT entries\n");
AKASHI Takahirof1149ce2018-09-11 15:59:03 +09001041 ret = -EIO;
Benoît Thébaudeau1254b442015-09-28 15:45:32 +02001042 goto exit;
1043 }
1044
1045 if (!size)
1046 set_start_cluster(mydata, retdent, 0);
1047 } else if (size) {
1048 ret = start_cluster = find_empty_cluster(mydata);
1049 if (ret < 0) {
1050 printf("Error: finding empty cluster\n");
AKASHI Takahirof1149ce2018-09-11 15:59:03 +09001051 ret = -ENOSPC;
Benoît Thébaudeau1254b442015-09-28 15:45:32 +02001052 goto exit;
1053 }
1054
1055 ret = check_overflow(mydata, start_cluster, size);
1056 if (ret) {
1057 printf("Error: %llu overflow\n", size);
AKASHI Takahirof1149ce2018-09-11 15:59:03 +09001058 ret = -ENOSPC;
Benoît Thébaudeau1254b442015-09-28 15:45:32 +02001059 goto exit;
1060 }
1061
1062 set_start_cluster(mydata, retdent, start_cluster);
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001063 }
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001064 } else {
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001065 /* Set short name to set alias checksum field in dir_slot */
1066 set_name(empty_dentptr, filename);
1067 fill_dir_slot(mydata, &empty_dentptr, filename);
1068
Benoît Thébaudeau1254b442015-09-28 15:45:32 +02001069 if (size) {
1070 ret = start_cluster = find_empty_cluster(mydata);
1071 if (ret < 0) {
1072 printf("Error: finding empty cluster\n");
AKASHI Takahirof1149ce2018-09-11 15:59:03 +09001073 ret = -ENOSPC;
Benoît Thébaudeau1254b442015-09-28 15:45:32 +02001074 goto exit;
1075 }
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001076
Benoît Thébaudeau1254b442015-09-28 15:45:32 +02001077 ret = check_overflow(mydata, start_cluster, size);
1078 if (ret) {
1079 printf("Error: %llu overflow\n", size);
AKASHI Takahirof1149ce2018-09-11 15:59:03 +09001080 ret = -ENOSPC;
Benoît Thébaudeau1254b442015-09-28 15:45:32 +02001081 goto exit;
1082 }
1083 } else {
1084 start_cluster = 0;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001085 }
1086
1087 /* Set attribute as archieve for regular file */
1088 fill_dentry(mydata, empty_dentptr, filename,
1089 start_cluster, size, 0x20);
1090
Benoît Thébaudeaue876be42015-09-28 15:45:31 +02001091 retdent = empty_dentptr;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001092 }
1093
Benoît Thébaudeaue876be42015-09-28 15:45:31 +02001094 ret = set_contents(mydata, retdent, buffer, size, actwrite);
1095 if (ret < 0) {
1096 printf("Error: writing contents\n");
AKASHI Takahirof1149ce2018-09-11 15:59:03 +09001097 ret = -EIO;
Benoît Thébaudeaue876be42015-09-28 15:45:31 +02001098 goto exit;
1099 }
1100 debug("attempt to write 0x%llx bytes\n", *actwrite);
1101
1102 /* Flush fat buffer */
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +02001103 ret = flush_dirty_fat_buffer(mydata);
Benoît Thébaudeaue876be42015-09-28 15:45:31 +02001104 if (ret) {
1105 printf("Error: flush fat buffer\n");
AKASHI Takahirof1149ce2018-09-11 15:59:03 +09001106 ret = -EIO;
Benoît Thébaudeaue876be42015-09-28 15:45:31 +02001107 goto exit;
1108 }
1109
1110 /* Write directory table to device */
1111 ret = set_cluster(mydata, dir_curclust, get_dentfromdir_block,
1112 mydata->clust_size * mydata->sect_size);
AKASHI Takahirof1149ce2018-09-11 15:59:03 +09001113 if (ret) {
Benoît Thébaudeaue876be42015-09-28 15:45:31 +02001114 printf("Error: writing directory entry\n");
AKASHI Takahirof1149ce2018-09-11 15:59:03 +09001115 ret = -EIO;
1116 }
Benoît Thébaudeaue876be42015-09-28 15:45:31 +02001117
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001118exit:
1119 free(mydata->fatbuf);
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001120 return ret;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001121}
1122
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001123int file_fat_write(const char *filename, void *buffer, loff_t offset,
1124 loff_t maxsize, loff_t *actwrite)
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001125{
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001126 if (offset != 0) {
Vagrant Cascadian0af49b92016-03-15 12:11:13 -07001127 printf("Error: non zero offset is currently not supported.\n");
AKASHI Takahirof1149ce2018-09-11 15:59:03 +09001128 return -EINVAL;
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001129 }
1130
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001131 printf("writing %s\n", filename);
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001132 return do_fat_write(filename, buffer, maxsize, actwrite);
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001133}