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