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