blob: c8e0fbf1a3b3d0b8e5458d10c5b2aa7af0a60681 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Donggeun Kimc30a15e2011-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 Kimc30a15e2011-10-24 21:15:28 +00006 */
7
Simon Glass4f6daac2023-01-28 15:00:16 -07008#define LOG_CATEGORY LOGC_FS
9
Tom Rinid678a592024-05-18 20:20:43 -060010#include <common.h>
Donggeun Kimc30a15e2011-10-24 21:15:28 +000011#include <command.h>
12#include <config.h>
Heinrich Schuchardt28cef9c2020-11-20 12:55:22 +010013#include <div64.h>
Donggeun Kimc30a15e2011-10-24 21:15:28 +000014#include <fat.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070016#include <malloc.h>
Donggeun Kimc30a15e2011-10-24 21:15:28 +000017#include <part.h>
Heinrich Schuchardt28cef9c2020-11-20 12:55:22 +010018#include <rand.h>
19#include <asm/byteorder.h>
Simon Glass90526e92020-05-10 11:39:56 -060020#include <asm/cache.h>
Heinrich Schuchardtba23c372024-04-09 22:06:07 +020021#include <dm/uclass.h>
Richard Genoudfb7e16c2012-12-13 00:47:36 +000022#include <linux/ctype.h>
Tom Rini9e374e72014-11-24 11:50:46 -050023#include <linux/math64.h>
Donggeun Kimc30a15e2011-10-24 21:15:28 +000024#include "fat.c"
25
Heinrich Schuchardt3a331ae2020-11-25 16:33:55 +010026static dir_entry *find_directory_entry(fat_itr *itr, char *filename);
Heinrich Schuchardt32a5f882020-11-22 09:58:44 +010027static int new_dir_table(fat_itr *itr);
Heinrich Schuchardt3a331ae2020-11-25 16:33:55 +010028
Heinrich Schuchardt28cef9c2020-11-20 12:55:22 +010029/* Characters that may only be used in long file names */
30static const char LONG_ONLY_CHARS[] = "+,;=[]";
31
Heinrich Schuchardt57b745e2020-11-22 19:19:39 +010032/* Combined size of the name and ext fields in the directory entry */
33#define SHORT_NAME_SIZE 11
34
Heinrich Schuchardt28cef9c2020-11-20 12:55:22 +010035/**
36 * str2fat() - convert string to valid FAT name characters
37 *
38 * Stop when reaching end of @src or a period.
39 * Ignore spaces.
40 * Replace characters that may only be used in long names by underscores.
41 * Convert lower case characters to upper case.
42 *
43 * To avoid assumptions about the code page we do not use characters
44 * above 0x7f for the short name.
45 *
46 * @dest: destination buffer
47 * @src: source buffer
48 * @length: size of destination buffer
49 * Return: number of bytes in destination buffer
50 */
51static int str2fat(char *dest, char *src, int length)
Donggeun Kimc30a15e2011-10-24 21:15:28 +000052{
53 int i;
54
Heinrich Schuchardt28cef9c2020-11-20 12:55:22 +010055 for (i = 0; i < length; ++src) {
56 char c = *src;
57
58 if (!c || c == '.')
59 break;
60 if (c == ' ')
61 continue;
62 if (strchr(LONG_ONLY_CHARS, c) || c > 0x7f)
63 c = '_';
64 else if (c >= 'a' && c <= 'z')
65 c &= 0xdf;
66 dest[i] = c;
67 ++i;
Donggeun Kimc30a15e2011-10-24 21:15:28 +000068 }
Heinrich Schuchardt28cef9c2020-11-20 12:55:22 +010069 return i;
70}
71
72/**
Heinrich Schuchardt3a331ae2020-11-25 16:33:55 +010073 * fat_move_to_cluster() - position to first directory entry in cluster
74 *
75 * @itr: directory iterator
76 * @cluster cluster
77 * Return: 0 for success, -EIO on error
78 */
79static int fat_move_to_cluster(fat_itr *itr, unsigned int cluster)
80{
81 unsigned int nbytes;
82
83 /* position to the start of the directory */
84 itr->next_clust = cluster;
85 itr->last_cluster = 0;
86 if (!fat_next_cluster(itr, &nbytes))
87 return -EIO;
88 itr->dent = (dir_entry *)itr->block;
89 itr->remaining = nbytes / sizeof(dir_entry) - 1;
90 return 0;
91}
92
93/**
Heinrich Schuchardt28cef9c2020-11-20 12:55:22 +010094 * set_name() - set short name in directory entry
95 *
96 * The function determines if the @filename is a valid short name.
97 * In this case no long name is needed.
98 *
99 * If a long name is needed, a short name is constructed.
100 *
Heinrich Schuchardt3a331ae2020-11-25 16:33:55 +0100101 * @itr: directory iterator
Heinrich Schuchardt28cef9c2020-11-20 12:55:22 +0100102 * @filename: long file name
Heinrich Schuchardt57b745e2020-11-22 19:19:39 +0100103 * @shortname: buffer of 11 bytes to receive chosen short name and extension
Heinrich Schuchardt28cef9c2020-11-20 12:55:22 +0100104 * Return: number of directory entries needed, negative on error
105 */
Heinrich Schuchardt3a331ae2020-11-25 16:33:55 +0100106static int set_name(fat_itr *itr, const char *filename, char *shortname)
Heinrich Schuchardt28cef9c2020-11-20 12:55:22 +0100107{
108 char *period;
109 char *pos;
110 int period_location;
111 char buf[13];
112 int i;
Heinrich Schuchardt57b745e2020-11-22 19:19:39 +0100113 int ret;
Heinrich Schuchardt41ac28c2021-01-26 00:04:19 +0100114 struct nameext dirent;
Heinrich Schuchardt28cef9c2020-11-20 12:55:22 +0100115
116 if (!filename)
117 return -EIO;
118
Heinrich Schuchardt57b745e2020-11-22 19:19:39 +0100119 /* Initialize buffer */
120 memset(&dirent, ' ', sizeof(dirent));
Heinrich Schuchardt28cef9c2020-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 Schuchardt57b745e2020-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 Schuchardt28cef9c2020-11-20 12:55:22 +0100132 if (period_location < 0)
133 return period_location;
Heinrich Schuchardt57b745e2020-11-22 19:19:39 +0100134 if (*dirent.name == ' ')
135 *dirent.name = '_';
Heinrich Schuchardt42cd7592023-07-26 10:33:13 +0200136 /* Substitute character 0xe5 signaling deletetion by character 0x05 */
137 if (*dirent.name == DELETED_FLAG)
138 *dirent.name = aRING;
Heinrich Schuchardt28cef9c2020-11-20 12:55:22 +0100139
140 /* If filename and short name are the same, quit. */
Heinrich Schuchardt57b745e2020-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;
Stefan Herbrechtsmeierfefd9492023-03-17 13:04:13 +0100145 } else if (!strcasecmp(buf, filename)) {
146 goto out_ret;
Heinrich Schuchardt57b745e2020-11-22 19:19:39 +0100147 }
Heinrich Schuchardt28cef9c2020-11-20 12:55:22 +0100148
149 /* Construct an indexed short name */
150 for (i = 1; i < 0x200000; ++i) {
151 int suffix_len;
152 int suffix_start;
153 int j;
154
155 /* To speed up the search use random numbers */
156 if (i < 10) {
157 j = i;
158 } else {
159 j = 30 - fls(i);
160 j = 10 + (rand() >> j);
161 }
162 sprintf(buf, "~%d", j);
163 suffix_len = strlen(buf);
164 suffix_start = 8 - suffix_len;
165 if (suffix_start > period_location)
166 suffix_start = period_location;
Heinrich Schuchardt57b745e2020-11-22 19:19:39 +0100167 memcpy(dirent.name + suffix_start, buf, suffix_len);
168 if (*dirent.ext != ' ')
Heinrich Schuchardt28cef9c2020-11-20 12:55:22 +0100169 sprintf(buf, "%.*s.%.3s", suffix_start + suffix_len,
Heinrich Schuchardt57b745e2020-11-22 19:19:39 +0100170 dirent.name, dirent.ext);
Heinrich Schuchardt28cef9c2020-11-20 12:55:22 +0100171 else
172 sprintf(buf, "%.*s", suffix_start + suffix_len,
Heinrich Schuchardt57b745e2020-11-22 19:19:39 +0100173 dirent.name);
Heinrich Schuchardt3a331ae2020-11-25 16:33:55 +0100174 debug("generated short name: %s\n", buf);
Heinrich Schuchardt28cef9c2020-11-20 12:55:22 +0100175
Heinrich Schuchardt3a331ae2020-11-25 16:33:55 +0100176 /* Check that the short name does not exist yet. */
177 ret = fat_move_to_cluster(itr, itr->start_clust);
178 if (ret)
179 return ret;
180 if (find_directory_entry(itr, buf))
181 continue;
182
Stefan Herbrechtsmeierfefd9492023-03-17 13:04:13 +0100183 goto out_ret;
Heinrich Schuchardt28cef9c2020-11-20 12:55:22 +0100184 }
185 return -EIO;
Stefan Herbrechtsmeierfefd9492023-03-17 13:04:13 +0100186out_ret:
187 debug("chosen short name: %s\n", buf);
188 /* Each long name directory entry takes 13 characters. */
189 ret = (strlen(filename) + 25) / 13;
Heinrich Schuchardt57b745e2020-11-22 19:19:39 +0100190out:
Heinrich Schuchardt41ac28c2021-01-26 00:04:19 +0100191 memcpy(shortname, &dirent, SHORT_NAME_SIZE);
Heinrich Schuchardt57b745e2020-11-22 19:19:39 +0100192 return ret;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000193}
194
195static int total_sector;
Donggeun Kim079df722012-03-22 04:38:55 +0000196static int disk_write(__u32 block, __u32 nr_blocks, void *buf)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000197{
Łukasz Majewski0a04ed82015-09-03 14:21:39 +0200198 ulong ret;
199
Simon Glass2a981dc2016-02-29 15:25:52 -0700200 if (!cur_dev)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000201 return -1;
202
Donggeun Kim079df722012-03-22 04:38:55 +0000203 if (cur_part_info.start + block + nr_blocks >
204 cur_part_info.start + total_sector) {
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000205 printf("error: overflow occurs\n");
206 return -1;
207 }
208
Simon Glass2a981dc2016-02-29 15:25:52 -0700209 ret = blk_dwrite(cur_dev, cur_part_info.start + block, nr_blocks, buf);
Łukasz Majewski0a04ed82015-09-03 14:21:39 +0200210 if (nr_blocks && ret == 0)
211 return -1;
212
213 return ret;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000214}
215
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000216/*
217 * Write fat buffer into block device
218 */
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200219static int flush_dirty_fat_buffer(fsdata *mydata)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000220{
221 int getsize = FATBUFBLOCKS;
222 __u32 fatlength = mydata->fatlength;
223 __u8 *bufptr = mydata->fatbuf;
224 __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
225
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200226 debug("debug: evicting %d, dirty: %d\n", mydata->fatbufnum,
227 (int)mydata->fat_dirty);
228
229 if ((!mydata->fat_dirty) || (mydata->fatbufnum == -1))
230 return 0;
231
Stefan Brüns6c1a8082016-12-17 00:27:50 +0100232 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
233 if (startblock + getsize > fatlength)
234 getsize = fatlength - startblock;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000235
Stefan Brüns6c1a8082016-12-17 00:27:50 +0100236 startblock += mydata->fat_sect;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000237
238 /* Write FAT buf */
239 if (disk_write(startblock, getsize, bufptr) < 0) {
240 debug("error: writing FAT blocks\n");
241 return -1;
242 }
243
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900244 if (mydata->fats == 2) {
Donggeun Kim627182e2011-12-20 18:34:27 +0000245 /* Update corresponding second FAT blocks */
246 startblock += mydata->fatlength;
247 if (disk_write(startblock, getsize, bufptr) < 0) {
248 debug("error: writing second FAT blocks\n");
249 return -1;
250 }
251 }
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200252 mydata->fat_dirty = 0;
Donggeun Kim627182e2011-12-20 18:34:27 +0000253
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000254 return 0;
255}
256
Heinrich Schuchardt32a5f882020-11-22 09:58:44 +0100257/**
258 * fat_find_empty_dentries() - find a sequence of available directory entries
259 *
260 * @itr: directory iterator
261 * @count: number of directory entries to find
262 * Return: 0 on success or negative error number
263 */
Heinrich Schuchardt3049a512020-11-22 11:54:22 +0100264static int fat_find_empty_dentries(fat_itr *itr, int count)
Heinrich Schuchardt32a5f882020-11-22 09:58:44 +0100265{
266 unsigned int cluster;
267 dir_entry *dent;
268 int remaining;
269 unsigned int n = 0;
270 int ret;
271
272 ret = fat_move_to_cluster(itr, itr->start_clust);
273 if (ret)
274 return ret;
275
276 for (;;) {
277 if (!itr->dent) {
278 log_debug("Not enough directory entries available\n");
279 return -ENOSPC;
280 }
Heinrich Schuchardt041f0af2021-01-21 00:23:33 +0100281 switch (itr->dent->nameext.name[0]) {
Heinrich Schuchardt32a5f882020-11-22 09:58:44 +0100282 case 0x00:
283 case DELETED_FLAG:
284 if (!n) {
285 /* Remember first deleted directory entry */
286 cluster = itr->clust;
287 dent = itr->dent;
288 remaining = itr->remaining;
289 }
290 ++n;
291 if (n == count)
292 goto out;
293 break;
294 default:
295 n = 0;
296 break;
297 }
298
299 next_dent(itr);
300 if (!itr->dent &&
301 (!itr->is_root || itr->fsdata->fatsize == 32) &&
302 new_dir_table(itr))
303 return -ENOSPC;
304 }
305out:
306 /* Position back to first directory entry */
307 if (itr->clust != cluster) {
308 ret = fat_move_to_cluster(itr, cluster);
309 if (ret)
310 return ret;
311 }
312 itr->dent = dent;
313 itr->remaining = remaining;
314 return 0;
315}
316
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000317/*
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000318 * Set the file name information from 'name' into 'slotptr',
319 */
320static int str2slot(dir_slot *slotptr, const char *name, int *idx)
321{
322 int j, end_idx = 0;
323
324 for (j = 0; j <= 8; j += 2) {
325 if (name[*idx] == 0x00) {
326 slotptr->name0_4[j] = 0;
327 slotptr->name0_4[j + 1] = 0;
328 end_idx++;
329 goto name0_4;
330 }
331 slotptr->name0_4[j] = name[*idx];
332 (*idx)++;
333 end_idx++;
334 }
335 for (j = 0; j <= 10; j += 2) {
336 if (name[*idx] == 0x00) {
337 slotptr->name5_10[j] = 0;
338 slotptr->name5_10[j + 1] = 0;
339 end_idx++;
340 goto name5_10;
341 }
342 slotptr->name5_10[j] = name[*idx];
343 (*idx)++;
344 end_idx++;
345 }
346 for (j = 0; j <= 2; j += 2) {
347 if (name[*idx] == 0x00) {
348 slotptr->name11_12[j] = 0;
349 slotptr->name11_12[j + 1] = 0;
350 end_idx++;
351 goto name11_12;
352 }
353 slotptr->name11_12[j] = name[*idx];
354 (*idx)++;
355 end_idx++;
356 }
357
358 if (name[*idx] == 0x00)
359 return 1;
360
361 return 0;
362/* Not used characters are filled with 0xff 0xff */
363name0_4:
364 for (; end_idx < 5; end_idx++) {
365 slotptr->name0_4[end_idx * 2] = 0xff;
366 slotptr->name0_4[end_idx * 2 + 1] = 0xff;
367 }
368 end_idx = 5;
369name5_10:
370 end_idx -= 5;
371 for (; end_idx < 6; end_idx++) {
372 slotptr->name5_10[end_idx * 2] = 0xff;
373 slotptr->name5_10[end_idx * 2 + 1] = 0xff;
374 }
375 end_idx = 11;
376name11_12:
377 end_idx -= 11;
378 for (; end_idx < 2; end_idx++) {
379 slotptr->name11_12[end_idx * 2] = 0xff;
380 slotptr->name11_12[end_idx * 2 + 1] = 0xff;
381 }
382
383 return 1;
384}
385
AKASHI Takahiro9c709c72019-05-24 14:10:36 +0900386static int flush_dir(fat_itr *itr);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000387
Heinrich Schuchardta3432492020-11-21 08:32:50 +0100388/**
389 * fill_dir_slot() - fill directory entries for long name
390 *
391 * @itr: directory iterator
392 * @l_name: long name
393 * @shortname: short name
394 * Return: 0 for success, -errno otherwise
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000395 */
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900396static int
Heinrich Schuchardta3432492020-11-21 08:32:50 +0100397fill_dir_slot(fat_itr *itr, const char *l_name, const char *shortname)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000398{
Tien Fong Chee7aa1a6b2016-07-27 23:08:56 -0700399 __u8 temp_dir_slot_buffer[MAX_LFN_SLOT * sizeof(dir_slot)];
400 dir_slot *slotptr = (dir_slot *)temp_dir_slot_buffer;
Anatolij Gustschin8506eb82011-12-15 03:12:14 +0000401 __u8 counter = 0, checksum;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000402 int idx = 0, ret;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000403
Stefan Brünsed76f912016-09-11 22:51:39 +0200404 /* Get short file name checksum value */
Heinrich Schuchardt041f0af2021-01-21 00:23:33 +0100405 checksum = mkcksum((void *)shortname);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000406
407 do {
408 memset(slotptr, 0x00, sizeof(dir_slot));
409 ret = str2slot(slotptr, l_name, &idx);
410 slotptr->id = ++counter;
411 slotptr->attr = ATTR_VFAT;
412 slotptr->alias_checksum = checksum;
413 slotptr++;
414 } while (ret == 0);
415
416 slotptr--;
417 slotptr->id |= LAST_LONG_ENTRY_MASK;
418
419 while (counter >= 1) {
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900420 memcpy(itr->dent, slotptr, sizeof(dir_slot));
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000421 slotptr--;
422 counter--;
AKASHI Takahiro9c709c72019-05-24 14:10:36 +0900423
Heinrich Schuchardte97eb632021-01-20 22:21:53 +0100424 if (!itr->remaining) {
425 /* Write directory table to device */
426 ret = flush_dir(itr);
427 if (ret)
428 return ret;
429 }
AKASHI Takahiro9c709c72019-05-24 14:10:36 +0900430
Heinrich Schuchardt3049a512020-11-22 11:54:22 +0100431 next_dent(itr);
432 if (!itr->dent)
433 return -EIO;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000434 }
435
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000436 return 0;
437}
438
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000439/*
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500440 * Set the entry at index 'entry' in a FAT (12/16/32) table.
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000441 */
442static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
443{
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500444 __u32 bufnum, offset, off16;
445 __u16 val1, val2;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000446
447 switch (mydata->fatsize) {
448 case 32:
449 bufnum = entry / FAT32BUFSIZE;
450 offset = entry - bufnum * FAT32BUFSIZE;
451 break;
452 case 16:
453 bufnum = entry / FAT16BUFSIZE;
454 offset = entry - bufnum * FAT16BUFSIZE;
455 break;
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500456 case 12:
457 bufnum = entry / FAT12BUFSIZE;
458 offset = entry - bufnum * FAT12BUFSIZE;
459 break;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000460 default:
461 /* Unsupported FAT size */
462 return -1;
463 }
464
465 /* Read a new block of FAT entries into the cache. */
466 if (bufnum != mydata->fatbufnum) {
467 int getsize = FATBUFBLOCKS;
468 __u8 *bufptr = mydata->fatbuf;
469 __u32 fatlength = mydata->fatlength;
470 __u32 startblock = bufnum * FATBUFBLOCKS;
471
Stefan Brüns6c1a8082016-12-17 00:27:50 +0100472 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
473 if (startblock + getsize > fatlength)
474 getsize = fatlength - startblock;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000475
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200476 if (flush_dirty_fat_buffer(mydata) < 0)
477 return -1;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000478
Stefan Brüns6c1a8082016-12-17 00:27:50 +0100479 startblock += mydata->fat_sect;
480
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000481 if (disk_read(startblock, getsize, bufptr) < 0) {
482 debug("Error reading FAT blocks\n");
483 return -1;
484 }
485 mydata->fatbufnum = bufnum;
486 }
487
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200488 /* Mark as dirty */
489 mydata->fat_dirty = 1;
490
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000491 /* Set the actual entry */
492 switch (mydata->fatsize) {
493 case 32:
494 ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
495 break;
496 case 16:
497 ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
498 break;
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500499 case 12:
500 off16 = (offset * 3) / 4;
501
502 switch (offset & 0x3) {
503 case 0:
504 val1 = cpu_to_le16(entry_value) & 0xfff;
505 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff;
506 ((__u16 *)mydata->fatbuf)[off16] |= val1;
507 break;
508 case 1:
509 val1 = cpu_to_le16(entry_value) & 0xf;
510 val2 = (cpu_to_le16(entry_value) >> 4) & 0xff;
511
512 ((__u16 *)mydata->fatbuf)[off16] &= ~0xf000;
513 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 12);
514
515 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xff;
516 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
517 break;
518 case 2:
519 val1 = cpu_to_le16(entry_value) & 0xff;
520 val2 = (cpu_to_le16(entry_value) >> 8) & 0xf;
521
522 ((__u16 *)mydata->fatbuf)[off16] &= ~0xff00;
523 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 8);
524
525 ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xf;
526 ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2;
527 break;
528 case 3:
529 val1 = cpu_to_le16(entry_value) & 0xfff;
530 ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff0;
531 ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 4);
532 break;
533 default:
534 break;
535 }
536
537 break;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000538 default:
539 return -1;
540 }
541
542 return 0;
543}
544
545/*
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500546 * Determine the next free cluster after 'entry' in a FAT (12/16/32) table
Stefan Brünsae1755b2016-09-11 22:51:41 +0200547 * and link it to 'entry'. EOC marker is not set on returned entry.
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000548 */
549static __u32 determine_fatent(fsdata *mydata, __u32 entry)
550{
551 __u32 next_fat, next_entry = entry + 1;
552
553 while (1) {
Stefan Brünsb8948d22016-12-17 00:27:51 +0100554 next_fat = get_fatent(mydata, next_entry);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000555 if (next_fat == 0) {
Stefan Brünsae1755b2016-09-11 22:51:41 +0200556 /* found free entry, link to entry */
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000557 set_fatent_value(mydata, entry, next_entry);
558 break;
559 }
560 next_entry++;
561 }
562 debug("FAT%d: entry: %08x, entry_value: %04x\n",
563 mydata->fatsize, entry, next_entry);
564
565 return next_entry;
566}
567
Heinrich Schuchardtf105fe72018-10-02 09:30:45 +0200568/**
AKASHI Takahiroa9f67062019-05-24 14:10:35 +0900569 * set_sectors() - write data to sectors
Heinrich Schuchardtf105fe72018-10-02 09:30:45 +0200570 *
AKASHI Takahiroa9f67062019-05-24 14:10:35 +0900571 * Write 'size' bytes from 'buffer' into the specified sector.
Heinrich Schuchardtf105fe72018-10-02 09:30:45 +0200572 *
573 * @mydata: data to be written
AKASHI Takahiroa9f67062019-05-24 14:10:35 +0900574 * @startsect: sector to be written to
Heinrich Schuchardtf105fe72018-10-02 09:30:45 +0200575 * @buffer: data to be written
576 * @size: bytes to be written (but not more than the size of a cluster)
577 * Return: 0 on success, -1 otherwise
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000578 */
579static int
AKASHI Takahiroa9f67062019-05-24 14:10:35 +0900580set_sectors(fsdata *mydata, u32 startsect, u8 *buffer, u32 size)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000581{
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200582 int ret;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000583
AKASHI Takahiroa9f67062019-05-24 14:10:35 +0900584 debug("startsect: %d\n", startsect);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000585
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200586 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
587 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
588
Heinrich Schuchardt1c381ce2018-09-13 19:42:47 +0200589 debug("FAT: Misaligned buffer address (%p)\n", buffer);
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200590
591 while (size >= mydata->sect_size) {
592 memcpy(tmpbuf, buffer, mydata->sect_size);
593 ret = disk_write(startsect++, 1, tmpbuf);
594 if (ret != 1) {
595 debug("Error writing data (got %d)\n", ret);
596 return -1;
597 }
598
599 buffer += mydata->sect_size;
600 size -= mydata->sect_size;
601 }
602 } else if (size >= mydata->sect_size) {
Heinrich Schuchardt84ca3052021-01-26 00:14:14 +0100603 u32 nsects;
604
AKASHI Takahiroa9f67062019-05-24 14:10:35 +0900605 nsects = size / mydata->sect_size;
606 ret = disk_write(startsect, nsects, buffer);
607 if (ret != nsects) {
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200608 debug("Error writing data (got %d)\n", ret);
Wu, Josh6b8f1852013-07-24 17:55:30 +0800609 return -1;
610 }
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200611
AKASHI Takahiroa9f67062019-05-24 14:10:35 +0900612 startsect += nsects;
613 buffer += nsects * mydata->sect_size;
614 size -= nsects * mydata->sect_size;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000615 }
616
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200617 if (size) {
618 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Heinrich Schuchardtf105fe72018-10-02 09:30:45 +0200619 /* Do not leak content of stack */
620 memset(tmpbuf, 0, mydata->sect_size);
Benoît Thébaudeau8133f432015-09-28 15:45:28 +0200621 memcpy(tmpbuf, buffer, size);
622 ret = disk_write(startsect, 1, tmpbuf);
623 if (ret != 1) {
624 debug("Error writing data (got %d)\n", ret);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000625 return -1;
626 }
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000627 }
628
629 return 0;
630}
631
AKASHI Takahiroa9f67062019-05-24 14:10:35 +0900632/**
633 * set_cluster() - write data to cluster
634 *
635 * Write 'size' bytes from 'buffer' into the specified cluster.
636 *
637 * @mydata: data to be written
638 * @clustnum: cluster to be written to
639 * @buffer: data to be written
640 * @size: bytes to be written (but not more than the size of a cluster)
641 * Return: 0 on success, -1 otherwise
642 */
643static int
644set_cluster(fsdata *mydata, u32 clustnum, u8 *buffer, u32 size)
645{
646 return set_sectors(mydata, clust_to_sect(mydata, clustnum),
647 buffer, size);
648}
649
Heinrich Schuchardte97eb632021-01-20 22:21:53 +0100650/**
651 * flush_dir() - flush directory
652 *
653 * @itr: directory iterator
654 * Return: 0 for success, -EIO on error
655 */
656static int flush_dir(fat_itr *itr)
AKASHI Takahiroa9f67062019-05-24 14:10:35 +0900657{
658 fsdata *mydata = itr->fsdata;
659 u32 startsect, sect_offset, nsects;
Heinrich Schuchardte97eb632021-01-20 22:21:53 +0100660 int ret;
AKASHI Takahiroa9f67062019-05-24 14:10:35 +0900661
Heinrich Schuchardte97eb632021-01-20 22:21:53 +0100662 if (!itr->is_root || mydata->fatsize == 32) {
663 ret = set_cluster(mydata, itr->clust, itr->block,
664 mydata->clust_size * mydata->sect_size);
665 goto out;
666 }
AKASHI Takahiroa9f67062019-05-24 14:10:35 +0900667
668 sect_offset = itr->clust * mydata->clust_size;
669 startsect = mydata->rootdir_sect + sect_offset;
670 /* do not write past the end of rootdir */
671 nsects = min_t(u32, mydata->clust_size,
672 mydata->rootdir_size - sect_offset);
673
Heinrich Schuchardte97eb632021-01-20 22:21:53 +0100674 ret = set_sectors(mydata, startsect, itr->block,
675 nsects * mydata->sect_size);
676out:
677 if (ret) {
678 log_err("Error: writing directory entry\n");
679 return -EIO;
680 }
681 return 0;
AKASHI Takahiroa9f67062019-05-24 14:10:35 +0900682}
683
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +0900684/*
685 * Read and modify data on existing and consecutive cluster blocks
686 */
687static int
688get_set_cluster(fsdata *mydata, __u32 clustnum, loff_t pos, __u8 *buffer,
689 loff_t size, loff_t *gotsize)
690{
Heinrich Schuchardt5a8d1f62020-07-06 07:48:14 +0200691 static u8 *tmpbuf_cluster;
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +0900692 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
693 __u32 startsect;
Heinrich Schuchardt84032b62023-07-30 16:44:04 +0200694 loff_t clustcount, wsize;
695 int i, ret;
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +0900696
697 *gotsize = 0;
698 if (!size)
699 return 0;
700
Heinrich Schuchardt5a8d1f62020-07-06 07:48:14 +0200701 if (!tmpbuf_cluster) {
702 tmpbuf_cluster = memalign(ARCH_DMA_MINALIGN, MAX_CLUSTSIZE);
703 if (!tmpbuf_cluster)
704 return -1;
705 }
706
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +0900707 assert(pos < bytesperclust);
708 startsect = clust_to_sect(mydata, clustnum);
709
710 debug("clustnum: %d, startsect: %d, pos: %lld\n",
711 clustnum, startsect, pos);
712
713 /* partial write at beginning */
714 if (pos) {
715 wsize = min(bytesperclust - pos, size);
716 ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster);
717 if (ret != mydata->clust_size) {
718 debug("Error reading data (got %d)\n", ret);
719 return -1;
720 }
721
722 memcpy(tmpbuf_cluster + pos, buffer, wsize);
723 ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster);
724 if (ret != mydata->clust_size) {
725 debug("Error writing data (got %d)\n", ret);
726 return -1;
727 }
728
729 size -= wsize;
730 buffer += wsize;
731 *gotsize += wsize;
732
733 startsect += mydata->clust_size;
734
735 if (!size)
736 return 0;
737 }
738
739 /* full-cluster write */
740 if (size >= bytesperclust) {
741 clustcount = lldiv(size, bytesperclust);
742
743 if (!((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1))) {
744 wsize = clustcount * bytesperclust;
745 ret = disk_write(startsect,
746 clustcount * mydata->clust_size,
747 buffer);
748 if (ret != clustcount * mydata->clust_size) {
749 debug("Error writing data (got %d)\n", ret);
750 return -1;
751 }
752
753 size -= wsize;
754 buffer += wsize;
755 *gotsize += wsize;
756
757 startsect += clustcount * mydata->clust_size;
758 } else {
759 for (i = 0; i < clustcount; i++) {
760 memcpy(tmpbuf_cluster, buffer, bytesperclust);
761 ret = disk_write(startsect,
762 mydata->clust_size,
763 tmpbuf_cluster);
764 if (ret != mydata->clust_size) {
765 debug("Error writing data (got %d)\n",
766 ret);
767 return -1;
768 }
769
770 size -= bytesperclust;
771 buffer += bytesperclust;
772 *gotsize += bytesperclust;
773
774 startsect += mydata->clust_size;
775 }
776 }
777 }
778
779 /* partial write at end */
780 if (size) {
781 wsize = size;
782 ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster);
783 if (ret != mydata->clust_size) {
784 debug("Error reading data (got %d)\n", ret);
785 return -1;
786 }
787 memcpy(tmpbuf_cluster, buffer, wsize);
788 ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster);
789 if (ret != mydata->clust_size) {
790 debug("Error writing data (got %d)\n", ret);
791 return -1;
792 }
793
794 size -= wsize;
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +0900795 *gotsize += wsize;
796 }
797
798 assert(!size);
799
800 return 0;
801}
802
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000803/*
804 * Find the first empty cluster
805 */
806static int find_empty_cluster(fsdata *mydata)
807{
808 __u32 fat_val, entry = 3;
809
810 while (1) {
Stefan Brünsb8948d22016-12-17 00:27:51 +0100811 fat_val = get_fatent(mydata, entry);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000812 if (fat_val == 0)
813 break;
814 entry++;
815 }
816
817 return entry;
818}
819
Heinrich Schuchardt569b0e12020-11-26 19:06:55 +0100820/**
821 * new_dir_table() - allocate a cluster for additional directory entries
822 *
823 * @itr: directory iterator
824 * Return: 0 on success, -EIO otherwise
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000825 */
AKASHI Takahiro9c709c72019-05-24 14:10:36 +0900826static int new_dir_table(fat_itr *itr)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000827{
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900828 fsdata *mydata = itr->fsdata;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000829 int dir_newclust = 0;
Heinrich Schuchardt569b0e12020-11-26 19:06:55 +0100830 int dir_oldclust = itr->clust;
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900831 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000832
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000833 dir_newclust = find_empty_cluster(mydata);
Heinrich Schuchardt569b0e12020-11-26 19:06:55 +0100834
835 /*
836 * Flush before updating FAT to ensure valid directory structure
837 * in case of failure.
838 */
839 itr->clust = dir_newclust;
840 itr->next_clust = dir_newclust;
841 memset(itr->block, 0x00, bytesperclust);
842 if (flush_dir(itr))
843 return -EIO;
844
845 set_fatent_value(mydata, dir_oldclust, dir_newclust);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000846 if (mydata->fatsize == 32)
847 set_fatent_value(mydata, dir_newclust, 0xffffff8);
848 else if (mydata->fatsize == 16)
849 set_fatent_value(mydata, dir_newclust, 0xfff8);
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500850 else if (mydata->fatsize == 12)
851 set_fatent_value(mydata, dir_newclust, 0xff8);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000852
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200853 if (flush_dirty_fat_buffer(mydata) < 0)
Heinrich Schuchardt569b0e12020-11-26 19:06:55 +0100854 return -EIO;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000855
AKASHI Takahiro4ced2032018-09-11 15:59:04 +0900856 itr->dent = (dir_entry *)itr->block;
857 itr->last_cluster = 1;
858 itr->remaining = bytesperclust / sizeof(dir_entry) - 1;
859
860 return 0;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000861}
862
863/*
864 * Set empty cluster from 'entry' to the end of a file
865 */
866static int clear_fatent(fsdata *mydata, __u32 entry)
867{
868 __u32 fat_val;
869
Philipp Skadorov49abbd92016-12-15 15:52:53 -0500870 while (!CHECK_CLUST(entry, mydata->fatsize)) {
Stefan Brünsb8948d22016-12-17 00:27:51 +0100871 fat_val = get_fatent(mydata, entry);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000872 if (fat_val != 0)
873 set_fatent_value(mydata, entry, 0);
874 else
875 break;
876
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000877 entry = fat_val;
878 }
879
880 /* Flush fat buffer */
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200881 if (flush_dirty_fat_buffer(mydata) < 0)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000882 return -1;
883
884 return 0;
885}
886
887/*
AKASHI Takahiro704df6a2018-09-11 15:59:05 +0900888 * Set start cluster in directory entry
889 */
890static void set_start_cluster(const fsdata *mydata, dir_entry *dentptr,
891 __u32 start_cluster)
892{
893 if (mydata->fatsize == 32)
894 dentptr->starthi =
895 cpu_to_le16((start_cluster & 0xffff0000) >> 16);
896 dentptr->start = cpu_to_le16(start_cluster & 0xffff);
897}
898
899/*
900 * Check whether adding a file makes the file system to
901 * exceed the size of the block device
902 * Return -1 when overflow occurs, otherwise return 0
903 */
904static int check_overflow(fsdata *mydata, __u32 clustnum, loff_t size)
905{
906 __u32 startsect, sect_num, offset;
907
908 if (clustnum > 0)
909 startsect = clust_to_sect(mydata, clustnum);
910 else
911 startsect = mydata->rootdir_sect;
912
913 sect_num = div_u64_rem(size, mydata->sect_size, &offset);
914
915 if (offset != 0)
916 sect_num++;
917
918 if (startsect + sect_num > total_sector)
919 return -1;
920 return 0;
921}
922
923/*
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000924 * Write at most 'maxsize' bytes from 'buffer' into
925 * the file associated with 'dentptr'
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800926 * Update the number of bytes written in *gotsize and return 0
927 * or return -1 on fatal errors.
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000928 */
929static int
AKASHI Takahiro704df6a2018-09-11 15:59:05 +0900930set_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, __u8 *buffer,
931 loff_t maxsize, loff_t *gotsize)
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000932{
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000933 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
934 __u32 curclust = START(dentptr);
935 __u32 endclust = 0, newclust = 0;
Heinrich Schuchardt7274b762019-02-25 19:42:48 +0100936 u64 cur_pos, filesize;
937 loff_t offset, actsize, wsize;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000938
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800939 *gotsize = 0;
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +0900940 filesize = pos + maxsize;
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000941
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800942 debug("%llu bytes\n", filesize);
Donggeun Kimc30a15e2011-10-24 21:15:28 +0000943
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +0900944 if (!filesize) {
945 if (!curclust)
946 return 0;
947 if (!CHECK_CLUST(curclust, mydata->fatsize) ||
948 IS_LAST_CLUST(curclust, mydata->fatsize)) {
949 clear_fatent(mydata, curclust);
950 set_start_cluster(mydata, dentptr, 0);
951 return 0;
952 }
953 debug("curclust: 0x%x\n", curclust);
954 debug("Invalid FAT entry\n");
955 return -1;
956 }
957
958 if (!curclust) {
959 assert(pos == 0);
960 goto set_clusters;
961 }
962
963 /* go to cluster at pos */
964 cur_pos = bytesperclust;
965 while (1) {
966 if (pos <= cur_pos)
967 break;
968 if (IS_LAST_CLUST(curclust, mydata->fatsize))
969 break;
970
971 newclust = get_fatent(mydata, curclust);
972 if (!IS_LAST_CLUST(newclust, mydata->fatsize) &&
973 CHECK_CLUST(newclust, mydata->fatsize)) {
974 debug("curclust: 0x%x\n", curclust);
975 debug("Invalid FAT entry\n");
976 return -1;
977 }
978
979 cur_pos += bytesperclust;
980 curclust = newclust;
981 }
982 if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
983 assert(pos == cur_pos);
984 goto set_clusters;
985 }
986
987 assert(pos < cur_pos);
988 cur_pos -= bytesperclust;
989
990 /* overwrite */
991 assert(IS_LAST_CLUST(curclust, mydata->fatsize) ||
992 !CHECK_CLUST(curclust, mydata->fatsize));
993
994 while (1) {
995 /* search for allocated consecutive clusters */
996 actsize = bytesperclust;
997 endclust = curclust;
998 while (1) {
999 if (filesize <= (cur_pos + actsize))
1000 break;
1001
1002 newclust = get_fatent(mydata, endclust);
1003
Marek Szyprowski5e615b72019-12-02 12:11:13 +01001004 if (newclust != endclust + 1)
1005 break;
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +09001006 if (IS_LAST_CLUST(newclust, mydata->fatsize))
1007 break;
1008 if (CHECK_CLUST(newclust, mydata->fatsize)) {
1009 debug("curclust: 0x%x\n", curclust);
1010 debug("Invalid FAT entry\n");
1011 return -1;
1012 }
1013
1014 actsize += bytesperclust;
1015 endclust = newclust;
1016 }
1017
1018 /* overwrite to <curclust..endclust> */
1019 if (pos < cur_pos)
1020 offset = 0;
1021 else
1022 offset = pos - cur_pos;
Marek Szyprowskia54ece42019-12-02 12:11:14 +01001023 wsize = min_t(unsigned long long, actsize, filesize - cur_pos);
1024 wsize -= offset;
1025
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +09001026 if (get_set_cluster(mydata, curclust, offset,
1027 buffer, wsize, &actsize)) {
1028 printf("Error get-and-setting cluster\n");
1029 return -1;
1030 }
1031 buffer += wsize;
1032 *gotsize += wsize;
1033 cur_pos += offset + wsize;
1034
1035 if (filesize <= cur_pos)
1036 break;
1037
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +09001038 if (IS_LAST_CLUST(newclust, mydata->fatsize))
1039 /* no more clusters */
1040 break;
1041
1042 curclust = newclust;
1043 }
1044
1045 if (filesize <= cur_pos) {
1046 /* no more write */
1047 newclust = get_fatent(mydata, endclust);
1048 if (!IS_LAST_CLUST(newclust, mydata->fatsize)) {
1049 /* truncate the rest */
1050 clear_fatent(mydata, newclust);
1051
1052 /* Mark end of file in FAT */
1053 if (mydata->fatsize == 12)
1054 newclust = 0xfff;
1055 else if (mydata->fatsize == 16)
1056 newclust = 0xffff;
1057 else if (mydata->fatsize == 32)
1058 newclust = 0xfffffff;
1059 set_fatent_value(mydata, endclust, newclust);
1060 }
1061
1062 return 0;
1063 }
1064
1065 curclust = endclust;
1066 filesize -= cur_pos;
Heinrich Schuchardt7274b762019-02-25 19:42:48 +01001067 assert(!do_div(cur_pos, bytesperclust));
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +09001068
1069set_clusters:
1070 /* allocate and write */
1071 assert(!pos);
1072
1073 /* Assure that curclust is valid */
1074 if (!curclust) {
1075 curclust = find_empty_cluster(mydata);
1076 set_start_cluster(mydata, dentptr, curclust);
1077 } else {
1078 newclust = get_fatent(mydata, curclust);
1079
1080 if (IS_LAST_CLUST(newclust, mydata->fatsize)) {
1081 newclust = determine_fatent(mydata, curclust);
1082 set_fatent_value(mydata, curclust, newclust);
1083 curclust = newclust;
1084 } else {
1085 debug("error: something wrong\n");
Benoît Thébaudeau1254b442015-09-28 15:45:32 +02001086 return -1;
1087 }
AKASHI Takahiro704df6a2018-09-11 15:59:05 +09001088 }
1089
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +09001090 /* TODO: already partially written */
AKASHI Takahiro704df6a2018-09-11 15:59:05 +09001091 if (check_overflow(mydata, curclust, filesize)) {
1092 printf("Error: no space left: %llu\n", filesize);
1093 return -1;
Benoît Thébaudeau1254b442015-09-28 15:45:32 +02001094 }
1095
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001096 actsize = bytesperclust;
1097 endclust = curclust;
1098 do {
1099 /* search for consecutive clusters */
1100 while (actsize < filesize) {
1101 newclust = determine_fatent(mydata, endclust);
1102
1103 if ((newclust - 1) != endclust)
AKASHI Takahiro704df6a2018-09-11 15:59:05 +09001104 /* write to <curclust..endclust> */
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001105 goto getit;
1106
1107 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Benoît Thébaudeau5e1a8602015-09-28 15:45:30 +02001108 debug("newclust: 0x%x\n", newclust);
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001109 debug("Invalid FAT entry\n");
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001110 return 0;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001111 }
1112 endclust = newclust;
1113 actsize += bytesperclust;
1114 }
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001115
1116 /* set remaining bytes */
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001117 actsize = filesize;
Heinrich Schuchardtf105fe72018-10-02 09:30:45 +02001118 if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001119 debug("error: writing cluster\n");
1120 return -1;
1121 }
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001122 *gotsize += actsize;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001123
1124 /* Mark end of file in FAT */
Philipp Skadorov49abbd92016-12-15 15:52:53 -05001125 if (mydata->fatsize == 12)
1126 newclust = 0xfff;
1127 else if (mydata->fatsize == 16)
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001128 newclust = 0xffff;
1129 else if (mydata->fatsize == 32)
1130 newclust = 0xfffffff;
1131 set_fatent_value(mydata, endclust, newclust);
1132
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001133 return 0;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001134getit:
Heinrich Schuchardtf105fe72018-10-02 09:30:45 +02001135 if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) {
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001136 debug("error: writing cluster\n");
1137 return -1;
1138 }
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001139 *gotsize += actsize;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001140 filesize -= actsize;
1141 buffer += actsize;
1142
Benoît Thébaudeau5e1a8602015-09-28 15:45:30 +02001143 if (CHECK_CLUST(newclust, mydata->fatsize)) {
1144 debug("newclust: 0x%x\n", newclust);
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001145 debug("Invalid FAT entry\n");
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001146 return 0;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001147 }
1148 actsize = bytesperclust;
1149 curclust = endclust = newclust;
1150 } while (1);
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001151
AKASHI Takahiro704df6a2018-09-11 15:59:05 +09001152 return 0;
Benoît Thébaudeau1254b442015-09-28 15:45:32 +02001153}
1154
Heinrich Schuchardt57b745e2020-11-22 19:19:39 +01001155/**
Heinrich Schuchardtba23c372024-04-09 22:06:07 +02001156 * dentry_set_time() - set change time
1157 *
1158 * @dentptr: directory entry
1159 */
1160static void dentry_set_time(dir_entry *dentptr)
1161{
1162 if (CONFIG_IS_ENABLED(DM_RTC)) {
1163 struct udevice *dev;
1164 struct rtc_time tm;
1165 u16 date;
1166 u16 time;
1167
1168 uclass_first_device(UCLASS_RTC, &dev);
1169 if (!dev)
1170 goto err;
1171 if (dm_rtc_get(dev, &tm))
1172 goto err;
1173 if (tm.tm_year < 1980 || tm.tm_year > 2107)
1174 goto err;
1175 date = (tm.tm_mday & 0x1f) |
1176 ((tm.tm_mon & 0xf) << 5) |
1177 ((tm.tm_year - 1980) << 9);
1178 time = (tm.tm_sec > 1) |
1179 ((tm.tm_min & 0x3f) << 5) |
1180 (tm.tm_hour << 11);
1181 dentptr->date = date;
1182 dentptr->time = time;
1183 return;
1184 }
1185err:
1186 dentptr->date = 0x2821; /* 2000-01-01 */
1187 dentptr->time = 0;
1188}
1189
1190/**
Heinrich Schuchardt57b745e2020-11-22 19:19:39 +01001191 * fill_dentry() - fill directory entry with shortname
1192 *
1193 * @mydata: private filesystem parameters
1194 * @dentptr: directory entry
1195 * @shortname: chosen short name
1196 * @start_cluster: first cluster of file
1197 * @size: file size
1198 * @attr: file attributes
Benoît Thébaudeau1254b442015-09-28 15:45:32 +02001199 */
1200static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
Heinrich Schuchardt57b745e2020-11-22 19:19:39 +01001201 const char *shortname, __u32 start_cluster, __u32 size, __u8 attr)
Benoît Thébaudeau1254b442015-09-28 15:45:32 +02001202{
Heinrich Schuchardt57b745e2020-11-22 19:19:39 +01001203 memset(dentptr, 0, sizeof(*dentptr));
1204
Benoît Thébaudeau1254b442015-09-28 15:45:32 +02001205 set_start_cluster(mydata, dentptr, start_cluster);
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001206 dentptr->size = cpu_to_le32(size);
1207
1208 dentptr->attr = attr;
1209
Heinrich Schuchardtba23c372024-04-09 22:06:07 +02001210 /* Set change date */
1211 dentry_set_time(dentptr);
1212 /* Set creation date */
1213 dentptr->cdate = dentptr->date;
1214 dentptr->ctime = dentptr->time;
1215
Heinrich Schuchardt041f0af2021-01-21 00:23:33 +01001216 memcpy(&dentptr->nameext, shortname, SHORT_NAME_SIZE);
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001217}
1218
Heinrich Schuchardt1e51c8d2020-11-26 16:10:01 +01001219/**
1220 * find_directory_entry() - find a directory entry by filename
1221 *
1222 * @itr: directory iterator
1223 * @filename: name of file to find
1224 * Return: directory entry or NULL
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001225 */
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001226static dir_entry *find_directory_entry(fat_itr *itr, char *filename)
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001227{
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001228 int match = 0;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001229
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001230 while (fat_itr_next(itr)) {
1231 /* check both long and short name: */
1232 if (!strcasecmp(filename, itr->name))
1233 match = 1;
1234 else if (itr->name != itr->s_name &&
1235 !strcasecmp(filename, itr->s_name))
1236 match = 1;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001237
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001238 if (!match)
1239 continue;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001240
Heinrich Schuchardt041f0af2021-01-21 00:23:33 +01001241 if (itr->dent->nameext.name[0] == '\0')
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001242 return NULL;
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001243 else
1244 return itr->dent;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001245 }
1246
1247 return NULL;
1248}
1249
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001250static int split_filename(char *filename, char **dirname, char **basename)
1251{
1252 char *p, *last_slash, *last_slash_cont;
1253
1254again:
1255 p = filename;
1256 last_slash = NULL;
1257 last_slash_cont = NULL;
1258 while (*p) {
1259 if (ISDIRDELIM(*p)) {
1260 last_slash = p;
1261 last_slash_cont = p;
1262 /* continuous slashes */
1263 while (ISDIRDELIM(*p))
1264 last_slash_cont = p++;
1265 if (!*p)
1266 break;
1267 }
1268 p++;
1269 }
1270
1271 if (last_slash) {
1272 if (last_slash_cont == (filename + strlen(filename) - 1)) {
1273 /* remove trailing slashes */
1274 *last_slash = '\0';
1275 goto again;
1276 }
1277
1278 if (last_slash == filename) {
1279 /* avoid ""(null) directory */
1280 *dirname = "/";
1281 } else {
1282 *last_slash = '\0';
1283 *dirname = filename;
1284 }
1285
1286 *last_slash_cont = '\0';
Heinrich Schuchardt3ecc5272021-01-30 14:12:10 +01001287 filename = last_slash_cont + 1;
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001288 } else {
1289 *dirname = "/"; /* root by default */
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001290 }
1291
Heinrich Schuchardt3ecc5272021-01-30 14:12:10 +01001292 /*
1293 * The FAT32 File System Specification v1.03 requires leading and
1294 * trailing spaces as well as trailing periods to be ignored.
1295 */
1296 for (; *filename == ' '; ++filename)
1297 ;
1298
1299 /* Keep special entries '.' and '..' */
1300 if (filename[0] == '.' &&
1301 (!filename[1] || (filename[1] == '.' && !filename[2])))
1302 goto done;
1303
1304 /* Remove trailing periods and spaces */
1305 for (p = filename + strlen(filename) - 1; p >= filename; --p) {
1306 switch (*p) {
1307 case ' ':
1308 case '.':
1309 *p = 0;
1310 break;
1311 default:
1312 goto done;
1313 }
1314 }
1315
1316done:
1317 *basename = filename;
1318
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001319 return 0;
1320}
1321
Heinrich Schuchardt7b437802019-05-12 09:59:18 +02001322/**
1323 * normalize_longname() - check long file name and convert to lower case
1324 *
1325 * We assume here that the FAT file system is using an 8bit code page.
1326 * Linux typically uses CP437, EDK2 assumes CP1250.
1327 *
1328 * @l_filename: preallocated buffer receiving the normalized name
1329 * @filename: filename to normalize
1330 * Return: 0 on success, -1 on failure
1331 */
AKASHI Takahiro25bb9da2018-09-11 15:59:02 +09001332static int normalize_longname(char *l_filename, const char *filename)
1333{
Heinrich Schuchardt7b437802019-05-12 09:59:18 +02001334 const char *p, illegal[] = "<>:\"/\\|?*";
Heinrich Schuchardt0be286c2021-01-30 11:08:21 +01001335 size_t len;
AKASHI Takahiro25bb9da2018-09-11 15:59:02 +09001336
Heinrich Schuchardt0be286c2021-01-30 11:08:21 +01001337 len = strlen(filename);
1338 if (!len || len >= VFAT_MAXLEN_BYTES || filename[len - 1] == '.')
AKASHI Takahiro25bb9da2018-09-11 15:59:02 +09001339 return -1;
Heinrich Schuchardt7b437802019-05-12 09:59:18 +02001340
1341 for (p = filename; *p; ++p) {
1342 if ((unsigned char)*p < 0x20)
1343 return -1;
1344 if (strchr(illegal, *p))
1345 return -1;
AKASHI Takahiro25bb9da2018-09-11 15:59:02 +09001346 }
1347
Heinrich Schuchardt7b437802019-05-12 09:59:18 +02001348 strcpy(l_filename, filename);
1349 downcase(l_filename, VFAT_MAXLEN_BYTES);
AKASHI Takahiro25bb9da2018-09-11 15:59:02 +09001350
1351 return 0;
1352}
1353
AKASHI Takahiro704df6a2018-09-11 15:59:05 +09001354int file_fat_write_at(const char *filename, loff_t pos, void *buffer,
1355 loff_t size, loff_t *actwrite)
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001356{
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001357 dir_entry *retdent;
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001358 fsdata datablock = { .fatbuf = NULL, };
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001359 fsdata *mydata = &datablock;
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001360 fat_itr *itr = NULL;
AKASHI Takahiro25bb9da2018-09-11 15:59:02 +09001361 int ret = -1;
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001362 char *filename_copy, *parent, *basename;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001363 char l_filename[VFAT_MAXLEN_BYTES];
1364
AKASHI Takahiro704df6a2018-09-11 15:59:05 +09001365 debug("writing %s\n", filename);
1366
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001367 filename_copy = strdup(filename);
1368 if (!filename_copy)
AKASHI Takahirof1149ce2018-09-11 15:59:03 +09001369 return -ENOMEM;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001370
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001371 split_filename(filename_copy, &parent, &basename);
1372 if (!strlen(basename)) {
1373 ret = -EINVAL;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001374 goto exit;
1375 }
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001376
Heinrich Schuchardt4c4006b2021-01-30 10:01:08 +01001377 if (normalize_longname(l_filename, basename)) {
1378 printf("FAT: illegal filename (%s)\n", basename);
AKASHI Takahiro25bb9da2018-09-11 15:59:02 +09001379 ret = -EINVAL;
1380 goto exit;
1381 }
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001382
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001383 itr = malloc_cache_aligned(sizeof(fat_itr));
1384 if (!itr) {
1385 ret = -ENOMEM;
1386 goto exit;
1387 }
1388
1389 ret = fat_itr_root(itr, &datablock);
1390 if (ret)
1391 goto exit;
1392
1393 total_sector = datablock.total_sect;
1394
1395 ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1396 if (ret) {
1397 printf("%s: doesn't exist (%d)\n", parent, ret);
1398 goto exit;
1399 }
1400
1401 retdent = find_directory_entry(itr, l_filename);
1402
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001403 if (retdent) {
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001404 if (fat_itr_isdir(itr)) {
1405 ret = -EISDIR;
1406 goto exit;
1407 }
1408
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +09001409 /* A file exists */
1410 if (pos == -1)
1411 /* Append to the end */
1412 pos = FAT2CPU32(retdent->size);
1413 if (pos > retdent->size) {
1414 /* No hole allowed */
1415 ret = -EINVAL;
1416 goto exit;
1417 }
1418
AKASHI Takahiro704df6a2018-09-11 15:59:05 +09001419 /* Update file size in a directory entry */
1420 retdent->size = cpu_to_le32(pos + size);
Heinrich Schuchardtba23c372024-04-09 22:06:07 +02001421 /* Update change date */
1422 dentry_set_time(retdent);
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001423 } else {
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001424 /* Create a new file */
Heinrich Schuchardt57b745e2020-11-22 19:19:39 +01001425 char shortname[SHORT_NAME_SIZE];
Heinrich Schuchardt3049a512020-11-22 11:54:22 +01001426 int ndent;
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001427
AKASHI Takahirocb8af8a2018-09-11 15:59:06 +09001428 if (pos) {
1429 /* No hole allowed */
1430 ret = -EINVAL;
1431 goto exit;
1432 }
1433
Heinrich Schuchardt28cef9c2020-11-20 12:55:22 +01001434 /* Check if long name is needed */
Heinrich Schuchardt4c4006b2021-01-30 10:01:08 +01001435 ndent = set_name(itr, basename, shortname);
Heinrich Schuchardt3049a512020-11-22 11:54:22 +01001436 if (ndent < 0) {
1437 ret = ndent;
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001438 goto exit;
Heinrich Schuchardt3049a512020-11-22 11:54:22 +01001439 }
1440 ret = fat_find_empty_dentries(itr, ndent);
1441 if (ret)
1442 goto exit;
1443 if (ndent > 1) {
Heinrich Schuchardt28cef9c2020-11-20 12:55:22 +01001444 /* Set long name entries */
Heinrich Schuchardt4c4006b2021-01-30 10:01:08 +01001445 ret = fill_dir_slot(itr, basename, shortname);
Heinrich Schuchardt28cef9c2020-11-20 12:55:22 +01001446 if (ret)
1447 goto exit;
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001448 }
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001449
AKASHI Takahiro9c709c72019-05-24 14:10:36 +09001450 /* Set short name entry */
Heinrich Schuchardt57b745e2020-11-22 19:19:39 +01001451 fill_dentry(itr->fsdata, itr->dent, shortname, 0, size,
Heinrich Schuchardt1ec29aa2020-11-22 11:13:33 +01001452 ATTR_ARCH);
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001453
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001454 retdent = itr->dent;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001455 }
1456
AKASHI Takahiro704df6a2018-09-11 15:59:05 +09001457 ret = set_contents(mydata, retdent, pos, buffer, size, actwrite);
Benoît Thébaudeaue876be42015-09-28 15:45:31 +02001458 if (ret < 0) {
1459 printf("Error: writing contents\n");
AKASHI Takahirof1149ce2018-09-11 15:59:03 +09001460 ret = -EIO;
Benoît Thébaudeaue876be42015-09-28 15:45:31 +02001461 goto exit;
1462 }
1463 debug("attempt to write 0x%llx bytes\n", *actwrite);
1464
1465 /* Flush fat buffer */
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +02001466 ret = flush_dirty_fat_buffer(mydata);
Benoît Thébaudeaue876be42015-09-28 15:45:31 +02001467 if (ret) {
1468 printf("Error: flush fat buffer\n");
AKASHI Takahirof1149ce2018-09-11 15:59:03 +09001469 ret = -EIO;
Benoît Thébaudeaue876be42015-09-28 15:45:31 +02001470 goto exit;
1471 }
1472
1473 /* Write directory table to device */
AKASHI Takahiroa9f67062019-05-24 14:10:35 +09001474 ret = flush_dir(itr);
Benoît Thébaudeaue876be42015-09-28 15:45:31 +02001475
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001476exit:
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001477 free(filename_copy);
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001478 free(mydata->fatbuf);
AKASHI Takahiro4ced2032018-09-11 15:59:04 +09001479 free(itr);
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001480 return ret;
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001481}
1482
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001483int file_fat_write(const char *filename, void *buffer, loff_t offset,
1484 loff_t maxsize, loff_t *actwrite)
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001485{
AKASHI Takahiro704df6a2018-09-11 15:59:05 +09001486 return file_fat_write_at(filename, offset, buffer, maxsize, actwrite);
Donggeun Kimc30a15e2011-10-24 21:15:28 +00001487}
AKASHI Takahiro31a18d52018-09-11 15:59:10 +09001488
AKASHI Takahirof8240ce2018-09-11 15:59:14 +09001489static int fat_dir_entries(fat_itr *itr)
1490{
1491 fat_itr *dirs;
1492 fsdata fsdata = { .fatbuf = NULL, }, *mydata = &fsdata;
1493 /* for FATBUFSIZE */
1494 int count;
1495
1496 dirs = malloc_cache_aligned(sizeof(fat_itr));
1497 if (!dirs) {
1498 debug("Error: allocating memory\n");
1499 count = -ENOMEM;
1500 goto exit;
1501 }
1502
1503 /* duplicate fsdata */
1504 fat_itr_child(dirs, itr);
1505 fsdata = *dirs->fsdata;
1506
1507 /* allocate local fat buffer */
1508 fsdata.fatbuf = malloc_cache_aligned(FATBUFSIZE);
1509 if (!fsdata.fatbuf) {
1510 debug("Error: allocating memory\n");
1511 count = -ENOMEM;
1512 goto exit;
1513 }
1514 fsdata.fatbufnum = -1;
1515 dirs->fsdata = &fsdata;
1516
1517 for (count = 0; fat_itr_next(dirs); count++)
1518 ;
1519
1520exit:
1521 free(fsdata.fatbuf);
1522 free(dirs);
1523 return count;
1524}
1525
Heinrich Schuchardt3d20d212020-11-19 07:31:18 +01001526/**
1527 * delete_single_dentry() - delete a single directory entry
1528 *
1529 * @itr: directory iterator
1530 * Return: 0 for success
1531 */
1532static int delete_single_dentry(fat_itr *itr)
1533{
1534 struct dir_entry *dent = itr->dent;
1535
1536 memset(dent, 0, sizeof(*dent));
Heinrich Schuchardt041f0af2021-01-21 00:23:33 +01001537 dent->nameext.name[0] = DELETED_FLAG;
Heinrich Schuchardt3d20d212020-11-19 07:31:18 +01001538
Heinrich Schuchardte97eb632021-01-20 22:21:53 +01001539 if (!itr->remaining)
1540 return flush_dir(itr);
Heinrich Schuchardt3d20d212020-11-19 07:31:18 +01001541 return 0;
1542}
1543
1544/**
1545 * delete_long_name() - delete long name directory entries
1546 *
1547 * @itr: directory iterator
1548 * Return: 0 for success
1549 */
1550static int delete_long_name(fat_itr *itr)
1551{
Heinrich Schuchardt041f0af2021-01-21 00:23:33 +01001552 int seqn = itr->dent->nameext.name[0] & ~LAST_LONG_ENTRY_MASK;
Heinrich Schuchardt3d20d212020-11-19 07:31:18 +01001553
1554 while (seqn--) {
Heinrich Schuchardt84ca3052021-01-26 00:14:14 +01001555 struct dir_entry *dent;
Heinrich Schuchardt3d20d212020-11-19 07:31:18 +01001556 int ret;
1557
1558 ret = delete_single_dentry(itr);
1559 if (ret)
1560 return ret;
1561 dent = next_dent(itr);
1562 if (!dent)
1563 return -EIO;
1564 }
1565 return 0;
1566}
1567
1568/**
1569 * delete_dentry_long() - remove directory entry
1570 *
1571 * @itr: directory iterator
1572 * Return: 0 for success
1573 */
1574static int delete_dentry_long(fat_itr *itr)
AKASHI Takahirof8240ce2018-09-11 15:59:14 +09001575{
1576 fsdata *mydata = itr->fsdata;
Heinrich Schuchardt3d20d212020-11-19 07:31:18 +01001577 dir_entry *dent = itr->dent;
AKASHI Takahirof8240ce2018-09-11 15:59:14 +09001578
1579 /* free cluster blocks */
Heinrich Schuchardt3d20d212020-11-19 07:31:18 +01001580 clear_fatent(mydata, START(dent));
AKASHI Takahirof8240ce2018-09-11 15:59:14 +09001581 if (flush_dirty_fat_buffer(mydata) < 0) {
1582 printf("Error: flush fat buffer\n");
1583 return -EIO;
1584 }
Heinrich Schuchardt3d20d212020-11-19 07:31:18 +01001585 /* Position to first directory entry for long name */
1586 if (itr->clust != itr->dent_clust) {
1587 int ret;
AKASHI Takahirof8240ce2018-09-11 15:59:14 +09001588
Heinrich Schuchardt3d20d212020-11-19 07:31:18 +01001589 ret = fat_move_to_cluster(itr, itr->dent_clust);
1590 if (ret)
1591 return ret;
1592 }
1593 itr->dent = itr->dent_start;
1594 itr->remaining = itr->dent_rem;
1595 dent = itr->dent_start;
1596 /* Delete long name */
1597 if ((dent->attr & ATTR_VFAT) == ATTR_VFAT &&
Heinrich Schuchardt041f0af2021-01-21 00:23:33 +01001598 (dent->nameext.name[0] & LAST_LONG_ENTRY_MASK)) {
Heinrich Schuchardt3d20d212020-11-19 07:31:18 +01001599 int ret;
AKASHI Takahirof8240ce2018-09-11 15:59:14 +09001600
Heinrich Schuchardt3d20d212020-11-19 07:31:18 +01001601 ret = delete_long_name(itr);
1602 if (ret)
1603 return ret;
1604 }
1605 /* Delete short name */
1606 delete_single_dentry(itr);
Heinrich Schuchardte97eb632021-01-20 22:21:53 +01001607 return flush_dir(itr);
AKASHI Takahirof8240ce2018-09-11 15:59:14 +09001608}
1609
1610int fat_unlink(const char *filename)
1611{
1612 fsdata fsdata = { .fatbuf = NULL, };
1613 fat_itr *itr = NULL;
1614 int n_entries, ret;
1615 char *filename_copy, *dirname, *basename;
1616
1617 filename_copy = strdup(filename);
Simon Glass78211de2023-07-15 21:39:06 -06001618 itr = malloc_cache_aligned(sizeof(fat_itr));
1619 if (!itr || !filename_copy) {
1620 printf("Error: out of memory\n");
Heinrich Schuchardt0d532e92018-10-02 06:58:00 +02001621 ret = -ENOMEM;
1622 goto exit;
1623 }
AKASHI Takahirof8240ce2018-09-11 15:59:14 +09001624 split_filename(filename_copy, &dirname, &basename);
1625
1626 if (!strcmp(dirname, "/") && !strcmp(basename, "")) {
1627 printf("Error: cannot remove root\n");
1628 ret = -EINVAL;
1629 goto exit;
1630 }
1631
AKASHI Takahirof8240ce2018-09-11 15:59:14 +09001632 ret = fat_itr_root(itr, &fsdata);
1633 if (ret)
1634 goto exit;
1635
1636 total_sector = fsdata.total_sect;
1637
1638 ret = fat_itr_resolve(itr, dirname, TYPE_DIR);
1639 if (ret) {
1640 printf("%s: doesn't exist (%d)\n", dirname, ret);
1641 ret = -ENOENT;
1642 goto exit;
1643 }
1644
1645 if (!find_directory_entry(itr, basename)) {
Simon Glass78211de2023-07-15 21:39:06 -06001646 log_err("%s: doesn't exist (%d)\n", basename, -ENOENT);
AKASHI Takahirof8240ce2018-09-11 15:59:14 +09001647 ret = -ENOENT;
1648 goto exit;
1649 }
1650
1651 if (fat_itr_isdir(itr)) {
1652 n_entries = fat_dir_entries(itr);
1653 if (n_entries < 0) {
1654 ret = n_entries;
1655 goto exit;
1656 }
1657 if (n_entries > 2) {
1658 printf("Error: directory is not empty: %d\n",
1659 n_entries);
1660 ret = -EINVAL;
1661 goto exit;
1662 }
1663 }
1664
Heinrich Schuchardt3d20d212020-11-19 07:31:18 +01001665 ret = delete_dentry_long(itr);
AKASHI Takahirof8240ce2018-09-11 15:59:14 +09001666
1667exit:
1668 free(fsdata.fatbuf);
1669 free(itr);
1670 free(filename_copy);
1671
1672 return ret;
1673}
1674
Heinrich Schuchardt4c4006b2021-01-30 10:01:08 +01001675int fat_mkdir(const char *dirname)
AKASHI Takahiro31a18d52018-09-11 15:59:10 +09001676{
1677 dir_entry *retdent;
1678 fsdata datablock = { .fatbuf = NULL, };
1679 fsdata *mydata = &datablock;
1680 fat_itr *itr = NULL;
Heinrich Schuchardt4c4006b2021-01-30 10:01:08 +01001681 char *dirname_copy, *parent, *basename;
AKASHI Takahiro31a18d52018-09-11 15:59:10 +09001682 char l_dirname[VFAT_MAXLEN_BYTES];
1683 int ret = -1;
1684 loff_t actwrite;
1685 unsigned int bytesperclust;
1686 dir_entry *dotdent = NULL;
1687
Heinrich Schuchardt4c4006b2021-01-30 10:01:08 +01001688 dirname_copy = strdup(dirname);
AKASHI Takahiro31a18d52018-09-11 15:59:10 +09001689 if (!dirname_copy)
1690 goto exit;
1691
Heinrich Schuchardt4c4006b2021-01-30 10:01:08 +01001692 split_filename(dirname_copy, &parent, &basename);
1693 if (!strlen(basename)) {
AKASHI Takahiro31a18d52018-09-11 15:59:10 +09001694 ret = -EINVAL;
1695 goto exit;
1696 }
1697
Heinrich Schuchardt4c4006b2021-01-30 10:01:08 +01001698 if (normalize_longname(l_dirname, basename)) {
1699 printf("FAT: illegal filename (%s)\n", basename);
AKASHI Takahiro31a18d52018-09-11 15:59:10 +09001700 ret = -EINVAL;
1701 goto exit;
1702 }
1703
1704 itr = malloc_cache_aligned(sizeof(fat_itr));
1705 if (!itr) {
1706 ret = -ENOMEM;
1707 goto exit;
1708 }
1709
1710 ret = fat_itr_root(itr, &datablock);
1711 if (ret)
1712 goto exit;
1713
1714 total_sector = datablock.total_sect;
1715
1716 ret = fat_itr_resolve(itr, parent, TYPE_DIR);
1717 if (ret) {
1718 printf("%s: doesn't exist (%d)\n", parent, ret);
1719 goto exit;
1720 }
1721
1722 retdent = find_directory_entry(itr, l_dirname);
1723
1724 if (retdent) {
1725 printf("%s: already exists\n", l_dirname);
1726 ret = -EEXIST;
1727 goto exit;
1728 } else {
Heinrich Schuchardt57b745e2020-11-22 19:19:39 +01001729 char shortname[SHORT_NAME_SIZE];
Heinrich Schuchardt3049a512020-11-22 11:54:22 +01001730 int ndent;
Heinrich Schuchardt57b745e2020-11-22 19:19:39 +01001731
AKASHI Takahiro31a18d52018-09-11 15:59:10 +09001732 if (itr->is_root) {
1733 /* root dir cannot have "." or ".." */
1734 if (!strcmp(l_dirname, ".") ||
1735 !strcmp(l_dirname, "..")) {
1736 ret = -EINVAL;
1737 goto exit;
1738 }
1739 }
1740
Heinrich Schuchardt28cef9c2020-11-20 12:55:22 +01001741 /* Check if long name is needed */
Heinrich Schuchardt4c4006b2021-01-30 10:01:08 +01001742 ndent = set_name(itr, basename, shortname);
Heinrich Schuchardt3049a512020-11-22 11:54:22 +01001743 if (ndent < 0) {
1744 ret = ndent;
Heinrich Schuchardt28cef9c2020-11-20 12:55:22 +01001745 goto exit;
Heinrich Schuchardt3049a512020-11-22 11:54:22 +01001746 }
1747 ret = fat_find_empty_dentries(itr, ndent);
1748 if (ret)
1749 goto exit;
1750 if (ndent > 1) {
Heinrich Schuchardt28cef9c2020-11-20 12:55:22 +01001751 /* Set long name entries */
Heinrich Schuchardt4c4006b2021-01-30 10:01:08 +01001752 ret = fill_dir_slot(itr, basename, shortname);
Heinrich Schuchardt28cef9c2020-11-20 12:55:22 +01001753 if (ret)
1754 goto exit;
1755 }
AKASHI Takahiro31a18d52018-09-11 15:59:10 +09001756
1757 /* Set attribute as archive for regular file */
Heinrich Schuchardt57b745e2020-11-22 19:19:39 +01001758 fill_dentry(itr->fsdata, itr->dent, shortname, 0, 0,
AKASHI Takahiro31a18d52018-09-11 15:59:10 +09001759 ATTR_DIR | ATTR_ARCH);
1760
1761 retdent = itr->dent;
1762 }
1763
1764 /* Default entries */
1765 bytesperclust = mydata->clust_size * mydata->sect_size;
1766 dotdent = malloc_cache_aligned(bytesperclust);
1767 if (!dotdent) {
1768 ret = -ENOMEM;
1769 goto exit;
1770 }
1771 memset(dotdent, 0, bytesperclust);
1772
Heinrich Schuchardt041f0af2021-01-21 00:23:33 +01001773 memcpy(&dotdent[0].nameext, ". ", 11);
AKASHI Takahiro31a18d52018-09-11 15:59:10 +09001774 dotdent[0].attr = ATTR_DIR | ATTR_ARCH;
1775
Heinrich Schuchardt041f0af2021-01-21 00:23:33 +01001776 memcpy(&dotdent[1].nameext, ".. ", 11);
AKASHI Takahiro31a18d52018-09-11 15:59:10 +09001777 dotdent[1].attr = ATTR_DIR | ATTR_ARCH;
Heinrich Schuchardtc5924112020-11-24 21:04:07 +01001778
1779 if (itr->is_root)
1780 set_start_cluster(mydata, &dotdent[1], 0);
1781 else
1782 set_start_cluster(mydata, &dotdent[1], itr->start_clust);
AKASHI Takahiro31a18d52018-09-11 15:59:10 +09001783
1784 ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
1785 bytesperclust, &actwrite);
1786 if (ret < 0) {
1787 printf("Error: writing contents\n");
1788 goto exit;
1789 }
1790 /* Write twice for "." */
1791 set_start_cluster(mydata, &dotdent[0], START(retdent));
1792 ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent,
1793 bytesperclust, &actwrite);
1794 if (ret < 0) {
1795 printf("Error: writing contents\n");
1796 goto exit;
1797 }
1798
1799 /* Flush fat buffer */
1800 ret = flush_dirty_fat_buffer(mydata);
1801 if (ret) {
1802 printf("Error: flush fat buffer\n");
Heinrich Schuchardte97eb632021-01-20 22:21:53 +01001803 ret = -EIO;
AKASHI Takahiro31a18d52018-09-11 15:59:10 +09001804 goto exit;
1805 }
1806
1807 /* Write directory table to device */
AKASHI Takahiroa9f67062019-05-24 14:10:35 +09001808 ret = flush_dir(itr);
AKASHI Takahiro31a18d52018-09-11 15:59:10 +09001809
1810exit:
1811 free(dirname_copy);
1812 free(mydata->fatbuf);
1813 free(itr);
1814 free(dotdent);
1815 return ret;
1816}