]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDriver1CommandsLib/Unload.c
Adding Driver1 profile commands to the UEFI Shell 2.0.
[mirror_edk2.git] / ShellPkg / Library / UefiShellDriver1CommandsLib / Unload.c
1 /** @file
2 Main file for Unload shell Driver1 function.
3
4 Copyright (c) 2010, 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 "UefiShellDriver1CommandsLib.h"
16
17 /**
18 Function to translate the EFI_MEMORY_TYPE into a string.
19
20 @param[in] Memory The memory type.
21
22 @retval A string representation of the type allocated from BS Pool.
23 **/
24 CHAR16*
25 EFIAPI
26 ConvertMemoryType (
27 IN CONST EFI_MEMORY_TYPE Memory
28 )
29 {
30 CHAR16 *RetVal;
31 RetVal = NULL;
32
33 switch (Memory) {
34 case EfiReservedMemoryType: StrnCatGrow(&RetVal, NULL, L"EfiReservedMemoryType", 0); break;
35 case EfiLoaderCode: StrnCatGrow(&RetVal, NULL, L"EfiLoaderCode", 0); break;
36 case EfiLoaderData: StrnCatGrow(&RetVal, NULL, L"EfiLoaderData", 0); break;
37 case EfiBootServicesCode: StrnCatGrow(&RetVal, NULL, L"EfiBootServicesCode", 0); break;
38 case EfiBootServicesData: StrnCatGrow(&RetVal, NULL, L"EfiBootServicesData", 0); break;
39 case EfiRuntimeServicesCode: StrnCatGrow(&RetVal, NULL, L"EfiRuntimeServicesCode", 0); break;
40 case EfiRuntimeServicesData: StrnCatGrow(&RetVal, NULL, L"EfiRuntimeServicesData", 0); break;
41 case EfiConventionalMemory: StrnCatGrow(&RetVal, NULL, L"EfiConventionalMemory", 0); break;
42 case EfiUnusableMemory: StrnCatGrow(&RetVal, NULL, L"EfiUnusableMemory", 0); break;
43 case EfiACPIReclaimMemory: StrnCatGrow(&RetVal, NULL, L"EfiACPIReclaimMemory", 0); break;
44 case EfiACPIMemoryNVS: StrnCatGrow(&RetVal, NULL, L"EfiACPIMemoryNVS", 0); break;
45 case EfiMemoryMappedIO: StrnCatGrow(&RetVal, NULL, L"EfiMemoryMappedIO", 0); break;
46 case EfiMemoryMappedIOPortSpace: StrnCatGrow(&RetVal, NULL, L"EfiMemoryMappedIOPortSpace", 0); break;
47 case EfiPalCode: StrnCatGrow(&RetVal, NULL, L"EfiPalCode", 0); break;
48 case EfiMaxMemoryType: StrnCatGrow(&RetVal, NULL, L"EfiMaxMemoryType", 0); break;
49 default: ASSERT(FALSE);
50 }
51 return (RetVal);
52 }
53
54 /**
55 Function to dump LoadedImage info from TheHandle.
56
57 @param[in] TheHandle The handle to dump info from.
58
59 @retval EFI_SUCCESS The info was dumped.
60 @retval EFI_INVALID_PARAMETER The handle did not have LoadedImage
61 **/
62 EFI_STATUS
63 EFIAPI
64 DumpLoadedImageProtocolInfo (
65 IN EFI_HANDLE TheHandle
66 )
67 {
68 EFI_LOADED_IMAGE_PROTOCOL *Image;
69 EFI_STATUS Status;
70 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *DevicePathToText;
71 CHAR16 *DevicePathText;
72 CHAR16 *CodeTypeText;
73 CHAR16 *DataTypeText;
74 CHAR8 *PdbPointer;
75
76 Image = NULL;
77
78 Status = gBS->OpenProtocol(TheHandle, &gEfiLoadedImageProtocolGuid, (VOID**)&Image, gImageHandle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
79 if (EFI_ERROR(Status)) {
80 return (EFI_INVALID_PARAMETER);
81 }
82
83 Status = gBS->LocateProtocol(
84 &gEfiDevicePathToTextProtocolGuid,
85 NULL,
86 (VOID**)&DevicePathToText);
87 //
88 // we now have the device path to text protocol
89 //
90 if (!EFI_ERROR(Status)) {
91 DevicePathText = DevicePathToText->ConvertDevicePathToText(Image->FilePath, TRUE, TRUE);
92 } else {
93 DevicePathText = NULL;
94 }
95
96 CodeTypeText = ConvertMemoryType(Image->ImageCodeType);
97 DataTypeText = ConvertMemoryType(Image->ImageDataType);
98 PdbPointer = (CHAR8*)PeCoffLoaderGetPdbPointer(Image->ImageBase);
99 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_UNLOAD_VERBOSE), gShellDriver1HiiHandle,
100 ConvertHandleToHandleIndex(TheHandle),
101 TheHandle,
102 Image,
103 Image->ParentHandle,
104 Image->SystemTable,
105 Image->DeviceHandle,
106 DevicePathText,
107 PdbPointer,
108 Image->ImageBase,
109 Image->ImageSize,
110 CodeTypeText,
111 DataTypeText
112 );
113
114 SHELL_FREE_NON_NULL(DevicePathText);
115 SHELL_FREE_NON_NULL(CodeTypeText);
116 SHELL_FREE_NON_NULL(DataTypeText);
117
118 return (EFI_SUCCESS);
119 }
120
121 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
122 {L"-n", TypeFlag},
123 {L"-v", TypeFlag},
124 {L"-verbose", TypeFlag},
125 {NULL, TypeMax}
126 };
127
128 /**
129 Function for 'unload' command.
130
131 @param[in] ImageHandle Handle to the Image (NULL if Internal).
132 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
133 **/
134 SHELL_STATUS
135 EFIAPI
136 ShellCommandRunUnload (
137 IN EFI_HANDLE ImageHandle,
138 IN EFI_SYSTEM_TABLE *SystemTable
139 )
140 {
141 EFI_STATUS Status;
142 LIST_ENTRY *Package;
143 CHAR16 *ProblemParam;
144 SHELL_STATUS ShellStatus;
145 EFI_HANDLE TheHandle;
146 CONST CHAR16 *Param1;
147 SHELL_PROMPT_RESPONSE *Resp;
148
149 ShellStatus = SHELL_SUCCESS;
150 Package = NULL;
151 Resp = NULL;
152
153 //
154 // initialize the shell lib (we must be in non-auto-init...)
155 //
156 Status = ShellInitialize();
157 ASSERT_EFI_ERROR(Status);
158
159 //
160 // parse the command line
161 //
162 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
163 if (EFI_ERROR(Status)) {
164 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
165 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDriver1HiiHandle, ProblemParam);
166 FreePool(ProblemParam);
167 ShellStatus = SHELL_INVALID_PARAMETER;
168 } else {
169 ASSERT(FALSE);
170 }
171 } else {
172 if (ShellCommandLineGetCount(Package) > 2){
173 //
174 // error for too many parameters
175 //
176 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDriver1HiiHandle);
177 ShellStatus = SHELL_INVALID_PARAMETER;
178 } else if (ShellCommandLineGetCount(Package) < 2) {
179 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDriver1HiiHandle);
180 ShellStatus = SHELL_INVALID_PARAMETER;
181 } else {
182 Param1 = ShellCommandLineGetRawValue(Package, 1);
183 if (Param1 == NULL || ConvertHandleIndexToHandle(StrHexToUintn(Param1)) == NULL){
184 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_INV_HANDLE), gShellDriver1HiiHandle, Param1);
185 ShellStatus = SHELL_INVALID_PARAMETER;
186 } else {
187 TheHandle = ConvertHandleIndexToHandle(StrHexToUintn(Param1));
188 ASSERT(TheHandle != NULL);
189 if (ShellCommandLineGetFlag(Package, L"-v") || ShellCommandLineGetFlag(Package, L"-verbose")) {
190 DumpLoadedImageProtocolInfo(TheHandle);
191 }
192
193 if (!ShellCommandLineGetFlag(Package, L"-n")) {
194 Status = ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_UNLOAD_CONF), gShellDriver1HiiHandle, StrHexToUintn(Param1));
195 Status = ShellPromptForResponse(ShellPromptResponseTypeYesNo, NULL, (VOID**)&Resp);
196 }
197 if (ShellCommandLineGetFlag(Package, L"-n") || (Resp != NULL && *Resp == ShellPromptResponseYes)) {
198 Status = gBS->UnloadImage(TheHandle);
199 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_HANDLE_RESULT), gShellDriver1HiiHandle, L"Unload", StrHexToUintn(Param1), Status);
200 }
201 SHELL_FREE_NON_NULL(Resp);
202 }
203 }
204 }
205 if (ShellStatus == SHELL_SUCCESS) {
206 if (Status == EFI_SECURITY_VIOLATION) {
207 ShellStatus = SHELL_SECURITY_VIOLATION;
208 } else if (Status == EFI_INVALID_PARAMETER) {
209 ShellStatus = SHELL_INVALID_PARAMETER;
210 } else if (EFI_ERROR(Status)) {
211 ShellStatus = SHELL_NOT_FOUND;
212 }
213 }
214
215 if (Package != NULL) {
216 ShellCommandLineFreeVarList(Package);
217 }
218
219 return (ShellStatus);
220 }