]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellLevel2CommandsLib/Map.c
Update all the code to consume the ConvertDevicePathToText, ConvertDevicePathNodeToTe...
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Map.c
1 /** @file
2 Main file for map shell level 2 command.
3
4 Copyright (c) 2009 - 2013, 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"F*", 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 if (CurrentName == NULL) {
418 return (EFI_OUT_OF_RESOURCES);
419 }
420 TempSpot = StrStr(CurrentName, L";");
421 if (TempSpot != NULL) {
422 *TempSpot = CHAR_NULL;
423 }
424 DevPathString = ConvertDevicePathToText(DevPath, TRUE, FALSE);
425 if (!SFO) {
426 TempLen = StrLen(CurrentName);
427 ShellPrintHiiEx (
428 -1,
429 -1,
430 NULL,
431 STRING_TOKEN (STR_MAP_ENTRY),
432 gShellLevel2HiiHandle,
433 CurrentName,
434 TempLen < StrLen(MapList)?MapList + TempLen+1:L"",
435 DevPathString
436 );
437 if (Verbose) {
438 //
439 // also print handle, media type, removable (y/n), and current directory
440 //
441 MediaType = GetDeviceMediaType(DevPath);
442 if ((TypeString != NULL &&MediaType != NULL && StrStr(TypeString, MediaType) != NULL) || TypeString == NULL) {
443 Removable = IsRemoveableDevice(DevPath);
444 TempSpot2 = ShellGetCurrentDir(CurrentName);
445 ShellPrintHiiEx (
446 -1,
447 -1,
448 NULL,
449 STRING_TOKEN (STR_MAP_ENTRY_VERBOSE),
450 gShellLevel2HiiHandle,
451 ConvertHandleToHandleIndex(Handle),
452 MediaType,
453 Removable?L"Yes":L"No",
454 TempSpot2
455 );
456 }
457 FreePool(MediaType);
458 }
459 } else {
460 TempLen = StrLen(CurrentName);
461 ShellPrintHiiEx (
462 -1,
463 -1,
464 NULL,
465 STRING_TOKEN (STR_MAP_SFO_MAPPINGS),
466 gShellLevel2HiiHandle,
467 CurrentName,
468 DevPathString,
469 TempLen < StrLen(MapList)?MapList + TempLen+1:L""
470 );
471 }
472 FreePool(DevPathString);
473 FreePool(CurrentName);
474 return EFI_SUCCESS;
475 }
476
477 /**
478 Delete Specific from the list of maps for device Handle.
479
480 @param[in] Specific The name to delete.
481 @param[in] Handle The device to look on.
482
483 @retval EFI_SUCCESS The delete was successful.
484 @retval EFI_NOT_FOUND Name was not a map on Handle.
485 **/
486 EFI_STATUS
487 EFIAPI
488 PerformSingleMappingDelete(
489 IN CONST CHAR16 *Specific,
490 IN CONST EFI_HANDLE Handle
491 )
492 {
493 EFI_DEVICE_PATH_PROTOCOL *DevPath;
494 EFI_DEVICE_PATH_PROTOCOL *DevPathCopy;
495 CONST CHAR16 *MapList;
496 CHAR16 *CurrentName;
497
498 DevPath = DevicePathFromHandle(Handle);
499 DevPathCopy = DevPath;
500 MapList = gEfiShellProtocol->GetMapFromDevicePath(&DevPathCopy);
501 CurrentName = NULL;
502
503 if (MapList == NULL) {
504 return (EFI_NOT_FOUND);
505 }
506 //
507 // if there is a specific and its not on the list...
508 //
509 if (!SearchList(MapList, Specific, &CurrentName, TRUE, FALSE, L";")) {
510 return (EFI_NOT_FOUND);
511 }
512 return (gEfiShellProtocol->SetMap(NULL, CurrentName));
513 }
514
515 CONST CHAR16 Cd[] = L"cd*";
516 CONST CHAR16 Hd[] = L"hd*";
517 CONST CHAR16 Fp[] = L"fp*";
518 CONST CHAR16 AnyF[] = L"F*";
519 /**
520 Function to display mapping information to the user.
521
522 If Specific is specified then Consist and Normal will be ignored since information will
523 be printed for the specific item only.
524
525 @param[in] Verbose TRUE to display (extra) verbose information.
526 @param[in] Consist TRUE to display consistent mappings.
527 @param[in] Normal TRUE to display normal (not consist) mappings.
528 @param[in] TypeString Pointer to string of filter types.
529 @param[in] SFO TRUE to display output in Standard Output Format.
530 @param[in] Specific Pointer to string for specific map to display.
531 @param[in] Header TRUE to print the header block.
532
533 @retval SHELL_SUCCESS The display was printed.
534 @retval SHELL_INVALID_PARAMETER One of Consist or Normal must be TRUE if no Specific.
535
536 **/
537 SHELL_STATUS
538 EFIAPI
539 PerformMappingDisplay(
540 IN CONST BOOLEAN Verbose,
541 IN CONST BOOLEAN Consist,
542 IN CONST BOOLEAN Normal,
543 IN CONST CHAR16 *TypeString,
544 IN CONST BOOLEAN SFO,
545 IN CONST CHAR16 *Specific OPTIONAL,
546 IN CONST BOOLEAN Header
547 )
548 {
549 EFI_STATUS Status;
550 EFI_HANDLE *HandleBuffer;
551 UINTN BufferSize;
552 UINTN LoopVar;
553 CHAR16 *Test;
554 BOOLEAN Found;
555
556 if (!Consist && !Normal && Specific == NULL && TypeString == NULL) {
557 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle);
558 return (SHELL_INVALID_PARAMETER);
559 }
560
561 if (TypeString != NULL) {
562 Test = (CHAR16*)Cd;
563 if (StrnCmp(TypeString, Test, StrLen(Test)-1) != 0) {
564 Test = (CHAR16*)Hd;
565 if (StrnCmp(TypeString, Test, StrLen(Test)-1) != 0) {
566 Test = (CHAR16*)Fp;
567 if (StrnCmp(TypeString, Test, StrLen(Test)-1) != 0) {
568 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, TypeString);
569 return (SHELL_INVALID_PARAMETER);
570 }
571 } else if (Test == NULL) {
572 Test = (CHAR16*)AnyF;
573 }
574 }
575 } else {
576 Test = NULL;
577 }
578
579 if (Header) {
580 //
581 // Print the header
582 //
583 if (!SFO) {
584 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MAP_HEADER), gShellLevel2HiiHandle);
585 } else {
586 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_SFO_HEADER), gShellLevel2HiiHandle, L"map");
587 }
588 }
589
590 BufferSize = 0;
591 HandleBuffer = NULL;
592
593 //
594 // Look up all SimpleFileSystems in the platform
595 //
596 Status = gBS->LocateHandle(
597 ByProtocol,
598 &gEfiSimpleFileSystemProtocolGuid,
599 NULL,
600 &BufferSize,
601 HandleBuffer);
602 if (Status == EFI_BUFFER_TOO_SMALL) {
603 HandleBuffer = AllocateZeroPool(BufferSize);
604 if (HandleBuffer == NULL) {
605 return (SHELL_OUT_OF_RESOURCES);
606 }
607 Status = gBS->LocateHandle(
608 ByProtocol,
609 &gEfiSimpleFileSystemProtocolGuid,
610 NULL,
611 &BufferSize,
612 HandleBuffer);
613 }
614
615 //
616 // Get the map name(s) for each one.
617 //
618 for ( LoopVar = 0, Found = FALSE
619 ; LoopVar < (BufferSize / sizeof(EFI_HANDLE)) && HandleBuffer != NULL
620 ; LoopVar ++
621 ){
622 Status = PerformSingleMappingDisplay(
623 Verbose,
624 Consist,
625 Normal,
626 Test,
627 SFO,
628 Specific,
629 HandleBuffer[LoopVar]);
630 if (!EFI_ERROR(Status)) {
631 Found = TRUE;
632 }
633 }
634
635 //
636 // Look up all BlockIo in the platform
637 //
638 Status = gBS->LocateHandle(
639 ByProtocol,
640 &gEfiBlockIoProtocolGuid,
641 NULL,
642 &BufferSize,
643 HandleBuffer);
644 if (Status == EFI_BUFFER_TOO_SMALL) {
645 SHELL_FREE_NON_NULL(HandleBuffer);
646 HandleBuffer = AllocateZeroPool(BufferSize);
647 if (HandleBuffer == NULL) {
648 return (SHELL_OUT_OF_RESOURCES);
649 }
650 Status = gBS->LocateHandle(
651 ByProtocol,
652 &gEfiBlockIoProtocolGuid,
653 NULL,
654 &BufferSize,
655 HandleBuffer);
656 }
657 if (!EFI_ERROR(Status) && HandleBuffer != NULL) {
658 //
659 // Get the map name(s) for each one.
660 //
661 for ( LoopVar = 0
662 ; LoopVar < BufferSize / sizeof(EFI_HANDLE)
663 ; LoopVar ++
664 ){
665 //
666 // Skip any that were already done...
667 //
668 if (gBS->OpenProtocol(
669 HandleBuffer[LoopVar],
670 &gEfiSimpleFileSystemProtocolGuid,
671 NULL,
672 gImageHandle,
673 NULL,
674 EFI_OPEN_PROTOCOL_TEST_PROTOCOL) == EFI_SUCCESS) {
675 continue;
676 }
677 Status = PerformSingleMappingDisplay(
678 Verbose,
679 Consist,
680 Normal,
681 Test,
682 SFO,
683 Specific,
684 HandleBuffer[LoopVar]);
685 if (!EFI_ERROR(Status)) {
686 Found = TRUE;
687 }
688 }
689 FreePool(HandleBuffer);
690 }
691 if (!Found) {
692 if (Specific != NULL) {
693 ShellPrintHiiEx(gST->ConOut->Mode->CursorColumn, gST->ConOut->Mode->CursorRow-1, NULL, STRING_TOKEN (STR_MAP_NF), gShellLevel2HiiHandle, Specific);
694 } else {
695 ShellPrintHiiEx(gST->ConOut->Mode->CursorColumn, gST->ConOut->Mode->CursorRow-1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);
696 }
697 }
698 return (SHELL_SUCCESS);
699 }
700
701 /**
702 Perform a mapping display and parse for multiple types in the TypeString.
703
704 @param[in] Verbose TRUE to use verbose output.
705 @param[in] Consist TRUE to display consistent names.
706 @param[in] Normal TRUE to display normal names.
707 @param[in] TypeString An optional comma-delimited list of types.
708 @param[in] SFO TRUE to display in SFO format. See Spec.
709 @param[in] Specific An optional specific map name to display alone.
710
711 @retval SHELL_INVALID_PARAMETER A parameter was invalid.
712 @retval SHELL_SUCCESS The display was successful.
713 @sa PerformMappingDisplay
714 **/
715 SHELL_STATUS
716 EFIAPI
717 PerformMappingDisplay2(
718 IN CONST BOOLEAN Verbose,
719 IN CONST BOOLEAN Consist,
720 IN CONST BOOLEAN Normal,
721 IN CONST CHAR16 *TypeString,
722 IN CONST BOOLEAN SFO,
723 IN CONST CHAR16 *Specific OPTIONAL
724 )
725 {
726 CONST CHAR16 *TypeWalker;
727 SHELL_STATUS ShellStatus;
728 CHAR16 *Comma;
729
730
731 if (TypeString == NULL) {
732 return (PerformMappingDisplay(Verbose, Consist, Normal, NULL, SFO, Specific, TRUE));
733 }
734 ShellStatus = SHELL_SUCCESS;
735 for (TypeWalker = TypeString ; TypeWalker != NULL && *TypeWalker != CHAR_NULL ;) {
736 Comma = StrStr(TypeWalker, L",");
737 if (Comma == 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 break;
744 } else {
745 *Comma = CHAR_NULL;
746 if (ShellStatus == SHELL_SUCCESS) {
747 ShellStatus = PerformMappingDisplay(Verbose, Consist, Normal, TypeWalker, SFO, Specific, (BOOLEAN)(TypeWalker == TypeString));
748 } else {
749 PerformMappingDisplay(Verbose, Consist, Normal, TypeWalker, SFO, Specific, (BOOLEAN)(TypeWalker == TypeString));
750 }
751 *Comma = L',';
752 TypeWalker = Comma + 1;
753 }
754 }
755
756 return (ShellStatus);
757 }
758
759 /**
760 Delete a specific map.
761
762 @param[in] Specific The pointer to the name of the map to delete.
763
764 @retval EFI_INVALID_PARAMETER Specific was NULL.
765 @retval EFI_SUCCESS The operation was successful.
766 @retval EFI_NOT_FOUND Specific could not be found.
767 **/
768 EFI_STATUS
769 EFIAPI
770 PerformMappingDelete(
771 IN CONST CHAR16 *Specific
772 )
773 {
774 EFI_STATUS Status;
775 EFI_HANDLE *HandleBuffer;
776 UINTN BufferSize;
777 UINTN LoopVar;
778 BOOLEAN Deleted;
779
780 if (Specific == NULL) {
781 return (EFI_INVALID_PARAMETER);
782 }
783
784 BufferSize = 0;
785 HandleBuffer = NULL;
786 Deleted = FALSE;
787
788 //
789 // Look up all SimpleFileSystems in the platform
790 //
791 Status = gBS->LocateHandle(
792 ByProtocol,
793 &gEfiDevicePathProtocolGuid,
794 NULL,
795 &BufferSize,
796 HandleBuffer);
797 if (Status == EFI_BUFFER_TOO_SMALL) {
798 HandleBuffer = AllocateZeroPool(BufferSize);
799 if (HandleBuffer == NULL) {
800 return (EFI_OUT_OF_RESOURCES);
801 }
802 Status = gBS->LocateHandle(
803 ByProtocol,
804 &gEfiDevicePathProtocolGuid,
805 NULL,
806 &BufferSize,
807 HandleBuffer);
808 }
809 if (EFI_ERROR(Status)) {
810 SHELL_FREE_NON_NULL(HandleBuffer);
811 return (Status);
812 }
813
814 if (HandleBuffer != NULL) {
815 //
816 // Get the map name(s) for each one.
817 //
818 for ( LoopVar = 0
819 ; LoopVar < BufferSize / sizeof(EFI_HANDLE)
820 ; LoopVar ++
821 ){
822 if (PerformSingleMappingDelete(Specific,HandleBuffer[LoopVar]) == SHELL_SUCCESS) {
823 Deleted = TRUE;
824 }
825 }
826 }
827 //
828 // Look up all BlockIo in the platform
829 //
830 Status = gBS->LocateHandle(
831 ByProtocol,
832 &gEfiBlockIoProtocolGuid,
833 NULL,
834 &BufferSize,
835 HandleBuffer);
836 if (Status == EFI_BUFFER_TOO_SMALL) {
837 FreePool(HandleBuffer);
838 HandleBuffer = AllocateZeroPool(BufferSize);
839 if (HandleBuffer == NULL) {
840 return (EFI_OUT_OF_RESOURCES);
841 }
842 Status = gBS->LocateHandle(
843 ByProtocol,
844 &gEfiBlockIoProtocolGuid,
845 NULL,
846 &BufferSize,
847 HandleBuffer);
848 }
849 if (EFI_ERROR(Status)) {
850 SHELL_FREE_NON_NULL(HandleBuffer);
851 return (Status);
852 }
853
854 if (HandleBuffer != NULL) {
855 //
856 // Get the map name(s) for each one.
857 //
858 for ( LoopVar = 0
859 ; LoopVar < BufferSize / sizeof(EFI_HANDLE)
860 ; LoopVar ++
861 ){
862 //
863 // Skip any that were already done...
864 //
865 if (gBS->OpenProtocol(
866 HandleBuffer[LoopVar],
867 &gEfiDevicePathProtocolGuid,
868 NULL,
869 gImageHandle,
870 NULL,
871 EFI_OPEN_PROTOCOL_TEST_PROTOCOL) == EFI_SUCCESS) {
872 continue;
873 }
874 if (PerformSingleMappingDelete(Specific,HandleBuffer[LoopVar]) == SHELL_SUCCESS) {
875 Deleted = TRUE;
876 }
877 }
878 }
879 SHELL_FREE_NON_NULL(HandleBuffer);
880 if (!Deleted) {
881 return (EFI_NOT_FOUND);
882 }
883 return (EFI_SUCCESS);
884 }
885
886 /**
887 function to add a mapping from mapping.
888
889 This function will get the device path associated with the mapping and call SetMap.
890
891 @param[in] Map The Map to add a mapping for
892 @param[in] SName The name of the new mapping
893
894 @retval SHELL_SUCCESS the mapping was added
895 @retval SHELL_INVALID_PARAMETER the device path for Map could not be retrieved.
896 @return Shell version of a return value from EfiShellProtocol->SetMap
897
898 **/
899 SHELL_STATUS
900 EFIAPI
901 AddMappingFromMapping(
902 IN CONST CHAR16 *Map,
903 IN CONST CHAR16 *SName
904 )
905 {
906 CONST EFI_DEVICE_PATH_PROTOCOL *DevPath;
907 EFI_STATUS Status;
908 CHAR16 *NewSName;
909
910 NewSName = AllocateZeroPool(StrSize(SName) + sizeof(CHAR16));
911 if (NewSName == NULL) {
912 return (SHELL_OUT_OF_RESOURCES);
913 }
914 StrCpy(NewSName, SName);
915 if (NewSName[StrLen(NewSName)-1] != L':') {
916 StrCat(NewSName, L":");
917 }
918
919 if (!IsNumberLetterOnly(NewSName, StrLen(NewSName)-1)) {
920 FreePool(NewSName);
921 return (SHELL_INVALID_PARAMETER);
922 }
923
924 DevPath = gEfiShellProtocol->GetDevicePathFromMap(Map);
925 if (DevPath == NULL) {
926 FreePool(NewSName);
927 return (SHELL_INVALID_PARAMETER);
928 }
929
930 Status = gEfiShellProtocol->SetMap(DevPath, NewSName);
931 FreePool(NewSName);
932 if (EFI_ERROR(Status)) {
933 return (SHELL_DEVICE_ERROR);
934 }
935 return (SHELL_SUCCESS);
936 }
937
938 /**
939 function to add a mapping from an EFI_HANDLE.
940
941 This function will get the device path associated with the Handle and call SetMap.
942
943 @param[in] Handle The handle to add a mapping for
944 @param[in] SName The name of the new mapping
945
946 @retval SHELL_SUCCESS the mapping was added
947 @retval SHELL_INVALID_PARAMETER SName was not valid for a map name.
948 @return Shell version of a return value from either
949 gBS->OpenProtocol or EfiShellProtocol->SetMap
950
951 **/
952 SHELL_STATUS
953 EFIAPI
954 AddMappingFromHandle(
955 IN CONST EFI_HANDLE Handle,
956 IN CONST CHAR16 *SName
957 )
958 {
959 EFI_DEVICE_PATH_PROTOCOL *DevPath;
960 EFI_STATUS Status;
961 CHAR16 *NewSName;
962
963 NewSName = AllocateZeroPool(StrSize(SName) + sizeof(CHAR16));
964 if (NewSName == NULL) {
965 return (SHELL_OUT_OF_RESOURCES);
966 }
967 StrCpy(NewSName, SName);
968 if (NewSName[StrLen(NewSName)-1] != L':') {
969 StrCat(NewSName, L":");
970 }
971
972 if (!IsNumberLetterOnly(NewSName, StrLen(NewSName)-1)) {
973 FreePool(NewSName);
974 return (SHELL_INVALID_PARAMETER);
975 }
976
977 Status = gBS->OpenProtocol(
978 Handle,
979 &gEfiDevicePathProtocolGuid,
980 (VOID**)&DevPath,
981 gImageHandle,
982 NULL,
983 EFI_OPEN_PROTOCOL_GET_PROTOCOL
984 );
985 if (EFI_ERROR(Status)) {
986 FreePool(NewSName);
987 return (SHELL_DEVICE_ERROR);
988 }
989 Status = gEfiShellProtocol->SetMap(DevPath, NewSName);
990 FreePool(NewSName);
991 if (EFI_ERROR(Status)) {
992 return (SHELL_DEVICE_ERROR);
993 }
994 return (SHELL_SUCCESS);
995 }
996
997 STATIC CONST SHELL_PARAM_ITEM MapParamList[] = {
998 {L"-d", TypeValue},
999 {L"-r", TypeFlag},
1000 {L"-v", TypeFlag},
1001 {L"-c", TypeFlag},
1002 {L"-f", TypeFlag},
1003 {L"-u", TypeFlag},
1004 {L"-t", TypeValue},
1005 {L"-sfo", TypeValue},
1006 {NULL, TypeMax}
1007 };
1008
1009 /**
1010 Function for 'map' command.
1011
1012 @param[in] ImageHandle Handle to the Image (NULL if Internal).
1013 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
1014 **/
1015 SHELL_STATUS
1016 EFIAPI
1017 ShellCommandRunMap (
1018 IN EFI_HANDLE ImageHandle,
1019 IN EFI_SYSTEM_TABLE *SystemTable
1020 )
1021 {
1022 EFI_STATUS Status;
1023 LIST_ENTRY *Package;
1024 CHAR16 *ProblemParam;
1025 CONST CHAR16 *SName;
1026 CONST CHAR16 *Mapping;
1027 EFI_HANDLE MapAsHandle;
1028 SHELL_STATUS ShellStatus;
1029 BOOLEAN SfoMode;
1030 BOOLEAN ConstMode;
1031 BOOLEAN NormlMode;
1032 CONST CHAR16 *Param1;
1033 CONST CHAR16 *TypeString;
1034 UINTN TempStringLength;
1035
1036 ProblemParam = NULL;
1037 Mapping = NULL;
1038 SName = NULL;
1039 ShellStatus = SHELL_SUCCESS;
1040 MapAsHandle = NULL;
1041
1042 //
1043 // initialize the shell lib (we must be in non-auto-init...)
1044 //
1045 Status = ShellInitialize();
1046 ASSERT_EFI_ERROR(Status);
1047
1048 Status = CommandInit();
1049 ASSERT_EFI_ERROR(Status);
1050
1051 //
1052 // parse the command line
1053 //
1054 Status = ShellCommandLineParse (MapParamList, &Package, &ProblemParam, TRUE);
1055 if (EFI_ERROR(Status)) {
1056 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
1057 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, ProblemParam);
1058 FreePool(ProblemParam);
1059 ShellStatus = SHELL_INVALID_PARAMETER;
1060 } else {
1061 ASSERT(FALSE);
1062 }
1063 } else {
1064 //
1065 // check for "-?"
1066 //
1067 SfoMode = ShellCommandLineGetFlag(Package, L"-sfo");
1068 ConstMode = ShellCommandLineGetFlag(Package, L"-c");
1069 NormlMode = ShellCommandLineGetFlag(Package, L"-f");
1070 if (ShellCommandLineGetFlag(Package, L"-?")) {
1071 ASSERT(FALSE);
1072 } else if (ShellCommandLineGetRawValue(Package, 3) != NULL) {
1073 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle);
1074 ShellStatus = SHELL_INVALID_PARAMETER;
1075 } else {
1076 //
1077 // Deleting a map name...
1078 //
1079 if (ShellCommandLineGetFlag(Package, L"-d")) {
1080 if ( ShellCommandLineGetFlag(Package, L"-r")
1081 || ShellCommandLineGetFlag(Package, L"-v")
1082 || ConstMode
1083 || NormlMode
1084 || ShellCommandLineGetFlag(Package, L"-u")
1085 || ShellCommandLineGetFlag(Package, L"-t")
1086 ){
1087 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_CON), gShellLevel2HiiHandle);
1088 ShellStatus = SHELL_INVALID_PARAMETER;
1089 } else {
1090 SName = ShellCommandLineGetValue(Package, L"-d");
1091 if (SName != NULL) {
1092 Status = PerformMappingDelete(SName);
1093 if (EFI_ERROR(Status)) {
1094 if (Status == EFI_ACCESS_DENIED) {
1095 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_AD), gShellLevel2HiiHandle);
1096 ShellStatus = SHELL_ACCESS_DENIED;
1097 } else if (Status == EFI_NOT_FOUND) {
1098 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MAP_NF), gShellLevel2HiiHandle, SName);
1099 ShellStatus = SHELL_INVALID_PARAMETER;
1100 } else {
1101 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_UK), gShellLevel2HiiHandle, Status);
1102 ShellStatus = SHELL_UNSUPPORTED;
1103 }
1104 }
1105 } else {
1106 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle);
1107 ShellStatus = SHELL_INVALID_PARAMETER;
1108 }
1109 }
1110 } else if ( ShellCommandLineGetFlag(Package, L"-r")
1111 // || ShellCommandLineGetFlag(Package, L"-v")
1112 || ConstMode
1113 || NormlMode
1114 || ShellCommandLineGetFlag(Package, L"-u")
1115 || ShellCommandLineGetFlag(Package, L"-t")
1116 ){
1117 if ( ShellCommandLineGetFlag(Package, L"-r")) {
1118 //
1119 // Do the reset
1120 //
1121 Status = ShellCommandCreateInitialMappingsAndPaths();
1122 if (EFI_ERROR(Status)) {
1123 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_UK), gShellLevel2HiiHandle, Status);
1124 ShellStatus = SHELL_UNSUPPORTED;
1125 }
1126 }
1127 if ( ShellStatus == SHELL_SUCCESS && ShellCommandLineGetFlag(Package, L"-u")) {
1128 //
1129 // Do the Update
1130 //
1131 Status = UpdateMapping();
1132 if (EFI_ERROR(Status)) {
1133 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_UK), gShellLevel2HiiHandle, Status);
1134 ShellStatus = SHELL_UNSUPPORTED;
1135 }
1136 }
1137 if (ShellStatus == SHELL_SUCCESS) {
1138 Param1 = ShellCommandLineGetRawValue(Package, 1);
1139 TypeString = ShellCommandLineGetValue(Package, L"-t");
1140 if (!ConstMode
1141 &&!NormlMode
1142 &&TypeString == NULL
1143 ) {
1144 //
1145 // now do the display...
1146 //
1147 ShellStatus = PerformMappingDisplay(
1148 ShellCommandLineGetFlag(Package, L"-v"),
1149 TRUE,
1150 TRUE,
1151 NULL,
1152 SfoMode,
1153 Param1,
1154 TRUE
1155 );
1156 } else {
1157 //
1158 // now do the display...
1159 //
1160 ShellStatus = PerformMappingDisplay2(
1161 ShellCommandLineGetFlag(Package, L"-v"),
1162 ConstMode,
1163 NormlMode,
1164 TypeString,
1165 SfoMode,
1166 Param1
1167 );
1168 }
1169 }
1170 } else {
1171 //
1172 // adding or displaying (there were no flags)
1173 //
1174 SName = ShellCommandLineGetRawValue(Package, 1);
1175 Mapping = ShellCommandLineGetRawValue(Package, 2);
1176 if ( SName == NULL
1177 && Mapping == NULL
1178 ){
1179 //
1180 // display only since no flags
1181 //
1182 ShellStatus = PerformMappingDisplay(
1183 ShellCommandLineGetFlag(Package, L"-v"),
1184 TRUE,
1185 TRUE,
1186 NULL,
1187 SfoMode,
1188 NULL,
1189 TRUE
1190 );
1191 } else if ( SName == NULL
1192 || Mapping == NULL
1193 ){
1194 //
1195 // Display only the one specified
1196 //
1197 ShellStatus = PerformMappingDisplay(
1198 FALSE,
1199 FALSE,
1200 FALSE,
1201 NULL,
1202 SfoMode,
1203 SName, // note the variable here...
1204 TRUE
1205 );
1206 } else {
1207 if (ShellIsHexOrDecimalNumber(Mapping, TRUE, FALSE)) {
1208 MapAsHandle = ConvertHandleIndexToHandle(ShellStrToUintn(Mapping));
1209 } else {
1210 MapAsHandle = NULL;
1211 }
1212 if (MapAsHandle == NULL && Mapping[StrLen(Mapping)-1] != L':') {
1213 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, Mapping);
1214 ShellStatus = SHELL_INVALID_PARAMETER;
1215 } else {
1216 if (MapAsHandle != NULL) {
1217 TempStringLength = StrLen(SName);
1218 if (!IsNumberLetterOnly(SName, TempStringLength-(SName[TempStringLength-1]==L':'?1:0))) {
1219 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, SName);
1220 ShellStatus = SHELL_INVALID_PARAMETER;
1221 } else {
1222 ShellStatus = AddMappingFromHandle(MapAsHandle, SName);
1223 }
1224 } else {
1225 TempStringLength = StrLen(SName);
1226 if (!IsNumberLetterOnly(SName, TempStringLength-(SName[TempStringLength-1]==L':'?1:0))) {
1227 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, SName);
1228 ShellStatus = SHELL_INVALID_PARAMETER;
1229 } else {
1230 ShellStatus = AddMappingFromMapping(Mapping, SName);
1231 }
1232 }
1233 if (ShellStatus != SHELL_SUCCESS) {
1234 switch (ShellStatus) {
1235 case SHELL_ACCESS_DENIED:
1236 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_AD), gShellLevel2HiiHandle);
1237 break;
1238 case SHELL_INVALID_PARAMETER:
1239 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellLevel2HiiHandle);
1240 break;
1241 case SHELL_DEVICE_ERROR:
1242 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MAP_NOF), gShellLevel2HiiHandle, Mapping);
1243 break;
1244 default:
1245 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_UK), gShellLevel2HiiHandle, ShellStatus|MAX_BIT);
1246 }
1247 } else {
1248 //
1249 // now do the display...
1250 //
1251 ShellStatus = PerformMappingDisplay(
1252 FALSE,
1253 FALSE,
1254 FALSE,
1255 NULL,
1256 SfoMode,
1257 SName,
1258 TRUE
1259 );
1260 } // we were sucessful so do an output
1261 } // got a valid map target
1262 } // got 2 variables
1263 } // we are adding a mapping
1264 } // got valid parameters
1265 }
1266
1267 //
1268 // free the command line package
1269 //
1270 ShellCommandLineFreeVarList (Package);
1271
1272 return (ShellStatus);
1273 }
1274