blob: 20a54a2418972ccbb9f2cc1e29bb2e7628975184 [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>
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010011#include <div64.h>
Donggeun Kim8f814002011-10-24 21:15:28 +000012#include <fat.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <malloc.h>
Donggeun Kim8f814002011-10-24 21:15:28 +000015#include <part.h>
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010016#include <rand.h>
17#include <asm/byteorder.h>
Simon Glass274e0b02020-05-10 11:39:56 -060018#include <asm/cache.h>
Richard Genoud2e372022012-12-13 00:47:36 +000019#include <linux/ctype.h>
Tom Rinia17b7bc2014-11-24 11:50:46 -050020#include <linux/math64.h>
Donggeun Kim8f814002011-10-24 21:15:28 +000021#include "fat.c"
22
Heinrich Schuchardt24977e52020-11-25 16:33:55 +010023static dir_entry *find_directory_entry(fat_itr *itr, char *filename);
Heinrich Schuchardt5bc30a52020-11-22 09:58:44 +010024static int new_dir_table(fat_itr *itr);
Heinrich Schuchardt24977e52020-11-25 16:33:55 +010025
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010026/* Characters that may only be used in long file names */
27static const char LONG_ONLY_CHARS[] = "+,;=[]";
28
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +010029/* Combined size of the name and ext fields in the directory entry */
30#define SHORT_NAME_SIZE 11
31
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010032/**
33 * str2fat() - convert string to valid FAT name characters
34 *
35 * Stop when reaching end of @src or a period.
36 * Ignore spaces.
37 * Replace characters that may only be used in long names by underscores.
38 * Convert lower case characters to upper case.
39 *
40 * To avoid assumptions about the code page we do not use characters
41 * above 0x7f for the short name.
42 *
43 * @dest: destination buffer
44 * @src: source buffer
45 * @length: size of destination buffer
46 * Return: number of bytes in destination buffer
47 */
48static int str2fat(char *dest, char *src, int length)
Donggeun Kim8f814002011-10-24 21:15:28 +000049{
50 int i;
51
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010052 for (i = 0; i < length; ++src) {
53 char c = *src;
54
55 if (!c || c == '.')
56 break;
57 if (c == ' ')
58 continue;
59 if (strchr(LONG_ONLY_CHARS, c) || c > 0x7f)
60 c = '_';
61 else if (c >= 'a' && c <= 'z')
62 c &= 0xdf;
63 dest[i] = c;
64 ++i;
Donggeun Kim8f814002011-10-24 21:15:28 +000065 }
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010066 return i;
Donggeun Kim8f814002011-10-24 21:15:28 +000067}
68
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010069/**
Heinrich Schuchardt24977e52020-11-25 16:33:55 +010070 * fat_move_to_cluster() - position to first directory entry in cluster
71 *
72 * @itr: directory iterator
73 * @cluster cluster
74 * Return: 0 for success, -EIO on error
75 */
76static int fat_move_to_cluster(fat_itr *itr, unsigned int cluster)
77{
78 unsigned int nbytes;
79
80 /* position to the start of the directory */
81 itr->next_clust = cluster;
82 itr->last_cluster = 0;
83 if (!fat_next_cluster(itr, &nbytes))
84 return -EIO;
85 itr->dent = (dir_entry *)itr->block;
86 itr->remaining = nbytes / sizeof(dir_entry) - 1;
87 return 0;
88}
89
90/**
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010091 * set_name() - set short name in directory entry
92 *
93 * The function determines if the @filename is a valid short name.
94 * In this case no long name is needed.
95 *
96 * If a long name is needed, a short name is constructed.
97 *
Heinrich Schuchardt24977e52020-11-25 16:33:55 +010098 * @itr: directory iterator
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010099 * @filename: long file name
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100100 * @shortname: buffer of 11 bytes to receive chosen short name and extension
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100101 * Return: number of directory entries needed, negative on error
102 */
Heinrich Schuchardt24977e52020-11-25 16:33:55 +0100103static int set_name(fat_itr *itr, const char *filename, char *shortname)
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100104{
105 char *period;
106 char *pos;
107 int period_location;
108 char buf[13];
109 int i;
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100110 int ret;
111 struct {
112 char name[8];
113 char ext[3];
114 } dirent;
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100115
116 if (!filename)
117 return -EIO;
118
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100119 /* Initialize buffer */
120 memset(&dirent, ' ', sizeof(dirent));
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100121
122 /* Convert filename to upper case short name */
123 period = strrchr(filename, '.');
124 pos = (char *)filename;
125 if (*pos == '.') {
126 pos = period + 1;
127 period = 0;
128 }
129 if (period)
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100130 str2fat(dirent.ext, period + 1, sizeof(dirent.ext));
131 period_location = str2fat(dirent.name, pos, sizeof(dirent.name));
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100132 if (period_location < 0)
133 return period_location;
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100134 if (*dirent.name == ' ')
135 *dirent.name = '_';
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100136 /* 0xe5 signals a deleted directory entry. Replace it by 0x05. */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100137 if (*dirent.name == 0xe5)
138 *dirent.name = 0x05;
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100139
140 /* If filename and short name are the same, quit. */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100141 sprintf(buf, "%.*s.%.3s", period_location, dirent.name, dirent.ext);
142 if (!strcmp(buf, filename)) {
143 ret = 1;
144 goto out;
145 }
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100146
147 /* Construct an indexed short name */
148 for (i = 1; i < 0x200000; ++i) {
149 int suffix_len;
150 int suffix_start;
151 int j;
152
153 /* To speed up the search use random numbers */
154 if (i < 10) {
155 j = i;
156 } else {
157 j = 30 - fls(i);
158 j = 10 + (rand() >> j);
159 }
160 sprintf(buf, "~%d", j);
161 suffix_len = strlen(buf);
162 suffix_start = 8 - suffix_len;
163 if (suffix_start > period_location)
164 suffix_start = period_location;
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100165 memcpy(dirent.name + suffix_start, buf, suffix_len);
166 if (*dirent.ext != ' ')
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100167 sprintf(buf, "%.*s.%.3s", suffix_start + suffix_len,
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100168 dirent.name, dirent.ext);
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100169 else
170 sprintf(buf, "%.*s", suffix_start + suffix_len,
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100171 dirent.name);
Heinrich Schuchardt24977e52020-11-25 16:33:55 +0100172 debug("generated short name: %s\n", buf);
173
174 /* Check that the short name does not exist yet. */
175 ret = fat_move_to_cluster(itr, itr->start_clust);
176 if (ret)
177 return ret;
178 if (find_directory_entry(itr, buf))
179 continue;
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100180
Heinrich Schuchardt24977e52020-11-25 16:33:55 +0100181 debug("chosen short name: %s\n", buf);
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100182 /* Each long name directory entry takes 13 characters. */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100183 ret = (strlen(filename) + 25) / 13;
184 goto out;
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100185 }
186 return -EIO;
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100187out:
188 memcpy(shortname, dirent.name, SHORT_NAME_SIZE);
189 return ret;
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100190}
191
Donggeun Kim8f814002011-10-24 21:15:28 +0000192static int total_sector;
Donggeun Kimcb5f3bc2012-03-22 04:38:55 +0000193static int disk_write(__u32 block, __u32 nr_blocks, void *buf)
Donggeun Kim8f814002011-10-24 21:15:28 +0000194{
Łukasz Majewskid6f23d82015-09-03 14:21:39 +0200195 ulong ret;
196
Simon Glass2ee8ada2016-02-29 15:25:52 -0700197 if (!cur_dev)
Donggeun Kim8f814002011-10-24 21:15:28 +0000198 return -1;
199
Donggeun Kimcb5f3bc2012-03-22 04:38:55 +0000200 if (cur_part_info.start + block + nr_blocks >
201 cur_part_info.start + total_sector) {
Donggeun Kim8f814002011-10-24 21:15:28 +0000202 printf("error: overflow occurs\n");
203 return -1;
204 }
205
Simon Glass2ee8ada2016-02-29 15:25:52 -0700206 ret = blk_dwrite(cur_dev, cur_part_info.start + block, nr_blocks, buf);
Łukasz Majewskid6f23d82015-09-03 14:21:39 +0200207 if (nr_blocks && ret == 0)
208 return -1;
209
210 return ret;
Donggeun Kim8f814002011-10-24 21:15:28 +0000211}
212
Donggeun Kim8f814002011-10-24 21:15:28 +0000213/*
214 * Write fat buffer into block device
215 */
Stefan Brüns751b31d2016-09-11 22:51:40 +0200216static int flush_dirty_fat_buffer(fsdata *mydata)
Donggeun Kim8f814002011-10-24 21:15:28 +0000217{
218 int getsize = FATBUFBLOCKS;
219 __u32 fatlength = mydata->fatlength;
220 __u8 *bufptr = mydata->fatbuf;
221 __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
222
Stefan Brüns751b31d2016-09-11 22:51:40 +0200223 debug("debug: evicting %d, dirty: %d\n", mydata->fatbufnum,
224 (int)mydata->fat_dirty);
225
226 if ((!mydata->fat_dirty) || (mydata->fatbufnum == -1))
227 return 0;
228
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100229 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
230 if (startblock + getsize > fatlength)
231 getsize = fatlength - startblock;
Donggeun Kim8f814002011-10-24 21:15:28 +0000232
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100233 startblock += mydata->fat_sect;
Donggeun Kim8f814002011-10-24 21:15:28 +0000234
235 /* Write FAT buf */
236 if (disk_write(startblock, getsize, bufptr) < 0) {
237 debug("error: writing FAT blocks\n");
238 return -1;
239 }
240
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900241 if (mydata->fats == 2) {
Donggeun Kimaf57ce22011-12-20 18:34:27 +0000242 /* Update corresponding second FAT blocks */
243 startblock += mydata->fatlength;
244 if (disk_write(startblock, getsize, bufptr) < 0) {
245 debug("error: writing second FAT blocks\n");
246 return -1;
247 }
248 }
Stefan Brüns751b31d2016-09-11 22:51:40 +0200249 mydata->fat_dirty = 0;
Donggeun Kimaf57ce22011-12-20 18:34:27 +0000250
Donggeun Kim8f814002011-10-24 21:15:28 +0000251 return 0;
252}
253
Heinrich Schuchardt5bc30a52020-11-22 09:58:44 +0100254/**
255 * fat_find_empty_dentries() - find a sequence of available directory entries
256 *
257 * @itr: directory iterator
258 * @count: number of directory entries to find
259 * Return: 0 on success or negative error number
260 */
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +0100261static int fat_find_empty_dentries(fat_itr *itr, int count)
Heinrich Schuchardt5bc30a52020-11-22 09:58:44 +0100262{
263 unsigned int cluster;
264 dir_entry *dent;
265 int remaining;
266 unsigned int n = 0;
267 int ret;
268
269 ret = fat_move_to_cluster(itr, itr->start_clust);
270 if (ret)
271 return ret;
272
273 for (;;) {
274 if (!itr->dent) {
275 log_debug("Not enough directory entries available\n");
276 return -ENOSPC;
277 }
278 switch (itr->dent->name[0]) {
279 case 0x00:
280 case DELETED_FLAG:
281 if (!n) {
282 /* Remember first deleted directory entry */
283 cluster = itr->clust;
284 dent = itr->dent;
285 remaining = itr->remaining;
286 }
287 ++n;
288 if (n == count)
289 goto out;
290 break;
291 default:
292 n = 0;
293 break;
294 }
295
296 next_dent(itr);
297 if (!itr->dent &&
298 (!itr->is_root || itr->fsdata->fatsize == 32) &&
299 new_dir_table(itr))
300 return -ENOSPC;
301 }
302out:
303 /* Position back to first directory entry */
304 if (itr->clust != cluster) {
305 ret = fat_move_to_cluster(itr, cluster);
306 if (ret)
307 return ret;
308 }
309 itr->dent = dent;
310 itr->remaining = remaining;
311 return 0;
312}
313
Donggeun Kim8f814002011-10-24 21:15:28 +0000314/*
Donggeun Kim8f814002011-10-24 21:15:28 +0000315 * Set the file name information from 'name' into 'slotptr',
316 */
317static int str2slot(dir_slot *slotptr, const char *name, int *idx)
318{
319 int j, end_idx = 0;
320
321 for (j = 0; j <= 8; j += 2) {
322 if (name[*idx] == 0x00) {
323 slotptr->name0_4[j] = 0;
324 slotptr->name0_4[j + 1] = 0;
325 end_idx++;
326 goto name0_4;
327 }
328 slotptr->name0_4[j] = name[*idx];
329 (*idx)++;
330 end_idx++;
331 }
332 for (j = 0; j <= 10; j += 2) {
333 if (name[*idx] == 0x00) {
334 slotptr->name5_10[j] = 0;
335 slotptr->name5_10[j + 1] = 0;
336 end_idx++;
337 goto name5_10;
338 }
339 slotptr->name5_10[j] = name[*idx];
340 (*idx)++;
341 end_idx++;
342 }
343 for (j = 0; j <= 2; j += 2) {
344 if (name[*idx] == 0x00) {
345 slotptr->name11_12[j] = 0;
346 slotptr->name11_12[j + 1] = 0;
347 end_idx++;
348 goto name11_12;
349 }
350 slotptr->name11_12[j] = name[*idx];
351 (*idx)++;
352 end_idx++;
353 }
354
355 if (name[*idx] == 0x00)
356 return 1;
357
358 return 0;
359/* Not used characters are filled with 0xff 0xff */
360name0_4:
361 for (; end_idx < 5; end_idx++) {
362 slotptr->name0_4[end_idx * 2] = 0xff;
363 slotptr->name0_4[end_idx * 2 + 1] = 0xff;
364 }
365 end_idx = 5;
366name5_10:
367 end_idx -= 5;
368 for (; end_idx < 6; end_idx++) {
369 slotptr->name5_10[end_idx * 2] = 0xff;
370 slotptr->name5_10[end_idx * 2 + 1] = 0xff;
371 }
372 end_idx = 11;
373name11_12:
374 end_idx -= 11;
375 for (; end_idx < 2; end_idx++) {
376 slotptr->name11_12[end_idx * 2] = 0xff;
377 slotptr->name11_12[end_idx * 2 + 1] = 0xff;
378 }
379
380 return 1;
381}
382
AKASHI Takahiroa2c0bd02019-05-24 14:10:36 +0900383static int flush_dir(fat_itr *itr);
Donggeun Kim8f814002011-10-24 21:15:28 +0000384
Heinrich Schuchardtac4ab752020-11-21 08:32:50 +0100385/**
386 * fill_dir_slot() - fill directory entries for long name
387 *
388 * @itr: directory iterator
389 * @l_name: long name
390 * @shortname: short name
391 * Return: 0 for success, -errno otherwise
Donggeun Kim8f814002011-10-24 21:15:28 +0000392 */
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900393static int
Heinrich Schuchardtac4ab752020-11-21 08:32:50 +0100394fill_dir_slot(fat_itr *itr, const char *l_name, const char *shortname)
Donggeun Kim8f814002011-10-24 21:15:28 +0000395{
Tien Fong Chee0117dbe2016-07-27 23:08:56 -0700396 __u8 temp_dir_slot_buffer[MAX_LFN_SLOT * sizeof(dir_slot)];
397 dir_slot *slotptr = (dir_slot *)temp_dir_slot_buffer;
Anatolij Gustschinf4f9d5d2011-12-15 03:12:14 +0000398 __u8 counter = 0, checksum;
Donggeun Kim8f814002011-10-24 21:15:28 +0000399 int idx = 0, ret;
Donggeun Kim8f814002011-10-24 21:15:28 +0000400
Stefan Brüns6c617d62016-09-11 22:51:39 +0200401 /* Get short file name checksum value */
Heinrich Schuchardtac4ab752020-11-21 08:32:50 +0100402 checksum = mkcksum(shortname, shortname + 8);
Donggeun Kim8f814002011-10-24 21:15:28 +0000403
404 do {
405 memset(slotptr, 0x00, sizeof(dir_slot));
406 ret = str2slot(slotptr, l_name, &idx);
407 slotptr->id = ++counter;
408 slotptr->attr = ATTR_VFAT;
409 slotptr->alias_checksum = checksum;
410 slotptr++;
411 } while (ret == 0);
412
413 slotptr--;
414 slotptr->id |= LAST_LONG_ENTRY_MASK;
415
416 while (counter >= 1) {
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900417 memcpy(itr->dent, slotptr, sizeof(dir_slot));
Donggeun Kim8f814002011-10-24 21:15:28 +0000418 slotptr--;
419 counter--;
AKASHI Takahiroa2c0bd02019-05-24 14:10:36 +0900420
421 if (itr->remaining == 0)
422 flush_dir(itr);
423
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +0100424 next_dent(itr);
425 if (!itr->dent)
426 return -EIO;
Donggeun Kim8f814002011-10-24 21:15:28 +0000427 }
428
Donggeun Kim8f814002011-10-24 21:15:28 +0000429 return 0;
430}
431
Donggeun Kim8f814002011-10-24 21:15:28 +0000432/*
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500433 * Set the entry at index 'entry' in a FAT (12/16/32) table.
Donggeun Kim8f814002011-10-24 21:15:28 +0000434 */
435static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
436{
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500437 __u32 bufnum, offset, off16;
438 __u16 val1, val2;
Donggeun Kim8f814002011-10-24 21:15:28 +0000439
440 switch (mydata->fatsize) {
441 case 32:
442 bufnum = entry / FAT32BUFSIZE;
443 offset = entry - bufnum * FAT32BUFSIZE;
444 break;
445 case 16:
446 bufnum = entry / FAT16BUFSIZE;
447 offset = entry - bufnum * FAT16BUFSIZE;
448 break;
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500449 case 12:
450 bufnum = entry / FAT12BUFSIZE;
451 offset = entry - bufnum * FAT12BUFSIZE;
452 break;
Donggeun Kim8f814002011-10-24 21:15:28 +0000453 default:
454 /* Unsupported FAT size */
455 return -1;
456 }
457
458 /* Read a new block of FAT entries into the cache. */
459 if (bufnum != mydata->fatbufnum) {
460 int getsize = FATBUFBLOCKS;
461 __u8 *bufptr = mydata->fatbuf;
462 __u32 fatlength = mydata->fatlength;
463 __u32 startblock = bufnum * FATBUFBLOCKS;
464
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100465 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
466 if (startblock + getsize > fatlength)
467 getsize = fatlength - startblock;
Donggeun Kim8f814002011-10-24 21:15:28 +0000468
Stefan Brüns751b31d2016-09-11 22:51:40 +0200469 if (flush_dirty_fat_buffer(mydata) < 0)
470 return -1;
Donggeun Kim8f814002011-10-24 21:15:28 +0000471
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100472 startblock += mydata->fat_sect;
473
Donggeun Kim8f814002011-10-24 21:15:28 +0000474 if (disk_read(startblock, getsize, bufptr) < 0) {
475 debug("Error reading FAT blocks\n");
476 return -1;
477 }
478 mydata->fatbufnum = bufnum;
479 }
480
Stefan Brüns751b31d2016-09-11 22:51:40 +0200481 /* Mark as dirty */
482 mydata->fat_dirty = 1;
483
Donggeun Kim8f814002011-10-24 21:15:28 +0000484 /* Set the actual entry */
485 switch (mydata->fatsize) {
486 case 32:
487 ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
488 break;
489 case 16:
490 ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
491 break;
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500492 case 12:
493 off16 = (offset * 3) / 4;
494
495 switch (offset & 0x3) {
496 case 0:
497 val1 = cpu_to_le16(entry_value) & 0xfff;
498 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff;
499 ((__u16 *)mydata->fatbuf)[off16] |= val1;
500 break;
501 case 1:
502 val1 = cpu_to_le16(entry_value) & 0xf;
503 val2 = (cpu_to_le16(entry_value) >> 4) & 0xff;
504
505 ((__u16 *)mydata->fatbuf)[off16] &= ~0xf000;
506 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 12);
507
508 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xff;
509 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
510 break;
511 case 2:
512 val1 = cpu_to_le16(entry_value) & 0xff;
513 val2 = (cpu_to_le16(entry_value) >> 8) & 0xf;
514
515 ((__u16 *)mydata->fatbuf)[off16] &= ~0xff00;
516 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 8);
517
518 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xf;
519 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
520 break;
521 case 3:
522 val1 = cpu_to_le16(entry_value) & 0xfff;
523 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff0;
524 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 4);
525 break;
526 default:
527 break;
528 }
529
530 break;
Donggeun Kim8f814002011-10-24 21:15:28 +0000531 default:
532 return -1;
533 }
534
535 return 0;
536}
537
538/*
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500539 * Determine the next free cluster after 'entry' in a FAT (12/16/32) table
Stefan Brüns14999402016-09-11 22:51:41 +0200540 * and link it to 'entry'. EOC marker is not set on returned entry.
Donggeun Kim8f814002011-10-24 21:15:28 +0000541 */
542static __u32 determine_fatent(fsdata *mydata, __u32 entry)
543{
544 __u32 next_fat, next_entry = entry + 1;
545
546 while (1) {
Stefan Brüns9f95d812016-12-17 00:27:51 +0100547 next_fat = get_fatent(mydata, next_entry);
Donggeun Kim8f814002011-10-24 21:15:28 +0000548 if (next_fat == 0) {
Stefan Brüns14999402016-09-11 22:51:41 +0200549 /* found free entry, link to entry */
Donggeun Kim8f814002011-10-24 21:15:28 +0000550 set_fatent_value(mydata, entry, next_entry);
551 break;
552 }
553 next_entry++;
554 }
555 debug("FAT%d: entry: %08x, entry_value: %04x\n",
556 mydata->fatsize, entry, next_entry);
557
558 return next_entry;
559}
560
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200561/**
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900562 * set_sectors() - write data to sectors
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200563 *
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900564 * Write 'size' bytes from 'buffer' into the specified sector.
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200565 *
566 * @mydata: data to be written
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900567 * @startsect: sector to be written to
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200568 * @buffer: data to be written
569 * @size: bytes to be written (but not more than the size of a cluster)
570 * Return: 0 on success, -1 otherwise
Donggeun Kim8f814002011-10-24 21:15:28 +0000571 */
572static int
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900573set_sectors(fsdata *mydata, u32 startsect, u8 *buffer, u32 size)
Donggeun Kim8f814002011-10-24 21:15:28 +0000574{
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900575 u32 nsects = 0;
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200576 int ret;
Donggeun Kim8f814002011-10-24 21:15:28 +0000577
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900578 debug("startsect: %d\n", startsect);
Donggeun Kim8f814002011-10-24 21:15:28 +0000579
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200580 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
581 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
582
Heinrich Schuchardtaa9d6ba2018-09-13 19:42:47 +0200583 debug("FAT: Misaligned buffer address (%p)\n", buffer);
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200584
585 while (size >= mydata->sect_size) {
586 memcpy(tmpbuf, buffer, mydata->sect_size);
587 ret = disk_write(startsect++, 1, tmpbuf);
588 if (ret != 1) {
589 debug("Error writing data (got %d)\n", ret);
590 return -1;
591 }
592
593 buffer += mydata->sect_size;
594 size -= mydata->sect_size;
595 }
596 } else if (size >= mydata->sect_size) {
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900597 nsects = size / mydata->sect_size;
598 ret = disk_write(startsect, nsects, buffer);
599 if (ret != nsects) {
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200600 debug("Error writing data (got %d)\n", ret);
Wu, Joshb0e38dd2013-07-24 17:55:30 +0800601 return -1;
602 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000603
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900604 startsect += nsects;
605 buffer += nsects * mydata->sect_size;
606 size -= nsects * mydata->sect_size;
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200607 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000608
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200609 if (size) {
610 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200611 /* Do not leak content of stack */
612 memset(tmpbuf, 0, mydata->sect_size);
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200613 memcpy(tmpbuf, buffer, size);
614 ret = disk_write(startsect, 1, tmpbuf);
615 if (ret != 1) {
616 debug("Error writing data (got %d)\n", ret);
Donggeun Kim8f814002011-10-24 21:15:28 +0000617 return -1;
618 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000619 }
620
621 return 0;
622}
623
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900624/**
625 * set_cluster() - write data to cluster
626 *
627 * Write 'size' bytes from 'buffer' into the specified cluster.
628 *
629 * @mydata: data to be written
630 * @clustnum: cluster to be written to
631 * @buffer: data to be written
632 * @size: bytes to be written (but not more than the size of a cluster)
633 * Return: 0 on success, -1 otherwise
634 */
635static int
636set_cluster(fsdata *mydata, u32 clustnum, u8 *buffer, u32 size)
637{
638 return set_sectors(mydata, clust_to_sect(mydata, clustnum),
639 buffer, size);
640}
641
642static int
643flush_dir(fat_itr *itr)
644{
645 fsdata *mydata = itr->fsdata;
646 u32 startsect, sect_offset, nsects;
647
648 if (!itr->is_root || mydata->fatsize == 32)
649 return set_cluster(mydata, itr->clust, itr->block,
650 mydata->clust_size * mydata->sect_size);
651
652 sect_offset = itr->clust * mydata->clust_size;
653 startsect = mydata->rootdir_sect + sect_offset;
654 /* do not write past the end of rootdir */
655 nsects = min_t(u32, mydata->clust_size,
656 mydata->rootdir_size - sect_offset);
657
658 return set_sectors(mydata, startsect, itr->block,
659 nsects * mydata->sect_size);
660}
661
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900662/*
663 * Read and modify data on existing and consecutive cluster blocks
664 */
665static int
666get_set_cluster(fsdata *mydata, __u32 clustnum, loff_t pos, __u8 *buffer,
667 loff_t size, loff_t *gotsize)
668{
Heinrich Schuchardt8a3ac6e2020-07-06 07:48:14 +0200669 static u8 *tmpbuf_cluster;
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900670 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
671 __u32 startsect;
672 loff_t wsize;
673 int clustcount, i, ret;
674
675 *gotsize = 0;
676 if (!size)
677 return 0;
678
Heinrich Schuchardt8a3ac6e2020-07-06 07:48:14 +0200679 if (!tmpbuf_cluster) {
680 tmpbuf_cluster = memalign(ARCH_DMA_MINALIGN, MAX_CLUSTSIZE);
681 if (!tmpbuf_cluster)
682 return -1;
683 }
684
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900685 assert(pos < bytesperclust);
686 startsect = clust_to_sect(mydata, clustnum);
687
688 debug("clustnum: %d, startsect: %d, pos: %lld\n",
689 clustnum, startsect, pos);
690
691 /* partial write at beginning */
692 if (pos) {
693 wsize = min(bytesperclust - pos, size);
694 ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster);
695 if (ret != mydata->clust_size) {
696 debug("Error reading data (got %d)\n", ret);
697 return -1;
698 }
699
700 memcpy(tmpbuf_cluster + pos, buffer, wsize);
701 ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster);
702 if (ret != mydata->clust_size) {
703 debug("Error writing data (got %d)\n", ret);
704 return -1;
705 }
706
707 size -= wsize;
708 buffer += wsize;
709 *gotsize += wsize;
710
711 startsect += mydata->clust_size;
712
713 if (!size)
714 return 0;
715 }
716
717 /* full-cluster write */
718 if (size >= bytesperclust) {
719 clustcount = lldiv(size, bytesperclust);
720
721 if (!((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1))) {
722 wsize = clustcount * bytesperclust;
723 ret = disk_write(startsect,
724 clustcount * mydata->clust_size,
725 buffer);
726 if (ret != clustcount * mydata->clust_size) {
727 debug("Error writing data (got %d)\n", ret);
728 return -1;
729 }
730
731 size -= wsize;
732 buffer += wsize;
733 *gotsize += wsize;
734
735 startsect += clustcount * mydata->clust_size;
736 } else {
737 for (i = 0; i < clustcount; i++) {
738 memcpy(tmpbuf_cluster, buffer, bytesperclust);
739 ret = disk_write(startsect,
740 mydata->clust_size,
741 tmpbuf_cluster);
742 if (ret != mydata->clust_size) {
743 debug("Error writing data (got %d)\n",
744 ret);
745 return -1;
746 }
747
748 size -= bytesperclust;
749 buffer += bytesperclust;
750 *gotsize += bytesperclust;
751
752 startsect += mydata->clust_size;
753 }
754 }
755 }
756
757 /* partial write at end */
758 if (size) {
759 wsize = size;
760 ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster);
761 if (ret != mydata->clust_size) {
762 debug("Error reading data (got %d)\n", ret);
763 return -1;
764 }
765 memcpy(tmpbuf_cluster, buffer, wsize);
766 ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster);
767 if (ret != mydata->clust_size) {
768 debug("Error writing data (got %d)\n", ret);
769 return -1;
770 }
771
772 size -= wsize;
773 buffer += wsize;
774 *gotsize += wsize;
775 }
776
777 assert(!size);
778
779 return 0;
780}
781
Donggeun Kim8f814002011-10-24 21:15:28 +0000782/*
783 * Find the first empty cluster
784 */
785static int find_empty_cluster(fsdata *mydata)
786{
787 __u32 fat_val, entry = 3;
788
789 while (1) {
Stefan Brüns9f95d812016-12-17 00:27:51 +0100790 fat_val = get_fatent(mydata, entry);
Donggeun Kim8f814002011-10-24 21:15:28 +0000791 if (fat_val == 0)
792 break;
793 entry++;
794 }
795
796 return entry;
797}
798
Heinrich Schuchardte09f3e42020-11-26 19:06:55 +0100799/**
800 * new_dir_table() - allocate a cluster for additional directory entries
801 *
802 * @itr: directory iterator
803 * Return: 0 on success, -EIO otherwise
Donggeun Kim8f814002011-10-24 21:15:28 +0000804 */
AKASHI Takahiroa2c0bd02019-05-24 14:10:36 +0900805static int new_dir_table(fat_itr *itr)
Donggeun Kim8f814002011-10-24 21:15:28 +0000806{
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900807 fsdata *mydata = itr->fsdata;
Donggeun Kim8f814002011-10-24 21:15:28 +0000808 int dir_newclust = 0;
Heinrich Schuchardte09f3e42020-11-26 19:06:55 +0100809 int dir_oldclust = itr->clust;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900810 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
Donggeun Kim8f814002011-10-24 21:15:28 +0000811
Donggeun Kim8f814002011-10-24 21:15:28 +0000812 dir_newclust = find_empty_cluster(mydata);
Heinrich Schuchardte09f3e42020-11-26 19:06:55 +0100813
814 /*
815 * Flush before updating FAT to ensure valid directory structure
816 * in case of failure.
817 */
818 itr->clust = dir_newclust;
819 itr->next_clust = dir_newclust;
820 memset(itr->block, 0x00, bytesperclust);
821 if (flush_dir(itr))
822 return -EIO;
823
824 set_fatent_value(mydata, dir_oldclust, dir_newclust);
Donggeun Kim8f814002011-10-24 21:15:28 +0000825 if (mydata->fatsize == 32)
826 set_fatent_value(mydata, dir_newclust, 0xffffff8);
827 else if (mydata->fatsize == 16)
828 set_fatent_value(mydata, dir_newclust, 0xfff8);
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500829 else if (mydata->fatsize == 12)
830 set_fatent_value(mydata, dir_newclust, 0xff8);
Donggeun Kim8f814002011-10-24 21:15:28 +0000831
Stefan Brüns751b31d2016-09-11 22:51:40 +0200832 if (flush_dirty_fat_buffer(mydata) < 0)
Heinrich Schuchardte09f3e42020-11-26 19:06:55 +0100833 return -EIO;
Donggeun Kim8f814002011-10-24 21:15:28 +0000834
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900835 itr->dent = (dir_entry *)itr->block;
836 itr->last_cluster = 1;
837 itr->remaining = bytesperclust / sizeof(dir_entry) - 1;
Donggeun Kim8f814002011-10-24 21:15:28 +0000838
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900839 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +0000840}
841
842/*
843 * Set empty cluster from 'entry' to the end of a file
844 */
845static int clear_fatent(fsdata *mydata, __u32 entry)
846{
847 __u32 fat_val;
848
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500849 while (!CHECK_CLUST(entry, mydata->fatsize)) {
Stefan Brüns9f95d812016-12-17 00:27:51 +0100850 fat_val = get_fatent(mydata, entry);
Donggeun Kim8f814002011-10-24 21:15:28 +0000851 if (fat_val != 0)
852 set_fatent_value(mydata, entry, 0);
853 else
854 break;
855
Donggeun Kim8f814002011-10-24 21:15:28 +0000856 entry = fat_val;
857 }
858
859 /* Flush fat buffer */
Stefan Brüns751b31d2016-09-11 22:51:40 +0200860 if (flush_dirty_fat_buffer(mydata) < 0)
Donggeun Kim8f814002011-10-24 21:15:28 +0000861 return -1;
862
863 return 0;
864}
865
866/*
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +0900867 * Set start cluster in directory entry
868 */
869static void set_start_cluster(const fsdata *mydata, dir_entry *dentptr,
870 __u32 start_cluster)
871{
872 if (mydata->fatsize == 32)
873 dentptr->starthi =
874 cpu_to_le16((start_cluster & 0xffff0000) >> 16);
875 dentptr->start = cpu_to_le16(start_cluster & 0xffff);
876}
877
878/*
879 * Check whether adding a file makes the file system to
880 * exceed the size of the block device
881 * Return -1 when overflow occurs, otherwise return 0
882 */
883static int check_overflow(fsdata *mydata, __u32 clustnum, loff_t size)
884{
885 __u32 startsect, sect_num, offset;
886
887 if (clustnum > 0)
888 startsect = clust_to_sect(mydata, clustnum);
889 else
890 startsect = mydata->rootdir_sect;
891
892 sect_num = div_u64_rem(size, mydata->sect_size, &offset);
893
894 if (offset != 0)
895 sect_num++;
896
897 if (startsect + sect_num > total_sector)
898 return -1;
899 return 0;
900}
901
902/*
Donggeun Kim8f814002011-10-24 21:15:28 +0000903 * Write at most 'maxsize' bytes from 'buffer' into
904 * the file associated with 'dentptr'
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800905 * Update the number of bytes written in *gotsize and return 0
906 * or return -1 on fatal errors.
Donggeun Kim8f814002011-10-24 21:15:28 +0000907 */
908static int
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +0900909set_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, __u8 *buffer,
910 loff_t maxsize, loff_t *gotsize)
Donggeun Kim8f814002011-10-24 21:15:28 +0000911{
Donggeun Kim8f814002011-10-24 21:15:28 +0000912 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
913 __u32 curclust = START(dentptr);
914 __u32 endclust = 0, newclust = 0;
Heinrich Schuchardtdb538a92019-02-25 19:42:48 +0100915 u64 cur_pos, filesize;
916 loff_t offset, actsize, wsize;
Donggeun Kim8f814002011-10-24 21:15:28 +0000917
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800918 *gotsize = 0;
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900919 filesize = pos + maxsize;
Donggeun Kim8f814002011-10-24 21:15:28 +0000920
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800921 debug("%llu bytes\n", filesize);
Donggeun Kim8f814002011-10-24 21:15:28 +0000922
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900923 if (!filesize) {
924 if (!curclust)
925 return 0;
926 if (!CHECK_CLUST(curclust, mydata->fatsize) ||
927 IS_LAST_CLUST(curclust, mydata->fatsize)) {
928 clear_fatent(mydata, curclust);
929 set_start_cluster(mydata, dentptr, 0);
930 return 0;
931 }
932 debug("curclust: 0x%x\n", curclust);
933 debug("Invalid FAT entry\n");
934 return -1;
935 }
936
937 if (!curclust) {
938 assert(pos == 0);
939 goto set_clusters;
940 }
941
942 /* go to cluster at pos */
943 cur_pos = bytesperclust;
944 while (1) {
945 if (pos <= cur_pos)
946 break;
947 if (IS_LAST_CLUST(curclust, mydata->fatsize))
948 break;
949
950 newclust = get_fatent(mydata, curclust);
951 if (!IS_LAST_CLUST(newclust, mydata->fatsize) &&
952 CHECK_CLUST(newclust, mydata->fatsize)) {
953 debug("curclust: 0x%x\n", curclust);
954 debug("Invalid FAT entry\n");
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +0200955 return -1;
956 }
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900957
958 cur_pos += bytesperclust;
959 curclust = newclust;
960 }
961 if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
962 assert(pos == cur_pos);
963 goto set_clusters;
964 }
965
966 assert(pos < cur_pos);
967 cur_pos -= bytesperclust;
968
969 /* overwrite */
970 assert(IS_LAST_CLUST(curclust, mydata->fatsize) ||
971 !CHECK_CLUST(curclust, mydata->fatsize));
972
973 while (1) {
974 /* search for allocated consecutive clusters */
975 actsize = bytesperclust;
976 endclust = curclust;
977 while (1) {
978 if (filesize <= (cur_pos + actsize))
979 break;
980
981 newclust = get_fatent(mydata, endclust);
982
Marek Szyprowski2f241672019-12-02 12:11:13 +0100983 if (newclust != endclust + 1)
984 break;
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900985 if (IS_LAST_CLUST(newclust, mydata->fatsize))
986 break;
987 if (CHECK_CLUST(newclust, mydata->fatsize)) {
988 debug("curclust: 0x%x\n", curclust);
989 debug("Invalid FAT entry\n");
990 return -1;
991 }
992
993 actsize += bytesperclust;
994 endclust = newclust;
995 }
996
997 /* overwrite to <curclust..endclust> */
998 if (pos < cur_pos)
999 offset = 0;
1000 else
1001 offset = pos - cur_pos;
Marek Szyprowski3a867ce2019-12-02 12:11:14 +01001002 wsize = min_t(unsigned long long, actsize, filesize - cur_pos);
1003 wsize -= offset;
1004
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001005 if (get_set_cluster(mydata, curclust, offset,
1006 buffer, wsize, &actsize)) {
1007 printf("Error get-and-setting cluster\n");
1008 return -1;
1009 }
1010 buffer += wsize;
1011 *gotsize += wsize;
1012 cur_pos += offset + wsize;
1013
1014 if (filesize <= cur_pos)
1015 break;
1016
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001017 if (IS_LAST_CLUST(newclust, mydata->fatsize))
1018 /* no more clusters */
1019 break;
1020
1021 curclust = newclust;
1022 }
1023
1024 if (filesize <= cur_pos) {
1025 /* no more write */
1026 newclust = get_fatent(mydata, endclust);
1027 if (!IS_LAST_CLUST(newclust, mydata->fatsize)) {
1028 /* truncate the rest */
1029 clear_fatent(mydata, newclust);
1030
1031 /* Mark end of file in FAT */
1032 if (mydata->fatsize == 12)
1033 newclust = 0xfff;
1034 else if (mydata->fatsize == 16)
1035 newclust = 0xffff;
1036 else if (mydata->fatsize == 32)
1037 newclust = 0xfffffff;
1038 set_fatent_value(mydata, endclust, newclust);
1039 }
1040
1041 return 0;
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001042 }
1043
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001044 curclust = endclust;
1045 filesize -= cur_pos;
Heinrich Schuchardtdb538a92019-02-25 19:42:48 +01001046 assert(!do_div(cur_pos, bytesperclust));
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001047
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001048set_clusters:
1049 /* allocate and write */
1050 assert(!pos);
1051
1052 /* Assure that curclust is valid */
1053 if (!curclust) {
1054 curclust = find_empty_cluster(mydata);
1055 set_start_cluster(mydata, dentptr, curclust);
1056 } else {
1057 newclust = get_fatent(mydata, curclust);
1058
1059 if (IS_LAST_CLUST(newclust, mydata->fatsize)) {
1060 newclust = determine_fatent(mydata, curclust);
1061 set_fatent_value(mydata, curclust, newclust);
1062 curclust = newclust;
1063 } else {
1064 debug("error: something wrong\n");
1065 return -1;
1066 }
1067 }
1068
1069 /* TODO: already partially written */
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001070 if (check_overflow(mydata, curclust, filesize)) {
1071 printf("Error: no space left: %llu\n", filesize);
1072 return -1;
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +02001073 }
1074
Donggeun Kim8f814002011-10-24 21:15:28 +00001075 actsize = bytesperclust;
1076 endclust = curclust;
1077 do {
1078 /* search for consecutive clusters */
1079 while (actsize < filesize) {
1080 newclust = determine_fatent(mydata, endclust);
1081
1082 if ((newclust - 1) != endclust)
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001083 /* write to <curclust..endclust> */
Donggeun Kim8f814002011-10-24 21:15:28 +00001084 goto getit;
1085
1086 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Benoît Thébaudeaue0b86942015-09-28 15:45:30 +02001087 debug("newclust: 0x%x\n", newclust);
Donggeun Kim8f814002011-10-24 21:15:28 +00001088 debug("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001089 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +00001090 }
1091 endclust = newclust;
1092 actsize += bytesperclust;
1093 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001094
1095 /* set remaining bytes */
Donggeun Kim8f814002011-10-24 21:15:28 +00001096 actsize = filesize;
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +02001097 if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
Donggeun Kim8f814002011-10-24 21:15:28 +00001098 debug("error: writing cluster\n");
1099 return -1;
1100 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001101 *gotsize += actsize;
Donggeun Kim8f814002011-10-24 21:15:28 +00001102
1103 /* Mark end of file in FAT */
Philipp Skadorov16f553d2016-12-15 15:52:53 -05001104 if (mydata->fatsize == 12)
1105 newclust = 0xfff;
1106 else if (mydata->fatsize == 16)
Donggeun Kim8f814002011-10-24 21:15:28 +00001107 newclust = 0xffff;
1108 else if (mydata->fatsize == 32)
1109 newclust = 0xfffffff;
1110 set_fatent_value(mydata, endclust, newclust);
1111
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001112 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +00001113getit:
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +02001114 if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
Donggeun Kim8f814002011-10-24 21:15:28 +00001115 debug("error: writing cluster\n");
1116 return -1;
1117 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001118 *gotsize += actsize;
Donggeun Kim8f814002011-10-24 21:15:28 +00001119 filesize -= actsize;
1120 buffer += actsize;
1121
Benoît Thébaudeaue0b86942015-09-28 15:45:30 +02001122 if (CHECK_CLUST(newclust, mydata->fatsize)) {
1123 debug("newclust: 0x%x\n", newclust);
Donggeun Kim8f814002011-10-24 21:15:28 +00001124 debug("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001125 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +00001126 }
1127 actsize = bytesperclust;
1128 curclust = endclust = newclust;
1129 } while (1);
Donggeun Kim8f814002011-10-24 21:15:28 +00001130
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001131 return 0;
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +02001132}
1133
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001134/**
1135 * fill_dentry() - fill directory entry with shortname
1136 *
1137 * @mydata: private filesystem parameters
1138 * @dentptr: directory entry
1139 * @shortname: chosen short name
1140 * @start_cluster: first cluster of file
1141 * @size: file size
1142 * @attr: file attributes
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +02001143 */
1144static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001145 const char *shortname, __u32 start_cluster, __u32 size, __u8 attr)
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +02001146{
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001147 memset(dentptr, 0, sizeof(*dentptr));
1148
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +02001149 set_start_cluster(mydata, dentptr, start_cluster);
Donggeun Kim8f814002011-10-24 21:15:28 +00001150 dentptr->size = cpu_to_le32(size);
1151
1152 dentptr->attr = attr;
1153
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001154 memcpy(dentptr->name, shortname, SHORT_NAME_SIZE);
Donggeun Kim8f814002011-10-24 21:15:28 +00001155}
1156
Heinrich Schuchardt7a3f6102020-11-26 16:10:01 +01001157/**
1158 * find_directory_entry() - find a directory entry by filename
1159 *
1160 * @itr: directory iterator
1161 * @filename: name of file to find
1162 * Return: directory entry or NULL
Donggeun Kim8f814002011-10-24 21:15:28 +00001163 */
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001164static dir_entry *find_directory_entry(fat_itr *itr, char *filename)
Donggeun Kim8f814002011-10-24 21:15:28 +00001165{
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001166 int match = 0;
Donggeun Kim8f814002011-10-24 21:15:28 +00001167
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001168 while (fat_itr_next(itr)) {
1169 /* check both long and short name: */
1170 if (!strcasecmp(filename, itr->name))
1171 match = 1;
1172 else if (itr->name != itr->s_name &&
1173 !strcasecmp(filename, itr->s_name))
1174 match = 1;
Donggeun Kim8f814002011-10-24 21:15:28 +00001175
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001176 if (!match)
1177 continue;
Donggeun Kim8f814002011-10-24 21:15:28 +00001178
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001179 if (itr->dent->name[0] == '\0')
Donggeun Kim8f814002011-10-24 21:15:28 +00001180 return NULL;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001181 else
1182 return itr->dent;
1183 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001184
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001185 return NULL;
1186}
Donggeun Kim8f814002011-10-24 21:15:28 +00001187
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001188static int split_filename(char *filename, char **dirname, char **basename)
1189{
1190 char *p, *last_slash, *last_slash_cont;
Donggeun Kim8f814002011-10-24 21:15:28 +00001191
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001192again:
1193 p = filename;
1194 last_slash = NULL;
1195 last_slash_cont = NULL;
1196 while (*p) {
1197 if (ISDIRDELIM(*p)) {
1198 last_slash = p;
1199 last_slash_cont = p;
1200 /* continuous slashes */
1201 while (ISDIRDELIM(*p))
1202 last_slash_cont = p++;
1203 if (!*p)
1204 break;
Donggeun Kim8f814002011-10-24 21:15:28 +00001205 }
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001206 p++;
1207 }
Wu, Josh675b23c2014-05-08 16:14:07 +08001208
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001209 if (last_slash) {
1210 if (last_slash_cont == (filename + strlen(filename) - 1)) {
1211 /* remove trailing slashes */
1212 *last_slash = '\0';
1213 goto again;
Wu, Josh675b23c2014-05-08 16:14:07 +08001214 }
1215
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001216 if (last_slash == filename) {
1217 /* avoid ""(null) directory */
1218 *dirname = "/";
1219 } else {
1220 *last_slash = '\0';
1221 *dirname = filename;
Donggeun Kim8f814002011-10-24 21:15:28 +00001222 }
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001223
1224 *last_slash_cont = '\0';
1225 *basename = last_slash_cont + 1;
1226 } else {
1227 *dirname = "/"; /* root by default */
1228 *basename = filename;
Donggeun Kim8f814002011-10-24 21:15:28 +00001229 }
1230
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001231 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +00001232}
1233
Heinrich Schuchardt58587212019-05-12 09:59:18 +02001234/**
1235 * normalize_longname() - check long file name and convert to lower case
1236 *
1237 * We assume here that the FAT file system is using an 8bit code page.
1238 * Linux typically uses CP437, EDK2 assumes CP1250.
1239 *
1240 * @l_filename: preallocated buffer receiving the normalized name
1241 * @filename: filename to normalize
1242 * Return: 0 on success, -1 on failure
1243 */
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001244static int normalize_longname(char *l_filename, const char *filename)
1245{
Heinrich Schuchardt58587212019-05-12 09:59:18 +02001246 const char *p, illegal[] = "<>:\"/\\|?*";
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001247
Heinrich Schuchardt58587212019-05-12 09:59:18 +02001248 if (strlen(filename) >= VFAT_MAXLEN_BYTES)
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001249 return -1;
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001250
Heinrich Schuchardt58587212019-05-12 09:59:18 +02001251 for (p = filename; *p; ++p) {
1252 if ((unsigned char)*p < 0x20)
1253 return -1;
1254 if (strchr(illegal, *p))
1255 return -1;
1256 }
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001257
Heinrich Schuchardt58587212019-05-12 09:59:18 +02001258 strcpy(l_filename, filename);
1259 downcase(l_filename, VFAT_MAXLEN_BYTES);
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001260
1261 return 0;
1262}
1263
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001264int file_fat_write_at(const char *filename, loff_t pos, void *buffer,
1265 loff_t size, loff_t *actwrite)
Donggeun Kim8f814002011-10-24 21:15:28 +00001266{
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001267 dir_entry *retdent;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001268 fsdata datablock = { .fatbuf = NULL, };
Donggeun Kim8f814002011-10-24 21:15:28 +00001269 fsdata *mydata = &datablock;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001270 fat_itr *itr = NULL;
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001271 int ret = -1;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001272 char *filename_copy, *parent, *basename;
Donggeun Kim8f814002011-10-24 21:15:28 +00001273 char l_filename[VFAT_MAXLEN_BYTES];
1274
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001275 debug("writing %s\n", filename);
1276
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001277 filename_copy = strdup(filename);
1278 if (!filename_copy)
1279 return -ENOMEM;
Donggeun Kim8f814002011-10-24 21:15:28 +00001280
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001281 split_filename(filename_copy, &parent, &basename);
1282 if (!strlen(basename)) {
1283 ret = -EINVAL;
1284 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001285 }
1286
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001287 filename = basename;
1288 if (normalize_longname(l_filename, filename)) {
1289 printf("FAT: illegal filename (%s)\n", filename);
1290 ret = -EINVAL;
1291 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001292 }
1293
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001294 itr = malloc_cache_aligned(sizeof(fat_itr));
1295 if (!itr) {
1296 ret = -ENOMEM;
1297 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001298 }
1299
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001300 ret = fat_itr_root(itr, &datablock);
1301 if (ret)
Donggeun Kim8f814002011-10-24 21:15:28 +00001302 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001303
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001304 total_sector = datablock.total_sect;
1305
1306 ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1307 if (ret) {
1308 printf("%s: doesn't exist (%d)\n", parent, ret);
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001309 goto exit;
1310 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001311
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001312 retdent = find_directory_entry(itr, l_filename);
1313
Donggeun Kim8f814002011-10-24 21:15:28 +00001314 if (retdent) {
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001315 if (fat_itr_isdir(itr)) {
1316 ret = -EISDIR;
1317 goto exit;
1318 }
1319
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001320 /* A file exists */
1321 if (pos == -1)
1322 /* Append to the end */
1323 pos = FAT2CPU32(retdent->size);
1324 if (pos > retdent->size) {
1325 /* No hole allowed */
1326 ret = -EINVAL;
1327 goto exit;
1328 }
1329
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001330 /* Update file size in a directory entry */
1331 retdent->size = cpu_to_le32(pos + size);
Donggeun Kim8f814002011-10-24 21:15:28 +00001332 } else {
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001333 /* Create a new file */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001334 char shortname[SHORT_NAME_SIZE];
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +01001335 int ndent;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001336
1337 if (itr->is_root) {
1338 /* root dir cannot have "." or ".." */
1339 if (!strcmp(l_filename, ".") ||
1340 !strcmp(l_filename, "..")) {
1341 ret = -EINVAL;
1342 goto exit;
1343 }
1344 }
1345
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001346 if (pos) {
1347 /* No hole allowed */
1348 ret = -EINVAL;
1349 goto exit;
1350 }
1351
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001352 /* Check if long name is needed */
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +01001353 ndent = set_name(itr, filename, shortname);
1354 if (ndent < 0) {
1355 ret = ndent;
1356 goto exit;
1357 }
1358 ret = fat_find_empty_dentries(itr, ndent);
1359 if (ret)
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001360 goto exit;
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +01001361 if (ndent > 1) {
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001362 /* Set long name entries */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001363 ret = fill_dir_slot(itr, filename, shortname);
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001364 if (ret)
1365 goto exit;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001366 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001367
AKASHI Takahiroa2c0bd02019-05-24 14:10:36 +09001368 /* Set short name entry */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001369 fill_dentry(itr->fsdata, itr->dent, shortname, 0, size,
Heinrich Schuchardt881c74a2020-11-22 11:13:33 +01001370 ATTR_ARCH);
Donggeun Kim8f814002011-10-24 21:15:28 +00001371
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001372 retdent = itr->dent;
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001373 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001374
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001375 ret = set_contents(mydata, retdent, pos, buffer, size, actwrite);
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001376 if (ret < 0) {
1377 printf("Error: writing contents\n");
AKASHI Takahiro6aa77692018-09-11 15:59:03 +09001378 ret = -EIO;
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001379 goto exit;
1380 }
1381 debug("attempt to write 0x%llx bytes\n", *actwrite);
Donggeun Kim8f814002011-10-24 21:15:28 +00001382
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001383 /* Flush fat buffer */
Stefan Brüns751b31d2016-09-11 22:51:40 +02001384 ret = flush_dirty_fat_buffer(mydata);
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001385 if (ret) {
1386 printf("Error: flush fat buffer\n");
AKASHI Takahiro6aa77692018-09-11 15:59:03 +09001387 ret = -EIO;
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001388 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001389 }
1390
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001391 /* Write directory table to device */
AKASHI Takahirod98c6742019-05-24 14:10:35 +09001392 ret = flush_dir(itr);
AKASHI Takahiro6aa77692018-09-11 15:59:03 +09001393 if (ret) {
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001394 printf("Error: writing directory entry\n");
AKASHI Takahiro6aa77692018-09-11 15:59:03 +09001395 ret = -EIO;
1396 }
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001397
Donggeun Kim8f814002011-10-24 21:15:28 +00001398exit:
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001399 free(filename_copy);
Donggeun Kim8f814002011-10-24 21:15:28 +00001400 free(mydata->fatbuf);
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001401 free(itr);
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001402 return ret;
Donggeun Kim8f814002011-10-24 21:15:28 +00001403}
1404
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001405int file_fat_write(const char *filename, void *buffer, loff_t offset,
1406 loff_t maxsize, loff_t *actwrite)
Donggeun Kim8f814002011-10-24 21:15:28 +00001407{
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001408 return file_fat_write_at(filename, offset, buffer, maxsize, actwrite);
Donggeun Kim8f814002011-10-24 21:15:28 +00001409}
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001410
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001411static int fat_dir_entries(fat_itr *itr)
1412{
1413 fat_itr *dirs;
1414 fsdata fsdata = { .fatbuf = NULL, }, *mydata = &fsdata;
1415 /* for FATBUFSIZE */
1416 int count;
1417
1418 dirs = malloc_cache_aligned(sizeof(fat_itr));
1419 if (!dirs) {
1420 debug("Error: allocating memory\n");
1421 count = -ENOMEM;
1422 goto exit;
1423 }
1424
1425 /* duplicate fsdata */
1426 fat_itr_child(dirs, itr);
1427 fsdata = *dirs->fsdata;
1428
1429 /* allocate local fat buffer */
1430 fsdata.fatbuf = malloc_cache_aligned(FATBUFSIZE);
1431 if (!fsdata.fatbuf) {
1432 debug("Error: allocating memory\n");
1433 count = -ENOMEM;
1434 goto exit;
1435 }
1436 fsdata.fatbufnum = -1;
1437 dirs->fsdata = &fsdata;
1438
1439 for (count = 0; fat_itr_next(dirs); count++)
1440 ;
1441
1442exit:
1443 free(fsdata.fatbuf);
1444 free(dirs);
1445 return count;
1446}
1447
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001448/**
1449 * delete_single_dentry() - delete a single directory entry
1450 *
1451 * @itr: directory iterator
1452 * Return: 0 for success
1453 */
1454static int delete_single_dentry(fat_itr *itr)
1455{
1456 struct dir_entry *dent = itr->dent;
1457
1458 memset(dent, 0, sizeof(*dent));
1459 dent->name[0] = DELETED_FLAG;
1460
1461 if (!itr->remaining) {
1462 if (flush_dir(itr)) {
1463 printf("error: writing directory entry\n");
1464 return -EIO;
1465 }
1466 }
1467 return 0;
1468}
1469
1470/**
1471 * delete_long_name() - delete long name directory entries
1472 *
1473 * @itr: directory iterator
1474 * Return: 0 for success
1475 */
1476static int delete_long_name(fat_itr *itr)
1477{
1478 struct dir_entry *dent = itr->dent;
1479 int seqn = itr->dent->name[0] & ~LAST_LONG_ENTRY_MASK;
1480
1481 while (seqn--) {
1482 int ret;
1483
1484 ret = delete_single_dentry(itr);
1485 if (ret)
1486 return ret;
1487 dent = next_dent(itr);
1488 if (!dent)
1489 return -EIO;
1490 }
1491 return 0;
1492}
1493
1494/**
1495 * delete_dentry_long() - remove directory entry
1496 *
1497 * @itr: directory iterator
1498 * Return: 0 for success
1499 */
1500static int delete_dentry_long(fat_itr *itr)
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001501{
1502 fsdata *mydata = itr->fsdata;
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001503 dir_entry *dent = itr->dent;
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001504
1505 /* free cluster blocks */
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001506 clear_fatent(mydata, START(dent));
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001507 if (flush_dirty_fat_buffer(mydata) < 0) {
1508 printf("Error: flush fat buffer\n");
1509 return -EIO;
1510 }
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001511 /* Position to first directory entry for long name */
1512 if (itr->clust != itr->dent_clust) {
1513 int ret;
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001514
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001515 ret = fat_move_to_cluster(itr, itr->dent_clust);
1516 if (ret)
1517 return ret;
1518 }
1519 itr->dent = itr->dent_start;
1520 itr->remaining = itr->dent_rem;
1521 dent = itr->dent_start;
1522 /* Delete long name */
1523 if ((dent->attr & ATTR_VFAT) == ATTR_VFAT &&
1524 (dent->name[0] & LAST_LONG_ENTRY_MASK)) {
1525 int ret;
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001526
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001527 ret = delete_long_name(itr);
1528 if (ret)
1529 return ret;
1530 }
1531 /* Delete short name */
1532 delete_single_dentry(itr);
AKASHI Takahirod98c6742019-05-24 14:10:35 +09001533 if (flush_dir(itr)) {
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001534 printf("error: writing directory entry\n");
1535 return -EIO;
1536 }
1537
1538 return 0;
1539}
1540
1541int fat_unlink(const char *filename)
1542{
1543 fsdata fsdata = { .fatbuf = NULL, };
1544 fat_itr *itr = NULL;
1545 int n_entries, ret;
1546 char *filename_copy, *dirname, *basename;
1547
1548 filename_copy = strdup(filename);
Heinrich Schuchardtb2242f62018-10-02 06:58:00 +02001549 if (!filename_copy) {
1550 printf("Error: allocating memory\n");
1551 ret = -ENOMEM;
1552 goto exit;
1553 }
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001554 split_filename(filename_copy, &dirname, &basename);
1555
1556 if (!strcmp(dirname, "/") && !strcmp(basename, "")) {
1557 printf("Error: cannot remove root\n");
1558 ret = -EINVAL;
1559 goto exit;
1560 }
1561
1562 itr = malloc_cache_aligned(sizeof(fat_itr));
1563 if (!itr) {
1564 printf("Error: allocating memory\n");
Heinrich Schuchardtb2242f62018-10-02 06:58:00 +02001565 ret = -ENOMEM;
1566 goto exit;
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001567 }
1568
1569 ret = fat_itr_root(itr, &fsdata);
1570 if (ret)
1571 goto exit;
1572
1573 total_sector = fsdata.total_sect;
1574
1575 ret = fat_itr_resolve(itr, dirname, TYPE_DIR);
1576 if (ret) {
1577 printf("%s: doesn't exist (%d)\n", dirname, ret);
1578 ret = -ENOENT;
1579 goto exit;
1580 }
1581
1582 if (!find_directory_entry(itr, basename)) {
1583 printf("%s: doesn't exist\n", basename);
1584 ret = -ENOENT;
1585 goto exit;
1586 }
1587
1588 if (fat_itr_isdir(itr)) {
1589 n_entries = fat_dir_entries(itr);
1590 if (n_entries < 0) {
1591 ret = n_entries;
1592 goto exit;
1593 }
1594 if (n_entries > 2) {
1595 printf("Error: directory is not empty: %d\n",
1596 n_entries);
1597 ret = -EINVAL;
1598 goto exit;
1599 }
1600 }
1601
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001602 ret = delete_dentry_long(itr);
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001603
1604exit:
1605 free(fsdata.fatbuf);
1606 free(itr);
1607 free(filename_copy);
1608
1609 return ret;
1610}
1611
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001612int fat_mkdir(const char *new_dirname)
1613{
1614 dir_entry *retdent;
1615 fsdata datablock = { .fatbuf = NULL, };
1616 fsdata *mydata = &datablock;
1617 fat_itr *itr = NULL;
1618 char *dirname_copy, *parent, *dirname;
1619 char l_dirname[VFAT_MAXLEN_BYTES];
1620 int ret = -1;
1621 loff_t actwrite;
1622 unsigned int bytesperclust;
1623 dir_entry *dotdent = NULL;
1624
1625 dirname_copy = strdup(new_dirname);
1626 if (!dirname_copy)
1627 goto exit;
1628
1629 split_filename(dirname_copy, &parent, &dirname);
1630 if (!strlen(dirname)) {
1631 ret = -EINVAL;
1632 goto exit;
1633 }
1634
1635 if (normalize_longname(l_dirname, dirname)) {
1636 printf("FAT: illegal filename (%s)\n", dirname);
1637 ret = -EINVAL;
1638 goto exit;
1639 }
1640
1641 itr = malloc_cache_aligned(sizeof(fat_itr));
1642 if (!itr) {
1643 ret = -ENOMEM;
1644 goto exit;
1645 }
1646
1647 ret = fat_itr_root(itr, &datablock);
1648 if (ret)
1649 goto exit;
1650
1651 total_sector = datablock.total_sect;
1652
1653 ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1654 if (ret) {
1655 printf("%s: doesn't exist (%d)\n", parent, ret);
1656 goto exit;
1657 }
1658
1659 retdent = find_directory_entry(itr, l_dirname);
1660
1661 if (retdent) {
1662 printf("%s: already exists\n", l_dirname);
1663 ret = -EEXIST;
1664 goto exit;
1665 } else {
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001666 char shortname[SHORT_NAME_SIZE];
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +01001667 int ndent;
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001668
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001669 if (itr->is_root) {
1670 /* root dir cannot have "." or ".." */
1671 if (!strcmp(l_dirname, ".") ||
1672 !strcmp(l_dirname, "..")) {
1673 ret = -EINVAL;
1674 goto exit;
1675 }
1676 }
1677
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001678 /* Check if long name is needed */
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +01001679 ndent = set_name(itr, dirname, shortname);
1680 if (ndent < 0) {
1681 ret = ndent;
1682 goto exit;
1683 }
1684 ret = fat_find_empty_dentries(itr, ndent);
1685 if (ret)
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001686 goto exit;
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +01001687 if (ndent > 1) {
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001688 /* Set long name entries */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001689 ret = fill_dir_slot(itr, dirname, shortname);
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001690 if (ret)
1691 goto exit;
1692 }
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001693
1694 /* Set attribute as archive for regular file */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001695 fill_dentry(itr->fsdata, itr->dent, shortname, 0, 0,
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001696 ATTR_DIR | ATTR_ARCH);
1697
1698 retdent = itr->dent;
1699 }
1700
1701 /* Default entries */
1702 bytesperclust = mydata->clust_size * mydata->sect_size;
1703 dotdent = malloc_cache_aligned(bytesperclust);
1704 if (!dotdent) {
1705 ret = -ENOMEM;
1706 goto exit;
1707 }
1708 memset(dotdent, 0, bytesperclust);
1709
1710 memcpy(dotdent[0].name, ". ", 8);
1711 memcpy(dotdent[0].ext, " ", 3);
1712 dotdent[0].attr = ATTR_DIR | ATTR_ARCH;
1713
1714 memcpy(dotdent[1].name, ".. ", 8);
1715 memcpy(dotdent[1].ext, " ", 3);
1716 dotdent[1].attr = ATTR_DIR | ATTR_ARCH;
Heinrich Schuchardt00cf0762020-11-24 21:04:07 +01001717
1718 if (itr->is_root)
1719 set_start_cluster(mydata, &dotdent[1], 0);
1720 else
1721 set_start_cluster(mydata, &dotdent[1], itr->start_clust);
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001722
1723 ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
1724 bytesperclust, &actwrite);
1725 if (ret < 0) {
1726 printf("Error: writing contents\n");
1727 goto exit;
1728 }
1729 /* Write twice for "." */
1730 set_start_cluster(mydata, &dotdent[0], START(retdent));
1731 ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
1732 bytesperclust, &actwrite);
1733 if (ret < 0) {
1734 printf("Error: writing contents\n");
1735 goto exit;
1736 }
1737
1738 /* Flush fat buffer */
1739 ret = flush_dirty_fat_buffer(mydata);
1740 if (ret) {
1741 printf("Error: flush fat buffer\n");
1742 goto exit;
1743 }
1744
1745 /* Write directory table to device */
AKASHI Takahirod98c6742019-05-24 14:10:35 +09001746 ret = flush_dir(itr);
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001747 if (ret)
1748 printf("Error: writing directory entry\n");
1749
1750exit:
1751 free(dirname_copy);
1752 free(mydata->fatbuf);
1753 free(itr);
1754 free(dotdent);
1755 return ret;
1756}