TianoCore EDK2 master
Loading...
Searching...
No Matches
EfiCompress.c
Go to the documentation of this file.
1
11#include "Compress.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 InShellFileHandle;
31 SHELL_FILE_HANDLE OutShellFileHandle;
32 UINT64 OutSize;
33 UINTN OutSize2;
34 VOID *OutBuffer;
35 UINT64 InSize;
36 UINTN InSize2;
37 VOID *InBuffer;
38 CHAR16 *InFileName;
39 CONST CHAR16 *OutFileName;
40 CONST CHAR16 *TempParam;
41
42 InFileName = NULL;
43 OutFileName = NULL;
44 OutSize = 0;
45 ShellStatus = SHELL_SUCCESS;
46 Status = EFI_SUCCESS;
47 OutBuffer = NULL;
48 InShellFileHandle = NULL;
49 OutShellFileHandle = NULL;
50 InBuffer = NULL;
51 Package = NULL;
52
53 //
54 // initialize the shell lib (we must be in non-auto-init...)
55 //
56 Status = ShellInitialize ();
57 ASSERT_EFI_ERROR (Status);
58
59 Status = CommandInit ();
60 ASSERT_EFI_ERROR (Status);
61
62 //
63 // parse the command line
64 //
65 Status = ShellCommandLineParse (EmptyParamList, &Package, &ProblemParam, TRUE);
66 if (EFI_ERROR (Status)) {
67 if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) {
68 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"eficompress", ProblemParam);
69 FreePool (ProblemParam);
70 ShellStatus = SHELL_INVALID_PARAMETER;
71 } else {
72 ASSERT (FALSE);
73 }
74 } else {
75 if (ShellCommandLineGetCount (Package) > 3) {
76 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"eficompress");
77 ShellStatus = SHELL_INVALID_PARAMETER;
78 } else if (ShellCommandLineGetCount (Package) < 3) {
79 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle, L"eficompress");
80 ShellStatus = SHELL_INVALID_PARAMETER;
81 } else {
82 TempParam = ShellCommandLineGetRawValue (Package, 1);
83 if (TempParam == NULL) {
84 ASSERT (TempParam != NULL);
85 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"eficompress");
86 ShellStatus = SHELL_INVALID_PARAMETER;
87 goto Exit;
88 }
89
90 InFileName = ShellFindFilePath (TempParam);
91 OutFileName = ShellCommandLineGetRawValue (Package, 2);
92 if ((InFileName == NULL) || (OutFileName == NULL)) {
93 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_FILE_FIND_FAIL), gShellDebug1HiiHandle, L"eficompress", TempParam);
94 ShellStatus = SHELL_NOT_FOUND;
95 } else {
96 if (ShellIsDirectory (InFileName) == EFI_SUCCESS) {
97 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_FILE_NOT_DIR), gShellDebug1HiiHandle, L"eficompress", InFileName);
98 ShellStatus = SHELL_INVALID_PARAMETER;
99 }
100
101 if (ShellIsDirectory (OutFileName) == EFI_SUCCESS) {
102 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_FILE_NOT_DIR), gShellDebug1HiiHandle, L"eficompress", OutFileName);
103 ShellStatus = SHELL_INVALID_PARAMETER;
104 }
105
106 if (ShellStatus == SHELL_SUCCESS) {
107 Status = ShellOpenFileByName (InFileName, &InShellFileHandle, EFI_FILE_MODE_READ, 0);
108 if (EFI_ERROR (Status)) {
109 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, L"eficompress", ShellCommandLineGetRawValue (Package, 1));
110 ShellStatus = SHELL_NOT_FOUND;
111 }
112
113 Status = ShellOpenFileByName (OutFileName, &OutShellFileHandle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE|EFI_FILE_MODE_CREATE, 0);
114 if (EFI_ERROR (Status)) {
115 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, L"eficompress", ShellCommandLineGetRawValue (Package, 2));
116 ShellStatus = SHELL_NOT_FOUND;
117 }
118 }
119
120 if (ShellStatus == SHELL_SUCCESS) {
121 Status = gEfiShellProtocol->GetFileSize (InShellFileHandle, &InSize);
122 ASSERT_EFI_ERROR (Status);
123 InBuffer = AllocateZeroPool ((UINTN)InSize);
124 if (InBuffer == NULL) {
125 Status = EFI_OUT_OF_RESOURCES;
126 } else {
127 InSize2 = (UINTN)InSize;
128 Status = gEfiShellProtocol->ReadFile (InShellFileHandle, &InSize2, InBuffer);
129 InSize = InSize2;
130 ASSERT_EFI_ERROR (Status);
131 Status = Compress (InBuffer, InSize, OutBuffer, &OutSize);
132 if (Status == EFI_BUFFER_TOO_SMALL) {
133 OutBuffer = AllocateZeroPool ((UINTN)OutSize);
134 if (OutBuffer == NULL) {
135 Status = EFI_OUT_OF_RESOURCES;
136 } else {
137 Status = Compress (InBuffer, InSize, OutBuffer, &OutSize);
138 }
139 }
140 }
141
142 if (EFI_ERROR (Status)) {
143 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_EFI_COMPRESS_FAIL), gShellDebug1HiiHandle, Status);
144 ShellStatus = ((Status == EFI_OUT_OF_RESOURCES) ? SHELL_OUT_OF_RESOURCES : SHELL_DEVICE_ERROR);
145 } else {
146 OutSize2 = (UINTN)OutSize;
147 Status = gEfiShellProtocol->WriteFile (OutShellFileHandle, &OutSize2, OutBuffer);
148 if (EFI_ERROR (Status)) {
149 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_FILE_WRITE_FAIL), gShellDebug1HiiHandle, L"eficompress", OutFileName);
150 ShellStatus = SHELL_DEVICE_ERROR;
151 }
152 }
153 }
154 }
155 }
156
158 }
159
160Exit:
161 if ((ShellStatus != SHELL_SUCCESS) && (Package != NULL)) {
163 }
164
165 if (InShellFileHandle != NULL) {
166 gEfiShellProtocol->CloseFile (InShellFileHandle);
167 }
168
169 if (OutShellFileHandle != NULL) {
170 gEfiShellProtocol->CloseFile (OutShellFileHandle);
171 }
172
173 SHELL_FREE_NON_NULL (InFileName);
174 SHELL_FREE_NON_NULL (InBuffer);
175 SHELL_FREE_NON_NULL (OutBuffer);
176
177 return (ShellStatus);
178}
UINT64 UINTN
EFI_STATUS Compress(IN VOID *SrcBuffer, IN UINT64 SrcSize, IN VOID *DstBuffer, IN OUT UINT64 *DstSize)
Definition: Compress.c:1324
SHELL_STATUS EFIAPI ShellCommandRunEfiCompress(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
Definition: EfiCompress.c:21
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
#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_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
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
VOID EFIAPI Exit(IN EFI_STATUS Status)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
#define STRING_TOKEN(t)