]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcImpl.c
SmmBaseHelper: Update MP state in Framework SMST before transferring control to Frame...
[mirror_edk2.git] / MdeModulePkg / Universal / Network / UefiPxeBcDxe / PxeBcImpl.c
CommitLineData
dc361cc5 1/** @file\r
f737cfb9 2 Interface routines for PxeBc.\r
e2851998 3\r
e5eed7d3
HT
4Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials\r
dc361cc5 6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
dc361cc5 13**/\r
14\r
15\r
16#include "PxeBcImpl.h"\r
17\r
434ce3fe 18UINT32 mPxeDhcpTimeout[4] = { 4, 8, 16, 32 };\r
f737cfb9 19\r
982a9eae 20/**\r
f737cfb9 21 Get and record the arp cache.\r
982a9eae 22\r
23 @param This Pointer to EFI_PXE_BC_PROTOCOL\r
24\r
25 @retval EFI_SUCCESS Arp cache updated successfully\r
f737cfb9 26 @retval others If error occurs when getting arp cache\r
982a9eae 27\r
28**/\r
982a9eae 29EFI_STATUS\r
30UpdateArpCache (\r
31 IN EFI_PXE_BASE_CODE_PROTOCOL * This\r
32 )\r
33{\r
34 PXEBC_PRIVATE_DATA *Private;\r
35 EFI_PXE_BASE_CODE_MODE *Mode;\r
36 EFI_STATUS Status;\r
37 UINT32 EntryLength;\r
38 UINT32 EntryCount;\r
39 EFI_ARP_FIND_DATA *Entries;\r
40 UINT32 Index;\r
41\r
42 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
43 Mode = Private->PxeBc.Mode;\r
44\r
f737cfb9 45 Status = Private->Arp->Find (\r
46 Private->Arp,\r
47 TRUE,\r
48 NULL,\r
49 &EntryLength,\r
50 &EntryCount,\r
51 &Entries,\r
52 TRUE\r
53 );\r
982a9eae 54 if (EFI_ERROR (Status)) {\r
55 return Status;\r
56 }\r
57\r
f737cfb9 58 Mode->ArpCacheEntries = MIN (\r
59 EntryCount,\r
60 EFI_PXE_BASE_CODE_MAX_ARP_ENTRIES\r
61 );\r
982a9eae 62 for (Index = 0; Index < Mode->ArpCacheEntries; Index ++) {\r
f737cfb9 63 CopyMem (\r
e2851998 64 &Mode->ArpCache[Index].IpAddr,\r
65 Entries + 1,\r
f737cfb9 66 Entries->SwAddressLength\r
67 );\r
68 CopyMem (\r
69 &Mode->ArpCache[Index].MacAddr,\r
70 (UINT8 *) (Entries + 1) + Entries->SwAddressLength,\r
71 Entries->HwAddressLength\r
72 );\r
982a9eae 73 //\r
74 // Slip to the next FindData.\r
75 //\r
f737cfb9 76 Entries = (EFI_ARP_FIND_DATA *) ((UINT8 *) Entries + EntryLength);\r
982a9eae 77 }\r
78\r
79 return EFI_SUCCESS;\r
80}\r
81\r
82/**\r
f737cfb9 83 Timeout routine to update arp cache.\r
982a9eae 84\r
85 @param Event Pointer to EFI_PXE_BC_PROTOCOL\r
86 @param Context Context of the timer event\r
87\r
88**/\r
982a9eae 89VOID\r
90EFIAPI\r
91ArpCacheUpdateTimeout (\r
92 IN EFI_EVENT Event,\r
93 IN VOID *Context\r
94 )\r
95{\r
96 UpdateArpCache ((EFI_PXE_BASE_CODE_PROTOCOL *) Context);\r
97}\r
98\r
99/**\r
f737cfb9 100 Do arp resolution from arp cache in PxeBcMode.\r
e2851998 101\r
f737cfb9 102 @param PxeBcMode The PXE BC mode to look into.\r
103 @param Ip4Addr The Ip4 address for resolution.\r
104 @param MacAddress The resoluted MAC address if the resolution is successful.\r
105 The value is undefined if resolution fails.\r
e2851998 106\r
f737cfb9 107 @retval TRUE The resolution is successful.\r
108 @retval FALSE Otherwise.\r
982a9eae 109\r
110**/\r
982a9eae 111BOOLEAN\r
112FindInArpCache (\r
f737cfb9 113 IN EFI_PXE_BASE_CODE_MODE *PxeBcMode,\r
114 IN EFI_IPv4_ADDRESS *Ip4Addr,\r
115 OUT EFI_MAC_ADDRESS *MacAddress\r
982a9eae 116 )\r
117{\r
118 UINT32 Index;\r
119\r
120 for (Index = 0; Index < PxeBcMode->ArpCacheEntries; Index ++) {\r
121 if (EFI_IP4_EQUAL (&PxeBcMode->ArpCache[Index].IpAddr.v4, Ip4Addr)) {\r
f737cfb9 122 CopyMem (\r
123 MacAddress,\r
124 &PxeBcMode->ArpCache[Index].MacAddr,\r
125 sizeof (EFI_MAC_ADDRESS)\r
126 );\r
982a9eae 127 return TRUE;\r
128 }\r
129 }\r
130\r
131 return FALSE;\r
132}\r
133\r
134/**\r
135 Notify function for the ICMP receive token, used to process\r
136 the received ICMP packets.\r
137\r
f737cfb9 138 @param Context The PXEBC private data.\r
982a9eae 139\r
982a9eae 140**/\r
982a9eae 141VOID\r
142EFIAPI\r
143IcmpErrorListenHandlerDpc (\r
144 IN VOID *Context\r
145 )\r
146{\r
147 EFI_STATUS Status;\r
148 EFI_IP4_RECEIVE_DATA *RxData;\r
149 EFI_IP4_PROTOCOL *Ip4;\r
150 PXEBC_PRIVATE_DATA *Private;\r
151 EFI_PXE_BASE_CODE_MODE *Mode;\r
152 UINTN Index;\r
153 UINT32 CopiedLen;\r
154 UINT8 *CopiedPointer;\r
155\r
156 Private = (PXEBC_PRIVATE_DATA *) Context;\r
157 Mode = &Private->Mode;\r
158 Status = Private->IcmpErrorRcvToken.Status;\r
159 RxData = Private->IcmpErrorRcvToken.Packet.RxData;\r
160 Ip4 = Private->Ip4;\r
161\r
f737cfb9 162 if (Status == EFI_ABORTED) {\r
982a9eae 163 //\r
164 // The reception is actively aborted by the consumer, directly return.\r
165 //\r
166 return;\r
167 }\r
168\r
f737cfb9 169 if (EFI_ERROR (Status) || (RxData == NULL)) {\r
982a9eae 170 //\r
171 // Only process the normal packets and the icmp error packets, if RxData is NULL\r
172 // with Status == EFI_SUCCESS or EFI_ICMP_ERROR, just resume the receive although\r
173 // this should be a bug of the low layer (IP).\r
174 //\r
175 goto Resume;\r
176 }\r
177\r
f737cfb9 178 if (EFI_IP4 (RxData->Header->SourceAddress) != 0 &&\r
f6b7393c 179 !NetIp4IsUnicast (EFI_NTOHL (RxData->Header->SourceAddress), 0)) {\r
982a9eae 180 //\r
181 // The source address is not zero and it's not a unicast IP address, discard it.\r
182 //\r
183 goto CleanUp;\r
184 }\r
185\r
186 if (!EFI_IP4_EQUAL (&RxData->Header->DestinationAddress, &Mode->StationIp.v4)) {\r
187 //\r
188 // The dest address is not equal to Station Ip address, discard it.\r
189 //\r
190 goto CleanUp;\r
191 }\r
192\r
193 //\r
194 // Constructor ICMP error packet\r
195 //\r
196 CopiedLen = 0;\r
197 CopiedPointer = (UINT8 *) &Mode->IcmpError;\r
198\r
199 for (Index = 0; Index < RxData->FragmentCount; Index ++) {\r
200 CopiedLen += RxData->FragmentTable[Index].FragmentLength;\r
201 if (CopiedLen <= sizeof (EFI_PXE_BASE_CODE_ICMP_ERROR)) {\r
f737cfb9 202 CopyMem (\r
203 CopiedPointer,\r
204 RxData->FragmentTable[Index].FragmentBuffer,\r
205 RxData->FragmentTable[Index].FragmentLength\r
206 );\r
982a9eae 207 } else {\r
f737cfb9 208 CopyMem (\r
209 CopiedPointer,\r
210 RxData->FragmentTable[Index].FragmentBuffer,\r
211 CopiedLen - sizeof (EFI_PXE_BASE_CODE_ICMP_ERROR)\r
212 );\r
982a9eae 213 }\r
214 CopiedPointer += CopiedLen;\r
215 }\r
216\r
217 goto Resume;\r
218\r
219CleanUp:\r
220 gBS->SignalEvent (RxData->RecycleSignal);\r
221\r
222Resume:\r
223 Ip4->Receive (Ip4, &(Private->IcmpErrorRcvToken));\r
224}\r
225\r
226/**\r
227 Request IcmpErrorListenHandlerDpc as a DPC at TPL_CALLBACK\r
228\r
229 @param Event The event signaled.\r
230 @param Context The context passed in by the event notifier.\r
231\r
982a9eae 232**/\r
982a9eae 233VOID\r
234EFIAPI\r
235IcmpErrorListenHandler (\r
236 IN EFI_EVENT Event,\r
237 IN VOID *Context\r
238 )\r
239{\r
240 //\r
241 // Request IpIoListenHandlerDpc as a DPC at TPL_CALLBACK\r
242 //\r
d8d26fb2 243 QueueDpc (TPL_CALLBACK, IcmpErrorListenHandlerDpc, Context);\r
982a9eae 244}\r
dc361cc5 245\r
e2851998 246/**\r
f737cfb9 247 Enables the use of the PXE Base Code Protocol functions.\r
248\r
249 This function enables the use of the PXE Base Code Protocol functions. If the\r
250 Started field of the EFI_PXE_BASE_CODE_MODE structure is already TRUE, then\r
251 EFI_ALREADY_STARTED will be returned. If UseIpv6 is TRUE, then IPv6 formatted\r
252 addresses will be used in this session. If UseIpv6 is FALSE, then IPv4 formatted\r
253 addresses will be used in this session. If UseIpv6 is TRUE, and the Ipv6Supported\r
254 field of the EFI_PXE_BASE_CODE_MODE structure is FALSE, then EFI_UNSUPPORTED will\r
255 be returned. If there is not enough memory or other resources to start the PXE\r
256 Base Code Protocol, then EFI_OUT_OF_RESOURCES will be returned. Otherwise, the\r
257 PXE Base Code Protocol will be started, and all of the fields of the EFI_PXE_BASE_CODE_MODE\r
258 structure will be initialized as follows:\r
259 StartedSet to TRUE.\r
260 Ipv6SupportedUnchanged.\r
261 Ipv6AvailableUnchanged.\r
262 UsingIpv6Set to UseIpv6.\r
263 BisSupportedUnchanged.\r
264 BisDetectedUnchanged.\r
265 AutoArpSet to TRUE.\r
266 SendGUIDSet to FALSE.\r
267 TTLSet to DEFAULT_TTL.\r
268 ToSSet to DEFAULT_ToS.\r
269 DhcpCompletedSet to FALSE.\r
270 ProxyOfferReceivedSet to FALSE.\r
271 StationIpSet to an address of all zeros.\r
272 SubnetMaskSet to a subnet mask of all zeros.\r
273 DhcpDiscoverZero-filled.\r
274 DhcpAckZero-filled.\r
275 ProxyOfferZero-filled.\r
276 PxeDiscoverValidSet to FALSE.\r
277 PxeDiscoverZero-filled.\r
278 PxeReplyValidSet to FALSE.\r
279 PxeReplyZero-filled.\r
280 PxeBisReplyValidSet to FALSE.\r
281 PxeBisReplyZero-filled.\r
282 IpFilterSet the Filters field to 0 and the IpCnt field to 0.\r
283 ArpCacheEntriesSet to 0.\r
284 ArpCacheZero-filled.\r
285 RouteTableEntriesSet to 0.\r
286 RouteTableZero-filled.\r
287 IcmpErrorReceivedSet to FALSE.\r
288 IcmpErrorZero-filled.\r
289 TftpErroReceivedSet to FALSE.\r
290 TftpErrorZero-filled.\r
291 MakeCallbacksSet to TRUE if the PXE Base Code Callback Protocol is available.\r
292 Set to FALSE if the PXE Base Code Callback Protocol is not available.\r
e2851998 293\r
f737cfb9 294 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
295 @param UseIpv6 Specifies the type of IP addresses that are to be used during the session\r
e2851998 296 that is being started. Set to TRUE for IPv6 addresses, and FALSE for\r
297 IPv4 addresses.\r
298\r
f737cfb9 299 @retval EFI_SUCCESS The PXE Base Code Protocol was started.\r
e2851998 300 @retval EFI_DEVICE_ERROR The network device encountered an error during this oper\r
f737cfb9 301 @retval EFI_UNSUPPORTED UseIpv6 is TRUE, but the Ipv6Supported field of the\r
e2851998 302 EFI_PXE_BASE_CODE_MODE structure is FALSE.\r
303 @retval EFI_ALREADY_STARTED The PXE Base Code Protocol is already in the started state.\r
f737cfb9 304 @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid\r
e2851998 305 EFI_PXE_BASE_CODE_PROTOCOL structure.\r
306 @retval EFI_OUT_OF_RESOURCES Could not allocate enough memory or other resources to start the\r
307 PXE Base Code Protocol.\r
308\r
dc361cc5 309**/\r
310EFI_STATUS\r
311EFIAPI\r
312EfiPxeBcStart (\r
313 IN EFI_PXE_BASE_CODE_PROTOCOL *This,\r
314 IN BOOLEAN UseIpv6\r
315 )\r
316{\r
317 PXEBC_PRIVATE_DATA *Private;\r
318 EFI_PXE_BASE_CODE_MODE *Mode;\r
319 EFI_STATUS Status;\r
320\r
321 if (This == NULL) {\r
322 return EFI_INVALID_PARAMETER;\r
323 }\r
324\r
325 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
326 Mode = Private->PxeBc.Mode;\r
327\r
328 if (Mode->Started) {\r
329 return EFI_ALREADY_STARTED;\r
330 }\r
331\r
332 if (UseIpv6) {\r
333 //\r
334 // IPv6 is not supported now.\r
335 //\r
336 return EFI_UNSUPPORTED;\r
337 }\r
338\r
339 //\r
340 // Configure the udp4 instance to let it receive data\r
341 //\r
f737cfb9 342 Status = Private->Udp4Read->Configure (\r
e2851998 343 Private->Udp4Read,\r
f737cfb9 344 &Private->Udp4CfgData\r
345 );\r
dc361cc5 346 if (EFI_ERROR (Status)) {\r
347 return Status;\r
348 }\r
349\r
11fa4298 350 Private->BlockSize = 0x8000;\r
dc361cc5 351 Private->AddressIsOk = FALSE;\r
352\r
353 ZeroMem (Mode, sizeof (EFI_PXE_BASE_CODE_MODE));\r
354\r
355 Mode->Started = TRUE;\r
356 Mode->TTL = DEFAULT_TTL;\r
357 Mode->ToS = DEFAULT_ToS;\r
358 Mode->AutoArp = TRUE;\r
359\r
982a9eae 360 //\r
361 // Create the event for Arp Cache checking.\r
362 //\r
363 Status = gBS->CreateEvent (\r
364 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
365 TPL_CALLBACK,\r
366 ArpCacheUpdateTimeout,\r
367 This,\r
368 &Private->GetArpCacheEvent\r
369 );\r
370 if (EFI_ERROR (Status)) {\r
371 goto ON_EXIT;\r
372 }\r
373\r
374 //\r
375 // Start the timeout timer event.\r
376 //\r
377 Status = gBS->SetTimer (\r
378 Private->GetArpCacheEvent,\r
379 TimerPeriodic,\r
380 TICKS_PER_SECOND\r
381 );\r
382\r
383 if (EFI_ERROR (Status)) {\r
384 goto ON_EXIT;\r
385 }\r
386\r
387 //\r
388 // Create ICMP error receiving event\r
389 //\r
390 Status = gBS->CreateEvent (\r
391 EVT_NOTIFY_SIGNAL,\r
392 TPL_NOTIFY,\r
393 IcmpErrorListenHandler,\r
394 Private,\r
395 &(Private->IcmpErrorRcvToken.Event)\r
396 );\r
397 if (EFI_ERROR (Status)) {\r
398 goto ON_EXIT;\r
399 }\r
400\r
401 Status = Private->Ip4->Configure (Private->Ip4, &Private->Ip4ConfigData);\r
402 if (EFI_ERROR (Status)) {\r
403 goto ON_EXIT;\r
404 }\r
405\r
406 //\r
407 // start to listen incoming packet\r
408 //\r
409 Status = Private->Ip4->Receive (Private->Ip4, &Private->IcmpErrorRcvToken);\r
410 if (!EFI_ERROR (Status)) {\r
411 return Status;\r
412 }\r
413\r
414ON_EXIT:\r
415 Private->Ip4->Configure (Private->Ip4, NULL);\r
416\r
417 if (Private->IcmpErrorRcvToken.Event != NULL) {\r
418 gBS->CloseEvent (Private->IcmpErrorRcvToken.Event);\r
419 }\r
420\r
421 if (Private->GetArpCacheEvent != NULL) {\r
422 gBS->SetTimer (Private->GetArpCacheEvent, TimerCancel, 0);\r
423 gBS->CloseEvent (Private->GetArpCacheEvent);\r
424 }\r
425\r
426 Mode->Started = FALSE;\r
427 Mode->TTL = 0;\r
428 Mode->ToS = 0;\r
429 Mode->AutoArp = FALSE;\r
430\r
431 return Status;\r
dc361cc5 432}\r
433\r
434\r
e2851998 435/**\r
f737cfb9 436 Disables the use of the PXE Base Code Protocol functions.\r
437\r
438 This function stops all activity on the network device. All the resources allocated\r
439 in Start() are released, the Started field of the EFI_PXE_BASE_CODE_MODE structure is\r
440 set to FALSE and EFI_SUCCESS is returned. If the Started field of the EFI_PXE_BASE_CODE_MODE\r
441 structure is already FALSE, then EFI_NOT_STARTED will be returned.\r
e2851998 442\r
f737cfb9 443 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
e2851998 444\r
f737cfb9 445 @retval EFI_SUCCESS The PXE Base Code Protocol was stopped.\r
e2851998 446 @retval EFI_NOT_STARTED The PXE Base Code Protocol is already in the stopped state.\r
f737cfb9 447 @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid\r
e2851998 448 EFI_PXE_BASE_CODE_PROTOCOL structure.\r
449 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.\r
450\r
dc361cc5 451**/\r
452EFI_STATUS\r
453EFIAPI\r
454EfiPxeBcStop (\r
455 IN EFI_PXE_BASE_CODE_PROTOCOL *This\r
456 )\r
457{\r
458 PXEBC_PRIVATE_DATA *Private;\r
459 EFI_PXE_BASE_CODE_MODE *Mode;\r
460\r
461 if (This == NULL) {\r
462 return EFI_INVALID_PARAMETER;\r
463 }\r
464\r
465 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
466 Mode = Private->PxeBc.Mode;\r
467\r
468 if (!Mode->Started) {\r
469 return EFI_NOT_STARTED;\r
470 }\r
471\r
982a9eae 472 Private->Ip4->Cancel (Private->Ip4, NULL);\r
473 //\r
474 // Dispatch the DPCs queued by the NotifyFunction of the canceled rx token's\r
475 // events.\r
476 //\r
d8d26fb2 477 DispatchDpc ();\r
982a9eae 478\r
479 Private->Ip4->Configure (Private->Ip4, NULL);\r
480\r
481 //\r
482 // Close the ICMP error receiving event.\r
483 //\r
484 gBS->CloseEvent (Private->IcmpErrorRcvToken.Event);\r
485\r
486 //\r
487 // Cancel the TimeoutEvent timer.\r
488 //\r
489 gBS->SetTimer (Private->GetArpCacheEvent, TimerCancel, 0);\r
490\r
491 //\r
492 // Close the TimeoutEvent event.\r
493 //\r
494 gBS->CloseEvent (Private->GetArpCacheEvent);\r
495\r
dc361cc5 496 Mode->Started = FALSE;\r
497\r
8792362f 498 Private->CurrentUdpSrcPort = 0;\r
499 Private->Udp4Write->Configure (Private->Udp4Write, NULL);\r
434ce3fe 500 Private->Udp4Read->Groups (Private->Udp4Read, FALSE, NULL);\r
8792362f 501 Private->Udp4Read->Configure (Private->Udp4Read, NULL);\r
dc361cc5 502\r
503 Private->Dhcp4->Stop (Private->Dhcp4);\r
504 Private->Dhcp4->Configure (Private->Dhcp4, NULL);\r
505\r
506 Private->FileSize = 0;\r
507\r
508 return EFI_SUCCESS;\r
509}\r
510\r
511\r
e2851998 512/**\r
f737cfb9 513 Attempts to complete a DHCPv4 D.O.R.A. (discover / offer / request / acknowledge) or DHCPv6\r
514 S.A.R.R (solicit / advertise / request / reply) sequence.\r
515\r
516 This function attempts to complete the DHCP sequence. If this sequence is completed,\r
517 then EFI_SUCCESS is returned, and the DhcpCompleted, ProxyOfferReceived, StationIp,\r
518 SubnetMask, DhcpDiscover, DhcpAck, and ProxyOffer fields of the EFI_PXE_BASE_CODE_MODE\r
519 structure are filled in.\r
520 If SortOffers is TRUE, then the cached DHCP offer packets will be sorted before\r
521 they are tried. If SortOffers is FALSE, then the cached DHCP offer packets will\r
522 be tried in the order in which they are received. Please see the Preboot Execution\r
523 Environment (PXE) Specification for additional details on the implementation of DHCP.\r
524 This function can take at least 31 seconds to timeout and return control to the\r
525 caller. If the DHCP sequence does not complete, then EFI_TIMEOUT will be returned.\r
526 If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,\r
527 then the DHCP sequence will be stopped and EFI_ABORTED will be returned.\r
e2851998 528\r
f737cfb9 529 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
530 @param SortOffers TRUE if the offers received should be sorted. Set to FALSE to try the\r
e2851998 531 offers in the order that they are received.\r
532\r
f737cfb9 533 @retval EFI_SUCCESS Valid DHCP has completed.\r
534 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.\r
535 @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid\r
e2851998 536 EFI_PXE_BASE_CODE_PROTOCOL structure.\r
537 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.\r
f737cfb9 538 @retval EFI_OUT_OF_RESOURCES Could not allocate enough memory to complete the DHCP Protocol.\r
539 @retval EFI_ABORTED The callback function aborted the DHCP Protocol.\r
540 @retval EFI_TIMEOUT The DHCP Protocol timed out.\r
541 @retval EFI_ICMP_ERROR An ICMP error packet was received during the DHCP session.\r
542 @retval EFI_NO_RESPONSE Valid PXE offer was not received.\r
e2851998 543\r
dc361cc5 544**/\r
545EFI_STATUS\r
546EFIAPI\r
547EfiPxeBcDhcp (\r
548 IN EFI_PXE_BASE_CODE_PROTOCOL *This,\r
549 IN BOOLEAN SortOffers\r
550 )\r
551{\r
552 PXEBC_PRIVATE_DATA *Private;\r
553 EFI_PXE_BASE_CODE_MODE *Mode;\r
554 EFI_DHCP4_PROTOCOL *Dhcp4;\r
555 EFI_DHCP4_CONFIG_DATA Dhcp4CfgData;\r
556 EFI_DHCP4_MODE_DATA Dhcp4Mode;\r
557 EFI_DHCP4_PACKET_OPTION *OptList[PXEBC_DHCP4_MAX_OPTION_NUM];\r
558 UINT32 OptCount;\r
dc361cc5 559 EFI_STATUS Status;\r
8d285ec0 560 EFI_ARP_CONFIG_DATA ArpConfigData;\r
dc361cc5 561\r
562 if (This == NULL) {\r
563 return EFI_INVALID_PARAMETER;\r
564 }\r
565\r
566 Status = EFI_SUCCESS;\r
567 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
568 Mode = Private->PxeBc.Mode;\r
569 Dhcp4 = Private->Dhcp4;\r
570 Private->Function = EFI_PXE_BASE_CODE_FUNCTION_DHCP;\r
571 Private->SortOffers = SortOffers;\r
572\r
573 if (!Mode->Started) {\r
574 return EFI_NOT_STARTED;\r
575 }\r
982a9eae 576\r
577 Mode->IcmpErrorReceived = FALSE;\r
578\r
dc361cc5 579 //\r
580 // Initialize the DHCP options and build the option list\r
581 //\r
582 OptCount = PxeBcBuildDhcpOptions (Private, OptList, TRUE);\r
583\r
584 //\r
585 // Set the DHCP4 config data.\r
434ce3fe 586 // The four discovery timeouts are 4, 8, 16, 32 seconds respectively.\r
dc361cc5 587 //\r
e48e37fc 588 ZeroMem (&Dhcp4CfgData, sizeof (EFI_DHCP4_CONFIG_DATA));\r
dc361cc5 589 Dhcp4CfgData.OptionCount = OptCount;\r
590 Dhcp4CfgData.OptionList = OptList;\r
591 Dhcp4CfgData.Dhcp4Callback = PxeBcDhcpCallBack;\r
592 Dhcp4CfgData.CallbackContext = Private;\r
434ce3fe 593 Dhcp4CfgData.DiscoverTryCount = 4;\r
594 Dhcp4CfgData.DiscoverTimeout = mPxeDhcpTimeout;\r
dc361cc5 595\r
434ce3fe 596 Status = Dhcp4->Configure (Dhcp4, &Dhcp4CfgData);\r
597 if (EFI_ERROR (Status)) {\r
598 goto ON_EXIT;\r
599 }\r
e2851998 600\r
434ce3fe 601 //\r
602 // Zero those arrays to record the varies numbers of DHCP OFFERS.\r
603 //\r
604 Private->GotProxyOffer = FALSE;\r
605 Private->NumOffers = 0;\r
606 Private->BootpIndex = 0;\r
607 ZeroMem (Private->ServerCount, sizeof (Private->ServerCount));\r
608 ZeroMem (Private->ProxyIndex, sizeof (Private->ProxyIndex));\r
dc361cc5 609\r
434ce3fe 610 Status = Dhcp4->Start (Dhcp4, NULL);\r
611 if (EFI_ERROR (Status)) {\r
612 if (Status == EFI_ICMP_ERROR) {\r
613 Mode->IcmpErrorReceived = TRUE;\r
dc361cc5 614 }\r
434ce3fe 615 goto ON_EXIT;\r
616 }\r
dc361cc5 617\r
434ce3fe 618 Status = Dhcp4->GetModeData (Dhcp4, &Dhcp4Mode);\r
619 if (EFI_ERROR (Status)) {\r
620 goto ON_EXIT;\r
621 }\r
dc361cc5 622\r
434ce3fe 623 ASSERT (Dhcp4Mode.State == Dhcp4Bound);\r
dc361cc5 624\r
434ce3fe 625 CopyMem (&Private->StationIp, &Dhcp4Mode.ClientAddress, sizeof (EFI_IPv4_ADDRESS));\r
626 CopyMem (&Private->SubnetMask, &Dhcp4Mode.SubnetMask, sizeof (EFI_IPv4_ADDRESS));\r
627 CopyMem (&Private->GatewayIp, &Dhcp4Mode.RouterAddress, sizeof (EFI_IPv4_ADDRESS));\r
319075ff 628\r
434ce3fe 629 CopyMem (&Mode->StationIp, &Private->StationIp, sizeof (EFI_IPv4_ADDRESS));\r
630 CopyMem (&Mode->SubnetMask, &Private->SubnetMask, sizeof (EFI_IPv4_ADDRESS));\r
dc361cc5 631\r
434ce3fe 632 //\r
633 // Check the selected offer to see whether BINL is required, if no or BINL is\r
634 // finished, set the various Mode members.\r
635 //\r
636 Status = PxeBcCheckSelectedOffer (Private);\r
637 if (!EFI_ERROR (Status)) {\r
638 goto ON_EXIT;\r
dc361cc5 639 }\r
640\r
434ce3fe 641ON_EXIT:\r
dc361cc5 642 if (EFI_ERROR (Status)) {\r
643 Dhcp4->Stop (Dhcp4);\r
644 Dhcp4->Configure (Dhcp4, NULL);\r
645 } else {\r
646 //\r
647 // Remove the previously configured option list and callback function\r
648 //\r
e48e37fc 649 ZeroMem (&Dhcp4CfgData, sizeof (EFI_DHCP4_CONFIG_DATA));\r
dc361cc5 650 Dhcp4->Configure (Dhcp4, &Dhcp4CfgData);\r
651\r
652 Private->AddressIsOk = TRUE;\r
8d285ec0 653\r
654 if (!Mode->UsingIpv6) {\r
655 //\r
656 // If in IPv4 mode, configure the corresponding ARP with this new\r
657 // station IP address.\r
658 //\r
659 ZeroMem (&ArpConfigData, sizeof (EFI_ARP_CONFIG_DATA));\r
660\r
661 ArpConfigData.SwAddressType = 0x0800;\r
662 ArpConfigData.SwAddressLength = sizeof (EFI_IPv4_ADDRESS);\r
663 ArpConfigData.StationAddress = &Private->StationIp.v4;\r
664\r
665 Private->Arp->Configure (Private->Arp, NULL);\r
666 Private->Arp->Configure (Private->Arp, &ArpConfigData);\r
982a9eae 667\r
668 //\r
669 // Updated the route table. Fill the first entry.\r
670 //\r
671 Mode->RouteTableEntries = 1;\r
672 Mode->RouteTable[0].IpAddr.Addr[0] = Private->StationIp.Addr[0] & Private->SubnetMask.Addr[0];\r
673 Mode->RouteTable[0].SubnetMask.Addr[0] = Private->SubnetMask.Addr[0];\r
674 Mode->RouteTable[0].GwAddr.Addr[0] = 0;\r
675\r
676 //\r
677 // Create the default route entry if there is a default router.\r
678 //\r
679 if (Private->GatewayIp.Addr[0] != 0) {\r
680 Mode->RouteTableEntries = 2;\r
681 Mode->RouteTable[1].IpAddr.Addr[0] = 0;\r
682 Mode->RouteTable[1].SubnetMask.Addr[0] = 0;\r
683 Mode->RouteTable[1].GwAddr.Addr[0] = Private->GatewayIp.Addr[0];\r
684 }\r
8d285ec0 685 }\r
dc361cc5 686 }\r
687\r
688 return Status;\r
689}\r
690\r
691\r
e2851998 692/**\r
f737cfb9 693 Attempts to complete the PXE Boot Server and/or boot image discovery sequence.\r
694\r
695 This function attempts to complete the PXE Boot Server and/or boot image discovery\r
696 sequence. If this sequence is completed, then EFI_SUCCESS is returned, and the\r
697 PxeDiscoverValid, PxeDiscover, PxeReplyReceived, and PxeReply fields of the\r
698 EFI_PXE_BASE_CODE_MODE structure are filled in. If UseBis is TRUE, then the\r
699 PxeBisReplyReceived and PxeBisReply fields of the EFI_PXE_BASE_CODE_MODE structure\r
700 will also be filled in. If UseBis is FALSE, then PxeBisReplyValid will be set to FALSE.\r
701 In the structure referenced by parameter Info, the PXE Boot Server list, SrvList[],\r
702 has two uses: It is the Boot Server IP address list used for unicast discovery\r
703 (if the UseUCast field is TRUE), and it is the list used for Boot Server verification\r
704 (if the MustUseList field is TRUE). Also, if the MustUseList field in that structure\r
705 is TRUE and the AcceptAnyResponse field in the SrvList[] array is TRUE, any Boot\r
706 Server reply of that type will be accepted. If the AcceptAnyResponse field is\r
707 FALSE, only responses from Boot Servers with matching IP addresses will be accepted.\r
708 This function can take at least 10 seconds to timeout and return control to the\r
709 caller. If the Discovery sequence does not complete, then EFI_TIMEOUT will be\r
710 returned. Please see the Preboot Execution Environment (PXE) Specification for\r
711 additional details on the implementation of the Discovery sequence.\r
712 If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,\r
713 then the Discovery sequence is stopped and EFI_ABORTED will be returned.\r
e2851998 714\r
f737cfb9 715 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
716 @param Type The type of bootstrap to perform.\r
717 @param Layer Pointer to the boot server layer number to discover, which must be\r
e2851998 718 PXE_BOOT_LAYER_INITIAL when a new server type is being\r
719 discovered.\r
720 @param UseBis TRUE if Boot Integrity Services are to be used. FALSE otherwise.\r
f737cfb9 721 @param Info Pointer to a data structure that contains additional information on the\r
e2851998 722 type of discovery operation that is to be performed.\r
723\r
f737cfb9 724 @retval EFI_SUCCESS The Discovery sequence has been completed.\r
725 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.\r
e2851998 726 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
727 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.\r
f737cfb9 728 @retval EFI_OUT_OF_RESOURCES Could not allocate enough memory to complete Discovery.\r
729 @retval EFI_ABORTED The callback function aborted the Discovery sequence.\r
730 @retval EFI_TIMEOUT The Discovery sequence timed out.\r
731 @retval EFI_ICMP_ERROR An ICMP error packet was received during the PXE discovery\r
e2851998 732 session.\r
733\r
dc361cc5 734**/\r
735EFI_STATUS\r
736EFIAPI\r
737EfiPxeBcDiscover (\r
738 IN EFI_PXE_BASE_CODE_PROTOCOL *This,\r
739 IN UINT16 Type,\r
740 IN UINT16 *Layer,\r
741 IN BOOLEAN UseBis,\r
742 IN EFI_PXE_BASE_CODE_DISCOVER_INFO *Info OPTIONAL\r
743 )\r
744{\r
745 PXEBC_PRIVATE_DATA *Private;\r
746 EFI_PXE_BASE_CODE_MODE *Mode;\r
747 EFI_PXE_BASE_CODE_DISCOVER_INFO DefaultInfo;\r
29a4f92d 748 EFI_PXE_BASE_CODE_DISCOVER_INFO *CreatedInfo;\r
dc361cc5 749 EFI_PXE_BASE_CODE_SRVLIST *SrvList;\r
750 EFI_PXE_BASE_CODE_SRVLIST DefaultSrvList;\r
751 PXEBC_CACHED_DHCP4_PACKET *Packet;\r
f737cfb9 752 PXEBC_VENDOR_OPTION *VendorOpt;\r
dc361cc5 753 UINT16 Index;\r
754 EFI_STATUS Status;\r
755 PXEBC_BOOT_SVR_ENTRY *BootSvrEntry;\r
756\r
757 if (This == NULL) {\r
758 return EFI_INVALID_PARAMETER;\r
759 }\r
760\r
761 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
762 Mode = Private->PxeBc.Mode;\r
763 BootSvrEntry = NULL;\r
764 SrvList = NULL;\r
29a4f92d 765 CreatedInfo = NULL;\r
dc361cc5 766 Status = EFI_DEVICE_ERROR;\r
767 Private->Function = EFI_PXE_BASE_CODE_FUNCTION_DISCOVER;\r
768\r
769 if (!Private->AddressIsOk) {\r
770 return EFI_INVALID_PARAMETER;\r
771 }\r
772\r
773 if (!Mode->Started) {\r
774 return EFI_NOT_STARTED;\r
775 }\r
776\r
982a9eae 777 Mode->IcmpErrorReceived = FALSE;\r
778\r
dc361cc5 779 //\r
780 // If layer isn't EFI_PXE_BASE_CODE_BOOT_LAYER_INITIAL,\r
781 // use the previous setting;\r
782 // If info isn't offered,\r
783 // use the cached DhcpAck and ProxyOffer packets.\r
784 //\r
785 if (*Layer != EFI_PXE_BASE_CODE_BOOT_LAYER_INITIAL) {\r
786\r
787 if (!Mode->PxeDiscoverValid || !Mode->PxeReplyReceived || (!Mode->PxeBisReplyReceived && UseBis)) {\r
788\r
789 return EFI_INVALID_PARAMETER;\r
790 }\r
791\r
792 DefaultInfo.IpCnt = 1;\r
793 DefaultInfo.UseUCast = TRUE;\r
794\r
795 DefaultSrvList.Type = Type;\r
796 DefaultSrvList.AcceptAnyResponse = FALSE;\r
797 DefaultSrvList.IpAddr.Addr[0] = Private->ServerIp.Addr[0];\r
798\r
799 SrvList = &DefaultSrvList;\r
800 Info = &DefaultInfo;\r
801 } else if (Info == NULL) {\r
802 //\r
803 // Create info by the cached packet before\r
804 //\r
805 Packet = (Mode->ProxyOfferReceived) ? &Private->ProxyOffer : &Private->Dhcp4Ack;\r
806 VendorOpt = &Packet->PxeVendorOption;\r
807\r
808 if (!Mode->DhcpAckReceived || !IS_VALID_DISCOVER_VENDOR_OPTION (VendorOpt->BitMap)) {\r
809 //\r
810 // Address is not acquired or no discovery options.\r
811 //\r
812 return EFI_INVALID_PARAMETER;\r
813 }\r
814\r
815 DefaultInfo.UseMCast = (BOOLEAN)!IS_DISABLE_MCAST_DISCOVER (VendorOpt->DiscoverCtrl);\r
816 DefaultInfo.UseBCast = (BOOLEAN)!IS_DISABLE_BCAST_DISCOVER (VendorOpt->DiscoverCtrl);\r
817 DefaultInfo.MustUseList = (BOOLEAN) IS_ENABLE_USE_SERVER_LIST (VendorOpt->DiscoverCtrl);\r
818 DefaultInfo.UseUCast = DefaultInfo.MustUseList;\r
819\r
820 if (DefaultInfo.UseMCast) {\r
821 //\r
822 // Get the multicast discover ip address from vendor option.\r
823 //\r
f737cfb9 824 CopyMem (\r
e2851998 825 &DefaultInfo.ServerMCastIp.Addr,\r
826 &VendorOpt->DiscoverMcastIp,\r
f737cfb9 827 sizeof (EFI_IPv4_ADDRESS)\r
828 );\r
dc361cc5 829 }\r
830\r
831 DefaultInfo.IpCnt = 0;\r
29a4f92d 832 Info = &DefaultInfo;\r
833 SrvList = Info->SrvList;\r
dc361cc5 834\r
835 if (DefaultInfo.MustUseList) {\r
836 BootSvrEntry = VendorOpt->BootSvr;\r
837 Status = EFI_INVALID_PARAMETER;\r
838\r
839 while (((UINT8) (BootSvrEntry - VendorOpt->BootSvr)) < VendorOpt->BootSvrLen) {\r
840\r
841 if (BootSvrEntry->Type == HTONS (Type)) {\r
842 Status = EFI_SUCCESS;\r
843 break;\r
844 }\r
845\r
846 BootSvrEntry = GET_NEXT_BOOT_SVR_ENTRY (BootSvrEntry);\r
847 }\r
848\r
849 if (EFI_ERROR (Status)) {\r
850 return Status;\r
851 }\r
852\r
853 DefaultInfo.IpCnt = BootSvrEntry->IpCnt;\r
29a4f92d 854\r
855 if (DefaultInfo.IpCnt >= 1) {\r
856 CreatedInfo = AllocatePool (sizeof (DefaultInfo) + (DefaultInfo.IpCnt - 1) * sizeof (*SrvList));\r
857 if (CreatedInfo == NULL) {\r
858 return EFI_OUT_OF_RESOURCES;\r
859 } \r
860 \r
861 CopyMem (CreatedInfo, &DefaultInfo, sizeof (DefaultInfo));\r
862 Info = CreatedInfo;\r
863 SrvList = Info->SrvList;\r
864 }\r
865\r
866 for (Index = 0; Index < DefaultInfo.IpCnt; Index++) {\r
867 CopyMem (&SrvList[Index].IpAddr, &BootSvrEntry->IpAddr[Index], sizeof (EFI_IPv4_ADDRESS));\r
868 SrvList[Index].AcceptAnyResponse = FALSE;\r
869 SrvList[Index].Type = BootSvrEntry->Type;\r
870 }\r
dc361cc5 871 }\r
872\r
dc361cc5 873 } else {\r
874\r
875 SrvList = Info->SrvList;\r
876\r
877 if (!SrvList[0].AcceptAnyResponse) {\r
878\r
879 for (Index = 1; Index < Info->IpCnt; Index++) {\r
880 if (SrvList[Index].AcceptAnyResponse) {\r
881 break;\r
882 }\r
883 }\r
884\r
885 if (Index != Info->IpCnt) {\r
886 return EFI_INVALID_PARAMETER;\r
887 }\r
888 }\r
889 }\r
890\r
891 if ((!Info->UseUCast && !Info->UseBCast && !Info->UseMCast) || (Info->MustUseList && Info->IpCnt == 0)) {\r
892\r
893 return EFI_INVALID_PARAMETER;\r
894 }\r
895 //\r
896 // Execute discover by UniCast/BroadCast/MultiCast\r
897 //\r
898 if (Info->UseUCast) {\r
899\r
900 for (Index = 0; Index < Info->IpCnt; Index++) {\r
901\r
902 if (BootSvrEntry == NULL) {\r
903 Private->ServerIp.Addr[0] = SrvList[Index].IpAddr.Addr[0];\r
904 } else {\r
f737cfb9 905 CopyMem (\r
e2851998 906 &Private->ServerIp,\r
907 &BootSvrEntry->IpAddr[Index],\r
f737cfb9 908 sizeof (EFI_IPv4_ADDRESS)\r
909 );\r
dc361cc5 910 }\r
911\r
912 Status = PxeBcDiscvBootService (\r
913 Private,\r
914 Type,\r
915 Layer,\r
916 UseBis,\r
917 &SrvList[Index].IpAddr,\r
918 0,\r
919 NULL,\r
920 TRUE,\r
921 &Private->PxeReply.Packet.Ack\r
922 );\r
29a4f92d 923 if (!EFI_ERROR (Status)) {\r
924 break;\r
925 } \r
dc361cc5 926 }\r
927\r
928 } else if (Info->UseMCast) {\r
929\r
930 Status = PxeBcDiscvBootService (\r
931 Private,\r
932 Type,\r
933 Layer,\r
934 UseBis,\r
935 &Info->ServerMCastIp,\r
936 0,\r
937 NULL,\r
938 TRUE,\r
939 &Private->PxeReply.Packet.Ack\r
940 );\r
941\r
942 } else if (Info->UseBCast) {\r
943\r
944 Status = PxeBcDiscvBootService (\r
945 Private,\r
946 Type,\r
947 Layer,\r
948 UseBis,\r
949 NULL,\r
950 Info->IpCnt,\r
951 SrvList,\r
952 TRUE,\r
953 &Private->PxeReply.Packet.Ack\r
954 );\r
955 }\r
956\r
957 if (EFI_ERROR (Status) || !Mode->PxeReplyReceived || (!Mode->PxeBisReplyReceived && UseBis)) {\r
982a9eae 958 if (Status == EFI_ICMP_ERROR) {\r
959 Mode->IcmpErrorReceived = TRUE;\r
960 } else {\r
961 Status = EFI_DEVICE_ERROR;\r
962 }\r
dc361cc5 963 } else {\r
964 PxeBcParseCachedDhcpPacket (&Private->PxeReply);\r
965 }\r
966\r
967 if (Mode->PxeBisReplyReceived) {\r
f737cfb9 968 CopyMem (\r
e2851998 969 &Private->ServerIp,\r
970 &Mode->PxeReply.Dhcpv4.BootpSiAddr,\r
f737cfb9 971 sizeof (EFI_IPv4_ADDRESS)\r
972 );\r
dc361cc5 973 }\r
974\r
29a4f92d 975 if (CreatedInfo != NULL) {\r
976 FreePool (CreatedInfo);\r
977 }\r
978 \r
dc361cc5 979 return Status;\r
980}\r
981\r
982\r
e2851998 983/**\r
f737cfb9 984 Used to perform TFTP and MTFTP services.\r
985\r
986 This function is used to perform TFTP and MTFTP services. This includes the\r
987 TFTP operations to get the size of a file, read a directory, read a file, and\r
988 write a file. It also includes the MTFTP operations to get the size of a file,\r
989 read a directory, and read a file. The type of operation is specified by Operation.\r
990 If the callback function that is invoked during the TFTP/MTFTP operation does\r
991 not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE, then EFI_ABORTED will\r
992 be returned.\r
993 For read operations, the return data will be placed in the buffer specified by\r
994 BufferPtr. If BufferSize is too small to contain the entire downloaded file,\r
995 then EFI_BUFFER_TOO_SMALL will be returned and BufferSize will be set to zero\r
996 or the size of the requested file (the size of the requested file is only returned\r
997 if the TFTP server supports TFTP options). If BufferSize is large enough for the\r
998 read operation, then BufferSize will be set to the size of the downloaded file,\r
999 and EFI_SUCCESS will be returned. Applications using the PxeBc.Mtftp() services\r
1000 should use the get-file-size operations to determine the size of the downloaded\r
1001 file prior to using the read-file operations-especially when downloading large\r
1002 (greater than 64 MB) files-instead of making two calls to the read-file operation.\r
1003 Following this recommendation will save time if the file is larger than expected\r
1004 and the TFTP server does not support TFTP option extensions. Without TFTP option\r
1005 extension support, the client has to download the entire file, counting and discarding\r
1006 the received packets, to determine the file size.\r
1007 For write operations, the data to be sent is in the buffer specified by BufferPtr.\r
1008 BufferSize specifies the number of bytes to send. If the write operation completes\r
1009 successfully, then EFI_SUCCESS will be returned.\r
1010 For TFTP "get file size" operations, the size of the requested file or directory\r
1011 is returned in BufferSize, and EFI_SUCCESS will be returned. If the TFTP server\r
1012 does not support options, the file will be downloaded into a bit bucket and the\r
1013 length of the downloaded file will be returned. For MTFTP "get file size" operations,\r
1014 if the MTFTP server does not support the "get file size" option, EFI_UNSUPPORTED\r
1015 will be returned.\r
1016 This function can take up to 10 seconds to timeout and return control to the caller.\r
1017 If the TFTP sequence does not complete, EFI_TIMEOUT will be returned.\r
1018 If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,\r
1019 then the TFTP sequence is stopped and EFI_ABORTED will be returned.\r
1020 The format of the data returned from a TFTP read directory operation is a null-terminated\r
1021 filename followed by a null-terminated information string, of the form\r
1022 "size year-month-day hour:minute:second" (i.e. %d %d-%d-%d %d:%d:%f - note that\r
1023 the seconds field can be a decimal number), where the date and time are UTC. For\r
1024 an MTFTP read directory command, there is additionally a null-terminated multicast\r
1025 IP address preceding the filename of the form %d.%d.%d.%d for IP v4. The final\r
1026 entry is itself null-terminated, so that the final information string is terminated\r
1027 with two null octets.\r
e2851998 1028\r
f737cfb9 1029 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
1030 @param Operation The type of operation to perform.\r
e2851998 1031 @param BufferPtr A pointer to the data buffer.\r
f737cfb9 1032 @param Overwrite Only used on write file operations. TRUE if a file on a remote server can\r
e2851998 1033 be overwritten.\r
f737cfb9 1034 @param BufferSize For get-file-size operations, *BufferSize returns the size of the\r
e2851998 1035 requested file.\r
f737cfb9 1036 @param BlockSize The requested block size to be used during a TFTP transfer.\r
1037 @param ServerIp The TFTP / MTFTP server IP address.\r
1038 @param Filename A Null-terminated ASCII string that specifies a directory name or a file\r
e2851998 1039 name.\r
f737cfb9 1040 @param Info Pointer to the MTFTP information.\r
e2851998 1041 @param DontUseBuffer Set to FALSE for normal TFTP and MTFTP read file operation.\r
1042\r
f737cfb9 1043 @retval EFI_SUCCESS The TFTP/MTFTP operation was completed.\r
1044 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.\r
e2851998 1045 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
1046 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.\r
1047 @retval EFI_BUFFER_TOO_SMALL The buffer is not large enough to complete the read operation.\r
f737cfb9 1048 @retval EFI_ABORTED The callback function aborted the TFTP/MTFTP operation.\r
1049 @retval EFI_TIMEOUT The TFTP/MTFTP operation timed out.\r
1050 @retval EFI_ICMP_ERROR An ICMP error packet was received during the MTFTP session.\r
1051 @retval EFI_TFTP_ERROR A TFTP error packet was received during the MTFTP session.\r
e2851998 1052\r
dc361cc5 1053**/\r
1054EFI_STATUS\r
1055EFIAPI\r
1056EfiPxeBcMtftp (\r
1057 IN EFI_PXE_BASE_CODE_PROTOCOL *This,\r
1058 IN EFI_PXE_BASE_CODE_TFTP_OPCODE Operation,\r
1059 IN OUT VOID *BufferPtr,\r
1060 IN BOOLEAN Overwrite,\r
1061 IN OUT UINT64 *BufferSize,\r
1062 IN UINTN *BlockSize OPTIONAL,\r
1063 IN EFI_IP_ADDRESS *ServerIp,\r
1064 IN UINT8 *Filename,\r
1065 IN EFI_PXE_BASE_CODE_MTFTP_INFO *Info OPTIONAL,\r
1066 IN BOOLEAN DontUseBuffer\r
1067 )\r
1068{\r
1069 PXEBC_PRIVATE_DATA *Private;\r
1070 EFI_MTFTP4_CONFIG_DATA Mtftp4Config;\r
1071 EFI_STATUS Status;\r
982a9eae 1072 EFI_PXE_BASE_CODE_MODE *Mode;\r
1073 EFI_MAC_ADDRESS TempMacAddr;\r
dc361cc5 1074\r
f6b7393c 1075 if ((This == NULL) ||\r
1076 (Filename == NULL) ||\r
1077 (BufferSize == NULL) ||\r
1078 ((ServerIp == NULL) || !NetIp4IsUnicast (NTOHL (ServerIp->Addr[0]), 0)) ||\r
1079 ((BufferPtr == NULL) && DontUseBuffer) ||\r
dc361cc5 1080 ((BlockSize != NULL) && (*BlockSize < 512))) {\r
1081\r
1082 return EFI_INVALID_PARAMETER;\r
1083 }\r
1084\r
1085 Status = EFI_DEVICE_ERROR;\r
1086 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
982a9eae 1087 Mode = &Private->Mode;\r
1088\r
1089 if (!Mode->AutoArp) {\r
1090 //\r
1091 // If AutoArp is set false, check arp cache\r
1092 //\r
1093 UpdateArpCache (This);\r
1094 if (!FindInArpCache (Mode, &ServerIp->v4, &TempMacAddr)) {\r
1095 return EFI_DEVICE_ERROR;\r
1096 }\r
1097 }\r
1098\r
1099 Mode->TftpErrorReceived = FALSE;\r
1100 Mode->IcmpErrorReceived = FALSE;\r
dc361cc5 1101\r
1102 Mtftp4Config.UseDefaultSetting = FALSE;\r
1103 Mtftp4Config.TimeoutValue = PXEBC_MTFTP_TIMEOUT;\r
1104 Mtftp4Config.TryCount = PXEBC_MTFTP_RETRIES;\r
1105\r
f737cfb9 1106 CopyMem (\r
e2851998 1107 &Mtftp4Config.StationIp,\r
1108 &Private->StationIp,\r
f737cfb9 1109 sizeof (EFI_IPv4_ADDRESS)\r
1110 );\r
1111 CopyMem (\r
e2851998 1112 &Mtftp4Config.SubnetMask,\r
1113 &Private->SubnetMask,\r
f737cfb9 1114 sizeof (EFI_IPv4_ADDRESS)\r
1115 );\r
1116 CopyMem (\r
e2851998 1117 &Mtftp4Config.GatewayIp,\r
1118 &Private->GatewayIp,\r
f737cfb9 1119 sizeof (EFI_IPv4_ADDRESS)\r
1120 );\r
1121 CopyMem (\r
e2851998 1122 &Mtftp4Config.ServerIp,\r
1123 ServerIp,\r
f737cfb9 1124 sizeof (EFI_IPv4_ADDRESS)\r
1125 );\r
dc361cc5 1126\r
1127 switch (Operation) {\r
1128\r
1129 case EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE:\r
1130\r
1131 Status = PxeBcTftpGetFileSize (\r
1132 Private,\r
1133 &Mtftp4Config,\r
1134 Filename,\r
1135 BlockSize,\r
1136 BufferSize\r
1137 );\r
1138\r
dc361cc5 1139 break;\r
1140\r
1141 case EFI_PXE_BASE_CODE_TFTP_READ_FILE:\r
1142\r
1143 Status = PxeBcTftpReadFile (\r
1144 Private,\r
1145 &Mtftp4Config,\r
1146 Filename,\r
1147 BlockSize,\r
1148 BufferPtr,\r
1149 BufferSize,\r
1150 DontUseBuffer\r
1151 );\r
1152\r
1153 break;\r
1154\r
1155 case EFI_PXE_BASE_CODE_TFTP_WRITE_FILE:\r
1156\r
1157 Status = PxeBcTftpWriteFile (\r
1158 Private,\r
1159 &Mtftp4Config,\r
1160 Filename,\r
1161 Overwrite,\r
1162 BlockSize,\r
1163 BufferPtr,\r
1164 BufferSize\r
1165 );\r
1166\r
1167 break;\r
1168\r
1169 case EFI_PXE_BASE_CODE_TFTP_READ_DIRECTORY:\r
1170\r
1171 Status = PxeBcTftpReadDirectory (\r
1172 Private,\r
1173 &Mtftp4Config,\r
1174 Filename,\r
1175 BlockSize,\r
1176 BufferPtr,\r
1177 BufferSize,\r
1178 DontUseBuffer\r
1179 );\r
1180\r
1181 break;\r
1182\r
1183 case EFI_PXE_BASE_CODE_MTFTP_GET_FILE_SIZE:\r
1184 case EFI_PXE_BASE_CODE_MTFTP_READ_FILE:\r
1185 case EFI_PXE_BASE_CODE_MTFTP_READ_DIRECTORY:\r
1186 Status = EFI_UNSUPPORTED;\r
1187 break;\r
1188\r
1189 default:\r
1190\r
1191 Status = EFI_INVALID_PARAMETER;\r
1192 break;\r
1193 }\r
1194\r
982a9eae 1195 if (Status == EFI_ICMP_ERROR) {\r
1196 Mode->IcmpErrorReceived = TRUE;\r
1197 }\r
1198\r
dc361cc5 1199 return Status;\r
1200}\r
1201\r
1202\r
e2851998 1203/**\r
f737cfb9 1204 Writes a UDP packet to the network interface.\r
1205\r
1206 This function writes a UDP packet specified by the (optional HeaderPtr and)\r
1207 BufferPtr parameters to the network interface. The UDP header is automatically\r
1208 built by this routine. It uses the parameters OpFlags, DestIp, DestPort, GatewayIp,\r
1209 SrcIp, and SrcPort to build this header. If the packet is successfully built and\r
1210 transmitted through the network interface, then EFI_SUCCESS will be returned.\r
1211 If a timeout occurs during the transmission of the packet, then EFI_TIMEOUT will\r
1212 be returned. If an ICMP error occurs during the transmission of the packet, then\r
1213 the IcmpErrorReceived field is set to TRUE, the IcmpError field is filled in and\r
1214 EFI_ICMP_ERROR will be returned. If the Callback Protocol does not return\r
1215 EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE, then EFI_ABORTED will be returned.\r
e2851998 1216\r
f737cfb9 1217 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
e2851998 1218 @param OpFlags The UDP operation flags.\r
f737cfb9 1219 @param DestIp The destination IP address.\r
e2851998 1220 @param DestPort The destination UDP port number.\r
1221 @param GatewayIp The gateway IP address.\r
f737cfb9 1222 @param SrcIp The source IP address.\r
1223 @param SrcPort The source UDP port number.\r
1224 @param HeaderSize An optional field which may be set to the length of a header at\r
e2851998 1225 HeaderPtr to be prefixed to the data at BufferPtr.\r
f737cfb9 1226 @param HeaderPtr If HeaderSize is not NULL, a pointer to a header to be prefixed to the\r
e2851998 1227 data at BufferPtr.\r
f737cfb9 1228 @param BufferSize A pointer to the size of the data at BufferPtr.\r
1229 @param BufferPtr A pointer to the data to be written.\r
e2851998 1230\r
f737cfb9 1231 @retval EFI_SUCCESS The UDP Write operation was completed.\r
1232 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.\r
e2851998 1233 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
1234 @retval EFI_BAD_BUFFER_SIZE The buffer is too long to be transmitted.\r
f737cfb9 1235 @retval EFI_ABORTED The callback function aborted the UDP Write operation.\r
1236 @retval EFI_TIMEOUT The UDP Write operation timed out.\r
e2851998 1237 @retval EFI_ICMP_ERROR An ICMP error packet was received during the UDP write session.\r
1238\r
dc361cc5 1239**/\r
1240EFI_STATUS\r
1241EFIAPI\r
1242EfiPxeBcUdpWrite (\r
1243 IN EFI_PXE_BASE_CODE_PROTOCOL *This,\r
1244 IN UINT16 OpFlags,\r
1245 IN EFI_IP_ADDRESS *DestIp,\r
1246 IN EFI_PXE_BASE_CODE_UDP_PORT *DestPort,\r
1247 IN EFI_IP_ADDRESS *GatewayIp OPTIONAL,\r
1248 IN EFI_IP_ADDRESS *SrcIp OPTIONAL,\r
1249 IN OUT EFI_PXE_BASE_CODE_UDP_PORT *SrcPort OPTIONAL,\r
1250 IN UINTN *HeaderSize OPTIONAL,\r
1251 IN VOID *HeaderPtr OPTIONAL,\r
1252 IN UINTN *BufferSize,\r
1253 IN VOID *BufferPtr\r
1254 )\r
1255{\r
1256 PXEBC_PRIVATE_DATA *Private;\r
1257 EFI_UDP4_PROTOCOL *Udp4;\r
1258 EFI_UDP4_COMPLETION_TOKEN Token;\r
1259 EFI_UDP4_TRANSMIT_DATA *Udp4TxData;\r
1260 UINT32 FragCount;\r
1261 UINT32 DataLength;\r
1262 EFI_UDP4_SESSION_DATA Udp4Session;\r
1263 EFI_STATUS Status;\r
1264 BOOLEAN IsDone;\r
982a9eae 1265 EFI_PXE_BASE_CODE_MODE *Mode;\r
1266 EFI_MAC_ADDRESS TempMacAddr;\r
dc361cc5 1267\r
1268 IsDone = FALSE;\r
1269\r
1270 if ((This == NULL) || (DestIp == NULL) || (DestPort == NULL)) {\r
1271 return EFI_INVALID_PARAMETER;\r
1272 }\r
1273\r
f6b7393c 1274 if ((GatewayIp != NULL) && !NetIp4IsUnicast (NTOHL (GatewayIp->Addr[0]), 0)) {\r
dc361cc5 1275 //\r
1276 // Gateway is provided but it's not a unicast IP address.\r
1277 //\r
1278 return EFI_INVALID_PARAMETER;\r
1279 }\r
1280\r
1281 if ((HeaderSize != NULL) && ((*HeaderSize == 0) || (HeaderPtr == NULL))) {\r
1282 //\r
1283 // The HeaderSize ptr isn't NULL and: 1. the value is zero; or 2. the HeaderPtr\r
1284 // is NULL.\r
1285 //\r
1286 return EFI_INVALID_PARAMETER;\r
1287 }\r
1288\r
1289 if ((BufferSize == NULL) || ((*BufferSize != 0) && (BufferPtr == NULL))) {\r
1290 return EFI_INVALID_PARAMETER;\r
1291 }\r
1292\r
1293 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
8792362f 1294 Udp4 = Private->Udp4Write;\r
982a9eae 1295 Mode = &Private->Mode;\r
8792362f 1296 if (!Mode->Started) {\r
1297 return EFI_NOT_STARTED;\r
1298 }\r
dc361cc5 1299\r
1300 if (!Private->AddressIsOk && (SrcIp == NULL)) {\r
1301 return EFI_INVALID_PARAMETER;\r
1302 }\r
1303\r
982a9eae 1304 if (!Mode->AutoArp) {\r
1305 //\r
1306 // If AutoArp is set false, check arp cache\r
1307 //\r
1308 UpdateArpCache (This);\r
1309 if (!FindInArpCache (Mode, &DestIp->v4, &TempMacAddr)) {\r
1310 return EFI_DEVICE_ERROR;\r
1311 }\r
1312 }\r
1313\r
1314 Mode->IcmpErrorReceived = FALSE;\r
1315\r
8792362f 1316 if ((Private->CurrentUdpSrcPort == 0) ||\r
f737cfb9 1317 ((SrcPort != NULL) && (*SrcPort != Private->CurrentUdpSrcPort))) {\r
8792362f 1318 //\r
1319 // Port is changed, (re)configure the Udp4Write instance\r
1320 //\r
1321 if (SrcPort != NULL) {\r
1322 Private->CurrentUdpSrcPort = *SrcPort;\r
dc361cc5 1323 }\r
dc361cc5 1324\r
8792362f 1325 Status = PxeBcConfigureUdpWriteInstance (\r
1326 Udp4,\r
1327 &Private->StationIp.v4,\r
1328 &Private->SubnetMask.v4,\r
1329 &Private->GatewayIp.v4,\r
1330 &Private->CurrentUdpSrcPort\r
1331 );\r
1332 if (EFI_ERROR (Status)) {\r
1333 Private->CurrentUdpSrcPort = 0;\r
1334 return EFI_INVALID_PARAMETER;\r
dc361cc5 1335 }\r
1336 }\r
1337\r
1338 ZeroMem (&Token, sizeof (EFI_UDP4_COMPLETION_TOKEN));\r
1339 ZeroMem (&Udp4Session, sizeof (EFI_UDP4_SESSION_DATA));\r
1340\r
e48e37fc 1341 CopyMem (&Udp4Session.DestinationAddress, DestIp, sizeof (EFI_IPv4_ADDRESS));\r
dc361cc5 1342 Udp4Session.DestinationPort = *DestPort;\r
8792362f 1343 if (SrcIp != NULL) {\r
1344 CopyMem (&Udp4Session.SourceAddress, SrcIp, sizeof (EFI_IPv4_ADDRESS));\r
1345 }\r
1346 if (SrcPort != NULL) {\r
1347 Udp4Session.SourcePort = *SrcPort;\r
1348 }\r
dc361cc5 1349\r
1350 FragCount = (HeaderSize != NULL) ? 2 : 1;\r
05c0e3cb 1351 Udp4TxData = (EFI_UDP4_TRANSMIT_DATA *) AllocateZeroPool (sizeof (EFI_UDP4_TRANSMIT_DATA) + (FragCount - 1) * sizeof (EFI_UDP4_FRAGMENT_DATA));\r
dc361cc5 1352 if (Udp4TxData == NULL) {\r
1353 return EFI_OUT_OF_RESOURCES;\r
1354 }\r
1355\r
1356 Udp4TxData->FragmentCount = FragCount;\r
1357 Udp4TxData->FragmentTable[FragCount - 1].FragmentLength = (UINT32) *BufferSize;\r
1358 Udp4TxData->FragmentTable[FragCount - 1].FragmentBuffer = BufferPtr;\r
1359 DataLength = (UINT32) *BufferSize;\r
1360\r
1361 if (FragCount == 2) {\r
1362\r
1363 Udp4TxData->FragmentTable[0].FragmentLength = (UINT32) *HeaderSize;\r
1364 Udp4TxData->FragmentTable[0].FragmentBuffer = HeaderPtr;\r
1365 DataLength += (UINT32) *HeaderSize;\r
1366 }\r
1367\r
8792362f 1368 if (GatewayIp != NULL) {\r
1369 Udp4TxData->GatewayAddress = (EFI_IPv4_ADDRESS *) GatewayIp;\r
1370 }\r
dc361cc5 1371 Udp4TxData->UdpSessionData = &Udp4Session;\r
1372 Udp4TxData->DataLength = DataLength;\r
1373 Token.Packet.TxData = Udp4TxData;\r
1374\r
1375 Status = gBS->CreateEvent (\r
1376 EVT_NOTIFY_SIGNAL,\r
e48e37fc 1377 TPL_NOTIFY,\r
dc361cc5 1378 PxeBcCommonNotify,\r
1379 &IsDone,\r
1380 &Token.Event\r
1381 );\r
1382 if (EFI_ERROR (Status)) {\r
1383 goto ON_EXIT;\r
1384 }\r
1385\r
1386 Status = Udp4->Transmit (Udp4, &Token);\r
1387 if (EFI_ERROR (Status)) {\r
982a9eae 1388 if (Status == EFI_ICMP_ERROR) {\r
1389 Mode->IcmpErrorReceived = TRUE;\r
1390 }\r
dc361cc5 1391 goto ON_EXIT;\r
1392 }\r
1393\r
1394 while (!IsDone) {\r
1395\r
1396 Udp4->Poll (Udp4);\r
1397 }\r
1398\r
1399 Status = Token.Status;\r
1400\r
1401ON_EXIT:\r
1402\r
1403 if (Token.Event != NULL) {\r
1404 gBS->CloseEvent (Token.Event);\r
1405 }\r
1406\r
766c7483 1407 FreePool (Udp4TxData);\r
dc361cc5 1408\r
1409 return Status;\r
1410}\r
1411\r
8d285ec0 1412/**\r
f737cfb9 1413 Decide whether the incoming UDP packet is acceptable per IP filter settings\r
1414 in provided PxeBcMode.\r
8d285ec0 1415\r
f737cfb9 1416 @param PxeBcMode Pointer to EFI_PXE_BASE_CODE_MODE.\r
1417 @param Session Received UDP session.\r
8d285ec0 1418\r
f737cfb9 1419 @retval TRUE The UDP package matches IP filters.\r
1420 @retval FALSE The UDP package doesn't matches IP filters.\r
8d285ec0 1421\r
1422**/\r
8d285ec0 1423BOOLEAN\r
1424CheckIpByFilter (\r
f737cfb9 1425 IN EFI_PXE_BASE_CODE_MODE *PxeBcMode,\r
1426 IN EFI_UDP4_SESSION_DATA *Session\r
8d285ec0 1427 )\r
1428{\r
1429 UINTN Index;\r
1430 EFI_IPv4_ADDRESS Ip4Address;\r
1431 EFI_IPv4_ADDRESS DestIp4Address;\r
1432\r
f737cfb9 1433 if ((PxeBcMode->IpFilter.Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS) != 0) {\r
8d285ec0 1434 return TRUE;\r
1435 }\r
1436\r
1437 CopyMem (&DestIp4Address, &Session->DestinationAddress, sizeof (DestIp4Address));\r
e2851998 1438 if (((PxeBcMode->IpFilter.Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS_MULTICAST) != 0) &&\r
982a9eae 1439 IP4_IS_MULTICAST (EFI_NTOHL (DestIp4Address))\r
8d285ec0 1440 ) {\r
1441 return TRUE;\r
1442 }\r
1443\r
e2851998 1444 if (((PxeBcMode->IpFilter.Filters & EFI_PXE_BASE_CODE_IP_FILTER_BROADCAST) != 0) &&\r
8d285ec0 1445 IP4_IS_LOCAL_BROADCAST (EFI_NTOHL (DestIp4Address))\r
1446 ) {\r
1447 return TRUE;\r
1448 }\r
1449\r
1450 CopyMem (&Ip4Address, &PxeBcMode->StationIp.v4, sizeof (Ip4Address));\r
e2851998 1451 if (((PxeBcMode->IpFilter.Filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP) != 0) &&\r
982a9eae 1452 EFI_IP4_EQUAL (&Ip4Address, &DestIp4Address)\r
8d285ec0 1453 ) {\r
1454 return TRUE;\r
1455 }\r
1456\r
894d038a 1457 ASSERT (PxeBcMode->IpFilter.IpCnt < EFI_PXE_BASE_CODE_MAX_IPCNT);\r
1458\r
f737cfb9 1459 for (Index = 0; Index < PxeBcMode->IpFilter.IpCnt; Index++) {\r
1460 CopyMem (\r
e2851998 1461 &Ip4Address,\r
1462 &PxeBcMode->IpFilter.IpList[Index].v4,\r
f737cfb9 1463 sizeof (Ip4Address)\r
1464 );\r
8d285ec0 1465 if (EFI_IP4_EQUAL (&Ip4Address, &DestIp4Address)) {\r
1466 return TRUE;\r
1467 }\r
1468 }\r
1469\r
1470 return FALSE;\r
1471}\r
dc361cc5 1472\r
e2851998 1473/**\r
f737cfb9 1474 Reads a UDP packet from the network interface.\r
1475\r
1476 This function reads a UDP packet from a network interface. The data contents\r
1477 are returned in (the optional HeaderPtr and) BufferPtr, and the size of the\r
1478 buffer received is returned in BufferSize . If the input BufferSize is smaller\r
1479 than the UDP packet received (less optional HeaderSize), it will be set to the\r
1480 required size, and EFI_BUFFER_TOO_SMALL will be returned. In this case, the\r
1481 contents of BufferPtr are undefined, and the packet is lost. If a UDP packet is\r
1482 successfully received, then EFI_SUCCESS will be returned, and the information\r
1483 from the UDP header will be returned in DestIp, DestPort, SrcIp, and SrcPort if\r
e2851998 1484 they are not NULL. Depending on the values of OpFlags and the DestIp, DestPort,\r
1485 SrcIp, and SrcPort input values, different types of UDP packet receive filtering\r
f737cfb9 1486 will be performed. The following tables summarize these receive filter operations.\r
e2851998 1487\r
f737cfb9 1488 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
e2851998 1489 @param OpFlags The UDP operation flags.\r
f737cfb9 1490 @param DestIp The destination IP address.\r
1491 @param DestPort The destination UDP port number.\r
1492 @param SrcIp The source IP address.\r
1493 @param SrcPort The source UDP port number.\r
1494 @param HeaderSize An optional field which may be set to the length of a header at\r
e2851998 1495 HeaderPtr to be prefixed to the data at BufferPtr.\r
f737cfb9 1496 @param HeaderPtr If HeaderSize is not NULL, a pointer to a header to be prefixed to the\r
e2851998 1497 data at BufferPtr.\r
f737cfb9 1498 @param BufferSize A pointer to the size of the data at BufferPtr.\r
1499 @param BufferPtr A pointer to the data to be read.\r
e2851998 1500\r
f737cfb9 1501 @retval EFI_SUCCESS The UDP Read operation was completed.\r
1502 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.\r
e2851998 1503 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
f737cfb9 1504 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.\r
1505 @retval EFI_BUFFER_TOO_SMALL The packet is larger than Buffer can hold.\r
1506 @retval EFI_ABORTED The callback function aborted the UDP Read operation.\r
e2851998 1507 @retval EFI_TIMEOUT The UDP Read operation timed out.\r
1508\r
dc361cc5 1509**/\r
1510EFI_STATUS\r
1511EFIAPI\r
1512EfiPxeBcUdpRead (\r
f737cfb9 1513 IN EFI_PXE_BASE_CODE_PROTOCOL *This,\r
1514 IN UINT16 OpFlags,\r
1515 IN OUT EFI_IP_ADDRESS *DestIp OPTIONAL,\r
1516 IN OUT EFI_PXE_BASE_CODE_UDP_PORT *DestPort OPTIONAL,\r
1517 IN OUT EFI_IP_ADDRESS *SrcIp OPTIONAL,\r
1518 IN OUT EFI_PXE_BASE_CODE_UDP_PORT *SrcPort OPTIONAL,\r
1519 IN UINTN *HeaderSize OPTIONAL,\r
1520 IN VOID *HeaderPtr OPTIONAL,\r
1521 IN OUT UINTN *BufferSize,\r
1522 IN VOID *BufferPtr\r
dc361cc5 1523 )\r
1524{\r
1525 PXEBC_PRIVATE_DATA *Private;\r
1526 EFI_PXE_BASE_CODE_MODE *Mode;\r
1527 EFI_UDP4_PROTOCOL *Udp4;\r
1528 EFI_UDP4_COMPLETION_TOKEN Token;\r
1529 EFI_UDP4_RECEIVE_DATA *RxData;\r
1530 EFI_UDP4_SESSION_DATA *Session;\r
1531 EFI_STATUS Status;\r
1532 BOOLEAN IsDone;\r
1533 BOOLEAN Matched;\r
1534 UINTN CopyLen;\r
1535\r
1536 if (This == NULL || DestIp == NULL || DestPort == NULL) {\r
1537 return EFI_INVALID_PARAMETER;\r
1538 }\r
1539\r
f737cfb9 1540 if (((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT) == 0 && (DestPort == NULL)) ||\r
1541 ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT) == 0 && (SrcIp == NULL)) ||\r
1542 ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT) == 0 && (SrcPort == NULL))) {\r
dc361cc5 1543 return EFI_INVALID_PARAMETER;\r
1544 }\r
1545\r
8792362f 1546 if (((HeaderSize != NULL) && (*HeaderSize == 0)) || ((HeaderSize != NULL) && (HeaderPtr == NULL))) {\r
dc361cc5 1547 return EFI_INVALID_PARAMETER;\r
1548 }\r
1549\r
29a4f92d 1550 if ((BufferSize == NULL) || (BufferPtr == NULL)) {\r
dc361cc5 1551 return EFI_INVALID_PARAMETER;\r
1552 }\r
1553\r
1554 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
1555 Mode = Private->PxeBc.Mode;\r
8792362f 1556 Udp4 = Private->Udp4Read;\r
dc361cc5 1557\r
1558 if (!Mode->Started) {\r
1559 return EFI_NOT_STARTED;\r
1560 }\r
1561\r
982a9eae 1562 Mode->IcmpErrorReceived = FALSE;\r
1563\r
dc361cc5 1564 Status = gBS->CreateEvent (\r
1565 EVT_NOTIFY_SIGNAL,\r
e48e37fc 1566 TPL_NOTIFY,\r
dc361cc5 1567 PxeBcCommonNotify,\r
1568 &IsDone,\r
1569 &Token.Event\r
1570 );\r
1571 if (EFI_ERROR (Status)) {\r
1572 return EFI_OUT_OF_RESOURCES;\r
1573 }\r
1574\r
8792362f 1575TRY_AGAIN:\r
1576\r
dc361cc5 1577 IsDone = FALSE;\r
1578 Status = Udp4->Receive (Udp4, &Token);\r
1579 if (EFI_ERROR (Status)) {\r
982a9eae 1580 if (Status == EFI_ICMP_ERROR) {\r
1581 Mode->IcmpErrorReceived = TRUE;\r
1582 }\r
dc361cc5 1583 goto ON_EXIT;\r
1584 }\r
1585\r
1586 Udp4->Poll (Udp4);\r
1587\r
1588 if (!IsDone) {\r
1589 Status = EFI_TIMEOUT;\r
1590 } else {\r
1591\r
1592 //\r
1593 // check whether this packet matches the filters\r
1594 //\r
1595 if (EFI_ERROR (Token.Status)){\r
1596 goto ON_EXIT;\r
1597 }\r
1598\r
1599 RxData = Token.Packet.RxData;\r
1600 Session = &RxData->UdpSession;\r
1601\r
319075ff 1602 Matched = TRUE;\r
dc361cc5 1603\r
f737cfb9 1604 if ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_USE_FILTER) != 0) {\r
319075ff 1605 Matched = FALSE;\r
8d285ec0 1606 //\r
1607 // Check UDP package by IP filter settings\r
1608 //\r
1609 if (CheckIpByFilter (Mode, Session)) {\r
1610 Matched = TRUE;\r
dc361cc5 1611 }\r
8d285ec0 1612 }\r
1613\r
1614 if (Matched) {\r
8792362f 1615 Matched = FALSE;\r
1616\r
8d285ec0 1617 //\r
1618 // Match the destination ip of the received udp dgram\r
1619 //\r
f737cfb9 1620 if ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_IP) != 0) {\r
8d285ec0 1621 Matched = TRUE;\r
1622\r
1623 if (DestIp != NULL) {\r
1624 CopyMem (DestIp, &Session->DestinationAddress, sizeof (EFI_IPv4_ADDRESS));\r
dc361cc5 1625 }\r
1626 } else {\r
8d285ec0 1627 if (DestIp != NULL) {\r
1628 if (EFI_IP4_EQUAL (DestIp, &Session->DestinationAddress)) {\r
1629 Matched = TRUE;\r
1630 }\r
1631 } else {\r
1632 if (EFI_IP4_EQUAL (&Private->StationIp, &Session->DestinationAddress)) {\r
1633 Matched = TRUE;\r
1634 }\r
dc361cc5 1635 }\r
1636 }\r
1637 }\r
1638\r
1639 if (Matched) {\r
1640 //\r
1641 // Match the destination port of the received udp dgram\r
1642 //\r
f737cfb9 1643 if ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT) != 0) {\r
dc361cc5 1644\r
1645 if (DestPort != NULL) {\r
1646 *DestPort = Session->DestinationPort;\r
1647 }\r
1648 } else {\r
1649\r
1650 if (*DestPort != Session->DestinationPort) {\r
1651 Matched = FALSE;\r
1652 }\r
1653 }\r
1654 }\r
1655\r
1656 if (Matched) {\r
1657 //\r
1658 // Match the source ip of the received udp dgram\r
1659 //\r
f737cfb9 1660 if ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP) != 0) {\r
dc361cc5 1661\r
1662 if (SrcIp != NULL) {\r
e48e37fc 1663 CopyMem (SrcIp, &Session->SourceAddress, sizeof (EFI_IPv4_ADDRESS));\r
dc361cc5 1664 }\r
1665 } else {\r
1666\r
1667 if (!EFI_IP4_EQUAL (SrcIp, &Session->SourceAddress)) {\r
1668 Matched = FALSE;\r
1669 }\r
1670 }\r
1671 }\r
1672\r
1673 if (Matched) {\r
1674 //\r
1675 // Match the source port of the received udp dgram\r
1676 //\r
f737cfb9 1677 if ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT) != 0) {\r
dc361cc5 1678\r
1679 if (SrcPort != NULL) {\r
1680 *SrcPort = Session->SourcePort;\r
1681 }\r
1682 } else {\r
1683\r
1684 if (*SrcPort != Session->SourcePort) {\r
1685 Matched = FALSE;\r
1686 }\r
1687 }\r
1688 }\r
1689\r
1690 if (Matched) {\r
1691\r
1692 CopyLen = 0;\r
1693\r
1694 if (HeaderSize != NULL) {\r
1695 CopyLen = MIN (*HeaderSize, RxData->DataLength);\r
e48e37fc 1696 CopyMem (HeaderPtr, RxData->FragmentTable[0].FragmentBuffer, CopyLen);\r
dc361cc5 1697 *HeaderSize = CopyLen;\r
1698 }\r
1699\r
1700 if (RxData->DataLength - CopyLen > *BufferSize) {\r
1701\r
1702 Status = EFI_BUFFER_TOO_SMALL;\r
1703 } else {\r
1704\r
1705 *BufferSize = RxData->DataLength - CopyLen;\r
f737cfb9 1706 CopyMem (\r
e2851998 1707 BufferPtr,\r
1708 (UINT8 *) RxData->FragmentTable[0].FragmentBuffer + CopyLen,\r
f737cfb9 1709 *BufferSize\r
1710 );\r
dc361cc5 1711 }\r
1712 } else {\r
1713\r
1714 Status = EFI_TIMEOUT;\r
1715 }\r
1716\r
1717 //\r
1718 // Recycle the RxData\r
1719 //\r
1720 gBS->SignalEvent (RxData->RecycleSignal);\r
8792362f 1721\r
1722 if (!Matched) {\r
1723 goto TRY_AGAIN;\r
1724 }\r
dc361cc5 1725 }\r
1726\r
1727ON_EXIT:\r
1728\r
1729 Udp4->Cancel (Udp4, &Token);\r
1730\r
1731 gBS->CloseEvent (Token.Event);\r
1732\r
1733 return Status;\r
1734}\r
1735\r
e2851998 1736/**\r
f737cfb9 1737 Updates the IP receive filters of a network device and enables software filtering.\r
e2851998 1738\r
f737cfb9 1739 The NewFilter field is used to modify the network device's current IP receive\r
1740 filter settings and to enable a software filter. This function updates the IpFilter\r
1741 field of the EFI_PXE_BASE_CODE_MODE structure with the contents of NewIpFilter.\r
1742 The software filter is used when the USE_FILTER in OpFlags is set to UdpRead().\r
1743 The current hardware filter remains in effect no matter what the settings of OpFlags\r
1744 are, so that the meaning of ANY_DEST_IP set in OpFlags to UdpRead() is from those\r
1745 packets whose reception is enabled in hardware-physical NIC address (unicast),\r
1746 broadcast address, logical address or addresses (multicast), or all (promiscuous).\r
1747 UdpRead() does not modify the IP filter settings.\r
1748 Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP receive\r
1749 filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.\r
1750 If an application or driver wishes to preserve the IP receive filter settings,\r
1751 it will have to preserve the IP receive filter settings before these calls, and\r
1752 use SetIpFilter() to restore them after the calls. If incompatible filtering is\r
1753 requested (for example, PROMISCUOUS with anything else) or if the device does not\r
1754 support a requested filter setting and it cannot be accommodated in software\r
1755 (for example, PROMISCUOUS not supported), EFI_INVALID_PARAMETER will be returned.\r
1756 The IPlist field is used to enable IPs other than the StationIP. They may be\r
1757 multicast or unicast. If IPcnt is set as well as EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP,\r
1758 then both the StationIP and the IPs from the IPlist will be used.\r
e2851998 1759\r
f737cfb9 1760 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
1761 @param NewFilter Pointer to the new set of IP receive filters.\r
e2851998 1762\r
f737cfb9 1763 @retval EFI_SUCCESS The IP receive filter settings were updated.\r
1764 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.\r
e2851998 1765 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
1766\r
dc361cc5 1767**/\r
1768EFI_STATUS\r
1769EFIAPI\r
1770EfiPxeBcSetIpFilter (\r
1771 IN EFI_PXE_BASE_CODE_PROTOCOL *This,\r
1772 IN EFI_PXE_BASE_CODE_IP_FILTER *NewFilter\r
1773 )\r
1774{\r
8d285ec0 1775 EFI_STATUS Status;\r
1776 PXEBC_PRIVATE_DATA *Private;\r
1777 EFI_PXE_BASE_CODE_MODE *Mode;\r
1778 UINTN Index;\r
1779 BOOLEAN PromiscuousNeed;\r
1780\r
1781 if (This == NULL) {\r
894d038a 1782 DEBUG ((EFI_D_ERROR, "This == NULL.\n"));\r
8d285ec0 1783 return EFI_INVALID_PARAMETER;\r
1784 }\r
1785\r
1786 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
1787 Mode = Private->PxeBc.Mode;\r
1788\r
894d038a 1789 if (NewFilter == NULL) {\r
1790 DEBUG ((EFI_D_ERROR, "NewFilter == NULL.\n"));\r
8d285ec0 1791 return EFI_INVALID_PARAMETER;\r
1792 }\r
1793\r
894d038a 1794 if (NewFilter->IpCnt > EFI_PXE_BASE_CODE_MAX_IPCNT) {\r
1795 DEBUG ((EFI_D_ERROR, "NewFilter->IpCnt > %d.\n", EFI_PXE_BASE_CODE_MAX_IPCNT));\r
8d285ec0 1796 return EFI_INVALID_PARAMETER;\r
1797 }\r
1798\r
1799 if (!Mode->Started) {\r
1800 DEBUG ((EFI_D_ERROR, "BC was not started.\n"));\r
1801 return EFI_NOT_STARTED;\r
1802 }\r
1803\r
1804 PromiscuousNeed = FALSE;\r
894d038a 1805\r
8d285ec0 1806 for (Index = 0; Index < NewFilter->IpCnt; ++Index) {\r
1807 if (IP4_IS_LOCAL_BROADCAST (EFI_IP4 (NewFilter->IpList[Index].v4))) {\r
1808 //\r
1809 // The IP is a broadcast address.\r
1810 //\r
1811 DEBUG ((EFI_D_ERROR, "There is broadcast address in NewFilter.\n"));\r
1812 return EFI_INVALID_PARAMETER;\r
1813 }\r
f6b7393c 1814 if (NetIp4IsUnicast (EFI_IP4 (NewFilter->IpList[Index].v4), 0) &&\r
e2851998 1815 ((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP) != 0)\r
8d285ec0 1816 ) {\r
1817 //\r
1818 // If EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP is set and IP4 address is in IpList,\r
1819 // promiscuous mode is needed.\r
1820 //\r
1821 PromiscuousNeed = TRUE;\r
1822 }\r
1823 }\r
1824\r
1825 //\r
1826 // Clear the UDP instance configuration, all joined groups will be left\r
1827 // during the operation.\r
1828 //\r
8792362f 1829 Private->Udp4Read->Configure (Private->Udp4Read, NULL);\r
8d285ec0 1830 Private->Udp4CfgData.AcceptPromiscuous = FALSE;\r
1831 Private->Udp4CfgData.AcceptBroadcast = FALSE;\r
1832\r
1833 if (PromiscuousNeed ||\r
e2851998 1834 ((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS) != 0) ||\r
1835 ((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS_MULTICAST) != 0)\r
8d285ec0 1836 ) {\r
1837 //\r
1838 // Configure the udp4 filter to receive all packages\r
1839 //\r
1840 Private->Udp4CfgData.AcceptPromiscuous = TRUE;\r
1841\r
1842 //\r
1843 // Configure the UDP instance with the new configuration.\r
1844 //\r
8792362f 1845 Status = Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);\r
8d285ec0 1846 if (EFI_ERROR (Status)) {\r
1847 return Status;\r
1848 }\r
1849\r
1850 } else {\r
1851\r
e2851998 1852 if ((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_BROADCAST) != 0) {\r
8d285ec0 1853 //\r
1854 // Configure the udp4 filter to receive all broadcast packages\r
1855 //\r
1856 Private->Udp4CfgData.AcceptBroadcast = TRUE;\r
1857 }\r
1858\r
1859 //\r
1860 // Configure the UDP instance with the new configuration.\r
1861 //\r
8792362f 1862 Status = Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);\r
8d285ec0 1863 if (EFI_ERROR (Status)) {\r
1864 return Status;\r
1865 }\r
1866\r
e2851998 1867 if ((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP) != 0) {\r
8d285ec0 1868\r
1869 for (Index = 0; Index < NewFilter->IpCnt; ++Index) {\r
1870 if (IP4_IS_MULTICAST (EFI_NTOHL (NewFilter->IpList[Index].v4))) {\r
1871 //\r
1872 // Join the mutilcast group\r
1873 //\r
8792362f 1874 Status = Private->Udp4Read->Groups (Private->Udp4Read, TRUE, &NewFilter->IpList[Index].v4);\r
8d285ec0 1875 if (EFI_ERROR (Status)) {\r
1876 return Status;\r
1877 }\r
1878 }\r
1879 }\r
1880 }\r
1881 }\r
1882\r
1883\r
1884 //\r
1885 // Save the new filter.\r
1886 //\r
1887 CopyMem (&Mode->IpFilter, NewFilter, sizeof (Mode->IpFilter));\r
1888\r
1889 return EFI_SUCCESS;\r
dc361cc5 1890}\r
1891\r
1892\r
e2851998 1893/**\r
f737cfb9 1894 Uses the ARP protocol to resolve a MAC address.\r
e2851998 1895\r
f737cfb9 1896 This function uses the ARP protocol to resolve a MAC address. The UsingIpv6 field\r
1897 of the EFI_PXE_BASE_CODE_MODE structure is used to determine if IPv4 or IPv6\r
1898 addresses are being used. The IP address specified by IpAddr is used to resolve\r
1899 a MAC address. If the ARP protocol succeeds in resolving the specified address,\r
1900 then the ArpCacheEntries and ArpCache fields of the EFI_PXE_BASE_CODE_MODE structure\r
1901 are updated, and EFI_SUCCESS is returned. If MacAddr is not NULL, the resolved\r
e2851998 1902 MAC address is placed there as well. If the PXE Base Code protocol is in the\r
1903 stopped state, then EFI_NOT_STARTED is returned. If the ARP protocol encounters\r
1904 a timeout condition while attempting to resolve an address, then EFI_TIMEOUT is\r
1905 returned. If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,\r
f737cfb9 1906 then EFI_ABORTED is returned.\r
e2851998 1907\r
f737cfb9 1908 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
1909 @param IpAddr Pointer to the IP address that is used to resolve a MAC address.\r
1910 @param MacAddr If not NULL, a pointer to the MAC address that was resolved with the\r
e2851998 1911 ARP protocol.\r
1912\r
f737cfb9 1913 @retval EFI_SUCCESS The IP or MAC address was resolved.\r
1914 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.\r
e2851998 1915 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
1916 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.\r
1917 @retval EFI_ICMP_ERROR Something error occur with the ICMP packet message.\r
1918\r
dc361cc5 1919**/\r
1920EFI_STATUS\r
1921EFIAPI\r
1922EfiPxeBcArp (\r
1923 IN EFI_PXE_BASE_CODE_PROTOCOL * This,\r
1924 IN EFI_IP_ADDRESS * IpAddr,\r
1925 IN EFI_MAC_ADDRESS * MacAddr OPTIONAL\r
1926 )\r
1927{\r
8d285ec0 1928 PXEBC_PRIVATE_DATA *Private;\r
1929 EFI_PXE_BASE_CODE_MODE *Mode;\r
1930 EFI_STATUS Status;\r
1931 EFI_MAC_ADDRESS TempMacAddr;\r
1932\r
1933 if (This == NULL || IpAddr == NULL) {\r
1934 return EFI_INVALID_PARAMETER;\r
1935 }\r
1936\r
1937 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
1938 Mode = Private->PxeBc.Mode;\r
1939\r
1940 if (!Mode->Started) {\r
1941 return EFI_NOT_STARTED;\r
1942 }\r
1943\r
1944 if (!Private->AddressIsOk || Mode->UsingIpv6) {\r
1945 //\r
1946 // We can't resolve the IP address if we don't have a local address now.\r
1947 // Don't have ARP for IPv6.\r
1948 //\r
1949 return EFI_INVALID_PARAMETER;\r
1950 }\r
1951\r
982a9eae 1952 Mode->IcmpErrorReceived = FALSE;\r
1953\r
1954 if (!Mode->AutoArp) {\r
1955 //\r
1956 // If AutoArp is set false, check arp cache\r
1957 //\r
1958 UpdateArpCache (This);\r
1959 if (!FindInArpCache (Mode, &IpAddr->v4, &TempMacAddr)) {\r
1960 return EFI_DEVICE_ERROR;\r
1961 }\r
1962 } else {\r
1963 Status = Private->Arp->Request (Private->Arp, &IpAddr->v4, NULL, &TempMacAddr);\r
1964 if (EFI_ERROR (Status)) {\r
1965 if (Status == EFI_ICMP_ERROR) {\r
1966 Mode->IcmpErrorReceived = TRUE;\r
1967 }\r
1968 return Status;\r
1969 }\r
8d285ec0 1970 }\r
1971\r
1972 if (MacAddr != NULL) {\r
1973 CopyMem (MacAddr, &TempMacAddr, sizeof (EFI_MAC_ADDRESS));\r
1974 }\r
1975\r
1976 return EFI_SUCCESS;\r
dc361cc5 1977}\r
1978\r
e2851998 1979/**\r
f737cfb9 1980 Updates the parameters that affect the operation of the PXE Base Code Protocol.\r
e2851998 1981\r
f737cfb9 1982 This function sets parameters that affect the operation of the PXE Base Code Protocol.\r
1983 The parameter specified by NewAutoArp is used to control the generation of ARP\r
1984 protocol packets. If NewAutoArp is TRUE, then ARP Protocol packets will be generated\r
1985 as required by the PXE Base Code Protocol. If NewAutoArp is FALSE, then no ARP\r
1986 Protocol packets will be generated. In this case, the only mappings that are\r
1987 available are those stored in the ArpCache of the EFI_PXE_BASE_CODE_MODE structure.\r
1988 If there are not enough mappings in the ArpCache to perform a PXE Base Code Protocol\r
1989 service, then the service will fail. This function updates the AutoArp field of\r
1990 the EFI_PXE_BASE_CODE_MODE structure to NewAutoArp.\r
1991 The SetParameters() call must be invoked after a Callback Protocol is installed\r
1992 to enable the use of callbacks.\r
e2851998 1993\r
f737cfb9 1994 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
1995 @param NewAutoArp If not NULL, a pointer to a value that specifies whether to replace the\r
e2851998 1996 current value of AutoARP.\r
f737cfb9 1997 @param NewSendGUID If not NULL, a pointer to a value that specifies whether to replace the\r
e2851998 1998 current value of SendGUID.\r
f737cfb9 1999 @param NewTTL If not NULL, a pointer to be used in place of the current value of TTL,\r
e2851998 2000 the "time to live" field of the IP header.\r
f737cfb9 2001 @param NewToS If not NULL, a pointer to be used in place of the current value of ToS,\r
e2851998 2002 the "type of service" field of the IP header.\r
f737cfb9 2003 @param NewMakeCallback If not NULL, a pointer to a value that specifies whether to replace the\r
e2851998 2004 current value of the MakeCallback field of the Mode structure.\r
2005\r
f737cfb9 2006 @retval EFI_SUCCESS The new parameters values were updated.\r
2007 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.\r
e2851998 2008 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
2009\r
dc361cc5 2010**/\r
2011EFI_STATUS\r
2012EFIAPI\r
2013EfiPxeBcSetParameters (\r
2014 IN EFI_PXE_BASE_CODE_PROTOCOL *This,\r
f737cfb9 2015 IN BOOLEAN *NewAutoArp OPTIONAL,\r
2016 IN BOOLEAN *NewSendGUID OPTIONAL,\r
2017 IN UINT8 *NewTTL OPTIONAL,\r
2018 IN UINT8 *NewToS OPTIONAL,\r
dc361cc5 2019 IN BOOLEAN *NewMakeCallback // OPTIONAL\r
2020 )\r
2021{\r
2022 PXEBC_PRIVATE_DATA *Private;\r
2023 EFI_PXE_BASE_CODE_MODE *Mode;\r
2024 EFI_STATUS Status;\r
2025\r
2026 Status = EFI_SUCCESS;\r
2027\r
2028 if (This == NULL) {\r
2029 Status = EFI_INVALID_PARAMETER;\r
2030 goto ON_EXIT;\r
2031 }\r
2032\r
2033 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
2034 Mode = Private->PxeBc.Mode;\r
2035\r
f737cfb9 2036 if (NewSendGUID != NULL && *NewSendGUID) {\r
dc361cc5 2037 //\r
2038 // FixMe, cann't locate SendGuid\r
2039 //\r
2040 }\r
2041\r
f737cfb9 2042 if (NewMakeCallback != NULL && *NewMakeCallback) {\r
dc361cc5 2043\r
2044 Status = gBS->HandleProtocol (\r
2045 Private->Controller,\r
2046 &gEfiPxeBaseCodeCallbackProtocolGuid,\r
2047 (VOID **) &Private->PxeBcCallback\r
2048 );\r
2049 if (EFI_ERROR (Status) || (Private->PxeBcCallback->Callback == NULL)) {\r
2050\r
2051 Status = EFI_INVALID_PARAMETER;\r
2052 goto ON_EXIT;\r
2053 }\r
2054 }\r
2055\r
2056 if (!Mode->Started) {\r
2057 Status = EFI_NOT_STARTED;\r
2058 goto ON_EXIT;\r
2059 }\r
2060\r
2061 if (NewMakeCallback != NULL) {\r
2062\r
2063 if (*NewMakeCallback) {\r
2064 //\r
2065 // Update the Callback protocol.\r
2066 //\r
2067 Status = gBS->HandleProtocol (\r
2068 Private->Controller,\r
2069 &gEfiPxeBaseCodeCallbackProtocolGuid,\r
2070 (VOID **) &Private->PxeBcCallback\r
2071 );\r
2072\r
2073 if (EFI_ERROR (Status) || (Private->PxeBcCallback->Callback == NULL)) {\r
2074 Status = EFI_INVALID_PARAMETER;\r
2075 goto ON_EXIT;\r
2076 }\r
2077 } else {\r
2078 Private->PxeBcCallback = NULL;\r
2079 }\r
2080\r
2081 Mode->MakeCallbacks = *NewMakeCallback;\r
2082 }\r
2083\r
2084 if (NewAutoArp != NULL) {\r
2085 Mode->AutoArp = *NewAutoArp;\r
2086 }\r
2087\r
2088 if (NewSendGUID != NULL) {\r
2089 Mode->SendGUID = *NewSendGUID;\r
2090 }\r
2091\r
2092 if (NewTTL != NULL) {\r
2093 Mode->TTL = *NewTTL;\r
2094 }\r
2095\r
2096 if (NewToS != NULL) {\r
2097 Mode->ToS = *NewToS;\r
2098 }\r
2099\r
2100ON_EXIT:\r
2101 return Status;\r
2102}\r
2103\r
e2851998 2104/**\r
f737cfb9 2105 Updates the station IP address and/or subnet mask values of a network device.\r
e2851998 2106\r
f737cfb9 2107 This function updates the station IP address and/or subnet mask values of a network\r
2108 device. The NewStationIp field is used to modify the network device's current IP address.\r
2109 If NewStationIP is NULL, then the current IP address will not be modified. Otherwise,\r
2110 this function updates the StationIp field of the EFI_PXE_BASE_CODE_MODE structure\r
2111 with NewStationIp. The NewSubnetMask field is used to modify the network device's current subnet\r
2112 mask. If NewSubnetMask is NULL, then the current subnet mask will not be modified.\r
2113 Otherwise, this function updates the SubnetMask field of the EFI_PXE_BASE_CODE_MODE\r
2114 structure with NewSubnetMask.\r
e2851998 2115\r
f737cfb9 2116 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
e2851998 2117 @param NewStationIp Pointer to the new IP address to be used by the network device.\r
2118 @param NewSubnetMask Pointer to the new subnet mask to be used by the network device.\r
2119\r
f737cfb9 2120 @retval EFI_SUCCESS The new station IP address and/or subnet mask were updated.\r
2121 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.\r
e2851998 2122 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
2123\r
dc361cc5 2124**/\r
2125EFI_STATUS\r
2126EFIAPI\r
2127EfiPxeBcSetStationIP (\r
2128 IN EFI_PXE_BASE_CODE_PROTOCOL * This,\r
f737cfb9 2129 IN EFI_IP_ADDRESS * NewStationIp OPTIONAL,\r
dc361cc5 2130 IN EFI_IP_ADDRESS * NewSubnetMask OPTIONAL\r
2131 )\r
2132{\r
2133 PXEBC_PRIVATE_DATA *Private;\r
2134 EFI_PXE_BASE_CODE_MODE *Mode;\r
8d285ec0 2135 EFI_ARP_CONFIG_DATA ArpConfigData;\r
dc361cc5 2136\r
2137 if (This == NULL) {\r
2138 return EFI_INVALID_PARAMETER;\r
2139 }\r
2140\r
f6b7393c 2141 if (NewStationIp != NULL && !NetIp4IsUnicast (NTOHL (NewStationIp->Addr[0]), 0)) {\r
dc361cc5 2142 return EFI_INVALID_PARAMETER;\r
2143 }\r
2144\r
2145 if (NewSubnetMask != NULL && !IP4_IS_VALID_NETMASK (NTOHL (NewSubnetMask->Addr[0]))) {\r
2146 return EFI_INVALID_PARAMETER;\r
2147 }\r
2148\r
2149 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
2150 Mode = Private->PxeBc.Mode;\r
2151\r
2152 if (!Mode->Started) {\r
2153 return EFI_NOT_STARTED;\r
2154 }\r
2155\r
2156 if (NewStationIp != NULL) {\r
555e76f8 2157 CopyMem (&Mode->StationIp, NewStationIp, sizeof (EFI_IP_ADDRESS));\r
2158 CopyMem (&Private->StationIp, NewStationIp, sizeof (EFI_IP_ADDRESS));\r
dc361cc5 2159 }\r
2160\r
2161 if (NewSubnetMask != NULL) {\r
555e76f8 2162 CopyMem (&Mode->SubnetMask, NewSubnetMask, sizeof (EFI_IP_ADDRESS));\r
2163 CopyMem (&Private->SubnetMask ,NewSubnetMask, sizeof (EFI_IP_ADDRESS));\r
dc361cc5 2164 }\r
2165\r
2166 Private->AddressIsOk = TRUE;\r
2167\r
8d285ec0 2168 if (!Mode->UsingIpv6) {\r
2169 //\r
2170 // If in IPv4 mode, configure the corresponding ARP with this new\r
2171 // station IP address.\r
2172 //\r
2173 ZeroMem (&ArpConfigData, sizeof (EFI_ARP_CONFIG_DATA));\r
2174\r
2175 ArpConfigData.SwAddressType = 0x0800;\r
2176 ArpConfigData.SwAddressLength = sizeof (EFI_IPv4_ADDRESS);\r
2177 ArpConfigData.StationAddress = &Private->StationIp.v4;\r
2178\r
2179 Private->Arp->Configure (Private->Arp, NULL);\r
2180 Private->Arp->Configure (Private->Arp, &ArpConfigData);\r
982a9eae 2181\r
2182 //\r
2183 // Update the route table.\r
2184 //\r
2185 Mode->RouteTableEntries = 1;\r
2186 Mode->RouteTable[0].IpAddr.Addr[0] = Private->StationIp.Addr[0] & Private->SubnetMask.Addr[0];\r
2187 Mode->RouteTable[0].SubnetMask.Addr[0] = Private->SubnetMask.Addr[0];\r
2188 Mode->RouteTable[0].GwAddr.Addr[0] = 0;\r
8d285ec0 2189 }\r
2190\r
dc361cc5 2191 return EFI_SUCCESS;\r
2192}\r
2193\r
e2851998 2194/**\r
f737cfb9 2195 Updates the contents of the cached DHCP and Discover packets.\r
e2851998 2196\r
f737cfb9 2197 The pointers to the new packets are used to update the contents of the cached\r
2198 packets in the EFI_PXE_BASE_CODE_MODE structure.\r
e2851998 2199\r
f737cfb9 2200 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
2201 @param NewDhcpDiscoverValid Pointer to a value that will replace the current\r
e2851998 2202 DhcpDiscoverValid field.\r
f737cfb9 2203 @param NewDhcpAckReceived Pointer to a value that will replace the current\r
e2851998 2204 DhcpAckReceived field.\r
f737cfb9 2205 @param NewProxyOfferReceived Pointer to a value that will replace the current\r
e2851998 2206 ProxyOfferReceived field.\r
2207 @param NewPxeDiscoverValid Pointer to a value that will replace the current\r
2208 ProxyOfferReceived field.\r
f737cfb9 2209 @param NewPxeReplyReceived Pointer to a value that will replace the current\r
e2851998 2210 PxeReplyReceived field.\r
f737cfb9 2211 @param NewPxeBisReplyReceived Pointer to a value that will replace the current\r
e2851998 2212 PxeBisReplyReceived field.\r
2213 @param NewDhcpDiscover Pointer to the new cached DHCP Discover packet contents.\r
f737cfb9 2214 @param NewDhcpAck Pointer to the new cached DHCP Ack packet contents.\r
2215 @param NewProxyOffer Pointer to the new cached Proxy Offer packet contents.\r
2216 @param NewPxeDiscover Pointer to the new cached PXE Discover packet contents.\r
2217 @param NewPxeReply Pointer to the new cached PXE Reply packet contents.\r
2218 @param NewPxeBisReply Pointer to the new cached PXE BIS Reply packet contents.\r
e2851998 2219\r
f737cfb9 2220 @retval EFI_SUCCESS The cached packet contents were updated.\r
2221 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.\r
2222 @retval EFI_INVALID_PARAMETER This is NULL or not point to a valid EFI_PXE_BASE_CODE_PROTOCOL structure.\r
e2851998 2223\r
dc361cc5 2224**/\r
2225EFI_STATUS\r
2226EFIAPI\r
2227EfiPxeBcSetPackets (\r
2228 IN EFI_PXE_BASE_CODE_PROTOCOL * This,\r
f737cfb9 2229 IN BOOLEAN * NewDhcpDiscoverValid OPTIONAL,\r
2230 IN BOOLEAN * NewDhcpAckReceived OPTIONAL,\r
2231 IN BOOLEAN * NewProxyOfferReceived OPTIONAL,\r
2232 IN BOOLEAN * NewPxeDiscoverValid OPTIONAL,\r
2233 IN BOOLEAN * NewPxeReplyReceived OPTIONAL,\r
2234 IN BOOLEAN * NewPxeBisReplyReceived OPTIONAL,\r
2235 IN EFI_PXE_BASE_CODE_PACKET * NewDhcpDiscover OPTIONAL,\r
2236 IN EFI_PXE_BASE_CODE_PACKET * NewDhcpAck OPTIONAL,\r
2237 IN EFI_PXE_BASE_CODE_PACKET * NewProxyOffer OPTIONAL,\r
2238 IN EFI_PXE_BASE_CODE_PACKET * NewPxeDiscover OPTIONAL,\r
2239 IN EFI_PXE_BASE_CODE_PACKET * NewPxeReply OPTIONAL,\r
dc361cc5 2240 IN EFI_PXE_BASE_CODE_PACKET * NewPxeBisReply OPTIONAL\r
2241 )\r
2242{\r
2243 PXEBC_PRIVATE_DATA *Private;\r
2244 EFI_PXE_BASE_CODE_MODE *Mode;\r
2245\r
2246 if (This == NULL) {\r
2247 return EFI_INVALID_PARAMETER;\r
2248 }\r
2249\r
2250 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
2251 Mode = Private->PxeBc.Mode;\r
2252\r
2253 if (!Mode->Started) {\r
2254 return EFI_NOT_STARTED;\r
2255 }\r
2256\r
dc361cc5 2257 if (NewDhcpDiscoverValid != NULL) {\r
2258 Mode->DhcpDiscoverValid = *NewDhcpDiscoverValid;\r
2259 }\r
2260\r
2261 if (NewDhcpAckReceived != NULL) {\r
2262 Mode->DhcpAckReceived = *NewDhcpAckReceived;\r
2263 }\r
2264\r
2265 if (NewProxyOfferReceived != NULL) {\r
2266 Mode->ProxyOfferReceived = *NewProxyOfferReceived;\r
2267 }\r
2268\r
2269 if (NewPxeDiscoverValid != NULL) {\r
2270 Mode->PxeDiscoverValid = *NewPxeDiscoverValid;\r
2271 }\r
2272\r
2273 if (NewPxeReplyReceived != NULL) {\r
2274 Mode->PxeReplyReceived = *NewPxeReplyReceived;\r
2275 }\r
2276\r
2277 if (NewPxeBisReplyReceived != NULL) {\r
2278 Mode->PxeBisReplyReceived = *NewPxeBisReplyReceived;\r
2279 }\r
2280\r
2281 if (NewDhcpDiscover != NULL) {\r
e48e37fc 2282 CopyMem (&Mode->DhcpDiscover, NewDhcpDiscover, sizeof (EFI_PXE_BASE_CODE_PACKET));\r
dc361cc5 2283 }\r
2284\r
2285 if (NewDhcpAck != NULL) {\r
e48e37fc 2286 CopyMem (&Mode->DhcpAck, NewDhcpAck, sizeof (EFI_PXE_BASE_CODE_PACKET));\r
dc361cc5 2287 }\r
2288\r
2289 if (NewProxyOffer != NULL) {\r
e48e37fc 2290 CopyMem (&Mode->ProxyOffer, NewProxyOffer, sizeof (EFI_PXE_BASE_CODE_PACKET));\r
dc361cc5 2291 }\r
2292\r
2293 if (NewPxeDiscover != NULL) {\r
e48e37fc 2294 CopyMem (&Mode->PxeDiscover, NewPxeDiscover, sizeof (EFI_PXE_BASE_CODE_PACKET));\r
dc361cc5 2295 }\r
2296\r
2297 if (NewPxeReply != NULL) {\r
e48e37fc 2298 CopyMem (&Mode->PxeReply, NewPxeReply, sizeof (EFI_PXE_BASE_CODE_PACKET));\r
dc361cc5 2299 }\r
2300\r
2301 if (NewPxeBisReply != NULL) {\r
e48e37fc 2302 CopyMem (&Mode->PxeBisReply, NewPxeBisReply, sizeof (EFI_PXE_BASE_CODE_PACKET));\r
dc361cc5 2303 }\r
2304\r
2305 return EFI_SUCCESS;\r
2306}\r
2307\r
2308EFI_PXE_BASE_CODE_PROTOCOL mPxeBcProtocolTemplate = {\r
2309 EFI_PXE_BASE_CODE_PROTOCOL_REVISION,\r
2310 EfiPxeBcStart,\r
2311 EfiPxeBcStop,\r
2312 EfiPxeBcDhcp,\r
2313 EfiPxeBcDiscover,\r
2314 EfiPxeBcMtftp,\r
2315 EfiPxeBcUdpWrite,\r
2316 EfiPxeBcUdpRead,\r
2317 EfiPxeBcSetIpFilter,\r
2318 EfiPxeBcArp,\r
2319 EfiPxeBcSetParameters,\r
2320 EfiPxeBcSetStationIP,\r
2321 EfiPxeBcSetPackets,\r
2322 NULL\r
2323};\r
2324\r
e2851998 2325/**\r
f737cfb9 2326 Callback function that is invoked when the PXE Base Code Protocol is about to transmit, has\r
e2851998 2327 received, or is waiting to receive a packet.\r
2328\r
f737cfb9 2329 This function is invoked when the PXE Base Code Protocol is about to transmit, has received,\r
2330 or is waiting to receive a packet. Parameters Function and Received specify the type of event.\r
2331 Parameters PacketLen and Packet specify the packet that generated the event. If these fields\r
2332 are zero and NULL respectively, then this is a status update callback. If the operation specified\r
2333 by Function is to continue, then CALLBACK_STATUS_CONTINUE should be returned. If the operation\r
2334 specified by Function should be aborted, then CALLBACK_STATUS_ABORT should be returned. Due to\r
2335 the polling nature of UEFI device drivers, a callback function should not execute for more than 5 ms.\r
2336 The SetParameters() function must be called after a Callback Protocol is installed to enable the\r
2337 use of callbacks.\r
e2851998 2338\r
f737cfb9 2339 @param This Pointer to the EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL instance.\r
e2851998 2340 @param Function The PXE Base Code Protocol function that is waiting for an event.\r
f737cfb9 2341 @param Received TRUE if the callback is being invoked due to a receive event. FALSE if\r
e2851998 2342 the callback is being invoked due to a transmit event.\r
f737cfb9 2343 @param PacketLength The length, in bytes, of Packet. This field will have a value of zero if\r
e2851998 2344 this is a wait for receive event.\r
f737cfb9 2345 @param PacketPtr If Received is TRUE, a pointer to the packet that was just received;\r
e2851998 2346 otherwise a pointer to the packet that is about to be transmitted.\r
2347\r
2348 @retval EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE if Function specifies a continue operation\r
f737cfb9 2349 @retval EFI_PXE_BASE_CODE_CALLBACK_STATUS_ABORT if Function specifies an abort operation\r
e2851998 2350\r
dc361cc5 2351**/\r
2352EFI_PXE_BASE_CODE_CALLBACK_STATUS\r
2353EFIAPI\r
2354EfiPxeLoadFileCallback (\r
2355 IN EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL * This,\r
2356 IN EFI_PXE_BASE_CODE_FUNCTION Function,\r
2357 IN BOOLEAN Received,\r
2358 IN UINT32 PacketLength,\r
2359 IN EFI_PXE_BASE_CODE_PACKET * PacketPtr OPTIONAL\r
2360 )\r
2361{\r
2362 EFI_INPUT_KEY Key;\r
2363 EFI_STATUS Status;\r
2364\r
2365 //\r
2366 // Catch Ctrl-C or ESC to abort.\r
2367 //\r
2368 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
2369\r
2370 if (!EFI_ERROR (Status)) {\r
2371\r
2372 if (Key.ScanCode == SCAN_ESC || Key.UnicodeChar == (0x1F & 'c')) {\r
2373\r
2374 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_ABORT;\r
2375 }\r
2376 }\r
2377 //\r
2378 // No print if receive packet\r
2379 //\r
2380 if (Received) {\r
2381 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE;\r
2382 }\r
2383 //\r
2384 // Print only for three functions\r
2385 //\r
2386 switch (Function) {\r
2387\r
2388 case EFI_PXE_BASE_CODE_FUNCTION_MTFTP:\r
2389 //\r
2390 // Print only for open MTFTP packets, not every MTFTP packets\r
2391 //\r
2392 if (PacketLength != 0 && PacketPtr != NULL) {\r
2393 if (PacketPtr->Raw[0x1C] != 0x00 || PacketPtr->Raw[0x1D] != 0x01) {\r
2394 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE;\r
2395 }\r
2396 }\r
2397 break;\r
2398\r
2399 case EFI_PXE_BASE_CODE_FUNCTION_DHCP:\r
2400 case EFI_PXE_BASE_CODE_FUNCTION_DISCOVER:\r
2401 break;\r
2402\r
2403 default:\r
2404 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE;\r
2405 }\r
2406\r
2407 if (PacketLength != 0 && PacketPtr != NULL) {\r
2408 //\r
2409 // Print '.' when transmit a packet\r
2410 //\r
2411 AsciiPrint (".");\r
2412\r
2413 }\r
2414\r
2415 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE;\r
2416}\r
2417\r
2418EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL mPxeBcCallBackTemplate = {\r
2419 EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL_REVISION,\r
2420 EfiPxeLoadFileCallback\r
2421};\r
2422\r
2423\r
2424/**\r
f737cfb9 2425 Find the boot file.\r
dc361cc5 2426\r
e2851998 2427 @param Private Pointer to PxeBc private data.\r
2428 @param BufferSize Pointer to buffer size.\r
2429 @param Buffer Pointer to buffer.\r
dc361cc5 2430\r
f737cfb9 2431 @retval EFI_SUCCESS Discover the boot file successfully.\r
2432 @retval EFI_TIMEOUT The TFTP/MTFTP operation timed out.\r
2433 @retval EFI_ABORTED PXE bootstrap server, so local boot need abort.\r
2434 @retval EFI_BUFFER_TOO_SMALL The buffer is too small to load the boot file.\r
e2851998 2435\r
dc361cc5 2436**/\r
2437EFI_STATUS\r
2438DiscoverBootFile (\r
2439 IN PXEBC_PRIVATE_DATA *Private,\r
2440 IN OUT UINT64 *BufferSize,\r
2441 IN VOID *Buffer\r
2442 )\r
2443{\r
2444 EFI_PXE_BASE_CODE_PROTOCOL *PxeBc;\r
2445 EFI_PXE_BASE_CODE_MODE *Mode;\r
2446 EFI_STATUS Status;\r
2447 UINT16 Type;\r
2448 UINT16 Layer;\r
2449 BOOLEAN UseBis;\r
dc361cc5 2450 PXEBC_CACHED_DHCP4_PACKET *Packet;\r
2451 UINT16 Value;\r
2452\r
2453 PxeBc = &Private->PxeBc;\r
2454 Mode = PxeBc->Mode;\r
2455 Type = EFI_PXE_BASE_CODE_BOOT_TYPE_BOOTSTRAP;\r
2456 Layer = EFI_PXE_BASE_CODE_BOOT_LAYER_INITIAL;\r
2457\r
2458 //\r
2459 // do DHCP.\r
2460 //\r
2461 Status = PxeBc->Dhcp (PxeBc, TRUE);\r
2462 if (EFI_ERROR (Status)) {\r
2463 return Status;\r
2464 }\r
2465\r
2466 //\r
2467 // Select a boot server\r
2468 //\r
2469 Status = PxeBcSelectBootPrompt (Private);\r
2470\r
2471 if (Status == EFI_SUCCESS) {\r
2472 Status = PxeBcSelectBootMenu (Private, &Type, TRUE);\r
2473 } else if (Status == EFI_TIMEOUT) {\r
2474 Status = PxeBcSelectBootMenu (Private, &Type, FALSE);\r
2475 }\r
2476\r
2477 if (!EFI_ERROR (Status)) {\r
2478\r
2479 if (Type == EFI_PXE_BASE_CODE_BOOT_TYPE_BOOTSTRAP) {\r
2480 //\r
2481 // Local boot(PXE bootstrap server) need abort\r
2482 //\r
2483 return EFI_ABORTED;\r
2484 }\r
2485\r
2486 UseBis = (BOOLEAN) (Mode->BisSupported && Mode->BisDetected);\r
2487 Status = PxeBc->Discover (PxeBc, Type, &Layer, UseBis, NULL);\r
2488 if (EFI_ERROR (Status)) {\r
2489 return Status;\r
2490 }\r
2491 }\r
2492\r
2493 *BufferSize = 0;\r
dc361cc5 2494\r
2495 //\r
2496 // Get bootfile name and (m)tftp server ip addresss\r
2497 //\r
2498 if (Mode->PxeReplyReceived) {\r
2499 Packet = &Private->PxeReply;\r
2500 } else if (Mode->ProxyOfferReceived) {\r
2501 Packet = &Private->ProxyOffer;\r
2502 } else {\r
2503 Packet = &Private->Dhcp4Ack;\r
2504 }\r
2505\r
319075ff 2506 //\r
2ce5c88a 2507 // Use siaddr(next server) in DHCPOFFER packet header, if zero, use option 54(server identifier)\r
2508 // in DHCPOFFER packet.\r
2509 // (It does not comply with PXE Spec, Ver2.1)\r
319075ff 2510 //\r
2ce5c88a 2511 if (EFI_IP4_EQUAL (&Packet->Packet.Offer.Dhcp4.Header.ServerAddr, &mZeroIp4Addr)) {\r
e48e37fc 2512 CopyMem (\r
dc361cc5 2513 &Private->ServerIp,\r
2514 Packet->Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_SERVER_ID]->Data,\r
2515 sizeof (EFI_IPv4_ADDRESS)\r
2516 );\r
319075ff 2517 } else {\r
2518 CopyMem (\r
e2851998 2519 &Private->ServerIp,\r
2520 &Packet->Packet.Offer.Dhcp4.Header.ServerAddr,\r
319075ff 2521 sizeof (EFI_IPv4_ADDRESS)\r
2522 );\r
2523 }\r
2524 if (Private->ServerIp.Addr[0] == 0) {\r
2525 return EFI_DEVICE_ERROR;\r
dc361cc5 2526 }\r
2527\r
2528 ASSERT (Packet->Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] != NULL);\r
2529\r
2530 //\r
2531 // bootlfile name\r
2532 //\r
2533 Private->BootFileName = (CHAR8 *) (Packet->Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_BOOTFILE]->Data);\r
2534\r
2535 if (Packet->Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_BOOTFILE_LEN] != NULL) {\r
2536 //\r
2537 // Already have the bootfile length option, compute the file size\r
2538 //\r
e48e37fc 2539 CopyMem (&Value, Packet->Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_BOOTFILE_LEN]->Data, sizeof (Value));\r
dc361cc5 2540 Value = NTOHS (Value);\r
2541 *BufferSize = 512 * Value;\r
2542 Status = EFI_BUFFER_TOO_SMALL;\r
2543 } else {\r
2544 //\r
2545 // Get the bootfile size from tftp\r
2546 //\r
2547 Status = PxeBc->Mtftp (\r
2548 PxeBc,\r
2549 EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE,\r
2550 Buffer,\r
2551 FALSE,\r
2552 BufferSize,\r
fa6d3ee4 2553 &Private->BlockSize,\r
dc361cc5 2554 &Private->ServerIp,\r
2555 (UINT8 *) Private->BootFileName,\r
2556 NULL,\r
2557 FALSE\r
2558 );\r
2559 }\r
2560\r
2561 Private->FileSize = (UINTN) *BufferSize;\r
2562\r
2563 return Status;\r
2564}\r
2565\r
dc361cc5 2566/**\r
f737cfb9 2567 Causes the driver to load a specified file.\r
2568\r
2569 @param This Protocol instance pointer.\r
2570 @param FilePath The device specific path of the file to load.\r
e2851998 2571 @param BootPolicy If TRUE, indicates that the request originates from the\r
f737cfb9 2572 boot manager is attempting to load FilePath as a boot\r
2573 selection. If FALSE, then FilePath must match as exact file\r
2574 to be loaded.\r
2575 @param BufferSize On input the size of Buffer in bytes. On output with a return\r
e2851998 2576 code of EFI_SUCCESS, the amount of data transferred to\r
f737cfb9 2577 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,\r
2578 the size of Buffer required to retrieve the requested file.\r
2579 @param Buffer The memory buffer to transfer the file to. IF Buffer is NULL,\r
e2851998 2580 then no the size of the requested file is returned in\r
f737cfb9 2581 BufferSize.\r
2582\r
2583 @retval EFI_SUCCESS The file was loaded.\r
2584 @retval EFI_UNSUPPORTED The device does not support the provided BootPolicy\r
e2851998 2585 @retval EFI_INVALID_PARAMETER FilePath is not a valid device path, or\r
f737cfb9 2586 BufferSize is NULL.\r
2587 @retval EFI_NO_MEDIA No medium was present to load the file.\r
2588 @retval EFI_DEVICE_ERROR The file was not loaded due to a device error.\r
2589 @retval EFI_NO_RESPONSE The remote system did not respond.\r
2590 @retval EFI_NOT_FOUND The file was not found.\r
2591 @retval EFI_ABORTED The file load process was manually cancelled.\r
dc361cc5 2592\r
2593**/\r
2594EFI_STATUS\r
2595EFIAPI\r
2596EfiPxeLoadFile (\r
2597 IN EFI_LOAD_FILE_PROTOCOL * This,\r
2598 IN EFI_DEVICE_PATH_PROTOCOL * FilePath,\r
2599 IN BOOLEAN BootPolicy,\r
2600 IN OUT UINTN *BufferSize,\r
2601 IN VOID *Buffer OPTIONAL\r
2602 )\r
2603{\r
2604 PXEBC_PRIVATE_DATA *Private;\r
2605 EFI_PXE_BASE_CODE_PROTOCOL *PxeBc;\r
2606 BOOLEAN NewMakeCallback;\r
dc361cc5 2607 EFI_STATUS Status;\r
2608 UINT64 TmpBufSize;\r
dd29f3ed 2609 BOOLEAN MediaPresent;\r
dc361cc5 2610\r
2611 Private = PXEBC_PRIVATE_DATA_FROM_LOADFILE (This);\r
2612 PxeBc = &Private->PxeBc;\r
2613 NewMakeCallback = FALSE;\r
dc361cc5 2614 Status = EFI_DEVICE_ERROR;\r
2615\r
2616 if (This == NULL || BufferSize == NULL) {\r
2617\r
2618 return EFI_INVALID_PARAMETER;\r
2619 }\r
2620\r
2621 //\r
2622 // Only support BootPolicy\r
2623 //\r
2624 if (!BootPolicy) {\r
2625 return EFI_UNSUPPORTED;\r
2626 }\r
2627\r
dd29f3ed 2628 //\r
2629 // Check media status before PXE start\r
2630 //\r
2631 MediaPresent = TRUE;\r
2632 NetLibDetectMedia (Private->Controller, &MediaPresent);\r
2633 if (!MediaPresent) {\r
2634 return EFI_NO_MEDIA;\r
2635 }\r
2636\r
dc361cc5 2637 Status = PxeBc->Start (PxeBc, FALSE);\r
2638 if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {\r
2639 return Status;\r
2640 }\r
2641\r
2642 Status = gBS->HandleProtocol (\r
2643 Private->Controller,\r
2644 &gEfiPxeBaseCodeCallbackProtocolGuid,\r
2645 (VOID **) &Private->PxeBcCallback\r
2646 );\r
2647 if (Status == EFI_UNSUPPORTED) {\r
2648\r
2649 CopyMem (&Private->LoadFileCallback, &mPxeBcCallBackTemplate, sizeof (Private->LoadFileCallback));\r
2650\r
2651 Status = gBS->InstallProtocolInterface (\r
2652 &Private->Controller,\r
2653 &gEfiPxeBaseCodeCallbackProtocolGuid,\r
2654 EFI_NATIVE_INTERFACE,\r
2655 &Private->LoadFileCallback\r
2656 );\r
2657\r
2658 NewMakeCallback = (BOOLEAN) (Status == EFI_SUCCESS);\r
2659\r
2660 Status = PxeBc->SetParameters (PxeBc, NULL, NULL, NULL, NULL, &NewMakeCallback);\r
2661 if (EFI_ERROR (Status)) {\r
2662 PxeBc->Stop (PxeBc);\r
2663 return Status;\r
2664 }\r
2665 }\r
2666\r
2667 if (Private->FileSize == 0) {\r
2668 TmpBufSize = 0;\r
2669 Status = DiscoverBootFile (Private, &TmpBufSize, Buffer);\r
2670\r
2671 if (sizeof (UINTN) < sizeof (UINT64) && (TmpBufSize > 0xFFFFFFFF)) {\r
2672 Status = EFI_DEVICE_ERROR;\r
39941daf 2673 } else if (TmpBufSize > 0 && *BufferSize >= (UINTN) TmpBufSize && Buffer != NULL) {\r
319075ff 2674 *BufferSize = (UINTN) TmpBufSize;\r
2675 Status = PxeBc->Mtftp (\r
2676 PxeBc,\r
2677 EFI_PXE_BASE_CODE_TFTP_READ_FILE,\r
2678 Buffer,\r
2679 FALSE,\r
2680 &TmpBufSize,\r
fa6d3ee4 2681 &Private->BlockSize,\r
319075ff 2682 &Private->ServerIp,\r
2683 (UINT8 *) Private->BootFileName,\r
2684 NULL,\r
2685 FALSE\r
2686 );\r
39941daf 2687 } else if (TmpBufSize > 0) {\r
dc361cc5 2688 *BufferSize = (UINTN) TmpBufSize;\r
319075ff 2689 Status = EFI_BUFFER_TOO_SMALL;\r
dc361cc5 2690 }\r
319075ff 2691 } else if (Buffer == NULL || Private->FileSize > *BufferSize) {\r
dc361cc5 2692 *BufferSize = Private->FileSize;\r
2693 Status = EFI_BUFFER_TOO_SMALL;\r
2694 } else {\r
2695 //\r
2696 // Download the file.\r
2697 //\r
2698 TmpBufSize = (UINT64) (*BufferSize);\r
2699 Status = PxeBc->Mtftp (\r
2700 PxeBc,\r
2701 EFI_PXE_BASE_CODE_TFTP_READ_FILE,\r
2702 Buffer,\r
2703 FALSE,\r
2704 &TmpBufSize,\r
fa6d3ee4 2705 &Private->BlockSize,\r
dc361cc5 2706 &Private->ServerIp,\r
2707 (UINT8 *) Private->BootFileName,\r
2708 NULL,\r
2709 FALSE\r
2710 );\r
2711 }\r
2712 //\r
2713 // If we added a callback protocol, now is the time to remove it.\r
2714 //\r
2715 if (NewMakeCallback) {\r
2716\r
2717 NewMakeCallback = FALSE;\r
2718\r
2719 PxeBc->SetParameters (PxeBc, NULL, NULL, NULL, NULL, &NewMakeCallback);\r
2720\r
2721 gBS->UninstallProtocolInterface (\r
2722 Private->Controller,\r
2723 &gEfiPxeBaseCodeCallbackProtocolGuid,\r
2724 &Private->LoadFileCallback\r
2725 );\r
2726 }\r
619eff3f 2727\r
dc361cc5 2728 //\r
2729 // Check download status\r
2730 //\r
619eff3f 2731 if (Status == EFI_SUCCESS) {\r
6ee21b60 2732 //\r
2733 // The functionality of PXE Base Code protocol will not be stopped,\r
2734 // when downloading is successfully.\r
2735 //\r
8792362f 2736 return EFI_SUCCESS;\r
dc361cc5 2737\r
619eff3f 2738 } else if (Status == EFI_BUFFER_TOO_SMALL) {\r
dc361cc5 2739 if (Buffer != NULL) {\r
2740 AsciiPrint ("PXE-E05: Download buffer is smaller than requested file.\n");\r
2741 } else {\r
ee6e8714 2742 //\r
2743 // The functionality of PXE Base Code protocol will not be stopped.\r
2744 //\r
dc361cc5 2745 return Status;\r
2746 }\r
dc361cc5 2747\r
619eff3f 2748 } else if (Status == EFI_DEVICE_ERROR) {\r
dc361cc5 2749 AsciiPrint ("PXE-E07: Network device error.\n");\r
dc361cc5 2750\r
619eff3f 2751 } else if (Status == EFI_OUT_OF_RESOURCES) {\r
dc361cc5 2752 AsciiPrint ("PXE-E09: Could not allocate I/O buffers.\n");\r
dc361cc5 2753\r
619eff3f 2754 } else if (Status == EFI_NO_MEDIA) {\r
dc361cc5 2755 AsciiPrint ("PXE-E12: Could not detect network connection.\n");\r
dc361cc5 2756\r
619eff3f 2757 } else if (Status == EFI_NO_RESPONSE) {\r
dc361cc5 2758 AsciiPrint ("PXE-E16: No offer received.\n");\r
dc361cc5 2759\r
619eff3f 2760 } else if (Status == EFI_TIMEOUT) {\r
dc361cc5 2761 AsciiPrint ("PXE-E18: Server response timeout.\n");\r
dc361cc5 2762\r
619eff3f 2763 } else if (Status == EFI_ABORTED) {\r
dc361cc5 2764 AsciiPrint ("PXE-E21: Remote boot cancelled.\n");\r
dc361cc5 2765\r
619eff3f 2766 } else if (Status == EFI_ICMP_ERROR) {\r
dc361cc5 2767 AsciiPrint ("PXE-E22: Client received ICMP error from server.\n");\r
dc361cc5 2768\r
619eff3f 2769 } else if (Status == EFI_TFTP_ERROR) {\r
dc361cc5 2770 AsciiPrint ("PXE-E23: Client received TFTP error from server.\n");\r
dc361cc5 2771\r
619eff3f 2772 } else {\r
dc361cc5 2773 AsciiPrint ("PXE-E99: Unexpected network error.\n");\r
dc361cc5 2774 }\r
2775\r
2776 PxeBc->Stop (PxeBc);\r
2777\r
2778 return Status;\r
2779}\r
2780\r
434ce3fe 2781EFI_LOAD_FILE_PROTOCOL mLoadFileProtocolTemplate = { EfiPxeLoadFile };\r
2782\r