]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/DynamicCommand/TftpDynamicCommand/Tftp.c
ShellBinPkg: 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
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 20EFI_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
28STATIC 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
42STATIC 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
45SHELL_FILE_HANDLE mFileHandle;\r
46\r
47// Path of the local file, Unicode encoded\r
48CONST 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
59STATIC\r
60BOOLEAN\r
0fcf66a4 61StringToUint16 (\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
84STATIC\r
85EFI_STATUS\r
0fcf66a4 86GetNicName (\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
111STATIC\r
112EFI_STATUS\r
0fcf66a4 113CreateServiceChildAndOpenProtocol (\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
133STATIC\r
134VOID\r
0fcf66a4 135CloseProtocolAndDestroyServiceChild (\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
157STATIC\r
158EFI_STATUS\r
0fcf66a4 159GetFileSize (\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
182STATIC\r
183EFI_STATUS\r
0fcf66a4 184DownloadFile (\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
205STATIC\r
206EFI_STATUS\r
7367e66f 207EFIAPI\r
0fcf66a4 208CheckPacket (\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
215EFI_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
227STATIC 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
274SHELL_STATUS\r
09610023 275RunTftp (\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
83463154
VO
551 ShellStatus = SHELL_SUCCESS;\r
552\r
68074260
RC
553 NextHandle:\r
554\r
68074260
RC
555 CloseProtocolAndDestroyServiceChild (\r
556 ControllerHandle,\r
557 &gEfiMtftp4ServiceBindingProtocolGuid,\r
558 &gEfiMtftp4ProtocolGuid,\r
559 Mtftp4ChildHandle\r
560 );\r
561 }\r
562\r
a0f2af3a 563 if ((UserNicName != NULL) && (!NicFound)) {\r
68074260
RC
564 ShellPrintHiiEx (\r
565 -1, -1, NULL, STRING_TOKEN (STR_TFTP_ERR_NIC_NOT_FOUND),\r
09610023 566 mTftpHiiHandle, UserNicName\r
68074260
RC
567 );\r
568 }\r
569\r
570 Error:\r
571\r
572 ShellCommandLineFreeVarList (CheckPackage);\r
573 if (AsciiRemoteFilePath != NULL) {\r
574 FreePool (AsciiRemoteFilePath);\r
575 }\r
576 if (Handles != NULL) {\r
577 FreePool (Handles);\r
578 }\r
579\r
83463154
VO
580 if ((ShellStatus != SHELL_SUCCESS) && (EFI_ERROR(Status))) {\r
581 ShellStatus = Status & ~MAX_BIT;\r
582 }\r
583\r
68074260
RC
584 return ShellStatus;\r
585}\r
586\r
587/**\r
588 Check and convert the UINT16 option values of the 'tftp' command\r
589\r
590 @param[in] ValueStr Value as an Unicode encoded string\r
591 @param[out] Value UINT16 value\r
592\r
593 @return TRUE The value was returned.\r
594 @return FALSE A parsing error occured.\r
595**/\r
596STATIC\r
597BOOLEAN\r
598StringToUint16 (\r
599 IN CONST CHAR16 *ValueStr,\r
600 OUT UINT16 *Value\r
601 )\r
602{\r
603 UINTN Val;\r
604\r
605 Val = ShellStrToUintn (ValueStr);\r
606 if (Val > MAX_UINT16) {\r
607 ShellPrintHiiEx (\r
608 -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV),\r
09610023 609 mTftpHiiHandle, L"tftp", ValueStr\r
68074260
RC
610 );\r
611 return FALSE;\r
612 }\r
613\r
6add86ab 614 *Value = (UINT16)Val;\r
68074260
RC
615 return TRUE;\r
616}\r
617\r
618/**\r
619 Get the name of the NIC.\r
620\r
621 @param[in] ControllerHandle The network physical device handle.\r
622 @param[in] NicNumber The network physical device number.\r
623 @param[out] NicName Address where to store the NIC name.\r
624 The memory area has to be at least\r
ba0014b9 625 IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH\r
6add86ab 626 double byte wide.\r
68074260
RC
627\r
628 @return EFI_SUCCESS The name of the NIC was returned.\r
629 @return Others The creation of the child for the Managed\r
630 Network Service failed or the opening of\r
631 the Managed Network Protocol failed or\r
632 the operational parameters for the\r
633 Managed Network Protocol could not be\r
634 read.\r
635**/\r
636STATIC\r
637EFI_STATUS\r
638GetNicName (\r
639 IN EFI_HANDLE ControllerHandle,\r
640 IN UINTN NicNumber,\r
641 OUT CHAR16 *NicName\r
642 )\r
643{\r
644 EFI_STATUS Status;\r
645 EFI_HANDLE MnpHandle;\r
646 EFI_MANAGED_NETWORK_PROTOCOL *Mnp;\r
647 EFI_SIMPLE_NETWORK_MODE SnpMode;\r
648\r
649 Status = CreateServiceChildAndOpenProtocol (\r
650 ControllerHandle,\r
651 &gEfiManagedNetworkServiceBindingProtocolGuid,\r
652 &gEfiManagedNetworkProtocolGuid,\r
653 &MnpHandle,\r
654 (VOID**)&Mnp\r
655 );\r
656 if (EFI_ERROR (Status)) {\r
657 goto Error;\r
658 }\r
659\r
660 Status = Mnp->GetModeData (Mnp, NULL, &SnpMode);\r
661 if (EFI_ERROR (Status) && (Status != EFI_NOT_STARTED)) {\r
662 goto Error;\r
663 }\r
664\r
665 UnicodeSPrint (\r
666 NicName,\r
6add86ab 667 IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH,\r
68074260
RC
668 SnpMode.IfType == NET_IFTYPE_ETHERNET ?\r
669 L"eth%d" :\r
670 L"unk%d" ,\r
671 NicNumber\r
672 );\r
673\r
674 Status = EFI_SUCCESS;\r
675\r
676Error:\r
677\r
678 if (MnpHandle != NULL) {\r
679 CloseProtocolAndDestroyServiceChild (\r
680 ControllerHandle,\r
681 &gEfiManagedNetworkServiceBindingProtocolGuid,\r
682 &gEfiManagedNetworkProtocolGuid,\r
683 MnpHandle\r
684 );\r
685 }\r
686\r
687 return Status;\r
688}\r
689\r
690/**\r
691 Create a child for the service identified by its service binding protocol GUID\r
692 and get from the child the interface of the protocol identified by its GUID.\r
693\r
694 @param[in] ControllerHandle Controller handle.\r
695 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the\r
696 service to be created.\r
697 @param[in] ProtocolGuid GUID of the protocol to be open.\r
698 @param[out] ChildHandle Address where the handler of the\r
699 created child is returned. NULL is\r
700 returned in case of error.\r
701 @param[out] Interface Address where a pointer to the\r
702 protocol interface is returned in\r
703 case of success.\r
704\r
705 @return EFI_SUCCESS The child was created and the protocol opened.\r
706 @return Others Either the creation of the child or the opening\r
707 of the protocol failed.\r
708**/\r
709STATIC\r
710EFI_STATUS\r
711CreateServiceChildAndOpenProtocol (\r
712 IN EFI_HANDLE ControllerHandle,\r
713 IN EFI_GUID *ServiceBindingProtocolGuid,\r
714 IN EFI_GUID *ProtocolGuid,\r
715 OUT EFI_HANDLE *ChildHandle,\r
716 OUT VOID **Interface\r
717 )\r
718{\r
719 EFI_STATUS Status;\r
720\r
721 *ChildHandle = NULL;\r
722 Status = NetLibCreateServiceChild (\r
723 ControllerHandle,\r
724 gImageHandle,\r
725 ServiceBindingProtocolGuid,\r
726 ChildHandle\r
727 );\r
728 if (!EFI_ERROR (Status)) {\r
729 Status = gBS->OpenProtocol (\r
730 *ChildHandle,\r
731 ProtocolGuid,\r
732 Interface,\r
733 gImageHandle,\r
734 ControllerHandle,\r
735 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
736 );\r
737 if (EFI_ERROR (Status)) {\r
738 NetLibDestroyServiceChild (\r
739 ControllerHandle,\r
740 gImageHandle,\r
741 ServiceBindingProtocolGuid,\r
742 *ChildHandle\r
743 );\r
744 *ChildHandle = NULL;\r
745 }\r
746 }\r
747\r
748 return Status;\r
749}\r
750\r
751/**\r
752 Close the protocol identified by its GUID on the child handle of the service\r
753 identified by its service binding protocol GUID, then destroy the child\r
754 handle.\r
755\r
756 @param[in] ControllerHandle Controller handle.\r
757 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the\r
758 service to be destroyed.\r
759 @param[in] ProtocolGuid GUID of the protocol to be closed.\r
760 @param[in] ChildHandle Handle of the child to be destroyed.\r
761\r
762**/\r
763STATIC\r
764VOID\r
765CloseProtocolAndDestroyServiceChild (\r
766 IN EFI_HANDLE ControllerHandle,\r
767 IN EFI_GUID *ServiceBindingProtocolGuid,\r
768 IN EFI_GUID *ProtocolGuid,\r
769 IN EFI_HANDLE ChildHandle\r
770 )\r
771{\r
772 gBS->CloseProtocol (\r
773 ChildHandle,\r
774 ProtocolGuid,\r
775 gImageHandle,\r
776 ControllerHandle\r
777 );\r
778\r
779 NetLibDestroyServiceChild (\r
780 ControllerHandle,\r
781 gImageHandle,\r
782 ServiceBindingProtocolGuid,\r
783 ChildHandle\r
784 );\r
785}\r
786\r
787/**\r
788 Worker function that gets the size in numbers of bytes of a file from a TFTP\r
789 server before to download the file.\r
790\r
791 @param[in] Mtftp4 MTFTP4 protocol interface\r
792 @param[in] FilePath Path of the file, ASCII encoded\r
793 @param[out] FileSize Address where to store the file size in number of\r
794 bytes.\r
795\r
796 @retval EFI_SUCCESS The size of the file was returned.\r
797 @retval EFI_UNSUPPORTED The server does not support the "tsize" option.\r
798 @retval Others Error when retrieving the information from the server\r
799 (see EFI_MTFTP4_PROTOCOL.GetInfo() status codes)\r
800 or error when parsing the response of the server.\r
801**/\r
802STATIC\r
803EFI_STATUS\r
804GetFileSize (\r
805 IN EFI_MTFTP4_PROTOCOL *Mtftp4,\r
806 IN CONST CHAR8 *FilePath,\r
807 OUT UINTN *FileSize\r
808 )\r
809{\r
810 EFI_STATUS Status;\r
811 EFI_MTFTP4_OPTION ReqOpt[1];\r
812 EFI_MTFTP4_PACKET *Packet;\r
813 UINT32 PktLen;\r
814 EFI_MTFTP4_OPTION *TableOfOptions;\r
815 EFI_MTFTP4_OPTION *Option;\r
816 UINT32 OptCnt;\r
817 UINT8 OptBuf[128];\r
818\r
819 ReqOpt[0].OptionStr = (UINT8*)"tsize";\r
820 OptBuf[0] = '0';\r
821 OptBuf[1] = 0;\r
822 ReqOpt[0].ValueStr = OptBuf;\r
823\r
824 Status = Mtftp4->GetInfo (\r
825 Mtftp4,\r
826 NULL,\r
827 (UINT8*)FilePath,\r
828 NULL,\r
829 1,\r
830 ReqOpt,\r
831 &PktLen,\r
832 &Packet\r
833 );\r
834\r
835 if (EFI_ERROR (Status)) {\r
836 goto Error;\r
837 }\r
838\r
839 Status = Mtftp4->ParseOptions (\r
840 Mtftp4,\r
841 PktLen,\r
842 Packet,\r
843 (UINT32 *) &OptCnt,\r
844 &TableOfOptions\r
845 );\r
846 if (EFI_ERROR (Status)) {\r
847 goto Error;\r
848 }\r
849\r
850 Option = TableOfOptions;\r
851 while (OptCnt != 0) {\r
852 if (AsciiStrnCmp ((CHAR8 *)Option->OptionStr, "tsize", 5) == 0) {\r
853 *FileSize = AsciiStrDecimalToUintn ((CHAR8 *)Option->ValueStr);\r
854 break;\r
855 }\r
856 OptCnt--;\r
857 Option++;\r
858 }\r
859 FreePool (TableOfOptions);\r
860\r
861 if (OptCnt == 0) {\r
862 Status = EFI_UNSUPPORTED;\r
863 }\r
864\r
865Error :\r
866\r
867 return Status;\r
868}\r
869\r
870/**\r
871 Worker function that download the data of a file from a TFTP server given\r
872 the path of the file and its size.\r
873\r
874 @param[in] Mtftp4 MTFTP4 protocol interface\r
875 @param[in] FilePath Path of the file, Unicode encoded\r
876 @param[in] AsciiFilePath Path of the file, ASCII encoded\r
877 @param[in] FileSize Size of the file in number of bytes\r
2be45bfe 878 @param[in] BlockSize Value of the TFTP blksize option\r
a7ab1c31 879 @param[in] WindowSize Value of the TFTP window size option\r
68074260
RC
880\r
881 @retval EFI_SUCCESS The file was downloaded.\r
882 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
883 @retval Others The downloading of the file from the server failed\r
884 (see EFI_MTFTP4_PROTOCOL.ReadFile() status codes).\r
885\r
886**/\r
887STATIC\r
888EFI_STATUS\r
889DownloadFile (\r
890 IN EFI_MTFTP4_PROTOCOL *Mtftp4,\r
891 IN CONST CHAR16 *FilePath,\r
892 IN CONST CHAR8 *AsciiFilePath,\r
893 IN UINTN FileSize,\r
2be45bfe 894 IN UINT16 BlockSize,\r
b85c8fb4 895 IN UINT16 WindowSize\r
68074260
RC
896 )\r
897{\r
898 EFI_STATUS Status;\r
68074260
RC
899 DOWNLOAD_CONTEXT *TftpContext;\r
900 EFI_MTFTP4_TOKEN Mtftp4Token;\r
5c6fdb5e
JW
901 UINT8 BlksizeBuf[10];\r
902 UINT8 WindowsizeBuf[10];\r
68074260 903\r
61d3f100
JW
904 ZeroMem (&Mtftp4Token, sizeof (EFI_MTFTP4_TOKEN));\r
905\r
68074260
RC
906 TftpContext = AllocatePool (sizeof (DOWNLOAD_CONTEXT));\r
907 if (TftpContext == NULL) {\r
908 Status = EFI_OUT_OF_RESOURCES;\r
909 goto Error;\r
910 }\r
911 TftpContext->FileSize = FileSize;\r
912 TftpContext->DownloadedNbOfBytes = 0;\r
913 TftpContext->LastReportedNbOfBytes = 0;\r
914\r
68074260 915 Mtftp4Token.Filename = (UINT8*)AsciiFilePath;\r
68074260
RC
916 Mtftp4Token.CheckPacket = CheckPacket;\r
917 Mtftp4Token.Context = (VOID*)TftpContext;\r
5c6fdb5e
JW
918 Mtftp4Token.OptionCount = 0;\r
919 Mtftp4Token.OptionList = AllocatePool (sizeof (EFI_MTFTP4_OPTION) * 2);\r
920 if (Mtftp4Token.OptionList == NULL) {\r
921 Status = EFI_OUT_OF_RESOURCES;\r
922 goto Error;\r
923 }\r
924\r
2be45bfe 925 if (BlockSize != MTFTP_DEFAULT_BLKSIZE) {\r
5c6fdb5e
JW
926 Mtftp4Token.OptionList[Mtftp4Token.OptionCount].OptionStr = (UINT8 *) "blksize";\r
927 AsciiSPrint ((CHAR8 *) BlksizeBuf, sizeof (BlksizeBuf), "%d", BlockSize);\r
928 Mtftp4Token.OptionList[Mtftp4Token.OptionCount].ValueStr = BlksizeBuf;\r
929 Mtftp4Token.OptionCount ++;\r
930 }\r
2be45bfe 931\r
5c6fdb5e
JW
932 if (WindowSize != MTFTP_DEFAULT_WINDOWSIZE) {\r
933 Mtftp4Token.OptionList[Mtftp4Token.OptionCount].OptionStr = (UINT8 *) "windowsize";\r
934 AsciiSPrint ((CHAR8 *) WindowsizeBuf, sizeof (WindowsizeBuf), "%d", WindowSize);\r
935 Mtftp4Token.OptionList[Mtftp4Token.OptionCount].ValueStr = WindowsizeBuf;\r
936 Mtftp4Token.OptionCount ++;\r
2be45bfe 937 }\r
68074260
RC
938\r
939 ShellPrintHiiEx (\r
940 -1, -1, NULL, STRING_TOKEN (STR_TFTP_DOWNLOADING),\r
09610023 941 mTftpHiiHandle, FilePath\r
68074260
RC
942 );\r
943\r
b85c8fb4
AL
944 //\r
945 // OPEN FILE\r
946 //\r
947 if (!EFI_ERROR (ShellFileExists (mLocalFilePath))) {\r
948 ShellDeleteFileByName (mLocalFilePath);\r
949 }\r
950\r
951 Status = ShellOpenFileByName (\r
952 mLocalFilePath,\r
953 &mFileHandle,\r
954 EFI_FILE_MODE_CREATE |\r
955 EFI_FILE_MODE_WRITE |\r
956 EFI_FILE_MODE_READ,\r
957 0\r
958 );\r
959 if (EFI_ERROR (Status)) {\r
960 ShellPrintHiiEx (\r
961 -1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL),\r
962 mTftpHiiHandle, L"tftp", mLocalFilePath\r
963 );\r
964 goto Error;\r
965 }\r
966\r
68074260
RC
967 Status = Mtftp4->ReadFile (Mtftp4, &Mtftp4Token);\r
968 ShellPrintHiiEx (\r
969 -1, -1, NULL, STRING_TOKEN (STR_GEN_CRLF),\r
09610023 970 mTftpHiiHandle\r
68074260
RC
971 );\r
972\r
b85c8fb4
AL
973 //\r
974 // CLOSE FILE\r
975 //\r
976 ShellCloseFile (&mFileHandle);\r
68074260 977\r
b85c8fb4 978Error :\r
5c6fdb5e 979 if (TftpContext != NULL) {\r
68074260
RC
980 FreePool (TftpContext);\r
981 }\r
982\r
5c6fdb5e
JW
983 if (Mtftp4Token.OptionList != NULL) {\r
984 FreePool (Mtftp4Token.OptionList);\r
985 }\r
986\r
b85c8fb4 987 return Status;\r
68074260
RC
988}\r
989\r
990/**\r
991 Update the progress of a file download\r
992 This procedure is called each time a new TFTP packet is received.\r
993\r
994 @param[in] This MTFTP4 protocol interface\r
995 @param[in] Token Parameters for the download of the file\r
996 @param[in] PacketLen Length of the packet\r
997 @param[in] Packet Address of the packet\r
998\r
999 @retval EFI_SUCCESS All packets are accepted.\r
1000\r
1001**/\r
1002STATIC\r
1003EFI_STATUS\r
7367e66f 1004EFIAPI\r
68074260
RC
1005CheckPacket (\r
1006 IN EFI_MTFTP4_PROTOCOL *This,\r
1007 IN EFI_MTFTP4_TOKEN *Token,\r
1008 IN UINT16 PacketLen,\r
1009 IN EFI_MTFTP4_PACKET *Packet\r
1010 )\r
1011{\r
1012 DOWNLOAD_CONTEXT *Context;\r
1013 CHAR16 Progress[TFTP_PROGRESS_MESSAGE_SIZE];\r
6add86ab 1014 UINTN NbOfKb;\r
68074260 1015 UINTN Index;\r
310b053a
JC
1016 UINTN LastStep;\r
1017 UINTN Step;\r
b85c8fb4 1018 UINTN DownloadLen;\r
da6b8feb 1019 EFI_STATUS Status;\r
68074260
RC
1020\r
1021 if ((NTOHS (Packet->OpCode)) != EFI_MTFTP4_OPCODE_DATA) {\r
1022 return EFI_SUCCESS;\r
1023 }\r
1024\r
1025 Context = (DOWNLOAD_CONTEXT*)Token->Context;\r
68074260
RC
1026\r
1027 //\r
1028 // The data in the packet are prepended with two UINT16 :\r
1029 // . OpCode = EFI_MTFTP4_OPCODE_DATA\r
1030 // . Block = the number of this block of data\r
1031 //\r
b85c8fb4
AL
1032 DownloadLen = (UINTN)PacketLen - sizeof (Packet->OpCode) - sizeof (Packet->Data.Block);\r
1033\r
1034 ShellSetFilePosition(mFileHandle, Context->DownloadedNbOfBytes);\r
1035 Status = ShellWriteFile (mFileHandle, &DownloadLen, Packet->Data.Data);\r
1036 if (EFI_ERROR (Status)) {\r
1037 if (Context->DownloadedNbOfBytes > 0) {\r
1038 ShellPrintHiiEx (\r
1039 -1, -1, NULL, STRING_TOKEN (STR_GEN_CRLF),\r
1040 mTftpHiiHandle\r
1041 );\r
1042 }\r
1043 ShellPrintHiiEx (\r
1044 -1, -1, NULL, STRING_TOKEN (STR_TFTP_ERR_WRITE),\r
1045 mTftpHiiHandle, mLocalFilePath, Status\r
1046 );\r
1047 return Status;\r
1048 }\r
1049\r
1050 if (Context->DownloadedNbOfBytes == 0) {\r
1051 ShellPrintEx (-1, -1, L"%s 0 Kb", mTftpProgressFrame);\r
1052 }\r
1053\r
1054 Context->DownloadedNbOfBytes += DownloadLen;\r
68074260
RC
1055 NbOfKb = Context->DownloadedNbOfBytes / 1024;\r
1056\r
1057 Progress[0] = L'\0';\r
6add86ab
JC
1058 LastStep = (Context->LastReportedNbOfBytes * TFTP_PROGRESS_SLIDER_STEPS) / Context->FileSize;\r
1059 Step = (Context->DownloadedNbOfBytes * TFTP_PROGRESS_SLIDER_STEPS) / Context->FileSize;\r
1060\r
68074260
RC
1061 if (Step <= LastStep) {\r
1062 return EFI_SUCCESS;\r
1063 }\r
1064\r
1065 ShellPrintEx (-1, -1, L"%s", mTftpProgressDelete);\r
1066\r
da6b8feb
QS
1067 Status = StrCpyS (Progress, TFTP_PROGRESS_MESSAGE_SIZE, mTftpProgressFrame);\r
1068 if (EFI_ERROR(Status)) {\r
1069 return Status;\r
1070 }\r
68074260
RC
1071 for (Index = 1; Index < Step; Index++) {\r
1072 Progress[Index] = L'=';\r
1073 }\r
1074 Progress[Step] = L'>';\r
1075\r
1076 UnicodeSPrint (\r
1077 Progress + (sizeof (mTftpProgressFrame) / sizeof (CHAR16)) - 1,\r
1078 sizeof (Progress) - sizeof (mTftpProgressFrame),\r
1079 L" %7d Kb",\r
1080 NbOfKb\r
1081 );\r
1082 Context->LastReportedNbOfBytes = Context->DownloadedNbOfBytes;\r
1083\r
1084 ShellPrintEx (-1, -1, L"%s", Progress);\r
1085\r
1086 return EFI_SUCCESS;\r
1087}\r
09610023
RN
1088\r
1089/**\r
1090 Retrive HII package list from ImageHandle and publish to HII database.\r
1091\r
1092 @param ImageHandle The image handle of the process.\r
1093\r
1094 @return HII handle.\r
1095**/\r
1096EFI_HANDLE\r
1097InitializeHiiPackage (\r
1098 EFI_HANDLE ImageHandle\r
1099 )\r
1100{\r
1101 EFI_STATUS Status;\r
1102 EFI_HII_PACKAGE_LIST_HEADER *PackageList;\r
1103 EFI_HANDLE HiiHandle;\r
1104\r
1105 //\r
1106 // Retrieve HII package list from ImageHandle\r
1107 //\r
1108 Status = gBS->OpenProtocol (\r
1109 ImageHandle,\r
1110 &gEfiHiiPackageListProtocolGuid,\r
1111 (VOID **)&PackageList,\r
1112 ImageHandle,\r
1113 NULL,\r
1114 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
1115 );\r
1116 ASSERT_EFI_ERROR (Status);\r
1117 if (EFI_ERROR (Status)) {\r
1118 return NULL;\r
1119 }\r
1120\r
1121 //\r
1122 // Publish HII package list to HII Database.\r
1123 //\r
1124 Status = gHiiDatabase->NewPackageList (\r
1125 gHiiDatabase,\r
1126 PackageList,\r
1127 NULL,\r
1128 &HiiHandle\r
1129 );\r
1130 ASSERT_EFI_ERROR (Status);\r
1131 if (EFI_ERROR (Status)) {\r
1132 return NULL;\r
1133 }\r
1134 return HiiHandle;\r
1135}\r