Commit | Line | Data |
---|---|---|
68074260 RC |
1 | /** @file\r |
2 | The implementation for the 'tftp' Shell command.\r | |
3 | \r | |
4 | Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>\r | |
ba0014b9 | 5 | Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved. <BR>\r |
6add86ab | 6 | (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>\r |
68074260 RC |
7 | \r |
8 | This program and the accompanying materials\r | |
9 | are licensed and made available under the terms and conditions of the BSD License\r | |
10 | which accompanies this distribution. The full text of the license may be found at\r | |
11 | http://opensource.org/licenses/bsd-license.php.\r | |
12 | \r | |
13 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
14 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
15 | **/\r | |
16 | \r | |
09610023 | 17 | #include "Tftp.h"\r |
68074260 | 18 | \r |
6add86ab | 19 | #define IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH 32\r |
09610023 | 20 | EFI_HANDLE mTftpHiiHandle;\r |
6add86ab | 21 | \r |
68074260 RC |
22 | /*\r |
23 | Constant strings and definitions related to the message indicating the amount of\r | |
24 | progress in the dowloading of a TFTP file.\r | |
25 | */\r | |
26 | \r | |
27 | // Frame for the progression slider\r | |
28 | STATIC CONST CHAR16 mTftpProgressFrame[] = L"[ ]";\r | |
29 | \r | |
30 | // Number of steps in the progression slider\r | |
31 | #define TFTP_PROGRESS_SLIDER_STEPS ((sizeof (mTftpProgressFrame) / sizeof (CHAR16)) - 3)\r | |
32 | \r | |
33 | // Size in number of characters plus one (final zero) of the message to\r | |
34 | // indicate the progress of a TFTP download. The format is "[(progress slider:\r | |
35 | // 40 characters)] (nb of KBytes downloaded so far: 7 characters) Kb". There\r | |
36 | // are thus the number of characters in mTftpProgressFrame[] plus 11 characters\r | |
37 | // (2 // spaces, "Kb" and seven characters for the number of KBytes).\r | |
38 | #define TFTP_PROGRESS_MESSAGE_SIZE ((sizeof (mTftpProgressFrame) / sizeof (CHAR16)) + 12)\r | |
39 | \r | |
40 | // String to delete the TFTP progress message to be able to update it :\r | |
41 | // (TFTP_PROGRESS_MESSAGE_SIZE-1) '\b'\r | |
42 | STATIC CONST CHAR16 mTftpProgressDelete[] = L"\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";\r | |
43 | \r | |
b85c8fb4 AL |
44 | // Local File Handle\r |
45 | SHELL_FILE_HANDLE mFileHandle;\r | |
46 | \r | |
47 | // Path of the local file, Unicode encoded\r | |
48 | CONST CHAR16 *mLocalFilePath;\r | |
49 | \r | |
0fcf66a4 QS |
50 | /**\r |
51 | Check and convert the UINT16 option values of the 'tftp' command\r | |
52 | \r | |
53 | @param[in] ValueStr Value as an Unicode encoded string\r | |
54 | @param[out] Value UINT16 value\r | |
55 | \r | |
56 | @return TRUE The value was returned.\r | |
57 | @return FALSE A parsing error occured.\r | |
58 | **/\r | |
ba0014b9 LG |
59 | STATIC\r |
60 | BOOLEAN\r | |
0fcf66a4 | 61 | StringToUint16 (\r |
68074260 RC |
62 | IN CONST CHAR16 *ValueStr,\r |
63 | OUT UINT16 *Value\r | |
64 | );\r | |
65 | \r | |
0fcf66a4 QS |
66 | /**\r |
67 | Get the name of the NIC.\r | |
68 | \r | |
69 | @param[in] ControllerHandle The network physical device handle.\r | |
70 | @param[in] NicNumber The network physical device number.\r | |
71 | @param[out] NicName Address where to store the NIC name.\r | |
72 | The memory area has to be at least\r | |
ba0014b9 | 73 | IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH\r |
6add86ab | 74 | double byte wide.\r |
0fcf66a4 QS |
75 | \r |
76 | @return EFI_SUCCESS The name of the NIC was returned.\r | |
77 | @return Others The creation of the child for the Managed\r | |
78 | Network Service failed or the opening of\r | |
79 | the Managed Network Protocol failed or\r | |
80 | the operational parameters for the\r | |
81 | Managed Network Protocol could not be\r | |
82 | read.\r | |
83 | **/\r | |
ba0014b9 LG |
84 | STATIC\r |
85 | EFI_STATUS\r | |
0fcf66a4 | 86 | GetNicName (\r |
68074260 | 87 | IN EFI_HANDLE ControllerHandle,\r |
a0f2af3a | 88 | IN UINTN NicNumber,\r |
68074260 RC |
89 | OUT CHAR16 *NicName\r |
90 | );\r | |
91 | \r | |
0fcf66a4 QS |
92 | /**\r |
93 | Create a child for the service identified by its service binding protocol GUID\r | |
94 | and get from the child the interface of the protocol identified by its GUID.\r | |
95 | \r | |
96 | @param[in] ControllerHandle Controller handle.\r | |
97 | @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the\r | |
98 | service to be created.\r | |
99 | @param[in] ProtocolGuid GUID of the protocol to be open.\r | |
100 | @param[out] ChildHandle Address where the handler of the\r | |
101 | created child is returned. NULL is\r | |
102 | returned in case of error.\r | |
103 | @param[out] Interface Address where a pointer to the\r | |
104 | protocol interface is returned in\r | |
105 | case of success.\r | |
106 | \r | |
107 | @return EFI_SUCCESS The child was created and the protocol opened.\r | |
108 | @return Others Either the creation of the child or the opening\r | |
109 | of the protocol failed.\r | |
110 | **/\r | |
ba0014b9 LG |
111 | STATIC\r |
112 | EFI_STATUS\r | |
0fcf66a4 | 113 | CreateServiceChildAndOpenProtocol (\r |
68074260 RC |
114 | IN EFI_HANDLE ControllerHandle,\r |
115 | IN EFI_GUID *ServiceBindingProtocolGuid,\r | |
116 | IN EFI_GUID *ProtocolGuid,\r | |
117 | OUT EFI_HANDLE *ChildHandle,\r | |
118 | OUT VOID **Interface\r | |
119 | );\r | |
120 | \r | |
0fcf66a4 QS |
121 | /**\r |
122 | Close the protocol identified by its GUID on the child handle of the service\r | |
123 | identified by its service binding protocol GUID, then destroy the child\r | |
124 | handle.\r | |
125 | \r | |
126 | @param[in] ControllerHandle Controller handle.\r | |
127 | @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the\r | |
128 | service to be destroyed.\r | |
129 | @param[in] ProtocolGuid GUID of the protocol to be closed.\r | |
130 | @param[in] ChildHandle Handle of the child to be destroyed.\r | |
131 | \r | |
132 | **/\r | |
ba0014b9 LG |
133 | STATIC\r |
134 | VOID\r | |
0fcf66a4 | 135 | CloseProtocolAndDestroyServiceChild (\r |
68074260 RC |
136 | IN EFI_HANDLE ControllerHandle,\r |
137 | IN EFI_GUID *ServiceBindingProtocolGuid,\r | |
138 | IN EFI_GUID *ProtocolGuid,\r | |
139 | IN EFI_HANDLE ChildHandle\r | |
140 | );\r | |
141 | \r | |
0fcf66a4 QS |
142 | /**\r |
143 | Worker function that gets the size in numbers of bytes of a file from a TFTP\r | |
144 | server before to download the file.\r | |
145 | \r | |
146 | @param[in] Mtftp4 MTFTP4 protocol interface\r | |
147 | @param[in] FilePath Path of the file, ASCII encoded\r | |
148 | @param[out] FileSize Address where to store the file size in number of\r | |
149 | bytes.\r | |
150 | \r | |
151 | @retval EFI_SUCCESS The size of the file was returned.\r | |
152 | @retval EFI_UNSUPPORTED The server does not support the "tsize" option.\r | |
153 | @retval Others Error when retrieving the information from the server\r | |
154 | (see EFI_MTFTP4_PROTOCOL.GetInfo() status codes)\r | |
155 | or error when parsing the response of the server.\r | |
156 | **/\r | |
ba0014b9 LG |
157 | STATIC\r |
158 | EFI_STATUS\r | |
0fcf66a4 | 159 | GetFileSize (\r |
68074260 RC |
160 | IN EFI_MTFTP4_PROTOCOL *Mtftp4,\r |
161 | IN CONST CHAR8 *FilePath,\r | |
162 | OUT UINTN *FileSize\r | |
163 | );\r | |
164 | \r | |
0fcf66a4 QS |
165 | /**\r |
166 | Worker function that download the data of a file from a TFTP server given\r | |
167 | the path of the file and its size.\r | |
168 | \r | |
169 | @param[in] Mtftp4 MTFTP4 protocol interface\r | |
170 | @param[in] FilePath Path of the file, Unicode encoded\r | |
171 | @param[in] AsciiFilePath Path of the file, ASCII encoded\r | |
172 | @param[in] FileSize Size of the file in number of bytes\r | |
2be45bfe | 173 | @param[in] BlockSize Value of the TFTP blksize option\r |
a7ab1c31 | 174 | @param[in] WindowSize Value of the TFTP window size option\r |
0fcf66a4 QS |
175 | \r |
176 | @retval EFI_SUCCESS The file was downloaded.\r | |
177 | @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r | |
178 | @retval Others The downloading of the file from the server failed\r | |
179 | (see EFI_MTFTP4_PROTOCOL.ReadFile() status codes).\r | |
180 | \r | |
181 | **/\r | |
ba0014b9 LG |
182 | STATIC\r |
183 | EFI_STATUS\r | |
0fcf66a4 | 184 | DownloadFile (\r |
68074260 RC |
185 | IN EFI_MTFTP4_PROTOCOL *Mtftp4,\r |
186 | IN CONST CHAR16 *FilePath,\r | |
187 | IN CONST CHAR8 *AsciiFilePath,\r | |
188 | IN UINTN FileSize,\r | |
2be45bfe | 189 | IN UINT16 BlockSize,\r |
b85c8fb4 | 190 | IN UINT16 WindowSize\r |
68074260 RC |
191 | );\r |
192 | \r | |
0fcf66a4 QS |
193 | /**\r |
194 | Update the progress of a file download\r | |
195 | This procedure is called each time a new TFTP packet is received.\r | |
196 | \r | |
197 | @param[in] This MTFTP4 protocol interface\r | |
198 | @param[in] Token Parameters for the download of the file\r | |
199 | @param[in] PacketLen Length of the packet\r | |
200 | @param[in] Packet Address of the packet\r | |
201 | \r | |
202 | @retval EFI_SUCCESS All packets are accepted.\r | |
203 | \r | |
204 | **/\r | |
ba0014b9 LG |
205 | STATIC\r |
206 | EFI_STATUS\r | |
7367e66f | 207 | EFIAPI\r |
0fcf66a4 | 208 | CheckPacket (\r |
68074260 RC |
209 | IN EFI_MTFTP4_PROTOCOL *This,\r |
210 | IN EFI_MTFTP4_TOKEN *Token,\r | |
211 | IN UINT16 PacketLen,\r | |
212 | IN EFI_MTFTP4_PACKET *Packet\r | |
213 | );\r | |
214 | \r | |
215 | EFI_MTFTP4_CONFIG_DATA DefaultMtftp4ConfigData = {\r | |
216 | TRUE, // Use default setting\r | |
217 | { { 0, 0, 0, 0 } }, // StationIp - Not relevant as UseDefaultSetting=TRUE\r | |
218 | { { 0, 0, 0, 0 } }, // SubnetMask - Not relevant as UseDefaultSetting=TRUE\r | |
219 | 0, // LocalPort - Automatically assigned port number.\r | |
220 | { { 0, 0, 0, 0 } }, // GatewayIp - Not relevant as UseDefaultSetting=TRUE\r | |
221 | { { 0, 0, 0, 0 } }, // ServerIp - Not known yet\r | |
222 | 69, // InitialServerPort - Standard TFTP server port\r | |
62ea70e3 | 223 | 6, // TryCount - The number of times to transmit request packets and wait for a response.\r |
68074260 RC |
224 | 4 // TimeoutValue - Retransmission timeout in seconds.\r |
225 | };\r | |
226 | \r | |
227 | STATIC CONST SHELL_PARAM_ITEM ParamList[] = {\r | |
228 | {L"-i", TypeValue},\r | |
229 | {L"-l", TypeValue},\r | |
230 | {L"-r", TypeValue},\r | |
231 | {L"-c", TypeValue},\r | |
232 | {L"-t", TypeValue},\r | |
2be45bfe | 233 | {L"-s", TypeValue},\r |
5c6fdb5e | 234 | {L"-w", TypeValue},\r |
68074260 RC |
235 | {NULL , TypeMax}\r |
236 | };\r | |
237 | \r | |
2be45bfe FS |
238 | ///\r |
239 | /// The default block size (512) of tftp is defined in the RFC1350.\r | |
240 | ///\r | |
241 | #define MTFTP_DEFAULT_BLKSIZE 512\r | |
242 | ///\r | |
243 | /// The valid range of block size option is defined in the RFC2348.\r | |
244 | ///\r | |
245 | #define MTFTP_MIN_BLKSIZE 8\r | |
246 | #define MTFTP_MAX_BLKSIZE 65464\r | |
5c6fdb5e JW |
247 | ///\r |
248 | /// The default windowsize (1) of tftp.\r | |
249 | ///\r | |
250 | #define MTFTP_DEFAULT_WINDOWSIZE 1\r | |
251 | ///\r | |
252 | /// The valid range of window size option.\r | |
253 | /// Note that: RFC 7440 does not mention max window size value, but for the\r | |
254 | /// stability reason, the value is limited to 64.\r | |
255 | ///\r | |
256 | #define MTFTP_MIN_WINDOWSIZE 1\r | |
257 | #define MTFTP_MAX_WINDOWSIZE 64\r | |
2be45bfe | 258 | \r |
68074260 RC |
259 | /**\r |
260 | Function for 'tftp' command.\r | |
261 | \r | |
262 | @param[in] ImageHandle Handle to the Image (NULL if Internal).\r | |
263 | @param[in] SystemTable Pointer to the System Table (NULL if Internal).\r | |
264 | \r | |
265 | @return SHELL_SUCCESS The 'tftp' command completed successfully.\r | |
266 | @return SHELL_ABORTED The Shell Library initialization failed.\r | |
267 | @return SHELL_INVALID_PARAMETER At least one of the command's arguments is\r | |
268 | not valid.\r | |
269 | @return SHELL_OUT_OF_RESOURCES A memory allocation failed.\r | |
270 | @return SHELL_NOT_FOUND Network Interface Card not found or server\r | |
271 | error or file error.\r | |
272 | \r | |
273 | **/\r | |
274 | SHELL_STATUS\r | |
09610023 | 275 | RunTftp (\r |
68074260 RC |
276 | IN EFI_HANDLE ImageHandle,\r |
277 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
278 | )\r | |
279 | {\r | |
280 | SHELL_STATUS ShellStatus;\r | |
281 | EFI_STATUS Status;\r | |
282 | LIST_ENTRY *CheckPackage;\r | |
283 | CHAR16 *ProblemParam;\r | |
284 | UINTN ParamCount;\r | |
285 | CONST CHAR16 *UserNicName;\r | |
286 | BOOLEAN NicFound;\r | |
287 | CONST CHAR16 *ValueStr;\r | |
288 | CONST CHAR16 *RemoteFilePath;\r | |
289 | CHAR8 *AsciiRemoteFilePath;\r | |
46213c8e | 290 | UINTN FilePathSize;\r |
68074260 | 291 | CONST CHAR16 *Walker;\r |
68074260 RC |
292 | EFI_MTFTP4_CONFIG_DATA Mtftp4ConfigData;\r |
293 | EFI_HANDLE *Handles;\r | |
294 | UINTN HandleCount;\r | |
295 | UINTN NicNumber;\r | |
6add86ab | 296 | CHAR16 NicName[IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH];\r |
68074260 RC |
297 | EFI_HANDLE ControllerHandle;\r |
298 | EFI_HANDLE Mtftp4ChildHandle;\r | |
299 | EFI_MTFTP4_PROTOCOL *Mtftp4;\r | |
300 | UINTN FileSize;\r | |
2be45bfe | 301 | UINT16 BlockSize;\r |
5c6fdb5e | 302 | UINT16 WindowSize;\r |
68074260 RC |
303 | \r |
304 | ShellStatus = SHELL_INVALID_PARAMETER;\r | |
305 | ProblemParam = NULL;\r | |
306 | NicFound = FALSE;\r | |
307 | AsciiRemoteFilePath = NULL;\r | |
308 | Handles = NULL;\r | |
6add86ab | 309 | FileSize = 0;\r |
2be45bfe | 310 | BlockSize = MTFTP_DEFAULT_BLKSIZE;\r |
5c6fdb5e | 311 | WindowSize = MTFTP_DEFAULT_WINDOWSIZE;\r |
68074260 RC |
312 | \r |
313 | //\r | |
314 | // Initialize the Shell library (we must be in non-auto-init...)\r | |
315 | //\r | |
316 | Status = ShellInitialize ();\r | |
317 | if (EFI_ERROR (Status)) {\r | |
318 | ASSERT_EFI_ERROR (Status);\r | |
319 | return SHELL_ABORTED;\r | |
320 | }\r | |
321 | \r | |
322 | //\r | |
323 | // Parse the command line.\r | |
324 | //\r | |
325 | Status = ShellCommandLineParse (ParamList, &CheckPackage, &ProblemParam, TRUE);\r | |
326 | if (EFI_ERROR (Status)) {\r | |
327 | if ((Status == EFI_VOLUME_CORRUPTED) &&\r | |
328 | (ProblemParam != NULL) ) {\r | |
329 | ShellPrintHiiEx (\r | |
09610023 | 330 | -1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), mTftpHiiHandle,\r |
68074260 RC |
331 | L"tftp", ProblemParam\r |
332 | );\r | |
333 | FreePool (ProblemParam);\r | |
334 | } else {\r | |
335 | ASSERT (FALSE);\r | |
336 | }\r | |
337 | goto Error;\r | |
338 | }\r | |
339 | \r | |
340 | //\r | |
341 | // Check the number of parameters\r | |
342 | //\r | |
343 | ParamCount = ShellCommandLineGetCount (CheckPackage);\r | |
344 | if (ParamCount > 4) {\r | |
345 | ShellPrintHiiEx (\r | |
346 | -1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY),\r | |
09610023 | 347 | mTftpHiiHandle, L"tftp"\r |
68074260 RC |
348 | );\r |
349 | goto Error;\r | |
350 | }\r | |
351 | if (ParamCount < 3) {\r | |
352 | ShellPrintHiiEx (\r | |
353 | -1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW),\r | |
09610023 | 354 | mTftpHiiHandle, L"tftp"\r |
68074260 RC |
355 | );\r |
356 | goto Error;\r | |
357 | }\r | |
358 | \r | |
ba601bdd | 359 | CopyMem (&Mtftp4ConfigData, &DefaultMtftp4ConfigData, sizeof (EFI_MTFTP4_CONFIG_DATA));\r |
68074260 RC |
360 | \r |
361 | //\r | |
362 | // Check the host IPv4 address\r | |
363 | //\r | |
364 | ValueStr = ShellCommandLineGetRawValue (CheckPackage, 1);\r | |
365 | Status = NetLibStrToIp4 (ValueStr, &Mtftp4ConfigData.ServerIp);\r | |
366 | if (EFI_ERROR (Status)) {\r | |
367 | ShellPrintHiiEx (\r | |
368 | -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV),\r | |
09610023 | 369 | mTftpHiiHandle, L"tftp", ValueStr\r |
68074260 RC |
370 | );\r |
371 | goto Error;\r | |
372 | }\r | |
373 | \r | |
374 | RemoteFilePath = ShellCommandLineGetRawValue (CheckPackage, 2);\r | |
c6173804 | 375 | ASSERT(RemoteFilePath != NULL);\r |
46213c8e SZ |
376 | FilePathSize = StrLen (RemoteFilePath) + 1;\r |
377 | AsciiRemoteFilePath = AllocatePool (FilePathSize);\r | |
68074260 RC |
378 | if (AsciiRemoteFilePath == NULL) {\r |
379 | ShellStatus = SHELL_OUT_OF_RESOURCES;\r | |
380 | goto Error;\r | |
381 | }\r | |
46213c8e | 382 | UnicodeStrToAsciiStrS (RemoteFilePath, AsciiRemoteFilePath, FilePathSize);\r |
68074260 RC |
383 | \r |
384 | if (ParamCount == 4) {\r | |
b85c8fb4 | 385 | mLocalFilePath = ShellCommandLineGetRawValue (CheckPackage, 3);\r |
68074260 RC |
386 | } else {\r |
387 | Walker = RemoteFilePath + StrLen (RemoteFilePath);\r | |
388 | while ((--Walker) >= RemoteFilePath) {\r | |
389 | if ((*Walker == L'\\') ||\r | |
390 | (*Walker == L'/' ) ) {\r | |
391 | break;\r | |
392 | }\r | |
393 | }\r | |
b85c8fb4 | 394 | mLocalFilePath = Walker + 1;\r |
68074260 RC |
395 | }\r |
396 | \r | |
397 | //\r | |
398 | // Get the name of the Network Interface Card to be used if any.\r | |
399 | //\r | |
400 | UserNicName = ShellCommandLineGetValue (CheckPackage, L"-i");\r | |
401 | \r | |
402 | ValueStr = ShellCommandLineGetValue (CheckPackage, L"-l");\r | |
403 | if (ValueStr != NULL) {\r | |
404 | if (!StringToUint16 (ValueStr, &Mtftp4ConfigData.LocalPort)) {\r | |
405 | goto Error;\r | |
406 | }\r | |
407 | }\r | |
408 | \r | |
409 | ValueStr = ShellCommandLineGetValue (CheckPackage, L"-r");\r | |
410 | if (ValueStr != NULL) {\r | |
411 | if (!StringToUint16 (ValueStr, &Mtftp4ConfigData.InitialServerPort)) {\r | |
412 | goto Error;\r | |
413 | }\r | |
414 | }\r | |
415 | \r | |
416 | ValueStr = ShellCommandLineGetValue (CheckPackage, L"-c");\r | |
417 | if (ValueStr != NULL) {\r | |
418 | if (!StringToUint16 (ValueStr, &Mtftp4ConfigData.TryCount)) {\r | |
419 | goto Error;\r | |
420 | }\r | |
62ea70e3 JW |
421 | \r |
422 | if (Mtftp4ConfigData.TryCount == 0) {\r | |
423 | Mtftp4ConfigData.TryCount = 6;\r | |
424 | }\r | |
68074260 RC |
425 | }\r |
426 | \r | |
427 | ValueStr = ShellCommandLineGetValue (CheckPackage, L"-t");\r | |
428 | if (ValueStr != NULL) {\r | |
429 | if (!StringToUint16 (ValueStr, &Mtftp4ConfigData.TimeoutValue)) {\r | |
430 | goto Error;\r | |
431 | }\r | |
432 | if (Mtftp4ConfigData.TimeoutValue == 0) {\r | |
433 | ShellPrintHiiEx (\r | |
434 | -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV),\r | |
09610023 | 435 | mTftpHiiHandle, L"tftp", ValueStr\r |
68074260 RC |
436 | );\r |
437 | goto Error;\r | |
438 | }\r | |
439 | }\r | |
440 | \r | |
2be45bfe FS |
441 | ValueStr = ShellCommandLineGetValue (CheckPackage, L"-s");\r |
442 | if (ValueStr != NULL) {\r | |
443 | if (!StringToUint16 (ValueStr, &BlockSize)) {\r | |
444 | goto Error;\r | |
445 | }\r | |
446 | if (BlockSize < MTFTP_MIN_BLKSIZE || BlockSize > MTFTP_MAX_BLKSIZE) {\r | |
447 | ShellPrintHiiEx (\r | |
448 | -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV),\r | |
09610023 | 449 | mTftpHiiHandle, L"tftp", ValueStr\r |
2be45bfe FS |
450 | );\r |
451 | goto Error;\r | |
452 | }\r | |
453 | }\r | |
454 | \r | |
5c6fdb5e JW |
455 | ValueStr = ShellCommandLineGetValue (CheckPackage, L"-w");\r |
456 | if (ValueStr != NULL) {\r | |
457 | if (!StringToUint16 (ValueStr, &WindowSize)) {\r | |
458 | goto Error;\r | |
459 | }\r | |
460 | if (WindowSize < MTFTP_MIN_WINDOWSIZE || WindowSize > MTFTP_MAX_WINDOWSIZE) {\r | |
461 | ShellPrintHiiEx (\r | |
462 | -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV),\r | |
463 | mTftpHiiHandle, L"tftp", ValueStr\r | |
464 | );\r | |
465 | goto Error;\r | |
466 | }\r | |
467 | }\r | |
468 | \r | |
68074260 RC |
469 | //\r |
470 | // Locate all MTFTP4 Service Binding protocols\r | |
471 | //\r | |
472 | ShellStatus = SHELL_NOT_FOUND;\r | |
473 | Status = gBS->LocateHandleBuffer (\r | |
474 | ByProtocol,\r | |
475 | &gEfiManagedNetworkServiceBindingProtocolGuid,\r | |
476 | NULL,\r | |
477 | &HandleCount,\r | |
478 | &Handles\r | |
479 | );\r | |
480 | if (EFI_ERROR (Status) || (HandleCount == 0)) {\r | |
481 | ShellPrintHiiEx (\r | |
482 | -1, -1, NULL, STRING_TOKEN (STR_TFTP_ERR_NO_NIC),\r | |
09610023 | 483 | mTftpHiiHandle\r |
68074260 RC |
484 | );\r |
485 | goto Error;\r | |
486 | }\r | |
487 | \r | |
488 | for (NicNumber = 0;\r | |
489 | (NicNumber < HandleCount) && (ShellStatus != SHELL_SUCCESS);\r | |
490 | NicNumber++) {\r | |
491 | ControllerHandle = Handles[NicNumber];\r | |
68074260 RC |
492 | \r |
493 | Status = GetNicName (ControllerHandle, NicNumber, NicName);\r | |
494 | if (EFI_ERROR (Status)) {\r | |
495 | ShellPrintHiiEx (\r | |
496 | -1, -1, NULL, STRING_TOKEN (STR_TFTP_ERR_NIC_NAME),\r | |
09610023 | 497 | mTftpHiiHandle, NicNumber, Status\r |
68074260 RC |
498 | );\r |
499 | continue;\r | |
500 | }\r | |
501 | \r | |
502 | if (UserNicName != NULL) {\r | |
503 | if (StrCmp (NicName, UserNicName) != 0) {\r | |
504 | continue;\r | |
505 | }\r | |
506 | NicFound = TRUE;\r | |
507 | }\r | |
508 | \r | |
509 | Status = CreateServiceChildAndOpenProtocol (\r | |
510 | ControllerHandle,\r | |
511 | &gEfiMtftp4ServiceBindingProtocolGuid,\r | |
512 | &gEfiMtftp4ProtocolGuid,\r | |
513 | &Mtftp4ChildHandle,\r | |
514 | (VOID**)&Mtftp4\r | |
515 | );\r | |
516 | if (EFI_ERROR (Status)) {\r | |
517 | ShellPrintHiiEx (\r | |
518 | -1, -1, NULL, STRING_TOKEN (STR_TFTP_ERR_OPEN_PROTOCOL),\r | |
09610023 | 519 | mTftpHiiHandle, NicName, Status\r |
68074260 RC |
520 | );\r |
521 | continue;\r | |
522 | }\r | |
523 | \r | |
524 | Status = Mtftp4->Configure (Mtftp4, &Mtftp4ConfigData);\r | |
525 | if (EFI_ERROR (Status)) {\r | |
526 | ShellPrintHiiEx (\r | |
527 | -1, -1, NULL, STRING_TOKEN (STR_TFTP_ERR_CONFIGURE),\r | |
09610023 | 528 | mTftpHiiHandle, NicName, Status\r |
68074260 RC |
529 | );\r |
530 | goto NextHandle;\r | |
531 | }\r | |
532 | \r | |
533 | Status = GetFileSize (Mtftp4, AsciiRemoteFilePath, &FileSize);\r | |
534 | if (EFI_ERROR (Status)) {\r | |
535 | ShellPrintHiiEx (\r | |
536 | -1, -1, NULL, STRING_TOKEN (STR_TFTP_ERR_FILE_SIZE),\r | |
09610023 | 537 | mTftpHiiHandle, RemoteFilePath, NicName, Status\r |
68074260 RC |
538 | );\r |
539 | goto NextHandle;\r | |
540 | }\r | |
541 | \r | |
b85c8fb4 | 542 | Status = DownloadFile (Mtftp4, RemoteFilePath, AsciiRemoteFilePath, FileSize, BlockSize, WindowSize);\r |
68074260 RC |
543 | if (EFI_ERROR (Status)) {\r |
544 | ShellPrintHiiEx (\r | |
545 | -1, -1, NULL, STRING_TOKEN (STR_TFTP_ERR_DOWNLOAD),\r | |
09610023 | 546 | mTftpHiiHandle, RemoteFilePath, NicName, Status\r |
68074260 RC |
547 | );\r |
548 | goto NextHandle;\r | |
549 | }\r | |
550 | \r | |
68074260 RC |
551 | NextHandle:\r |
552 | \r | |
68074260 RC |
553 | CloseProtocolAndDestroyServiceChild (\r |
554 | ControllerHandle,\r | |
555 | &gEfiMtftp4ServiceBindingProtocolGuid,\r | |
556 | &gEfiMtftp4ProtocolGuid,\r | |
557 | Mtftp4ChildHandle\r | |
558 | );\r | |
559 | }\r | |
560 | \r | |
a0f2af3a | 561 | if ((UserNicName != NULL) && (!NicFound)) {\r |
68074260 RC |
562 | ShellPrintHiiEx (\r |
563 | -1, -1, NULL, STRING_TOKEN (STR_TFTP_ERR_NIC_NOT_FOUND),\r | |
09610023 | 564 | mTftpHiiHandle, UserNicName\r |
68074260 RC |
565 | );\r |
566 | }\r | |
567 | \r | |
568 | Error:\r | |
569 | \r | |
570 | ShellCommandLineFreeVarList (CheckPackage);\r | |
571 | if (AsciiRemoteFilePath != NULL) {\r | |
572 | FreePool (AsciiRemoteFilePath);\r | |
573 | }\r | |
574 | if (Handles != NULL) {\r | |
575 | FreePool (Handles);\r | |
576 | }\r | |
577 | \r | |
578 | return ShellStatus;\r | |
579 | }\r | |
580 | \r | |
581 | /**\r | |
582 | Check and convert the UINT16 option values of the 'tftp' command\r | |
583 | \r | |
584 | @param[in] ValueStr Value as an Unicode encoded string\r | |
585 | @param[out] Value UINT16 value\r | |
586 | \r | |
587 | @return TRUE The value was returned.\r | |
588 | @return FALSE A parsing error occured.\r | |
589 | **/\r | |
590 | STATIC\r | |
591 | BOOLEAN\r | |
592 | StringToUint16 (\r | |
593 | IN CONST CHAR16 *ValueStr,\r | |
594 | OUT UINT16 *Value\r | |
595 | )\r | |
596 | {\r | |
597 | UINTN Val;\r | |
598 | \r | |
599 | Val = ShellStrToUintn (ValueStr);\r | |
600 | if (Val > MAX_UINT16) {\r | |
601 | ShellPrintHiiEx (\r | |
602 | -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV),\r | |
09610023 | 603 | mTftpHiiHandle, L"tftp", ValueStr\r |
68074260 RC |
604 | );\r |
605 | return FALSE;\r | |
606 | }\r | |
607 | \r | |
6add86ab | 608 | *Value = (UINT16)Val;\r |
68074260 RC |
609 | return TRUE;\r |
610 | }\r | |
611 | \r | |
612 | /**\r | |
613 | Get the name of the NIC.\r | |
614 | \r | |
615 | @param[in] ControllerHandle The network physical device handle.\r | |
616 | @param[in] NicNumber The network physical device number.\r | |
617 | @param[out] NicName Address where to store the NIC name.\r | |
618 | The memory area has to be at least\r | |
ba0014b9 | 619 | IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH\r |
6add86ab | 620 | double byte wide.\r |
68074260 RC |
621 | \r |
622 | @return EFI_SUCCESS The name of the NIC was returned.\r | |
623 | @return Others The creation of the child for the Managed\r | |
624 | Network Service failed or the opening of\r | |
625 | the Managed Network Protocol failed or\r | |
626 | the operational parameters for the\r | |
627 | Managed Network Protocol could not be\r | |
628 | read.\r | |
629 | **/\r | |
630 | STATIC\r | |
631 | EFI_STATUS\r | |
632 | GetNicName (\r | |
633 | IN EFI_HANDLE ControllerHandle,\r | |
634 | IN UINTN NicNumber,\r | |
635 | OUT CHAR16 *NicName\r | |
636 | )\r | |
637 | {\r | |
638 | EFI_STATUS Status;\r | |
639 | EFI_HANDLE MnpHandle;\r | |
640 | EFI_MANAGED_NETWORK_PROTOCOL *Mnp;\r | |
641 | EFI_SIMPLE_NETWORK_MODE SnpMode;\r | |
642 | \r | |
643 | Status = CreateServiceChildAndOpenProtocol (\r | |
644 | ControllerHandle,\r | |
645 | &gEfiManagedNetworkServiceBindingProtocolGuid,\r | |
646 | &gEfiManagedNetworkProtocolGuid,\r | |
647 | &MnpHandle,\r | |
648 | (VOID**)&Mnp\r | |
649 | );\r | |
650 | if (EFI_ERROR (Status)) {\r | |
651 | goto Error;\r | |
652 | }\r | |
653 | \r | |
654 | Status = Mnp->GetModeData (Mnp, NULL, &SnpMode);\r | |
655 | if (EFI_ERROR (Status) && (Status != EFI_NOT_STARTED)) {\r | |
656 | goto Error;\r | |
657 | }\r | |
658 | \r | |
659 | UnicodeSPrint (\r | |
660 | NicName,\r | |
6add86ab | 661 | IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH,\r |
68074260 RC |
662 | SnpMode.IfType == NET_IFTYPE_ETHERNET ?\r |
663 | L"eth%d" :\r | |
664 | L"unk%d" ,\r | |
665 | NicNumber\r | |
666 | );\r | |
667 | \r | |
668 | Status = EFI_SUCCESS;\r | |
669 | \r | |
670 | Error:\r | |
671 | \r | |
672 | if (MnpHandle != NULL) {\r | |
673 | CloseProtocolAndDestroyServiceChild (\r | |
674 | ControllerHandle,\r | |
675 | &gEfiManagedNetworkServiceBindingProtocolGuid,\r | |
676 | &gEfiManagedNetworkProtocolGuid,\r | |
677 | MnpHandle\r | |
678 | );\r | |
679 | }\r | |
680 | \r | |
681 | return Status;\r | |
682 | }\r | |
683 | \r | |
684 | /**\r | |
685 | Create a child for the service identified by its service binding protocol GUID\r | |
686 | and get from the child the interface of the protocol identified by its GUID.\r | |
687 | \r | |
688 | @param[in] ControllerHandle Controller handle.\r | |
689 | @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the\r | |
690 | service to be created.\r | |
691 | @param[in] ProtocolGuid GUID of the protocol to be open.\r | |
692 | @param[out] ChildHandle Address where the handler of the\r | |
693 | created child is returned. NULL is\r | |
694 | returned in case of error.\r | |
695 | @param[out] Interface Address where a pointer to the\r | |
696 | protocol interface is returned in\r | |
697 | case of success.\r | |
698 | \r | |
699 | @return EFI_SUCCESS The child was created and the protocol opened.\r | |
700 | @return Others Either the creation of the child or the opening\r | |
701 | of the protocol failed.\r | |
702 | **/\r | |
703 | STATIC\r | |
704 | EFI_STATUS\r | |
705 | CreateServiceChildAndOpenProtocol (\r | |
706 | IN EFI_HANDLE ControllerHandle,\r | |
707 | IN EFI_GUID *ServiceBindingProtocolGuid,\r | |
708 | IN EFI_GUID *ProtocolGuid,\r | |
709 | OUT EFI_HANDLE *ChildHandle,\r | |
710 | OUT VOID **Interface\r | |
711 | )\r | |
712 | {\r | |
713 | EFI_STATUS Status;\r | |
714 | \r | |
715 | *ChildHandle = NULL;\r | |
716 | Status = NetLibCreateServiceChild (\r | |
717 | ControllerHandle,\r | |
718 | gImageHandle,\r | |
719 | ServiceBindingProtocolGuid,\r | |
720 | ChildHandle\r | |
721 | );\r | |
722 | if (!EFI_ERROR (Status)) {\r | |
723 | Status = gBS->OpenProtocol (\r | |
724 | *ChildHandle,\r | |
725 | ProtocolGuid,\r | |
726 | Interface,\r | |
727 | gImageHandle,\r | |
728 | ControllerHandle,\r | |
729 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r | |
730 | );\r | |
731 | if (EFI_ERROR (Status)) {\r | |
732 | NetLibDestroyServiceChild (\r | |
733 | ControllerHandle,\r | |
734 | gImageHandle,\r | |
735 | ServiceBindingProtocolGuid,\r | |
736 | *ChildHandle\r | |
737 | );\r | |
738 | *ChildHandle = NULL;\r | |
739 | }\r | |
740 | }\r | |
741 | \r | |
742 | return Status;\r | |
743 | }\r | |
744 | \r | |
745 | /**\r | |
746 | Close the protocol identified by its GUID on the child handle of the service\r | |
747 | identified by its service binding protocol GUID, then destroy the child\r | |
748 | handle.\r | |
749 | \r | |
750 | @param[in] ControllerHandle Controller handle.\r | |
751 | @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the\r | |
752 | service to be destroyed.\r | |
753 | @param[in] ProtocolGuid GUID of the protocol to be closed.\r | |
754 | @param[in] ChildHandle Handle of the child to be destroyed.\r | |
755 | \r | |
756 | **/\r | |
757 | STATIC\r | |
758 | VOID\r | |
759 | CloseProtocolAndDestroyServiceChild (\r | |
760 | IN EFI_HANDLE ControllerHandle,\r | |
761 | IN EFI_GUID *ServiceBindingProtocolGuid,\r | |
762 | IN EFI_GUID *ProtocolGuid,\r | |
763 | IN EFI_HANDLE ChildHandle\r | |
764 | )\r | |
765 | {\r | |
766 | gBS->CloseProtocol (\r | |
767 | ChildHandle,\r | |
768 | ProtocolGuid,\r | |
769 | gImageHandle,\r | |
770 | ControllerHandle\r | |
771 | );\r | |
772 | \r | |
773 | NetLibDestroyServiceChild (\r | |
774 | ControllerHandle,\r | |
775 | gImageHandle,\r | |
776 | ServiceBindingProtocolGuid,\r | |
777 | ChildHandle\r | |
778 | );\r | |
779 | }\r | |
780 | \r | |
781 | /**\r | |
782 | Worker function that gets the size in numbers of bytes of a file from a TFTP\r | |
783 | server before to download the file.\r | |
784 | \r | |
785 | @param[in] Mtftp4 MTFTP4 protocol interface\r | |
786 | @param[in] FilePath Path of the file, ASCII encoded\r | |
787 | @param[out] FileSize Address where to store the file size in number of\r | |
788 | bytes.\r | |
789 | \r | |
790 | @retval EFI_SUCCESS The size of the file was returned.\r | |
791 | @retval EFI_UNSUPPORTED The server does not support the "tsize" option.\r | |
792 | @retval Others Error when retrieving the information from the server\r | |
793 | (see EFI_MTFTP4_PROTOCOL.GetInfo() status codes)\r | |
794 | or error when parsing the response of the server.\r | |
795 | **/\r | |
796 | STATIC\r | |
797 | EFI_STATUS\r | |
798 | GetFileSize (\r | |
799 | IN EFI_MTFTP4_PROTOCOL *Mtftp4,\r | |
800 | IN CONST CHAR8 *FilePath,\r | |
801 | OUT UINTN *FileSize\r | |
802 | )\r | |
803 | {\r | |
804 | EFI_STATUS Status;\r | |
805 | EFI_MTFTP4_OPTION ReqOpt[1];\r | |
806 | EFI_MTFTP4_PACKET *Packet;\r | |
807 | UINT32 PktLen;\r | |
808 | EFI_MTFTP4_OPTION *TableOfOptions;\r | |
809 | EFI_MTFTP4_OPTION *Option;\r | |
810 | UINT32 OptCnt;\r | |
811 | UINT8 OptBuf[128];\r | |
812 | \r | |
813 | ReqOpt[0].OptionStr = (UINT8*)"tsize";\r | |
814 | OptBuf[0] = '0';\r | |
815 | OptBuf[1] = 0;\r | |
816 | ReqOpt[0].ValueStr = OptBuf;\r | |
817 | \r | |
818 | Status = Mtftp4->GetInfo (\r | |
819 | Mtftp4,\r | |
820 | NULL,\r | |
821 | (UINT8*)FilePath,\r | |
822 | NULL,\r | |
823 | 1,\r | |
824 | ReqOpt,\r | |
825 | &PktLen,\r | |
826 | &Packet\r | |
827 | );\r | |
828 | \r | |
829 | if (EFI_ERROR (Status)) {\r | |
830 | goto Error;\r | |
831 | }\r | |
832 | \r | |
833 | Status = Mtftp4->ParseOptions (\r | |
834 | Mtftp4,\r | |
835 | PktLen,\r | |
836 | Packet,\r | |
837 | (UINT32 *) &OptCnt,\r | |
838 | &TableOfOptions\r | |
839 | );\r | |
840 | if (EFI_ERROR (Status)) {\r | |
841 | goto Error;\r | |
842 | }\r | |
843 | \r | |
844 | Option = TableOfOptions;\r | |
845 | while (OptCnt != 0) {\r | |
846 | if (AsciiStrnCmp ((CHAR8 *)Option->OptionStr, "tsize", 5) == 0) {\r | |
847 | *FileSize = AsciiStrDecimalToUintn ((CHAR8 *)Option->ValueStr);\r | |
848 | break;\r | |
849 | }\r | |
850 | OptCnt--;\r | |
851 | Option++;\r | |
852 | }\r | |
853 | FreePool (TableOfOptions);\r | |
854 | \r | |
855 | if (OptCnt == 0) {\r | |
856 | Status = EFI_UNSUPPORTED;\r | |
857 | }\r | |
858 | \r | |
859 | Error :\r | |
860 | \r | |
861 | return Status;\r | |
862 | }\r | |
863 | \r | |
864 | /**\r | |
865 | Worker function that download the data of a file from a TFTP server given\r | |
866 | the path of the file and its size.\r | |
867 | \r | |
868 | @param[in] Mtftp4 MTFTP4 protocol interface\r | |
869 | @param[in] FilePath Path of the file, Unicode encoded\r | |
870 | @param[in] AsciiFilePath Path of the file, ASCII encoded\r | |
871 | @param[in] FileSize Size of the file in number of bytes\r | |
2be45bfe | 872 | @param[in] BlockSize Value of the TFTP blksize option\r |
a7ab1c31 | 873 | @param[in] WindowSize Value of the TFTP window size option\r |
68074260 RC |
874 | \r |
875 | @retval EFI_SUCCESS The file was downloaded.\r | |
876 | @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r | |
877 | @retval Others The downloading of the file from the server failed\r | |
878 | (see EFI_MTFTP4_PROTOCOL.ReadFile() status codes).\r | |
879 | \r | |
880 | **/\r | |
881 | STATIC\r | |
882 | EFI_STATUS\r | |
883 | DownloadFile (\r | |
884 | IN EFI_MTFTP4_PROTOCOL *Mtftp4,\r | |
885 | IN CONST CHAR16 *FilePath,\r | |
886 | IN CONST CHAR8 *AsciiFilePath,\r | |
887 | IN UINTN FileSize,\r | |
2be45bfe | 888 | IN UINT16 BlockSize,\r |
b85c8fb4 | 889 | IN UINT16 WindowSize\r |
68074260 RC |
890 | )\r |
891 | {\r | |
892 | EFI_STATUS Status;\r | |
68074260 RC |
893 | DOWNLOAD_CONTEXT *TftpContext;\r |
894 | EFI_MTFTP4_TOKEN Mtftp4Token;\r | |
5c6fdb5e JW |
895 | UINT8 BlksizeBuf[10];\r |
896 | UINT8 WindowsizeBuf[10];\r | |
68074260 | 897 | \r |
61d3f100 JW |
898 | ZeroMem (&Mtftp4Token, sizeof (EFI_MTFTP4_TOKEN));\r |
899 | \r | |
68074260 RC |
900 | TftpContext = AllocatePool (sizeof (DOWNLOAD_CONTEXT));\r |
901 | if (TftpContext == NULL) {\r | |
902 | Status = EFI_OUT_OF_RESOURCES;\r | |
903 | goto Error;\r | |
904 | }\r | |
905 | TftpContext->FileSize = FileSize;\r | |
906 | TftpContext->DownloadedNbOfBytes = 0;\r | |
907 | TftpContext->LastReportedNbOfBytes = 0;\r | |
908 | \r | |
68074260 | 909 | Mtftp4Token.Filename = (UINT8*)AsciiFilePath;\r |
68074260 RC |
910 | Mtftp4Token.CheckPacket = CheckPacket;\r |
911 | Mtftp4Token.Context = (VOID*)TftpContext;\r | |
5c6fdb5e JW |
912 | Mtftp4Token.OptionCount = 0;\r |
913 | Mtftp4Token.OptionList = AllocatePool (sizeof (EFI_MTFTP4_OPTION) * 2);\r | |
914 | if (Mtftp4Token.OptionList == NULL) {\r | |
915 | Status = EFI_OUT_OF_RESOURCES;\r | |
916 | goto Error;\r | |
917 | }\r | |
918 | \r | |
2be45bfe | 919 | if (BlockSize != MTFTP_DEFAULT_BLKSIZE) {\r |
5c6fdb5e JW |
920 | Mtftp4Token.OptionList[Mtftp4Token.OptionCount].OptionStr = (UINT8 *) "blksize";\r |
921 | AsciiSPrint ((CHAR8 *) BlksizeBuf, sizeof (BlksizeBuf), "%d", BlockSize);\r | |
922 | Mtftp4Token.OptionList[Mtftp4Token.OptionCount].ValueStr = BlksizeBuf;\r | |
923 | Mtftp4Token.OptionCount ++;\r | |
924 | }\r | |
2be45bfe | 925 | \r |
5c6fdb5e JW |
926 | if (WindowSize != MTFTP_DEFAULT_WINDOWSIZE) {\r |
927 | Mtftp4Token.OptionList[Mtftp4Token.OptionCount].OptionStr = (UINT8 *) "windowsize";\r | |
928 | AsciiSPrint ((CHAR8 *) WindowsizeBuf, sizeof (WindowsizeBuf), "%d", WindowSize);\r | |
929 | Mtftp4Token.OptionList[Mtftp4Token.OptionCount].ValueStr = WindowsizeBuf;\r | |
930 | Mtftp4Token.OptionCount ++;\r | |
2be45bfe | 931 | }\r |
68074260 RC |
932 | \r |
933 | ShellPrintHiiEx (\r | |
934 | -1, -1, NULL, STRING_TOKEN (STR_TFTP_DOWNLOADING),\r | |
09610023 | 935 | mTftpHiiHandle, FilePath\r |
68074260 RC |
936 | );\r |
937 | \r | |
b85c8fb4 AL |
938 | //\r |
939 | // OPEN FILE\r | |
940 | //\r | |
941 | if (!EFI_ERROR (ShellFileExists (mLocalFilePath))) {\r | |
942 | ShellDeleteFileByName (mLocalFilePath);\r | |
943 | }\r | |
944 | \r | |
945 | Status = ShellOpenFileByName (\r | |
946 | mLocalFilePath,\r | |
947 | &mFileHandle,\r | |
948 | EFI_FILE_MODE_CREATE |\r | |
949 | EFI_FILE_MODE_WRITE |\r | |
950 | EFI_FILE_MODE_READ,\r | |
951 | 0\r | |
952 | );\r | |
953 | if (EFI_ERROR (Status)) {\r | |
954 | ShellPrintHiiEx (\r | |
955 | -1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL),\r | |
956 | mTftpHiiHandle, L"tftp", mLocalFilePath\r | |
957 | );\r | |
958 | goto Error;\r | |
959 | }\r | |
960 | \r | |
68074260 RC |
961 | Status = Mtftp4->ReadFile (Mtftp4, &Mtftp4Token);\r |
962 | ShellPrintHiiEx (\r | |
963 | -1, -1, NULL, STRING_TOKEN (STR_GEN_CRLF),\r | |
09610023 | 964 | mTftpHiiHandle\r |
68074260 RC |
965 | );\r |
966 | \r | |
b85c8fb4 AL |
967 | //\r |
968 | // CLOSE FILE\r | |
969 | //\r | |
970 | ShellCloseFile (&mFileHandle);\r | |
68074260 | 971 | \r |
b85c8fb4 | 972 | Error :\r |
5c6fdb5e | 973 | if (TftpContext != NULL) {\r |
68074260 RC |
974 | FreePool (TftpContext);\r |
975 | }\r | |
976 | \r | |
5c6fdb5e JW |
977 | if (Mtftp4Token.OptionList != NULL) {\r |
978 | FreePool (Mtftp4Token.OptionList);\r | |
979 | }\r | |
980 | \r | |
b85c8fb4 | 981 | return Status;\r |
68074260 RC |
982 | }\r |
983 | \r | |
984 | /**\r | |
985 | Update the progress of a file download\r | |
986 | This procedure is called each time a new TFTP packet is received.\r | |
987 | \r | |
988 | @param[in] This MTFTP4 protocol interface\r | |
989 | @param[in] Token Parameters for the download of the file\r | |
990 | @param[in] PacketLen Length of the packet\r | |
991 | @param[in] Packet Address of the packet\r | |
992 | \r | |
993 | @retval EFI_SUCCESS All packets are accepted.\r | |
994 | \r | |
995 | **/\r | |
996 | STATIC\r | |
997 | EFI_STATUS\r | |
7367e66f | 998 | EFIAPI\r |
68074260 RC |
999 | CheckPacket (\r |
1000 | IN EFI_MTFTP4_PROTOCOL *This,\r | |
1001 | IN EFI_MTFTP4_TOKEN *Token,\r | |
1002 | IN UINT16 PacketLen,\r | |
1003 | IN EFI_MTFTP4_PACKET *Packet\r | |
1004 | )\r | |
1005 | {\r | |
1006 | DOWNLOAD_CONTEXT *Context;\r | |
1007 | CHAR16 Progress[TFTP_PROGRESS_MESSAGE_SIZE];\r | |
6add86ab | 1008 | UINTN NbOfKb;\r |
68074260 | 1009 | UINTN Index;\r |
310b053a JC |
1010 | UINTN LastStep;\r |
1011 | UINTN Step;\r | |
b85c8fb4 | 1012 | UINTN DownloadLen;\r |
da6b8feb | 1013 | EFI_STATUS Status;\r |
68074260 RC |
1014 | \r |
1015 | if ((NTOHS (Packet->OpCode)) != EFI_MTFTP4_OPCODE_DATA) {\r | |
1016 | return EFI_SUCCESS;\r | |
1017 | }\r | |
1018 | \r | |
1019 | Context = (DOWNLOAD_CONTEXT*)Token->Context;\r | |
68074260 RC |
1020 | \r |
1021 | //\r | |
1022 | // The data in the packet are prepended with two UINT16 :\r | |
1023 | // . OpCode = EFI_MTFTP4_OPCODE_DATA\r | |
1024 | // . Block = the number of this block of data\r | |
1025 | //\r | |
b85c8fb4 AL |
1026 | DownloadLen = (UINTN)PacketLen - sizeof (Packet->OpCode) - sizeof (Packet->Data.Block);\r |
1027 | \r | |
1028 | ShellSetFilePosition(mFileHandle, Context->DownloadedNbOfBytes);\r | |
1029 | Status = ShellWriteFile (mFileHandle, &DownloadLen, Packet->Data.Data);\r | |
1030 | if (EFI_ERROR (Status)) {\r | |
1031 | if (Context->DownloadedNbOfBytes > 0) {\r | |
1032 | ShellPrintHiiEx (\r | |
1033 | -1, -1, NULL, STRING_TOKEN (STR_GEN_CRLF),\r | |
1034 | mTftpHiiHandle\r | |
1035 | );\r | |
1036 | }\r | |
1037 | ShellPrintHiiEx (\r | |
1038 | -1, -1, NULL, STRING_TOKEN (STR_TFTP_ERR_WRITE),\r | |
1039 | mTftpHiiHandle, mLocalFilePath, Status\r | |
1040 | );\r | |
1041 | return Status;\r | |
1042 | }\r | |
1043 | \r | |
1044 | if (Context->DownloadedNbOfBytes == 0) {\r | |
1045 | ShellPrintEx (-1, -1, L"%s 0 Kb", mTftpProgressFrame);\r | |
1046 | }\r | |
1047 | \r | |
1048 | Context->DownloadedNbOfBytes += DownloadLen;\r | |
68074260 RC |
1049 | NbOfKb = Context->DownloadedNbOfBytes / 1024;\r |
1050 | \r | |
1051 | Progress[0] = L'\0';\r | |
6add86ab JC |
1052 | LastStep = (Context->LastReportedNbOfBytes * TFTP_PROGRESS_SLIDER_STEPS) / Context->FileSize;\r |
1053 | Step = (Context->DownloadedNbOfBytes * TFTP_PROGRESS_SLIDER_STEPS) / Context->FileSize;\r | |
1054 | \r | |
68074260 RC |
1055 | if (Step <= LastStep) {\r |
1056 | return EFI_SUCCESS;\r | |
1057 | }\r | |
1058 | \r | |
1059 | ShellPrintEx (-1, -1, L"%s", mTftpProgressDelete);\r | |
1060 | \r | |
da6b8feb QS |
1061 | Status = StrCpyS (Progress, TFTP_PROGRESS_MESSAGE_SIZE, mTftpProgressFrame);\r |
1062 | if (EFI_ERROR(Status)) {\r | |
1063 | return Status;\r | |
1064 | }\r | |
68074260 RC |
1065 | for (Index = 1; Index < Step; Index++) {\r |
1066 | Progress[Index] = L'=';\r | |
1067 | }\r | |
1068 | Progress[Step] = L'>';\r | |
1069 | \r | |
1070 | UnicodeSPrint (\r | |
1071 | Progress + (sizeof (mTftpProgressFrame) / sizeof (CHAR16)) - 1,\r | |
1072 | sizeof (Progress) - sizeof (mTftpProgressFrame),\r | |
1073 | L" %7d Kb",\r | |
1074 | NbOfKb\r | |
1075 | );\r | |
1076 | Context->LastReportedNbOfBytes = Context->DownloadedNbOfBytes;\r | |
1077 | \r | |
1078 | ShellPrintEx (-1, -1, L"%s", Progress);\r | |
1079 | \r | |
1080 | return EFI_SUCCESS;\r | |
1081 | }\r | |
09610023 RN |
1082 | \r |
1083 | /**\r | |
1084 | Retrive HII package list from ImageHandle and publish to HII database.\r | |
1085 | \r | |
1086 | @param ImageHandle The image handle of the process.\r | |
1087 | \r | |
1088 | @return HII handle.\r | |
1089 | **/\r | |
1090 | EFI_HANDLE\r | |
1091 | InitializeHiiPackage (\r | |
1092 | EFI_HANDLE ImageHandle\r | |
1093 | )\r | |
1094 | {\r | |
1095 | EFI_STATUS Status;\r | |
1096 | EFI_HII_PACKAGE_LIST_HEADER *PackageList;\r | |
1097 | EFI_HANDLE HiiHandle;\r | |
1098 | \r | |
1099 | //\r | |
1100 | // Retrieve HII package list from ImageHandle\r | |
1101 | //\r | |
1102 | Status = gBS->OpenProtocol (\r | |
1103 | ImageHandle,\r | |
1104 | &gEfiHiiPackageListProtocolGuid,\r | |
1105 | (VOID **)&PackageList,\r | |
1106 | ImageHandle,\r | |
1107 | NULL,\r | |
1108 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r | |
1109 | );\r | |
1110 | ASSERT_EFI_ERROR (Status);\r | |
1111 | if (EFI_ERROR (Status)) {\r | |
1112 | return NULL;\r | |
1113 | }\r | |
1114 | \r | |
1115 | //\r | |
1116 | // Publish HII package list to HII Database.\r | |
1117 | //\r | |
1118 | Status = gHiiDatabase->NewPackageList (\r | |
1119 | gHiiDatabase,\r | |
1120 | PackageList,\r | |
1121 | NULL,\r | |
1122 | &HiiHandle\r | |
1123 | );\r | |
1124 | ASSERT_EFI_ERROR (Status);\r | |
1125 | if (EFI_ERROR (Status)) {\r | |
1126 | return NULL;\r | |
1127 | }\r | |
1128 | return HiiHandle;\r | |
1129 | }\r |