blob: 0132ef9b42f0b6aff795760371c288507800ab66 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Donggeun Kim8f814002011-10-24 21:15:28 +00002/*
3 * fat_write.c
4 *
5 * R/W (V)FAT 12/16/32 filesystem implementation by Donggeun Kim
Donggeun Kim8f814002011-10-24 21:15:28 +00006 */
7
8#include <common.h>
9#include <command.h>
10#include <config.h>
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010011#include <div64.h>
Donggeun Kim8f814002011-10-24 21:15:28 +000012#include <fat.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <malloc.h>
Donggeun Kim8f814002011-10-24 21:15:28 +000015#include <part.h>
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010016#include <rand.h>
17#include <asm/byteorder.h>
Simon Glass274e0b02020-05-10 11:39:56 -060018#include <asm/cache.h>
Richard Genoud2e372022012-12-13 00:47:36 +000019#include <linux/ctype.h>
Tom Rinia17b7bc2014-11-24 11:50:46 -050020#include <linux/math64.h>
Donggeun Kim8f814002011-10-24 21:15:28 +000021#include "fat.c"
22
Heinrich Schuchardt24977e52020-11-25 16:33:55 +010023static dir_entry *find_directory_entry(fat_itr *itr, char *filename);
Heinrich Schuchardt5bc30a52020-11-22 09:58:44 +010024static int new_dir_table(fat_itr *itr);
Heinrich Schuchardt24977e52020-11-25 16:33:55 +010025
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010026/* Characters that may only be used in long file names */
27static const char LONG_ONLY_CHARS[] = "+,;=[]";
28
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +010029/* Combined size of the name and ext fields in the directory entry */
30#define SHORT_NAME_SIZE 11
31
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010032/**
33 * str2fat() - convert string to valid FAT name characters
34 *
35 * Stop when reaching end of @src or a period.
36 * Ignore spaces.
37 * Replace characters that may only be used in long names by underscores.
38 * Convert lower case characters to upper case.
39 *
40 * To avoid assumptions about the code page we do not use characters
41 * above 0x7f for the short name.
42 *
43 * @dest: destination buffer
44 * @src: source buffer
45 * @length: size of destination buffer
46 * Return: number of bytes in destination buffer
47 */
48static int str2fat(char *dest, char *src, int length)
Donggeun Kim8f814002011-10-24 21:15:28 +000049{
50 int i;
51
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010052 for (i = 0; i < length; ++src) {
53 char c = *src;
54
55 if (!c || c == '.')
56 break;
57 if (c == ' ')
58 continue;
59 if (strchr(LONG_ONLY_CHARS, c) || c > 0x7f)
60 c = '_';
61 else if (c >= 'a' && c <= 'z')
62 c &= 0xdf;
63 dest[i] = c;
64 ++i;
Donggeun Kim8f814002011-10-24 21:15:28 +000065 }
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010066 return i;
Donggeun Kim8f814002011-10-24 21:15:28 +000067}
68
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010069/**
Heinrich Schuchardt24977e52020-11-25 16:33:55 +010070 * fat_move_to_cluster() - position to first directory entry in cluster
71 *
72 * @itr: directory iterator
73 * @cluster cluster
74 * Return: 0 for success, -EIO on error
75 */
76static int fat_move_to_cluster(fat_itr *itr, unsigned int cluster)
77{
78 unsigned int nbytes;
79
80 /* position to the start of the directory */
81 itr->next_clust = cluster;
82 itr->last_cluster = 0;
83 if (!fat_next_cluster(itr, &nbytes))
84 return -EIO;
85 itr->dent = (dir_entry *)itr->block;
86 itr->remaining = nbytes / sizeof(dir_entry) - 1;
87 return 0;
88}
89
90/**
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010091 * set_name() - set short name in directory entry
92 *
93 * The function determines if the @filename is a valid short name.
94 * In this case no long name is needed.
95 *
96 * If a long name is needed, a short name is constructed.
97 *
Heinrich Schuchardt24977e52020-11-25 16:33:55 +010098 * @itr: directory iterator
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010099 * @filename: long file name
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100100 * @shortname: buffer of 11 bytes to receive chosen short name and extension
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100101 * Return: number of directory entries needed, negative on error
102 */
Heinrich Schuchardt24977e52020-11-25 16:33:55 +0100103static int set_name(fat_itr *itr, const char *filename, char *shortname)
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100104{
105 char *period;
106 char *pos;
107 int period_location;
108 char buf[13];
109 int i;
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100110 int ret;
111 struct {
112 char name[8];
113 char ext[3];
114 } dirent;
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100115
116 if (!filename)
117 return -EIO;
118
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100119 /* Initialize buffer */
120 memset(&dirent, ' ', sizeof(dirent));
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100121
122 /* Convert filename to upper case short name */
123 period = strrchr(filename, '.');
124 pos = (char *)filename;
125 if (*pos == '.') {
126 pos = period + 1;
127 period = 0;
128 }
129 if (period)
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100130 str2fat(dirent.ext, period + 1, sizeof(dirent.ext));
131 period_location = str2fat(dirent.name, pos, sizeof(dirent.name));
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100132 if (period_location < 0)
133 return period_location;
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100134 if (*dirent.name == ' ')
135 *dirent.name = '_';
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100136 /* 0xe5 signals a deleted directory entry. Replace it by 0x05. */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100137 if (*dirent.name == 0xe5)
138 *dirent.name = 0x05;
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100139
140 /* If filename and short name are the same, quit. */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100141 sprintf(buf, "%.*s.%.3s", period_location, dirent.name, dirent.ext);
142 if (!strcmp(buf, filename)) {
143 ret = 1;
144 goto out;
145 }
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100146
147 /* Construct an indexed short name */
148 for (i = 1; i < 0x200000; ++i) {
149 int suffix_len;
150 int suffix_start;
151 int j;
152
153 /* To speed up the search use random numbers */
154 if (i < 10) {
155 j = i;
156 } else {
157 j = 30 - fls(i);
158 j = 10 + (rand() >> j);
159 }
160 sprintf(buf, "~%d", j);
161 suffix_len = strlen(buf);
162 suffix_start = 8 - suffix_len;
163 if (suffix_start > period_location)
164 suffix_start = period_location;
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100165 memcpy(dirent.name + suffix_start, buf, suffix_len);
166 if (*dirent.ext != ' ')
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100167 sprintf(buf, "%.*s.%.3s", suffix_start + suffix_len,
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100168 dirent.name, dirent.ext);
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100169 else
170 sprintf(buf, "%.*s", suffix_start + suffix_len,
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100171 dirent.name);
Heinrich Schuchardt24977e52020-11-25 16:33:55 +0100172 debug("generated short name: %s\n", buf);
173
174 /* Check that the short name does not exist yet. */
175 ret = fat_move_to_cluster(itr, itr->start_clust);
176 if (ret)
177 return ret;
178 if (find_directory_entry(itr, buf))
179 continue;
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100180
Heinrich Schuchardt24977e52020-11-25 16:33:55 +0100181 debug("chosen short name: %s\n", buf);
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100182 /* Each long name directory entry takes 13 characters. */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100183 ret = (strlen(filename) + 25) / 13;
184 goto out;
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100185 }
186 return -EIO;
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100187out:
188 memcpy(shortname, dirent.name, SHORT_NAME_SIZE);
189 return ret;
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100190}
191
Donggeun Kim8f814002011-10-24 21:15:28 +0000192static int total_sector;
Donggeun Kimcb5f3bc2012-03-22 04:38:55 +0000193static int disk_write(__u32 block, __u32 nr_blocks, void *buf)
Donggeun Kim8f814002011-10-24 21:15:28 +0000194{
Łukasz Majewskid6f23d82015-09-03 14:21:39 +0200195 ulong ret;
196
Simon Glass2ee8ada2016-02-29 15:25:52 -0700197 if (!cur_dev)
Donggeun Kim8f814002011-10-24 21:15:28 +0000198 return -1;
199
Donggeun Kimcb5f3bc2012-03-22 04:38:55 +0000200 if (cur_part_info.start + block + nr_blocks >
201 cur_part_info.start + total_sector) {
Donggeun Kim8f814002011-10-24 21:15:28 +0000202 printf("error: overflow occurs\n");
203 return -1;
204 }
205
Simon Glass2ee8ada2016-02-29 15:25:52 -0700206 ret = blk_dwrite(cur_dev, cur_part_info.start + block, nr_blocks, buf);
Łukasz Majewskid6f23d82015-09-03 14:21:39 +0200207 if (nr_blocks && ret == 0)
208 return -1;
209
210 return ret;
Donggeun Kim8f814002011-10-24 21:15:28 +0000211}
212
Donggeun Kim8f814002011-10-24 21:15:28 +0000213/*
214 * Write fat buffer into block device
215 */
Stefan Brüns751b31d2016-09-11 22:51:40 +0200216static int flush_dirty_fat_buffer(fsdata *mydata)
Donggeun Kim8f814002011-10-24 21:15:28 +0000217{
218 int getsize = FATBUFBLOCKS;
219 __u32 fatlength = mydata->fatlength;
220 __u8 *bufptr = mydata->fatbuf;
221 __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
222
Stefan Brüns751b31d2016-09-11 22:51:40 +0200223 debug("debug: evicting %d, dirty: %d\n", mydata->fatbufnum,
224 (int)mydata->fat_dirty);
225
226 if ((!mydata->fat_dirty) || (mydata->fatbufnum == -1))
227 return 0;
228
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100229 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
230 if (startblock + getsize > fatlength)
231 getsize = fatlength - startblock;
Donggeun Kim8f814002011-10-24 21:15:28 +0000232
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100233 startblock += mydata->fat_sect;
Donggeun Kim8f814002011-10-24 21:15:28 +0000234
235 /* Write FAT buf */
236 if (disk_write(startblock, getsize, bufptr) < 0) {
237 debug("error: writing FAT blocks\n");
238 return -1;
239 }
240
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900241 if (mydata->fats == 2) {
Donggeun Kimaf57ce22011-12-20 18:34:27 +0000242 /* Update corresponding second FAT blocks */
243 startblock += mydata->fatlength;
244 if (disk_write(startblock, getsize, bufptr) < 0) {
245 debug("error: writing second FAT blocks\n");
246 return -1;
247 }
248 }
Stefan Brüns751b31d2016-09-11 22:51:40 +0200249 mydata->fat_dirty = 0;
Donggeun Kimaf57ce22011-12-20 18:34:27 +0000250
Donggeun Kim8f814002011-10-24 21:15:28 +0000251 return 0;
252}
253
Heinrich Schuchardt5bc30a52020-11-22 09:58:44 +0100254/**
255 * fat_find_empty_dentries() - find a sequence of available directory entries
256 *
257 * @itr: directory iterator
258 * @count: number of directory entries to find
259 * Return: 0 on success or negative error number
260 */
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +0100261static int fat_find_empty_dentries(fat_itr *itr, int count)
Heinrich Schuchardt5bc30a52020-11-22 09:58:44 +0100262{
263 unsigned int cluster;
264 dir_entry *dent;
265 int remaining;
266 unsigned int n = 0;
267 int ret;
268
269 ret = fat_move_to_cluster(itr, itr->start_clust);
270 if (ret)
271 return ret;
272
273 for (;;) {
274 if (!itr->dent) {
275 log_debug("Not enough directory entries available\n");
276 return -ENOSPC;
277 }
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100278 switch (itr->dent->nameext.name[0]) {
Heinrich Schuchardt5bc30a52020-11-22 09:58:44 +0100279 case 0x00:
280 case DELETED_FLAG:
281 if (!n) {
282 /* Remember first deleted directory entry */
283 cluster = itr->clust;
284 dent = itr->dent;
285 remaining = itr->remaining;
286 }
287 ++n;
288 if (n == count)
289 goto out;
290 break;
291 default:
292 n = 0;
293 break;
294 }
295
296 next_dent(itr);
297 if (!itr->dent &&
298 (!itr->is_root || itr->fsdata->fatsize == 32) &&
299 new_dir_table(itr))
300 return -ENOSPC;
301 }
302out:
303 /* Position back to first directory entry */
304 if (itr->clust != cluster) {
305 ret = fat_move_to_cluster(itr, cluster);
306 if (ret)
307 return ret;
308 }
309 itr->dent = dent;
310 itr->remaining = remaining;
311 return 0;
312}
313
Donggeun Kim8f814002011-10-24 21:15:28 +0000314/*
Donggeun Kim8f814002011-10-24 21:15:28 +0000315 * Set the file name information from 'name' into 'slotptr',
316 */
317static int str2slot(dir_slot *slotptr, const char *name, int *idx)
318{
319 int j, end_idx = 0;
320
321 for (j = 0; j <= 8; j += 2) {
322 if (name[*idx] == 0x00) {
323 slotptr->name0_4[j] = 0;
324 slotptr->name0_4[j + 1] = 0;
325 end_idx++;
326 goto name0_4;
327 }
328 slotptr->name0_4[j] = name[*idx];
329 (*idx)++;
330 end_idx++;
331 }
332 for (j = 0; j <= 10; j += 2) {
333 if (name[*idx] == 0x00) {
334 slotptr->name5_10[j] = 0;
335 slotptr->name5_10[j + 1] = 0;
336 end_idx++;
337 goto name5_10;
338 }
339 slotptr->name5_10[j] = name[*idx];
340 (*idx)++;
341 end_idx++;
342 }
343 for (j = 0; j <= 2; j += 2) {
344 if (name[*idx] == 0x00) {
345 slotptr->name11_12[j] = 0;
346 slotptr->name11_12[j + 1] = 0;
347 end_idx++;
348 goto name11_12;
349 }
350 slotptr->name11_12[j] = name[*idx];
351 (*idx)++;
352 end_idx++;
353 }
354
355 if (name[*idx] == 0x00)
356 return 1;
357
358 return 0;
359/* Not used characters are filled with 0xff 0xff */
360name0_4:
361 for (; end_idx < 5; end_idx++) {
362 slotptr->name0_4[end_idx * 2] = 0xff;
363 slotptr->name0_4[end_idx * 2 + 1] = 0xff;
364 }
365 end_idx = 5;
366name5_10:
367 end_idx -= 5;
368 for (; end_idx < 6; end_idx++) {
369 slotptr->name5_10[end_idx * 2] = 0xff;
370 slotptr->name5_10[end_idx * 2 + 1] = 0xff;
371 }
372 end_idx = 11;
373name11_12:
374 end_idx -= 11;
375 for (; end_idx < 2; end_idx++) {
376 slotptr->name11_12[end_idx * 2] = 0xff;
377 slotptr->name11_12[end_idx * 2 + 1] = 0xff;
378 }
379
380 return 1;
381}
382
AKASHI Takahiroa2c0bd02019-05-24 14:10:36 +0900383static int flush_dir(fat_itr *itr);
Donggeun Kim8f814002011-10-24 21:15:28 +0000384
Heinrich Schuchardtac4ab752020-11-21 08:32:50 +0100385/**
386 * fill_dir_slot() - fill directory entries for long name
387 *
388 * @itr: directory iterator
389 * @l_name: long name
390 * @shortname: short name
391 * Return: 0 for success, -errno otherwise
Donggeun Kim8f814002011-10-24 21:15:28 +0000392 */
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900393static int
Heinrich Schuchardtac4ab752020-11-21 08:32:50 +0100394fill_dir_slot(fat_itr *itr, const char *l_name, const char *shortname)
Donggeun Kim8f814002011-10-24 21:15:28 +0000395{
Tien Fong Chee0117dbe2016-07-27 23:08:56 -0700396 __u8 temp_dir_slot_buffer[MAX_LFN_SLOT * sizeof(dir_slot)];
397 dir_slot *slotptr = (dir_slot *)temp_dir_slot_buffer;
Anatolij Gustschinf4f9d5d2011-12-15 03:12:14 +0000398 __u8 counter = 0, checksum;
Donggeun Kim8f814002011-10-24 21:15:28 +0000399 int idx = 0, ret;
Donggeun Kim8f814002011-10-24 21:15:28 +0000400
Stefan Brüns6c617d62016-09-11 22:51:39 +0200401 /* Get short file name checksum value */
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100402 checksum = mkcksum((void *)shortname);
Donggeun Kim8f814002011-10-24 21:15:28 +0000403
404 do {
405 memset(slotptr, 0x00, sizeof(dir_slot));
406 ret = str2slot(slotptr, l_name, &idx);
407 slotptr->id = ++counter;
408 slotptr->attr = ATTR_VFAT;
409 slotptr->alias_checksum = checksum;
410 slotptr++;
411 } while (ret == 0);
412
413 slotptr--;
414 slotptr->id |= LAST_LONG_ENTRY_MASK;
415
416 while (counter >= 1) {
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900417 memcpy(itr->dent, slotptr, sizeof(dir_slot));
Donggeun Kim8f814002011-10-24 21:15:28 +0000418 slotptr--;
419 counter--;
AKASHI Takahiroa2c0bd02019-05-24 14:10:36 +0900420
Heinrich Schuchardt913339a2021-01-20 22:21:53 +0100421 if (!itr->remaining) {
422 /* Write directory table to device */
423 ret = flush_dir(itr);
424 if (ret)
425 return ret;
426 }
AKASHI Takahiroa2c0bd02019-05-24 14:10:36 +0900427
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +0100428 next_dent(itr);
429 if (!itr->dent)
430 return -EIO;
Donggeun Kim8f814002011-10-24 21:15:28 +0000431 }
432
Donggeun Kim8f814002011-10-24 21:15:28 +0000433 return 0;
434}
435
Donggeun Kim8f814002011-10-24 21:15:28 +0000436/*
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500437 * Set the entry at index 'entry' in a FAT (12/16/32) table.
Donggeun Kim8f814002011-10-24 21:15:28 +0000438 */
439static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
440{
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500441 __u32 bufnum, offset, off16;
442 __u16 val1, val2;
Donggeun Kim8f814002011-10-24 21:15:28 +0000443
444 switch (mydata->fatsize) {
445 case 32:
446 bufnum = entry / FAT32BUFSIZE;
447 offset = entry - bufnum * FAT32BUFSIZE;
448 break;
449 case 16:
450 bufnum = entry / FAT16BUFSIZE;
451 offset = entry - bufnum * FAT16BUFSIZE;
452 break;
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500453 case 12:
454 bufnum = entry / FAT12BUFSIZE;
455 offset = entry - bufnum * FAT12BUFSIZE;
456 break;
Donggeun Kim8f814002011-10-24 21:15:28 +0000457 default:
458 /* Unsupported FAT size */
459 return -1;
460 }
461
462 /* Read a new block of FAT entries into the cache. */
463 if (bufnum != mydata->fatbufnum) {
464 int getsize = FATBUFBLOCKS;
465 __u8 *bufptr = mydata->fatbuf;
466 __u32 fatlength = mydata->fatlength;
467 __u32 startblock = bufnum * FATBUFBLOCKS;
468
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100469 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
470 if (startblock + getsize > fatlength)
471 getsize = fatlength - startblock;
Donggeun Kim8f814002011-10-24 21:15:28 +0000472
Stefan Brüns751b31d2016-09-11 22:51:40 +0200473 if (flush_dirty_fat_buffer(mydata) < 0)
474 return -1;
Donggeun Kim8f814002011-10-24 21:15:28 +0000475
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100476 startblock += mydata->fat_sect;
477
Donggeun Kim8f814002011-10-24 21:15:28 +0000478 if (disk_read(startblock, getsize, bufptr) < 0) {
479 debug("Error reading FAT blocks\n");
480 return -1;
481 }
482 mydata->fatbufnum = bufnum;
483 }
484
Stefan Brüns751b31d2016-09-11 22:51:40 +0200485 /* Mark as dirty */
486 mydata->fat_dirty = 1;
487
Donggeun Kim8f814002011-10-24 21:15:28 +0000488 /* Set the actual entry */
489 switch (mydata->fatsize) {
490 case 32:
491 ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
492 break;
493 case 16:
494 ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
495 break;
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500496 case 12:
497 off16 = (offset * 3) / 4;
498
499 switch (offset & 0x3) {
500 case 0:
501 val1 = cpu_to_le16(entry_value) & 0xfff;
502 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff;
503 ((__u16 *)mydata->fatbuf)[off16] |= val1;
504 break;
505 case 1:
506 val1 = cpu_to_le16(entry_value) & 0xf;
507 val2 = (cpu_to_le16(entry_value) >> 4) & 0xff;
508
509 ((__u16 *)mydata->fatbuf)[off16] &= ~0xf000;
510 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 12);
511
512 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xff;
513 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
514 break;
515 case 2:
516 val1 = cpu_to_le16(entry_value) & 0xff;
517 val2 = (cpu_to_le16(entry_value) >> 8) & 0xf;
518
519 ((__u16 *)mydata->fatbuf)[off16] &= ~0xff00;
520 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 8);
521
522 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xf;
523 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
524 break;
525 case 3:
526 val1 = cpu_to_le16(entry_value) & 0xfff;
527 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff0;
528 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 4);
529 break;
530 default:
531 break;
532 }
533
534 break;
Donggeun Kim8f814002011-10-24 21:15:28 +0000535 default:
536 return -1;
537 }
538
539 return 0;
540}
541
542/*
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500543 * Determine the next free cluster after 'entry' in a FAT (12/16/32) table
Stefan Brüns14999402016-09-11 22:51:41 +0200544 * and link it to 'entry'. EOC marker is not set on returned entry.
Donggeun Kim8f814002011-10-24 21:15:28 +0000545 */
546static __u32 determine_fatent(fsdata *mydata, __u32 entry)
547{
548 __u32 next_fat, next_entry = entry + 1;
549
550 while (1) {
Stefan Brüns9f95d812016-12-17 00:27:51 +0100551 next_fat = get_fatent(mydata, next_entry);
Donggeun Kim8f814002011-10-24 21:15:28 +0000552 if (next_fat == 0) {
Stefan Brüns14999402016-09-11 22:51:41 +0200553 /* found free entry, link to entry */
Donggeun Kim8f814002011-10-24 21:15:28 +0000554 set_fatent_value(mydata, entry, next_entry);
555 break;
556 }
557 next_entry++;
558 }
559 debug("FAT%d: entry: %08x, entry_value: %04x\n",
560 mydata->fatsize, entry, next_entry);
561
562 return next_entry;
563}
564
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200565/**
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900566 * set_sectors() - write data to sectors
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200567 *
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900568 * Write 'size' bytes from 'buffer' into the specified sector.
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200569 *
570 * @mydata: data to be written
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900571 * @startsect: sector to be written to
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200572 * @buffer: data to be written
573 * @size: bytes to be written (but not more than the size of a cluster)
574 * Return: 0 on success, -1 otherwise
Donggeun Kim8f814002011-10-24 21:15:28 +0000575 */
576static int
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900577set_sectors(fsdata *mydata, u32 startsect, u8 *buffer, u32 size)
Donggeun Kim8f814002011-10-24 21:15:28 +0000578{
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900579 u32 nsects = 0;
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200580 int ret;
Donggeun Kim8f814002011-10-24 21:15:28 +0000581
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900582 debug("startsect: %d\n", startsect);
Donggeun Kim8f814002011-10-24 21:15:28 +0000583
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200584 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
585 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
586
Heinrich Schuchardtaa9d6ba2018-09-13 19:42:47 +0200587 debug("FAT: Misaligned buffer address (%p)\n", buffer);
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200588
589 while (size >= mydata->sect_size) {
590 memcpy(tmpbuf, buffer, mydata->sect_size);
591 ret = disk_write(startsect++, 1, tmpbuf);
592 if (ret != 1) {
593 debug("Error writing data (got %d)\n", ret);
594 return -1;
595 }
596
597 buffer += mydata->sect_size;
598 size -= mydata->sect_size;
599 }
600 } else if (size >= mydata->sect_size) {
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;
791 buffer += wsize;
792 *gotsize += wsize;
793 }
794
795 assert(!size);
796
797 return 0;
798}
799
Donggeun Kim8f814002011-10-24 21:15:28 +0000800/*
801 * Find the first empty cluster
802 */
803static int find_empty_cluster(fsdata *mydata)
804{
805 __u32 fat_val, entry = 3;
806
807 while (1) {
Stefan Brüns9f95d812016-12-17 00:27:51 +0100808 fat_val = get_fatent(mydata, entry);
Donggeun Kim8f814002011-10-24 21:15:28 +0000809 if (fat_val == 0)
810 break;
811 entry++;
812 }
813
814 return entry;
815}
816
Heinrich Schuchardte09f3e42020-11-26 19:06:55 +0100817/**
818 * new_dir_table() - allocate a cluster for additional directory entries
819 *
820 * @itr: directory iterator
821 * Return: 0 on success, -EIO otherwise
Donggeun Kim8f814002011-10-24 21:15:28 +0000822 */
AKASHI Takahiroa2c0bd02019-05-24 14:10:36 +0900823static int new_dir_table(fat_itr *itr)
Donggeun Kim8f814002011-10-24 21:15:28 +0000824{
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900825 fsdata *mydata = itr->fsdata;
Donggeun Kim8f814002011-10-24 21:15:28 +0000826 int dir_newclust = 0;
Heinrich Schuchardte09f3e42020-11-26 19:06:55 +0100827 int dir_oldclust = itr->clust;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900828 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
Donggeun Kim8f814002011-10-24 21:15:28 +0000829
Donggeun Kim8f814002011-10-24 21:15:28 +0000830 dir_newclust = find_empty_cluster(mydata);
Heinrich Schuchardte09f3e42020-11-26 19:06:55 +0100831
832 /*
833 * Flush before updating FAT to ensure valid directory structure
834 * in case of failure.
835 */
836 itr->clust = dir_newclust;
837 itr->next_clust = dir_newclust;
838 memset(itr->block, 0x00, bytesperclust);
839 if (flush_dir(itr))
840 return -EIO;
841
842 set_fatent_value(mydata, dir_oldclust, dir_newclust);
Donggeun Kim8f814002011-10-24 21:15:28 +0000843 if (mydata->fatsize == 32)
844 set_fatent_value(mydata, dir_newclust, 0xffffff8);
845 else if (mydata->fatsize == 16)
846 set_fatent_value(mydata, dir_newclust, 0xfff8);
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500847 else if (mydata->fatsize == 12)
848 set_fatent_value(mydata, dir_newclust, 0xff8);
Donggeun Kim8f814002011-10-24 21:15:28 +0000849
Stefan Brüns751b31d2016-09-11 22:51:40 +0200850 if (flush_dirty_fat_buffer(mydata) < 0)
Heinrich Schuchardte09f3e42020-11-26 19:06:55 +0100851 return -EIO;
Donggeun Kim8f814002011-10-24 21:15:28 +0000852
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900853 itr->dent = (dir_entry *)itr->block;
854 itr->last_cluster = 1;
855 itr->remaining = bytesperclust / sizeof(dir_entry) - 1;
Donggeun Kim8f814002011-10-24 21:15:28 +0000856
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900857 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +0000858}
859
860/*
861 * Set empty cluster from 'entry' to the end of a file
862 */
863static int clear_fatent(fsdata *mydata, __u32 entry)
864{
865 __u32 fat_val;
866
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500867 while (!CHECK_CLUST(entry, mydata->fatsize)) {
Stefan Brüns9f95d812016-12-17 00:27:51 +0100868 fat_val = get_fatent(mydata, entry);
Donggeun Kim8f814002011-10-24 21:15:28 +0000869 if (fat_val != 0)
870 set_fatent_value(mydata, entry, 0);
871 else
872 break;
873
Donggeun Kim8f814002011-10-24 21:15:28 +0000874 entry = fat_val;
875 }
876
877 /* Flush fat buffer */
Stefan Brüns751b31d2016-09-11 22:51:40 +0200878 if (flush_dirty_fat_buffer(mydata) < 0)
Donggeun Kim8f814002011-10-24 21:15:28 +0000879 return -1;
880
881 return 0;
882}
883
884/*
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +0900885 * Set start cluster in directory entry
886 */
887static void set_start_cluster(const fsdata *mydata, dir_entry *dentptr,
888 __u32 start_cluster)
889{
890 if (mydata->fatsize == 32)
891 dentptr->starthi =
892 cpu_to_le16((start_cluster & 0xffff0000) >> 16);
893 dentptr->start = cpu_to_le16(start_cluster & 0xffff);
894}
895
896/*
897 * Check whether adding a file makes the file system to
898 * exceed the size of the block device
899 * Return -1 when overflow occurs, otherwise return 0
900 */
901static int check_overflow(fsdata *mydata, __u32 clustnum, loff_t size)
902{
903 __u32 startsect, sect_num, offset;
904
905 if (clustnum > 0)
906 startsect = clust_to_sect(mydata, clustnum);
907 else
908 startsect = mydata->rootdir_sect;
909
910 sect_num = div_u64_rem(size, mydata->sect_size, &offset);
911
912 if (offset != 0)
913 sect_num++;
914
915 if (startsect + sect_num > total_sector)
916 return -1;
917 return 0;
918}
919
920/*
Donggeun Kim8f814002011-10-24 21:15:28 +0000921 * Write at most 'maxsize' bytes from 'buffer' into
922 * the file associated with 'dentptr'
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800923 * Update the number of bytes written in *gotsize and return 0
924 * or return -1 on fatal errors.
Donggeun Kim8f814002011-10-24 21:15:28 +0000925 */
926static int
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +0900927set_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, __u8 *buffer,
928 loff_t maxsize, loff_t *gotsize)
Donggeun Kim8f814002011-10-24 21:15:28 +0000929{
Donggeun Kim8f814002011-10-24 21:15:28 +0000930 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
931 __u32 curclust = START(dentptr);
932 __u32 endclust = 0, newclust = 0;
Heinrich Schuchardtdb538a92019-02-25 19:42:48 +0100933 u64 cur_pos, filesize;
934 loff_t offset, actsize, wsize;
Donggeun Kim8f814002011-10-24 21:15:28 +0000935
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800936 *gotsize = 0;
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900937 filesize = pos + maxsize;
Donggeun Kim8f814002011-10-24 21:15:28 +0000938
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800939 debug("%llu bytes\n", filesize);
Donggeun Kim8f814002011-10-24 21:15:28 +0000940
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900941 if (!filesize) {
942 if (!curclust)
943 return 0;
944 if (!CHECK_CLUST(curclust, mydata->fatsize) ||
945 IS_LAST_CLUST(curclust, mydata->fatsize)) {
946 clear_fatent(mydata, curclust);
947 set_start_cluster(mydata, dentptr, 0);
948 return 0;
949 }
950 debug("curclust: 0x%x\n", curclust);
951 debug("Invalid FAT entry\n");
952 return -1;
953 }
954
955 if (!curclust) {
956 assert(pos == 0);
957 goto set_clusters;
958 }
959
960 /* go to cluster at pos */
961 cur_pos = bytesperclust;
962 while (1) {
963 if (pos <= cur_pos)
964 break;
965 if (IS_LAST_CLUST(curclust, mydata->fatsize))
966 break;
967
968 newclust = get_fatent(mydata, curclust);
969 if (!IS_LAST_CLUST(newclust, mydata->fatsize) &&
970 CHECK_CLUST(newclust, mydata->fatsize)) {
971 debug("curclust: 0x%x\n", curclust);
972 debug("Invalid FAT entry\n");
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +0200973 return -1;
974 }
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900975
976 cur_pos += bytesperclust;
977 curclust = newclust;
978 }
979 if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
980 assert(pos == cur_pos);
981 goto set_clusters;
982 }
983
984 assert(pos < cur_pos);
985 cur_pos -= bytesperclust;
986
987 /* overwrite */
988 assert(IS_LAST_CLUST(curclust, mydata->fatsize) ||
989 !CHECK_CLUST(curclust, mydata->fatsize));
990
991 while (1) {
992 /* search for allocated consecutive clusters */
993 actsize = bytesperclust;
994 endclust = curclust;
995 while (1) {
996 if (filesize <= (cur_pos + actsize))
997 break;
998
999 newclust = get_fatent(mydata, endclust);
1000
Marek Szyprowski2f241672019-12-02 12:11:13 +01001001 if (newclust != endclust + 1)
1002 break;
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001003 if (IS_LAST_CLUST(newclust, mydata->fatsize))
1004 break;
1005 if (CHECK_CLUST(newclust, mydata->fatsize)) {
1006 debug("curclust: 0x%x\n", curclust);
1007 debug("Invalid FAT entry\n");
1008 return -1;
1009 }
1010
1011 actsize += bytesperclust;
1012 endclust = newclust;
1013 }
1014
1015 /* overwrite to <curclust..endclust> */
1016 if (pos < cur_pos)
1017 offset = 0;
1018 else
1019 offset = pos - cur_pos;
Marek Szyprowski3a867ce2019-12-02 12:11:14 +01001020 wsize = min_t(unsigned long long, actsize, filesize - cur_pos);
1021 wsize -= offset;
1022
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001023 if (get_set_cluster(mydata, curclust, offset,
1024 buffer, wsize, &actsize)) {
1025 printf("Error get-and-setting cluster\n");
1026 return -1;
1027 }
1028 buffer += wsize;
1029 *gotsize += wsize;
1030 cur_pos += offset + wsize;
1031
1032 if (filesize <= cur_pos)
1033 break;
1034
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001035 if (IS_LAST_CLUST(newclust, mydata->fatsize))
1036 /* no more clusters */
1037 break;
1038
1039 curclust = newclust;
1040 }
1041
1042 if (filesize <= cur_pos) {
1043 /* no more write */
1044 newclust = get_fatent(mydata, endclust);
1045 if (!IS_LAST_CLUST(newclust, mydata->fatsize)) {
1046 /* truncate the rest */
1047 clear_fatent(mydata, newclust);
1048
1049 /* Mark end of file in FAT */
1050 if (mydata->fatsize == 12)
1051 newclust = 0xfff;
1052 else if (mydata->fatsize == 16)
1053 newclust = 0xffff;
1054 else if (mydata->fatsize == 32)
1055 newclust = 0xfffffff;
1056 set_fatent_value(mydata, endclust, newclust);
1057 }
1058
1059 return 0;
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001060 }
1061
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001062 curclust = endclust;
1063 filesize -= cur_pos;
Heinrich Schuchardtdb538a92019-02-25 19:42:48 +01001064 assert(!do_div(cur_pos, bytesperclust));
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001065
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001066set_clusters:
1067 /* allocate and write */
1068 assert(!pos);
1069
1070 /* Assure that curclust is valid */
1071 if (!curclust) {
1072 curclust = find_empty_cluster(mydata);
1073 set_start_cluster(mydata, dentptr, curclust);
1074 } else {
1075 newclust = get_fatent(mydata, curclust);
1076
1077 if (IS_LAST_CLUST(newclust, mydata->fatsize)) {
1078 newclust = determine_fatent(mydata, curclust);
1079 set_fatent_value(mydata, curclust, newclust);
1080 curclust = newclust;
1081 } else {
1082 debug("error: something wrong\n");
1083 return -1;
1084 }
1085 }
1086
1087 /* TODO: already partially written */
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001088 if (check_overflow(mydata, curclust, filesize)) {
1089 printf("Error: no space left: %llu\n", filesize);
1090 return -1;
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +02001091 }
1092
Donggeun Kim8f814002011-10-24 21:15:28 +00001093 actsize = bytesperclust;
1094 endclust = curclust;
1095 do {
1096 /* search for consecutive clusters */
1097 while (actsize < filesize) {
1098 newclust = determine_fatent(mydata, endclust);
1099
1100 if ((newclust - 1) != endclust)
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001101 /* write to <curclust..endclust> */
Donggeun Kim8f814002011-10-24 21:15:28 +00001102 goto getit;
1103
1104 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Benoît Thébaudeaue0b86942015-09-28 15:45:30 +02001105 debug("newclust: 0x%x\n", newclust);
Donggeun Kim8f814002011-10-24 21:15:28 +00001106 debug("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001107 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +00001108 }
1109 endclust = newclust;
1110 actsize += bytesperclust;
1111 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001112
1113 /* set remaining bytes */
Donggeun Kim8f814002011-10-24 21:15:28 +00001114 actsize = filesize;
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +02001115 if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
Donggeun Kim8f814002011-10-24 21:15:28 +00001116 debug("error: writing cluster\n");
1117 return -1;
1118 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001119 *gotsize += actsize;
Donggeun Kim8f814002011-10-24 21:15:28 +00001120
1121 /* Mark end of file in FAT */
Philipp Skadorov16f553d2016-12-15 15:52:53 -05001122 if (mydata->fatsize == 12)
1123 newclust = 0xfff;
1124 else if (mydata->fatsize == 16)
Donggeun Kim8f814002011-10-24 21:15:28 +00001125 newclust = 0xffff;
1126 else if (mydata->fatsize == 32)
1127 newclust = 0xfffffff;
1128 set_fatent_value(mydata, endclust, newclust);
1129
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001130 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +00001131getit:
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +02001132 if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
Donggeun Kim8f814002011-10-24 21:15:28 +00001133 debug("error: writing cluster\n");
1134 return -1;
1135 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001136 *gotsize += actsize;
Donggeun Kim8f814002011-10-24 21:15:28 +00001137 filesize -= actsize;
1138 buffer += actsize;
1139
Benoît Thébaudeaue0b86942015-09-28 15:45:30 +02001140 if (CHECK_CLUST(newclust, mydata->fatsize)) {
1141 debug("newclust: 0x%x\n", newclust);
Donggeun Kim8f814002011-10-24 21:15:28 +00001142 debug("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001143 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +00001144 }
1145 actsize = bytesperclust;
1146 curclust = endclust = newclust;
1147 } while (1);
Donggeun Kim8f814002011-10-24 21:15:28 +00001148
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001149 return 0;
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +02001150}
1151
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001152/**
1153 * fill_dentry() - fill directory entry with shortname
1154 *
1155 * @mydata: private filesystem parameters
1156 * @dentptr: directory entry
1157 * @shortname: chosen short name
1158 * @start_cluster: first cluster of file
1159 * @size: file size
1160 * @attr: file attributes
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +02001161 */
1162static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001163 const char *shortname, __u32 start_cluster, __u32 size, __u8 attr)
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +02001164{
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001165 memset(dentptr, 0, sizeof(*dentptr));
1166
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +02001167 set_start_cluster(mydata, dentptr, start_cluster);
Donggeun Kim8f814002011-10-24 21:15:28 +00001168 dentptr->size = cpu_to_le32(size);
1169
1170 dentptr->attr = attr;
1171
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +01001172 memcpy(&dentptr->nameext, shortname, SHORT_NAME_SIZE);
Donggeun Kim8f814002011-10-24 21:15:28 +00001173}
1174
Heinrich Schuchardt7a3f6102020-11-26 16:10:01 +01001175/**
1176 * find_directory_entry() - find a directory entry by filename
1177 *
1178 * @itr: directory iterator
1179 * @filename: name of file to find
1180 * Return: directory entry or NULL
Donggeun Kim8f814002011-10-24 21:15:28 +00001181 */
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001182static dir_entry *find_directory_entry(fat_itr *itr, char *filename)
Donggeun Kim8f814002011-10-24 21:15:28 +00001183{
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001184 int match = 0;
Donggeun Kim8f814002011-10-24 21:15:28 +00001185
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001186 while (fat_itr_next(itr)) {
1187 /* check both long and short name: */
1188 if (!strcasecmp(filename, itr->name))
1189 match = 1;
1190 else if (itr->name != itr->s_name &&
1191 !strcasecmp(filename, itr->s_name))
1192 match = 1;
Donggeun Kim8f814002011-10-24 21:15:28 +00001193
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001194 if (!match)
1195 continue;
Donggeun Kim8f814002011-10-24 21:15:28 +00001196
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +01001197 if (itr->dent->nameext.name[0] == '\0')
Donggeun Kim8f814002011-10-24 21:15:28 +00001198 return NULL;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001199 else
1200 return itr->dent;
1201 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001202
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001203 return NULL;
1204}
Donggeun Kim8f814002011-10-24 21:15:28 +00001205
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001206static int split_filename(char *filename, char **dirname, char **basename)
1207{
1208 char *p, *last_slash, *last_slash_cont;
Donggeun Kim8f814002011-10-24 21:15:28 +00001209
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001210again:
1211 p = filename;
1212 last_slash = NULL;
1213 last_slash_cont = NULL;
1214 while (*p) {
1215 if (ISDIRDELIM(*p)) {
1216 last_slash = p;
1217 last_slash_cont = p;
1218 /* continuous slashes */
1219 while (ISDIRDELIM(*p))
1220 last_slash_cont = p++;
1221 if (!*p)
1222 break;
Donggeun Kim8f814002011-10-24 21:15:28 +00001223 }
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001224 p++;
1225 }
Wu, Josh675b23c2014-05-08 16:14:07 +08001226
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001227 if (last_slash) {
1228 if (last_slash_cont == (filename + strlen(filename) - 1)) {
1229 /* remove trailing slashes */
1230 *last_slash = '\0';
1231 goto again;
Wu, Josh675b23c2014-05-08 16:14:07 +08001232 }
1233
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001234 if (last_slash == filename) {
1235 /* avoid ""(null) directory */
1236 *dirname = "/";
1237 } else {
1238 *last_slash = '\0';
1239 *dirname = filename;
Donggeun Kim8f814002011-10-24 21:15:28 +00001240 }
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001241
1242 *last_slash_cont = '\0';
1243 *basename = last_slash_cont + 1;
1244 } else {
1245 *dirname = "/"; /* root by default */
1246 *basename = filename;
Donggeun Kim8f814002011-10-24 21:15:28 +00001247 }
1248
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001249 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +00001250}
1251
Heinrich Schuchardt58587212019-05-12 09:59:18 +02001252/**
1253 * normalize_longname() - check long file name and convert to lower case
1254 *
1255 * We assume here that the FAT file system is using an 8bit code page.
1256 * Linux typically uses CP437, EDK2 assumes CP1250.
1257 *
1258 * @l_filename: preallocated buffer receiving the normalized name
1259 * @filename: filename to normalize
1260 * Return: 0 on success, -1 on failure
1261 */
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001262static int normalize_longname(char *l_filename, const char *filename)
1263{
Heinrich Schuchardt58587212019-05-12 09:59:18 +02001264 const char *p, illegal[] = "<>:\"/\\|?*";
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001265
Heinrich Schuchardt58587212019-05-12 09:59:18 +02001266 if (strlen(filename) >= VFAT_MAXLEN_BYTES)
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001267 return -1;
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001268
Heinrich Schuchardt58587212019-05-12 09:59:18 +02001269 for (p = filename; *p; ++p) {
1270 if ((unsigned char)*p < 0x20)
1271 return -1;
1272 if (strchr(illegal, *p))
1273 return -1;
1274 }
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001275
Heinrich Schuchardt58587212019-05-12 09:59:18 +02001276 strcpy(l_filename, filename);
1277 downcase(l_filename, VFAT_MAXLEN_BYTES);
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001278
1279 return 0;
1280}
1281
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001282int file_fat_write_at(const char *filename, loff_t pos, void *buffer,
1283 loff_t size, loff_t *actwrite)
Donggeun Kim8f814002011-10-24 21:15:28 +00001284{
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001285 dir_entry *retdent;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001286 fsdata datablock = { .fatbuf = NULL, };
Donggeun Kim8f814002011-10-24 21:15:28 +00001287 fsdata *mydata = &datablock;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001288 fat_itr *itr = NULL;
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001289 int ret = -1;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001290 char *filename_copy, *parent, *basename;
Donggeun Kim8f814002011-10-24 21:15:28 +00001291 char l_filename[VFAT_MAXLEN_BYTES];
1292
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001293 debug("writing %s\n", filename);
1294
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001295 filename_copy = strdup(filename);
1296 if (!filename_copy)
1297 return -ENOMEM;
Donggeun Kim8f814002011-10-24 21:15:28 +00001298
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001299 split_filename(filename_copy, &parent, &basename);
1300 if (!strlen(basename)) {
1301 ret = -EINVAL;
1302 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001303 }
1304
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001305 filename = basename;
1306 if (normalize_longname(l_filename, filename)) {
1307 printf("FAT: illegal filename (%s)\n", filename);
1308 ret = -EINVAL;
1309 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001310 }
1311
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001312 itr = malloc_cache_aligned(sizeof(fat_itr));
1313 if (!itr) {
1314 ret = -ENOMEM;
1315 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001316 }
1317
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001318 ret = fat_itr_root(itr, &datablock);
1319 if (ret)
Donggeun Kim8f814002011-10-24 21:15:28 +00001320 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001321
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001322 total_sector = datablock.total_sect;
1323
1324 ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1325 if (ret) {
1326 printf("%s: doesn't exist (%d)\n", parent, ret);
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001327 goto exit;
1328 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001329
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001330 retdent = find_directory_entry(itr, l_filename);
1331
Donggeun Kim8f814002011-10-24 21:15:28 +00001332 if (retdent) {
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001333 if (fat_itr_isdir(itr)) {
1334 ret = -EISDIR;
1335 goto exit;
1336 }
1337
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001338 /* A file exists */
1339 if (pos == -1)
1340 /* Append to the end */
1341 pos = FAT2CPU32(retdent->size);
1342 if (pos > retdent->size) {
1343 /* No hole allowed */
1344 ret = -EINVAL;
1345 goto exit;
1346 }
1347
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001348 /* Update file size in a directory entry */
1349 retdent->size = cpu_to_le32(pos + size);
Donggeun Kim8f814002011-10-24 21:15:28 +00001350 } else {
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001351 /* Create a new file */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001352 char shortname[SHORT_NAME_SIZE];
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +01001353 int ndent;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001354
1355 if (itr->is_root) {
1356 /* root dir cannot have "." or ".." */
1357 if (!strcmp(l_filename, ".") ||
1358 !strcmp(l_filename, "..")) {
1359 ret = -EINVAL;
1360 goto exit;
1361 }
1362 }
1363
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001364 if (pos) {
1365 /* No hole allowed */
1366 ret = -EINVAL;
1367 goto exit;
1368 }
1369
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001370 /* Check if long name is needed */
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +01001371 ndent = set_name(itr, filename, shortname);
1372 if (ndent < 0) {
1373 ret = ndent;
1374 goto exit;
1375 }
1376 ret = fat_find_empty_dentries(itr, ndent);
1377 if (ret)
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001378 goto exit;
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +01001379 if (ndent > 1) {
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001380 /* Set long name entries */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001381 ret = fill_dir_slot(itr, filename, shortname);
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001382 if (ret)
1383 goto exit;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001384 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001385
AKASHI Takahiroa2c0bd02019-05-24 14:10:36 +09001386 /* Set short name entry */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001387 fill_dentry(itr->fsdata, itr->dent, shortname, 0, size,
Heinrich Schuchardt881c74a2020-11-22 11:13:33 +01001388 ATTR_ARCH);
Donggeun Kim8f814002011-10-24 21:15:28 +00001389
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001390 retdent = itr->dent;
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001391 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001392
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001393 ret = set_contents(mydata, retdent, pos, buffer, size, actwrite);
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001394 if (ret < 0) {
1395 printf("Error: writing contents\n");
AKASHI Takahiro6aa77692018-09-11 15:59:03 +09001396 ret = -EIO;
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001397 goto exit;
1398 }
1399 debug("attempt to write 0x%llx bytes\n", *actwrite);
Donggeun Kim8f814002011-10-24 21:15:28 +00001400
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001401 /* Flush fat buffer */
Stefan Brüns751b31d2016-09-11 22:51:40 +02001402 ret = flush_dirty_fat_buffer(mydata);
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001403 if (ret) {
1404 printf("Error: flush fat buffer\n");
AKASHI Takahiro6aa77692018-09-11 15:59:03 +09001405 ret = -EIO;
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001406 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001407 }
1408
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001409 /* Write directory table to device */
AKASHI Takahirod98c6742019-05-24 14:10:35 +09001410 ret = flush_dir(itr);
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001411
Donggeun Kim8f814002011-10-24 21:15:28 +00001412exit:
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001413 free(filename_copy);
Donggeun Kim8f814002011-10-24 21:15:28 +00001414 free(mydata->fatbuf);
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001415 free(itr);
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001416 return ret;
Donggeun Kim8f814002011-10-24 21:15:28 +00001417}
1418
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001419int file_fat_write(const char *filename, void *buffer, loff_t offset,
1420 loff_t maxsize, loff_t *actwrite)
Donggeun Kim8f814002011-10-24 21:15:28 +00001421{
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001422 return file_fat_write_at(filename, offset, buffer, maxsize, actwrite);
Donggeun Kim8f814002011-10-24 21:15:28 +00001423}
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001424
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001425static int fat_dir_entries(fat_itr *itr)
1426{
1427 fat_itr *dirs;
1428 fsdata fsdata = { .fatbuf = NULL, }, *mydata = &fsdata;
1429 /* for FATBUFSIZE */
1430 int count;
1431
1432 dirs = malloc_cache_aligned(sizeof(fat_itr));
1433 if (!dirs) {
1434 debug("Error: allocating memory\n");
1435 count = -ENOMEM;
1436 goto exit;
1437 }
1438
1439 /* duplicate fsdata */
1440 fat_itr_child(dirs, itr);
1441 fsdata = *dirs->fsdata;
1442
1443 /* allocate local fat buffer */
1444 fsdata.fatbuf = malloc_cache_aligned(FATBUFSIZE);
1445 if (!fsdata.fatbuf) {
1446 debug("Error: allocating memory\n");
1447 count = -ENOMEM;
1448 goto exit;
1449 }
1450 fsdata.fatbufnum = -1;
1451 dirs->fsdata = &fsdata;
1452
1453 for (count = 0; fat_itr_next(dirs); count++)
1454 ;
1455
1456exit:
1457 free(fsdata.fatbuf);
1458 free(dirs);
1459 return count;
1460}
1461
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001462/**
1463 * delete_single_dentry() - delete a single directory entry
1464 *
1465 * @itr: directory iterator
1466 * Return: 0 for success
1467 */
1468static int delete_single_dentry(fat_itr *itr)
1469{
1470 struct dir_entry *dent = itr->dent;
1471
1472 memset(dent, 0, sizeof(*dent));
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +01001473 dent->nameext.name[0] = DELETED_FLAG;
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001474
Heinrich Schuchardt913339a2021-01-20 22:21:53 +01001475 if (!itr->remaining)
1476 return flush_dir(itr);
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001477 return 0;
1478}
1479
1480/**
1481 * delete_long_name() - delete long name directory entries
1482 *
1483 * @itr: directory iterator
1484 * Return: 0 for success
1485 */
1486static int delete_long_name(fat_itr *itr)
1487{
1488 struct dir_entry *dent = itr->dent;
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +01001489 int seqn = itr->dent->nameext.name[0] & ~LAST_LONG_ENTRY_MASK;
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001490
1491 while (seqn--) {
1492 int ret;
1493
1494 ret = delete_single_dentry(itr);
1495 if (ret)
1496 return ret;
1497 dent = next_dent(itr);
1498 if (!dent)
1499 return -EIO;
1500 }
1501 return 0;
1502}
1503
1504/**
1505 * delete_dentry_long() - remove directory entry
1506 *
1507 * @itr: directory iterator
1508 * Return: 0 for success
1509 */
1510static int delete_dentry_long(fat_itr *itr)
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001511{
1512 fsdata *mydata = itr->fsdata;
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001513 dir_entry *dent = itr->dent;
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001514
1515 /* free cluster blocks */
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001516 clear_fatent(mydata, START(dent));
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001517 if (flush_dirty_fat_buffer(mydata) < 0) {
1518 printf("Error: flush fat buffer\n");
1519 return -EIO;
1520 }
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001521 /* Position to first directory entry for long name */
1522 if (itr->clust != itr->dent_clust) {
1523 int ret;
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001524
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001525 ret = fat_move_to_cluster(itr, itr->dent_clust);
1526 if (ret)
1527 return ret;
1528 }
1529 itr->dent = itr->dent_start;
1530 itr->remaining = itr->dent_rem;
1531 dent = itr->dent_start;
1532 /* Delete long name */
1533 if ((dent->attr & ATTR_VFAT) == ATTR_VFAT &&
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +01001534 (dent->nameext.name[0] & LAST_LONG_ENTRY_MASK)) {
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001535 int ret;
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001536
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001537 ret = delete_long_name(itr);
1538 if (ret)
1539 return ret;
1540 }
1541 /* Delete short name */
1542 delete_single_dentry(itr);
Heinrich Schuchardt913339a2021-01-20 22:21:53 +01001543 return flush_dir(itr);
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001544}
1545
1546int fat_unlink(const char *filename)
1547{
1548 fsdata fsdata = { .fatbuf = NULL, };
1549 fat_itr *itr = NULL;
1550 int n_entries, ret;
1551 char *filename_copy, *dirname, *basename;
1552
1553 filename_copy = strdup(filename);
Heinrich Schuchardtb2242f62018-10-02 06:58:00 +02001554 if (!filename_copy) {
1555 printf("Error: allocating memory\n");
1556 ret = -ENOMEM;
1557 goto exit;
1558 }
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001559 split_filename(filename_copy, &dirname, &basename);
1560
1561 if (!strcmp(dirname, "/") && !strcmp(basename, "")) {
1562 printf("Error: cannot remove root\n");
1563 ret = -EINVAL;
1564 goto exit;
1565 }
1566
1567 itr = malloc_cache_aligned(sizeof(fat_itr));
1568 if (!itr) {
1569 printf("Error: allocating memory\n");
Heinrich Schuchardtb2242f62018-10-02 06:58:00 +02001570 ret = -ENOMEM;
1571 goto exit;
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001572 }
1573
1574 ret = fat_itr_root(itr, &fsdata);
1575 if (ret)
1576 goto exit;
1577
1578 total_sector = fsdata.total_sect;
1579
1580 ret = fat_itr_resolve(itr, dirname, TYPE_DIR);
1581 if (ret) {
1582 printf("%s: doesn't exist (%d)\n", dirname, ret);
1583 ret = -ENOENT;
1584 goto exit;
1585 }
1586
1587 if (!find_directory_entry(itr, basename)) {
1588 printf("%s: doesn't exist\n", basename);
1589 ret = -ENOENT;
1590 goto exit;
1591 }
1592
1593 if (fat_itr_isdir(itr)) {
1594 n_entries = fat_dir_entries(itr);
1595 if (n_entries < 0) {
1596 ret = n_entries;
1597 goto exit;
1598 }
1599 if (n_entries > 2) {
1600 printf("Error: directory is not empty: %d\n",
1601 n_entries);
1602 ret = -EINVAL;
1603 goto exit;
1604 }
1605 }
1606
Heinrich Schuchardt284e85b2020-11-19 07:31:18 +01001607 ret = delete_dentry_long(itr);
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001608
1609exit:
1610 free(fsdata.fatbuf);
1611 free(itr);
1612 free(filename_copy);
1613
1614 return ret;
1615}
1616
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001617int fat_mkdir(const char *new_dirname)
1618{
1619 dir_entry *retdent;
1620 fsdata datablock = { .fatbuf = NULL, };
1621 fsdata *mydata = &datablock;
1622 fat_itr *itr = NULL;
1623 char *dirname_copy, *parent, *dirname;
1624 char l_dirname[VFAT_MAXLEN_BYTES];
1625 int ret = -1;
1626 loff_t actwrite;
1627 unsigned int bytesperclust;
1628 dir_entry *dotdent = NULL;
1629
1630 dirname_copy = strdup(new_dirname);
1631 if (!dirname_copy)
1632 goto exit;
1633
1634 split_filename(dirname_copy, &parent, &dirname);
1635 if (!strlen(dirname)) {
1636 ret = -EINVAL;
1637 goto exit;
1638 }
1639
1640 if (normalize_longname(l_dirname, dirname)) {
1641 printf("FAT: illegal filename (%s)\n", dirname);
1642 ret = -EINVAL;
1643 goto exit;
1644 }
1645
1646 itr = malloc_cache_aligned(sizeof(fat_itr));
1647 if (!itr) {
1648 ret = -ENOMEM;
1649 goto exit;
1650 }
1651
1652 ret = fat_itr_root(itr, &datablock);
1653 if (ret)
1654 goto exit;
1655
1656 total_sector = datablock.total_sect;
1657
1658 ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1659 if (ret) {
1660 printf("%s: doesn't exist (%d)\n", parent, ret);
1661 goto exit;
1662 }
1663
1664 retdent = find_directory_entry(itr, l_dirname);
1665
1666 if (retdent) {
1667 printf("%s: already exists\n", l_dirname);
1668 ret = -EEXIST;
1669 goto exit;
1670 } else {
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001671 char shortname[SHORT_NAME_SIZE];
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +01001672 int ndent;
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001673
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001674 if (itr->is_root) {
1675 /* root dir cannot have "." or ".." */
1676 if (!strcmp(l_dirname, ".") ||
1677 !strcmp(l_dirname, "..")) {
1678 ret = -EINVAL;
1679 goto exit;
1680 }
1681 }
1682
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001683 /* Check if long name is needed */
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +01001684 ndent = set_name(itr, dirname, shortname);
1685 if (ndent < 0) {
1686 ret = ndent;
1687 goto exit;
1688 }
1689 ret = fat_find_empty_dentries(itr, ndent);
1690 if (ret)
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001691 goto exit;
Heinrich Schuchardt524d13a2020-11-22 11:54:22 +01001692 if (ndent > 1) {
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001693 /* Set long name entries */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001694 ret = fill_dir_slot(itr, dirname, shortname);
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001695 if (ret)
1696 goto exit;
1697 }
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001698
1699 /* Set attribute as archive for regular file */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001700 fill_dentry(itr->fsdata, itr->dent, shortname, 0, 0,
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001701 ATTR_DIR | ATTR_ARCH);
1702
1703 retdent = itr->dent;
1704 }
1705
1706 /* Default entries */
1707 bytesperclust = mydata->clust_size * mydata->sect_size;
1708 dotdent = malloc_cache_aligned(bytesperclust);
1709 if (!dotdent) {
1710 ret = -ENOMEM;
1711 goto exit;
1712 }
1713 memset(dotdent, 0, bytesperclust);
1714
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +01001715 memcpy(&dotdent[0].nameext, ". ", 11);
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001716 dotdent[0].attr = ATTR_DIR | ATTR_ARCH;
1717
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +01001718 memcpy(&dotdent[1].nameext, ".. ", 11);
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001719 dotdent[1].attr = ATTR_DIR | ATTR_ARCH;
Heinrich Schuchardt00cf0762020-11-24 21:04:07 +01001720
1721 if (itr->is_root)
1722 set_start_cluster(mydata, &dotdent[1], 0);
1723 else
1724 set_start_cluster(mydata, &dotdent[1], itr->start_clust);
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001725
1726 ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
1727 bytesperclust, &actwrite);
1728 if (ret < 0) {
1729 printf("Error: writing contents\n");
1730 goto exit;
1731 }
1732 /* Write twice for "." */
1733 set_start_cluster(mydata, &dotdent[0], START(retdent));
1734 ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
1735 bytesperclust, &actwrite);
1736 if (ret < 0) {
1737 printf("Error: writing contents\n");
1738 goto exit;
1739 }
1740
1741 /* Flush fat buffer */
1742 ret = flush_dirty_fat_buffer(mydata);
1743 if (ret) {
1744 printf("Error: flush fat buffer\n");
Heinrich Schuchardt913339a2021-01-20 22:21:53 +01001745 ret = -EIO;
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001746 goto exit;
1747 }
1748
1749 /* Write directory table to device */
AKASHI Takahirod98c6742019-05-24 14:10:35 +09001750 ret = flush_dir(itr);
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001751
1752exit:
1753 free(dirname_copy);
1754 free(mydata->fatbuf);
1755 free(itr);
1756 free(dotdent);
1757 return ret;
1758}