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