Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 2 | /* |
| 3 | * fat_write.c |
| 4 | * |
| 5 | * R/W (V)FAT 12/16/32 filesystem implementation by Donggeun Kim |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Simon Glass | 468247d | 2023-01-28 15:00:16 -0700 | [diff] [blame] | 8 | #define LOG_CATEGORY LOGC_FS |
| 9 | |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 10 | #include <common.h> |
| 11 | #include <command.h> |
| 12 | #include <config.h> |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 13 | #include <div64.h> |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 14 | #include <fat.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 15 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 16 | #include <malloc.h> |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 17 | #include <part.h> |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 18 | #include <rand.h> |
| 19 | #include <asm/byteorder.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 20 | #include <asm/cache.h> |
Heinrich Schuchardt | cab43b0 | 2024-04-09 22:06:07 +0200 | [diff] [blame] | 21 | #include <dm/uclass.h> |
Richard Genoud | 2e37202 | 2012-12-13 00:47:36 +0000 | [diff] [blame] | 22 | #include <linux/ctype.h> |
Tom Rini | a17b7bc | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 23 | #include <linux/math64.h> |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 24 | #include "fat.c" |
| 25 | |
Heinrich Schuchardt | 24977e5 | 2020-11-25 16:33:55 +0100 | [diff] [blame] | 26 | static dir_entry *find_directory_entry(fat_itr *itr, char *filename); |
Heinrich Schuchardt | 5bc30a5 | 2020-11-22 09:58:44 +0100 | [diff] [blame] | 27 | static int new_dir_table(fat_itr *itr); |
Heinrich Schuchardt | 24977e5 | 2020-11-25 16:33:55 +0100 | [diff] [blame] | 28 | |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 29 | /* Characters that may only be used in long file names */ |
| 30 | static const char LONG_ONLY_CHARS[] = "+,;=[]"; |
| 31 | |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 32 | /* Combined size of the name and ext fields in the directory entry */ |
| 33 | #define SHORT_NAME_SIZE 11 |
| 34 | |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 35 | /** |
| 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 | */ |
| 51 | static int str2fat(char *dest, char *src, int length) |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 52 | { |
| 53 | int i; |
| 54 | |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 55 | 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 Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 68 | } |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 69 | return i; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 72 | /** |
Heinrich Schuchardt | 24977e5 | 2020-11-25 16:33:55 +0100 | [diff] [blame] | 73 | * 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 | */ |
| 79 | static 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 Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 94 | * 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 Schuchardt | 24977e5 | 2020-11-25 16:33:55 +0100 | [diff] [blame] | 101 | * @itr: directory iterator |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 102 | * @filename: long file name |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 103 | * @shortname: buffer of 11 bytes to receive chosen short name and extension |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 104 | * Return: number of directory entries needed, negative on error |
| 105 | */ |
Heinrich Schuchardt | 24977e5 | 2020-11-25 16:33:55 +0100 | [diff] [blame] | 106 | static int set_name(fat_itr *itr, const char *filename, char *shortname) |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 107 | { |
| 108 | char *period; |
| 109 | char *pos; |
| 110 | int period_location; |
| 111 | char buf[13]; |
| 112 | int i; |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 113 | int ret; |
Heinrich Schuchardt | cfb7c2b | 2021-01-26 00:04:19 +0100 | [diff] [blame] | 114 | struct nameext dirent; |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 115 | |
| 116 | if (!filename) |
| 117 | return -EIO; |
| 118 | |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 119 | /* Initialize buffer */ |
| 120 | memset(&dirent, ' ', sizeof(dirent)); |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 121 | |
| 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 Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 130 | str2fat(dirent.ext, period + 1, sizeof(dirent.ext)); |
| 131 | period_location = str2fat(dirent.name, pos, sizeof(dirent.name)); |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 132 | if (period_location < 0) |
| 133 | return period_location; |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 134 | if (*dirent.name == ' ') |
| 135 | *dirent.name = '_'; |
Heinrich Schuchardt | d701a22 | 2023-07-26 10:33:13 +0200 | [diff] [blame] | 136 | /* Substitute character 0xe5 signaling deletetion by character 0x05 */ |
| 137 | if (*dirent.name == DELETED_FLAG) |
| 138 | *dirent.name = aRING; |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 139 | |
| 140 | /* If filename and short name are the same, quit. */ |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 141 | sprintf(buf, "%.*s.%.3s", period_location, dirent.name, dirent.ext); |
| 142 | if (!strcmp(buf, filename)) { |
| 143 | ret = 1; |
| 144 | goto out; |
Stefan Herbrechtsmeier | 4052d1f | 2023-03-17 13:04:13 +0100 | [diff] [blame] | 145 | } else if (!strcasecmp(buf, filename)) { |
| 146 | goto out_ret; |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 147 | } |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 148 | |
| 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 Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 167 | memcpy(dirent.name + suffix_start, buf, suffix_len); |
| 168 | if (*dirent.ext != ' ') |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 169 | sprintf(buf, "%.*s.%.3s", suffix_start + suffix_len, |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 170 | dirent.name, dirent.ext); |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 171 | else |
| 172 | sprintf(buf, "%.*s", suffix_start + suffix_len, |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 173 | dirent.name); |
Heinrich Schuchardt | 24977e5 | 2020-11-25 16:33:55 +0100 | [diff] [blame] | 174 | debug("generated short name: %s\n", buf); |
| 175 | |
| 176 | /* 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; |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 182 | |
Stefan Herbrechtsmeier | 4052d1f | 2023-03-17 13:04:13 +0100 | [diff] [blame] | 183 | goto out_ret; |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 184 | } |
| 185 | return -EIO; |
Stefan Herbrechtsmeier | 4052d1f | 2023-03-17 13:04:13 +0100 | [diff] [blame] | 186 | out_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 Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 190 | out: |
Heinrich Schuchardt | cfb7c2b | 2021-01-26 00:04:19 +0100 | [diff] [blame] | 191 | memcpy(shortname, &dirent, SHORT_NAME_SIZE); |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 192 | return ret; |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 193 | } |
| 194 | |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 195 | static int total_sector; |
Donggeun Kim | cb5f3bc | 2012-03-22 04:38:55 +0000 | [diff] [blame] | 196 | static int disk_write(__u32 block, __u32 nr_blocks, void *buf) |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 197 | { |
Łukasz Majewski | d6f23d8 | 2015-09-03 14:21:39 +0200 | [diff] [blame] | 198 | ulong ret; |
| 199 | |
Simon Glass | 2ee8ada | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 200 | if (!cur_dev) |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 201 | return -1; |
| 202 | |
Donggeun Kim | cb5f3bc | 2012-03-22 04:38:55 +0000 | [diff] [blame] | 203 | if (cur_part_info.start + block + nr_blocks > |
| 204 | cur_part_info.start + total_sector) { |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 205 | printf("error: overflow occurs\n"); |
| 206 | return -1; |
| 207 | } |
| 208 | |
Simon Glass | 2ee8ada | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 209 | ret = blk_dwrite(cur_dev, cur_part_info.start + block, nr_blocks, buf); |
Łukasz Majewski | d6f23d8 | 2015-09-03 14:21:39 +0200 | [diff] [blame] | 210 | if (nr_blocks && ret == 0) |
| 211 | return -1; |
| 212 | |
| 213 | return ret; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 216 | /* |
| 217 | * Write fat buffer into block device |
| 218 | */ |
Stefan Brüns | 751b31d | 2016-09-11 22:51:40 +0200 | [diff] [blame] | 219 | static int flush_dirty_fat_buffer(fsdata *mydata) |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 220 | { |
| 221 | int getsize = FATBUFBLOCKS; |
| 222 | __u32 fatlength = mydata->fatlength; |
| 223 | __u8 *bufptr = mydata->fatbuf; |
| 224 | __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS; |
| 225 | |
Stefan Brüns | 751b31d | 2016-09-11 22:51:40 +0200 | [diff] [blame] | 226 | 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üns | 58bf86f | 2016-12-17 00:27:50 +0100 | [diff] [blame] | 232 | /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */ |
| 233 | if (startblock + getsize > fatlength) |
| 234 | getsize = fatlength - startblock; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 235 | |
Stefan Brüns | 58bf86f | 2016-12-17 00:27:50 +0100 | [diff] [blame] | 236 | startblock += mydata->fat_sect; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 237 | |
| 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 Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 244 | if (mydata->fats == 2) { |
Donggeun Kim | af57ce2 | 2011-12-20 18:34:27 +0000 | [diff] [blame] | 245 | /* 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üns | 751b31d | 2016-09-11 22:51:40 +0200 | [diff] [blame] | 252 | mydata->fat_dirty = 0; |
Donggeun Kim | af57ce2 | 2011-12-20 18:34:27 +0000 | [diff] [blame] | 253 | |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 254 | return 0; |
| 255 | } |
| 256 | |
Heinrich Schuchardt | 5bc30a5 | 2020-11-22 09:58:44 +0100 | [diff] [blame] | 257 | /** |
| 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 Schuchardt | 524d13a | 2020-11-22 11:54:22 +0100 | [diff] [blame] | 264 | static int fat_find_empty_dentries(fat_itr *itr, int count) |
Heinrich Schuchardt | 5bc30a5 | 2020-11-22 09:58:44 +0100 | [diff] [blame] | 265 | { |
| 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 Schuchardt | 66ea58c | 2021-01-21 00:23:33 +0100 | [diff] [blame] | 281 | switch (itr->dent->nameext.name[0]) { |
Heinrich Schuchardt | 5bc30a5 | 2020-11-22 09:58:44 +0100 | [diff] [blame] | 282 | 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 | } |
| 305 | out: |
| 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 Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 317 | /* |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 318 | * Set the file name information from 'name' into 'slotptr', |
| 319 | */ |
| 320 | static 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 */ |
| 363 | name0_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; |
| 369 | name5_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; |
| 376 | name11_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 Takahiro | a2c0bd0 | 2019-05-24 14:10:36 +0900 | [diff] [blame] | 386 | static int flush_dir(fat_itr *itr); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 387 | |
Heinrich Schuchardt | ac4ab75 | 2020-11-21 08:32:50 +0100 | [diff] [blame] | 388 | /** |
| 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 Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 395 | */ |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 396 | static int |
Heinrich Schuchardt | ac4ab75 | 2020-11-21 08:32:50 +0100 | [diff] [blame] | 397 | fill_dir_slot(fat_itr *itr, const char *l_name, const char *shortname) |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 398 | { |
Tien Fong Chee | 0117dbe | 2016-07-27 23:08:56 -0700 | [diff] [blame] | 399 | __u8 temp_dir_slot_buffer[MAX_LFN_SLOT * sizeof(dir_slot)]; |
| 400 | dir_slot *slotptr = (dir_slot *)temp_dir_slot_buffer; |
Anatolij Gustschin | f4f9d5d | 2011-12-15 03:12:14 +0000 | [diff] [blame] | 401 | __u8 counter = 0, checksum; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 402 | int idx = 0, ret; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 403 | |
Stefan Brüns | 6c617d6 | 2016-09-11 22:51:39 +0200 | [diff] [blame] | 404 | /* Get short file name checksum value */ |
Heinrich Schuchardt | 66ea58c | 2021-01-21 00:23:33 +0100 | [diff] [blame] | 405 | checksum = mkcksum((void *)shortname); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 406 | |
| 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 Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 420 | memcpy(itr->dent, slotptr, sizeof(dir_slot)); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 421 | slotptr--; |
| 422 | counter--; |
AKASHI Takahiro | a2c0bd0 | 2019-05-24 14:10:36 +0900 | [diff] [blame] | 423 | |
Heinrich Schuchardt | 913339a | 2021-01-20 22:21:53 +0100 | [diff] [blame] | 424 | if (!itr->remaining) { |
| 425 | /* Write directory table to device */ |
| 426 | ret = flush_dir(itr); |
| 427 | if (ret) |
| 428 | return ret; |
| 429 | } |
AKASHI Takahiro | a2c0bd0 | 2019-05-24 14:10:36 +0900 | [diff] [blame] | 430 | |
Heinrich Schuchardt | 524d13a | 2020-11-22 11:54:22 +0100 | [diff] [blame] | 431 | next_dent(itr); |
| 432 | if (!itr->dent) |
| 433 | return -EIO; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 436 | return 0; |
| 437 | } |
| 438 | |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 439 | /* |
Philipp Skadorov | 16f553d | 2016-12-15 15:52:53 -0500 | [diff] [blame] | 440 | * Set the entry at index 'entry' in a FAT (12/16/32) table. |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 441 | */ |
| 442 | static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value) |
| 443 | { |
Philipp Skadorov | 16f553d | 2016-12-15 15:52:53 -0500 | [diff] [blame] | 444 | __u32 bufnum, offset, off16; |
| 445 | __u16 val1, val2; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 446 | |
| 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 Skadorov | 16f553d | 2016-12-15 15:52:53 -0500 | [diff] [blame] | 456 | case 12: |
| 457 | bufnum = entry / FAT12BUFSIZE; |
| 458 | offset = entry - bufnum * FAT12BUFSIZE; |
| 459 | break; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 460 | 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üns | 58bf86f | 2016-12-17 00:27:50 +0100 | [diff] [blame] | 472 | /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */ |
| 473 | if (startblock + getsize > fatlength) |
| 474 | getsize = fatlength - startblock; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 475 | |
Stefan Brüns | 751b31d | 2016-09-11 22:51:40 +0200 | [diff] [blame] | 476 | if (flush_dirty_fat_buffer(mydata) < 0) |
| 477 | return -1; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 478 | |
Stefan Brüns | 58bf86f | 2016-12-17 00:27:50 +0100 | [diff] [blame] | 479 | startblock += mydata->fat_sect; |
| 480 | |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 481 | 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üns | 751b31d | 2016-09-11 22:51:40 +0200 | [diff] [blame] | 488 | /* Mark as dirty */ |
| 489 | mydata->fat_dirty = 1; |
| 490 | |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 491 | /* 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 Skadorov | 16f553d | 2016-12-15 15:52:53 -0500 | [diff] [blame] | 499 | 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 Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 538 | default: |
| 539 | return -1; |
| 540 | } |
| 541 | |
| 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | /* |
Philipp Skadorov | 16f553d | 2016-12-15 15:52:53 -0500 | [diff] [blame] | 546 | * Determine the next free cluster after 'entry' in a FAT (12/16/32) table |
Stefan Brüns | 1499940 | 2016-09-11 22:51:41 +0200 | [diff] [blame] | 547 | * and link it to 'entry'. EOC marker is not set on returned entry. |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 548 | */ |
| 549 | static __u32 determine_fatent(fsdata *mydata, __u32 entry) |
| 550 | { |
| 551 | __u32 next_fat, next_entry = entry + 1; |
| 552 | |
| 553 | while (1) { |
Stefan Brüns | 9f95d81 | 2016-12-17 00:27:51 +0100 | [diff] [blame] | 554 | next_fat = get_fatent(mydata, next_entry); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 555 | if (next_fat == 0) { |
Stefan Brüns | 1499940 | 2016-09-11 22:51:41 +0200 | [diff] [blame] | 556 | /* found free entry, link to entry */ |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 557 | 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 Schuchardt | 9fb8db4 | 2018-10-02 09:30:45 +0200 | [diff] [blame] | 568 | /** |
AKASHI Takahiro | d98c674 | 2019-05-24 14:10:35 +0900 | [diff] [blame] | 569 | * set_sectors() - write data to sectors |
Heinrich Schuchardt | 9fb8db4 | 2018-10-02 09:30:45 +0200 | [diff] [blame] | 570 | * |
AKASHI Takahiro | d98c674 | 2019-05-24 14:10:35 +0900 | [diff] [blame] | 571 | * Write 'size' bytes from 'buffer' into the specified sector. |
Heinrich Schuchardt | 9fb8db4 | 2018-10-02 09:30:45 +0200 | [diff] [blame] | 572 | * |
| 573 | * @mydata: data to be written |
AKASHI Takahiro | d98c674 | 2019-05-24 14:10:35 +0900 | [diff] [blame] | 574 | * @startsect: sector to be written to |
Heinrich Schuchardt | 9fb8db4 | 2018-10-02 09:30:45 +0200 | [diff] [blame] | 575 | * @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 Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 578 | */ |
| 579 | static int |
AKASHI Takahiro | d98c674 | 2019-05-24 14:10:35 +0900 | [diff] [blame] | 580 | set_sectors(fsdata *mydata, u32 startsect, u8 *buffer, u32 size) |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 581 | { |
Benoît Thébaudeau | ccc945b | 2015-09-28 15:45:28 +0200 | [diff] [blame] | 582 | int ret; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 583 | |
AKASHI Takahiro | d98c674 | 2019-05-24 14:10:35 +0900 | [diff] [blame] | 584 | debug("startsect: %d\n", startsect); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 585 | |
Benoît Thébaudeau | ccc945b | 2015-09-28 15:45:28 +0200 | [diff] [blame] | 586 | if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) { |
| 587 | ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size); |
| 588 | |
Heinrich Schuchardt | aa9d6ba | 2018-09-13 19:42:47 +0200 | [diff] [blame] | 589 | debug("FAT: Misaligned buffer address (%p)\n", buffer); |
Benoît Thébaudeau | ccc945b | 2015-09-28 15:45:28 +0200 | [diff] [blame] | 590 | |
| 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 Schuchardt | eb3587b | 2021-01-26 00:14:14 +0100 | [diff] [blame] | 603 | u32 nsects; |
| 604 | |
AKASHI Takahiro | d98c674 | 2019-05-24 14:10:35 +0900 | [diff] [blame] | 605 | nsects = size / mydata->sect_size; |
| 606 | ret = disk_write(startsect, nsects, buffer); |
| 607 | if (ret != nsects) { |
Benoît Thébaudeau | ccc945b | 2015-09-28 15:45:28 +0200 | [diff] [blame] | 608 | debug("Error writing data (got %d)\n", ret); |
Wu, Josh | b0e38dd | 2013-07-24 17:55:30 +0800 | [diff] [blame] | 609 | return -1; |
| 610 | } |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 611 | |
AKASHI Takahiro | d98c674 | 2019-05-24 14:10:35 +0900 | [diff] [blame] | 612 | startsect += nsects; |
| 613 | buffer += nsects * mydata->sect_size; |
| 614 | size -= nsects * mydata->sect_size; |
Benoît Thébaudeau | ccc945b | 2015-09-28 15:45:28 +0200 | [diff] [blame] | 615 | } |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 616 | |
Benoît Thébaudeau | ccc945b | 2015-09-28 15:45:28 +0200 | [diff] [blame] | 617 | if (size) { |
| 618 | ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size); |
Heinrich Schuchardt | 9fb8db4 | 2018-10-02 09:30:45 +0200 | [diff] [blame] | 619 | /* Do not leak content of stack */ |
| 620 | memset(tmpbuf, 0, mydata->sect_size); |
Benoît Thébaudeau | ccc945b | 2015-09-28 15:45:28 +0200 | [diff] [blame] | 621 | 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 Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 625 | return -1; |
| 626 | } |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | return 0; |
| 630 | } |
| 631 | |
AKASHI Takahiro | d98c674 | 2019-05-24 14:10:35 +0900 | [diff] [blame] | 632 | /** |
| 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 | */ |
| 643 | static int |
| 644 | set_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 Schuchardt | 913339a | 2021-01-20 22:21:53 +0100 | [diff] [blame] | 650 | /** |
| 651 | * flush_dir() - flush directory |
| 652 | * |
| 653 | * @itr: directory iterator |
| 654 | * Return: 0 for success, -EIO on error |
| 655 | */ |
| 656 | static int flush_dir(fat_itr *itr) |
AKASHI Takahiro | d98c674 | 2019-05-24 14:10:35 +0900 | [diff] [blame] | 657 | { |
| 658 | fsdata *mydata = itr->fsdata; |
| 659 | u32 startsect, sect_offset, nsects; |
Heinrich Schuchardt | 913339a | 2021-01-20 22:21:53 +0100 | [diff] [blame] | 660 | int ret; |
AKASHI Takahiro | d98c674 | 2019-05-24 14:10:35 +0900 | [diff] [blame] | 661 | |
Heinrich Schuchardt | 913339a | 2021-01-20 22:21:53 +0100 | [diff] [blame] | 662 | 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 Takahiro | d98c674 | 2019-05-24 14:10:35 +0900 | [diff] [blame] | 667 | |
| 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 Schuchardt | 913339a | 2021-01-20 22:21:53 +0100 | [diff] [blame] | 674 | ret = set_sectors(mydata, startsect, itr->block, |
| 675 | nsects * mydata->sect_size); |
| 676 | out: |
| 677 | if (ret) { |
| 678 | log_err("Error: writing directory entry\n"); |
| 679 | return -EIO; |
| 680 | } |
| 681 | return 0; |
AKASHI Takahiro | d98c674 | 2019-05-24 14:10:35 +0900 | [diff] [blame] | 682 | } |
| 683 | |
AKASHI Takahiro | b07f704 | 2018-09-11 15:59:06 +0900 | [diff] [blame] | 684 | /* |
| 685 | * Read and modify data on existing and consecutive cluster blocks |
| 686 | */ |
| 687 | static int |
| 688 | get_set_cluster(fsdata *mydata, __u32 clustnum, loff_t pos, __u8 *buffer, |
| 689 | loff_t size, loff_t *gotsize) |
| 690 | { |
Heinrich Schuchardt | 8a3ac6e | 2020-07-06 07:48:14 +0200 | [diff] [blame] | 691 | static u8 *tmpbuf_cluster; |
AKASHI Takahiro | b07f704 | 2018-09-11 15:59:06 +0900 | [diff] [blame] | 692 | unsigned int bytesperclust = mydata->clust_size * mydata->sect_size; |
| 693 | __u32 startsect; |
Heinrich Schuchardt | f0be00e | 2023-07-30 16:44:04 +0200 | [diff] [blame] | 694 | loff_t clustcount, wsize; |
| 695 | int i, ret; |
AKASHI Takahiro | b07f704 | 2018-09-11 15:59:06 +0900 | [diff] [blame] | 696 | |
| 697 | *gotsize = 0; |
| 698 | if (!size) |
| 699 | return 0; |
| 700 | |
Heinrich Schuchardt | 8a3ac6e | 2020-07-06 07:48:14 +0200 | [diff] [blame] | 701 | if (!tmpbuf_cluster) { |
| 702 | tmpbuf_cluster = memalign(ARCH_DMA_MINALIGN, MAX_CLUSTSIZE); |
| 703 | if (!tmpbuf_cluster) |
| 704 | return -1; |
| 705 | } |
| 706 | |
AKASHI Takahiro | b07f704 | 2018-09-11 15:59:06 +0900 | [diff] [blame] | 707 | 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 Takahiro | b07f704 | 2018-09-11 15:59:06 +0900 | [diff] [blame] | 795 | *gotsize += wsize; |
| 796 | } |
| 797 | |
| 798 | assert(!size); |
| 799 | |
| 800 | return 0; |
| 801 | } |
| 802 | |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 803 | /* |
| 804 | * Find the first empty cluster |
| 805 | */ |
| 806 | static int find_empty_cluster(fsdata *mydata) |
| 807 | { |
| 808 | __u32 fat_val, entry = 3; |
| 809 | |
| 810 | while (1) { |
Stefan Brüns | 9f95d81 | 2016-12-17 00:27:51 +0100 | [diff] [blame] | 811 | fat_val = get_fatent(mydata, entry); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 812 | if (fat_val == 0) |
| 813 | break; |
| 814 | entry++; |
| 815 | } |
| 816 | |
| 817 | return entry; |
| 818 | } |
| 819 | |
Heinrich Schuchardt | e09f3e4 | 2020-11-26 19:06:55 +0100 | [diff] [blame] | 820 | /** |
| 821 | * new_dir_table() - allocate a cluster for additional directory entries |
| 822 | * |
| 823 | * @itr: directory iterator |
| 824 | * Return: 0 on success, -EIO otherwise |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 825 | */ |
AKASHI Takahiro | a2c0bd0 | 2019-05-24 14:10:36 +0900 | [diff] [blame] | 826 | static int new_dir_table(fat_itr *itr) |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 827 | { |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 828 | fsdata *mydata = itr->fsdata; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 829 | int dir_newclust = 0; |
Heinrich Schuchardt | e09f3e4 | 2020-11-26 19:06:55 +0100 | [diff] [blame] | 830 | int dir_oldclust = itr->clust; |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 831 | unsigned int bytesperclust = mydata->clust_size * mydata->sect_size; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 832 | |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 833 | dir_newclust = find_empty_cluster(mydata); |
Heinrich Schuchardt | e09f3e4 | 2020-11-26 19:06:55 +0100 | [diff] [blame] | 834 | |
| 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 Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 846 | 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 Skadorov | 16f553d | 2016-12-15 15:52:53 -0500 | [diff] [blame] | 850 | else if (mydata->fatsize == 12) |
| 851 | set_fatent_value(mydata, dir_newclust, 0xff8); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 852 | |
Stefan Brüns | 751b31d | 2016-09-11 22:51:40 +0200 | [diff] [blame] | 853 | if (flush_dirty_fat_buffer(mydata) < 0) |
Heinrich Schuchardt | e09f3e4 | 2020-11-26 19:06:55 +0100 | [diff] [blame] | 854 | return -EIO; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 855 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 856 | itr->dent = (dir_entry *)itr->block; |
| 857 | itr->last_cluster = 1; |
| 858 | itr->remaining = bytesperclust / sizeof(dir_entry) - 1; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 859 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 860 | return 0; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | /* |
| 864 | * Set empty cluster from 'entry' to the end of a file |
| 865 | */ |
| 866 | static int clear_fatent(fsdata *mydata, __u32 entry) |
| 867 | { |
| 868 | __u32 fat_val; |
| 869 | |
Philipp Skadorov | 16f553d | 2016-12-15 15:52:53 -0500 | [diff] [blame] | 870 | while (!CHECK_CLUST(entry, mydata->fatsize)) { |
Stefan Brüns | 9f95d81 | 2016-12-17 00:27:51 +0100 | [diff] [blame] | 871 | fat_val = get_fatent(mydata, entry); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 872 | if (fat_val != 0) |
| 873 | set_fatent_value(mydata, entry, 0); |
| 874 | else |
| 875 | break; |
| 876 | |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 877 | entry = fat_val; |
| 878 | } |
| 879 | |
| 880 | /* Flush fat buffer */ |
Stefan Brüns | 751b31d | 2016-09-11 22:51:40 +0200 | [diff] [blame] | 881 | if (flush_dirty_fat_buffer(mydata) < 0) |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 882 | return -1; |
| 883 | |
| 884 | return 0; |
| 885 | } |
| 886 | |
| 887 | /* |
AKASHI Takahiro | ed8b1e4 | 2018-09-11 15:59:05 +0900 | [diff] [blame] | 888 | * Set start cluster in directory entry |
| 889 | */ |
| 890 | static 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 | */ |
| 904 | static 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 Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 924 | * Write at most 'maxsize' bytes from 'buffer' into |
| 925 | * the file associated with 'dentptr' |
Suriyan Ramasami | 441c223 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 926 | * Update the number of bytes written in *gotsize and return 0 |
| 927 | * or return -1 on fatal errors. |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 928 | */ |
| 929 | static int |
AKASHI Takahiro | ed8b1e4 | 2018-09-11 15:59:05 +0900 | [diff] [blame] | 930 | set_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, __u8 *buffer, |
| 931 | loff_t maxsize, loff_t *gotsize) |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 932 | { |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 933 | unsigned int bytesperclust = mydata->clust_size * mydata->sect_size; |
| 934 | __u32 curclust = START(dentptr); |
| 935 | __u32 endclust = 0, newclust = 0; |
Heinrich Schuchardt | db538a9 | 2019-02-25 19:42:48 +0100 | [diff] [blame] | 936 | u64 cur_pos, filesize; |
| 937 | loff_t offset, actsize, wsize; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 938 | |
Suriyan Ramasami | 441c223 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 939 | *gotsize = 0; |
AKASHI Takahiro | b07f704 | 2018-09-11 15:59:06 +0900 | [diff] [blame] | 940 | filesize = pos + maxsize; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 941 | |
Suriyan Ramasami | 441c223 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 942 | debug("%llu bytes\n", filesize); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 943 | |
AKASHI Takahiro | b07f704 | 2018-09-11 15:59:06 +0900 | [diff] [blame] | 944 | 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"); |
Benoît Thébaudeau | 8fed7d3 | 2015-09-28 15:45:32 +0200 | [diff] [blame] | 976 | return -1; |
| 977 | } |
AKASHI Takahiro | b07f704 | 2018-09-11 15:59:06 +0900 | [diff] [blame] | 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 Szyprowski | 2f24167 | 2019-12-02 12:11:13 +0100 | [diff] [blame] | 1004 | if (newclust != endclust + 1) |
| 1005 | break; |
AKASHI Takahiro | b07f704 | 2018-09-11 15:59:06 +0900 | [diff] [blame] | 1006 | 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 Szyprowski | 3a867ce | 2019-12-02 12:11:14 +0100 | [diff] [blame] | 1023 | wsize = min_t(unsigned long long, actsize, filesize - cur_pos); |
| 1024 | wsize -= offset; |
| 1025 | |
AKASHI Takahiro | b07f704 | 2018-09-11 15:59:06 +0900 | [diff] [blame] | 1026 | 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 Takahiro | b07f704 | 2018-09-11 15:59:06 +0900 | [diff] [blame] | 1038 | 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; |
AKASHI Takahiro | ed8b1e4 | 2018-09-11 15:59:05 +0900 | [diff] [blame] | 1063 | } |
| 1064 | |
AKASHI Takahiro | b07f704 | 2018-09-11 15:59:06 +0900 | [diff] [blame] | 1065 | curclust = endclust; |
| 1066 | filesize -= cur_pos; |
Heinrich Schuchardt | db538a9 | 2019-02-25 19:42:48 +0100 | [diff] [blame] | 1067 | assert(!do_div(cur_pos, bytesperclust)); |
AKASHI Takahiro | ed8b1e4 | 2018-09-11 15:59:05 +0900 | [diff] [blame] | 1068 | |
AKASHI Takahiro | b07f704 | 2018-09-11 15:59:06 +0900 | [diff] [blame] | 1069 | set_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"); |
| 1086 | return -1; |
| 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | /* TODO: already partially written */ |
AKASHI Takahiro | ed8b1e4 | 2018-09-11 15:59:05 +0900 | [diff] [blame] | 1091 | if (check_overflow(mydata, curclust, filesize)) { |
| 1092 | printf("Error: no space left: %llu\n", filesize); |
| 1093 | return -1; |
Benoît Thébaudeau | 8fed7d3 | 2015-09-28 15:45:32 +0200 | [diff] [blame] | 1094 | } |
| 1095 | |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1096 | 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 Takahiro | ed8b1e4 | 2018-09-11 15:59:05 +0900 | [diff] [blame] | 1104 | /* write to <curclust..endclust> */ |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1105 | goto getit; |
| 1106 | |
| 1107 | if (CHECK_CLUST(newclust, mydata->fatsize)) { |
Benoît Thébaudeau | e0b8694 | 2015-09-28 15:45:30 +0200 | [diff] [blame] | 1108 | debug("newclust: 0x%x\n", newclust); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1109 | debug("Invalid FAT entry\n"); |
Suriyan Ramasami | 441c223 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1110 | return 0; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1111 | } |
| 1112 | endclust = newclust; |
| 1113 | actsize += bytesperclust; |
| 1114 | } |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1115 | |
| 1116 | /* set remaining bytes */ |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1117 | actsize = filesize; |
Heinrich Schuchardt | 9fb8db4 | 2018-10-02 09:30:45 +0200 | [diff] [blame] | 1118 | if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) { |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1119 | debug("error: writing cluster\n"); |
| 1120 | return -1; |
| 1121 | } |
Suriyan Ramasami | 441c223 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1122 | *gotsize += actsize; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1123 | |
| 1124 | /* Mark end of file in FAT */ |
Philipp Skadorov | 16f553d | 2016-12-15 15:52:53 -0500 | [diff] [blame] | 1125 | if (mydata->fatsize == 12) |
| 1126 | newclust = 0xfff; |
| 1127 | else if (mydata->fatsize == 16) |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1128 | newclust = 0xffff; |
| 1129 | else if (mydata->fatsize == 32) |
| 1130 | newclust = 0xfffffff; |
| 1131 | set_fatent_value(mydata, endclust, newclust); |
| 1132 | |
Suriyan Ramasami | 441c223 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1133 | return 0; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1134 | getit: |
Heinrich Schuchardt | 9fb8db4 | 2018-10-02 09:30:45 +0200 | [diff] [blame] | 1135 | if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) { |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1136 | debug("error: writing cluster\n"); |
| 1137 | return -1; |
| 1138 | } |
Suriyan Ramasami | 441c223 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1139 | *gotsize += actsize; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1140 | filesize -= actsize; |
| 1141 | buffer += actsize; |
| 1142 | |
Benoît Thébaudeau | e0b8694 | 2015-09-28 15:45:30 +0200 | [diff] [blame] | 1143 | if (CHECK_CLUST(newclust, mydata->fatsize)) { |
| 1144 | debug("newclust: 0x%x\n", newclust); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1145 | debug("Invalid FAT entry\n"); |
Suriyan Ramasami | 441c223 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1146 | return 0; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1147 | } |
| 1148 | actsize = bytesperclust; |
| 1149 | curclust = endclust = newclust; |
| 1150 | } while (1); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1151 | |
AKASHI Takahiro | ed8b1e4 | 2018-09-11 15:59:05 +0900 | [diff] [blame] | 1152 | return 0; |
Benoît Thébaudeau | 8fed7d3 | 2015-09-28 15:45:32 +0200 | [diff] [blame] | 1153 | } |
| 1154 | |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 1155 | /** |
Heinrich Schuchardt | cab43b0 | 2024-04-09 22:06:07 +0200 | [diff] [blame] | 1156 | * dentry_set_time() - set change time |
| 1157 | * |
| 1158 | * @dentptr: directory entry |
| 1159 | */ |
| 1160 | static 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 | } |
| 1185 | err: |
| 1186 | dentptr->date = 0x2821; /* 2000-01-01 */ |
| 1187 | dentptr->time = 0; |
| 1188 | } |
| 1189 | |
| 1190 | /** |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 1191 | * 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ébaudeau | 8fed7d3 | 2015-09-28 15:45:32 +0200 | [diff] [blame] | 1199 | */ |
| 1200 | static void fill_dentry(fsdata *mydata, dir_entry *dentptr, |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 1201 | const char *shortname, __u32 start_cluster, __u32 size, __u8 attr) |
Benoît Thébaudeau | 8fed7d3 | 2015-09-28 15:45:32 +0200 | [diff] [blame] | 1202 | { |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 1203 | memset(dentptr, 0, sizeof(*dentptr)); |
| 1204 | |
Benoît Thébaudeau | 8fed7d3 | 2015-09-28 15:45:32 +0200 | [diff] [blame] | 1205 | set_start_cluster(mydata, dentptr, start_cluster); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1206 | dentptr->size = cpu_to_le32(size); |
| 1207 | |
| 1208 | dentptr->attr = attr; |
| 1209 | |
Heinrich Schuchardt | cab43b0 | 2024-04-09 22:06:07 +0200 | [diff] [blame] | 1210 | /* Set change date */ |
| 1211 | dentry_set_time(dentptr); |
| 1212 | /* Set creation date */ |
| 1213 | dentptr->cdate = dentptr->date; |
| 1214 | dentptr->ctime = dentptr->time; |
| 1215 | |
Heinrich Schuchardt | 66ea58c | 2021-01-21 00:23:33 +0100 | [diff] [blame] | 1216 | memcpy(&dentptr->nameext, shortname, SHORT_NAME_SIZE); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
Heinrich Schuchardt | 7a3f610 | 2020-11-26 16:10:01 +0100 | [diff] [blame] | 1219 | /** |
| 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 Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1225 | */ |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1226 | static dir_entry *find_directory_entry(fat_itr *itr, char *filename) |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1227 | { |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1228 | int match = 0; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1229 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1230 | 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 Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1237 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1238 | if (!match) |
| 1239 | continue; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1240 | |
Heinrich Schuchardt | 66ea58c | 2021-01-21 00:23:33 +0100 | [diff] [blame] | 1241 | if (itr->dent->nameext.name[0] == '\0') |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1242 | return NULL; |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1243 | else |
| 1244 | return itr->dent; |
| 1245 | } |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1246 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1247 | return NULL; |
| 1248 | } |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1249 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1250 | static int split_filename(char *filename, char **dirname, char **basename) |
| 1251 | { |
| 1252 | char *p, *last_slash, *last_slash_cont; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1253 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1254 | again: |
| 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; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1267 | } |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1268 | p++; |
| 1269 | } |
Wu, Josh | 675b23c | 2014-05-08 16:14:07 +0800 | [diff] [blame] | 1270 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1271 | if (last_slash) { |
| 1272 | if (last_slash_cont == (filename + strlen(filename) - 1)) { |
| 1273 | /* remove trailing slashes */ |
| 1274 | *last_slash = '\0'; |
| 1275 | goto again; |
Wu, Josh | 675b23c | 2014-05-08 16:14:07 +0800 | [diff] [blame] | 1276 | } |
| 1277 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1278 | if (last_slash == filename) { |
| 1279 | /* avoid ""(null) directory */ |
| 1280 | *dirname = "/"; |
| 1281 | } else { |
| 1282 | *last_slash = '\0'; |
| 1283 | *dirname = filename; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1284 | } |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1285 | |
| 1286 | *last_slash_cont = '\0'; |
Heinrich Schuchardt | 2e1b17b | 2021-01-30 14:12:10 +0100 | [diff] [blame] | 1287 | filename = last_slash_cont + 1; |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1288 | } else { |
| 1289 | *dirname = "/"; /* root by default */ |
Heinrich Schuchardt | 2e1b17b | 2021-01-30 14:12:10 +0100 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | /* |
| 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 | } |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
Heinrich Schuchardt | 2e1b17b | 2021-01-30 14:12:10 +0100 | [diff] [blame] | 1316 | done: |
| 1317 | *basename = filename; |
| 1318 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1319 | return 0; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
Heinrich Schuchardt | 5858721 | 2019-05-12 09:59:18 +0200 | [diff] [blame] | 1322 | /** |
| 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 Takahiro | c83df1a | 2018-09-11 15:59:02 +0900 | [diff] [blame] | 1332 | static int normalize_longname(char *l_filename, const char *filename) |
| 1333 | { |
Heinrich Schuchardt | 5858721 | 2019-05-12 09:59:18 +0200 | [diff] [blame] | 1334 | const char *p, illegal[] = "<>:\"/\\|?*"; |
Heinrich Schuchardt | b36d546 | 2021-01-30 11:08:21 +0100 | [diff] [blame] | 1335 | size_t len; |
AKASHI Takahiro | c83df1a | 2018-09-11 15:59:02 +0900 | [diff] [blame] | 1336 | |
Heinrich Schuchardt | b36d546 | 2021-01-30 11:08:21 +0100 | [diff] [blame] | 1337 | len = strlen(filename); |
| 1338 | if (!len || len >= VFAT_MAXLEN_BYTES || filename[len - 1] == '.') |
AKASHI Takahiro | c83df1a | 2018-09-11 15:59:02 +0900 | [diff] [blame] | 1339 | return -1; |
AKASHI Takahiro | c83df1a | 2018-09-11 15:59:02 +0900 | [diff] [blame] | 1340 | |
Heinrich Schuchardt | 5858721 | 2019-05-12 09:59:18 +0200 | [diff] [blame] | 1341 | for (p = filename; *p; ++p) { |
| 1342 | if ((unsigned char)*p < 0x20) |
| 1343 | return -1; |
| 1344 | if (strchr(illegal, *p)) |
| 1345 | return -1; |
| 1346 | } |
AKASHI Takahiro | c83df1a | 2018-09-11 15:59:02 +0900 | [diff] [blame] | 1347 | |
Heinrich Schuchardt | 5858721 | 2019-05-12 09:59:18 +0200 | [diff] [blame] | 1348 | strcpy(l_filename, filename); |
| 1349 | downcase(l_filename, VFAT_MAXLEN_BYTES); |
AKASHI Takahiro | c83df1a | 2018-09-11 15:59:02 +0900 | [diff] [blame] | 1350 | |
| 1351 | return 0; |
| 1352 | } |
| 1353 | |
AKASHI Takahiro | ed8b1e4 | 2018-09-11 15:59:05 +0900 | [diff] [blame] | 1354 | int file_fat_write_at(const char *filename, loff_t pos, void *buffer, |
| 1355 | loff_t size, loff_t *actwrite) |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1356 | { |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1357 | dir_entry *retdent; |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1358 | fsdata datablock = { .fatbuf = NULL, }; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1359 | fsdata *mydata = &datablock; |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1360 | fat_itr *itr = NULL; |
AKASHI Takahiro | c83df1a | 2018-09-11 15:59:02 +0900 | [diff] [blame] | 1361 | int ret = -1; |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1362 | char *filename_copy, *parent, *basename; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1363 | char l_filename[VFAT_MAXLEN_BYTES]; |
| 1364 | |
AKASHI Takahiro | ed8b1e4 | 2018-09-11 15:59:05 +0900 | [diff] [blame] | 1365 | debug("writing %s\n", filename); |
| 1366 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1367 | filename_copy = strdup(filename); |
| 1368 | if (!filename_copy) |
| 1369 | return -ENOMEM; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1370 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1371 | split_filename(filename_copy, &parent, &basename); |
| 1372 | if (!strlen(basename)) { |
| 1373 | ret = -EINVAL; |
| 1374 | goto exit; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
Heinrich Schuchardt | 502cb89 | 2021-01-30 10:01:08 +0100 | [diff] [blame] | 1377 | if (normalize_longname(l_filename, basename)) { |
| 1378 | printf("FAT: illegal filename (%s)\n", basename); |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1379 | ret = -EINVAL; |
| 1380 | goto exit; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1383 | itr = malloc_cache_aligned(sizeof(fat_itr)); |
| 1384 | if (!itr) { |
| 1385 | ret = -ENOMEM; |
| 1386 | goto exit; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1389 | ret = fat_itr_root(itr, &datablock); |
| 1390 | if (ret) |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1391 | goto exit; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1392 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 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); |
AKASHI Takahiro | c83df1a | 2018-09-11 15:59:02 +0900 | [diff] [blame] | 1398 | goto exit; |
| 1399 | } |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1400 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1401 | retdent = find_directory_entry(itr, l_filename); |
| 1402 | |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1403 | if (retdent) { |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1404 | if (fat_itr_isdir(itr)) { |
| 1405 | ret = -EISDIR; |
| 1406 | goto exit; |
| 1407 | } |
| 1408 | |
AKASHI Takahiro | b07f704 | 2018-09-11 15:59:06 +0900 | [diff] [blame] | 1409 | /* 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 Takahiro | ed8b1e4 | 2018-09-11 15:59:05 +0900 | [diff] [blame] | 1419 | /* Update file size in a directory entry */ |
| 1420 | retdent->size = cpu_to_le32(pos + size); |
Heinrich Schuchardt | cab43b0 | 2024-04-09 22:06:07 +0200 | [diff] [blame] | 1421 | /* Update change date */ |
| 1422 | dentry_set_time(retdent); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1423 | } else { |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1424 | /* Create a new file */ |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 1425 | char shortname[SHORT_NAME_SIZE]; |
Heinrich Schuchardt | 524d13a | 2020-11-22 11:54:22 +0100 | [diff] [blame] | 1426 | int ndent; |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1427 | |
AKASHI Takahiro | b07f704 | 2018-09-11 15:59:06 +0900 | [diff] [blame] | 1428 | if (pos) { |
| 1429 | /* No hole allowed */ |
| 1430 | ret = -EINVAL; |
| 1431 | goto exit; |
| 1432 | } |
| 1433 | |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 1434 | /* Check if long name is needed */ |
Heinrich Schuchardt | 502cb89 | 2021-01-30 10:01:08 +0100 | [diff] [blame] | 1435 | ndent = set_name(itr, basename, shortname); |
Heinrich Schuchardt | 524d13a | 2020-11-22 11:54:22 +0100 | [diff] [blame] | 1436 | if (ndent < 0) { |
| 1437 | ret = ndent; |
| 1438 | goto exit; |
| 1439 | } |
| 1440 | ret = fat_find_empty_dentries(itr, ndent); |
| 1441 | if (ret) |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1442 | goto exit; |
Heinrich Schuchardt | 524d13a | 2020-11-22 11:54:22 +0100 | [diff] [blame] | 1443 | if (ndent > 1) { |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 1444 | /* Set long name entries */ |
Heinrich Schuchardt | 502cb89 | 2021-01-30 10:01:08 +0100 | [diff] [blame] | 1445 | ret = fill_dir_slot(itr, basename, shortname); |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 1446 | if (ret) |
| 1447 | goto exit; |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1448 | } |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1449 | |
AKASHI Takahiro | a2c0bd0 | 2019-05-24 14:10:36 +0900 | [diff] [blame] | 1450 | /* Set short name entry */ |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 1451 | fill_dentry(itr->fsdata, itr->dent, shortname, 0, size, |
Heinrich Schuchardt | 881c74a | 2020-11-22 11:13:33 +0100 | [diff] [blame] | 1452 | ATTR_ARCH); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1453 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1454 | retdent = itr->dent; |
Benoît Thébaudeau | d1d390a | 2015-09-28 15:45:31 +0200 | [diff] [blame] | 1455 | } |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1456 | |
AKASHI Takahiro | ed8b1e4 | 2018-09-11 15:59:05 +0900 | [diff] [blame] | 1457 | ret = set_contents(mydata, retdent, pos, buffer, size, actwrite); |
Benoît Thébaudeau | d1d390a | 2015-09-28 15:45:31 +0200 | [diff] [blame] | 1458 | if (ret < 0) { |
| 1459 | printf("Error: writing contents\n"); |
AKASHI Takahiro | 6aa7769 | 2018-09-11 15:59:03 +0900 | [diff] [blame] | 1460 | ret = -EIO; |
Benoît Thébaudeau | d1d390a | 2015-09-28 15:45:31 +0200 | [diff] [blame] | 1461 | goto exit; |
| 1462 | } |
| 1463 | debug("attempt to write 0x%llx bytes\n", *actwrite); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1464 | |
Benoît Thébaudeau | d1d390a | 2015-09-28 15:45:31 +0200 | [diff] [blame] | 1465 | /* Flush fat buffer */ |
Stefan Brüns | 751b31d | 2016-09-11 22:51:40 +0200 | [diff] [blame] | 1466 | ret = flush_dirty_fat_buffer(mydata); |
Benoît Thébaudeau | d1d390a | 2015-09-28 15:45:31 +0200 | [diff] [blame] | 1467 | if (ret) { |
| 1468 | printf("Error: flush fat buffer\n"); |
AKASHI Takahiro | 6aa7769 | 2018-09-11 15:59:03 +0900 | [diff] [blame] | 1469 | ret = -EIO; |
Benoît Thébaudeau | d1d390a | 2015-09-28 15:45:31 +0200 | [diff] [blame] | 1470 | goto exit; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
Benoît Thébaudeau | d1d390a | 2015-09-28 15:45:31 +0200 | [diff] [blame] | 1473 | /* Write directory table to device */ |
AKASHI Takahiro | d98c674 | 2019-05-24 14:10:35 +0900 | [diff] [blame] | 1474 | ret = flush_dir(itr); |
Benoît Thébaudeau | d1d390a | 2015-09-28 15:45:31 +0200 | [diff] [blame] | 1475 | |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1476 | exit: |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1477 | free(filename_copy); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1478 | free(mydata->fatbuf); |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1479 | free(itr); |
Suriyan Ramasami | 441c223 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1480 | return ret; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
Suriyan Ramasami | 441c223 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1483 | int file_fat_write(const char *filename, void *buffer, loff_t offset, |
| 1484 | loff_t maxsize, loff_t *actwrite) |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1485 | { |
AKASHI Takahiro | ed8b1e4 | 2018-09-11 15:59:05 +0900 | [diff] [blame] | 1486 | return file_fat_write_at(filename, offset, buffer, maxsize, actwrite); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1487 | } |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 1488 | |
AKASHI Takahiro | 73b3497 | 2018-09-11 15:59:14 +0900 | [diff] [blame] | 1489 | static 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 | |
| 1520 | exit: |
| 1521 | free(fsdata.fatbuf); |
| 1522 | free(dirs); |
| 1523 | return count; |
| 1524 | } |
| 1525 | |
Heinrich Schuchardt | 284e85b | 2020-11-19 07:31:18 +0100 | [diff] [blame] | 1526 | /** |
| 1527 | * delete_single_dentry() - delete a single directory entry |
| 1528 | * |
| 1529 | * @itr: directory iterator |
| 1530 | * Return: 0 for success |
| 1531 | */ |
| 1532 | static int delete_single_dentry(fat_itr *itr) |
| 1533 | { |
| 1534 | struct dir_entry *dent = itr->dent; |
| 1535 | |
| 1536 | memset(dent, 0, sizeof(*dent)); |
Heinrich Schuchardt | 66ea58c | 2021-01-21 00:23:33 +0100 | [diff] [blame] | 1537 | dent->nameext.name[0] = DELETED_FLAG; |
Heinrich Schuchardt | 284e85b | 2020-11-19 07:31:18 +0100 | [diff] [blame] | 1538 | |
Heinrich Schuchardt | 913339a | 2021-01-20 22:21:53 +0100 | [diff] [blame] | 1539 | if (!itr->remaining) |
| 1540 | return flush_dir(itr); |
Heinrich Schuchardt | 284e85b | 2020-11-19 07:31:18 +0100 | [diff] [blame] | 1541 | 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 | */ |
| 1550 | static int delete_long_name(fat_itr *itr) |
| 1551 | { |
Heinrich Schuchardt | 66ea58c | 2021-01-21 00:23:33 +0100 | [diff] [blame] | 1552 | int seqn = itr->dent->nameext.name[0] & ~LAST_LONG_ENTRY_MASK; |
Heinrich Schuchardt | 284e85b | 2020-11-19 07:31:18 +0100 | [diff] [blame] | 1553 | |
| 1554 | while (seqn--) { |
Heinrich Schuchardt | eb3587b | 2021-01-26 00:14:14 +0100 | [diff] [blame] | 1555 | struct dir_entry *dent; |
Heinrich Schuchardt | 284e85b | 2020-11-19 07:31:18 +0100 | [diff] [blame] | 1556 | 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 | */ |
| 1574 | static int delete_dentry_long(fat_itr *itr) |
AKASHI Takahiro | 73b3497 | 2018-09-11 15:59:14 +0900 | [diff] [blame] | 1575 | { |
| 1576 | fsdata *mydata = itr->fsdata; |
Heinrich Schuchardt | 284e85b | 2020-11-19 07:31:18 +0100 | [diff] [blame] | 1577 | dir_entry *dent = itr->dent; |
AKASHI Takahiro | 73b3497 | 2018-09-11 15:59:14 +0900 | [diff] [blame] | 1578 | |
| 1579 | /* free cluster blocks */ |
Heinrich Schuchardt | 284e85b | 2020-11-19 07:31:18 +0100 | [diff] [blame] | 1580 | clear_fatent(mydata, START(dent)); |
AKASHI Takahiro | 73b3497 | 2018-09-11 15:59:14 +0900 | [diff] [blame] | 1581 | if (flush_dirty_fat_buffer(mydata) < 0) { |
| 1582 | printf("Error: flush fat buffer\n"); |
| 1583 | return -EIO; |
| 1584 | } |
Heinrich Schuchardt | 284e85b | 2020-11-19 07:31:18 +0100 | [diff] [blame] | 1585 | /* Position to first directory entry for long name */ |
| 1586 | if (itr->clust != itr->dent_clust) { |
| 1587 | int ret; |
AKASHI Takahiro | 73b3497 | 2018-09-11 15:59:14 +0900 | [diff] [blame] | 1588 | |
Heinrich Schuchardt | 284e85b | 2020-11-19 07:31:18 +0100 | [diff] [blame] | 1589 | 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 Schuchardt | 66ea58c | 2021-01-21 00:23:33 +0100 | [diff] [blame] | 1598 | (dent->nameext.name[0] & LAST_LONG_ENTRY_MASK)) { |
Heinrich Schuchardt | 284e85b | 2020-11-19 07:31:18 +0100 | [diff] [blame] | 1599 | int ret; |
AKASHI Takahiro | 73b3497 | 2018-09-11 15:59:14 +0900 | [diff] [blame] | 1600 | |
Heinrich Schuchardt | 284e85b | 2020-11-19 07:31:18 +0100 | [diff] [blame] | 1601 | ret = delete_long_name(itr); |
| 1602 | if (ret) |
| 1603 | return ret; |
| 1604 | } |
| 1605 | /* Delete short name */ |
| 1606 | delete_single_dentry(itr); |
Heinrich Schuchardt | 913339a | 2021-01-20 22:21:53 +0100 | [diff] [blame] | 1607 | return flush_dir(itr); |
AKASHI Takahiro | 73b3497 | 2018-09-11 15:59:14 +0900 | [diff] [blame] | 1608 | } |
| 1609 | |
| 1610 | int 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 Glass | 4745981 | 2023-07-15 21:39:06 -0600 | [diff] [blame] | 1618 | itr = malloc_cache_aligned(sizeof(fat_itr)); |
| 1619 | if (!itr || !filename_copy) { |
| 1620 | printf("Error: out of memory\n"); |
Heinrich Schuchardt | b2242f6 | 2018-10-02 06:58:00 +0200 | [diff] [blame] | 1621 | ret = -ENOMEM; |
| 1622 | goto exit; |
| 1623 | } |
AKASHI Takahiro | 73b3497 | 2018-09-11 15:59:14 +0900 | [diff] [blame] | 1624 | 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 Takahiro | 73b3497 | 2018-09-11 15:59:14 +0900 | [diff] [blame] | 1632 | 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 Glass | 4745981 | 2023-07-15 21:39:06 -0600 | [diff] [blame] | 1646 | log_err("%s: doesn't exist (%d)\n", basename, -ENOENT); |
AKASHI Takahiro | 73b3497 | 2018-09-11 15:59:14 +0900 | [diff] [blame] | 1647 | 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 Schuchardt | 284e85b | 2020-11-19 07:31:18 +0100 | [diff] [blame] | 1665 | ret = delete_dentry_long(itr); |
AKASHI Takahiro | 73b3497 | 2018-09-11 15:59:14 +0900 | [diff] [blame] | 1666 | |
| 1667 | exit: |
| 1668 | free(fsdata.fatbuf); |
| 1669 | free(itr); |
| 1670 | free(filename_copy); |
| 1671 | |
| 1672 | return ret; |
| 1673 | } |
| 1674 | |
Heinrich Schuchardt | 502cb89 | 2021-01-30 10:01:08 +0100 | [diff] [blame] | 1675 | int fat_mkdir(const char *dirname) |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 1676 | { |
| 1677 | dir_entry *retdent; |
| 1678 | fsdata datablock = { .fatbuf = NULL, }; |
| 1679 | fsdata *mydata = &datablock; |
| 1680 | fat_itr *itr = NULL; |
Heinrich Schuchardt | 502cb89 | 2021-01-30 10:01:08 +0100 | [diff] [blame] | 1681 | char *dirname_copy, *parent, *basename; |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 1682 | 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 Schuchardt | 502cb89 | 2021-01-30 10:01:08 +0100 | [diff] [blame] | 1688 | dirname_copy = strdup(dirname); |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 1689 | if (!dirname_copy) |
| 1690 | goto exit; |
| 1691 | |
Heinrich Schuchardt | 502cb89 | 2021-01-30 10:01:08 +0100 | [diff] [blame] | 1692 | split_filename(dirname_copy, &parent, &basename); |
| 1693 | if (!strlen(basename)) { |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 1694 | ret = -EINVAL; |
| 1695 | goto exit; |
| 1696 | } |
| 1697 | |
Heinrich Schuchardt | 502cb89 | 2021-01-30 10:01:08 +0100 | [diff] [blame] | 1698 | if (normalize_longname(l_dirname, basename)) { |
| 1699 | printf("FAT: illegal filename (%s)\n", basename); |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 1700 | 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 Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 1729 | char shortname[SHORT_NAME_SIZE]; |
Heinrich Schuchardt | 524d13a | 2020-11-22 11:54:22 +0100 | [diff] [blame] | 1730 | int ndent; |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 1731 | |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 1732 | 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 Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 1741 | /* Check if long name is needed */ |
Heinrich Schuchardt | 502cb89 | 2021-01-30 10:01:08 +0100 | [diff] [blame] | 1742 | ndent = set_name(itr, basename, shortname); |
Heinrich Schuchardt | 524d13a | 2020-11-22 11:54:22 +0100 | [diff] [blame] | 1743 | if (ndent < 0) { |
| 1744 | ret = ndent; |
| 1745 | goto exit; |
| 1746 | } |
| 1747 | ret = fat_find_empty_dentries(itr, ndent); |
| 1748 | if (ret) |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 1749 | goto exit; |
Heinrich Schuchardt | 524d13a | 2020-11-22 11:54:22 +0100 | [diff] [blame] | 1750 | if (ndent > 1) { |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 1751 | /* Set long name entries */ |
Heinrich Schuchardt | 502cb89 | 2021-01-30 10:01:08 +0100 | [diff] [blame] | 1752 | ret = fill_dir_slot(itr, basename, shortname); |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 1753 | if (ret) |
| 1754 | goto exit; |
| 1755 | } |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 1756 | |
| 1757 | /* Set attribute as archive for regular file */ |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 1758 | fill_dentry(itr->fsdata, itr->dent, shortname, 0, 0, |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 1759 | 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 Schuchardt | 66ea58c | 2021-01-21 00:23:33 +0100 | [diff] [blame] | 1773 | memcpy(&dotdent[0].nameext, ". ", 11); |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 1774 | dotdent[0].attr = ATTR_DIR | ATTR_ARCH; |
| 1775 | |
Heinrich Schuchardt | 66ea58c | 2021-01-21 00:23:33 +0100 | [diff] [blame] | 1776 | memcpy(&dotdent[1].nameext, ".. ", 11); |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 1777 | dotdent[1].attr = ATTR_DIR | ATTR_ARCH; |
Heinrich Schuchardt | 00cf076 | 2020-11-24 21:04:07 +0100 | [diff] [blame] | 1778 | |
| 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 Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 1783 | |
| 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 Schuchardt | 913339a | 2021-01-20 22:21:53 +0100 | [diff] [blame] | 1803 | ret = -EIO; |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 1804 | goto exit; |
| 1805 | } |
| 1806 | |
| 1807 | /* Write directory table to device */ |
AKASHI Takahiro | d98c674 | 2019-05-24 14:10:35 +0900 | [diff] [blame] | 1808 | ret = flush_dir(itr); |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 1809 | |
| 1810 | exit: |
| 1811 | free(dirname_copy); |
| 1812 | free(mydata->fatbuf); |
| 1813 | free(itr); |
| 1814 | free(dotdent); |
| 1815 | return ret; |
| 1816 | } |