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