blob: 00541ebc3a4a8a5b75480d04558b0c23cff66d07 [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
Simon Glass468247d2023-01-28 15:00:16 -07008#define LOG_CATEGORY LOGC_FS
9
Donggeun Kim8f814002011-10-24 21:15:28 +000010#include <common.h>
11#include <command.h>
12#include <config.h>
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010013#include <div64.h>
Donggeun Kim8f814002011-10-24 21:15:28 +000014#include <fat.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070016#include <malloc.h>
Donggeun Kim8f814002011-10-24 21:15:28 +000017#include <part.h>
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010018#include <rand.h>
19#include <asm/byteorder.h>
Simon Glass274e0b02020-05-10 11:39:56 -060020#include <asm/cache.h>
Richard Genoud2e372022012-12-13 00:47:36 +000021#include <linux/ctype.h>
Tom Rinia17b7bc2014-11-24 11:50:46 -050022#include <linux/math64.h>
Donggeun Kim8f814002011-10-24 21:15:28 +000023#include "fat.c"
24
Heinrich Schuchardt24977e52020-11-25 16:33:55 +010025static dir_entry *find_directory_entry(fat_itr *itr, char *filename);
Heinrich Schuchardt5bc30a52020-11-22 09:58:44 +010026static int new_dir_table(fat_itr *itr);
Heinrich Schuchardt24977e52020-11-25 16:33:55 +010027
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010028/* Characters that may only be used in long file names */
29static const char LONG_ONLY_CHARS[] = "+,;=[]";
30
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +010031/* Combined size of the name and ext fields in the directory entry */
32#define SHORT_NAME_SIZE 11
33
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010034/**
35 * str2fat() - convert string to valid FAT name characters
36 *
37 * Stop when reaching end of @src or a period.
38 * Ignore spaces.
39 * Replace characters that may only be used in long names by underscores.
40 * Convert lower case characters to upper case.
41 *
42 * To avoid assumptions about the code page we do not use characters
43 * above 0x7f for the short name.
44 *
45 * @dest: destination buffer
46 * @src: source buffer
47 * @length: size of destination buffer
48 * Return: number of bytes in destination buffer
49 */
50static int str2fat(char *dest, char *src, int length)
Donggeun Kim8f814002011-10-24 21:15:28 +000051{
52 int i;
53
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010054 for (i = 0; i < length; ++src) {
55 char c = *src;
56
57 if (!c || c == '.')
58 break;
59 if (c == ' ')
60 continue;
61 if (strchr(LONG_ONLY_CHARS, c) || c > 0x7f)
62 c = '_';
63 else if (c >= 'a' && c <= 'z')
64 c &= 0xdf;
65 dest[i] = c;
66 ++i;
Donggeun Kim8f814002011-10-24 21:15:28 +000067 }
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010068 return i;
Donggeun Kim8f814002011-10-24 21:15:28 +000069}
70
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010071/**
Heinrich Schuchardt24977e52020-11-25 16:33:55 +010072 * fat_move_to_cluster() - position to first directory entry in cluster
73 *
74 * @itr: directory iterator
75 * @cluster cluster
76 * Return: 0 for success, -EIO on error
77 */
78static int fat_move_to_cluster(fat_itr *itr, unsigned int cluster)
79{
80 unsigned int nbytes;
81
82 /* position to the start of the directory */
83 itr->next_clust = cluster;
84 itr->last_cluster = 0;
85 if (!fat_next_cluster(itr, &nbytes))
86 return -EIO;
87 itr->dent = (dir_entry *)itr->block;
88 itr->remaining = nbytes / sizeof(dir_entry) - 1;
89 return 0;
90}
91
92/**
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010093 * set_name() - set short name in directory entry
94 *
95 * The function determines if the @filename is a valid short name.
96 * In this case no long name is needed.
97 *
98 * If a long name is needed, a short name is constructed.
99 *
Heinrich Schuchardt24977e52020-11-25 16:33:55 +0100100 * @itr: directory iterator
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100101 * @filename: long file name
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100102 * @shortname: buffer of 11 bytes to receive chosen short name and extension
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100103 * Return: number of directory entries needed, negative on error
104 */
Heinrich Schuchardt24977e52020-11-25 16:33:55 +0100105static int set_name(fat_itr *itr, const char *filename, char *shortname)
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100106{
107 char *period;
108 char *pos;
109 int period_location;
110 char buf[13];
111 int i;
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100112 int ret;
Heinrich Schuchardtcfb7c2b2021-01-26 00:04:19 +0100113 struct nameext dirent;
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100114
115 if (!filename)
116 return -EIO;
117
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100118 /* Initialize buffer */
119 memset(&dirent, ' ', sizeof(dirent));
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100120
121 /* Convert filename to upper case short name */
122 period = strrchr(filename, '.');
123 pos = (char *)filename;
124 if (*pos == '.') {
125 pos = period + 1;
126 period = 0;
127 }
128 if (period)
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100129 str2fat(dirent.ext, period + 1, sizeof(dirent.ext));
130 period_location = str2fat(dirent.name, pos, sizeof(dirent.name));
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100131 if (period_location < 0)
132 return period_location;
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100133 if (*dirent.name == ' ')
134 *dirent.name = '_';
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100135 /* 0xe5 signals a deleted directory entry. Replace it by 0x05. */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100136 if (*dirent.name == 0xe5)
137 *dirent.name = 0x05;
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100138
139 /* If filename and short name are the same, quit. */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100140 sprintf(buf, "%.*s.%.3s", period_location, dirent.name, dirent.ext);
141 if (!strcmp(buf, filename)) {
142 ret = 1;
143 goto out;
144 }
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100145
146 /* Construct an indexed short name */
147 for (i = 1; i < 0x200000; ++i) {
148 int suffix_len;
149 int suffix_start;
150 int j;
151
152 /* To speed up the search use random numbers */
153 if (i < 10) {
154 j = i;
155 } else {
156 j = 30 - fls(i);
157 j = 10 + (rand() >> j);
158 }
159 sprintf(buf, "~%d", j);
160 suffix_len = strlen(buf);
161 suffix_start = 8 - suffix_len;
162 if (suffix_start > period_location)
163 suffix_start = period_location;
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100164 memcpy(dirent.name + suffix_start, buf, suffix_len);
165 if (*dirent.ext != ' ')
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100166 sprintf(buf, "%.*s.%.3s", suffix_start + suffix_len,
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100167 dirent.name, dirent.ext);
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100168 else
169 sprintf(buf, "%.*s", suffix_start + suffix_len,
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100170 dirent.name);
Heinrich Schuchardt24977e52020-11-25 16:33:55 +0100171 debug("generated short name: %s\n", buf);
172
173 /* Check that the short name does not exist yet. */
174 ret = fat_move_to_cluster(itr, itr->start_clust);
175 if (ret)
176 return ret;
177 if (find_directory_entry(itr, buf))
178 continue;
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100179
Heinrich Schuchardt24977e52020-11-25 16:33:55 +0100180 debug("chosen short name: %s\n", buf);
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100181 /* Each long name directory entry takes 13 characters. */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100182 ret = (strlen(filename) + 25) / 13;
183 goto out;
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100184 }
185 return -EIO;
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100186out:
Heinrich Schuchardtcfb7c2b2021-01-26 00:04:19 +0100187 memcpy(shortname, &dirent, SHORT_NAME_SIZE);
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100188 return ret;
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100189}
190
Donggeun Kim8f814002011-10-24 21:15:28 +0000191static int total_sector;
Donggeun Kimcb5f3bc2012-03-22 04:38:55 +0000192static int disk_write(__u32 block, __u32 nr_blocks, void *buf)
Donggeun Kim8f814002011-10-24 21:15:28 +0000193{
Łukasz Majewskid6f23d82015-09-03 14:21:39 +0200194 ulong ret;
195
Simon Glass2ee8ada2016-02-29 15:25:52 -0700196 if (!cur_dev)
Donggeun Kim8f814002011-10-24 21:15:28 +0000197 return -1;
198
Donggeun Kimcb5f3bc2012-03-22 04:38:55 +0000199 if (cur_part_info.start + block + nr_blocks >
200 cur_part_info.start + total_sector) {
Donggeun Kim8f814002011-10-24 21:15:28 +0000201 printf("error: overflow occurs\n");
202 return -1;
203 }
204
Simon Glass2ee8ada2016-02-29 15:25:52 -0700205 ret = blk_dwrite(cur_dev, cur_part_info.start + block, nr_blocks, buf);
Łukasz Majewskid6f23d82015-09-03 14:21:39 +0200206 if (nr_blocks && ret == 0)
207 return -1;
208
209 return ret;
Donggeun Kim8f814002011-10-24 21:15:28 +0000210}
211
Donggeun Kim8f814002011-10-24 21:15:28 +0000212/*
213 * Write fat buffer into block device
214 */
Stefan Brüns751b31d2016-09-11 22:51:40 +0200215static int flush_dirty_fat_buffer(fsdata *mydata)
Donggeun Kim8f814002011-10-24 21:15:28 +0000216{
217 int getsize = FATBUFBLOCKS;
218 __u32 fatlength = mydata->fatlength;
219 __u8 *bufptr = mydata->fatbuf;
220 __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
221
Stefan Brüns751b31d2016-09-11 22:51:40 +0200222 debug("debug: evicting %d, dirty: %d\n", mydata->fatbufnum,
223 (int)mydata->fat_dirty);
224
225 if ((!mydata->fat_dirty) || (mydata->fatbufnum == -1))
226 return 0;
227
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100228 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
229 if (startblock + getsize > fatlength)
230 getsize = fatlength - startblock;
Donggeun Kim8f814002011-10-24 21:15:28 +0000231
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100232 startblock += mydata->fat_sect;
Donggeun Kim8f814002011-10-24 21:15:28 +0000233
234 /* Write FAT buf */
235 if (disk_write(startblock, getsize, bufptr) < 0) {
236 debug("error: writing FAT blocks\n");
237 return -1;
238 }
239
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900240 if (mydata->fats == 2) {
Donggeun Kimaf57ce22011-12-20 18:34:27 +0000241 /* Update corresponding second FAT blocks */
242 startblock += mydata->fatlength;
243 if (disk_write(startblock, getsize, bufptr) < 0) {
244 debug("error: writing second FAT blocks\n");
245 return -1;
246 }
247 }
Stefan Brüns751b31d2016-09-11 22:51:40 +0200248 mydata->fat_dirty = 0;
Donggeun Kimaf57ce22011-12-20 18:34:27 +0000249
Donggeun Kim8f814002011-10-24 21:15:28 +0000250 return 0;
251}
252
Heinrich Schuchardt5bc30a52020-11-22 09:58:44 +0100253/**
254 * fat_find_empty_dentries() - find a sequence of available directory entries
255 *
256 * @itr: directory iterator
257 * @count: number of directory entries to find
258 * Return: 0 on success or negative error number
259 */
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +0100260static int fat_find_empty_dentries(fat_itr *itr, int count)
Heinrich Schuchardt5bc30a52020-11-22 09:58:44 +0100261{
262 unsigned int cluster;
263 dir_entry *dent;
264 int remaining;
265 unsigned int n = 0;
266 int ret;
267
268 ret = fat_move_to_cluster(itr, itr->start_clust);
269 if (ret)
270 return ret;
271
272 for (;;) {
273 if (!itr->dent) {
274 log_debug("Not enough directory entries available\n");
275 return -ENOSPC;
276 }
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100277 switch (itr->dent->nameext.name[0]) {
Heinrich Schuchardt5bc30a52020-11-22 09:58:44 +0100278 case 0x00:
279 case DELETED_FLAG:
280 if (!n) {
281 /* Remember first deleted directory entry */
282 cluster = itr->clust;
283 dent = itr->dent;
284 remaining = itr->remaining;
285 }
286 ++n;
287 if (n == count)
288 goto out;
289 break;
290 default:
291 n = 0;
292 break;
293 }
294
295 next_dent(itr);
296 if (!itr->dent &&
297 (!itr->is_root || itr->fsdata->fatsize == 32) &&
298 new_dir_table(itr))
299 return -ENOSPC;
300 }
301out:
302 /* Position back to first directory entry */
303 if (itr->clust != cluster) {
304 ret = fat_move_to_cluster(itr, cluster);
305 if (ret)
306 return ret;
307 }
308 itr->dent = dent;
309 itr->remaining = remaining;
310 return 0;
311}
312
Donggeun Kim8f814002011-10-24 21:15:28 +0000313/*
Donggeun Kim8f814002011-10-24 21:15:28 +0000314 * Set the file name information from 'name' into 'slotptr',
315 */
316static int str2slot(dir_slot *slotptr, const char *name, int *idx)
317{
318 int j, end_idx = 0;
319
320 for (j = 0; j <= 8; j += 2) {
321 if (name[*idx] == 0x00) {
322 slotptr->name0_4[j] = 0;
323 slotptr->name0_4[j + 1] = 0;
324 end_idx++;
325 goto name0_4;
326 }
327 slotptr->name0_4[j] = name[*idx];
328 (*idx)++;
329 end_idx++;
330 }
331 for (j = 0; j <= 10; j += 2) {
332 if (name[*idx] == 0x00) {
333 slotptr->name5_10[j] = 0;
334 slotptr->name5_10[j + 1] = 0;
335 end_idx++;
336 goto name5_10;
337 }
338 slotptr->name5_10[j] = name[*idx];
339 (*idx)++;
340 end_idx++;
341 }
342 for (j = 0; j <= 2; j += 2) {
343 if (name[*idx] == 0x00) {
344 slotptr->name11_12[j] = 0;
345 slotptr->name11_12[j + 1] = 0;
346 end_idx++;
347 goto name11_12;
348 }
349 slotptr->name11_12[j] = name[*idx];
350 (*idx)++;
351 end_idx++;
352 }
353
354 if (name[*idx] == 0x00)
355 return 1;
356
357 return 0;
358/* Not used characters are filled with 0xff 0xff */
359name0_4:
360 for (; end_idx < 5; end_idx++) {
361 slotptr->name0_4[end_idx * 2] = 0xff;
362 slotptr->name0_4[end_idx * 2 + 1] = 0xff;
363 }
364 end_idx = 5;
365name5_10:
366 end_idx -= 5;
367 for (; end_idx < 6; end_idx++) {
368 slotptr->name5_10[end_idx * 2] = 0xff;
369 slotptr->name5_10[end_idx * 2 + 1] = 0xff;
370 }
371 end_idx = 11;
372name11_12:
373 end_idx -= 11;
374 for (; end_idx < 2; end_idx++) {
375 slotptr->name11_12[end_idx * 2] = 0xff;
376 slotptr->name11_12[end_idx * 2 + 1] = 0xff;
377 }
378
379 return 1;
380}
381
AKASHI Takahiroa2c0bd02019-05-24 14:10:36 +0900382static int flush_dir(fat_itr *itr);
Donggeun Kim8f814002011-10-24 21:15:28 +0000383
Heinrich Schuchardtac4ab752020-11-21 08:32:50 +0100384/**
385 * fill_dir_slot() - fill directory entries for long name
386 *
387 * @itr: directory iterator
388 * @l_name: long name
389 * @shortname: short name
390 * Return: 0 for success, -errno otherwise
Donggeun Kim8f814002011-10-24 21:15:28 +0000391 */
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900392static int
Heinrich Schuchardtac4ab752020-11-21 08:32:50 +0100393fill_dir_slot(fat_itr *itr, const char *l_name, const char *shortname)
Donggeun Kim8f814002011-10-24 21:15:28 +0000394{
Tien Fong Chee0117dbe2016-07-27 23:08:56 -0700395 __u8 temp_dir_slot_buffer[MAX_LFN_SLOT * sizeof(dir_slot)];
396 dir_slot *slotptr = (dir_slot *)temp_dir_slot_buffer;
Anatolij Gustschinf4f9d5d2011-12-15 03:12:14 +0000397 __u8 counter = 0, checksum;
Donggeun Kim8f814002011-10-24 21:15:28 +0000398 int idx = 0, ret;
Donggeun Kim8f814002011-10-24 21:15:28 +0000399
Stefan Brüns6c617d62016-09-11 22:51:39 +0200400 /* Get short file name checksum value */
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100401 checksum = mkcksum((void *)shortname);
Donggeun Kim8f814002011-10-24 21:15:28 +0000402
403 do {
404 memset(slotptr, 0x00, sizeof(dir_slot));
405 ret = str2slot(slotptr, l_name, &idx);
406 slotptr->id = ++counter;
407 slotptr->attr = ATTR_VFAT;
408 slotptr->alias_checksum = checksum;
409 slotptr++;
410 } while (ret == 0);
411
412 slotptr--;
413 slotptr->id |= LAST_LONG_ENTRY_MASK;
414
415 while (counter >= 1) {
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900416 memcpy(itr->dent, slotptr, sizeof(dir_slot));
Donggeun Kim8f814002011-10-24 21:15:28 +0000417 slotptr--;
418 counter--;
AKASHI Takahiroa2c0bd02019-05-24 14:10:36 +0900419
Heinrich Schuchardt913339a2021-01-20 22:21:53 +0100420 if (!itr->remaining) {
421 /* Write directory table to device */
422 ret = flush_dir(itr);
423 if (ret)
424 return ret;
425 }
AKASHI Takahiroa2c0bd02019-05-24 14:10:36 +0900426
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +0100427 next_dent(itr);
428 if (!itr->dent)
429 return -EIO;
Donggeun Kim8f814002011-10-24 21:15:28 +0000430 }
431
Donggeun Kim8f814002011-10-24 21:15:28 +0000432 return 0;
433}
434
Donggeun Kim8f814002011-10-24 21:15:28 +0000435/*
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500436 * Set the entry at index 'entry' in a FAT (12/16/32) table.
Donggeun Kim8f814002011-10-24 21:15:28 +0000437 */
438static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
439{
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500440 __u32 bufnum, offset, off16;
441 __u16 val1, val2;
Donggeun Kim8f814002011-10-24 21:15:28 +0000442
443 switch (mydata->fatsize) {
444 case 32:
445 bufnum = entry / FAT32BUFSIZE;
446 offset = entry - bufnum * FAT32BUFSIZE;
447 break;
448 case 16:
449 bufnum = entry / FAT16BUFSIZE;
450 offset = entry - bufnum * FAT16BUFSIZE;
451 break;
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500452 case 12:
453 bufnum = entry / FAT12BUFSIZE;
454 offset = entry - bufnum * FAT12BUFSIZE;
455 break;
Donggeun Kim8f814002011-10-24 21:15:28 +0000456 default:
457 /* Unsupported FAT size */
458 return -1;
459 }
460
461 /* Read a new block of FAT entries into the cache. */
462 if (bufnum != mydata->fatbufnum) {
463 int getsize = FATBUFBLOCKS;
464 __u8 *bufptr = mydata->fatbuf;
465 __u32 fatlength = mydata->fatlength;
466 __u32 startblock = bufnum * FATBUFBLOCKS;
467
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100468 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
469 if (startblock + getsize > fatlength)
470 getsize = fatlength - startblock;
Donggeun Kim8f814002011-10-24 21:15:28 +0000471
Stefan Brüns751b31d2016-09-11 22:51:40 +0200472 if (flush_dirty_fat_buffer(mydata) < 0)
473 return -1;
Donggeun Kim8f814002011-10-24 21:15:28 +0000474
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100475 startblock += mydata->fat_sect;
476
Donggeun Kim8f814002011-10-24 21:15:28 +0000477 if (disk_read(startblock, getsize, bufptr) < 0) {
478 debug("Error reading FAT blocks\n");
479 return -1;
480 }
481 mydata->fatbufnum = bufnum;
482 }
483
Stefan Brüns751b31d2016-09-11 22:51:40 +0200484 /* Mark as dirty */
485 mydata->fat_dirty = 1;
486
Donggeun Kim8f814002011-10-24 21:15:28 +0000487 /* Set the actual entry */
488 switch (mydata->fatsize) {
489 case 32:
490 ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
491 break;
492 case 16:
493 ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
494 break;
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500495 case 12:
496 off16 = (offset * 3) / 4;
497
498 switch (offset & 0x3) {
499 case 0:
500 val1 = cpu_to_le16(entry_value) & 0xfff;
501 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff;
502 ((__u16 *)mydata->fatbuf)[off16] |= val1;
503 break;
504 case 1:
505 val1 = cpu_to_le16(entry_value) & 0xf;
506 val2 = (cpu_to_le16(entry_value) >> 4) & 0xff;
507
508 ((__u16 *)mydata->fatbuf)[off16] &= ~0xf000;
509 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 12);
510
511 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xff;
512 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
513 break;
514 case 2:
515 val1 = cpu_to_le16(entry_value) & 0xff;
516 val2 = (cpu_to_le16(entry_value) >> 8) & 0xf;
517
518 ((__u16 *)mydata->fatbuf)[off16] &= ~0xff00;
519 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 8);
520
521 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xf;
522 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
523 break;
524 case 3:
525 val1 = cpu_to_le16(entry_value) & 0xfff;
526 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff0;
527 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 4);
528 break;
529 default:
530 break;
531 }
532
533 break;
Donggeun Kim8f814002011-10-24 21:15:28 +0000534 default:
535 return -1;
536 }
537
538 return 0;
539}
540
541/*
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500542 * Determine the next free cluster after 'entry' in a FAT (12/16/32) table
Stefan Brüns14999402016-09-11 22:51:41 +0200543 * and link it to 'entry'. EOC marker is not set on returned entry.
Donggeun Kim8f814002011-10-24 21:15:28 +0000544 */
545static __u32 determine_fatent(fsdata *mydata, __u32 entry)
546{
547 __u32 next_fat, next_entry = entry + 1;
548
549 while (1) {
Stefan Brüns9f95d812016-12-17 00:27:51 +0100550 next_fat = get_fatent(mydata, next_entry);
Donggeun Kim8f814002011-10-24 21:15:28 +0000551 if (next_fat == 0) {
Stefan Brüns14999402016-09-11 22:51:41 +0200552 /* found free entry, link to entry */
Donggeun Kim8f814002011-10-24 21:15:28 +0000553 set_fatent_value(mydata, entry, next_entry);
554 break;
555 }
556 next_entry++;
557 }
558 debug("FAT%d: entry: %08x, entry_value: %04x\n",
559 mydata->fatsize, entry, next_entry);
560
561 return next_entry;
562}
563
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200564/**
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900565 * set_sectors() - write data to sectors
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200566 *
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900567 * Write 'size' bytes from 'buffer' into the specified sector.
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200568 *
569 * @mydata: data to be written
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900570 * @startsect: sector to be written to
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200571 * @buffer: data to be written
572 * @size: bytes to be written (but not more than the size of a cluster)
573 * Return: 0 on success, -1 otherwise
Donggeun Kim8f814002011-10-24 21:15:28 +0000574 */
575static int
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900576set_sectors(fsdata *mydata, u32 startsect, u8 *buffer, u32 size)
Donggeun Kim8f814002011-10-24 21:15:28 +0000577{
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200578 int ret;
Donggeun Kim8f814002011-10-24 21:15:28 +0000579
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900580 debug("startsect: %d\n", startsect);
Donggeun Kim8f814002011-10-24 21:15:28 +0000581
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200582 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
583 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
584
Heinrich Schuchardtaa9d6ba2018-09-13 19:42:47 +0200585 debug("FAT: Misaligned buffer address (%p)\n", buffer);
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200586
587 while (size >= mydata->sect_size) {
588 memcpy(tmpbuf, buffer, mydata->sect_size);
589 ret = disk_write(startsect++, 1, tmpbuf);
590 if (ret != 1) {
591 debug("Error writing data (got %d)\n", ret);
592 return -1;
593 }
594
595 buffer += mydata->sect_size;
596 size -= mydata->sect_size;
597 }
598 } else if (size >= mydata->sect_size) {
Heinrich Schuchardteb3587b2021-01-26 00:14:14 +0100599 u32 nsects;
600
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900601 nsects = size / mydata->sect_size;
602 ret = disk_write(startsect, nsects, buffer);
603 if (ret != nsects) {
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200604 debug("Error writing data (got %d)\n", ret);
Wu, Joshb0e38dd2013-07-24 17:55:30 +0800605 return -1;
606 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000607
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900608 startsect += nsects;
609 buffer += nsects * mydata->sect_size;
610 size -= nsects * mydata->sect_size;
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200611 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000612
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200613 if (size) {
614 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200615 /* Do not leak content of stack */
616 memset(tmpbuf, 0, mydata->sect_size);
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200617 memcpy(tmpbuf, buffer, size);
618 ret = disk_write(startsect, 1, tmpbuf);
619 if (ret != 1) {
620 debug("Error writing data (got %d)\n", ret);
Donggeun Kim8f814002011-10-24 21:15:28 +0000621 return -1;
622 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000623 }
624
625 return 0;
626}
627
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900628/**
629 * set_cluster() - write data to cluster
630 *
631 * Write 'size' bytes from 'buffer' into the specified cluster.
632 *
633 * @mydata: data to be written
634 * @clustnum: cluster to be written to
635 * @buffer: data to be written
636 * @size: bytes to be written (but not more than the size of a cluster)
637 * Return: 0 on success, -1 otherwise
638 */
639static int
640set_cluster(fsdata *mydata, u32 clustnum, u8 *buffer, u32 size)
641{
642 return set_sectors(mydata, clust_to_sect(mydata, clustnum),
643 buffer, size);
644}
645
Heinrich Schuchardt913339a2021-01-20 22:21:53 +0100646/**
647 * flush_dir() - flush directory
648 *
649 * @itr: directory iterator
650 * Return: 0 for success, -EIO on error
651 */
652static int flush_dir(fat_itr *itr)
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900653{
654 fsdata *mydata = itr->fsdata;
655 u32 startsect, sect_offset, nsects;
Heinrich Schuchardt913339a2021-01-20 22:21:53 +0100656 int ret;
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900657
Heinrich Schuchardt913339a2021-01-20 22:21:53 +0100658 if (!itr->is_root || mydata->fatsize == 32) {
659 ret = set_cluster(mydata, itr->clust, itr->block,
660 mydata->clust_size * mydata->sect_size);
661 goto out;
662 }
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900663
664 sect_offset = itr->clust * mydata->clust_size;
665 startsect = mydata->rootdir_sect + sect_offset;
666 /* do not write past the end of rootdir */
667 nsects = min_t(u32, mydata->clust_size,
668 mydata->rootdir_size - sect_offset);
669
Heinrich Schuchardt913339a2021-01-20 22:21:53 +0100670 ret = set_sectors(mydata, startsect, itr->block,
671 nsects * mydata->sect_size);
672out:
673 if (ret) {
674 log_err("Error: writing directory entry\n");
675 return -EIO;
676 }
677 return 0;
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900678}
679
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900680/*
681 * Read and modify data on existing and consecutive cluster blocks
682 */
683static int
684get_set_cluster(fsdata *mydata, __u32 clustnum, loff_t pos, __u8 *buffer,
685 loff_t size, loff_t *gotsize)
686{
Heinrich Schuchardt8a3ac6e2020-07-06 07:48:14 +0200687 static u8 *tmpbuf_cluster;
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900688 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
689 __u32 startsect;
690 loff_t wsize;
691 int clustcount, i, ret;
692
693 *gotsize = 0;
694 if (!size)
695 return 0;
696
Heinrich Schuchardt8a3ac6e2020-07-06 07:48:14 +0200697 if (!tmpbuf_cluster) {
698 tmpbuf_cluster = memalign(ARCH_DMA_MINALIGN, MAX_CLUSTSIZE);
699 if (!tmpbuf_cluster)
700 return -1;
701 }
702
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900703 assert(pos < bytesperclust);
704 startsect = clust_to_sect(mydata, clustnum);
705
706 debug("clustnum: %d, startsect: %d, pos: %lld\n",
707 clustnum, startsect, pos);
708
709 /* partial write at beginning */
710 if (pos) {
711 wsize = min(bytesperclust - pos, size);
712 ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster);
713 if (ret != mydata->clust_size) {
714 debug("Error reading data (got %d)\n", ret);
715 return -1;
716 }
717
718 memcpy(tmpbuf_cluster + pos, buffer, wsize);
719 ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster);
720 if (ret != mydata->clust_size) {
721 debug("Error writing data (got %d)\n", ret);
722 return -1;
723 }
724
725 size -= wsize;
726 buffer += wsize;
727 *gotsize += wsize;
728
729 startsect += mydata->clust_size;
730
731 if (!size)
732 return 0;
733 }
734
735 /* full-cluster write */
736 if (size >= bytesperclust) {
737 clustcount = lldiv(size, bytesperclust);
738
739 if (!((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1))) {
740 wsize = clustcount * bytesperclust;
741 ret = disk_write(startsect,
742 clustcount * mydata->clust_size,
743 buffer);
744 if (ret != clustcount * mydata->clust_size) {
745 debug("Error writing data (got %d)\n", ret);
746 return -1;
747 }
748
749 size -= wsize;
750 buffer += wsize;
751 *gotsize += wsize;
752
753 startsect += clustcount * mydata->clust_size;
754 } else {
755 for (i = 0; i < clustcount; i++) {
756 memcpy(tmpbuf_cluster, buffer, bytesperclust);
757 ret = disk_write(startsect,
758 mydata->clust_size,
759 tmpbuf_cluster);
760 if (ret != mydata->clust_size) {
761 debug("Error writing data (got %d)\n",
762 ret);
763 return -1;
764 }
765
766 size -= bytesperclust;
767 buffer += bytesperclust;
768 *gotsize += bytesperclust;
769
770 startsect += mydata->clust_size;
771 }
772 }
773 }
774
775 /* partial write at end */
776 if (size) {
777 wsize = size;
778 ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster);
779 if (ret != mydata->clust_size) {
780 debug("Error reading data (got %d)\n", ret);
781 return -1;
782 }
783 memcpy(tmpbuf_cluster, buffer, wsize);
784 ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster);
785 if (ret != mydata->clust_size) {
786 debug("Error writing data (got %d)\n", ret);
787 return -1;
788 }
789
790 size -= wsize;
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900791 *gotsize += wsize;
792 }
793
794 assert(!size);
795
796 return 0;
797}
798
Donggeun Kim8f814002011-10-24 21:15:28 +0000799/*
800 * Find the first empty cluster
801 */
802static int find_empty_cluster(fsdata *mydata)
803{
804 __u32 fat_val, entry = 3;
805
806 while (1) {
Stefan Brüns9f95d812016-12-17 00:27:51 +0100807 fat_val = get_fatent(mydata, entry);
Donggeun Kim8f814002011-10-24 21:15:28 +0000808 if (fat_val == 0)
809 break;
810 entry++;
811 }
812
813 return entry;
814}
815
Heinrich Schuchardte09f3e42020-11-26 19:06:55 +0100816/**
817 * new_dir_table() - allocate a cluster for additional directory entries
818 *
819 * @itr: directory iterator
820 * Return: 0 on success, -EIO otherwise
Donggeun Kim8f814002011-10-24 21:15:28 +0000821 */
AKASHI Takahiroa2c0bd02019-05-24 14:10:36 +0900822static int new_dir_table(fat_itr *itr)
Donggeun Kim8f814002011-10-24 21:15:28 +0000823{
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900824 fsdata *mydata = itr->fsdata;
Donggeun Kim8f814002011-10-24 21:15:28 +0000825 int dir_newclust = 0;
Heinrich Schuchardte09f3e42020-11-26 19:06:55 +0100826 int dir_oldclust = itr->clust;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900827 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
Donggeun Kim8f814002011-10-24 21:15:28 +0000828
Donggeun Kim8f814002011-10-24 21:15:28 +0000829 dir_newclust = find_empty_cluster(mydata);
Heinrich Schuchardte09f3e42020-11-26 19:06:55 +0100830
831 /*
832 * Flush before updating FAT to ensure valid directory structure
833 * in case of failure.
834 */
835 itr->clust = dir_newclust;
836 itr->next_clust = dir_newclust;
837 memset(itr->block, 0x00, bytesperclust);
838 if (flush_dir(itr))
839 return -EIO;
840
841 set_fatent_value(mydata, dir_oldclust, dir_newclust);
Donggeun Kim8f814002011-10-24 21:15:28 +0000842 if (mydata->fatsize == 32)
843 set_fatent_value(mydata, dir_newclust, 0xffffff8);
844 else if (mydata->fatsize == 16)
845 set_fatent_value(mydata, dir_newclust, 0xfff8);
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500846 else if (mydata->fatsize == 12)
847 set_fatent_value(mydata, dir_newclust, 0xff8);
Donggeun Kim8f814002011-10-24 21:15:28 +0000848
Stefan Brüns751b31d2016-09-11 22:51:40 +0200849 if (flush_dirty_fat_buffer(mydata) < 0)
Heinrich Schuchardte09f3e42020-11-26 19:06:55 +0100850 return -EIO;
Donggeun Kim8f814002011-10-24 21:15:28 +0000851
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900852 itr->dent = (dir_entry *)itr->block;
853 itr->last_cluster = 1;
854 itr->remaining = bytesperclust / sizeof(dir_entry) - 1;
Donggeun Kim8f814002011-10-24 21:15:28 +0000855
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900856 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +0000857}
858
859/*
860 * Set empty cluster from 'entry' to the end of a file
861 */
862static int clear_fatent(fsdata *mydata, __u32 entry)
863{
864 __u32 fat_val;
865
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500866 while (!CHECK_CLUST(entry, mydata->fatsize)) {
Stefan Brüns9f95d812016-12-17 00:27:51 +0100867 fat_val = get_fatent(mydata, entry);
Donggeun Kim8f814002011-10-24 21:15:28 +0000868 if (fat_val != 0)
869 set_fatent_value(mydata, entry, 0);
870 else
871 break;
872
Donggeun Kim8f814002011-10-24 21:15:28 +0000873 entry = fat_val;
874 }
875
876 /* Flush fat buffer */
Stefan Brüns751b31d2016-09-11 22:51:40 +0200877 if (flush_dirty_fat_buffer(mydata) < 0)
Donggeun Kim8f814002011-10-24 21:15:28 +0000878 return -1;
879
880 return 0;
881}
882
883/*
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +0900884 * Set start cluster in directory entry
885 */
886static void set_start_cluster(const fsdata *mydata, dir_entry *dentptr,
887 __u32 start_cluster)
888{
889 if (mydata->fatsize == 32)
890 dentptr->starthi =
891 cpu_to_le16((start_cluster & 0xffff0000) >> 16);
892 dentptr->start = cpu_to_le16(start_cluster & 0xffff);
893}
894
895/*
896 * Check whether adding a file makes the file system to
897 * exceed the size of the block device
898 * Return -1 when overflow occurs, otherwise return 0
899 */
900static int check_overflow(fsdata *mydata, __u32 clustnum, loff_t size)
901{
902 __u32 startsect, sect_num, offset;
903
904 if (clustnum > 0)
905 startsect = clust_to_sect(mydata, clustnum);
906 else
907 startsect = mydata->rootdir_sect;
908
909 sect_num = div_u64_rem(size, mydata->sect_size, &offset);
910
911 if (offset != 0)
912 sect_num++;
913
914 if (startsect + sect_num > total_sector)
915 return -1;
916 return 0;
917}
918
919/*
Donggeun Kim8f814002011-10-24 21:15:28 +0000920 * Write at most 'maxsize' bytes from 'buffer' into
921 * the file associated with 'dentptr'
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800922 * Update the number of bytes written in *gotsize and return 0
923 * or return -1 on fatal errors.
Donggeun Kim8f814002011-10-24 21:15:28 +0000924 */
925static int
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +0900926set_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, __u8 *buffer,
927 loff_t maxsize, loff_t *gotsize)
Donggeun Kim8f814002011-10-24 21:15:28 +0000928{
Donggeun Kim8f814002011-10-24 21:15:28 +0000929 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
930 __u32 curclust = START(dentptr);
931 __u32 endclust = 0, newclust = 0;
Heinrich Schuchardtdb538a92019-02-25 19:42:48 +0100932 u64 cur_pos, filesize;
933 loff_t offset, actsize, wsize;
Donggeun Kim8f814002011-10-24 21:15:28 +0000934
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800935 *gotsize = 0;
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900936 filesize = pos + maxsize;
Donggeun Kim8f814002011-10-24 21:15:28 +0000937
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800938 debug("%llu bytes\n", filesize);
Donggeun Kim8f814002011-10-24 21:15:28 +0000939
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900940 if (!filesize) {
941 if (!curclust)
942 return 0;
943 if (!CHECK_CLUST(curclust, mydata->fatsize) ||
944 IS_LAST_CLUST(curclust, mydata->fatsize)) {
945 clear_fatent(mydata, curclust);
946 set_start_cluster(mydata, dentptr, 0);
947 return 0;
948 }
949 debug("curclust: 0x%x\n", curclust);
950 debug("Invalid FAT entry\n");
951 return -1;
952 }
953
954 if (!curclust) {
955 assert(pos == 0);
956 goto set_clusters;
957 }
958
959 /* go to cluster at pos */
960 cur_pos = bytesperclust;
961 while (1) {
962 if (pos <= cur_pos)
963 break;
964 if (IS_LAST_CLUST(curclust, mydata->fatsize))
965 break;
966
967 newclust = get_fatent(mydata, curclust);
968 if (!IS_LAST_CLUST(newclust, mydata->fatsize) &&
969 CHECK_CLUST(newclust, mydata->fatsize)) {
970 debug("curclust: 0x%x\n", curclust);
971 debug("Invalid FAT entry\n");
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +0200972 return -1;
973 }
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900974
975 cur_pos += bytesperclust;
976 curclust = newclust;
977 }
978 if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
979 assert(pos == cur_pos);
980 goto set_clusters;
981 }
982
983 assert(pos < cur_pos);
984 cur_pos -= bytesperclust;
985
986 /* overwrite */
987 assert(IS_LAST_CLUST(curclust, mydata->fatsize) ||
988 !CHECK_CLUST(curclust, mydata->fatsize));
989
990 while (1) {
991 /* search for allocated consecutive clusters */
992 actsize = bytesperclust;
993 endclust = curclust;
994 while (1) {
995 if (filesize <= (cur_pos + actsize))
996 break;
997
998 newclust = get_fatent(mydata, endclust);
999
Marek Szyprowski2f241672019-12-02 12:11:13 +01001000 if (newclust != endclust + 1)
1001 break;
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001002 if (IS_LAST_CLUST(newclust, mydata->fatsize))
1003 break;
1004 if (CHECK_CLUST(newclust, mydata->fatsize)) {
1005 debug("curclust: 0x%x\n", curclust);
1006 debug("Invalid FAT entry\n");
1007 return -1;
1008 }
1009
1010 actsize += bytesperclust;
1011 endclust = newclust;
1012 }
1013
1014 /* overwrite to <curclust..endclust> */
1015 if (pos < cur_pos)
1016 offset = 0;
1017 else
1018 offset = pos - cur_pos;
Marek Szyprowski3a867ce2019-12-02 12:11:14 +01001019 wsize = min_t(unsigned long long, actsize, filesize - cur_pos);
1020 wsize -= offset;
1021
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001022 if (get_set_cluster(mydata, curclust, offset,
1023 buffer, wsize, &actsize)) {
1024 printf("Error get-and-setting cluster\n");
1025 return -1;
1026 }
1027 buffer += wsize;
1028 *gotsize += wsize;
1029 cur_pos += offset + wsize;
1030
1031 if (filesize <= cur_pos)
1032 break;
1033
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001034 if (IS_LAST_CLUST(newclust, mydata->fatsize))
1035 /* no more clusters */
1036 break;
1037
1038 curclust = newclust;
1039 }
1040
1041 if (filesize <= cur_pos) {
1042 /* no more write */
1043 newclust = get_fatent(mydata, endclust);
1044 if (!IS_LAST_CLUST(newclust, mydata->fatsize)) {
1045 /* truncate the rest */
1046 clear_fatent(mydata, newclust);
1047
1048 /* Mark end of file in FAT */
1049 if (mydata->fatsize == 12)
1050 newclust = 0xfff;
1051 else if (mydata->fatsize == 16)
1052 newclust = 0xffff;
1053 else if (mydata->fatsize == 32)
1054 newclust = 0xfffffff;
1055 set_fatent_value(mydata, endclust, newclust);
1056 }
1057
1058 return 0;
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001059 }
1060
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001061 curclust = endclust;
1062 filesize -= cur_pos;
Heinrich Schuchardtdb538a92019-02-25 19:42:48 +01001063 assert(!do_div(cur_pos, bytesperclust));
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001064
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001065set_clusters:
1066 /* allocate and write */
1067 assert(!pos);
1068
1069 /* Assure that curclust is valid */
1070 if (!curclust) {
1071 curclust = find_empty_cluster(mydata);
1072 set_start_cluster(mydata, dentptr, curclust);
1073 } else {
1074 newclust = get_fatent(mydata, curclust);
1075
1076 if (IS_LAST_CLUST(newclust, mydata->fatsize)) {
1077 newclust = determine_fatent(mydata, curclust);
1078 set_fatent_value(mydata, curclust, newclust);
1079 curclust = newclust;
1080 } else {
1081 debug("error: something wrong\n");
1082 return -1;
1083 }
1084 }
1085
1086 /* TODO: already partially written */
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001087 if (check_overflow(mydata, curclust, filesize)) {
1088 printf("Error: no space left: %llu\n", filesize);
1089 return -1;
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +02001090 }
1091
Donggeun Kim8f814002011-10-24 21:15:28 +00001092 actsize = bytesperclust;
1093 endclust = curclust;
1094 do {
1095 /* search for consecutive clusters */
1096 while (actsize < filesize) {
1097 newclust = determine_fatent(mydata, endclust);
1098
1099 if ((newclust - 1) != endclust)
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001100 /* write to <curclust..endclust> */
Donggeun Kim8f814002011-10-24 21:15:28 +00001101 goto getit;
1102
1103 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Benoît Thébaudeaue0b86942015-09-28 15:45:30 +02001104 debug("newclust: 0x%x\n", newclust);
Donggeun Kim8f814002011-10-24 21:15:28 +00001105 debug("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001106 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +00001107 }
1108 endclust = newclust;
1109 actsize += bytesperclust;
1110 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001111
1112 /* set remaining bytes */
Donggeun Kim8f814002011-10-24 21:15:28 +00001113 actsize = filesize;
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
1120 /* Mark end of file in FAT */
Philipp Skadorov16f553d2016-12-15 15:52:53 -05001121 if (mydata->fatsize == 12)
1122 newclust = 0xfff;
1123 else if (mydata->fatsize == 16)
Donggeun Kim8f814002011-10-24 21:15:28 +00001124 newclust = 0xffff;
1125 else if (mydata->fatsize == 32)
1126 newclust = 0xfffffff;
1127 set_fatent_value(mydata, endclust, newclust);
1128
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001129 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +00001130getit:
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +02001131 if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
Donggeun Kim8f814002011-10-24 21:15:28 +00001132 debug("error: writing cluster\n");
1133 return -1;
1134 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001135 *gotsize += actsize;
Donggeun Kim8f814002011-10-24 21:15:28 +00001136 filesize -= actsize;
1137 buffer += actsize;
1138
Benoît Thébaudeaue0b86942015-09-28 15:45:30 +02001139 if (CHECK_CLUST(newclust, mydata->fatsize)) {
1140 debug("newclust: 0x%x\n", newclust);
Donggeun Kim8f814002011-10-24 21:15:28 +00001141 debug("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001142 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +00001143 }
1144 actsize = bytesperclust;
1145 curclust = endclust = newclust;
1146 } while (1);
Donggeun Kim8f814002011-10-24 21:15:28 +00001147
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001148 return 0;
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +02001149}
1150
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001151/**
1152 * fill_dentry() - fill directory entry with shortname
1153 *
1154 * @mydata: private filesystem parameters
1155 * @dentptr: directory entry
1156 * @shortname: chosen short name
1157 * @start_cluster: first cluster of file
1158 * @size: file size
1159 * @attr: file attributes
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +02001160 */
1161static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001162 const char *shortname, __u32 start_cluster, __u32 size, __u8 attr)
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +02001163{
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001164 memset(dentptr, 0, sizeof(*dentptr));
1165
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +02001166 set_start_cluster(mydata, dentptr, start_cluster);
Donggeun Kim8f814002011-10-24 21:15:28 +00001167 dentptr->size = cpu_to_le32(size);
1168
1169 dentptr->attr = attr;
1170
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +01001171 memcpy(&dentptr->nameext, shortname, SHORT_NAME_SIZE);
Donggeun Kim8f814002011-10-24 21:15:28 +00001172}
1173
Heinrich Schuchardt7a3f6102020-11-26 16:10:01 +01001174/**
1175 * find_directory_entry() - find a directory entry by filename
1176 *
1177 * @itr: directory iterator
1178 * @filename: name of file to find
1179 * Return: directory entry or NULL
Donggeun Kim8f814002011-10-24 21:15:28 +00001180 */
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001181static dir_entry *find_directory_entry(fat_itr *itr, char *filename)
Donggeun Kim8f814002011-10-24 21:15:28 +00001182{
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001183 int match = 0;
Donggeun Kim8f814002011-10-24 21:15:28 +00001184
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001185 while (fat_itr_next(itr)) {
1186 /* check both long and short name: */
1187 if (!strcasecmp(filename, itr->name))
1188 match = 1;
1189 else if (itr->name != itr->s_name &&
1190 !strcasecmp(filename, itr->s_name))
1191 match = 1;
Donggeun Kim8f814002011-10-24 21:15:28 +00001192
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001193 if (!match)
1194 continue;
Donggeun Kim8f814002011-10-24 21:15:28 +00001195
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +01001196 if (itr->dent->nameext.name[0] == '\0')
Donggeun Kim8f814002011-10-24 21:15:28 +00001197 return NULL;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001198 else
1199 return itr->dent;
1200 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001201
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001202 return NULL;
1203}
Donggeun Kim8f814002011-10-24 21:15:28 +00001204
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001205static int split_filename(char *filename, char **dirname, char **basename)
1206{
1207 char *p, *last_slash, *last_slash_cont;
Donggeun Kim8f814002011-10-24 21:15:28 +00001208
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001209again:
1210 p = filename;
1211 last_slash = NULL;
1212 last_slash_cont = NULL;
1213 while (*p) {
1214 if (ISDIRDELIM(*p)) {
1215 last_slash = p;
1216 last_slash_cont = p;
1217 /* continuous slashes */
1218 while (ISDIRDELIM(*p))
1219 last_slash_cont = p++;
1220 if (!*p)
1221 break;
Donggeun Kim8f814002011-10-24 21:15:28 +00001222 }
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001223 p++;
1224 }
Wu, Josh675b23c2014-05-08 16:14:07 +08001225
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001226 if (last_slash) {
1227 if (last_slash_cont == (filename + strlen(filename) - 1)) {
1228 /* remove trailing slashes */
1229 *last_slash = '\0';
1230 goto again;
Wu, Josh675b23c2014-05-08 16:14:07 +08001231 }
1232
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001233 if (last_slash == filename) {
1234 /* avoid ""(null) directory */
1235 *dirname = "/";
1236 } else {
1237 *last_slash = '\0';
1238 *dirname = filename;
Donggeun Kim8f814002011-10-24 21:15:28 +00001239 }
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001240
1241 *last_slash_cont = '\0';
Heinrich Schuchardt2e1b17b2021-01-30 14:12:10 +01001242 filename = last_slash_cont + 1;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001243 } else {
1244 *dirname = "/"; /* root by default */
Heinrich Schuchardt2e1b17b2021-01-30 14:12:10 +01001245 }
1246
1247 /*
1248 * The FAT32 File System Specification v1.03 requires leading and
1249 * trailing spaces as well as trailing periods to be ignored.
1250 */
1251 for (; *filename == ' '; ++filename)
1252 ;
1253
1254 /* Keep special entries '.' and '..' */
1255 if (filename[0] == '.' &&
1256 (!filename[1] || (filename[1] == '.' && !filename[2])))
1257 goto done;
1258
1259 /* Remove trailing periods and spaces */
1260 for (p = filename + strlen(filename) - 1; p >= filename; --p) {
1261 switch (*p) {
1262 case ' ':
1263 case '.':
1264 *p = 0;
1265 break;
1266 default:
1267 goto done;
1268 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001269 }
1270
Heinrich Schuchardt2e1b17b2021-01-30 14:12:10 +01001271done:
1272 *basename = filename;
1273
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001274 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +00001275}
1276
Heinrich Schuchardt58587212019-05-12 09:59:18 +02001277/**
1278 * normalize_longname() - check long file name and convert to lower case
1279 *
1280 * We assume here that the FAT file system is using an 8bit code page.
1281 * Linux typically uses CP437, EDK2 assumes CP1250.
1282 *
1283 * @l_filename: preallocated buffer receiving the normalized name
1284 * @filename: filename to normalize
1285 * Return: 0 on success, -1 on failure
1286 */
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001287static int normalize_longname(char *l_filename, const char *filename)
1288{
Heinrich Schuchardt58587212019-05-12 09:59:18 +02001289 const char *p, illegal[] = "<>:\"/\\|?*";
Heinrich Schuchardtb36d5462021-01-30 11:08:21 +01001290 size_t len;
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001291
Heinrich Schuchardtb36d5462021-01-30 11:08:21 +01001292 len = strlen(filename);
1293 if (!len || len >= VFAT_MAXLEN_BYTES || filename[len - 1] == '.')
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001294 return -1;
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001295
Heinrich Schuchardt58587212019-05-12 09:59:18 +02001296 for (p = filename; *p; ++p) {
1297 if ((unsigned char)*p < 0x20)
1298 return -1;
1299 if (strchr(illegal, *p))
1300 return -1;
1301 }
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001302
Heinrich Schuchardt58587212019-05-12 09:59:18 +02001303 strcpy(l_filename, filename);
1304 downcase(l_filename, VFAT_MAXLEN_BYTES);
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001305
1306 return 0;
1307}
1308
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001309int file_fat_write_at(const char *filename, loff_t pos, void *buffer,
1310 loff_t size, loff_t *actwrite)
Donggeun Kim8f814002011-10-24 21:15:28 +00001311{
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001312 dir_entry *retdent;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001313 fsdata datablock = { .fatbuf = NULL, };
Donggeun Kim8f814002011-10-24 21:15:28 +00001314 fsdata *mydata = &datablock;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001315 fat_itr *itr = NULL;
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001316 int ret = -1;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001317 char *filename_copy, *parent, *basename;
Donggeun Kim8f814002011-10-24 21:15:28 +00001318 char l_filename[VFAT_MAXLEN_BYTES];
1319
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001320 debug("writing %s\n", filename);
1321
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001322 filename_copy = strdup(filename);
1323 if (!filename_copy)
1324 return -ENOMEM;
Donggeun Kim8f814002011-10-24 21:15:28 +00001325
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001326 split_filename(filename_copy, &parent, &basename);
1327 if (!strlen(basename)) {
1328 ret = -EINVAL;
1329 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001330 }
1331
Heinrich Schuchardt502cb892021-01-30 10:01:08 +01001332 if (normalize_longname(l_filename, basename)) {
1333 printf("FAT: illegal filename (%s)\n", basename);
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001334 ret = -EINVAL;
1335 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001336 }
1337
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001338 itr = malloc_cache_aligned(sizeof(fat_itr));
1339 if (!itr) {
1340 ret = -ENOMEM;
1341 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001342 }
1343
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001344 ret = fat_itr_root(itr, &datablock);
1345 if (ret)
Donggeun Kim8f814002011-10-24 21:15:28 +00001346 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001347
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001348 total_sector = datablock.total_sect;
1349
1350 ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1351 if (ret) {
1352 printf("%s: doesn't exist (%d)\n", parent, ret);
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001353 goto exit;
1354 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001355
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001356 retdent = find_directory_entry(itr, l_filename);
1357
Donggeun Kim8f814002011-10-24 21:15:28 +00001358 if (retdent) {
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001359 if (fat_itr_isdir(itr)) {
1360 ret = -EISDIR;
1361 goto exit;
1362 }
1363
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001364 /* A file exists */
1365 if (pos == -1)
1366 /* Append to the end */
1367 pos = FAT2CPU32(retdent->size);
1368 if (pos > retdent->size) {
1369 /* No hole allowed */
1370 ret = -EINVAL;
1371 goto exit;
1372 }
1373
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001374 /* Update file size in a directory entry */
1375 retdent->size = cpu_to_le32(pos + size);
Donggeun Kim8f814002011-10-24 21:15:28 +00001376 } else {
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001377 /* Create a new file */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001378 char shortname[SHORT_NAME_SIZE];
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +01001379 int ndent;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001380
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001381 if (pos) {
1382 /* No hole allowed */
1383 ret = -EINVAL;
1384 goto exit;
1385 }
1386
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001387 /* Check if long name is needed */
Heinrich Schuchardt502cb892021-01-30 10:01:08 +01001388 ndent = set_name(itr, basename, shortname);
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +01001389 if (ndent < 0) {
1390 ret = ndent;
1391 goto exit;
1392 }
1393 ret = fat_find_empty_dentries(itr, ndent);
1394 if (ret)
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001395 goto exit;
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +01001396 if (ndent > 1) {
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001397 /* Set long name entries */
Heinrich Schuchardt502cb892021-01-30 10:01:08 +01001398 ret = fill_dir_slot(itr, basename, shortname);
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001399 if (ret)
1400 goto exit;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001401 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001402
AKASHI Takahiroa2c0bd02019-05-24 14:10:36 +09001403 /* Set short name entry */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001404 fill_dentry(itr->fsdata, itr->dent, shortname, 0, size,
Heinrich Schuchardt881c74a2020-11-22 11:13:33 +01001405 ATTR_ARCH);
Donggeun Kim8f814002011-10-24 21:15:28 +00001406
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001407 retdent = itr->dent;
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001408 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001409
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001410 ret = set_contents(mydata, retdent, pos, buffer, size, actwrite);
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001411 if (ret < 0) {
1412 printf("Error: writing contents\n");
AKASHI Takahiro6aa77692018-09-11 15:59:03 +09001413 ret = -EIO;
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001414 goto exit;
1415 }
1416 debug("attempt to write 0x%llx bytes\n", *actwrite);
Donggeun Kim8f814002011-10-24 21:15:28 +00001417
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001418 /* Flush fat buffer */
Stefan Brüns751b31d2016-09-11 22:51:40 +02001419 ret = flush_dirty_fat_buffer(mydata);
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001420 if (ret) {
1421 printf("Error: flush fat buffer\n");
AKASHI Takahiro6aa77692018-09-11 15:59:03 +09001422 ret = -EIO;
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001423 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001424 }
1425
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001426 /* Write directory table to device */
AKASHI Takahirod98c6742019-05-24 14:10:35 +09001427 ret = flush_dir(itr);
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001428
Donggeun Kim8f814002011-10-24 21:15:28 +00001429exit:
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001430 free(filename_copy);
Donggeun Kim8f814002011-10-24 21:15:28 +00001431 free(mydata->fatbuf);
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001432 free(itr);
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001433 return ret;
Donggeun Kim8f814002011-10-24 21:15:28 +00001434}
1435
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001436int file_fat_write(const char *filename, void *buffer, loff_t offset,
1437 loff_t maxsize, loff_t *actwrite)
Donggeun Kim8f814002011-10-24 21:15:28 +00001438{
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001439 return file_fat_write_at(filename, offset, buffer, maxsize, actwrite);
Donggeun Kim8f814002011-10-24 21:15:28 +00001440}
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001441
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001442static int fat_dir_entries(fat_itr *itr)
1443{
1444 fat_itr *dirs;
1445 fsdata fsdata = { .fatbuf = NULL, }, *mydata = &fsdata;
1446 /* for FATBUFSIZE */
1447 int count;
1448
1449 dirs = malloc_cache_aligned(sizeof(fat_itr));
1450 if (!dirs) {
1451 debug("Error: allocating memory\n");
1452 count = -ENOMEM;
1453 goto exit;
1454 }
1455
1456 /* duplicate fsdata */
1457 fat_itr_child(dirs, itr);
1458 fsdata = *dirs->fsdata;
1459
1460 /* allocate local fat buffer */
1461 fsdata.fatbuf = malloc_cache_aligned(FATBUFSIZE);
1462 if (!fsdata.fatbuf) {
1463 debug("Error: allocating memory\n");
1464 count = -ENOMEM;
1465 goto exit;
1466 }
1467 fsdata.fatbufnum = -1;
1468 dirs->fsdata = &fsdata;
1469
1470 for (count = 0; fat_itr_next(dirs); count++)
1471 ;
1472
1473exit:
1474 free(fsdata.fatbuf);
1475 free(dirs);
1476 return count;
1477}
1478
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001479/**
1480 * delete_single_dentry() - delete a single directory entry
1481 *
1482 * @itr: directory iterator
1483 * Return: 0 for success
1484 */
1485static int delete_single_dentry(fat_itr *itr)
1486{
1487 struct dir_entry *dent = itr->dent;
1488
1489 memset(dent, 0, sizeof(*dent));
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +01001490 dent->nameext.name[0] = DELETED_FLAG;
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001491
Heinrich Schuchardt913339a2021-01-20 22:21:53 +01001492 if (!itr->remaining)
1493 return flush_dir(itr);
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001494 return 0;
1495}
1496
1497/**
1498 * delete_long_name() - delete long name directory entries
1499 *
1500 * @itr: directory iterator
1501 * Return: 0 for success
1502 */
1503static int delete_long_name(fat_itr *itr)
1504{
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +01001505 int seqn = itr->dent->nameext.name[0] & ~LAST_LONG_ENTRY_MASK;
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001506
1507 while (seqn--) {
Heinrich Schuchardteb3587b2021-01-26 00:14:14 +01001508 struct dir_entry *dent;
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001509 int ret;
1510
1511 ret = delete_single_dentry(itr);
1512 if (ret)
1513 return ret;
1514 dent = next_dent(itr);
1515 if (!dent)
1516 return -EIO;
1517 }
1518 return 0;
1519}
1520
1521/**
1522 * delete_dentry_long() - remove directory entry
1523 *
1524 * @itr: directory iterator
1525 * Return: 0 for success
1526 */
1527static int delete_dentry_long(fat_itr *itr)
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001528{
1529 fsdata *mydata = itr->fsdata;
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001530 dir_entry *dent = itr->dent;
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001531
1532 /* free cluster blocks */
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001533 clear_fatent(mydata, START(dent));
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001534 if (flush_dirty_fat_buffer(mydata) < 0) {
1535 printf("Error: flush fat buffer\n");
1536 return -EIO;
1537 }
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001538 /* Position to first directory entry for long name */
1539 if (itr->clust != itr->dent_clust) {
1540 int ret;
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001541
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001542 ret = fat_move_to_cluster(itr, itr->dent_clust);
1543 if (ret)
1544 return ret;
1545 }
1546 itr->dent = itr->dent_start;
1547 itr->remaining = itr->dent_rem;
1548 dent = itr->dent_start;
1549 /* Delete long name */
1550 if ((dent->attr & ATTR_VFAT) == ATTR_VFAT &&
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +01001551 (dent->nameext.name[0] & LAST_LONG_ENTRY_MASK)) {
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001552 int ret;
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001553
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001554 ret = delete_long_name(itr);
1555 if (ret)
1556 return ret;
1557 }
1558 /* Delete short name */
1559 delete_single_dentry(itr);
Heinrich Schuchardt913339a2021-01-20 22:21:53 +01001560 return flush_dir(itr);
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001561}
1562
1563int fat_unlink(const char *filename)
1564{
1565 fsdata fsdata = { .fatbuf = NULL, };
1566 fat_itr *itr = NULL;
1567 int n_entries, ret;
1568 char *filename_copy, *dirname, *basename;
1569
1570 filename_copy = strdup(filename);
Heinrich Schuchardtb2242f62018-10-02 06:58:00 +02001571 if (!filename_copy) {
1572 printf("Error: allocating memory\n");
1573 ret = -ENOMEM;
1574 goto exit;
1575 }
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001576 split_filename(filename_copy, &dirname, &basename);
1577
1578 if (!strcmp(dirname, "/") && !strcmp(basename, "")) {
1579 printf("Error: cannot remove root\n");
1580 ret = -EINVAL;
1581 goto exit;
1582 }
1583
1584 itr = malloc_cache_aligned(sizeof(fat_itr));
1585 if (!itr) {
1586 printf("Error: allocating memory\n");
Heinrich Schuchardtb2242f62018-10-02 06:58:00 +02001587 ret = -ENOMEM;
1588 goto exit;
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001589 }
1590
1591 ret = fat_itr_root(itr, &fsdata);
1592 if (ret)
1593 goto exit;
1594
1595 total_sector = fsdata.total_sect;
1596
1597 ret = fat_itr_resolve(itr, dirname, TYPE_DIR);
1598 if (ret) {
1599 printf("%s: doesn't exist (%d)\n", dirname, ret);
1600 ret = -ENOENT;
1601 goto exit;
1602 }
1603
1604 if (!find_directory_entry(itr, basename)) {
1605 printf("%s: doesn't exist\n", basename);
1606 ret = -ENOENT;
1607 goto exit;
1608 }
1609
1610 if (fat_itr_isdir(itr)) {
1611 n_entries = fat_dir_entries(itr);
1612 if (n_entries < 0) {
1613 ret = n_entries;
1614 goto exit;
1615 }
1616 if (n_entries > 2) {
1617 printf("Error: directory is not empty: %d\n",
1618 n_entries);
1619 ret = -EINVAL;
1620 goto exit;
1621 }
1622 }
1623
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001624 ret = delete_dentry_long(itr);
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001625
1626exit:
1627 free(fsdata.fatbuf);
1628 free(itr);
1629 free(filename_copy);
1630
1631 return ret;
1632}
1633
Heinrich Schuchardt502cb892021-01-30 10:01:08 +01001634int fat_mkdir(const char *dirname)
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001635{
1636 dir_entry *retdent;
1637 fsdata datablock = { .fatbuf = NULL, };
1638 fsdata *mydata = &datablock;
1639 fat_itr *itr = NULL;
Heinrich Schuchardt502cb892021-01-30 10:01:08 +01001640 char *dirname_copy, *parent, *basename;
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001641 char l_dirname[VFAT_MAXLEN_BYTES];
1642 int ret = -1;
1643 loff_t actwrite;
1644 unsigned int bytesperclust;
1645 dir_entry *dotdent = NULL;
1646
Heinrich Schuchardt502cb892021-01-30 10:01:08 +01001647 dirname_copy = strdup(dirname);
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001648 if (!dirname_copy)
1649 goto exit;
1650
Heinrich Schuchardt502cb892021-01-30 10:01:08 +01001651 split_filename(dirname_copy, &parent, &basename);
1652 if (!strlen(basename)) {
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001653 ret = -EINVAL;
1654 goto exit;
1655 }
1656
Heinrich Schuchardt502cb892021-01-30 10:01:08 +01001657 if (normalize_longname(l_dirname, basename)) {
1658 printf("FAT: illegal filename (%s)\n", basename);
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001659 ret = -EINVAL;
1660 goto exit;
1661 }
1662
1663 itr = malloc_cache_aligned(sizeof(fat_itr));
1664 if (!itr) {
1665 ret = -ENOMEM;
1666 goto exit;
1667 }
1668
1669 ret = fat_itr_root(itr, &datablock);
1670 if (ret)
1671 goto exit;
1672
1673 total_sector = datablock.total_sect;
1674
1675 ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1676 if (ret) {
1677 printf("%s: doesn't exist (%d)\n", parent, ret);
1678 goto exit;
1679 }
1680
1681 retdent = find_directory_entry(itr, l_dirname);
1682
1683 if (retdent) {
1684 printf("%s: already exists\n", l_dirname);
1685 ret = -EEXIST;
1686 goto exit;
1687 } else {
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001688 char shortname[SHORT_NAME_SIZE];
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +01001689 int ndent;
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001690
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001691 if (itr->is_root) {
1692 /* root dir cannot have "." or ".." */
1693 if (!strcmp(l_dirname, ".") ||
1694 !strcmp(l_dirname, "..")) {
1695 ret = -EINVAL;
1696 goto exit;
1697 }
1698 }
1699
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001700 /* Check if long name is needed */
Heinrich Schuchardt502cb892021-01-30 10:01:08 +01001701 ndent = set_name(itr, basename, shortname);
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +01001702 if (ndent < 0) {
1703 ret = ndent;
1704 goto exit;
1705 }
1706 ret = fat_find_empty_dentries(itr, ndent);
1707 if (ret)
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001708 goto exit;
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +01001709 if (ndent > 1) {
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001710 /* Set long name entries */
Heinrich Schuchardt502cb892021-01-30 10:01:08 +01001711 ret = fill_dir_slot(itr, basename, shortname);
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001712 if (ret)
1713 goto exit;
1714 }
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001715
1716 /* Set attribute as archive for regular file */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001717 fill_dentry(itr->fsdata, itr->dent, shortname, 0, 0,
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001718 ATTR_DIR | ATTR_ARCH);
1719
1720 retdent = itr->dent;
1721 }
1722
1723 /* Default entries */
1724 bytesperclust = mydata->clust_size * mydata->sect_size;
1725 dotdent = malloc_cache_aligned(bytesperclust);
1726 if (!dotdent) {
1727 ret = -ENOMEM;
1728 goto exit;
1729 }
1730 memset(dotdent, 0, bytesperclust);
1731
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +01001732 memcpy(&dotdent[0].nameext, ". ", 11);
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001733 dotdent[0].attr = ATTR_DIR | ATTR_ARCH;
1734
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +01001735 memcpy(&dotdent[1].nameext, ".. ", 11);
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001736 dotdent[1].attr = ATTR_DIR | ATTR_ARCH;
Heinrich Schuchardt00cf0762020-11-24 21:04:07 +01001737
1738 if (itr->is_root)
1739 set_start_cluster(mydata, &dotdent[1], 0);
1740 else
1741 set_start_cluster(mydata, &dotdent[1], itr->start_clust);
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001742
1743 ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
1744 bytesperclust, &actwrite);
1745 if (ret < 0) {
1746 printf("Error: writing contents\n");
1747 goto exit;
1748 }
1749 /* Write twice for "." */
1750 set_start_cluster(mydata, &dotdent[0], START(retdent));
1751 ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
1752 bytesperclust, &actwrite);
1753 if (ret < 0) {
1754 printf("Error: writing contents\n");
1755 goto exit;
1756 }
1757
1758 /* Flush fat buffer */
1759 ret = flush_dirty_fat_buffer(mydata);
1760 if (ret) {
1761 printf("Error: flush fat buffer\n");
Heinrich Schuchardt913339a2021-01-20 22:21:53 +01001762 ret = -EIO;
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001763 goto exit;
1764 }
1765
1766 /* Write directory table to device */
AKASHI Takahirod98c6742019-05-24 14:10:35 +09001767 ret = flush_dir(itr);
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001768
1769exit:
1770 free(dirname_copy);
1771 free(mydata->fatbuf);
1772 free(itr);
1773 free(dotdent);
1774 return ret;
1775}