1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
/** @file
Main file for EfiCompress shell Debug1 function.
(C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "UefiShellDebug1CommandsLib.h"
#include "Compress.h"
/**
Compress the full contents of an input file and write the compressed payload
to an output file handle.
@param[in] InShellFileHandle Source file to read and compress.
@param[in] OutShellFileHandle Destination file to receive compressed data.
@param[in] OutFileName Name of the output file for error reporting.
@retval SHELL_SUCCESS Compression and write completed successfully.
@retval SHELL_OUT_OF_RESOURCES A required buffer allocation failed.
@retval SHELL_DEVICE_ERROR Read, compress, or write failed.
**/
STATIC
SHELL_STATUS
CompressFile (
IN SHELL_FILE_HANDLE InShellFileHandle,
IN SHELL_FILE_HANDLE OutShellFileHandle,
IN CONST CHAR16 *OutFileName
)
{
EFI_STATUS Status;
UINT64 OutSize;
UINTN OutSize2;
VOID *OutBuffer;
UINT64 InSize;
UINTN InSize2;
VOID *InBuffer;
SHELL_STATUS ShellStatus;
OutSize = 0;
OutBuffer = NULL;
InBuffer = NULL;
ShellStatus = SHELL_SUCCESS;
Status = gEfiShellProtocol->GetFileSize (InShellFileHandle, &InSize);
if (EFI_ERROR (Status)) {
ASSERT_EFI_ERROR (Status);
ShellPrintHiiDefaultEx (STRING_TOKEN (STR_EFI_COMPRESS_FAIL), gShellDebug1HiiHandle, Status);
return SHELL_DEVICE_ERROR;
}
InBuffer = AllocateZeroPool ((UINTN)InSize);
if (InBuffer == NULL) {
ShellPrintHiiDefaultEx (STRING_TOKEN (STR_EFI_COMPRESS_FAIL), gShellDebug1HiiHandle, EFI_OUT_OF_RESOURCES);
return SHELL_OUT_OF_RESOURCES;
}
InSize2 = (UINTN)InSize;
Status = gEfiShellProtocol->ReadFile (InShellFileHandle, &InSize2, InBuffer);
if (EFI_ERROR (Status)) {
ASSERT_EFI_ERROR (Status);
ShellPrintHiiDefaultEx (STRING_TOKEN (STR_EFI_COMPRESS_FAIL), gShellDebug1HiiHandle, Status);
ShellStatus = SHELL_DEVICE_ERROR;
goto Exit;
}
InSize = InSize2;
Status = Compress (InBuffer, InSize, OutBuffer, &OutSize);
if (Status == EFI_BUFFER_TOO_SMALL) {
OutBuffer = AllocateZeroPool ((UINTN)OutSize);
if (OutBuffer == NULL) {
ShellPrintHiiDefaultEx (STRING_TOKEN (STR_EFI_COMPRESS_FAIL), gShellDebug1HiiHandle, EFI_OUT_OF_RESOURCES);
ShellStatus = SHELL_OUT_OF_RESOURCES;
goto Exit;
}
Status = Compress (InBuffer, InSize, OutBuffer, &OutSize);
}
if (EFI_ERROR (Status)) {
ShellPrintHiiDefaultEx (STRING_TOKEN (STR_EFI_COMPRESS_FAIL), gShellDebug1HiiHandle, Status);
ShellStatus = SHELL_DEVICE_ERROR;
goto Exit;
}
OutSize2 = (UINTN)OutSize;
Status = gEfiShellProtocol->WriteFile (OutShellFileHandle, &OutSize2, OutBuffer);
if (EFI_ERROR (Status)) {
ShellPrintHiiDefaultEx (STRING_TOKEN (STR_FILE_WRITE_FAIL), gShellDebug1HiiHandle, L"eficompress", OutFileName);
ShellStatus = SHELL_DEVICE_ERROR;
goto Exit;
}
Exit:
SHELL_FREE_NON_NULL (InBuffer);
SHELL_FREE_NON_NULL (OutBuffer);
return ShellStatus;
}
/**
Validate that a path is not a directory and open it with the requested mode.
@param[in] FileName Path to open.
@param[in] OpenMode Mode passed to ShellOpenFileByName().
@param[out] ShellFileHandle Opened shell file handle.
@retval SHELL_SUCCESS The file was opened successfully.
@retval SHELL_INVALID_PARAMETER FileName names a directory.
@retval SHELL_NOT_FOUND The file could not be opened.
**/
STATIC
SHELL_STATUS
OpenFileHelper (
IN CONST CHAR16 *FileName,
IN UINT64 OpenMode,
OUT SHELL_FILE_HANDLE *ShellFileHandle
)
{
EFI_STATUS Status;
ASSERT (ShellFileHandle != NULL);
if (ShellIsDirectory (FileName) == EFI_SUCCESS) {
ShellPrintHiiDefaultEx (STRING_TOKEN (STR_FILE_NOT_DIR), gShellDebug1HiiHandle, L"eficompress", FileName);
return SHELL_INVALID_PARAMETER;
}
Status = ShellOpenFileByName (FileName, ShellFileHandle, OpenMode, 0);
if (EFI_ERROR (Status)) {
ShellPrintHiiDefaultEx (STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, L"eficompress", FileName);
return SHELL_NOT_FOUND;
}
return SHELL_SUCCESS;
}
/** Main function of the 'EfiCompress' command.
@param[in] Package List of input parameter for the command.
**/
STATIC
SHELL_STATUS
MainCmdEfiCompress (
LIST_ENTRY *Package
)
{
SHELL_STATUS ShellStatus;
SHELL_FILE_HANDLE InShellFileHandle;
SHELL_FILE_HANDLE OutShellFileHandle;
CHAR16 *InFileName;
CONST CHAR16 *OutFileName;
CONST CHAR16 *TempParam;
InFileName = NULL;
OutFileName = NULL;
ShellStatus = SHELL_SUCCESS;
InShellFileHandle = NULL;
OutShellFileHandle = NULL;
if (ShellCommandLineGetCount (Package) > 3) {
ShellPrintHiiDefaultEx (STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"eficompress");
return SHELL_INVALID_PARAMETER;
} else if (ShellCommandLineGetCount (Package) < 3) {
ShellPrintHiiDefaultEx (STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle, L"eficompress");
return SHELL_INVALID_PARAMETER;
}
TempParam = ShellCommandLineGetRawValue (Package, 1);
if (TempParam == NULL) {
ASSERT (TempParam != NULL);
ShellPrintHiiDefaultEx (STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"eficompress");
return SHELL_INVALID_PARAMETER;
}
InFileName = ShellFindFilePath (TempParam);
OutFileName = (CHAR16 *)ShellCommandLineGetRawValue (Package, 2);
if ((InFileName == NULL) || (OutFileName == NULL)) {
ShellPrintHiiDefaultEx (STRING_TOKEN (STR_FILE_FIND_FAIL), gShellDebug1HiiHandle, L"eficompress", TempParam);
ShellStatus = SHELL_NOT_FOUND;
goto Exit;
}
ShellStatus = OpenFileHelper (InFileName, EFI_FILE_MODE_READ, &InShellFileHandle);
if (ShellStatus != SHELL_SUCCESS) {
goto Exit;
}
ShellStatus = OpenFileHelper (OutFileName, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE|EFI_FILE_MODE_CREATE, &OutShellFileHandle);
if (ShellStatus != SHELL_SUCCESS) {
goto Exit;
}
ShellStatus = CompressFile (InShellFileHandle, OutShellFileHandle, OutFileName);
Exit:
if (InShellFileHandle != NULL) {
gEfiShellProtocol->CloseFile (InShellFileHandle);
}
if (OutShellFileHandle != NULL) {
gEfiShellProtocol->CloseFile (OutShellFileHandle);
}
SHELL_FREE_NON_NULL (InFileName);
return ShellStatus;
}
/**
Function for 'compress' command.
@param[in] ImageHandle Handle to the Image (NULL if Internal).
@param[in] SystemTable Pointer to the System Table (NULL if Internal).
**/
SHELL_STATUS
EFIAPI
ShellCommandRunEfiCompress (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
EFI_STATUS Status;
LIST_ENTRY *Package;
CHAR16 *ProblemParam;
SHELL_STATUS ShellStatus;
ShellStatus = SHELL_SUCCESS;
Status = EFI_SUCCESS;
Package = NULL;
//
// initialize the shell lib (we must be in non-auto-init...)
//
Status = ShellInitialize ();
ASSERT_EFI_ERROR (Status);
Status = CommandInit ();
ASSERT_EFI_ERROR (Status);
//
// parse the command line
//
Status = ShellCommandLineParse (EmptyParamList, &Package, &ProblemParam, TRUE);
if (EFI_ERROR (Status)) {
if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) {
ShellPrintHiiDefaultEx (STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"eficompress", ProblemParam);
FreePool (ProblemParam);
ShellStatus = SHELL_INVALID_PARAMETER;
} else {
ASSERT (FALSE);
}
return ShellStatus;
}
ShellStatus = MainCmdEfiCompress (Package);
ShellCommandLineFreeVarList (Package);
return (ShellStatus);
}
|