blob: 941f8789ab80d2d15d9b68b2be93c1f3b13590aa [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);
24
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010025/* Characters that may only be used in long file names */
26static const char LONG_ONLY_CHARS[] = "+,;=[]";
27
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +010028/* Combined size of the name and ext fields in the directory entry */
29#define SHORT_NAME_SIZE 11
30
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010031/**
32 * str2fat() - convert string to valid FAT name characters
33 *
34 * Stop when reaching end of @src or a period.
35 * Ignore spaces.
36 * Replace characters that may only be used in long names by underscores.
37 * Convert lower case characters to upper case.
38 *
39 * To avoid assumptions about the code page we do not use characters
40 * above 0x7f for the short name.
41 *
42 * @dest: destination buffer
43 * @src: source buffer
44 * @length: size of destination buffer
45 * Return: number of bytes in destination buffer
46 */
47static int str2fat(char *dest, char *src, int length)
Donggeun Kim8f814002011-10-24 21:15:28 +000048{
49 int i;
50
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010051 for (i = 0; i < length; ++src) {
52 char c = *src;
53
54 if (!c || c == '.')
55 break;
56 if (c == ' ')
57 continue;
58 if (strchr(LONG_ONLY_CHARS, c) || c > 0x7f)
59 c = '_';
60 else if (c >= 'a' && c <= 'z')
61 c &= 0xdf;
62 dest[i] = c;
63 ++i;
Donggeun Kim8f814002011-10-24 21:15:28 +000064 }
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010065 return i;
Donggeun Kim8f814002011-10-24 21:15:28 +000066}
67
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010068/**
Heinrich Schuchardt24977e52020-11-25 16:33:55 +010069 * fat_move_to_cluster() - position to first directory entry in cluster
70 *
71 * @itr: directory iterator
72 * @cluster cluster
73 * Return: 0 for success, -EIO on error
74 */
75static int fat_move_to_cluster(fat_itr *itr, unsigned int cluster)
76{
77 unsigned int nbytes;
78
79 /* position to the start of the directory */
80 itr->next_clust = cluster;
81 itr->last_cluster = 0;
82 if (!fat_next_cluster(itr, &nbytes))
83 return -EIO;
84 itr->dent = (dir_entry *)itr->block;
85 itr->remaining = nbytes / sizeof(dir_entry) - 1;
86 return 0;
87}
88
89/**
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010090 * set_name() - set short name in directory entry
91 *
92 * The function determines if the @filename is a valid short name.
93 * In this case no long name is needed.
94 *
95 * If a long name is needed, a short name is constructed.
96 *
Heinrich Schuchardt24977e52020-11-25 16:33:55 +010097 * @itr: directory iterator
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +010098 * @filename: long file name
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +010099 * @shortname: buffer of 11 bytes to receive chosen short name and extension
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100100 * Return: number of directory entries needed, negative on error
101 */
Heinrich Schuchardt24977e52020-11-25 16:33:55 +0100102static int set_name(fat_itr *itr, const char *filename, char *shortname)
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100103{
104 char *period;
105 char *pos;
106 int period_location;
107 char buf[13];
108 int i;
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100109 int ret;
110 struct {
111 char name[8];
112 char ext[3];
113 } dirent;
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100114
115 if (!filename)
116 return -EIO;
117
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100118 /* Initialize buffer */
119 memset(&dirent, ' ', sizeof(dirent));
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100120
121 /* Convert filename to upper case short name */
122 period = strrchr(filename, '.');
123 pos = (char *)filename;
124 if (*pos == '.') {
125 pos = period + 1;
126 period = 0;
127 }
128 if (period)
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100129 str2fat(dirent.ext, period + 1, sizeof(dirent.ext));
130 period_location = str2fat(dirent.name, pos, sizeof(dirent.name));
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100131 if (period_location < 0)
132 return period_location;
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100133 if (*dirent.name == ' ')
134 *dirent.name = '_';
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100135 /* 0xe5 signals a deleted directory entry. Replace it by 0x05. */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100136 if (*dirent.name == 0xe5)
137 *dirent.name = 0x05;
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100138
139 /* If filename and short name are the same, quit. */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100140 sprintf(buf, "%.*s.%.3s", period_location, dirent.name, dirent.ext);
141 if (!strcmp(buf, filename)) {
142 ret = 1;
143 goto out;
144 }
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100145
146 /* Construct an indexed short name */
147 for (i = 1; i < 0x200000; ++i) {
148 int suffix_len;
149 int suffix_start;
150 int j;
151
152 /* To speed up the search use random numbers */
153 if (i < 10) {
154 j = i;
155 } else {
156 j = 30 - fls(i);
157 j = 10 + (rand() >> j);
158 }
159 sprintf(buf, "~%d", j);
160 suffix_len = strlen(buf);
161 suffix_start = 8 - suffix_len;
162 if (suffix_start > period_location)
163 suffix_start = period_location;
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100164 memcpy(dirent.name + suffix_start, buf, suffix_len);
165 if (*dirent.ext != ' ')
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100166 sprintf(buf, "%.*s.%.3s", suffix_start + suffix_len,
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100167 dirent.name, dirent.ext);
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100168 else
169 sprintf(buf, "%.*s", suffix_start + suffix_len,
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100170 dirent.name);
Heinrich Schuchardt24977e52020-11-25 16:33:55 +0100171 debug("generated short name: %s\n", buf);
172
173 /* Check that the short name does not exist yet. */
174 ret = fat_move_to_cluster(itr, itr->start_clust);
175 if (ret)
176 return ret;
177 if (find_directory_entry(itr, buf))
178 continue;
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100179
Heinrich Schuchardt24977e52020-11-25 16:33:55 +0100180 debug("chosen short name: %s\n", buf);
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100181 /* Each long name directory entry takes 13 characters. */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100182 ret = (strlen(filename) + 25) / 13;
183 goto out;
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100184 }
185 return -EIO;
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +0100186out:
187 memcpy(shortname, dirent.name, SHORT_NAME_SIZE);
188 return ret;
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +0100189}
190
Donggeun Kim8f814002011-10-24 21:15:28 +0000191static int total_sector;
Donggeun Kimcb5f3bc2012-03-22 04:38:55 +0000192static int disk_write(__u32 block, __u32 nr_blocks, void *buf)
Donggeun Kim8f814002011-10-24 21:15:28 +0000193{
Łukasz Majewskid6f23d82015-09-03 14:21:39 +0200194 ulong ret;
195
Simon Glass2ee8ada2016-02-29 15:25:52 -0700196 if (!cur_dev)
Donggeun Kim8f814002011-10-24 21:15:28 +0000197 return -1;
198
Donggeun Kimcb5f3bc2012-03-22 04:38:55 +0000199 if (cur_part_info.start + block + nr_blocks >
200 cur_part_info.start + total_sector) {
Donggeun Kim8f814002011-10-24 21:15:28 +0000201 printf("error: overflow occurs\n");
202 return -1;
203 }
204
Simon Glass2ee8ada2016-02-29 15:25:52 -0700205 ret = blk_dwrite(cur_dev, cur_part_info.start + block, nr_blocks, buf);
Łukasz Majewskid6f23d82015-09-03 14:21:39 +0200206 if (nr_blocks && ret == 0)
207 return -1;
208
209 return ret;
Donggeun Kim8f814002011-10-24 21:15:28 +0000210}
211
Donggeun Kim8f814002011-10-24 21:15:28 +0000212/*
213 * Write fat buffer into block device
214 */
Stefan Brüns751b31d2016-09-11 22:51:40 +0200215static int flush_dirty_fat_buffer(fsdata *mydata)
Donggeun Kim8f814002011-10-24 21:15:28 +0000216{
217 int getsize = FATBUFBLOCKS;
218 __u32 fatlength = mydata->fatlength;
219 __u8 *bufptr = mydata->fatbuf;
220 __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
221
Stefan Brüns751b31d2016-09-11 22:51:40 +0200222 debug("debug: evicting %d, dirty: %d\n", mydata->fatbufnum,
223 (int)mydata->fat_dirty);
224
225 if ((!mydata->fat_dirty) || (mydata->fatbufnum == -1))
226 return 0;
227
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100228 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
229 if (startblock + getsize > fatlength)
230 getsize = fatlength - startblock;
Donggeun Kim8f814002011-10-24 21:15:28 +0000231
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100232 startblock += mydata->fat_sect;
Donggeun Kim8f814002011-10-24 21:15:28 +0000233
234 /* Write FAT buf */
235 if (disk_write(startblock, getsize, bufptr) < 0) {
236 debug("error: writing FAT blocks\n");
237 return -1;
238 }
239
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900240 if (mydata->fats == 2) {
Donggeun Kimaf57ce22011-12-20 18:34:27 +0000241 /* Update corresponding second FAT blocks */
242 startblock += mydata->fatlength;
243 if (disk_write(startblock, getsize, bufptr) < 0) {
244 debug("error: writing second FAT blocks\n");
245 return -1;
246 }
247 }
Stefan Brüns751b31d2016-09-11 22:51:40 +0200248 mydata->fat_dirty = 0;
Donggeun Kimaf57ce22011-12-20 18:34:27 +0000249
Donggeun Kim8f814002011-10-24 21:15:28 +0000250 return 0;
251}
252
253/*
Donggeun Kim8f814002011-10-24 21:15:28 +0000254 * Set the file name information from 'name' into 'slotptr',
255 */
256static int str2slot(dir_slot *slotptr, const char *name, int *idx)
257{
258 int j, end_idx = 0;
259
260 for (j = 0; j <= 8; j += 2) {
261 if (name[*idx] == 0x00) {
262 slotptr->name0_4[j] = 0;
263 slotptr->name0_4[j + 1] = 0;
264 end_idx++;
265 goto name0_4;
266 }
267 slotptr->name0_4[j] = name[*idx];
268 (*idx)++;
269 end_idx++;
270 }
271 for (j = 0; j <= 10; j += 2) {
272 if (name[*idx] == 0x00) {
273 slotptr->name5_10[j] = 0;
274 slotptr->name5_10[j + 1] = 0;
275 end_idx++;
276 goto name5_10;
277 }
278 slotptr->name5_10[j] = name[*idx];
279 (*idx)++;
280 end_idx++;
281 }
282 for (j = 0; j <= 2; j += 2) {
283 if (name[*idx] == 0x00) {
284 slotptr->name11_12[j] = 0;
285 slotptr->name11_12[j + 1] = 0;
286 end_idx++;
287 goto name11_12;
288 }
289 slotptr->name11_12[j] = name[*idx];
290 (*idx)++;
291 end_idx++;
292 }
293
294 if (name[*idx] == 0x00)
295 return 1;
296
297 return 0;
298/* Not used characters are filled with 0xff 0xff */
299name0_4:
300 for (; end_idx < 5; end_idx++) {
301 slotptr->name0_4[end_idx * 2] = 0xff;
302 slotptr->name0_4[end_idx * 2 + 1] = 0xff;
303 }
304 end_idx = 5;
305name5_10:
306 end_idx -= 5;
307 for (; end_idx < 6; end_idx++) {
308 slotptr->name5_10[end_idx * 2] = 0xff;
309 slotptr->name5_10[end_idx * 2 + 1] = 0xff;
310 }
311 end_idx = 11;
312name11_12:
313 end_idx -= 11;
314 for (; end_idx < 2; end_idx++) {
315 slotptr->name11_12[end_idx * 2] = 0xff;
316 slotptr->name11_12[end_idx * 2 + 1] = 0xff;
317 }
318
319 return 1;
320}
321
AKASHI Takahiroa2c0bd02019-05-24 14:10:36 +0900322static int new_dir_table(fat_itr *itr);
323static int flush_dir(fat_itr *itr);
Donggeun Kim8f814002011-10-24 21:15:28 +0000324
Heinrich Schuchardtac4ab752020-11-21 08:32:50 +0100325/**
326 * fill_dir_slot() - fill directory entries for long name
327 *
328 * @itr: directory iterator
329 * @l_name: long name
330 * @shortname: short name
331 * Return: 0 for success, -errno otherwise
Donggeun Kim8f814002011-10-24 21:15:28 +0000332 */
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900333static int
Heinrich Schuchardtac4ab752020-11-21 08:32:50 +0100334fill_dir_slot(fat_itr *itr, const char *l_name, const char *shortname)
Donggeun Kim8f814002011-10-24 21:15:28 +0000335{
Tien Fong Chee0117dbe2016-07-27 23:08:56 -0700336 __u8 temp_dir_slot_buffer[MAX_LFN_SLOT * sizeof(dir_slot)];
337 dir_slot *slotptr = (dir_slot *)temp_dir_slot_buffer;
Anatolij Gustschinf4f9d5d2011-12-15 03:12:14 +0000338 __u8 counter = 0, checksum;
Donggeun Kim8f814002011-10-24 21:15:28 +0000339 int idx = 0, ret;
Donggeun Kim8f814002011-10-24 21:15:28 +0000340
Stefan Brüns6c617d62016-09-11 22:51:39 +0200341 /* Get short file name checksum value */
Heinrich Schuchardtac4ab752020-11-21 08:32:50 +0100342 checksum = mkcksum(shortname, shortname + 8);
Donggeun Kim8f814002011-10-24 21:15:28 +0000343
344 do {
345 memset(slotptr, 0x00, sizeof(dir_slot));
346 ret = str2slot(slotptr, l_name, &idx);
347 slotptr->id = ++counter;
348 slotptr->attr = ATTR_VFAT;
349 slotptr->alias_checksum = checksum;
350 slotptr++;
351 } while (ret == 0);
352
353 slotptr--;
354 slotptr->id |= LAST_LONG_ENTRY_MASK;
355
356 while (counter >= 1) {
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900357 memcpy(itr->dent, slotptr, sizeof(dir_slot));
Donggeun Kim8f814002011-10-24 21:15:28 +0000358 slotptr--;
359 counter--;
AKASHI Takahiroa2c0bd02019-05-24 14:10:36 +0900360
361 if (itr->remaining == 0)
362 flush_dir(itr);
363
AKASHI Takahiroc3269332019-05-24 14:10:37 +0900364 /* allocate a cluster for more entries */
Heinrich Schuchardt3b9d8432020-11-22 19:24:46 +0100365 if (!next_dent(itr) && !itr->dent)
Heinrich Schuchardta8021522020-11-19 12:24:44 +0100366 if ((itr->is_root && itr->fsdata->fatsize != 32) ||
AKASHI Takahiroc3269332019-05-24 14:10:37 +0900367 new_dir_table(itr))
Heinrich Schuchardtac4ab752020-11-21 08:32:50 +0100368 return -EIO;
Donggeun Kim8f814002011-10-24 21:15:28 +0000369 }
370
Donggeun Kim8f814002011-10-24 21:15:28 +0000371 return 0;
372}
373
Donggeun Kim8f814002011-10-24 21:15:28 +0000374/*
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500375 * Set the entry at index 'entry' in a FAT (12/16/32) table.
Donggeun Kim8f814002011-10-24 21:15:28 +0000376 */
377static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
378{
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500379 __u32 bufnum, offset, off16;
380 __u16 val1, val2;
Donggeun Kim8f814002011-10-24 21:15:28 +0000381
382 switch (mydata->fatsize) {
383 case 32:
384 bufnum = entry / FAT32BUFSIZE;
385 offset = entry - bufnum * FAT32BUFSIZE;
386 break;
387 case 16:
388 bufnum = entry / FAT16BUFSIZE;
389 offset = entry - bufnum * FAT16BUFSIZE;
390 break;
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500391 case 12:
392 bufnum = entry / FAT12BUFSIZE;
393 offset = entry - bufnum * FAT12BUFSIZE;
394 break;
Donggeun Kim8f814002011-10-24 21:15:28 +0000395 default:
396 /* Unsupported FAT size */
397 return -1;
398 }
399
400 /* Read a new block of FAT entries into the cache. */
401 if (bufnum != mydata->fatbufnum) {
402 int getsize = FATBUFBLOCKS;
403 __u8 *bufptr = mydata->fatbuf;
404 __u32 fatlength = mydata->fatlength;
405 __u32 startblock = bufnum * FATBUFBLOCKS;
406
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100407 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
408 if (startblock + getsize > fatlength)
409 getsize = fatlength - startblock;
Donggeun Kim8f814002011-10-24 21:15:28 +0000410
Stefan Brüns751b31d2016-09-11 22:51:40 +0200411 if (flush_dirty_fat_buffer(mydata) < 0)
412 return -1;
Donggeun Kim8f814002011-10-24 21:15:28 +0000413
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100414 startblock += mydata->fat_sect;
415
Donggeun Kim8f814002011-10-24 21:15:28 +0000416 if (disk_read(startblock, getsize, bufptr) < 0) {
417 debug("Error reading FAT blocks\n");
418 return -1;
419 }
420 mydata->fatbufnum = bufnum;
421 }
422
Stefan Brüns751b31d2016-09-11 22:51:40 +0200423 /* Mark as dirty */
424 mydata->fat_dirty = 1;
425
Donggeun Kim8f814002011-10-24 21:15:28 +0000426 /* Set the actual entry */
427 switch (mydata->fatsize) {
428 case 32:
429 ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
430 break;
431 case 16:
432 ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
433 break;
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500434 case 12:
435 off16 = (offset * 3) / 4;
436
437 switch (offset & 0x3) {
438 case 0:
439 val1 = cpu_to_le16(entry_value) & 0xfff;
440 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff;
441 ((__u16 *)mydata->fatbuf)[off16] |= val1;
442 break;
443 case 1:
444 val1 = cpu_to_le16(entry_value) & 0xf;
445 val2 = (cpu_to_le16(entry_value) >> 4) & 0xff;
446
447 ((__u16 *)mydata->fatbuf)[off16] &= ~0xf000;
448 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 12);
449
450 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xff;
451 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
452 break;
453 case 2:
454 val1 = cpu_to_le16(entry_value) & 0xff;
455 val2 = (cpu_to_le16(entry_value) >> 8) & 0xf;
456
457 ((__u16 *)mydata->fatbuf)[off16] &= ~0xff00;
458 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 8);
459
460 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xf;
461 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
462 break;
463 case 3:
464 val1 = cpu_to_le16(entry_value) & 0xfff;
465 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff0;
466 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 4);
467 break;
468 default:
469 break;
470 }
471
472 break;
Donggeun Kim8f814002011-10-24 21:15:28 +0000473 default:
474 return -1;
475 }
476
477 return 0;
478}
479
480/*
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500481 * Determine the next free cluster after 'entry' in a FAT (12/16/32) table
Stefan Brüns14999402016-09-11 22:51:41 +0200482 * and link it to 'entry'. EOC marker is not set on returned entry.
Donggeun Kim8f814002011-10-24 21:15:28 +0000483 */
484static __u32 determine_fatent(fsdata *mydata, __u32 entry)
485{
486 __u32 next_fat, next_entry = entry + 1;
487
488 while (1) {
Stefan Brüns9f95d812016-12-17 00:27:51 +0100489 next_fat = get_fatent(mydata, next_entry);
Donggeun Kim8f814002011-10-24 21:15:28 +0000490 if (next_fat == 0) {
Stefan Brüns14999402016-09-11 22:51:41 +0200491 /* found free entry, link to entry */
Donggeun Kim8f814002011-10-24 21:15:28 +0000492 set_fatent_value(mydata, entry, next_entry);
493 break;
494 }
495 next_entry++;
496 }
497 debug("FAT%d: entry: %08x, entry_value: %04x\n",
498 mydata->fatsize, entry, next_entry);
499
500 return next_entry;
501}
502
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200503/**
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900504 * set_sectors() - write data to sectors
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200505 *
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900506 * Write 'size' bytes from 'buffer' into the specified sector.
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200507 *
508 * @mydata: data to be written
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900509 * @startsect: sector to be written to
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200510 * @buffer: data to be written
511 * @size: bytes to be written (but not more than the size of a cluster)
512 * Return: 0 on success, -1 otherwise
Donggeun Kim8f814002011-10-24 21:15:28 +0000513 */
514static int
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900515set_sectors(fsdata *mydata, u32 startsect, u8 *buffer, u32 size)
Donggeun Kim8f814002011-10-24 21:15:28 +0000516{
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900517 u32 nsects = 0;
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200518 int ret;
Donggeun Kim8f814002011-10-24 21:15:28 +0000519
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900520 debug("startsect: %d\n", startsect);
Donggeun Kim8f814002011-10-24 21:15:28 +0000521
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200522 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
523 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
524
Heinrich Schuchardtaa9d6ba2018-09-13 19:42:47 +0200525 debug("FAT: Misaligned buffer address (%p)\n", buffer);
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200526
527 while (size >= mydata->sect_size) {
528 memcpy(tmpbuf, buffer, mydata->sect_size);
529 ret = disk_write(startsect++, 1, tmpbuf);
530 if (ret != 1) {
531 debug("Error writing data (got %d)\n", ret);
532 return -1;
533 }
534
535 buffer += mydata->sect_size;
536 size -= mydata->sect_size;
537 }
538 } else if (size >= mydata->sect_size) {
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900539 nsects = size / mydata->sect_size;
540 ret = disk_write(startsect, nsects, buffer);
541 if (ret != nsects) {
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200542 debug("Error writing data (got %d)\n", ret);
Wu, Joshb0e38dd2013-07-24 17:55:30 +0800543 return -1;
544 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000545
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900546 startsect += nsects;
547 buffer += nsects * mydata->sect_size;
548 size -= nsects * mydata->sect_size;
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200549 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000550
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200551 if (size) {
552 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +0200553 /* Do not leak content of stack */
554 memset(tmpbuf, 0, mydata->sect_size);
Benoît Thébaudeauccc945b2015-09-28 15:45:28 +0200555 memcpy(tmpbuf, buffer, size);
556 ret = disk_write(startsect, 1, tmpbuf);
557 if (ret != 1) {
558 debug("Error writing data (got %d)\n", ret);
Donggeun Kim8f814002011-10-24 21:15:28 +0000559 return -1;
560 }
Donggeun Kim8f814002011-10-24 21:15:28 +0000561 }
562
563 return 0;
564}
565
AKASHI Takahirod98c6742019-05-24 14:10:35 +0900566/**
567 * set_cluster() - write data to cluster
568 *
569 * Write 'size' bytes from 'buffer' into the specified cluster.
570 *
571 * @mydata: data to be written
572 * @clustnum: cluster to be written to
573 * @buffer: data to be written
574 * @size: bytes to be written (but not more than the size of a cluster)
575 * Return: 0 on success, -1 otherwise
576 */
577static int
578set_cluster(fsdata *mydata, u32 clustnum, u8 *buffer, u32 size)
579{
580 return set_sectors(mydata, clust_to_sect(mydata, clustnum),
581 buffer, size);
582}
583
584static int
585flush_dir(fat_itr *itr)
586{
587 fsdata *mydata = itr->fsdata;
588 u32 startsect, sect_offset, nsects;
589
590 if (!itr->is_root || mydata->fatsize == 32)
591 return set_cluster(mydata, itr->clust, itr->block,
592 mydata->clust_size * mydata->sect_size);
593
594 sect_offset = itr->clust * mydata->clust_size;
595 startsect = mydata->rootdir_sect + sect_offset;
596 /* do not write past the end of rootdir */
597 nsects = min_t(u32, mydata->clust_size,
598 mydata->rootdir_size - sect_offset);
599
600 return set_sectors(mydata, startsect, itr->block,
601 nsects * mydata->sect_size);
602}
603
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900604/*
605 * Read and modify data on existing and consecutive cluster blocks
606 */
607static int
608get_set_cluster(fsdata *mydata, __u32 clustnum, loff_t pos, __u8 *buffer,
609 loff_t size, loff_t *gotsize)
610{
Heinrich Schuchardt8a3ac6e2020-07-06 07:48:14 +0200611 static u8 *tmpbuf_cluster;
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900612 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
613 __u32 startsect;
614 loff_t wsize;
615 int clustcount, i, ret;
616
617 *gotsize = 0;
618 if (!size)
619 return 0;
620
Heinrich Schuchardt8a3ac6e2020-07-06 07:48:14 +0200621 if (!tmpbuf_cluster) {
622 tmpbuf_cluster = memalign(ARCH_DMA_MINALIGN, MAX_CLUSTSIZE);
623 if (!tmpbuf_cluster)
624 return -1;
625 }
626
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900627 assert(pos < bytesperclust);
628 startsect = clust_to_sect(mydata, clustnum);
629
630 debug("clustnum: %d, startsect: %d, pos: %lld\n",
631 clustnum, startsect, pos);
632
633 /* partial write at beginning */
634 if (pos) {
635 wsize = min(bytesperclust - pos, size);
636 ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster);
637 if (ret != mydata->clust_size) {
638 debug("Error reading data (got %d)\n", ret);
639 return -1;
640 }
641
642 memcpy(tmpbuf_cluster + pos, buffer, wsize);
643 ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster);
644 if (ret != mydata->clust_size) {
645 debug("Error writing data (got %d)\n", ret);
646 return -1;
647 }
648
649 size -= wsize;
650 buffer += wsize;
651 *gotsize += wsize;
652
653 startsect += mydata->clust_size;
654
655 if (!size)
656 return 0;
657 }
658
659 /* full-cluster write */
660 if (size >= bytesperclust) {
661 clustcount = lldiv(size, bytesperclust);
662
663 if (!((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1))) {
664 wsize = clustcount * bytesperclust;
665 ret = disk_write(startsect,
666 clustcount * mydata->clust_size,
667 buffer);
668 if (ret != clustcount * mydata->clust_size) {
669 debug("Error writing data (got %d)\n", ret);
670 return -1;
671 }
672
673 size -= wsize;
674 buffer += wsize;
675 *gotsize += wsize;
676
677 startsect += clustcount * mydata->clust_size;
678 } else {
679 for (i = 0; i < clustcount; i++) {
680 memcpy(tmpbuf_cluster, buffer, bytesperclust);
681 ret = disk_write(startsect,
682 mydata->clust_size,
683 tmpbuf_cluster);
684 if (ret != mydata->clust_size) {
685 debug("Error writing data (got %d)\n",
686 ret);
687 return -1;
688 }
689
690 size -= bytesperclust;
691 buffer += bytesperclust;
692 *gotsize += bytesperclust;
693
694 startsect += mydata->clust_size;
695 }
696 }
697 }
698
699 /* partial write at end */
700 if (size) {
701 wsize = size;
702 ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster);
703 if (ret != mydata->clust_size) {
704 debug("Error reading data (got %d)\n", ret);
705 return -1;
706 }
707 memcpy(tmpbuf_cluster, buffer, wsize);
708 ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster);
709 if (ret != mydata->clust_size) {
710 debug("Error writing data (got %d)\n", ret);
711 return -1;
712 }
713
714 size -= wsize;
715 buffer += wsize;
716 *gotsize += wsize;
717 }
718
719 assert(!size);
720
721 return 0;
722}
723
Donggeun Kim8f814002011-10-24 21:15:28 +0000724/*
725 * Find the first empty cluster
726 */
727static int find_empty_cluster(fsdata *mydata)
728{
729 __u32 fat_val, entry = 3;
730
731 while (1) {
Stefan Brüns9f95d812016-12-17 00:27:51 +0100732 fat_val = get_fatent(mydata, entry);
Donggeun Kim8f814002011-10-24 21:15:28 +0000733 if (fat_val == 0)
734 break;
735 entry++;
736 }
737
738 return entry;
739}
740
Heinrich Schuchardte09f3e42020-11-26 19:06:55 +0100741/**
742 * new_dir_table() - allocate a cluster for additional directory entries
743 *
744 * @itr: directory iterator
745 * Return: 0 on success, -EIO otherwise
Donggeun Kim8f814002011-10-24 21:15:28 +0000746 */
AKASHI Takahiroa2c0bd02019-05-24 14:10:36 +0900747static int new_dir_table(fat_itr *itr)
Donggeun Kim8f814002011-10-24 21:15:28 +0000748{
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900749 fsdata *mydata = itr->fsdata;
Donggeun Kim8f814002011-10-24 21:15:28 +0000750 int dir_newclust = 0;
Heinrich Schuchardte09f3e42020-11-26 19:06:55 +0100751 int dir_oldclust = itr->clust;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900752 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
Donggeun Kim8f814002011-10-24 21:15:28 +0000753
Donggeun Kim8f814002011-10-24 21:15:28 +0000754 dir_newclust = find_empty_cluster(mydata);
Heinrich Schuchardte09f3e42020-11-26 19:06:55 +0100755
756 /*
757 * Flush before updating FAT to ensure valid directory structure
758 * in case of failure.
759 */
760 itr->clust = dir_newclust;
761 itr->next_clust = dir_newclust;
762 memset(itr->block, 0x00, bytesperclust);
763 if (flush_dir(itr))
764 return -EIO;
765
766 set_fatent_value(mydata, dir_oldclust, dir_newclust);
Donggeun Kim8f814002011-10-24 21:15:28 +0000767 if (mydata->fatsize == 32)
768 set_fatent_value(mydata, dir_newclust, 0xffffff8);
769 else if (mydata->fatsize == 16)
770 set_fatent_value(mydata, dir_newclust, 0xfff8);
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500771 else if (mydata->fatsize == 12)
772 set_fatent_value(mydata, dir_newclust, 0xff8);
Donggeun Kim8f814002011-10-24 21:15:28 +0000773
Stefan Brüns751b31d2016-09-11 22:51:40 +0200774 if (flush_dirty_fat_buffer(mydata) < 0)
Heinrich Schuchardte09f3e42020-11-26 19:06:55 +0100775 return -EIO;
Donggeun Kim8f814002011-10-24 21:15:28 +0000776
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900777 itr->dent = (dir_entry *)itr->block;
778 itr->last_cluster = 1;
779 itr->remaining = bytesperclust / sizeof(dir_entry) - 1;
Donggeun Kim8f814002011-10-24 21:15:28 +0000780
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +0900781 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +0000782}
783
784/*
785 * Set empty cluster from 'entry' to the end of a file
786 */
787static int clear_fatent(fsdata *mydata, __u32 entry)
788{
789 __u32 fat_val;
790
Philipp Skadorov16f553d2016-12-15 15:52:53 -0500791 while (!CHECK_CLUST(entry, mydata->fatsize)) {
Stefan Brüns9f95d812016-12-17 00:27:51 +0100792 fat_val = get_fatent(mydata, entry);
Donggeun Kim8f814002011-10-24 21:15:28 +0000793 if (fat_val != 0)
794 set_fatent_value(mydata, entry, 0);
795 else
796 break;
797
Donggeun Kim8f814002011-10-24 21:15:28 +0000798 entry = fat_val;
799 }
800
801 /* Flush fat buffer */
Stefan Brüns751b31d2016-09-11 22:51:40 +0200802 if (flush_dirty_fat_buffer(mydata) < 0)
Donggeun Kim8f814002011-10-24 21:15:28 +0000803 return -1;
804
805 return 0;
806}
807
808/*
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +0900809 * Set start cluster in directory entry
810 */
811static void set_start_cluster(const fsdata *mydata, dir_entry *dentptr,
812 __u32 start_cluster)
813{
814 if (mydata->fatsize == 32)
815 dentptr->starthi =
816 cpu_to_le16((start_cluster & 0xffff0000) >> 16);
817 dentptr->start = cpu_to_le16(start_cluster & 0xffff);
818}
819
820/*
821 * Check whether adding a file makes the file system to
822 * exceed the size of the block device
823 * Return -1 when overflow occurs, otherwise return 0
824 */
825static int check_overflow(fsdata *mydata, __u32 clustnum, loff_t size)
826{
827 __u32 startsect, sect_num, offset;
828
829 if (clustnum > 0)
830 startsect = clust_to_sect(mydata, clustnum);
831 else
832 startsect = mydata->rootdir_sect;
833
834 sect_num = div_u64_rem(size, mydata->sect_size, &offset);
835
836 if (offset != 0)
837 sect_num++;
838
839 if (startsect + sect_num > total_sector)
840 return -1;
841 return 0;
842}
843
844/*
Donggeun Kim8f814002011-10-24 21:15:28 +0000845 * Write at most 'maxsize' bytes from 'buffer' into
846 * the file associated with 'dentptr'
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800847 * Update the number of bytes written in *gotsize and return 0
848 * or return -1 on fatal errors.
Donggeun Kim8f814002011-10-24 21:15:28 +0000849 */
850static int
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +0900851set_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, __u8 *buffer,
852 loff_t maxsize, loff_t *gotsize)
Donggeun Kim8f814002011-10-24 21:15:28 +0000853{
Donggeun Kim8f814002011-10-24 21:15:28 +0000854 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
855 __u32 curclust = START(dentptr);
856 __u32 endclust = 0, newclust = 0;
Heinrich Schuchardtdb538a92019-02-25 19:42:48 +0100857 u64 cur_pos, filesize;
858 loff_t offset, actsize, wsize;
Donggeun Kim8f814002011-10-24 21:15:28 +0000859
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800860 *gotsize = 0;
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900861 filesize = pos + maxsize;
Donggeun Kim8f814002011-10-24 21:15:28 +0000862
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800863 debug("%llu bytes\n", filesize);
Donggeun Kim8f814002011-10-24 21:15:28 +0000864
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900865 if (!filesize) {
866 if (!curclust)
867 return 0;
868 if (!CHECK_CLUST(curclust, mydata->fatsize) ||
869 IS_LAST_CLUST(curclust, mydata->fatsize)) {
870 clear_fatent(mydata, curclust);
871 set_start_cluster(mydata, dentptr, 0);
872 return 0;
873 }
874 debug("curclust: 0x%x\n", curclust);
875 debug("Invalid FAT entry\n");
876 return -1;
877 }
878
879 if (!curclust) {
880 assert(pos == 0);
881 goto set_clusters;
882 }
883
884 /* go to cluster at pos */
885 cur_pos = bytesperclust;
886 while (1) {
887 if (pos <= cur_pos)
888 break;
889 if (IS_LAST_CLUST(curclust, mydata->fatsize))
890 break;
891
892 newclust = get_fatent(mydata, curclust);
893 if (!IS_LAST_CLUST(newclust, mydata->fatsize) &&
894 CHECK_CLUST(newclust, mydata->fatsize)) {
895 debug("curclust: 0x%x\n", curclust);
896 debug("Invalid FAT entry\n");
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +0200897 return -1;
898 }
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900899
900 cur_pos += bytesperclust;
901 curclust = newclust;
902 }
903 if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
904 assert(pos == cur_pos);
905 goto set_clusters;
906 }
907
908 assert(pos < cur_pos);
909 cur_pos -= bytesperclust;
910
911 /* overwrite */
912 assert(IS_LAST_CLUST(curclust, mydata->fatsize) ||
913 !CHECK_CLUST(curclust, mydata->fatsize));
914
915 while (1) {
916 /* search for allocated consecutive clusters */
917 actsize = bytesperclust;
918 endclust = curclust;
919 while (1) {
920 if (filesize <= (cur_pos + actsize))
921 break;
922
923 newclust = get_fatent(mydata, endclust);
924
Marek Szyprowski2f241672019-12-02 12:11:13 +0100925 if (newclust != endclust + 1)
926 break;
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900927 if (IS_LAST_CLUST(newclust, mydata->fatsize))
928 break;
929 if (CHECK_CLUST(newclust, mydata->fatsize)) {
930 debug("curclust: 0x%x\n", curclust);
931 debug("Invalid FAT entry\n");
932 return -1;
933 }
934
935 actsize += bytesperclust;
936 endclust = newclust;
937 }
938
939 /* overwrite to <curclust..endclust> */
940 if (pos < cur_pos)
941 offset = 0;
942 else
943 offset = pos - cur_pos;
Marek Szyprowski3a867ce2019-12-02 12:11:14 +0100944 wsize = min_t(unsigned long long, actsize, filesize - cur_pos);
945 wsize -= offset;
946
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900947 if (get_set_cluster(mydata, curclust, offset,
948 buffer, wsize, &actsize)) {
949 printf("Error get-and-setting cluster\n");
950 return -1;
951 }
952 buffer += wsize;
953 *gotsize += wsize;
954 cur_pos += offset + wsize;
955
956 if (filesize <= cur_pos)
957 break;
958
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900959 if (IS_LAST_CLUST(newclust, mydata->fatsize))
960 /* no more clusters */
961 break;
962
963 curclust = newclust;
964 }
965
966 if (filesize <= cur_pos) {
967 /* no more write */
968 newclust = get_fatent(mydata, endclust);
969 if (!IS_LAST_CLUST(newclust, mydata->fatsize)) {
970 /* truncate the rest */
971 clear_fatent(mydata, newclust);
972
973 /* Mark end of file in FAT */
974 if (mydata->fatsize == 12)
975 newclust = 0xfff;
976 else if (mydata->fatsize == 16)
977 newclust = 0xffff;
978 else if (mydata->fatsize == 32)
979 newclust = 0xfffffff;
980 set_fatent_value(mydata, endclust, newclust);
981 }
982
983 return 0;
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +0900984 }
985
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900986 curclust = endclust;
987 filesize -= cur_pos;
Heinrich Schuchardtdb538a92019-02-25 19:42:48 +0100988 assert(!do_div(cur_pos, bytesperclust));
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +0900989
AKASHI Takahirob07f7042018-09-11 15:59:06 +0900990set_clusters:
991 /* allocate and write */
992 assert(!pos);
993
994 /* Assure that curclust is valid */
995 if (!curclust) {
996 curclust = find_empty_cluster(mydata);
997 set_start_cluster(mydata, dentptr, curclust);
998 } else {
999 newclust = get_fatent(mydata, curclust);
1000
1001 if (IS_LAST_CLUST(newclust, mydata->fatsize)) {
1002 newclust = determine_fatent(mydata, curclust);
1003 set_fatent_value(mydata, curclust, newclust);
1004 curclust = newclust;
1005 } else {
1006 debug("error: something wrong\n");
1007 return -1;
1008 }
1009 }
1010
1011 /* TODO: already partially written */
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001012 if (check_overflow(mydata, curclust, filesize)) {
1013 printf("Error: no space left: %llu\n", filesize);
1014 return -1;
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +02001015 }
1016
Donggeun Kim8f814002011-10-24 21:15:28 +00001017 actsize = bytesperclust;
1018 endclust = curclust;
1019 do {
1020 /* search for consecutive clusters */
1021 while (actsize < filesize) {
1022 newclust = determine_fatent(mydata, endclust);
1023
1024 if ((newclust - 1) != endclust)
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001025 /* write to <curclust..endclust> */
Donggeun Kim8f814002011-10-24 21:15:28 +00001026 goto getit;
1027
1028 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Benoît Thébaudeaue0b86942015-09-28 15:45:30 +02001029 debug("newclust: 0x%x\n", newclust);
Donggeun Kim8f814002011-10-24 21:15:28 +00001030 debug("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001031 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +00001032 }
1033 endclust = newclust;
1034 actsize += bytesperclust;
1035 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001036
1037 /* set remaining bytes */
Donggeun Kim8f814002011-10-24 21:15:28 +00001038 actsize = filesize;
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +02001039 if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
Donggeun Kim8f814002011-10-24 21:15:28 +00001040 debug("error: writing cluster\n");
1041 return -1;
1042 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001043 *gotsize += actsize;
Donggeun Kim8f814002011-10-24 21:15:28 +00001044
1045 /* Mark end of file in FAT */
Philipp Skadorov16f553d2016-12-15 15:52:53 -05001046 if (mydata->fatsize == 12)
1047 newclust = 0xfff;
1048 else if (mydata->fatsize == 16)
Donggeun Kim8f814002011-10-24 21:15:28 +00001049 newclust = 0xffff;
1050 else if (mydata->fatsize == 32)
1051 newclust = 0xfffffff;
1052 set_fatent_value(mydata, endclust, newclust);
1053
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001054 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +00001055getit:
Heinrich Schuchardt9fb8db42018-10-02 09:30:45 +02001056 if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
Donggeun Kim8f814002011-10-24 21:15:28 +00001057 debug("error: writing cluster\n");
1058 return -1;
1059 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001060 *gotsize += actsize;
Donggeun Kim8f814002011-10-24 21:15:28 +00001061 filesize -= actsize;
1062 buffer += actsize;
1063
Benoît Thébaudeaue0b86942015-09-28 15:45:30 +02001064 if (CHECK_CLUST(newclust, mydata->fatsize)) {
1065 debug("newclust: 0x%x\n", newclust);
Donggeun Kim8f814002011-10-24 21:15:28 +00001066 debug("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001067 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +00001068 }
1069 actsize = bytesperclust;
1070 curclust = endclust = newclust;
1071 } while (1);
Donggeun Kim8f814002011-10-24 21:15:28 +00001072
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001073 return 0;
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +02001074}
1075
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001076/**
1077 * fill_dentry() - fill directory entry with shortname
1078 *
1079 * @mydata: private filesystem parameters
1080 * @dentptr: directory entry
1081 * @shortname: chosen short name
1082 * @start_cluster: first cluster of file
1083 * @size: file size
1084 * @attr: file attributes
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +02001085 */
1086static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001087 const char *shortname, __u32 start_cluster, __u32 size, __u8 attr)
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +02001088{
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001089 memset(dentptr, 0, sizeof(*dentptr));
1090
Benoît Thébaudeau8fed7d32015-09-28 15:45:32 +02001091 set_start_cluster(mydata, dentptr, start_cluster);
Donggeun Kim8f814002011-10-24 21:15:28 +00001092 dentptr->size = cpu_to_le32(size);
1093
1094 dentptr->attr = attr;
1095
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001096 memcpy(dentptr->name, shortname, SHORT_NAME_SIZE);
Donggeun Kim8f814002011-10-24 21:15:28 +00001097}
1098
1099/*
Donggeun Kim8f814002011-10-24 21:15:28 +00001100 * Find a directory entry based on filename or start cluster number
1101 * If the directory entry is not found,
1102 * the new position for writing a directory entry will be returned
1103 */
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001104static dir_entry *find_directory_entry(fat_itr *itr, char *filename)
Donggeun Kim8f814002011-10-24 21:15:28 +00001105{
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001106 int match = 0;
Donggeun Kim8f814002011-10-24 21:15:28 +00001107
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001108 while (fat_itr_next(itr)) {
1109 /* check both long and short name: */
1110 if (!strcasecmp(filename, itr->name))
1111 match = 1;
1112 else if (itr->name != itr->s_name &&
1113 !strcasecmp(filename, itr->s_name))
1114 match = 1;
Donggeun Kim8f814002011-10-24 21:15:28 +00001115
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001116 if (!match)
1117 continue;
Donggeun Kim8f814002011-10-24 21:15:28 +00001118
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001119 if (itr->dent->name[0] == '\0')
Donggeun Kim8f814002011-10-24 21:15:28 +00001120 return NULL;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001121 else
1122 return itr->dent;
1123 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001124
AKASHI Takahiroc3269332019-05-24 14:10:37 +09001125 /* allocate a cluster for more entries */
1126 if (!itr->dent &&
1127 (!itr->is_root || itr->fsdata->fatsize == 32) &&
1128 new_dir_table(itr))
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001129 /* indicate that allocating dent failed */
1130 itr->dent = NULL;
Donggeun Kim8f814002011-10-24 21:15:28 +00001131
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001132 return NULL;
1133}
Donggeun Kim8f814002011-10-24 21:15:28 +00001134
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001135static int split_filename(char *filename, char **dirname, char **basename)
1136{
1137 char *p, *last_slash, *last_slash_cont;
Donggeun Kim8f814002011-10-24 21:15:28 +00001138
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001139again:
1140 p = filename;
1141 last_slash = NULL;
1142 last_slash_cont = NULL;
1143 while (*p) {
1144 if (ISDIRDELIM(*p)) {
1145 last_slash = p;
1146 last_slash_cont = p;
1147 /* continuous slashes */
1148 while (ISDIRDELIM(*p))
1149 last_slash_cont = p++;
1150 if (!*p)
1151 break;
Donggeun Kim8f814002011-10-24 21:15:28 +00001152 }
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001153 p++;
1154 }
Wu, Josh675b23c2014-05-08 16:14:07 +08001155
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001156 if (last_slash) {
1157 if (last_slash_cont == (filename + strlen(filename) - 1)) {
1158 /* remove trailing slashes */
1159 *last_slash = '\0';
1160 goto again;
Wu, Josh675b23c2014-05-08 16:14:07 +08001161 }
1162
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001163 if (last_slash == filename) {
1164 /* avoid ""(null) directory */
1165 *dirname = "/";
1166 } else {
1167 *last_slash = '\0';
1168 *dirname = filename;
Donggeun Kim8f814002011-10-24 21:15:28 +00001169 }
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001170
1171 *last_slash_cont = '\0';
1172 *basename = last_slash_cont + 1;
1173 } else {
1174 *dirname = "/"; /* root by default */
1175 *basename = filename;
Donggeun Kim8f814002011-10-24 21:15:28 +00001176 }
1177
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001178 return 0;
Donggeun Kim8f814002011-10-24 21:15:28 +00001179}
1180
Heinrich Schuchardt58587212019-05-12 09:59:18 +02001181/**
1182 * normalize_longname() - check long file name and convert to lower case
1183 *
1184 * We assume here that the FAT file system is using an 8bit code page.
1185 * Linux typically uses CP437, EDK2 assumes CP1250.
1186 *
1187 * @l_filename: preallocated buffer receiving the normalized name
1188 * @filename: filename to normalize
1189 * Return: 0 on success, -1 on failure
1190 */
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001191static int normalize_longname(char *l_filename, const char *filename)
1192{
Heinrich Schuchardt58587212019-05-12 09:59:18 +02001193 const char *p, illegal[] = "<>:\"/\\|?*";
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001194
Heinrich Schuchardt58587212019-05-12 09:59:18 +02001195 if (strlen(filename) >= VFAT_MAXLEN_BYTES)
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001196 return -1;
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001197
Heinrich Schuchardt58587212019-05-12 09:59:18 +02001198 for (p = filename; *p; ++p) {
1199 if ((unsigned char)*p < 0x20)
1200 return -1;
1201 if (strchr(illegal, *p))
1202 return -1;
1203 }
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001204
Heinrich Schuchardt58587212019-05-12 09:59:18 +02001205 strcpy(l_filename, filename);
1206 downcase(l_filename, VFAT_MAXLEN_BYTES);
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001207
1208 return 0;
1209}
1210
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001211int file_fat_write_at(const char *filename, loff_t pos, void *buffer,
1212 loff_t size, loff_t *actwrite)
Donggeun Kim8f814002011-10-24 21:15:28 +00001213{
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001214 dir_entry *retdent;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001215 fsdata datablock = { .fatbuf = NULL, };
Donggeun Kim8f814002011-10-24 21:15:28 +00001216 fsdata *mydata = &datablock;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001217 fat_itr *itr = NULL;
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001218 int ret = -1;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001219 char *filename_copy, *parent, *basename;
Donggeun Kim8f814002011-10-24 21:15:28 +00001220 char l_filename[VFAT_MAXLEN_BYTES];
1221
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001222 debug("writing %s\n", filename);
1223
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001224 filename_copy = strdup(filename);
1225 if (!filename_copy)
1226 return -ENOMEM;
Donggeun Kim8f814002011-10-24 21:15:28 +00001227
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001228 split_filename(filename_copy, &parent, &basename);
1229 if (!strlen(basename)) {
1230 ret = -EINVAL;
1231 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001232 }
1233
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001234 filename = basename;
1235 if (normalize_longname(l_filename, filename)) {
1236 printf("FAT: illegal filename (%s)\n", filename);
1237 ret = -EINVAL;
1238 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001239 }
1240
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001241 itr = malloc_cache_aligned(sizeof(fat_itr));
1242 if (!itr) {
1243 ret = -ENOMEM;
1244 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001245 }
1246
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001247 ret = fat_itr_root(itr, &datablock);
1248 if (ret)
Donggeun Kim8f814002011-10-24 21:15:28 +00001249 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001250
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001251 total_sector = datablock.total_sect;
1252
1253 ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1254 if (ret) {
1255 printf("%s: doesn't exist (%d)\n", parent, ret);
AKASHI Takahiroc83df1a2018-09-11 15:59:02 +09001256 goto exit;
1257 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001258
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001259 retdent = find_directory_entry(itr, l_filename);
1260
Donggeun Kim8f814002011-10-24 21:15:28 +00001261 if (retdent) {
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001262 if (fat_itr_isdir(itr)) {
1263 ret = -EISDIR;
1264 goto exit;
1265 }
1266
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001267 /* A file exists */
1268 if (pos == -1)
1269 /* Append to the end */
1270 pos = FAT2CPU32(retdent->size);
1271 if (pos > retdent->size) {
1272 /* No hole allowed */
1273 ret = -EINVAL;
1274 goto exit;
1275 }
1276
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001277 /* Update file size in a directory entry */
1278 retdent->size = cpu_to_le32(pos + size);
Donggeun Kim8f814002011-10-24 21:15:28 +00001279 } else {
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001280 /* Create a new file */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001281 char shortname[SHORT_NAME_SIZE];
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001282
1283 if (itr->is_root) {
1284 /* root dir cannot have "." or ".." */
1285 if (!strcmp(l_filename, ".") ||
1286 !strcmp(l_filename, "..")) {
1287 ret = -EINVAL;
1288 goto exit;
1289 }
1290 }
1291
1292 if (!itr->dent) {
1293 printf("Error: allocating new dir entry\n");
1294 ret = -EIO;
1295 goto exit;
1296 }
1297
AKASHI Takahirob07f7042018-09-11 15:59:06 +09001298 if (pos) {
1299 /* No hole allowed */
1300 ret = -EINVAL;
1301 goto exit;
1302 }
1303
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001304 /* Check if long name is needed */
Heinrich Schuchardt24977e52020-11-25 16:33:55 +01001305 ret = set_name(itr, filename, shortname);
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001306 if (ret < 0)
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001307 goto exit;
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001308 if (ret > 1) {
1309 /* Set long name entries */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001310 ret = fill_dir_slot(itr, filename, shortname);
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001311 if (ret)
1312 goto exit;
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001313 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001314
AKASHI Takahiroa2c0bd02019-05-24 14:10:36 +09001315 /* Set short name entry */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001316 fill_dentry(itr->fsdata, itr->dent, shortname, 0, size,
Heinrich Schuchardt881c74a2020-11-22 11:13:33 +01001317 ATTR_ARCH);
Donggeun Kim8f814002011-10-24 21:15:28 +00001318
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001319 retdent = itr->dent;
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001320 }
Donggeun Kim8f814002011-10-24 21:15:28 +00001321
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001322 ret = set_contents(mydata, retdent, pos, buffer, size, actwrite);
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001323 if (ret < 0) {
1324 printf("Error: writing contents\n");
AKASHI Takahiro6aa77692018-09-11 15:59:03 +09001325 ret = -EIO;
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001326 goto exit;
1327 }
1328 debug("attempt to write 0x%llx bytes\n", *actwrite);
Donggeun Kim8f814002011-10-24 21:15:28 +00001329
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001330 /* Flush fat buffer */
Stefan Brüns751b31d2016-09-11 22:51:40 +02001331 ret = flush_dirty_fat_buffer(mydata);
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001332 if (ret) {
1333 printf("Error: flush fat buffer\n");
AKASHI Takahiro6aa77692018-09-11 15:59:03 +09001334 ret = -EIO;
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001335 goto exit;
Donggeun Kim8f814002011-10-24 21:15:28 +00001336 }
1337
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001338 /* Write directory table to device */
AKASHI Takahirod98c6742019-05-24 14:10:35 +09001339 ret = flush_dir(itr);
AKASHI Takahiro6aa77692018-09-11 15:59:03 +09001340 if (ret) {
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001341 printf("Error: writing directory entry\n");
AKASHI Takahiro6aa77692018-09-11 15:59:03 +09001342 ret = -EIO;
1343 }
Benoît Thébaudeaud1d390a2015-09-28 15:45:31 +02001344
Donggeun Kim8f814002011-10-24 21:15:28 +00001345exit:
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001346 free(filename_copy);
Donggeun Kim8f814002011-10-24 21:15:28 +00001347 free(mydata->fatbuf);
AKASHI Takahiro49ca96b2018-09-11 15:59:04 +09001348 free(itr);
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001349 return ret;
Donggeun Kim8f814002011-10-24 21:15:28 +00001350}
1351
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001352int file_fat_write(const char *filename, void *buffer, loff_t offset,
1353 loff_t maxsize, loff_t *actwrite)
Donggeun Kim8f814002011-10-24 21:15:28 +00001354{
AKASHI Takahiroed8b1e42018-09-11 15:59:05 +09001355 return file_fat_write_at(filename, offset, buffer, maxsize, actwrite);
Donggeun Kim8f814002011-10-24 21:15:28 +00001356}
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001357
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001358static int fat_dir_entries(fat_itr *itr)
1359{
1360 fat_itr *dirs;
1361 fsdata fsdata = { .fatbuf = NULL, }, *mydata = &fsdata;
1362 /* for FATBUFSIZE */
1363 int count;
1364
1365 dirs = malloc_cache_aligned(sizeof(fat_itr));
1366 if (!dirs) {
1367 debug("Error: allocating memory\n");
1368 count = -ENOMEM;
1369 goto exit;
1370 }
1371
1372 /* duplicate fsdata */
1373 fat_itr_child(dirs, itr);
1374 fsdata = *dirs->fsdata;
1375
1376 /* allocate local fat buffer */
1377 fsdata.fatbuf = malloc_cache_aligned(FATBUFSIZE);
1378 if (!fsdata.fatbuf) {
1379 debug("Error: allocating memory\n");
1380 count = -ENOMEM;
1381 goto exit;
1382 }
1383 fsdata.fatbufnum = -1;
1384 dirs->fsdata = &fsdata;
1385
1386 for (count = 0; fat_itr_next(dirs); count++)
1387 ;
1388
1389exit:
1390 free(fsdata.fatbuf);
1391 free(dirs);
1392 return count;
1393}
1394
1395static int delete_dentry(fat_itr *itr)
1396{
1397 fsdata *mydata = itr->fsdata;
1398 dir_entry *dentptr = itr->dent;
1399
1400 /* free cluster blocks */
1401 clear_fatent(mydata, START(dentptr));
1402 if (flush_dirty_fat_buffer(mydata) < 0) {
1403 printf("Error: flush fat buffer\n");
1404 return -EIO;
1405 }
1406
1407 /*
1408 * update a directory entry
1409 * TODO:
1410 * - long file name support
1411 * - find and mark the "new" first invalid entry as name[0]=0x00
1412 */
1413 memset(dentptr, 0, sizeof(*dentptr));
1414 dentptr->name[0] = 0xe5;
1415
AKASHI Takahirod98c6742019-05-24 14:10:35 +09001416 if (flush_dir(itr)) {
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001417 printf("error: writing directory entry\n");
1418 return -EIO;
1419 }
1420
1421 return 0;
1422}
1423
1424int fat_unlink(const char *filename)
1425{
1426 fsdata fsdata = { .fatbuf = NULL, };
1427 fat_itr *itr = NULL;
1428 int n_entries, ret;
1429 char *filename_copy, *dirname, *basename;
1430
1431 filename_copy = strdup(filename);
Heinrich Schuchardtb2242f62018-10-02 06:58:00 +02001432 if (!filename_copy) {
1433 printf("Error: allocating memory\n");
1434 ret = -ENOMEM;
1435 goto exit;
1436 }
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001437 split_filename(filename_copy, &dirname, &basename);
1438
1439 if (!strcmp(dirname, "/") && !strcmp(basename, "")) {
1440 printf("Error: cannot remove root\n");
1441 ret = -EINVAL;
1442 goto exit;
1443 }
1444
1445 itr = malloc_cache_aligned(sizeof(fat_itr));
1446 if (!itr) {
1447 printf("Error: allocating memory\n");
Heinrich Schuchardtb2242f62018-10-02 06:58:00 +02001448 ret = -ENOMEM;
1449 goto exit;
AKASHI Takahiro73b34972018-09-11 15:59:14 +09001450 }
1451
1452 ret = fat_itr_root(itr, &fsdata);
1453 if (ret)
1454 goto exit;
1455
1456 total_sector = fsdata.total_sect;
1457
1458 ret = fat_itr_resolve(itr, dirname, TYPE_DIR);
1459 if (ret) {
1460 printf("%s: doesn't exist (%d)\n", dirname, ret);
1461 ret = -ENOENT;
1462 goto exit;
1463 }
1464
1465 if (!find_directory_entry(itr, basename)) {
1466 printf("%s: doesn't exist\n", basename);
1467 ret = -ENOENT;
1468 goto exit;
1469 }
1470
1471 if (fat_itr_isdir(itr)) {
1472 n_entries = fat_dir_entries(itr);
1473 if (n_entries < 0) {
1474 ret = n_entries;
1475 goto exit;
1476 }
1477 if (n_entries > 2) {
1478 printf("Error: directory is not empty: %d\n",
1479 n_entries);
1480 ret = -EINVAL;
1481 goto exit;
1482 }
1483 }
1484
1485 ret = delete_dentry(itr);
1486
1487exit:
1488 free(fsdata.fatbuf);
1489 free(itr);
1490 free(filename_copy);
1491
1492 return ret;
1493}
1494
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001495int fat_mkdir(const char *new_dirname)
1496{
1497 dir_entry *retdent;
1498 fsdata datablock = { .fatbuf = NULL, };
1499 fsdata *mydata = &datablock;
1500 fat_itr *itr = NULL;
1501 char *dirname_copy, *parent, *dirname;
1502 char l_dirname[VFAT_MAXLEN_BYTES];
1503 int ret = -1;
1504 loff_t actwrite;
1505 unsigned int bytesperclust;
1506 dir_entry *dotdent = NULL;
1507
1508 dirname_copy = strdup(new_dirname);
1509 if (!dirname_copy)
1510 goto exit;
1511
1512 split_filename(dirname_copy, &parent, &dirname);
1513 if (!strlen(dirname)) {
1514 ret = -EINVAL;
1515 goto exit;
1516 }
1517
1518 if (normalize_longname(l_dirname, dirname)) {
1519 printf("FAT: illegal filename (%s)\n", dirname);
1520 ret = -EINVAL;
1521 goto exit;
1522 }
1523
1524 itr = malloc_cache_aligned(sizeof(fat_itr));
1525 if (!itr) {
1526 ret = -ENOMEM;
1527 goto exit;
1528 }
1529
1530 ret = fat_itr_root(itr, &datablock);
1531 if (ret)
1532 goto exit;
1533
1534 total_sector = datablock.total_sect;
1535
1536 ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1537 if (ret) {
1538 printf("%s: doesn't exist (%d)\n", parent, ret);
1539 goto exit;
1540 }
1541
1542 retdent = find_directory_entry(itr, l_dirname);
1543
1544 if (retdent) {
1545 printf("%s: already exists\n", l_dirname);
1546 ret = -EEXIST;
1547 goto exit;
1548 } else {
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001549 char shortname[SHORT_NAME_SIZE];
1550
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001551 if (itr->is_root) {
1552 /* root dir cannot have "." or ".." */
1553 if (!strcmp(l_dirname, ".") ||
1554 !strcmp(l_dirname, "..")) {
1555 ret = -EINVAL;
1556 goto exit;
1557 }
1558 }
1559
1560 if (!itr->dent) {
1561 printf("Error: allocating new dir entry\n");
1562 ret = -EIO;
1563 goto exit;
1564 }
1565
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001566 /* Check if long name is needed */
Heinrich Schuchardt24977e52020-11-25 16:33:55 +01001567 ret = set_name(itr, dirname, shortname);
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001568 if (ret < 0)
1569 goto exit;
1570 if (ret > 1) {
1571 /* Set long name entries */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001572 ret = fill_dir_slot(itr, dirname, shortname);
Heinrich Schuchardtba9c44e2020-11-20 12:55:22 +01001573 if (ret)
1574 goto exit;
1575 }
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001576
1577 /* Set attribute as archive for regular file */
Heinrich Schuchardt7afbd602020-11-22 19:19:39 +01001578 fill_dentry(itr->fsdata, itr->dent, shortname, 0, 0,
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001579 ATTR_DIR | ATTR_ARCH);
1580
1581 retdent = itr->dent;
1582 }
1583
1584 /* Default entries */
1585 bytesperclust = mydata->clust_size * mydata->sect_size;
1586 dotdent = malloc_cache_aligned(bytesperclust);
1587 if (!dotdent) {
1588 ret = -ENOMEM;
1589 goto exit;
1590 }
1591 memset(dotdent, 0, bytesperclust);
1592
1593 memcpy(dotdent[0].name, ". ", 8);
1594 memcpy(dotdent[0].ext, " ", 3);
1595 dotdent[0].attr = ATTR_DIR | ATTR_ARCH;
1596
1597 memcpy(dotdent[1].name, ".. ", 8);
1598 memcpy(dotdent[1].ext, " ", 3);
1599 dotdent[1].attr = ATTR_DIR | ATTR_ARCH;
Heinrich Schuchardt00cf0762020-11-24 21:04:07 +01001600
1601 if (itr->is_root)
1602 set_start_cluster(mydata, &dotdent[1], 0);
1603 else
1604 set_start_cluster(mydata, &dotdent[1], itr->start_clust);
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001605
1606 ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
1607 bytesperclust, &actwrite);
1608 if (ret < 0) {
1609 printf("Error: writing contents\n");
1610 goto exit;
1611 }
1612 /* Write twice for "." */
1613 set_start_cluster(mydata, &dotdent[0], START(retdent));
1614 ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
1615 bytesperclust, &actwrite);
1616 if (ret < 0) {
1617 printf("Error: writing contents\n");
1618 goto exit;
1619 }
1620
1621 /* Flush fat buffer */
1622 ret = flush_dirty_fat_buffer(mydata);
1623 if (ret) {
1624 printf("Error: flush fat buffer\n");
1625 goto exit;
1626 }
1627
1628 /* Write directory table to device */
AKASHI Takahirod98c6742019-05-24 14:10:35 +09001629 ret = flush_dir(itr);
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +09001630 if (ret)
1631 printf("Error: writing directory entry\n");
1632
1633exit:
1634 free(dirname_copy);
1635 free(mydata->fatbuf);
1636 free(itr);
1637 free(dotdent);
1638 return ret;
1639}