]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDriver1CommandsLib/Unload.c
connect - add comments and add input verification
[mirror_edk2.git] / ShellPkg / Library / UefiShellDriver1CommandsLib / Unload.c
1 /** @file
2 Main file for Unload shell Driver1 function.
3
4 Copyright (c) 2010 - 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 "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 UINT64 Value;
149
150 ShellStatus = SHELL_SUCCESS;
151 Package = NULL;
152 Resp = NULL;
153 Value = 0;
154 TheHandle = NULL;
155
156 //
157 // initialize the shell lib (we must be in non-auto-init...)
158 //
159 Status = ShellInitialize();
160 ASSERT_EFI_ERROR(Status);
161
162 //
163 // parse the command line
164 //
165 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
166 if (EFI_ERROR(Status)) {
167 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
168 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDriver1HiiHandle, ProblemParam);
169 FreePool(ProblemParam);
170 ShellStatus = SHELL_INVALID_PARAMETER;
171 } else {
172 ASSERT(FALSE);
173 }
174 } else {
175 if (ShellCommandLineGetCount(Package) > 2){
176 //
177 // error for too many parameters
178 //
179 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDriver1HiiHandle);
180 ShellStatus = SHELL_INVALID_PARAMETER;
181 } else if (ShellCommandLineGetCount(Package) < 2) {
182 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDriver1HiiHandle);
183 ShellStatus = SHELL_INVALID_PARAMETER;
184 } else {
185 Param1 = ShellCommandLineGetRawValue(Package, 1);
186 if (Param1 != NULL) {
187 Status = ShellConvertStringToUint64(Param1, &Value, TRUE, FALSE);
188 TheHandle = ConvertHandleIndexToHandle((UINTN)Value);
189 }
190
191 if (EFI_ERROR(Status) || Param1 == NULL || TheHandle == NULL){
192 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_INV_HANDLE), gShellDriver1HiiHandle, Param1);
193 ShellStatus = SHELL_INVALID_PARAMETER;
194 } else {
195 ASSERT(TheHandle != NULL);
196 if (ShellCommandLineGetFlag(Package, L"-v") || ShellCommandLineGetFlag(Package, L"-verbose")) {
197 DumpLoadedImageProtocolInfo(TheHandle);
198 }
199
200 if (!ShellCommandLineGetFlag(Package, L"-n")) {
201 Status = ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_UNLOAD_CONF), gShellDriver1HiiHandle, (UINTN)TheHandle);
202 Status = ShellPromptForResponse(ShellPromptResponseTypeYesNo, NULL, (VOID**)&Resp);
203 }
204 if (ShellCommandLineGetFlag(Package, L"-n") || (Resp != NULL && *Resp == ShellPromptResponseYes)) {
205 Status = gBS->UnloadImage(TheHandle);
206 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_HANDLE_RESULT), gShellDriver1HiiHandle, L"Unload", (UINTN)TheHandle, Status);
207 }
208 SHELL_FREE_NON_NULL(Resp);
209 }
210 }
211 }
212 if (ShellStatus == SHELL_SUCCESS) {
213 if (Status == EFI_SECURITY_VIOLATION) {
214 ShellStatus = SHELL_SECURITY_VIOLATION;
215 } else if (Status == EFI_INVALID_PARAMETER) {
216 ShellStatus = SHELL_INVALID_PARAMETER;
217 } else if (EFI_ERROR(Status)) {
218 ShellStatus = SHELL_NOT_FOUND;
219 }
220 }
221
222 if (Package != NULL) {
223 ShellCommandLineFreeVarList(Package);
224 }
225
226 return (ShellStatus);
227 }