]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDriver1CommandsLib/Unload.c
Update all the code to consume the ConvertDevicePathToText, ConvertDevicePathNodeToTe...
[mirror_edk2.git] / ShellPkg / Library / UefiShellDriver1CommandsLib / Unload.c
1 /** @file
2 Main file for Unload shell Driver1 function.
3
4 Copyright (c) 2010 - 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 "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 CHAR16 *DevicePathText;
71 CHAR16 *CodeTypeText;
72 CHAR16 *DataTypeText;
73 CHAR8 *PdbPointer;
74
75 Image = NULL;
76
77 Status = gBS->OpenProtocol(TheHandle, &gEfiLoadedImageProtocolGuid, (VOID**)&Image, gImageHandle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
78 if (EFI_ERROR(Status)) {
79 return (EFI_INVALID_PARAMETER);
80 }
81 DevicePathText = ConvertDevicePathToText(Image->FilePath, TRUE, TRUE);
82 CodeTypeText = ConvertMemoryType(Image->ImageCodeType);
83 DataTypeText = ConvertMemoryType(Image->ImageDataType);
84 PdbPointer = (CHAR8*)PeCoffLoaderGetPdbPointer(Image->ImageBase);
85 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_UNLOAD_VERBOSE), gShellDriver1HiiHandle,
86 ConvertHandleToHandleIndex(TheHandle),
87 TheHandle,
88 Image,
89 Image->ParentHandle,
90 Image->SystemTable,
91 Image->DeviceHandle,
92 DevicePathText,
93 PdbPointer,
94 Image->ImageBase,
95 Image->ImageSize,
96 CodeTypeText,
97 DataTypeText
98 );
99
100 SHELL_FREE_NON_NULL(DevicePathText);
101 SHELL_FREE_NON_NULL(CodeTypeText);
102 SHELL_FREE_NON_NULL(DataTypeText);
103
104 return (EFI_SUCCESS);
105 }
106
107 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
108 {L"-n", TypeFlag},
109 {L"-v", TypeFlag},
110 {L"-verbose", TypeFlag},
111 {NULL, TypeMax}
112 };
113
114 /**
115 Function for 'unload' command.
116
117 @param[in] ImageHandle Handle to the Image (NULL if Internal).
118 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
119 **/
120 SHELL_STATUS
121 EFIAPI
122 ShellCommandRunUnload (
123 IN EFI_HANDLE ImageHandle,
124 IN EFI_SYSTEM_TABLE *SystemTable
125 )
126 {
127 EFI_STATUS Status;
128 LIST_ENTRY *Package;
129 CHAR16 *ProblemParam;
130 SHELL_STATUS ShellStatus;
131 EFI_HANDLE TheHandle;
132 CONST CHAR16 *Param1;
133 SHELL_PROMPT_RESPONSE *Resp;
134 UINT64 Value;
135
136 ShellStatus = SHELL_SUCCESS;
137 Package = NULL;
138 Resp = NULL;
139 Value = 0;
140 TheHandle = NULL;
141
142 //
143 // initialize the shell lib (we must be in non-auto-init...)
144 //
145 Status = ShellInitialize();
146 ASSERT_EFI_ERROR(Status);
147
148 //
149 // parse the command line
150 //
151 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
152 if (EFI_ERROR(Status)) {
153 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
154 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDriver1HiiHandle, ProblemParam);
155 FreePool(ProblemParam);
156 ShellStatus = SHELL_INVALID_PARAMETER;
157 } else {
158 ASSERT(FALSE);
159 }
160 } else {
161 if (ShellCommandLineGetCount(Package) > 2){
162 //
163 // error for too many parameters
164 //
165 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDriver1HiiHandle);
166 ShellStatus = SHELL_INVALID_PARAMETER;
167 } else if (ShellCommandLineGetCount(Package) < 2) {
168 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDriver1HiiHandle);
169 ShellStatus = SHELL_INVALID_PARAMETER;
170 } else {
171 Param1 = ShellCommandLineGetRawValue(Package, 1);
172 if (Param1 != NULL) {
173 Status = ShellConvertStringToUint64(Param1, &Value, TRUE, FALSE);
174 TheHandle = ConvertHandleIndexToHandle((UINTN)Value);
175 }
176
177 if (EFI_ERROR(Status) || Param1 == NULL || TheHandle == NULL){
178 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_INV_HANDLE), gShellDriver1HiiHandle, Param1);
179 ShellStatus = SHELL_INVALID_PARAMETER;
180 } else {
181 ASSERT(TheHandle != NULL);
182 if (ShellCommandLineGetFlag(Package, L"-v") || ShellCommandLineGetFlag(Package, L"-verbose")) {
183 DumpLoadedImageProtocolInfo(TheHandle);
184 }
185
186 if (!ShellCommandLineGetFlag(Package, L"-n")) {
187 Status = ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_UNLOAD_CONF), gShellDriver1HiiHandle, (UINTN)TheHandle);
188 Status = ShellPromptForResponse(ShellPromptResponseTypeYesNo, NULL, (VOID**)&Resp);
189 }
190 if (ShellCommandLineGetFlag(Package, L"-n") || (Resp != NULL && *Resp == ShellPromptResponseYes)) {
191 Status = gBS->UnloadImage(TheHandle);
192 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_HANDLE_RESULT), gShellDriver1HiiHandle, L"Unload", (UINTN)TheHandle, Status);
193 }
194 SHELL_FREE_NON_NULL(Resp);
195 }
196 }
197 }
198 if (ShellStatus == SHELL_SUCCESS) {
199 if (Status == EFI_SECURITY_VIOLATION) {
200 ShellStatus = SHELL_SECURITY_VIOLATION;
201 } else if (Status == EFI_INVALID_PARAMETER) {
202 ShellStatus = SHELL_INVALID_PARAMETER;
203 } else if (EFI_ERROR(Status)) {
204 ShellStatus = SHELL_NOT_FOUND;
205 }
206 }
207
208 if (Package != NULL) {
209 ShellCommandLineFreeVarList(Package);
210 }
211
212 return (ShellStatus);
213 }