blob: aae3a6a3d18ccfd567d45ff6c8016d9c59c4c5d2 [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;
Heinrich Schuchardtcfb7c2b2021-01-26 00:04:19 +0100111 struct nameext dirent;
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100112
113 if (!filename)
114 return -EIO;
115
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100116 /* Initialize buffer */
117 memset(&dirent, ' ', sizeof(dirent));
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100118
119 /* Convert filename to upper case short name */
120 period = strrchr(filename, '.');
121 pos = (char *)filename;
122 if (*pos == '.') {
123 pos = period + 1;
124 period = 0;
125 }
126 if (period)
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100127 str2fat(dirent.ext, period + 1, sizeof(dirent.ext));
128 period_location = str2fat(dirent.name, pos, sizeof(dirent.name));
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100129 if (period_location < 0)
130 return period_location;
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100131 if (*dirent.name == ' ')
132 *dirent.name = '_';
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100133 /* 0xe5 signals a deleted directory entry. Replace it by 0x05. */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100134 if (*dirent.name == 0xe5)
135 *dirent.name = 0x05;
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100136
137 /* If filename and short name are the same, quit. */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100138 sprintf(buf, "%.*s.%.3s", period_location, dirent.name, dirent.ext);
139 if (!strcmp(buf, filename)) {
140 ret = 1;
141 goto out;
142 }
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100143
144 /* Construct an indexed short name */
145 for (i = 1; i < 0x200000; ++i) {
146 int suffix_len;
147 int suffix_start;
148 int j;
149
150 /* To speed up the search use random numbers */
151 if (i < 10) {
152 j = i;
153 } else {
154 j = 30 - fls(i);
155 j = 10 + (rand() >> j);
156 }
157 sprintf(buf, "~%d", j);
158 suffix_len = strlen(buf);
159 suffix_start = 8 - suffix_len;
160 if (suffix_start > period_location)
161 suffix_start = period_location;
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100162 memcpy(dirent.name + suffix_start, buf, suffix_len);
163 if (*dirent.ext != ' ')
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100164 sprintf(buf, "%.*s.%.3s", suffix_start + suffix_len,
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100165 dirent.name, dirent.ext);
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100166 else
167 sprintf(buf, "%.*s", suffix_start + suffix_len,
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100168 dirent.name);
Heinrich Schuchardt24977e52020-11-25 16:33:55 +0100169 debug("generated short name: %s\n", buf);
170
171 /* Check that the short name does not exist yet. */
172 ret = fat_move_to_cluster(itr, itr->start_clust);
173 if (ret)
174 return ret;
175 if (find_directory_entry(itr, buf))
176 continue;
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100177
Heinrich Schuchardt24977e52020-11-25 16:33:55 +0100178 debug("chosen short name: %s\n", buf);
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100179 /* Each long name directory entry takes 13 characters. */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100180 ret = (strlen(filename) + 25) / 13;
181 goto out;
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100182 }
183 return -EIO;
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100184out:
Heinrich Schuchardtcfb7c2b2021-01-26 00:04:19 +0100185 memcpy(shortname, &dirent, SHORT_NAME_SIZE);
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100186 return ret;
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100187}
188
Donggeun Kim8f814002011-10-24 21:15:28 +0000189static int total_sector;
Donggeun Kimcb5f3bc2012-03-22 04:38:55 +0000190static int disk_write(__u32 block, __u32 nr_blocks, void *buf)
Donggeun Kim8f814002011-10-24 21:15:28 +0000191{
Łukasz Majewskid6f23d82015-09-03 14:21:39 +0200192 ulong ret;
193
Simon Glass2ee8ada2016-02-29 15:25:52 -0700194 if (!cur_dev)
Donggeun Kim8f814002011-10-24 21:15:28 +0000195 return -1;
196
Donggeun Kimcb5f3bc2012-03-22 04:38:55 +0000197 if (cur_part_info.start + block + nr_blocks >
198 cur_part_info.start + total_sector) {
Donggeun Kim8f814002011-10-24 21:15:28 +0000199 printf("error: overflow occurs\n");
200 return -1;
201 }
202
Simon Glass2ee8ada2016-02-29 15:25:52 -0700203 ret = blk_dwrite(cur_dev, cur_part_info.start + block, nr_blocks, buf);
Łukasz Majewskid6f23d82015-09-03 14:21:39 +0200204 if (nr_blocks && ret == 0)
205 return -1;
206
207 return ret;
Donggeun Kim8f814002011-10-24 21:15:28 +0000208}
209
Donggeun Kim8f814002011-10-24 21:15:28 +0000210/*
211 * Write fat buffer into block device
212 */
Stefan Brüns751b31d2016-09-11 22:51:40 +0200213static int flush_dirty_fat_buffer(fsdata *mydata)
Donggeun Kim8f814002011-10-24 21:15:28 +0000214{
215 int getsize = FATBUFBLOCKS;
216 __u32 fatlength = mydata->fatlength;
217 __u8 *bufptr = mydata->fatbuf;
218 __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
219
Stefan Brüns751b31d2016-09-11 22:51:40 +0200220 debug("debug: evicting %d, dirty: %d\n", mydata->fatbufnum,
221 (int)mydata->fat_dirty);
222
223 if ((!mydata->fat_dirty) || (mydata->fatbufnum == -1))
224 return 0;
225
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100226 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
227 if (startblock + getsize > fatlength)
228 getsize = fatlength - startblock;
Donggeun Kim8f814002011-10-24 21:15:28 +0000229
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100230 startblock += mydata->fat_sect;
Donggeun Kim8f814002011-10-24 21:15:28 +0000231
232 /* Write FAT buf */
233 if (disk_write(startblock, getsize, bufptr) < 0) {
234 debug("error: writing FAT blocks\n");
235 return -1;
236 }
237
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900238 if (mydata->fats == 2) {
Donggeun Kimaf57ce22011-12-20 18:34:27 +0000239 /* Update corresponding second FAT blocks */
240 startblock += mydata->fatlength;
241 if (disk_write(startblock, getsize, bufptr) < 0) {
242 debug("error: writing second FAT blocks\n");
243 return -1;
244 }
245 }
Stefan Brüns751b31d2016-09-11 22:51:40 +0200246 mydata->fat_dirty = 0;
Donggeun Kimaf57ce22011-12-20 18:34:27 +0000247
Donggeun Kim8f814002011-10-24 21:15:28 +0000248 return 0;
249}
250
Heinrich Schuchardt5bc30a52020-11-22 09:58:44 +0100251/**
252 * fat_find_empty_dentries() - find a sequence of available directory entries
253 *
254 * @itr: directory iterator
255 * @count: number of directory entries to find
256 * Return: 0 on success or negative error number
257 */
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +0100258static int fat_find_empty_dentries(fat_itr *itr, int count)
Heinrich Schuchardt5bc30a52020-11-22 09:58:44 +0100259{
260 unsigned int cluster;
261 dir_entry *dent;
262 int remaining;
263 unsigned int n = 0;
264 int ret;
265
266 ret = fat_move_to_cluster(itr, itr->start_clust);
267 if (ret)
268 return ret;
269
270 for (;;) {
271 if (!itr->dent) {
272 log_debug("Not enough directory entries available\n");
273 return -ENOSPC;
274 }
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100275 switch (itr->dent->nameext.name[0]) {
Heinrich Schuchardt5bc30a52020-11-22 09:58:44 +0100276 case 0x00:
277 case DELETED_FLAG:
278 if (!n) {
279 /* Remember first deleted directory entry */
280 cluster = itr->clust;
281 dent = itr->dent;
282 remaining = itr->remaining;
283 }
284 ++n;
285 if (n == count)
286 goto out;
287 break;
288 default:
289 n = 0;
290 break;
291 }
292
293 next_dent(itr);
294 if (!itr->dent &&
295 (!itr->is_root || itr->fsdata->fatsize == 32) &&
296 new_dir_table(itr))
297 return -ENOSPC;
298 }
299out:
300 /* Position back to first directory entry */
301 if (itr->clust != cluster) {
302 ret = fat_move_to_cluster(itr, cluster);
303 if (ret)
304 return ret;
305 }
306 itr->dent = dent;
307 itr->remaining = remaining;
308 return 0;
309}
310
Donggeun Kim8f814002011-10-24 21:15:28 +0000311/*
Donggeun Kim8f814002011-10-24 21:15:28 +0000312 * Set the file name information from 'name' into 'slotptr',
313 */
314static int str2slot(dir_slot *slotptr, const char *name, int *idx)
315{
316 int j, end_idx = 0;
317
318 for (j = 0; j <= 8; j += 2) {
319 if (name[*idx] == 0x00) {
320 slotptr->name0_4[j] = 0;
321 slotptr->name0_4[j + 1] = 0;
322 end_idx++;
323 goto name0_4;
324 }
325 slotptr->name0_4[j] = name[*idx];
326 (*idx)++;
327 end_idx++;
328 }
329 for (j = 0; j <= 10; j += 2) {
330 if (name[*idx] == 0x00) {
331 slotptr->name5_10[j] = 0;
332 slotptr->name5_10[j + 1] = 0;
333 end_idx++;
334 goto name5_10;
335 }
336 slotptr->name5_10[j] = name[*idx];
337 (*idx)++;
338 end_idx++;
339 }
340 for (j = 0; j <= 2; j += 2) {
341 if (name[*idx] == 0x00) {
342 slotptr->name11_12[j] = 0;
343 slotptr->name11_12[j + 1] = 0;
344 end_idx++;
345 goto name11_12;
346 }
347 slotptr->name11_12[j] = name[*idx];
348 (*idx)++;
349 end_idx++;
350 }
351
352 if (name[*idx] == 0x00)
353 return 1;
354
355 return 0;
356/* Not used characters are filled with 0xff 0xff */
357name0_4:
358 for (; end_idx < 5; end_idx++) {
359 slotptr->name0_4[end_idx * 2] = 0xff;
360 slotptr->name0_4[end_idx * 2 + 1] = 0xff;
361 }
362 end_idx = 5;
363name5_10:
364 end_idx -= 5;
365 for (; end_idx < 6; end_idx++) {
366 slotptr->name5_10[end_idx * 2] = 0xff;
367 slotptr->name5_10[end_idx * 2 + 1] = 0xff;
368 }
369 end_idx = 11;
370name11_12:
371 end_idx -= 11;
372 for (; end_idx < 2; end_idx++) {
373 slotptr->name11_12[end_idx * 2] = 0xff;
374 slotptr->name11_12[end_idx * 2 + 1] = 0xff;
375 }
376
377 return 1;
378}
379
AKASHI Takahiroa2c0bd02019-05-24 14:10:36 +0900380static int flush_dir(fat_itr *itr);
Donggeun Kim8f814002011-10-24 21:15:28 +0000381
Heinrich Schuchardtac4ab752020-11-21 08:32:50 +0100382/**
383 * fill_dir_slot() - fill directory entries for long name
384 *
385 * @itr: directory iterator
386 * @l_name: long name
387 * @shortname: short name
388 * Return: 0 for success, -errno otherwise
Donggeun Kim8f814002011-10-24 21:15:28 +0000389 */
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900390static int
Heinrich Schuchardtac4ab752020-11-21 08:32:50 +0100391fill_dir_slot(fat_itr *itr, const char *l_name, const char *shortname)
Donggeun Kim8f814002011-10-24 21:15:28 +0000392{
Tien Fong Chee0117dbe2016-07-27 23:08:56 -0700393 __u8 temp_dir_slot_buffer[MAX_LFN_SLOT * sizeof(dir_slot)];
394 dir_slot *slotptr = (dir_slot *)temp_dir_slot_buffer;
Anatolij Gustschinf4f9d5d2011-12-15 03:12:14 +0000395 __u8 counter = 0, checksum;
Donggeun Kim8f814002011-10-24 21:15:28 +0000396 int idx = 0, ret;
Donggeun Kim8f814002011-10-24 21:15:28 +0000397
Stefan Brüns6c617d62016-09-11 22:51:39 +0200398 /* Get short file name checksum value */
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100399 checksum = mkcksum((void *)shortname);
Donggeun Kim8f814002011-10-24 21:15:28 +0000400
401 do {
402 memset(slotptr, 0x00, sizeof(dir_slot));
403 ret = str2slot(slotptr, l_name, &idx);
404 slotptr->id = ++counter;
405 slotptr->attr = ATTR_VFAT;
406 slotptr->alias_checksum = checksum;
407 slotptr++;
408 } while (ret == 0);
409
410 slotptr--;
411 slotptr->id |= LAST_LONG_ENTRY_MASK;
412
413 while (counter >= 1) {
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900414 memcpy(itr->dent, slotptr, sizeof(dir_slot));
Donggeun Kim8f814002011-10-24 21:15:28 +0000415 slotptr--;
416 counter--;
AKASHI Takahiroa2c0bd02019-05-24 14:10:36 +0900417
Heinrich Schuchardt913339a2021-01-20 22:21:53 +0100418 if (!itr->remaining) {
419 /* Write directory table to device */
420 ret = flush_dir(itr);
421 if (ret)
422 return ret;
423 }
AKASHI Takahiroa2c0bd02019-05-24 14:10:36 +0900424
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +0100425 next_dent(itr);
426 if (!itr->dent)
427 return -EIO;
Donggeun Kim8f814002011-10-24 21:15:28 +0000428 }
429
Donggeun Kim8f814002011-10-24 21:15:28 +0000430 return 0;
431}
432
Donggeun Kim8f814002011-10-24 21:15:28 +0000433/*
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500434 * Set the entry at index 'entry' in a FAT (12/16/32) table.
Donggeun Kim8f814002011-10-24 21:15:28 +0000435 */
436static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
437{
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500438 __u32 bufnum, offset, off16;
439 __u16 val1, val2;
Donggeun Kim8f814002011-10-24 21:15:28 +0000440
441 switch (mydata->fatsize) {
442 case 32:
443 bufnum = entry / FAT32BUFSIZE;
444 offset = entry - bufnum * FAT32BUFSIZE;
445 break;
446 case 16:
447 bufnum = entry / FAT16BUFSIZE;
448 offset = entry - bufnum * FAT16BUFSIZE;
449 break;
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500450 case 12:
451 bufnum = entry / FAT12BUFSIZE;
452 offset = entry - bufnum * FAT12BUFSIZE;
453 break;
Donggeun Kim8f814002011-10-24 21:15:28 +0000454 default:
455 /* Unsupported FAT size */
456 return -1;
457 }
458
459 /* Read a new block of FAT entries into the cache. */
460 if (bufnum != mydata->fatbufnum) {
461 int getsize = FATBUFBLOCKS;
462 __u8 *bufptr = mydata->fatbuf;
463 __u32 fatlength = mydata->fatlength;
464 __u32 startblock = bufnum * FATBUFBLOCKS;
465
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100466 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
467 if (startblock + getsize > fatlength)
468 getsize = fatlength - startblock;
Donggeun Kim8f814002011-10-24 21:15:28 +0000469
Stefan Brüns751b31d2016-09-11 22:51:40 +0200470 if (flush_dirty_fat_buffer(mydata) < 0)
471 return -1;
Donggeun Kim8f814002011-10-24 21:15:28 +0000472
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100473 startblock += mydata->fat_sect;
474
Donggeun Kim8f814002011-10-24 21:15:28 +0000475 if (disk_read(startblock, getsize, bufptr) < 0) {
476 debug("Error reading FAT blocks\n");
477 return -1;
478 }
479 mydata->fatbufnum = bufnum;
480 }
481
Stefan Brüns751b31d2016-09-11 22:51:40 +0200482 /* Mark as dirty */
483 mydata->fat_dirty = 1;
484
Donggeun Kim8f814002011-10-24 21:15:28 +0000485 /* Set the actual entry */
486 switch (mydata->fatsize) {
487 case 32:
488 ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
489 break;
490 case 16:
491 ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
492 break;
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500493 case 12:
494 off16 = (offset * 3) / 4;
495
496 switch (offset & 0x3) {
497 case 0:
498 val1 = cpu_to_le16(entry_value) & 0xfff;
499 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff;
500 ((__u16 *)mydata->fatbuf)[off16] |= val1;
501 break;
502 case 1:
503 val1 = cpu_to_le16(entry_value) & 0xf;
504 val2 = (cpu_to_le16(entry_value) >> 4) & 0xff;
505
506 ((__u16 *)mydata->fatbuf)[off16] &= ~0xf000;
507 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 12);
508
509 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xff;
510 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
511 break;
512 case 2:
513 val1 = cpu_to_le16(entry_value) & 0xff;
514 val2 = (cpu_to_le16(entry_value) >> 8) & 0xf;
515
516 ((__u16 *)mydata->fatbuf)[off16] &= ~0xff00;
517 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 8);
518
519 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xf;
520 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
521 break;
522 case 3:
523 val1 = cpu_to_le16(entry_value) & 0xfff;
524 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff0;
525 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 4);
526 break;
527 default:
528 break;
529 }
530
531 break;
Donggeun Kim8f814002011-10-24 21:15:28 +0000532 default:
533 return -1;
534 }
535
536 return 0;
537}
538
539/*
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500540 * Determine the next free cluster after 'entry' in a FAT (12/16/32) table
Stefan Brüns14999402016-09-11 22:51:41 +0200541 * and link it to 'entry'. EOC marker is not set on returned entry.
Donggeun Kim8f814002011-10-24 21:15:28 +0000542 */
543static __u32 determine_fatent(fsdata *mydata, __u32 entry)
544{
545 __u32 next_fat, next_entry = entry + 1;
546
547 while (1) {
Stefan Brüns9f95d812016-12-17 00:27:51 +0100548 next_fat = get_fatent(mydata, next_entry);
Donggeun Kim8f814002011-10-24 21:15:28 +0000549 if (next_fat == 0) {
Stefan Brüns14999402016-09-11 22:51:41 +0200550 /* found free entry, link to entry */
Donggeun Kim8f814002011-10-24 21:15:28 +0000551 set_fatent_value(mydata, entry, next_entry);
552 break;
553 }
554 next_entry++;
555 }
556 debug("FAT%d: entry: %08x, entry_value: %04x\n",
557 mydata->fatsize, entry, next_entry);
558
559 return next_entry;
560}
561
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200562/**
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900563 * set_sectors() - write data to sectors
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200564 *
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900565 * Write 'size' bytes from 'buffer' into the specified sector.
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200566 *
567 * @mydata: data to be written
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900568 * @startsect: sector to be written to
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200569 * @buffer: data to be written
570 * @size: bytes to be written (but not more than the size of a cluster)
571 * Return: 0 on success, -1 otherwise
Donggeun Kim8f814002011-10-24 21:15:28 +0000572 */
573static int
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900574set_sectors(fsdata *mydata, u32 startsect, u8 *buffer, u32 size)
Donggeun Kim8f814002011-10-24 21:15:28 +0000575{
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900576 u32 nsects = 0;
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200577 int ret;
Donggeun Kim8f814002011-10-24 21:15:28 +0000578
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900579 debug("startsect: %d\n", startsect);
Donggeun Kim8f814002011-10-24 21:15:28 +0000580
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200581 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
582 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
583
Heinrich Schuchardtaa9d6ba2018-09-13 19:42:47 +0200584 debug("FAT: Misaligned buffer address (%p)\n", buffer);
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200585
586 while (size >= mydata->sect_size) {
587 memcpy(tmpbuf, buffer, mydata->sect_size);
588 ret = disk_write(startsect++, 1, tmpbuf);
589 if (ret != 1) {
590 debug("Error writing data (got %d)\n", ret);
591 return -1;
592 }
593
594 buffer += mydata->sect_size;
595 size -= mydata->sect_size;
596 }
597 } else if (size >= mydata->sect_size) {
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900598 nsects = size / mydata->sect_size;
599 ret = disk_write(startsect, nsects, buffer);
600 if (ret != nsects) {
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200601 debug("Error writing data (got %d)\n", ret);
Wu, Joshb0e38dd2013-07-24 17:55:30 +0800602 return -1;
603 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000604
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900605 startsect += nsects;
606 buffer += nsects * mydata->sect_size;
607 size -= nsects * mydata->sect_size;
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200608 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000609
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200610 if (size) {
611 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200612 /* Do not leak content of stack */
613 memset(tmpbuf, 0, mydata->sect_size);
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200614 memcpy(tmpbuf, buffer, size);
615 ret = disk_write(startsect, 1, tmpbuf);
616 if (ret != 1) {
617 debug("Error writing data (got %d)\n", ret);
Donggeun Kim8f814002011-10-24 21:15:28 +0000618 return -1;
619 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000620 }
621
622 return 0;
623}
624
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900625/**
626 * set_cluster() - write data to cluster
627 *
628 * Write 'size' bytes from 'buffer' into the specified cluster.
629 *
630 * @mydata: data to be written
631 * @clustnum: cluster to be written to
632 * @buffer: data to be written
633 * @size: bytes to be written (but not more than the size of a cluster)
634 * Return: 0 on success, -1 otherwise
635 */
636static int
637set_cluster(fsdata *mydata, u32 clustnum, u8 *buffer, u32 size)
638{
639 return set_sectors(mydata, clust_to_sect(mydata, clustnum),
640 buffer, size);
641}
642
Heinrich Schuchardt913339a2021-01-20 22:21:53 +0100643/**
644 * flush_dir() - flush directory
645 *
646 * @itr: directory iterator
647 * Return: 0 for success, -EIO on error
648 */
649static int flush_dir(fat_itr *itr)
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900650{
651 fsdata *mydata = itr->fsdata;
652 u32 startsect, sect_offset, nsects;
Heinrich Schuchardt913339a2021-01-20 22:21:53 +0100653 int ret;
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900654
Heinrich Schuchardt913339a2021-01-20 22:21:53 +0100655 if (!itr->is_root || mydata->fatsize == 32) {
656 ret = set_cluster(mydata, itr->clust, itr->block,
657 mydata->clust_size * mydata->sect_size);
658 goto out;
659 }
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900660
661 sect_offset = itr->clust * mydata->clust_size;
662 startsect = mydata->rootdir_sect + sect_offset;
663 /* do not write past the end of rootdir */
664 nsects = min_t(u32, mydata->clust_size,
665 mydata->rootdir_size - sect_offset);
666
Heinrich Schuchardt913339a2021-01-20 22:21:53 +0100667 ret = set_sectors(mydata, startsect, itr->block,
668 nsects * mydata->sect_size);
669out:
670 if (ret) {
671 log_err("Error: writing directory entry\n");
672 return -EIO;
673 }
674 return 0;
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900675}
676
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900677/*
678 * Read and modify data on existing and consecutive cluster blocks
679 */
680static int
681get_set_cluster(fsdata *mydata, __u32 clustnum, loff_t pos, __u8 *buffer,
682 loff_t size, loff_t *gotsize)
683{
Heinrich Schuchardt8a3ac6e2020-07-06 07:48:14 +0200684 static u8 *tmpbuf_cluster;
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900685 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
686 __u32 startsect;
687 loff_t wsize;
688 int clustcount, i, ret;
689
690 *gotsize = 0;
691 if (!size)
692 return 0;
693
Heinrich Schuchardt8a3ac6e2020-07-06 07:48:14 +0200694 if (!tmpbuf_cluster) {
695 tmpbuf_cluster = memalign(ARCH_DMA_MINALIGN, MAX_CLUSTSIZE);
696 if (!tmpbuf_cluster)
697 return -1;
698 }
699
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900700 assert(pos < bytesperclust);
701 startsect = clust_to_sect(mydata, clustnum);
702
703 debug("clustnum: %d, startsect: %d, pos: %lld\n",
704 clustnum, startsect, pos);
705
706 /* partial write at beginning */
707 if (pos) {
708 wsize = min(bytesperclust - pos, size);
709 ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster);
710 if (ret != mydata->clust_size) {
711 debug("Error reading data (got %d)\n", ret);
712 return -1;
713 }
714
715 memcpy(tmpbuf_cluster + pos, buffer, wsize);
716 ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster);
717 if (ret != mydata->clust_size) {
718 debug("Error writing data (got %d)\n", ret);
719 return -1;
720 }
721
722 size -= wsize;
723 buffer += wsize;
724 *gotsize += wsize;
725
726 startsect += mydata->clust_size;
727
728 if (!size)
729 return 0;
730 }
731
732 /* full-cluster write */
733 if (size >= bytesperclust) {
734 clustcount = lldiv(size, bytesperclust);
735
736 if (!((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1))) {
737 wsize = clustcount * bytesperclust;
738 ret = disk_write(startsect,
739 clustcount * mydata->clust_size,
740 buffer);
741 if (ret != clustcount * mydata->clust_size) {
742 debug("Error writing data (got %d)\n", ret);
743 return -1;
744 }
745
746 size -= wsize;
747 buffer += wsize;
748 *gotsize += wsize;
749
750 startsect += clustcount * mydata->clust_size;
751 } else {
752 for (i = 0; i < clustcount; i++) {
753 memcpy(tmpbuf_cluster, buffer, bytesperclust);
754 ret = disk_write(startsect,
755 mydata->clust_size,
756 tmpbuf_cluster);
757 if (ret != mydata->clust_size) {
758 debug("Error writing data (got %d)\n",
759 ret);
760 return -1;
761 }
762
763 size -= bytesperclust;
764 buffer += bytesperclust;
765 *gotsize += bytesperclust;
766
767 startsect += mydata->clust_size;
768 }
769 }
770 }
771
772 /* partial write at end */
773 if (size) {
774 wsize = size;
775 ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster);
776 if (ret != mydata->clust_size) {
777 debug("Error reading data (got %d)\n", ret);
778 return -1;
779 }
780 memcpy(tmpbuf_cluster, buffer, wsize);
781 ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster);
782 if (ret != mydata->clust_size) {
783 debug("Error writing data (got %d)\n", ret);
784 return -1;
785 }
786
787 size -= wsize;
788 buffer += wsize;
789 *gotsize += wsize;
790 }
791
792 assert(!size);
793
794 return 0;
795}
796
Donggeun Kim8f814002011-10-24 21:15:28 +0000797/*
798 * Find the first empty cluster
799 */
800static int find_empty_cluster(fsdata *mydata)
801{
802 __u32 fat_val, entry = 3;
803
804 while (1) {
Stefan Brüns9f95d812016-12-17 00:27:51 +0100805 fat_val = get_fatent(mydata, entry);
Donggeun Kim8f814002011-10-24 21:15:28 +0000806 if (fat_val == 0)
807 break;
808 entry++;
809 }
810
811 return entry;
812}
813
Heinrich Schuchardte09f3e42020-11-26 19:06:55 +0100814/**
815 * new_dir_table() - allocate a cluster for additional directory entries
816 *
817 * @itr: directory iterator
818 * Return: 0 on success, -EIO otherwise
Donggeun Kim8f814002011-10-24 21:15:28 +0000819 */
AKASHI Takahiroa2c0bd02019-05-24 14:10:36 +0900820static int new_dir_table(fat_itr *itr)
Donggeun Kim8f814002011-10-24 21:15:28 +0000821{
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900822 fsdata *mydata = itr->fsdata;
Donggeun Kim8f814002011-10-24 21:15:28 +0000823 int dir_newclust = 0;
Heinrich Schuchardte09f3e42020-11-26 19:06:55 +0100824 int dir_oldclust = itr->clust;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900825 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
Donggeun Kim8f814002011-10-24 21:15:28 +0000826
Donggeun Kim8f814002011-10-24 21:15:28 +0000827 dir_newclust = find_empty_cluster(mydata);
Heinrich Schuchardte09f3e42020-11-26 19:06:55 +0100828
829 /*
830 * Flush before updating FAT to ensure valid directory structure
831 * in case of failure.
832 */
833 itr->clust = dir_newclust;
834 itr->next_clust = dir_newclust;
835 memset(itr->block, 0x00, bytesperclust);
836 if (flush_dir(itr))
837 return -EIO;
838
839 set_fatent_value(mydata, dir_oldclust, dir_newclust);
Donggeun Kim8f814002011-10-24 21:15:28 +0000840 if (mydata->fatsize == 32)
841 set_fatent_value(mydata, dir_newclust, 0xffffff8);
842 else if (mydata->fatsize == 16)
843 set_fatent_value(mydata, dir_newclust, 0xfff8);
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500844 else if (mydata->fatsize == 12)
845 set_fatent_value(mydata, dir_newclust, 0xff8);
Donggeun Kim8f814002011-10-24 21:15:28 +0000846
Stefan Brüns751b31d2016-09-11 22:51:40 +0200847 if (flush_dirty_fat_buffer(mydata) < 0)
Heinrich Schuchardte09f3e42020-11-26 19:06:55 +0100848 return -EIO;
Donggeun Kim8f814002011-10-24 21:15:28 +0000849
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900850 itr->dent = (dir_entry *)itr->block;
851 itr->last_cluster = 1;
852 itr->remaining = bytesperclust / sizeof(dir_entry) - 1;
Donggeun Kim8f814002011-10-24 21:15:28 +0000853
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900854 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +0000855}
856
857/*
858 * Set empty cluster from 'entry' to the end of a file
859 */
860static int clear_fatent(fsdata *mydata, __u32 entry)
861{
862 __u32 fat_val;
863
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500864 while (!CHECK_CLUST(entry, mydata->fatsize)) {
Stefan Brüns9f95d812016-12-17 00:27:51 +0100865 fat_val = get_fatent(mydata, entry);
Donggeun Kim8f814002011-10-24 21:15:28 +0000866 if (fat_val != 0)
867 set_fatent_value(mydata, entry, 0);
868 else
869 break;
870
Donggeun Kim8f814002011-10-24 21:15:28 +0000871 entry = fat_val;
872 }
873
874 /* Flush fat buffer */
Stefan Brüns751b31d2016-09-11 22:51:40 +0200875 if (flush_dirty_fat_buffer(mydata) < 0)
Donggeun Kim8f814002011-10-24 21:15:28 +0000876 return -1;
877
878 return 0;
879}
880
881/*
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +0900882 * Set start cluster in directory entry
883 */
884static void set_start_cluster(const fsdata *mydata, dir_entry *dentptr,
885 __u32 start_cluster)
886{
887 if (mydata->fatsize == 32)
888 dentptr->starthi =
889 cpu_to_le16((start_cluster & 0xffff0000) >> 16);
890 dentptr->start = cpu_to_le16(start_cluster & 0xffff);
891}
892
893/*
894 * Check whether adding a file makes the file system to
895 * exceed the size of the block device
896 * Return -1 when overflow occurs, otherwise return 0
897 */
898static int check_overflow(fsdata *mydata, __u32 clustnum, loff_t size)
899{
900 __u32 startsect, sect_num, offset;
901
902 if (clustnum > 0)
903 startsect = clust_to_sect(mydata, clustnum);
904 else
905 startsect = mydata->rootdir_sect;
906
907 sect_num = div_u64_rem(size, mydata->sect_size, &offset);
908
909 if (offset != 0)
910 sect_num++;
911
912 if (startsect + sect_num > total_sector)
913 return -1;
914 return 0;
915}
916
917/*
Donggeun Kim8f814002011-10-24 21:15:28 +0000918 * Write at most 'maxsize' bytes from 'buffer' into
919 * the file associated with 'dentptr'
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800920 * Update the number of bytes written in *gotsize and return 0
921 * or return -1 on fatal errors.
Donggeun Kim8f814002011-10-24 21:15:28 +0000922 */
923static int
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +0900924set_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, __u8 *buffer,
925 loff_t maxsize, loff_t *gotsize)
Donggeun Kim8f814002011-10-24 21:15:28 +0000926{
Donggeun Kim8f814002011-10-24 21:15:28 +0000927 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
928 __u32 curclust = START(dentptr);
929 __u32 endclust = 0, newclust = 0;
Heinrich Schuchardtdb538a92019-02-25 19:42:48 +0100930 u64 cur_pos, filesize;
931 loff_t offset, actsize, wsize;
Donggeun Kim8f814002011-10-24 21:15:28 +0000932
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800933 *gotsize = 0;
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900934 filesize = pos + maxsize;
Donggeun Kim8f814002011-10-24 21:15:28 +0000935
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800936 debug("%llu bytes\n", filesize);
Donggeun Kim8f814002011-10-24 21:15:28 +0000937
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900938 if (!filesize) {
939 if (!curclust)
940 return 0;
941 if (!CHECK_CLUST(curclust, mydata->fatsize) ||
942 IS_LAST_CLUST(curclust, mydata->fatsize)) {
943 clear_fatent(mydata, curclust);
944 set_start_cluster(mydata, dentptr, 0);
945 return 0;
946 }
947 debug("curclust: 0x%x\n", curclust);
948 debug("Invalid FAT entry\n");
949 return -1;
950 }
951
952 if (!curclust) {
953 assert(pos == 0);
954 goto set_clusters;
955 }
956
957 /* go to cluster at pos */
958 cur_pos = bytesperclust;
959 while (1) {
960 if (pos <= cur_pos)
961 break;
962 if (IS_LAST_CLUST(curclust, mydata->fatsize))
963 break;
964
965 newclust = get_fatent(mydata, curclust);
966 if (!IS_LAST_CLUST(newclust, mydata->fatsize) &&
967 CHECK_CLUST(newclust, mydata->fatsize)) {
968 debug("curclust: 0x%x\n", curclust);
969 debug("Invalid FAT entry\n");
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +0200970 return -1;
971 }
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900972
973 cur_pos += bytesperclust;
974 curclust = newclust;
975 }
976 if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
977 assert(pos == cur_pos);
978 goto set_clusters;
979 }
980
981 assert(pos < cur_pos);
982 cur_pos -= bytesperclust;
983
984 /* overwrite */
985 assert(IS_LAST_CLUST(curclust, mydata->fatsize) ||
986 !CHECK_CLUST(curclust, mydata->fatsize));
987
988 while (1) {
989 /* search for allocated consecutive clusters */
990 actsize = bytesperclust;
991 endclust = curclust;
992 while (1) {
993 if (filesize <= (cur_pos + actsize))
994 break;
995
996 newclust = get_fatent(mydata, endclust);
997
Marek Szyprowski2f241672019-12-02 12:11:13 +0100998 if (newclust != endclust + 1)
999 break;
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001000 if (IS_LAST_CLUST(newclust, mydata->fatsize))
1001 break;
1002 if (CHECK_CLUST(newclust, mydata->fatsize)) {
1003 debug("curclust: 0x%x\n", curclust);
1004 debug("Invalid FAT entry\n");
1005 return -1;
1006 }
1007
1008 actsize += bytesperclust;
1009 endclust = newclust;
1010 }
1011
1012 /* overwrite to <curclust..endclust> */
1013 if (pos < cur_pos)
1014 offset = 0;
1015 else
1016 offset = pos - cur_pos;
Marek Szyprowski3a867ce2019-12-02 12:11:14 +01001017 wsize = min_t(unsigned long long, actsize, filesize - cur_pos);
1018 wsize -= offset;
1019
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001020 if (get_set_cluster(mydata, curclust, offset,
1021 buffer, wsize, &actsize)) {
1022 printf("Error get-and-setting cluster\n");
1023 return -1;
1024 }
1025 buffer += wsize;
1026 *gotsize += wsize;
1027 cur_pos += offset + wsize;
1028
1029 if (filesize <= cur_pos)
1030 break;
1031
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001032 if (IS_LAST_CLUST(newclust, mydata->fatsize))
1033 /* no more clusters */
1034 break;
1035
1036 curclust = newclust;
1037 }
1038
1039 if (filesize <= cur_pos) {
1040 /* no more write */
1041 newclust = get_fatent(mydata, endclust);
1042 if (!IS_LAST_CLUST(newclust, mydata->fatsize)) {
1043 /* truncate the rest */
1044 clear_fatent(mydata, newclust);
1045
1046 /* Mark end of file in FAT */
1047 if (mydata->fatsize == 12)
1048 newclust = 0xfff;
1049 else if (mydata->fatsize == 16)
1050 newclust = 0xffff;
1051 else if (mydata->fatsize == 32)
1052 newclust = 0xfffffff;
1053 set_fatent_value(mydata, endclust, newclust);
1054 }
1055
1056 return 0;
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001057 }
1058
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001059 curclust = endclust;
1060 filesize -= cur_pos;
Heinrich Schuchardtdb538a92019-02-25 19:42:48 +01001061 assert(!do_div(cur_pos, bytesperclust));
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001062
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001063set_clusters:
1064 /* allocate and write */
1065 assert(!pos);
1066
1067 /* Assure that curclust is valid */
1068 if (!curclust) {
1069 curclust = find_empty_cluster(mydata);
1070 set_start_cluster(mydata, dentptr, curclust);
1071 } else {
1072 newclust = get_fatent(mydata, curclust);
1073
1074 if (IS_LAST_CLUST(newclust, mydata->fatsize)) {
1075 newclust = determine_fatent(mydata, curclust);
1076 set_fatent_value(mydata, curclust, newclust);
1077 curclust = newclust;
1078 } else {
1079 debug("error: something wrong\n");
1080 return -1;
1081 }
1082 }
1083
1084 /* TODO: already partially written */
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001085 if (check_overflow(mydata, curclust, filesize)) {
1086 printf("Error: no space left: %llu\n", filesize);
1087 return -1;
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +02001088 }
1089
Donggeun Kim8f814002011-10-24 21:15:28 +00001090 actsize = bytesperclust;
1091 endclust = curclust;
1092 do {
1093 /* search for consecutive clusters */
1094 while (actsize < filesize) {
1095 newclust = determine_fatent(mydata, endclust);
1096
1097 if ((newclust - 1) != endclust)
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001098 /* write to <curclust..endclust> */
Donggeun Kim8f814002011-10-24 21:15:28 +00001099 goto getit;
1100
1101 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Benoît Thébaudeaue0b86942015-09-28 15:45:30 +02001102 debug("newclust: 0x%x\n", newclust);
Donggeun Kim8f814002011-10-24 21:15:28 +00001103 debug("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001104 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +00001105 }
1106 endclust = newclust;
1107 actsize += bytesperclust;
1108 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001109
1110 /* set remaining bytes */
Donggeun Kim8f814002011-10-24 21:15:28 +00001111 actsize = filesize;
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +02001112 if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
Donggeun Kim8f814002011-10-24 21:15:28 +00001113 debug("error: writing cluster\n");
1114 return -1;
1115 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001116 *gotsize += actsize;
Donggeun Kim8f814002011-10-24 21:15:28 +00001117
1118 /* Mark end of file in FAT */
Philipp Skadorov16f553d2016-12-15 15:52:53 -05001119 if (mydata->fatsize == 12)
1120 newclust = 0xfff;
1121 else if (mydata->fatsize == 16)
Donggeun Kim8f814002011-10-24 21:15:28 +00001122 newclust = 0xffff;
1123 else if (mydata->fatsize == 32)
1124 newclust = 0xfffffff;
1125 set_fatent_value(mydata, endclust, newclust);
1126
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001127 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +00001128getit:
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +02001129 if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
Donggeun Kim8f814002011-10-24 21:15:28 +00001130 debug("error: writing cluster\n");
1131 return -1;
1132 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001133 *gotsize += actsize;
Donggeun Kim8f814002011-10-24 21:15:28 +00001134 filesize -= actsize;
1135 buffer += actsize;
1136
Benoît Thébaudeaue0b86942015-09-28 15:45:30 +02001137 if (CHECK_CLUST(newclust, mydata->fatsize)) {
1138 debug("newclust: 0x%x\n", newclust);
Donggeun Kim8f814002011-10-24 21:15:28 +00001139 debug("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001140 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +00001141 }
1142 actsize = bytesperclust;
1143 curclust = endclust = newclust;
1144 } while (1);
Donggeun Kim8f814002011-10-24 21:15:28 +00001145
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001146 return 0;
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +02001147}
1148
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001149/**
1150 * fill_dentry() - fill directory entry with shortname
1151 *
1152 * @mydata: private filesystem parameters
1153 * @dentptr: directory entry
1154 * @shortname: chosen short name
1155 * @start_cluster: first cluster of file
1156 * @size: file size
1157 * @attr: file attributes
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +02001158 */
1159static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001160 const char *shortname, __u32 start_cluster, __u32 size, __u8 attr)
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +02001161{
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001162 memset(dentptr, 0, sizeof(*dentptr));
1163
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +02001164 set_start_cluster(mydata, dentptr, start_cluster);
Donggeun Kim8f814002011-10-24 21:15:28 +00001165 dentptr->size = cpu_to_le32(size);
1166
1167 dentptr->attr = attr;
1168
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +01001169 memcpy(&dentptr->nameext, shortname, SHORT_NAME_SIZE);
Donggeun Kim8f814002011-10-24 21:15:28 +00001170}
1171
Heinrich Schuchardt7a3f6102020-11-26 16:10:01 +01001172/**
1173 * find_directory_entry() - find a directory entry by filename
1174 *
1175 * @itr: directory iterator
1176 * @filename: name of file to find
1177 * Return: directory entry or NULL
Donggeun Kim8f814002011-10-24 21:15:28 +00001178 */
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001179static dir_entry *find_directory_entry(fat_itr *itr, char *filename)
Donggeun Kim8f814002011-10-24 21:15:28 +00001180{
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001181 int match = 0;
Donggeun Kim8f814002011-10-24 21:15:28 +00001182
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001183 while (fat_itr_next(itr)) {
1184 /* check both long and short name: */
1185 if (!strcasecmp(filename, itr->name))
1186 match = 1;
1187 else if (itr->name != itr->s_name &&
1188 !strcasecmp(filename, itr->s_name))
1189 match = 1;
Donggeun Kim8f814002011-10-24 21:15:28 +00001190
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001191 if (!match)
1192 continue;
Donggeun Kim8f814002011-10-24 21:15:28 +00001193
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +01001194 if (itr->dent->nameext.name[0] == '\0')
Donggeun Kim8f814002011-10-24 21:15:28 +00001195 return NULL;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001196 else
1197 return itr->dent;
1198 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001199
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001200 return NULL;
1201}
Donggeun Kim8f814002011-10-24 21:15:28 +00001202
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001203static int split_filename(char *filename, char **dirname, char **basename)
1204{
1205 char *p, *last_slash, *last_slash_cont;
Donggeun Kim8f814002011-10-24 21:15:28 +00001206
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001207again:
1208 p = filename;
1209 last_slash = NULL;
1210 last_slash_cont = NULL;
1211 while (*p) {
1212 if (ISDIRDELIM(*p)) {
1213 last_slash = p;
1214 last_slash_cont = p;
1215 /* continuous slashes */
1216 while (ISDIRDELIM(*p))
1217 last_slash_cont = p++;
1218 if (!*p)
1219 break;
Donggeun Kim8f814002011-10-24 21:15:28 +00001220 }
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001221 p++;
1222 }
Wu, Josh675b23c2014-05-08 16:14:07 +08001223
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001224 if (last_slash) {
1225 if (last_slash_cont == (filename + strlen(filename) - 1)) {
1226 /* remove trailing slashes */
1227 *last_slash = '\0';
1228 goto again;
Wu, Josh675b23c2014-05-08 16:14:07 +08001229 }
1230
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001231 if (last_slash == filename) {
1232 /* avoid ""(null) directory */
1233 *dirname = "/";
1234 } else {
1235 *last_slash = '\0';
1236 *dirname = filename;
Donggeun Kim8f814002011-10-24 21:15:28 +00001237 }
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001238
1239 *last_slash_cont = '\0';
1240 *basename = last_slash_cont + 1;
1241 } else {
1242 *dirname = "/"; /* root by default */
1243 *basename = filename;
Donggeun Kim8f814002011-10-24 21:15:28 +00001244 }
1245
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001246 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +00001247}
1248
Heinrich Schuchardt58587212019-05-12 09:59:18 +02001249/**
1250 * normalize_longname() - check long file name and convert to lower case
1251 *
1252 * We assume here that the FAT file system is using an 8bit code page.
1253 * Linux typically uses CP437, EDK2 assumes CP1250.
1254 *
1255 * @l_filename: preallocated buffer receiving the normalized name
1256 * @filename: filename to normalize
1257 * Return: 0 on success, -1 on failure
1258 */
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001259static int normalize_longname(char *l_filename, const char *filename)
1260{
Heinrich Schuchardt58587212019-05-12 09:59:18 +02001261 const char *p, illegal[] = "<>:\"/\\|?*";
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001262
Heinrich Schuchardt58587212019-05-12 09:59:18 +02001263 if (strlen(filename) >= VFAT_MAXLEN_BYTES)
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001264 return -1;
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001265
Heinrich Schuchardt58587212019-05-12 09:59:18 +02001266 for (p = filename; *p; ++p) {
1267 if ((unsigned char)*p < 0x20)
1268 return -1;
1269 if (strchr(illegal, *p))
1270 return -1;
1271 }
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001272
Heinrich Schuchardt58587212019-05-12 09:59:18 +02001273 strcpy(l_filename, filename);
1274 downcase(l_filename, VFAT_MAXLEN_BYTES);
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001275
1276 return 0;
1277}
1278
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001279int file_fat_write_at(const char *filename, loff_t pos, void *buffer,
1280 loff_t size, loff_t *actwrite)
Donggeun Kim8f814002011-10-24 21:15:28 +00001281{
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001282 dir_entry *retdent;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001283 fsdata datablock = { .fatbuf = NULL, };
Donggeun Kim8f814002011-10-24 21:15:28 +00001284 fsdata *mydata = &datablock;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001285 fat_itr *itr = NULL;
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001286 int ret = -1;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001287 char *filename_copy, *parent, *basename;
Donggeun Kim8f814002011-10-24 21:15:28 +00001288 char l_filename[VFAT_MAXLEN_BYTES];
1289
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001290 debug("writing %s\n", filename);
1291
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001292 filename_copy = strdup(filename);
1293 if (!filename_copy)
1294 return -ENOMEM;
Donggeun Kim8f814002011-10-24 21:15:28 +00001295
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001296 split_filename(filename_copy, &parent, &basename);
1297 if (!strlen(basename)) {
1298 ret = -EINVAL;
1299 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001300 }
1301
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001302 filename = basename;
1303 if (normalize_longname(l_filename, filename)) {
1304 printf("FAT: illegal filename (%s)\n", filename);
1305 ret = -EINVAL;
1306 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001307 }
1308
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001309 itr = malloc_cache_aligned(sizeof(fat_itr));
1310 if (!itr) {
1311 ret = -ENOMEM;
1312 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001313 }
1314
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001315 ret = fat_itr_root(itr, &datablock);
1316 if (ret)
Donggeun Kim8f814002011-10-24 21:15:28 +00001317 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001318
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001319 total_sector = datablock.total_sect;
1320
1321 ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1322 if (ret) {
1323 printf("%s: doesn't exist (%d)\n", parent, ret);
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001324 goto exit;
1325 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001326
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001327 retdent = find_directory_entry(itr, l_filename);
1328
Donggeun Kim8f814002011-10-24 21:15:28 +00001329 if (retdent) {
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001330 if (fat_itr_isdir(itr)) {
1331 ret = -EISDIR;
1332 goto exit;
1333 }
1334
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001335 /* A file exists */
1336 if (pos == -1)
1337 /* Append to the end */
1338 pos = FAT2CPU32(retdent->size);
1339 if (pos > retdent->size) {
1340 /* No hole allowed */
1341 ret = -EINVAL;
1342 goto exit;
1343 }
1344
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001345 /* Update file size in a directory entry */
1346 retdent->size = cpu_to_le32(pos + size);
Donggeun Kim8f814002011-10-24 21:15:28 +00001347 } else {
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001348 /* Create a new file */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001349 char shortname[SHORT_NAME_SIZE];
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +01001350 int ndent;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001351
1352 if (itr->is_root) {
1353 /* root dir cannot have "." or ".." */
1354 if (!strcmp(l_filename, ".") ||
1355 !strcmp(l_filename, "..")) {
1356 ret = -EINVAL;
1357 goto exit;
1358 }
1359 }
1360
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001361 if (pos) {
1362 /* No hole allowed */
1363 ret = -EINVAL;
1364 goto exit;
1365 }
1366
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001367 /* Check if long name is needed */
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +01001368 ndent = set_name(itr, filename, shortname);
1369 if (ndent < 0) {
1370 ret = ndent;
1371 goto exit;
1372 }
1373 ret = fat_find_empty_dentries(itr, ndent);
1374 if (ret)
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001375 goto exit;
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +01001376 if (ndent > 1) {
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001377 /* Set long name entries */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001378 ret = fill_dir_slot(itr, filename, shortname);
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001379 if (ret)
1380 goto exit;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001381 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001382
AKASHI Takahiroa2c0bd02019-05-24 14:10:36 +09001383 /* Set short name entry */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001384 fill_dentry(itr->fsdata, itr->dent, shortname, 0, size,
Heinrich Schuchardt881c74a2020-11-22 11:13:33 +01001385 ATTR_ARCH);
Donggeun Kim8f814002011-10-24 21:15:28 +00001386
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001387 retdent = itr->dent;
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001388 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001389
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001390 ret = set_contents(mydata, retdent, pos, buffer, size, actwrite);
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001391 if (ret < 0) {
1392 printf("Error: writing contents\n");
AKASHI Takahiro6aa77692018-09-11 15:59:03 +09001393 ret = -EIO;
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001394 goto exit;
1395 }
1396 debug("attempt to write 0x%llx bytes\n", *actwrite);
Donggeun Kim8f814002011-10-24 21:15:28 +00001397
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001398 /* Flush fat buffer */
Stefan Brüns751b31d2016-09-11 22:51:40 +02001399 ret = flush_dirty_fat_buffer(mydata);
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001400 if (ret) {
1401 printf("Error: flush fat buffer\n");
AKASHI Takahiro6aa77692018-09-11 15:59:03 +09001402 ret = -EIO;
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001403 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001404 }
1405
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001406 /* Write directory table to device */
AKASHI Takahirod98c6742019-05-24 14:10:35 +09001407 ret = flush_dir(itr);
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001408
Donggeun Kim8f814002011-10-24 21:15:28 +00001409exit:
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001410 free(filename_copy);
Donggeun Kim8f814002011-10-24 21:15:28 +00001411 free(mydata->fatbuf);
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001412 free(itr);
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001413 return ret;
Donggeun Kim8f814002011-10-24 21:15:28 +00001414}
1415
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001416int file_fat_write(const char *filename, void *buffer, loff_t offset,
1417 loff_t maxsize, loff_t *actwrite)
Donggeun Kim8f814002011-10-24 21:15:28 +00001418{
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001419 return file_fat_write_at(filename, offset, buffer, maxsize, actwrite);
Donggeun Kim8f814002011-10-24 21:15:28 +00001420}
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001421
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001422static int fat_dir_entries(fat_itr *itr)
1423{
1424 fat_itr *dirs;
1425 fsdata fsdata = { .fatbuf = NULL, }, *mydata = &fsdata;
1426 /* for FATBUFSIZE */
1427 int count;
1428
1429 dirs = malloc_cache_aligned(sizeof(fat_itr));
1430 if (!dirs) {
1431 debug("Error: allocating memory\n");
1432 count = -ENOMEM;
1433 goto exit;
1434 }
1435
1436 /* duplicate fsdata */
1437 fat_itr_child(dirs, itr);
1438 fsdata = *dirs->fsdata;
1439
1440 /* allocate local fat buffer */
1441 fsdata.fatbuf = malloc_cache_aligned(FATBUFSIZE);
1442 if (!fsdata.fatbuf) {
1443 debug("Error: allocating memory\n");
1444 count = -ENOMEM;
1445 goto exit;
1446 }
1447 fsdata.fatbufnum = -1;
1448 dirs->fsdata = &fsdata;
1449
1450 for (count = 0; fat_itr_next(dirs); count++)
1451 ;
1452
1453exit:
1454 free(fsdata.fatbuf);
1455 free(dirs);
1456 return count;
1457}
1458
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001459/**
1460 * delete_single_dentry() - delete a single directory entry
1461 *
1462 * @itr: directory iterator
1463 * Return: 0 for success
1464 */
1465static int delete_single_dentry(fat_itr *itr)
1466{
1467 struct dir_entry *dent = itr->dent;
1468
1469 memset(dent, 0, sizeof(*dent));
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +01001470 dent->nameext.name[0] = DELETED_FLAG;
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001471
Heinrich Schuchardt913339a2021-01-20 22:21:53 +01001472 if (!itr->remaining)
1473 return flush_dir(itr);
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001474 return 0;
1475}
1476
1477/**
1478 * delete_long_name() - delete long name directory entries
1479 *
1480 * @itr: directory iterator
1481 * Return: 0 for success
1482 */
1483static int delete_long_name(fat_itr *itr)
1484{
1485 struct dir_entry *dent = itr->dent;
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +01001486 int seqn = itr->dent->nameext.name[0] & ~LAST_LONG_ENTRY_MASK;
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001487
1488 while (seqn--) {
1489 int ret;
1490
1491 ret = delete_single_dentry(itr);
1492 if (ret)
1493 return ret;
1494 dent = next_dent(itr);
1495 if (!dent)
1496 return -EIO;
1497 }
1498 return 0;
1499}
1500
1501/**
1502 * delete_dentry_long() - remove directory entry
1503 *
1504 * @itr: directory iterator
1505 * Return: 0 for success
1506 */
1507static int delete_dentry_long(fat_itr *itr)
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001508{
1509 fsdata *mydata = itr->fsdata;
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001510 dir_entry *dent = itr->dent;
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001511
1512 /* free cluster blocks */
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001513 clear_fatent(mydata, START(dent));
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001514 if (flush_dirty_fat_buffer(mydata) < 0) {
1515 printf("Error: flush fat buffer\n");
1516 return -EIO;
1517 }
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001518 /* Position to first directory entry for long name */
1519 if (itr->clust != itr->dent_clust) {
1520 int ret;
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001521
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001522 ret = fat_move_to_cluster(itr, itr->dent_clust);
1523 if (ret)
1524 return ret;
1525 }
1526 itr->dent = itr->dent_start;
1527 itr->remaining = itr->dent_rem;
1528 dent = itr->dent_start;
1529 /* Delete long name */
1530 if ((dent->attr & ATTR_VFAT) == ATTR_VFAT &&
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +01001531 (dent->nameext.name[0] & LAST_LONG_ENTRY_MASK)) {
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001532 int ret;
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001533
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001534 ret = delete_long_name(itr);
1535 if (ret)
1536 return ret;
1537 }
1538 /* Delete short name */
1539 delete_single_dentry(itr);
Heinrich Schuchardt913339a2021-01-20 22:21:53 +01001540 return flush_dir(itr);
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001541}
1542
1543int fat_unlink(const char *filename)
1544{
1545 fsdata fsdata = { .fatbuf = NULL, };
1546 fat_itr *itr = NULL;
1547 int n_entries, ret;
1548 char *filename_copy, *dirname, *basename;
1549
1550 filename_copy = strdup(filename);
Heinrich Schuchardtb2242f62018-10-02 06:58:00 +02001551 if (!filename_copy) {
1552 printf("Error: allocating memory\n");
1553 ret = -ENOMEM;
1554 goto exit;
1555 }
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001556 split_filename(filename_copy, &dirname, &basename);
1557
1558 if (!strcmp(dirname, "/") && !strcmp(basename, "")) {
1559 printf("Error: cannot remove root\n");
1560 ret = -EINVAL;
1561 goto exit;
1562 }
1563
1564 itr = malloc_cache_aligned(sizeof(fat_itr));
1565 if (!itr) {
1566 printf("Error: allocating memory\n");
Heinrich Schuchardtb2242f62018-10-02 06:58:00 +02001567 ret = -ENOMEM;
1568 goto exit;
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001569 }
1570
1571 ret = fat_itr_root(itr, &fsdata);
1572 if (ret)
1573 goto exit;
1574
1575 total_sector = fsdata.total_sect;
1576
1577 ret = fat_itr_resolve(itr, dirname, TYPE_DIR);
1578 if (ret) {
1579 printf("%s: doesn't exist (%d)\n", dirname, ret);
1580 ret = -ENOENT;
1581 goto exit;
1582 }
1583
1584 if (!find_directory_entry(itr, basename)) {
1585 printf("%s: doesn't exist\n", basename);
1586 ret = -ENOENT;
1587 goto exit;
1588 }
1589
1590 if (fat_itr_isdir(itr)) {
1591 n_entries = fat_dir_entries(itr);
1592 if (n_entries < 0) {
1593 ret = n_entries;
1594 goto exit;
1595 }
1596 if (n_entries > 2) {
1597 printf("Error: directory is not empty: %d\n",
1598 n_entries);
1599 ret = -EINVAL;
1600 goto exit;
1601 }
1602 }
1603
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001604 ret = delete_dentry_long(itr);
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001605
1606exit:
1607 free(fsdata.fatbuf);
1608 free(itr);
1609 free(filename_copy);
1610
1611 return ret;
1612}
1613
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001614int fat_mkdir(const char *new_dirname)
1615{
1616 dir_entry *retdent;
1617 fsdata datablock = { .fatbuf = NULL, };
1618 fsdata *mydata = &datablock;
1619 fat_itr *itr = NULL;
1620 char *dirname_copy, *parent, *dirname;
1621 char l_dirname[VFAT_MAXLEN_BYTES];
1622 int ret = -1;
1623 loff_t actwrite;
1624 unsigned int bytesperclust;
1625 dir_entry *dotdent = NULL;
1626
1627 dirname_copy = strdup(new_dirname);
1628 if (!dirname_copy)
1629 goto exit;
1630
1631 split_filename(dirname_copy, &parent, &dirname);
1632 if (!strlen(dirname)) {
1633 ret = -EINVAL;
1634 goto exit;
1635 }
1636
1637 if (normalize_longname(l_dirname, dirname)) {
1638 printf("FAT: illegal filename (%s)\n", dirname);
1639 ret = -EINVAL;
1640 goto exit;
1641 }
1642
1643 itr = malloc_cache_aligned(sizeof(fat_itr));
1644 if (!itr) {
1645 ret = -ENOMEM;
1646 goto exit;
1647 }
1648
1649 ret = fat_itr_root(itr, &datablock);
1650 if (ret)
1651 goto exit;
1652
1653 total_sector = datablock.total_sect;
1654
1655 ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1656 if (ret) {
1657 printf("%s: doesn't exist (%d)\n", parent, ret);
1658 goto exit;
1659 }
1660
1661 retdent = find_directory_entry(itr, l_dirname);
1662
1663 if (retdent) {
1664 printf("%s: already exists\n", l_dirname);
1665 ret = -EEXIST;
1666 goto exit;
1667 } else {
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001668 char shortname[SHORT_NAME_SIZE];
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +01001669 int ndent;
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001670
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001671 if (itr->is_root) {
1672 /* root dir cannot have "." or ".." */
1673 if (!strcmp(l_dirname, ".") ||
1674 !strcmp(l_dirname, "..")) {
1675 ret = -EINVAL;
1676 goto exit;
1677 }
1678 }
1679
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001680 /* Check if long name is needed */
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +01001681 ndent = set_name(itr, dirname, shortname);
1682 if (ndent < 0) {
1683 ret = ndent;
1684 goto exit;
1685 }
1686 ret = fat_find_empty_dentries(itr, ndent);
1687 if (ret)
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001688 goto exit;
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +01001689 if (ndent > 1) {
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001690 /* Set long name entries */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001691 ret = fill_dir_slot(itr, dirname, shortname);
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001692 if (ret)
1693 goto exit;
1694 }
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001695
1696 /* Set attribute as archive for regular file */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001697 fill_dentry(itr->fsdata, itr->dent, shortname, 0, 0,
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001698 ATTR_DIR | ATTR_ARCH);
1699
1700 retdent = itr->dent;
1701 }
1702
1703 /* Default entries */
1704 bytesperclust = mydata->clust_size * mydata->sect_size;
1705 dotdent = malloc_cache_aligned(bytesperclust);
1706 if (!dotdent) {
1707 ret = -ENOMEM;
1708 goto exit;
1709 }
1710 memset(dotdent, 0, bytesperclust);
1711
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +01001712 memcpy(&dotdent[0].nameext, ". ", 11);
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001713 dotdent[0].attr = ATTR_DIR | ATTR_ARCH;
1714
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +01001715 memcpy(&dotdent[1].nameext, ".. ", 11);
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001716 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");
Heinrich Schuchardt913339a2021-01-20 22:21:53 +01001742 ret = -EIO;
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001743 goto exit;
1744 }
1745
1746 /* Write directory table to device */
AKASHI Takahirod98c6742019-05-24 14:10:35 +09001747 ret = flush_dir(itr);
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001748
1749exit:
1750 free(dirname_copy);
1751 free(mydata->fatbuf);
1752 free(itr);
1753 free(dotdent);
1754 return ret;
1755}