-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattrib.c
496 lines (420 loc) · 9.99 KB
/
attrib.c
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*++
Copyright (c) 2005 - 2006, Intel Corporation
All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
Module Name:
attrib.c
Abstract:
EFI shell command "attrib" - set file attributes
Revision History
--*/
#include "EfiShellLib.h"
#include "attrib.h"
extern UINT8 STRING_ARRAY_NAME[];
//
// This is the generated header file which includes whatever needs to be exported (strings + IFR)
//
#include STRING_DEFINES_FILE
//
// Function declarations
//
EFI_STATUS
EFIAPI
InitializeAttrib (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
);
EFI_STATUS
AttribSet (
IN CHAR16 *Str,
IN OUT UINT64 *Attr
);
EFI_STATUS
AttribFile (
IN SHELL_FILE_ARG *Arg,
IN UINT64 Remove,
IN UINT64 Add
);
//
// Global Variables
//
EFI_HII_HANDLE HiiHandle;
EFI_GUID EfiAttribGuid = EFI_ATTRIB_GUID;
SHELL_VAR_CHECK_ITEM AttribCheckList[] = {
{
L"+a",
0x01,
0x02,
FlagTypeSingle
},
{
L"-a",
0x02,
0x01,
FlagTypeSingle
},
{
L"+s",
0x04,
0x08,
FlagTypeSingle
},
{
L"-s",
0x08,
0x04,
FlagTypeSingle
},
{
L"+h",
0x10,
0x20,
FlagTypeSingle
},
{
L"-h",
0x20,
0x10,
FlagTypeSingle
},
{
L"+r",
0x40,
0x80,
FlagTypeSingle
},
{
L"-r",
0x80,
0x40,
FlagTypeSingle
},
{
L"-b",
0x100,
0,
FlagTypeSingle
},
{
L"-?",
0x200,
0,
FlagTypeSingle
},
{
NULL,
0,
0,
0
}
};
//
// Entry Point
//
EFI_BOOTSHELL_CODE(
EFI_APPLICATION_ENTRY_POINT(InitializeAttrib)
)
EFI_STATUS
EFIAPI
InitializeAttrib (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
/*++
Routine Description:
Command entry point. Parses command line arguments and calls internal
function to perform actual action.
Arguments:
ImageHandle The image handle.
SystemTable The system table.
Returns:
EFI_SUCCESS - Command completed successfully
EFI_INVALID_PARAMETER - Command usage error
Other value - Unknown error
--*/
{
UINTN Index;
EFI_LIST_ENTRY FileList;
EFI_LIST_ENTRY *Link;
SHELL_FILE_ARG *Arg;
UINT64 Remove;
UINT64 Add;
EFI_STATUS Status;
SHELL_VAR_CHECK_CODE RetCode;
CHAR16 *Useful;
SHELL_ARG_LIST *Item;
SHELL_VAR_CHECK_PACKAGE ChkPck;
ZeroMem (&ChkPck, sizeof (SHELL_VAR_CHECK_PACKAGE));
//
// We are no being installed as an internal command driver, initialize
// as an nshell app and run
//
EFI_SHELL_APP_INIT (ImageHandle, SystemTable);
//
// Enable tab key which can pause the output
//
EnableOutputTabPause();
Status = LibFilterNullArgs ();
if (EFI_ERROR (Status)) {
return Status;
}
//
// Register our string package with HII and return the handle to it.
// If previously registered we will simply receive the handle
//
EFI_SHELL_STR_INIT (HiiHandle, STRING_ARRAY_NAME, EfiAttribGuid);
if (!EFI_PROPER_VERSION (0, 99)) {
PrintToken (
STRING_TOKEN (STR_SHELLENV_GNC_COMMAND_NOT_SUPPORT),
HiiHandle,
L"attrib",
EFI_VERSION_0_99
);
Status = EFI_UNSUPPORTED;
goto Quit;
}
RetCode = LibCheckVariables (SI, AttribCheckList, &ChkPck, &Useful);
if (VarCheckOk != RetCode) {
switch (RetCode) {
case VarCheckConflict:
PrintToken (STRING_TOKEN (STR_SHELLENV_GNC_FLAG_CONFLICT), HiiHandle, L"attrib", Useful);
break;
case VarCheckDuplicate:
PrintToken (STRING_TOKEN (STR_SHELLENV_GNC_DUP_FLAG), HiiHandle, L"attrib", Useful);
break;
case VarCheckUnknown:
PrintToken (STRING_TOKEN (STR_SHELLENV_GNC_UNKNOWN_FLAG), HiiHandle, L"attrib", Useful);
break;
default:
break;
}
Status = EFI_INVALID_PARAMETER;
goto Quit;
}
//
// Out put help.
//
if (LibCheckVarGetFlag (&ChkPck, L"-b") != NULL) {
EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);
}
if (LibCheckVarGetFlag (&ChkPck, L"-?") != NULL) {
if (IS_OLD_SHELL) {
PrintToken (STRING_TOKEN (STR_NO_HELP), HiiHandle);
goto Quit;
}
if (ChkPck.ValueCount > 0 ||
ChkPck.FlagCount > 2 ||
(2 == ChkPck.FlagCount && !LibCheckVarGetFlag (&ChkPck, L"-b"))
) {
PrintToken (STRING_TOKEN (STR_SHELLENV_GNC_TOO_MANY), HiiHandle, L"attrib");
Status = EFI_INVALID_PARAMETER;
} else {
PrintToken (STRING_TOKEN (STR_ATTRIB_VERBOSE_HELP), HiiHandle);
Status = EFI_SUCCESS;
}
goto Quit;
}
//
// Local Variable Initializations
//
InitializeListHead (&FileList);
Link = NULL;
Arg = NULL;
Remove = 0;
Add = 0;
//
// Parse command line arguments
//
Item = GetFirstFlag (&ChkPck);
for (Index = 0; Index < ChkPck.FlagCount; Index += 1) {
if (Item->FlagStr[0] == '-') {
//
// Attributes to remove
//
Status = AttribSet (&Item->FlagStr[1], &Remove);
if (EFI_ERROR (Status)) {
PrintToken (STRING_TOKEN (STR_SHELLENV_GNC_INVALID_ARG), HiiHandle, L"attrib", Item->FlagStr);
goto Done;
}
} else if (Item->FlagStr[0] == '+') {
//
// Attributes to Add
//
Status = AttribSet (&Item->FlagStr[1], &Add);
if (EFI_ERROR (Status)) {
PrintToken (STRING_TOKEN (STR_SHELLENV_GNC_INVALID_ARG), HiiHandle, L"attrib", Item->FlagStr);
goto Done;
}
} else {
//
// we should never get here
//
ASSERT (FALSE);
}
Item = GetNextArg (Item);
}
Item = GetFirstArg (&ChkPck);
for (Index = 0; Index < ChkPck.ValueCount; Index += 1) {
Status = ShellFileMetaArg (Item->VarStr, &FileList);
if (EFI_ERROR (Status)) {
PrintToken (STRING_TOKEN (STR_ATTRIB_CANNOT_OPEN), HiiHandle, L"attrib", Item->VarStr, Status);
goto Done;
}
Item = GetNextArg (Item);
}
//
// if no file is specified, get the whole directory
//
if (IsListEmpty (&FileList)) {
Status = ShellFileMetaArg (L"*", &FileList);
if (EFI_ERROR (Status)) {
PrintToken (STRING_TOKEN (STR_ATTRIB_CANNOT_OPEN_DIR), HiiHandle, L"attrib", Status);
goto Done;
}
}
ShellDelDupFileArg (&FileList);
//
// Attrib each file
//
for (Link = FileList.Flink; Link != &FileList; Link = Link->Flink) {
//
// Break the execution?
//
if (GetExecutionBreak ()) {
goto Done;
}
Arg = CR (Link, SHELL_FILE_ARG, Link, SHELL_FILE_ARG_SIGNATURE);
Status = AttribFile (Arg, Remove, Add);
}
Done:
ShellFreeFileList (&FileList);
Quit:
LibCheckVarFreeVarList (&ChkPck);
LibUnInitializeStrings ();
return Status;
}
EFI_STATUS
AttribSet (
IN CHAR16 *Str,
IN OUT UINT64 *Attr
)
{
//
// Convert to Lower, lest case/break not equal
//
StrLwr (Str);
while (*Str) {
//
// Check one by one
//
switch (*Str) {
case 'b':
EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);
break;
case 'a':
*Attr |= EFI_FILE_ARCHIVE;
break;
case 's':
*Attr |= EFI_FILE_SYSTEM;
break;
case 'h':
*Attr |= EFI_FILE_HIDDEN;
break;
case 'r':
*Attr |= EFI_FILE_READ_ONLY;
break;
default:
return EFI_INVALID_PARAMETER;
}
Str += 1;
}
return EFI_SUCCESS;
}
EFI_STATUS
AttribFile (
IN SHELL_FILE_ARG *Arg,
IN UINT64 Remove,
IN UINT64 Add
)
/*++
Routine Description:
Set/remove attributes of a file
Arguments:
Arg The file whose attributes are to be modified.
Remove Bitmap of the attributes to be removed.
Add Bitmap of the attributes to be added.
Returns:
EFI_SUCCESS Set attribute of file successfully
EFI_ABORTED Press 'q' to abort
--*/
{
UINT64 Attr;
EFI_STATUS Status;
EFI_FILE_INFO *Info;
//
// Local variable initializations
//
Attr = 0;
Status = EFI_SUCCESS;
Info = NULL;
//
// Validate the file
//
Status = Arg->Status;
if (!EFI_ERROR (Status)) {
//
// Attrib it
//
Info = Arg->Info;
if (Add || Remove) {
Info->Attribute = Info->Attribute & (~Remove) | Add;
Status = Arg->Handle->SetInfo (
Arg->Handle,
&gEfiFileInfoGuid,
(UINTN) Info->Size,
Info
);
}
}
//
// Output result
//
if (EFI_ERROR (Status)) {
PrintToken (STRING_TOKEN (STR_ATTRIB_CANNOT_SET), HiiHandle, L"attrib", Arg->FullName, Status);
return Status;
} else {
Attr = Info->Attribute;
PrintToken (
STRING_TOKEN (STR_ATTRIB_EFI_DASHR),
HiiHandle,
L"attrib",
Attr & EFI_FILE_DIRECTORY ? 'D' : ' ',
Attr & EFI_FILE_ARCHIVE ? 'A' : ' ',
Attr & EFI_FILE_SYSTEM ? 'S' : ' ',
Attr & EFI_FILE_HIDDEN ? 'H' : ' ',
Attr & EFI_FILE_READ_ONLY ? 'R' : ' ',
Arg->FullName
);
}
return EFI_SUCCESS;
}
EFI_STATUS
EFIAPI
InitializeAttribGetLineHelp (
OUT CHAR16 **Str
)
/*++
Routine Description:
Get this command's line help
Arguments:
Str - The line help
Returns:
EFI_SUCCESS - Success
--*/
{
return LibCmdGetStringByToken (STRING_ARRAY_NAME, &EfiAttribGuid, STRING_TOKEN (STR_ATTRIB_LINE_HELP), Str);
}