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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
|
/** @file
Member functions of EFI_SHELL_PARAMETERS_PROTOCOL and functions for creation,
manipulation, and initialization of EFI_SHELL_PARAMETERS_PROTOCOL.
Copyright (C) 2014, Red Hat, Inc.
Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
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.
**/
#include "Shell.h"
/**
Return the next parameter's end from a command line string.
@param[in] String the string to parse
**/
CONST CHAR16*
EFIAPI
FindEndOfParameter(
IN CONST CHAR16 *String
)
{
CONST CHAR16 *First;
CONST CHAR16 *CloseQuote;
First = FindFirstCharacter(String, L" \"", L'^');
//
// nothing, all one parameter remaining
//
if (*First == CHAR_NULL) {
return (First);
}
//
// If space before a quote (or neither found, i.e. both CHAR_NULL),
// then that's the end.
//
if (*First == L' ') {
return (First);
}
CloseQuote = FindFirstCharacter (First+1, L"\"", L'^');
//
// We did not find a terminator...
//
if (*CloseQuote == CHAR_NULL) {
return (NULL);
}
return (FindEndOfParameter (CloseQuote+1));
}
/**
Return the next parameter from a command line string.
This function moves the next parameter from Walker into TempParameter and moves
Walker up past that parameter for recursive calling. When the final parameter
is moved *Walker will be set to NULL;
Temp Parameter must be large enough to hold the parameter before calling this
function.
This will also remove all remaining ^ characters after processing.
@param[in, out] Walker pointer to string of command line. Adjusted to
reminaing command line on return
@param[in, out] TempParameter pointer to string of command line item extracted.
@param[in] Length buffer size of TempParameter.
@return EFI_INALID_PARAMETER A required parameter was NULL or pointed to a NULL or empty string.
@return EFI_NOT_FOUND A closing " could not be found on the specified string
**/
EFI_STATUS
EFIAPI
GetNextParameter(
IN OUT CHAR16 **Walker,
IN OUT CHAR16 **TempParameter,
IN CONST UINTN Length
)
{
CONST CHAR16 *NextDelim;
if (Walker == NULL
||*Walker == NULL
||TempParameter == NULL
||*TempParameter == NULL
){
return (EFI_INVALID_PARAMETER);
}
//
// make sure we dont have any leading spaces
//
while ((*Walker)[0] == L' ') {
(*Walker)++;
}
//
// make sure we still have some params now...
//
if (StrLen(*Walker) == 0) {
DEBUG_CODE_BEGIN();
*Walker = NULL;
DEBUG_CODE_END();
return (EFI_INVALID_PARAMETER);
}
NextDelim = FindEndOfParameter(*Walker);
if (NextDelim == NULL){
DEBUG_CODE_BEGIN();
*Walker = NULL;
DEBUG_CODE_END();
return (EFI_NOT_FOUND);
}
StrnCpy(*TempParameter, (*Walker), NextDelim - *Walker);
//
// Add a CHAR_NULL if we didnt get one via the copy
//
if (*NextDelim != CHAR_NULL) {
(*TempParameter)[NextDelim - *Walker] = CHAR_NULL;
}
//
// Update Walker for the next iteration through the function
//
*Walker = (CHAR16*)NextDelim;
//
// Remove any non-escaped quotes in the string
// Remove any remaining escape characters in the string
//
for (NextDelim = FindFirstCharacter(*TempParameter, L"\"^", CHAR_NULL)
; *NextDelim != CHAR_NULL
; NextDelim = FindFirstCharacter(NextDelim, L"\"^", CHAR_NULL)
) {
if (*NextDelim == L'^') {
//
// eliminate the escape ^
//
CopyMem ((CHAR16*)NextDelim, NextDelim + 1, StrSize (NextDelim + 1));
NextDelim++;
} else if (*NextDelim == L'\"') {
//
// eliminate the unescaped quote
//
CopyMem ((CHAR16*)NextDelim, NextDelim + 1, StrSize (NextDelim + 1));
}
}
return EFI_SUCCESS;
}
/**
Function to populate Argc and Argv.
This function parses the CommandLine and divides it into standard C style Argc/Argv
parameters for inclusion in EFI_SHELL_PARAMETERS_PROTOCOL. this supports space
delimited and quote surrounded parameter definition.
All special character processing (alias, environment variable, redirection,
etc... must be complete before calling this API.
@param[in] CommandLine String of command line to parse
@param[in, out] Argv pointer to array of strings; one for each parameter
@param[in, out] Argc pointer to number of strings in Argv array
@return EFI_SUCCESS the operation was sucessful
@return EFI_OUT_OF_RESOURCES a memory allocation failed.
**/
EFI_STATUS
EFIAPI
ParseCommandLineToArgs(
IN CONST CHAR16 *CommandLine,
IN OUT CHAR16 ***Argv,
IN OUT UINTN *Argc
)
{
UINTN Count;
CHAR16 *TempParameter;
CHAR16 *Walker;
CHAR16 *NewParam;
UINTN Size;
ASSERT(Argc != NULL);
ASSERT(Argv != NULL);
if (CommandLine == NULL || StrLen(CommandLine)==0) {
(*Argc) = 0;
(*Argv) = NULL;
return (EFI_SUCCESS);
}
Size = StrSize(CommandLine);
TempParameter = AllocateZeroPool(Size);
if (TempParameter == NULL) {
return (EFI_OUT_OF_RESOURCES);
}
for ( Count = 0
, Walker = (CHAR16*)CommandLine
; Walker != NULL && *Walker != CHAR_NULL
; Count++
) {
if (EFI_ERROR(GetNextParameter(&Walker, &TempParameter, Size))) {
break;
}
}
//
// lets allocate the pointer array
//
(*Argv) = AllocateZeroPool((Count)*sizeof(CHAR16*));
if (*Argv == NULL) {
SHELL_FREE_NON_NULL(TempParameter);
return (EFI_OUT_OF_RESOURCES);
}
*Argc = 0;
Walker = (CHAR16*)CommandLine;
while(Walker != NULL && *Walker != CHAR_NULL) {
SetMem16(TempParameter, Size, CHAR_NULL);
if (EFI_ERROR(GetNextParameter(&Walker, &TempParameter, Size))) {
SHELL_FREE_NON_NULL(TempParameter);
return (EFI_INVALID_PARAMETER);
}
NewParam = AllocateCopyPool(StrSize(TempParameter), TempParameter);
if (NewParam == NULL){
SHELL_FREE_NON_NULL(TempParameter);
return (EFI_OUT_OF_RESOURCES);
}
((CHAR16**)(*Argv))[(*Argc)] = NewParam;
(*Argc)++;
}
ASSERT(Count >= (*Argc));
SHELL_FREE_NON_NULL(TempParameter);
return (EFI_SUCCESS);
}
/**
creates a new EFI_SHELL_PARAMETERS_PROTOCOL instance and populates it and then
installs it on our handle and if there is an existing version of the protocol
that one is cached for removal later.
@param[in, out] NewShellParameters on a successful return, a pointer to pointer
to the newly installed interface.
@param[in, out] RootShellInstance on a successful return, pointer to boolean.
TRUE if this is the root shell instance.
@retval EFI_SUCCESS the operation completed successfully.
@return other the operation failed.
@sa ReinstallProtocolInterface
@sa InstallProtocolInterface
@sa ParseCommandLineToArgs
**/
EFI_STATUS
EFIAPI
CreatePopulateInstallShellParametersProtocol (
IN OUT EFI_SHELL_PARAMETERS_PROTOCOL **NewShellParameters,
IN OUT BOOLEAN *RootShellInstance
)
{
EFI_STATUS Status;
EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
CHAR16 *FullCommandLine;
UINTN Size;
Size = 0;
FullCommandLine = NULL;
LoadedImage = NULL;
//
// Assert for valid parameters
//
ASSERT(NewShellParameters != NULL);
ASSERT(RootShellInstance != NULL);
//
// See if we have a shell parameters placed on us
//
Status = gBS->OpenProtocol (
gImageHandle,
&gEfiShellParametersProtocolGuid,
(VOID **) &ShellInfoObject.OldShellParameters,
gImageHandle,
NULL,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
//
// if we don't then we must be the root shell (error is expected)
//
if (EFI_ERROR (Status)) {
*RootShellInstance = TRUE;
}
//
// Allocate the new structure
//
*NewShellParameters = AllocateZeroPool(sizeof(EFI_SHELL_PARAMETERS_PROTOCOL));
if ((*NewShellParameters) == NULL) {
return (EFI_OUT_OF_RESOURCES);
}
//
// get loaded image protocol
//
Status = gBS->OpenProtocol (
gImageHandle,
&gEfiLoadedImageProtocolGuid,
(VOID **) &LoadedImage,
gImageHandle,
NULL,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
ASSERT_EFI_ERROR(Status);
//
// Build the full command line
//
Status = SHELL_GET_ENVIRONMENT_VARIABLE(L"ShellOpt", &Size, FullCommandLine);
if (Status == EFI_BUFFER_TOO_SMALL) {
FullCommandLine = AllocateZeroPool(Size + LoadedImage->LoadOptionsSize);
Status = SHELL_GET_ENVIRONMENT_VARIABLE(L"ShellOpt", &Size, FullCommandLine);
}
if (Status == EFI_NOT_FOUND) {
//
// no parameters via environment... ok
//
} else {
if (EFI_ERROR(Status)) {
return (Status);
}
}
if (Size == 0 && LoadedImage->LoadOptionsSize != 0) {
ASSERT(FullCommandLine == NULL);
//
// Now we need to include a NULL terminator in the size.
//
Size = LoadedImage->LoadOptionsSize + sizeof(FullCommandLine[0]);
FullCommandLine = AllocateZeroPool(Size);
}
if (FullCommandLine != NULL) {
CopyMem (FullCommandLine, LoadedImage->LoadOptions, LoadedImage->LoadOptionsSize);
//
// Populate Argc and Argv
//
Status = ParseCommandLineToArgs(FullCommandLine,
&(*NewShellParameters)->Argv,
&(*NewShellParameters)->Argc);
FreePool(FullCommandLine);
ASSERT_EFI_ERROR(Status);
} else {
(*NewShellParameters)->Argv = NULL;
(*NewShellParameters)->Argc = 0;
}
//
// Populate the 3 faked file systems...
//
if (*RootShellInstance) {
(*NewShellParameters)->StdIn = &FileInterfaceStdIn;
(*NewShellParameters)->StdOut = &FileInterfaceStdOut;
(*NewShellParameters)->StdErr = &FileInterfaceStdErr;
Status = gBS->InstallProtocolInterface(&gImageHandle,
&gEfiShellParametersProtocolGuid,
EFI_NATIVE_INTERFACE,
(VOID*)(*NewShellParameters));
} else {
//
// copy from the existing ones
//
(*NewShellParameters)->StdIn = ShellInfoObject.OldShellParameters->StdIn;
(*NewShellParameters)->StdOut = ShellInfoObject.OldShellParameters->StdOut;
(*NewShellParameters)->StdErr = ShellInfoObject.OldShellParameters->StdErr;
Status = gBS->ReinstallProtocolInterface(gImageHandle,
&gEfiShellParametersProtocolGuid,
(VOID*)ShellInfoObject.OldShellParameters,
(VOID*)(*NewShellParameters));
}
return (Status);
}
/**
frees all memory used by createion and installation of shell parameters protocol
and if there was an old version installed it will restore that one.
@param NewShellParameters the interface of EFI_SHELL_PARAMETERS_PROTOCOL that is
being cleaned up.
@retval EFI_SUCCESS the cleanup was successful
@return other the cleanup failed
@sa ReinstallProtocolInterface
@sa UninstallProtocolInterface
**/
EFI_STATUS
EFIAPI
CleanUpShellParametersProtocol (
IN OUT EFI_SHELL_PARAMETERS_PROTOCOL *NewShellParameters
)
{
EFI_STATUS Status;
UINTN LoopCounter;
//
// If the old exists we need to restore it
//
if (ShellInfoObject.OldShellParameters != NULL) {
Status = gBS->ReinstallProtocolInterface(gImageHandle,
&gEfiShellParametersProtocolGuid,
(VOID*)NewShellParameters,
(VOID*)ShellInfoObject.OldShellParameters);
DEBUG_CODE(ShellInfoObject.OldShellParameters = NULL;);
} else {
//
// No old one, just uninstall us...
//
Status = gBS->UninstallProtocolInterface(gImageHandle,
&gEfiShellParametersProtocolGuid,
(VOID*)NewShellParameters);
}
if (NewShellParameters->Argv != NULL) {
for ( LoopCounter = 0
; LoopCounter < NewShellParameters->Argc
; LoopCounter++
){
FreePool(NewShellParameters->Argv[LoopCounter]);
}
FreePool(NewShellParameters->Argv);
}
FreePool(NewShellParameters);
return (Status);
}
/**
Determin if a file name represents a unicode file.
@param[in] FileName Pointer to the filename to open.
@retval EFI_SUCCESS The file is a unicode file.
@return An error upon failure.
**/
EFI_STATUS
EFIAPI
IsUnicodeFile(
IN CONST CHAR16 *FileName
)
{
SHELL_FILE_HANDLE Handle;
EFI_STATUS Status;
UINT64 OriginalFilePosition;
UINTN CharSize;
CHAR16 CharBuffer;
Status = gEfiShellProtocol->OpenFileByName(FileName, &Handle, EFI_FILE_MODE_READ);
if (EFI_ERROR(Status)) {
return (Status);
}
gEfiShellProtocol->GetFilePosition(Handle, &OriginalFilePosition);
gEfiShellProtocol->SetFilePosition(Handle, 0);
CharSize = sizeof(CHAR16);
Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);
if (EFI_ERROR(Status) || CharBuffer != gUnicodeFileTag) {
Status = EFI_BUFFER_TOO_SMALL;
}
gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);
gEfiShellProtocol->CloseFile(Handle);
return (Status);
}
/**
Strips out quotes sections of a string.
All of the characters between quotes is replaced with spaces.
@param[in, out] TheString A pointer to the string to update.
**/
VOID
EFIAPI
StripQuotes (
IN OUT CHAR16 *TheString
)
{
BOOLEAN RemoveNow;
for (RemoveNow = FALSE ; TheString != NULL && *TheString != CHAR_NULL ; TheString++) {
if (*TheString == L'^' && *(TheString + 1) == L'\"') {
TheString++;
} else if (*TheString == L'\"') {
RemoveNow = (BOOLEAN)!RemoveNow;
} else if (RemoveNow) {
*TheString = L' ';
}
}
}
/**
Calcualte the 32-bit CRC in a EFI table using the service provided by the
gRuntime service.
@param Hdr Pointer to an EFI standard header
**/
VOID
CalculateEfiHdrCrc (
IN OUT EFI_TABLE_HEADER *Hdr
)
{
UINT32 Crc;
Hdr->CRC32 = 0;
//
// If gBS->CalculateCrce32 () == CoreEfiNotAvailableYet () then
// Crc will come back as zero if we set it to zero here
//
Crc = 0;
gBS->CalculateCrc32 ((UINT8 *)Hdr, Hdr->HeaderSize, &Crc);
Hdr->CRC32 = Crc;
}
/**
Fix a string to only have the file name, removing starting at the first space of whatever is quoted.
@param[in] FileName The filename to start with.
@retval NULL FileName was invalid.
@return The modified FileName.
**/
CHAR16*
EFIAPI
FixFileName (
IN CHAR16 *FileName
)
{
CHAR16 *Copy;
CHAR16 *TempLocation;
if (FileName == NULL) {
return (NULL);
}
if (FileName[0] == L'\"') {
Copy = FileName+1;
if ((TempLocation = StrStr(Copy , L"\"")) != NULL) {
TempLocation[0] = CHAR_NULL;
}
} else {
Copy = FileName;
while(Copy[0] == L' ') {
Copy++;
}
if ((TempLocation = StrStr(Copy , L" ")) != NULL) {
TempLocation[0] = CHAR_NULL;
}
}
if (Copy[0] == CHAR_NULL) {
return (NULL);
}
return (Copy);
}
/**
Fix a string to only have the environment variable name, removing starting at the first space of whatever is quoted and removing the leading and trailing %.
@param[in] FileName The filename to start with.
@retval NULL FileName was invalid.
@return The modified FileName.
**/
CHAR16*
EFIAPI
FixVarName (
IN CHAR16 *FileName
)
{
CHAR16 *Copy;
CHAR16 *TempLocation;
Copy = FileName;
if (FileName[0] == L'%') {
Copy = FileName+1;
if ((TempLocation = StrStr(Copy , L"%")) != NULL) {
TempLocation[0] = CHAR_NULL;
}
}
return (FixFileName(Copy));
}
/**
Remove the unicode file tag from the begining of the file buffer since that will not be
used by StdIn.
@param[in] Handle Pointer to the handle of the file to be processed.
@retval EFI_SUCCESS The unicode file tag has been moved successfully.
**/
EFI_STATUS
EFIAPI
RemoveFileTag(
IN SHELL_FILE_HANDLE *Handle
)
{
UINTN CharSize;
CHAR16 CharBuffer;
CharSize = sizeof(CHAR16);
CharBuffer = 0;
gEfiShellProtocol->ReadFile(*Handle, &CharSize, &CharBuffer);
if (CharBuffer != gUnicodeFileTag) {
gEfiShellProtocol->SetFilePosition(*Handle, 0);
}
return (EFI_SUCCESS);
}
/**
Write the unicode file tag to the specified file.
It is the caller's responsibility to ensure that
ShellInfoObject.NewEfiShellProtocol has been initialized before calling this
function.
@param[in] FileHandle The file to write the unicode file tag to.
@return Status code from ShellInfoObject.NewEfiShellProtocol->WriteFile.
**/
EFI_STATUS
WriteFileTag (
IN SHELL_FILE_HANDLE FileHandle
)
{
CHAR16 FileTag;
UINTN Size;
EFI_STATUS Status;
FileTag = gUnicodeFileTag;
Size = sizeof FileTag;
Status = ShellInfoObject.NewEfiShellProtocol->WriteFile (FileHandle, &Size,
&FileTag);
ASSERT (EFI_ERROR (Status) || Size == sizeof FileTag);
return Status;
}
/**
Funcion will replace the current StdIn and StdOut in the ShellParameters protocol
structure by parsing NewCommandLine. The current values are returned to the
user.
This will also update the system table.
@param[in, out] ShellParameters Pointer to parameter structure to modify.
@param[in] NewCommandLine The new command line to parse and use.
@param[out] OldStdIn Pointer to old StdIn.
@param[out] OldStdOut Pointer to old StdOut.
@param[out] OldStdErr Pointer to old StdErr.
@param[out] SystemTableInfo Pointer to old system table information.
@retval EFI_SUCCESS Operation was sucessful, Argv and Argc are valid.
@retval EFI_OUT_OF_RESOURCES A memory allocation failed.
**/
EFI_STATUS
EFIAPI
UpdateStdInStdOutStdErr(
IN OUT EFI_SHELL_PARAMETERS_PROTOCOL *ShellParameters,
IN CHAR16 *NewCommandLine,
OUT SHELL_FILE_HANDLE *OldStdIn,
OUT SHELL_FILE_HANDLE *OldStdOut,
OUT SHELL_FILE_HANDLE *OldStdErr,
OUT SYSTEM_TABLE_INFO *SystemTableInfo
)
{
CHAR16 *CommandLineCopy;
CHAR16 *CommandLineWalker;
CHAR16 *StdErrFileName;
CHAR16 *StdOutFileName;
CHAR16 *StdInFileName;
CHAR16 *StdInVarName;
CHAR16 *StdOutVarName;
CHAR16 *StdErrVarName;
EFI_STATUS Status;
SHELL_FILE_HANDLE TempHandle;
UINT64 FileSize;
BOOLEAN OutUnicode;
BOOLEAN InUnicode;
BOOLEAN ErrUnicode;
BOOLEAN OutAppend;
BOOLEAN ErrAppend;
UINTN Size;
SPLIT_LIST *Split;
CHAR16 *FirstLocation;
OutUnicode = TRUE;
InUnicode = TRUE;
ErrUnicode = TRUE;
StdInVarName = NULL;
StdOutVarName = NULL;
StdErrVarName = NULL;
StdErrFileName = NULL;
StdInFileName = NULL;
StdOutFileName = NULL;
ErrAppend = FALSE;
OutAppend = FALSE;
CommandLineCopy = NULL;
FirstLocation = NULL;
if (ShellParameters == NULL || SystemTableInfo == NULL || OldStdIn == NULL || OldStdOut == NULL || OldStdErr == NULL) {
return (EFI_INVALID_PARAMETER);
}
SystemTableInfo->ConIn = gST->ConIn;
SystemTableInfo->ConInHandle = gST->ConsoleInHandle;
SystemTableInfo->ConOut = gST->ConOut;
SystemTableInfo->ConOutHandle = gST->ConsoleOutHandle;
SystemTableInfo->ErrOut = gST->StdErr;
SystemTableInfo->ErrOutHandle = gST->StandardErrorHandle;
*OldStdIn = ShellParameters->StdIn;
*OldStdOut = ShellParameters->StdOut;
*OldStdErr = ShellParameters->StdErr;
if (NewCommandLine == NULL) {
return (EFI_SUCCESS);
}
CommandLineCopy = StrnCatGrow(&CommandLineCopy, NULL, NewCommandLine, 0);
if (CommandLineCopy == NULL) {
return (EFI_OUT_OF_RESOURCES);
}
Status = EFI_SUCCESS;
Split = NULL;
FirstLocation = CommandLineCopy + StrLen(CommandLineCopy);
StripQuotes(CommandLineCopy);
if (!IsListEmpty(&ShellInfoObject.SplitList.Link)) {
Split = (SPLIT_LIST*)GetFirstNode(&ShellInfoObject.SplitList.Link);
if (Split != NULL && Split->SplitStdIn != NULL) {
ShellParameters->StdIn = Split->SplitStdIn;
}
if (Split != NULL && Split->SplitStdOut != NULL) {
ShellParameters->StdOut = Split->SplitStdOut;
}
}
if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 2>>v ")) != NULL) {
FirstLocation = MIN(CommandLineWalker, FirstLocation);
SetMem16(CommandLineWalker, 12, L' ');
StdErrVarName = CommandLineWalker += 6;
ErrAppend = TRUE;
if (StrStr(CommandLineWalker, L" 2>>v ") != NULL) {
Status = EFI_NOT_FOUND;
}
}
if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 1>>v ")) != NULL) {
FirstLocation = MIN(CommandLineWalker, FirstLocation);
SetMem16(CommandLineWalker, 12, L' ');
StdOutVarName = CommandLineWalker += 6;
OutAppend = TRUE;
if (StrStr(CommandLineWalker, L" 1>>v ") != NULL) {
Status = EFI_NOT_FOUND;
}
} else if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" >>v ")) != NULL) {
FirstLocation = MIN(CommandLineWalker, FirstLocation);
SetMem16(CommandLineWalker, 10, L' ');
StdOutVarName = CommandLineWalker += 5;
OutAppend = TRUE;
if (StrStr(CommandLineWalker, L" >>v ") != NULL) {
Status = EFI_NOT_FOUND;
}
} else if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" >v ")) != NULL) {
FirstLocation = MIN(CommandLineWalker, FirstLocation);
SetMem16(CommandLineWalker, 8, L' ');
StdOutVarName = CommandLineWalker += 4;
OutAppend = FALSE;
if (StrStr(CommandLineWalker, L" >v ") != NULL) {
Status = EFI_NOT_FOUND;
}
}
if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 1>>a ")) != NULL) {
FirstLocation = MIN(CommandLineWalker, FirstLocation);
SetMem16(CommandLineWalker, 12, L' ');
StdOutFileName = CommandLineWalker += 6;
OutAppend = TRUE;
OutUnicode = FALSE;
if (StrStr(CommandLineWalker, L" 1>>a ") != NULL) {
Status = EFI_NOT_FOUND;
}
}
if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 1>> ")) != NULL) {
FirstLocation = MIN(CommandLineWalker, FirstLocation);
SetMem16(CommandLineWalker, 10, L' ');
if (StdOutFileName != NULL) {
Status = EFI_INVALID_PARAMETER;
} else {
StdOutFileName = CommandLineWalker += 5;
OutAppend = TRUE;
}
if (StrStr(CommandLineWalker, L" 1>> ") != NULL) {
Status = EFI_NOT_FOUND;
}
}
if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" >> ")) != NULL) {
FirstLocation = MIN(CommandLineWalker, FirstLocation);
SetMem16(CommandLineWalker, 8, L' ');
if (StdOutFileName != NULL) {
Status = EFI_INVALID_PARAMETER;
} else {
StdOutFileName = CommandLineWalker += 4;
OutAppend = TRUE;
}
if (StrStr(CommandLineWalker, L" >> ") != NULL) {
Status = EFI_NOT_FOUND;
}
}
if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" >>a ")) != NULL) {
FirstLocation = MIN(CommandLineWalker, FirstLocation);
SetMem16(CommandLineWalker, 10, L' ');
if (StdOutFileName != NULL) {
Status = EFI_INVALID_PARAMETER;
} else {
StdOutFileName = CommandLineWalker += 5;
OutAppend = TRUE;
OutUnicode = FALSE;
}
if (StrStr(CommandLineWalker, L" >>a ") != NULL) {
Status = EFI_NOT_FOUND;
}
}
if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 1>a ")) != NULL) {
FirstLocation = MIN(CommandLineWalker, FirstLocation);
SetMem16(CommandLineWalker, 10, L' ');
if (StdOutFileName != NULL) {
Status = EFI_INVALID_PARAMETER;
} else {
StdOutFileName = CommandLineWalker += 5;
OutAppend = FALSE;
OutUnicode = FALSE;
}
if (StrStr(CommandLineWalker, L" 1>a ") != NULL) {
Status = EFI_NOT_FOUND;
}
}
if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" >a ")) != NULL) {
FirstLocation = MIN(CommandLineWalker, FirstLocation);
SetMem16(CommandLineWalker, 8, L' ');
if (StdOutFileName != NULL) {
Status = EFI_INVALID_PARAMETER;
} else {
StdOutFileName = CommandLineWalker += 4;
OutAppend = FALSE;
OutUnicode = FALSE;
}
if (StrStr(CommandLineWalker, L" >a ") != NULL) {
Status = EFI_NOT_FOUND;
}
}
if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 2>> ")) != NULL) {
FirstLocation = MIN(CommandLineWalker, FirstLocation);
SetMem16(CommandLineWalker, 10, L' ');
if (StdErrFileName != NULL) {
Status = EFI_INVALID_PARAMETER;
} else {
StdErrFileName = CommandLineWalker += 5;
ErrAppend = TRUE;
}
if (StrStr(CommandLineWalker, L" 2>> ") != NULL) {
Status = EFI_NOT_FOUND;
}
}
if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 2>v ")) != NULL) {
FirstLocation = MIN(CommandLineWalker, FirstLocation);
SetMem16(CommandLineWalker, 10, L' ');
if (StdErrVarName != NULL) {
Status = EFI_INVALID_PARAMETER;
} else {
StdErrVarName = CommandLineWalker += 5;
ErrAppend = FALSE;
}
if (StrStr(CommandLineWalker, L" 2>v ") != NULL) {
Status = EFI_NOT_FOUND;
}
}
if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 1>v ")) != NULL) {
FirstLocation = MIN(CommandLineWalker, FirstLocation);
SetMem16(CommandLineWalker, 10, L' ');
if (StdOutVarName != NULL) {
Status = EFI_INVALID_PARAMETER;
} else {
StdOutVarName = CommandLineWalker += 5;
OutAppend = FALSE;
}
if (StrStr(CommandLineWalker, L" 1>v ") != NULL) {
Status = EFI_NOT_FOUND;
}
}
if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 2>a ")) != NULL) {
FirstLocation = MIN(CommandLineWalker, FirstLocation);
SetMem16(CommandLineWalker, 10, L' ');
if (StdErrFileName != NULL) {
Status = EFI_INVALID_PARAMETER;
} else {
StdErrFileName = CommandLineWalker += 5;
ErrAppend = FALSE;
ErrUnicode = FALSE;
}
if (StrStr(CommandLineWalker, L" 2>a ") != NULL) {
Status = EFI_NOT_FOUND;
}
}
if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 2> ")) != NULL) {
FirstLocation = MIN(CommandLineWalker, FirstLocation);
SetMem16(CommandLineWalker, 8, L' ');
if (StdErrFileName != NULL) {
Status = EFI_INVALID_PARAMETER;
} else {
StdErrFileName = CommandLineWalker += 4;
ErrAppend = FALSE;
}
if (StrStr(CommandLineWalker, L" 2> ") != NULL) {
Status = EFI_NOT_FOUND;
}
}
if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 1> ")) != NULL) {
FirstLocation = MIN(CommandLineWalker, FirstLocation);
SetMem16(CommandLineWalker, 8, L' ');
if (StdOutFileName != NULL) {
Status = EFI_INVALID_PARAMETER;
} else {
StdOutFileName = CommandLineWalker += 4;
OutAppend = FALSE;
}
if (StrStr(CommandLineWalker, L" 1> ") != NULL) {
Status = EFI_NOT_FOUND;
}
}
if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" > ")) != NULL) {
FirstLocation = MIN(CommandLineWalker, FirstLocation);
SetMem16(CommandLineWalker, 6, L' ');
if (StdOutFileName != NULL) {
Status = EFI_INVALID_PARAMETER;
} else {
StdOutFileName = CommandLineWalker += 3;
OutAppend = FALSE;
}
if (StrStr(CommandLineWalker, L" > ") != NULL) {
Status = EFI_NOT_FOUND;
}
}
if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" < ")) != NULL) {
FirstLocation = MIN(CommandLineWalker, FirstLocation);
SetMem16(CommandLineWalker, 6, L' ');
if (StdInFileName != NULL) {
Status = EFI_INVALID_PARAMETER;
} else {
StdInFileName = CommandLineWalker += 3;
}
if (StrStr(CommandLineWalker, L" < ") != NULL) {
Status = EFI_NOT_FOUND;
}
}
if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" <a ")) != NULL) {
FirstLocation = MIN(CommandLineWalker, FirstLocation);
SetMem16(CommandLineWalker, 8, L' ');
if (StdInFileName != NULL) {
Status = EFI_INVALID_PARAMETER;
} else {
StdInFileName = CommandLineWalker += 4;
InUnicode = FALSE;
}
if (StrStr(CommandLineWalker, L" <a ") != NULL) {
Status = EFI_NOT_FOUND;
}
}
if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" <v ")) != NULL) {
FirstLocation = MIN(CommandLineWalker, FirstLocation);
SetMem16(CommandLineWalker, 8, L' ');
if (StdInVarName != NULL) {
Status = EFI_INVALID_PARAMETER;
} else {
StdInVarName = CommandLineWalker += 4;
}
if (StrStr(CommandLineWalker, L" <v ") != NULL) {
Status = EFI_NOT_FOUND;
}
}
//
// re-populate the string to support any filenames that were in quotes.
//
StrnCpy(CommandLineCopy, NewCommandLine, StrLen(NewCommandLine));
if (FirstLocation != CommandLineCopy + StrLen(CommandLineCopy)
&& ((UINTN)(FirstLocation - CommandLineCopy) < StrLen(NewCommandLine))
){
*(NewCommandLine + (UINTN)(FirstLocation - CommandLineCopy)) = CHAR_NULL;
}
if (!EFI_ERROR(Status)) {
if (StdErrFileName != NULL) {
if ((StdErrFileName = FixFileName(StdErrFileName)) == NULL) {
Status = EFI_INVALID_PARAMETER;
}
}
if (StdOutFileName != NULL) {
if ((StdOutFileName = FixFileName(StdOutFileName)) == NULL) {
Status = EFI_INVALID_PARAMETER;
}
}
if (StdInFileName != NULL) {
if ((StdInFileName = FixFileName(StdInFileName)) == NULL) {
Status = EFI_INVALID_PARAMETER;
}
}
if (StdErrVarName != NULL) {
if ((StdErrVarName = FixVarName(StdErrVarName)) == NULL) {
Status = EFI_INVALID_PARAMETER;
}
}
if (StdOutVarName != NULL) {
if ((StdOutVarName = FixVarName(StdOutVarName)) == NULL) {
Status = EFI_INVALID_PARAMETER;
}
}
if (StdInVarName != NULL) {
if ((StdInVarName = FixVarName(StdInVarName)) == NULL) {
Status = EFI_INVALID_PARAMETER;
}
}
//
// Verify not the same and not duplicating something from a split
//
if (
//
// Check that no 2 filenames are the same
//
(StdErrFileName != NULL && StdOutFileName!= NULL && StringNoCaseCompare(&StdErrFileName, &StdOutFileName) == 0)
||(StdErrFileName != NULL && StdInFileName != NULL && StringNoCaseCompare(&StdErrFileName, &StdInFileName ) == 0)
||(StdOutFileName != NULL && StdInFileName != NULL && StringNoCaseCompare(&StdOutFileName, &StdInFileName ) == 0)
//
// Check that no 2 variable names are the same
//
||(StdErrVarName != NULL && StdInVarName != NULL && StringNoCaseCompare(&StdErrVarName , &StdInVarName ) == 0)
||(StdOutVarName != NULL && StdInVarName != NULL && StringNoCaseCompare(&StdOutVarName , &StdInVarName ) == 0)
||(StdErrVarName != NULL && StdOutVarName != NULL && StringNoCaseCompare(&StdErrVarName , &StdOutVarName ) == 0)
//
// When a split (using | operator) is in place some are not allowed
//
||(Split != NULL && Split->SplitStdIn != NULL && (StdInVarName != NULL || StdInFileName != NULL))
||(Split != NULL && Split->SplitStdOut != NULL && (StdOutVarName != NULL || StdOutFileName != NULL))
//
// Check that nothing is trying to be output to 2 locations.
//
||(StdErrFileName != NULL && StdErrVarName != NULL)
||(StdOutFileName != NULL && StdOutVarName != NULL)
||(StdInFileName != NULL && StdInVarName != NULL)
//
// Check for no volatile environment variables
//
||(StdErrVarName != NULL && !IsVolatileEnv(StdErrVarName))
||(StdOutVarName != NULL && !IsVolatileEnv(StdOutVarName))
//
// Cant redirect during a reconnect operation.
//
||(StrStr(NewCommandLine, L"connect -r") != NULL
&& (StdOutVarName != NULL || StdOutFileName != NULL || StdErrFileName != NULL || StdErrVarName != NULL))
//
// Check that filetypes (Unicode/Ascii) do not change during an append
//
||(StdOutFileName != NULL && OutUnicode && OutAppend && (!EFI_ERROR(ShellFileExists(StdOutFileName)) && EFI_ERROR(IsUnicodeFile(StdOutFileName))))
||(StdErrFileName != NULL && ErrUnicode && ErrAppend && (!EFI_ERROR(ShellFileExists(StdErrFileName)) && EFI_ERROR(IsUnicodeFile(StdErrFileName))))
||(StdOutFileName != NULL && !OutUnicode && OutAppend && (!EFI_ERROR(ShellFileExists(StdOutFileName)) && !EFI_ERROR(IsUnicodeFile(StdOutFileName))))
||(StdErrFileName != NULL && !ErrUnicode && ErrAppend && (!EFI_ERROR(ShellFileExists(StdErrFileName)) && !EFI_ERROR(IsUnicodeFile(StdErrFileName))))
){
Status = EFI_INVALID_PARAMETER;
ShellParameters->StdIn = *OldStdIn;
ShellParameters->StdOut = *OldStdOut;
ShellParameters->StdErr = *OldStdErr;
} else if (!EFI_ERROR(Status)){
//
// Open the Std<Whatever> and we should not have conflicts here...
//
//
// StdErr to a file
//
if (StdErrFileName != NULL) {
if (!ErrAppend) {
//
// delete existing file.
//
ShellInfoObject.NewEfiShellProtocol->DeleteFileByName(StdErrFileName);
}
Status = ShellOpenFileByName(StdErrFileName, &TempHandle, EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ|EFI_FILE_MODE_CREATE,0);
if (!ErrAppend && ErrUnicode && !EFI_ERROR(Status)) {
Status = WriteFileTag (TempHandle);
}
if (!ErrUnicode && !EFI_ERROR(Status)) {
TempHandle = CreateFileInterfaceFile(TempHandle, FALSE);
ASSERT(TempHandle != NULL);
}
if (!EFI_ERROR(Status)) {
ShellParameters->StdErr = TempHandle;
gST->StdErr = CreateSimpleTextOutOnFile(TempHandle, &gST->StandardErrorHandle, gST->StdErr);
}
}
//
// StdOut to a file
//
if (!EFI_ERROR(Status) && StdOutFileName != NULL) {
if (!OutAppend) {
//
// delete existing file.
//
ShellInfoObject.NewEfiShellProtocol->DeleteFileByName(StdOutFileName);
}
Status = ShellOpenFileByName(StdOutFileName, &TempHandle, EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ|EFI_FILE_MODE_CREATE,0);
if (TempHandle == NULL) {
Status = EFI_INVALID_PARAMETER;
} else {
if (StrStr(StdOutFileName, L"NUL")==StdOutFileName) {
//no-op
} else if (!OutAppend && OutUnicode && !EFI_ERROR(Status)) {
Status = WriteFileTag (TempHandle);
} else if (OutAppend) {
Status = ShellInfoObject.NewEfiShellProtocol->GetFileSize(TempHandle, &FileSize);
if (!EFI_ERROR(Status)) {
//
// When appending to a new unicode file, write the file tag.
// Otherwise (ie. when appending to a new ASCII file, or an
// existent file with any encoding), just seek to the end.
//
Status = (FileSize == 0 && OutUnicode) ?
WriteFileTag (TempHandle) :
ShellInfoObject.NewEfiShellProtocol->SetFilePosition (
TempHandle,
FileSize);
}
}
if (!OutUnicode && !EFI_ERROR(Status)) {
TempHandle = CreateFileInterfaceFile(TempHandle, FALSE);
ASSERT(TempHandle != NULL);
}
if (!EFI_ERROR(Status)) {
ShellParameters->StdOut = TempHandle;
gST->ConOut = CreateSimpleTextOutOnFile(TempHandle, &gST->ConsoleOutHandle, gST->ConOut);
}
}
}
//
// StdOut to a var
//
if (!EFI_ERROR(Status) && StdOutVarName != NULL) {
if (!OutAppend) {
//
// delete existing variable.
//
SHELL_SET_ENVIRONMENT_VARIABLE_V(StdOutVarName, 0, L"");
}
TempHandle = CreateFileInterfaceEnv(StdOutVarName);
ASSERT(TempHandle != NULL);
ShellParameters->StdOut = TempHandle;
gST->ConOut = CreateSimpleTextOutOnFile(TempHandle, &gST->ConsoleOutHandle, gST->ConOut);
}
//
// StdErr to a var
//
if (!EFI_ERROR(Status) && StdErrVarName != NULL) {
if (!ErrAppend) {
//
// delete existing variable.
//
SHELL_SET_ENVIRONMENT_VARIABLE_V(StdErrVarName, 0, L"");
}
TempHandle = CreateFileInterfaceEnv(StdErrVarName);
ASSERT(TempHandle != NULL);
ShellParameters->StdErr = TempHandle;
gST->StdErr = CreateSimpleTextOutOnFile(TempHandle, &gST->StandardErrorHandle, gST->StdErr);
}
//
// StdIn from a var
//
if (!EFI_ERROR(Status) && StdInVarName != NULL) {
TempHandle = CreateFileInterfaceEnv(StdInVarName);
if (TempHandle == NULL) {
Status = EFI_OUT_OF_RESOURCES;
} else {
if (!InUnicode) {
TempHandle = CreateFileInterfaceFile(TempHandle, FALSE);
}
Size = 0;
if (TempHandle == NULL || ((EFI_FILE_PROTOCOL*)TempHandle)->Read(TempHandle, &Size, NULL) != EFI_BUFFER_TOO_SMALL) {
Status = EFI_INVALID_PARAMETER;
} else {
ShellParameters->StdIn = TempHandle;
gST->ConIn = CreateSimpleTextInOnFile(TempHandle, &gST->ConsoleInHandle);
}
}
}
//
// StdIn from a file
//
if (!EFI_ERROR(Status) && StdInFileName != NULL) {
Status = ShellOpenFileByName(
StdInFileName,
&TempHandle,
EFI_FILE_MODE_READ,
0);
if (InUnicode) {
//
// Chop off the 0xFEFF if it's there...
//
RemoveFileTag(&TempHandle);
} else if (!EFI_ERROR(Status)) {
//
// Create the ASCII->Unicode conversion layer
//
TempHandle = CreateFileInterfaceFile(TempHandle, FALSE);
}
if (!EFI_ERROR(Status)) {
ShellParameters->StdIn = TempHandle;
gST->ConIn = CreateSimpleTextInOnFile(TempHandle, &gST->ConsoleInHandle);
}
}
}
}
FreePool(CommandLineCopy);
CalculateEfiHdrCrc(&gST->Hdr);
if (gST->ConIn == NULL ||gST->ConOut == NULL) {
Status = EFI_OUT_OF_RESOURCES;
}
if (Status == EFI_NOT_FOUND) {
ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_REDUNDA_REDIR), ShellInfoObject.HiiHandle);
} else if (EFI_ERROR(Status)) {
ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_REDIR), ShellInfoObject.HiiHandle);
}
return (Status);
}
/**
Funcion will replace the current StdIn and StdOut in the ShellParameters protocol
structure with StdIn and StdOut. The current values are de-allocated.
@param[in, out] ShellParameters Pointer to parameter structure to modify.
@param[in] OldStdIn Pointer to old StdIn.
@param[in] OldStdOut Pointer to old StdOut.
@param[in] OldStdErr Pointer to old StdErr.
@param[in] SystemTableInfo Pointer to old system table information.
**/
EFI_STATUS
EFIAPI
RestoreStdInStdOutStdErr (
IN OUT EFI_SHELL_PARAMETERS_PROTOCOL *ShellParameters,
IN SHELL_FILE_HANDLE *OldStdIn,
IN SHELL_FILE_HANDLE *OldStdOut,
IN SHELL_FILE_HANDLE *OldStdErr,
IN SYSTEM_TABLE_INFO *SystemTableInfo
)
{
SPLIT_LIST *Split;
if (ShellParameters == NULL
||OldStdIn == NULL
||OldStdOut == NULL
||OldStdErr == NULL
||SystemTableInfo == NULL) {
return (EFI_INVALID_PARAMETER);
}
if (!IsListEmpty(&ShellInfoObject.SplitList.Link)) {
Split = (SPLIT_LIST*)GetFirstNode(&ShellInfoObject.SplitList.Link);
} else {
Split = NULL;
}
if (ShellParameters->StdIn != *OldStdIn) {
if ((Split != NULL && Split->SplitStdIn != ShellParameters->StdIn) || Split == NULL) {
gEfiShellProtocol->CloseFile(ShellParameters->StdIn);
}
ShellParameters->StdIn = *OldStdIn;
}
if (ShellParameters->StdOut != *OldStdOut) {
if ((Split != NULL && Split->SplitStdOut != ShellParameters->StdOut) || Split == NULL) {
gEfiShellProtocol->CloseFile(ShellParameters->StdOut);
}
ShellParameters->StdOut = *OldStdOut;
}
if (ShellParameters->StdErr != *OldStdErr) {
gEfiShellProtocol->CloseFile(ShellParameters->StdErr);
ShellParameters->StdErr = *OldStdErr;
}
if (gST->ConIn != SystemTableInfo->ConIn) {
CloseSimpleTextInOnFile(gST->ConIn);
gST->ConIn = SystemTableInfo->ConIn;
gST->ConsoleInHandle = SystemTableInfo->ConInHandle;
}
if (gST->ConOut != SystemTableInfo->ConOut) {
CloseSimpleTextOutOnFile(gST->ConOut);
gST->ConOut = SystemTableInfo->ConOut;
gST->ConsoleOutHandle = SystemTableInfo->ConOutHandle;
}
if (gST->StdErr != SystemTableInfo->ErrOut) {
CloseSimpleTextOutOnFile(gST->StdErr);
gST->StdErr = SystemTableInfo->ErrOut;
gST->StandardErrorHandle = SystemTableInfo->ErrOutHandle;
}
CalculateEfiHdrCrc(&gST->Hdr);
return (EFI_SUCCESS);
}
/**
Funcion will replace the current Argc and Argv in the ShellParameters protocol
structure by parsing NewCommandLine. The current values are returned to the
user.
If OldArgv or OldArgc is NULL then that value is not returned.
@param[in, out] ShellParameters Pointer to parameter structure to modify.
@param[in] NewCommandLine The new command line to parse and use.
@param[out] OldArgv Pointer to old list of parameters.
@param[out] OldArgc Pointer to old number of items in Argv list.
@retval EFI_SUCCESS Operation was sucessful, Argv and Argc are valid.
@retval EFI_OUT_OF_RESOURCES A memory allocation failed.
**/
EFI_STATUS
EFIAPI
UpdateArgcArgv(
IN OUT EFI_SHELL_PARAMETERS_PROTOCOL *ShellParameters,
IN CONST CHAR16 *NewCommandLine,
OUT CHAR16 ***OldArgv OPTIONAL,
OUT UINTN *OldArgc OPTIONAL
)
{
ASSERT(ShellParameters != NULL);
if (OldArgc != NULL) {
*OldArgc = ShellParameters->Argc;
}
if (OldArgc != NULL) {
*OldArgv = ShellParameters->Argv;
}
return (ParseCommandLineToArgs(NewCommandLine, &(ShellParameters->Argv), &(ShellParameters->Argc)));
}
/**
Funcion will replace the current Argc and Argv in the ShellParameters protocol
structure with Argv and Argc. The current values are de-allocated and the
OldArgv must not be deallocated by the caller.
@param[in, out] ShellParameters pointer to parameter structure to modify
@param[in] OldArgv pointer to old list of parameters
@param[in] OldArgc pointer to old number of items in Argv list
**/
VOID
EFIAPI
RestoreArgcArgv(
IN OUT EFI_SHELL_PARAMETERS_PROTOCOL *ShellParameters,
IN CHAR16 ***OldArgv,
IN UINTN *OldArgc
)
{
UINTN LoopCounter;
ASSERT(ShellParameters != NULL);
ASSERT(OldArgv != NULL);
ASSERT(OldArgc != NULL);
if (ShellParameters->Argv != NULL) {
for ( LoopCounter = 0
; LoopCounter < ShellParameters->Argc
; LoopCounter++
){
FreePool(ShellParameters->Argv[LoopCounter]);
}
FreePool(ShellParameters->Argv);
}
ShellParameters->Argv = *OldArgv;
*OldArgv = NULL;
ShellParameters->Argc = *OldArgc;
*OldArgc = 0;
}
|