summaryrefslogtreecommitdiff
path: root/Core/EM/FileSystem/DirectoryHandler.c
blob: 9acd1d2f26481437791478e380b46548c7763c31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2009, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**         5555 Oakbrook Pkwy, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************

//**********************************************************************
// $Header: /Alaska/SOURCE/Core/Modules/FileSystem/DirectoryHandler.c 6     7/07/10 2:54p Pats $
//
// $Revision: 6 $
//
// $Date: 7/07/10 2:54p $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Core/Modules/FileSystem/DirectoryHandler.c $
// 
// 6     7/07/10 2:54p Pats
// EIP 38291: Fails Klocwork test.
// Problem: No error return if short name is null in GenUniqueShortName()
// Solution: Added error return.
// 
// 5     7/02/09 5:47p Pats
// Updated to latest coding standard. No code changes.
//
// 4     4/13/07 7:07p Pats
// Edited to conform with coding standards. No code changes.
//
// 3     11/03/05 2:16p Srinin
// Fixed VC7.1 warning msg.
//
// 2     9/08/05 4:41p Pats
// Fixed problem of short filename having exactly 8 characters in mame and
// 3 in ext being handled as long file name (and ~ name generated).
//
// 1     4/26/05 6:05p Srinin
//
//
//**********************************************************************

//<AMI_FHDR_START>
//----------------------------------------------------------------------
//
// Name:    DirectoryHandler.c
//
// Description: Fat File System driver -- Directory Functions
//
//----------------------------------------------------------------------
//<AMI_FHDR_END>

//----------------------------------------------------------------------

#include "FileSystem.h"

//----------------------------------------------------------------------


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:       GetNameComponent
//
// Description:     Gets the name component from a path
//
// Parameters:      CHAR16 *Path - Pointer to input path
//                  CHAR16 *Name - Pointer to output name
//
// Return value:    CHAR16 * path truncated to name
//
// Modified:        None
//
// Referral(s):     None
//
// NOTE(S):
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

CHAR16 *
GetNameComponent (
    IN CHAR16  *Path,
    OUT CHAR16 *Name
)
{
    UINTN Len = 0;
    
    while (*Path && *Path != '\\') {
        if (Len < 259) {
            Name[Len] = *Path;
            Len += 1;
        }
        
        Path += 1;
    }
    
    Name[Len] = 0;
    
// Remove trailing '\'
    while (*Path == '\\') {
        Path += 1;
    }
    
    return Path;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:       GenShortFilename
//
// Description:     Generates a DOS 8.3 name component from a path
//
// Parameters:      VOLUME_INFO *Vol - Pointer to volume information structure
//                  CHAR16 *InPath - Pointer to input path
//                  CHAR8 *ShortName - Pointer to generated DOS 8.3 name
//
// Return value:    EFI_STATUS - Status of operation (EFI_SUCCESS)
//
// Modified:        None
//
// Referral(s):     None
//
// NOTE(S):
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
GenShortFilename (
    IN VOLUME_INFO *Vol,
    IN CHAR16      *InPath,
    OUT CHAR8      *ShortName
)
{
    CHAR16 *Ext;
    CHAR16 *p;
    CHAR16 CharC;
    
    MemSet(ShortName, 11, 0x20); // Fill short name with spaces
    
    // If this is a '.' or '..' name then it's a special form
    if (InPath[0] == '.' && InPath[1] == 0) {
        ShortName[0] = '.';
        goto Done;
    }
    
    if (InPath[0] == '.' && InPath[1] == '.' && InPath[2] == 0) {
        ShortName[0] = '.';
        ShortName[1] = '.';
        goto Done;
    }
    
// Find the last '.'
    Ext = NULL;
    
    for (p = InPath; *p; p++) {
        if (*p == '.') {
            Ext = p;
        }
    }
    
    if (!Ext) {
        Ext = p;
    }
    
    // Create 8.3 name.  Convert chars to fat values.  Skip any '.' or ' '.
    CharC = *Ext; // Save first char of ext.
    *Ext = 0;  // Zero-terminate name
    Vol->UnicodeCollationInterface->StrToFat (Vol->UnicodeCollationInterface,
            InPath,
            8,
            ShortName);
    *Ext = CharC; // Restore first char of ext.
    Vol->UnicodeCollationInterface->StrToFat (Vol->UnicodeCollationInterface,
            Ext,
            3,
            ShortName+8);
            
Done:
    return EFI_SUCCESS;
    
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:       GenUniqueShortFname
//
// Description:     Generates a unique DOS 8.3 name from a possible long name
//
// Parameters:      FILE_HANDLE *fh - Pointer to the file handle structure
//                  CHAR16 *LongName - Pointer to the possible long name
//                  CHAR8 *ShortName - Pointer to generated DOS 8.3 name
//                  BOOLEAN *LfnNeeded - Indicates a long name entry is
//                                       needed for this input name
//
// Return value:    EFI_STATUS - EFI_SUCCESS - Unique name generated
//                               EFI_ACCESS_DENIED - this name already exists
//                               on the volume
//
// Modified:        None
//
// Referral(s):     None
//
// NOTE(S):
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
GenUniqueShortFname (
    IN FILE_HANDLE *fh,
    IN CHAR16      *LongName,
    OUT CHAR8      *ShortName,
    OUT BOOLEAN    *LfnNeeded
)
{

    EFI_STATUS     Status;
    VOLUME_INFO    *Vol = fh->VolumeInterface;
    DIR_ENTRY_LIST *Del;
    CHAR8          Temp[12];
    CHAR16         Temp2[13];
    CHAR16         Temp3[13];
    CHAR8          CounterString[6];
    UINTN          Counter, CounterStringLen;
    UINTN          k;
    CHAR8          *s;
    
    Status = GenShortFilename(Vol, LongName, ShortName);
    
    if (EFI_ERROR(Status)) return Status;
    
    // Check for dot or dotdot entries
    if ((LongName[0] == '.' && LongName[1] == 0) ||
            (LongName[0] == '.' && LongName[1] == '.' && LongName[2] == 0)) {
        *LfnNeeded = FALSE;
        return EFI_SUCCESS;
    }
    
    if (!fh->DirList.pHead) {
        ReadAllDirectoryEntries (fh);
    }
    
    // Find out if the input name is already a valid short name -- i.e., no
    // ~Counter is needed.
    k = Wcslen(LongName) * sizeof(CHAR16) + 2;      // Include Null character
    
    if (k <= 26) {  // If it could be a legal short name... 
                    // (13 * 2 including '.' + 2 for NULL)
        ExtractShortFileName(Vol, Temp2, ShortName);
        pBS->CopyMem(Temp3, LongName, k);
        // Make an upper case copy of the input
        Vol->UnicodeCollationInterface->StrUpr(Vol->UnicodeCollationInterface, Temp3);
        
        if ( !Wcscmp(Temp2, Temp3 )) {
            // If the extracted name is same as upper case copy of input...
            // If the extracted name doesn't equal the non-upper case original,
            // it must have some lower case letters in it, 
            // so therefore a long name entry is needed.
            if (!Wcscmp(Temp2, LongName)) *LfnNeeded = FALSE;
            
            else *LfnNeeded = TRUE;
            
            if (FindMatchingDirEntry (fh, LongName, &Del)) {
                return EFI_ACCESS_DENIED; // Name already exists, can't create
                
            } else {
                return EFI_SUCCESS;  // Name is ok, return
            }
        }
    }
    
    // Now append "~Counter" to the short name, incrementing "Counter"
    // until the file name is unique in that Path
    Zeromemory (CounterString, sizeof(CounterString));
    
    for (Counter = 1; Counter < 65536; Counter++) {
        pBS->CopyMem(Temp, ShortName, 11);
        if (Temp[0] == ' ') return EFI_INVALID_PARAMETER;
        ItoaEx(Counter, CounterString, 10, FALSE);
        CounterStringLen = Strlen(CounterString);
        
        // Right-justify the "~Counter"
        for (k = 0; k < 7 - CounterStringLen; k += 1) {
            if (Temp[k] == ' ') {
                k--;    // In case the name is short
                break;
            }
        }
        
        // Append the "~Counter" to the name
        Temp[k++] = '~';
        
        for (s = CounterString; *s; Temp[k++] = *s++) ;
        
        // Search for the generated name
        Temp[11] = 0;
        
        if (!FindMatchingSFNDirEntry (fh, Temp, &Del)) {
            pBS->CopyMem(ShortName, Temp, 11);
            *LfnNeeded = TRUE;
            return EFI_SUCCESS;
        }
    }
    
    return EFI_ACCESS_DENIED;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:       CopyCharsToLfnSlot
//
// Description:     Copies a character to the appropriate slot of a long name
//                  directory structure
//
// Parameters:      DIR_ENTRY_LFN *Slot - Pointer to long name directory
//                                        structure
//                  UINT32 *SlotPos - Pointer to slot position to fill
//                  UINT16 Ch - The character to be placed in the slot
//
// Return value:    None
//
// Modified:        None
//
// Referral(s):     None
//
// NOTE(S):
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

static VOID
CopyCharsToLfnSlot (
    DIR_ENTRY_LFN *Slot,
    UINT32        SlotPos,
    UINT16        Ch
)
{
    if ((SlotPos >= 0)  && (SlotPos <= 4)) Slot->Dir_Name0_4[SlotPos] = Ch;
    
    else if ((SlotPos >= 5)  && (SlotPos <= 10)) Slot->Dir_Name5_10 [SlotPos-5]  = Ch;
    
    else if ((SlotPos >= 11) && (SlotPos <= 12)) Slot->Dir_Name11_12[SlotPos-11] = Ch;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:       SplitLFN
//
// Description:     Splits a long file name into long name directory enties.
//
// Parameters:      CHAR16 *LongName - Pointer to a long file name
//                  DIR_ENTRY_32 *ShortEntry - Pointer to the corresponding
//                                             short directory entry
//                  DIR_ENTRY_LFN *Slot - The structure of the long directory
//                                        entries to be filled
//
// Return value:    EFI_STATUS - Status of the operation (EFI_SUCCESS)
//
// Modified:        None
//
// Referral(s):     None
//
// NOTE(S):
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
SplitLFN (
    IN CHAR16         *LongName,
    IN DIR_ENTRY_32   *ShortEntry,
    OUT DIR_ENTRY_LFN *Slot,
    OUT UINT32        *NumSlots
)
{
    UINT32 NamePos;
    UINT32 SlotPos;
    UINT8  Order;
    UINT8  Checksum = LfnChecksum((UINT8 *)ShortEntry);
    UINT32 CharCnt;
    UINT32 Position;
    INTN   i;
    
    CharCnt = (UINT32) Wcslen(LongName);
    *NumSlots = CharCnt / 13;
    
    if (CharCnt % 13) *NumSlots += 1; // Name not evenly divisible by 13
    
    Order = *NumSlots;
    Position = 13 * (Order - 1); // Starting position for extracting characters.
    
    for (i=0; Order; i++, Order--) {
        // Fill in the non-name stuff
        Slot[i].Dir_Order    = Order; // 1-based numeration
        Slot[i].Dir_Attr     = (ATTR_LONG_NAME);
        Slot[i].Dir_Reserved = 0;
        Slot[i].Dir_Checksum = Checksum;
        Slot[i].Dir_FstClus  = 0;
        Slot[i].Dir_FstClus  = 0;
        // Fill in the name
        NamePos = Position;
        SlotPos = 0;
        
        while (LongName[NamePos] && SlotPos < 13) {
            CopyCharsToLfnSlot(&Slot[i], SlotPos++, LongName[NamePos]);
            
            if (++NamePos == MAX_LFN_LENGTH) return EFI_INVALID_PARAMETER;
        }
        
        if (i == 0) { // If last slot
            // Mark the slot as last
            Slot[i].Dir_Order |= 0x40;
            // Insert a Unicode NULL terminator, only if it fits into the slots
            CopyCharsToLfnSlot(&Slot[i], SlotPos++, 0x0000);
            
            // Pad the remaining characters of the slot with FFFFh
            while (SlotPos < 13) CopyCharsToLfnSlot(&Slot[i], SlotPos++, 0xFFFF);
        }
        
        Position -= 13; // Back up to next section to process
    }
    
    return EFI_SUCCESS;
}

//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2009, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**         5555 Oakbrook Pkwy, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************