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