1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
31 #include "timefuncs.h"
33 #include "rbunicode.h"
34 /*#define LOGF_ENABLE*/
37 #define BYTES2INT16(array,pos) \
38 (array[pos] | (array[pos+1] << 8 ))
39 #define BYTES2INT32(array,pos) \
40 ((long)array[pos] | ((long)array[pos+1] << 8 ) | \
41 ((long)array[pos+2] << 16 ) | ((long)array[pos+3] << 24 ))
43 #define FATTYPE_FAT12 0
44 #define FATTYPE_FAT16 1
45 #define FATTYPE_FAT32 2
47 /* BPB offsets; generic */
50 #define BPB_BYTSPERSEC 11
51 #define BPB_SECPERCLUS 13
52 #define BPB_RSVDSECCNT 14
53 #define BPB_NUMFATS 16
54 #define BPB_ROOTENTCNT 17
55 #define BPB_TOTSEC16 19
57 #define BPB_FATSZ16 22
58 #define BPB_SECPERTRK 24
59 #define BPB_NUMHEADS 26
60 #define BPB_HIDDSEC 28
61 #define BPB_TOTSEC32 32
65 #define BS_RESERVED1 37
69 #define BS_FILSYSTYPE 54
72 #define BPB_FATSZ32 36
73 #define BPB_EXTFLAGS 40
75 #define BPB_ROOTCLUS 44
77 #define BPB_BKBOOTSEC 50
78 #define BS_32_DRVNUM 64
79 #define BS_32_BOOTSIG 66
80 #define BS_32_VOLID 67
81 #define BS_32_VOLLAB 71
82 #define BS_32_FILSYSTYPE 82
84 #define BPB_LAST_WORD 510
88 #define FAT_ATTR_LONG_NAME (FAT_ATTR_READ_ONLY | FAT_ATTR_HIDDEN | \
89 FAT_ATTR_SYSTEM | FAT_ATTR_VOLUME_ID)
90 #define FAT_ATTR_LONG_NAME_MASK (FAT_ATTR_READ_ONLY | FAT_ATTR_HIDDEN | \
91 FAT_ATTR_SYSTEM | FAT_ATTR_VOLUME_ID | \
92 FAT_ATTR_DIRECTORY | FAT_ATTR_ARCHIVE )
95 #define FAT_NTRES_LC_NAME 0x08
96 #define FAT_NTRES_LC_EXT 0x10
99 #define FATDIR_ATTR 11
100 #define FATDIR_NTRES 12
101 #define FATDIR_CRTTIMETENTH 13
102 #define FATDIR_CRTTIME 14
103 #define FATDIR_CRTDATE 16
104 #define FATDIR_LSTACCDATE 18
105 #define FATDIR_FSTCLUSHI 20
106 #define FATDIR_WRTTIME 22
107 #define FATDIR_WRTDATE 24
108 #define FATDIR_FSTCLUSLO 26
109 #define FATDIR_FILESIZE 28
111 #define FATLONG_ORDER 0
112 #define FATLONG_TYPE 12
113 #define FATLONG_CHKSUM 13
114 #define FATLONG_LAST_LONG_ENTRY 0x40
115 #define FATLONG_NAME_BYTES_PER_ENTRY 26
116 /* at most 20 LFN entries, keep coherent with fat_dir->longname size ! */
117 #define FATLONG_MAX_ORDER 20
119 #define FATLONG_NAME_CHUNKS 3
120 static unsigned char FATLONG_NAME_POS
[FATLONG_NAME_CHUNKS
] = {1, 14, 28};
121 static unsigned char FATLONG_NAME_SIZE
[FATLONG_NAME_CHUNKS
] = {10, 12, 4};
123 #define CLUSTERS_PER_FAT_SECTOR (SECTOR_SIZE / 4)
124 #define CLUSTERS_PER_FAT16_SECTOR (SECTOR_SIZE / 2)
125 #define DIR_ENTRIES_PER_SECTOR (SECTOR_SIZE / DIR_ENTRY_SIZE)
126 #define DIR_ENTRY_SIZE 32
127 #define NAME_BYTES_PER_ENTRY 13
128 #define FAT_BAD_MARK 0x0ffffff7
129 #define FAT_EOF_MARK 0x0ffffff8
130 #define FAT_LONGNAME_PAD_BYTE 0xff
131 #define FAT_LONGNAME_PAD_UCS 0xffff
134 unsigned long freecount
; /* last known free cluster count */
135 unsigned long nextfree
; /* first cluster to start looking for free
136 clusters, or 0xffffffff for no hint */
139 #define FSINFO_FREECOUNT 488
140 #define FSINFO_NEXTFREE 492
142 /* Note: This struct doesn't hold the raw values after mounting if
143 * bpb_bytspersec isn't 512. All sector counts are normalized to 512 byte
144 * physical sectors. */
147 int bpb_bytspersec
; /* Bytes per sector, typically 512 */
148 unsigned int bpb_secperclus
; /* Sectors per cluster */
149 int bpb_rsvdseccnt
; /* Number of reserved sectors */
150 int bpb_numfats
; /* Number of FAT structures, typically 2 */
151 int bpb_totsec16
; /* Number of sectors on the volume (old 16-bit) */
152 int bpb_media
; /* Media type (typically 0xf0 or 0xf8) */
153 int bpb_fatsz16
; /* Number of used sectors per FAT structure */
154 unsigned long bpb_totsec32
; /* Number of sectors on the volume
156 unsigned int last_word
; /* 0xAA55 */
158 /**** FAT32 specific *****/
163 /* variables for internal use */
164 unsigned long fatsize
;
165 unsigned long totalsectors
;
166 unsigned long rootdirsector
;
167 unsigned long firstdatasector
;
168 unsigned long startsector
;
169 unsigned long dataclusters
;
170 struct fsinfo fsinfo
;
171 #ifdef HAVE_FAT16SUPPORT
172 int bpb_rootentcnt
; /* Number of dir entries in the root */
173 /* internals for FAT16 support */
174 bool is_fat16
; /* true if we mounted a FAT16 partition, false if FAT32 */
175 unsigned int rootdiroffset
; /* sector offset of root dir relative to start
176 * of first pseudo cluster */
177 #endif /* #ifdef HAVE_FAT16SUPPORT */
178 #ifdef HAVE_MULTIVOLUME
179 #ifdef HAVE_MULTIDRIVE
180 int drive
; /* on which physical device is this located */
182 bool mounted
; /* flag if this volume is mounted */
186 static struct bpb fat_bpbs
[NUM_VOLUMES
]; /* mounted partition info */
187 static bool initialized
= false;
189 static int update_fsinfo(IF_MV_NONVOID(struct bpb
* fat_bpb
));
190 static int flush_fat(IF_MV_NONVOID(struct bpb
* fat_bpb
));
191 static int bpb_is_sane(IF_MV_NONVOID(struct bpb
* fat_bpb
));
192 static void *cache_fat_sector(IF_MV2(struct bpb
* fat_bpb
,)
193 long secnum
, bool dirty
);
194 static void create_dos_name(const unsigned char *name
, unsigned char *newname
);
195 static void randomize_dos_name(unsigned char *name
);
196 static unsigned long find_free_cluster(IF_MV2(struct bpb
* fat_bpb
,)
197 unsigned long start
);
198 static int transfer(IF_MV2(struct bpb
* fat_bpb
,) unsigned long start
,
199 long count
, char* buf
, bool write
);
201 #define FAT_CACHE_SIZE 0x20
202 #define FAT_CACHE_MASK (FAT_CACHE_SIZE-1)
204 struct fat_cache_entry
209 #ifdef HAVE_MULTIVOLUME
210 struct bpb
* fat_vol
; /* shared cache for all volumes */
214 static char fat_cache_sectors
[FAT_CACHE_SIZE
][SECTOR_SIZE
];
215 static struct fat_cache_entry fat_cache
[FAT_CACHE_SIZE
];
216 static struct mutex cache_mutex SHAREDBSS_ATTR
;
218 #if defined(HAVE_HOTSWAP) && !(CONFIG_STORAGE & STORAGE_MMC) /* A better condition ?? */
221 mutex_lock(&cache_mutex
);
224 void fat_unlock(void)
226 mutex_unlock(&cache_mutex
);
230 static long cluster2sec(IF_MV2(struct bpb
* fat_bpb
,) long cluster
)
232 #ifndef HAVE_MULTIVOLUME
233 struct bpb
* fat_bpb
= &fat_bpbs
[0];
235 #ifdef HAVE_FAT16SUPPORT
236 /* negative clusters (FAT16 root dir) don't get the 2 offset */
237 int zerocluster
= cluster
< 0 ? 0 : 2;
239 const long zerocluster
= 2;
242 if (cluster
> (long)(fat_bpb
->dataclusters
+ 1))
244 DEBUGF( "cluster2sec() - Bad cluster number (%ld)\n", cluster
);
248 return (cluster
- zerocluster
) * fat_bpb
->bpb_secperclus
249 + fat_bpb
->firstdatasector
;
252 void fat_size(IF_MV2(int volume
,) unsigned long* size
, unsigned long* free
)
254 #ifndef HAVE_MULTIVOLUME
255 const int volume
= 0;
257 struct bpb
* fat_bpb
= &fat_bpbs
[volume
];
259 *size
= fat_bpb
->dataclusters
* (fat_bpb
->bpb_secperclus
* SECTOR_SIZE
/ 1024);
261 *free
= fat_bpb
->fsinfo
.freecount
* (fat_bpb
->bpb_secperclus
* SECTOR_SIZE
/ 1024);
271 mutex_init(&cache_mutex
);
274 #ifdef HAVE_PRIORITY_SCHEDULING
275 /* Disable this because it is dangerous due to the assumption that
276 * mutex_unlock won't yield */
277 mutex_set_preempt(&cache_mutex
, false);
280 /* mark the FAT cache as unused */
281 for(i
= 0;i
< FAT_CACHE_SIZE
;i
++)
283 fat_cache
[i
].secnum
= 8; /* We use a "safe" sector just in case */
284 fat_cache
[i
].inuse
= false;
285 fat_cache
[i
].dirty
= false;
286 #ifdef HAVE_MULTIVOLUME
287 fat_cache
[i
].fat_vol
= NULL
;
290 #ifdef HAVE_MULTIVOLUME
291 /* mark the possible volumes as not mounted */
292 for (i
=0; i
<NUM_VOLUMES
;i
++)
294 fat_bpbs
[i
].mounted
= false;
299 /* fat_mount_internal is split out of fat_mount() to avoid having both the sector
300 * buffer used here and the sector buffer used by update_fsinfo() on stack */
301 static int fat_mount_internal(IF_MV2(int volume
,) IF_MD2(int drive
,) long startsector
)
303 #ifndef HAVE_MULTIVOLUME
304 const int volume
= 0;
306 struct bpb
* fat_bpb
= &fat_bpbs
[volume
];
307 unsigned char buf
[SECTOR_SIZE
];
311 #ifdef HAVE_FAT16SUPPORT
315 /* Read the sector */
316 rc
= storage_read_sectors(IF_MD2(drive
,) startsector
,1,buf
);
319 DEBUGF( "fat_mount() - Couldn't read BPB (error code %d)\n", rc
);
323 memset(fat_bpb
, 0, sizeof(struct bpb
));
324 fat_bpb
->startsector
= startsector
;
325 #ifdef HAVE_MULTIDRIVE
326 fat_bpb
->drive
= drive
;
329 fat_bpb
->bpb_bytspersec
= BYTES2INT16(buf
,BPB_BYTSPERSEC
);
330 secmult
= fat_bpb
->bpb_bytspersec
/ SECTOR_SIZE
;
331 /* Sanity check is performed later */
333 fat_bpb
->bpb_secperclus
= secmult
* buf
[BPB_SECPERCLUS
];
334 fat_bpb
->bpb_rsvdseccnt
= secmult
* BYTES2INT16(buf
,BPB_RSVDSECCNT
);
335 fat_bpb
->bpb_numfats
= buf
[BPB_NUMFATS
];
336 fat_bpb
->bpb_media
= buf
[BPB_MEDIA
];
337 fat_bpb
->bpb_fatsz16
= secmult
* BYTES2INT16(buf
,BPB_FATSZ16
);
338 fat_bpb
->bpb_fatsz32
= secmult
* BYTES2INT32(buf
,BPB_FATSZ32
);
339 fat_bpb
->bpb_totsec16
= secmult
* BYTES2INT16(buf
,BPB_TOTSEC16
);
340 fat_bpb
->bpb_totsec32
= secmult
* BYTES2INT32(buf
,BPB_TOTSEC32
);
341 fat_bpb
->last_word
= BYTES2INT16(buf
,BPB_LAST_WORD
);
343 /* calculate a few commonly used values */
344 if (fat_bpb
->bpb_fatsz16
!= 0)
345 fat_bpb
->fatsize
= fat_bpb
->bpb_fatsz16
;
347 fat_bpb
->fatsize
= fat_bpb
->bpb_fatsz32
;
349 if (fat_bpb
->bpb_totsec16
!= 0)
350 fat_bpb
->totalsectors
= fat_bpb
->bpb_totsec16
;
352 fat_bpb
->totalsectors
= fat_bpb
->bpb_totsec32
;
354 #ifdef HAVE_FAT16SUPPORT
355 fat_bpb
->bpb_rootentcnt
= BYTES2INT16(buf
,BPB_ROOTENTCNT
);
356 if (!fat_bpb
->bpb_bytspersec
)
358 rootdirsectors
= secmult
* ((fat_bpb
->bpb_rootentcnt
* DIR_ENTRY_SIZE
359 + fat_bpb
->bpb_bytspersec
- 1) / fat_bpb
->bpb_bytspersec
);
360 #endif /* #ifdef HAVE_FAT16SUPPORT */
362 fat_bpb
->firstdatasector
= fat_bpb
->bpb_rsvdseccnt
363 #ifdef HAVE_FAT16SUPPORT
366 + fat_bpb
->bpb_numfats
* fat_bpb
->fatsize
;
368 /* Determine FAT type */
369 datasec
= fat_bpb
->totalsectors
- fat_bpb
->firstdatasector
;
370 if (fat_bpb
->bpb_secperclus
)
371 fat_bpb
->dataclusters
= datasec
/ fat_bpb
->bpb_secperclus
;
377 we are sometimes testing with "illegally small" fat32 images,
378 so we don't use the proper fat32 test case for test code
380 if ( fat_bpb
->bpb_fatsz16
)
382 if ( fat_bpb
->dataclusters
< 65525 )
385 #ifdef HAVE_FAT16SUPPORT
386 fat_bpb
->is_fat16
= true;
387 if (fat_bpb
->dataclusters
< 4085)
389 DEBUGF("This is FAT12. Go away!\n");
392 #else /* #ifdef HAVE_FAT16SUPPORT */
393 DEBUGF("This is not FAT32. Go away!\n");
395 #endif /* #ifndef HAVE_FAT16SUPPORT */
398 #ifdef HAVE_FAT16SUPPORT
399 if (fat_bpb
->is_fat16
)
400 { /* FAT16 specific part of BPB */
402 fat_bpb
->rootdirsector
= fat_bpb
->bpb_rsvdseccnt
403 + fat_bpb
->bpb_numfats
* fat_bpb
->bpb_fatsz16
;
404 dirclusters
= ((rootdirsectors
+ fat_bpb
->bpb_secperclus
- 1)
405 / fat_bpb
->bpb_secperclus
); /* rounded up, to full clusters */
406 /* I assign negative pseudo cluster numbers for the root directory,
407 their range is counted upward until -1. */
408 fat_bpb
->bpb_rootclus
= 0 - dirclusters
; /* backwards, before the data*/
409 fat_bpb
->rootdiroffset
= dirclusters
* fat_bpb
->bpb_secperclus
413 #endif /* #ifdef HAVE_FAT16SUPPORT */
414 { /* FAT32 specific part of BPB */
415 fat_bpb
->bpb_rootclus
= BYTES2INT32(buf
,BPB_ROOTCLUS
);
416 fat_bpb
->bpb_fsinfo
= secmult
* BYTES2INT16(buf
,BPB_FSINFO
);
417 fat_bpb
->rootdirsector
= cluster2sec(IF_MV2(fat_bpb
,)
418 fat_bpb
->bpb_rootclus
);
421 rc
= bpb_is_sane(IF_MV(fat_bpb
));
424 DEBUGF( "fat_mount() - BPB is not sane\n");
428 #ifdef HAVE_FAT16SUPPORT
429 if (fat_bpb
->is_fat16
)
431 fat_bpb
->fsinfo
.freecount
= 0xffffffff; /* force recalc below */
432 fat_bpb
->fsinfo
.nextfree
= 0xffffffff;
435 #endif /* #ifdef HAVE_FAT16SUPPORT */
437 /* Read the fsinfo sector */
438 rc
= storage_read_sectors(IF_MD2(drive
,)
439 startsector
+ fat_bpb
->bpb_fsinfo
, 1, buf
);
442 DEBUGF( "fat_mount() - Couldn't read FSInfo (error code %d)\n", rc
);
445 fat_bpb
->fsinfo
.freecount
= BYTES2INT32(buf
, FSINFO_FREECOUNT
);
446 fat_bpb
->fsinfo
.nextfree
= BYTES2INT32(buf
, FSINFO_NEXTFREE
);
451 int fat_mount(IF_MV2(int volume
,) IF_MD2(int drive
,) long startsector
)
453 #ifndef HAVE_MULTIVOLUME
454 const int volume
= 0;
456 struct bpb
* fat_bpb
= &fat_bpbs
[volume
];
459 rc
= fat_mount_internal(IF_MV2(volume
,) IF_MD2(drive
,) startsector
);
463 /* calculate freecount if unset */
464 if ( fat_bpb
->fsinfo
.freecount
== 0xffffffff )
466 fat_recalc_free(IF_MV(volume
));
469 LDEBUGF("Freecount: %ld\n",fat_bpb
->fsinfo
.freecount
);
470 LDEBUGF("Nextfree: 0x%lx\n",fat_bpb
->fsinfo
.nextfree
);
471 LDEBUGF("Cluster count: 0x%lx\n",fat_bpb
->dataclusters
);
472 LDEBUGF("Sectors per cluster: %d\n",fat_bpb
->bpb_secperclus
);
473 LDEBUGF("FAT sectors: 0x%lx\n",fat_bpb
->fatsize
);
475 #ifdef HAVE_MULTIVOLUME
476 fat_bpb
->mounted
= true;
482 int fat_unmount(int volume
, bool flush
)
485 #ifdef HAVE_MULTIVOLUME
486 struct bpb
* fat_bpb
= &fat_bpbs
[volume
];
493 rc
= flush_fat(IF_MV(fat_bpb
)); /* the clean way, while still alive */
496 { /* volume is not accessible any more, e.g. MMC removed */
498 mutex_lock(&cache_mutex
);
499 for(i
= 0;i
< FAT_CACHE_SIZE
;i
++)
501 struct fat_cache_entry
*fce
= &fat_cache
[i
];
503 #ifdef HAVE_MULTIVOLUME
504 && fce
->fat_vol
== fat_bpb
508 fce
->inuse
= false; /* discard all from that volume */
512 mutex_unlock(&cache_mutex
);
515 #ifdef HAVE_MULTIVOLUME
516 fat_bpb
->mounted
= false;
521 void fat_recalc_free(IF_MV_NONVOID(int volume
))
523 #ifndef HAVE_MULTIVOLUME
524 const int volume
= 0;
526 struct bpb
* fat_bpb
= &fat_bpbs
[volume
];
529 #ifdef HAVE_FAT16SUPPORT
530 if (fat_bpb
->is_fat16
)
532 for (i
= 0; i
<fat_bpb
->fatsize
; i
++) {
534 unsigned short* fat
= cache_fat_sector(IF_MV2(fat_bpb
,) i
, false);
535 for (j
= 0; j
< CLUSTERS_PER_FAT16_SECTOR
; j
++) {
536 unsigned int c
= i
* CLUSTERS_PER_FAT16_SECTOR
+ j
;
537 if ( c
> fat_bpb
->dataclusters
+1 ) /* nr 0 is unused */
540 if (letoh16(fat
[j
]) == 0x0000) {
542 if ( fat_bpb
->fsinfo
.nextfree
== 0xffffffff )
543 fat_bpb
->fsinfo
.nextfree
= c
;
549 #endif /* #ifdef HAVE_FAT16SUPPORT */
551 for (i
= 0; i
<fat_bpb
->fatsize
; i
++) {
553 unsigned long* fat
= cache_fat_sector(IF_MV2(fat_bpb
,) i
, false);
554 for (j
= 0; j
< CLUSTERS_PER_FAT_SECTOR
; j
++) {
555 unsigned long c
= i
* CLUSTERS_PER_FAT_SECTOR
+ j
;
556 if ( c
> fat_bpb
->dataclusters
+1 ) /* nr 0 is unused */
559 if (!(letoh32(fat
[j
]) & 0x0fffffff)) {
561 if ( fat_bpb
->fsinfo
.nextfree
== 0xffffffff )
562 fat_bpb
->fsinfo
.nextfree
= c
;
567 fat_bpb
->fsinfo
.freecount
= free
;
568 update_fsinfo(IF_MV(fat_bpb
));
571 static int bpb_is_sane(IF_MV_NONVOID(struct bpb
* fat_bpb
))
573 #ifndef HAVE_MULTIVOLUME
574 struct bpb
* fat_bpb
= &fat_bpbs
[0];
576 if(fat_bpb
->bpb_bytspersec
% SECTOR_SIZE
)
578 DEBUGF( "bpb_is_sane() - Error: sector size is not sane (%d)\n",
579 fat_bpb
->bpb_bytspersec
);
582 if((long)fat_bpb
->bpb_secperclus
* (long)fat_bpb
->bpb_bytspersec
585 DEBUGF( "bpb_is_sane() - Error: cluster size is larger than 128K "
587 fat_bpb
->bpb_bytspersec
, fat_bpb
->bpb_secperclus
,
588 fat_bpb
->bpb_bytspersec
* fat_bpb
->bpb_secperclus
);
591 if(fat_bpb
->bpb_numfats
!= 2)
593 DEBUGF( "bpb_is_sane() - Warning: NumFATS is not 2 (%d)\n",
594 fat_bpb
->bpb_numfats
);
596 if(fat_bpb
->bpb_media
!= 0xf0 && fat_bpb
->bpb_media
< 0xf8)
598 DEBUGF( "bpb_is_sane() - Warning: Non-standard "
599 "media type (0x%02x)\n",
602 if(fat_bpb
->last_word
!= 0xaa55)
604 DEBUGF( "bpb_is_sane() - Error: Last word is not "
605 "0xaa55 (0x%04x)\n", fat_bpb
->last_word
);
609 if (fat_bpb
->fsinfo
.freecount
>
610 (fat_bpb
->totalsectors
- fat_bpb
->firstdatasector
)/
611 fat_bpb
->bpb_secperclus
)
613 DEBUGF( "bpb_is_sane() - Error: FSInfo.Freecount > disk size "
614 "(0x%04lx)\n", fat_bpb
->fsinfo
.freecount
);
621 static void flush_fat_sector(struct fat_cache_entry
*fce
,
622 unsigned char *sectorbuf
)
627 /* With multivolume, use only the FAT info from the cached sector! */
628 #ifdef HAVE_MULTIVOLUME
629 secnum
= fce
->secnum
+ fce
->fat_vol
->startsector
;
631 secnum
= fce
->secnum
+ fat_bpbs
[0].startsector
;
634 /* Write to the first FAT */
635 rc
= storage_write_sectors(IF_MD2(fce
->fat_vol
->drive
,)
640 panicf("flush_fat_sector() - Could not write sector %ld"
644 #ifdef HAVE_MULTIVOLUME
645 if(fce
->fat_vol
->bpb_numfats
> 1)
647 if(fat_bpbs
[0].bpb_numfats
> 1)
650 /* Write to the second FAT */
651 #ifdef HAVE_MULTIVOLUME
652 secnum
+= fce
->fat_vol
->fatsize
;
654 secnum
+= fat_bpbs
[0].fatsize
;
656 rc
= storage_write_sectors(IF_MD2(fce
->fat_vol
->drive
,)
657 secnum
, 1, sectorbuf
);
660 panicf("flush_fat_sector() - Could not write sector %ld"
668 /* Note: The returned pointer is only safely valid until the next
669 task switch! (Any subsequent ata read/write may yield.) */
670 static void *cache_fat_sector(IF_MV2(struct bpb
* fat_bpb
,)
671 long fatsector
, bool dirty
)
673 #ifndef HAVE_MULTIVOLUME
674 struct bpb
* fat_bpb
= &fat_bpbs
[0];
676 long secnum
= fatsector
+ fat_bpb
->bpb_rsvdseccnt
;
677 int cache_index
= secnum
& FAT_CACHE_MASK
;
678 struct fat_cache_entry
*fce
= &fat_cache
[cache_index
];
679 unsigned char *sectorbuf
= &fat_cache_sectors
[cache_index
][0];
682 mutex_lock(&cache_mutex
); /* make changes atomic */
684 /* Delete the cache entry if it isn't the sector we want */
685 if(fce
->inuse
&& (fce
->secnum
!= secnum
686 #ifdef HAVE_MULTIVOLUME
687 || fce
->fat_vol
!= fat_bpb
691 /* Write back if it is dirty */
694 flush_fat_sector(fce
, sectorbuf
);
699 /* Load the sector if it is not cached */
702 rc
= storage_read_sectors(IF_MD2(fat_bpb
->drive
,)
703 secnum
+ fat_bpb
->startsector
,1,
707 DEBUGF( "cache_fat_sector() - Could not read sector %ld"
708 " (error %d)\n", secnum
, rc
);
709 mutex_unlock(&cache_mutex
);
713 fce
->secnum
= secnum
;
714 #ifdef HAVE_MULTIVOLUME
715 fce
->fat_vol
= fat_bpb
;
719 fce
->dirty
= true; /* dirt remains, sticky until flushed */
720 mutex_unlock(&cache_mutex
);
724 static unsigned long find_free_cluster(IF_MV2(struct bpb
* fat_bpb
,)
725 unsigned long startcluster
)
727 #ifndef HAVE_MULTIVOLUME
728 struct bpb
* fat_bpb
= &fat_bpbs
[0];
730 unsigned long sector
;
731 unsigned long offset
;
734 #ifdef HAVE_FAT16SUPPORT
735 if (fat_bpb
->is_fat16
)
737 sector
= startcluster
/ CLUSTERS_PER_FAT16_SECTOR
;
738 offset
= startcluster
% CLUSTERS_PER_FAT16_SECTOR
;
740 for (i
= 0; i
<fat_bpb
->fatsize
; i
++) {
742 unsigned int nr
= (i
+ sector
) % fat_bpb
->fatsize
;
743 unsigned short* fat
= cache_fat_sector(IF_MV2(fat_bpb
,) nr
, false);
746 for (j
= 0; j
< CLUSTERS_PER_FAT16_SECTOR
; j
++) {
747 int k
= (j
+ offset
) % CLUSTERS_PER_FAT16_SECTOR
;
748 if (letoh16(fat
[k
]) == 0x0000) {
749 unsigned int c
= nr
* CLUSTERS_PER_FAT16_SECTOR
+ k
;
750 /* Ignore the reserved clusters 0 & 1, and also
751 cluster numbers out of bounds */
752 if ( c
< 2 || c
> fat_bpb
->dataclusters
+1 )
754 LDEBUGF("find_free_cluster(%lx) == %x\n",startcluster
,c
);
755 fat_bpb
->fsinfo
.nextfree
= c
;
763 #endif /* #ifdef HAVE_FAT16SUPPORT */
765 sector
= startcluster
/ CLUSTERS_PER_FAT_SECTOR
;
766 offset
= startcluster
% CLUSTERS_PER_FAT_SECTOR
;
768 for (i
= 0; i
<fat_bpb
->fatsize
; i
++) {
770 unsigned long nr
= (i
+ sector
) % fat_bpb
->fatsize
;
771 unsigned long* fat
= cache_fat_sector(IF_MV2(fat_bpb
,) nr
, false);
774 for (j
= 0; j
< CLUSTERS_PER_FAT_SECTOR
; j
++) {
775 int k
= (j
+ offset
) % CLUSTERS_PER_FAT_SECTOR
;
776 if (!(letoh32(fat
[k
]) & 0x0fffffff)) {
777 unsigned long c
= nr
* CLUSTERS_PER_FAT_SECTOR
+ k
;
778 /* Ignore the reserved clusters 0 & 1, and also
779 cluster numbers out of bounds */
780 if ( c
< 2 || c
> fat_bpb
->dataclusters
+1 )
782 LDEBUGF("find_free_cluster(%lx) == %lx\n",startcluster
,c
);
783 fat_bpb
->fsinfo
.nextfree
= c
;
791 LDEBUGF("find_free_cluster(%lx) == 0\n",startcluster
);
792 return 0; /* 0 is an illegal cluster number */
795 static int update_fat_entry(IF_MV2(struct bpb
* fat_bpb
,) unsigned long entry
,
798 #ifndef HAVE_MULTIVOLUME
799 struct bpb
* fat_bpb
= &fat_bpbs
[0];
801 #ifdef HAVE_FAT16SUPPORT
802 if (fat_bpb
->is_fat16
)
804 int sector
= entry
/ CLUSTERS_PER_FAT16_SECTOR
;
805 int offset
= entry
% CLUSTERS_PER_FAT16_SECTOR
;
810 LDEBUGF("update_fat_entry(%lx,%lx)\n",entry
,val
);
813 panicf("Creating FAT loop: %lx,%lx\n",entry
,val
);
816 panicf("Updating reserved FAT entry %ld.\n",entry
);
818 sec
= cache_fat_sector(IF_MV2(fat_bpb
,) sector
, true);
821 DEBUGF( "update_fat_entry() - Could not cache sector %d\n", sector
);
826 if (letoh16(sec
[offset
]) == 0x0000 && fat_bpb
->fsinfo
.freecount
> 0)
827 fat_bpb
->fsinfo
.freecount
--;
830 if (letoh16(sec
[offset
]))
831 fat_bpb
->fsinfo
.freecount
++;
834 LDEBUGF("update_fat_entry: %lu free clusters\n",
835 fat_bpb
->fsinfo
.freecount
);
837 sec
[offset
] = htole16(val
);
840 #endif /* #ifdef HAVE_FAT16SUPPORT */
842 long sector
= entry
/ CLUSTERS_PER_FAT_SECTOR
;
843 int offset
= entry
% CLUSTERS_PER_FAT_SECTOR
;
846 LDEBUGF("update_fat_entry(%lx,%lx)\n",entry
,val
);
849 panicf("Creating FAT loop: %lx,%lx\n",entry
,val
);
852 panicf("Updating reserved FAT entry %ld.\n",entry
);
854 sec
= cache_fat_sector(IF_MV2(fat_bpb
,) sector
, true);
857 DEBUGF("update_fat_entry() - Could not cache sector %ld\n", sector
);
862 if (!(letoh32(sec
[offset
]) & 0x0fffffff) &&
863 fat_bpb
->fsinfo
.freecount
> 0)
864 fat_bpb
->fsinfo
.freecount
--;
867 if (letoh32(sec
[offset
]) & 0x0fffffff)
868 fat_bpb
->fsinfo
.freecount
++;
871 LDEBUGF("update_fat_entry: %ld free clusters\n",
872 fat_bpb
->fsinfo
.freecount
);
874 /* don't change top 4 bits */
875 sec
[offset
] &= htole32(0xf0000000);
876 sec
[offset
] |= htole32(val
& 0x0fffffff);
882 static long read_fat_entry(IF_MV2(struct bpb
* fat_bpb
,) unsigned long entry
)
884 #ifdef HAVE_FAT16SUPPORT
885 #ifndef HAVE_MULTIVOLUME
886 struct bpb
* fat_bpb
= &fat_bpbs
[0];
888 if (fat_bpb
->is_fat16
)
890 int sector
= entry
/ CLUSTERS_PER_FAT16_SECTOR
;
891 int offset
= entry
% CLUSTERS_PER_FAT16_SECTOR
;
894 sec
= cache_fat_sector(IF_MV2(fat_bpb
,) sector
, false);
897 DEBUGF( "read_fat_entry() - Could not cache sector %d\n", sector
);
901 return letoh16(sec
[offset
]);
904 #endif /* #ifdef HAVE_FAT16SUPPORT */
906 long sector
= entry
/ CLUSTERS_PER_FAT_SECTOR
;
907 int offset
= entry
% CLUSTERS_PER_FAT_SECTOR
;
910 sec
= cache_fat_sector(IF_MV2(fat_bpb
,) sector
, false);
913 DEBUGF( "read_fat_entry() - Could not cache sector %ld\n", sector
);
917 return letoh32(sec
[offset
]) & 0x0fffffff;
921 static long get_next_cluster(IF_MV2(struct bpb
* fat_bpb
,) long cluster
)
924 long eof_mark
= FAT_EOF_MARK
;
926 #ifdef HAVE_FAT16SUPPORT
927 #ifndef HAVE_MULTIVOLUME
928 struct bpb
* fat_bpb
= &fat_bpbs
[0];
930 if (fat_bpb
->is_fat16
)
932 eof_mark
&= 0xFFFF; /* only 16 bit */
933 if (cluster
< 0) /* FAT16 root dir */
934 return cluster
+ 1; /* don't use the FAT */
937 next_cluster
= read_fat_entry(IF_MV2(fat_bpb
,) cluster
);
939 /* is this last cluster in chain? */
940 if ( next_cluster
>= eof_mark
)
946 static int update_fsinfo(IF_MV_NONVOID(struct bpb
* fat_bpb
))
948 #ifndef HAVE_MULTIVOLUME
949 struct bpb
* fat_bpb
= &fat_bpbs
[0];
951 unsigned char fsinfo
[SECTOR_SIZE
];
952 unsigned long* intptr
;
955 #ifdef HAVE_FAT16SUPPORT
956 if (fat_bpb
->is_fat16
)
957 return 0; /* FAT16 has no FsInfo */
958 #endif /* #ifdef HAVE_FAT16SUPPORT */
961 rc
= storage_read_sectors(IF_MD2(fat_bpb
->drive
,)
962 fat_bpb
->startsector
+ fat_bpb
->bpb_fsinfo
, 1,fsinfo
);
965 DEBUGF( "flush_fat() - Couldn't read FSInfo (error code %d)\n", rc
);
968 intptr
= (long*)&(fsinfo
[FSINFO_FREECOUNT
]);
969 *intptr
= htole32(fat_bpb
->fsinfo
.freecount
);
971 intptr
= (long*)&(fsinfo
[FSINFO_NEXTFREE
]);
972 *intptr
= htole32(fat_bpb
->fsinfo
.nextfree
);
974 rc
= storage_write_sectors(IF_MD2(fat_bpb
->drive
,)
975 fat_bpb
->startsector
+ fat_bpb
->bpb_fsinfo
,1,fsinfo
);
978 DEBUGF( "flush_fat() - Couldn't write FSInfo (error code %d)\n", rc
);
985 static int flush_fat(IF_MV_NONVOID(struct bpb
* fat_bpb
))
990 LDEBUGF("flush_fat()\n");
992 mutex_lock(&cache_mutex
);
993 for(i
= 0;i
< FAT_CACHE_SIZE
;i
++)
995 struct fat_cache_entry
*fce
= &fat_cache
[i
];
997 #ifdef HAVE_MULTIVOLUME
998 && fce
->fat_vol
== fat_bpb
1002 sec
= fat_cache_sectors
[i
];
1003 flush_fat_sector(fce
, sec
);
1006 mutex_unlock(&cache_mutex
);
1008 rc
= update_fsinfo(IF_MV(fat_bpb
));
1015 static void fat_time(unsigned short* date
,
1016 unsigned short* time
,
1017 unsigned short* tenth
)
1020 struct tm
* tm
= get_time();
1023 *date
= ((tm
->tm_year
- 80) << 9) |
1024 ((tm
->tm_mon
+ 1) << 5) |
1028 *time
= (tm
->tm_hour
<< 11) |
1033 *tenth
= (tm
->tm_sec
& 1) * 100;
1035 /* non-RTC version returns an increment from the supplied time, or a
1036 * fixed standard time/date if no time given as input */
1038 /* Macros to convert a 2-digit string to a decimal constant.
1039 (YEAR), MONTH and DAY are set by the date command, which outputs
1040 DAY as 00..31 and MONTH as 01..12. The leading zero would lead to
1041 misinterpretation as an octal constant. */
1042 #define S100(x) 1 ## x
1043 #define C2DIG2DEC(x) (S100(x)-100)
1044 /* The actual build date, as FAT date constant */
1045 #define BUILD_DATE_FAT (((YEAR - 1980) << 9) \
1046 | (C2DIG2DEC(MONTH) << 5) \
1049 bool date_forced
= false;
1050 bool next_day
= false;
1051 unsigned time2
= 0; /* double time, for CRTTIME with 1s precision */
1053 if (date
&& *date
< BUILD_DATE_FAT
)
1055 *date
= BUILD_DATE_FAT
;
1062 if (time2
== 0 || date_forced
)
1064 time2
= (11 < 6) | 11; /* set to 00:11:11 */
1068 unsigned mins
= (time2
>> 6) & 0x3f;
1069 unsigned hours
= (time2
>> 12) & 0x1f;
1071 mins
= 11 * ((mins
/11) + 1); /* advance to next multiple of 11 */
1074 mins
= 11; /* 00 would be a bad marker */
1081 time2
= (hours
<< 12) | (mins
<< 6) | mins
; /* secs = mins */
1087 *tenth
= (time2
& 1) * 100;
1089 if (date
&& next_day
)
1091 static const unsigned char daysinmonth
[] =
1092 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
1093 unsigned day
= *date
& 0x1f;
1094 unsigned month
= (*date
>> 5) & 0x0f;
1095 unsigned year
= (*date
>> 9) & 0x7f;
1097 /* simplification: ignore leap years */
1098 if (++day
> daysinmonth
[month
-1])
1107 *date
= (year
<< 9) | (month
<< 5) | day
;
1110 #endif /* CONFIG_RTC */
1113 static int write_long_name(struct fat_file
* file
,
1114 unsigned int firstentry
,
1115 unsigned int numentries
,
1116 const unsigned char* name
,
1117 const unsigned char* shortname
,
1119 unsigned char *sector_buffer
)
1121 unsigned char* entry
;
1122 unsigned int idx
= firstentry
% DIR_ENTRIES_PER_SECTOR
;
1123 unsigned int sector
= firstentry
/ DIR_ENTRIES_PER_SECTOR
;
1124 unsigned char chksum
= 0;
1125 unsigned int i
, j
=0;
1126 unsigned int nameidx
=0, namelen
= utf8length(name
);
1128 unsigned short name_utf16
[namelen
+ 1];
1130 LDEBUGF("write_long_name(file:%lx, first:%d, num:%d, name:%s)\n",
1131 file
->firstcluster
, firstentry
, numentries
, name
);
1133 rc
= fat_seek(file
, sector
);
1137 rc
= fat_readwrite(file
, 1, sector_buffer
, false);
1141 /* calculate shortname checksum */
1142 for (i
=11; i
>0; i
--)
1143 chksum
= ((chksum
& 1) ? 0x80 : 0) + (chksum
>> 1) + shortname
[j
++];
1145 /* calc position of last name segment */
1146 if ( namelen
> NAME_BYTES_PER_ENTRY
)
1148 nameidx
< (namelen
- NAME_BYTES_PER_ENTRY
);
1149 nameidx
+= NAME_BYTES_PER_ENTRY
);
1151 /* we need to convert the name first */
1152 /* since it is written in reverse order */
1153 for (i
= 0; i
<= namelen
; i
++)
1154 name
= utf8decode(name
, &name_utf16
[i
]);
1156 for (i
=0; i
< numentries
; i
++) {
1158 if ( idx
>= DIR_ENTRIES_PER_SECTOR
) {
1159 /* update current sector */
1160 rc
= fat_seek(file
, sector
);
1164 rc
= fat_readwrite(file
, 1, sector_buffer
, true);
1168 /* read next sector */
1169 rc
= fat_readwrite(file
, 1, sector_buffer
, false);
1171 LDEBUGF("Failed writing new sector: %d\n",rc
);
1176 memset(sector_buffer
, 0, SECTOR_SIZE
);
1182 entry
= sector_buffer
+ idx
* DIR_ENTRY_SIZE
;
1184 /* verify this entry is free */
1185 if (entry
[0] && entry
[0] != 0xe5 )
1186 panicf("Dir entry %d in sector %x is not free! "
1187 "%02x %02x %02x %02x",
1189 entry
[0], entry
[1], entry
[2], entry
[3]);
1191 memset(entry
, 0, DIR_ENTRY_SIZE
);
1192 if ( i
+1 < numentries
) {
1193 /* longname entry */
1194 unsigned int k
, l
= nameidx
;
1196 entry
[FATLONG_ORDER
] = numentries
-i
-1;
1198 /* mark this as last long entry */
1199 entry
[FATLONG_ORDER
] |= FATLONG_LAST_LONG_ENTRY
;
1201 /* pad name with 0xffff */
1202 for (k
=1; k
<11; k
++) entry
[k
] = FAT_LONGNAME_PAD_BYTE
;
1203 for (k
=14; k
<26; k
++) entry
[k
] = FAT_LONGNAME_PAD_BYTE
;
1204 for (k
=28; k
<32; k
++) entry
[k
] = FAT_LONGNAME_PAD_BYTE
;
1207 for (k
=0; k
<5 && l
<= namelen
; k
++) {
1208 entry
[k
*2 + 1] = (unsigned char)(name_utf16
[l
] & 0xff);
1209 entry
[k
*2 + 2] = (unsigned char)(name_utf16
[l
++] >> 8);
1211 for (k
=0; k
<6 && l
<= namelen
; k
++) {
1212 entry
[k
*2 + 14] = (unsigned char)(name_utf16
[l
] & 0xff);
1213 entry
[k
*2 + 15] = (unsigned char)(name_utf16
[l
++] >> 8);
1215 for (k
=0; k
<2 && l
<= namelen
; k
++) {
1216 entry
[k
*2 + 28] = (unsigned char)(name_utf16
[l
] & 0xff);
1217 entry
[k
*2 + 29] = (unsigned char)(name_utf16
[l
++] >> 8);
1220 entry
[FATDIR_ATTR
] = FAT_ATTR_LONG_NAME
;
1221 entry
[FATDIR_FSTCLUSLO
] = 0;
1222 entry
[FATLONG_TYPE
] = 0;
1223 entry
[FATLONG_CHKSUM
] = chksum
;
1224 LDEBUGF("Longname entry %d: %s\n", idx
, name
+nameidx
);
1227 /* shortname entry */
1228 unsigned short date
=0, time
=0, tenth
=0;
1229 LDEBUGF("Shortname entry: %s\n", shortname
);
1230 memcpy(entry
+ FATDIR_NAME
, shortname
, 11);
1231 entry
[FATDIR_ATTR
] = is_directory
?FAT_ATTR_DIRECTORY
:0;
1232 entry
[FATDIR_NTRES
] = 0;
1234 fat_time(&date
, &time
, &tenth
);
1235 entry
[FATDIR_CRTTIMETENTH
] = tenth
;
1236 *(unsigned short*)(entry
+ FATDIR_CRTTIME
) = htole16(time
);
1237 *(unsigned short*)(entry
+ FATDIR_WRTTIME
) = htole16(time
);
1238 *(unsigned short*)(entry
+ FATDIR_CRTDATE
) = htole16(date
);
1239 *(unsigned short*)(entry
+ FATDIR_WRTDATE
) = htole16(date
);
1240 *(unsigned short*)(entry
+ FATDIR_LSTACCDATE
) = htole16(date
);
1243 nameidx
-= NAME_BYTES_PER_ENTRY
;
1246 /* update last sector */
1247 rc
= fat_seek(file
, sector
);
1251 rc
= fat_readwrite(file
, 1, sector_buffer
, true);
1258 static int fat_checkname(const unsigned char* newname
)
1260 static const char invalid_chars
[] = "\"*/:<>?\\|";
1261 int len
= strlen(newname
);
1262 /* More sanity checks are probably needed */
1263 if (len
> 255 || newname
[len
- 1] == '.')
1269 if (*newname
< ' ' || strchr(invalid_chars
, *newname
) != NULL
)
1273 /* check trailing space(s) */
1274 if(*(--newname
) == ' ')
1280 static int add_dir_entry(struct fat_dir
* dir
,
1281 struct fat_file
* file
,
1286 #ifdef HAVE_MULTIVOLUME
1287 struct bpb
* fat_bpb
= &fat_bpbs
[dir
->file
.volume
];
1289 struct bpb
* fat_bpb
= &fat_bpbs
[0];
1291 unsigned char buf
[SECTOR_SIZE
];
1292 unsigned char shortname
[12];
1294 unsigned int sector
;
1296 int entries_needed
, entries_found
= 0;
1299 LDEBUGF( "add_dir_entry(%s,%lx)\n",
1300 name
, file
->firstcluster
);
1302 /* Don't check dotdirs name for validity */
1303 if (dotdir
== false) {
1304 rc
= fat_checkname(name
);
1306 /* filename is invalid */
1311 #ifdef HAVE_MULTIVOLUME
1312 file
->volume
= dir
->file
.volume
; /* inherit the volume, to make sure */
1315 /* The "." and ".." directory entries must not be long names */
1318 strlcpy(shortname
, name
, 12);
1319 for(i
= strlen(shortname
); i
< 12; i
++)
1324 create_dos_name(name
, shortname
);
1326 /* one dir entry needed for every 13 bytes of filename,
1327 plus one entry for the short name */
1328 entries_needed
= (utf8length(name
) + (NAME_BYTES_PER_ENTRY
-1))
1329 / NAME_BYTES_PER_ENTRY
+ 1;
1335 rc
= fat_seek(&dir
->file
, 0);
1339 /* step 1: search for free entries and check for duplicate shortname */
1340 for (sector
= 0; !done
; sector
++)
1344 rc
= fat_readwrite(&dir
->file
, 1, buf
, false);
1346 DEBUGF( "add_dir_entry() - Couldn't read dir"
1347 " (error code %d)\n", rc
);
1351 if (rc
== 0) { /* current end of dir reached */
1352 LDEBUGF("End of dir on cluster boundary\n");
1356 /* look for free slots */
1357 for (i
= 0; i
< DIR_ENTRIES_PER_SECTOR
; i
++)
1359 switch (buf
[i
* DIR_ENTRY_SIZE
]) {
1361 entries_found
+= DIR_ENTRIES_PER_SECTOR
- i
;
1362 LDEBUGF("Found end of dir %d\n",
1363 sector
* DIR_ENTRIES_PER_SECTOR
+ i
);
1364 i
= DIR_ENTRIES_PER_SECTOR
- 1;
1370 LDEBUGF("Found free entry %d (%d/%d)\n",
1371 sector
* DIR_ENTRIES_PER_SECTOR
+ i
,
1372 entries_found
, entries_needed
);
1378 /* check that our intended shortname doesn't already exist */
1379 if (!strncmp(shortname
, buf
+ i
* DIR_ENTRY_SIZE
, 11)) {
1380 /* shortname exists already, make a new one */
1381 randomize_dos_name(shortname
);
1382 LDEBUGF("Duplicate shortname, changing to %s\n",
1385 /* name has changed, we need to restart search */
1390 if (firstentry
< 0 && (entries_found
>= entries_needed
))
1391 firstentry
= sector
* DIR_ENTRIES_PER_SECTOR
+ i
+ 1
1396 /* step 2: extend the dir if necessary */
1399 LDEBUGF("Adding new sector(s) to dir\n");
1400 rc
= fat_seek(&dir
->file
, sector
);
1403 memset(buf
, 0, sizeof buf
);
1405 /* we must clear whole clusters */
1406 for (; (entries_found
< entries_needed
) ||
1407 (dir
->file
.sectornum
< (int)fat_bpb
->bpb_secperclus
); sector
++)
1409 if (sector
>= (65536/DIR_ENTRIES_PER_SECTOR
))
1410 return -5; /* dir too large -- FAT specification */
1412 rc
= fat_readwrite(&dir
->file
, 1, buf
, true);
1413 if (rc
< 1) /* No more room or something went wrong */
1416 entries_found
+= DIR_ENTRIES_PER_SECTOR
;
1419 firstentry
= sector
* DIR_ENTRIES_PER_SECTOR
- entries_found
;
1422 /* step 3: add entry */
1423 sector
= firstentry
/ DIR_ENTRIES_PER_SECTOR
;
1424 LDEBUGF("Adding longname to entry %d in sector %d\n",
1425 firstentry
, sector
);
1427 rc
= write_long_name(&dir
->file
, firstentry
,
1428 entries_needed
, name
,
1429 shortname
, is_directory
, buf
);
1433 /* remember where the shortname dir entry is located */
1434 file
->direntry
= firstentry
+ entries_needed
- 1;
1435 file
->direntries
= entries_needed
;
1436 file
->dircluster
= dir
->file
.firstcluster
;
1437 LDEBUGF("Added new dir entry %d, using %d slots.\n",
1438 file
->direntry
, file
->direntries
);
1443 static unsigned char char2dos(unsigned char c
, int* randomize
)
1445 static const char invalid_chars
[] = "\"*+,./:;<=>?[\\]|";
1448 c
= 0; /* Illegal char, remove */
1449 else if (strchr(invalid_chars
, c
) != NULL
)
1451 /* Illegal char, replace */
1453 *randomize
= 1; /* as per FAT spec */
1461 static void create_dos_name(const unsigned char *name
, unsigned char *newname
)
1467 /* Find extension part */
1468 ext
= strrchr(name
, '.');
1469 if (ext
== name
) /* handle .dotnames */
1472 /* needs to randomize? */
1473 if((ext
&& (strlen(ext
) > 4)) ||
1474 ((ext
? (unsigned int)(ext
-name
) : strlen(name
)) > 8) )
1478 for (i
= 0; *name
&& (!ext
|| name
< ext
) && (i
< 8); name
++)
1480 unsigned char c
= char2dos(*name
, &randomize
);
1485 /* Pad both name and extension */
1489 if (newname
[0] == 0xe5) /* Special kanji character */
1493 { /* Extension part */
1495 for (i
= 8; *ext
&& (i
< 11); ext
++)
1497 unsigned char c
= char2dos(*ext
, &randomize
);
1504 randomize_dos_name(newname
);
1507 static void randomize_dos_name(unsigned char *name
)
1509 unsigned char* tilde
= NULL
; /* ~ location */
1510 unsigned char* lastpt
= NULL
; /* last point of filename */
1511 unsigned char* nameptr
= name
; /* working copy of name pointer */
1512 unsigned char num
[9]; /* holds number as string */
1520 /* hunt for ~ and where to put it */
1521 if((!tilde
) && (*nameptr
== '~'))
1523 if((!lastpt
) && ((*nameptr
== ' ' || *nameptr
== '~')))
1529 /* extract current count and increment */
1530 memcpy(num
,tilde
+1,7-(unsigned int)(tilde
-name
));
1531 num
[7-(unsigned int)(tilde
-name
)] = 0;
1532 cnt
= atoi(num
) + 1;
1534 cnt
%= 10000000; /* protection */
1535 snprintf(num
, 9, "~%d", cnt
); /* allow room for trailing zero */
1536 numlen
= strlen(num
); /* required space */
1537 offset
= (unsigned int)(lastpt
? lastpt
- name
: 8); /* prev startpoint */
1538 if(offset
> (8-numlen
)) offset
= 8-numlen
; /* correct for new numlen */
1540 memcpy(&name
[offset
], num
, numlen
);
1542 /* in special case of counter overflow: pad with spaces */
1543 for(offset
= offset
+numlen
; offset
< 8; offset
++)
1547 static int update_short_entry( struct fat_file
* file
, long size
, int attr
)
1549 unsigned char buf
[SECTOR_SIZE
];
1550 int sector
= file
->direntry
/ DIR_ENTRIES_PER_SECTOR
;
1551 unsigned char* entry
=
1552 buf
+ DIR_ENTRY_SIZE
* (file
->direntry
% DIR_ENTRIES_PER_SECTOR
);
1553 unsigned long* sizeptr
;
1554 unsigned short* clusptr
;
1555 struct fat_file dir
;
1558 LDEBUGF("update_file_size(cluster:%lx entry:%d size:%ld)\n",
1559 file
->firstcluster
, file
->direntry
, size
);
1561 /* create a temporary file handle for the dir holding this file */
1562 rc
= fat_open(IF_MV2(file
->volume
,) file
->dircluster
, &dir
, NULL
);
1566 rc
= fat_seek( &dir
, sector
);
1570 rc
= fat_readwrite(&dir
, 1, buf
, false);
1574 if (!entry
[0] || entry
[0] == 0xe5)
1575 panicf("Updating size on empty dir entry %d\n", file
->direntry
);
1577 entry
[FATDIR_ATTR
] = attr
& 0xFF;
1579 clusptr
= (short*)(entry
+ FATDIR_FSTCLUSHI
);
1580 *clusptr
= htole16(file
->firstcluster
>> 16);
1582 clusptr
= (short*)(entry
+ FATDIR_FSTCLUSLO
);
1583 *clusptr
= htole16(file
->firstcluster
& 0xffff);
1585 sizeptr
= (long*)(entry
+ FATDIR_FILESIZE
);
1586 *sizeptr
= htole32(size
);
1590 unsigned short time
= 0;
1591 unsigned short date
= 0;
1593 /* get old time to increment from */
1594 unsigned short time
= htole16(*(unsigned short*)(entry
+FATDIR_WRTTIME
));
1595 unsigned short date
= htole16(*(unsigned short*)(entry
+FATDIR_WRTDATE
));
1597 fat_time(&date
, &time
, NULL
);
1598 *(unsigned short*)(entry
+ FATDIR_WRTTIME
) = htole16(time
);
1599 *(unsigned short*)(entry
+ FATDIR_WRTDATE
) = htole16(date
);
1600 *(unsigned short*)(entry
+ FATDIR_LSTACCDATE
) = htole16(date
);
1603 rc
= fat_seek( &dir
, sector
);
1607 rc
= fat_readwrite(&dir
, 1, buf
, true);
1614 static int parse_direntry(struct fat_direntry
*de
, const unsigned char *buf
)
1620 memset(de
, 0, sizeof(struct fat_direntry
));
1621 de
->attr
= buf
[FATDIR_ATTR
];
1622 de
->crttimetenth
= buf
[FATDIR_CRTTIMETENTH
];
1623 de
->crtdate
= BYTES2INT16(buf
,FATDIR_CRTDATE
);
1624 de
->crttime
= BYTES2INT16(buf
,FATDIR_CRTTIME
);
1625 de
->wrtdate
= BYTES2INT16(buf
,FATDIR_WRTDATE
);
1626 de
->wrttime
= BYTES2INT16(buf
,FATDIR_WRTTIME
);
1627 de
->filesize
= BYTES2INT32(buf
,FATDIR_FILESIZE
);
1628 de
->firstcluster
= ((long)(unsigned)BYTES2INT16(buf
,FATDIR_FSTCLUSLO
)) |
1629 ((long)(unsigned)BYTES2INT16(buf
,FATDIR_FSTCLUSHI
) << 16);
1630 /* The double cast is to prevent a sign-extension to be done on CalmRISC16.
1631 (the result of the shift is always considered signed) */
1634 lowercase
= (buf
[FATDIR_NTRES
] & FAT_NTRES_LC_NAME
);
1635 c
= buf
[FATDIR_NAME
];
1636 if (c
== 0x05) /* special kanji char */
1640 de
->name
[j
++] = lowercase
? tolower(c
) : c
;
1643 c
= buf
[FATDIR_NAME
+i
];
1645 if (buf
[FATDIR_NAME
+8] != ' ') {
1646 lowercase
= (buf
[FATDIR_NTRES
] & FAT_NTRES_LC_EXT
);
1647 de
->name
[j
++] = '.';
1648 for (i
= 8; (i
< 11) && ((c
= buf
[FATDIR_NAME
+i
]) != ' '); i
++)
1649 de
->name
[j
++] = lowercase
? tolower(c
) : c
;
1654 int fat_open(IF_MV2(int volume
,)
1656 struct fat_file
*file
,
1657 const struct fat_dir
* dir
)
1659 /* Remember where the file's dir entry is located
1660 * Do it before assigning other fields so that fat_open
1661 * can be called with file == &dir->file (see fat_opendir) */
1663 file
->direntry
= dir
->entry
- 1;
1664 file
->direntries
= dir
->entrycount
;
1665 file
->dircluster
= dir
->file
.firstcluster
;
1668 file
->firstcluster
= startcluster
;
1669 file
->lastcluster
= startcluster
;
1670 file
->lastsector
= 0;
1671 file
->clusternum
= 0;
1672 file
->sectornum
= 0;
1674 #ifdef HAVE_MULTIVOLUME
1675 file
->volume
= volume
;
1676 /* fixme: remove error check when done */
1677 if (volume
>= NUM_VOLUMES
|| !fat_bpbs
[volume
].mounted
)
1679 LDEBUGF("fat_open() illegal volume %d\n", volume
);
1684 LDEBUGF("fat_open(%lx), entry %d\n",startcluster
,file
->direntry
);
1688 int fat_create_file(const char* name
,
1689 struct fat_file
* file
,
1690 struct fat_dir
* dir
)
1694 LDEBUGF("fat_create_file(\"%s\",%lx,%lx)\n",name
,(long)file
,(long)dir
);
1695 rc
= add_dir_entry(dir
, file
, name
, false, false);
1697 file
->firstcluster
= 0;
1698 file
->lastcluster
= 0;
1699 file
->lastsector
= 0;
1700 file
->clusternum
= 0;
1701 file
->sectornum
= 0;
1708 /* noinline because this is only split out of fat_create_dir to make sure
1709 * the sector buffer doesn't remain on the stack, to avoid nasty stack
1710 * overflows later on (when flush_fat() is called */
1711 static __attribute__((noinline
)) int fat_clear_cluster(int sector
,
1712 struct bpb
*fat_bpb
)
1714 unsigned char buf
[SECTOR_SIZE
];
1716 memset(buf
, 0, sizeof buf
);
1717 for(i
= 0;i
< (int)fat_bpb
->bpb_secperclus
;i
++) {
1718 rc
= transfer(IF_MV2(fat_bpb
,) sector
+ i
, 1, buf
, true );
1725 int fat_create_dir(const char* name
,
1726 struct fat_dir
* newdir
,
1727 struct fat_dir
* dir
)
1729 #ifdef HAVE_MULTIVOLUME
1730 struct bpb
* fat_bpb
= &fat_bpbs
[dir
->file
.volume
];
1732 struct bpb
* fat_bpb
= &fat_bpbs
[0];
1736 struct fat_file dummyfile
;
1738 LDEBUGF("fat_create_dir(\"%s\",%lx,%lx)\n",name
,(long)newdir
,(long)dir
);
1740 memset(newdir
, 0, sizeof(struct fat_dir
));
1741 memset(&dummyfile
, 0, sizeof(struct fat_file
));
1743 /* First, add the entry in the parent directory */
1744 rc
= add_dir_entry(dir
, &newdir
->file
, name
, true, false);
1748 /* Allocate a new cluster for the directory */
1749 newdir
->file
.firstcluster
= find_free_cluster(IF_MV2(fat_bpb
,)
1750 fat_bpb
->fsinfo
.nextfree
);
1751 if(newdir
->file
.firstcluster
== 0)
1754 update_fat_entry(IF_MV2(fat_bpb
,) newdir
->file
.firstcluster
, FAT_EOF_MARK
);
1756 /* Clear the entire cluster */
1757 sector
= cluster2sec(IF_MV2(fat_bpb
,) newdir
->file
.firstcluster
);
1758 rc
= fat_clear_cluster(sector
,fat_bpb
);
1763 /* Then add the "." entry */
1764 rc
= add_dir_entry(newdir
, &dummyfile
, ".", true, true);
1767 dummyfile
.firstcluster
= newdir
->file
.firstcluster
;
1768 update_short_entry(&dummyfile
, 0, FAT_ATTR_DIRECTORY
);
1770 /* and the ".." entry */
1771 rc
= add_dir_entry(newdir
, &dummyfile
, "..", true, true);
1775 /* The root cluster is cluster 0 in the ".." entry */
1776 if(dir
->file
.firstcluster
== fat_bpb
->bpb_rootclus
)
1777 dummyfile
.firstcluster
= 0;
1779 dummyfile
.firstcluster
= dir
->file
.firstcluster
;
1780 update_short_entry(&dummyfile
, 0, FAT_ATTR_DIRECTORY
);
1782 /* Set the firstcluster field in the direntry */
1783 update_short_entry(&newdir
->file
, 0, FAT_ATTR_DIRECTORY
);
1785 rc
= flush_fat(IF_MV(fat_bpb
));
1792 int fat_truncate(const struct fat_file
*file
)
1794 /* truncate trailing clusters */
1796 long last
= file
->lastcluster
;
1797 #ifdef HAVE_MULTIVOLUME
1798 struct bpb
* fat_bpb
= &fat_bpbs
[file
->volume
];
1801 LDEBUGF("fat_truncate(%lx, %lx)\n", file
->firstcluster
, last
);
1803 for ( last
= get_next_cluster(IF_MV2(fat_bpb
,) last
); last
; last
= next
) {
1804 next
= get_next_cluster(IF_MV2(fat_bpb
,) last
);
1805 update_fat_entry(IF_MV2(fat_bpb
,) last
,0);
1807 if (file
->lastcluster
)
1808 update_fat_entry(IF_MV2(fat_bpb
,) file
->lastcluster
,FAT_EOF_MARK
);
1813 int fat_closewrite(struct fat_file
*file
, long size
, int attr
)
1816 #ifdef HAVE_MULTIVOLUME
1817 struct bpb
* fat_bpb
= &fat_bpbs
[file
->volume
];
1819 LDEBUGF("fat_closewrite(size=%ld)\n",size
);
1823 if ( file
->firstcluster
) {
1824 update_fat_entry(IF_MV2(fat_bpb
,) file
->firstcluster
, 0);
1825 file
->firstcluster
= 0;
1829 if (file
->dircluster
) {
1830 rc
= update_short_entry(file
, size
, attr
);
1835 flush_fat(IF_MV(fat_bpb
));
1838 if ( file
->firstcluster
) {
1840 #ifdef HAVE_MULTIVOLUME
1841 struct bpb
* fat_bpb
= &fat_bpbs
[file
->volume
];
1843 struct bpb
* fat_bpb
= &fat_bpbs
[0];
1848 for ( next
= file
->firstcluster
; next
;
1849 next
= get_next_cluster(IF_MV2(fat_bpb
,) next
) ) {
1850 LDEBUGF("cluster %ld: %lx\n", count
, next
);
1853 len
= count
* fat_bpb
->bpb_secperclus
* SECTOR_SIZE
;
1854 LDEBUGF("File is %ld clusters (chainlen=%ld, size=%ld)\n",
1856 if ( len
> size
+ fat_bpb
->bpb_secperclus
* SECTOR_SIZE
)
1857 panicf("Cluster chain is too long\n");
1859 panicf("Cluster chain is too short\n");
1866 static int free_direntries(struct fat_file
* file
)
1868 unsigned char buf
[SECTOR_SIZE
];
1869 struct fat_file dir
;
1870 int numentries
= file
->direntries
;
1871 unsigned int entry
= file
->direntry
- numentries
+ 1;
1872 unsigned int sector
= entry
/ DIR_ENTRIES_PER_SECTOR
;
1876 /* create a temporary file handle for the dir holding this file */
1877 rc
= fat_open(IF_MV2(file
->volume
,) file
->dircluster
, &dir
, NULL
);
1881 rc
= fat_seek( &dir
, sector
);
1885 rc
= fat_readwrite(&dir
, 1, buf
, false);
1889 for (i
=0; i
< numentries
; i
++) {
1890 LDEBUGF("Clearing dir entry %d (%d/%d)\n",
1891 entry
, i
+1, numentries
);
1892 buf
[(entry
% DIR_ENTRIES_PER_SECTOR
) * DIR_ENTRY_SIZE
] = 0xe5;
1895 if ( (entry
% DIR_ENTRIES_PER_SECTOR
) == 0 ) {
1896 /* flush this sector */
1897 rc
= fat_seek(&dir
, sector
);
1901 rc
= fat_readwrite(&dir
, 1, buf
, true);
1905 if ( i
+1 < numentries
) {
1906 /* read next sector */
1907 rc
= fat_readwrite(&dir
, 1, buf
, false);
1915 if ( entry
% DIR_ENTRIES_PER_SECTOR
) {
1916 /* flush this sector */
1917 rc
= fat_seek(&dir
, sector
);
1921 rc
= fat_readwrite(&dir
, 1, buf
, true);
1929 int fat_remove(struct fat_file
* file
)
1931 long next
, last
= file
->firstcluster
;
1933 #ifdef HAVE_MULTIVOLUME
1934 struct bpb
* fat_bpb
= &fat_bpbs
[file
->volume
];
1937 LDEBUGF("fat_remove(%lx)\n",last
);
1940 next
= get_next_cluster(IF_MV2(fat_bpb
,) last
);
1941 update_fat_entry(IF_MV2(fat_bpb
,) last
,0);
1945 if ( file
->dircluster
) {
1946 rc
= free_direntries(file
);
1951 file
->firstcluster
= 0;
1952 file
->dircluster
= 0;
1954 rc
= flush_fat(IF_MV(fat_bpb
));
1961 int fat_rename(struct fat_file
* file
,
1962 struct fat_dir
* dir
,
1963 const unsigned char* newname
,
1968 struct fat_file olddir_file
;
1969 struct fat_file newfile
= *file
;
1970 unsigned char* entry
= NULL
;
1971 unsigned short* clusptr
= NULL
;
1972 unsigned int parentcluster
;
1973 #ifdef HAVE_MULTIVOLUME
1974 struct bpb
* fat_bpb
= &fat_bpbs
[file
->volume
];
1976 if (file
->volume
!= dir
->file
.volume
) {
1977 DEBUGF("No rename across volumes!\n");
1981 struct bpb
* fat_bpb
= &fat_bpbs
[0];
1984 if ( !file
->dircluster
) {
1985 DEBUGF("File has no dir cluster!\n");
1989 /* create new name */
1990 rc
= add_dir_entry(dir
, &newfile
, newname
, false, false);
1994 /* write size and cluster link */
1995 rc
= update_short_entry(&newfile
, size
, attr
);
1999 /* remove old name */
2000 rc
= free_direntries(file
);
2004 rc
= flush_fat(IF_MV(fat_bpb
));
2008 /* if renaming a directory, update the .. entry to make sure
2009 it points to its parent directory (we don't check if it was a move) */
2010 if(FAT_ATTR_DIRECTORY
== attr
) {
2011 unsigned char buf
[SECTOR_SIZE
];
2012 /* open the dir that was renamed, we re-use the olddir_file struct */
2013 rc
= fat_open(IF_MV2(file
->volume
,) newfile
.firstcluster
, &olddir_file
, NULL
);
2017 /* get the first sector of the dir */
2018 rc
= fat_seek(&olddir_file
, 0);
2022 rc
= fat_readwrite(&olddir_file
, 1, buf
, false);
2026 /* parent cluster is 0 if parent dir is the root - FAT spec (p.29) */
2027 if(dir
->file
.firstcluster
== fat_bpb
->bpb_rootclus
)
2030 parentcluster
= dir
->file
.firstcluster
;
2032 entry
= buf
+ DIR_ENTRY_SIZE
;
2033 if(strncmp(".. ", entry
, 11))
2035 /* .. entry must be second entry according to FAT spec (p.29) */
2036 DEBUGF("Second dir entry is not double-dot!\n");
2039 clusptr
= (short*)(entry
+ FATDIR_FSTCLUSHI
);
2040 *clusptr
= htole16(parentcluster
>> 16);
2042 clusptr
= (short*)(entry
+ FATDIR_FSTCLUSLO
);
2043 *clusptr
= htole16(parentcluster
& 0xffff);
2045 /* write back this sector */
2046 rc
= fat_seek(&olddir_file
, 0);
2050 rc
= fat_readwrite(&olddir_file
, 1, buf
, true);
2058 static long next_write_cluster(struct fat_file
* file
,
2062 #ifdef HAVE_MULTIVOLUME
2063 struct bpb
* fat_bpb
= &fat_bpbs
[file
->volume
];
2065 struct bpb
* fat_bpb
= &fat_bpbs
[0];
2070 LDEBUGF("next_write_cluster(%lx,%lx)\n",file
->firstcluster
, oldcluster
);
2073 cluster
= get_next_cluster(IF_MV2(fat_bpb
,) oldcluster
);
2077 cluster
= find_free_cluster(IF_MV2(fat_bpb
,) oldcluster
+1);
2078 else if (oldcluster
== 0)
2079 cluster
= find_free_cluster(IF_MV2(fat_bpb
,)
2080 fat_bpb
->fsinfo
.nextfree
);
2081 #ifdef HAVE_FAT16SUPPORT
2082 else /* negative, pseudo-cluster of the root dir */
2083 return 0; /* impossible to append something to the root */
2088 update_fat_entry(IF_MV2(fat_bpb
,) oldcluster
, cluster
);
2090 file
->firstcluster
= cluster
;
2091 update_fat_entry(IF_MV2(fat_bpb
,) cluster
, FAT_EOF_MARK
);
2095 if (fat_bpb
->fsinfo
.freecount
>0)
2096 panicf("There is free space, but find_free_cluster() "
2097 "didn't find it!\n");
2099 DEBUGF("next_write_cluster(): Disk full!\n");
2103 sector
= cluster2sec(IF_MV2(fat_bpb
,) cluster
);
2107 *newsector
= sector
;
2111 static int transfer(IF_MV2(struct bpb
* fat_bpb
,)
2112 unsigned long start
, long count
, char* buf
, bool write
)
2114 #ifndef HAVE_MULTIVOLUME
2115 struct bpb
* fat_bpb
= &fat_bpbs
[0];
2119 LDEBUGF("transfer(s=%lx, c=%lx, %s)\n",
2120 start
+ fat_bpb
->startsector
, count
, write
?"write":"read");
2122 unsigned long firstallowed
;
2123 #ifdef HAVE_FAT16SUPPORT
2124 if (fat_bpb
->is_fat16
)
2125 firstallowed
= fat_bpb
->rootdirsector
;
2128 firstallowed
= fat_bpb
->firstdatasector
;
2130 if (start
< firstallowed
)
2131 panicf("Write %ld before data\n", firstallowed
- start
);
2132 if (start
+ count
> fat_bpb
->totalsectors
)
2133 panicf("Write %ld after data\n",
2134 start
+ count
- fat_bpb
->totalsectors
);
2135 rc
= storage_write_sectors(IF_MD2(fat_bpb
->drive
,)
2136 start
+ fat_bpb
->startsector
, count
, buf
);
2139 rc
= storage_read_sectors(IF_MD2(fat_bpb
->drive
,)
2140 start
+ fat_bpb
->startsector
, count
, buf
);
2142 DEBUGF( "transfer() - Couldn't %s sector %lx"
2143 " (error code %d)\n",
2144 write
? "write":"read", start
, rc
);
2151 long fat_readwrite( struct fat_file
*file
, long sectorcount
,
2152 void* buf
, bool write
)
2154 #ifdef HAVE_MULTIVOLUME
2155 struct bpb
* fat_bpb
= &fat_bpbs
[file
->volume
];
2157 struct bpb
* fat_bpb
= &fat_bpbs
[0];
2159 long cluster
= file
->lastcluster
;
2160 long sector
= file
->lastsector
;
2161 long clusternum
= file
->clusternum
;
2162 long numsec
= file
->sectornum
;
2163 bool eof
= file
->eof
;
2164 long first
=0, last
=0;
2168 LDEBUGF( "fat_readwrite(file:%lx,count:0x%lx,buf:%lx,%s)\n",
2169 file
->firstcluster
,sectorcount
,(long)buf
,write
?"write":"read");
2170 LDEBUGF( "fat_readwrite: sec=%lx numsec=%ld eof=%d\n",
2171 sector
,numsec
, eof
?1:0);
2176 /* find sequential sectors and write them all at once */
2177 for (i
=0; (i
< sectorcount
) && (sector
> -1); i
++ ) {
2179 if ( numsec
> (long)fat_bpb
->bpb_secperclus
|| !cluster
) {
2180 long oldcluster
= cluster
;
2181 long oldsector
= sector
;
2182 long oldnumsec
= numsec
;
2184 cluster
= next_write_cluster(file
, cluster
, §or
);
2186 cluster
= get_next_cluster(IF_MV2(fat_bpb
,) cluster
);
2187 sector
= cluster2sec(IF_MV2(fat_bpb
,) cluster
);
2196 /* remember last cluster, in case
2197 we want to append to the file */
2199 cluster
= oldcluster
;
2202 i
= -1; /* Error code */
2213 /* look up first sector of file */
2214 sector
= cluster2sec(IF_MV2(fat_bpb
,) file
->firstcluster
);
2216 #ifdef HAVE_FAT16SUPPORT
2217 if (file
->firstcluster
< 0)
2218 { /* FAT16 root dir */
2219 sector
+= fat_bpb
->rootdiroffset
;
2220 numsec
+= fat_bpb
->rootdiroffset
;
2229 if ( ((sector
!= first
) && (sector
!= last
+1)) || /* not sequential */
2230 (last
-first
+1 == 256) ) { /* max 256 sectors per ata request */
2231 long count
= last
- first
+ 1;
2232 rc
= transfer(IF_MV2(fat_bpb
,) first
, count
, buf
, write
);
2236 buf
= (char *)buf
+ count
* SECTOR_SIZE
;
2240 if ((i
== sectorcount
-1) && /* last sector requested */
2243 long count
= sector
- first
+ 1;
2244 rc
= transfer(IF_MV2(fat_bpb
,) first
, count
, buf
, write
);
2252 file
->lastcluster
= cluster
;
2253 file
->lastsector
= sector
;
2254 file
->clusternum
= clusternum
;
2255 file
->sectornum
= numsec
;
2258 /* if eof, don't report last block as read/written */
2262 DEBUGF("Sectors written: %ld\n", i
);
2266 int fat_seek(struct fat_file
*file
, unsigned long seeksector
)
2268 #ifdef HAVE_MULTIVOLUME
2269 struct bpb
* fat_bpb
= &fat_bpbs
[file
->volume
];
2271 struct bpb
* fat_bpb
= &fat_bpbs
[0];
2273 long clusternum
=0, numclusters
=0, sectornum
=0, sector
=0;
2274 long cluster
= file
->firstcluster
;
2277 #ifdef HAVE_FAT16SUPPORT
2278 if (cluster
< 0) /* FAT16 root dir */
2279 seeksector
+= fat_bpb
->rootdiroffset
;
2284 /* we need to find the sector BEFORE the requested, since
2285 the file struct stores the last accessed sector */
2287 numclusters
= clusternum
= seeksector
/ fat_bpb
->bpb_secperclus
;
2288 sectornum
= seeksector
% fat_bpb
->bpb_secperclus
;
2290 if (file
->clusternum
&& clusternum
>= file
->clusternum
)
2292 cluster
= file
->lastcluster
;
2293 numclusters
-= file
->clusternum
;
2296 for (i
=0; i
<numclusters
; i
++) {
2297 cluster
= get_next_cluster(IF_MV2(fat_bpb
,) cluster
);
2299 DEBUGF("Seeking beyond the end of the file! "
2300 "(sector %ld, cluster %ld)\n", seeksector
, i
);
2305 sector
= cluster2sec(IF_MV2(fat_bpb
,) cluster
) + sectornum
;
2311 LDEBUGF("fat_seek(%lx, %lx) == %lx, %lx, %lx\n",
2312 file
->firstcluster
, seeksector
, cluster
, sector
, sectornum
);
2314 file
->lastcluster
= cluster
;
2315 file
->lastsector
= sector
;
2316 file
->clusternum
= clusternum
;
2317 file
->sectornum
= sectornum
+ 1;
2321 int fat_opendir(IF_MV2(int volume
,)
2322 struct fat_dir
*dir
, unsigned long startcluster
,
2323 const struct fat_dir
*parent_dir
)
2325 #ifdef HAVE_MULTIVOLUME
2326 struct bpb
* fat_bpb
= &fat_bpbs
[volume
];
2327 /* fixme: remove error check when done */
2328 if (volume
>= NUM_VOLUMES
|| !fat_bpbs
[volume
].mounted
)
2330 LDEBUGF("fat_open() illegal volume %d\n", volume
);
2334 struct bpb
* fat_bpb
= &fat_bpbs
[0];
2338 if (startcluster
== 0)
2339 startcluster
= fat_bpb
->bpb_rootclus
;
2341 rc
= fat_open(IF_MV2(volume
,) startcluster
, &dir
->file
, parent_dir
);
2344 DEBUGF( "fat_opendir() - Couldn't open dir"
2345 " (error code %d)\n", rc
);
2349 /* assign them after fat_open call so that fat_opendir can be called with the same
2350 * fat_dir as parent and result */
2357 int fat_getnext(struct fat_dir
*dir
, struct fat_direntry
*entry
)
2363 unsigned char firstbyte
;
2364 /* Long file names are stored in special entries. Each entry holds
2365 up to 13 characters. Names can be max 255 chars (not bytes!) long */
2366 /* The number of long entries in the long name can be retrieve from the first
2367 * long entry because there are stored in reverse order and have an ordinal */
2369 /* The long entries are expected to be in order, so remember the last ordinal */
2370 int last_long_ord
= 0;
2372 dir
->entrycount
= 0;
2376 if ( !(dir
->entry
% DIR_ENTRIES_PER_SECTOR
) || !dir
->sector
)
2378 rc
= fat_readwrite(&dir
->file
, 1, dir
->sectorcache
, false);
2385 DEBUGF( "fat_getnext() - Couldn't read dir"
2386 " (error code %d)\n", rc
);
2389 dir
->sector
= dir
->file
.lastsector
;
2392 for (i
= dir
->entry
% DIR_ENTRIES_PER_SECTOR
;
2393 i
< DIR_ENTRIES_PER_SECTOR
; i
++) {
2394 unsigned int entrypos
= i
* DIR_ENTRY_SIZE
;
2396 firstbyte
= dir
->sectorcache
[entrypos
];
2399 if (firstbyte
== 0xe5) {
2401 dir
->entrycount
= 0;
2405 if (firstbyte
== 0) {
2408 dir
->entrycount
= 0;
2415 if ( ( dir
->sectorcache
[entrypos
+ FATDIR_ATTR
] &
2416 FAT_ATTR_LONG_NAME_MASK
) == FAT_ATTR_LONG_NAME
) {
2417 /* extract ordinal */
2418 order
= dir
->sectorcache
[entrypos
+ FATLONG_ORDER
] & ~FATLONG_LAST_LONG_ENTRY
;
2419 /* is this entry the first long entry ? (first in order but containing last part) */
2420 if (dir
->sectorcache
[entrypos
+ FATLONG_ORDER
] & FATLONG_LAST_LONG_ENTRY
) {
2421 /* check that order is not too big ! (and non-zero) */
2422 if(order
<= 0 || order
> FATLONG_MAX_ORDER
)
2423 continue; /* ignore the whole LFN, will trigger lots of warnings */
2425 last_long_ord
= order
;
2428 /* check orphan entry */
2429 if (nb_longs
== 0) {
2430 logf("fat warning: orphan LFN entry");
2436 if (order
!= (last_long_ord
- 1)) {
2437 logf("fat warning: wrong LFN ordinal");
2438 /* ignore the whole LFN, will trigger lots of warnings */
2442 last_long_ord
= order
;
2445 /* copy part, reuse [order] for another purpose :) */
2446 order
= (order
- 1) * FATLONG_NAME_BYTES_PER_ENTRY
;
2447 for(j
= 0; j
< FATLONG_NAME_CHUNKS
; j
++) {
2448 memcpy(dir
->longname
+ order
,
2449 dir
->sectorcache
+ entrypos
+ FATLONG_NAME_POS
[j
],
2450 FATLONG_NAME_SIZE
[j
]);
2451 order
+= FATLONG_NAME_SIZE
[j
];
2455 if ( parse_direntry(entry
, dir
->sectorcache
+ entrypos
) ) {
2457 /* don't return volume id entry */
2459 (FAT_ATTR_VOLUME_ID
|FAT_ATTR_DIRECTORY
))
2460 == FAT_ATTR_VOLUME_ID
)
2463 /* replace shortname with longname? */
2464 /* check that the long name is complete */
2465 if (nb_longs
!= 0 && last_long_ord
== 1) {
2466 /* hold a copy of the shortname in case the long one is too long */
2467 unsigned char shortname
[13]; /* 8+3+dot+\0 */
2468 int longname_utf8len
= 0;
2469 /* One character at a time, add 1 for trailing \0, 4 is the maximum size
2470 * of a UTF8 encoded character in rockbox */
2471 unsigned char longname_utf8segm
[4 + 1];
2474 /* Temporarily store short name */
2475 strcpy(shortname
, entry
->name
);
2478 /* Convert the FAT name to a utf8-encoded one.
2479 * The name is not necessary NUL-terminated ! */
2480 for (j
= 0; j
< nb_longs
* FATLONG_NAME_BYTES_PER_ENTRY
; j
+= 2) {
2481 ucs
= dir
->longname
[j
] | (dir
->longname
[j
+ 1] << 8);
2482 if(ucs
== 0 || ucs
== FAT_LONGNAME_PAD_UCS
)
2484 /* utf8encode will return a pointer after the converted
2485 * string, subtract the pointer to the start to get the length of it */
2486 segm_utf8len
= utf8encode(ucs
, longname_utf8segm
) - longname_utf8segm
;
2488 /* warn the trailing zero ! (FAT_FILENAME_BYTES includes it) */
2489 if (longname_utf8len
+ segm_utf8len
>= FAT_FILENAME_BYTES
) {
2490 /* force use of short name */
2491 longname_utf8len
= FAT_FILENAME_BYTES
+ 1;
2492 break; /* fallback later */
2495 longname_utf8segm
[segm_utf8len
] = 0;
2496 strcat(entry
->name
+ longname_utf8len
, longname_utf8segm
);
2497 longname_utf8len
+= segm_utf8len
;
2501 /* Does the utf8-encoded name fit into the entry? */
2502 /* warn the trailing zero ! (FAT_FILENAME_BYTES includes it) */
2503 if (longname_utf8len
>= FAT_FILENAME_BYTES
) {
2504 /* Take the short DOS name. Need to utf8-encode it
2505 since it may contain chars from the upper half of
2506 the OEM code page which wouldn't be a valid utf8.
2507 Beware: this file will be shown with strange
2508 glyphs in file browser since unicode 0x80 to 0x9F
2509 are control characters. */
2510 logf("SN-DOS: %s", shortname
);
2511 unsigned char *utf8
;
2512 utf8
= iso_decode(shortname
, entry
->name
, -1,
2515 logf("SN: %s", entry
->name
);
2517 logf("LN: %s", entry
->name
);
2518 logf("LNLen: %d", longname_utf8len
);
2531 unsigned int fat_get_cluster_size(IF_MV_NONVOID(int volume
))
2533 #ifndef HAVE_MULTIVOLUME
2534 const int volume
= 0;
2536 struct bpb
* fat_bpb
= &fat_bpbs
[volume
];
2537 return fat_bpb
->bpb_secperclus
* SECTOR_SIZE
;
2540 #ifdef HAVE_MULTIVOLUME
2541 bool fat_ismounted(int volume
)
2543 return (volume
<NUM_VOLUMES
&& fat_bpbs
[volume
].mounted
);