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