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