TianoCore EDK2 master
Loading...
Searching...
No Matches
EfiDecompress.c
Go to the documentation of this file.
1
11#include <Protocol/Decompress.h>
12
20EFIAPI
22 IN EFI_HANDLE ImageHandle,
23 IN EFI_SYSTEM_TABLE *SystemTable
24 )
25{
26 EFI_STATUS Status;
27 LIST_ENTRY *Package;
28 CHAR16 *ProblemParam;
29 SHELL_STATUS ShellStatus;
30 SHELL_FILE_HANDLE InFileHandle;
31 SHELL_FILE_HANDLE OutFileHandle;
32 UINT32 OutSize;
33 UINTN OutSizeTemp;
34 VOID *OutBuffer;
35 UINTN InSize;
36 VOID *InBuffer;
37 CHAR16 *InFileName;
38 CONST CHAR16 *OutFileName;
39 UINT64 Temp64Bit;
40 UINT32 ScratchSize;
41 VOID *ScratchBuffer;
43 CONST CHAR16 *TempParam;
44
45 InFileName = NULL;
46 OutFileName = NULL;
47 OutSize = 0;
48 ScratchSize = 0;
49 ShellStatus = SHELL_SUCCESS;
50 Status = EFI_SUCCESS;
51 OutBuffer = NULL;
52 InBuffer = NULL;
53 ScratchBuffer = NULL;
54 InFileHandle = NULL;
55 OutFileHandle = NULL;
57
58 //
59 // initialize the shell lib (we must be in non-auto-init...)
60 //
61 Status = ShellInitialize ();
62 ASSERT_EFI_ERROR (Status);
63
64 Status = CommandInit ();
65 ASSERT_EFI_ERROR (Status);
66
67 //
68 // parse the command line
69 //
70 Status = ShellCommandLineParse (EmptyParamList, &Package, &ProblemParam, TRUE);
71 if (EFI_ERROR (Status)) {
72 if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) {
73 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"efidecompress", ProblemParam);
74 FreePool (ProblemParam);
75 ShellStatus = SHELL_INVALID_PARAMETER;
76 } else {
77 ASSERT (FALSE);
78 }
79 } else {
80 if (ShellCommandLineGetCount (Package) > 3) {
81 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"efidecompress");
82 ShellStatus = SHELL_INVALID_PARAMETER;
83 } else if (ShellCommandLineGetCount (Package) < 3) {
84 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle, L"efidecompress");
85 ShellStatus = SHELL_INVALID_PARAMETER;
86 } else {
87 TempParam = ShellCommandLineGetRawValue (Package, 1);
88 if (TempParam == NULL) {
89 ASSERT (TempParam != NULL);
90 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"efidecompress");
91 ShellStatus = SHELL_INVALID_PARAMETER;
92 goto Done;
93 }
94
95 InFileName = ShellFindFilePath (TempParam);
96 OutFileName = ShellCommandLineGetRawValue (Package, 2);
97 if ((InFileName == NULL) || (OutFileName == NULL)) {
98 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_FILE_FIND_FAIL), gShellDebug1HiiHandle, L"efidecompress", TempParam);
99 ShellStatus = SHELL_NOT_FOUND;
100 } else {
101 if (ShellIsDirectory (InFileName) == EFI_SUCCESS) {
102 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_FILE_NOT_DIR), gShellDebug1HiiHandle, L"efidecompress", InFileName);
103 ShellStatus = SHELL_INVALID_PARAMETER;
104 }
105
106 if (ShellIsDirectory (OutFileName) == EFI_SUCCESS) {
107 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_FILE_NOT_DIR), gShellDebug1HiiHandle, L"efidecompress", OutFileName);
108 ShellStatus = SHELL_INVALID_PARAMETER;
109 }
110
111 if (ShellStatus == SHELL_SUCCESS) {
112 Status = ShellOpenFileByName (InFileName, &InFileHandle, EFI_FILE_MODE_READ, 0);
113 if (EFI_ERROR (Status)) {
114 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, L"efidecompress", ShellCommandLineGetRawValue (Package, 1));
115 ShellStatus = SHELL_NOT_FOUND;
116 }
117 }
118
119 if (ShellStatus == SHELL_SUCCESS) {
120 Status = FileHandleGetSize (InFileHandle, &Temp64Bit);
121 if (EFI_ERROR (Status)) {
122 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, L"efidecompress", ShellCommandLineGetRawValue (Package, 1));
123 ShellStatus = SHELL_NOT_FOUND;
124 }
125 }
126
127 if (ShellStatus == SHELL_SUCCESS) {
128 //
129 // Limit the File Size to UINT32, even though calls accept UINTN.
130 // 32 bits = 4gb.
131 //
132 Status = SafeUint64ToUint32 (Temp64Bit, (UINT32 *)&InSize);
133 if (EFI_ERROR (Status)) {
134 ASSERT_EFI_ERROR (Status);
135 ShellStatus = SHELL_BAD_BUFFER_SIZE;
136 goto Done;
137 }
138
139 InBuffer = AllocateZeroPool (InSize);
140 if (InBuffer == NULL) {
141 Status = EFI_OUT_OF_RESOURCES;
142 } else {
143 Status = gEfiShellProtocol->ReadFile (InFileHandle, &InSize, InBuffer);
144 ASSERT_EFI_ERROR (Status);
145
146 Status = gBS->LocateProtocol (&gEfiDecompressProtocolGuid, NULL, (VOID **)&Decompress);
147 ASSERT_EFI_ERROR (Status);
148
149 Status = Decompress->GetInfo (Decompress, InBuffer, (UINT32)InSize, &OutSize, &ScratchSize);
150 }
151
152 if (EFI_ERROR (Status) || (OutSize == 0)) {
153 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_EFI_DECOMPRESS_NOPE), gShellDebug1HiiHandle, InFileName);
154 ShellStatus = SHELL_NOT_FOUND;
155 } else {
156 Status = ShellOpenFileByName (OutFileName, &OutFileHandle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE|EFI_FILE_MODE_CREATE, 0);
157 if (EFI_ERROR (Status)) {
158 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_FILE_OPEN_FAIL), gShellDebug1HiiHandle, ShellCommandLineGetRawValue (Package, 2), Status);
159 ShellStatus = SHELL_NOT_FOUND;
160 } else {
161 OutBuffer = AllocateZeroPool (OutSize);
162 ScratchBuffer = AllocateZeroPool (ScratchSize);
163 if ((OutBuffer == NULL) || (ScratchBuffer == NULL)) {
164 Status = EFI_OUT_OF_RESOURCES;
165 } else {
166 Status = Decompress->Decompress (Decompress, InBuffer, (UINT32)InSize, OutBuffer, OutSize, ScratchBuffer, ScratchSize);
167 }
168 }
169 }
170
171 if (EFI_ERROR (Status)) {
172 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_EFI_DECOMPRESS_FAIL), gShellDebug1HiiHandle, Status);
173 ShellStatus = ((Status == EFI_OUT_OF_RESOURCES) ? SHELL_OUT_OF_RESOURCES : SHELL_DEVICE_ERROR);
174 } else {
175 OutSizeTemp = OutSize;
176 Status = gEfiShellProtocol->WriteFile (OutFileHandle, &OutSizeTemp, OutBuffer);
177 OutSize = (UINT32)OutSizeTemp;
178 if (EFI_ERROR (Status)) {
179 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_FILE_WRITE_FAIL), gShellDebug1HiiHandle, L"efidecompress", OutFileName, Status);
180 ShellStatus = SHELL_DEVICE_ERROR;
181 }
182 }
183 }
184 }
185 }
186
187Done:
188
190 }
191
192 if (InFileHandle != NULL) {
193 gEfiShellProtocol->CloseFile (InFileHandle);
194 }
195
196 if (OutFileHandle != NULL) {
197 gEfiShellProtocol->CloseFile (OutFileHandle);
198 }
199
200 SHELL_FREE_NON_NULL (InFileName);
201 SHELL_FREE_NON_NULL (InBuffer);
202 SHELL_FREE_NON_NULL (OutBuffer);
203 SHELL_FREE_NON_NULL (ScratchBuffer);
204
205 return (ShellStatus);
206}
UINT64 UINTN
SHELL_STATUS EFIAPI ShellCommandRunEfiDecompress(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
Definition: EfiDecompress.c:21
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
EFI_STATUS EFIAPI FileHandleGetSize(IN EFI_FILE_HANDLE FileHandle, OUT UINT64 *Size)
EFI_STATUS EFIAPI Decompress(IN CONST EFI_PEI_DECOMPRESS_PPI *This, IN CONST EFI_COMPRESSION_SECTION *CompressionSection, OUT VOID **OutputBuffer, OUT UINTN *OutputSize)
Definition: DxeLoad.c:674
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
SHELL_STATUS
Definition: Shell.h:21
@ SHELL_OUT_OF_RESOURCES
Definition: Shell.h:73
@ SHELL_BAD_BUFFER_SIZE
Definition: Shell.h:45
@ SHELL_SUCCESS
Definition: Shell.h:25
@ SHELL_NOT_FOUND
Definition: Shell.h:101
@ SHELL_DEVICE_ERROR
Definition: Shell.h:63
@ SHELL_INVALID_PARAMETER
Definition: Shell.h:35
RETURN_STATUS EFIAPI SafeUint64ToUint32(IN UINT64 Operand, OUT UINT32 *Result)
Definition: SafeIntLib.c:2698
EFI_STATUS EFIAPI CommandInit(VOID)
#define ShellCommandLineParse(CheckList, CheckPackage, ProblemParam, AutoPageBreak)
Make it easy to upgrade from older versions of the shell library.
Definition: ShellLib.h:755
EFI_STATUS EFIAPI ShellPrintHiiEx(IN INT32 Col OPTIONAL, IN INT32 Row OPTIONAL, IN CONST CHAR8 *Language OPTIONAL, IN CONST EFI_STRING_ID HiiFormatStringId, IN CONST EFI_HII_HANDLE HiiFormatHandle,...)
EFI_STATUS EFIAPI ShellOpenFileByName(IN CONST CHAR16 *FileName, OUT SHELL_FILE_HANDLE *FileHandle, IN UINT64 OpenMode, IN UINT64 Attributes)
Definition: UefiShellLib.c:720
VOID EFIAPI ShellCommandLineFreeVarList(IN LIST_ENTRY *CheckPackage)
EFI_STATUS EFIAPI ShellInitialize(VOID)
Definition: UefiShellLib.c:532
EFI_STATUS EFIAPI ShellIsDirectory(IN CONST CHAR16 *DirName)
CHAR16 *EFIAPI ShellFindFilePath(IN CONST CHAR16 *FileName)
CONST CHAR16 *EFIAPI ShellCommandLineGetRawValue(IN CONST LIST_ENTRY *CONST CheckPackage, IN UINTN Position)
UINTN EFIAPI ShellCommandLineGetCount(IN CONST LIST_ENTRY *CheckPackage)
SHELL_PARAM_ITEM EmptyParamList[]
Helper structure for no parameters (besides -? and -b)
Definition: UefiShellLib.c:19
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_BOOT_SERVICES * gBS
#define STRING_TOKEN(t)