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