]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellLevel2CommandsLib/Map.c
ShellPkg: Fix buffer overflow issue in 'map' command.
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Map.c
1 /** @file
2 Main file for map shell level 2 command.
3
4 (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "UefiShellLevel2CommandsLib.h"
17 #include <Protocol/SimpleFileSystem.h>
18 #include <Protocol/BlockIo.h>
19 #include <Library/DevicePathLib.h>
20 #include <Library/HandleParsingLib.h>
21 #include <Library/SortLib.h>
22
23 /**
24 Determine if a string has only numbers and letters.
25
26 This is useful for such things as Map names which can only be letters and numbers.
27
28 @param[in] String pointer to the string to analyze,
29 @param[in] Len Number of characters to analyze.
30
31 @retval TRUE String has only numbers and letters
32 @retval FALSE String has at least one other character.
33 **/
34 BOOLEAN
35 EFIAPI
36 IsNumberLetterOnly(
37 IN CONST CHAR16 *String,
38 IN CONST UINTN Len
39 )
40 {
41 UINTN Count;
42 for (Count = 0 ; Count < Len && String != NULL && *String != CHAR_NULL ; String++,Count++) {
43 if (! ((*String >= L'a' && *String <= L'z') ||
44 (*String >= L'A' && *String <= L'Z') ||
45 (*String >= L'0' && *String <= L'9'))
46 ){
47 return (FALSE);
48 }
49 }
50 return (TRUE);
51 }
52
53 /**
54 Do a search in the Target delimited list.
55
56 @param[in] List The list to seatch in.
57 @param[in] MetaTarget The item to search for. MetaMatching supported.
58 @param[out] FullName Optional pointer to an allocated buffer containing
59 the match.
60 @param[in] Meta TRUE to use MetaMatching.
61 @param[in] SkipTrailingNumbers TRUE to allow for numbers after the MetaTarget.
62 @param[in] Target The single character that delimits list
63 items (";" normally).
64 **/
65 BOOLEAN
66 EFIAPI
67 SearchList(
68 IN CONST CHAR16 *List,
69 IN CONST CHAR16 *MetaTarget,
70 OUT CHAR16 **FullName OPTIONAL,
71 IN CONST BOOLEAN Meta,
72 IN CONST BOOLEAN SkipTrailingNumbers,
73 IN CONST CHAR16 *Target
74
75 )
76 {
77 CHAR16 *TempList;
78 CONST CHAR16 *ListWalker;
79 BOOLEAN Result;
80 CHAR16 *TempSpot;
81
82 for (ListWalker = List , TempList = NULL
83 ; ListWalker != NULL && *ListWalker != CHAR_NULL
84 ;
85 ) {
86 TempList = StrnCatGrow(&TempList, NULL, ListWalker, 0);
87 ASSERT(TempList != NULL);
88 TempSpot = StrStr(TempList, Target);
89 if (TempSpot != NULL) {
90 *TempSpot = CHAR_NULL;
91 }
92
93 while (SkipTrailingNumbers && (ShellIsDecimalDigitCharacter(TempList[StrLen(TempList)-1]) || TempList[StrLen(TempList)-1] == L':')) {
94 TempList[StrLen(TempList)-1] = CHAR_NULL;
95 }
96
97 ListWalker = StrStr(ListWalker, Target);
98 while(ListWalker != NULL && *ListWalker == *Target) {
99 ListWalker++;
100 }
101 if (Meta) {
102 Result = gUnicodeCollation->MetaiMatch(gUnicodeCollation, (CHAR16*)TempList, (CHAR16*)MetaTarget);
103 } else {
104 Result = (BOOLEAN)(StrCmp(TempList, MetaTarget)==0);
105 }
106 if (Result) {
107 if (FullName != NULL) {
108 *FullName = TempList;
109 } else {
110 FreePool(TempList);
111 }
112 return (TRUE);
113 }
114 FreePool(TempList);
115 TempList = NULL;
116 }
117
118 return (FALSE);
119 }
120
121 /**
122 Determine what type of device is represented and return it's string. The
123 string is in allocated memory and must be callee freed. The HII is is listed below.
124 The actual string cannot be determined.
125
126 @param[in] DevicePath The device to analyze.
127
128 @retval STR_MAP_MEDIA_UNKNOWN The media type is unknown.
129 @retval STR_MAP_MEDIA_HARDDISK The media is a hard drive.
130 @retval STR_MAP_MEDIA_CDROM The media is a CD ROM.
131 @retval STR_MAP_MEDIA_FLOPPY The media is a floppy drive.
132 **/
133 CHAR16*
134 EFIAPI
135 GetDeviceMediaType (
136 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
137 )
138 {
139 ACPI_HID_DEVICE_PATH *Acpi;
140
141 //
142 // Parse the device path:
143 // Devicepath sub type mediatype
144 // MEDIA_HANRDDRIVE_DP -> Hard Disk
145 // MEDIA_CDROM_DP -> CD Rom
146 // Acpi.HID = 0X0604 -> Floppy
147 //
148 if (NULL == DevicePath) {
149 return HiiGetString(gShellLevel2HiiHandle, STRING_TOKEN(STR_MAP_MEDIA_UNKNOWN), NULL);
150 }
151
152 for (;!IsDevicePathEndType (DevicePath) ;DevicePath = NextDevicePathNode (DevicePath)) {
153 if (DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) {
154 switch (DevicePathSubType (DevicePath)) {
155 case MEDIA_HARDDRIVE_DP:
156 return HiiGetString(gShellLevel2HiiHandle, STRING_TOKEN(STR_MAP_MEDIA_HARDDISK), NULL);
157 case MEDIA_CDROM_DP:
158 return HiiGetString(gShellLevel2HiiHandle, STRING_TOKEN(STR_MAP_MEDIA_CDROM), NULL);
159 }
160 } else if (DevicePathType (DevicePath) == ACPI_DEVICE_PATH) {
161 Acpi = (ACPI_HID_DEVICE_PATH *) DevicePath;
162 if (EISA_ID_TO_NUM (Acpi->HID) == 0x0604) {
163 return HiiGetString(gShellLevel2HiiHandle, STRING_TOKEN(STR_MAP_MEDIA_FLOPPY), NULL);
164 }
165 }
166 }
167
168 return HiiGetString(gShellLevel2HiiHandle, STRING_TOKEN(STR_MAP_MEDIA_UNKNOWN), NULL);
169 }
170
171 /**
172 Function to detemine if a handle has removable storage.
173
174 @param[in] DevicePath DevicePath to test.
175
176 @retval TRUE The handle has removable storage.
177 @retval FALSE The handle does not have removable storage.
178 **/
179 BOOLEAN
180 EFIAPI
181 IsRemoveableDevice (
182 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
183 )
184 {
185 if (NULL == DevicePath) {
186 return FALSE;
187 }
188
189 while (!IsDevicePathEndType (DevicePath)) {
190 if (DevicePathType (DevicePath) == MESSAGING_DEVICE_PATH) {
191 switch (DevicePathSubType (DevicePath)) {
192 case MSG_USB_DP:
193 case MSG_SCSI_DP:
194 return TRUE;
195 default:
196 return FALSE;
197 }
198 }
199 DevicePath = NextDevicePathNode (DevicePath);
200 }
201 return FALSE;
202 }
203
204 /**
205 Function to detemine if a something on the map list matches.
206
207 @param[in] MapList The pointer to the list to test.
208 @param[in] Specific The pointer to a specific name to test for.
209 @param[in] TypeString The pointer to the list of types.
210 @param[in] Normal Always show normal mappings.
211 @param[in] Consist Always show consistent mappings.
212
213 @retval TRUE The map should be displayed.
214 @retval FALSE The map should not be displayed.
215 **/
216 BOOLEAN
217 EFIAPI
218 MappingListHasType(
219 IN CONST CHAR16 *MapList,
220 IN CONST CHAR16 *Specific,
221 IN CONST CHAR16 *TypeString,
222 IN CONST BOOLEAN Normal,
223 IN CONST BOOLEAN Consist
224 )
225 {
226 CHAR16 *NewSpecific;
227 RETURN_STATUS Status;
228
229 //
230 // specific has priority
231 //
232 if (Specific != NULL) {
233 NewSpecific = AllocateCopyPool(StrSize(Specific) + sizeof(CHAR16), Specific);
234 if (NewSpecific == NULL){
235 return FALSE;
236 }
237 if (NewSpecific[StrLen(NewSpecific)-1] != L':') {
238 Status = StrnCatS(NewSpecific, (StrSize(Specific) + sizeof(CHAR16))/sizeof(CHAR16), L":", StrLen(L":"));
239 if (EFI_ERROR (Status)) {
240 FreePool(NewSpecific);
241 return FALSE;
242 }
243 }
244
245 if (SearchList(MapList, NewSpecific, NULL, TRUE, FALSE, L";")) {
246 FreePool(NewSpecific);
247 return (TRUE);
248 }
249 FreePool(NewSpecific);
250 }
251 if ( Consist
252 && Specific == NULL
253 && (SearchList(MapList, L"HD*", NULL, TRUE, TRUE, L";")
254 ||SearchList(MapList, L"CD*", NULL, TRUE, TRUE, L";")
255 ||SearchList(MapList, L"F*", NULL, TRUE, TRUE, L";")
256 ||SearchList(MapList, L"FP*", NULL, TRUE, TRUE, L";"))){
257 return (TRUE);
258 }
259
260 if ( Normal
261 && Specific == NULL
262 && (SearchList(MapList, L"FS", NULL, FALSE, TRUE, L";")
263 ||SearchList(MapList, L"BLK", NULL, FALSE, TRUE, L";"))){
264 return (TRUE);
265 }
266
267 if (TypeString != NULL && SearchList(MapList, TypeString, NULL, TRUE, TRUE, L";")) {
268 return (TRUE);
269 }
270 return (FALSE);
271 }
272
273
274 /**
275 Display a single map line for device Handle if conditions are met.
276
277 @param[in] Verbose TRUE to display (extra) verbose information.
278 @param[in] Consist TRUE to display consistent mappings.
279 @param[in] Normal TRUE to display normal (not consist) mappings.
280 @param[in] TypeString pointer to string of filter types.
281 @param[in] SFO TRUE to display output in Standard Output Format.
282 @param[in] Specific pointer to string for specific map to display.
283 @param[in] Handle The handle to display from.
284
285 @retval EFI_SUCCESS The mapping was displayed.
286 **/
287 EFI_STATUS
288 EFIAPI
289 PerformSingleMappingDisplay(
290 IN CONST BOOLEAN Verbose,
291 IN CONST BOOLEAN Consist,
292 IN CONST BOOLEAN Normal,
293 IN CONST CHAR16 *TypeString,
294 IN CONST BOOLEAN SFO,
295 IN CONST CHAR16 *Specific OPTIONAL,
296 IN CONST EFI_HANDLE Handle
297 )
298 {
299 EFI_DEVICE_PATH_PROTOCOL *DevPath;
300 EFI_DEVICE_PATH_PROTOCOL *DevPathCopy;
301 CONST CHAR16 *MapList;
302 CHAR16 *CurrentName;
303 CHAR16 *MediaType;
304 CHAR16 *DevPathString;
305 CHAR16 *TempSpot;
306 CHAR16 *Alias;
307 UINTN TempLen;
308 BOOLEAN Removable;
309 CONST CHAR16 *TempSpot2;
310
311 Alias = NULL;
312 TempSpot2 = NULL;
313 CurrentName = NULL;
314 DevPath = DevicePathFromHandle(Handle);
315 DevPathCopy = DevPath;
316 MapList = gEfiShellProtocol->GetMapFromDevicePath(&DevPathCopy);
317 if (MapList == NULL) {
318 return EFI_NOT_FOUND;
319 }
320
321 if (!MappingListHasType(MapList, Specific, TypeString, Normal, Consist)){
322 return EFI_NOT_FOUND;
323 }
324
325 if (Normal || !Consist) {
326 //
327 // need the Normal here since people can use both on command line. otherwise unused.
328 //
329
330 //
331 // Allocate a name
332 //
333 CurrentName = NULL;
334 CurrentName = StrnCatGrow(&CurrentName, 0, MapList, 0);
335 if (CurrentName == NULL) {
336 return (EFI_OUT_OF_RESOURCES);
337 }
338
339 //
340 // Chop off the other names that become "Alias(s)"
341 // leaving just the normal name
342 //
343 TempSpot = StrStr(CurrentName, L";");
344 if (TempSpot != NULL) {
345 *TempSpot = CHAR_NULL;
346 }
347 } else {
348 CurrentName = NULL;
349
350 //
351 // Skip the first name. This is the standard name.
352 //
353 TempSpot = StrStr(MapList, L";");
354 if (TempSpot != NULL) {
355 TempSpot++;
356 }
357 SearchList(TempSpot, L"HD*", &CurrentName, TRUE, FALSE, L";");
358 if (CurrentName == NULL) {
359 SearchList(TempSpot, L"CD*", &CurrentName, TRUE, FALSE, L";");
360 }
361 if (CurrentName == NULL) {
362 SearchList(TempSpot, L"FP*", &CurrentName, TRUE, FALSE, L";");
363 }
364 if (CurrentName == NULL) {
365 SearchList(TempSpot, L"F*", &CurrentName, TRUE, FALSE, L";");
366 }
367 if (CurrentName == NULL) {
368 //
369 // We didnt find anything, so just the first one in the list...
370 //
371 CurrentName = StrnCatGrow(&CurrentName, 0, MapList, 0);
372 if (CurrentName == NULL) {
373 return (EFI_OUT_OF_RESOURCES);
374 }
375 TempSpot = StrStr(CurrentName, L";");
376 if (TempSpot != NULL) {
377 *TempSpot = CHAR_NULL;
378 }
379 } else {
380 Alias = StrnCatGrow(&Alias, 0, MapList, 0);
381 if (Alias == NULL) {
382 return EFI_OUT_OF_RESOURCES;
383 }
384 TempSpot = StrStr(Alias, CurrentName);
385 if (TempSpot != NULL) {
386 TempSpot2 = StrStr(TempSpot, L";");
387 if (TempSpot2 != NULL) {
388 TempSpot2++; // Move past ";" from CurrentName
389 CopyMem(TempSpot, TempSpot2, StrSize(TempSpot2));
390 } else {
391 *TempSpot = CHAR_NULL;
392 }
393 }
394 if (Alias[StrLen(Alias)-1] == L';') {
395 Alias[StrLen(Alias)-1] = CHAR_NULL;
396 }
397 }
398 }
399 DevPathString = ConvertDevicePathToText(DevPath, TRUE, FALSE);
400 TempLen = StrLen(CurrentName);
401 if (!SFO) {
402 ShellPrintHiiEx (
403 -1,
404 -1,
405 NULL,
406 STRING_TOKEN (STR_MAP_ENTRY),
407 gShellLevel2HiiHandle,
408 CurrentName,
409 Alias!=NULL?Alias:(TempLen < StrLen(MapList)?MapList + TempLen+1:L""),
410 DevPathString
411 );
412 if (Verbose) {
413 //
414 // also print handle, media type, removable (y/n), and current directory
415 //
416 MediaType = GetDeviceMediaType(DevPath);
417 if ((TypeString != NULL &&MediaType != NULL && StrStr(TypeString, MediaType) != NULL) || TypeString == NULL) {
418 Removable = IsRemoveableDevice(DevPath);
419 TempSpot2 = ShellGetCurrentDir(CurrentName);
420 ShellPrintHiiEx (
421 -1,
422 -1,
423 NULL,
424 STRING_TOKEN (STR_MAP_ENTRY_VERBOSE),
425 gShellLevel2HiiHandle,
426 ConvertHandleToHandleIndex(Handle),
427 MediaType,
428 Removable?L"Yes":L"No",
429 TempSpot2
430 );
431 }
432 SHELL_FREE_NON_NULL(MediaType);
433 }
434 } else {
435 ShellPrintHiiEx (
436 -1,
437 -1,
438 NULL,
439 STRING_TOKEN (STR_MAP_SFO_MAPPINGS),
440 gShellLevel2HiiHandle,
441 CurrentName,
442 DevPathString,
443 Consist?L"":(TempLen < StrLen(MapList)?MapList + TempLen+1:L"")
444 );
445 }
446 SHELL_FREE_NON_NULL(DevPathString);
447 SHELL_FREE_NON_NULL(CurrentName);
448 SHELL_FREE_NON_NULL(Alias);
449 return EFI_SUCCESS;
450 }
451
452 /**
453 Delete Specific from the list of maps for device Handle.
454
455 @param[in] Specific The name to delete.
456 @param[in] Handle The device to look on.
457
458 @retval EFI_SUCCESS The delete was successful.
459 @retval EFI_NOT_FOUND Name was not a map on Handle.
460 **/
461 EFI_STATUS
462 EFIAPI
463 PerformSingleMappingDelete(
464 IN CONST CHAR16 *Specific,
465 IN CONST EFI_HANDLE Handle
466 )
467 {
468 EFI_DEVICE_PATH_PROTOCOL *DevPath;
469 EFI_DEVICE_PATH_PROTOCOL *DevPathCopy;
470 CONST CHAR16 *MapList;
471 CHAR16 *CurrentName;
472
473 DevPath = DevicePathFromHandle(Handle);
474 DevPathCopy = DevPath;
475 MapList = gEfiShellProtocol->GetMapFromDevicePath(&DevPathCopy);
476 CurrentName = NULL;
477
478 if (MapList == NULL) {
479 return (EFI_NOT_FOUND);
480 }
481 //
482 // if there is a specific and its not on the list...
483 //
484 if (!SearchList(MapList, Specific, &CurrentName, TRUE, FALSE, L";")) {
485 return (EFI_NOT_FOUND);
486 }
487 return (gEfiShellProtocol->SetMap(NULL, CurrentName));
488 }
489
490 CONST CHAR16 Cd[] = L"cd*";
491 CONST CHAR16 Hd[] = L"hd*";
492 CONST CHAR16 Fp[] = L"fp*";
493 CONST CHAR16 AnyF[] = L"F*";
494 /**
495 Function to display mapping information to the user.
496
497 If Specific is specified then Consist and Normal will be ignored since information will
498 be printed for the specific item only.
499
500 @param[in] Verbose TRUE to display (extra) verbose information.
501 @param[in] Consist TRUE to display consistent mappings.
502 @param[in] Normal TRUE to display normal (not consist) mappings.
503 @param[in] TypeString Pointer to string of filter types.
504 @param[in] SFO TRUE to display output in Standard Output Format.
505 @param[in] Specific Pointer to string for specific map to display.
506 @param[in] Header TRUE to print the header block.
507
508 @retval SHELL_SUCCESS The display was printed.
509 @retval SHELL_INVALID_PARAMETER One of Consist or Normal must be TRUE if no Specific.
510
511 **/
512 SHELL_STATUS
513 EFIAPI
514 PerformMappingDisplay(
515 IN CONST BOOLEAN Verbose,
516 IN CONST BOOLEAN Consist,
517 IN CONST BOOLEAN Normal,
518 IN CONST CHAR16 *TypeString,
519 IN CONST BOOLEAN SFO,
520 IN CONST CHAR16 *Specific OPTIONAL,
521 IN CONST BOOLEAN Header
522 )
523 {
524 EFI_STATUS Status;
525 EFI_HANDLE *HandleBuffer;
526 UINTN BufferSize;
527 UINTN LoopVar;
528 CHAR16 *Test;
529 BOOLEAN Found;
530
531 if (!Consist && !Normal && Specific == NULL && TypeString == NULL) {
532 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle, L"map");
533 return (SHELL_INVALID_PARAMETER);
534 }
535
536 if (TypeString != NULL) {
537 Test = (CHAR16*)Cd;
538 if (StrnCmp(TypeString, Test, StrLen(Test)-1) != 0) {
539 Test = (CHAR16*)Hd;
540 if (StrnCmp(TypeString, Test, StrLen(Test)-1) != 0) {
541 Test = (CHAR16*)Fp;
542 if (StrnCmp(TypeString, Test, StrLen(Test)-1) != 0) {
543 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellLevel2HiiHandle, L"map", TypeString);
544 return (SHELL_INVALID_PARAMETER);
545 }
546 } else if (Test == NULL) {
547 Test = (CHAR16*)AnyF;
548 }
549 }
550 } else {
551 Test = NULL;
552 }
553
554 if (Header) {
555 //
556 // Print the header
557 //
558 if (!SFO) {
559 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MAP_HEADER), gShellLevel2HiiHandle);
560 } else {
561 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_SFO_HEADER), gShellLevel2HiiHandle, L"map");
562 }
563 }
564
565 BufferSize = 0;
566 HandleBuffer = NULL;
567
568 //
569 // Look up all SimpleFileSystems in the platform
570 //
571 Status = gBS->LocateHandle(
572 ByProtocol,
573 &gEfiSimpleFileSystemProtocolGuid,
574 NULL,
575 &BufferSize,
576 HandleBuffer);
577 if (Status == EFI_BUFFER_TOO_SMALL) {
578 HandleBuffer = AllocateZeroPool(BufferSize);
579 if (HandleBuffer == NULL) {
580 return (SHELL_OUT_OF_RESOURCES);
581 }
582 Status = gBS->LocateHandle(
583 ByProtocol,
584 &gEfiSimpleFileSystemProtocolGuid,
585 NULL,
586 &BufferSize,
587 HandleBuffer);
588 }
589
590 //
591 // Get the map name(s) for each one.
592 //
593 for ( LoopVar = 0, Found = FALSE
594 ; LoopVar < (BufferSize / sizeof(EFI_HANDLE)) && HandleBuffer != NULL
595 ; LoopVar ++
596 ){
597 Status = PerformSingleMappingDisplay(
598 Verbose,
599 Consist,
600 Normal,
601 Test,
602 SFO,
603 Specific,
604 HandleBuffer[LoopVar]);
605 if (!EFI_ERROR(Status)) {
606 Found = TRUE;
607 }
608 }
609
610 //
611 // Look up all BlockIo in the platform
612 //
613 Status = gBS->LocateHandle(
614 ByProtocol,
615 &gEfiBlockIoProtocolGuid,
616 NULL,
617 &BufferSize,
618 HandleBuffer);
619 if (Status == EFI_BUFFER_TOO_SMALL) {
620 SHELL_FREE_NON_NULL(HandleBuffer);
621 HandleBuffer = AllocateZeroPool(BufferSize);
622 if (HandleBuffer == NULL) {
623 return (SHELL_OUT_OF_RESOURCES);
624 }
625 Status = gBS->LocateHandle(
626 ByProtocol,
627 &gEfiBlockIoProtocolGuid,
628 NULL,
629 &BufferSize,
630 HandleBuffer);
631 }
632 if (!EFI_ERROR(Status) && HandleBuffer != NULL) {
633 //
634 // Get the map name(s) for each one.
635 //
636 for ( LoopVar = 0
637 ; LoopVar < BufferSize / sizeof(EFI_HANDLE)
638 ; LoopVar ++
639 ){
640 //
641 // Skip any that were already done...
642 //
643 if (gBS->OpenProtocol(
644 HandleBuffer[LoopVar],
645 &gEfiSimpleFileSystemProtocolGuid,
646 NULL,
647 gImageHandle,
648 NULL,
649 EFI_OPEN_PROTOCOL_TEST_PROTOCOL) == EFI_SUCCESS) {
650 continue;
651 }
652 Status = PerformSingleMappingDisplay(
653 Verbose,
654 Consist,
655 Normal,
656 Test,
657 SFO,
658 Specific,
659 HandleBuffer[LoopVar]);
660 if (!EFI_ERROR(Status)) {
661 Found = TRUE;
662 }
663 }
664 FreePool(HandleBuffer);
665 }
666 if (!Found) {
667 if (Specific != NULL) {
668 ShellPrintHiiEx(gST->ConOut->Mode->CursorColumn, gST->ConOut->Mode->CursorRow-1, NULL, STRING_TOKEN (STR_MAP_NF), gShellLevel2HiiHandle, L"map", Specific);
669 } else {
670 ShellPrintHiiEx(gST->ConOut->Mode->CursorColumn, gST->ConOut->Mode->CursorRow-1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle, L"map");
671 }
672 }
673 return (SHELL_SUCCESS);
674 }
675
676 /**
677 Perform a mapping display and parse for multiple types in the TypeString.
678
679 @param[in] Verbose TRUE to use verbose output.
680 @param[in] Consist TRUE to display consistent names.
681 @param[in] Normal TRUE to display normal names.
682 @param[in] TypeString An optional comma-delimited list of types.
683 @param[in] SFO TRUE to display in SFO format. See Spec.
684 @param[in] Specific An optional specific map name to display alone.
685
686 @retval SHELL_INVALID_PARAMETER A parameter was invalid.
687 @retval SHELL_SUCCESS The display was successful.
688 @sa PerformMappingDisplay
689 **/
690 SHELL_STATUS
691 EFIAPI
692 PerformMappingDisplay2(
693 IN CONST BOOLEAN Verbose,
694 IN CONST BOOLEAN Consist,
695 IN CONST BOOLEAN Normal,
696 IN CONST CHAR16 *TypeString,
697 IN CONST BOOLEAN SFO,
698 IN CONST CHAR16 *Specific OPTIONAL
699 )
700 {
701 CONST CHAR16 *TypeWalker;
702 SHELL_STATUS ShellStatus;
703 CHAR16 *Comma;
704
705
706 if (TypeString == NULL) {
707 return (PerformMappingDisplay(Verbose, Consist, Normal, NULL, SFO, Specific, TRUE));
708 }
709 ShellStatus = SHELL_SUCCESS;
710 for (TypeWalker = TypeString ; TypeWalker != NULL && *TypeWalker != CHAR_NULL ;) {
711 Comma = StrStr(TypeWalker, L",");
712 if (Comma == NULL) {
713 if (ShellStatus == SHELL_SUCCESS) {
714 ShellStatus = PerformMappingDisplay(Verbose, Consist, Normal, TypeWalker, SFO, Specific, (BOOLEAN)(TypeWalker == TypeString));
715 } else {
716 PerformMappingDisplay(Verbose, Consist, Normal, TypeWalker, SFO, Specific, (BOOLEAN)(TypeWalker == TypeString));
717 }
718 break;
719 } else {
720 *Comma = CHAR_NULL;
721 if (ShellStatus == SHELL_SUCCESS) {
722 ShellStatus = PerformMappingDisplay(Verbose, Consist, Normal, TypeWalker, SFO, Specific, (BOOLEAN)(TypeWalker == TypeString));
723 } else {
724 PerformMappingDisplay(Verbose, Consist, Normal, TypeWalker, SFO, Specific, (BOOLEAN)(TypeWalker == TypeString));
725 }
726 *Comma = L',';
727 TypeWalker = Comma + 1;
728 }
729 }
730
731 return (ShellStatus);
732 }
733
734 /**
735 Delete a specific map.
736
737 @param[in] Specific The pointer to the name of the map to delete.
738
739 @retval EFI_INVALID_PARAMETER Specific was NULL.
740 @retval EFI_SUCCESS The operation was successful.
741 @retval EFI_NOT_FOUND Specific could not be found.
742 **/
743 EFI_STATUS
744 EFIAPI
745 PerformMappingDelete(
746 IN CONST CHAR16 *Specific
747 )
748 {
749 EFI_STATUS Status;
750 EFI_HANDLE *HandleBuffer;
751 UINTN BufferSize;
752 UINTN LoopVar;
753 BOOLEAN Deleted;
754
755 if (Specific == NULL) {
756 return (EFI_INVALID_PARAMETER);
757 }
758
759 BufferSize = 0;
760 HandleBuffer = NULL;
761 Deleted = FALSE;
762
763 //
764 // Look up all SimpleFileSystems in the platform
765 //
766 Status = gBS->LocateHandle(
767 ByProtocol,
768 &gEfiDevicePathProtocolGuid,
769 NULL,
770 &BufferSize,
771 HandleBuffer);
772 if (Status == EFI_BUFFER_TOO_SMALL) {
773 HandleBuffer = AllocateZeroPool(BufferSize);
774 if (HandleBuffer == NULL) {
775 return (EFI_OUT_OF_RESOURCES);
776 }
777 Status = gBS->LocateHandle(
778 ByProtocol,
779 &gEfiDevicePathProtocolGuid,
780 NULL,
781 &BufferSize,
782 HandleBuffer);
783 }
784 if (EFI_ERROR(Status)) {
785 SHELL_FREE_NON_NULL(HandleBuffer);
786 return (Status);
787 }
788
789 if (HandleBuffer != NULL) {
790 //
791 // Get the map name(s) for each one.
792 //
793 for ( LoopVar = 0
794 ; LoopVar < BufferSize / sizeof(EFI_HANDLE)
795 ; LoopVar ++
796 ){
797 if (PerformSingleMappingDelete(Specific,HandleBuffer[LoopVar]) == SHELL_SUCCESS) {
798 Deleted = TRUE;
799 }
800 }
801 }
802 //
803 // Look up all BlockIo in the platform
804 //
805 Status = gBS->LocateHandle(
806 ByProtocol,
807 &gEfiBlockIoProtocolGuid,
808 NULL,
809 &BufferSize,
810 HandleBuffer);
811 if (Status == EFI_BUFFER_TOO_SMALL) {
812 FreePool(HandleBuffer);
813 HandleBuffer = AllocateZeroPool(BufferSize);
814 if (HandleBuffer == NULL) {
815 return (EFI_OUT_OF_RESOURCES);
816 }
817 Status = gBS->LocateHandle(
818 ByProtocol,
819 &gEfiBlockIoProtocolGuid,
820 NULL,
821 &BufferSize,
822 HandleBuffer);
823 }
824 if (EFI_ERROR(Status)) {
825 SHELL_FREE_NON_NULL(HandleBuffer);
826 return (Status);
827 }
828
829 if (HandleBuffer != NULL) {
830 //
831 // Get the map name(s) for each one.
832 //
833 for ( LoopVar = 0
834 ; LoopVar < BufferSize / sizeof(EFI_HANDLE)
835 ; LoopVar ++
836 ){
837 //
838 // Skip any that were already done...
839 //
840 if (gBS->OpenProtocol(
841 HandleBuffer[LoopVar],
842 &gEfiDevicePathProtocolGuid,
843 NULL,
844 gImageHandle,
845 NULL,
846 EFI_OPEN_PROTOCOL_TEST_PROTOCOL) == EFI_SUCCESS) {
847 continue;
848 }
849 if (PerformSingleMappingDelete(Specific,HandleBuffer[LoopVar]) == SHELL_SUCCESS) {
850 Deleted = TRUE;
851 }
852 }
853 }
854 SHELL_FREE_NON_NULL(HandleBuffer);
855 if (!Deleted) {
856 return (EFI_NOT_FOUND);
857 }
858 return (EFI_SUCCESS);
859 }
860
861 /**
862 function to add a mapping from mapping.
863
864 This function will get the device path associated with the mapping and call SetMap.
865
866 @param[in] Map The Map to add a mapping for
867 @param[in] SName The name of the new mapping
868
869 @retval SHELL_SUCCESS the mapping was added
870 @retval SHELL_INVALID_PARAMETER the device path for Map could not be retrieved.
871 @return Shell version of a return value from EfiShellProtocol->SetMap
872
873 **/
874 SHELL_STATUS
875 EFIAPI
876 AddMappingFromMapping(
877 IN CONST CHAR16 *Map,
878 IN CONST CHAR16 *SName
879 )
880 {
881 CONST EFI_DEVICE_PATH_PROTOCOL *DevPath;
882 EFI_STATUS Status;
883 CHAR16 *NewSName;
884 RETURN_STATUS StrRetStatus;
885
886 NewSName = AllocateCopyPool(StrSize(SName) + sizeof(CHAR16), SName);
887 if (NewSName == NULL) {
888 return (SHELL_OUT_OF_RESOURCES);
889 }
890 if (NewSName[StrLen(NewSName)-1] != L':') {
891 StrRetStatus = StrnCatS(NewSName, (StrSize(SName) + sizeof(CHAR16))/sizeof(CHAR16), L":", StrLen(L":"));
892 if (EFI_ERROR(StrRetStatus)) {
893 FreePool(NewSName);
894 return ((SHELL_STATUS) (StrRetStatus & (~MAX_BIT)));
895 }
896 }
897
898 if (!IsNumberLetterOnly(NewSName, StrLen(NewSName)-1)) {
899 FreePool(NewSName);
900 return (SHELL_INVALID_PARAMETER);
901 }
902
903 DevPath = gEfiShellProtocol->GetDevicePathFromMap(Map);
904 if (DevPath == NULL) {
905 FreePool(NewSName);
906 return (SHELL_INVALID_PARAMETER);
907 }
908
909 Status = gEfiShellProtocol->SetMap(DevPath, NewSName);
910 FreePool(NewSName);
911 if (EFI_ERROR(Status)) {
912 return (SHELL_DEVICE_ERROR);
913 }
914 return (SHELL_SUCCESS);
915 }
916
917 /**
918 function to add a mapping from an EFI_HANDLE.
919
920 This function will get the device path associated with the Handle and call SetMap.
921
922 @param[in] Handle The handle to add a mapping for
923 @param[in] SName The name of the new mapping
924
925 @retval SHELL_SUCCESS the mapping was added
926 @retval SHELL_INVALID_PARAMETER SName was not valid for a map name.
927 @return Shell version of a return value from either
928 gBS->OpenProtocol or EfiShellProtocol->SetMap
929
930 **/
931 SHELL_STATUS
932 EFIAPI
933 AddMappingFromHandle(
934 IN CONST EFI_HANDLE Handle,
935 IN CONST CHAR16 *SName
936 )
937 {
938 EFI_DEVICE_PATH_PROTOCOL *DevPath;
939 EFI_STATUS Status;
940 CHAR16 *NewSName;
941 RETURN_STATUS StrRetStatus;
942
943 NewSName = AllocateCopyPool(StrSize(SName) + sizeof(CHAR16), SName);
944 if (NewSName == NULL) {
945 return (SHELL_OUT_OF_RESOURCES);
946 }
947 if (NewSName[StrLen(NewSName)-1] != L':') {
948 StrRetStatus = StrnCatS(NewSName, (StrSize(SName) + sizeof(CHAR16))/sizeof(CHAR16), L":", StrLen(L":"));
949 if (EFI_ERROR(StrRetStatus)) {
950 FreePool(NewSName);
951 return ((SHELL_STATUS) (StrRetStatus & (~MAX_BIT)));
952 }
953 }
954
955 if (!IsNumberLetterOnly(NewSName, StrLen(NewSName)-1)) {
956 FreePool(NewSName);
957 return (SHELL_INVALID_PARAMETER);
958 }
959
960 Status = gBS->OpenProtocol(
961 Handle,
962 &gEfiDevicePathProtocolGuid,
963 (VOID**)&DevPath,
964 gImageHandle,
965 NULL,
966 EFI_OPEN_PROTOCOL_GET_PROTOCOL
967 );
968 if (EFI_ERROR(Status)) {
969 FreePool(NewSName);
970 return (SHELL_DEVICE_ERROR);
971 }
972 Status = gEfiShellProtocol->SetMap(DevPath, NewSName);
973 FreePool(NewSName);
974 if (EFI_ERROR(Status)) {
975 return (SHELL_DEVICE_ERROR);
976 }
977 return (SHELL_SUCCESS);
978 }
979
980 STATIC CONST SHELL_PARAM_ITEM MapParamList[] = {
981 {L"-d", TypeValue},
982 {L"-r", TypeFlag},
983 {L"-v", TypeFlag},
984 {L"-c", TypeFlag},
985 {L"-f", TypeFlag},
986 {L"-u", TypeFlag},
987 {L"-t", TypeValue},
988 {L"-sfo", TypeValue},
989 {NULL, TypeMax}
990 };
991
992 /**
993 Function for 'map' command.
994
995 @param[in] ImageHandle Handle to the Image (NULL if Internal).
996 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
997 **/
998 SHELL_STATUS
999 EFIAPI
1000 ShellCommandRunMap (
1001 IN EFI_HANDLE ImageHandle,
1002 IN EFI_SYSTEM_TABLE *SystemTable
1003 )
1004 {
1005 EFI_STATUS Status;
1006 LIST_ENTRY *Package;
1007 CHAR16 *ProblemParam;
1008 CONST CHAR16 *SName;
1009 CONST CHAR16 *Mapping;
1010 EFI_HANDLE MapAsHandle;
1011 SHELL_STATUS ShellStatus;
1012 BOOLEAN SfoMode;
1013 BOOLEAN ConstMode;
1014 BOOLEAN NormlMode;
1015 CONST CHAR16 *Param1;
1016 CONST CHAR16 *TypeString;
1017 UINTN TempStringLength;
1018
1019 ProblemParam = NULL;
1020 Mapping = NULL;
1021 SName = NULL;
1022 ShellStatus = SHELL_SUCCESS;
1023 MapAsHandle = NULL;
1024
1025 //
1026 // initialize the shell lib (we must be in non-auto-init...)
1027 //
1028 Status = ShellInitialize();
1029 ASSERT_EFI_ERROR(Status);
1030
1031 Status = CommandInit();
1032 ASSERT_EFI_ERROR(Status);
1033
1034 //
1035 // parse the command line
1036 //
1037 Status = ShellCommandLineParse (MapParamList, &Package, &ProblemParam, TRUE);
1038 if (EFI_ERROR(Status)) {
1039 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
1040 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, L"map", ProblemParam);
1041 FreePool(ProblemParam);
1042 ShellStatus = SHELL_INVALID_PARAMETER;
1043 } else {
1044 ASSERT(FALSE);
1045 }
1046 } else {
1047 //
1048 // check for "-?"
1049 //
1050 SfoMode = ShellCommandLineGetFlag(Package, L"-sfo");
1051 ConstMode = ShellCommandLineGetFlag(Package, L"-c");
1052 NormlMode = ShellCommandLineGetFlag(Package, L"-f");
1053 if (ShellCommandLineGetFlag(Package, L"-?")) {
1054 ASSERT(FALSE);
1055 } else if (ShellCommandLineGetRawValue(Package, 3) != NULL) {
1056 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle, L"map");
1057 ShellStatus = SHELL_INVALID_PARAMETER;
1058 } else {
1059 //
1060 // Deleting a map name...
1061 //
1062 if (ShellCommandLineGetFlag(Package, L"-d")) {
1063 if ( ShellCommandLineGetFlag(Package, L"-r")
1064 || ShellCommandLineGetFlag(Package, L"-v")
1065 || ConstMode
1066 || NormlMode
1067 || ShellCommandLineGetFlag(Package, L"-u")
1068 || ShellCommandLineGetFlag(Package, L"-t")
1069 ){
1070 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_CON), gShellLevel2HiiHandle, L"map");
1071 ShellStatus = SHELL_INVALID_PARAMETER;
1072 } else {
1073 SName = ShellCommandLineGetValue(Package, L"-d");
1074 if (SName != NULL) {
1075 Status = PerformMappingDelete(SName);
1076 if (EFI_ERROR(Status)) {
1077 if (Status == EFI_ACCESS_DENIED) {
1078 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_AD), gShellLevel2HiiHandle, L"map");
1079 ShellStatus = SHELL_ACCESS_DENIED;
1080 } else if (Status == EFI_NOT_FOUND) {
1081 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MAP_NF), gShellLevel2HiiHandle, L"map", SName);
1082 ShellStatus = SHELL_INVALID_PARAMETER;
1083 } else {
1084 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_UK), gShellLevel2HiiHandle, L"map", Status);
1085 ShellStatus = SHELL_UNSUPPORTED;
1086 }
1087 }
1088 } else {
1089 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle, L"map");
1090 ShellStatus = SHELL_INVALID_PARAMETER;
1091 }
1092 }
1093 } else if ( ShellCommandLineGetFlag(Package, L"-r")
1094 // || ShellCommandLineGetFlag(Package, L"-v")
1095 || ConstMode
1096 || NormlMode
1097 || ShellCommandLineGetFlag(Package, L"-u")
1098 || ShellCommandLineGetFlag(Package, L"-t")
1099 ){
1100 if ( ShellCommandLineGetFlag(Package, L"-r")) {
1101 //
1102 // Do the reset
1103 //
1104 Status = ShellCommandCreateInitialMappingsAndPaths();
1105 if (EFI_ERROR(Status)) {
1106 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_UK), gShellLevel2HiiHandle, L"map", Status);
1107 ShellStatus = SHELL_UNSUPPORTED;
1108 }
1109 }
1110 if ( ShellStatus == SHELL_SUCCESS && ShellCommandLineGetFlag(Package, L"-u")) {
1111 //
1112 // Do the Update
1113 //
1114 Status = ShellCommandUpdateMapping ();
1115 if (EFI_ERROR(Status)) {
1116 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_UK), gShellLevel2HiiHandle, L"map", Status);
1117 ShellStatus = SHELL_UNSUPPORTED;
1118 }
1119 }
1120 if (ShellStatus == SHELL_SUCCESS) {
1121 Param1 = ShellCommandLineGetRawValue(Package, 1);
1122 TypeString = ShellCommandLineGetValue(Package, L"-t");
1123 if (!ConstMode
1124 &&!NormlMode
1125 &&TypeString == NULL
1126 ) {
1127 //
1128 // now do the display...
1129 //
1130 ShellStatus = PerformMappingDisplay(
1131 ShellCommandLineGetFlag(Package, L"-v"),
1132 TRUE,
1133 TRUE,
1134 NULL,
1135 SfoMode,
1136 Param1,
1137 TRUE
1138 );
1139 } else {
1140 //
1141 // now do the display...
1142 //
1143 ShellStatus = PerformMappingDisplay2(
1144 ShellCommandLineGetFlag(Package, L"-v"),
1145 ConstMode,
1146 NormlMode,
1147 TypeString,
1148 SfoMode,
1149 Param1
1150 );
1151 }
1152 }
1153 } else {
1154 //
1155 // adding or displaying (there were no flags)
1156 //
1157 SName = ShellCommandLineGetRawValue(Package, 1);
1158 Mapping = ShellCommandLineGetRawValue(Package, 2);
1159 if ( SName == NULL
1160 && Mapping == NULL
1161 ){
1162 //
1163 // display only since no flags
1164 //
1165 ShellStatus = PerformMappingDisplay(
1166 ShellCommandLineGetFlag(Package, L"-v"),
1167 TRUE,
1168 TRUE,
1169 NULL,
1170 SfoMode,
1171 NULL,
1172 TRUE
1173 );
1174 } else if ( SName == NULL
1175 || Mapping == NULL
1176 ){
1177 //
1178 // Display only the one specified
1179 //
1180 ShellStatus = PerformMappingDisplay(
1181 FALSE,
1182 FALSE,
1183 FALSE,
1184 NULL,
1185 SfoMode,
1186 SName, // note the variable here...
1187 TRUE
1188 );
1189 } else {
1190 if (ShellIsHexOrDecimalNumber(Mapping, TRUE, FALSE)) {
1191 MapAsHandle = ConvertHandleIndexToHandle(ShellStrToUintn(Mapping));
1192 } else {
1193 MapAsHandle = NULL;
1194 }
1195 if (MapAsHandle == NULL && Mapping[StrLen(Mapping)-1] != L':') {
1196 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellLevel2HiiHandle, L"map", Mapping);
1197 ShellStatus = SHELL_INVALID_PARAMETER;
1198 } else {
1199 if (MapAsHandle != NULL) {
1200 TempStringLength = StrLen(SName);
1201 if (!IsNumberLetterOnly(SName, TempStringLength-(SName[TempStringLength-1]==L':'?1:0))) {
1202 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellLevel2HiiHandle, L"map", SName);
1203 ShellStatus = SHELL_INVALID_PARAMETER;
1204 } else {
1205 ShellStatus = AddMappingFromHandle(MapAsHandle, SName);
1206 }
1207 } else {
1208 TempStringLength = StrLen(SName);
1209 if (!IsNumberLetterOnly(SName, TempStringLength-(SName[TempStringLength-1]==L':'?1:0))) {
1210 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellLevel2HiiHandle, L"map", SName);
1211 ShellStatus = SHELL_INVALID_PARAMETER;
1212 } else {
1213 ShellStatus = AddMappingFromMapping(Mapping, SName);
1214 }
1215 }
1216 if (ShellStatus != SHELL_SUCCESS) {
1217 switch (ShellStatus) {
1218 case SHELL_ACCESS_DENIED:
1219 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_AD), gShellLevel2HiiHandle, L"map");
1220 break;
1221 case SHELL_INVALID_PARAMETER:
1222 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellLevel2HiiHandle, L"map", Mapping);
1223 break;
1224 case SHELL_DEVICE_ERROR:
1225 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MAP_NOF), gShellLevel2HiiHandle, L"map", Mapping);
1226 break;
1227 default:
1228 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_UK), gShellLevel2HiiHandle, L"map", ShellStatus|MAX_BIT);
1229 }
1230 } else {
1231 //
1232 // now do the display...
1233 //
1234 ShellStatus = PerformMappingDisplay(
1235 FALSE,
1236 FALSE,
1237 FALSE,
1238 NULL,
1239 SfoMode,
1240 SName,
1241 TRUE
1242 );
1243 } // we were sucessful so do an output
1244 } // got a valid map target
1245 } // got 2 variables
1246 } // we are adding a mapping
1247 } // got valid parameters
1248 }
1249
1250 //
1251 // free the command line package
1252 //
1253 ShellCommandLineFreeVarList (Package);
1254
1255 return (ShellStatus);
1256 }
1257