blob: 4605020854c705c669fe2abc276c49b32d7417b5 [file] [log] [blame]
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001/*
2 * fat_write.c
3 *
4 * R/W (V)FAT 12/16/32 filesystem implementation by Donggeun Kim
5 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Donggeun Kimc30a15e2011-10-24 21:15:28 +00007 */
8
9#include <common.h>
10#include <command.h>
11#include <config.h>
12#include <fat.h>
13#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
Donggeun Kim627182e2011-12-20 18:34:27 +0000103static __u8 num_of_fats;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000104/*
105 * Write fat buffer into block device
106 */
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200107static int flush_dirty_fat_buffer(fsdata *mydata)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000108{
109 int getsize = FATBUFBLOCKS;
110 __u32 fatlength = mydata->fatlength;
111 __u8 *bufptr = mydata->fatbuf;
112 __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
113
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200114 debug("debug: evicting %d, dirty: %d\n", mydata->fatbufnum,
115 (int)mydata->fat_dirty);
116
117 if ((!mydata->fat_dirty) || (mydata->fatbufnum == -1))
118 return 0;
119
Stefan Brüns6c1a8082016-12-17 00:27:50 +0100120 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
121 if (startblock + getsize > fatlength)
122 getsize = fatlength - startblock;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000123
Stefan Brüns6c1a8082016-12-17 00:27:50 +0100124 startblock += mydata->fat_sect;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000125
126 /* Write FAT buf */
127 if (disk_write(startblock, getsize, bufptr) < 0) {
128 debug("error: writing FAT blocks\n");
129 return -1;
130 }
131
Donggeun Kim627182e2011-12-20 18:34:27 +0000132 if (num_of_fats == 2) {
133 /* Update corresponding second FAT blocks */
134 startblock += mydata->fatlength;
135 if (disk_write(startblock, getsize, bufptr) < 0) {
136 debug("error: writing second FAT blocks\n");
137 return -1;
138 }
139 }
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200140 mydata->fat_dirty = 0;
Donggeun Kim627182e2011-12-20 18:34:27 +0000141
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000142 return 0;
143}
144
145/*
146 * Get the entry at index 'entry' in a FAT (12/16/32) table.
147 * On failure 0x00 is returned.
148 * When bufnum is changed, write back the previous fatbuf to the disk.
149 */
150static __u32 get_fatent_value(fsdata *mydata, __u32 entry)
151{
152 __u32 bufnum;
153 __u32 off16, offset;
154 __u32 ret = 0x00;
155 __u16 val1, val2;
156
Wu, Joshdd6d7962014-05-08 16:14:07 +0800157 if (CHECK_CLUST(entry, mydata->fatsize)) {
158 printf("Error: Invalid FAT entry: 0x%08x\n", entry);
159 return ret;
160 }
161
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000162 switch (mydata->fatsize) {
163 case 32:
164 bufnum = entry / FAT32BUFSIZE;
165 offset = entry - bufnum * FAT32BUFSIZE;
166 break;
167 case 16:
168 bufnum = entry / FAT16BUFSIZE;
169 offset = entry - bufnum * FAT16BUFSIZE;
170 break;
171 case 12:
172 bufnum = entry / FAT12BUFSIZE;
173 offset = entry - bufnum * FAT12BUFSIZE;
174 break;
175
176 default:
177 /* Unsupported FAT size */
178 return ret;
179 }
180
181 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
182 mydata->fatsize, entry, entry, offset, offset);
183
184 /* Read a new block of FAT entries into the cache. */
185 if (bufnum != mydata->fatbufnum) {
186 int getsize = FATBUFBLOCKS;
187 __u8 *bufptr = mydata->fatbuf;
188 __u32 fatlength = mydata->fatlength;
189 __u32 startblock = bufnum * FATBUFBLOCKS;
190
Stefan Brüns6c1a8082016-12-17 00:27:50 +0100191 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
192 if (startblock + getsize > fatlength)
193 getsize = fatlength - startblock;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000194
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000195 startblock += mydata->fat_sect; /* Offset from start of disk */
196
197 /* Write back the fatbuf to the disk */
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200198 if (flush_dirty_fat_buffer(mydata) < 0)
199 return -1;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000200
201 if (disk_read(startblock, getsize, bufptr) < 0) {
202 debug("Error reading FAT blocks\n");
203 return ret;
204 }
205 mydata->fatbufnum = bufnum;
206 }
207
208 /* Get the actual entry from the table */
209 switch (mydata->fatsize) {
210 case 32:
211 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
212 break;
213 case 16:
214 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
215 break;
216 case 12:
217 off16 = (offset * 3) / 4;
218
219 switch (offset & 0x3) {
220 case 0:
221 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
222 ret &= 0xfff;
223 break;
224 case 1:
225 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
226 val1 &= 0xf000;
227 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
228 val2 &= 0x00ff;
229 ret = (val2 << 4) | (val1 >> 12);
230 break;
231 case 2:
232 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
233 val1 &= 0xff00;
234 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
235 val2 &= 0x000f;
236 ret = (val2 << 8) | (val1 >> 8);
237 break;
238 case 3:
239 ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
240 ret = (ret & 0xfff0) >> 4;
241 break;
242 default:
243 break;
244 }
245 break;
246 }
247 debug("FAT%d: ret: %08x, entry: %08x, offset: %04x\n",
248 mydata->fatsize, ret, entry, offset);
249
250 return ret;
251}
252
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000253/*
254 * Set the file name information from 'name' into 'slotptr',
255 */
256static int str2slot(dir_slot *slotptr, const char *name, int *idx)
257{
258 int j, end_idx = 0;
259
260 for (j = 0; j <= 8; j += 2) {
261 if (name[*idx] == 0x00) {
262 slotptr->name0_4[j] = 0;
263 slotptr->name0_4[j + 1] = 0;
264 end_idx++;
265 goto name0_4;
266 }
267 slotptr->name0_4[j] = name[*idx];
268 (*idx)++;
269 end_idx++;
270 }
271 for (j = 0; j <= 10; j += 2) {
272 if (name[*idx] == 0x00) {
273 slotptr->name5_10[j] = 0;
274 slotptr->name5_10[j + 1] = 0;
275 end_idx++;
276 goto name5_10;
277 }
278 slotptr->name5_10[j] = name[*idx];
279 (*idx)++;
280 end_idx++;
281 }
282 for (j = 0; j <= 2; j += 2) {
283 if (name[*idx] == 0x00) {
284 slotptr->name11_12[j] = 0;
285 slotptr->name11_12[j + 1] = 0;
286 end_idx++;
287 goto name11_12;
288 }
289 slotptr->name11_12[j] = name[*idx];
290 (*idx)++;
291 end_idx++;
292 }
293
294 if (name[*idx] == 0x00)
295 return 1;
296
297 return 0;
298/* Not used characters are filled with 0xff 0xff */
299name0_4:
300 for (; end_idx < 5; end_idx++) {
301 slotptr->name0_4[end_idx * 2] = 0xff;
302 slotptr->name0_4[end_idx * 2 + 1] = 0xff;
303 }
304 end_idx = 5;
305name5_10:
306 end_idx -= 5;
307 for (; end_idx < 6; end_idx++) {
308 slotptr->name5_10[end_idx * 2] = 0xff;
309 slotptr->name5_10[end_idx * 2 + 1] = 0xff;
310 }
311 end_idx = 11;
312name11_12:
313 end_idx -= 11;
314 for (; end_idx < 2; end_idx++) {
315 slotptr->name11_12[end_idx * 2] = 0xff;
316 slotptr->name11_12[end_idx * 2 + 1] = 0xff;
317 }
318
319 return 1;
320}
321
322static int is_next_clust(fsdata *mydata, dir_entry *dentptr);
323static void flush_dir_table(fsdata *mydata, dir_entry **dentptr);
324
325/*
326 * Fill dir_slot entries with appropriate name, id, and attr
327 * The real directory entry is returned by 'dentptr'
328 */
329static void
330fill_dir_slot(fsdata *mydata, dir_entry **dentptr, const char *l_name)
331{
Tien Fong Chee7aa1a6b2016-07-27 23:08:56 -0700332 __u8 temp_dir_slot_buffer[MAX_LFN_SLOT * sizeof(dir_slot)];
333 dir_slot *slotptr = (dir_slot *)temp_dir_slot_buffer;
Anatolij Gustschin8506eb82011-12-15 03:12:14 +0000334 __u8 counter = 0, checksum;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000335 int idx = 0, ret;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000336
Stefan Brünsed76f912016-09-11 22:51:39 +0200337 /* Get short file name checksum value */
Marek Vasutff04f6d2012-10-09 07:20:22 +0000338 checksum = mkcksum((*dentptr)->name, (*dentptr)->ext);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000339
340 do {
341 memset(slotptr, 0x00, sizeof(dir_slot));
342 ret = str2slot(slotptr, l_name, &idx);
343 slotptr->id = ++counter;
344 slotptr->attr = ATTR_VFAT;
345 slotptr->alias_checksum = checksum;
346 slotptr++;
347 } while (ret == 0);
348
349 slotptr--;
350 slotptr->id |= LAST_LONG_ENTRY_MASK;
351
352 while (counter >= 1) {
353 if (is_next_clust(mydata, *dentptr)) {
354 /* A new cluster is allocated for directory table */
355 flush_dir_table(mydata, dentptr);
356 }
357 memcpy(*dentptr, slotptr, sizeof(dir_slot));
358 (*dentptr)++;
359 slotptr--;
360 counter--;
361 }
362
363 if (is_next_clust(mydata, *dentptr)) {
364 /* A new cluster is allocated for directory table */
365 flush_dir_table(mydata, dentptr);
366 }
367}
368
369static __u32 dir_curclust;
370
371/*
372 * Extract the full long filename starting at 'retdent' (which is really
373 * a slot) into 'l_name'. If successful also copy the real directory entry
374 * into 'retdent'
375 * If additional adjacent cluster for directory entries is read into memory,
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000376 * then 'get_contents_vfatname_block' is copied into 'get_dentfromdir_block' and
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000377 * the location of the real directory entry is returned by 'retdent'
378 * Return 0 on success, -1 otherwise.
379 */
380static int
381get_long_file_name(fsdata *mydata, int curclust, __u8 *cluster,
382 dir_entry **retdent, char *l_name)
383{
384 dir_entry *realdent;
385 dir_slot *slotptr = (dir_slot *)(*retdent);
386 dir_slot *slotptr2 = NULL;
387 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
388 PREFETCH_BLOCKS :
389 mydata->clust_size);
390 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
391 int idx = 0, cur_position = 0;
392
393 if (counter > VFAT_MAXSEQ) {
394 debug("Error: VFAT name is too long\n");
395 return -1;
396 }
397
398 while ((__u8 *)slotptr < buflimit) {
399 if (counter == 0)
400 break;
401 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
402 return -1;
403 slotptr++;
404 counter--;
405 }
406
407 if ((__u8 *)slotptr >= buflimit) {
408 if (curclust == 0)
409 return -1;
410 curclust = get_fatent_value(mydata, dir_curclust);
411 if (CHECK_CLUST(curclust, mydata->fatsize)) {
412 debug("curclust: 0x%x\n", curclust);
413 printf("Invalid FAT entry\n");
414 return -1;
415 }
416
417 dir_curclust = curclust;
418
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000419 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000420 mydata->clust_size * mydata->sect_size) != 0) {
421 debug("Error: reading directory block\n");
422 return -1;
423 }
424
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000425 slotptr2 = (dir_slot *)get_contents_vfatname_block;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000426 while (counter > 0) {
427 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
428 & 0xff) != counter)
429 return -1;
430 slotptr2++;
431 counter--;
432 }
433
434 /* Save the real directory entry */
435 realdent = (dir_entry *)slotptr2;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000436 while ((__u8 *)slotptr2 > get_contents_vfatname_block) {
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000437 slotptr2--;
438 slot2str(slotptr2, l_name, &idx);
439 }
440 } else {
441 /* Save the real directory entry */
442 realdent = (dir_entry *)slotptr;
443 }
444
445 do {
446 slotptr--;
447 if (slot2str(slotptr, l_name, &idx))
448 break;
449 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
450
451 l_name[idx] = '\0';
452 if (*l_name == DELETED_FLAG)
453 *l_name = '\0';
454 else if (*l_name == aRING)
455 *l_name = DELETED_FLAG;
456 downcase(l_name);
457
458 /* Return the real directory entry */
459 *retdent = realdent;
460
461 if (slotptr2) {
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000462 memcpy(get_dentfromdir_block, get_contents_vfatname_block,
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000463 mydata->clust_size * mydata->sect_size);
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000464 cur_position = (__u8 *)realdent - get_contents_vfatname_block;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000465 *retdent = (dir_entry *) &get_dentfromdir_block[cur_position];
466 }
467
468 return 0;
469}
470
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000471/*
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500472 * Set the entry at index 'entry' in a FAT (12/16/32) table.
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000473 */
474static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
475{
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500476 __u32 bufnum, offset, off16;
477 __u16 val1, val2;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000478
479 switch (mydata->fatsize) {
480 case 32:
481 bufnum = entry / FAT32BUFSIZE;
482 offset = entry - bufnum * FAT32BUFSIZE;
483 break;
484 case 16:
485 bufnum = entry / FAT16BUFSIZE;
486 offset = entry - bufnum * FAT16BUFSIZE;
487 break;
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500488 case 12:
489 bufnum = entry / FAT12BUFSIZE;
490 offset = entry - bufnum * FAT12BUFSIZE;
491 break;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000492 default:
493 /* Unsupported FAT size */
494 return -1;
495 }
496
497 /* Read a new block of FAT entries into the cache. */
498 if (bufnum != mydata->fatbufnum) {
499 int getsize = FATBUFBLOCKS;
500 __u8 *bufptr = mydata->fatbuf;
501 __u32 fatlength = mydata->fatlength;
502 __u32 startblock = bufnum * FATBUFBLOCKS;
503
Stefan Brüns6c1a8082016-12-17 00:27:50 +0100504 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
505 if (startblock + getsize > fatlength)
506 getsize = fatlength - startblock;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000507
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200508 if (flush_dirty_fat_buffer(mydata) < 0)
509 return -1;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000510
Stefan Brüns6c1a8082016-12-17 00:27:50 +0100511 startblock += mydata->fat_sect;
512
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000513 if (disk_read(startblock, getsize, bufptr) < 0) {
514 debug("Error reading FAT blocks\n");
515 return -1;
516 }
517 mydata->fatbufnum = bufnum;
518 }
519
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200520 /* Mark as dirty */
521 mydata->fat_dirty = 1;
522
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000523 /* Set the actual entry */
524 switch (mydata->fatsize) {
525 case 32:
526 ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
527 break;
528 case 16:
529 ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
530 break;
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500531 case 12:
532 off16 = (offset * 3) / 4;
533
534 switch (offset & 0x3) {
535 case 0:
536 val1 = cpu_to_le16(entry_value) & 0xfff;
537 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff;
538 ((__u16 *)mydata->fatbuf)[off16] |= val1;
539 break;
540 case 1:
541 val1 = cpu_to_le16(entry_value) & 0xf;
542 val2 = (cpu_to_le16(entry_value) >> 4) & 0xff;
543
544 ((__u16 *)mydata->fatbuf)[off16] &= ~0xf000;
545 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 12);
546
547 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xff;
548 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
549 break;
550 case 2:
551 val1 = cpu_to_le16(entry_value) & 0xff;
552 val2 = (cpu_to_le16(entry_value) >> 8) & 0xf;
553
554 ((__u16 *)mydata->fatbuf)[off16] &= ~0xff00;
555 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 8);
556
557 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xf;
558 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
559 break;
560 case 3:
561 val1 = cpu_to_le16(entry_value) & 0xfff;
562 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff0;
563 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 4);
564 break;
565 default:
566 break;
567 }
568
569 break;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000570 default:
571 return -1;
572 }
573
574 return 0;
575}
576
577/*
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500578 * Determine the next free cluster after 'entry' in a FAT (12/16/32) table
Stefan Brünsae1755b2016-09-11 22:51:41 +0200579 * and link it to 'entry'. EOC marker is not set on returned entry.
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000580 */
581static __u32 determine_fatent(fsdata *mydata, __u32 entry)
582{
583 __u32 next_fat, next_entry = entry + 1;
584
585 while (1) {
586 next_fat = get_fatent_value(mydata, next_entry);
587 if (next_fat == 0) {
Stefan Brünsae1755b2016-09-11 22:51:41 +0200588 /* found free entry, link to entry */
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000589 set_fatent_value(mydata, entry, next_entry);
590 break;
591 }
592 next_entry++;
593 }
594 debug("FAT%d: entry: %08x, entry_value: %04x\n",
595 mydata->fatsize, entry, next_entry);
596
597 return next_entry;
598}
599
600/*
601 * Write at most 'size' bytes from 'buffer' into the specified cluster.
602 * Return 0 on success, -1 otherwise.
603 */
604static int
605set_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer,
606 unsigned long size)
607{
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200608 __u32 idx = 0;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000609 __u32 startsect;
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200610 int ret;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000611
612 if (clustnum > 0)
613 startsect = mydata->data_begin +
614 clustnum * mydata->clust_size;
615 else
616 startsect = mydata->rootdir_sect;
617
618 debug("clustnum: %d, startsect: %d\n", clustnum, startsect);
619
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200620 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
621 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
622
623 printf("FAT: Misaligned buffer address (%p)\n", buffer);
624
625 while (size >= mydata->sect_size) {
626 memcpy(tmpbuf, buffer, mydata->sect_size);
627 ret = disk_write(startsect++, 1, tmpbuf);
628 if (ret != 1) {
629 debug("Error writing data (got %d)\n", ret);
630 return -1;
631 }
632
633 buffer += mydata->sect_size;
634 size -= mydata->sect_size;
635 }
636 } else if (size >= mydata->sect_size) {
637 idx = size / mydata->sect_size;
638 ret = disk_write(startsect, idx, buffer);
639 if (ret != idx) {
640 debug("Error writing data (got %d)\n", ret);
Wu, Josh6b8f1852013-07-24 17:55:30 +0800641 return -1;
642 }
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200643
644 startsect += idx;
645 idx *= mydata->sect_size;
646 buffer += idx;
647 size -= idx;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000648 }
649
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200650 if (size) {
651 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000652
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200653 memcpy(tmpbuf, buffer, size);
654 ret = disk_write(startsect, 1, tmpbuf);
655 if (ret != 1) {
656 debug("Error writing data (got %d)\n", ret);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000657 return -1;
658 }
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000659 }
660
661 return 0;
662}
663
664/*
665 * Find the first empty cluster
666 */
667static int find_empty_cluster(fsdata *mydata)
668{
669 __u32 fat_val, entry = 3;
670
671 while (1) {
672 fat_val = get_fatent_value(mydata, entry);
673 if (fat_val == 0)
674 break;
675 entry++;
676 }
677
678 return entry;
679}
680
681/*
682 * Write directory entries in 'get_dentfromdir_block' to block device
683 */
684static void flush_dir_table(fsdata *mydata, dir_entry **dentptr)
685{
686 int dir_newclust = 0;
687
688 if (set_cluster(mydata, dir_curclust,
689 get_dentfromdir_block,
690 mydata->clust_size * mydata->sect_size) != 0) {
691 printf("error: wrinting directory entry\n");
692 return;
693 }
694 dir_newclust = find_empty_cluster(mydata);
695 set_fatent_value(mydata, dir_curclust, dir_newclust);
696 if (mydata->fatsize == 32)
697 set_fatent_value(mydata, dir_newclust, 0xffffff8);
698 else if (mydata->fatsize == 16)
699 set_fatent_value(mydata, dir_newclust, 0xfff8);
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500700 else if (mydata->fatsize == 12)
701 set_fatent_value(mydata, dir_newclust, 0xff8);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000702
703 dir_curclust = dir_newclust;
704
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200705 if (flush_dirty_fat_buffer(mydata) < 0)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000706 return;
707
708 memset(get_dentfromdir_block, 0x00,
709 mydata->clust_size * mydata->sect_size);
710
711 *dentptr = (dir_entry *) get_dentfromdir_block;
712}
713
714/*
715 * Set empty cluster from 'entry' to the end of a file
716 */
717static int clear_fatent(fsdata *mydata, __u32 entry)
718{
719 __u32 fat_val;
720
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500721 while (!CHECK_CLUST(entry, mydata->fatsize)) {
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000722 fat_val = get_fatent_value(mydata, entry);
723 if (fat_val != 0)
724 set_fatent_value(mydata, entry, 0);
725 else
726 break;
727
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000728 entry = fat_val;
729 }
730
731 /* Flush fat buffer */
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200732 if (flush_dirty_fat_buffer(mydata) < 0)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000733 return -1;
734
735 return 0;
736}
737
738/*
739 * Write at most 'maxsize' bytes from 'buffer' into
740 * the file associated with 'dentptr'
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800741 * Update the number of bytes written in *gotsize and return 0
742 * or return -1 on fatal errors.
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000743 */
744static int
745set_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800746 loff_t maxsize, loff_t *gotsize)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000747{
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800748 loff_t filesize = FAT2CPU32(dentptr->size);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000749 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
750 __u32 curclust = START(dentptr);
751 __u32 endclust = 0, newclust = 0;
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800752 loff_t actsize;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000753
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800754 *gotsize = 0;
755 debug("Filesize: %llu bytes\n", filesize);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000756
757 if (maxsize > 0 && filesize > maxsize)
758 filesize = maxsize;
759
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800760 debug("%llu bytes\n", filesize);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000761
Benoît Thébaudeau1254b442015-09-28 15:45:32 +0200762 if (!curclust) {
763 if (filesize) {
764 debug("error: nonempty clusterless file!\n");
765 return -1;
766 }
767 return 0;
768 }
769
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000770 actsize = bytesperclust;
771 endclust = curclust;
772 do {
773 /* search for consecutive clusters */
774 while (actsize < filesize) {
775 newclust = determine_fatent(mydata, endclust);
776
777 if ((newclust - 1) != endclust)
778 goto getit;
779
780 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Benoît Thébaudeau5e1a8602015-09-28 15:45:30 +0200781 debug("newclust: 0x%x\n", newclust);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000782 debug("Invalid FAT entry\n");
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800783 return 0;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000784 }
785 endclust = newclust;
786 actsize += bytesperclust;
787 }
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000788
789 /* set remaining bytes */
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000790 actsize = filesize;
Benoît Thébaudeau1d7f2ec2015-09-28 15:45:29 +0200791 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000792 debug("error: writing cluster\n");
793 return -1;
794 }
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800795 *gotsize += actsize;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000796
797 /* Mark end of file in FAT */
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500798 if (mydata->fatsize == 12)
799 newclust = 0xfff;
800 else if (mydata->fatsize == 16)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000801 newclust = 0xffff;
802 else if (mydata->fatsize == 32)
803 newclust = 0xfffffff;
804 set_fatent_value(mydata, endclust, newclust);
805
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800806 return 0;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000807getit:
808 if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
809 debug("error: writing cluster\n");
810 return -1;
811 }
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800812 *gotsize += actsize;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000813 filesize -= actsize;
814 buffer += actsize;
815
Benoît Thébaudeau5e1a8602015-09-28 15:45:30 +0200816 if (CHECK_CLUST(newclust, mydata->fatsize)) {
817 debug("newclust: 0x%x\n", newclust);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000818 debug("Invalid FAT entry\n");
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800819 return 0;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000820 }
821 actsize = bytesperclust;
822 curclust = endclust = newclust;
823 } while (1);
824}
825
826/*
Benoît Thébaudeau1254b442015-09-28 15:45:32 +0200827 * Set start cluster in directory entry
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000828 */
Benoît Thébaudeau1254b442015-09-28 15:45:32 +0200829static void set_start_cluster(const fsdata *mydata, dir_entry *dentptr,
830 __u32 start_cluster)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000831{
832 if (mydata->fatsize == 32)
833 dentptr->starthi =
834 cpu_to_le16((start_cluster & 0xffff0000) >> 16);
835 dentptr->start = cpu_to_le16(start_cluster & 0xffff);
Benoît Thébaudeau1254b442015-09-28 15:45:32 +0200836}
837
838/*
839 * Fill dir_entry
840 */
841static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
842 const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
843{
844 set_start_cluster(mydata, dentptr, start_cluster);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000845 dentptr->size = cpu_to_le32(size);
846
847 dentptr->attr = attr;
848
849 set_name(dentptr, filename);
850}
851
852/*
853 * Check whether adding a file makes the file system to
854 * exceed the size of the block device
855 * Return -1 when overflow occurs, otherwise return 0
856 */
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800857static int check_overflow(fsdata *mydata, __u32 clustnum, loff_t size)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000858{
Tom Rini9e374e72014-11-24 11:50:46 -0500859 __u32 startsect, sect_num, offset;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000860
861 if (clustnum > 0) {
862 startsect = mydata->data_begin +
863 clustnum * mydata->clust_size;
864 } else {
865 startsect = mydata->rootdir_sect;
866 }
867
Tom Rini9e374e72014-11-24 11:50:46 -0500868 sect_num = div_u64_rem(size, mydata->sect_size, &offset);
869
870 if (offset != 0)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000871 sect_num++;
872
Donggeun Kim079df722012-03-22 04:38:55 +0000873 if (startsect + sect_num > cur_part_info.start + total_sector)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000874 return -1;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000875 return 0;
876}
877
878/*
879 * Check if adding several entries exceed one cluster boundary
880 */
881static int is_next_clust(fsdata *mydata, dir_entry *dentptr)
882{
883 int cur_position;
884
885 cur_position = (__u8 *)dentptr - get_dentfromdir_block;
886
887 if (cur_position >= mydata->clust_size * mydata->sect_size)
888 return 1;
889 else
890 return 0;
891}
892
893static dir_entry *empty_dentptr;
894/*
895 * Find a directory entry based on filename or start cluster number
896 * If the directory entry is not found,
897 * the new position for writing a directory entry will be returned
898 */
899static dir_entry *find_directory_entry(fsdata *mydata, int startsect,
900 char *filename, dir_entry *retdent, __u32 start)
901{
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000902 __u32 curclust = (startsect - mydata->data_begin) / mydata->clust_size;
903
904 debug("get_dentfromdir: %s\n", filename);
905
906 while (1) {
907 dir_entry *dentptr;
908
909 int i;
910
911 if (get_cluster(mydata, curclust, get_dentfromdir_block,
912 mydata->clust_size * mydata->sect_size) != 0) {
913 printf("Error: reading directory block\n");
914 return NULL;
915 }
916
917 dentptr = (dir_entry *)get_dentfromdir_block;
918
919 dir_curclust = curclust;
920
921 for (i = 0; i < DIRENTSPERCLUST; i++) {
922 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
923
924 l_name[0] = '\0';
925 if (dentptr->name[0] == DELETED_FLAG) {
926 dentptr++;
927 if (is_next_clust(mydata, dentptr))
928 break;
929 continue;
930 }
931 if ((dentptr->attr & ATTR_VOLUME)) {
Richard Genoudcb940c72012-12-13 03:30:10 +0000932 if (vfat_enabled &&
933 (dentptr->attr & ATTR_VFAT) &&
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000934 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000935 get_long_file_name(mydata, curclust,
936 get_dentfromdir_block,
937 &dentptr, l_name);
938 debug("vfatname: |%s|\n", l_name);
Richard Genoudcb940c72012-12-13 03:30:10 +0000939 } else {
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000940 /* Volume label or VFAT entry */
941 dentptr++;
942 if (is_next_clust(mydata, dentptr))
943 break;
944 continue;
945 }
946 }
947 if (dentptr->name[0] == 0) {
948 debug("Dentname == NULL - %d\n", i);
949 empty_dentptr = dentptr;
950 return NULL;
951 }
952
953 get_name(dentptr, s_name);
954
955 if (strcmp(filename, s_name)
956 && strcmp(filename, l_name)) {
957 debug("Mismatch: |%s|%s|\n",
958 s_name, l_name);
959 dentptr++;
960 if (is_next_clust(mydata, dentptr))
961 break;
962 continue;
963 }
964
965 memcpy(retdent, dentptr, sizeof(dir_entry));
966
967 debug("DentName: %s", s_name);
968 debug(", start: 0x%x", START(dentptr));
969 debug(", size: 0x%x %s\n",
970 FAT2CPU32(dentptr->size),
971 (dentptr->attr & ATTR_DIR) ?
972 "(DIR)" : "");
973
974 return dentptr;
975 }
976
Wu, Joshdd6d7962014-05-08 16:14:07 +0800977 /*
978 * In FAT16/12, the root dir is locate before data area, shows
979 * in following:
980 * -------------------------------------------------------------
981 * | Boot | FAT1 & 2 | Root dir | Data (start from cluster #2) |
982 * -------------------------------------------------------------
983 *
984 * As a result if curclust is in Root dir, it is a negative
985 * number or 0, 1.
986 *
987 */
988 if (mydata->fatsize != 32 && (int)curclust <= 1) {
989 /* Current clust is in root dir, set to next clust */
990 curclust++;
991 if ((int)curclust <= 1)
992 continue; /* continue to find */
993
994 /* Reach the end of root dir */
995 empty_dentptr = dentptr;
996 return NULL;
997 }
998
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000999 curclust = get_fatent_value(mydata, dir_curclust);
Wu, Josh2e98f702014-05-08 16:14:06 +08001000 if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001001 empty_dentptr = dentptr;
1002 return NULL;
1003 }
1004 if (CHECK_CLUST(curclust, mydata->fatsize)) {
1005 debug("curclust: 0x%x\n", curclust);
1006 debug("Invalid FAT entry\n");
1007 return NULL;
1008 }
1009 }
1010
1011 return NULL;
1012}
1013
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001014static int do_fat_write(const char *filename, void *buffer, loff_t size,
1015 loff_t *actwrite)
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001016{
1017 dir_entry *dentptr, *retdent;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001018 __u32 startsect;
1019 __u32 start_cluster;
1020 boot_sector bs;
1021 volume_info volinfo;
1022 fsdata datablock;
1023 fsdata *mydata = &datablock;
1024 int cursect;
Anatolij Gustschinbf6b6af2012-03-24 23:40:56 +01001025 int ret = -1, name_len;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001026 char l_filename[VFAT_MAXLEN_BYTES];
1027
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001028 *actwrite = size;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001029 dir_curclust = 0;
1030
1031 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
1032 debug("error: reading boot sector\n");
1033 return -1;
1034 }
1035
1036 total_sector = bs.total_sect;
1037 if (total_sector == 0)
Steve Raee04350d2014-05-26 11:52:23 -07001038 total_sector = (int)cur_part_info.size; /* cast of lbaint_t */
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001039
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001040 if (mydata->fatsize == 32)
1041 mydata->fatlength = bs.fat32_length;
1042 else
1043 mydata->fatlength = bs.fat_length;
1044
1045 mydata->fat_sect = bs.reserved;
1046
1047 cursect = mydata->rootdir_sect
1048 = mydata->fat_sect + mydata->fatlength * bs.fats;
Donggeun Kim627182e2011-12-20 18:34:27 +00001049 num_of_fats = bs.fats;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001050
1051 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
1052 mydata->clust_size = bs.cluster_size;
1053
1054 if (mydata->fatsize == 32) {
1055 mydata->data_begin = mydata->rootdir_sect -
1056 (mydata->clust_size * 2);
1057 } else {
1058 int rootdir_size;
1059
1060 rootdir_size = ((bs.dir_entries[1] * (int)256 +
1061 bs.dir_entries[0]) *
1062 sizeof(dir_entry)) /
1063 mydata->sect_size;
1064 mydata->data_begin = mydata->rootdir_sect +
1065 rootdir_size -
1066 (mydata->clust_size * 2);
1067 }
1068
1069 mydata->fatbufnum = -1;
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +02001070 mydata->fat_dirty = 0;
Nobuhiro Iwamatsu8abd0532014-04-08 11:12:46 +09001071 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001072 if (mydata->fatbuf == NULL) {
1073 debug("Error: allocating memory\n");
1074 return -1;
1075 }
1076
1077 if (disk_read(cursect,
1078 (mydata->fatsize == 32) ?
1079 (mydata->clust_size) :
Benoît Thébaudeau1170e632012-09-18 08:14:56 +00001080 PREFETCH_BLOCKS, do_fat_read_at_block) < 0) {
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001081 debug("Error: reading rootdir block\n");
1082 goto exit;
1083 }
Benoît Thébaudeau1170e632012-09-18 08:14:56 +00001084 dentptr = (dir_entry *) do_fat_read_at_block;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001085
1086 name_len = strlen(filename);
Anatolij Gustschin8506eb82011-12-15 03:12:14 +00001087 if (name_len >= VFAT_MAXLEN_BYTES)
1088 name_len = VFAT_MAXLEN_BYTES - 1;
1089
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001090 memcpy(l_filename, filename, name_len);
Anatolij Gustschin8506eb82011-12-15 03:12:14 +00001091 l_filename[name_len] = 0; /* terminate the string */
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001092 downcase(l_filename);
1093
1094 startsect = mydata->rootdir_sect;
1095 retdent = find_directory_entry(mydata, startsect,
1096 l_filename, dentptr, 0);
1097 if (retdent) {
1098 /* Update file size and start_cluster in a directory entry */
1099 retdent->size = cpu_to_le32(size);
Benoît Thébaudeaue876be42015-09-28 15:45:31 +02001100 start_cluster = START(retdent);
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001101
Benoît Thébaudeau1254b442015-09-28 15:45:32 +02001102 if (start_cluster) {
1103 if (size) {
1104 ret = check_overflow(mydata, start_cluster,
1105 size);
1106 if (ret) {
1107 printf("Error: %llu overflow\n", size);
1108 goto exit;
1109 }
1110 }
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001111
Benoît Thébaudeau1254b442015-09-28 15:45:32 +02001112 ret = clear_fatent(mydata, start_cluster);
1113 if (ret) {
1114 printf("Error: clearing FAT entries\n");
1115 goto exit;
1116 }
1117
1118 if (!size)
1119 set_start_cluster(mydata, retdent, 0);
1120 } else if (size) {
1121 ret = start_cluster = find_empty_cluster(mydata);
1122 if (ret < 0) {
1123 printf("Error: finding empty cluster\n");
1124 goto exit;
1125 }
1126
1127 ret = check_overflow(mydata, start_cluster, size);
1128 if (ret) {
1129 printf("Error: %llu overflow\n", size);
1130 goto exit;
1131 }
1132
1133 set_start_cluster(mydata, retdent, start_cluster);
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001134 }
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001135 } else {
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001136 /* Set short name to set alias checksum field in dir_slot */
1137 set_name(empty_dentptr, filename);
1138 fill_dir_slot(mydata, &empty_dentptr, filename);
1139
Benoît Thébaudeau1254b442015-09-28 15:45:32 +02001140 if (size) {
1141 ret = start_cluster = find_empty_cluster(mydata);
1142 if (ret < 0) {
1143 printf("Error: finding empty cluster\n");
1144 goto exit;
1145 }
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001146
Benoît Thébaudeau1254b442015-09-28 15:45:32 +02001147 ret = check_overflow(mydata, start_cluster, size);
1148 if (ret) {
1149 printf("Error: %llu overflow\n", size);
1150 goto exit;
1151 }
1152 } else {
1153 start_cluster = 0;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001154 }
1155
1156 /* Set attribute as archieve for regular file */
1157 fill_dentry(mydata, empty_dentptr, filename,
1158 start_cluster, size, 0x20);
1159
Benoît Thébaudeaue876be42015-09-28 15:45:31 +02001160 retdent = empty_dentptr;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001161 }
1162
Benoît Thébaudeaue876be42015-09-28 15:45:31 +02001163 ret = set_contents(mydata, retdent, buffer, size, actwrite);
1164 if (ret < 0) {
1165 printf("Error: writing contents\n");
1166 goto exit;
1167 }
1168 debug("attempt to write 0x%llx bytes\n", *actwrite);
1169
1170 /* Flush fat buffer */
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +02001171 ret = flush_dirty_fat_buffer(mydata);
Benoît Thébaudeaue876be42015-09-28 15:45:31 +02001172 if (ret) {
1173 printf("Error: flush fat buffer\n");
1174 goto exit;
1175 }
1176
1177 /* Write directory table to device */
1178 ret = set_cluster(mydata, dir_curclust, get_dentfromdir_block,
1179 mydata->clust_size * mydata->sect_size);
1180 if (ret)
1181 printf("Error: writing directory entry\n");
1182
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001183exit:
1184 free(mydata->fatbuf);
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001185 return ret;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001186}
1187
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001188int file_fat_write(const char *filename, void *buffer, loff_t offset,
1189 loff_t maxsize, loff_t *actwrite)
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001190{
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001191 if (offset != 0) {
Vagrant Cascadian0af49b92016-03-15 12:11:13 -07001192 printf("Error: non zero offset is currently not supported.\n");
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001193 return -1;
1194 }
1195
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001196 printf("writing %s\n", filename);
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001197 return do_fat_write(filename, buffer, maxsize, actwrite);
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001198}