]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
71f95118 WD |
2 | /* |
3 | * fat.c | |
4 | * | |
5 | * R/O (V)FAT 12/16/32 filesystem implementation by Marcus Sundberg | |
6 | * | |
7 | * 2002-07-28 - [email protected] - ported to ppcboot v1.1.6 | |
8 | * 2003-03-10 - [email protected] - ported to uboot | |
71f95118 WD |
9 | */ |
10 | ||
11 | #include <common.h> | |
2a981dc2 | 12 | #include <blk.h> |
71f95118 | 13 | #include <config.h> |
ac497771 | 14 | #include <exports.h> |
71f95118 | 15 | #include <fat.h> |
1f40366b | 16 | #include <fs.h> |
f7ae49fc | 17 | #include <log.h> |
71f95118 | 18 | #include <asm/byteorder.h> |
7205e407 | 19 | #include <part.h> |
9a800ac7 | 20 | #include <malloc.h> |
cf92e05c | 21 | #include <memalign.h> |
90526e9f | 22 | #include <asm/cache.h> |
9a800ac7 | 23 | #include <linux/compiler.h> |
fb7e16cc | 24 | #include <linux/ctype.h> |
71f95118 | 25 | |
71f95118 | 26 | /* |
21a24c3b RC |
27 | * Convert a string to lowercase. Converts at most 'len' characters, |
28 | * 'len' may be larger than the length of 'str' if 'str' is NULL | |
29 | * terminated. | |
71f95118 | 30 | */ |
21a24c3b | 31 | static void downcase(char *str, size_t len) |
71f95118 | 32 | { |
21a24c3b | 33 | while (*str != '\0' && len--) { |
fb7e16cc | 34 | *str = tolower(*str); |
71f95118 WD |
35 | str++; |
36 | } | |
37 | } | |
38 | ||
4101f687 | 39 | static struct blk_desc *cur_dev; |
0528979f | 40 | static struct disk_partition cur_part_info; |
7385c28e | 41 | |
9813b750 | 42 | #define DOS_BOOT_MAGIC_OFFSET 0x1fe |
7205e407 | 43 | #define DOS_FS_TYPE_OFFSET 0x36 |
66c2d73c | 44 | #define DOS_FS32_TYPE_OFFSET 0x52 |
71f95118 | 45 | |
9813b750 | 46 | static int disk_read(__u32 block, __u32 nr_blocks, void *buf) |
71f95118 | 47 | { |
0a04ed86 ŁM |
48 | ulong ret; |
49 | ||
2a981dc2 | 50 | if (!cur_dev) |
7205e407 | 51 | return -1; |
7385c28e | 52 | |
2a981dc2 | 53 | ret = blk_dread(cur_dev, cur_part_info.start + block, nr_blocks, buf); |
0a04ed86 | 54 | |
42a9f147 | 55 | if (ret != nr_blocks) |
0a04ed86 ŁM |
56 | return -1; |
57 | ||
58 | return ret; | |
71f95118 WD |
59 | } |
60 | ||
0528979f | 61 | int fat_set_blk_dev(struct blk_desc *dev_desc, struct disk_partition *info) |
71f95118 | 62 | { |
9a800ac7 | 63 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz); |
7205e407 | 64 | |
5e8f9831 SW |
65 | cur_dev = dev_desc; |
66 | cur_part_info = *info; | |
9813b750 KM |
67 | |
68 | /* Make sure it has a valid FAT header */ | |
69 | if (disk_read(0, 1, buffer) != 1) { | |
70 | cur_dev = NULL; | |
71 | return -1; | |
bf1060ea | 72 | } |
9813b750 KM |
73 | |
74 | /* Check if it's actually a DOS volume */ | |
75 | if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) { | |
76 | cur_dev = NULL; | |
77 | return -1; | |
78 | } | |
79 | ||
80 | /* Check for FAT12/FAT16/FAT32 filesystem */ | |
81 | if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3)) | |
82 | return 0; | |
83 | if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5)) | |
84 | return 0; | |
85 | ||
86 | cur_dev = NULL; | |
87 | return -1; | |
71f95118 WD |
88 | } |
89 | ||
4101f687 | 90 | int fat_register_device(struct blk_desc *dev_desc, int part_no) |
5e8f9831 | 91 | { |
0528979f | 92 | struct disk_partition info; |
5e8f9831 SW |
93 | |
94 | /* First close any currently found FAT filesystem */ | |
95 | cur_dev = NULL; | |
96 | ||
97 | /* Read the partition table, if present */ | |
3e8bd469 | 98 | if (part_get_info(dev_desc, part_no, &info)) { |
5e8f9831 SW |
99 | if (part_no != 0) { |
100 | printf("** Partition %d not valid on device %d **\n", | |
bcce53d0 | 101 | part_no, dev_desc->devnum); |
5e8f9831 SW |
102 | return -1; |
103 | } | |
104 | ||
105 | info.start = 0; | |
106 | info.size = dev_desc->lba; | |
107 | info.blksz = dev_desc->blksz; | |
108 | info.name[0] = 0; | |
109 | info.type[0] = 0; | |
110 | info.bootable = 0; | |
b331cd62 | 111 | #if CONFIG_IS_ENABLED(PARTITION_UUIDS) |
5e8f9831 SW |
112 | info.uuid[0] = 0; |
113 | #endif | |
114 | } | |
115 | ||
116 | return fat_set_blk_dev(dev_desc, &info); | |
117 | } | |
9813b750 | 118 | |
71f95118 WD |
119 | /* |
120 | * Extract zero terminated short name from a directory entry. | |
121 | */ | |
9795e07b | 122 | static void get_name(dir_entry *dirent, char *s_name) |
71f95118 WD |
123 | { |
124 | char *ptr; | |
125 | ||
7385c28e | 126 | memcpy(s_name, dirent->name, 8); |
71f95118 WD |
127 | s_name[8] = '\0'; |
128 | ptr = s_name; | |
129 | while (*ptr && *ptr != ' ') | |
130 | ptr++; | |
21a24c3b RC |
131 | if (dirent->lcase & CASE_LOWER_BASE) |
132 | downcase(s_name, (unsigned)(ptr - s_name)); | |
71f95118 | 133 | if (dirent->ext[0] && dirent->ext[0] != ' ') { |
21a24c3b | 134 | *ptr++ = '.'; |
7385c28e | 135 | memcpy(ptr, dirent->ext, 3); |
21a24c3b RC |
136 | if (dirent->lcase & CASE_LOWER_EXT) |
137 | downcase(ptr, 3); | |
71f95118 WD |
138 | ptr[3] = '\0'; |
139 | while (*ptr && *ptr != ' ') | |
140 | ptr++; | |
141 | } | |
142 | *ptr = '\0'; | |
143 | if (*s_name == DELETED_FLAG) | |
144 | *s_name = '\0'; | |
145 | else if (*s_name == aRING) | |
3c2c2f42 | 146 | *s_name = DELETED_FLAG; |
71f95118 WD |
147 | } |
148 | ||
b8948d2a | 149 | static int flush_dirty_fat_buffer(fsdata *mydata); |
d8c3ea99 TFC |
150 | |
151 | #if !CONFIG_IS_ENABLED(FAT_WRITE) | |
b8948d2a SB |
152 | /* Stub for read only operation */ |
153 | int flush_dirty_fat_buffer(fsdata *mydata) | |
154 | { | |
155 | (void)(mydata); | |
156 | return 0; | |
157 | } | |
158 | #endif | |
159 | ||
71f95118 WD |
160 | /* |
161 | * Get the entry at index 'entry' in a FAT (12/16/32) table. | |
162 | * On failure 0x00 is returned. | |
163 | */ | |
9795e07b | 164 | static __u32 get_fatent(fsdata *mydata, __u32 entry) |
71f95118 WD |
165 | { |
166 | __u32 bufnum; | |
b352caea | 167 | __u32 offset, off8; |
71f95118 WD |
168 | __u32 ret = 0x00; |
169 | ||
b8948d2a SB |
170 | if (CHECK_CLUST(entry, mydata->fatsize)) { |
171 | printf("Error: Invalid FAT entry: 0x%08x\n", entry); | |
172 | return ret; | |
173 | } | |
174 | ||
71f95118 WD |
175 | switch (mydata->fatsize) { |
176 | case 32: | |
177 | bufnum = entry / FAT32BUFSIZE; | |
178 | offset = entry - bufnum * FAT32BUFSIZE; | |
179 | break; | |
180 | case 16: | |
181 | bufnum = entry / FAT16BUFSIZE; | |
182 | offset = entry - bufnum * FAT16BUFSIZE; | |
183 | break; | |
184 | case 12: | |
185 | bufnum = entry / FAT12BUFSIZE; | |
186 | offset = entry - bufnum * FAT12BUFSIZE; | |
187 | break; | |
188 | ||
189 | default: | |
190 | /* Unsupported FAT size */ | |
191 | return ret; | |
192 | } | |
193 | ||
b8948d2a | 194 | debug("FAT%d: entry: 0x%08x = %d, offset: 0x%04x = %d\n", |
7385c28e | 195 | mydata->fatsize, entry, entry, offset, offset); |
2aa98c66 | 196 | |
71f95118 WD |
197 | /* Read a new block of FAT entries into the cache. */ |
198 | if (bufnum != mydata->fatbufnum) { | |
60b36f0f | 199 | __u32 getsize = FATBUFBLOCKS; |
71f95118 WD |
200 | __u8 *bufptr = mydata->fatbuf; |
201 | __u32 fatlength = mydata->fatlength; | |
202 | __u32 startblock = bufnum * FATBUFBLOCKS; | |
203 | ||
6c1a8080 | 204 | /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */ |
8006dd2e BT |
205 | if (startblock + getsize > fatlength) |
206 | getsize = fatlength - startblock; | |
60b36f0f | 207 | |
71f95118 WD |
208 | startblock += mydata->fat_sect; /* Offset from start of disk */ |
209 | ||
b8948d2a SB |
210 | /* Write back the fatbuf to the disk */ |
211 | if (flush_dirty_fat_buffer(mydata) < 0) | |
212 | return -1; | |
213 | ||
71f95118 | 214 | if (disk_read(startblock, getsize, bufptr) < 0) { |
7385c28e | 215 | debug("Error reading FAT blocks\n"); |
71f95118 WD |
216 | return ret; |
217 | } | |
218 | mydata->fatbufnum = bufnum; | |
219 | } | |
220 | ||
221 | /* Get the actual entry from the table */ | |
222 | switch (mydata->fatsize) { | |
223 | case 32: | |
7385c28e | 224 | ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]); |
71f95118 WD |
225 | break; |
226 | case 16: | |
7385c28e | 227 | ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]); |
71f95118 | 228 | break; |
7385c28e | 229 | case 12: |
b352caea SB |
230 | off8 = (offset * 3) / 2; |
231 | /* fatbut + off8 may be unaligned, read in byte granularity */ | |
232 | ret = mydata->fatbuf[off8] + (mydata->fatbuf[off8 + 1] << 8); | |
8d48c92b SB |
233 | |
234 | if (offset & 0x1) | |
235 | ret >>= 4; | |
236 | ret &= 0xfff; | |
71f95118 | 237 | } |
b8948d2a SB |
238 | debug("FAT%d: ret: 0x%08x, entry: 0x%08x, offset: 0x%04x\n", |
239 | mydata->fatsize, ret, entry, offset); | |
71f95118 WD |
240 | |
241 | return ret; | |
242 | } | |
243 | ||
71f95118 WD |
244 | /* |
245 | * Read at most 'size' bytes from the specified cluster into 'buffer'. | |
246 | * Return 0 on success, -1 otherwise. | |
247 | */ | |
248 | static int | |
9795e07b | 249 | get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size) |
71f95118 | 250 | { |
3f270f42 | 251 | __u32 idx = 0; |
71f95118 | 252 | __u32 startsect; |
46236b14 | 253 | int ret; |
71f95118 WD |
254 | |
255 | if (clustnum > 0) { | |
265edc03 | 256 | startsect = clust_to_sect(mydata, clustnum); |
71f95118 WD |
257 | } else { |
258 | startsect = mydata->rootdir_sect; | |
259 | } | |
260 | ||
7385c28e WD |
261 | debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect); |
262 | ||
cc63b25e | 263 | if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) { |
9a800ac7 | 264 | ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size); |
7385c28e | 265 | |
1c381ceb | 266 | debug("FAT: Misaligned buffer address (%p)\n", buffer); |
cc63b25e BT |
267 | |
268 | while (size >= mydata->sect_size) { | |
269 | ret = disk_read(startsect++, 1, tmpbuf); | |
270 | if (ret != 1) { | |
271 | debug("Error reading data (got %d)\n", ret); | |
272 | return -1; | |
273 | } | |
274 | ||
275 | memcpy(buffer, tmpbuf, mydata->sect_size); | |
276 | buffer += mydata->sect_size; | |
277 | size -= mydata->sect_size; | |
278 | } | |
279 | } else { | |
ac497771 | 280 | idx = size / mydata->sect_size; |
5b3ddb17 JW |
281 | if (idx == 0) |
282 | ret = 0; | |
283 | else | |
284 | ret = disk_read(startsect, idx, buffer); | |
cc63b25e BT |
285 | if (ret != idx) { |
286 | debug("Error reading data (got %d)\n", ret); | |
287 | return -1; | |
288 | } | |
289 | startsect += idx; | |
290 | idx *= mydata->sect_size; | |
291 | buffer += idx; | |
292 | size -= idx; | |
293 | } | |
294 | if (size) { | |
295 | ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size); | |
296 | ||
297 | ret = disk_read(startsect, 1, tmpbuf); | |
46236b14 KM |
298 | if (ret != 1) { |
299 | debug("Error reading data (got %d)\n", ret); | |
7205e407 | 300 | return -1; |
71f95118 | 301 | } |
7205e407 | 302 | |
cc63b25e | 303 | memcpy(buffer, tmpbuf, size); |
71f95118 WD |
304 | } |
305 | ||
306 | return 0; | |
307 | } | |
308 | ||
c7a86d16 HS |
309 | /** |
310 | * get_contents() - read from file | |
311 | * | |
1170e634 | 312 | * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr' |
c7a86d16 HS |
313 | * into 'buffer'. Update the number of bytes read in *gotsize or return -1 on |
314 | * fatal errors. | |
315 | * | |
316 | * @mydata: file system description | |
317 | * @dentprt: directory entry pointer | |
318 | * @pos: position from where to read | |
319 | * @buffer: buffer into which to read | |
320 | * @maxsize: maximum number of bytes to read | |
321 | * @gotsize: number of bytes actually read | |
322 | * Return: -1 on error, otherwise 0 | |
71f95118 | 323 | */ |
1ad0b98a SR |
324 | static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, |
325 | __u8 *buffer, loff_t maxsize, loff_t *gotsize) | |
71f95118 | 326 | { |
1ad0b98a | 327 | loff_t filesize = FAT2CPU32(dentptr->size); |
ac497771 | 328 | unsigned int bytesperclust = mydata->clust_size * mydata->sect_size; |
71f95118 | 329 | __u32 curclust = START(dentptr); |
7205e407 | 330 | __u32 endclust, newclust; |
1ad0b98a | 331 | loff_t actsize; |
71f95118 | 332 | |
1ad0b98a SR |
333 | *gotsize = 0; |
334 | debug("Filesize: %llu bytes\n", filesize); | |
71f95118 | 335 | |
1170e634 | 336 | if (pos >= filesize) { |
1ad0b98a SR |
337 | debug("Read position past EOF: %llu\n", pos); |
338 | return 0; | |
1170e634 BT |
339 | } |
340 | ||
341 | if (maxsize > 0 && filesize > pos + maxsize) | |
342 | filesize = pos + maxsize; | |
71f95118 | 343 | |
1ad0b98a | 344 | debug("%llu bytes\n", filesize); |
7385c28e | 345 | |
1170e634 BT |
346 | actsize = bytesperclust; |
347 | ||
348 | /* go to cluster at pos */ | |
349 | while (actsize <= pos) { | |
350 | curclust = get_fatent(mydata, curclust); | |
351 | if (CHECK_CLUST(curclust, mydata->fatsize)) { | |
352 | debug("curclust: 0x%x\n", curclust); | |
c7a86d16 HS |
353 | printf("Invalid FAT entry\n"); |
354 | return -1; | |
1170e634 BT |
355 | } |
356 | actsize += bytesperclust; | |
357 | } | |
358 | ||
359 | /* actsize > pos */ | |
360 | actsize -= bytesperclust; | |
361 | filesize -= actsize; | |
362 | pos -= actsize; | |
363 | ||
364 | /* align to beginning of next cluster if any */ | |
365 | if (pos) { | |
8537874a TFC |
366 | __u8 *tmp_buffer; |
367 | ||
1ad0b98a | 368 | actsize = min(filesize, (loff_t)bytesperclust); |
8537874a TFC |
369 | tmp_buffer = malloc_cache_aligned(actsize); |
370 | if (!tmp_buffer) { | |
371 | debug("Error: allocating buffer\n"); | |
f1368381 | 372 | return -1; |
8537874a TFC |
373 | } |
374 | ||
375 | if (get_cluster(mydata, curclust, tmp_buffer, actsize) != 0) { | |
1170e634 | 376 | printf("Error reading cluster\n"); |
8537874a | 377 | free(tmp_buffer); |
1170e634 BT |
378 | return -1; |
379 | } | |
380 | filesize -= actsize; | |
381 | actsize -= pos; | |
8537874a TFC |
382 | memcpy(buffer, tmp_buffer + pos, actsize); |
383 | free(tmp_buffer); | |
1ad0b98a | 384 | *gotsize += actsize; |
1170e634 | 385 | if (!filesize) |
1ad0b98a | 386 | return 0; |
1170e634 BT |
387 | buffer += actsize; |
388 | ||
389 | curclust = get_fatent(mydata, curclust); | |
390 | if (CHECK_CLUST(curclust, mydata->fatsize)) { | |
391 | debug("curclust: 0x%x\n", curclust); | |
c7a86d16 HS |
392 | printf("Invalid FAT entry\n"); |
393 | return -1; | |
1170e634 BT |
394 | } |
395 | } | |
396 | ||
7385c28e WD |
397 | actsize = bytesperclust; |
398 | endclust = curclust; | |
71f95118 WD |
399 | |
400 | do { | |
7205e407 | 401 | /* search for consecutive clusters */ |
7385c28e | 402 | while (actsize < filesize) { |
7205e407 | 403 | newclust = get_fatent(mydata, endclust); |
7385c28e | 404 | if ((newclust - 1) != endclust) |
7205e407 | 405 | goto getit; |
8ce4e5c2 | 406 | if (CHECK_CLUST(newclust, mydata->fatsize)) { |
7385c28e | 407 | debug("curclust: 0x%x\n", newclust); |
c7a86d16 HS |
408 | printf("Invalid FAT entry\n"); |
409 | return -1; | |
7205e407 | 410 | } |
7385c28e WD |
411 | endclust = newclust; |
412 | actsize += bytesperclust; | |
7205e407 | 413 | } |
7385c28e | 414 | |
7205e407 | 415 | /* get remaining bytes */ |
7385c28e | 416 | actsize = filesize; |
0880e5bb | 417 | if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) { |
7385c28e | 418 | printf("Error reading cluster\n"); |
7205e407 WD |
419 | return -1; |
420 | } | |
1ad0b98a SR |
421 | *gotsize += actsize; |
422 | return 0; | |
7205e407 WD |
423 | getit: |
424 | if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) { | |
7385c28e | 425 | printf("Error reading cluster\n"); |
7205e407 WD |
426 | return -1; |
427 | } | |
1ad0b98a | 428 | *gotsize += (int)actsize; |
7205e407 WD |
429 | filesize -= actsize; |
430 | buffer += actsize; | |
7385c28e | 431 | |
7205e407 | 432 | curclust = get_fatent(mydata, endclust); |
8ce4e5c2 | 433 | if (CHECK_CLUST(curclust, mydata->fatsize)) { |
7385c28e WD |
434 | debug("curclust: 0x%x\n", curclust); |
435 | printf("Invalid FAT entry\n"); | |
c7a86d16 | 436 | return -1; |
71f95118 | 437 | } |
7385c28e WD |
438 | actsize = bytesperclust; |
439 | endclust = curclust; | |
71f95118 WD |
440 | } while (1); |
441 | } | |
442 | ||
71f95118 WD |
443 | /* |
444 | * Extract the file name information from 'slotptr' into 'l_name', | |
445 | * starting at l_name[*idx]. | |
446 | * Return 1 if terminator (zero byte) is found, 0 otherwise. | |
447 | */ | |
9795e07b | 448 | static int slot2str(dir_slot *slotptr, char *l_name, int *idx) |
71f95118 WD |
449 | { |
450 | int j; | |
451 | ||
452 | for (j = 0; j <= 8; j += 2) { | |
453 | l_name[*idx] = slotptr->name0_4[j]; | |
7385c28e WD |
454 | if (l_name[*idx] == 0x00) |
455 | return 1; | |
71f95118 WD |
456 | (*idx)++; |
457 | } | |
458 | for (j = 0; j <= 10; j += 2) { | |
459 | l_name[*idx] = slotptr->name5_10[j]; | |
7385c28e WD |
460 | if (l_name[*idx] == 0x00) |
461 | return 1; | |
71f95118 WD |
462 | (*idx)++; |
463 | } | |
464 | for (j = 0; j <= 2; j += 2) { | |
465 | l_name[*idx] = slotptr->name11_12[j]; | |
7385c28e WD |
466 | if (l_name[*idx] == 0x00) |
467 | return 1; | |
71f95118 WD |
468 | (*idx)++; |
469 | } | |
470 | ||
471 | return 0; | |
472 | } | |
473 | ||
71f95118 | 474 | /* Calculate short name checksum */ |
ff04f6d1 | 475 | static __u8 mkcksum(const char name[8], const char ext[3]) |
71f95118 WD |
476 | { |
477 | int i; | |
7385c28e | 478 | |
71f95118 WD |
479 | __u8 ret = 0; |
480 | ||
6ad77d88 | 481 | for (i = 0; i < 8; i++) |
ff04f6d1 | 482 | ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i]; |
6ad77d88 | 483 | for (i = 0; i < 3; i++) |
ff04f6d1 | 484 | ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i]; |
71f95118 WD |
485 | |
486 | return ret; | |
487 | } | |
71f95118 | 488 | |
71f95118 WD |
489 | /* |
490 | * Read boot sector and volume info from a FAT filesystem | |
491 | */ | |
492 | static int | |
9795e07b | 493 | read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize) |
71f95118 | 494 | { |
ac497771 | 495 | __u8 *block; |
71f95118 | 496 | volume_info *vistart; |
ac497771 SS |
497 | int ret = 0; |
498 | ||
499 | if (cur_dev == NULL) { | |
500 | debug("Error: no device selected\n"); | |
501 | return -1; | |
502 | } | |
503 | ||
09fa964b | 504 | block = malloc_cache_aligned(cur_dev->blksz); |
ac497771 SS |
505 | if (block == NULL) { |
506 | debug("Error: allocating block\n"); | |
507 | return -1; | |
508 | } | |
71f95118 | 509 | |
9795e07b | 510 | if (disk_read(0, 1, block) < 0) { |
7385c28e | 511 | debug("Error: reading block\n"); |
ac497771 | 512 | goto fail; |
71f95118 WD |
513 | } |
514 | ||
515 | memcpy(bs, block, sizeof(boot_sector)); | |
7385c28e WD |
516 | bs->reserved = FAT2CPU16(bs->reserved); |
517 | bs->fat_length = FAT2CPU16(bs->fat_length); | |
518 | bs->secs_track = FAT2CPU16(bs->secs_track); | |
519 | bs->heads = FAT2CPU16(bs->heads); | |
520 | bs->total_sect = FAT2CPU32(bs->total_sect); | |
71f95118 WD |
521 | |
522 | /* FAT32 entries */ | |
523 | if (bs->fat_length == 0) { | |
524 | /* Assume FAT32 */ | |
525 | bs->fat32_length = FAT2CPU32(bs->fat32_length); | |
7385c28e | 526 | bs->flags = FAT2CPU16(bs->flags); |
71f95118 | 527 | bs->root_cluster = FAT2CPU32(bs->root_cluster); |
7385c28e WD |
528 | bs->info_sector = FAT2CPU16(bs->info_sector); |
529 | bs->backup_boot = FAT2CPU16(bs->backup_boot); | |
530 | vistart = (volume_info *)(block + sizeof(boot_sector)); | |
71f95118 WD |
531 | *fatsize = 32; |
532 | } else { | |
7385c28e | 533 | vistart = (volume_info *)&(bs->fat32_length); |
71f95118 WD |
534 | *fatsize = 0; |
535 | } | |
536 | memcpy(volinfo, vistart, sizeof(volume_info)); | |
537 | ||
71f95118 | 538 | if (*fatsize == 32) { |
7385c28e | 539 | if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0) |
ac497771 | 540 | goto exit; |
71f95118 | 541 | } else { |
651351fe | 542 | if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) { |
71f95118 | 543 | *fatsize = 12; |
ac497771 | 544 | goto exit; |
71f95118 | 545 | } |
651351fe | 546 | if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) { |
71f95118 | 547 | *fatsize = 16; |
ac497771 | 548 | goto exit; |
71f95118 WD |
549 | } |
550 | } | |
551 | ||
7385c28e | 552 | debug("Error: broken fs_type sign\n"); |
ac497771 SS |
553 | fail: |
554 | ret = -1; | |
555 | exit: | |
556 | free(block); | |
557 | return ret; | |
71f95118 WD |
558 | } |
559 | ||
45449980 | 560 | static int get_fs_info(fsdata *mydata) |
71f95118 | 561 | { |
7385c28e WD |
562 | boot_sector bs; |
563 | volume_info volinfo; | |
45449980 | 564 | int ret; |
7385c28e | 565 | |
45449980 RC |
566 | ret = read_bootsectandvi(&bs, &volinfo, &mydata->fatsize); |
567 | if (ret) { | |
7385c28e | 568 | debug("Error: reading boot sector\n"); |
45449980 | 569 | return ret; |
7385c28e WD |
570 | } |
571 | ||
40e21916 | 572 | if (mydata->fatsize == 32) { |
7385c28e | 573 | mydata->fatlength = bs.fat32_length; |
f23101f9 | 574 | mydata->total_sect = bs.total_sect; |
40e21916 | 575 | } else { |
7385c28e | 576 | mydata->fatlength = bs.fat_length; |
f23101f9 AT |
577 | mydata->total_sect = (bs.sectors[1] << 8) + bs.sectors[0]; |
578 | if (!mydata->total_sect) | |
579 | mydata->total_sect = bs.total_sect; | |
40e21916 | 580 | } |
f23101f9 AT |
581 | if (!mydata->total_sect) /* unlikely */ |
582 | mydata->total_sect = (u32)cur_part_info.size; | |
7385c28e | 583 | |
f23101f9 | 584 | mydata->fats = bs.fats; |
7385c28e WD |
585 | mydata->fat_sect = bs.reserved; |
586 | ||
45449980 | 587 | mydata->rootdir_sect = mydata->fat_sect + mydata->fatlength * bs.fats; |
7385c28e | 588 | |
ac497771 | 589 | mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0]; |
7385c28e | 590 | mydata->clust_size = bs.cluster_size; |
46236b14 KM |
591 | if (mydata->sect_size != cur_part_info.blksz) { |
592 | printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n", | |
593 | mydata->sect_size, cur_part_info.blksz); | |
594 | return -1; | |
595 | } | |
cd80a4fe PW |
596 | if (mydata->clust_size == 0) { |
597 | printf("Error: FAT cluster size not set\n"); | |
598 | return -1; | |
599 | } | |
600 | if ((unsigned int)mydata->clust_size * mydata->sect_size > | |
601 | MAX_CLUSTSIZE) { | |
602 | printf("Error: FAT cluster size too big (cs=%u, max=%u)\n", | |
603 | (unsigned int)mydata->clust_size * mydata->sect_size, | |
604 | MAX_CLUSTSIZE); | |
605 | return -1; | |
606 | } | |
7385c28e WD |
607 | |
608 | if (mydata->fatsize == 32) { | |
609 | mydata->data_begin = mydata->rootdir_sect - | |
610 | (mydata->clust_size * 2); | |
c6e3baa5 | 611 | mydata->root_cluster = bs.root_cluster; |
7385c28e | 612 | } else { |
45449980 RC |
613 | mydata->rootdir_size = ((bs.dir_entries[1] * (int)256 + |
614 | bs.dir_entries[0]) * | |
615 | sizeof(dir_entry)) / | |
616 | mydata->sect_size; | |
7385c28e | 617 | mydata->data_begin = mydata->rootdir_sect + |
45449980 | 618 | mydata->rootdir_size - |
7385c28e | 619 | (mydata->clust_size * 2); |
9b18358d AH |
620 | |
621 | /* | |
622 | * The root directory is not cluster-aligned and may be on a | |
623 | * "negative" cluster, this will be handled specially in | |
624 | * next_cluster(). | |
625 | */ | |
626 | mydata->root_cluster = 0; | |
7385c28e WD |
627 | } |
628 | ||
629 | mydata->fatbufnum = -1; | |
3c0ed9c3 | 630 | mydata->fat_dirty = 0; |
09fa964b | 631 | mydata->fatbuf = malloc_cache_aligned(FATBUFSIZE); |
ac497771 SS |
632 | if (mydata->fatbuf == NULL) { |
633 | debug("Error: allocating memory\n"); | |
634 | return -1; | |
635 | } | |
71f95118 | 636 | |
7385c28e WD |
637 | debug("FAT%d, fat_sect: %d, fatlength: %d\n", |
638 | mydata->fatsize, mydata->fat_sect, mydata->fatlength); | |
639 | debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n" | |
640 | "Data begins at: %d\n", | |
c6e3baa5 | 641 | mydata->root_cluster, |
7385c28e | 642 | mydata->rootdir_sect, |
ac497771 SS |
643 | mydata->rootdir_sect * mydata->sect_size, mydata->data_begin); |
644 | debug("Sector size: %d, cluster size: %d\n", mydata->sect_size, | |
645 | mydata->clust_size); | |
7385c28e | 646 | |
45449980 RC |
647 | return 0; |
648 | } | |
649 | ||
c6e3baa5 RC |
650 | |
651 | /* | |
652 | * Directory iterator, to simplify filesystem traversal | |
653 | * | |
654 | * Implements an iterator pattern to traverse directory tables, | |
655 | * transparently handling directory tables split across multiple | |
656 | * clusters, and the difference between FAT12/FAT16 root directory | |
657 | * (contiguous) and subdirectories + FAT32 root (chained). | |
658 | * | |
659 | * Rough usage: | |
660 | * | |
661 | * for (fat_itr_root(&itr, fsdata); fat_itr_next(&itr); ) { | |
662 | * // to traverse down to a subdirectory pointed to by | |
663 | * // current iterator position: | |
664 | * fat_itr_child(&itr, &itr); | |
665 | * } | |
666 | * | |
667 | * For more complete example, see fat_itr_resolve() | |
668 | */ | |
669 | ||
670 | typedef struct { | |
671 | fsdata *fsdata; /* filesystem parameters */ | |
3a10e072 | 672 | unsigned start_clust; /* first cluster */ |
c6e3baa5 | 673 | unsigned clust; /* current cluster */ |
f528c140 | 674 | unsigned next_clust; /* next cluster if remaining == 0 */ |
c6e3baa5 RC |
675 | int last_cluster; /* set once we've read last cluster */ |
676 | int is_root; /* is iterator at root directory */ | |
677 | int remaining; /* remaining dent's in current cluster */ | |
678 | ||
679 | /* current iterator position values: */ | |
680 | dir_entry *dent; /* current directory entry */ | |
681 | char l_name[VFAT_MAXLEN_BYTES]; /* long (vfat) name */ | |
682 | char s_name[14]; /* short 8.3 name */ | |
683 | char *name; /* l_name if there is one, else s_name */ | |
684 | ||
685 | /* storage for current cluster in memory: */ | |
686 | u8 block[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN); | |
687 | } fat_itr; | |
688 | ||
689 | static int fat_itr_isdir(fat_itr *itr); | |
690 | ||
691 | /** | |
692 | * fat_itr_root() - initialize an iterator to start at the root | |
693 | * directory | |
694 | * | |
695 | * @itr: iterator to initialize | |
696 | * @fsdata: filesystem data for the partition | |
697 | * @return 0 on success, else -errno | |
698 | */ | |
699 | static int fat_itr_root(fat_itr *itr, fsdata *fsdata) | |
700 | { | |
701 | if (get_fs_info(fsdata)) | |
702 | return -ENXIO; | |
703 | ||
704 | itr->fsdata = fsdata; | |
3a10e072 | 705 | itr->start_clust = 0; |
c6e3baa5 | 706 | itr->clust = fsdata->root_cluster; |
f528c140 | 707 | itr->next_clust = fsdata->root_cluster; |
c6e3baa5 RC |
708 | itr->dent = NULL; |
709 | itr->remaining = 0; | |
710 | itr->last_cluster = 0; | |
711 | itr->is_root = 1; | |
712 | ||
713 | return 0; | |
714 | } | |
715 | ||
716 | /** | |
717 | * fat_itr_child() - initialize an iterator to descend into a sub- | |
718 | * directory | |
719 | * | |
720 | * Initializes 'itr' to iterate the contents of the directory at | |
721 | * the current cursor position of 'parent'. It is an error to | |
722 | * call this if the current cursor of 'parent' is pointing at a | |
723 | * regular file. | |
724 | * | |
725 | * Note that 'itr' and 'parent' can be the same pointer if you do | |
726 | * not need to preserve 'parent' after this call, which is useful | |
727 | * for traversing directory structure to resolve a file/directory. | |
728 | * | |
729 | * @itr: iterator to initialize | |
730 | * @parent: the iterator pointing at a directory entry in the | |
731 | * parent directory of the directory to iterate | |
732 | */ | |
733 | static void fat_itr_child(fat_itr *itr, fat_itr *parent) | |
734 | { | |
735 | fsdata *mydata = parent->fsdata; /* for silly macros */ | |
736 | unsigned clustnum = START(parent->dent); | |
737 | ||
738 | assert(fat_itr_isdir(parent)); | |
739 | ||
740 | itr->fsdata = parent->fsdata; | |
3a10e072 | 741 | itr->start_clust = clustnum; |
c6e3baa5 RC |
742 | if (clustnum > 0) { |
743 | itr->clust = clustnum; | |
f528c140 | 744 | itr->next_clust = clustnum; |
8df87314 | 745 | itr->is_root = 0; |
c6e3baa5 RC |
746 | } else { |
747 | itr->clust = parent->fsdata->root_cluster; | |
f528c140 | 748 | itr->next_clust = parent->fsdata->root_cluster; |
8df87314 | 749 | itr->is_root = 1; |
c6e3baa5 RC |
750 | } |
751 | itr->dent = NULL; | |
752 | itr->remaining = 0; | |
753 | itr->last_cluster = 0; | |
c6e3baa5 RC |
754 | } |
755 | ||
9b18358d | 756 | static void *next_cluster(fat_itr *itr, unsigned *nbytes) |
c6e3baa5 RC |
757 | { |
758 | fsdata *mydata = itr->fsdata; /* for silly macros */ | |
759 | int ret; | |
760 | u32 sect; | |
9b18358d | 761 | u32 read_size; |
c6e3baa5 RC |
762 | |
763 | /* have we reached the end? */ | |
764 | if (itr->last_cluster) | |
765 | return NULL; | |
766 | ||
9b18358d AH |
767 | if (itr->is_root && itr->fsdata->fatsize != 32) { |
768 | /* | |
769 | * The root directory is located before the data area and | |
770 | * cannot be indexed using the regular unsigned cluster | |
771 | * numbers (it may start at a "negative" cluster or not at a | |
772 | * cluster boundary at all), so consider itr->next_clust to be | |
773 | * a offset in cluster-sized units from the start of rootdir. | |
774 | */ | |
775 | unsigned sect_offset = itr->next_clust * itr->fsdata->clust_size; | |
776 | unsigned remaining_sects = itr->fsdata->rootdir_size - sect_offset; | |
777 | sect = itr->fsdata->rootdir_sect + sect_offset; | |
778 | /* do not read past the end of rootdir */ | |
779 | read_size = min_t(u32, itr->fsdata->clust_size, | |
780 | remaining_sects); | |
781 | } else { | |
782 | sect = clust_to_sect(itr->fsdata, itr->next_clust); | |
783 | read_size = itr->fsdata->clust_size; | |
784 | } | |
c6e3baa5 | 785 | |
9b18358d AH |
786 | debug("FAT read(sect=%d), clust_size=%d, read_size=%u, DIRENTSPERBLOCK=%zd\n", |
787 | sect, itr->fsdata->clust_size, read_size, DIRENTSPERBLOCK); | |
c6e3baa5 RC |
788 | |
789 | /* | |
790 | * NOTE: do_fat_read_at() had complicated logic to deal w/ | |
791 | * vfat names that span multiple clusters in the fat16 case, | |
792 | * which get_dentfromdir() probably also needed (and was | |
793 | * missing). And not entirely sure what fat32 didn't have | |
794 | * the same issue.. We solve that by only caring about one | |
795 | * dent at a time and iteratively constructing the vfat long | |
796 | * name. | |
797 | */ | |
9b18358d | 798 | ret = disk_read(sect, read_size, itr->block); |
c6e3baa5 RC |
799 | if (ret < 0) { |
800 | debug("Error: reading block\n"); | |
801 | return NULL; | |
802 | } | |
803 | ||
9b18358d | 804 | *nbytes = read_size * itr->fsdata->sect_size; |
f528c140 | 805 | itr->clust = itr->next_clust; |
c6e3baa5 | 806 | if (itr->is_root && itr->fsdata->fatsize != 32) { |
f528c140 | 807 | itr->next_clust++; |
9b18358d | 808 | if (itr->next_clust * itr->fsdata->clust_size >= |
c6e3baa5 | 809 | itr->fsdata->rootdir_size) { |
f528c140 | 810 | debug("nextclust: 0x%x\n", itr->next_clust); |
c6e3baa5 RC |
811 | itr->last_cluster = 1; |
812 | } | |
813 | } else { | |
f528c140 AT |
814 | itr->next_clust = get_fatent(itr->fsdata, itr->next_clust); |
815 | if (CHECK_CLUST(itr->next_clust, itr->fsdata->fatsize)) { | |
816 | debug("nextclust: 0x%x\n", itr->next_clust); | |
c6e3baa5 RC |
817 | itr->last_cluster = 1; |
818 | } | |
819 | } | |
820 | ||
821 | return itr->block; | |
822 | } | |
823 | ||
824 | static dir_entry *next_dent(fat_itr *itr) | |
825 | { | |
826 | if (itr->remaining == 0) { | |
9b18358d AH |
827 | unsigned nbytes; |
828 | struct dir_entry *dent = next_cluster(itr, &nbytes); | |
c6e3baa5 RC |
829 | |
830 | /* have we reached the last cluster? */ | |
f528c140 AT |
831 | if (!dent) { |
832 | /* a sign for no more entries left */ | |
833 | itr->dent = NULL; | |
c6e3baa5 | 834 | return NULL; |
f528c140 | 835 | } |
c6e3baa5 RC |
836 | |
837 | itr->remaining = nbytes / sizeof(dir_entry) - 1; | |
838 | itr->dent = dent; | |
839 | } else { | |
840 | itr->remaining--; | |
841 | itr->dent++; | |
842 | } | |
843 | ||
844 | /* have we reached the last valid entry? */ | |
845 | if (itr->dent->name[0] == 0) | |
846 | return NULL; | |
847 | ||
848 | return itr->dent; | |
849 | } | |
850 | ||
851 | static dir_entry *extract_vfat_name(fat_itr *itr) | |
852 | { | |
853 | struct dir_entry *dent = itr->dent; | |
854 | int seqn = itr->dent->name[0] & ~LAST_LONG_ENTRY_MASK; | |
855 | u8 chksum, alias_checksum = ((dir_slot *)dent)->alias_checksum; | |
856 | int n = 0; | |
857 | ||
858 | while (seqn--) { | |
859 | char buf[13]; | |
860 | int idx = 0; | |
861 | ||
862 | slot2str((dir_slot *)dent, buf, &idx); | |
863 | ||
8b021bb9 PW |
864 | if (n + idx >= sizeof(itr->l_name)) |
865 | return NULL; | |
866 | ||
c6e3baa5 RC |
867 | /* shift accumulated long-name up and copy new part in: */ |
868 | memmove(itr->l_name + idx, itr->l_name, n); | |
869 | memcpy(itr->l_name, buf, idx); | |
870 | n += idx; | |
871 | ||
872 | dent = next_dent(itr); | |
873 | if (!dent) | |
874 | return NULL; | |
875 | } | |
876 | ||
39606d46 AT |
877 | /* |
878 | * We are now at the short file name entry. | |
879 | * If it is marked as deleted, just skip it. | |
880 | */ | |
881 | if (dent->name[0] == DELETED_FLAG || | |
882 | dent->name[0] == aRING) | |
883 | return NULL; | |
884 | ||
c6e3baa5 RC |
885 | itr->l_name[n] = '\0'; |
886 | ||
887 | chksum = mkcksum(dent->name, dent->ext); | |
888 | ||
889 | /* checksum mismatch could mean deleted file, etc.. skip it: */ | |
890 | if (chksum != alias_checksum) { | |
891 | debug("** chksum=%x, alias_checksum=%x, l_name=%s, s_name=%8s.%3s\n", | |
892 | chksum, alias_checksum, itr->l_name, dent->name, dent->ext); | |
893 | return NULL; | |
894 | } | |
895 | ||
896 | return dent; | |
897 | } | |
898 | ||
899 | /** | |
900 | * fat_itr_next() - step to the next entry in a directory | |
901 | * | |
902 | * Must be called once on a new iterator before the cursor is valid. | |
903 | * | |
904 | * @itr: the iterator to iterate | |
905 | * @return boolean, 1 if success or 0 if no more entries in the | |
906 | * current directory | |
907 | */ | |
908 | static int fat_itr_next(fat_itr *itr) | |
909 | { | |
910 | dir_entry *dent; | |
911 | ||
912 | itr->name = NULL; | |
913 | ||
39606d46 AT |
914 | /* |
915 | * One logical directory entry consist of following slots: | |
916 | * name[0] Attributes | |
917 | * dent[N - N]: LFN[N - 1] N|0x40 ATTR_VFAT | |
918 | * ... | |
919 | * dent[N - 2]: LFN[1] 2 ATTR_VFAT | |
920 | * dent[N - 1]: LFN[0] 1 ATTR_VFAT | |
921 | * dent[N]: SFN ATTR_ARCH | |
922 | */ | |
923 | ||
c6e3baa5 RC |
924 | while (1) { |
925 | dent = next_dent(itr); | |
926 | if (!dent) | |
927 | return 0; | |
928 | ||
929 | if (dent->name[0] == DELETED_FLAG || | |
930 | dent->name[0] == aRING) | |
931 | continue; | |
932 | ||
933 | if (dent->attr & ATTR_VOLUME) { | |
8bad6cb1 | 934 | if ((dent->attr & ATTR_VFAT) == ATTR_VFAT && |
c6e3baa5 | 935 | (dent->name[0] & LAST_LONG_ENTRY_MASK)) { |
39606d46 | 936 | /* long file name */ |
c6e3baa5 | 937 | dent = extract_vfat_name(itr); |
39606d46 AT |
938 | /* |
939 | * If succeeded, dent has a valid short file | |
940 | * name entry for the current entry. | |
941 | * If failed, itr points to a current bogus | |
942 | * entry. So after fetching a next one, | |
943 | * it may have a short file name entry | |
944 | * for this bogus entry so that we can still | |
945 | * check for a short name. | |
946 | */ | |
c6e3baa5 RC |
947 | if (!dent) |
948 | continue; | |
949 | itr->name = itr->l_name; | |
950 | break; | |
951 | } else { | |
952 | /* Volume label or VFAT entry, skip */ | |
953 | continue; | |
954 | } | |
1788a969 | 955 | } |
c6e3baa5 | 956 | |
39606d46 | 957 | /* short file name */ |
c6e3baa5 RC |
958 | break; |
959 | } | |
960 | ||
961 | get_name(dent, itr->s_name); | |
962 | if (!itr->name) | |
963 | itr->name = itr->s_name; | |
964 | ||
965 | return 1; | |
966 | } | |
967 | ||
968 | /** | |
969 | * fat_itr_isdir() - is current cursor position pointing to a directory | |
970 | * | |
971 | * @itr: the iterator | |
972 | * @return true if cursor is at a directory | |
973 | */ | |
974 | static int fat_itr_isdir(fat_itr *itr) | |
975 | { | |
976 | return !!(itr->dent->attr & ATTR_DIR); | |
977 | } | |
978 | ||
979 | /* | |
980 | * Helpers: | |
981 | */ | |
982 | ||
983 | #define TYPE_FILE 0x1 | |
984 | #define TYPE_DIR 0x2 | |
985 | #define TYPE_ANY (TYPE_FILE | TYPE_DIR) | |
986 | ||
987 | /** | |
988 | * fat_itr_resolve() - traverse directory structure to resolve the | |
989 | * requested path. | |
990 | * | |
991 | * Traverse directory structure to the requested path. If the specified | |
992 | * path is to a directory, this will descend into the directory and | |
993 | * leave it iterator at the start of the directory. If the path is to a | |
994 | * file, it will leave the iterator in the parent directory with current | |
995 | * cursor at file's entry in the directory. | |
996 | * | |
997 | * @itr: iterator initialized to root | |
998 | * @path: the requested path | |
999 | * @type: bitmask of allowable file types | |
1000 | * @return 0 on success or -errno | |
1001 | */ | |
1002 | static int fat_itr_resolve(fat_itr *itr, const char *path, unsigned type) | |
1003 | { | |
1004 | const char *next; | |
1005 | ||
1006 | /* chomp any extra leading slashes: */ | |
1007 | while (path[0] && ISDIRDELIM(path[0])) | |
1008 | path++; | |
1009 | ||
1010 | /* are we at the end? */ | |
1011 | if (strlen(path) == 0) { | |
1012 | if (!(type & TYPE_DIR)) | |
1013 | return -ENOENT; | |
1014 | return 0; | |
1015 | } | |
1016 | ||
1017 | /* find length of next path entry: */ | |
1018 | next = path; | |
1019 | while (next[0] && !ISDIRDELIM(next[0])) | |
1020 | next++; | |
1021 | ||
b94b6be5 AT |
1022 | if (itr->is_root) { |
1023 | /* root dir doesn't have "." nor ".." */ | |
1024 | if ((((next - path) == 1) && !strncmp(path, ".", 1)) || | |
1025 | (((next - path) == 2) && !strncmp(path, "..", 2))) { | |
1026 | /* point back to itself */ | |
1027 | itr->clust = itr->fsdata->root_cluster; | |
f528c140 | 1028 | itr->next_clust = itr->fsdata->root_cluster; |
b94b6be5 AT |
1029 | itr->dent = NULL; |
1030 | itr->remaining = 0; | |
1031 | itr->last_cluster = 0; | |
1032 | ||
1033 | if (next[0] == 0) { | |
1034 | if (type & TYPE_DIR) | |
1035 | return 0; | |
1036 | else | |
1037 | return -ENOENT; | |
1038 | } | |
1039 | ||
1040 | return fat_itr_resolve(itr, next, type); | |
1041 | } | |
1042 | } | |
1043 | ||
c6e3baa5 RC |
1044 | while (fat_itr_next(itr)) { |
1045 | int match = 0; | |
1046 | unsigned n = max(strlen(itr->name), (size_t)(next - path)); | |
1047 | ||
1048 | /* check both long and short name: */ | |
1049 | if (!strncasecmp(path, itr->name, n)) | |
1050 | match = 1; | |
1051 | else if (itr->name != itr->s_name && | |
1052 | !strncasecmp(path, itr->s_name, n)) | |
1053 | match = 1; | |
1054 | ||
1055 | if (!match) | |
1056 | continue; | |
1057 | ||
1058 | if (fat_itr_isdir(itr)) { | |
1059 | /* recurse into directory: */ | |
1060 | fat_itr_child(itr, itr); | |
1061 | return fat_itr_resolve(itr, next, type); | |
1062 | } else if (next[0]) { | |
1063 | /* | |
1064 | * If next is not empty then we have a case | |
1065 | * like: /path/to/realfile/nonsense | |
1066 | */ | |
1067 | debug("bad trailing path: %s\n", next); | |
1068 | return -ENOENT; | |
1069 | } else if (!(type & TYPE_FILE)) { | |
1070 | return -ENOTDIR; | |
1071 | } else { | |
1072 | return 0; | |
1073 | } | |
1074 | } | |
1075 | ||
1076 | return -ENOENT; | |
1077 | } | |
1078 | ||
9795e07b | 1079 | int file_fat_detectfs(void) |
71f95118 | 1080 | { |
7385c28e WD |
1081 | boot_sector bs; |
1082 | volume_info volinfo; | |
1083 | int fatsize; | |
1084 | char vol_label[12]; | |
71f95118 | 1085 | |
7385c28e | 1086 | if (cur_dev == NULL) { |
7205e407 WD |
1087 | printf("No current device\n"); |
1088 | return 1; | |
1089 | } | |
7385c28e | 1090 | |
fc843a02 | 1091 | #if defined(CONFIG_IDE) || \ |
10e40d54 | 1092 | defined(CONFIG_SATA) || \ |
c649e3c9 | 1093 | defined(CONFIG_SCSI) || \ |
dd60d122 | 1094 | defined(CONFIG_CMD_USB) || \ |
21f6f963 | 1095 | defined(CONFIG_MMC) |
7205e407 | 1096 | printf("Interface: "); |
7385c28e WD |
1097 | switch (cur_dev->if_type) { |
1098 | case IF_TYPE_IDE: | |
1099 | printf("IDE"); | |
1100 | break; | |
1101 | case IF_TYPE_SATA: | |
1102 | printf("SATA"); | |
1103 | break; | |
1104 | case IF_TYPE_SCSI: | |
1105 | printf("SCSI"); | |
1106 | break; | |
1107 | case IF_TYPE_ATAPI: | |
1108 | printf("ATAPI"); | |
1109 | break; | |
1110 | case IF_TYPE_USB: | |
1111 | printf("USB"); | |
1112 | break; | |
1113 | case IF_TYPE_DOC: | |
1114 | printf("DOC"); | |
1115 | break; | |
1116 | case IF_TYPE_MMC: | |
1117 | printf("MMC"); | |
1118 | break; | |
1119 | default: | |
1120 | printf("Unknown"); | |
7205e407 | 1121 | } |
7385c28e | 1122 | |
bcce53d0 | 1123 | printf("\n Device %d: ", cur_dev->devnum); |
7205e407 WD |
1124 | dev_print(cur_dev); |
1125 | #endif | |
7385c28e WD |
1126 | |
1127 | if (read_bootsectandvi(&bs, &volinfo, &fatsize)) { | |
7205e407 WD |
1128 | printf("\nNo valid FAT fs found\n"); |
1129 | return 1; | |
1130 | } | |
7385c28e WD |
1131 | |
1132 | memcpy(vol_label, volinfo.volume_label, 11); | |
7205e407 | 1133 | vol_label[11] = '\0'; |
7385c28e WD |
1134 | volinfo.fs_type[5] = '\0'; |
1135 | ||
461f86e6 | 1136 | printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label); |
7385c28e | 1137 | |
7205e407 | 1138 | return 0; |
71f95118 WD |
1139 | } |
1140 | ||
b7b5f319 SW |
1141 | int fat_exists(const char *filename) |
1142 | { | |
8eafae20 | 1143 | fsdata fsdata; |
2460098c | 1144 | fat_itr *itr; |
1ad0b98a | 1145 | int ret; |
1ad0b98a | 1146 | |
09fa964b | 1147 | itr = malloc_cache_aligned(sizeof(fat_itr)); |
af609e37 TT |
1148 | if (!itr) |
1149 | return 0; | |
8eafae20 RC |
1150 | ret = fat_itr_root(itr, &fsdata); |
1151 | if (ret) | |
af609e37 | 1152 | goto out; |
8eafae20 RC |
1153 | |
1154 | ret = fat_itr_resolve(itr, filename, TYPE_ANY); | |
725ffdb5 | 1155 | free(fsdata.fatbuf); |
af609e37 | 1156 | out: |
2460098c | 1157 | free(itr); |
1ad0b98a | 1158 | return ret == 0; |
b7b5f319 SW |
1159 | } |
1160 | ||
d455d878 | 1161 | int fat_size(const char *filename, loff_t *size) |
cf659819 | 1162 | { |
8eafae20 | 1163 | fsdata fsdata; |
2460098c | 1164 | fat_itr *itr; |
8eafae20 RC |
1165 | int ret; |
1166 | ||
09fa964b | 1167 | itr = malloc_cache_aligned(sizeof(fat_itr)); |
af609e37 TT |
1168 | if (!itr) |
1169 | return -ENOMEM; | |
8eafae20 RC |
1170 | ret = fat_itr_root(itr, &fsdata); |
1171 | if (ret) | |
af609e37 | 1172 | goto out_free_itr; |
8eafae20 RC |
1173 | |
1174 | ret = fat_itr_resolve(itr, filename, TYPE_FILE); | |
1175 | if (ret) { | |
1176 | /* | |
1177 | * Directories don't have size, but fs_size() is not | |
1178 | * expected to fail if passed a directory path: | |
1179 | */ | |
725ffdb5 | 1180 | free(fsdata.fatbuf); |
d0cd30eb AD |
1181 | ret = fat_itr_root(itr, &fsdata); |
1182 | if (ret) | |
1183 | goto out_free_itr; | |
1184 | ret = fat_itr_resolve(itr, filename, TYPE_DIR); | |
1185 | if (!ret) | |
8eafae20 | 1186 | *size = 0; |
af609e37 | 1187 | goto out_free_both; |
8eafae20 RC |
1188 | } |
1189 | ||
1190 | *size = FAT2CPU32(itr->dent->size); | |
af609e37 | 1191 | out_free_both: |
725ffdb5 | 1192 | free(fsdata.fatbuf); |
af609e37 | 1193 | out_free_itr: |
2460098c | 1194 | free(itr); |
725ffdb5 | 1195 | return ret; |
cf659819 SW |
1196 | } |
1197 | ||
1ad0b98a SR |
1198 | int file_fat_read_at(const char *filename, loff_t pos, void *buffer, |
1199 | loff_t maxsize, loff_t *actread) | |
71f95118 | 1200 | { |
8eafae20 | 1201 | fsdata fsdata; |
2460098c | 1202 | fat_itr *itr; |
8eafae20 RC |
1203 | int ret; |
1204 | ||
09fa964b | 1205 | itr = malloc_cache_aligned(sizeof(fat_itr)); |
af609e37 TT |
1206 | if (!itr) |
1207 | return -ENOMEM; | |
8eafae20 RC |
1208 | ret = fat_itr_root(itr, &fsdata); |
1209 | if (ret) | |
af609e37 | 1210 | goto out_free_itr; |
8eafae20 RC |
1211 | |
1212 | ret = fat_itr_resolve(itr, filename, TYPE_FILE); | |
1213 | if (ret) | |
af609e37 | 1214 | goto out_free_both; |
8eafae20 | 1215 | |
287c04e1 | 1216 | debug("reading %s at pos %llu\n", filename, pos); |
e48485f5 TFC |
1217 | |
1218 | /* For saving default max clustersize memory allocated to malloc pool */ | |
1219 | dir_entry *dentptr = itr->dent; | |
1220 | ||
e48485f5 | 1221 | ret = get_contents(&fsdata, dentptr, pos, buffer, maxsize, actread); |
725ffdb5 | 1222 | |
af609e37 | 1223 | out_free_both: |
725ffdb5 | 1224 | free(fsdata.fatbuf); |
af609e37 | 1225 | out_free_itr: |
2460098c | 1226 | free(itr); |
725ffdb5 | 1227 | return ret; |
1170e634 BT |
1228 | } |
1229 | ||
1ad0b98a | 1230 | int file_fat_read(const char *filename, void *buffer, int maxsize) |
1170e634 | 1231 | { |
1ad0b98a SR |
1232 | loff_t actread; |
1233 | int ret; | |
1234 | ||
1235 | ret = file_fat_read_at(filename, 0, buffer, maxsize, &actread); | |
1236 | if (ret) | |
1237 | return ret; | |
1238 | else | |
1239 | return actread; | |
71f95118 | 1240 | } |
e6d52415 | 1241 | |
d455d878 SR |
1242 | int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len, |
1243 | loff_t *actread) | |
e6d52415 | 1244 | { |
1ad0b98a | 1245 | int ret; |
e6d52415 | 1246 | |
d455d878 SR |
1247 | ret = file_fat_read_at(filename, offset, buf, len, actread); |
1248 | if (ret) | |
e6d52415 | 1249 | printf("** Unable to read file %s **\n", filename); |
e6d52415 | 1250 | |
d455d878 | 1251 | return ret; |
e6d52415 SG |
1252 | } |
1253 | ||
1f40366b RC |
1254 | typedef struct { |
1255 | struct fs_dir_stream parent; | |
1256 | struct fs_dirent dirent; | |
1257 | fsdata fsdata; | |
1258 | fat_itr itr; | |
1259 | } fat_dir; | |
1260 | ||
1261 | int fat_opendir(const char *filename, struct fs_dir_stream **dirsp) | |
1262 | { | |
34dd853c | 1263 | fat_dir *dir; |
1f40366b RC |
1264 | int ret; |
1265 | ||
34dd853c | 1266 | dir = malloc_cache_aligned(sizeof(*dir)); |
1f40366b RC |
1267 | if (!dir) |
1268 | return -ENOMEM; | |
34dd853c | 1269 | memset(dir, 0, sizeof(*dir)); |
1f40366b RC |
1270 | |
1271 | ret = fat_itr_root(&dir->itr, &dir->fsdata); | |
1272 | if (ret) | |
af609e37 | 1273 | goto fail_free_dir; |
1f40366b RC |
1274 | |
1275 | ret = fat_itr_resolve(&dir->itr, filename, TYPE_DIR); | |
1276 | if (ret) | |
af609e37 | 1277 | goto fail_free_both; |
1f40366b RC |
1278 | |
1279 | *dirsp = (struct fs_dir_stream *)dir; | |
1280 | return 0; | |
1281 | ||
af609e37 | 1282 | fail_free_both: |
725ffdb5 | 1283 | free(dir->fsdata.fatbuf); |
af609e37 | 1284 | fail_free_dir: |
1f40366b RC |
1285 | free(dir); |
1286 | return ret; | |
1287 | } | |
1288 | ||
1289 | int fat_readdir(struct fs_dir_stream *dirs, struct fs_dirent **dentp) | |
1290 | { | |
1291 | fat_dir *dir = (fat_dir *)dirs; | |
1292 | struct fs_dirent *dent = &dir->dirent; | |
1293 | ||
1294 | if (!fat_itr_next(&dir->itr)) | |
1295 | return -ENOENT; | |
1296 | ||
1297 | memset(dent, 0, sizeof(*dent)); | |
1298 | strcpy(dent->name, dir->itr.name); | |
1299 | ||
1300 | if (fat_itr_isdir(&dir->itr)) { | |
1301 | dent->type = FS_DT_DIR; | |
1302 | } else { | |
1303 | dent->type = FS_DT_REG; | |
1304 | dent->size = FAT2CPU32(dir->itr.dent->size); | |
1305 | } | |
1306 | ||
1307 | *dentp = dent; | |
1308 | ||
1309 | return 0; | |
1310 | } | |
1311 | ||
1312 | void fat_closedir(struct fs_dir_stream *dirs) | |
1313 | { | |
1314 | fat_dir *dir = (fat_dir *)dirs; | |
725ffdb5 | 1315 | free(dir->fsdata.fatbuf); |
1f40366b RC |
1316 | free(dir); |
1317 | } | |
1318 | ||
e6d52415 SG |
1319 | void fat_close(void) |
1320 | { | |
1321 | } |