]>
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> | |
13 | #include <config.h> | |
ac497771 | 14 | #include <exports.h> |
71f95118 WD |
15 | #include <fat.h> |
16 | #include <asm/byteorder.h> | |
7205e407 | 17 | #include <part.h> |
9a800ac7 EN |
18 | #include <malloc.h> |
19 | #include <linux/compiler.h> | |
fb7e16cc | 20 | #include <linux/ctype.h> |
71f95118 | 21 | |
cb940c7e RG |
22 | #ifdef CONFIG_SUPPORT_VFAT |
23 | static const int vfat_enabled = 1; | |
24 | #else | |
25 | static const int vfat_enabled = 0; | |
26 | #endif | |
27 | ||
71f95118 WD |
28 | /* |
29 | * Convert a string to lowercase. | |
30 | */ | |
9795e07b | 31 | static void downcase(char *str) |
71f95118 WD |
32 | { |
33 | while (*str != '\0') { | |
fb7e16cc | 34 | *str = tolower(*str); |
71f95118 WD |
35 | str++; |
36 | } | |
37 | } | |
38 | ||
9813b750 | 39 | static block_dev_desc_t *cur_dev; |
9813b750 | 40 | static disk_partition_t cur_part_info; |
7385c28e | 41 | |
9813b750 | 42 | #define DOS_BOOT_MAGIC_OFFSET 0x1fe |
7205e407 | 43 | #define DOS_FS_TYPE_OFFSET 0x36 |
66c2d73c | 44 | #define DOS_FS32_TYPE_OFFSET 0x52 |
71f95118 | 45 | |
9813b750 | 46 | static int disk_read(__u32 block, __u32 nr_blocks, void *buf) |
71f95118 | 47 | { |
9813b750 | 48 | if (!cur_dev || !cur_dev->block_read) |
7205e407 | 49 | return -1; |
7385c28e | 50 | |
9813b750 KM |
51 | return cur_dev->block_read(cur_dev->dev, |
52 | cur_part_info.start + block, nr_blocks, buf); | |
71f95118 WD |
53 | } |
54 | ||
5e8f9831 | 55 | int fat_set_blk_dev(block_dev_desc_t *dev_desc, disk_partition_t *info) |
71f95118 | 56 | { |
9a800ac7 | 57 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz); |
7205e407 | 58 | |
5e8f9831 SW |
59 | cur_dev = dev_desc; |
60 | cur_part_info = *info; | |
9813b750 KM |
61 | |
62 | /* Make sure it has a valid FAT header */ | |
63 | if (disk_read(0, 1, buffer) != 1) { | |
64 | cur_dev = NULL; | |
65 | return -1; | |
bf1060ea | 66 | } |
9813b750 KM |
67 | |
68 | /* Check if it's actually a DOS volume */ | |
69 | if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) { | |
70 | cur_dev = NULL; | |
71 | return -1; | |
72 | } | |
73 | ||
74 | /* Check for FAT12/FAT16/FAT32 filesystem */ | |
75 | if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3)) | |
76 | return 0; | |
77 | if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5)) | |
78 | return 0; | |
79 | ||
80 | cur_dev = NULL; | |
81 | return -1; | |
71f95118 WD |
82 | } |
83 | ||
5e8f9831 SW |
84 | int fat_register_device(block_dev_desc_t *dev_desc, int part_no) |
85 | { | |
86 | disk_partition_t info; | |
87 | ||
88 | /* First close any currently found FAT filesystem */ | |
89 | cur_dev = NULL; | |
90 | ||
91 | /* Read the partition table, if present */ | |
92 | if (get_partition_info(dev_desc, part_no, &info)) { | |
93 | if (part_no != 0) { | |
94 | printf("** Partition %d not valid on device %d **\n", | |
95 | part_no, dev_desc->dev); | |
96 | return -1; | |
97 | } | |
98 | ||
99 | info.start = 0; | |
100 | info.size = dev_desc->lba; | |
101 | info.blksz = dev_desc->blksz; | |
102 | info.name[0] = 0; | |
103 | info.type[0] = 0; | |
104 | info.bootable = 0; | |
105 | #ifdef CONFIG_PARTITION_UUIDS | |
106 | info.uuid[0] = 0; | |
107 | #endif | |
108 | } | |
109 | ||
110 | return fat_set_blk_dev(dev_desc, &info); | |
111 | } | |
9813b750 | 112 | |
71f95118 WD |
113 | /* |
114 | * Get the first occurence of a directory delimiter ('/' or '\') in a string. | |
115 | * Return index into string if found, -1 otherwise. | |
116 | */ | |
9795e07b | 117 | static int dirdelim(char *str) |
71f95118 WD |
118 | { |
119 | char *start = str; | |
120 | ||
121 | while (*str != '\0') { | |
7385c28e WD |
122 | if (ISDIRDELIM(*str)) |
123 | return str - start; | |
71f95118 WD |
124 | str++; |
125 | } | |
126 | return -1; | |
127 | } | |
128 | ||
71f95118 WD |
129 | /* |
130 | * Extract zero terminated short name from a directory entry. | |
131 | */ | |
9795e07b | 132 | static void get_name(dir_entry *dirent, char *s_name) |
71f95118 WD |
133 | { |
134 | char *ptr; | |
135 | ||
7385c28e | 136 | memcpy(s_name, dirent->name, 8); |
71f95118 WD |
137 | s_name[8] = '\0'; |
138 | ptr = s_name; | |
139 | while (*ptr && *ptr != ' ') | |
140 | ptr++; | |
141 | if (dirent->ext[0] && dirent->ext[0] != ' ') { | |
142 | *ptr = '.'; | |
143 | ptr++; | |
7385c28e | 144 | memcpy(ptr, dirent->ext, 3); |
71f95118 WD |
145 | ptr[3] = '\0'; |
146 | while (*ptr && *ptr != ' ') | |
147 | ptr++; | |
148 | } | |
149 | *ptr = '\0'; | |
150 | if (*s_name == DELETED_FLAG) | |
151 | *s_name = '\0'; | |
152 | else if (*s_name == aRING) | |
3c2c2f42 | 153 | *s_name = DELETED_FLAG; |
7385c28e | 154 | downcase(s_name); |
71f95118 WD |
155 | } |
156 | ||
157 | /* | |
158 | * Get the entry at index 'entry' in a FAT (12/16/32) table. | |
159 | * On failure 0x00 is returned. | |
160 | */ | |
9795e07b | 161 | static __u32 get_fatent(fsdata *mydata, __u32 entry) |
71f95118 WD |
162 | { |
163 | __u32 bufnum; | |
7385c28e | 164 | __u32 off16, offset; |
71f95118 | 165 | __u32 ret = 0x00; |
7385c28e | 166 | __u16 val1, val2; |
71f95118 WD |
167 | |
168 | switch (mydata->fatsize) { | |
169 | case 32: | |
170 | bufnum = entry / FAT32BUFSIZE; | |
171 | offset = entry - bufnum * FAT32BUFSIZE; | |
172 | break; | |
173 | case 16: | |
174 | bufnum = entry / FAT16BUFSIZE; | |
175 | offset = entry - bufnum * FAT16BUFSIZE; | |
176 | break; | |
177 | case 12: | |
178 | bufnum = entry / FAT12BUFSIZE; | |
179 | offset = entry - bufnum * FAT12BUFSIZE; | |
180 | break; | |
181 | ||
182 | default: | |
183 | /* Unsupported FAT size */ | |
184 | return ret; | |
185 | } | |
186 | ||
7385c28e WD |
187 | debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n", |
188 | mydata->fatsize, entry, entry, offset, offset); | |
2aa98c66 | 189 | |
71f95118 WD |
190 | /* Read a new block of FAT entries into the cache. */ |
191 | if (bufnum != mydata->fatbufnum) { | |
60b36f0f | 192 | __u32 getsize = FATBUFBLOCKS; |
71f95118 WD |
193 | __u8 *bufptr = mydata->fatbuf; |
194 | __u32 fatlength = mydata->fatlength; | |
195 | __u32 startblock = bufnum * FATBUFBLOCKS; | |
196 | ||
8006dd2e BT |
197 | if (startblock + getsize > fatlength) |
198 | getsize = fatlength - startblock; | |
60b36f0f | 199 | |
71f95118 WD |
200 | startblock += mydata->fat_sect; /* Offset from start of disk */ |
201 | ||
71f95118 | 202 | if (disk_read(startblock, getsize, bufptr) < 0) { |
7385c28e | 203 | debug("Error reading FAT blocks\n"); |
71f95118 WD |
204 | return ret; |
205 | } | |
206 | mydata->fatbufnum = bufnum; | |
207 | } | |
208 | ||
209 | /* Get the actual entry from the table */ | |
210 | switch (mydata->fatsize) { | |
211 | case 32: | |
7385c28e | 212 | ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]); |
71f95118 WD |
213 | break; |
214 | case 16: | |
7385c28e | 215 | ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]); |
71f95118 | 216 | break; |
7385c28e WD |
217 | case 12: |
218 | off16 = (offset * 3) / 4; | |
71f95118 WD |
219 | |
220 | switch (offset & 0x3) { | |
221 | case 0: | |
7385c28e | 222 | ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]); |
71f95118 WD |
223 | ret &= 0xfff; |
224 | break; | |
225 | case 1: | |
7385c28e | 226 | val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]); |
71f95118 | 227 | val1 &= 0xf000; |
7385c28e | 228 | val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]); |
71f95118 WD |
229 | val2 &= 0x00ff; |
230 | ret = (val2 << 4) | (val1 >> 12); | |
231 | break; | |
232 | case 2: | |
7385c28e | 233 | val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]); |
71f95118 | 234 | val1 &= 0xff00; |
7385c28e | 235 | val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]); |
71f95118 WD |
236 | val2 &= 0x000f; |
237 | ret = (val2 << 8) | (val1 >> 8); | |
238 | break; | |
239 | case 3: | |
7385c28e | 240 | ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]); |
71f95118 WD |
241 | ret = (ret & 0xfff0) >> 4; |
242 | break; | |
243 | default: | |
244 | break; | |
245 | } | |
7385c28e | 246 | break; |
71f95118 | 247 | } |
7385c28e WD |
248 | debug("FAT%d: ret: %08x, offset: %04x\n", |
249 | mydata->fatsize, ret, offset); | |
71f95118 WD |
250 | |
251 | return ret; | |
252 | } | |
253 | ||
71f95118 WD |
254 | /* |
255 | * Read at most 'size' bytes from the specified cluster into 'buffer'. | |
256 | * Return 0 on success, -1 otherwise. | |
257 | */ | |
258 | static int | |
9795e07b | 259 | get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size) |
71f95118 | 260 | { |
3f270f42 | 261 | __u32 idx = 0; |
71f95118 | 262 | __u32 startsect; |
46236b14 | 263 | int ret; |
71f95118 WD |
264 | |
265 | if (clustnum > 0) { | |
7385c28e WD |
266 | startsect = mydata->data_begin + |
267 | clustnum * mydata->clust_size; | |
71f95118 WD |
268 | } else { |
269 | startsect = mydata->rootdir_sect; | |
270 | } | |
271 | ||
7385c28e WD |
272 | debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect); |
273 | ||
cc63b25e | 274 | if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) { |
9a800ac7 | 275 | ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size); |
7385c28e | 276 | |
cc63b25e BT |
277 | printf("FAT: Misaligned buffer address (%p)\n", buffer); |
278 | ||
279 | while (size >= mydata->sect_size) { | |
280 | ret = disk_read(startsect++, 1, tmpbuf); | |
281 | if (ret != 1) { | |
282 | debug("Error reading data (got %d)\n", ret); | |
283 | return -1; | |
284 | } | |
285 | ||
286 | memcpy(buffer, tmpbuf, mydata->sect_size); | |
287 | buffer += mydata->sect_size; | |
288 | size -= mydata->sect_size; | |
289 | } | |
290 | } else { | |
ac497771 | 291 | idx = size / mydata->sect_size; |
cc63b25e BT |
292 | ret = disk_read(startsect, idx, buffer); |
293 | if (ret != idx) { | |
294 | debug("Error reading data (got %d)\n", ret); | |
295 | return -1; | |
296 | } | |
297 | startsect += idx; | |
298 | idx *= mydata->sect_size; | |
299 | buffer += idx; | |
300 | size -= idx; | |
301 | } | |
302 | if (size) { | |
303 | ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size); | |
304 | ||
305 | ret = disk_read(startsect, 1, tmpbuf); | |
46236b14 KM |
306 | if (ret != 1) { |
307 | debug("Error reading data (got %d)\n", ret); | |
7205e407 | 308 | return -1; |
71f95118 | 309 | } |
7205e407 | 310 | |
cc63b25e | 311 | memcpy(buffer, tmpbuf, size); |
71f95118 WD |
312 | } |
313 | ||
314 | return 0; | |
315 | } | |
316 | ||
71f95118 | 317 | /* |
1170e634 | 318 | * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr' |
71f95118 WD |
319 | * into 'buffer'. |
320 | * Return the number of bytes read or -1 on fatal errors. | |
321 | */ | |
1170e634 BT |
322 | __u8 get_contents_vfatname_block[MAX_CLUSTSIZE] |
323 | __aligned(ARCH_DMA_MINALIGN); | |
324 | ||
71f95118 | 325 | static long |
1170e634 BT |
326 | get_contents(fsdata *mydata, dir_entry *dentptr, unsigned long pos, |
327 | __u8 *buffer, unsigned long maxsize) | |
71f95118 WD |
328 | { |
329 | unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0; | |
ac497771 | 330 | unsigned int bytesperclust = mydata->clust_size * mydata->sect_size; |
71f95118 | 331 | __u32 curclust = START(dentptr); |
7205e407 WD |
332 | __u32 endclust, newclust; |
333 | unsigned long actsize; | |
71f95118 | 334 | |
7385c28e | 335 | debug("Filesize: %ld bytes\n", filesize); |
71f95118 | 336 | |
1170e634 BT |
337 | if (pos >= filesize) { |
338 | debug("Read position past EOF: %lu\n", pos); | |
339 | return gotsize; | |
340 | } | |
341 | ||
342 | if (maxsize > 0 && filesize > pos + maxsize) | |
343 | filesize = pos + maxsize; | |
71f95118 | 344 | |
7385c28e WD |
345 | debug("%ld bytes\n", filesize); |
346 | ||
1170e634 BT |
347 | actsize = bytesperclust; |
348 | ||
349 | /* go to cluster at pos */ | |
350 | while (actsize <= pos) { | |
351 | curclust = get_fatent(mydata, curclust); | |
352 | if (CHECK_CLUST(curclust, mydata->fatsize)) { | |
353 | debug("curclust: 0x%x\n", curclust); | |
354 | debug("Invalid FAT entry\n"); | |
355 | return gotsize; | |
356 | } | |
357 | actsize += bytesperclust; | |
358 | } | |
359 | ||
360 | /* actsize > pos */ | |
361 | actsize -= bytesperclust; | |
362 | filesize -= actsize; | |
363 | pos -= actsize; | |
364 | ||
365 | /* align to beginning of next cluster if any */ | |
366 | if (pos) { | |
367 | actsize = min(filesize, bytesperclust); | |
368 | if (get_cluster(mydata, curclust, get_contents_vfatname_block, | |
369 | (int)actsize) != 0) { | |
370 | printf("Error reading cluster\n"); | |
371 | return -1; | |
372 | } | |
373 | filesize -= actsize; | |
374 | actsize -= pos; | |
375 | memcpy(buffer, get_contents_vfatname_block + pos, actsize); | |
376 | gotsize += actsize; | |
377 | if (!filesize) | |
378 | return gotsize; | |
379 | buffer += actsize; | |
380 | ||
381 | curclust = get_fatent(mydata, curclust); | |
382 | if (CHECK_CLUST(curclust, mydata->fatsize)) { | |
383 | debug("curclust: 0x%x\n", curclust); | |
384 | debug("Invalid FAT entry\n"); | |
385 | return gotsize; | |
386 | } | |
387 | } | |
388 | ||
7385c28e WD |
389 | actsize = bytesperclust; |
390 | endclust = curclust; | |
71f95118 WD |
391 | |
392 | do { | |
7205e407 | 393 | /* search for consecutive clusters */ |
7385c28e | 394 | while (actsize < filesize) { |
7205e407 | 395 | newclust = get_fatent(mydata, endclust); |
7385c28e | 396 | if ((newclust - 1) != endclust) |
7205e407 | 397 | goto getit; |
8ce4e5c2 | 398 | if (CHECK_CLUST(newclust, mydata->fatsize)) { |
7385c28e WD |
399 | debug("curclust: 0x%x\n", newclust); |
400 | debug("Invalid FAT entry\n"); | |
7205e407 WD |
401 | return gotsize; |
402 | } | |
7385c28e WD |
403 | endclust = newclust; |
404 | actsize += bytesperclust; | |
7205e407 | 405 | } |
7385c28e | 406 | |
7205e407 | 407 | /* get remaining bytes */ |
7385c28e | 408 | actsize = filesize; |
0880e5bb | 409 | if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) { |
7385c28e | 410 | printf("Error reading cluster\n"); |
7205e407 WD |
411 | return -1; |
412 | } | |
7385c28e | 413 | gotsize += actsize; |
7205e407 WD |
414 | return gotsize; |
415 | getit: | |
416 | if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) { | |
7385c28e | 417 | printf("Error reading cluster\n"); |
7205e407 WD |
418 | return -1; |
419 | } | |
420 | gotsize += (int)actsize; | |
421 | filesize -= actsize; | |
422 | buffer += actsize; | |
7385c28e | 423 | |
7205e407 | 424 | curclust = get_fatent(mydata, endclust); |
8ce4e5c2 | 425 | if (CHECK_CLUST(curclust, mydata->fatsize)) { |
7385c28e WD |
426 | debug("curclust: 0x%x\n", curclust); |
427 | printf("Invalid FAT entry\n"); | |
71f95118 WD |
428 | return gotsize; |
429 | } | |
7385c28e WD |
430 | actsize = bytesperclust; |
431 | endclust = curclust; | |
71f95118 WD |
432 | } while (1); |
433 | } | |
434 | ||
71f95118 WD |
435 | /* |
436 | * Extract the file name information from 'slotptr' into 'l_name', | |
437 | * starting at l_name[*idx]. | |
438 | * Return 1 if terminator (zero byte) is found, 0 otherwise. | |
439 | */ | |
9795e07b | 440 | static int slot2str(dir_slot *slotptr, char *l_name, int *idx) |
71f95118 WD |
441 | { |
442 | int j; | |
443 | ||
444 | for (j = 0; j <= 8; j += 2) { | |
445 | l_name[*idx] = slotptr->name0_4[j]; | |
7385c28e WD |
446 | if (l_name[*idx] == 0x00) |
447 | return 1; | |
71f95118 WD |
448 | (*idx)++; |
449 | } | |
450 | for (j = 0; j <= 10; j += 2) { | |
451 | l_name[*idx] = slotptr->name5_10[j]; | |
7385c28e WD |
452 | if (l_name[*idx] == 0x00) |
453 | return 1; | |
71f95118 WD |
454 | (*idx)++; |
455 | } | |
456 | for (j = 0; j <= 2; j += 2) { | |
457 | l_name[*idx] = slotptr->name11_12[j]; | |
7385c28e WD |
458 | if (l_name[*idx] == 0x00) |
459 | return 1; | |
71f95118 WD |
460 | (*idx)++; |
461 | } | |
462 | ||
463 | return 0; | |
464 | } | |
465 | ||
71f95118 WD |
466 | /* |
467 | * Extract the full long filename starting at 'retdent' (which is really | |
468 | * a slot) into 'l_name'. If successful also copy the real directory entry | |
469 | * into 'retdent' | |
470 | * Return 0 on success, -1 otherwise. | |
471 | */ | |
472 | static int | |
9795e07b BT |
473 | get_vfatname(fsdata *mydata, int curclust, __u8 *cluster, |
474 | dir_entry *retdent, char *l_name) | |
71f95118 WD |
475 | { |
476 | dir_entry *realdent; | |
7385c28e | 477 | dir_slot *slotptr = (dir_slot *)retdent; |
025421ea SS |
478 | __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ? |
479 | PREFETCH_BLOCKS : | |
480 | mydata->clust_size); | |
7385c28e | 481 | __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff; |
71f95118 WD |
482 | int idx = 0; |
483 | ||
3831530d MZ |
484 | if (counter > VFAT_MAXSEQ) { |
485 | debug("Error: VFAT name is too long\n"); | |
486 | return -1; | |
487 | } | |
488 | ||
489 | while ((__u8 *)slotptr < buflimit) { | |
7385c28e WD |
490 | if (counter == 0) |
491 | break; | |
2d1a537d WD |
492 | if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter) |
493 | return -1; | |
71f95118 WD |
494 | slotptr++; |
495 | counter--; | |
496 | } | |
497 | ||
3831530d | 498 | if ((__u8 *)slotptr >= buflimit) { |
71f95118 WD |
499 | dir_slot *slotptr2; |
500 | ||
3831530d MZ |
501 | if (curclust == 0) |
502 | return -1; | |
71f95118 | 503 | curclust = get_fatent(mydata, curclust); |
8ce4e5c2 | 504 | if (CHECK_CLUST(curclust, mydata->fatsize)) { |
7385c28e WD |
505 | debug("curclust: 0x%x\n", curclust); |
506 | printf("Invalid FAT entry\n"); | |
71f95118 WD |
507 | return -1; |
508 | } | |
7385c28e | 509 | |
1170e634 | 510 | if (get_cluster(mydata, curclust, get_contents_vfatname_block, |
ac497771 | 511 | mydata->clust_size * mydata->sect_size) != 0) { |
7385c28e | 512 | debug("Error: reading directory block\n"); |
71f95118 WD |
513 | return -1; |
514 | } | |
7385c28e | 515 | |
1170e634 | 516 | slotptr2 = (dir_slot *)get_contents_vfatname_block; |
3831530d MZ |
517 | while (counter > 0) { |
518 | if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK) | |
519 | & 0xff) != counter) | |
520 | return -1; | |
71f95118 | 521 | slotptr2++; |
3831530d MZ |
522 | counter--; |
523 | } | |
7385c28e | 524 | |
71f95118 | 525 | /* Save the real directory entry */ |
3831530d | 526 | realdent = (dir_entry *)slotptr2; |
1170e634 | 527 | while ((__u8 *)slotptr2 > get_contents_vfatname_block) { |
71f95118 | 528 | slotptr2--; |
3831530d | 529 | slot2str(slotptr2, l_name, &idx); |
71f95118 WD |
530 | } |
531 | } else { | |
532 | /* Save the real directory entry */ | |
7385c28e | 533 | realdent = (dir_entry *)slotptr; |
71f95118 WD |
534 | } |
535 | ||
536 | do { | |
537 | slotptr--; | |
7385c28e WD |
538 | if (slot2str(slotptr, l_name, &idx)) |
539 | break; | |
2d1a537d | 540 | } while (!(slotptr->id & LAST_LONG_ENTRY_MASK)); |
71f95118 WD |
541 | |
542 | l_name[idx] = '\0'; | |
7385c28e WD |
543 | if (*l_name == DELETED_FLAG) |
544 | *l_name = '\0'; | |
545 | else if (*l_name == aRING) | |
546 | *l_name = DELETED_FLAG; | |
71f95118 WD |
547 | downcase(l_name); |
548 | ||
549 | /* Return the real directory entry */ | |
550 | memcpy(retdent, realdent, sizeof(dir_entry)); | |
551 | ||
552 | return 0; | |
553 | } | |
554 | ||
71f95118 | 555 | /* Calculate short name checksum */ |
ff04f6d1 | 556 | static __u8 mkcksum(const char name[8], const char ext[3]) |
71f95118 WD |
557 | { |
558 | int i; | |
7385c28e | 559 | |
71f95118 WD |
560 | __u8 ret = 0; |
561 | ||
6ad77d88 | 562 | for (i = 0; i < 8; i++) |
ff04f6d1 | 563 | ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i]; |
6ad77d88 | 564 | for (i = 0; i < 3; i++) |
ff04f6d1 | 565 | ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i]; |
71f95118 WD |
566 | |
567 | return ret; | |
568 | } | |
71f95118 WD |
569 | |
570 | /* | |
571 | * Get the directory entry associated with 'filename' from the directory | |
572 | * starting at 'startsect' | |
573 | */ | |
9a800ac7 EN |
574 | __u8 get_dentfromdir_block[MAX_CLUSTSIZE] |
575 | __aligned(ARCH_DMA_MINALIGN); | |
7385c28e | 576 | |
9795e07b BT |
577 | static dir_entry *get_dentfromdir(fsdata *mydata, int startsect, |
578 | char *filename, dir_entry *retdent, | |
579 | int dols) | |
71f95118 | 580 | { |
7385c28e WD |
581 | __u16 prevcksum = 0xffff; |
582 | __u32 curclust = START(retdent); | |
583 | int files = 0, dirs = 0; | |
71f95118 | 584 | |
7385c28e | 585 | debug("get_dentfromdir: %s\n", filename); |
71f95118 | 586 | |
7385c28e WD |
587 | while (1) { |
588 | dir_entry *dentptr; | |
589 | ||
590 | int i; | |
591 | ||
592 | if (get_cluster(mydata, curclust, get_dentfromdir_block, | |
ac497771 | 593 | mydata->clust_size * mydata->sect_size) != 0) { |
7385c28e WD |
594 | debug("Error: reading directory block\n"); |
595 | return NULL; | |
596 | } | |
597 | ||
598 | dentptr = (dir_entry *)get_dentfromdir_block; | |
599 | ||
600 | for (i = 0; i < DIRENTSPERCLUST; i++) { | |
3831530d | 601 | char s_name[14], l_name[VFAT_MAXLEN_BYTES]; |
7385c28e WD |
602 | |
603 | l_name[0] = '\0'; | |
604 | if (dentptr->name[0] == DELETED_FLAG) { | |
605 | dentptr++; | |
606 | continue; | |
607 | } | |
608 | if ((dentptr->attr & ATTR_VOLUME)) { | |
cb940c7e RG |
609 | if (vfat_enabled && |
610 | (dentptr->attr & ATTR_VFAT) == ATTR_VFAT && | |
206d68fd | 611 | (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) { |
7385c28e WD |
612 | prevcksum = ((dir_slot *)dentptr)->alias_checksum; |
613 | get_vfatname(mydata, curclust, | |
614 | get_dentfromdir_block, | |
615 | dentptr, l_name); | |
616 | if (dols) { | |
617 | int isdir; | |
618 | char dirc; | |
619 | int doit = 0; | |
620 | ||
621 | isdir = (dentptr->attr & ATTR_DIR); | |
622 | ||
623 | if (isdir) { | |
624 | dirs++; | |
625 | dirc = '/'; | |
626 | doit = 1; | |
627 | } else { | |
628 | dirc = ' '; | |
629 | if (l_name[0] != 0) { | |
630 | files++; | |
631 | doit = 1; | |
632 | } | |
633 | } | |
634 | if (doit) { | |
635 | if (dirc == ' ') { | |
636 | printf(" %8ld %s%c\n", | |
637 | (long)FAT2CPU32(dentptr->size), | |
638 | l_name, | |
639 | dirc); | |
640 | } else { | |
641 | printf(" %s%c\n", | |
642 | l_name, | |
643 | dirc); | |
644 | } | |
645 | } | |
646 | dentptr++; | |
647 | continue; | |
648 | } | |
649 | debug("vfatname: |%s|\n", l_name); | |
cb940c7e | 650 | } else { |
7385c28e WD |
651 | /* Volume label or VFAT entry */ |
652 | dentptr++; | |
653 | continue; | |
654 | } | |
71f95118 | 655 | } |
7385c28e WD |
656 | if (dentptr->name[0] == 0) { |
657 | if (dols) { | |
658 | printf("\n%d file(s), %d dir(s)\n\n", | |
659 | files, dirs); | |
660 | } | |
661 | debug("Dentname == NULL - %d\n", i); | |
662 | return NULL; | |
71f95118 | 663 | } |
cb940c7e RG |
664 | if (vfat_enabled) { |
665 | __u8 csum = mkcksum(dentptr->name, dentptr->ext); | |
666 | if (dols && csum == prevcksum) { | |
667 | prevcksum = 0xffff; | |
668 | dentptr++; | |
669 | continue; | |
670 | } | |
7385c28e | 671 | } |
cb940c7e | 672 | |
7385c28e WD |
673 | get_name(dentptr, s_name); |
674 | if (dols) { | |
675 | int isdir = (dentptr->attr & ATTR_DIR); | |
676 | char dirc; | |
677 | int doit = 0; | |
678 | ||
679 | if (isdir) { | |
680 | dirs++; | |
681 | dirc = '/'; | |
682 | doit = 1; | |
683 | } else { | |
684 | dirc = ' '; | |
685 | if (s_name[0] != 0) { | |
686 | files++; | |
687 | doit = 1; | |
688 | } | |
689 | } | |
690 | ||
691 | if (doit) { | |
692 | if (dirc == ' ') { | |
693 | printf(" %8ld %s%c\n", | |
694 | (long)FAT2CPU32(dentptr->size), | |
695 | s_name, dirc); | |
696 | } else { | |
697 | printf(" %s%c\n", | |
698 | s_name, dirc); | |
699 | } | |
700 | } | |
701 | ||
702 | dentptr++; | |
703 | continue; | |
704 | } | |
705 | ||
706 | if (strcmp(filename, s_name) | |
707 | && strcmp(filename, l_name)) { | |
708 | debug("Mismatch: |%s|%s|\n", s_name, l_name); | |
709 | dentptr++; | |
710 | continue; | |
711 | } | |
712 | ||
713 | memcpy(retdent, dentptr, sizeof(dir_entry)); | |
714 | ||
715 | debug("DentName: %s", s_name); | |
716 | debug(", start: 0x%x", START(dentptr)); | |
717 | debug(", size: 0x%x %s\n", | |
718 | FAT2CPU32(dentptr->size), | |
719 | (dentptr->attr & ATTR_DIR) ? "(DIR)" : ""); | |
720 | ||
721 | return retdent; | |
71f95118 | 722 | } |
7385c28e WD |
723 | |
724 | curclust = get_fatent(mydata, curclust); | |
725 | if (CHECK_CLUST(curclust, mydata->fatsize)) { | |
726 | debug("curclust: 0x%x\n", curclust); | |
727 | printf("Invalid FAT entry\n"); | |
728 | return NULL; | |
71f95118 | 729 | } |
71f95118 | 730 | } |
71f95118 | 731 | |
7385c28e | 732 | return NULL; |
71f95118 WD |
733 | } |
734 | ||
71f95118 WD |
735 | /* |
736 | * Read boot sector and volume info from a FAT filesystem | |
737 | */ | |
738 | static int | |
9795e07b | 739 | read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize) |
71f95118 | 740 | { |
ac497771 | 741 | __u8 *block; |
71f95118 | 742 | volume_info *vistart; |
ac497771 SS |
743 | int ret = 0; |
744 | ||
745 | if (cur_dev == NULL) { | |
746 | debug("Error: no device selected\n"); | |
747 | return -1; | |
748 | } | |
749 | ||
9a800ac7 | 750 | block = memalign(ARCH_DMA_MINALIGN, cur_dev->blksz); |
ac497771 SS |
751 | if (block == NULL) { |
752 | debug("Error: allocating block\n"); | |
753 | return -1; | |
754 | } | |
71f95118 | 755 | |
9795e07b | 756 | if (disk_read(0, 1, block) < 0) { |
7385c28e | 757 | debug("Error: reading block\n"); |
ac497771 | 758 | goto fail; |
71f95118 WD |
759 | } |
760 | ||
761 | memcpy(bs, block, sizeof(boot_sector)); | |
7385c28e WD |
762 | bs->reserved = FAT2CPU16(bs->reserved); |
763 | bs->fat_length = FAT2CPU16(bs->fat_length); | |
764 | bs->secs_track = FAT2CPU16(bs->secs_track); | |
765 | bs->heads = FAT2CPU16(bs->heads); | |
766 | bs->total_sect = FAT2CPU32(bs->total_sect); | |
71f95118 WD |
767 | |
768 | /* FAT32 entries */ | |
769 | if (bs->fat_length == 0) { | |
770 | /* Assume FAT32 */ | |
771 | bs->fat32_length = FAT2CPU32(bs->fat32_length); | |
7385c28e | 772 | bs->flags = FAT2CPU16(bs->flags); |
71f95118 | 773 | bs->root_cluster = FAT2CPU32(bs->root_cluster); |
7385c28e WD |
774 | bs->info_sector = FAT2CPU16(bs->info_sector); |
775 | bs->backup_boot = FAT2CPU16(bs->backup_boot); | |
776 | vistart = (volume_info *)(block + sizeof(boot_sector)); | |
71f95118 WD |
777 | *fatsize = 32; |
778 | } else { | |
7385c28e | 779 | vistart = (volume_info *)&(bs->fat32_length); |
71f95118 WD |
780 | *fatsize = 0; |
781 | } | |
782 | memcpy(volinfo, vistart, sizeof(volume_info)); | |
783 | ||
71f95118 | 784 | if (*fatsize == 32) { |
7385c28e | 785 | if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0) |
ac497771 | 786 | goto exit; |
71f95118 | 787 | } else { |
651351fe | 788 | if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) { |
71f95118 | 789 | *fatsize = 12; |
ac497771 | 790 | goto exit; |
71f95118 | 791 | } |
651351fe | 792 | if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) { |
71f95118 | 793 | *fatsize = 16; |
ac497771 | 794 | goto exit; |
71f95118 WD |
795 | } |
796 | } | |
797 | ||
7385c28e | 798 | debug("Error: broken fs_type sign\n"); |
ac497771 SS |
799 | fail: |
800 | ret = -1; | |
801 | exit: | |
802 | free(block); | |
803 | return ret; | |
71f95118 WD |
804 | } |
805 | ||
1170e634 | 806 | __u8 do_fat_read_at_block[MAX_CLUSTSIZE] |
9a800ac7 | 807 | __aligned(ARCH_DMA_MINALIGN); |
7385c28e | 808 | |
20cc00dd | 809 | long |
1170e634 BT |
810 | do_fat_read_at(const char *filename, unsigned long pos, void *buffer, |
811 | unsigned long maxsize, int dols) | |
71f95118 | 812 | { |
7385c28e WD |
813 | char fnamecopy[2048]; |
814 | boot_sector bs; | |
815 | volume_info volinfo; | |
816 | fsdata datablock; | |
817 | fsdata *mydata = &datablock; | |
cd1b042c | 818 | dir_entry *dentptr = NULL; |
7385c28e WD |
819 | __u16 prevcksum = 0xffff; |
820 | char *subname = ""; | |
3f270f42 | 821 | __u32 cursect; |
7385c28e WD |
822 | int idx, isdir = 0; |
823 | int files = 0, dirs = 0; | |
ac497771 | 824 | long ret = -1; |
7385c28e | 825 | int firsttime; |
40e21916 | 826 | __u32 root_cluster = 0; |
3f270f42 | 827 | int rootdir_size = 0; |
7385c28e WD |
828 | int j; |
829 | ||
830 | if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) { | |
831 | debug("Error: reading boot sector\n"); | |
832 | return -1; | |
833 | } | |
834 | ||
40e21916 SS |
835 | if (mydata->fatsize == 32) { |
836 | root_cluster = bs.root_cluster; | |
7385c28e | 837 | mydata->fatlength = bs.fat32_length; |
40e21916 | 838 | } else { |
7385c28e | 839 | mydata->fatlength = bs.fat_length; |
40e21916 | 840 | } |
7385c28e WD |
841 | |
842 | mydata->fat_sect = bs.reserved; | |
843 | ||
844 | cursect = mydata->rootdir_sect | |
845 | = mydata->fat_sect + mydata->fatlength * bs.fats; | |
846 | ||
ac497771 | 847 | mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0]; |
7385c28e | 848 | mydata->clust_size = bs.cluster_size; |
46236b14 KM |
849 | if (mydata->sect_size != cur_part_info.blksz) { |
850 | printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n", | |
851 | mydata->sect_size, cur_part_info.blksz); | |
852 | return -1; | |
853 | } | |
7385c28e WD |
854 | |
855 | if (mydata->fatsize == 32) { | |
856 | mydata->data_begin = mydata->rootdir_sect - | |
857 | (mydata->clust_size * 2); | |
858 | } else { | |
7385c28e WD |
859 | rootdir_size = ((bs.dir_entries[1] * (int)256 + |
860 | bs.dir_entries[0]) * | |
861 | sizeof(dir_entry)) / | |
ac497771 | 862 | mydata->sect_size; |
7385c28e WD |
863 | mydata->data_begin = mydata->rootdir_sect + |
864 | rootdir_size - | |
865 | (mydata->clust_size * 2); | |
866 | } | |
867 | ||
868 | mydata->fatbufnum = -1; | |
9a800ac7 | 869 | mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE); |
ac497771 SS |
870 | if (mydata->fatbuf == NULL) { |
871 | debug("Error: allocating memory\n"); | |
872 | return -1; | |
873 | } | |
71f95118 | 874 | |
cb940c7e RG |
875 | if (vfat_enabled) |
876 | debug("VFAT Support enabled\n"); | |
877 | ||
7385c28e WD |
878 | debug("FAT%d, fat_sect: %d, fatlength: %d\n", |
879 | mydata->fatsize, mydata->fat_sect, mydata->fatlength); | |
880 | debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n" | |
881 | "Data begins at: %d\n", | |
882 | root_cluster, | |
883 | mydata->rootdir_sect, | |
ac497771 SS |
884 | mydata->rootdir_sect * mydata->sect_size, mydata->data_begin); |
885 | debug("Sector size: %d, cluster size: %d\n", mydata->sect_size, | |
886 | mydata->clust_size); | |
7385c28e WD |
887 | |
888 | /* "cwd" is always the root... */ | |
889 | while (ISDIRDELIM(*filename)) | |
890 | filename++; | |
891 | ||
892 | /* Make a copy of the filename and convert it to lowercase */ | |
893 | strcpy(fnamecopy, filename); | |
894 | downcase(fnamecopy); | |
895 | ||
896 | if (*fnamecopy == '\0') { | |
897 | if (!dols) | |
ac497771 | 898 | goto exit; |
71f95118 | 899 | |
7385c28e WD |
900 | dols = LS_ROOT; |
901 | } else if ((idx = dirdelim(fnamecopy)) >= 0) { | |
902 | isdir = 1; | |
903 | fnamecopy[idx] = '\0'; | |
904 | subname = fnamecopy + idx + 1; | |
905 | ||
906 | /* Handle multiple delimiters */ | |
907 | while (ISDIRDELIM(*subname)) | |
908 | subname++; | |
909 | } else if (dols) { | |
910 | isdir = 1; | |
71f95118 | 911 | } |
71f95118 | 912 | |
7385c28e WD |
913 | j = 0; |
914 | while (1) { | |
915 | int i; | |
916 | ||
cd1b042c BT |
917 | if (j == 0) { |
918 | debug("FAT read sect=%d, clust_size=%d, DIRENTSPERBLOCK=%zd\n", | |
919 | cursect, mydata->clust_size, DIRENTSPERBLOCK); | |
7385c28e | 920 | |
cd1b042c BT |
921 | if (disk_read(cursect, |
922 | (mydata->fatsize == 32) ? | |
923 | (mydata->clust_size) : | |
924 | PREFETCH_BLOCKS, | |
1170e634 | 925 | do_fat_read_at_block) < 0) { |
cd1b042c BT |
926 | debug("Error: reading rootdir block\n"); |
927 | goto exit; | |
928 | } | |
7385c28e | 929 | |
1170e634 | 930 | dentptr = (dir_entry *) do_fat_read_at_block; |
cd1b042c | 931 | } |
7385c28e WD |
932 | |
933 | for (i = 0; i < DIRENTSPERBLOCK; i++) { | |
3831530d | 934 | char s_name[14], l_name[VFAT_MAXLEN_BYTES]; |
ff04f6d1 | 935 | __u8 csum; |
7385c28e WD |
936 | |
937 | l_name[0] = '\0'; | |
3831530d MZ |
938 | if (dentptr->name[0] == DELETED_FLAG) { |
939 | dentptr++; | |
940 | continue; | |
941 | } | |
ff04f6d1 | 942 | |
cb940c7e RG |
943 | if (vfat_enabled) |
944 | csum = mkcksum(dentptr->name, dentptr->ext); | |
945 | ||
ff04f6d1 | 946 | if (dentptr->attr & ATTR_VOLUME) { |
cb940c7e RG |
947 | if (vfat_enabled && |
948 | (dentptr->attr & ATTR_VFAT) == ATTR_VFAT && | |
7385c28e WD |
949 | (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) { |
950 | prevcksum = | |
951 | ((dir_slot *)dentptr)->alias_checksum; | |
952 | ||
3831530d | 953 | get_vfatname(mydata, |
40e21916 | 954 | root_cluster, |
1170e634 | 955 | do_fat_read_at_block, |
7385c28e WD |
956 | dentptr, l_name); |
957 | ||
958 | if (dols == LS_ROOT) { | |
959 | char dirc; | |
960 | int doit = 0; | |
961 | int isdir = | |
962 | (dentptr->attr & ATTR_DIR); | |
963 | ||
964 | if (isdir) { | |
965 | dirs++; | |
966 | dirc = '/'; | |
967 | doit = 1; | |
968 | } else { | |
969 | dirc = ' '; | |
970 | if (l_name[0] != 0) { | |
971 | files++; | |
972 | doit = 1; | |
973 | } | |
974 | } | |
975 | if (doit) { | |
976 | if (dirc == ' ') { | |
977 | printf(" %8ld %s%c\n", | |
978 | (long)FAT2CPU32(dentptr->size), | |
979 | l_name, | |
980 | dirc); | |
981 | } else { | |
982 | printf(" %s%c\n", | |
983 | l_name, | |
984 | dirc); | |
985 | } | |
986 | } | |
987 | dentptr++; | |
988 | continue; | |
989 | } | |
990 | debug("Rootvfatname: |%s|\n", | |
991 | l_name); | |
cb940c7e | 992 | } else { |
7385c28e WD |
993 | /* Volume label or VFAT entry */ |
994 | dentptr++; | |
995 | continue; | |
996 | } | |
997 | } else if (dentptr->name[0] == 0) { | |
998 | debug("RootDentname == NULL - %d\n", i); | |
999 | if (dols == LS_ROOT) { | |
1000 | printf("\n%d file(s), %d dir(s)\n\n", | |
1001 | files, dirs); | |
ac497771 | 1002 | ret = 0; |
7385c28e | 1003 | } |
ac497771 | 1004 | goto exit; |
71f95118 | 1005 | } |
cb940c7e RG |
1006 | else if (vfat_enabled && |
1007 | dols == LS_ROOT && csum == prevcksum) { | |
bf34e7d9 | 1008 | prevcksum = 0xffff; |
7385c28e WD |
1009 | dentptr++; |
1010 | continue; | |
71f95118 | 1011 | } |
cb940c7e | 1012 | |
7385c28e WD |
1013 | get_name(dentptr, s_name); |
1014 | ||
1015 | if (dols == LS_ROOT) { | |
1016 | int isdir = (dentptr->attr & ATTR_DIR); | |
1017 | char dirc; | |
1018 | int doit = 0; | |
1019 | ||
1020 | if (isdir) { | |
1021 | dirc = '/'; | |
1022 | if (s_name[0] != 0) { | |
1023 | dirs++; | |
1024 | doit = 1; | |
1025 | } | |
1026 | } else { | |
1027 | dirc = ' '; | |
1028 | if (s_name[0] != 0) { | |
1029 | files++; | |
1030 | doit = 1; | |
1031 | } | |
1032 | } | |
1033 | if (doit) { | |
1034 | if (dirc == ' ') { | |
1035 | printf(" %8ld %s%c\n", | |
1036 | (long)FAT2CPU32(dentptr->size), | |
1037 | s_name, dirc); | |
1038 | } else { | |
1039 | printf(" %s%c\n", | |
1040 | s_name, dirc); | |
1041 | } | |
1042 | } | |
1043 | dentptr++; | |
1044 | continue; | |
1045 | } | |
1046 | ||
1047 | if (strcmp(fnamecopy, s_name) | |
1048 | && strcmp(fnamecopy, l_name)) { | |
1049 | debug("RootMismatch: |%s|%s|\n", s_name, | |
1050 | l_name); | |
1051 | dentptr++; | |
1052 | continue; | |
1053 | } | |
1054 | ||
1055 | if (isdir && !(dentptr->attr & ATTR_DIR)) | |
ac497771 | 1056 | goto exit; |
7385c28e WD |
1057 | |
1058 | debug("RootName: %s", s_name); | |
1059 | debug(", start: 0x%x", START(dentptr)); | |
1060 | debug(", size: 0x%x %s\n", | |
1061 | FAT2CPU32(dentptr->size), | |
1062 | isdir ? "(DIR)" : ""); | |
1063 | ||
1064 | goto rootdir_done; /* We got a match */ | |
71f95118 | 1065 | } |
7385c28e WD |
1066 | debug("END LOOP: j=%d clust_size=%d\n", j, |
1067 | mydata->clust_size); | |
1068 | ||
1069 | /* | |
1070 | * On FAT32 we must fetch the FAT entries for the next | |
1071 | * root directory clusters when a cluster has been | |
1072 | * completely processed. | |
1073 | */ | |
3f270f42 | 1074 | ++j; |
cd1b042c BT |
1075 | int rootdir_end = 0; |
1076 | if (mydata->fatsize == 32) { | |
1077 | if (j == mydata->clust_size) { | |
1078 | int nxtsect = 0; | |
1079 | int nxt_clust = 0; | |
7385c28e | 1080 | |
cd1b042c BT |
1081 | nxt_clust = get_fatent(mydata, root_cluster); |
1082 | rootdir_end = CHECK_CLUST(nxt_clust, 32); | |
7385c28e | 1083 | |
cd1b042c BT |
1084 | nxtsect = mydata->data_begin + |
1085 | (nxt_clust * mydata->clust_size); | |
7385c28e | 1086 | |
cd1b042c | 1087 | root_cluster = nxt_clust; |
7385c28e | 1088 | |
cd1b042c BT |
1089 | cursect = nxtsect; |
1090 | j = 0; | |
1091 | } | |
71f95118 | 1092 | } else { |
cd1b042c BT |
1093 | if (j == PREFETCH_BLOCKS) |
1094 | j = 0; | |
1095 | ||
1096 | rootdir_end = (++cursect - mydata->rootdir_sect >= | |
1097 | rootdir_size); | |
71f95118 | 1098 | } |
3f270f42 EH |
1099 | |
1100 | /* If end of rootdir reached */ | |
cd1b042c | 1101 | if (rootdir_end) { |
3f270f42 EH |
1102 | if (dols == LS_ROOT) { |
1103 | printf("\n%d file(s), %d dir(s)\n\n", | |
1104 | files, dirs); | |
ac497771 | 1105 | ret = 0; |
3f270f42 | 1106 | } |
ac497771 | 1107 | goto exit; |
3f270f42 | 1108 | } |
7385c28e WD |
1109 | } |
1110 | rootdir_done: | |
71f95118 | 1111 | |
7385c28e | 1112 | firsttime = 1; |
71f95118 | 1113 | |
7385c28e WD |
1114 | while (isdir) { |
1115 | int startsect = mydata->data_begin | |
1116 | + START(dentptr) * mydata->clust_size; | |
1117 | dir_entry dent; | |
1118 | char *nextname = NULL; | |
71f95118 | 1119 | |
7385c28e WD |
1120 | dent = *dentptr; |
1121 | dentptr = &dent; | |
71f95118 | 1122 | |
7385c28e WD |
1123 | idx = dirdelim(subname); |
1124 | ||
1125 | if (idx >= 0) { | |
1126 | subname[idx] = '\0'; | |
1127 | nextname = subname + idx + 1; | |
1128 | /* Handle multiple delimiters */ | |
1129 | while (ISDIRDELIM(*nextname)) | |
1130 | nextname++; | |
1131 | if (dols && *nextname == '\0') | |
1132 | firsttime = 0; | |
1133 | } else { | |
1134 | if (dols && firsttime) { | |
1135 | firsttime = 0; | |
1136 | } else { | |
1137 | isdir = 0; | |
1138 | } | |
1139 | } | |
1140 | ||
1141 | if (get_dentfromdir(mydata, startsect, subname, dentptr, | |
1142 | isdir ? 0 : dols) == NULL) { | |
1143 | if (dols && !isdir) | |
ac497771 SS |
1144 | ret = 0; |
1145 | goto exit; | |
7385c28e WD |
1146 | } |
1147 | ||
7ee46ceb BT |
1148 | if (isdir && !(dentptr->attr & ATTR_DIR)) |
1149 | goto exit; | |
1150 | ||
1151 | if (idx >= 0) | |
7385c28e | 1152 | subname = nextname; |
71f95118 | 1153 | } |
71f95118 | 1154 | |
1170e634 | 1155 | ret = get_contents(mydata, dentptr, pos, buffer, maxsize); |
7385c28e | 1156 | debug("Size: %d, got: %ld\n", FAT2CPU32(dentptr->size), ret); |
71f95118 | 1157 | |
ac497771 SS |
1158 | exit: |
1159 | free(mydata->fatbuf); | |
7385c28e WD |
1160 | return ret; |
1161 | } | |
71f95118 | 1162 | |
1170e634 BT |
1163 | long |
1164 | do_fat_read(const char *filename, void *buffer, unsigned long maxsize, int dols) | |
1165 | { | |
1166 | return do_fat_read_at(filename, 0, buffer, maxsize, dols); | |
1167 | } | |
1168 | ||
9795e07b | 1169 | int file_fat_detectfs(void) |
71f95118 | 1170 | { |
7385c28e WD |
1171 | boot_sector bs; |
1172 | volume_info volinfo; | |
1173 | int fatsize; | |
1174 | char vol_label[12]; | |
71f95118 | 1175 | |
7385c28e | 1176 | if (cur_dev == NULL) { |
7205e407 WD |
1177 | printf("No current device\n"); |
1178 | return 1; | |
1179 | } | |
7385c28e | 1180 | |
dd60d122 | 1181 | #if defined(CONFIG_CMD_IDE) || \ |
8c5170a7 | 1182 | defined(CONFIG_CMD_SATA) || \ |
dd60d122 JL |
1183 | defined(CONFIG_CMD_SCSI) || \ |
1184 | defined(CONFIG_CMD_USB) || \ | |
21f6f963 | 1185 | defined(CONFIG_MMC) |
7205e407 | 1186 | printf("Interface: "); |
7385c28e WD |
1187 | switch (cur_dev->if_type) { |
1188 | case IF_TYPE_IDE: | |
1189 | printf("IDE"); | |
1190 | break; | |
1191 | case IF_TYPE_SATA: | |
1192 | printf("SATA"); | |
1193 | break; | |
1194 | case IF_TYPE_SCSI: | |
1195 | printf("SCSI"); | |
1196 | break; | |
1197 | case IF_TYPE_ATAPI: | |
1198 | printf("ATAPI"); | |
1199 | break; | |
1200 | case IF_TYPE_USB: | |
1201 | printf("USB"); | |
1202 | break; | |
1203 | case IF_TYPE_DOC: | |
1204 | printf("DOC"); | |
1205 | break; | |
1206 | case IF_TYPE_MMC: | |
1207 | printf("MMC"); | |
1208 | break; | |
1209 | default: | |
1210 | printf("Unknown"); | |
7205e407 | 1211 | } |
7385c28e WD |
1212 | |
1213 | printf("\n Device %d: ", cur_dev->dev); | |
7205e407 WD |
1214 | dev_print(cur_dev); |
1215 | #endif | |
7385c28e WD |
1216 | |
1217 | if (read_bootsectandvi(&bs, &volinfo, &fatsize)) { | |
7205e407 WD |
1218 | printf("\nNo valid FAT fs found\n"); |
1219 | return 1; | |
1220 | } | |
7385c28e WD |
1221 | |
1222 | memcpy(vol_label, volinfo.volume_label, 11); | |
7205e407 | 1223 | vol_label[11] = '\0'; |
7385c28e WD |
1224 | volinfo.fs_type[5] = '\0'; |
1225 | ||
461f86e6 | 1226 | printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label); |
7385c28e | 1227 | |
7205e407 | 1228 | return 0; |
71f95118 WD |
1229 | } |
1230 | ||
9795e07b | 1231 | int file_fat_ls(const char *dir) |
71f95118 WD |
1232 | { |
1233 | return do_fat_read(dir, NULL, 0, LS_YES); | |
1234 | } | |
1235 | ||
1170e634 BT |
1236 | long file_fat_read_at(const char *filename, unsigned long pos, void *buffer, |
1237 | unsigned long maxsize) | |
71f95118 | 1238 | { |
7385c28e | 1239 | printf("reading %s\n", filename); |
1170e634 BT |
1240 | return do_fat_read_at(filename, pos, buffer, maxsize, LS_NO); |
1241 | } | |
1242 | ||
1243 | long file_fat_read(const char *filename, void *buffer, unsigned long maxsize) | |
1244 | { | |
1245 | return file_fat_read_at(filename, 0, buffer, maxsize); | |
71f95118 | 1246 | } |
e6d52415 SG |
1247 | |
1248 | int fat_read_file(const char *filename, void *buf, int offset, int len) | |
1249 | { | |
1250 | int len_read; | |
1251 | ||
1252 | len_read = file_fat_read_at(filename, offset, buf, len); | |
1253 | if (len_read == -1) { | |
1254 | printf("** Unable to read file %s **\n", filename); | |
1255 | return -1; | |
1256 | } | |
1257 | ||
1258 | return len_read; | |
1259 | } | |
1260 | ||
1261 | void fat_close(void) | |
1262 | { | |
1263 | } |