1 /* lfn.c - Functions for handling VFAT long filenames */
3 /* Written 1998 by Roman Hodek */
12 __u8 id
; /* sequence number for slot */
13 __u8 name0_4
[10]; /* first 5 characters in name */
14 __u8 attr
; /* attribute byte */
15 __u8 reserved
; /* always 0 */
16 __u8 alias_checksum
; /* checksum for 8.3 alias */
17 __u8 name5_10
[12]; /* 6 more characters in name */
18 __u16 start
; /* starting cluster number, 0 in long slots */
19 __u8 name11_12
[4]; /* last 2 characters in name */
22 #define LFN_ID_START 0x40
23 #define LFN_ID_SLOTMASK 0x1f
25 #define CHARS_PER_LFN 13
27 /* These modul-global vars represent the state of the LFN parser */
28 unsigned char *lfn_unicode
= NULL
;
29 unsigned char lfn_checksum
;
31 loff_t
*lfn_offsets
= NULL
;
34 static unsigned char fat_uni2esc
[64] = {
35 '0', '1', '2', '3', '4', '5', '6', '7',
36 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
37 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
38 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
39 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
40 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
41 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
42 'u', 'v', 'w', 'x', 'y', 'z', '+', '-'
45 /* This defines which unicode chars are directly convertable to ISO-8859-1 */
46 #define UNICODE_CONVERTABLE(cl,ch) (ch == 0 && (cl < 0x80 || cl >= 0xa0))
48 /* for maxlen param */
49 #define UNTIL_0 INT_MAX
51 static void copy_lfn_part( char *dst
, LFN_ENT
*lfn
);
52 static char *cnv_unicode( const unsigned char *uni
, int maxlen
, int use_q
);
54 /* Convert name part in 'lfn' from unicode to ASCII */
55 static __inline
char* CNV_THIS_PART(LFN_ENT
*lfn
)
57 char __part_uni
[CHARS_PER_LFN
*2];
58 copy_lfn_part( __part_uni
, lfn
);
59 return cnv_unicode( (unsigned char*)__part_uni
, CHARS_PER_LFN
, 0 );
62 /* Convert name parts collected so far (from previous slots) from unicode to
64 #define CNV_PARTS_SO_FAR() \
65 (cnv_unicode( lfn_unicode+(lfn_slot*CHARS_PER_LFN*2), \
66 lfn_parts*CHARS_PER_LFN, 0 ))
68 /* This function converts an unicode string to a normal ASCII string, assuming
69 * ISO-8859-1 charset. Characters not in 8859-1 are converted to the same
70 * escape notation as used by the kernel, i.e. the uuencode-like ":xxx" */
71 static char *cnv_unicode( const unsigned char *uni
, int maxlen
, int use_q
)
73 const unsigned char *up
;
74 unsigned char *out
, *cp
;
77 for( len
= 0, up
= uni
; (up
-uni
)/2 < maxlen
&& (up
[0] || up
[1]); up
+= 2 ){
78 if (UNICODE_CONVERTABLE(up
[0],up
[1]))
83 cp
= out
= use_q
? qalloc( &FsCheckMemQueue
, len
+1 ) : vfalloc( len
+1 );
85 for( up
= uni
; (up
-uni
)/2 < maxlen
&& (up
[0] || up
[1]); up
+= 2 ) {
86 if (UNICODE_CONVERTABLE(up
[0],up
[1]))
89 /* here the same escape notation is used as in the Linux kernel */
91 val
= (up
[1] << 8) + up
[0];
92 cp
[2] = fat_uni2esc
[val
& 0x3f];
94 cp
[1] = fat_uni2esc
[val
& 0x3f];
96 cp
[0] = fat_uni2esc
[val
& 0x3f];
106 static void copy_lfn_part( char *dst
, LFN_ENT
*lfn
)
108 memcpy( dst
, lfn
->name0_4
, 10 );
109 memcpy( dst
+10, lfn
->name5_10
, 12 );
110 memcpy( dst
+22, lfn
->name11_12
, 4 );
114 static void clear_lfn_slots( int start
, int end
)
119 /* New dir entry is zeroed except first byte, which is set to 0xe5.
120 * This is to avoid that some FAT-reading OSes (not Linux! ;) stop reading
121 * a directory at the first zero entry...
123 memset( &empty
, 0, sizeof(empty
) );
124 empty
.id
= DELETED_FLAG
;
126 for( i
= start
; i
<= end
; ++i
) {
127 fs_write( lfn_offsets
[i
], sizeof(LFN_ENT
), &empty
);
131 void lfn_reset( void )
134 vffree( lfn_unicode
);
137 vffree( lfn_offsets
);
143 /* This function is only called with de->attr == VFAT_LN_ATTR. It stores part
144 * of the long name. */
145 void lfn_add_slot( DIR_ENT
*de
, loff_t dir_offset
)
147 LFN_ENT
*lfn
= (LFN_ENT
*)de
;
150 if (de
->attr
!= VFAT_LN_ATTR
)
151 die("lfn_add_slot called with non-LFN directory entry");
153 if (lfn
->id
& LFN_ID_START
) {
154 if (lfn_slot
!= -1) {
156 /* There is already a LFN "in progess", so it is an error that a
157 * new start entry is here. */
158 /* Causes: 1) if slot# == expected: start bit set mysteriously, 2)
159 * old LFN overwritten by new one */
160 /* Fixes: 1) delete previous LFN 2) if slot# == expected and
161 * checksum ok: clear start bit */
162 /* XXX: Should delay that until next LFN known (then can better
163 * display the name) */
164 VfatPrint( "A new long file name starts within an old one.\n" );
165 if ((lfn
->id
& LFN_ID_SLOTMASK
) == lfn_slot
&&
166 lfn
->alias_checksum
== lfn_checksum
) {
167 char *part1
= CNV_THIS_PART(lfn
);
168 char *part2
= CNV_PARTS_SO_FAR();
169 VfatPrint( " It could be that the LFN start bit is wrong here\n"
170 " if \"%s\" seems to match \"%s\".\n", part1
, part2
);
175 if (FsCheckFlags
& FSCHECK_INTERACTIVE
) {
176 VfatPrint( "1: Delete previous LFN\n2: Leave it as it is.\n" );
178 VfatPrint( "3: Clear start bit and concatenate LFNs\n" );
180 else VfatPrint( " Not auto-correcting this.\n" );
181 if (FsCheckFlags
& FSCHECK_INTERACTIVE
) {
182 switch( get_key( can_clear
? "123" : "12", "?" )) {
184 clear_lfn_slots( 0, lfn_parts
-1 );
190 lfn
->id
&= ~LFN_ID_START
;
191 fs_write( dir_offset
+offsetof(LFN_ENT
,id
),
192 sizeof(lfn
->id
), &lfn
->id
);
197 lfn_slot
= lfn
->id
& LFN_ID_SLOTMASK
;
198 lfn_checksum
= lfn
->alias_checksum
;
199 lfn_unicode
= vfalloc( (lfn_slot
*CHARS_PER_LFN
+1)*2 );
200 lfn_offsets
= vfalloc( lfn_slot
*sizeof(loff_t
) );
203 else if (lfn_slot
== -1) {
204 /* No LFN in progress, but slot found; start bit missing */
205 /* Causes: 1) start bit got lost, 2) Previous slot with start bit got
207 /* Fixes: 1) delete LFN, 2) set start bit */
208 char *part
= CNV_THIS_PART(lfn
);
209 VfatPrint( "Long filename fragment \"%s\" found outside a LFN "
210 "sequence.\n (Maybe the start bit is missing on the "
211 "last fragment)\n", part
);
212 if (FsCheckFlags
& FSCHECK_INTERACTIVE
) {
213 VfatPrint( "1: Delete fragment\n2: Leave it as it is.\n"
214 "3: Set start bit\n" );
216 else VfatPrint( " Not auto-correcting this.\n" );
217 if (FsCheckFlags
& FSCHECK_INTERACTIVE
) {
218 switch( get_key( "123", "?" )) {
221 lfn_offsets
= vfalloc( sizeof(loff_t
) );
222 lfn_offsets
[0] = dir_offset
;
223 clear_lfn_slots( 0, 0 );
230 lfn
->id
|= LFN_ID_START
;
231 fs_write( dir_offset
+offsetof(LFN_ENT
,id
),
232 sizeof(lfn
->id
), &lfn
->id
);
233 lfn_slot
= lfn
->id
& LFN_ID_SLOTMASK
;
234 lfn_checksum
= lfn
->alias_checksum
;
235 lfn_unicode
= vfalloc( (lfn_slot
*CHARS_PER_LFN
+1)*2 );
236 lfn_offsets
= vfalloc( lfn_slot
*sizeof(loff_t
) );
242 else if ((lfn
->id
& LFN_ID_SLOTMASK
) != lfn_slot
) {
243 /* wrong sequence number */
244 /* Causes: 1) seq-no destroyed */
245 /* Fixes: 1) delete LFN, 2) fix number (maybe only if following parts
246 * are ok?, maybe only if checksum is ok?) (Attention: space
247 * for name was allocated before!) */
249 VfatPrint( "Unexpected long filename sequence number "
250 "(%d vs. expected %d).\n",
251 (lfn
->id
& LFN_ID_SLOTMASK
), lfn_slot
);
252 if (lfn
->alias_checksum
== lfn_checksum
) {
253 char *part1
= CNV_THIS_PART(lfn
);
254 char *part2
= CNV_PARTS_SO_FAR();
255 VfatPrint( " It could be that just the number is wrong\n"
256 " if \"%s\" seems to match \"%s\".\n", part1
, part2
);
261 if (FsCheckFlags
& FSCHECK_INTERACTIVE
) {
262 VfatPrint( "1: Delete LFN\n2: Leave it as it is (and ignore LFN so far)\n" );
264 VfatPrint( "3: Correct sequence number\n" );
266 else VfatPrint( " Not auto-correcting this.\n" );
267 if (FsCheckFlags
& FSCHECK_INTERACTIVE
) {
268 switch( get_key( can_fix
? "123" : "12", "?" )) {
270 lfn_offsets
[lfn_parts
++] = dir_offset
;
271 clear_lfn_slots( 0, lfn_parts
-1 );
278 lfn
->id
= (lfn
->id
& ~LFN_ID_SLOTMASK
) | lfn_slot
;
279 fs_write( dir_offset
+offsetof(LFN_ENT
,id
),
280 sizeof(lfn
->id
), &lfn
->id
);
286 if (lfn
->alias_checksum
!= lfn_checksum
) {
287 /* checksum mismatch */
288 /* Causes: 1) checksum field here destroyed */
289 /* Fixes: 1) delete LFN, 2) fix checksum */
290 VfatPrint( "Checksum in long filename part wrong "
291 "(%02x vs. expected %02x).\n",
292 lfn
->alias_checksum
, lfn_checksum
);
293 if (FsCheckFlags
& FSCHECK_INTERACTIVE
) {
294 VfatPrint( "1: Delete LFN\n2: Leave it as it is.\n"
295 "3: Correct checksum\n" );
297 else VfatPrint( " Not auto-correcting this.\n" );
298 if (FsCheckFlags
& FSCHECK_INTERACTIVE
) {
299 switch( get_key( "123", "?" )) {
301 lfn_offsets
[lfn_parts
++] = dir_offset
;
302 clear_lfn_slots( 0, lfn_parts
-1 );
308 lfn
->alias_checksum
= lfn_checksum
;
309 fs_write( dir_offset
+offsetof(LFN_ENT
,alias_checksum
),
310 sizeof(lfn
->alias_checksum
), &lfn
->alias_checksum
);
316 if (lfn_slot
!= -1) {
318 offset
= lfn_slot
* CHARS_PER_LFN
*2;
319 copy_lfn_part( (char*)lfn_unicode
+offset
, lfn
);
320 if (lfn
->id
& LFN_ID_START
)
321 lfn_unicode
[offset
+26] = lfn_unicode
[offset
+27] = 0;
322 lfn_offsets
[lfn_parts
++] = dir_offset
;
325 if (lfn
->reserved
!= 0) {
326 VfatPrint( "Reserved field in VFAT long filename slot is not 0 "
327 "(but 0x%02x).\n", lfn
->reserved
);
328 if (FsCheckFlags
& FSCHECK_INTERACTIVE
)
329 VfatPrint( "1: Fix.\n2: Leave it.\n" );
330 else VfatPrint( "Auto-setting to 0.\n" );
331 if (!(FsCheckFlags
& FSCHECK_INTERACTIVE
) || get_key("12","?") == '1') {
333 fs_write( dir_offset
+offsetof(LFN_ENT
,reserved
),
334 sizeof(lfn
->reserved
), &lfn
->reserved
);
337 if (lfn
->start
!= CT_LE_W(0)) {
338 VfatPrint( "Start cluster field in VFAT long filename slot is not 0 "
339 "(but 0x%04x).\n", lfn
->start
);
340 if (FsCheckFlags
& FSCHECK_INTERACTIVE
)
341 VfatPrint( "1: Fix.\n2: Leave it.\n" );
342 else VfatPrint( "Auto-setting to 0.\n" );
343 if (!(FsCheckFlags
& FSCHECK_INTERACTIVE
) || get_key("12","?") == '1') {
344 lfn
->start
= CT_LE_W(0);
345 fs_write( dir_offset
+offsetof(LFN_ENT
,start
),
346 sizeof(lfn
->start
),&lfn
->start
);
352 /* This function is always called when de->attr != VFAT_LN_ATTR is found, to
353 * retrieve the previously constructed LFN. */
354 char *lfn_get( DIR_ENT
*de
)
360 if (de
->attr
== VFAT_LN_ATTR
)
361 die("lfn_get called with LFN directory entry");
365 VfatPrint( "lcase=%02x\n",de
->lcase
);
369 /* no long name for this file */
373 /* The long name isn't finished yet. */
374 /* Causes: 1) LFN slot overwritten by non-VFAT aware tool */
375 /* Fixes: 1) delete LFN 2) move overwriting entry to somewhere else
376 * and let user enter missing part of LFN (hard to do :-()
377 * 3) renumber entries and truncate name */
378 char *long_name
= CNV_PARTS_SO_FAR();
379 char *short_name
= file_name(de
->name
);
380 VfatPrint( "Unfinished long file name \"%s\".\n"
381 " (Start may have been overwritten by %s)\n",
382 long_name
, short_name
);
384 if (FsCheckFlags
& FSCHECK_INTERACTIVE
) {
385 VfatPrint( "1: Delete LFN\n2: Leave it as it is.\n"
386 "3: Fix numbering (truncates long name and attaches "
387 "it to short name %s)\n", short_name
);
389 else VfatPrint( " Not auto-correcting this.\n" );
390 if (FsCheckFlags
& FSCHECK_INTERACTIVE
) {
391 switch( get_key( "123", "?" )) {
393 clear_lfn_slots( 0, lfn_parts
-1 );
400 for( i
= 0; i
< lfn_parts
; ++i
) {
401 __u8 id
= (lfn_parts
-i
) | (i
==0 ? LFN_ID_START
: 0);
402 fs_write( lfn_offsets
[i
]+offsetof(LFN_ENT
,id
),
405 memmove( lfn_unicode
, lfn_unicode
+lfn_slot
*CHARS_PER_LFN
*2,
406 lfn_parts
*CHARS_PER_LFN
*2 );
412 for (sum
= 0, i
= 0; i
< 8; i
++)
413 sum
= (((sum
&1) << 7) | ((sum
&0xfe) >> 1)) + de
->name
[i
];
415 for (i
= 0; i
< 3; i
++)
416 sum
= (((sum
&1) << 7) | ((sum
&0xfe) >> 1)) + de
->ext
[i
];
418 if (sum
!= lfn_checksum
) {
419 /* checksum doesn't match, long name doesn't apply to this alias */
420 /* Causes: 1) alias renamed */
421 /* Fixes: 1) Fix checksum in LFN entries */
422 char *long_name
= CNV_PARTS_SO_FAR();
423 char *short_name
= file_name(de
->name
);
424 VfatPrint( "Wrong checksum for long file name \"%s\".\n"
425 " (Short name %s may have changed without updating the long name)\n",
426 long_name
, short_name
);
428 if (FsCheckFlags
& FSCHECK_INTERACTIVE
) {
429 VfatPrint( "1: Delete LFN\n2: Leave it as it is.\n"
430 "3: Fix checksum (attaches to short name %s)\n",
433 else VfatPrint( " Not auto-correcting this.\n" );
434 if (FsCheckFlags
& FSCHECK_INTERACTIVE
) {
435 switch( get_key( "123", "?" )) {
437 clear_lfn_slots( 0, lfn_parts
-1 );
444 for( i
= 0; i
< lfn_parts
; ++i
) {
445 fs_write( lfn_offsets
[i
]+offsetof(LFN_ENT
,alias_checksum
),
453 lfn
= cnv_unicode( lfn_unicode
, UNTIL_0
, 1 );
458 void lfn_check_orphaned(void)
465 long_name
= CNV_PARTS_SO_FAR();
466 VfatPrint("Orphaned long file name part \"%s\"\n", long_name
);
467 if (FsCheckFlags
& FSCHECK_INTERACTIVE
)
468 VfatPrint( "1: Delete.\n2: Leave it.\n" );
469 else VfatPrint( " Auto-deleting.\n" );
470 if (!(FsCheckFlags
& FSCHECK_INTERACTIVE
) || get_key("12","?") == '1') {
471 clear_lfn_slots(0, lfn_parts
- 1);
476 /* Local Variables: */