1 | /** @file
|
---|
2 | Main file for EfiCompress shell Debug1 function.
|
---|
3 |
|
---|
4 | (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
|
---|
5 | Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
|
---|
6 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
7 |
|
---|
8 | **/
|
---|
9 |
|
---|
10 | #include "UefiShellDebug1CommandsLib.h"
|
---|
11 | #include "Compress.h"
|
---|
12 |
|
---|
13 | /**
|
---|
14 | Function for 'compress' command.
|
---|
15 |
|
---|
16 | @param[in] ImageHandle Handle to the Image (NULL if Internal).
|
---|
17 | @param[in] SystemTable Pointer to the System Table (NULL if Internal).
|
---|
18 | **/
|
---|
19 | SHELL_STATUS
|
---|
20 | EFIAPI
|
---|
21 | ShellCommandRunEfiCompress (
|
---|
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 |
|
---|
52 | //
|
---|
53 | // initialize the shell lib (we must be in non-auto-init...)
|
---|
54 | //
|
---|
55 | Status = ShellInitialize ();
|
---|
56 | ASSERT_EFI_ERROR (Status);
|
---|
57 |
|
---|
58 | Status = CommandInit ();
|
---|
59 | ASSERT_EFI_ERROR (Status);
|
---|
60 |
|
---|
61 | //
|
---|
62 | // parse the command line
|
---|
63 | //
|
---|
64 | Status = ShellCommandLineParse (EmptyParamList, &Package, &ProblemParam, TRUE);
|
---|
65 | if (EFI_ERROR (Status)) {
|
---|
66 | if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) {
|
---|
67 | ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"eficompress", ProblemParam);
|
---|
68 | FreePool (ProblemParam);
|
---|
69 | ShellStatus = SHELL_INVALID_PARAMETER;
|
---|
70 | } else {
|
---|
71 | ASSERT (FALSE);
|
---|
72 | }
|
---|
73 | } else {
|
---|
74 | if (ShellCommandLineGetCount (Package) > 3) {
|
---|
75 | ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"eficompress");
|
---|
76 | ShellStatus = SHELL_INVALID_PARAMETER;
|
---|
77 | } else if (ShellCommandLineGetCount (Package) < 3) {
|
---|
78 | ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle, L"eficompress");
|
---|
79 | ShellStatus = SHELL_INVALID_PARAMETER;
|
---|
80 | } else {
|
---|
81 | TempParam = ShellCommandLineGetRawValue (Package, 1);
|
---|
82 | ASSERT (TempParam != NULL);
|
---|
83 | InFileName = ShellFindFilePath (TempParam);
|
---|
84 | OutFileName = ShellCommandLineGetRawValue (Package, 2);
|
---|
85 | if (InFileName == NULL) {
|
---|
86 | ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_FILE_FIND_FAIL), gShellDebug1HiiHandle, L"eficompress", TempParam);
|
---|
87 | ShellStatus = SHELL_NOT_FOUND;
|
---|
88 | } else {
|
---|
89 | if (ShellIsDirectory (InFileName) == EFI_SUCCESS) {
|
---|
90 | ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_FILE_NOT_DIR), gShellDebug1HiiHandle, L"eficompress", InFileName);
|
---|
91 | ShellStatus = SHELL_INVALID_PARAMETER;
|
---|
92 | }
|
---|
93 |
|
---|
94 | if (ShellIsDirectory (OutFileName) == EFI_SUCCESS) {
|
---|
95 | ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_FILE_NOT_DIR), gShellDebug1HiiHandle, L"eficompress", OutFileName);
|
---|
96 | ShellStatus = SHELL_INVALID_PARAMETER;
|
---|
97 | }
|
---|
98 |
|
---|
99 | if (ShellStatus == SHELL_SUCCESS) {
|
---|
100 | Status = ShellOpenFileByName (InFileName, &InShellFileHandle, EFI_FILE_MODE_READ, 0);
|
---|
101 | if (EFI_ERROR (Status)) {
|
---|
102 | ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, L"eficompress", ShellCommandLineGetRawValue (Package, 1));
|
---|
103 | ShellStatus = SHELL_NOT_FOUND;
|
---|
104 | }
|
---|
105 |
|
---|
106 | Status = ShellOpenFileByName (OutFileName, &OutShellFileHandle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE|EFI_FILE_MODE_CREATE, 0);
|
---|
107 | if (EFI_ERROR (Status)) {
|
---|
108 | ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, L"eficompress", ShellCommandLineGetRawValue (Package, 2));
|
---|
109 | ShellStatus = SHELL_NOT_FOUND;
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | if (ShellStatus == SHELL_SUCCESS) {
|
---|
114 | Status = gEfiShellProtocol->GetFileSize (InShellFileHandle, &InSize);
|
---|
115 | ASSERT_EFI_ERROR (Status);
|
---|
116 | InBuffer = AllocateZeroPool ((UINTN)InSize);
|
---|
117 | if (InBuffer == NULL) {
|
---|
118 | Status = EFI_OUT_OF_RESOURCES;
|
---|
119 | } else {
|
---|
120 | InSize2 = (UINTN)InSize;
|
---|
121 | Status = gEfiShellProtocol->ReadFile (InShellFileHandle, &InSize2, InBuffer);
|
---|
122 | InSize = InSize2;
|
---|
123 | ASSERT_EFI_ERROR (Status);
|
---|
124 | Status = Compress (InBuffer, InSize, OutBuffer, &OutSize);
|
---|
125 | if (Status == EFI_BUFFER_TOO_SMALL) {
|
---|
126 | OutBuffer = AllocateZeroPool ((UINTN)OutSize);
|
---|
127 | if (OutBuffer == NULL) {
|
---|
128 | Status = EFI_OUT_OF_RESOURCES;
|
---|
129 | } else {
|
---|
130 | Status = Compress (InBuffer, InSize, OutBuffer, &OutSize);
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | if (EFI_ERROR (Status)) {
|
---|
136 | ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_EFI_COMPRESS_FAIL), gShellDebug1HiiHandle, Status);
|
---|
137 | ShellStatus = ((Status == EFI_OUT_OF_RESOURCES) ? SHELL_OUT_OF_RESOURCES : SHELL_DEVICE_ERROR);
|
---|
138 | } else {
|
---|
139 | OutSize2 = (UINTN)OutSize;
|
---|
140 | Status = gEfiShellProtocol->WriteFile (OutShellFileHandle, &OutSize2, OutBuffer);
|
---|
141 | if (EFI_ERROR (Status)) {
|
---|
142 | ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_FILE_WRITE_FAIL), gShellDebug1HiiHandle, L"eficompress", OutFileName);
|
---|
143 | ShellStatus = SHELL_DEVICE_ERROR;
|
---|
144 | }
|
---|
145 | }
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | ShellCommandLineFreeVarList (Package);
|
---|
151 | }
|
---|
152 |
|
---|
153 | if (InShellFileHandle != NULL) {
|
---|
154 | gEfiShellProtocol->CloseFile (InShellFileHandle);
|
---|
155 | }
|
---|
156 |
|
---|
157 | if (OutShellFileHandle != NULL) {
|
---|
158 | gEfiShellProtocol->CloseFile (OutShellFileHandle);
|
---|
159 | }
|
---|
160 |
|
---|
161 | SHELL_FREE_NON_NULL (InFileName);
|
---|
162 | SHELL_FREE_NON_NULL (InBuffer);
|
---|
163 | SHELL_FREE_NON_NULL (OutBuffer);
|
---|
164 |
|
---|
165 | return (ShellStatus);
|
---|
166 | }
|
---|