]>
Commit | Line | Data |
---|---|---|
71f95118 WD |
1 | /* |
2 | * fat.c | |
3 | * | |
4 | * R/O (V)FAT 12/16/32 filesystem implementation by Marcus Sundberg | |
5 | * | |
6 | * 2002-07-28 - rjones@nexus-tech.net - ported to ppcboot v1.1.6 | |
7 | * 2003-03-10 - kharris@nexus-tech.net - ported to uboot | |
8 | * | |
1a459660 | 9 | * SPDX-License-Identifier: GPL-2.0+ |
71f95118 WD |
10 | */ |
11 | ||
12 | #include <common.h> | |
2a981dc2 | 13 | #include <blk.h> |
71f95118 | 14 | #include <config.h> |
ac497771 | 15 | #include <exports.h> |
71f95118 WD |
16 | #include <fat.h> |
17 | #include <asm/byteorder.h> | |
7205e407 | 18 | #include <part.h> |
9a800ac7 | 19 | #include <malloc.h> |
cf92e05c | 20 | #include <memalign.h> |
9a800ac7 | 21 | #include <linux/compiler.h> |
fb7e16cc | 22 | #include <linux/ctype.h> |
71f95118 | 23 | |
cb940c7e RG |
24 | #ifdef CONFIG_SUPPORT_VFAT |
25 | static const int vfat_enabled = 1; | |
26 | #else | |
27 | static const int vfat_enabled = 0; | |
28 | #endif | |
29 | ||
71f95118 WD |
30 | /* |
31 | * Convert a string to lowercase. | |
32 | */ | |
9795e07b | 33 | static void downcase(char *str) |
71f95118 WD |
34 | { |
35 | while (*str != '\0') { | |
fb7e16cc | 36 | *str = tolower(*str); |
71f95118 WD |
37 | str++; |
38 | } | |
39 | } | |
40 | ||
4101f687 | 41 | static struct blk_desc *cur_dev; |
9813b750 | 42 | static disk_partition_t cur_part_info; |
7385c28e | 43 | |
9813b750 | 44 | #define DOS_BOOT_MAGIC_OFFSET 0x1fe |
7205e407 | 45 | #define DOS_FS_TYPE_OFFSET 0x36 |
66c2d73c | 46 | #define DOS_FS32_TYPE_OFFSET 0x52 |
71f95118 | 47 | |
9813b750 | 48 | static int disk_read(__u32 block, __u32 nr_blocks, void *buf) |
71f95118 | 49 | { |
0a04ed86 ŁM |
50 | ulong ret; |
51 | ||
2a981dc2 | 52 | if (!cur_dev) |
7205e407 | 53 | return -1; |
7385c28e | 54 | |
2a981dc2 | 55 | ret = blk_dread(cur_dev, cur_part_info.start + block, nr_blocks, buf); |
0a04ed86 | 56 | |
42a9f147 | 57 | if (ret != nr_blocks) |
0a04ed86 ŁM |
58 | return -1; |
59 | ||
60 | return ret; | |
71f95118 WD |
61 | } |
62 | ||
4101f687 | 63 | int fat_set_blk_dev(struct blk_desc *dev_desc, disk_partition_t *info) |
71f95118 | 64 | { |
9a800ac7 | 65 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz); |
7205e407 | 66 | |
5e8f9831 SW |
67 | cur_dev = dev_desc; |
68 | cur_part_info = *info; | |
9813b750 KM |
69 | |
70 | /* Make sure it has a valid FAT header */ | |
71 | if (disk_read(0, 1, buffer) != 1) { | |
72 | cur_dev = NULL; | |
73 | return -1; | |
bf1060ea | 74 | } |
9813b750 KM |
75 | |
76 | /* Check if it's actually a DOS volume */ | |
77 | if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) { | |
78 | cur_dev = NULL; | |
79 | return -1; | |
80 | } | |
81 | ||
82 | /* Check for FAT12/FAT16/FAT32 filesystem */ | |
83 | if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3)) | |
84 | return 0; | |
85 | if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5)) | |
86 | return 0; | |
87 | ||
88 | cur_dev = NULL; | |
89 | return -1; | |
71f95118 WD |
90 | } |
91 | ||
4101f687 | 92 | int fat_register_device(struct blk_desc *dev_desc, int part_no) |
5e8f9831 SW |
93 | { |
94 | disk_partition_t info; | |
95 | ||
96 | /* First close any currently found FAT filesystem */ | |
97 | cur_dev = NULL; | |
98 | ||
99 | /* Read the partition table, if present */ | |
3e8bd469 | 100 | if (part_get_info(dev_desc, part_no, &info)) { |
5e8f9831 SW |
101 | if (part_no != 0) { |
102 | printf("** Partition %d not valid on device %d **\n", | |
bcce53d0 | 103 | part_no, dev_desc->devnum); |
5e8f9831 SW |
104 | return -1; |
105 | } | |
106 | ||
107 | info.start = 0; | |
108 | info.size = dev_desc->lba; | |
109 | info.blksz = dev_desc->blksz; | |
110 | info.name[0] = 0; | |
111 | info.type[0] = 0; | |
112 | info.bootable = 0; | |
b331cd62 | 113 | #if CONFIG_IS_ENABLED(PARTITION_UUIDS) |
5e8f9831 SW |
114 | info.uuid[0] = 0; |
115 | #endif | |
116 | } | |
117 | ||
118 | return fat_set_blk_dev(dev_desc, &info); | |
119 | } | |
9813b750 | 120 | |
71f95118 WD |
121 | /* |
122 | * Get the first occurence of a directory delimiter ('/' or '\') in a string. | |
123 | * Return index into string if found, -1 otherwise. | |
124 | */ | |
9795e07b | 125 | static int dirdelim(char *str) |
71f95118 WD |
126 | { |
127 | char *start = str; | |
128 | ||
129 | while (*str != '\0') { | |
7385c28e WD |
130 | if (ISDIRDELIM(*str)) |
131 | return str - start; | |
71f95118 WD |
132 | str++; |
133 | } | |
134 | return -1; | |
135 | } | |
136 | ||
71f95118 WD |
137 | /* |
138 | * Extract zero terminated short name from a directory entry. | |
139 | */ | |
9795e07b | 140 | static void get_name(dir_entry *dirent, char *s_name) |
71f95118 WD |
141 | { |
142 | char *ptr; | |
143 | ||
7385c28e | 144 | memcpy(s_name, dirent->name, 8); |
71f95118 WD |
145 | s_name[8] = '\0'; |
146 | ptr = s_name; | |
147 | while (*ptr && *ptr != ' ') | |
148 | ptr++; | |
149 | if (dirent->ext[0] && dirent->ext[0] != ' ') { | |
150 | *ptr = '.'; | |
151 | ptr++; | |
7385c28e | 152 | memcpy(ptr, dirent->ext, 3); |
71f95118 WD |
153 | ptr[3] = '\0'; |
154 | while (*ptr && *ptr != ' ') | |
155 | ptr++; | |
156 | } | |
157 | *ptr = '\0'; | |
158 | if (*s_name == DELETED_FLAG) | |
159 | *s_name = '\0'; | |
160 | else if (*s_name == aRING) | |
3c2c2f42 | 161 | *s_name = DELETED_FLAG; |
7385c28e | 162 | downcase(s_name); |
71f95118 WD |
163 | } |
164 | ||
b8948d2a SB |
165 | static int flush_dirty_fat_buffer(fsdata *mydata); |
166 | #if !defined(CONFIG_FAT_WRITE) | |
167 | /* Stub for read only operation */ | |
168 | int flush_dirty_fat_buffer(fsdata *mydata) | |
169 | { | |
170 | (void)(mydata); | |
171 | return 0; | |
172 | } | |
173 | #endif | |
174 | ||
71f95118 WD |
175 | /* |
176 | * Get the entry at index 'entry' in a FAT (12/16/32) table. | |
177 | * On failure 0x00 is returned. | |
178 | */ | |
9795e07b | 179 | static __u32 get_fatent(fsdata *mydata, __u32 entry) |
71f95118 WD |
180 | { |
181 | __u32 bufnum; | |
b352caea | 182 | __u32 offset, off8; |
71f95118 WD |
183 | __u32 ret = 0x00; |
184 | ||
b8948d2a SB |
185 | if (CHECK_CLUST(entry, mydata->fatsize)) { |
186 | printf("Error: Invalid FAT entry: 0x%08x\n", entry); | |
187 | return ret; | |
188 | } | |
189 | ||
71f95118 WD |
190 | switch (mydata->fatsize) { |
191 | case 32: | |
192 | bufnum = entry / FAT32BUFSIZE; | |
193 | offset = entry - bufnum * FAT32BUFSIZE; | |
194 | break; | |
195 | case 16: | |
196 | bufnum = entry / FAT16BUFSIZE; | |
197 | offset = entry - bufnum * FAT16BUFSIZE; | |
198 | break; | |
199 | case 12: | |
200 | bufnum = entry / FAT12BUFSIZE; | |
201 | offset = entry - bufnum * FAT12BUFSIZE; | |
202 | break; | |
203 | ||
204 | default: | |
205 | /* Unsupported FAT size */ | |
206 | return ret; | |
207 | } | |
208 | ||
b8948d2a | 209 | debug("FAT%d: entry: 0x%08x = %d, offset: 0x%04x = %d\n", |
7385c28e | 210 | mydata->fatsize, entry, entry, offset, offset); |
2aa98c66 | 211 | |
71f95118 WD |
212 | /* Read a new block of FAT entries into the cache. */ |
213 | if (bufnum != mydata->fatbufnum) { | |
60b36f0f | 214 | __u32 getsize = FATBUFBLOCKS; |
71f95118 WD |
215 | __u8 *bufptr = mydata->fatbuf; |
216 | __u32 fatlength = mydata->fatlength; | |
217 | __u32 startblock = bufnum * FATBUFBLOCKS; | |
218 | ||
6c1a8080 | 219 | /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */ |
8006dd2e BT |
220 | if (startblock + getsize > fatlength) |
221 | getsize = fatlength - startblock; | |
60b36f0f | 222 | |
71f95118 WD |
223 | startblock += mydata->fat_sect; /* Offset from start of disk */ |
224 | ||
b8948d2a SB |
225 | /* Write back the fatbuf to the disk */ |
226 | if (flush_dirty_fat_buffer(mydata) < 0) | |
227 | return -1; | |
228 | ||
71f95118 | 229 | if (disk_read(startblock, getsize, bufptr) < 0) { |
7385c28e | 230 | debug("Error reading FAT blocks\n"); |
71f95118 WD |
231 | return ret; |
232 | } | |
233 | mydata->fatbufnum = bufnum; | |
234 | } | |
235 | ||
236 | /* Get the actual entry from the table */ | |
237 | switch (mydata->fatsize) { | |
238 | case 32: | |
7385c28e | 239 | ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]); |
71f95118 WD |
240 | break; |
241 | case 16: | |
7385c28e | 242 | ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]); |
71f95118 | 243 | break; |
7385c28e | 244 | case 12: |
b352caea SB |
245 | off8 = (offset * 3) / 2; |
246 | /* fatbut + off8 may be unaligned, read in byte granularity */ | |
247 | ret = mydata->fatbuf[off8] + (mydata->fatbuf[off8 + 1] << 8); | |
8d48c92b SB |
248 | |
249 | if (offset & 0x1) | |
250 | ret >>= 4; | |
251 | ret &= 0xfff; | |
71f95118 | 252 | } |
b8948d2a SB |
253 | debug("FAT%d: ret: 0x%08x, entry: 0x%08x, offset: 0x%04x\n", |
254 | mydata->fatsize, ret, entry, offset); | |
71f95118 WD |
255 | |
256 | return ret; | |
257 | } | |
258 | ||
71f95118 WD |
259 | /* |
260 | * Read at most 'size' bytes from the specified cluster into 'buffer'. | |
261 | * Return 0 on success, -1 otherwise. | |
262 | */ | |
263 | static int | |
9795e07b | 264 | get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size) |
71f95118 | 265 | { |
3f270f42 | 266 | __u32 idx = 0; |
71f95118 | 267 | __u32 startsect; |
46236b14 | 268 | int ret; |
71f95118 WD |
269 | |
270 | if (clustnum > 0) { | |
7385c28e WD |
271 | startsect = mydata->data_begin + |
272 | clustnum * mydata->clust_size; | |
71f95118 WD |
273 | } else { |
274 | startsect = mydata->rootdir_sect; | |
275 | } | |
276 | ||
7385c28e WD |
277 | debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect); |
278 | ||
cc63b25e | 279 | if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) { |
9a800ac7 | 280 | ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size); |
7385c28e | 281 | |
cc63b25e BT |
282 | printf("FAT: Misaligned buffer address (%p)\n", buffer); |
283 | ||
284 | while (size >= mydata->sect_size) { | |
285 | ret = disk_read(startsect++, 1, tmpbuf); | |
286 | if (ret != 1) { | |
287 | debug("Error reading data (got %d)\n", ret); | |
288 | return -1; | |
289 | } | |
290 | ||
291 | memcpy(buffer, tmpbuf, mydata->sect_size); | |
292 | buffer += mydata->sect_size; | |
293 | size -= mydata->sect_size; | |
294 | } | |
295 | } else { | |
ac497771 | 296 | idx = size / mydata->sect_size; |
cc63b25e BT |
297 | ret = disk_read(startsect, idx, buffer); |
298 | if (ret != idx) { | |
299 | debug("Error reading data (got %d)\n", ret); | |
300 | return -1; | |
301 | } | |
302 | startsect += idx; | |
303 | idx *= mydata->sect_size; | |
304 | buffer += idx; | |
305 | size -= idx; | |
306 | } | |
307 | if (size) { | |
308 | ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size); | |
309 | ||
310 | ret = disk_read(startsect, 1, tmpbuf); | |
46236b14 KM |
311 | if (ret != 1) { |
312 | debug("Error reading data (got %d)\n", ret); | |
7205e407 | 313 | return -1; |
71f95118 | 314 | } |
7205e407 | 315 | |
cc63b25e | 316 | memcpy(buffer, tmpbuf, size); |
71f95118 WD |
317 | } |
318 | ||
319 | return 0; | |
320 | } | |
321 | ||
71f95118 | 322 | /* |
1170e634 | 323 | * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr' |
71f95118 | 324 | * into 'buffer'. |
1ad0b98a | 325 | * Update the number of bytes read in *gotsize or return -1 on fatal errors. |
71f95118 | 326 | */ |
1170e634 BT |
327 | __u8 get_contents_vfatname_block[MAX_CLUSTSIZE] |
328 | __aligned(ARCH_DMA_MINALIGN); | |
329 | ||
1ad0b98a SR |
330 | static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, |
331 | __u8 *buffer, loff_t maxsize, loff_t *gotsize) | |
71f95118 | 332 | { |
1ad0b98a | 333 | loff_t filesize = FAT2CPU32(dentptr->size); |
ac497771 | 334 | unsigned int bytesperclust = mydata->clust_size * mydata->sect_size; |
71f95118 | 335 | __u32 curclust = START(dentptr); |
7205e407 | 336 | __u32 endclust, newclust; |
1ad0b98a | 337 | loff_t actsize; |
71f95118 | 338 | |
1ad0b98a SR |
339 | *gotsize = 0; |
340 | debug("Filesize: %llu bytes\n", filesize); | |
71f95118 | 341 | |
1170e634 | 342 | if (pos >= filesize) { |
1ad0b98a SR |
343 | debug("Read position past EOF: %llu\n", pos); |
344 | return 0; | |
1170e634 BT |
345 | } |
346 | ||
347 | if (maxsize > 0 && filesize > pos + maxsize) | |
348 | filesize = pos + maxsize; | |
71f95118 | 349 | |
1ad0b98a | 350 | debug("%llu bytes\n", filesize); |
7385c28e | 351 | |
1170e634 BT |
352 | actsize = bytesperclust; |
353 | ||
354 | /* go to cluster at pos */ | |
355 | while (actsize <= pos) { | |
356 | curclust = get_fatent(mydata, curclust); | |
357 | if (CHECK_CLUST(curclust, mydata->fatsize)) { | |
358 | debug("curclust: 0x%x\n", curclust); | |
359 | debug("Invalid FAT entry\n"); | |
1ad0b98a | 360 | return 0; |
1170e634 BT |
361 | } |
362 | actsize += bytesperclust; | |
363 | } | |
364 | ||
365 | /* actsize > pos */ | |
366 | actsize -= bytesperclust; | |
367 | filesize -= actsize; | |
368 | pos -= actsize; | |
369 | ||
370 | /* align to beginning of next cluster if any */ | |
371 | if (pos) { | |
1ad0b98a | 372 | actsize = min(filesize, (loff_t)bytesperclust); |
1170e634 BT |
373 | if (get_cluster(mydata, curclust, get_contents_vfatname_block, |
374 | (int)actsize) != 0) { | |
375 | printf("Error reading cluster\n"); | |
376 | return -1; | |
377 | } | |
378 | filesize -= actsize; | |
379 | actsize -= pos; | |
380 | memcpy(buffer, get_contents_vfatname_block + pos, actsize); | |
1ad0b98a | 381 | *gotsize += actsize; |
1170e634 | 382 | if (!filesize) |
1ad0b98a | 383 | return 0; |
1170e634 BT |
384 | buffer += actsize; |
385 | ||
386 | curclust = get_fatent(mydata, curclust); | |
387 | if (CHECK_CLUST(curclust, mydata->fatsize)) { | |
388 | debug("curclust: 0x%x\n", curclust); | |
389 | debug("Invalid FAT entry\n"); | |
1ad0b98a | 390 | return 0; |
1170e634 BT |
391 | } |
392 | } | |
393 | ||
7385c28e WD |
394 | actsize = bytesperclust; |
395 | endclust = curclust; | |
71f95118 WD |
396 | |
397 | do { | |
7205e407 | 398 | /* search for consecutive clusters */ |
7385c28e | 399 | while (actsize < filesize) { |
7205e407 | 400 | newclust = get_fatent(mydata, endclust); |
7385c28e | 401 | if ((newclust - 1) != endclust) |
7205e407 | 402 | goto getit; |
8ce4e5c2 | 403 | if (CHECK_CLUST(newclust, mydata->fatsize)) { |
7385c28e WD |
404 | debug("curclust: 0x%x\n", newclust); |
405 | debug("Invalid FAT entry\n"); | |
1ad0b98a | 406 | return 0; |
7205e407 | 407 | } |
7385c28e WD |
408 | endclust = newclust; |
409 | actsize += bytesperclust; | |
7205e407 | 410 | } |
7385c28e | 411 | |
7205e407 | 412 | /* get remaining bytes */ |
7385c28e | 413 | actsize = filesize; |
0880e5bb | 414 | if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) { |
7385c28e | 415 | printf("Error reading cluster\n"); |
7205e407 WD |
416 | return -1; |
417 | } | |
1ad0b98a SR |
418 | *gotsize += actsize; |
419 | return 0; | |
7205e407 WD |
420 | getit: |
421 | if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) { | |
7385c28e | 422 | printf("Error reading cluster\n"); |
7205e407 WD |
423 | return -1; |
424 | } | |
1ad0b98a | 425 | *gotsize += (int)actsize; |
7205e407 WD |
426 | filesize -= actsize; |
427 | buffer += actsize; | |
7385c28e | 428 | |
7205e407 | 429 | curclust = get_fatent(mydata, endclust); |
8ce4e5c2 | 430 | if (CHECK_CLUST(curclust, mydata->fatsize)) { |
7385c28e WD |
431 | debug("curclust: 0x%x\n", curclust); |
432 | printf("Invalid FAT entry\n"); | |
1ad0b98a | 433 | return 0; |
71f95118 | 434 | } |
7385c28e WD |
435 | actsize = bytesperclust; |
436 | endclust = curclust; | |
71f95118 WD |
437 | } while (1); |
438 | } | |
439 | ||
71f95118 WD |
440 | /* |
441 | * Extract the file name information from 'slotptr' into 'l_name', | |
442 | * starting at l_name[*idx]. | |
443 | * Return 1 if terminator (zero byte) is found, 0 otherwise. | |
444 | */ | |
9795e07b | 445 | static int slot2str(dir_slot *slotptr, char *l_name, int *idx) |
71f95118 WD |
446 | { |
447 | int j; | |
448 | ||
449 | for (j = 0; j <= 8; j += 2) { | |
450 | l_name[*idx] = slotptr->name0_4[j]; | |
7385c28e WD |
451 | if (l_name[*idx] == 0x00) |
452 | return 1; | |
71f95118 WD |
453 | (*idx)++; |
454 | } | |
455 | for (j = 0; j <= 10; j += 2) { | |
456 | l_name[*idx] = slotptr->name5_10[j]; | |
7385c28e WD |
457 | if (l_name[*idx] == 0x00) |
458 | return 1; | |
71f95118 WD |
459 | (*idx)++; |
460 | } | |
461 | for (j = 0; j <= 2; j += 2) { | |
462 | l_name[*idx] = slotptr->name11_12[j]; | |
7385c28e WD |
463 | if (l_name[*idx] == 0x00) |
464 | return 1; | |
71f95118 WD |
465 | (*idx)++; |
466 | } | |
467 | ||
468 | return 0; | |
469 | } | |
470 | ||
71f95118 WD |
471 | /* |
472 | * Extract the full long filename starting at 'retdent' (which is really | |
473 | * a slot) into 'l_name'. If successful also copy the real directory entry | |
474 | * into 'retdent' | |
475 | * Return 0 on success, -1 otherwise. | |
476 | */ | |
477 | static int | |
9795e07b BT |
478 | get_vfatname(fsdata *mydata, int curclust, __u8 *cluster, |
479 | dir_entry *retdent, char *l_name) | |
71f95118 WD |
480 | { |
481 | dir_entry *realdent; | |
7385c28e | 482 | dir_slot *slotptr = (dir_slot *)retdent; |
025421ea SS |
483 | __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ? |
484 | PREFETCH_BLOCKS : | |
485 | mydata->clust_size); | |
7385c28e | 486 | __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff; |
71f95118 WD |
487 | int idx = 0; |
488 | ||
3831530d MZ |
489 | if (counter > VFAT_MAXSEQ) { |
490 | debug("Error: VFAT name is too long\n"); | |
491 | return -1; | |
492 | } | |
493 | ||
494 | while ((__u8 *)slotptr < buflimit) { | |
7385c28e WD |
495 | if (counter == 0) |
496 | break; | |
2d1a537d WD |
497 | if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter) |
498 | return -1; | |
71f95118 WD |
499 | slotptr++; |
500 | counter--; | |
501 | } | |
502 | ||
3831530d | 503 | if ((__u8 *)slotptr >= buflimit) { |
71f95118 WD |
504 | dir_slot *slotptr2; |
505 | ||
3831530d MZ |
506 | if (curclust == 0) |
507 | return -1; | |
71f95118 | 508 | curclust = get_fatent(mydata, curclust); |
8ce4e5c2 | 509 | if (CHECK_CLUST(curclust, mydata->fatsize)) { |
7385c28e WD |
510 | debug("curclust: 0x%x\n", curclust); |
511 | printf("Invalid FAT entry\n"); | |
71f95118 WD |
512 | return -1; |
513 | } | |
7385c28e | 514 | |
1170e634 | 515 | if (get_cluster(mydata, curclust, get_contents_vfatname_block, |
ac497771 | 516 | mydata->clust_size * mydata->sect_size) != 0) { |
7385c28e | 517 | debug("Error: reading directory block\n"); |
71f95118 WD |
518 | return -1; |
519 | } | |
7385c28e | 520 | |
1170e634 | 521 | slotptr2 = (dir_slot *)get_contents_vfatname_block; |
3831530d MZ |
522 | while (counter > 0) { |
523 | if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK) | |
524 | & 0xff) != counter) | |
525 | return -1; | |
71f95118 | 526 | slotptr2++; |
3831530d MZ |
527 | counter--; |
528 | } | |
7385c28e | 529 | |
71f95118 | 530 | /* Save the real directory entry */ |
3831530d | 531 | realdent = (dir_entry *)slotptr2; |
1170e634 | 532 | while ((__u8 *)slotptr2 > get_contents_vfatname_block) { |
71f95118 | 533 | slotptr2--; |
3831530d | 534 | slot2str(slotptr2, l_name, &idx); |
71f95118 WD |
535 | } |
536 | } else { | |
537 | /* Save the real directory entry */ | |
7385c28e | 538 | realdent = (dir_entry *)slotptr; |
71f95118 WD |
539 | } |
540 | ||
541 | do { | |
542 | slotptr--; | |
7385c28e WD |
543 | if (slot2str(slotptr, l_name, &idx)) |
544 | break; | |
2d1a537d | 545 | } while (!(slotptr->id & LAST_LONG_ENTRY_MASK)); |
71f95118 WD |
546 | |
547 | l_name[idx] = '\0'; | |
7385c28e WD |
548 | if (*l_name == DELETED_FLAG) |
549 | *l_name = '\0'; | |
550 | else if (*l_name == aRING) | |
551 | *l_name = DELETED_FLAG; | |
71f95118 WD |
552 | downcase(l_name); |
553 | ||
554 | /* Return the real directory entry */ | |
555 | memcpy(retdent, realdent, sizeof(dir_entry)); | |
556 | ||
557 | return 0; | |
558 | } | |
559 | ||
71f95118 | 560 | /* Calculate short name checksum */ |
ff04f6d1 | 561 | static __u8 mkcksum(const char name[8], const char ext[3]) |
71f95118 WD |
562 | { |
563 | int i; | |
7385c28e | 564 | |
71f95118 WD |
565 | __u8 ret = 0; |
566 | ||
6ad77d88 | 567 | for (i = 0; i < 8; i++) |
ff04f6d1 | 568 | ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i]; |
6ad77d88 | 569 | for (i = 0; i < 3; i++) |
ff04f6d1 | 570 | ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i]; |
71f95118 WD |
571 | |
572 | return ret; | |
573 | } | |
71f95118 WD |
574 | |
575 | /* | |
576 | * Get the directory entry associated with 'filename' from the directory | |
577 | * starting at 'startsect' | |
578 | */ | |
9a800ac7 EN |
579 | __u8 get_dentfromdir_block[MAX_CLUSTSIZE] |
580 | __aligned(ARCH_DMA_MINALIGN); | |
7385c28e | 581 | |
9795e07b BT |
582 | static dir_entry *get_dentfromdir(fsdata *mydata, int startsect, |
583 | char *filename, dir_entry *retdent, | |
584 | int dols) | |
71f95118 | 585 | { |
7385c28e WD |
586 | __u16 prevcksum = 0xffff; |
587 | __u32 curclust = START(retdent); | |
588 | int files = 0, dirs = 0; | |
71f95118 | 589 | |
7385c28e | 590 | debug("get_dentfromdir: %s\n", filename); |
71f95118 | 591 | |
7385c28e WD |
592 | while (1) { |
593 | dir_entry *dentptr; | |
594 | ||
595 | int i; | |
596 | ||
597 | if (get_cluster(mydata, curclust, get_dentfromdir_block, | |
ac497771 | 598 | mydata->clust_size * mydata->sect_size) != 0) { |
7385c28e WD |
599 | debug("Error: reading directory block\n"); |
600 | return NULL; | |
601 | } | |
602 | ||
603 | dentptr = (dir_entry *)get_dentfromdir_block; | |
604 | ||
605 | for (i = 0; i < DIRENTSPERCLUST; i++) { | |
3831530d | 606 | char s_name[14], l_name[VFAT_MAXLEN_BYTES]; |
7385c28e WD |
607 | |
608 | l_name[0] = '\0'; | |
609 | if (dentptr->name[0] == DELETED_FLAG) { | |
610 | dentptr++; | |
611 | continue; | |
612 | } | |
613 | if ((dentptr->attr & ATTR_VOLUME)) { | |
cb940c7e RG |
614 | if (vfat_enabled && |
615 | (dentptr->attr & ATTR_VFAT) == ATTR_VFAT && | |
206d68fd | 616 | (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) { |
7385c28e WD |
617 | prevcksum = ((dir_slot *)dentptr)->alias_checksum; |
618 | get_vfatname(mydata, curclust, | |
619 | get_dentfromdir_block, | |
620 | dentptr, l_name); | |
621 | if (dols) { | |
622 | int isdir; | |
623 | char dirc; | |
624 | int doit = 0; | |
625 | ||
626 | isdir = (dentptr->attr & ATTR_DIR); | |
627 | ||
628 | if (isdir) { | |
629 | dirs++; | |
630 | dirc = '/'; | |
631 | doit = 1; | |
632 | } else { | |
633 | dirc = ' '; | |
634 | if (l_name[0] != 0) { | |
635 | files++; | |
636 | doit = 1; | |
637 | } | |
638 | } | |
639 | if (doit) { | |
640 | if (dirc == ' ') { | |
1ad0b98a SR |
641 | printf(" %8u %s%c\n", |
642 | FAT2CPU32(dentptr->size), | |
7385c28e WD |
643 | l_name, |
644 | dirc); | |
645 | } else { | |
646 | printf(" %s%c\n", | |
647 | l_name, | |
648 | dirc); | |
649 | } | |
650 | } | |
651 | dentptr++; | |
652 | continue; | |
653 | } | |
654 | debug("vfatname: |%s|\n", l_name); | |
cb940c7e | 655 | } else { |
7385c28e WD |
656 | /* Volume label or VFAT entry */ |
657 | dentptr++; | |
658 | continue; | |
659 | } | |
71f95118 | 660 | } |
7385c28e WD |
661 | if (dentptr->name[0] == 0) { |
662 | if (dols) { | |
663 | printf("\n%d file(s), %d dir(s)\n\n", | |
664 | files, dirs); | |
665 | } | |
666 | debug("Dentname == NULL - %d\n", i); | |
667 | return NULL; | |
71f95118 | 668 | } |
cb940c7e RG |
669 | if (vfat_enabled) { |
670 | __u8 csum = mkcksum(dentptr->name, dentptr->ext); | |
671 | if (dols && csum == prevcksum) { | |
672 | prevcksum = 0xffff; | |
673 | dentptr++; | |
674 | continue; | |
675 | } | |
7385c28e | 676 | } |
cb940c7e | 677 | |
7385c28e WD |
678 | get_name(dentptr, s_name); |
679 | if (dols) { | |
680 | int isdir = (dentptr->attr & ATTR_DIR); | |
681 | char dirc; | |
682 | int doit = 0; | |
683 | ||
684 | if (isdir) { | |
685 | dirs++; | |
686 | dirc = '/'; | |
687 | doit = 1; | |
688 | } else { | |
689 | dirc = ' '; | |
690 | if (s_name[0] != 0) { | |
691 | files++; | |
692 | doit = 1; | |
693 | } | |
694 | } | |
695 | ||
696 | if (doit) { | |
697 | if (dirc == ' ') { | |
1ad0b98a SR |
698 | printf(" %8u %s%c\n", |
699 | FAT2CPU32(dentptr->size), | |
7385c28e WD |
700 | s_name, dirc); |
701 | } else { | |
702 | printf(" %s%c\n", | |
703 | s_name, dirc); | |
704 | } | |
705 | } | |
706 | ||
707 | dentptr++; | |
708 | continue; | |
709 | } | |
710 | ||
711 | if (strcmp(filename, s_name) | |
712 | && strcmp(filename, l_name)) { | |
713 | debug("Mismatch: |%s|%s|\n", s_name, l_name); | |
714 | dentptr++; | |
715 | continue; | |
716 | } | |
717 | ||
718 | memcpy(retdent, dentptr, sizeof(dir_entry)); | |
719 | ||
720 | debug("DentName: %s", s_name); | |
721 | debug(", start: 0x%x", START(dentptr)); | |
722 | debug(", size: 0x%x %s\n", | |
723 | FAT2CPU32(dentptr->size), | |
724 | (dentptr->attr & ATTR_DIR) ? "(DIR)" : ""); | |
725 | ||
726 | return retdent; | |
71f95118 | 727 | } |
7385c28e WD |
728 | |
729 | curclust = get_fatent(mydata, curclust); | |
730 | if (CHECK_CLUST(curclust, mydata->fatsize)) { | |
731 | debug("curclust: 0x%x\n", curclust); | |
732 | printf("Invalid FAT entry\n"); | |
733 | return NULL; | |
71f95118 | 734 | } |
71f95118 | 735 | } |
71f95118 | 736 | |
7385c28e | 737 | return NULL; |
71f95118 WD |
738 | } |
739 | ||
71f95118 WD |
740 | /* |
741 | * Read boot sector and volume info from a FAT filesystem | |
742 | */ | |
743 | static int | |
9795e07b | 744 | read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize) |
71f95118 | 745 | { |
ac497771 | 746 | __u8 *block; |
71f95118 | 747 | volume_info *vistart; |
ac497771 SS |
748 | int ret = 0; |
749 | ||
750 | if (cur_dev == NULL) { | |
751 | debug("Error: no device selected\n"); | |
752 | return -1; | |
753 | } | |
754 | ||
9a800ac7 | 755 | block = memalign(ARCH_DMA_MINALIGN, cur_dev->blksz); |
ac497771 SS |
756 | if (block == NULL) { |
757 | debug("Error: allocating block\n"); | |
758 | return -1; | |
759 | } | |
71f95118 | 760 | |
9795e07b | 761 | if (disk_read(0, 1, block) < 0) { |
7385c28e | 762 | debug("Error: reading block\n"); |
ac497771 | 763 | goto fail; |
71f95118 WD |
764 | } |
765 | ||
766 | memcpy(bs, block, sizeof(boot_sector)); | |
7385c28e WD |
767 | bs->reserved = FAT2CPU16(bs->reserved); |
768 | bs->fat_length = FAT2CPU16(bs->fat_length); | |
769 | bs->secs_track = FAT2CPU16(bs->secs_track); | |
770 | bs->heads = FAT2CPU16(bs->heads); | |
771 | bs->total_sect = FAT2CPU32(bs->total_sect); | |
71f95118 WD |
772 | |
773 | /* FAT32 entries */ | |
774 | if (bs->fat_length == 0) { | |
775 | /* Assume FAT32 */ | |
776 | bs->fat32_length = FAT2CPU32(bs->fat32_length); | |
7385c28e | 777 | bs->flags = FAT2CPU16(bs->flags); |
71f95118 | 778 | bs->root_cluster = FAT2CPU32(bs->root_cluster); |
7385c28e WD |
779 | bs->info_sector = FAT2CPU16(bs->info_sector); |
780 | bs->backup_boot = FAT2CPU16(bs->backup_boot); | |
781 | vistart = (volume_info *)(block + sizeof(boot_sector)); | |
71f95118 WD |
782 | *fatsize = 32; |
783 | } else { | |
7385c28e | 784 | vistart = (volume_info *)&(bs->fat32_length); |
71f95118 WD |
785 | *fatsize = 0; |
786 | } | |
787 | memcpy(volinfo, vistart, sizeof(volume_info)); | |
788 | ||
71f95118 | 789 | if (*fatsize == 32) { |
7385c28e | 790 | if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0) |
ac497771 | 791 | goto exit; |
71f95118 | 792 | } else { |
651351fe | 793 | if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) { |
71f95118 | 794 | *fatsize = 12; |
ac497771 | 795 | goto exit; |
71f95118 | 796 | } |
651351fe | 797 | if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) { |
71f95118 | 798 | *fatsize = 16; |
ac497771 | 799 | goto exit; |
71f95118 WD |
800 | } |
801 | } | |
802 | ||
7385c28e | 803 | debug("Error: broken fs_type sign\n"); |
ac497771 SS |
804 | fail: |
805 | ret = -1; | |
806 | exit: | |
807 | free(block); | |
808 | return ret; | |
71f95118 WD |
809 | } |
810 | ||
45449980 | 811 | static int get_fs_info(fsdata *mydata) |
71f95118 | 812 | { |
7385c28e WD |
813 | boot_sector bs; |
814 | volume_info volinfo; | |
45449980 | 815 | int ret; |
7385c28e | 816 | |
45449980 RC |
817 | ret = read_bootsectandvi(&bs, &volinfo, &mydata->fatsize); |
818 | if (ret) { | |
7385c28e | 819 | debug("Error: reading boot sector\n"); |
45449980 | 820 | return ret; |
7385c28e WD |
821 | } |
822 | ||
40e21916 | 823 | if (mydata->fatsize == 32) { |
7385c28e | 824 | mydata->fatlength = bs.fat32_length; |
40e21916 | 825 | } else { |
7385c28e | 826 | mydata->fatlength = bs.fat_length; |
40e21916 | 827 | } |
7385c28e WD |
828 | |
829 | mydata->fat_sect = bs.reserved; | |
830 | ||
45449980 | 831 | mydata->rootdir_sect = mydata->fat_sect + mydata->fatlength * bs.fats; |
7385c28e | 832 | |
ac497771 | 833 | mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0]; |
7385c28e | 834 | mydata->clust_size = bs.cluster_size; |
46236b14 KM |
835 | if (mydata->sect_size != cur_part_info.blksz) { |
836 | printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n", | |
837 | mydata->sect_size, cur_part_info.blksz); | |
838 | return -1; | |
839 | } | |
7385c28e WD |
840 | |
841 | if (mydata->fatsize == 32) { | |
842 | mydata->data_begin = mydata->rootdir_sect - | |
843 | (mydata->clust_size * 2); | |
c6e3baa5 | 844 | mydata->root_cluster = bs.root_cluster; |
7385c28e | 845 | } else { |
45449980 RC |
846 | mydata->rootdir_size = ((bs.dir_entries[1] * (int)256 + |
847 | bs.dir_entries[0]) * | |
848 | sizeof(dir_entry)) / | |
849 | mydata->sect_size; | |
7385c28e | 850 | mydata->data_begin = mydata->rootdir_sect + |
45449980 | 851 | mydata->rootdir_size - |
7385c28e | 852 | (mydata->clust_size * 2); |
c6e3baa5 RC |
853 | mydata->root_cluster = (mydata->rootdir_sect - |
854 | mydata->data_begin) / | |
855 | mydata->clust_size; | |
7385c28e WD |
856 | } |
857 | ||
858 | mydata->fatbufnum = -1; | |
3c0ed9c3 | 859 | mydata->fat_dirty = 0; |
9a800ac7 | 860 | mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE); |
ac497771 SS |
861 | if (mydata->fatbuf == NULL) { |
862 | debug("Error: allocating memory\n"); | |
863 | return -1; | |
864 | } | |
71f95118 | 865 | |
cb940c7e RG |
866 | if (vfat_enabled) |
867 | debug("VFAT Support enabled\n"); | |
868 | ||
7385c28e WD |
869 | debug("FAT%d, fat_sect: %d, fatlength: %d\n", |
870 | mydata->fatsize, mydata->fat_sect, mydata->fatlength); | |
871 | debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n" | |
872 | "Data begins at: %d\n", | |
c6e3baa5 | 873 | mydata->root_cluster, |
7385c28e | 874 | mydata->rootdir_sect, |
ac497771 SS |
875 | mydata->rootdir_sect * mydata->sect_size, mydata->data_begin); |
876 | debug("Sector size: %d, cluster size: %d\n", mydata->sect_size, | |
877 | mydata->clust_size); | |
7385c28e | 878 | |
45449980 RC |
879 | return 0; |
880 | } | |
881 | ||
882 | __u8 do_fat_read_at_block[MAX_CLUSTSIZE] | |
883 | __aligned(ARCH_DMA_MINALIGN); | |
884 | ||
885 | int do_fat_read_at(const char *filename, loff_t pos, void *buffer, | |
886 | loff_t maxsize, int dols, int dogetsize, loff_t *size) | |
887 | { | |
888 | char fnamecopy[2048]; | |
889 | fsdata datablock; | |
890 | fsdata *mydata = &datablock; | |
891 | dir_entry *dentptr = NULL; | |
892 | __u16 prevcksum = 0xffff; | |
893 | char *subname = ""; | |
894 | __u32 cursect; | |
895 | int idx, isdir = 0; | |
896 | int files = 0, dirs = 0; | |
897 | int ret = -1; | |
898 | int firsttime; | |
899 | __u32 root_cluster = 0; | |
900 | __u32 read_blk; | |
901 | int rootdir_size = 0; | |
902 | int buffer_blk_cnt; | |
903 | int do_read; | |
904 | __u8 *dir_ptr; | |
905 | ||
906 | if (get_fs_info(mydata)) | |
907 | return -1; | |
908 | ||
909 | cursect = mydata->rootdir_sect; | |
910 | ||
7385c28e WD |
911 | /* "cwd" is always the root... */ |
912 | while (ISDIRDELIM(*filename)) | |
913 | filename++; | |
914 | ||
915 | /* Make a copy of the filename and convert it to lowercase */ | |
916 | strcpy(fnamecopy, filename); | |
917 | downcase(fnamecopy); | |
918 | ||
18a10d46 | 919 | root_reparse: |
7385c28e WD |
920 | if (*fnamecopy == '\0') { |
921 | if (!dols) | |
ac497771 | 922 | goto exit; |
71f95118 | 923 | |
7385c28e WD |
924 | dols = LS_ROOT; |
925 | } else if ((idx = dirdelim(fnamecopy)) >= 0) { | |
926 | isdir = 1; | |
927 | fnamecopy[idx] = '\0'; | |
928 | subname = fnamecopy + idx + 1; | |
929 | ||
930 | /* Handle multiple delimiters */ | |
931 | while (ISDIRDELIM(*subname)) | |
932 | subname++; | |
933 | } else if (dols) { | |
934 | isdir = 1; | |
71f95118 | 935 | } |
71f95118 | 936 | |
64f65e1e PM |
937 | buffer_blk_cnt = 0; |
938 | firsttime = 1; | |
7385c28e WD |
939 | while (1) { |
940 | int i; | |
941 | ||
64f65e1e PM |
942 | if (mydata->fatsize == 32 || firsttime) { |
943 | dir_ptr = do_fat_read_at_block; | |
944 | firsttime = 0; | |
945 | } else { | |
946 | /** | |
947 | * FAT16 sector buffer modification: | |
948 | * Each loop, the second buffered block is moved to | |
949 | * the buffer begin, and two next sectors are read | |
950 | * next to the previously moved one. So the sector | |
951 | * buffer keeps always 3 sectors for fat16. | |
952 | * And the current sector is the buffer second sector | |
953 | * beside the "firsttime" read, when it is the first one. | |
954 | * | |
955 | * PREFETCH_BLOCKS is 2 for FAT16 == loop[0:1] | |
956 | * n = computed root dir sector | |
957 | * loop | cursect-1 | cursect | cursect+1 | | |
958 | * 0 | sector n+0 | sector n+1 | none | | |
959 | * 1 | none | sector n+0 | sector n+1 | | |
960 | * 0 | sector n+1 | sector n+2 | sector n+3 | | |
961 | * 1 | sector n+3 | ... | |
962 | */ | |
963 | dir_ptr = (do_fat_read_at_block + mydata->sect_size); | |
964 | memcpy(do_fat_read_at_block, dir_ptr, mydata->sect_size); | |
965 | } | |
966 | ||
967 | do_read = 1; | |
968 | ||
969 | if (mydata->fatsize == 32 && buffer_blk_cnt) | |
970 | do_read = 0; | |
971 | ||
972 | if (do_read) { | |
973 | read_blk = (mydata->fatsize == 32) ? | |
974 | mydata->clust_size : PREFETCH_BLOCKS; | |
975 | ||
976 | debug("FAT read(sect=%d, cnt:%d), clust_size=%d, DIRENTSPERBLOCK=%zd\n", | |
977 | cursect, read_blk, mydata->clust_size, DIRENTSPERBLOCK); | |
7385c28e | 978 | |
64f65e1e | 979 | if (disk_read(cursect, read_blk, dir_ptr) < 0) { |
cd1b042c BT |
980 | debug("Error: reading rootdir block\n"); |
981 | goto exit; | |
982 | } | |
7385c28e | 983 | |
64f65e1e | 984 | dentptr = (dir_entry *)dir_ptr; |
cd1b042c | 985 | } |
7385c28e WD |
986 | |
987 | for (i = 0; i < DIRENTSPERBLOCK; i++) { | |
3831530d | 988 | char s_name[14], l_name[VFAT_MAXLEN_BYTES]; |
ff04f6d1 | 989 | __u8 csum; |
7385c28e WD |
990 | |
991 | l_name[0] = '\0'; | |
3831530d MZ |
992 | if (dentptr->name[0] == DELETED_FLAG) { |
993 | dentptr++; | |
994 | continue; | |
995 | } | |
ff04f6d1 | 996 | |
cb940c7e RG |
997 | if (vfat_enabled) |
998 | csum = mkcksum(dentptr->name, dentptr->ext); | |
999 | ||
ff04f6d1 | 1000 | if (dentptr->attr & ATTR_VOLUME) { |
cb940c7e RG |
1001 | if (vfat_enabled && |
1002 | (dentptr->attr & ATTR_VFAT) == ATTR_VFAT && | |
7385c28e WD |
1003 | (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) { |
1004 | prevcksum = | |
1005 | ((dir_slot *)dentptr)->alias_checksum; | |
1006 | ||
3831530d | 1007 | get_vfatname(mydata, |
40e21916 | 1008 | root_cluster, |
64f65e1e | 1009 | dir_ptr, |
7385c28e WD |
1010 | dentptr, l_name); |
1011 | ||
1012 | if (dols == LS_ROOT) { | |
1013 | char dirc; | |
1014 | int doit = 0; | |
1015 | int isdir = | |
1016 | (dentptr->attr & ATTR_DIR); | |
1017 | ||
1018 | if (isdir) { | |
1019 | dirs++; | |
1020 | dirc = '/'; | |
1021 | doit = 1; | |
1022 | } else { | |
1023 | dirc = ' '; | |
1024 | if (l_name[0] != 0) { | |
1025 | files++; | |
1026 | doit = 1; | |
1027 | } | |
1028 | } | |
1029 | if (doit) { | |
1030 | if (dirc == ' ') { | |
1ad0b98a SR |
1031 | printf(" %8u %s%c\n", |
1032 | FAT2CPU32(dentptr->size), | |
7385c28e WD |
1033 | l_name, |
1034 | dirc); | |
1035 | } else { | |
1036 | printf(" %s%c\n", | |
1037 | l_name, | |
1038 | dirc); | |
1039 | } | |
1040 | } | |
1041 | dentptr++; | |
1042 | continue; | |
1043 | } | |
1044 | debug("Rootvfatname: |%s|\n", | |
1045 | l_name); | |
cb940c7e | 1046 | } else { |
7385c28e WD |
1047 | /* Volume label or VFAT entry */ |
1048 | dentptr++; | |
1049 | continue; | |
1050 | } | |
1051 | } else if (dentptr->name[0] == 0) { | |
1052 | debug("RootDentname == NULL - %d\n", i); | |
1053 | if (dols == LS_ROOT) { | |
1054 | printf("\n%d file(s), %d dir(s)\n\n", | |
1055 | files, dirs); | |
ac497771 | 1056 | ret = 0; |
7385c28e | 1057 | } |
ac497771 | 1058 | goto exit; |
71f95118 | 1059 | } |
cb940c7e RG |
1060 | else if (vfat_enabled && |
1061 | dols == LS_ROOT && csum == prevcksum) { | |
bf34e7d9 | 1062 | prevcksum = 0xffff; |
7385c28e WD |
1063 | dentptr++; |
1064 | continue; | |
71f95118 | 1065 | } |
cb940c7e | 1066 | |
7385c28e WD |
1067 | get_name(dentptr, s_name); |
1068 | ||
1069 | if (dols == LS_ROOT) { | |
1070 | int isdir = (dentptr->attr & ATTR_DIR); | |
1071 | char dirc; | |
1072 | int doit = 0; | |
1073 | ||
1074 | if (isdir) { | |
1075 | dirc = '/'; | |
1076 | if (s_name[0] != 0) { | |
1077 | dirs++; | |
1078 | doit = 1; | |
1079 | } | |
1080 | } else { | |
1081 | dirc = ' '; | |
1082 | if (s_name[0] != 0) { | |
1083 | files++; | |
1084 | doit = 1; | |
1085 | } | |
1086 | } | |
1087 | if (doit) { | |
1088 | if (dirc == ' ') { | |
1ad0b98a SR |
1089 | printf(" %8u %s%c\n", |
1090 | FAT2CPU32(dentptr->size), | |
7385c28e WD |
1091 | s_name, dirc); |
1092 | } else { | |
1093 | printf(" %s%c\n", | |
1094 | s_name, dirc); | |
1095 | } | |
1096 | } | |
1097 | dentptr++; | |
1098 | continue; | |
1099 | } | |
1100 | ||
1101 | if (strcmp(fnamecopy, s_name) | |
1102 | && strcmp(fnamecopy, l_name)) { | |
1103 | debug("RootMismatch: |%s|%s|\n", s_name, | |
1104 | l_name); | |
1105 | dentptr++; | |
1106 | continue; | |
1107 | } | |
1108 | ||
1109 | if (isdir && !(dentptr->attr & ATTR_DIR)) | |
ac497771 | 1110 | goto exit; |
7385c28e WD |
1111 | |
1112 | debug("RootName: %s", s_name); | |
1113 | debug(", start: 0x%x", START(dentptr)); | |
1114 | debug(", size: 0x%x %s\n", | |
1115 | FAT2CPU32(dentptr->size), | |
1116 | isdir ? "(DIR)" : ""); | |
1117 | ||
1118 | goto rootdir_done; /* We got a match */ | |
71f95118 | 1119 | } |
64f65e1e | 1120 | debug("END LOOP: buffer_blk_cnt=%d clust_size=%d\n", buffer_blk_cnt, |
7385c28e WD |
1121 | mydata->clust_size); |
1122 | ||
1123 | /* | |
1124 | * On FAT32 we must fetch the FAT entries for the next | |
1125 | * root directory clusters when a cluster has been | |
1126 | * completely processed. | |
1127 | */ | |
64f65e1e | 1128 | ++buffer_blk_cnt; |
cd1b042c BT |
1129 | int rootdir_end = 0; |
1130 | if (mydata->fatsize == 32) { | |
64f65e1e | 1131 | if (buffer_blk_cnt == mydata->clust_size) { |
cd1b042c BT |
1132 | int nxtsect = 0; |
1133 | int nxt_clust = 0; | |
7385c28e | 1134 | |
cd1b042c BT |
1135 | nxt_clust = get_fatent(mydata, root_cluster); |
1136 | rootdir_end = CHECK_CLUST(nxt_clust, 32); | |
7385c28e | 1137 | |
cd1b042c BT |
1138 | nxtsect = mydata->data_begin + |
1139 | (nxt_clust * mydata->clust_size); | |
7385c28e | 1140 | |
cd1b042c | 1141 | root_cluster = nxt_clust; |
7385c28e | 1142 | |
cd1b042c | 1143 | cursect = nxtsect; |
64f65e1e | 1144 | buffer_blk_cnt = 0; |
cd1b042c | 1145 | } |
71f95118 | 1146 | } else { |
64f65e1e PM |
1147 | if (buffer_blk_cnt == PREFETCH_BLOCKS) |
1148 | buffer_blk_cnt = 0; | |
cd1b042c BT |
1149 | |
1150 | rootdir_end = (++cursect - mydata->rootdir_sect >= | |
1151 | rootdir_size); | |
71f95118 | 1152 | } |
3f270f42 EH |
1153 | |
1154 | /* If end of rootdir reached */ | |
cd1b042c | 1155 | if (rootdir_end) { |
3f270f42 EH |
1156 | if (dols == LS_ROOT) { |
1157 | printf("\n%d file(s), %d dir(s)\n\n", | |
1158 | files, dirs); | |
1ad0b98a | 1159 | *size = 0; |
3f270f42 | 1160 | } |
ac497771 | 1161 | goto exit; |
3f270f42 | 1162 | } |
7385c28e WD |
1163 | } |
1164 | rootdir_done: | |
71f95118 | 1165 | |
7385c28e | 1166 | firsttime = 1; |
71f95118 | 1167 | |
7385c28e WD |
1168 | while (isdir) { |
1169 | int startsect = mydata->data_begin | |
1170 | + START(dentptr) * mydata->clust_size; | |
1171 | dir_entry dent; | |
1172 | char *nextname = NULL; | |
71f95118 | 1173 | |
7385c28e WD |
1174 | dent = *dentptr; |
1175 | dentptr = &dent; | |
71f95118 | 1176 | |
7385c28e WD |
1177 | idx = dirdelim(subname); |
1178 | ||
1179 | if (idx >= 0) { | |
1180 | subname[idx] = '\0'; | |
1181 | nextname = subname + idx + 1; | |
1182 | /* Handle multiple delimiters */ | |
1183 | while (ISDIRDELIM(*nextname)) | |
1184 | nextname++; | |
1185 | if (dols && *nextname == '\0') | |
1186 | firsttime = 0; | |
1187 | } else { | |
1188 | if (dols && firsttime) { | |
1189 | firsttime = 0; | |
1190 | } else { | |
1191 | isdir = 0; | |
1192 | } | |
1193 | } | |
1194 | ||
1195 | if (get_dentfromdir(mydata, startsect, subname, dentptr, | |
1196 | isdir ? 0 : dols) == NULL) { | |
1197 | if (dols && !isdir) | |
1ad0b98a | 1198 | *size = 0; |
ac497771 | 1199 | goto exit; |
7385c28e WD |
1200 | } |
1201 | ||
7ee46ceb BT |
1202 | if (isdir && !(dentptr->attr & ATTR_DIR)) |
1203 | goto exit; | |
1204 | ||
18a10d46 SW |
1205 | /* |
1206 | * If we are looking for a directory, and found a directory | |
1207 | * type entry, and the entry is for the root directory (as | |
1208 | * denoted by a cluster number of 0), jump back to the start | |
1209 | * of the function, since at least on FAT12/16, the root dir | |
1210 | * lives in a hard-coded location and needs special handling | |
1211 | * to parse, rather than simply following the cluster linked | |
1212 | * list in the FAT, like other directories. | |
1213 | */ | |
1214 | if (isdir && (dentptr->attr & ATTR_DIR) && !START(dentptr)) { | |
1215 | /* | |
1216 | * Modify the filename to remove the prefix that gets | |
1217 | * back to the root directory, so the initial root dir | |
1218 | * parsing code can continue from where we are without | |
1219 | * confusion. | |
1220 | */ | |
1221 | strcpy(fnamecopy, nextname ?: ""); | |
1222 | /* | |
1223 | * Set up state the same way as the function does when | |
1224 | * first started. This is required for the root dir | |
1225 | * parsing code operates in its expected environment. | |
1226 | */ | |
1227 | subname = ""; | |
1228 | cursect = mydata->rootdir_sect; | |
1229 | isdir = 0; | |
1230 | goto root_reparse; | |
1231 | } | |
1232 | ||
7ee46ceb | 1233 | if (idx >= 0) |
7385c28e | 1234 | subname = nextname; |
71f95118 | 1235 | } |
71f95118 | 1236 | |
1ad0b98a SR |
1237 | if (dogetsize) { |
1238 | *size = FAT2CPU32(dentptr->size); | |
1239 | ret = 0; | |
1240 | } else { | |
1241 | ret = get_contents(mydata, dentptr, pos, buffer, maxsize, size); | |
1242 | } | |
1243 | debug("Size: %u, got: %llu\n", FAT2CPU32(dentptr->size), *size); | |
71f95118 | 1244 | |
ac497771 SS |
1245 | exit: |
1246 | free(mydata->fatbuf); | |
7385c28e WD |
1247 | return ret; |
1248 | } | |
71f95118 | 1249 | |
c6e3baa5 RC |
1250 | |
1251 | /* | |
1252 | * Directory iterator, to simplify filesystem traversal | |
1253 | * | |
1254 | * Implements an iterator pattern to traverse directory tables, | |
1255 | * transparently handling directory tables split across multiple | |
1256 | * clusters, and the difference between FAT12/FAT16 root directory | |
1257 | * (contiguous) and subdirectories + FAT32 root (chained). | |
1258 | * | |
1259 | * Rough usage: | |
1260 | * | |
1261 | * for (fat_itr_root(&itr, fsdata); fat_itr_next(&itr); ) { | |
1262 | * // to traverse down to a subdirectory pointed to by | |
1263 | * // current iterator position: | |
1264 | * fat_itr_child(&itr, &itr); | |
1265 | * } | |
1266 | * | |
1267 | * For more complete example, see fat_itr_resolve() | |
1268 | */ | |
1269 | ||
1270 | typedef struct { | |
1271 | fsdata *fsdata; /* filesystem parameters */ | |
1272 | unsigned clust; /* current cluster */ | |
1273 | int last_cluster; /* set once we've read last cluster */ | |
1274 | int is_root; /* is iterator at root directory */ | |
1275 | int remaining; /* remaining dent's in current cluster */ | |
1276 | ||
1277 | /* current iterator position values: */ | |
1278 | dir_entry *dent; /* current directory entry */ | |
1279 | char l_name[VFAT_MAXLEN_BYTES]; /* long (vfat) name */ | |
1280 | char s_name[14]; /* short 8.3 name */ | |
1281 | char *name; /* l_name if there is one, else s_name */ | |
1282 | ||
1283 | /* storage for current cluster in memory: */ | |
1284 | u8 block[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN); | |
1285 | } fat_itr; | |
1286 | ||
1287 | static int fat_itr_isdir(fat_itr *itr); | |
1288 | ||
1289 | /** | |
1290 | * fat_itr_root() - initialize an iterator to start at the root | |
1291 | * directory | |
1292 | * | |
1293 | * @itr: iterator to initialize | |
1294 | * @fsdata: filesystem data for the partition | |
1295 | * @return 0 on success, else -errno | |
1296 | */ | |
1297 | static int fat_itr_root(fat_itr *itr, fsdata *fsdata) | |
1298 | { | |
1299 | if (get_fs_info(fsdata)) | |
1300 | return -ENXIO; | |
1301 | ||
1302 | itr->fsdata = fsdata; | |
1303 | itr->clust = fsdata->root_cluster; | |
1304 | itr->dent = NULL; | |
1305 | itr->remaining = 0; | |
1306 | itr->last_cluster = 0; | |
1307 | itr->is_root = 1; | |
1308 | ||
1309 | return 0; | |
1310 | } | |
1311 | ||
1312 | /** | |
1313 | * fat_itr_child() - initialize an iterator to descend into a sub- | |
1314 | * directory | |
1315 | * | |
1316 | * Initializes 'itr' to iterate the contents of the directory at | |
1317 | * the current cursor position of 'parent'. It is an error to | |
1318 | * call this if the current cursor of 'parent' is pointing at a | |
1319 | * regular file. | |
1320 | * | |
1321 | * Note that 'itr' and 'parent' can be the same pointer if you do | |
1322 | * not need to preserve 'parent' after this call, which is useful | |
1323 | * for traversing directory structure to resolve a file/directory. | |
1324 | * | |
1325 | * @itr: iterator to initialize | |
1326 | * @parent: the iterator pointing at a directory entry in the | |
1327 | * parent directory of the directory to iterate | |
1328 | */ | |
1329 | static void fat_itr_child(fat_itr *itr, fat_itr *parent) | |
1330 | { | |
1331 | fsdata *mydata = parent->fsdata; /* for silly macros */ | |
1332 | unsigned clustnum = START(parent->dent); | |
1333 | ||
1334 | assert(fat_itr_isdir(parent)); | |
1335 | ||
1336 | itr->fsdata = parent->fsdata; | |
1337 | if (clustnum > 0) { | |
1338 | itr->clust = clustnum; | |
1339 | } else { | |
1340 | itr->clust = parent->fsdata->root_cluster; | |
1341 | } | |
1342 | itr->dent = NULL; | |
1343 | itr->remaining = 0; | |
1344 | itr->last_cluster = 0; | |
1345 | itr->is_root = 0; | |
1346 | } | |
1347 | ||
1348 | static void *next_cluster(fat_itr *itr) | |
1349 | { | |
1350 | fsdata *mydata = itr->fsdata; /* for silly macros */ | |
1351 | int ret; | |
1352 | u32 sect; | |
1353 | ||
1354 | /* have we reached the end? */ | |
1355 | if (itr->last_cluster) | |
1356 | return NULL; | |
1357 | ||
1358 | sect = clust_to_sect(itr->fsdata, itr->clust); | |
1359 | ||
1360 | debug("FAT read(sect=%d), clust_size=%d, DIRENTSPERBLOCK=%zd\n", | |
1361 | sect, itr->fsdata->clust_size, DIRENTSPERBLOCK); | |
1362 | ||
1363 | /* | |
1364 | * NOTE: do_fat_read_at() had complicated logic to deal w/ | |
1365 | * vfat names that span multiple clusters in the fat16 case, | |
1366 | * which get_dentfromdir() probably also needed (and was | |
1367 | * missing). And not entirely sure what fat32 didn't have | |
1368 | * the same issue.. We solve that by only caring about one | |
1369 | * dent at a time and iteratively constructing the vfat long | |
1370 | * name. | |
1371 | */ | |
1372 | ret = disk_read(sect, itr->fsdata->clust_size, | |
1373 | itr->block); | |
1374 | if (ret < 0) { | |
1375 | debug("Error: reading block\n"); | |
1376 | return NULL; | |
1377 | } | |
1378 | ||
1379 | if (itr->is_root && itr->fsdata->fatsize != 32) { | |
1380 | itr->clust++; | |
1381 | sect = clust_to_sect(itr->fsdata, itr->clust); | |
1382 | if (sect - itr->fsdata->rootdir_sect >= | |
1383 | itr->fsdata->rootdir_size) { | |
1384 | debug("cursect: 0x%x\n", itr->clust); | |
1385 | itr->last_cluster = 1; | |
1386 | } | |
1387 | } else { | |
1388 | itr->clust = get_fatent(itr->fsdata, itr->clust); | |
1389 | if (CHECK_CLUST(itr->clust, itr->fsdata->fatsize)) { | |
1390 | debug("cursect: 0x%x\n", itr->clust); | |
1391 | itr->last_cluster = 1; | |
1392 | } | |
1393 | } | |
1394 | ||
1395 | return itr->block; | |
1396 | } | |
1397 | ||
1398 | static dir_entry *next_dent(fat_itr *itr) | |
1399 | { | |
1400 | if (itr->remaining == 0) { | |
1401 | struct dir_entry *dent = next_cluster(itr); | |
1402 | unsigned nbytes = itr->fsdata->sect_size * | |
1403 | itr->fsdata->clust_size; | |
1404 | ||
1405 | /* have we reached the last cluster? */ | |
1406 | if (!dent) | |
1407 | return NULL; | |
1408 | ||
1409 | itr->remaining = nbytes / sizeof(dir_entry) - 1; | |
1410 | itr->dent = dent; | |
1411 | } else { | |
1412 | itr->remaining--; | |
1413 | itr->dent++; | |
1414 | } | |
1415 | ||
1416 | /* have we reached the last valid entry? */ | |
1417 | if (itr->dent->name[0] == 0) | |
1418 | return NULL; | |
1419 | ||
1420 | return itr->dent; | |
1421 | } | |
1422 | ||
1423 | static dir_entry *extract_vfat_name(fat_itr *itr) | |
1424 | { | |
1425 | struct dir_entry *dent = itr->dent; | |
1426 | int seqn = itr->dent->name[0] & ~LAST_LONG_ENTRY_MASK; | |
1427 | u8 chksum, alias_checksum = ((dir_slot *)dent)->alias_checksum; | |
1428 | int n = 0; | |
1429 | ||
1430 | while (seqn--) { | |
1431 | char buf[13]; | |
1432 | int idx = 0; | |
1433 | ||
1434 | slot2str((dir_slot *)dent, buf, &idx); | |
1435 | ||
1436 | /* shift accumulated long-name up and copy new part in: */ | |
1437 | memmove(itr->l_name + idx, itr->l_name, n); | |
1438 | memcpy(itr->l_name, buf, idx); | |
1439 | n += idx; | |
1440 | ||
1441 | dent = next_dent(itr); | |
1442 | if (!dent) | |
1443 | return NULL; | |
1444 | } | |
1445 | ||
1446 | itr->l_name[n] = '\0'; | |
1447 | ||
1448 | chksum = mkcksum(dent->name, dent->ext); | |
1449 | ||
1450 | /* checksum mismatch could mean deleted file, etc.. skip it: */ | |
1451 | if (chksum != alias_checksum) { | |
1452 | debug("** chksum=%x, alias_checksum=%x, l_name=%s, s_name=%8s.%3s\n", | |
1453 | chksum, alias_checksum, itr->l_name, dent->name, dent->ext); | |
1454 | return NULL; | |
1455 | } | |
1456 | ||
1457 | return dent; | |
1458 | } | |
1459 | ||
1460 | /** | |
1461 | * fat_itr_next() - step to the next entry in a directory | |
1462 | * | |
1463 | * Must be called once on a new iterator before the cursor is valid. | |
1464 | * | |
1465 | * @itr: the iterator to iterate | |
1466 | * @return boolean, 1 if success or 0 if no more entries in the | |
1467 | * current directory | |
1468 | */ | |
1469 | static int fat_itr_next(fat_itr *itr) | |
1470 | { | |
1471 | dir_entry *dent; | |
1472 | ||
1473 | itr->name = NULL; | |
1474 | ||
1475 | while (1) { | |
1476 | dent = next_dent(itr); | |
1477 | if (!dent) | |
1478 | return 0; | |
1479 | ||
1480 | if (dent->name[0] == DELETED_FLAG || | |
1481 | dent->name[0] == aRING) | |
1482 | continue; | |
1483 | ||
1484 | if (dent->attr & ATTR_VOLUME) { | |
1485 | if (vfat_enabled && | |
1486 | (dent->attr & ATTR_VFAT) == ATTR_VFAT && | |
1487 | (dent->name[0] & LAST_LONG_ENTRY_MASK)) { | |
1488 | dent = extract_vfat_name(itr); | |
1489 | if (!dent) | |
1490 | continue; | |
1491 | itr->name = itr->l_name; | |
1492 | break; | |
1493 | } else { | |
1494 | /* Volume label or VFAT entry, skip */ | |
1495 | continue; | |
1496 | } | |
1497 | } | |
1498 | ||
1499 | break; | |
1500 | } | |
1501 | ||
1502 | get_name(dent, itr->s_name); | |
1503 | if (!itr->name) | |
1504 | itr->name = itr->s_name; | |
1505 | ||
1506 | return 1; | |
1507 | } | |
1508 | ||
1509 | /** | |
1510 | * fat_itr_isdir() - is current cursor position pointing to a directory | |
1511 | * | |
1512 | * @itr: the iterator | |
1513 | * @return true if cursor is at a directory | |
1514 | */ | |
1515 | static int fat_itr_isdir(fat_itr *itr) | |
1516 | { | |
1517 | return !!(itr->dent->attr & ATTR_DIR); | |
1518 | } | |
1519 | ||
1520 | /* | |
1521 | * Helpers: | |
1522 | */ | |
1523 | ||
1524 | #define TYPE_FILE 0x1 | |
1525 | #define TYPE_DIR 0x2 | |
1526 | #define TYPE_ANY (TYPE_FILE | TYPE_DIR) | |
1527 | ||
1528 | /** | |
1529 | * fat_itr_resolve() - traverse directory structure to resolve the | |
1530 | * requested path. | |
1531 | * | |
1532 | * Traverse directory structure to the requested path. If the specified | |
1533 | * path is to a directory, this will descend into the directory and | |
1534 | * leave it iterator at the start of the directory. If the path is to a | |
1535 | * file, it will leave the iterator in the parent directory with current | |
1536 | * cursor at file's entry in the directory. | |
1537 | * | |
1538 | * @itr: iterator initialized to root | |
1539 | * @path: the requested path | |
1540 | * @type: bitmask of allowable file types | |
1541 | * @return 0 on success or -errno | |
1542 | */ | |
1543 | static int fat_itr_resolve(fat_itr *itr, const char *path, unsigned type) | |
1544 | { | |
1545 | const char *next; | |
1546 | ||
1547 | /* chomp any extra leading slashes: */ | |
1548 | while (path[0] && ISDIRDELIM(path[0])) | |
1549 | path++; | |
1550 | ||
1551 | /* are we at the end? */ | |
1552 | if (strlen(path) == 0) { | |
1553 | if (!(type & TYPE_DIR)) | |
1554 | return -ENOENT; | |
1555 | return 0; | |
1556 | } | |
1557 | ||
1558 | /* find length of next path entry: */ | |
1559 | next = path; | |
1560 | while (next[0] && !ISDIRDELIM(next[0])) | |
1561 | next++; | |
1562 | ||
1563 | while (fat_itr_next(itr)) { | |
1564 | int match = 0; | |
1565 | unsigned n = max(strlen(itr->name), (size_t)(next - path)); | |
1566 | ||
1567 | /* check both long and short name: */ | |
1568 | if (!strncasecmp(path, itr->name, n)) | |
1569 | match = 1; | |
1570 | else if (itr->name != itr->s_name && | |
1571 | !strncasecmp(path, itr->s_name, n)) | |
1572 | match = 1; | |
1573 | ||
1574 | if (!match) | |
1575 | continue; | |
1576 | ||
1577 | if (fat_itr_isdir(itr)) { | |
1578 | /* recurse into directory: */ | |
1579 | fat_itr_child(itr, itr); | |
1580 | return fat_itr_resolve(itr, next, type); | |
1581 | } else if (next[0]) { | |
1582 | /* | |
1583 | * If next is not empty then we have a case | |
1584 | * like: /path/to/realfile/nonsense | |
1585 | */ | |
1586 | debug("bad trailing path: %s\n", next); | |
1587 | return -ENOENT; | |
1588 | } else if (!(type & TYPE_FILE)) { | |
1589 | return -ENOTDIR; | |
1590 | } else { | |
1591 | return 0; | |
1592 | } | |
1593 | } | |
1594 | ||
1595 | return -ENOENT; | |
1596 | } | |
1597 | ||
1ad0b98a SR |
1598 | int do_fat_read(const char *filename, void *buffer, loff_t maxsize, int dols, |
1599 | loff_t *actread) | |
1170e634 | 1600 | { |
1ad0b98a | 1601 | return do_fat_read_at(filename, 0, buffer, maxsize, dols, 0, actread); |
1170e634 BT |
1602 | } |
1603 | ||
9795e07b | 1604 | int file_fat_detectfs(void) |
71f95118 | 1605 | { |
7385c28e WD |
1606 | boot_sector bs; |
1607 | volume_info volinfo; | |
1608 | int fatsize; | |
1609 | char vol_label[12]; | |
71f95118 | 1610 | |
7385c28e | 1611 | if (cur_dev == NULL) { |
7205e407 WD |
1612 | printf("No current device\n"); |
1613 | return 1; | |
1614 | } | |
7385c28e | 1615 | |
fc843a02 | 1616 | #if defined(CONFIG_IDE) || \ |
10e40d54 | 1617 | defined(CONFIG_SATA) || \ |
c649e3c9 | 1618 | defined(CONFIG_SCSI) || \ |
dd60d122 | 1619 | defined(CONFIG_CMD_USB) || \ |
21f6f963 | 1620 | defined(CONFIG_MMC) |
7205e407 | 1621 | printf("Interface: "); |
7385c28e WD |
1622 | switch (cur_dev->if_type) { |
1623 | case IF_TYPE_IDE: | |
1624 | printf("IDE"); | |
1625 | break; | |
1626 | case IF_TYPE_SATA: | |
1627 | printf("SATA"); | |
1628 | break; | |
1629 | case IF_TYPE_SCSI: | |
1630 | printf("SCSI"); | |
1631 | break; | |
1632 | case IF_TYPE_ATAPI: | |
1633 | printf("ATAPI"); | |
1634 | break; | |
1635 | case IF_TYPE_USB: | |
1636 | printf("USB"); | |
1637 | break; | |
1638 | case IF_TYPE_DOC: | |
1639 | printf("DOC"); | |
1640 | break; | |
1641 | case IF_TYPE_MMC: | |
1642 | printf("MMC"); | |
1643 | break; | |
1644 | default: | |
1645 | printf("Unknown"); | |
7205e407 | 1646 | } |
7385c28e | 1647 | |
bcce53d0 | 1648 | printf("\n Device %d: ", cur_dev->devnum); |
7205e407 WD |
1649 | dev_print(cur_dev); |
1650 | #endif | |
7385c28e WD |
1651 | |
1652 | if (read_bootsectandvi(&bs, &volinfo, &fatsize)) { | |
7205e407 WD |
1653 | printf("\nNo valid FAT fs found\n"); |
1654 | return 1; | |
1655 | } | |
7385c28e WD |
1656 | |
1657 | memcpy(vol_label, volinfo.volume_label, 11); | |
7205e407 | 1658 | vol_label[11] = '\0'; |
7385c28e WD |
1659 | volinfo.fs_type[5] = '\0'; |
1660 | ||
461f86e6 | 1661 | printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label); |
7385c28e | 1662 | |
7205e407 | 1663 | return 0; |
71f95118 WD |
1664 | } |
1665 | ||
9795e07b | 1666 | int file_fat_ls(const char *dir) |
71f95118 | 1667 | { |
1ad0b98a SR |
1668 | loff_t size; |
1669 | ||
1670 | return do_fat_read(dir, NULL, 0, LS_YES, &size); | |
71f95118 WD |
1671 | } |
1672 | ||
b7b5f319 SW |
1673 | int fat_exists(const char *filename) |
1674 | { | |
1ad0b98a SR |
1675 | int ret; |
1676 | loff_t size; | |
1677 | ||
1678 | ret = do_fat_read_at(filename, 0, NULL, 0, LS_NO, 1, &size); | |
1679 | return ret == 0; | |
b7b5f319 SW |
1680 | } |
1681 | ||
d455d878 | 1682 | int fat_size(const char *filename, loff_t *size) |
cf659819 | 1683 | { |
d455d878 | 1684 | return do_fat_read_at(filename, 0, NULL, 0, LS_NO, 1, size); |
cf659819 SW |
1685 | } |
1686 | ||
1ad0b98a SR |
1687 | int file_fat_read_at(const char *filename, loff_t pos, void *buffer, |
1688 | loff_t maxsize, loff_t *actread) | |
71f95118 | 1689 | { |
7385c28e | 1690 | printf("reading %s\n", filename); |
1ad0b98a SR |
1691 | return do_fat_read_at(filename, pos, buffer, maxsize, LS_NO, 0, |
1692 | actread); | |
1170e634 BT |
1693 | } |
1694 | ||
1ad0b98a | 1695 | int file_fat_read(const char *filename, void *buffer, int maxsize) |
1170e634 | 1696 | { |
1ad0b98a SR |
1697 | loff_t actread; |
1698 | int ret; | |
1699 | ||
1700 | ret = file_fat_read_at(filename, 0, buffer, maxsize, &actread); | |
1701 | if (ret) | |
1702 | return ret; | |
1703 | else | |
1704 | return actread; | |
71f95118 | 1705 | } |
e6d52415 | 1706 | |
d455d878 SR |
1707 | int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len, |
1708 | loff_t *actread) | |
e6d52415 | 1709 | { |
1ad0b98a | 1710 | int ret; |
e6d52415 | 1711 | |
d455d878 SR |
1712 | ret = file_fat_read_at(filename, offset, buf, len, actread); |
1713 | if (ret) | |
e6d52415 | 1714 | printf("** Unable to read file %s **\n", filename); |
e6d52415 | 1715 | |
d455d878 | 1716 | return ret; |
e6d52415 SG |
1717 | } |
1718 | ||
1719 | void fat_close(void) | |
1720 | { | |
1721 | } |