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