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