]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/DynamicCommand/TftpDynamicCommand/Tftp.c
ShellPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / ShellPkg / DynamicCommand / TftpDynamicCommand / Tftp.c
CommitLineData
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 7\r
56ba3746 8 SPDX-License-Identifier: BSD-2-Clause-Patent\r
68074260
RC
9**/\r
10\r
09610023 11#include "Tftp.h"\r
68074260 12\r
6add86ab 13#define IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH 32\r
09610023 14EFI_HANDLE mTftpHiiHandle;\r
6add86ab 15\r
68074260
RC
16/*\r
17 Constant strings and definitions related to the message indicating the amount of\r
18 progress in the dowloading of a TFTP file.\r
19*/\r
20\r
21// Frame for the progression slider\r
22STATIC CONST CHAR16 mTftpProgressFrame[] = L"[ ]";\r
23\r
24// Number of steps in the progression slider\r
25#define TFTP_PROGRESS_SLIDER_STEPS ((sizeof (mTftpProgressFrame) / sizeof (CHAR16)) - 3)\r
26\r
27// Size in number of characters plus one (final zero) of the message to\r
28// indicate the progress of a TFTP download. The format is "[(progress slider:\r
29// 40 characters)] (nb of KBytes downloaded so far: 7 characters) Kb". There\r
30// are thus the number of characters in mTftpProgressFrame[] plus 11 characters\r
31// (2 // spaces, "Kb" and seven characters for the number of KBytes).\r
32#define TFTP_PROGRESS_MESSAGE_SIZE ((sizeof (mTftpProgressFrame) / sizeof (CHAR16)) + 12)\r
33\r
34// String to delete the TFTP progress message to be able to update it :\r
35// (TFTP_PROGRESS_MESSAGE_SIZE-1) '\b'\r
36STATIC 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
37\r
b85c8fb4
AL
38// Local File Handle\r
39SHELL_FILE_HANDLE mFileHandle;\r
40\r
41// Path of the local file, Unicode encoded\r
42CONST CHAR16 *mLocalFilePath;\r
43\r
0fcf66a4
QS
44/**\r
45 Check and convert the UINT16 option values of the 'tftp' command\r
46\r
47 @param[in] ValueStr Value as an Unicode encoded string\r
48 @param[out] Value UINT16 value\r
49\r
50 @return TRUE The value was returned.\r
51 @return FALSE A parsing error occured.\r
52**/\r
ba0014b9
LG
53STATIC\r
54BOOLEAN\r
0fcf66a4 55StringToUint16 (\r
68074260
RC
56 IN CONST CHAR16 *ValueStr,\r
57 OUT UINT16 *Value\r
58 );\r
59\r
0fcf66a4
QS
60/**\r
61 Get the name of the NIC.\r
62\r
63 @param[in] ControllerHandle The network physical device handle.\r
64 @param[in] NicNumber The network physical device number.\r
65 @param[out] NicName Address where to store the NIC name.\r
66 The memory area has to be at least\r
ba0014b9 67 IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH\r
6add86ab 68 double byte wide.\r
0fcf66a4
QS
69\r
70 @return EFI_SUCCESS The name of the NIC was returned.\r
71 @return Others The creation of the child for the Managed\r
72 Network Service failed or the opening of\r
73 the Managed Network Protocol failed or\r
74 the operational parameters for the\r
75 Managed Network Protocol could not be\r
76 read.\r
77**/\r
ba0014b9
LG
78STATIC\r
79EFI_STATUS\r
0fcf66a4 80GetNicName (\r
68074260 81 IN EFI_HANDLE ControllerHandle,\r
a0f2af3a 82 IN UINTN NicNumber,\r
68074260
RC
83 OUT CHAR16 *NicName\r
84 );\r
85\r
0fcf66a4
QS
86/**\r
87 Create a child for the service identified by its service binding protocol GUID\r
88 and get from the child the interface of the protocol identified by its GUID.\r
89\r
90 @param[in] ControllerHandle Controller handle.\r
91 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the\r
92 service to be created.\r
93 @param[in] ProtocolGuid GUID of the protocol to be open.\r
94 @param[out] ChildHandle Address where the handler of the\r
95 created child is returned. NULL is\r
96 returned in case of error.\r
97 @param[out] Interface Address where a pointer to the\r
98 protocol interface is returned in\r
99 case of success.\r
100\r
101 @return EFI_SUCCESS The child was created and the protocol opened.\r
102 @return Others Either the creation of the child or the opening\r
103 of the protocol failed.\r
104**/\r
ba0014b9
LG
105STATIC\r
106EFI_STATUS\r
0fcf66a4 107CreateServiceChildAndOpenProtocol (\r
68074260
RC
108 IN EFI_HANDLE ControllerHandle,\r
109 IN EFI_GUID *ServiceBindingProtocolGuid,\r
110 IN EFI_GUID *ProtocolGuid,\r
111 OUT EFI_HANDLE *ChildHandle,\r
112 OUT VOID **Interface\r
113 );\r
114\r
0fcf66a4
QS
115/**\r
116 Close the protocol identified by its GUID on the child handle of the service\r
117 identified by its service binding protocol GUID, then destroy the child\r
118 handle.\r
119\r
120 @param[in] ControllerHandle Controller handle.\r
121 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the\r
122 service to be destroyed.\r
123 @param[in] ProtocolGuid GUID of the protocol to be closed.\r
124 @param[in] ChildHandle Handle of the child to be destroyed.\r
125\r
126**/\r
ba0014b9
LG
127STATIC\r
128VOID\r
0fcf66a4 129CloseProtocolAndDestroyServiceChild (\r
68074260
RC
130 IN EFI_HANDLE ControllerHandle,\r
131 IN EFI_GUID *ServiceBindingProtocolGuid,\r
132 IN EFI_GUID *ProtocolGuid,\r
133 IN EFI_HANDLE ChildHandle\r
134 );\r
135\r
0fcf66a4
QS
136/**\r
137 Worker function that gets the size in numbers of bytes of a file from a TFTP\r
138 server before to download the file.\r
139\r
140 @param[in] Mtftp4 MTFTP4 protocol interface\r
141 @param[in] FilePath Path of the file, ASCII encoded\r
142 @param[out] FileSize Address where to store the file size in number of\r
143 bytes.\r
144\r
145 @retval EFI_SUCCESS The size of the file was returned.\r
146 @retval EFI_UNSUPPORTED The server does not support the "tsize" option.\r
147 @retval Others Error when retrieving the information from the server\r
148 (see EFI_MTFTP4_PROTOCOL.GetInfo() status codes)\r
149 or error when parsing the response of the server.\r
150**/\r
ba0014b9
LG
151STATIC\r
152EFI_STATUS\r
0fcf66a4 153GetFileSize (\r
68074260
RC
154 IN EFI_MTFTP4_PROTOCOL *Mtftp4,\r
155 IN CONST CHAR8 *FilePath,\r
156 OUT UINTN *FileSize\r
157 );\r
158\r
0fcf66a4
QS
159/**\r
160 Worker function that download the data of a file from a TFTP server given\r
161 the path of the file and its size.\r
162\r
163 @param[in] Mtftp4 MTFTP4 protocol interface\r
164 @param[in] FilePath Path of the file, Unicode encoded\r
165 @param[in] AsciiFilePath Path of the file, ASCII encoded\r
166 @param[in] FileSize Size of the file in number of bytes\r
2be45bfe 167 @param[in] BlockSize Value of the TFTP blksize option\r
a7ab1c31 168 @param[in] WindowSize Value of the TFTP window size option\r
0fcf66a4
QS
169\r
170 @retval EFI_SUCCESS The file was downloaded.\r
171 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
172 @retval Others The downloading of the file from the server failed\r
173 (see EFI_MTFTP4_PROTOCOL.ReadFile() status codes).\r
174\r
175**/\r
ba0014b9
LG
176STATIC\r
177EFI_STATUS\r
0fcf66a4 178DownloadFile (\r
68074260
RC
179 IN EFI_MTFTP4_PROTOCOL *Mtftp4,\r
180 IN CONST CHAR16 *FilePath,\r
181 IN CONST CHAR8 *AsciiFilePath,\r
182 IN UINTN FileSize,\r
2be45bfe 183 IN UINT16 BlockSize,\r
b85c8fb4 184 IN UINT16 WindowSize\r
68074260
RC
185 );\r
186\r
0fcf66a4
QS
187/**\r
188 Update the progress of a file download\r
189 This procedure is called each time a new TFTP packet is received.\r
190\r
191 @param[in] This MTFTP4 protocol interface\r
192 @param[in] Token Parameters for the download of the file\r
193 @param[in] PacketLen Length of the packet\r
194 @param[in] Packet Address of the packet\r
195\r
196 @retval EFI_SUCCESS All packets are accepted.\r
197\r
198**/\r
ba0014b9
LG
199STATIC\r
200EFI_STATUS\r
7367e66f 201EFIAPI\r
0fcf66a4 202CheckPacket (\r
68074260
RC
203 IN EFI_MTFTP4_PROTOCOL *This,\r
204 IN EFI_MTFTP4_TOKEN *Token,\r
205 IN UINT16 PacketLen,\r
206 IN EFI_MTFTP4_PACKET *Packet\r
207 );\r
208\r
209EFI_MTFTP4_CONFIG_DATA DefaultMtftp4ConfigData = {\r
210 TRUE, // Use default setting\r
211 { { 0, 0, 0, 0 } }, // StationIp - Not relevant as UseDefaultSetting=TRUE\r
212 { { 0, 0, 0, 0 } }, // SubnetMask - Not relevant as UseDefaultSetting=TRUE\r
213 0, // LocalPort - Automatically assigned port number.\r
214 { { 0, 0, 0, 0 } }, // GatewayIp - Not relevant as UseDefaultSetting=TRUE\r
215 { { 0, 0, 0, 0 } }, // ServerIp - Not known yet\r
216 69, // InitialServerPort - Standard TFTP server port\r
62ea70e3 217 6, // TryCount - The number of times to transmit request packets and wait for a response.\r
68074260
RC
218 4 // TimeoutValue - Retransmission timeout in seconds.\r
219};\r
220\r
221STATIC CONST SHELL_PARAM_ITEM ParamList[] = {\r
222 {L"-i", TypeValue},\r
223 {L"-l", TypeValue},\r
224 {L"-r", TypeValue},\r
225 {L"-c", TypeValue},\r
226 {L"-t", TypeValue},\r
2be45bfe 227 {L"-s", TypeValue},\r
5c6fdb5e 228 {L"-w", TypeValue},\r
68074260
RC
229 {NULL , TypeMax}\r
230 };\r
231\r
2be45bfe
FS
232///\r
233/// The default block size (512) of tftp is defined in the RFC1350.\r
234///\r
235#define MTFTP_DEFAULT_BLKSIZE 512\r
236///\r
237/// The valid range of block size option is defined in the RFC2348.\r
238///\r
239#define MTFTP_MIN_BLKSIZE 8\r
240#define MTFTP_MAX_BLKSIZE 65464\r
5c6fdb5e
JW
241///\r
242/// The default windowsize (1) of tftp.\r
243///\r
244#define MTFTP_DEFAULT_WINDOWSIZE 1\r
245///\r
246/// The valid range of window size option.\r
247/// Note that: RFC 7440 does not mention max window size value, but for the\r
248/// stability reason, the value is limited to 64.\r
249///\r
250#define MTFTP_MIN_WINDOWSIZE 1\r
251#define MTFTP_MAX_WINDOWSIZE 64\r
2be45bfe 252\r
68074260
RC
253/**\r
254 Function for 'tftp' command.\r
255\r
256 @param[in] ImageHandle Handle to the Image (NULL if Internal).\r
257 @param[in] SystemTable Pointer to the System Table (NULL if Internal).\r
258\r
259 @return SHELL_SUCCESS The 'tftp' command completed successfully.\r
260 @return SHELL_ABORTED The Shell Library initialization failed.\r
261 @return SHELL_INVALID_PARAMETER At least one of the command's arguments is\r
262 not valid.\r
263 @return SHELL_OUT_OF_RESOURCES A memory allocation failed.\r
264 @return SHELL_NOT_FOUND Network Interface Card not found or server\r
265 error or file error.\r
266\r
267**/\r
268SHELL_STATUS\r
09610023 269RunTftp (\r
68074260
RC
270 IN EFI_HANDLE ImageHandle,\r
271 IN EFI_SYSTEM_TABLE *SystemTable\r
272 )\r
273{\r
274 SHELL_STATUS ShellStatus;\r
275 EFI_STATUS Status;\r
276 LIST_ENTRY *CheckPackage;\r
277 CHAR16 *ProblemParam;\r
278 UINTN ParamCount;\r
279 CONST CHAR16 *UserNicName;\r
280 BOOLEAN NicFound;\r
281 CONST CHAR16 *ValueStr;\r
282 CONST CHAR16 *RemoteFilePath;\r
283 CHAR8 *AsciiRemoteFilePath;\r
46213c8e 284 UINTN FilePathSize;\r
68074260 285 CONST CHAR16 *Walker;\r
68074260
RC
286 EFI_MTFTP4_CONFIG_DATA Mtftp4ConfigData;\r
287 EFI_HANDLE *Handles;\r
288 UINTN HandleCount;\r
289 UINTN NicNumber;\r
6add86ab 290 CHAR16 NicName[IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH];\r
68074260
RC
291 EFI_HANDLE ControllerHandle;\r
292 EFI_HANDLE Mtftp4ChildHandle;\r
293 EFI_MTFTP4_PROTOCOL *Mtftp4;\r
294 UINTN FileSize;\r
2be45bfe 295 UINT16 BlockSize;\r
5c6fdb5e 296 UINT16 WindowSize;\r
68074260
RC
297\r
298 ShellStatus = SHELL_INVALID_PARAMETER;\r
299 ProblemParam = NULL;\r
300 NicFound = FALSE;\r
301 AsciiRemoteFilePath = NULL;\r
302 Handles = NULL;\r
6add86ab 303 FileSize = 0;\r
2be45bfe 304 BlockSize = MTFTP_DEFAULT_BLKSIZE;\r
5c6fdb5e 305 WindowSize = MTFTP_DEFAULT_WINDOWSIZE;\r
68074260
RC
306\r
307 //\r
308 // Initialize the Shell library (we must be in non-auto-init...)\r
309 //\r
310 Status = ShellInitialize ();\r
311 if (EFI_ERROR (Status)) {\r
312 ASSERT_EFI_ERROR (Status);\r
313 return SHELL_ABORTED;\r
314 }\r
315\r
316 //\r
317 // Parse the command line.\r
318 //\r
319 Status = ShellCommandLineParse (ParamList, &CheckPackage, &ProblemParam, TRUE);\r
320 if (EFI_ERROR (Status)) {\r
321 if ((Status == EFI_VOLUME_CORRUPTED) &&\r
322 (ProblemParam != NULL) ) {\r
323 ShellPrintHiiEx (\r
09610023 324 -1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), mTftpHiiHandle,\r
68074260
RC
325 L"tftp", ProblemParam\r
326 );\r
327 FreePool (ProblemParam);\r
328 } else {\r
329 ASSERT (FALSE);\r
330 }\r
331 goto Error;\r
332 }\r
333\r
334 //\r
335 // Check the number of parameters\r
336 //\r
337 ParamCount = ShellCommandLineGetCount (CheckPackage);\r
338 if (ParamCount > 4) {\r
339 ShellPrintHiiEx (\r
340 -1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY),\r
09610023 341 mTftpHiiHandle, L"tftp"\r
68074260
RC
342 );\r
343 goto Error;\r
344 }\r
345 if (ParamCount < 3) {\r
346 ShellPrintHiiEx (\r
347 -1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW),\r
09610023 348 mTftpHiiHandle, L"tftp"\r
68074260
RC
349 );\r
350 goto Error;\r
351 }\r
352\r
ba601bdd 353 CopyMem (&Mtftp4ConfigData, &DefaultMtftp4ConfigData, sizeof (EFI_MTFTP4_CONFIG_DATA));\r
68074260
RC
354\r
355 //\r
356 // Check the host IPv4 address\r
357 //\r
358 ValueStr = ShellCommandLineGetRawValue (CheckPackage, 1);\r
359 Status = NetLibStrToIp4 (ValueStr, &Mtftp4ConfigData.ServerIp);\r
360 if (EFI_ERROR (Status)) {\r
361 ShellPrintHiiEx (\r
362 -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV),\r
09610023 363 mTftpHiiHandle, L"tftp", ValueStr\r
68074260
RC
364 );\r
365 goto Error;\r
366 }\r
367\r
368 RemoteFilePath = ShellCommandLineGetRawValue (CheckPackage, 2);\r
c6173804 369 ASSERT(RemoteFilePath != NULL);\r
46213c8e
SZ
370 FilePathSize = StrLen (RemoteFilePath) + 1;\r
371 AsciiRemoteFilePath = AllocatePool (FilePathSize);\r
68074260
RC
372 if (AsciiRemoteFilePath == NULL) {\r
373 ShellStatus = SHELL_OUT_OF_RESOURCES;\r
374 goto Error;\r
375 }\r
46213c8e 376 UnicodeStrToAsciiStrS (RemoteFilePath, AsciiRemoteFilePath, FilePathSize);\r
68074260
RC
377\r
378 if (ParamCount == 4) {\r
b85c8fb4 379 mLocalFilePath = ShellCommandLineGetRawValue (CheckPackage, 3);\r
68074260
RC
380 } else {\r
381 Walker = RemoteFilePath + StrLen (RemoteFilePath);\r
382 while ((--Walker) >= RemoteFilePath) {\r
383 if ((*Walker == L'\\') ||\r
384 (*Walker == L'/' ) ) {\r
385 break;\r
386 }\r
387 }\r
b85c8fb4 388 mLocalFilePath = Walker + 1;\r
68074260
RC
389 }\r
390\r
391 //\r
392 // Get the name of the Network Interface Card to be used if any.\r
393 //\r
394 UserNicName = ShellCommandLineGetValue (CheckPackage, L"-i");\r
395\r
396 ValueStr = ShellCommandLineGetValue (CheckPackage, L"-l");\r
397 if (ValueStr != NULL) {\r
398 if (!StringToUint16 (ValueStr, &Mtftp4ConfigData.LocalPort)) {\r
399 goto Error;\r
400 }\r
401 }\r
402\r
403 ValueStr = ShellCommandLineGetValue (CheckPackage, L"-r");\r
404 if (ValueStr != NULL) {\r
405 if (!StringToUint16 (ValueStr, &Mtftp4ConfigData.InitialServerPort)) {\r
406 goto Error;\r
407 }\r
408 }\r
409\r
410 ValueStr = ShellCommandLineGetValue (CheckPackage, L"-c");\r
411 if (ValueStr != NULL) {\r
412 if (!StringToUint16 (ValueStr, &Mtftp4ConfigData.TryCount)) {\r
413 goto Error;\r
414 }\r
62ea70e3
JW
415\r
416 if (Mtftp4ConfigData.TryCount == 0) {\r
417 Mtftp4ConfigData.TryCount = 6;\r
418 }\r
68074260
RC
419 }\r
420\r
421 ValueStr = ShellCommandLineGetValue (CheckPackage, L"-t");\r
422 if (ValueStr != NULL) {\r
423 if (!StringToUint16 (ValueStr, &Mtftp4ConfigData.TimeoutValue)) {\r
424 goto Error;\r
425 }\r
426 if (Mtftp4ConfigData.TimeoutValue == 0) {\r
427 ShellPrintHiiEx (\r
428 -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV),\r
09610023 429 mTftpHiiHandle, L"tftp", ValueStr\r
68074260
RC
430 );\r
431 goto Error;\r
432 }\r
433 }\r
434\r
2be45bfe
FS
435 ValueStr = ShellCommandLineGetValue (CheckPackage, L"-s");\r
436 if (ValueStr != NULL) {\r
437 if (!StringToUint16 (ValueStr, &BlockSize)) {\r
438 goto Error;\r
439 }\r
440 if (BlockSize < MTFTP_MIN_BLKSIZE || BlockSize > MTFTP_MAX_BLKSIZE) {\r
441 ShellPrintHiiEx (\r
442 -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV),\r
09610023 443 mTftpHiiHandle, L"tftp", ValueStr\r
2be45bfe
FS
444 );\r
445 goto Error;\r
446 }\r
447 }\r
448\r
5c6fdb5e
JW
449 ValueStr = ShellCommandLineGetValue (CheckPackage, L"-w");\r
450 if (ValueStr != NULL) {\r
451 if (!StringToUint16 (ValueStr, &WindowSize)) {\r
452 goto Error;\r
453 }\r
454 if (WindowSize < MTFTP_MIN_WINDOWSIZE || WindowSize > MTFTP_MAX_WINDOWSIZE) {\r
455 ShellPrintHiiEx (\r
456 -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV),\r
457 mTftpHiiHandle, L"tftp", ValueStr\r
458 );\r
459 goto Error;\r
460 }\r
461 }\r
462\r
68074260
RC
463 //\r
464 // Locate all MTFTP4 Service Binding protocols\r
465 //\r
466 ShellStatus = SHELL_NOT_FOUND;\r
467 Status = gBS->LocateHandleBuffer (\r
468 ByProtocol,\r
469 &gEfiManagedNetworkServiceBindingProtocolGuid,\r
470 NULL,\r
471 &HandleCount,\r
472 &Handles\r
473 );\r
474 if (EFI_ERROR (Status) || (HandleCount == 0)) {\r
475 ShellPrintHiiEx (\r
476 -1, -1, NULL, STRING_TOKEN (STR_TFTP_ERR_NO_NIC),\r
09610023 477 mTftpHiiHandle\r
68074260
RC
478 );\r
479 goto Error;\r
480 }\r
481\r
482 for (NicNumber = 0;\r
483 (NicNumber < HandleCount) && (ShellStatus != SHELL_SUCCESS);\r
484 NicNumber++) {\r
485 ControllerHandle = Handles[NicNumber];\r
68074260
RC
486\r
487 Status = GetNicName (ControllerHandle, NicNumber, NicName);\r
488 if (EFI_ERROR (Status)) {\r
489 ShellPrintHiiEx (\r
490 -1, -1, NULL, STRING_TOKEN (STR_TFTP_ERR_NIC_NAME),\r
09610023 491 mTftpHiiHandle, NicNumber, Status\r
68074260
RC
492 );\r
493 continue;\r
494 }\r
495\r
496 if (UserNicName != NULL) {\r
497 if (StrCmp (NicName, UserNicName) != 0) {\r
498 continue;\r
499 }\r
500 NicFound = TRUE;\r
501 }\r
502\r
503 Status = CreateServiceChildAndOpenProtocol (\r
504 ControllerHandle,\r
505 &gEfiMtftp4ServiceBindingProtocolGuid,\r
506 &gEfiMtftp4ProtocolGuid,\r
507 &Mtftp4ChildHandle,\r
508 (VOID**)&Mtftp4\r
509 );\r
510 if (EFI_ERROR (Status)) {\r
511 ShellPrintHiiEx (\r
512 -1, -1, NULL, STRING_TOKEN (STR_TFTP_ERR_OPEN_PROTOCOL),\r
09610023 513 mTftpHiiHandle, NicName, Status\r
68074260
RC
514 );\r
515 continue;\r
516 }\r
517\r
518 Status = Mtftp4->Configure (Mtftp4, &Mtftp4ConfigData);\r
519 if (EFI_ERROR (Status)) {\r
520 ShellPrintHiiEx (\r
521 -1, -1, NULL, STRING_TOKEN (STR_TFTP_ERR_CONFIGURE),\r
09610023 522 mTftpHiiHandle, NicName, Status\r
68074260
RC
523 );\r
524 goto NextHandle;\r
525 }\r
526\r
527 Status = GetFileSize (Mtftp4, AsciiRemoteFilePath, &FileSize);\r
528 if (EFI_ERROR (Status)) {\r
529 ShellPrintHiiEx (\r
530 -1, -1, NULL, STRING_TOKEN (STR_TFTP_ERR_FILE_SIZE),\r
09610023 531 mTftpHiiHandle, RemoteFilePath, NicName, Status\r
68074260
RC
532 );\r
533 goto NextHandle;\r
534 }\r
535\r
b85c8fb4 536 Status = DownloadFile (Mtftp4, RemoteFilePath, AsciiRemoteFilePath, FileSize, BlockSize, WindowSize);\r
68074260
RC
537 if (EFI_ERROR (Status)) {\r
538 ShellPrintHiiEx (\r
539 -1, -1, NULL, STRING_TOKEN (STR_TFTP_ERR_DOWNLOAD),\r
09610023 540 mTftpHiiHandle, RemoteFilePath, NicName, Status\r
68074260
RC
541 );\r
542 goto NextHandle;\r
543 }\r
544\r
83463154
VO
545 ShellStatus = SHELL_SUCCESS;\r
546\r
68074260
RC
547 NextHandle:\r
548\r
68074260
RC
549 CloseProtocolAndDestroyServiceChild (\r
550 ControllerHandle,\r
551 &gEfiMtftp4ServiceBindingProtocolGuid,\r
552 &gEfiMtftp4ProtocolGuid,\r
553 Mtftp4ChildHandle\r
554 );\r
555 }\r
556\r
a0f2af3a 557 if ((UserNicName != NULL) && (!NicFound)) {\r
68074260
RC
558 ShellPrintHiiEx (\r
559 -1, -1, NULL, STRING_TOKEN (STR_TFTP_ERR_NIC_NOT_FOUND),\r
09610023 560 mTftpHiiHandle, UserNicName\r
68074260
RC
561 );\r
562 }\r
563\r
564 Error:\r
565\r
566 ShellCommandLineFreeVarList (CheckPackage);\r
567 if (AsciiRemoteFilePath != NULL) {\r
568 FreePool (AsciiRemoteFilePath);\r
569 }\r
570 if (Handles != NULL) {\r
571 FreePool (Handles);\r
572 }\r
573\r
83463154
VO
574 if ((ShellStatus != SHELL_SUCCESS) && (EFI_ERROR(Status))) {\r
575 ShellStatus = Status & ~MAX_BIT;\r
576 }\r
577\r
68074260
RC
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
590STATIC\r
591BOOLEAN\r
592StringToUint16 (\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
630STATIC\r
631EFI_STATUS\r
632GetNicName (\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
670Error:\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
703STATIC\r
704EFI_STATUS\r
705CreateServiceChildAndOpenProtocol (\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
757STATIC\r
758VOID\r
759CloseProtocolAndDestroyServiceChild (\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
796STATIC\r
797EFI_STATUS\r
798GetFileSize (\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
859Error :\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
881STATIC\r
882EFI_STATUS\r
883DownloadFile (\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 972Error :\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
996STATIC\r
997EFI_STATUS\r
7367e66f 998EFIAPI\r
68074260
RC
999CheckPacket (\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
1090EFI_HANDLE\r
1091InitializeHiiPackage (\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