blob: 25d3318cd0a8d8baaefee5cd6bc91977511a08ac [file] [log] [blame]
wdenk7a428cc2003-06-15 22:40:42 +00001/*
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 *
9 * See file CREDITS for list of people who contributed to this
10 * project.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * MA 02111-1307 USA
26 */
27
28#include <common.h>
29#include <config.h>
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +000030#include <exports.h>
wdenk7a428cc2003-06-15 22:40:42 +000031#include <fat.h>
32#include <asm/byteorder.h>
wdenk2c9b05d2003-09-10 22:30:53 +000033#include <part.h>
Eric Nelson014705b2012-04-11 04:08:53 +000034#include <malloc.h>
35#include <linux/compiler.h>
wdenk7a428cc2003-06-15 22:40:42 +000036
wdenk7a428cc2003-06-15 22:40:42 +000037/*
38 * Convert a string to lowercase.
39 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +020040static void downcase(char *str)
wdenk7a428cc2003-06-15 22:40:42 +000041{
42 while (*str != '\0') {
43 TOLOWER(*str);
44 str++;
45 }
46}
47
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000048static block_dev_desc_t *cur_dev;
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000049static disk_partition_t cur_part_info;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020050
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000051#define DOS_BOOT_MAGIC_OFFSET 0x1fe
wdenk2c9b05d2003-09-10 22:30:53 +000052#define DOS_FS_TYPE_OFFSET 0x36
Wolfgang Denk7b2290c2010-07-19 11:36:57 +020053#define DOS_FS32_TYPE_OFFSET 0x52
wdenk7a428cc2003-06-15 22:40:42 +000054
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000055static int disk_read(__u32 block, __u32 nr_blocks, void *buf)
wdenk7a428cc2003-06-15 22:40:42 +000056{
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000057 if (!cur_dev || !cur_dev->block_read)
wdenk2c9b05d2003-09-10 22:30:53 +000058 return -1;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020059
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000060 return cur_dev->block_read(cur_dev->dev,
61 cur_part_info.start + block, nr_blocks, buf);
wdenk7a428cc2003-06-15 22:40:42 +000062}
63
Stephen Warren288904d2012-10-17 06:44:59 +000064int fat_set_blk_dev(block_dev_desc_t *dev_desc, disk_partition_t *info)
wdenk7a428cc2003-06-15 22:40:42 +000065{
Eric Nelson014705b2012-04-11 04:08:53 +000066 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
wdenk2c9b05d2003-09-10 22:30:53 +000067
Stephen Warren288904d2012-10-17 06:44:59 +000068 cur_dev = dev_desc;
69 cur_part_info = *info;
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000070
71 /* Make sure it has a valid FAT header */
72 if (disk_read(0, 1, buffer) != 1) {
73 cur_dev = NULL;
74 return -1;
Wolfgang Denke78f2582007-08-07 16:02:13 +020075 }
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000076
77 /* Check if it's actually a DOS volume */
78 if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) {
79 cur_dev = NULL;
80 return -1;
81 }
82
83 /* Check for FAT12/FAT16/FAT32 filesystem */
84 if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3))
85 return 0;
86 if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5))
87 return 0;
88
89 cur_dev = NULL;
90 return -1;
wdenk7a428cc2003-06-15 22:40:42 +000091}
92
Stephen Warren288904d2012-10-17 06:44:59 +000093int fat_register_device(block_dev_desc_t *dev_desc, int part_no)
94{
95 disk_partition_t info;
96
97 /* First close any currently found FAT filesystem */
98 cur_dev = NULL;
99
100 /* Read the partition table, if present */
101 if (get_partition_info(dev_desc, part_no, &info)) {
102 if (part_no != 0) {
103 printf("** Partition %d not valid on device %d **\n",
104 part_no, dev_desc->dev);
105 return -1;
106 }
107
108 info.start = 0;
109 info.size = dev_desc->lba;
110 info.blksz = dev_desc->blksz;
111 info.name[0] = 0;
112 info.type[0] = 0;
113 info.bootable = 0;
114#ifdef CONFIG_PARTITION_UUIDS
115 info.uuid[0] = 0;
116#endif
117 }
118
119 return fat_set_blk_dev(dev_desc, &info);
120}
Kyle Moffett1b3b4b72011-12-21 07:08:10 +0000121
wdenk7a428cc2003-06-15 22:40:42 +0000122/*
123 * Get the first occurence of a directory delimiter ('/' or '\') in a string.
124 * Return index into string if found, -1 otherwise.
125 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200126static int dirdelim(char *str)
wdenk7a428cc2003-06-15 22:40:42 +0000127{
128 char *start = str;
129
130 while (*str != '\0') {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200131 if (ISDIRDELIM(*str))
132 return str - start;
wdenk7a428cc2003-06-15 22:40:42 +0000133 str++;
134 }
135 return -1;
136}
137
wdenk7a428cc2003-06-15 22:40:42 +0000138/*
139 * Extract zero terminated short name from a directory entry.
140 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200141static void get_name(dir_entry *dirent, char *s_name)
wdenk7a428cc2003-06-15 22:40:42 +0000142{
143 char *ptr;
144
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200145 memcpy(s_name, dirent->name, 8);
wdenk7a428cc2003-06-15 22:40:42 +0000146 s_name[8] = '\0';
147 ptr = s_name;
148 while (*ptr && *ptr != ' ')
149 ptr++;
150 if (dirent->ext[0] && dirent->ext[0] != ' ') {
151 *ptr = '.';
152 ptr++;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200153 memcpy(ptr, dirent->ext, 3);
wdenk7a428cc2003-06-15 22:40:42 +0000154 ptr[3] = '\0';
155 while (*ptr && *ptr != ' ')
156 ptr++;
157 }
158 *ptr = '\0';
159 if (*s_name == DELETED_FLAG)
160 *s_name = '\0';
161 else if (*s_name == aRING)
Remy Bohmer7c8f2ce2008-11-27 22:30:27 +0100162 *s_name = DELETED_FLAG;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200163 downcase(s_name);
wdenk7a428cc2003-06-15 22:40:42 +0000164}
165
166/*
167 * Get the entry at index 'entry' in a FAT (12/16/32) table.
168 * On failure 0x00 is returned.
169 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200170static __u32 get_fatent(fsdata *mydata, __u32 entry)
wdenk7a428cc2003-06-15 22:40:42 +0000171{
172 __u32 bufnum;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200173 __u32 off16, offset;
wdenk7a428cc2003-06-15 22:40:42 +0000174 __u32 ret = 0x00;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200175 __u16 val1, val2;
wdenk7a428cc2003-06-15 22:40:42 +0000176
177 switch (mydata->fatsize) {
178 case 32:
179 bufnum = entry / FAT32BUFSIZE;
180 offset = entry - bufnum * FAT32BUFSIZE;
181 break;
182 case 16:
183 bufnum = entry / FAT16BUFSIZE;
184 offset = entry - bufnum * FAT16BUFSIZE;
185 break;
186 case 12:
187 bufnum = entry / FAT12BUFSIZE;
188 offset = entry - bufnum * FAT12BUFSIZE;
189 break;
190
191 default:
192 /* Unsupported FAT size */
193 return ret;
194 }
195
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200196 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
197 mydata->fatsize, entry, entry, offset, offset);
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200198
wdenk7a428cc2003-06-15 22:40:42 +0000199 /* Read a new block of FAT entries into the cache. */
200 if (bufnum != mydata->fatbufnum) {
Sergei Shtylyovd5916532011-08-08 09:39:29 +0000201 __u32 getsize = FATBUFBLOCKS;
wdenk7a428cc2003-06-15 22:40:42 +0000202 __u8 *bufptr = mydata->fatbuf;
203 __u32 fatlength = mydata->fatlength;
204 __u32 startblock = bufnum * FATBUFBLOCKS;
205
Benoît Thébaudeau808a5fd2012-07-20 15:19:29 +0200206 if (startblock + getsize > fatlength)
207 getsize = fatlength - startblock;
Sergei Shtylyovd5916532011-08-08 09:39:29 +0000208
wdenk7a428cc2003-06-15 22:40:42 +0000209 startblock += mydata->fat_sect; /* Offset from start of disk */
210
wdenk7a428cc2003-06-15 22:40:42 +0000211 if (disk_read(startblock, getsize, bufptr) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200212 debug("Error reading FAT blocks\n");
wdenk7a428cc2003-06-15 22:40:42 +0000213 return ret;
214 }
215 mydata->fatbufnum = bufnum;
216 }
217
218 /* Get the actual entry from the table */
219 switch (mydata->fatsize) {
220 case 32:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200221 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
wdenk7a428cc2003-06-15 22:40:42 +0000222 break;
223 case 16:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200224 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
wdenk7a428cc2003-06-15 22:40:42 +0000225 break;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200226 case 12:
227 off16 = (offset * 3) / 4;
wdenk7a428cc2003-06-15 22:40:42 +0000228
229 switch (offset & 0x3) {
230 case 0:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200231 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
wdenk7a428cc2003-06-15 22:40:42 +0000232 ret &= 0xfff;
233 break;
234 case 1:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200235 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk7a428cc2003-06-15 22:40:42 +0000236 val1 &= 0xf000;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200237 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
wdenk7a428cc2003-06-15 22:40:42 +0000238 val2 &= 0x00ff;
239 ret = (val2 << 4) | (val1 >> 12);
240 break;
241 case 2:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200242 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk7a428cc2003-06-15 22:40:42 +0000243 val1 &= 0xff00;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200244 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
wdenk7a428cc2003-06-15 22:40:42 +0000245 val2 &= 0x000f;
246 ret = (val2 << 8) | (val1 >> 8);
247 break;
248 case 3:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200249 ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk7a428cc2003-06-15 22:40:42 +0000250 ret = (ret & 0xfff0) >> 4;
251 break;
252 default:
253 break;
254 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200255 break;
wdenk7a428cc2003-06-15 22:40:42 +0000256 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200257 debug("FAT%d: ret: %08x, offset: %04x\n",
258 mydata->fatsize, ret, offset);
wdenk7a428cc2003-06-15 22:40:42 +0000259
260 return ret;
261}
262
wdenk7a428cc2003-06-15 22:40:42 +0000263/*
264 * Read at most 'size' bytes from the specified cluster into 'buffer'.
265 * Return 0 on success, -1 otherwise.
266 */
267static int
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200268get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
wdenk7a428cc2003-06-15 22:40:42 +0000269{
Erik Hansen4ea89662011-03-24 10:15:37 +0100270 __u32 idx = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000271 __u32 startsect;
Kyle Moffett83c8a782011-12-20 07:41:13 +0000272 int ret;
wdenk7a428cc2003-06-15 22:40:42 +0000273
274 if (clustnum > 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200275 startsect = mydata->data_begin +
276 clustnum * mydata->clust_size;
wdenk7a428cc2003-06-15 22:40:42 +0000277 } else {
278 startsect = mydata->rootdir_sect;
279 }
280
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200281 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
282
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200283 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
Eric Nelson014705b2012-04-11 04:08:53 +0000284 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200285
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200286 printf("FAT: Misaligned buffer address (%p)\n", buffer);
287
288 while (size >= mydata->sect_size) {
289 ret = disk_read(startsect++, 1, tmpbuf);
290 if (ret != 1) {
291 debug("Error reading data (got %d)\n", ret);
292 return -1;
293 }
294
295 memcpy(buffer, tmpbuf, mydata->sect_size);
296 buffer += mydata->sect_size;
297 size -= mydata->sect_size;
298 }
299 } else {
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000300 idx = size / mydata->sect_size;
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200301 ret = disk_read(startsect, idx, buffer);
302 if (ret != idx) {
303 debug("Error reading data (got %d)\n", ret);
304 return -1;
305 }
306 startsect += idx;
307 idx *= mydata->sect_size;
308 buffer += idx;
309 size -= idx;
310 }
311 if (size) {
312 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
313
314 ret = disk_read(startsect, 1, tmpbuf);
Kyle Moffett83c8a782011-12-20 07:41:13 +0000315 if (ret != 1) {
316 debug("Error reading data (got %d)\n", ret);
wdenk2c9b05d2003-09-10 22:30:53 +0000317 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000318 }
wdenk2c9b05d2003-09-10 22:30:53 +0000319
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200320 memcpy(buffer, tmpbuf, size);
wdenk7a428cc2003-06-15 22:40:42 +0000321 }
322
323 return 0;
324}
325
wdenk7a428cc2003-06-15 22:40:42 +0000326/*
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000327 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
wdenk7a428cc2003-06-15 22:40:42 +0000328 * into 'buffer'.
329 * Return the number of bytes read or -1 on fatal errors.
330 */
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000331__u8 get_contents_vfatname_block[MAX_CLUSTSIZE]
332 __aligned(ARCH_DMA_MINALIGN);
333
wdenk7a428cc2003-06-15 22:40:42 +0000334static long
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000335get_contents(fsdata *mydata, dir_entry *dentptr, unsigned long pos,
336 __u8 *buffer, unsigned long maxsize)
wdenk7a428cc2003-06-15 22:40:42 +0000337{
338 unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000339 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
wdenk7a428cc2003-06-15 22:40:42 +0000340 __u32 curclust = START(dentptr);
wdenk2c9b05d2003-09-10 22:30:53 +0000341 __u32 endclust, newclust;
342 unsigned long actsize;
wdenk7a428cc2003-06-15 22:40:42 +0000343
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200344 debug("Filesize: %ld bytes\n", filesize);
wdenk7a428cc2003-06-15 22:40:42 +0000345
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000346 if (pos >= filesize) {
347 debug("Read position past EOF: %lu\n", pos);
348 return gotsize;
349 }
350
351 if (maxsize > 0 && filesize > pos + maxsize)
352 filesize = pos + maxsize;
wdenk7a428cc2003-06-15 22:40:42 +0000353
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200354 debug("%ld bytes\n", filesize);
wdenk7a428cc2003-06-15 22:40:42 +0000355
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200356 actsize = bytesperclust;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000357
358 /* go to cluster at pos */
359 while (actsize <= pos) {
360 curclust = get_fatent(mydata, curclust);
361 if (CHECK_CLUST(curclust, mydata->fatsize)) {
362 debug("curclust: 0x%x\n", curclust);
363 debug("Invalid FAT entry\n");
364 return gotsize;
365 }
366 actsize += bytesperclust;
367 }
368
369 /* actsize > pos */
370 actsize -= bytesperclust;
371 filesize -= actsize;
372 pos -= actsize;
373
374 /* align to beginning of next cluster if any */
375 if (pos) {
376 actsize = min(filesize, bytesperclust);
377 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
378 (int)actsize) != 0) {
379 printf("Error reading cluster\n");
380 return -1;
381 }
382 filesize -= actsize;
383 actsize -= pos;
384 memcpy(buffer, get_contents_vfatname_block + pos, actsize);
385 gotsize += actsize;
386 if (!filesize)
387 return gotsize;
388 buffer += actsize;
389
390 curclust = get_fatent(mydata, curclust);
391 if (CHECK_CLUST(curclust, mydata->fatsize)) {
392 debug("curclust: 0x%x\n", curclust);
393 debug("Invalid FAT entry\n");
394 return gotsize;
395 }
396 }
397
398 actsize = bytesperclust;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200399 endclust = curclust;
400
wdenk7a428cc2003-06-15 22:40:42 +0000401 do {
wdenk2c9b05d2003-09-10 22:30:53 +0000402 /* search for consecutive clusters */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200403 while (actsize < filesize) {
wdenk2c9b05d2003-09-10 22:30:53 +0000404 newclust = get_fatent(mydata, endclust);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200405 if ((newclust - 1) != endclust)
wdenk2c9b05d2003-09-10 22:30:53 +0000406 goto getit;
michael2c4d2982008-03-02 23:33:46 +0100407 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200408 debug("curclust: 0x%x\n", newclust);
409 debug("Invalid FAT entry\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000410 return gotsize;
411 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200412 endclust = newclust;
413 actsize += bytesperclust;
wdenk2c9b05d2003-09-10 22:30:53 +0000414 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200415
wdenk2c9b05d2003-09-10 22:30:53 +0000416 /* get remaining bytes */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200417 actsize = filesize;
Benoît Thébaudeau01938d82012-07-20 15:21:37 +0200418 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200419 printf("Error reading cluster\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000420 return -1;
421 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200422 gotsize += actsize;
wdenk2c9b05d2003-09-10 22:30:53 +0000423 return gotsize;
424getit:
425 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200426 printf("Error reading cluster\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000427 return -1;
428 }
429 gotsize += (int)actsize;
430 filesize -= actsize;
431 buffer += actsize;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200432
wdenk2c9b05d2003-09-10 22:30:53 +0000433 curclust = get_fatent(mydata, endclust);
michael2c4d2982008-03-02 23:33:46 +0100434 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200435 debug("curclust: 0x%x\n", curclust);
436 printf("Invalid FAT entry\n");
wdenk7a428cc2003-06-15 22:40:42 +0000437 return gotsize;
438 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200439 actsize = bytesperclust;
440 endclust = curclust;
wdenk7a428cc2003-06-15 22:40:42 +0000441 } while (1);
442}
443
wdenk7a428cc2003-06-15 22:40:42 +0000444#ifdef CONFIG_SUPPORT_VFAT
445/*
446 * Extract the file name information from 'slotptr' into 'l_name',
447 * starting at l_name[*idx].
448 * Return 1 if terminator (zero byte) is found, 0 otherwise.
449 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200450static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
wdenk7a428cc2003-06-15 22:40:42 +0000451{
452 int j;
453
454 for (j = 0; j <= 8; j += 2) {
455 l_name[*idx] = slotptr->name0_4[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200456 if (l_name[*idx] == 0x00)
457 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000458 (*idx)++;
459 }
460 for (j = 0; j <= 10; j += 2) {
461 l_name[*idx] = slotptr->name5_10[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200462 if (l_name[*idx] == 0x00)
463 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000464 (*idx)++;
465 }
466 for (j = 0; j <= 2; j += 2) {
467 l_name[*idx] = slotptr->name11_12[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200468 if (l_name[*idx] == 0x00)
469 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000470 (*idx)++;
471 }
472
473 return 0;
474}
475
wdenk7a428cc2003-06-15 22:40:42 +0000476/*
477 * Extract the full long filename starting at 'retdent' (which is really
478 * a slot) into 'l_name'. If successful also copy the real directory entry
479 * into 'retdent'
480 * Return 0 on success, -1 otherwise.
481 */
482static int
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200483get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
484 dir_entry *retdent, char *l_name)
wdenk7a428cc2003-06-15 22:40:42 +0000485{
486 dir_entry *realdent;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200487 dir_slot *slotptr = (dir_slot *)retdent;
Sergei Shtylyov51de6cf2011-08-19 09:37:46 +0000488 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
489 PREFETCH_BLOCKS :
490 mydata->clust_size);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200491 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
wdenk7a428cc2003-06-15 22:40:42 +0000492 int idx = 0;
493
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300494 if (counter > VFAT_MAXSEQ) {
495 debug("Error: VFAT name is too long\n");
496 return -1;
497 }
498
499 while ((__u8 *)slotptr < buflimit) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200500 if (counter == 0)
501 break;
wdenk2ebee312004-02-23 19:30:57 +0000502 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
503 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000504 slotptr++;
505 counter--;
506 }
507
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300508 if ((__u8 *)slotptr >= buflimit) {
wdenk7a428cc2003-06-15 22:40:42 +0000509 dir_slot *slotptr2;
510
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300511 if (curclust == 0)
512 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000513 curclust = get_fatent(mydata, curclust);
michael2c4d2982008-03-02 23:33:46 +0100514 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200515 debug("curclust: 0x%x\n", curclust);
516 printf("Invalid FAT entry\n");
wdenk7a428cc2003-06-15 22:40:42 +0000517 return -1;
518 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200519
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000520 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000521 mydata->clust_size * mydata->sect_size) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200522 debug("Error: reading directory block\n");
wdenk7a428cc2003-06-15 22:40:42 +0000523 return -1;
524 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200525
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000526 slotptr2 = (dir_slot *)get_contents_vfatname_block;
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300527 while (counter > 0) {
528 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
529 & 0xff) != counter)
530 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000531 slotptr2++;
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300532 counter--;
533 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200534
wdenk7a428cc2003-06-15 22:40:42 +0000535 /* Save the real directory entry */
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300536 realdent = (dir_entry *)slotptr2;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000537 while ((__u8 *)slotptr2 > get_contents_vfatname_block) {
wdenk7a428cc2003-06-15 22:40:42 +0000538 slotptr2--;
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300539 slot2str(slotptr2, l_name, &idx);
wdenk7a428cc2003-06-15 22:40:42 +0000540 }
541 } else {
542 /* Save the real directory entry */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200543 realdent = (dir_entry *)slotptr;
wdenk7a428cc2003-06-15 22:40:42 +0000544 }
545
546 do {
547 slotptr--;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200548 if (slot2str(slotptr, l_name, &idx))
549 break;
wdenk2ebee312004-02-23 19:30:57 +0000550 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
wdenk7a428cc2003-06-15 22:40:42 +0000551
552 l_name[idx] = '\0';
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200553 if (*l_name == DELETED_FLAG)
554 *l_name = '\0';
555 else if (*l_name == aRING)
556 *l_name = DELETED_FLAG;
wdenk7a428cc2003-06-15 22:40:42 +0000557 downcase(l_name);
558
559 /* Return the real directory entry */
560 memcpy(retdent, realdent, sizeof(dir_entry));
561
562 return 0;
563}
564
wdenk7a428cc2003-06-15 22:40:42 +0000565/* Calculate short name checksum */
Marek Vasut47a470f2012-10-09 07:20:22 +0000566static __u8 mkcksum(const char name[8], const char ext[3])
wdenk7a428cc2003-06-15 22:40:42 +0000567{
568 int i;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200569
wdenk7a428cc2003-06-15 22:40:42 +0000570 __u8 ret = 0;
571
Marek Vasut4ab1f642013-01-11 03:35:48 +0000572 for (i = 0; i < 8; i++)
Marek Vasut47a470f2012-10-09 07:20:22 +0000573 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i];
Marek Vasut4ab1f642013-01-11 03:35:48 +0000574 for (i = 0; i < 3; i++)
Marek Vasut47a470f2012-10-09 07:20:22 +0000575 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i];
wdenk7a428cc2003-06-15 22:40:42 +0000576
577 return ret;
578}
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200579#endif /* CONFIG_SUPPORT_VFAT */
wdenk7a428cc2003-06-15 22:40:42 +0000580
581/*
582 * Get the directory entry associated with 'filename' from the directory
583 * starting at 'startsect'
584 */
Eric Nelson014705b2012-04-11 04:08:53 +0000585__u8 get_dentfromdir_block[MAX_CLUSTSIZE]
586 __aligned(ARCH_DMA_MINALIGN);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200587
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200588static dir_entry *get_dentfromdir(fsdata *mydata, int startsect,
589 char *filename, dir_entry *retdent,
590 int dols)
wdenk7a428cc2003-06-15 22:40:42 +0000591{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200592 __u16 prevcksum = 0xffff;
593 __u32 curclust = START(retdent);
594 int files = 0, dirs = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000595
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200596 debug("get_dentfromdir: %s\n", filename);
wdenk7a428cc2003-06-15 22:40:42 +0000597
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200598 while (1) {
599 dir_entry *dentptr;
600
601 int i;
602
603 if (get_cluster(mydata, curclust, get_dentfromdir_block,
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000604 mydata->clust_size * mydata->sect_size) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200605 debug("Error: reading directory block\n");
606 return NULL;
607 }
wdenk7a428cc2003-06-15 22:40:42 +0000608
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200609 dentptr = (dir_entry *)get_dentfromdir_block;
610
611 for (i = 0; i < DIRENTSPERCLUST; i++) {
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300612 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200613
614 l_name[0] = '\0';
615 if (dentptr->name[0] == DELETED_FLAG) {
616 dentptr++;
617 continue;
618 }
619 if ((dentptr->attr & ATTR_VOLUME)) {
wdenk7a428cc2003-06-15 22:40:42 +0000620#ifdef CONFIG_SUPPORT_VFAT
J. Vijayanand575e21e2011-10-19 07:43:08 +0000621 if ((dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
622 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200623 prevcksum = ((dir_slot *)dentptr)->alias_checksum;
624 get_vfatname(mydata, curclust,
625 get_dentfromdir_block,
626 dentptr, l_name);
627 if (dols) {
628 int isdir;
629 char dirc;
630 int doit = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000631
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200632 isdir = (dentptr->attr & ATTR_DIR);
633
634 if (isdir) {
635 dirs++;
636 dirc = '/';
637 doit = 1;
638 } else {
639 dirc = ' ';
640 if (l_name[0] != 0) {
641 files++;
642 doit = 1;
643 }
644 }
645 if (doit) {
646 if (dirc == ' ') {
647 printf(" %8ld %s%c\n",
648 (long)FAT2CPU32(dentptr->size),
649 l_name,
650 dirc);
651 } else {
652 printf(" %s%c\n",
653 l_name,
654 dirc);
655 }
656 }
657 dentptr++;
658 continue;
659 }
660 debug("vfatname: |%s|\n", l_name);
661 } else
662#endif
663 {
664 /* Volume label or VFAT entry */
665 dentptr++;
666 continue;
667 }
wdenk7a428cc2003-06-15 22:40:42 +0000668 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200669 if (dentptr->name[0] == 0) {
670 if (dols) {
671 printf("\n%d file(s), %d dir(s)\n\n",
672 files, dirs);
673 }
674 debug("Dentname == NULL - %d\n", i);
675 return NULL;
wdenk7a428cc2003-06-15 22:40:42 +0000676 }
wdenk7a428cc2003-06-15 22:40:42 +0000677#ifdef CONFIG_SUPPORT_VFAT
Marek Vasut47a470f2012-10-09 07:20:22 +0000678 __u8 csum = mkcksum(dentptr->name, dentptr->ext);
679 if (dols && csum == prevcksum) {
Sergei Shtylyov891240a2012-01-02 06:54:29 +0000680 prevcksum = 0xffff;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200681 dentptr++;
682 continue;
683 }
wdenk7a428cc2003-06-15 22:40:42 +0000684#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200685 get_name(dentptr, s_name);
686 if (dols) {
687 int isdir = (dentptr->attr & ATTR_DIR);
688 char dirc;
689 int doit = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000690
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200691 if (isdir) {
692 dirs++;
693 dirc = '/';
694 doit = 1;
695 } else {
696 dirc = ' ';
697 if (s_name[0] != 0) {
698 files++;
699 doit = 1;
700 }
701 }
wdenk7a428cc2003-06-15 22:40:42 +0000702
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200703 if (doit) {
704 if (dirc == ' ') {
705 printf(" %8ld %s%c\n",
706 (long)FAT2CPU32(dentptr->size),
707 s_name, dirc);
708 } else {
709 printf(" %s%c\n",
710 s_name, dirc);
711 }
712 }
wdenk7a428cc2003-06-15 22:40:42 +0000713
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200714 dentptr++;
715 continue;
716 }
717
718 if (strcmp(filename, s_name)
719 && strcmp(filename, l_name)) {
720 debug("Mismatch: |%s|%s|\n", s_name, l_name);
721 dentptr++;
722 continue;
723 }
724
725 memcpy(retdent, dentptr, sizeof(dir_entry));
726
727 debug("DentName: %s", s_name);
728 debug(", start: 0x%x", START(dentptr));
729 debug(", size: 0x%x %s\n",
730 FAT2CPU32(dentptr->size),
731 (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
732
733 return retdent;
734 }
735
736 curclust = get_fatent(mydata, curclust);
737 if (CHECK_CLUST(curclust, mydata->fatsize)) {
738 debug("curclust: 0x%x\n", curclust);
739 printf("Invalid FAT entry\n");
740 return NULL;
741 }
wdenk7a428cc2003-06-15 22:40:42 +0000742 }
wdenk7a428cc2003-06-15 22:40:42 +0000743
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200744 return NULL;
wdenk7a428cc2003-06-15 22:40:42 +0000745}
746
wdenk7a428cc2003-06-15 22:40:42 +0000747/*
748 * Read boot sector and volume info from a FAT filesystem
749 */
750static int
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200751read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
wdenk7a428cc2003-06-15 22:40:42 +0000752{
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000753 __u8 *block;
wdenk7a428cc2003-06-15 22:40:42 +0000754 volume_info *vistart;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000755 int ret = 0;
756
757 if (cur_dev == NULL) {
758 debug("Error: no device selected\n");
759 return -1;
760 }
761
Eric Nelson014705b2012-04-11 04:08:53 +0000762 block = memalign(ARCH_DMA_MINALIGN, cur_dev->blksz);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000763 if (block == NULL) {
764 debug("Error: allocating block\n");
765 return -1;
766 }
wdenk7a428cc2003-06-15 22:40:42 +0000767
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200768 if (disk_read(0, 1, block) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200769 debug("Error: reading block\n");
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000770 goto fail;
wdenk7a428cc2003-06-15 22:40:42 +0000771 }
772
773 memcpy(bs, block, sizeof(boot_sector));
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200774 bs->reserved = FAT2CPU16(bs->reserved);
775 bs->fat_length = FAT2CPU16(bs->fat_length);
776 bs->secs_track = FAT2CPU16(bs->secs_track);
777 bs->heads = FAT2CPU16(bs->heads);
778 bs->total_sect = FAT2CPU32(bs->total_sect);
wdenk7a428cc2003-06-15 22:40:42 +0000779
780 /* FAT32 entries */
781 if (bs->fat_length == 0) {
782 /* Assume FAT32 */
783 bs->fat32_length = FAT2CPU32(bs->fat32_length);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200784 bs->flags = FAT2CPU16(bs->flags);
wdenk7a428cc2003-06-15 22:40:42 +0000785 bs->root_cluster = FAT2CPU32(bs->root_cluster);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200786 bs->info_sector = FAT2CPU16(bs->info_sector);
787 bs->backup_boot = FAT2CPU16(bs->backup_boot);
788 vistart = (volume_info *)(block + sizeof(boot_sector));
wdenk7a428cc2003-06-15 22:40:42 +0000789 *fatsize = 32;
790 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200791 vistart = (volume_info *)&(bs->fat32_length);
wdenk7a428cc2003-06-15 22:40:42 +0000792 *fatsize = 0;
793 }
794 memcpy(volinfo, vistart, sizeof(volume_info));
795
wdenk7a428cc2003-06-15 22:40:42 +0000796 if (*fatsize == 32) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200797 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000798 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000799 } else {
Tom Rix155abaa2009-05-20 07:55:41 -0500800 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk7a428cc2003-06-15 22:40:42 +0000801 *fatsize = 12;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000802 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000803 }
Tom Rix155abaa2009-05-20 07:55:41 -0500804 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk7a428cc2003-06-15 22:40:42 +0000805 *fatsize = 16;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000806 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000807 }
808 }
809
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200810 debug("Error: broken fs_type sign\n");
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000811fail:
812 ret = -1;
813exit:
814 free(block);
815 return ret;
wdenk7a428cc2003-06-15 22:40:42 +0000816}
817
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000818__u8 do_fat_read_at_block[MAX_CLUSTSIZE]
Eric Nelson014705b2012-04-11 04:08:53 +0000819 __aligned(ARCH_DMA_MINALIGN);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200820
stroese83082242004-12-16 17:57:26 +0000821long
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000822do_fat_read_at(const char *filename, unsigned long pos, void *buffer,
823 unsigned long maxsize, int dols)
wdenk7a428cc2003-06-15 22:40:42 +0000824{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200825 char fnamecopy[2048];
826 boot_sector bs;
827 volume_info volinfo;
828 fsdata datablock;
829 fsdata *mydata = &datablock;
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +0200830 dir_entry *dentptr = NULL;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200831 __u16 prevcksum = 0xffff;
832 char *subname = "";
Erik Hansen4ea89662011-03-24 10:15:37 +0100833 __u32 cursect;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200834 int idx, isdir = 0;
835 int files = 0, dirs = 0;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000836 long ret = -1;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200837 int firsttime;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000838 __u32 root_cluster = 0;
Erik Hansen4ea89662011-03-24 10:15:37 +0100839 int rootdir_size = 0;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200840 int j;
wdenk7a428cc2003-06-15 22:40:42 +0000841
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200842 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
843 debug("Error: reading boot sector\n");
844 return -1;
845 }
846
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000847 if (mydata->fatsize == 32) {
848 root_cluster = bs.root_cluster;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200849 mydata->fatlength = bs.fat32_length;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000850 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200851 mydata->fatlength = bs.fat_length;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000852 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200853
854 mydata->fat_sect = bs.reserved;
855
856 cursect = mydata->rootdir_sect
857 = mydata->fat_sect + mydata->fatlength * bs.fats;
858
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000859 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200860 mydata->clust_size = bs.cluster_size;
Kyle Moffett83c8a782011-12-20 07:41:13 +0000861 if (mydata->sect_size != cur_part_info.blksz) {
862 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
863 mydata->sect_size, cur_part_info.blksz);
864 return -1;
865 }
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200866
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200867 if (mydata->fatsize == 32) {
868 mydata->data_begin = mydata->rootdir_sect -
869 (mydata->clust_size * 2);
870 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200871 rootdir_size = ((bs.dir_entries[1] * (int)256 +
872 bs.dir_entries[0]) *
873 sizeof(dir_entry)) /
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000874 mydata->sect_size;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200875 mydata->data_begin = mydata->rootdir_sect +
876 rootdir_size -
877 (mydata->clust_size * 2);
878 }
879
880 mydata->fatbufnum = -1;
Eric Nelson014705b2012-04-11 04:08:53 +0000881 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000882 if (mydata->fatbuf == NULL) {
883 debug("Error: allocating memory\n");
884 return -1;
885 }
wdenk7a428cc2003-06-15 22:40:42 +0000886
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200887#ifdef CONFIG_SUPPORT_VFAT
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200888 debug("VFAT Support enabled\n");
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200889#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200890 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
891 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
892 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
893 "Data begins at: %d\n",
894 root_cluster,
895 mydata->rootdir_sect,
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000896 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
897 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
898 mydata->clust_size);
wdenk7a428cc2003-06-15 22:40:42 +0000899
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200900 /* "cwd" is always the root... */
901 while (ISDIRDELIM(*filename))
902 filename++;
wdenk7a428cc2003-06-15 22:40:42 +0000903
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200904 /* Make a copy of the filename and convert it to lowercase */
905 strcpy(fnamecopy, filename);
906 downcase(fnamecopy);
wdenk7a428cc2003-06-15 22:40:42 +0000907
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200908 if (*fnamecopy == '\0') {
909 if (!dols)
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000910 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200911
912 dols = LS_ROOT;
913 } else if ((idx = dirdelim(fnamecopy)) >= 0) {
914 isdir = 1;
915 fnamecopy[idx] = '\0';
916 subname = fnamecopy + idx + 1;
917
918 /* Handle multiple delimiters */
919 while (ISDIRDELIM(*subname))
920 subname++;
921 } else if (dols) {
922 isdir = 1;
wdenk7a428cc2003-06-15 22:40:42 +0000923 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200924
925 j = 0;
926 while (1) {
927 int i;
928
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +0200929 if (j == 0) {
930 debug("FAT read sect=%d, clust_size=%d, DIRENTSPERBLOCK=%zd\n",
931 cursect, mydata->clust_size, DIRENTSPERBLOCK);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200932
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +0200933 if (disk_read(cursect,
934 (mydata->fatsize == 32) ?
935 (mydata->clust_size) :
936 PREFETCH_BLOCKS,
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000937 do_fat_read_at_block) < 0) {
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +0200938 debug("Error: reading rootdir block\n");
939 goto exit;
940 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200941
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000942 dentptr = (dir_entry *) do_fat_read_at_block;
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +0200943 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200944
945 for (i = 0; i < DIRENTSPERBLOCK; i++) {
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300946 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
Marek Vasut47a470f2012-10-09 07:20:22 +0000947 __u8 csum;
wdenk7a428cc2003-06-15 22:40:42 +0000948
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200949 l_name[0] = '\0';
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300950 if (dentptr->name[0] == DELETED_FLAG) {
951 dentptr++;
952 continue;
953 }
Marek Vasut47a470f2012-10-09 07:20:22 +0000954
955 csum = mkcksum(dentptr->name, dentptr->ext);
956 if (dentptr->attr & ATTR_VOLUME) {
wdenk7a428cc2003-06-15 22:40:42 +0000957#ifdef CONFIG_SUPPORT_VFAT
J. Vijayanand575e21e2011-10-19 07:43:08 +0000958 if ((dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200959 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
960 prevcksum =
961 ((dir_slot *)dentptr)->alias_checksum;
wdenk7a428cc2003-06-15 22:40:42 +0000962
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300963 get_vfatname(mydata,
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000964 root_cluster,
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000965 do_fat_read_at_block,
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200966 dentptr, l_name);
967
968 if (dols == LS_ROOT) {
969 char dirc;
970 int doit = 0;
971 int isdir =
972 (dentptr->attr & ATTR_DIR);
973
974 if (isdir) {
975 dirs++;
976 dirc = '/';
977 doit = 1;
978 } else {
979 dirc = ' ';
980 if (l_name[0] != 0) {
981 files++;
982 doit = 1;
983 }
984 }
985 if (doit) {
986 if (dirc == ' ') {
987 printf(" %8ld %s%c\n",
988 (long)FAT2CPU32(dentptr->size),
989 l_name,
990 dirc);
991 } else {
992 printf(" %s%c\n",
993 l_name,
994 dirc);
995 }
996 }
997 dentptr++;
998 continue;
999 }
1000 debug("Rootvfatname: |%s|\n",
1001 l_name);
1002 } else
wdenk7a428cc2003-06-15 22:40:42 +00001003#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001004 {
1005 /* Volume label or VFAT entry */
1006 dentptr++;
1007 continue;
1008 }
1009 } else if (dentptr->name[0] == 0) {
1010 debug("RootDentname == NULL - %d\n", i);
1011 if (dols == LS_ROOT) {
1012 printf("\n%d file(s), %d dir(s)\n\n",
1013 files, dirs);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001014 ret = 0;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001015 }
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001016 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001017 }
wdenk7a428cc2003-06-15 22:40:42 +00001018#ifdef CONFIG_SUPPORT_VFAT
Marek Vasut47a470f2012-10-09 07:20:22 +00001019 else if (dols == LS_ROOT && csum == prevcksum) {
Sergei Shtylyov891240a2012-01-02 06:54:29 +00001020 prevcksum = 0xffff;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001021 dentptr++;
1022 continue;
1023 }
wdenk7a428cc2003-06-15 22:40:42 +00001024#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001025 get_name(dentptr, s_name);
wdenk7a428cc2003-06-15 22:40:42 +00001026
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001027 if (dols == LS_ROOT) {
1028 int isdir = (dentptr->attr & ATTR_DIR);
1029 char dirc;
1030 int doit = 0;
1031
1032 if (isdir) {
1033 dirc = '/';
1034 if (s_name[0] != 0) {
1035 dirs++;
1036 doit = 1;
1037 }
1038 } else {
1039 dirc = ' ';
1040 if (s_name[0] != 0) {
1041 files++;
1042 doit = 1;
1043 }
1044 }
1045 if (doit) {
1046 if (dirc == ' ') {
1047 printf(" %8ld %s%c\n",
1048 (long)FAT2CPU32(dentptr->size),
1049 s_name, dirc);
1050 } else {
1051 printf(" %s%c\n",
1052 s_name, dirc);
1053 }
1054 }
1055 dentptr++;
1056 continue;
1057 }
1058
1059 if (strcmp(fnamecopy, s_name)
1060 && strcmp(fnamecopy, l_name)) {
1061 debug("RootMismatch: |%s|%s|\n", s_name,
1062 l_name);
1063 dentptr++;
1064 continue;
1065 }
1066
1067 if (isdir && !(dentptr->attr & ATTR_DIR))
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001068 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001069
1070 debug("RootName: %s", s_name);
1071 debug(", start: 0x%x", START(dentptr));
1072 debug(", size: 0x%x %s\n",
1073 FAT2CPU32(dentptr->size),
1074 isdir ? "(DIR)" : "");
1075
1076 goto rootdir_done; /* We got a match */
wdenk7a428cc2003-06-15 22:40:42 +00001077 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001078 debug("END LOOP: j=%d clust_size=%d\n", j,
1079 mydata->clust_size);
wdenk7a428cc2003-06-15 22:40:42 +00001080
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001081 /*
1082 * On FAT32 we must fetch the FAT entries for the next
1083 * root directory clusters when a cluster has been
1084 * completely processed.
1085 */
Erik Hansen4ea89662011-03-24 10:15:37 +01001086 ++j;
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001087 int rootdir_end = 0;
1088 if (mydata->fatsize == 32) {
1089 if (j == mydata->clust_size) {
1090 int nxtsect = 0;
1091 int nxt_clust = 0;
wdenk7a428cc2003-06-15 22:40:42 +00001092
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001093 nxt_clust = get_fatent(mydata, root_cluster);
1094 rootdir_end = CHECK_CLUST(nxt_clust, 32);
Wolfgang Denka48d0a32010-07-19 11:36:58 +02001095
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001096 nxtsect = mydata->data_begin +
1097 (nxt_clust * mydata->clust_size);
Wolfgang Denka48d0a32010-07-19 11:36:58 +02001098
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001099 root_cluster = nxt_clust;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001100
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001101 cursect = nxtsect;
1102 j = 0;
1103 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001104 } else {
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001105 if (j == PREFETCH_BLOCKS)
1106 j = 0;
1107
1108 rootdir_end = (++cursect - mydata->rootdir_sect >=
1109 rootdir_size);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001110 }
Erik Hansen4ea89662011-03-24 10:15:37 +01001111
1112 /* If end of rootdir reached */
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001113 if (rootdir_end) {
Erik Hansen4ea89662011-03-24 10:15:37 +01001114 if (dols == LS_ROOT) {
1115 printf("\n%d file(s), %d dir(s)\n\n",
1116 files, dirs);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001117 ret = 0;
Erik Hansen4ea89662011-03-24 10:15:37 +01001118 }
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001119 goto exit;
Erik Hansen4ea89662011-03-24 10:15:37 +01001120 }
Wolfgang Denka48d0a32010-07-19 11:36:58 +02001121 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001122rootdir_done:
wdenk7a428cc2003-06-15 22:40:42 +00001123
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001124 firsttime = 1;
wdenk7a428cc2003-06-15 22:40:42 +00001125
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001126 while (isdir) {
1127 int startsect = mydata->data_begin
1128 + START(dentptr) * mydata->clust_size;
1129 dir_entry dent;
1130 char *nextname = NULL;
wdenk7a428cc2003-06-15 22:40:42 +00001131
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001132 dent = *dentptr;
1133 dentptr = &dent;
wdenk7a428cc2003-06-15 22:40:42 +00001134
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001135 idx = dirdelim(subname);
wdenk7a428cc2003-06-15 22:40:42 +00001136
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001137 if (idx >= 0) {
1138 subname[idx] = '\0';
1139 nextname = subname + idx + 1;
1140 /* Handle multiple delimiters */
1141 while (ISDIRDELIM(*nextname))
1142 nextname++;
1143 if (dols && *nextname == '\0')
1144 firsttime = 0;
1145 } else {
1146 if (dols && firsttime) {
1147 firsttime = 0;
1148 } else {
1149 isdir = 0;
1150 }
1151 }
1152
1153 if (get_dentfromdir(mydata, startsect, subname, dentptr,
1154 isdir ? 0 : dols) == NULL) {
1155 if (dols && !isdir)
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001156 ret = 0;
1157 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001158 }
1159
Benoît Thébaudeau7ca2a8a2012-07-20 03:20:29 +00001160 if (isdir && !(dentptr->attr & ATTR_DIR))
1161 goto exit;
1162
1163 if (idx >= 0)
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001164 subname = nextname;
wdenk7a428cc2003-06-15 22:40:42 +00001165 }
wdenk7a428cc2003-06-15 22:40:42 +00001166
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001167 ret = get_contents(mydata, dentptr, pos, buffer, maxsize);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001168 debug("Size: %d, got: %ld\n", FAT2CPU32(dentptr->size), ret);
wdenk7a428cc2003-06-15 22:40:42 +00001169
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001170exit:
1171 free(mydata->fatbuf);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001172 return ret;
1173}
wdenk7a428cc2003-06-15 22:40:42 +00001174
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001175long
1176do_fat_read(const char *filename, void *buffer, unsigned long maxsize, int dols)
1177{
1178 return do_fat_read_at(filename, 0, buffer, maxsize, dols);
1179}
1180
Benoît Thébaudeau0a120372012-07-20 15:18:44 +02001181int file_fat_detectfs(void)
wdenk7a428cc2003-06-15 22:40:42 +00001182{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001183 boot_sector bs;
1184 volume_info volinfo;
1185 int fatsize;
1186 char vol_label[12];
wdenk7a428cc2003-06-15 22:40:42 +00001187
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001188 if (cur_dev == NULL) {
wdenk2c9b05d2003-09-10 22:30:53 +00001189 printf("No current device\n");
1190 return 1;
1191 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001192
Jon Loeliger80eb47c2007-07-09 17:56:50 -05001193#if defined(CONFIG_CMD_IDE) || \
Sonic Zhang853208e2008-12-09 23:20:18 -05001194 defined(CONFIG_CMD_SATA) || \
Jon Loeliger80eb47c2007-07-09 17:56:50 -05001195 defined(CONFIG_CMD_SCSI) || \
1196 defined(CONFIG_CMD_USB) || \
Andy Fleming1efe5782008-01-16 13:06:59 -06001197 defined(CONFIG_MMC)
wdenk2c9b05d2003-09-10 22:30:53 +00001198 printf("Interface: ");
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001199 switch (cur_dev->if_type) {
1200 case IF_TYPE_IDE:
1201 printf("IDE");
1202 break;
1203 case IF_TYPE_SATA:
1204 printf("SATA");
1205 break;
1206 case IF_TYPE_SCSI:
1207 printf("SCSI");
1208 break;
1209 case IF_TYPE_ATAPI:
1210 printf("ATAPI");
1211 break;
1212 case IF_TYPE_USB:
1213 printf("USB");
1214 break;
1215 case IF_TYPE_DOC:
1216 printf("DOC");
1217 break;
1218 case IF_TYPE_MMC:
1219 printf("MMC");
1220 break;
1221 default:
1222 printf("Unknown");
wdenk2c9b05d2003-09-10 22:30:53 +00001223 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001224
1225 printf("\n Device %d: ", cur_dev->dev);
wdenk2c9b05d2003-09-10 22:30:53 +00001226 dev_print(cur_dev);
1227#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001228
1229 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
wdenk2c9b05d2003-09-10 22:30:53 +00001230 printf("\nNo valid FAT fs found\n");
1231 return 1;
1232 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001233
1234 memcpy(vol_label, volinfo.volume_label, 11);
wdenk2c9b05d2003-09-10 22:30:53 +00001235 vol_label[11] = '\0';
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001236 volinfo.fs_type[5] = '\0';
1237
Stephen Warrend398dbe2012-10-17 06:44:57 +00001238 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001239
wdenk2c9b05d2003-09-10 22:30:53 +00001240 return 0;
wdenk7a428cc2003-06-15 22:40:42 +00001241}
1242
Benoît Thébaudeau0a120372012-07-20 15:18:44 +02001243int file_fat_ls(const char *dir)
wdenk7a428cc2003-06-15 22:40:42 +00001244{
1245 return do_fat_read(dir, NULL, 0, LS_YES);
1246}
1247
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001248long file_fat_read_at(const char *filename, unsigned long pos, void *buffer,
1249 unsigned long maxsize)
wdenk7a428cc2003-06-15 22:40:42 +00001250{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001251 printf("reading %s\n", filename);
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001252 return do_fat_read_at(filename, pos, buffer, maxsize, LS_NO);
1253}
1254
1255long file_fat_read(const char *filename, void *buffer, unsigned long maxsize)
1256{
1257 return file_fat_read_at(filename, 0, buffer, maxsize);
wdenk7a428cc2003-06-15 22:40:42 +00001258}