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 <command.h> |
| 11 | #include <config.h> |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 12 | #include <div64.h> |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 13 | #include <fat.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 15 | #include <malloc.h> |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 16 | #include <part.h> |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 17 | #include <rand.h> |
| 18 | #include <asm/byteorder.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 19 | #include <asm/cache.h> |
Heinrich Schuchardt | cab43b0 | 2024-04-09 22:06:07 +0200 | [diff] [blame] | 20 | #include <dm/uclass.h> |
Richard Genoud | 2e37202 | 2012-12-13 00:47:36 +0000 | [diff] [blame] | 21 | #include <linux/ctype.h> |
Tom Rini | a17b7bc | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 22 | #include <linux/math64.h> |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 23 | #include "fat.c" |
| 24 | |
Heinrich Schuchardt | 24977e5 | 2020-11-25 16:33:55 +0100 | [diff] [blame] | 25 | static dir_entry *find_directory_entry(fat_itr *itr, char *filename); |
Heinrich Schuchardt | 5bc30a5 | 2020-11-22 09:58:44 +0100 | [diff] [blame] | 26 | static int new_dir_table(fat_itr *itr); |
Heinrich Schuchardt | 24977e5 | 2020-11-25 16:33:55 +0100 | [diff] [blame] | 27 | |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 28 | /* Characters that may only be used in long file names */ |
| 29 | static const char LONG_ONLY_CHARS[] = "+,;=[]"; |
| 30 | |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 31 | /* Combined size of the name and ext fields in the directory entry */ |
| 32 | #define SHORT_NAME_SIZE 11 |
| 33 | |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 34 | /** |
| 35 | * str2fat() - convert string to valid FAT name characters |
| 36 | * |
| 37 | * Stop when reaching end of @src or a period. |
| 38 | * Ignore spaces. |
| 39 | * Replace characters that may only be used in long names by underscores. |
| 40 | * Convert lower case characters to upper case. |
| 41 | * |
| 42 | * To avoid assumptions about the code page we do not use characters |
| 43 | * above 0x7f for the short name. |
| 44 | * |
| 45 | * @dest: destination buffer |
| 46 | * @src: source buffer |
| 47 | * @length: size of destination buffer |
| 48 | * Return: number of bytes in destination buffer |
| 49 | */ |
| 50 | static int str2fat(char *dest, char *src, int length) |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 51 | { |
| 52 | int i; |
| 53 | |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 54 | for (i = 0; i < length; ++src) { |
| 55 | char c = *src; |
| 56 | |
| 57 | if (!c || c == '.') |
| 58 | break; |
| 59 | if (c == ' ') |
| 60 | continue; |
| 61 | if (strchr(LONG_ONLY_CHARS, c) || c > 0x7f) |
| 62 | c = '_'; |
| 63 | else if (c >= 'a' && c <= 'z') |
| 64 | c &= 0xdf; |
| 65 | dest[i] = c; |
| 66 | ++i; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 67 | } |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 68 | return i; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 71 | /** |
Heinrich Schuchardt | 24977e5 | 2020-11-25 16:33:55 +0100 | [diff] [blame] | 72 | * fat_move_to_cluster() - position to first directory entry in cluster |
| 73 | * |
| 74 | * @itr: directory iterator |
| 75 | * @cluster cluster |
| 76 | * Return: 0 for success, -EIO on error |
| 77 | */ |
| 78 | static int fat_move_to_cluster(fat_itr *itr, unsigned int cluster) |
| 79 | { |
| 80 | unsigned int nbytes; |
| 81 | |
| 82 | /* position to the start of the directory */ |
| 83 | itr->next_clust = cluster; |
| 84 | itr->last_cluster = 0; |
| 85 | if (!fat_next_cluster(itr, &nbytes)) |
| 86 | return -EIO; |
| 87 | itr->dent = (dir_entry *)itr->block; |
| 88 | itr->remaining = nbytes / sizeof(dir_entry) - 1; |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | /** |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 93 | * set_name() - set short name in directory entry |
| 94 | * |
| 95 | * The function determines if the @filename is a valid short name. |
| 96 | * In this case no long name is needed. |
| 97 | * |
| 98 | * If a long name is needed, a short name is constructed. |
| 99 | * |
Heinrich Schuchardt | 24977e5 | 2020-11-25 16:33:55 +0100 | [diff] [blame] | 100 | * @itr: directory iterator |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 101 | * @filename: long file name |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 102 | * @shortname: buffer of 11 bytes to receive chosen short name and extension |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 103 | * Return: number of directory entries needed, negative on error |
| 104 | */ |
Heinrich Schuchardt | 24977e5 | 2020-11-25 16:33:55 +0100 | [diff] [blame] | 105 | static int set_name(fat_itr *itr, const char *filename, char *shortname) |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 106 | { |
| 107 | char *period; |
| 108 | char *pos; |
| 109 | int period_location; |
| 110 | char buf[13]; |
| 111 | int i; |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 112 | int ret; |
Heinrich Schuchardt | cfb7c2b | 2021-01-26 00:04:19 +0100 | [diff] [blame] | 113 | struct nameext dirent; |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 114 | |
| 115 | if (!filename) |
| 116 | return -EIO; |
| 117 | |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 118 | /* Initialize buffer */ |
| 119 | memset(&dirent, ' ', sizeof(dirent)); |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 120 | |
| 121 | /* Convert filename to upper case short name */ |
| 122 | period = strrchr(filename, '.'); |
| 123 | pos = (char *)filename; |
| 124 | if (*pos == '.') { |
| 125 | pos = period + 1; |
| 126 | period = 0; |
| 127 | } |
| 128 | if (period) |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 129 | str2fat(dirent.ext, period + 1, sizeof(dirent.ext)); |
| 130 | period_location = str2fat(dirent.name, pos, sizeof(dirent.name)); |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 131 | if (period_location < 0) |
| 132 | return period_location; |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 133 | if (*dirent.name == ' ') |
| 134 | *dirent.name = '_'; |
Heinrich Schuchardt | d701a22 | 2023-07-26 10:33:13 +0200 | [diff] [blame] | 135 | /* Substitute character 0xe5 signaling deletetion by character 0x05 */ |
| 136 | if (*dirent.name == DELETED_FLAG) |
| 137 | *dirent.name = aRING; |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 138 | |
| 139 | /* If filename and short name are the same, quit. */ |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 140 | sprintf(buf, "%.*s.%.3s", period_location, dirent.name, dirent.ext); |
| 141 | if (!strcmp(buf, filename)) { |
| 142 | ret = 1; |
| 143 | goto out; |
Stefan Herbrechtsmeier | 4052d1f | 2023-03-17 13:04:13 +0100 | [diff] [blame] | 144 | } else if (!strcasecmp(buf, filename)) { |
| 145 | goto out_ret; |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 146 | } |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 147 | |
| 148 | /* Construct an indexed short name */ |
| 149 | for (i = 1; i < 0x200000; ++i) { |
| 150 | int suffix_len; |
| 151 | int suffix_start; |
| 152 | int j; |
| 153 | |
| 154 | /* To speed up the search use random numbers */ |
| 155 | if (i < 10) { |
| 156 | j = i; |
| 157 | } else { |
| 158 | j = 30 - fls(i); |
| 159 | j = 10 + (rand() >> j); |
| 160 | } |
| 161 | sprintf(buf, "~%d", j); |
| 162 | suffix_len = strlen(buf); |
| 163 | suffix_start = 8 - suffix_len; |
| 164 | if (suffix_start > period_location) |
| 165 | suffix_start = period_location; |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 166 | memcpy(dirent.name + suffix_start, buf, suffix_len); |
| 167 | if (*dirent.ext != ' ') |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 168 | sprintf(buf, "%.*s.%.3s", suffix_start + suffix_len, |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 169 | dirent.name, dirent.ext); |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 170 | else |
| 171 | sprintf(buf, "%.*s", suffix_start + suffix_len, |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 172 | dirent.name); |
Heinrich Schuchardt | 24977e5 | 2020-11-25 16:33:55 +0100 | [diff] [blame] | 173 | debug("generated short name: %s\n", buf); |
| 174 | |
| 175 | /* Check that the short name does not exist yet. */ |
| 176 | ret = fat_move_to_cluster(itr, itr->start_clust); |
| 177 | if (ret) |
| 178 | return ret; |
| 179 | if (find_directory_entry(itr, buf)) |
| 180 | continue; |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 181 | |
Stefan Herbrechtsmeier | 4052d1f | 2023-03-17 13:04:13 +0100 | [diff] [blame] | 182 | goto out_ret; |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 183 | } |
| 184 | return -EIO; |
Stefan Herbrechtsmeier | 4052d1f | 2023-03-17 13:04:13 +0100 | [diff] [blame] | 185 | out_ret: |
| 186 | debug("chosen short name: %s\n", buf); |
| 187 | /* Each long name directory entry takes 13 characters. */ |
| 188 | ret = (strlen(filename) + 25) / 13; |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 189 | out: |
Heinrich Schuchardt | cfb7c2b | 2021-01-26 00:04:19 +0100 | [diff] [blame] | 190 | memcpy(shortname, &dirent, SHORT_NAME_SIZE); |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 191 | return ret; |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 194 | static int total_sector; |
Donggeun Kim | cb5f3bc | 2012-03-22 04:38:55 +0000 | [diff] [blame] | 195 | static int disk_write(__u32 block, __u32 nr_blocks, void *buf) |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 196 | { |
Łukasz Majewski | d6f23d8 | 2015-09-03 14:21:39 +0200 | [diff] [blame] | 197 | ulong ret; |
| 198 | |
Simon Glass | 2ee8ada | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 199 | if (!cur_dev) |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 200 | return -1; |
| 201 | |
Donggeun Kim | cb5f3bc | 2012-03-22 04:38:55 +0000 | [diff] [blame] | 202 | if (cur_part_info.start + block + nr_blocks > |
| 203 | cur_part_info.start + total_sector) { |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 204 | printf("error: overflow occurs\n"); |
| 205 | return -1; |
| 206 | } |
| 207 | |
Simon Glass | 2ee8ada | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 208 | 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] | 209 | if (nr_blocks && ret == 0) |
| 210 | return -1; |
| 211 | |
| 212 | return ret; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 215 | /* |
| 216 | * Write fat buffer into block device |
| 217 | */ |
Stefan Brüns | 751b31d | 2016-09-11 22:51:40 +0200 | [diff] [blame] | 218 | static int flush_dirty_fat_buffer(fsdata *mydata) |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 219 | { |
| 220 | int getsize = FATBUFBLOCKS; |
| 221 | __u32 fatlength = mydata->fatlength; |
| 222 | __u8 *bufptr = mydata->fatbuf; |
| 223 | __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS; |
| 224 | |
Stefan Brüns | 751b31d | 2016-09-11 22:51:40 +0200 | [diff] [blame] | 225 | debug("debug: evicting %d, dirty: %d\n", mydata->fatbufnum, |
| 226 | (int)mydata->fat_dirty); |
| 227 | |
| 228 | if ((!mydata->fat_dirty) || (mydata->fatbufnum == -1)) |
| 229 | return 0; |
| 230 | |
Stefan Brüns | 58bf86f | 2016-12-17 00:27:50 +0100 | [diff] [blame] | 231 | /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */ |
| 232 | if (startblock + getsize > fatlength) |
| 233 | getsize = fatlength - startblock; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 234 | |
Stefan Brüns | 58bf86f | 2016-12-17 00:27:50 +0100 | [diff] [blame] | 235 | startblock += mydata->fat_sect; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 236 | |
| 237 | /* Write FAT buf */ |
| 238 | if (disk_write(startblock, getsize, bufptr) < 0) { |
| 239 | debug("error: writing FAT blocks\n"); |
| 240 | return -1; |
| 241 | } |
| 242 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 243 | if (mydata->fats == 2) { |
Donggeun Kim | af57ce2 | 2011-12-20 18:34:27 +0000 | [diff] [blame] | 244 | /* Update corresponding second FAT blocks */ |
| 245 | startblock += mydata->fatlength; |
| 246 | if (disk_write(startblock, getsize, bufptr) < 0) { |
| 247 | debug("error: writing second FAT blocks\n"); |
| 248 | return -1; |
| 249 | } |
| 250 | } |
Stefan Brüns | 751b31d | 2016-09-11 22:51:40 +0200 | [diff] [blame] | 251 | mydata->fat_dirty = 0; |
Donggeun Kim | af57ce2 | 2011-12-20 18:34:27 +0000 | [diff] [blame] | 252 | |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 253 | return 0; |
| 254 | } |
| 255 | |
Heinrich Schuchardt | 5bc30a5 | 2020-11-22 09:58:44 +0100 | [diff] [blame] | 256 | /** |
| 257 | * fat_find_empty_dentries() - find a sequence of available directory entries |
| 258 | * |
| 259 | * @itr: directory iterator |
| 260 | * @count: number of directory entries to find |
| 261 | * Return: 0 on success or negative error number |
| 262 | */ |
Heinrich Schuchardt | 524d13a | 2020-11-22 11:54:22 +0100 | [diff] [blame] | 263 | static int fat_find_empty_dentries(fat_itr *itr, int count) |
Heinrich Schuchardt | 5bc30a5 | 2020-11-22 09:58:44 +0100 | [diff] [blame] | 264 | { |
| 265 | unsigned int cluster; |
| 266 | dir_entry *dent; |
| 267 | int remaining; |
| 268 | unsigned int n = 0; |
| 269 | int ret; |
| 270 | |
| 271 | ret = fat_move_to_cluster(itr, itr->start_clust); |
| 272 | if (ret) |
| 273 | return ret; |
| 274 | |
| 275 | for (;;) { |
| 276 | if (!itr->dent) { |
| 277 | log_debug("Not enough directory entries available\n"); |
| 278 | return -ENOSPC; |
| 279 | } |
Heinrich Schuchardt | 66ea58c | 2021-01-21 00:23:33 +0100 | [diff] [blame] | 280 | switch (itr->dent->nameext.name[0]) { |
Heinrich Schuchardt | 5bc30a5 | 2020-11-22 09:58:44 +0100 | [diff] [blame] | 281 | case 0x00: |
| 282 | case DELETED_FLAG: |
| 283 | if (!n) { |
| 284 | /* Remember first deleted directory entry */ |
| 285 | cluster = itr->clust; |
| 286 | dent = itr->dent; |
| 287 | remaining = itr->remaining; |
| 288 | } |
| 289 | ++n; |
| 290 | if (n == count) |
| 291 | goto out; |
| 292 | break; |
| 293 | default: |
| 294 | n = 0; |
| 295 | break; |
| 296 | } |
| 297 | |
| 298 | next_dent(itr); |
| 299 | if (!itr->dent && |
| 300 | (!itr->is_root || itr->fsdata->fatsize == 32) && |
| 301 | new_dir_table(itr)) |
| 302 | return -ENOSPC; |
| 303 | } |
| 304 | out: |
| 305 | /* Position back to first directory entry */ |
| 306 | if (itr->clust != cluster) { |
| 307 | ret = fat_move_to_cluster(itr, cluster); |
| 308 | if (ret) |
| 309 | return ret; |
| 310 | } |
| 311 | itr->dent = dent; |
| 312 | itr->remaining = remaining; |
| 313 | return 0; |
| 314 | } |
| 315 | |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 316 | /* |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 317 | * Set the file name information from 'name' into 'slotptr', |
| 318 | */ |
| 319 | static int str2slot(dir_slot *slotptr, const char *name, int *idx) |
| 320 | { |
| 321 | int j, end_idx = 0; |
| 322 | |
| 323 | for (j = 0; j <= 8; j += 2) { |
| 324 | if (name[*idx] == 0x00) { |
| 325 | slotptr->name0_4[j] = 0; |
| 326 | slotptr->name0_4[j + 1] = 0; |
| 327 | end_idx++; |
| 328 | goto name0_4; |
| 329 | } |
| 330 | slotptr->name0_4[j] = name[*idx]; |
| 331 | (*idx)++; |
| 332 | end_idx++; |
| 333 | } |
| 334 | for (j = 0; j <= 10; j += 2) { |
| 335 | if (name[*idx] == 0x00) { |
| 336 | slotptr->name5_10[j] = 0; |
| 337 | slotptr->name5_10[j + 1] = 0; |
| 338 | end_idx++; |
| 339 | goto name5_10; |
| 340 | } |
| 341 | slotptr->name5_10[j] = name[*idx]; |
| 342 | (*idx)++; |
| 343 | end_idx++; |
| 344 | } |
| 345 | for (j = 0; j <= 2; j += 2) { |
| 346 | if (name[*idx] == 0x00) { |
| 347 | slotptr->name11_12[j] = 0; |
| 348 | slotptr->name11_12[j + 1] = 0; |
| 349 | end_idx++; |
| 350 | goto name11_12; |
| 351 | } |
| 352 | slotptr->name11_12[j] = name[*idx]; |
| 353 | (*idx)++; |
| 354 | end_idx++; |
| 355 | } |
| 356 | |
| 357 | if (name[*idx] == 0x00) |
| 358 | return 1; |
| 359 | |
| 360 | return 0; |
| 361 | /* Not used characters are filled with 0xff 0xff */ |
| 362 | name0_4: |
| 363 | for (; end_idx < 5; end_idx++) { |
| 364 | slotptr->name0_4[end_idx * 2] = 0xff; |
| 365 | slotptr->name0_4[end_idx * 2 + 1] = 0xff; |
| 366 | } |
| 367 | end_idx = 5; |
| 368 | name5_10: |
| 369 | end_idx -= 5; |
| 370 | for (; end_idx < 6; end_idx++) { |
| 371 | slotptr->name5_10[end_idx * 2] = 0xff; |
| 372 | slotptr->name5_10[end_idx * 2 + 1] = 0xff; |
| 373 | } |
| 374 | end_idx = 11; |
| 375 | name11_12: |
| 376 | end_idx -= 11; |
| 377 | for (; end_idx < 2; end_idx++) { |
| 378 | slotptr->name11_12[end_idx * 2] = 0xff; |
| 379 | slotptr->name11_12[end_idx * 2 + 1] = 0xff; |
| 380 | } |
| 381 | |
| 382 | return 1; |
| 383 | } |
| 384 | |
AKASHI Takahiro | a2c0bd0 | 2019-05-24 14:10:36 +0900 | [diff] [blame] | 385 | static int flush_dir(fat_itr *itr); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 386 | |
Heinrich Schuchardt | ac4ab75 | 2020-11-21 08:32:50 +0100 | [diff] [blame] | 387 | /** |
| 388 | * fill_dir_slot() - fill directory entries for long name |
| 389 | * |
| 390 | * @itr: directory iterator |
| 391 | * @l_name: long name |
| 392 | * @shortname: short name |
| 393 | * Return: 0 for success, -errno otherwise |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 394 | */ |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 395 | static int |
Heinrich Schuchardt | ac4ab75 | 2020-11-21 08:32:50 +0100 | [diff] [blame] | 396 | 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] | 397 | { |
Tien Fong Chee | 0117dbe | 2016-07-27 23:08:56 -0700 | [diff] [blame] | 398 | __u8 temp_dir_slot_buffer[MAX_LFN_SLOT * sizeof(dir_slot)]; |
| 399 | dir_slot *slotptr = (dir_slot *)temp_dir_slot_buffer; |
Anatolij Gustschin | f4f9d5d | 2011-12-15 03:12:14 +0000 | [diff] [blame] | 400 | __u8 counter = 0, checksum; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 401 | int idx = 0, ret; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 402 | |
Stefan Brüns | 6c617d6 | 2016-09-11 22:51:39 +0200 | [diff] [blame] | 403 | /* Get short file name checksum value */ |
Heinrich Schuchardt | 66ea58c | 2021-01-21 00:23:33 +0100 | [diff] [blame] | 404 | checksum = mkcksum((void *)shortname); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 405 | |
| 406 | do { |
| 407 | memset(slotptr, 0x00, sizeof(dir_slot)); |
| 408 | ret = str2slot(slotptr, l_name, &idx); |
| 409 | slotptr->id = ++counter; |
| 410 | slotptr->attr = ATTR_VFAT; |
| 411 | slotptr->alias_checksum = checksum; |
| 412 | slotptr++; |
| 413 | } while (ret == 0); |
| 414 | |
| 415 | slotptr--; |
| 416 | slotptr->id |= LAST_LONG_ENTRY_MASK; |
| 417 | |
| 418 | while (counter >= 1) { |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 419 | memcpy(itr->dent, slotptr, sizeof(dir_slot)); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 420 | slotptr--; |
| 421 | counter--; |
AKASHI Takahiro | a2c0bd0 | 2019-05-24 14:10:36 +0900 | [diff] [blame] | 422 | |
Heinrich Schuchardt | 913339a | 2021-01-20 22:21:53 +0100 | [diff] [blame] | 423 | if (!itr->remaining) { |
| 424 | /* Write directory table to device */ |
| 425 | ret = flush_dir(itr); |
| 426 | if (ret) |
| 427 | return ret; |
| 428 | } |
AKASHI Takahiro | a2c0bd0 | 2019-05-24 14:10:36 +0900 | [diff] [blame] | 429 | |
Heinrich Schuchardt | 524d13a | 2020-11-22 11:54:22 +0100 | [diff] [blame] | 430 | next_dent(itr); |
| 431 | if (!itr->dent) |
| 432 | return -EIO; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 435 | return 0; |
| 436 | } |
| 437 | |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 438 | /* |
Philipp Skadorov | 16f553d | 2016-12-15 15:52:53 -0500 | [diff] [blame] | 439 | * 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] | 440 | */ |
| 441 | static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value) |
| 442 | { |
Philipp Skadorov | 16f553d | 2016-12-15 15:52:53 -0500 | [diff] [blame] | 443 | __u32 bufnum, offset, off16; |
| 444 | __u16 val1, val2; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 445 | |
| 446 | switch (mydata->fatsize) { |
| 447 | case 32: |
| 448 | bufnum = entry / FAT32BUFSIZE; |
| 449 | offset = entry - bufnum * FAT32BUFSIZE; |
| 450 | break; |
| 451 | case 16: |
| 452 | bufnum = entry / FAT16BUFSIZE; |
| 453 | offset = entry - bufnum * FAT16BUFSIZE; |
| 454 | break; |
Philipp Skadorov | 16f553d | 2016-12-15 15:52:53 -0500 | [diff] [blame] | 455 | case 12: |
| 456 | bufnum = entry / FAT12BUFSIZE; |
| 457 | offset = entry - bufnum * FAT12BUFSIZE; |
| 458 | break; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 459 | default: |
| 460 | /* Unsupported FAT size */ |
| 461 | return -1; |
| 462 | } |
| 463 | |
| 464 | /* Read a new block of FAT entries into the cache. */ |
| 465 | if (bufnum != mydata->fatbufnum) { |
| 466 | int getsize = FATBUFBLOCKS; |
| 467 | __u8 *bufptr = mydata->fatbuf; |
| 468 | __u32 fatlength = mydata->fatlength; |
| 469 | __u32 startblock = bufnum * FATBUFBLOCKS; |
| 470 | |
Stefan Brüns | 58bf86f | 2016-12-17 00:27:50 +0100 | [diff] [blame] | 471 | /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */ |
| 472 | if (startblock + getsize > fatlength) |
| 473 | getsize = fatlength - startblock; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 474 | |
Stefan Brüns | 751b31d | 2016-09-11 22:51:40 +0200 | [diff] [blame] | 475 | if (flush_dirty_fat_buffer(mydata) < 0) |
| 476 | return -1; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 477 | |
Stefan Brüns | 58bf86f | 2016-12-17 00:27:50 +0100 | [diff] [blame] | 478 | startblock += mydata->fat_sect; |
| 479 | |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 480 | if (disk_read(startblock, getsize, bufptr) < 0) { |
| 481 | debug("Error reading FAT blocks\n"); |
| 482 | return -1; |
| 483 | } |
| 484 | mydata->fatbufnum = bufnum; |
| 485 | } |
| 486 | |
Stefan Brüns | 751b31d | 2016-09-11 22:51:40 +0200 | [diff] [blame] | 487 | /* Mark as dirty */ |
| 488 | mydata->fat_dirty = 1; |
| 489 | |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 490 | /* Set the actual entry */ |
| 491 | switch (mydata->fatsize) { |
| 492 | case 32: |
| 493 | ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value); |
| 494 | break; |
| 495 | case 16: |
| 496 | ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value); |
| 497 | break; |
Philipp Skadorov | 16f553d | 2016-12-15 15:52:53 -0500 | [diff] [blame] | 498 | case 12: |
| 499 | off16 = (offset * 3) / 4; |
| 500 | |
| 501 | switch (offset & 0x3) { |
| 502 | case 0: |
| 503 | val1 = cpu_to_le16(entry_value) & 0xfff; |
| 504 | ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff; |
| 505 | ((__u16 *)mydata->fatbuf)[off16] |= val1; |
| 506 | break; |
| 507 | case 1: |
| 508 | val1 = cpu_to_le16(entry_value) & 0xf; |
| 509 | val2 = (cpu_to_le16(entry_value) >> 4) & 0xff; |
| 510 | |
| 511 | ((__u16 *)mydata->fatbuf)[off16] &= ~0xf000; |
| 512 | ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 12); |
| 513 | |
| 514 | ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xff; |
| 515 | ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2; |
| 516 | break; |
| 517 | case 2: |
| 518 | val1 = cpu_to_le16(entry_value) & 0xff; |
| 519 | val2 = (cpu_to_le16(entry_value) >> 8) & 0xf; |
| 520 | |
| 521 | ((__u16 *)mydata->fatbuf)[off16] &= ~0xff00; |
| 522 | ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 8); |
| 523 | |
| 524 | ((__u16 *)mydata->fatbuf)[off16 + 1] &= ~0xf; |
| 525 | ((__u16 *)mydata->fatbuf)[off16 + 1] |= val2; |
| 526 | break; |
| 527 | case 3: |
| 528 | val1 = cpu_to_le16(entry_value) & 0xfff; |
| 529 | ((__u16 *)mydata->fatbuf)[off16] &= ~0xfff0; |
| 530 | ((__u16 *)mydata->fatbuf)[off16] |= (val1 << 4); |
| 531 | break; |
| 532 | default: |
| 533 | break; |
| 534 | } |
| 535 | |
| 536 | break; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 537 | default: |
| 538 | return -1; |
| 539 | } |
| 540 | |
| 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | /* |
Philipp Skadorov | 16f553d | 2016-12-15 15:52:53 -0500 | [diff] [blame] | 545 | * 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] | 546 | * 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] | 547 | */ |
| 548 | static __u32 determine_fatent(fsdata *mydata, __u32 entry) |
| 549 | { |
| 550 | __u32 next_fat, next_entry = entry + 1; |
| 551 | |
| 552 | while (1) { |
Stefan Brüns | 9f95d81 | 2016-12-17 00:27:51 +0100 | [diff] [blame] | 553 | next_fat = get_fatent(mydata, next_entry); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 554 | if (next_fat == 0) { |
Stefan Brüns | 1499940 | 2016-09-11 22:51:41 +0200 | [diff] [blame] | 555 | /* found free entry, link to entry */ |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 556 | set_fatent_value(mydata, entry, next_entry); |
| 557 | break; |
| 558 | } |
| 559 | next_entry++; |
| 560 | } |
| 561 | debug("FAT%d: entry: %08x, entry_value: %04x\n", |
| 562 | mydata->fatsize, entry, next_entry); |
| 563 | |
| 564 | return next_entry; |
| 565 | } |
| 566 | |
Heinrich Schuchardt | 9fb8db4 | 2018-10-02 09:30:45 +0200 | [diff] [blame] | 567 | /** |
AKASHI Takahiro | d98c674 | 2019-05-24 14:10:35 +0900 | [diff] [blame] | 568 | * set_sectors() - write data to sectors |
Heinrich Schuchardt | 9fb8db4 | 2018-10-02 09:30:45 +0200 | [diff] [blame] | 569 | * |
AKASHI Takahiro | d98c674 | 2019-05-24 14:10:35 +0900 | [diff] [blame] | 570 | * Write 'size' bytes from 'buffer' into the specified sector. |
Heinrich Schuchardt | 9fb8db4 | 2018-10-02 09:30:45 +0200 | [diff] [blame] | 571 | * |
| 572 | * @mydata: data to be written |
AKASHI Takahiro | d98c674 | 2019-05-24 14:10:35 +0900 | [diff] [blame] | 573 | * @startsect: sector to be written to |
Heinrich Schuchardt | 9fb8db4 | 2018-10-02 09:30:45 +0200 | [diff] [blame] | 574 | * @buffer: data to be written |
| 575 | * @size: bytes to be written (but not more than the size of a cluster) |
| 576 | * Return: 0 on success, -1 otherwise |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 577 | */ |
| 578 | static int |
AKASHI Takahiro | d98c674 | 2019-05-24 14:10:35 +0900 | [diff] [blame] | 579 | set_sectors(fsdata *mydata, u32 startsect, u8 *buffer, u32 size) |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 580 | { |
Benoît Thébaudeau | ccc945b | 2015-09-28 15:45:28 +0200 | [diff] [blame] | 581 | int ret; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 582 | |
AKASHI Takahiro | d98c674 | 2019-05-24 14:10:35 +0900 | [diff] [blame] | 583 | debug("startsect: %d\n", startsect); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 584 | |
Benoît Thébaudeau | ccc945b | 2015-09-28 15:45:28 +0200 | [diff] [blame] | 585 | if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) { |
| 586 | ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size); |
| 587 | |
Heinrich Schuchardt | aa9d6ba | 2018-09-13 19:42:47 +0200 | [diff] [blame] | 588 | debug("FAT: Misaligned buffer address (%p)\n", buffer); |
Benoît Thébaudeau | ccc945b | 2015-09-28 15:45:28 +0200 | [diff] [blame] | 589 | |
| 590 | while (size >= mydata->sect_size) { |
| 591 | memcpy(tmpbuf, buffer, mydata->sect_size); |
| 592 | ret = disk_write(startsect++, 1, tmpbuf); |
| 593 | if (ret != 1) { |
| 594 | debug("Error writing data (got %d)\n", ret); |
| 595 | return -1; |
| 596 | } |
| 597 | |
| 598 | buffer += mydata->sect_size; |
| 599 | size -= mydata->sect_size; |
| 600 | } |
| 601 | } else if (size >= mydata->sect_size) { |
Heinrich Schuchardt | eb3587b | 2021-01-26 00:14:14 +0100 | [diff] [blame] | 602 | u32 nsects; |
| 603 | |
AKASHI Takahiro | d98c674 | 2019-05-24 14:10:35 +0900 | [diff] [blame] | 604 | nsects = size / mydata->sect_size; |
| 605 | ret = disk_write(startsect, nsects, buffer); |
| 606 | if (ret != nsects) { |
Benoît Thébaudeau | ccc945b | 2015-09-28 15:45:28 +0200 | [diff] [blame] | 607 | debug("Error writing data (got %d)\n", ret); |
Wu, Josh | b0e38dd | 2013-07-24 17:55:30 +0800 | [diff] [blame] | 608 | return -1; |
| 609 | } |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 610 | |
AKASHI Takahiro | d98c674 | 2019-05-24 14:10:35 +0900 | [diff] [blame] | 611 | startsect += nsects; |
| 612 | buffer += nsects * mydata->sect_size; |
| 613 | size -= nsects * mydata->sect_size; |
Benoît Thébaudeau | ccc945b | 2015-09-28 15:45:28 +0200 | [diff] [blame] | 614 | } |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 615 | |
Benoît Thébaudeau | ccc945b | 2015-09-28 15:45:28 +0200 | [diff] [blame] | 616 | if (size) { |
| 617 | ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size); |
Heinrich Schuchardt | 9fb8db4 | 2018-10-02 09:30:45 +0200 | [diff] [blame] | 618 | /* Do not leak content of stack */ |
| 619 | memset(tmpbuf, 0, mydata->sect_size); |
Benoît Thébaudeau | ccc945b | 2015-09-28 15:45:28 +0200 | [diff] [blame] | 620 | memcpy(tmpbuf, buffer, size); |
| 621 | ret = disk_write(startsect, 1, tmpbuf); |
| 622 | if (ret != 1) { |
| 623 | debug("Error writing data (got %d)\n", ret); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 624 | return -1; |
| 625 | } |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | return 0; |
| 629 | } |
| 630 | |
AKASHI Takahiro | d98c674 | 2019-05-24 14:10:35 +0900 | [diff] [blame] | 631 | /** |
| 632 | * set_cluster() - write data to cluster |
| 633 | * |
| 634 | * Write 'size' bytes from 'buffer' into the specified cluster. |
| 635 | * |
| 636 | * @mydata: data to be written |
| 637 | * @clustnum: cluster to be written to |
| 638 | * @buffer: data to be written |
| 639 | * @size: bytes to be written (but not more than the size of a cluster) |
| 640 | * Return: 0 on success, -1 otherwise |
| 641 | */ |
| 642 | static int |
| 643 | set_cluster(fsdata *mydata, u32 clustnum, u8 *buffer, u32 size) |
| 644 | { |
| 645 | return set_sectors(mydata, clust_to_sect(mydata, clustnum), |
| 646 | buffer, size); |
| 647 | } |
| 648 | |
Heinrich Schuchardt | 913339a | 2021-01-20 22:21:53 +0100 | [diff] [blame] | 649 | /** |
| 650 | * flush_dir() - flush directory |
| 651 | * |
| 652 | * @itr: directory iterator |
| 653 | * Return: 0 for success, -EIO on error |
| 654 | */ |
| 655 | static int flush_dir(fat_itr *itr) |
AKASHI Takahiro | d98c674 | 2019-05-24 14:10:35 +0900 | [diff] [blame] | 656 | { |
| 657 | fsdata *mydata = itr->fsdata; |
| 658 | u32 startsect, sect_offset, nsects; |
Heinrich Schuchardt | 913339a | 2021-01-20 22:21:53 +0100 | [diff] [blame] | 659 | int ret; |
AKASHI Takahiro | d98c674 | 2019-05-24 14:10:35 +0900 | [diff] [blame] | 660 | |
Heinrich Schuchardt | 913339a | 2021-01-20 22:21:53 +0100 | [diff] [blame] | 661 | if (!itr->is_root || mydata->fatsize == 32) { |
| 662 | ret = set_cluster(mydata, itr->clust, itr->block, |
| 663 | mydata->clust_size * mydata->sect_size); |
| 664 | goto out; |
| 665 | } |
AKASHI Takahiro | d98c674 | 2019-05-24 14:10:35 +0900 | [diff] [blame] | 666 | |
| 667 | sect_offset = itr->clust * mydata->clust_size; |
| 668 | startsect = mydata->rootdir_sect + sect_offset; |
| 669 | /* do not write past the end of rootdir */ |
| 670 | nsects = min_t(u32, mydata->clust_size, |
| 671 | mydata->rootdir_size - sect_offset); |
| 672 | |
Heinrich Schuchardt | 913339a | 2021-01-20 22:21:53 +0100 | [diff] [blame] | 673 | ret = set_sectors(mydata, startsect, itr->block, |
| 674 | nsects * mydata->sect_size); |
| 675 | out: |
| 676 | if (ret) { |
| 677 | log_err("Error: writing directory entry\n"); |
| 678 | return -EIO; |
| 679 | } |
| 680 | return 0; |
AKASHI Takahiro | d98c674 | 2019-05-24 14:10:35 +0900 | [diff] [blame] | 681 | } |
| 682 | |
AKASHI Takahiro | b07f704 | 2018-09-11 15:59:06 +0900 | [diff] [blame] | 683 | /* |
| 684 | * Read and modify data on existing and consecutive cluster blocks |
| 685 | */ |
| 686 | static int |
| 687 | get_set_cluster(fsdata *mydata, __u32 clustnum, loff_t pos, __u8 *buffer, |
| 688 | loff_t size, loff_t *gotsize) |
| 689 | { |
Heinrich Schuchardt | 8a3ac6e | 2020-07-06 07:48:14 +0200 | [diff] [blame] | 690 | static u8 *tmpbuf_cluster; |
AKASHI Takahiro | b07f704 | 2018-09-11 15:59:06 +0900 | [diff] [blame] | 691 | unsigned int bytesperclust = mydata->clust_size * mydata->sect_size; |
| 692 | __u32 startsect; |
Heinrich Schuchardt | f0be00e | 2023-07-30 16:44:04 +0200 | [diff] [blame] | 693 | loff_t clustcount, wsize; |
| 694 | int i, ret; |
AKASHI Takahiro | b07f704 | 2018-09-11 15:59:06 +0900 | [diff] [blame] | 695 | |
| 696 | *gotsize = 0; |
| 697 | if (!size) |
| 698 | return 0; |
| 699 | |
Heinrich Schuchardt | 8a3ac6e | 2020-07-06 07:48:14 +0200 | [diff] [blame] | 700 | if (!tmpbuf_cluster) { |
| 701 | tmpbuf_cluster = memalign(ARCH_DMA_MINALIGN, MAX_CLUSTSIZE); |
| 702 | if (!tmpbuf_cluster) |
| 703 | return -1; |
| 704 | } |
| 705 | |
AKASHI Takahiro | b07f704 | 2018-09-11 15:59:06 +0900 | [diff] [blame] | 706 | assert(pos < bytesperclust); |
| 707 | startsect = clust_to_sect(mydata, clustnum); |
| 708 | |
| 709 | debug("clustnum: %d, startsect: %d, pos: %lld\n", |
| 710 | clustnum, startsect, pos); |
| 711 | |
| 712 | /* partial write at beginning */ |
| 713 | if (pos) { |
| 714 | wsize = min(bytesperclust - pos, size); |
| 715 | ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster); |
| 716 | if (ret != mydata->clust_size) { |
| 717 | debug("Error reading data (got %d)\n", ret); |
| 718 | return -1; |
| 719 | } |
| 720 | |
| 721 | memcpy(tmpbuf_cluster + pos, buffer, wsize); |
| 722 | ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster); |
| 723 | if (ret != mydata->clust_size) { |
| 724 | debug("Error writing data (got %d)\n", ret); |
| 725 | return -1; |
| 726 | } |
| 727 | |
| 728 | size -= wsize; |
| 729 | buffer += wsize; |
| 730 | *gotsize += wsize; |
| 731 | |
| 732 | startsect += mydata->clust_size; |
| 733 | |
| 734 | if (!size) |
| 735 | return 0; |
| 736 | } |
| 737 | |
| 738 | /* full-cluster write */ |
| 739 | if (size >= bytesperclust) { |
| 740 | clustcount = lldiv(size, bytesperclust); |
| 741 | |
| 742 | if (!((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1))) { |
| 743 | wsize = clustcount * bytesperclust; |
| 744 | ret = disk_write(startsect, |
| 745 | clustcount * mydata->clust_size, |
| 746 | buffer); |
| 747 | if (ret != clustcount * mydata->clust_size) { |
| 748 | debug("Error writing data (got %d)\n", ret); |
| 749 | return -1; |
| 750 | } |
| 751 | |
| 752 | size -= wsize; |
| 753 | buffer += wsize; |
| 754 | *gotsize += wsize; |
| 755 | |
| 756 | startsect += clustcount * mydata->clust_size; |
| 757 | } else { |
| 758 | for (i = 0; i < clustcount; i++) { |
| 759 | memcpy(tmpbuf_cluster, buffer, bytesperclust); |
| 760 | ret = disk_write(startsect, |
| 761 | mydata->clust_size, |
| 762 | tmpbuf_cluster); |
| 763 | if (ret != mydata->clust_size) { |
| 764 | debug("Error writing data (got %d)\n", |
| 765 | ret); |
| 766 | return -1; |
| 767 | } |
| 768 | |
| 769 | size -= bytesperclust; |
| 770 | buffer += bytesperclust; |
| 771 | *gotsize += bytesperclust; |
| 772 | |
| 773 | startsect += mydata->clust_size; |
| 774 | } |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | /* partial write at end */ |
| 779 | if (size) { |
| 780 | wsize = size; |
| 781 | ret = disk_read(startsect, mydata->clust_size, tmpbuf_cluster); |
| 782 | if (ret != mydata->clust_size) { |
| 783 | debug("Error reading data (got %d)\n", ret); |
| 784 | return -1; |
| 785 | } |
| 786 | memcpy(tmpbuf_cluster, buffer, wsize); |
| 787 | ret = disk_write(startsect, mydata->clust_size, tmpbuf_cluster); |
| 788 | if (ret != mydata->clust_size) { |
| 789 | debug("Error writing data (got %d)\n", ret); |
| 790 | return -1; |
| 791 | } |
| 792 | |
| 793 | size -= wsize; |
AKASHI Takahiro | b07f704 | 2018-09-11 15:59:06 +0900 | [diff] [blame] | 794 | *gotsize += wsize; |
| 795 | } |
| 796 | |
| 797 | assert(!size); |
| 798 | |
| 799 | return 0; |
| 800 | } |
| 801 | |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 802 | /* |
| 803 | * Find the first empty cluster |
| 804 | */ |
| 805 | static int find_empty_cluster(fsdata *mydata) |
| 806 | { |
| 807 | __u32 fat_val, entry = 3; |
| 808 | |
| 809 | while (1) { |
Stefan Brüns | 9f95d81 | 2016-12-17 00:27:51 +0100 | [diff] [blame] | 810 | fat_val = get_fatent(mydata, entry); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 811 | if (fat_val == 0) |
| 812 | break; |
| 813 | entry++; |
| 814 | } |
| 815 | |
| 816 | return entry; |
| 817 | } |
| 818 | |
Heinrich Schuchardt | e09f3e4 | 2020-11-26 19:06:55 +0100 | [diff] [blame] | 819 | /** |
| 820 | * new_dir_table() - allocate a cluster for additional directory entries |
| 821 | * |
| 822 | * @itr: directory iterator |
| 823 | * Return: 0 on success, -EIO otherwise |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 824 | */ |
AKASHI Takahiro | a2c0bd0 | 2019-05-24 14:10:36 +0900 | [diff] [blame] | 825 | static int new_dir_table(fat_itr *itr) |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 826 | { |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 827 | fsdata *mydata = itr->fsdata; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 828 | int dir_newclust = 0; |
Heinrich Schuchardt | e09f3e4 | 2020-11-26 19:06:55 +0100 | [diff] [blame] | 829 | int dir_oldclust = itr->clust; |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 830 | unsigned int bytesperclust = mydata->clust_size * mydata->sect_size; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 831 | |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 832 | dir_newclust = find_empty_cluster(mydata); |
Heinrich Schuchardt | e09f3e4 | 2020-11-26 19:06:55 +0100 | [diff] [blame] | 833 | |
| 834 | /* |
| 835 | * Flush before updating FAT to ensure valid directory structure |
| 836 | * in case of failure. |
| 837 | */ |
| 838 | itr->clust = dir_newclust; |
| 839 | itr->next_clust = dir_newclust; |
| 840 | memset(itr->block, 0x00, bytesperclust); |
| 841 | if (flush_dir(itr)) |
| 842 | return -EIO; |
| 843 | |
| 844 | set_fatent_value(mydata, dir_oldclust, dir_newclust); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 845 | if (mydata->fatsize == 32) |
| 846 | set_fatent_value(mydata, dir_newclust, 0xffffff8); |
| 847 | else if (mydata->fatsize == 16) |
| 848 | set_fatent_value(mydata, dir_newclust, 0xfff8); |
Philipp Skadorov | 16f553d | 2016-12-15 15:52:53 -0500 | [diff] [blame] | 849 | else if (mydata->fatsize == 12) |
| 850 | set_fatent_value(mydata, dir_newclust, 0xff8); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 851 | |
Stefan Brüns | 751b31d | 2016-09-11 22:51:40 +0200 | [diff] [blame] | 852 | if (flush_dirty_fat_buffer(mydata) < 0) |
Heinrich Schuchardt | e09f3e4 | 2020-11-26 19:06:55 +0100 | [diff] [blame] | 853 | return -EIO; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 854 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 855 | itr->dent = (dir_entry *)itr->block; |
| 856 | itr->last_cluster = 1; |
| 857 | itr->remaining = bytesperclust / sizeof(dir_entry) - 1; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 858 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 859 | return 0; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | /* |
| 863 | * Set empty cluster from 'entry' to the end of a file |
| 864 | */ |
| 865 | static int clear_fatent(fsdata *mydata, __u32 entry) |
| 866 | { |
| 867 | __u32 fat_val; |
| 868 | |
Philipp Skadorov | 16f553d | 2016-12-15 15:52:53 -0500 | [diff] [blame] | 869 | while (!CHECK_CLUST(entry, mydata->fatsize)) { |
Stefan Brüns | 9f95d81 | 2016-12-17 00:27:51 +0100 | [diff] [blame] | 870 | fat_val = get_fatent(mydata, entry); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 871 | if (fat_val != 0) |
| 872 | set_fatent_value(mydata, entry, 0); |
| 873 | else |
| 874 | break; |
| 875 | |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 876 | entry = fat_val; |
| 877 | } |
| 878 | |
| 879 | /* Flush fat buffer */ |
Stefan Brüns | 751b31d | 2016-09-11 22:51:40 +0200 | [diff] [blame] | 880 | if (flush_dirty_fat_buffer(mydata) < 0) |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 881 | return -1; |
| 882 | |
| 883 | return 0; |
| 884 | } |
| 885 | |
| 886 | /* |
AKASHI Takahiro | ed8b1e4 | 2018-09-11 15:59:05 +0900 | [diff] [blame] | 887 | * Set start cluster in directory entry |
| 888 | */ |
| 889 | static void set_start_cluster(const fsdata *mydata, dir_entry *dentptr, |
| 890 | __u32 start_cluster) |
| 891 | { |
| 892 | if (mydata->fatsize == 32) |
| 893 | dentptr->starthi = |
| 894 | cpu_to_le16((start_cluster & 0xffff0000) >> 16); |
| 895 | dentptr->start = cpu_to_le16(start_cluster & 0xffff); |
| 896 | } |
| 897 | |
| 898 | /* |
| 899 | * Check whether adding a file makes the file system to |
| 900 | * exceed the size of the block device |
| 901 | * Return -1 when overflow occurs, otherwise return 0 |
| 902 | */ |
| 903 | static int check_overflow(fsdata *mydata, __u32 clustnum, loff_t size) |
| 904 | { |
| 905 | __u32 startsect, sect_num, offset; |
| 906 | |
| 907 | if (clustnum > 0) |
| 908 | startsect = clust_to_sect(mydata, clustnum); |
| 909 | else |
| 910 | startsect = mydata->rootdir_sect; |
| 911 | |
| 912 | sect_num = div_u64_rem(size, mydata->sect_size, &offset); |
| 913 | |
| 914 | if (offset != 0) |
| 915 | sect_num++; |
| 916 | |
| 917 | if (startsect + sect_num > total_sector) |
| 918 | return -1; |
| 919 | return 0; |
| 920 | } |
| 921 | |
| 922 | /* |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 923 | * Write at most 'maxsize' bytes from 'buffer' into |
| 924 | * the file associated with 'dentptr' |
Suriyan Ramasami | 441c223 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 925 | * Update the number of bytes written in *gotsize and return 0 |
| 926 | * or return -1 on fatal errors. |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 927 | */ |
| 928 | static int |
AKASHI Takahiro | ed8b1e4 | 2018-09-11 15:59:05 +0900 | [diff] [blame] | 929 | set_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, __u8 *buffer, |
| 930 | loff_t maxsize, loff_t *gotsize) |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 931 | { |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 932 | unsigned int bytesperclust = mydata->clust_size * mydata->sect_size; |
| 933 | __u32 curclust = START(dentptr); |
| 934 | __u32 endclust = 0, newclust = 0; |
Heinrich Schuchardt | db538a9 | 2019-02-25 19:42:48 +0100 | [diff] [blame] | 935 | u64 cur_pos, filesize; |
| 936 | loff_t offset, actsize, wsize; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 937 | |
Suriyan Ramasami | 441c223 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 938 | *gotsize = 0; |
AKASHI Takahiro | b07f704 | 2018-09-11 15:59:06 +0900 | [diff] [blame] | 939 | filesize = pos + maxsize; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 940 | |
Suriyan Ramasami | 441c223 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 941 | debug("%llu bytes\n", filesize); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 942 | |
AKASHI Takahiro | b07f704 | 2018-09-11 15:59:06 +0900 | [diff] [blame] | 943 | if (!filesize) { |
| 944 | if (!curclust) |
| 945 | return 0; |
| 946 | if (!CHECK_CLUST(curclust, mydata->fatsize) || |
| 947 | IS_LAST_CLUST(curclust, mydata->fatsize)) { |
| 948 | clear_fatent(mydata, curclust); |
| 949 | set_start_cluster(mydata, dentptr, 0); |
| 950 | return 0; |
| 951 | } |
| 952 | debug("curclust: 0x%x\n", curclust); |
| 953 | debug("Invalid FAT entry\n"); |
| 954 | return -1; |
| 955 | } |
| 956 | |
| 957 | if (!curclust) { |
| 958 | assert(pos == 0); |
| 959 | goto set_clusters; |
| 960 | } |
| 961 | |
| 962 | /* go to cluster at pos */ |
| 963 | cur_pos = bytesperclust; |
| 964 | while (1) { |
| 965 | if (pos <= cur_pos) |
| 966 | break; |
| 967 | if (IS_LAST_CLUST(curclust, mydata->fatsize)) |
| 968 | break; |
| 969 | |
| 970 | newclust = get_fatent(mydata, curclust); |
| 971 | if (!IS_LAST_CLUST(newclust, mydata->fatsize) && |
| 972 | CHECK_CLUST(newclust, mydata->fatsize)) { |
| 973 | debug("curclust: 0x%x\n", curclust); |
| 974 | debug("Invalid FAT entry\n"); |
Benoît Thébaudeau | 8fed7d3 | 2015-09-28 15:45:32 +0200 | [diff] [blame] | 975 | return -1; |
| 976 | } |
AKASHI Takahiro | b07f704 | 2018-09-11 15:59:06 +0900 | [diff] [blame] | 977 | |
| 978 | cur_pos += bytesperclust; |
| 979 | curclust = newclust; |
| 980 | } |
| 981 | if (IS_LAST_CLUST(curclust, mydata->fatsize)) { |
| 982 | assert(pos == cur_pos); |
| 983 | goto set_clusters; |
| 984 | } |
| 985 | |
| 986 | assert(pos < cur_pos); |
| 987 | cur_pos -= bytesperclust; |
| 988 | |
| 989 | /* overwrite */ |
| 990 | assert(IS_LAST_CLUST(curclust, mydata->fatsize) || |
| 991 | !CHECK_CLUST(curclust, mydata->fatsize)); |
| 992 | |
| 993 | while (1) { |
| 994 | /* search for allocated consecutive clusters */ |
| 995 | actsize = bytesperclust; |
| 996 | endclust = curclust; |
| 997 | while (1) { |
| 998 | if (filesize <= (cur_pos + actsize)) |
| 999 | break; |
| 1000 | |
| 1001 | newclust = get_fatent(mydata, endclust); |
| 1002 | |
Marek Szyprowski | 2f24167 | 2019-12-02 12:11:13 +0100 | [diff] [blame] | 1003 | if (newclust != endclust + 1) |
| 1004 | break; |
AKASHI Takahiro | b07f704 | 2018-09-11 15:59:06 +0900 | [diff] [blame] | 1005 | if (IS_LAST_CLUST(newclust, mydata->fatsize)) |
| 1006 | break; |
| 1007 | if (CHECK_CLUST(newclust, mydata->fatsize)) { |
| 1008 | debug("curclust: 0x%x\n", curclust); |
| 1009 | debug("Invalid FAT entry\n"); |
| 1010 | return -1; |
| 1011 | } |
| 1012 | |
| 1013 | actsize += bytesperclust; |
| 1014 | endclust = newclust; |
| 1015 | } |
| 1016 | |
| 1017 | /* overwrite to <curclust..endclust> */ |
| 1018 | if (pos < cur_pos) |
| 1019 | offset = 0; |
| 1020 | else |
| 1021 | offset = pos - cur_pos; |
Marek Szyprowski | 3a867ce | 2019-12-02 12:11:14 +0100 | [diff] [blame] | 1022 | wsize = min_t(unsigned long long, actsize, filesize - cur_pos); |
| 1023 | wsize -= offset; |
| 1024 | |
AKASHI Takahiro | b07f704 | 2018-09-11 15:59:06 +0900 | [diff] [blame] | 1025 | if (get_set_cluster(mydata, curclust, offset, |
| 1026 | buffer, wsize, &actsize)) { |
| 1027 | printf("Error get-and-setting cluster\n"); |
| 1028 | return -1; |
| 1029 | } |
| 1030 | buffer += wsize; |
| 1031 | *gotsize += wsize; |
| 1032 | cur_pos += offset + wsize; |
| 1033 | |
| 1034 | if (filesize <= cur_pos) |
| 1035 | break; |
| 1036 | |
AKASHI Takahiro | b07f704 | 2018-09-11 15:59:06 +0900 | [diff] [blame] | 1037 | if (IS_LAST_CLUST(newclust, mydata->fatsize)) |
| 1038 | /* no more clusters */ |
| 1039 | break; |
| 1040 | |
| 1041 | curclust = newclust; |
| 1042 | } |
| 1043 | |
| 1044 | if (filesize <= cur_pos) { |
| 1045 | /* no more write */ |
| 1046 | newclust = get_fatent(mydata, endclust); |
| 1047 | if (!IS_LAST_CLUST(newclust, mydata->fatsize)) { |
| 1048 | /* truncate the rest */ |
| 1049 | clear_fatent(mydata, newclust); |
| 1050 | |
| 1051 | /* Mark end of file in FAT */ |
| 1052 | if (mydata->fatsize == 12) |
| 1053 | newclust = 0xfff; |
| 1054 | else if (mydata->fatsize == 16) |
| 1055 | newclust = 0xffff; |
| 1056 | else if (mydata->fatsize == 32) |
| 1057 | newclust = 0xfffffff; |
| 1058 | set_fatent_value(mydata, endclust, newclust); |
| 1059 | } |
| 1060 | |
| 1061 | return 0; |
AKASHI Takahiro | ed8b1e4 | 2018-09-11 15:59:05 +0900 | [diff] [blame] | 1062 | } |
| 1063 | |
AKASHI Takahiro | b07f704 | 2018-09-11 15:59:06 +0900 | [diff] [blame] | 1064 | curclust = endclust; |
| 1065 | filesize -= cur_pos; |
Heinrich Schuchardt | db538a9 | 2019-02-25 19:42:48 +0100 | [diff] [blame] | 1066 | assert(!do_div(cur_pos, bytesperclust)); |
AKASHI Takahiro | ed8b1e4 | 2018-09-11 15:59:05 +0900 | [diff] [blame] | 1067 | |
AKASHI Takahiro | b07f704 | 2018-09-11 15:59:06 +0900 | [diff] [blame] | 1068 | set_clusters: |
| 1069 | /* allocate and write */ |
| 1070 | assert(!pos); |
| 1071 | |
| 1072 | /* Assure that curclust is valid */ |
| 1073 | if (!curclust) { |
| 1074 | curclust = find_empty_cluster(mydata); |
| 1075 | set_start_cluster(mydata, dentptr, curclust); |
| 1076 | } else { |
| 1077 | newclust = get_fatent(mydata, curclust); |
| 1078 | |
| 1079 | if (IS_LAST_CLUST(newclust, mydata->fatsize)) { |
| 1080 | newclust = determine_fatent(mydata, curclust); |
| 1081 | set_fatent_value(mydata, curclust, newclust); |
| 1082 | curclust = newclust; |
| 1083 | } else { |
| 1084 | debug("error: something wrong\n"); |
| 1085 | return -1; |
| 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | /* TODO: already partially written */ |
AKASHI Takahiro | ed8b1e4 | 2018-09-11 15:59:05 +0900 | [diff] [blame] | 1090 | if (check_overflow(mydata, curclust, filesize)) { |
| 1091 | printf("Error: no space left: %llu\n", filesize); |
| 1092 | return -1; |
Benoît Thébaudeau | 8fed7d3 | 2015-09-28 15:45:32 +0200 | [diff] [blame] | 1093 | } |
| 1094 | |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1095 | actsize = bytesperclust; |
| 1096 | endclust = curclust; |
| 1097 | do { |
| 1098 | /* search for consecutive clusters */ |
| 1099 | while (actsize < filesize) { |
| 1100 | newclust = determine_fatent(mydata, endclust); |
| 1101 | |
| 1102 | if ((newclust - 1) != endclust) |
AKASHI Takahiro | ed8b1e4 | 2018-09-11 15:59:05 +0900 | [diff] [blame] | 1103 | /* write to <curclust..endclust> */ |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1104 | goto getit; |
| 1105 | |
| 1106 | if (CHECK_CLUST(newclust, mydata->fatsize)) { |
Benoît Thébaudeau | e0b8694 | 2015-09-28 15:45:30 +0200 | [diff] [blame] | 1107 | debug("newclust: 0x%x\n", newclust); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1108 | debug("Invalid FAT entry\n"); |
Suriyan Ramasami | 441c223 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1109 | return 0; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1110 | } |
| 1111 | endclust = newclust; |
| 1112 | actsize += bytesperclust; |
| 1113 | } |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1114 | |
| 1115 | /* set remaining bytes */ |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1116 | actsize = filesize; |
Heinrich Schuchardt | 9fb8db4 | 2018-10-02 09:30:45 +0200 | [diff] [blame] | 1117 | if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) { |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1118 | debug("error: writing cluster\n"); |
| 1119 | return -1; |
| 1120 | } |
Suriyan Ramasami | 441c223 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1121 | *gotsize += actsize; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1122 | |
| 1123 | /* Mark end of file in FAT */ |
Philipp Skadorov | 16f553d | 2016-12-15 15:52:53 -0500 | [diff] [blame] | 1124 | if (mydata->fatsize == 12) |
| 1125 | newclust = 0xfff; |
| 1126 | else if (mydata->fatsize == 16) |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1127 | newclust = 0xffff; |
| 1128 | else if (mydata->fatsize == 32) |
| 1129 | newclust = 0xfffffff; |
| 1130 | set_fatent_value(mydata, endclust, newclust); |
| 1131 | |
Suriyan Ramasami | 441c223 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1132 | return 0; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1133 | getit: |
Heinrich Schuchardt | 9fb8db4 | 2018-10-02 09:30:45 +0200 | [diff] [blame] | 1134 | if (set_cluster(mydata, curclust, buffer, (u32)actsize) != 0) { |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1135 | debug("error: writing cluster\n"); |
| 1136 | return -1; |
| 1137 | } |
Suriyan Ramasami | 441c223 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1138 | *gotsize += actsize; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1139 | filesize -= actsize; |
| 1140 | buffer += actsize; |
| 1141 | |
Benoît Thébaudeau | e0b8694 | 2015-09-28 15:45:30 +0200 | [diff] [blame] | 1142 | if (CHECK_CLUST(newclust, mydata->fatsize)) { |
| 1143 | debug("newclust: 0x%x\n", newclust); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1144 | debug("Invalid FAT entry\n"); |
Suriyan Ramasami | 441c223 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1145 | return 0; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1146 | } |
| 1147 | actsize = bytesperclust; |
| 1148 | curclust = endclust = newclust; |
| 1149 | } while (1); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1150 | |
AKASHI Takahiro | ed8b1e4 | 2018-09-11 15:59:05 +0900 | [diff] [blame] | 1151 | return 0; |
Benoît Thébaudeau | 8fed7d3 | 2015-09-28 15:45:32 +0200 | [diff] [blame] | 1152 | } |
| 1153 | |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 1154 | /** |
Heinrich Schuchardt | cab43b0 | 2024-04-09 22:06:07 +0200 | [diff] [blame] | 1155 | * dentry_set_time() - set change time |
| 1156 | * |
| 1157 | * @dentptr: directory entry |
| 1158 | */ |
| 1159 | static void dentry_set_time(dir_entry *dentptr) |
| 1160 | { |
| 1161 | if (CONFIG_IS_ENABLED(DM_RTC)) { |
| 1162 | struct udevice *dev; |
| 1163 | struct rtc_time tm; |
| 1164 | u16 date; |
| 1165 | u16 time; |
| 1166 | |
| 1167 | uclass_first_device(UCLASS_RTC, &dev); |
| 1168 | if (!dev) |
| 1169 | goto err; |
| 1170 | if (dm_rtc_get(dev, &tm)) |
| 1171 | goto err; |
| 1172 | if (tm.tm_year < 1980 || tm.tm_year > 2107) |
| 1173 | goto err; |
| 1174 | date = (tm.tm_mday & 0x1f) | |
| 1175 | ((tm.tm_mon & 0xf) << 5) | |
| 1176 | ((tm.tm_year - 1980) << 9); |
| 1177 | time = (tm.tm_sec > 1) | |
| 1178 | ((tm.tm_min & 0x3f) << 5) | |
| 1179 | (tm.tm_hour << 11); |
| 1180 | dentptr->date = date; |
| 1181 | dentptr->time = time; |
| 1182 | return; |
| 1183 | } |
| 1184 | err: |
| 1185 | dentptr->date = 0x2821; /* 2000-01-01 */ |
| 1186 | dentptr->time = 0; |
| 1187 | } |
| 1188 | |
| 1189 | /** |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 1190 | * fill_dentry() - fill directory entry with shortname |
| 1191 | * |
| 1192 | * @mydata: private filesystem parameters |
| 1193 | * @dentptr: directory entry |
| 1194 | * @shortname: chosen short name |
| 1195 | * @start_cluster: first cluster of file |
| 1196 | * @size: file size |
| 1197 | * @attr: file attributes |
Benoît Thébaudeau | 8fed7d3 | 2015-09-28 15:45:32 +0200 | [diff] [blame] | 1198 | */ |
| 1199 | static void fill_dentry(fsdata *mydata, dir_entry *dentptr, |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 1200 | const char *shortname, __u32 start_cluster, __u32 size, __u8 attr) |
Benoît Thébaudeau | 8fed7d3 | 2015-09-28 15:45:32 +0200 | [diff] [blame] | 1201 | { |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 1202 | memset(dentptr, 0, sizeof(*dentptr)); |
| 1203 | |
Benoît Thébaudeau | 8fed7d3 | 2015-09-28 15:45:32 +0200 | [diff] [blame] | 1204 | set_start_cluster(mydata, dentptr, start_cluster); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1205 | dentptr->size = cpu_to_le32(size); |
| 1206 | |
| 1207 | dentptr->attr = attr; |
| 1208 | |
Heinrich Schuchardt | cab43b0 | 2024-04-09 22:06:07 +0200 | [diff] [blame] | 1209 | /* Set change date */ |
| 1210 | dentry_set_time(dentptr); |
| 1211 | /* Set creation date */ |
| 1212 | dentptr->cdate = dentptr->date; |
| 1213 | dentptr->ctime = dentptr->time; |
| 1214 | |
Heinrich Schuchardt | 66ea58c | 2021-01-21 00:23:33 +0100 | [diff] [blame] | 1215 | memcpy(&dentptr->nameext, shortname, SHORT_NAME_SIZE); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Heinrich Schuchardt | 7a3f610 | 2020-11-26 16:10:01 +0100 | [diff] [blame] | 1218 | /** |
| 1219 | * find_directory_entry() - find a directory entry by filename |
| 1220 | * |
| 1221 | * @itr: directory iterator |
| 1222 | * @filename: name of file to find |
| 1223 | * Return: directory entry or NULL |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1224 | */ |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1225 | static dir_entry *find_directory_entry(fat_itr *itr, char *filename) |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1226 | { |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1227 | int match = 0; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1228 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1229 | while (fat_itr_next(itr)) { |
| 1230 | /* check both long and short name: */ |
| 1231 | if (!strcasecmp(filename, itr->name)) |
| 1232 | match = 1; |
| 1233 | else if (itr->name != itr->s_name && |
| 1234 | !strcasecmp(filename, itr->s_name)) |
| 1235 | match = 1; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1236 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1237 | if (!match) |
| 1238 | continue; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1239 | |
Heinrich Schuchardt | 66ea58c | 2021-01-21 00:23:33 +0100 | [diff] [blame] | 1240 | if (itr->dent->nameext.name[0] == '\0') |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1241 | return NULL; |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1242 | else |
| 1243 | return itr->dent; |
| 1244 | } |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1245 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1246 | return NULL; |
| 1247 | } |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1248 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1249 | static int split_filename(char *filename, char **dirname, char **basename) |
| 1250 | { |
| 1251 | char *p, *last_slash, *last_slash_cont; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1252 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1253 | again: |
| 1254 | p = filename; |
| 1255 | last_slash = NULL; |
| 1256 | last_slash_cont = NULL; |
| 1257 | while (*p) { |
| 1258 | if (ISDIRDELIM(*p)) { |
| 1259 | last_slash = p; |
| 1260 | last_slash_cont = p; |
| 1261 | /* continuous slashes */ |
| 1262 | while (ISDIRDELIM(*p)) |
| 1263 | last_slash_cont = p++; |
| 1264 | if (!*p) |
| 1265 | break; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1266 | } |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1267 | p++; |
| 1268 | } |
Wu, Josh | 675b23c | 2014-05-08 16:14:07 +0800 | [diff] [blame] | 1269 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1270 | if (last_slash) { |
| 1271 | if (last_slash_cont == (filename + strlen(filename) - 1)) { |
| 1272 | /* remove trailing slashes */ |
| 1273 | *last_slash = '\0'; |
| 1274 | goto again; |
Wu, Josh | 675b23c | 2014-05-08 16:14:07 +0800 | [diff] [blame] | 1275 | } |
| 1276 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1277 | if (last_slash == filename) { |
| 1278 | /* avoid ""(null) directory */ |
| 1279 | *dirname = "/"; |
| 1280 | } else { |
| 1281 | *last_slash = '\0'; |
| 1282 | *dirname = filename; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1283 | } |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1284 | |
| 1285 | *last_slash_cont = '\0'; |
Heinrich Schuchardt | 2e1b17b | 2021-01-30 14:12:10 +0100 | [diff] [blame] | 1286 | filename = last_slash_cont + 1; |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1287 | } else { |
| 1288 | *dirname = "/"; /* root by default */ |
Heinrich Schuchardt | 2e1b17b | 2021-01-30 14:12:10 +0100 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | /* |
| 1292 | * The FAT32 File System Specification v1.03 requires leading and |
| 1293 | * trailing spaces as well as trailing periods to be ignored. |
| 1294 | */ |
| 1295 | for (; *filename == ' '; ++filename) |
| 1296 | ; |
| 1297 | |
| 1298 | /* Keep special entries '.' and '..' */ |
| 1299 | if (filename[0] == '.' && |
| 1300 | (!filename[1] || (filename[1] == '.' && !filename[2]))) |
| 1301 | goto done; |
| 1302 | |
| 1303 | /* Remove trailing periods and spaces */ |
| 1304 | for (p = filename + strlen(filename) - 1; p >= filename; --p) { |
| 1305 | switch (*p) { |
| 1306 | case ' ': |
| 1307 | case '.': |
| 1308 | *p = 0; |
| 1309 | break; |
| 1310 | default: |
| 1311 | goto done; |
| 1312 | } |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
Heinrich Schuchardt | 2e1b17b | 2021-01-30 14:12:10 +0100 | [diff] [blame] | 1315 | done: |
| 1316 | *basename = filename; |
| 1317 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1318 | return 0; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
Heinrich Schuchardt | 5858721 | 2019-05-12 09:59:18 +0200 | [diff] [blame] | 1321 | /** |
| 1322 | * normalize_longname() - check long file name and convert to lower case |
| 1323 | * |
| 1324 | * We assume here that the FAT file system is using an 8bit code page. |
| 1325 | * Linux typically uses CP437, EDK2 assumes CP1250. |
| 1326 | * |
| 1327 | * @l_filename: preallocated buffer receiving the normalized name |
| 1328 | * @filename: filename to normalize |
| 1329 | * Return: 0 on success, -1 on failure |
| 1330 | */ |
AKASHI Takahiro | c83df1a | 2018-09-11 15:59:02 +0900 | [diff] [blame] | 1331 | static int normalize_longname(char *l_filename, const char *filename) |
| 1332 | { |
Heinrich Schuchardt | 5858721 | 2019-05-12 09:59:18 +0200 | [diff] [blame] | 1333 | const char *p, illegal[] = "<>:\"/\\|?*"; |
Heinrich Schuchardt | b36d546 | 2021-01-30 11:08:21 +0100 | [diff] [blame] | 1334 | size_t len; |
AKASHI Takahiro | c83df1a | 2018-09-11 15:59:02 +0900 | [diff] [blame] | 1335 | |
Heinrich Schuchardt | b36d546 | 2021-01-30 11:08:21 +0100 | [diff] [blame] | 1336 | len = strlen(filename); |
| 1337 | if (!len || len >= VFAT_MAXLEN_BYTES || filename[len - 1] == '.') |
AKASHI Takahiro | c83df1a | 2018-09-11 15:59:02 +0900 | [diff] [blame] | 1338 | return -1; |
AKASHI Takahiro | c83df1a | 2018-09-11 15:59:02 +0900 | [diff] [blame] | 1339 | |
Heinrich Schuchardt | 5858721 | 2019-05-12 09:59:18 +0200 | [diff] [blame] | 1340 | for (p = filename; *p; ++p) { |
| 1341 | if ((unsigned char)*p < 0x20) |
| 1342 | return -1; |
| 1343 | if (strchr(illegal, *p)) |
| 1344 | return -1; |
| 1345 | } |
AKASHI Takahiro | c83df1a | 2018-09-11 15:59:02 +0900 | [diff] [blame] | 1346 | |
Heinrich Schuchardt | 5858721 | 2019-05-12 09:59:18 +0200 | [diff] [blame] | 1347 | strcpy(l_filename, filename); |
| 1348 | downcase(l_filename, VFAT_MAXLEN_BYTES); |
AKASHI Takahiro | c83df1a | 2018-09-11 15:59:02 +0900 | [diff] [blame] | 1349 | |
| 1350 | return 0; |
| 1351 | } |
| 1352 | |
AKASHI Takahiro | ed8b1e4 | 2018-09-11 15:59:05 +0900 | [diff] [blame] | 1353 | int file_fat_write_at(const char *filename, loff_t pos, void *buffer, |
| 1354 | loff_t size, loff_t *actwrite) |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1355 | { |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1356 | dir_entry *retdent; |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1357 | fsdata datablock = { .fatbuf = NULL, }; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1358 | fsdata *mydata = &datablock; |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1359 | fat_itr *itr = NULL; |
AKASHI Takahiro | c83df1a | 2018-09-11 15:59:02 +0900 | [diff] [blame] | 1360 | int ret = -1; |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1361 | char *filename_copy, *parent, *basename; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1362 | char l_filename[VFAT_MAXLEN_BYTES]; |
| 1363 | |
AKASHI Takahiro | ed8b1e4 | 2018-09-11 15:59:05 +0900 | [diff] [blame] | 1364 | debug("writing %s\n", filename); |
| 1365 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1366 | filename_copy = strdup(filename); |
| 1367 | if (!filename_copy) |
| 1368 | return -ENOMEM; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1369 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1370 | split_filename(filename_copy, &parent, &basename); |
| 1371 | if (!strlen(basename)) { |
| 1372 | ret = -EINVAL; |
| 1373 | goto exit; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
Heinrich Schuchardt | 502cb89 | 2021-01-30 10:01:08 +0100 | [diff] [blame] | 1376 | if (normalize_longname(l_filename, basename)) { |
| 1377 | printf("FAT: illegal filename (%s)\n", basename); |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1378 | ret = -EINVAL; |
| 1379 | goto exit; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1382 | itr = malloc_cache_aligned(sizeof(fat_itr)); |
| 1383 | if (!itr) { |
| 1384 | ret = -ENOMEM; |
| 1385 | goto exit; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1388 | ret = fat_itr_root(itr, &datablock); |
| 1389 | if (ret) |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1390 | goto exit; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1391 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1392 | total_sector = datablock.total_sect; |
| 1393 | |
| 1394 | ret = fat_itr_resolve(itr, parent, TYPE_DIR); |
| 1395 | if (ret) { |
| 1396 | printf("%s: doesn't exist (%d)\n", parent, ret); |
AKASHI Takahiro | c83df1a | 2018-09-11 15:59:02 +0900 | [diff] [blame] | 1397 | goto exit; |
| 1398 | } |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1399 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1400 | retdent = find_directory_entry(itr, l_filename); |
| 1401 | |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1402 | if (retdent) { |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1403 | if (fat_itr_isdir(itr)) { |
| 1404 | ret = -EISDIR; |
| 1405 | goto exit; |
| 1406 | } |
| 1407 | |
AKASHI Takahiro | b07f704 | 2018-09-11 15:59:06 +0900 | [diff] [blame] | 1408 | /* A file exists */ |
| 1409 | if (pos == -1) |
| 1410 | /* Append to the end */ |
| 1411 | pos = FAT2CPU32(retdent->size); |
| 1412 | if (pos > retdent->size) { |
| 1413 | /* No hole allowed */ |
| 1414 | ret = -EINVAL; |
| 1415 | goto exit; |
| 1416 | } |
| 1417 | |
AKASHI Takahiro | ed8b1e4 | 2018-09-11 15:59:05 +0900 | [diff] [blame] | 1418 | /* Update file size in a directory entry */ |
| 1419 | retdent->size = cpu_to_le32(pos + size); |
Heinrich Schuchardt | cab43b0 | 2024-04-09 22:06:07 +0200 | [diff] [blame] | 1420 | /* Update change date */ |
| 1421 | dentry_set_time(retdent); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1422 | } else { |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1423 | /* Create a new file */ |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 1424 | char shortname[SHORT_NAME_SIZE]; |
Heinrich Schuchardt | 524d13a | 2020-11-22 11:54:22 +0100 | [diff] [blame] | 1425 | int ndent; |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1426 | |
AKASHI Takahiro | b07f704 | 2018-09-11 15:59:06 +0900 | [diff] [blame] | 1427 | if (pos) { |
| 1428 | /* No hole allowed */ |
| 1429 | ret = -EINVAL; |
| 1430 | goto exit; |
| 1431 | } |
| 1432 | |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 1433 | /* Check if long name is needed */ |
Heinrich Schuchardt | 502cb89 | 2021-01-30 10:01:08 +0100 | [diff] [blame] | 1434 | ndent = set_name(itr, basename, shortname); |
Heinrich Schuchardt | 524d13a | 2020-11-22 11:54:22 +0100 | [diff] [blame] | 1435 | if (ndent < 0) { |
| 1436 | ret = ndent; |
| 1437 | goto exit; |
| 1438 | } |
| 1439 | ret = fat_find_empty_dentries(itr, ndent); |
| 1440 | if (ret) |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1441 | goto exit; |
Heinrich Schuchardt | 524d13a | 2020-11-22 11:54:22 +0100 | [diff] [blame] | 1442 | if (ndent > 1) { |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 1443 | /* Set long name entries */ |
Heinrich Schuchardt | 502cb89 | 2021-01-30 10:01:08 +0100 | [diff] [blame] | 1444 | ret = fill_dir_slot(itr, basename, shortname); |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 1445 | if (ret) |
| 1446 | goto exit; |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1447 | } |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1448 | |
AKASHI Takahiro | a2c0bd0 | 2019-05-24 14:10:36 +0900 | [diff] [blame] | 1449 | /* Set short name entry */ |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 1450 | fill_dentry(itr->fsdata, itr->dent, shortname, 0, size, |
Heinrich Schuchardt | 881c74a | 2020-11-22 11:13:33 +0100 | [diff] [blame] | 1451 | ATTR_ARCH); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1452 | |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1453 | retdent = itr->dent; |
Benoît Thébaudeau | d1d390a | 2015-09-28 15:45:31 +0200 | [diff] [blame] | 1454 | } |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1455 | |
AKASHI Takahiro | ed8b1e4 | 2018-09-11 15:59:05 +0900 | [diff] [blame] | 1456 | ret = set_contents(mydata, retdent, pos, buffer, size, actwrite); |
Benoît Thébaudeau | d1d390a | 2015-09-28 15:45:31 +0200 | [diff] [blame] | 1457 | if (ret < 0) { |
| 1458 | printf("Error: writing contents\n"); |
AKASHI Takahiro | 6aa7769 | 2018-09-11 15:59:03 +0900 | [diff] [blame] | 1459 | ret = -EIO; |
Benoît Thébaudeau | d1d390a | 2015-09-28 15:45:31 +0200 | [diff] [blame] | 1460 | goto exit; |
| 1461 | } |
| 1462 | debug("attempt to write 0x%llx bytes\n", *actwrite); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1463 | |
Benoît Thébaudeau | d1d390a | 2015-09-28 15:45:31 +0200 | [diff] [blame] | 1464 | /* Flush fat buffer */ |
Stefan Brüns | 751b31d | 2016-09-11 22:51:40 +0200 | [diff] [blame] | 1465 | ret = flush_dirty_fat_buffer(mydata); |
Benoît Thébaudeau | d1d390a | 2015-09-28 15:45:31 +0200 | [diff] [blame] | 1466 | if (ret) { |
| 1467 | printf("Error: flush fat buffer\n"); |
AKASHI Takahiro | 6aa7769 | 2018-09-11 15:59:03 +0900 | [diff] [blame] | 1468 | ret = -EIO; |
Benoît Thébaudeau | d1d390a | 2015-09-28 15:45:31 +0200 | [diff] [blame] | 1469 | goto exit; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
Benoît Thébaudeau | d1d390a | 2015-09-28 15:45:31 +0200 | [diff] [blame] | 1472 | /* Write directory table to device */ |
AKASHI Takahiro | d98c674 | 2019-05-24 14:10:35 +0900 | [diff] [blame] | 1473 | ret = flush_dir(itr); |
Benoît Thébaudeau | d1d390a | 2015-09-28 15:45:31 +0200 | [diff] [blame] | 1474 | |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1475 | exit: |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1476 | free(filename_copy); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1477 | free(mydata->fatbuf); |
AKASHI Takahiro | 49ca96b | 2018-09-11 15:59:04 +0900 | [diff] [blame] | 1478 | free(itr); |
Suriyan Ramasami | 441c223 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1479 | return ret; |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
Suriyan Ramasami | 441c223 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1482 | int file_fat_write(const char *filename, void *buffer, loff_t offset, |
| 1483 | loff_t maxsize, loff_t *actwrite) |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1484 | { |
AKASHI Takahiro | ed8b1e4 | 2018-09-11 15:59:05 +0900 | [diff] [blame] | 1485 | return file_fat_write_at(filename, offset, buffer, maxsize, actwrite); |
Donggeun Kim | 8f81400 | 2011-10-24 21:15:28 +0000 | [diff] [blame] | 1486 | } |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 1487 | |
AKASHI Takahiro | 73b3497 | 2018-09-11 15:59:14 +0900 | [diff] [blame] | 1488 | static int fat_dir_entries(fat_itr *itr) |
| 1489 | { |
| 1490 | fat_itr *dirs; |
| 1491 | fsdata fsdata = { .fatbuf = NULL, }, *mydata = &fsdata; |
| 1492 | /* for FATBUFSIZE */ |
| 1493 | int count; |
| 1494 | |
| 1495 | dirs = malloc_cache_aligned(sizeof(fat_itr)); |
| 1496 | if (!dirs) { |
| 1497 | debug("Error: allocating memory\n"); |
| 1498 | count = -ENOMEM; |
| 1499 | goto exit; |
| 1500 | } |
| 1501 | |
| 1502 | /* duplicate fsdata */ |
| 1503 | fat_itr_child(dirs, itr); |
| 1504 | fsdata = *dirs->fsdata; |
| 1505 | |
| 1506 | /* allocate local fat buffer */ |
| 1507 | fsdata.fatbuf = malloc_cache_aligned(FATBUFSIZE); |
| 1508 | if (!fsdata.fatbuf) { |
| 1509 | debug("Error: allocating memory\n"); |
| 1510 | count = -ENOMEM; |
| 1511 | goto exit; |
| 1512 | } |
| 1513 | fsdata.fatbufnum = -1; |
| 1514 | dirs->fsdata = &fsdata; |
| 1515 | |
| 1516 | for (count = 0; fat_itr_next(dirs); count++) |
| 1517 | ; |
| 1518 | |
| 1519 | exit: |
| 1520 | free(fsdata.fatbuf); |
| 1521 | free(dirs); |
| 1522 | return count; |
| 1523 | } |
| 1524 | |
Heinrich Schuchardt | 284e85b | 2020-11-19 07:31:18 +0100 | [diff] [blame] | 1525 | /** |
| 1526 | * delete_single_dentry() - delete a single directory entry |
| 1527 | * |
| 1528 | * @itr: directory iterator |
| 1529 | * Return: 0 for success |
| 1530 | */ |
| 1531 | static int delete_single_dentry(fat_itr *itr) |
| 1532 | { |
| 1533 | struct dir_entry *dent = itr->dent; |
| 1534 | |
| 1535 | memset(dent, 0, sizeof(*dent)); |
Heinrich Schuchardt | 66ea58c | 2021-01-21 00:23:33 +0100 | [diff] [blame] | 1536 | dent->nameext.name[0] = DELETED_FLAG; |
Heinrich Schuchardt | 284e85b | 2020-11-19 07:31:18 +0100 | [diff] [blame] | 1537 | |
Heinrich Schuchardt | 913339a | 2021-01-20 22:21:53 +0100 | [diff] [blame] | 1538 | if (!itr->remaining) |
| 1539 | return flush_dir(itr); |
Heinrich Schuchardt | 284e85b | 2020-11-19 07:31:18 +0100 | [diff] [blame] | 1540 | return 0; |
| 1541 | } |
| 1542 | |
| 1543 | /** |
| 1544 | * delete_long_name() - delete long name directory entries |
| 1545 | * |
| 1546 | * @itr: directory iterator |
| 1547 | * Return: 0 for success |
| 1548 | */ |
| 1549 | static int delete_long_name(fat_itr *itr) |
| 1550 | { |
Heinrich Schuchardt | 66ea58c | 2021-01-21 00:23:33 +0100 | [diff] [blame] | 1551 | int seqn = itr->dent->nameext.name[0] & ~LAST_LONG_ENTRY_MASK; |
Heinrich Schuchardt | 284e85b | 2020-11-19 07:31:18 +0100 | [diff] [blame] | 1552 | |
| 1553 | while (seqn--) { |
Heinrich Schuchardt | eb3587b | 2021-01-26 00:14:14 +0100 | [diff] [blame] | 1554 | struct dir_entry *dent; |
Heinrich Schuchardt | 284e85b | 2020-11-19 07:31:18 +0100 | [diff] [blame] | 1555 | int ret; |
| 1556 | |
| 1557 | ret = delete_single_dentry(itr); |
| 1558 | if (ret) |
| 1559 | return ret; |
| 1560 | dent = next_dent(itr); |
| 1561 | if (!dent) |
| 1562 | return -EIO; |
| 1563 | } |
| 1564 | return 0; |
| 1565 | } |
| 1566 | |
| 1567 | /** |
| 1568 | * delete_dentry_long() - remove directory entry |
| 1569 | * |
| 1570 | * @itr: directory iterator |
| 1571 | * Return: 0 for success |
| 1572 | */ |
| 1573 | static int delete_dentry_long(fat_itr *itr) |
AKASHI Takahiro | 73b3497 | 2018-09-11 15:59:14 +0900 | [diff] [blame] | 1574 | { |
| 1575 | fsdata *mydata = itr->fsdata; |
Heinrich Schuchardt | 284e85b | 2020-11-19 07:31:18 +0100 | [diff] [blame] | 1576 | dir_entry *dent = itr->dent; |
AKASHI Takahiro | 73b3497 | 2018-09-11 15:59:14 +0900 | [diff] [blame] | 1577 | |
| 1578 | /* free cluster blocks */ |
Heinrich Schuchardt | 284e85b | 2020-11-19 07:31:18 +0100 | [diff] [blame] | 1579 | clear_fatent(mydata, START(dent)); |
AKASHI Takahiro | 73b3497 | 2018-09-11 15:59:14 +0900 | [diff] [blame] | 1580 | if (flush_dirty_fat_buffer(mydata) < 0) { |
| 1581 | printf("Error: flush fat buffer\n"); |
| 1582 | return -EIO; |
| 1583 | } |
Heinrich Schuchardt | 284e85b | 2020-11-19 07:31:18 +0100 | [diff] [blame] | 1584 | /* Position to first directory entry for long name */ |
| 1585 | if (itr->clust != itr->dent_clust) { |
| 1586 | int ret; |
AKASHI Takahiro | 73b3497 | 2018-09-11 15:59:14 +0900 | [diff] [blame] | 1587 | |
Heinrich Schuchardt | 284e85b | 2020-11-19 07:31:18 +0100 | [diff] [blame] | 1588 | ret = fat_move_to_cluster(itr, itr->dent_clust); |
| 1589 | if (ret) |
| 1590 | return ret; |
| 1591 | } |
| 1592 | itr->dent = itr->dent_start; |
| 1593 | itr->remaining = itr->dent_rem; |
| 1594 | dent = itr->dent_start; |
| 1595 | /* Delete long name */ |
| 1596 | if ((dent->attr & ATTR_VFAT) == ATTR_VFAT && |
Heinrich Schuchardt | 66ea58c | 2021-01-21 00:23:33 +0100 | [diff] [blame] | 1597 | (dent->nameext.name[0] & LAST_LONG_ENTRY_MASK)) { |
Heinrich Schuchardt | 284e85b | 2020-11-19 07:31:18 +0100 | [diff] [blame] | 1598 | int ret; |
AKASHI Takahiro | 73b3497 | 2018-09-11 15:59:14 +0900 | [diff] [blame] | 1599 | |
Heinrich Schuchardt | 284e85b | 2020-11-19 07:31:18 +0100 | [diff] [blame] | 1600 | ret = delete_long_name(itr); |
| 1601 | if (ret) |
| 1602 | return ret; |
| 1603 | } |
| 1604 | /* Delete short name */ |
| 1605 | delete_single_dentry(itr); |
Heinrich Schuchardt | 913339a | 2021-01-20 22:21:53 +0100 | [diff] [blame] | 1606 | return flush_dir(itr); |
AKASHI Takahiro | 73b3497 | 2018-09-11 15:59:14 +0900 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | int fat_unlink(const char *filename) |
| 1610 | { |
| 1611 | fsdata fsdata = { .fatbuf = NULL, }; |
| 1612 | fat_itr *itr = NULL; |
| 1613 | int n_entries, ret; |
| 1614 | char *filename_copy, *dirname, *basename; |
| 1615 | |
| 1616 | filename_copy = strdup(filename); |
Simon Glass | 4745981 | 2023-07-15 21:39:06 -0600 | [diff] [blame] | 1617 | itr = malloc_cache_aligned(sizeof(fat_itr)); |
| 1618 | if (!itr || !filename_copy) { |
| 1619 | printf("Error: out of memory\n"); |
Heinrich Schuchardt | b2242f6 | 2018-10-02 06:58:00 +0200 | [diff] [blame] | 1620 | ret = -ENOMEM; |
| 1621 | goto exit; |
| 1622 | } |
AKASHI Takahiro | 73b3497 | 2018-09-11 15:59:14 +0900 | [diff] [blame] | 1623 | split_filename(filename_copy, &dirname, &basename); |
| 1624 | |
| 1625 | if (!strcmp(dirname, "/") && !strcmp(basename, "")) { |
| 1626 | printf("Error: cannot remove root\n"); |
| 1627 | ret = -EINVAL; |
| 1628 | goto exit; |
| 1629 | } |
| 1630 | |
AKASHI Takahiro | 73b3497 | 2018-09-11 15:59:14 +0900 | [diff] [blame] | 1631 | ret = fat_itr_root(itr, &fsdata); |
| 1632 | if (ret) |
| 1633 | goto exit; |
| 1634 | |
| 1635 | total_sector = fsdata.total_sect; |
| 1636 | |
| 1637 | ret = fat_itr_resolve(itr, dirname, TYPE_DIR); |
| 1638 | if (ret) { |
| 1639 | printf("%s: doesn't exist (%d)\n", dirname, ret); |
| 1640 | ret = -ENOENT; |
| 1641 | goto exit; |
| 1642 | } |
| 1643 | |
| 1644 | if (!find_directory_entry(itr, basename)) { |
Simon Glass | 4745981 | 2023-07-15 21:39:06 -0600 | [diff] [blame] | 1645 | log_err("%s: doesn't exist (%d)\n", basename, -ENOENT); |
AKASHI Takahiro | 73b3497 | 2018-09-11 15:59:14 +0900 | [diff] [blame] | 1646 | ret = -ENOENT; |
| 1647 | goto exit; |
| 1648 | } |
| 1649 | |
| 1650 | if (fat_itr_isdir(itr)) { |
| 1651 | n_entries = fat_dir_entries(itr); |
| 1652 | if (n_entries < 0) { |
| 1653 | ret = n_entries; |
| 1654 | goto exit; |
| 1655 | } |
| 1656 | if (n_entries > 2) { |
| 1657 | printf("Error: directory is not empty: %d\n", |
| 1658 | n_entries); |
| 1659 | ret = -EINVAL; |
| 1660 | goto exit; |
| 1661 | } |
| 1662 | } |
| 1663 | |
Heinrich Schuchardt | 284e85b | 2020-11-19 07:31:18 +0100 | [diff] [blame] | 1664 | ret = delete_dentry_long(itr); |
AKASHI Takahiro | 73b3497 | 2018-09-11 15:59:14 +0900 | [diff] [blame] | 1665 | |
| 1666 | exit: |
| 1667 | free(fsdata.fatbuf); |
| 1668 | free(itr); |
| 1669 | free(filename_copy); |
| 1670 | |
| 1671 | return ret; |
| 1672 | } |
| 1673 | |
Heinrich Schuchardt | 502cb89 | 2021-01-30 10:01:08 +0100 | [diff] [blame] | 1674 | int fat_mkdir(const char *dirname) |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 1675 | { |
| 1676 | dir_entry *retdent; |
| 1677 | fsdata datablock = { .fatbuf = NULL, }; |
| 1678 | fsdata *mydata = &datablock; |
| 1679 | fat_itr *itr = NULL; |
Heinrich Schuchardt | 502cb89 | 2021-01-30 10:01:08 +0100 | [diff] [blame] | 1680 | char *dirname_copy, *parent, *basename; |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 1681 | char l_dirname[VFAT_MAXLEN_BYTES]; |
| 1682 | int ret = -1; |
| 1683 | loff_t actwrite; |
| 1684 | unsigned int bytesperclust; |
| 1685 | dir_entry *dotdent = NULL; |
| 1686 | |
Heinrich Schuchardt | 502cb89 | 2021-01-30 10:01:08 +0100 | [diff] [blame] | 1687 | dirname_copy = strdup(dirname); |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 1688 | if (!dirname_copy) |
| 1689 | goto exit; |
| 1690 | |
Heinrich Schuchardt | 502cb89 | 2021-01-30 10:01:08 +0100 | [diff] [blame] | 1691 | split_filename(dirname_copy, &parent, &basename); |
| 1692 | if (!strlen(basename)) { |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 1693 | ret = -EINVAL; |
| 1694 | goto exit; |
| 1695 | } |
| 1696 | |
Heinrich Schuchardt | 502cb89 | 2021-01-30 10:01:08 +0100 | [diff] [blame] | 1697 | if (normalize_longname(l_dirname, basename)) { |
| 1698 | printf("FAT: illegal filename (%s)\n", basename); |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 1699 | ret = -EINVAL; |
| 1700 | goto exit; |
| 1701 | } |
| 1702 | |
| 1703 | itr = malloc_cache_aligned(sizeof(fat_itr)); |
| 1704 | if (!itr) { |
| 1705 | ret = -ENOMEM; |
| 1706 | goto exit; |
| 1707 | } |
| 1708 | |
| 1709 | ret = fat_itr_root(itr, &datablock); |
| 1710 | if (ret) |
| 1711 | goto exit; |
| 1712 | |
| 1713 | total_sector = datablock.total_sect; |
| 1714 | |
| 1715 | ret = fat_itr_resolve(itr, parent, TYPE_DIR); |
| 1716 | if (ret) { |
| 1717 | printf("%s: doesn't exist (%d)\n", parent, ret); |
| 1718 | goto exit; |
| 1719 | } |
| 1720 | |
| 1721 | retdent = find_directory_entry(itr, l_dirname); |
| 1722 | |
| 1723 | if (retdent) { |
| 1724 | printf("%s: already exists\n", l_dirname); |
| 1725 | ret = -EEXIST; |
| 1726 | goto exit; |
| 1727 | } else { |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 1728 | char shortname[SHORT_NAME_SIZE]; |
Heinrich Schuchardt | 524d13a | 2020-11-22 11:54:22 +0100 | [diff] [blame] | 1729 | int ndent; |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 1730 | |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 1731 | if (itr->is_root) { |
| 1732 | /* root dir cannot have "." or ".." */ |
| 1733 | if (!strcmp(l_dirname, ".") || |
| 1734 | !strcmp(l_dirname, "..")) { |
| 1735 | ret = -EINVAL; |
| 1736 | goto exit; |
| 1737 | } |
| 1738 | } |
| 1739 | |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 1740 | /* Check if long name is needed */ |
Heinrich Schuchardt | 502cb89 | 2021-01-30 10:01:08 +0100 | [diff] [blame] | 1741 | ndent = set_name(itr, basename, shortname); |
Heinrich Schuchardt | 524d13a | 2020-11-22 11:54:22 +0100 | [diff] [blame] | 1742 | if (ndent < 0) { |
| 1743 | ret = ndent; |
| 1744 | goto exit; |
| 1745 | } |
| 1746 | ret = fat_find_empty_dentries(itr, ndent); |
| 1747 | if (ret) |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 1748 | goto exit; |
Heinrich Schuchardt | 524d13a | 2020-11-22 11:54:22 +0100 | [diff] [blame] | 1749 | if (ndent > 1) { |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 1750 | /* Set long name entries */ |
Heinrich Schuchardt | 502cb89 | 2021-01-30 10:01:08 +0100 | [diff] [blame] | 1751 | ret = fill_dir_slot(itr, basename, shortname); |
Heinrich Schuchardt | ba9c44e | 2020-11-20 12:55:22 +0100 | [diff] [blame] | 1752 | if (ret) |
| 1753 | goto exit; |
| 1754 | } |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 1755 | |
| 1756 | /* Set attribute as archive for regular file */ |
Heinrich Schuchardt | 7afbd60 | 2020-11-22 19:19:39 +0100 | [diff] [blame] | 1757 | fill_dentry(itr->fsdata, itr->dent, shortname, 0, 0, |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 1758 | ATTR_DIR | ATTR_ARCH); |
| 1759 | |
| 1760 | retdent = itr->dent; |
| 1761 | } |
| 1762 | |
| 1763 | /* Default entries */ |
| 1764 | bytesperclust = mydata->clust_size * mydata->sect_size; |
| 1765 | dotdent = malloc_cache_aligned(bytesperclust); |
| 1766 | if (!dotdent) { |
| 1767 | ret = -ENOMEM; |
| 1768 | goto exit; |
| 1769 | } |
| 1770 | memset(dotdent, 0, bytesperclust); |
| 1771 | |
Heinrich Schuchardt | 66ea58c | 2021-01-21 00:23:33 +0100 | [diff] [blame] | 1772 | memcpy(&dotdent[0].nameext, ". ", 11); |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 1773 | dotdent[0].attr = ATTR_DIR | ATTR_ARCH; |
| 1774 | |
Heinrich Schuchardt | 66ea58c | 2021-01-21 00:23:33 +0100 | [diff] [blame] | 1775 | memcpy(&dotdent[1].nameext, ".. ", 11); |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 1776 | dotdent[1].attr = ATTR_DIR | ATTR_ARCH; |
Heinrich Schuchardt | 00cf076 | 2020-11-24 21:04:07 +0100 | [diff] [blame] | 1777 | |
| 1778 | if (itr->is_root) |
| 1779 | set_start_cluster(mydata, &dotdent[1], 0); |
| 1780 | else |
| 1781 | set_start_cluster(mydata, &dotdent[1], itr->start_clust); |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 1782 | |
| 1783 | ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent, |
| 1784 | bytesperclust, &actwrite); |
| 1785 | if (ret < 0) { |
| 1786 | printf("Error: writing contents\n"); |
| 1787 | goto exit; |
| 1788 | } |
| 1789 | /* Write twice for "." */ |
| 1790 | set_start_cluster(mydata, &dotdent[0], START(retdent)); |
| 1791 | ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent, |
| 1792 | bytesperclust, &actwrite); |
| 1793 | if (ret < 0) { |
| 1794 | printf("Error: writing contents\n"); |
| 1795 | goto exit; |
| 1796 | } |
| 1797 | |
| 1798 | /* Flush fat buffer */ |
| 1799 | ret = flush_dirty_fat_buffer(mydata); |
| 1800 | if (ret) { |
| 1801 | printf("Error: flush fat buffer\n"); |
Heinrich Schuchardt | 913339a | 2021-01-20 22:21:53 +0100 | [diff] [blame] | 1802 | ret = -EIO; |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 1803 | goto exit; |
| 1804 | } |
| 1805 | |
| 1806 | /* Write directory table to device */ |
AKASHI Takahiro | d98c674 | 2019-05-24 14:10:35 +0900 | [diff] [blame] | 1807 | ret = flush_dir(itr); |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 1808 | |
| 1809 | exit: |
| 1810 | free(dirname_copy); |
| 1811 | free(mydata->fatbuf); |
| 1812 | free(itr); |
| 1813 | free(dotdent); |
| 1814 | return ret; |
| 1815 | } |