]> git.proxmox.com Git - mirror_edk2.git/blame - NetworkPkg/UefiPxeBcDxe/PxeBcImpl.c
Add more check for the code to make it more safely.
[mirror_edk2.git] / NetworkPkg / UefiPxeBcDxe / PxeBcImpl.c
CommitLineData
a3bcde70
HT
1/** @file\r
2 This implementation of EFI_PXE_BASE_CODE_PROTOCOL and EFI_LOAD_FILE_PROTOCOL.\r
3\r
620f846f 4 Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>\r
a3bcde70
HT
5\r
6 This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php.\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "PxeBcImpl.h"\r
17\r
18\r
19/**\r
20 Enables the use of the PXE Base Code Protocol functions.\r
21\r
22 This function enables the use of the PXE Base Code Protocol functions. If the\r
23 Started field of the EFI_PXE_BASE_CODE_MODE structure is already TRUE, then\r
24 EFI_ALREADY_STARTED will be returned. If UseIpv6 is TRUE, then IPv6 formatted\r
25 addresses will be used in this session. If UseIpv6 is FALSE, then IPv4 formatted\r
26 addresses will be used in this session. If UseIpv6 is TRUE, and the Ipv6Supported\r
27 field of the EFI_PXE_BASE_CODE_MODE structure is FALSE, then EFI_UNSUPPORTED will\r
28 be returned. If there is not enough memory or other resources to start the PXE\r
29 Base Code Protocol, then EFI_OUT_OF_RESOURCES will be returned. Otherwise, the\r
30 PXE Base Code Protocol will be started.\r
31\r
32 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
33 @param[in] UseIpv6 Specifies the type of IP addresses that are to be\r
34 used during the session that is being started.\r
35 Set to TRUE for IPv6, and FALSE for IPv4.\r
36\r
37 @retval EFI_SUCCESS The PXE Base Code Protocol was started.\r
38 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.\r
39 @retval EFI_UNSUPPORTED UseIpv6 is TRUE, but the Ipv6Supported field of the\r
40 EFI_PXE_BASE_CODE_MODE structure is FALSE.\r
41 @retval EFI_ALREADY_STARTED The PXE Base Code Protocol is already in the started state.\r
42 @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid\r
43 EFI_PXE_BASE_CODE_PROTOCOL structure.\r
44 @retval EFI_OUT_OF_RESOURCES Could not allocate enough memory or other resources to start the\r
45 PXE Base Code Protocol.\r
46\r
47**/\r
48EFI_STATUS\r
49EFIAPI\r
50EfiPxeBcStart (\r
51 IN EFI_PXE_BASE_CODE_PROTOCOL *This,\r
52 IN BOOLEAN UseIpv6\r
53 )\r
54{\r
55 PXEBC_PRIVATE_DATA *Private;\r
56 EFI_PXE_BASE_CODE_MODE *Mode;\r
57 UINTN Index;\r
58 EFI_STATUS Status;\r
59\r
60 if (This == NULL) {\r
61 return EFI_INVALID_PARAMETER;\r
62 }\r
63\r
64 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
65 Mode = Private->PxeBc.Mode;\r
66\r
67 if (Mode->Started) {\r
68 return EFI_ALREADY_STARTED;\r
69 }\r
70\r
71 //\r
72 // Detect whether using IPv6 or not, and set it into mode data.\r
73 //\r
74 if (UseIpv6 && Mode->Ipv6Available && Mode->Ipv6Supported && Private->Ip6Nic != NULL) {\r
75 Mode->UsingIpv6 = TRUE;\r
76 } else if (!UseIpv6 && Private->Ip4Nic != NULL) {\r
77 Mode->UsingIpv6 = FALSE;\r
78 } else {\r
79 return EFI_UNSUPPORTED;\r
80 }\r
81\r
82 if (Mode->UsingIpv6) {\r
83 AsciiPrint ("\n>>Start PXE over IPv6");\r
ce280355 84 //\r
85 // Configure udp6 instance to receive data.\r
86 //\r
87 Status = Private->Udp6Read->Configure (\r
88 Private->Udp6Read,\r
89 &Private->Udp6CfgData\r
90 );\r
91 if (EFI_ERROR (Status)) {\r
92 goto ON_ERROR;\r
93 }\r
94 \r
a3bcde70
HT
95 //\r
96 // Configure block size for TFTP as a default value to handle all link layers.\r
97 //\r
98 Private->BlockSize = (UINTN) (Private->Ip6MaxPacketSize -\r
99 PXEBC_DEFAULT_UDP_OVERHEAD_SIZE - PXEBC_DEFAULT_TFTP_OVERHEAD_SIZE);\r
100\r
101 //\r
102 // PXE over IPv6 starts here, initialize the fields and list header.\r
103 //\r
104 Private->Ip6Policy = PXEBC_IP6_POLICY_MAX;\r
105 Private->ProxyOffer.Dhcp6.Packet.Offer.Size = PXEBC_DHCP6_PACKET_MAX_SIZE;\r
106 Private->DhcpAck.Dhcp6.Packet.Ack.Size = PXEBC_DHCP6_PACKET_MAX_SIZE;\r
107 Private->PxeReply.Dhcp6.Packet.Ack.Size = PXEBC_DHCP6_PACKET_MAX_SIZE;\r
108\r
109 for (Index = 0; Index < PXEBC_OFFER_MAX_NUM; Index++) {\r
110 Private->OfferBuffer[Index].Dhcp6.Packet.Offer.Size = PXEBC_DHCP6_PACKET_MAX_SIZE;\r
111 }\r
112\r
113 //\r
114 // Create event and set status for token to capture ICMP6 error message.\r
115 //\r
116 Private->Icmp6Token.Status = EFI_NOT_READY;\r
117 Status = gBS->CreateEvent (\r
118 EVT_NOTIFY_SIGNAL,\r
119 TPL_NOTIFY,\r
120 PxeBcIcmp6ErrorUpdate,\r
121 Private,\r
122 &Private->Icmp6Token.Event\r
123 );\r
124 if (EFI_ERROR (Status)) {\r
125 goto ON_ERROR;\r
126 }\r
127 } else {\r
128 AsciiPrint ("\n>>Start PXE over IPv4");\r
ce280355 129 //\r
130 // Configure udp4 instance to receive data.\r
131 //\r
132 Status = Private->Udp4Read->Configure (\r
133 Private->Udp4Read,\r
134 &Private->Udp4CfgData\r
135 );\r
136 if (EFI_ERROR (Status)) {\r
137 goto ON_ERROR;\r
138 }\r
139 \r
a3bcde70
HT
140 //\r
141 // Configure block size for TFTP as a default value to handle all link layers.\r
142 //\r
143 Private->BlockSize = (UINTN) (Private->Ip4MaxPacketSize -\r
144 PXEBC_DEFAULT_UDP_OVERHEAD_SIZE - PXEBC_DEFAULT_TFTP_OVERHEAD_SIZE);\r
145\r
146 //\r
147 // PXE over IPv4 starts here, initialize the fields.\r
148 //\r
149 Private->ProxyOffer.Dhcp4.Packet.Offer.Size = PXEBC_DHCP4_PACKET_MAX_SIZE;\r
150 Private->DhcpAck.Dhcp4.Packet.Ack.Size = PXEBC_DHCP4_PACKET_MAX_SIZE;\r
151 Private->PxeReply.Dhcp4.Packet.Ack.Size = PXEBC_DHCP4_PACKET_MAX_SIZE;\r
152\r
153 for (Index = 0; Index < PXEBC_OFFER_MAX_NUM; Index++) {\r
154 Private->OfferBuffer[Index].Dhcp4.Packet.Offer.Size = PXEBC_DHCP4_PACKET_MAX_SIZE;\r
155 }\r
156\r
157 PxeBcSeedDhcp4Packet (&Private->SeedPacket, Private->Udp4Read);\r
158\r
159 //\r
160 // Create the event for Arp cache update.\r
161 //\r
162 Status = gBS->CreateEvent (\r
163 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
164 TPL_CALLBACK,\r
165 PxeBcArpCacheUpdate,\r
166 Private,\r
167 &Private->ArpUpdateEvent\r
168 );\r
169 if (EFI_ERROR (Status)) {\r
170 goto ON_ERROR;\r
171 }\r
172\r
173 //\r
174 // Start a periodic timer by second to update Arp cache.\r
175 //\r
176 Status = gBS->SetTimer (\r
177 Private->ArpUpdateEvent,\r
178 TimerPeriodic,\r
179 TICKS_PER_SECOND\r
180 );\r
181 if (EFI_ERROR (Status)) {\r
182 goto ON_ERROR;\r
183 }\r
184\r
185 //\r
186 // Create event and set status for token to capture ICMP error message.\r
187 //\r
188 Private->Icmp6Token.Status = EFI_NOT_READY;\r
189 Status = gBS->CreateEvent (\r
190 EVT_NOTIFY_SIGNAL,\r
191 TPL_NOTIFY,\r
192 PxeBcIcmpErrorUpdate,\r
193 Private,\r
194 &Private->IcmpToken.Event\r
195 );\r
196 if (EFI_ERROR (Status)) {\r
197 goto ON_ERROR;\r
198 }\r
199 }\r
200\r
201 //\r
202 // If PcdTftpBlockSize is set to non-zero, override the default value.\r
203 //\r
204 if (PcdGet64 (PcdTftpBlockSize) != 0) {\r
205 Private->BlockSize = (UINTN) PcdGet64 (PcdTftpBlockSize);\r
206 }\r
207\r
208 //\r
209 // Create event for UdpRead/UdpWrite timeout since they are both blocking API.\r
210 //\r
211 Status = gBS->CreateEvent (\r
212 EVT_TIMER,\r
213 TPL_CALLBACK,\r
214 NULL,\r
215 NULL,\r
216 &Private->UdpTimeOutEvent\r
217 );\r
218 if (EFI_ERROR (Status)) {\r
219 goto ON_ERROR;\r
220 }\r
221\r
222 Private->IsAddressOk = FALSE;\r
223 Mode->Started = TRUE;\r
224\r
225 return EFI_SUCCESS;\r
226\r
227ON_ERROR:\r
228 if (Mode->UsingIpv6) {\r
229 if (Private->Icmp6Token.Event != NULL) {\r
230 gBS->CloseEvent (Private->Icmp6Token.Event);\r
231 Private->Icmp6Token.Event = NULL;\r
232 }\r
233 Private->Udp6Read->Configure (Private->Udp6Read, NULL);\r
234 Private->Ip6->Configure (Private->Ip6, NULL);\r
235 } else {\r
236 if (Private->ArpUpdateEvent != NULL) {\r
237 gBS->CloseEvent (Private->ArpUpdateEvent);\r
238 Private->ArpUpdateEvent = NULL;\r
239 }\r
240 if (Private->IcmpToken.Event != NULL) {\r
241 gBS->CloseEvent (Private->IcmpToken.Event);\r
242 Private->IcmpToken.Event = NULL;\r
243 }\r
244 Private->Udp4Read->Configure (Private->Udp4Read, NULL);\r
245 Private->Ip4->Configure (Private->Ip4, NULL);\r
246 }\r
247 return Status;\r
248}\r
249\r
250\r
251/**\r
252 Disable the use of the PXE Base Code Protocol functions.\r
253\r
254 This function stops all activity on the network device. All the resources allocated\r
255 in Start() are released, the Started field of the EFI_PXE_BASE_CODE_MODE structure is\r
256 set to FALSE, and EFI_SUCCESS is returned. If the Started field of the EFI_PXE_BASE_CODE_MODE\r
257 structure is already FALSE, then EFI_NOT_STARTED will be returned.\r
258\r
259 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
260\r
261 @retval EFI_SUCCESS The PXE Base Code Protocol was stopped.\r
262 @retval EFI_NOT_STARTED The PXE Base Code Protocol is already in the stopped state.\r
263 @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid\r
264 EFI_PXE_BASE_CODE_PROTOCOL structure.\r
265 @retval Others\r
266\r
267**/\r
268EFI_STATUS\r
269EFIAPI\r
270EfiPxeBcStop (\r
271 IN EFI_PXE_BASE_CODE_PROTOCOL *This\r
272 )\r
273{\r
274 PXEBC_PRIVATE_DATA *Private;\r
275 EFI_PXE_BASE_CODE_MODE *Mode;\r
276 BOOLEAN Ipv6Supported;\r
277 BOOLEAN Ipv6Available;\r
278\r
279 if (This == NULL) {\r
280 return EFI_INVALID_PARAMETER;\r
281 }\r
282\r
283 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
284 Mode = Private->PxeBc.Mode;\r
285 Ipv6Supported = Mode->Ipv6Supported;\r
286 Ipv6Available = Mode->Ipv6Available;\r
287\r
288 if (!Mode->Started) {\r
289 return EFI_NOT_STARTED;\r
290 }\r
291\r
292 if (Mode->UsingIpv6) {\r
293 //\r
294 // Configure all the instances for IPv6 as NULL.\r
295 //\r
296 ZeroMem (&Private->Udp6CfgData.StationAddress, sizeof (EFI_IPv6_ADDRESS));\r
297 ZeroMem (&Private->Ip6CfgData.StationAddress, sizeof (EFI_IPv6_ADDRESS));\r
298 Private->Dhcp6->Stop (Private->Dhcp6);\r
299 Private->Dhcp6->Configure (Private->Dhcp6, NULL);\r
300 Private->Udp6Write->Configure (Private->Udp6Write, NULL);\r
301 Private->Udp6Read->Groups (Private->Udp6Read, FALSE, NULL);\r
302 Private->Udp6Read->Configure (Private->Udp6Read, NULL);\r
303 Private->Ip6->Cancel (Private->Ip6, &Private->Icmp6Token);\r
304 Private->Ip6->Configure (Private->Ip6, NULL);\r
305 PxeBcUnregisterIp6Address (Private);\r
306 if (Private->Icmp6Token.Event != NULL) {\r
307 gBS->CloseEvent (Private->Icmp6Token.Event);\r
308 Private->Icmp6Token.Event = NULL;\r
309 }\r
310 if (Private->Dhcp6Request != NULL) {\r
311 FreePool (Private->Dhcp6Request);\r
312 Private->Dhcp6Request = NULL;\r
313 }\r
314 if (Private->BootFileName != NULL) {\r
315 FreePool (Private->BootFileName);\r
316 Private->BootFileName = NULL;\r
317 }\r
318 } else {\r
319 //\r
320 // Configure all the instances for IPv4 as NULL.\r
321 //\r
322 ZeroMem (&Private->Udp4CfgData.StationAddress, sizeof (EFI_IPv4_ADDRESS));\r
323 ZeroMem (&Private->Udp4CfgData.SubnetMask, sizeof (EFI_IPv4_ADDRESS));\r
324 ZeroMem (&Private->Ip4CfgData.StationAddress, sizeof (EFI_IPv4_ADDRESS));\r
325 ZeroMem (&Private->Ip4CfgData.SubnetMask, sizeof (EFI_IPv4_ADDRESS));\r
326 Private->Dhcp4->Stop (Private->Dhcp4);\r
327 Private->Dhcp4->Configure (Private->Dhcp4, NULL);\r
328 Private->Udp4Write->Configure (Private->Udp4Write, NULL);\r
329 Private->Udp4Read->Groups (Private->Udp4Read, FALSE, NULL);\r
330 Private->Udp4Read->Configure (Private->Udp4Read, NULL);\r
331 Private->Ip4->Cancel (Private->Ip4, &Private->IcmpToken);\r
332 Private->Ip4->Configure (Private->Ip4, NULL);\r
333 if (Private->ArpUpdateEvent != NULL) {\r
334 gBS->CloseEvent (Private->ArpUpdateEvent);\r
335 Private->ArpUpdateEvent = NULL;\r
336 }\r
337 if (Private->IcmpToken.Event != NULL) {\r
338 gBS->CloseEvent (Private->IcmpToken.Event);\r
339 Private->IcmpToken.Event = NULL;\r
340 }\r
341 }\r
342\r
343 gBS->CloseEvent (Private->UdpTimeOutEvent);\r
344 Private->CurSrcPort = 0;\r
345 Private->BootFileSize = 0;\r
129b8b09 346 Private->SolicitTimes = 0;\r
347 Private->ElapsedTime = 0;\r
620f846f
FS
348 ZeroMem (&Private->StationIp, sizeof (EFI_IP_ADDRESS));\r
349 ZeroMem (&Private->SubnetMask, sizeof (EFI_IP_ADDRESS));\r
350 ZeroMem (&Private->GatewayIp, sizeof (EFI_IP_ADDRESS));\r
351 ZeroMem (&Private->ServerIp, sizeof (EFI_IP_ADDRESS));\r
a3bcde70
HT
352\r
353 //\r
354 // Reset the mode data.\r
355 //\r
356 ZeroMem (Mode, sizeof (EFI_PXE_BASE_CODE_MODE));\r
357 Mode->Ipv6Available = Ipv6Available;\r
358 Mode->Ipv6Supported = Ipv6Supported;\r
359 Mode->AutoArp = TRUE;\r
360 Mode->TTL = DEFAULT_TTL;\r
361 Mode->ToS = DEFAULT_ToS;\r
362\r
363 return EFI_SUCCESS;\r
364}\r
365\r
366\r
367/**\r
368 Attempts to complete a DHCPv4 D.O.R.A. (discover / offer / request / acknowledge) or DHCPv6\r
369 S.A.R.R (solicit / advertise / request / reply) sequence.\r
370\r
371 If SortOffers is TRUE, then the cached DHCP offer packets will be sorted before\r
372 they are tried. If SortOffers is FALSE, then the cached DHCP offer packets will\r
373 be tried in the order in which they are received. Please see the Preboot Execution\r
374 Environment (PXE) Specification and Unified Extensible Firmware Interface (UEFI)\r
375 Specification for additional details on the implementation of DHCP.\r
376 If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,\r
377 then the DHCP sequence will be stopped and EFI_ABORTED will be returned.\r
378\r
379 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
380 @param[in] SortOffers TRUE if the offers received should be sorted. Set to FALSE to\r
381 try the offers in the order that they are received.\r
382\r
383 @retval EFI_SUCCESS Valid DHCP has completed.\r
384 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.\r
385 @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid\r
386 EFI_PXE_BASE_CODE_PROTOCOL structure.\r
387 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.\r
388 @retval EFI_OUT_OF_RESOURCES Could not allocate enough memory to complete the DHCP Protocol.\r
389 @retval EFI_ABORTED The callback function aborted the DHCP Protocol.\r
390 @retval EFI_TIMEOUT The DHCP Protocol timed out.\r
391 @retval EFI_ICMP_ERROR An ICMP error packet was received during the DHCP session.\r
392 @retval EFI_NO_RESPONSE Valid PXE offer was not received.\r
393\r
394**/\r
395EFI_STATUS\r
396EFIAPI\r
397EfiPxeBcDhcp (\r
398 IN EFI_PXE_BASE_CODE_PROTOCOL *This,\r
399 IN BOOLEAN SortOffers\r
400 )\r
401{\r
402 PXEBC_PRIVATE_DATA *Private;\r
403 EFI_PXE_BASE_CODE_MODE *Mode;\r
404 EFI_STATUS Status;\r
405 EFI_PXE_BASE_CODE_IP_FILTER IpFilter;\r
406\r
407 if (This == NULL) {\r
408 return EFI_INVALID_PARAMETER;\r
409 }\r
410\r
411 Status = EFI_SUCCESS;\r
412 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
413 Mode = Private->PxeBc.Mode;\r
414 Mode->IcmpErrorReceived = FALSE;\r
415 Private->Function = EFI_PXE_BASE_CODE_FUNCTION_DHCP;\r
416 Private->IsOfferSorted = SortOffers;\r
4496ff75 417 Private->SolicitTimes = 0;\r
418 Private->ElapsedTime = 0;\r
a3bcde70
HT
419\r
420 if (!Mode->Started) {\r
421 return EFI_NOT_STARTED;\r
422 }\r
423\r
424 if (Mode->UsingIpv6) {\r
425\r
426 //\r
427 // Stop Udp6Read instance\r
428 //\r
429 Private->Udp6Read->Configure (Private->Udp6Read, NULL);\r
430\r
431 //\r
432 // Start S.A.R.R. process to get a IPv6 address and other boot information.\r
433 //\r
434 Status = PxeBcDhcp6Sarr (Private, Private->Dhcp6);\r
435 } else {\r
436\r
437 //\r
438 // Stop Udp4Read instance\r
439 //\r
440 Private->Udp4Read->Configure (Private->Udp4Read, NULL);\r
441\r
442 //\r
443 // Start D.O.R.A. process to get a IPv4 address and other boot information.\r
444 //\r
445 Status = PxeBcDhcp4Dora (Private, Private->Dhcp4);\r
446 }\r
1ac9cb8a 447\r
448 //\r
449 // Reconfigure the UDP instance with the default configuration.\r
450 //\r
357af285 451 if (Mode->UsingIpv6) {\r
452 Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData);\r
453 } else {\r
454 Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);\r
455 }\r
a3bcde70
HT
456 //\r
457 // Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP\r
458 // receive filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.\r
459 //\r
460 ZeroMem(&IpFilter, sizeof (EFI_PXE_BASE_CODE_IP_FILTER));\r
461 IpFilter.Filters = EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP;\r
462 This->SetIpFilter (This, &IpFilter);\r
463\r
464 return Status;\r
465}\r
466\r
467\r
468/**\r
469 Attempts to complete the PXE Boot Server and/or boot image discovery sequence.\r
470\r
471 This function attempts to complete the PXE Boot Server and/or boot image discovery\r
472 sequence. If this sequence is completed, then EFI_SUCCESS is returned, and the\r
473 PxeDiscoverValid, PxeDiscover, PxeReplyReceived, and PxeReply fields of the\r
474 EFI_PXE_BASE_CODE_MODE structure are filled in. If UseBis is TRUE, then the\r
475 PxeBisReplyReceived and PxeBisReply fields of the EFI_PXE_BASE_CODE_MODE structure\r
476 will also be filled in. If UseBis is FALSE, then PxeBisReplyValid will be set to FALSE.\r
477 In the structure referenced by parameter Info, the PXE Boot Server list, SrvList[],\r
478 has two uses: It is the Boot Server IP address list used for unicast discovery\r
479 (if the UseUCast field is TRUE), and it is the list used for Boot Server verification\r
480 (if the MustUseList field is TRUE). Also, if the MustUseList field in that structure\r
481 is TRUE and the AcceptAnyResponse field in the SrvList[] array is TRUE, any Boot\r
482 Server reply of that type will be accepted. If the AcceptAnyResponse field is\r
483 FALSE, only responses from Boot Servers with matching IP addresses will be accepted.\r
484 This function can take at least 10 seconds to timeout and return control to the\r
485 caller. If the Discovery sequence does not complete, then EFI_TIMEOUT will be\r
486 returned. Please see the Preboot Execution Environment (PXE) Specification for\r
487 additional details on the implementation of the Discovery sequence.\r
488 If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,\r
489 then the Discovery sequence is stopped and EFI_ABORTED will be returned.\r
490\r
491 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
492 @param[in] Type The type of bootstrap to perform.\r
493 @param[in] Layer Pointer to the boot server layer number to discover, which must be\r
494 PXE_BOOT_LAYER_INITIAL when a new server type is being\r
495 discovered.\r
496 @param[in] UseBis TRUE if Boot Integrity Services are to be used. FALSE otherwise.\r
497 @param[in] Info Pointer to a data structure that contains additional information\r
498 on the type of discovery operation that is to be performed.\r
499 It is optional.\r
500\r
501 @retval EFI_SUCCESS The Discovery sequence has been completed.\r
502 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.\r
503 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
504 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.\r
505 @retval EFI_OUT_OF_RESOURCES Could not allocate enough memory to complete Discovery.\r
506 @retval EFI_ABORTED The callback function aborted the Discovery sequence.\r
507 @retval EFI_TIMEOUT The Discovery sequence timed out.\r
508 @retval EFI_ICMP_ERROR An ICMP error packet was received during the PXE discovery\r
509 session.\r
510\r
511**/\r
512EFI_STATUS\r
513EFIAPI\r
514EfiPxeBcDiscover (\r
515 IN EFI_PXE_BASE_CODE_PROTOCOL *This,\r
516 IN UINT16 Type,\r
517 IN UINT16 *Layer,\r
518 IN BOOLEAN UseBis,\r
519 IN EFI_PXE_BASE_CODE_DISCOVER_INFO *Info OPTIONAL\r
520 )\r
521{\r
522 PXEBC_PRIVATE_DATA *Private;\r
523 EFI_PXE_BASE_CODE_MODE *Mode;\r
524 EFI_PXE_BASE_CODE_DISCOVER_INFO DefaultInfo;\r
525 EFI_PXE_BASE_CODE_SRVLIST *SrvList;\r
526 PXEBC_BOOT_SVR_ENTRY *BootSvrEntry;\r
527 UINT16 Index;\r
528 EFI_STATUS Status;\r
529 EFI_PXE_BASE_CODE_IP_FILTER IpFilter;\r
9063c328 530 EFI_PXE_BASE_CODE_DISCOVER_INFO *NewCreatedInfo;\r
a3bcde70
HT
531\r
532 if (This == NULL) {\r
533 return EFI_INVALID_PARAMETER;\r
534 }\r
535\r
536 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
537 Mode = Private->PxeBc.Mode;\r
538 Mode->IcmpErrorReceived = FALSE;\r
539 BootSvrEntry = NULL;\r
540 SrvList = NULL;\r
541 Status = EFI_DEVICE_ERROR;\r
542 Private->Function = EFI_PXE_BASE_CODE_FUNCTION_DISCOVER;\r
9063c328 543 NewCreatedInfo = NULL;\r
a3bcde70
HT
544\r
545 if (!Mode->Started) {\r
546 return EFI_NOT_STARTED;\r
547 }\r
548\r
549 //\r
550 // Station address should be ready before do discover.\r
551 //\r
552 if (!Private->IsAddressOk) {\r
553 return EFI_INVALID_PARAMETER;\r
554 }\r
555\r
556 if (Mode->UsingIpv6) {\r
557\r
558 //\r
559 // Stop Udp6Read instance\r
560 //\r
561 Private->Udp6Read->Configure (Private->Udp6Read, NULL);\r
562 } else {\r
563\r
564 //\r
565 // Stop Udp4Read instance\r
566 //\r
567 Private->Udp4Read->Configure (Private->Udp4Read, NULL);\r
568 }\r
569\r
570 //\r
571 // There are 3 methods to get the information for discover.\r
572 //\r
f402291b 573 ZeroMem (&DefaultInfo, sizeof (EFI_PXE_BASE_CODE_DISCOVER_INFO));\r
a3bcde70
HT
574 if (*Layer != EFI_PXE_BASE_CODE_BOOT_LAYER_INITIAL) {\r
575 //\r
576 // 1. Take the previous setting as the discover info.\r
577 //\r
578 if (!Mode->PxeDiscoverValid ||\r
579 !Mode->PxeReplyReceived ||\r
580 (!Mode->PxeBisReplyReceived && UseBis)) {\r
581 Status = EFI_INVALID_PARAMETER;\r
582 goto ON_EXIT;\r
583 }\r
584\r
585 Info = &DefaultInfo;\r
586 Info->IpCnt = 1;\r
587 Info->UseUCast = TRUE;\r
588 SrvList = Info->SrvList;\r
589 SrvList[0].Type = Type;\r
590 SrvList[0].AcceptAnyResponse = FALSE;\r
591\r
592 CopyMem (&SrvList->IpAddr, &Private->ServerIp, sizeof (EFI_IP_ADDRESS));\r
593\r
594 } else if (Info == NULL) {\r
595 //\r
596 // 2. Extract the discover information from the cached packets if unspecified.\r
597 //\r
9063c328 598 NewCreatedInfo = &DefaultInfo;\r
599 Status = PxeBcExtractDiscoverInfo (Private, Type, &NewCreatedInfo, &BootSvrEntry, &SrvList);\r
a3bcde70
HT
600 if (EFI_ERROR (Status)) {\r
601 goto ON_EXIT;\r
602 }\r
393a3169 603 ASSERT (NewCreatedInfo != NULL);\r
9063c328 604 Info = NewCreatedInfo;\r
a3bcde70
HT
605 } else {\r
606 //\r
607 // 3. Take the pass-in information as the discover info, and validate the server list.\r
608 //\r
609 SrvList = Info->SrvList;\r
610\r
611 if (!SrvList[0].AcceptAnyResponse) {\r
612 for (Index = 1; Index < Info->IpCnt; Index++) {\r
613 if (SrvList[Index].AcceptAnyResponse) {\r
614 break;\r
615 }\r
616 }\r
617 if (Index != Info->IpCnt) {\r
618 //\r
619 // It's invalid if the first server doesn't accecpt any response\r
928927dd 620 // but any of the other servers does accept any response.\r
a3bcde70
HT
621 //\r
622 Status = EFI_INVALID_PARAMETER;\r
623 goto ON_EXIT;\r
624 }\r
625 }\r
626 }\r
627\r
628 //\r
629 // Info and BootSvrEntry/SrvList are all ready by now, so execute discover by UniCast/BroadCast/MultiCast.\r
630 //\r
631 if ((!Info->UseUCast && !Info->UseBCast && !Info->UseMCast) ||\r
632 (Info->MustUseList && Info->IpCnt == 0)) {\r
633 Status = EFI_INVALID_PARAMETER;\r
634 goto ON_EXIT;\r
635 }\r
636\r
637 Private->IsDoDiscover = TRUE;\r
638\r
9063c328 639 if (Info->UseMCast) {\r
a3bcde70
HT
640 //\r
641 // Do discover by multicast.\r
642 //\r
643 Status = PxeBcDiscoverBootServer (\r
644 Private,\r
645 Type,\r
646 Layer,\r
647 UseBis,\r
648 &Info->ServerMCastIp,\r
9063c328 649 Info->IpCnt,\r
650 SrvList\r
a3bcde70
HT
651 );\r
652\r
653 } else if (Info->UseBCast) {\r
654 //\r
655 // Do discover by broadcast, but only valid for IPv4.\r
656 //\r
657 ASSERT (!Mode->UsingIpv6);\r
658 Status = PxeBcDiscoverBootServer (\r
659 Private,\r
660 Type,\r
661 Layer,\r
662 UseBis,\r
663 NULL,\r
664 Info->IpCnt,\r
665 SrvList\r
666 );\r
9063c328 667\r
668 } else if (Info->UseUCast) {\r
669 //\r
670 // Do discover by unicast.\r
671 //\r
672 for (Index = 0; Index < Info->IpCnt; Index++) {\r
673 if (BootSvrEntry == NULL) {\r
674 CopyMem (&Private->ServerIp, &SrvList[Index].IpAddr, sizeof (EFI_IP_ADDRESS));\r
675 } else {\r
676 ASSERT (!Mode->UsingIpv6);\r
677 ZeroMem (&Private->ServerIp, sizeof (EFI_IP_ADDRESS));\r
678 CopyMem (&Private->ServerIp, &BootSvrEntry->IpAddr[Index], sizeof (EFI_IPv4_ADDRESS));\r
679 }\r
680\r
681 Status = PxeBcDiscoverBootServer (\r
682 Private,\r
683 Type,\r
684 Layer,\r
685 UseBis,\r
686 &Private->ServerIp,\r
687 Info->IpCnt,\r
688 SrvList\r
689 );\r
690 }\r
a3bcde70
HT
691 }\r
692\r
1ac9cb8a 693 if (!EFI_ERROR (Status)) {\r
a3bcde70
HT
694 //\r
695 // Parse the cached PXE reply packet, and store it into mode data if valid.\r
696 //\r
697 if (Mode->UsingIpv6) {\r
698 Status = PxeBcParseDhcp6Packet (&Private->PxeReply.Dhcp6);\r
699 if (!EFI_ERROR (Status)) {\r
700 CopyMem (\r
701 &Mode->PxeReply.Dhcpv6,\r
9063c328 702 &Private->PxeReply.Dhcp6.Packet.Ack.Dhcp6,\r
703 Private->PxeReply.Dhcp6.Packet.Ack.Length\r
a3bcde70
HT
704 );\r
705 Mode->PxeReplyReceived = TRUE;\r
706 Mode->PxeDiscoverValid = TRUE;\r
707 }\r
708 } else {\r
709 Status = PxeBcParseDhcp4Packet (&Private->PxeReply.Dhcp4);\r
710 if (!EFI_ERROR (Status)) {\r
711 CopyMem (\r
712 &Mode->PxeReply.Dhcpv4,\r
9063c328 713 &Private->PxeReply.Dhcp4.Packet.Ack.Dhcp4,\r
714 Private->PxeReply.Dhcp4.Packet.Ack.Length\r
a3bcde70
HT
715 );\r
716 Mode->PxeReplyReceived = TRUE;\r
717 Mode->PxeDiscoverValid = TRUE;\r
718 }\r
719 }\r
720 }\r
721\r
722ON_EXIT:\r
723\r
9063c328 724 if (NewCreatedInfo != NULL && NewCreatedInfo != &DefaultInfo) {\r
725 FreePool (NewCreatedInfo);\r
726 }\r
727 \r
18127352 728 if (Mode->UsingIpv6) {\r
357af285 729 Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData);\r
18127352 730 } else {\r
357af285 731 Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);\r
18127352 732 }\r
733 \r
a3bcde70
HT
734 //\r
735 // Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP\r
736 // receive filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.\r
737 //\r
738 ZeroMem(&IpFilter, sizeof (EFI_PXE_BASE_CODE_IP_FILTER));\r
739 IpFilter.Filters = EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP;\r
740 This->SetIpFilter (This, &IpFilter);\r
741\r
742 return Status;\r
743}\r
744\r
745\r
746/**\r
747 Used to perform TFTP and MTFTP services.\r
748\r
749 This function is used to perform TFTP and MTFTP services. This includes the\r
750 TFTP operations to get the size of a file, read a directory, read a file, and\r
751 write a file. It also includes the MTFTP operations to get the size of a file,\r
752 read a directory, and read a file. The type of operation is specified by Operation.\r
753 If the callback function that is invoked during the TFTP/MTFTP operation does\r
754 not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE, then EFI_ABORTED will\r
755 be returned.\r
756 For read operations, the return data will be placed in the buffer specified by\r
757 BufferPtr. If BufferSize is too small to contain the entire downloaded file,\r
758 then EFI_BUFFER_TOO_SMALL will be returned and BufferSize will be set to zero,\r
759 or the size of the requested file. (NOTE: the size of the requested file is only returned\r
760 if the TFTP server supports TFTP options). If BufferSize is large enough for the\r
761 read operation, then BufferSize will be set to the size of the downloaded file,\r
762 and EFI_SUCCESS will be returned. Applications using the PxeBc.Mtftp() services\r
763 should use the get-file-size operations to determine the size of the downloaded\r
764 file prior to using the read-file operations-especially when downloading large\r
765 (greater than 64 MB) files-instead of making two calls to the read-file operation.\r
766 Following this recommendation will save time if the file is larger than expected\r
767 and the TFTP server does not support TFTP option extensions. Without TFTP option\r
768 extension support, the client must download the entire file, counting and discarding\r
769 the received packets, to determine the file size.\r
770 For write operations, the data to be sent is in the buffer specified by BufferPtr.\r
771 BufferSize specifies the number of bytes to send. If the write operation completes\r
772 successfully, then EFI_SUCCESS will be returned.\r
773 For TFTP "get file size" operations, the size of the requested file or directory\r
774 is returned in BufferSize, and EFI_SUCCESS will be returned. If the TFTP server\r
775 does not support options, the file will be downloaded into a bit bucket and the\r
776 length of the downloaded file will be returned. For MTFTP "get file size" operations,\r
777 if the MTFTP server does not support the "get file size" option, EFI_UNSUPPORTED\r
778 will be returned.\r
779 This function can take up to 10 seconds to timeout and return control to the caller.\r
780 If the TFTP sequence does not complete, EFI_TIMEOUT will be returned.\r
781 If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,\r
782 then the TFTP sequence is stopped and EFI_ABORTED will be returned.\r
783\r
784 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
785 @param[in] Operation The type of operation to perform.\r
786 @param[in, out] BufferPtr A pointer to the data buffer.\r
787 @param[in] Overwrite Only used on write file operations. TRUE if a file on a remote\r
788 server can be overwritten.\r
789 @param[in, out] BufferSize For get-file-size operations, *BufferSize returns the size of the\r
790 requested file.\r
791 @param[in] BlockSize The requested block size to be used during a TFTP transfer.\r
792 @param[in] ServerIp The TFTP / MTFTP server IP address.\r
793 @param[in] Filename A Null-terminated ASCII string that specifies a directory name\r
794 or a file name.\r
795 @param[in] Info Pointer to the MTFTP information.\r
796 @param[in] DontUseBuffer Set to FALSE for normal TFTP and MTFTP read file operation.\r
797\r
798 @retval EFI_SUCCESS The TFTP/MTFTP operation was completed.\r
799 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.\r
800 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
801 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.\r
802 @retval EFI_BUFFER_TOO_SMALL The buffer is not large enough to complete the read operation.\r
803 @retval EFI_ABORTED The callback function aborted the TFTP/MTFTP operation.\r
804 @retval EFI_TIMEOUT The TFTP/MTFTP operation timed out.\r
805 @retval EFI_ICMP_ERROR An ICMP error packet was received during the MTFTP session.\r
806 @retval EFI_TFTP_ERROR A TFTP error packet was received during the MTFTP session.\r
807\r
808**/\r
809EFI_STATUS\r
810EFIAPI\r
811EfiPxeBcMtftp (\r
812 IN EFI_PXE_BASE_CODE_PROTOCOL *This,\r
813 IN EFI_PXE_BASE_CODE_TFTP_OPCODE Operation,\r
814 IN OUT VOID *BufferPtr OPTIONAL,\r
815 IN BOOLEAN Overwrite,\r
816 IN OUT UINT64 *BufferSize,\r
817 IN UINTN *BlockSize OPTIONAL,\r
818 IN EFI_IP_ADDRESS *ServerIp,\r
819 IN UINT8 *Filename,\r
820 IN EFI_PXE_BASE_CODE_MTFTP_INFO *Info OPTIONAL,\r
821 IN BOOLEAN DontUseBuffer\r
822 )\r
823{\r
824 PXEBC_PRIVATE_DATA *Private;\r
825 EFI_PXE_BASE_CODE_MODE *Mode;\r
826 EFI_MTFTP4_CONFIG_DATA Mtftp4Config;\r
827 EFI_MTFTP6_CONFIG_DATA Mtftp6Config;\r
828 VOID *Config;\r
829 EFI_STATUS Status;\r
830 EFI_PXE_BASE_CODE_IP_FILTER IpFilter;\r
831\r
832\r
833 if ((This == NULL) ||\r
834 (Filename == NULL) ||\r
835 (BufferSize == NULL) ||\r
836 (ServerIp == NULL) ||\r
837 ((BufferPtr == NULL) && DontUseBuffer) ||\r
838 ((BlockSize != NULL) && (*BlockSize < PXE_MTFTP_DEFAULT_BLOCK_SIZE)) ||\r
839 (!NetIp4IsUnicast (NTOHL (ServerIp->Addr[0]), 0) && !NetIp6IsValidUnicast (&ServerIp->v6))) {\r
840 return EFI_INVALID_PARAMETER;\r
841 }\r
842\r
843 Config = NULL;\r
844 Status = EFI_DEVICE_ERROR;\r
845 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
846 Mode = Private->PxeBc.Mode;\r
847\r
848 if (Mode->UsingIpv6) {\r
849 //\r
850 // Set configuration data for Mtftp6 instance.\r
851 //\r
852 ZeroMem (&Mtftp6Config, sizeof (EFI_MTFTP6_CONFIG_DATA));\r
853 Config = &Mtftp6Config;\r
854 Mtftp6Config.TimeoutValue = PXEBC_MTFTP_TIMEOUT;\r
855 Mtftp6Config.TryCount = PXEBC_MTFTP_RETRIES;\r
856 CopyMem (&Mtftp6Config.StationIp, &Private->StationIp.v6, sizeof (EFI_IPv6_ADDRESS));\r
857 CopyMem (&Mtftp6Config.ServerIp, &ServerIp->v6, sizeof (EFI_IPv6_ADDRESS));\r
858 //\r
859 // Stop Udp6Read instance\r
860 //\r
861 Private->Udp6Read->Configure (Private->Udp6Read, NULL);\r
862 } else {\r
863 //\r
864 // Set configuration data for Mtftp4 instance.\r
865 //\r
866 ZeroMem (&Mtftp4Config, sizeof (EFI_MTFTP4_CONFIG_DATA));\r
867 Config = &Mtftp4Config;\r
868 Mtftp4Config.UseDefaultSetting = FALSE;\r
869 Mtftp4Config.TimeoutValue = PXEBC_MTFTP_TIMEOUT;\r
870 Mtftp4Config.TryCount = PXEBC_MTFTP_RETRIES;\r
871 CopyMem (&Mtftp4Config.StationIp, &Private->StationIp.v4, sizeof (EFI_IPv4_ADDRESS));\r
872 CopyMem (&Mtftp4Config.SubnetMask, &Private->SubnetMask.v4, sizeof (EFI_IPv4_ADDRESS));\r
873 CopyMem (&Mtftp4Config.GatewayIp, &Private->GatewayIp.v4, sizeof (EFI_IPv4_ADDRESS));\r
874 CopyMem (&Mtftp4Config.ServerIp, &ServerIp->v4, sizeof (EFI_IPv4_ADDRESS));\r
875 //\r
876 // Stop Udp4Read instance\r
877 //\r
878 Private->Udp4Read->Configure (Private->Udp4Read, NULL);\r
879 }\r
880\r
881 Mode->TftpErrorReceived = FALSE;\r
882 Mode->IcmpErrorReceived = FALSE;\r
883\r
884 switch (Operation) {\r
885\r
886 case EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE:\r
887 //\r
888 // Send TFTP request to get file size.\r
889 //\r
890 Status = PxeBcTftpGetFileSize (\r
891 Private,\r
892 Config,\r
893 Filename,\r
894 BlockSize,\r
895 BufferSize\r
896 );\r
897\r
898 break;\r
899\r
900 case EFI_PXE_BASE_CODE_TFTP_READ_FILE:\r
901 //\r
902 // Send TFTP request to read file.\r
903 //\r
904 Status = PxeBcTftpReadFile (\r
905 Private,\r
906 Config,\r
907 Filename,\r
908 BlockSize,\r
909 BufferPtr,\r
910 BufferSize,\r
911 DontUseBuffer\r
912 );\r
913\r
914 break;\r
915\r
916 case EFI_PXE_BASE_CODE_TFTP_WRITE_FILE:\r
917 //\r
918 // Send TFTP request to write file.\r
919 //\r
920 Status = PxeBcTftpWriteFile (\r
921 Private,\r
922 Config,\r
923 Filename,\r
924 Overwrite,\r
925 BlockSize,\r
926 BufferPtr,\r
927 BufferSize\r
928 );\r
929\r
930 break;\r
931\r
932 case EFI_PXE_BASE_CODE_TFTP_READ_DIRECTORY:\r
933 //\r
934 // Send TFTP request to read directory.\r
935 //\r
936 Status = PxeBcTftpReadDirectory (\r
937 Private,\r
938 Config,\r
939 Filename,\r
940 BlockSize,\r
941 BufferPtr,\r
942 BufferSize,\r
943 DontUseBuffer\r
944 );\r
945\r
946 break;\r
947\r
948 case EFI_PXE_BASE_CODE_MTFTP_GET_FILE_SIZE:\r
949 case EFI_PXE_BASE_CODE_MTFTP_READ_FILE:\r
950 case EFI_PXE_BASE_CODE_MTFTP_READ_DIRECTORY:\r
951 Status = EFI_UNSUPPORTED;\r
952\r
953 break;\r
954\r
955 default:\r
956 Status = EFI_INVALID_PARAMETER;\r
957\r
958 break;\r
959 }\r
960\r
961 if (Status == EFI_ICMP_ERROR) {\r
962 Mode->IcmpErrorReceived = TRUE;\r
963 }\r
964\r
1ac9cb8a 965 //\r
966 // Reconfigure the UDP instance with the default configuration.\r
967 //\r
18127352 968 if (Mode->UsingIpv6) {\r
357af285 969 Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData);\r
18127352 970 } else {\r
357af285 971 Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);\r
18127352 972 }\r
a3bcde70
HT
973 //\r
974 // Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP\r
975 // receive filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.\r
976 //\r
977 ZeroMem(&IpFilter, sizeof (EFI_PXE_BASE_CODE_IP_FILTER));\r
978 IpFilter.Filters = EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP;\r
979 This->SetIpFilter (This, &IpFilter);\r
980\r
981 return Status;\r
982}\r
983\r
984\r
985/**\r
986 Writes a UDP packet to the network interface.\r
987\r
988 This function writes a UDP packet specified by the (optional HeaderPtr and)\r
989 BufferPtr parameters to the network interface. The UDP header is automatically\r
990 built by this routine. It uses the parameters OpFlags, DestIp, DestPort, GatewayIp,\r
991 SrcIp, and SrcPort to build this header. If the packet is successfully built and\r
992 transmitted through the network interface, then EFI_SUCCESS will be returned.\r
993 If a timeout occurs during the transmission of the packet, then EFI_TIMEOUT will\r
994 be returned. If an ICMP error occurs during the transmission of the packet, then\r
995 the IcmpErrorReceived field is set to TRUE, the IcmpError field is filled in and\r
996 EFI_ICMP_ERROR will be returned. If the Callback Protocol does not return\r
997 EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE, then EFI_ABORTED will be returned.\r
998\r
999 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
1000 @param[in] OpFlags The UDP operation flags.\r
1001 @param[in] DestIp The destination IP address.\r
1002 @param[in] DestPort The destination UDP port number.\r
1003 @param[in] GatewayIp The gateway IP address.\r
1004 @param[in] SrcIp The source IP address.\r
1005 @param[in, out] SrcPort The source UDP port number.\r
1006 @param[in] HeaderSize An optional field which may be set to the length of a header\r
1007 at HeaderPtr to be prefixed to the data at BufferPtr.\r
1008 @param[in] HeaderPtr If HeaderSize is not NULL, a pointer to a header to be\r
1009 prefixed to the data at BufferPtr.\r
1010 @param[in] BufferSize A pointer to the size of the data at BufferPtr.\r
1011 @param[in] BufferPtr A pointer to the data to be written.\r
1012\r
1013 @retval EFI_SUCCESS The UDP Write operation completed.\r
1014 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.\r
1015 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
1016 @retval EFI_BAD_BUFFER_SIZE The buffer is too long to be transmitted.\r
1017 @retval EFI_ABORTED The callback function aborted the UDP Write operation.\r
1018 @retval EFI_TIMEOUT The UDP Write operation timed out.\r
1019 @retval EFI_ICMP_ERROR An ICMP error packet was received during the UDP write session.\r
1020\r
1021**/\r
1022EFI_STATUS\r
1023EFIAPI\r
1024EfiPxeBcUdpWrite (\r
1025 IN EFI_PXE_BASE_CODE_PROTOCOL *This,\r
1026 IN UINT16 OpFlags,\r
1027 IN EFI_IP_ADDRESS *DestIp,\r
1028 IN EFI_PXE_BASE_CODE_UDP_PORT *DestPort,\r
1029 IN EFI_IP_ADDRESS *GatewayIp OPTIONAL,\r
1030 IN EFI_IP_ADDRESS *SrcIp OPTIONAL,\r
1031 IN OUT EFI_PXE_BASE_CODE_UDP_PORT *SrcPort OPTIONAL,\r
1032 IN UINTN *HeaderSize OPTIONAL,\r
1033 IN VOID *HeaderPtr OPTIONAL,\r
1034 IN UINTN *BufferSize,\r
1035 IN VOID *BufferPtr\r
1036 )\r
1037{\r
1038 PXEBC_PRIVATE_DATA *Private;\r
1039 EFI_PXE_BASE_CODE_MODE *Mode;\r
1040 EFI_UDP4_SESSION_DATA Udp4Session;\r
1041 EFI_UDP6_SESSION_DATA Udp6Session;\r
1042 EFI_STATUS Status;\r
1043 BOOLEAN DoNotFragment;\r
1044\r
1045 if (This == NULL || DestIp == NULL || DestPort == NULL) {\r
1046 return EFI_INVALID_PARAMETER;\r
1047 }\r
1048\r
1049 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
1050 Mode = Private->PxeBc.Mode;\r
1051\r
1052 if ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_MAY_FRAGMENT) != 0) {\r
1053 DoNotFragment = FALSE;\r
1054 } else {\r
1055 DoNotFragment = TRUE;\r
1056 }\r
1057\r
1058 if (!Mode->UsingIpv6 && GatewayIp != NULL && !NetIp4IsUnicast (NTOHL (GatewayIp->Addr[0]), 0)) {\r
1059 //\r
1060 // Gateway is provided but it's not a unicast IPv4 address, while it will be ignored for IPv6.\r
1061 //\r
1062 return EFI_INVALID_PARAMETER;\r
1063 }\r
1064\r
1065 if (HeaderSize != NULL && (*HeaderSize == 0 || HeaderPtr == NULL)) {\r
1066 return EFI_INVALID_PARAMETER;\r
1067 }\r
1068\r
1069 if (BufferSize == NULL || (*BufferSize != 0 && BufferPtr == NULL)) {\r
1070 return EFI_INVALID_PARAMETER;\r
1071 }\r
1072\r
1073 if (!Mode->Started) {\r
1074 return EFI_NOT_STARTED;\r
1075 }\r
1076\r
1077 if (!Private->IsAddressOk && SrcIp == NULL) {\r
1078 return EFI_INVALID_PARAMETER;\r
1079 }\r
1080\r
1081 if (Private->CurSrcPort == 0 ||\r
1082 (SrcPort != NULL && *SrcPort != Private->CurSrcPort)) {\r
1083 //\r
1084 // Reconfigure UDPv4/UDPv6 for UdpWrite if the source port changed.\r
1085 //\r
1086 if (SrcPort != NULL) {\r
1087 Private->CurSrcPort = *SrcPort;\r
1088 }\r
1089 }\r
1090\r
1091 if (Mode->UsingIpv6) {\r
1092 Status = PxeBcConfigUdp6Write (\r
1093 Private->Udp6Write,\r
1094 &Private->StationIp.v6,\r
1095 &Private->CurSrcPort\r
1096 );\r
1097 } else {\r
1098 //\r
1099 // Configure the UDPv4 instance with gateway information from DHCP server as default.\r
1100 //\r
1101 Status = PxeBcConfigUdp4Write (\r
1102 Private->Udp4Write,\r
1103 &Private->StationIp.v4,\r
1104 &Private->SubnetMask.v4,\r
1105 &Private->GatewayIp.v4,\r
1106 &Private->CurSrcPort,\r
1107 DoNotFragment\r
1108 );\r
1109 }\r
1110\r
1111 if (EFI_ERROR (Status)) {\r
1112 Private->CurSrcPort = 0;\r
1113 return EFI_INVALID_PARAMETER;\r
1114 } else if (SrcPort != NULL) {\r
1115 *SrcPort = Private->CurSrcPort;\r
1116 }\r
1117\r
1118 //\r
1119 // Start a timer as timeout event for this blocking API.\r
1120 //\r
1121 gBS->SetTimer (Private->UdpTimeOutEvent, TimerRelative, PXEBC_UDP_TIMEOUT);\r
1122\r
1123 if (Mode->UsingIpv6) {\r
1124 //\r
1125 // Construct UDPv6 session data.\r
1126 //\r
1127 ZeroMem (&Udp6Session, sizeof (EFI_UDP6_SESSION_DATA));\r
1128 CopyMem (&Udp6Session.DestinationAddress, DestIp, sizeof (EFI_IPv6_ADDRESS));\r
1129 Udp6Session.DestinationPort = *DestPort;\r
1130 if (SrcIp != NULL) {\r
1131 CopyMem (&Udp6Session.SourceAddress, SrcIp, sizeof (EFI_IPv6_ADDRESS));\r
1132 }\r
1133 if (SrcPort != NULL) {\r
1134 Udp6Session.SourcePort = *SrcPort;\r
1135 }\r
1136\r
1137 Status = PxeBcUdp6Write (\r
1138 Private->Udp6Write,\r
1139 &Udp6Session,\r
1140 Private->UdpTimeOutEvent,\r
1141 HeaderSize,\r
1142 HeaderPtr,\r
1143 BufferSize,\r
1144 BufferPtr\r
1145 );\r
1146 } else {\r
1147 //\r
1148 // Construct UDPv4 session data.\r
1149 //\r
1150 ZeroMem (&Udp4Session, sizeof (EFI_UDP4_SESSION_DATA));\r
1151 CopyMem (&Udp4Session.DestinationAddress, DestIp, sizeof (EFI_IPv4_ADDRESS));\r
1152 Udp4Session.DestinationPort = *DestPort;\r
1153 if (SrcIp != NULL) {\r
1154 CopyMem (&Udp4Session.SourceAddress, SrcIp, sizeof (EFI_IPv4_ADDRESS));\r
1155 }\r
1156 if (SrcPort != NULL) {\r
1157 Udp4Session.SourcePort = *SrcPort;\r
1158 }\r
1159 //\r
1160 // Override the gateway information if user specified.\r
1161 //\r
1162 Status = PxeBcUdp4Write (\r
1163 Private->Udp4Write,\r
1164 &Udp4Session,\r
1165 Private->UdpTimeOutEvent,\r
1166 (EFI_IPv4_ADDRESS *) GatewayIp,\r
1167 HeaderSize,\r
1168 HeaderPtr,\r
1169 BufferSize,\r
1170 BufferPtr\r
1171 );\r
1172 }\r
1173\r
1174 gBS->SetTimer (Private->UdpTimeOutEvent, TimerCancel, 0);\r
1175\r
1176\r
1177 //\r
1178 // Reset the UdpWrite instance.\r
1179 //\r
1180 if (Mode->UsingIpv6) {\r
1181 Private->Udp6Write->Configure (Private->Udp6Write, NULL);\r
1182 } else {\r
1183 Private->Udp4Write->Configure (Private->Udp4Write, NULL);\r
1184 }\r
1185\r
1186 return Status;\r
1187}\r
1188\r
1189\r
1190/**\r
1191 Reads a UDP packet from the network interface.\r
1192+\r
1193 This function reads a UDP packet from a network interface. The data contents\r
1194 are returned in (the optional HeaderPtr and) BufferPtr, and the size of the\r
1195 buffer received is returned in BufferSize . If the input BufferSize is smaller\r
1196 than the UDP packet received (less optional HeaderSize), it will be set to the\r
1197 required size, and EFI_BUFFER_TOO_SMALL will be returned. In this case, the\r
1198 contents of BufferPtr are undefined, and the packet is lost. If a UDP packet is\r
1199 successfully received, then EFI_SUCCESS will be returned, and the information\r
1200 from the UDP header will be returned in DestIp, DestPort, SrcIp, and SrcPort if\r
1201 they are not NULL. Depending on the values of OpFlags and the DestIp, DestPort,\r
1202 SrcIp, and SrcPort input values, different types of UDP packet receive filtering\r
1203 will be performed. The following tables summarize these receive filter operations.\r
1204\r
1205 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
1206 @param[in] OpFlags The UDP operation flags.\r
1207 @param[in, out] DestIp The destination IP address.\r
1208 @param[in, out] DestPort The destination UDP port number.\r
1209 @param[in, out] SrcIp The source IP address.\r
1210 @param[in, out] SrcPort The source UDP port number.\r
1211 @param[in] HeaderSize An optional field which may be set to the length of a\r
1212 header at HeaderPtr to be prefixed to the data at BufferPtr.\r
1213 @param[in] HeaderPtr If HeaderSize is not NULL, a pointer to a header to be\r
1214 prefixed to the data at BufferPtr.\r
1215 @param[in, out] BufferSize A pointer to the size of the data at BufferPtr.\r
1216 @param[in] BufferPtr A pointer to the data to be read.\r
1217\r
1218 @retval EFI_SUCCESS The UDP Read operation was completed.\r
1219 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.\r
1220 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
1221 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.\r
1222 @retval EFI_BUFFER_TOO_SMALL The packet is larger than Buffer can hold.\r
1223 @retval EFI_ABORTED The callback function aborted the UDP Read operation.\r
1224 @retval EFI_TIMEOUT The UDP Read operation timed out.\r
1225\r
1226**/\r
1227EFI_STATUS\r
1228EFIAPI\r
1229EfiPxeBcUdpRead (\r
1230 IN EFI_PXE_BASE_CODE_PROTOCOL *This,\r
1231 IN UINT16 OpFlags,\r
1232 IN OUT EFI_IP_ADDRESS *DestIp OPTIONAL,\r
1233 IN OUT EFI_PXE_BASE_CODE_UDP_PORT *DestPort OPTIONAL,\r
1234 IN OUT EFI_IP_ADDRESS *SrcIp OPTIONAL,\r
1235 IN OUT EFI_PXE_BASE_CODE_UDP_PORT *SrcPort OPTIONAL,\r
1236 IN UINTN *HeaderSize OPTIONAL,\r
1237 IN VOID *HeaderPtr OPTIONAL,\r
1238 IN OUT UINTN *BufferSize,\r
1239 IN VOID *BufferPtr\r
1240 )\r
1241{\r
1242 PXEBC_PRIVATE_DATA *Private;\r
1243 EFI_PXE_BASE_CODE_MODE *Mode;\r
1244 EFI_UDP4_COMPLETION_TOKEN Udp4Token;\r
1245 EFI_UDP6_COMPLETION_TOKEN Udp6Token;\r
1246 EFI_UDP4_RECEIVE_DATA *Udp4Rx;\r
1247 EFI_UDP6_RECEIVE_DATA *Udp6Rx;\r
1248 EFI_STATUS Status;\r
1249 BOOLEAN IsDone;\r
1250 BOOLEAN IsMatched;\r
1251 UINTN CopiedLen;\r
eb2710af 1252 UINTN HeaderLen;\r
1253 UINTN HeaderCopiedLen;\r
1254 UINTN BufferCopiedLen;\r
1255 UINT32 FragmentLength;\r
1256 UINTN FragmentIndex;\r
1257 UINT8 *FragmentBuffer;\r
a3bcde70
HT
1258\r
1259 if (This == NULL || DestIp == NULL || DestPort == NULL) {\r
1260 return EFI_INVALID_PARAMETER;\r
1261 }\r
1262\r
1263 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
1264 Mode = Private->PxeBc.Mode;\r
1265 IsDone = FALSE;\r
1266 IsMatched = FALSE;\r
1267 Udp4Rx = NULL;\r
1268 Udp6Rx = NULL;\r
1269\r
1270 if (((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT) != 0 && DestPort == NULL) ||\r
1271 ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP) != 0 && SrcIp == NULL) ||\r
1272 ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT) != 0 && SrcPort == NULL)) {\r
1273 return EFI_INVALID_PARAMETER;\r
1274 }\r
1275\r
1276 if ((HeaderSize != NULL && *HeaderSize == 0) || (HeaderSize != NULL && HeaderPtr == NULL)) {\r
1277 return EFI_INVALID_PARAMETER;\r
1278 }\r
1279\r
1280 if ((BufferSize == NULL) || (BufferPtr == NULL)) {\r
1281 return EFI_INVALID_PARAMETER;\r
1282 }\r
1283\r
1284 if (!Mode->Started) {\r
1285 return EFI_NOT_STARTED;\r
1286 }\r
1287\r
1288 ZeroMem (&Udp6Token, sizeof (EFI_UDP6_COMPLETION_TOKEN));\r
1289 ZeroMem (&Udp4Token, sizeof (EFI_UDP4_COMPLETION_TOKEN));\r
1290\r
1291 if (Mode->UsingIpv6) {\r
1292 Status = gBS->CreateEvent (\r
1293 EVT_NOTIFY_SIGNAL,\r
1294 TPL_NOTIFY,\r
1295 PxeBcCommonNotify,\r
1296 &IsDone,\r
1297 &Udp6Token.Event\r
1298 );\r
1299 if (EFI_ERROR (Status)) {\r
1300 return EFI_OUT_OF_RESOURCES;\r
1301 }\r
1302 } else {\r
1303 Status = gBS->CreateEvent (\r
1304 EVT_NOTIFY_SIGNAL,\r
1305 TPL_NOTIFY,\r
1306 PxeBcCommonNotify,\r
1307 &IsDone,\r
1308 &Udp4Token.Event\r
1309 );\r
1310 if (EFI_ERROR (Status)) {\r
1311 return EFI_OUT_OF_RESOURCES;\r
1312 }\r
1313 }\r
1314\r
1315 //\r
1316 // Start a timer as timeout event for this blocking API.\r
1317 //\r
1318 gBS->SetTimer (Private->UdpTimeOutEvent, TimerRelative, PXEBC_UDP_TIMEOUT);\r
1319 Mode->IcmpErrorReceived = FALSE;\r
1320\r
1321 //\r
1322 // Read packet by Udp4Read/Udp6Read until matched or timeout.\r
1323 //\r
1324 while (!IsMatched && !EFI_ERROR (Status)) {\r
1325 if (Mode->UsingIpv6) {\r
1326 Status = PxeBcUdp6Read (\r
1327 Private->Udp6Read,\r
1328 &Udp6Token,\r
1329 Mode,\r
1330 Private->UdpTimeOutEvent,\r
1331 OpFlags,\r
1332 &IsDone,\r
1333 &IsMatched,\r
1334 DestIp,\r
1335 DestPort,\r
1336 SrcIp,\r
1337 SrcPort\r
1338 );\r
1339 } else {\r
1340 Status = PxeBcUdp4Read (\r
1341 Private->Udp4Read,\r
1342 &Udp4Token,\r
1343 Mode,\r
1344 Private->UdpTimeOutEvent,\r
1345 OpFlags,\r
1346 &IsDone,\r
1347 &IsMatched,\r
1348 DestIp,\r
1349 DestPort,\r
1350 SrcIp,\r
1351 SrcPort\r
1352 );\r
1353 }\r
1354 }\r
1355\r
1356 if (Status == EFI_ICMP_ERROR ||\r
1357 Status == EFI_NETWORK_UNREACHABLE ||\r
1358 Status == EFI_HOST_UNREACHABLE ||\r
1359 Status == EFI_PROTOCOL_UNREACHABLE ||\r
1360 Status == EFI_PORT_UNREACHABLE) {\r
1361 //\r
1362 // Get different return status for icmp error from Udp, refers to UEFI spec.\r
1363 //\r
1364 Mode->IcmpErrorReceived = TRUE;\r
1365 }\r
1366 gBS->SetTimer (Private->UdpTimeOutEvent, TimerCancel, 0);\r
1367\r
1368 if (IsMatched) {\r
1369 //\r
1370 // Copy the rececived packet to user if matched by filter.\r
1371 //\r
a3bcde70
HT
1372 if (Mode->UsingIpv6) {\r
1373 Udp6Rx = Udp6Token.Packet.RxData;\r
1374 ASSERT (Udp6Rx != NULL);\r
eb2710af 1375\r
1376 HeaderLen = 0;\r
a3bcde70 1377 if (HeaderSize != NULL) {\r
eb2710af 1378 HeaderLen = MIN (*HeaderSize, Udp6Rx->DataLength);\r
a3bcde70 1379 }\r
eb2710af 1380\r
1381 if (Udp6Rx->DataLength - HeaderLen > *BufferSize) {\r
a3bcde70
HT
1382 Status = EFI_BUFFER_TOO_SMALL;\r
1383 } else {\r
d40002ba 1384 if (HeaderSize != NULL) {\r
1385 *HeaderSize = HeaderLen;\r
1386 }\r
eb2710af 1387 *BufferSize = Udp6Rx->DataLength - HeaderLen;\r
1388\r
1389 HeaderCopiedLen = 0;\r
1390 BufferCopiedLen = 0;\r
1391 for (FragmentIndex = 0; FragmentIndex < Udp6Rx->FragmentCount; FragmentIndex++) {\r
1392 FragmentLength = Udp6Rx->FragmentTable[FragmentIndex].FragmentLength;\r
1393 FragmentBuffer = Udp6Rx->FragmentTable[FragmentIndex].FragmentBuffer;\r
1394 if (HeaderCopiedLen + FragmentLength < HeaderLen) {\r
1395 //\r
1396 // Copy the header part of received data.\r
1397 //\r
1398 CopyMem ((UINT8 *) HeaderPtr + HeaderCopiedLen, FragmentBuffer, FragmentLength);\r
1399 HeaderCopiedLen += FragmentLength;\r
1400 } else if (HeaderCopiedLen < HeaderLen) {\r
1401 //\r
1402 // Copy the header part of received data.\r
1403 //\r
1404 CopiedLen = HeaderLen - HeaderCopiedLen;\r
1405 CopyMem ((UINT8 *) HeaderPtr + HeaderCopiedLen, FragmentBuffer, CopiedLen);\r
1406 HeaderCopiedLen += CopiedLen;\r
1407\r
1408 //\r
1409 // Copy the other part of received data.\r
1410 //\r
1411 CopyMem ((UINT8 *) BufferPtr + BufferCopiedLen, FragmentBuffer + CopiedLen, FragmentLength - CopiedLen);\r
1412 BufferCopiedLen += (FragmentLength - CopiedLen);\r
1413 } else {\r
1414 //\r
1415 // Copy the other part of received data.\r
1416 //\r
1417 CopyMem ((UINT8 *) BufferPtr + BufferCopiedLen, FragmentBuffer, FragmentLength);\r
1418 BufferCopiedLen += FragmentLength;\r
1419 }\r
1420 }\r
a3bcde70
HT
1421 }\r
1422 //\r
1423 // Recycle the receiving buffer after copy to user.\r
1424 //\r
1425 gBS->SignalEvent (Udp6Rx->RecycleSignal);\r
1426 } else {\r
1427 Udp4Rx = Udp4Token.Packet.RxData;\r
1428 ASSERT (Udp4Rx != NULL);\r
eb2710af 1429\r
1430 HeaderLen = 0;\r
a3bcde70 1431 if (HeaderSize != NULL) {\r
eb2710af 1432 HeaderLen = MIN (*HeaderSize, Udp4Rx->DataLength);\r
a3bcde70 1433 }\r
eb2710af 1434\r
1435 if (Udp4Rx->DataLength - HeaderLen > *BufferSize) {\r
a3bcde70
HT
1436 Status = EFI_BUFFER_TOO_SMALL;\r
1437 } else {\r
d40002ba 1438 if (HeaderSize != NULL) {\r
1439 *HeaderSize = HeaderLen;\r
1440 }\r
eb2710af 1441 *BufferSize = Udp4Rx->DataLength - HeaderLen;\r
1442\r
1443 HeaderCopiedLen = 0;\r
1444 BufferCopiedLen = 0;\r
1445 for (FragmentIndex = 0; FragmentIndex < Udp4Rx->FragmentCount; FragmentIndex++) {\r
1446 FragmentLength = Udp4Rx->FragmentTable[FragmentIndex].FragmentLength;\r
1447 FragmentBuffer = Udp4Rx->FragmentTable[FragmentIndex].FragmentBuffer;\r
1448 if (HeaderCopiedLen + FragmentLength < HeaderLen) {\r
1449 //\r
1450 // Copy the header part of received data.\r
1451 //\r
1452 CopyMem ((UINT8 *) HeaderPtr + HeaderCopiedLen, FragmentBuffer, FragmentLength);\r
1453 HeaderCopiedLen += FragmentLength;\r
1454 } else if (HeaderCopiedLen < HeaderLen) {\r
1455 //\r
1456 // Copy the header part of received data.\r
1457 //\r
1458 CopiedLen = HeaderLen - HeaderCopiedLen;\r
1459 CopyMem ((UINT8 *) HeaderPtr + HeaderCopiedLen, FragmentBuffer, CopiedLen);\r
1460 HeaderCopiedLen += CopiedLen;\r
1461\r
1462 //\r
1463 // Copy the other part of received data.\r
1464 //\r
1465 CopyMem ((UINT8 *) BufferPtr + BufferCopiedLen, FragmentBuffer + CopiedLen, FragmentLength - CopiedLen);\r
1466 BufferCopiedLen += (FragmentLength - CopiedLen);\r
1467 } else {\r
1468 //\r
1469 // Copy the other part of received data.\r
1470 //\r
1471 CopyMem ((UINT8 *) BufferPtr + BufferCopiedLen, FragmentBuffer, FragmentLength);\r
1472 BufferCopiedLen += FragmentLength;\r
1473 }\r
1474 }\r
a3bcde70
HT
1475 }\r
1476 //\r
1477 // Recycle the receiving buffer after copy to user.\r
1478 //\r
1479 gBS->SignalEvent (Udp4Rx->RecycleSignal);\r
1480 }\r
1481 }\r
1482\r
1483 if (Mode->UsingIpv6) {\r
1484 Private->Udp6Read->Cancel (Private->Udp6Read, &Udp6Token);\r
1485 gBS->CloseEvent (Udp6Token.Event);\r
1486 } else {\r
1487 Private->Udp4Read->Cancel (Private->Udp4Read, &Udp4Token);\r
1488 gBS->CloseEvent (Udp4Token.Event);\r
1489 }\r
1490\r
1491 return Status;\r
1492}\r
1493\r
1494\r
1495/**\r
1496 Updates the IP receive filters of a network device and enables software filtering.\r
1497\r
1498 The NewFilter field is used to modify the network device's current IP receive\r
1499 filter settings and to enable a software filter. This function updates the IpFilter\r
1500 field of the EFI_PXE_BASE_CODE_MODE structure with the contents of NewIpFilter.\r
1501 The software filter is used when the USE_FILTER in OpFlags is set to UdpRead().\r
1502 The current hardware filter remains in effect no matter what the settings of OpFlags.\r
1503 This is so that the meaning of ANY_DEST_IP set in OpFlags to UdpRead() is from those\r
1504 packets whose reception is enabled in hardware-physical NIC address (unicast),\r
1505 broadcast address, logical address or addresses (multicast), or all (promiscuous).\r
1506 UdpRead() does not modify the IP filter settings.\r
1507 Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP receive\r
1508 filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.\r
1509 If an application or driver wishes to preserve the IP receive filter settings,\r
1510 it will have to preserve the IP receive filter settings before these calls, and\r
1511 use SetIpFilter() to restore them after the calls. If incompatible filtering is\r
1512 requested (for example, PROMISCUOUS with anything else), or if the device does not\r
1513 support a requested filter setting and it cannot be accommodated in software\r
1514 (for example, PROMISCUOUS not supported), EFI_INVALID_PARAMETER will be returned.\r
1515 The IPlist field is used to enable IPs other than the StationIP. They may be\r
1516 multicast or unicast. If IPcnt is set as well as EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP,\r
1517 then both the StationIP and the IPs from the IPlist will be used.\r
1518\r
1519 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
1520 @param[in] NewFilter Pointer to the new set of IP receive filters.\r
1521\r
1522 @retval EFI_SUCCESS The IP receive filter settings were updated.\r
1523 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.\r
1524 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
1525\r
1526**/\r
1527EFI_STATUS\r
1528EFIAPI\r
1529EfiPxeBcSetIpFilter (\r
1530 IN EFI_PXE_BASE_CODE_PROTOCOL *This,\r
1531 IN EFI_PXE_BASE_CODE_IP_FILTER *NewFilter\r
1532 )\r
1533{\r
1534 EFI_STATUS Status;\r
1535 PXEBC_PRIVATE_DATA *Private;\r
1536 EFI_PXE_BASE_CODE_MODE *Mode;\r
18127352 1537 EFI_UDP4_CONFIG_DATA *Udp4Cfg;\r
1538 EFI_UDP6_CONFIG_DATA *Udp6Cfg;\r
a3bcde70
HT
1539 UINTN Index;\r
1540 BOOLEAN NeedPromiscuous;\r
18127352 1541 BOOLEAN AcceptPromiscuous;\r
1542 BOOLEAN AcceptBroadcast;\r
1543 BOOLEAN MultiCastUpdate;\r
a3bcde70
HT
1544\r
1545 if (This == NULL || NewFilter == NULL) {\r
1546 return EFI_INVALID_PARAMETER;\r
1547 }\r
1548\r
1549 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
1550 Mode = Private->PxeBc.Mode;\r
1551 Status = EFI_SUCCESS;\r
1552 NeedPromiscuous = FALSE;\r
1553\r
1554 if (!Mode->Started) {\r
1555 return EFI_NOT_STARTED;\r
1556 }\r
1557\r
1558 for (Index = 0; Index < NewFilter->IpCnt; Index++) {\r
1559 ASSERT (Index < EFI_PXE_BASE_CODE_MAX_IPCNT);\r
1560 if (!Mode->UsingIpv6 &&\r
1561 IP4_IS_LOCAL_BROADCAST (EFI_IP4 (NewFilter->IpList[Index].v4))) {\r
1562 //\r
1563 // IPv4 broadcast address should not be in IP filter.\r
1564 //\r
1565 return EFI_INVALID_PARAMETER;\r
1566 }\r
1567 if ((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP) != 0 &&\r
1568 (NetIp4IsUnicast (EFI_IP4 (NewFilter->IpList[Index].v4), 0) ||\r
1569 NetIp6IsValidUnicast (&NewFilter->IpList[Index].v6))) {\r
1570 //\r
1571 // If EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP is set and IPv4/IPv6 address\r
1572 // is in IpList, promiscuous mode is needed.\r
1573 //\r
1574 NeedPromiscuous = TRUE;\r
1575 }\r
1576 }\r
1577\r
18127352 1578 AcceptPromiscuous = FALSE;\r
1579 AcceptBroadcast = FALSE;\r
1580 MultiCastUpdate = FALSE;\r
a3bcde70
HT
1581\r
1582 if (NeedPromiscuous ||\r
1583 (NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS) != 0 ||\r
1584 (NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS_MULTICAST) != 0) {\r
1585 //\r
1586 // Configure UDPv4/UDPv6 as promiscuous mode to receive all packets.\r
1587 //\r
18127352 1588 AcceptPromiscuous = TRUE;\r
a3bcde70
HT
1589 } else if ((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_BROADCAST) != 0) {\r
1590 //\r
1591 // Configure UDPv4 to receive all broadcast packets.\r
1592 //\r
18127352 1593 AcceptBroadcast = TRUE;\r
a3bcde70
HT
1594 }\r
1595\r
1596 //\r
18127352 1597 // In multicast condition when Promiscuous FALSE and IpCnt no-zero.\r
1598 // Here check if there is any update of the multicast ip address. If yes,\r
1599 // we need leave the old multicast group (by Config UDP instance to NULL),\r
1600 // and join the new multicast group.\r
a3bcde70 1601 //\r
18127352 1602 if (!AcceptPromiscuous) {\r
1603 if ((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP) != 0) {\r
1604 if (Mode->IpFilter.IpCnt != NewFilter->IpCnt) {\r
1605 MultiCastUpdate = TRUE;\r
1606 } else if (CompareMem (Mode->IpFilter.IpList, NewFilter->IpList, NewFilter->IpCnt * sizeof (EFI_IP_ADDRESS)) != 0 ) {\r
1607 MultiCastUpdate = TRUE;\r
1608 }\r
1609 }\r
a3bcde70
HT
1610 }\r
1611\r
18127352 1612 if (!Mode->UsingIpv6) {\r
1613 //\r
1614 // Check whether we need reconfigure the UDP4 instance.\r
1615 //\r
1616 Udp4Cfg = &Private->Udp4CfgData;\r
1617 if ((AcceptPromiscuous != Udp4Cfg->AcceptPromiscuous) ||\r
1618 (AcceptBroadcast != Udp4Cfg->AcceptBroadcast) || MultiCastUpdate) {\r
1619 //\r
1620 // Clear the UDP4 instance configuration, all joined groups will be left\r
1621 // during the operation.\r
a3bcde70 1622 //\r
18127352 1623 Private->Udp4Read->Configure (Private->Udp4Read, NULL);\r
1624 \r
a3bcde70 1625 //\r
18127352 1626 // Configure the UDP instance with the new configuration.\r
1627 //\r
1628 Udp4Cfg->AcceptPromiscuous = AcceptPromiscuous;\r
1629 Udp4Cfg->AcceptBroadcast = AcceptBroadcast;\r
1630 Status = Private->Udp4Read->Configure (Private->Udp4Read, Udp4Cfg);\r
1631 if (EFI_ERROR (Status)) {\r
1632 return Status;\r
1633 }\r
1634 \r
1635 //\r
1636 // In not Promiscuous mode, need to join the new multicast group.\r
1637 //\r
1638 if (!AcceptPromiscuous) {\r
1639 for (Index = 0; Index < NewFilter->IpCnt; ++Index) {\r
1640 if (IP4_IS_MULTICAST (EFI_NTOHL (NewFilter->IpList[Index].v4))) {\r
1641 //\r
1642 // Join the mutilcast group.\r
1643 //\r
1644 Status = Private->Udp4Read->Groups (Private->Udp4Read, TRUE, &NewFilter->IpList[Index].v4);\r
1645 if (EFI_ERROR (Status)) {\r
1646 return Status;\r
1647 }\r
a3bcde70
HT
1648 }\r
1649 }\r
18127352 1650 }\r
1651 }\r
1652 } else {\r
1653 //\r
1654 // Check whether we need reconfigure the UDP6 instance.\r
1655 //\r
1656 Udp6Cfg = &Private->Udp6CfgData;\r
1657 if ((AcceptPromiscuous != Udp6Cfg->AcceptPromiscuous) || MultiCastUpdate) {\r
1658 //\r
1659 // Clear the UDP6 instance configuration, all joined groups will be left\r
1660 // during the operation.\r
1661 //\r
1662 Private->Udp6Read->Configure (Private->Udp6Read, NULL);\r
1663 \r
1664 //\r
1665 // Configure the UDP instance with the new configuration.\r
1666 //\r
1667 Udp6Cfg->AcceptPromiscuous = AcceptPromiscuous;\r
1668 Status = Private->Udp6Read->Configure (Private->Udp6Read, Udp6Cfg);\r
1669 if (EFI_ERROR (Status)) {\r
1670 return Status;\r
1671 }\r
1672 \r
1673 //\r
1674 // In not Promiscuous mode, need to join the new multicast group.\r
1675 //\r
1676 if (!AcceptPromiscuous) {\r
1677 for (Index = 0; Index < NewFilter->IpCnt; ++Index) {\r
1678 if (IP6_IS_MULTICAST (&NewFilter->IpList[Index].v6)) {\r
1679 //\r
1680 // Join the mutilcast group.\r
1681 //\r
1682 Status = Private->Udp6Read->Groups (Private->Udp6Read, TRUE, &NewFilter->IpList[Index].v6);\r
1683 if (EFI_ERROR (Status)) {\r
1684 return Status;\r
1685 }\r
a3bcde70
HT
1686 }\r
1687 }\r
1688 }\r
1689 }\r
1690 }\r
1691\r
1692 //\r
1693 // Save the new IP filter into mode data.\r
1694 //\r
1695 CopyMem (&Mode->IpFilter, NewFilter, sizeof (Mode->IpFilter));\r
1696\r
a3bcde70
HT
1697 return Status;\r
1698}\r
1699\r
1700\r
1701/**\r
1702 Uses the ARP protocol to resolve a MAC address. It is not supported for IPv6.\r
1703\r
1704 This function uses the ARP protocol to resolve a MAC address. The IP address specified\r
1705 by IpAddr is used to resolve a MAC address. If the ARP protocol succeeds in resolving\r
1706 the specified address, then the ArpCacheEntries and ArpCache fields of the mode data\r
1707 are updated, and EFI_SUCCESS is returned. If MacAddr is not NULL, the resolved\r
1708 MAC address is placed there as well. If the PXE Base Code protocol is in the\r
1709 stopped state, then EFI_NOT_STARTED is returned. If the ARP protocol encounters\r
1710 a timeout condition while attempting to resolve an address, then EFI_TIMEOUT is\r
1711 returned. If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,\r
1712 then EFI_ABORTED is returned.\r
1713\r
1714 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
1715 @param[in] IpAddr Pointer to the IP address that is used to resolve a MAC address.\r
1716 @param[in] MacAddr If not NULL, a pointer to the MAC address that was resolved with the\r
1717 ARP protocol.\r
1718\r
1719 @retval EFI_SUCCESS The IP or MAC address was resolved.\r
1720 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.\r
1721 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
1722 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.\r
1723 @retval EFI_ICMP_ERROR An error occur with the ICMP packet message.\r
1724\r
1725**/\r
1726EFI_STATUS\r
1727EFIAPI\r
1728EfiPxeBcArp (\r
1729 IN EFI_PXE_BASE_CODE_PROTOCOL *This,\r
1730 IN EFI_IP_ADDRESS *IpAddr,\r
1731 IN EFI_MAC_ADDRESS *MacAddr OPTIONAL\r
1732 )\r
1733{\r
1734 PXEBC_PRIVATE_DATA *Private;\r
1735 EFI_PXE_BASE_CODE_MODE *Mode;\r
1736 EFI_EVENT ResolvedEvent;\r
1737 EFI_STATUS Status;\r
1738 EFI_MAC_ADDRESS TempMac;\r
1739 EFI_MAC_ADDRESS ZeroMac;\r
1740 BOOLEAN IsResolved;\r
1741\r
1742 if (This == NULL || IpAddr == NULL) {\r
1743 return EFI_INVALID_PARAMETER;\r
1744 }\r
1745\r
1746 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
1747 Mode = Private->PxeBc.Mode;\r
1748 ResolvedEvent = NULL;\r
1749 Status = EFI_SUCCESS;\r
1750 IsResolved = FALSE;\r
1751\r
1752 if (!Mode->Started) {\r
1753 return EFI_NOT_STARTED;\r
1754 }\r
1755\r
1756 if (Mode->UsingIpv6) {\r
1757 return EFI_UNSUPPORTED;\r
1758 }\r
1759\r
1760 //\r
1761 // Station address should be ready before do arp.\r
1762 //\r
1763 if (!Private->IsAddressOk) {\r
1764 return EFI_INVALID_PARAMETER;\r
1765 }\r
1766\r
1767 Mode->IcmpErrorReceived = FALSE;\r
1768 ZeroMem (&TempMac, sizeof (EFI_MAC_ADDRESS));\r
1769 ZeroMem (&ZeroMac, sizeof (EFI_MAC_ADDRESS));\r
1770\r
1771 if (!Mode->AutoArp) {\r
1772 //\r
1773 // If AutoArp is FALSE, only search in the current Arp cache.\r
1774 //\r
1775 PxeBcArpCacheUpdate (NULL, Private);\r
1776 if (!PxeBcCheckArpCache (Mode, &IpAddr->v4, &TempMac)) {\r
1777 Status = EFI_DEVICE_ERROR;\r
1778 goto ON_EXIT;\r
1779 }\r
1780 } else {\r
1781 Status = gBS->CreateEvent (\r
1782 EVT_NOTIFY_SIGNAL,\r
1783 TPL_NOTIFY,\r
1784 PxeBcCommonNotify,\r
1785 &IsResolved,\r
1786 &ResolvedEvent\r
1787 );\r
1788 if (EFI_ERROR (Status)) {\r
1789 goto ON_EXIT;\r
1790 }\r
1791\r
1792 //\r
1793 // If AutoArp is TRUE, try to send Arp request on initiative.\r
1794 //\r
1795 Status = Private->Arp->Request (Private->Arp, &IpAddr->v4, ResolvedEvent, &TempMac);\r
1796 if (EFI_ERROR (Status) && Status != EFI_NOT_READY) {\r
1797 goto ON_EXIT;\r
1798 }\r
1799\r
1800 while (!IsResolved) {\r
1801 if (CompareMem (&TempMac, &ZeroMac, sizeof (EFI_MAC_ADDRESS)) != 0) {\r
1802 break;\r
1803 }\r
1804 }\r
1805 if (CompareMem (&TempMac, &ZeroMac, sizeof (EFI_MAC_ADDRESS)) != 0) {\r
1806 Status = EFI_SUCCESS;\r
1807 } else {\r
1808 Status = EFI_TIMEOUT;\r
1809 }\r
1810 }\r
1811\r
1812 //\r
1813 // Copy the Mac address to user if needed.\r
1814 //\r
1815 if (MacAddr != NULL && !EFI_ERROR (Status)) {\r
1816 CopyMem (MacAddr, &TempMac, sizeof (EFI_MAC_ADDRESS));\r
1817 }\r
1818\r
1819ON_EXIT:\r
1820 if (ResolvedEvent != NULL) {\r
1821 gBS->CloseEvent (ResolvedEvent);\r
1822 }\r
1823 return Status;\r
1824}\r
1825\r
1826\r
1827/**\r
1828 Updates the parameters that affect the operation of the PXE Base Code Protocol.\r
1829\r
1830 This function sets parameters that affect the operation of the PXE Base Code Protocol.\r
1831 The parameter specified by NewAutoArp is used to control the generation of ARP\r
1832 protocol packets. If NewAutoArp is TRUE, then ARP Protocol packets will be generated\r
1833 as required by the PXE Base Code Protocol. If NewAutoArp is FALSE, then no ARP\r
1834 Protocol packets will be generated. In this case, the only mappings that are\r
1835 available are those stored in the ArpCache of the EFI_PXE_BASE_CODE_MODE structure.\r
1836 If there are not enough mappings in the ArpCache to perform a PXE Base Code Protocol\r
1837 service, then the service will fail. This function updates the AutoArp field of\r
1838 the EFI_PXE_BASE_CODE_MODE structure to NewAutoArp.\r
1839 The SetParameters() call must be invoked after a Callback Protocol is installed\r
1840 to enable the use of callbacks.\r
1841\r
1842 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
1843 @param[in] NewAutoArp If not NULL, a pointer to a value that specifies whether to replace the\r
1844 current value of AutoARP.\r
1845 @param[in] NewSendGUID If not NULL, a pointer to a value that specifies whether to replace the\r
1846 current value of SendGUID.\r
1847 @param[in] NewTTL If not NULL, a pointer to be used in place of the current value of TTL,\r
1848 the "time to live" field of the IP header.\r
1849 @param[in] NewToS If not NULL, a pointer to be used in place of the current value of ToS,\r
1850 the "type of service" field of the IP header.\r
1851 @param[in] NewMakeCallback If not NULL, a pointer to a value that specifies whether to replace the\r
1852 current value of the MakeCallback field of the Mode structure.\r
1853\r
1854 @retval EFI_SUCCESS The new parameters values were updated.\r
1855 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.\r
1856 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
1857\r
1858**/\r
1859EFI_STATUS\r
1860EFIAPI\r
1861EfiPxeBcSetParameters (\r
1862 IN EFI_PXE_BASE_CODE_PROTOCOL *This,\r
1863 IN BOOLEAN *NewAutoArp OPTIONAL,\r
1864 IN BOOLEAN *NewSendGUID OPTIONAL,\r
1865 IN UINT8 *NewTTL OPTIONAL,\r
1866 IN UINT8 *NewToS OPTIONAL,\r
1867 IN BOOLEAN *NewMakeCallback OPTIONAL\r
1868 )\r
1869{\r
1870 PXEBC_PRIVATE_DATA *Private;\r
1871 EFI_PXE_BASE_CODE_MODE *Mode;\r
1872 EFI_GUID SystemGuid;\r
1873 EFI_STATUS Status;\r
1874\r
1875 if (This == NULL) {\r
1876 return EFI_INVALID_PARAMETER;\r
1877 }\r
1878\r
1879 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
1880 Mode = Private->PxeBc.Mode;\r
1881\r
1882 if (!Mode->Started) {\r
1883 return EFI_NOT_STARTED;\r
1884 }\r
1885\r
1886 if (NewMakeCallback != NULL) {\r
1887 if (*NewMakeCallback) {\r
1888 //\r
1889 // Update the previous PxeBcCallback protocol.\r
1890 //\r
1891 Status = gBS->HandleProtocol (\r
1892 Private->Controller,\r
1893 &gEfiPxeBaseCodeCallbackProtocolGuid,\r
1894 (VOID **) &Private->PxeBcCallback\r
1895 );\r
1896\r
1897 if (EFI_ERROR (Status) || (Private->PxeBcCallback->Callback == NULL)) {\r
1898 return EFI_INVALID_PARAMETER;\r
1899 }\r
1900 } else {\r
1901 Private->PxeBcCallback = NULL;\r
1902 }\r
1903 Mode->MakeCallbacks = *NewMakeCallback;\r
1904 }\r
1905\r
1906 if (NewSendGUID != NULL) {\r
19ddbb25 1907 if (*NewSendGUID && EFI_ERROR (NetLibGetSystemGuid (&SystemGuid))) {\r
a3bcde70
HT
1908 return EFI_INVALID_PARAMETER;\r
1909 }\r
1910 Mode->SendGUID = *NewSendGUID;\r
1911 }\r
1912\r
1913 if (NewAutoArp != NULL) {\r
1914 Mode->AutoArp = *NewAutoArp;\r
1915 }\r
1916\r
1917 if (NewTTL != NULL) {\r
1918 Mode->TTL = *NewTTL;\r
1919 }\r
1920\r
1921 if (NewToS != NULL) {\r
1922 Mode->ToS = *NewToS;\r
1923 }\r
1924\r
1925 return EFI_SUCCESS;\r
1926}\r
1927\r
1928\r
1929/**\r
1930 Updates the station IP address and/or subnet mask values of a network device.\r
1931\r
1932 This function updates the station IP address and/or subnet mask values of a network\r
1933 device. The NewStationIp field is used to modify the network device's current IP address.\r
1934 If NewStationIP is NULL, then the current IP address will not be modified. Otherwise,\r
1935 this function updates the StationIp field of the EFI_PXE_BASE_CODE_MODE structure\r
1936 with NewStationIp. The NewSubnetMask field is used to modify the network device's current subnet\r
1937 mask. If NewSubnetMask is NULL, then the current subnet mask will not be modified.\r
1938 Otherwise, this function updates the SubnetMask field of the EFI_PXE_BASE_CODE_MODE\r
1939 structure with NewSubnetMask.\r
1940\r
1941 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
1942 @param[in] NewStationIp Pointer to the new IP address to be used by the network device.\r
1943 @param[in] NewSubnetMask Pointer to the new subnet mask to be used by the network device.\r
1944\r
1945 @retval EFI_SUCCESS The new station IP address and/or subnet mask were updated.\r
1946 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.\r
1947 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
1948\r
1949**/\r
1950EFI_STATUS\r
1951EFIAPI\r
1952EfiPxeBcSetStationIP (\r
1953 IN EFI_PXE_BASE_CODE_PROTOCOL *This,\r
1954 IN EFI_IP_ADDRESS *NewStationIp OPTIONAL,\r
1955 IN EFI_IP_ADDRESS *NewSubnetMask OPTIONAL\r
1956 )\r
1957{\r
1958 EFI_STATUS Status;\r
1959 PXEBC_PRIVATE_DATA *Private;\r
1960 EFI_PXE_BASE_CODE_MODE *Mode;\r
1961 EFI_ARP_CONFIG_DATA ArpConfigData;\r
1962\r
1963 if (This == NULL) {\r
1964 return EFI_INVALID_PARAMETER;\r
1965 }\r
1966\r
1967 if (NewStationIp != NULL &&\r
1968 (!NetIp4IsUnicast (NTOHL (NewStationIp->Addr[0]), 0) &&\r
1969 !NetIp6IsValidUnicast (&NewStationIp->v6))) {\r
1970 return EFI_INVALID_PARAMETER;\r
1971 }\r
1972\r
1973 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
1974 Mode = Private->PxeBc.Mode;\r
1975 Status = EFI_SUCCESS;\r
1976\r
1977 if (!Mode->UsingIpv6 &&\r
1978 NewSubnetMask != NULL &&\r
1979 !IP4_IS_VALID_NETMASK (NTOHL (NewSubnetMask->Addr[0]))) {\r
1980 return EFI_INVALID_PARAMETER;\r
1981 }\r
1982\r
1983 if (!Mode->Started) {\r
1984 return EFI_NOT_STARTED;\r
1985 }\r
1986\r
1987 if (Mode->UsingIpv6 && NewStationIp != NULL) {\r
1988 //\r
1989 // Set the IPv6 address by Ip6Config protocol.\r
1990 //\r
1991 Status = PxeBcRegisterIp6Address (Private, &NewStationIp->v6);\r
1992 if (EFI_ERROR (Status)) {\r
1993 goto ON_EXIT;\r
1994 }\r
1995 } else if (!Mode->UsingIpv6 && NewStationIp != NULL) {\r
1996 //\r
1997 // Configure the corresponding ARP with the IPv4 address.\r
1998 //\r
1999 ZeroMem (&ArpConfigData, sizeof (EFI_ARP_CONFIG_DATA));\r
2000\r
2001 ArpConfigData.SwAddressType = 0x0800;\r
2002 ArpConfigData.SwAddressLength = (UINT8) sizeof (EFI_IPv4_ADDRESS);\r
2003 ArpConfigData.StationAddress = &NewStationIp->v4;\r
2004\r
2005 Private->Arp->Configure (Private->Arp, NULL);\r
2006 Private->Arp->Configure (Private->Arp, &ArpConfigData);\r
2007\r
2008 if (NewSubnetMask != NULL) {\r
2009 Mode->RouteTableEntries = 1;\r
2010 Mode->RouteTable[0].IpAddr.Addr[0] = NewStationIp->Addr[0] & NewSubnetMask->Addr[0];\r
2011 Mode->RouteTable[0].SubnetMask.Addr[0] = NewSubnetMask->Addr[0];\r
2012 Mode->RouteTable[0].GwAddr.Addr[0] = 0;\r
2013 }\r
2014\r
2015 Private->IsAddressOk = TRUE;\r
2016 }\r
2017\r
2018 if (NewStationIp != NULL) {\r
2019 CopyMem (&Mode->StationIp, NewStationIp, sizeof (EFI_IP_ADDRESS));\r
2020 CopyMem (&Private->StationIp, NewStationIp, sizeof (EFI_IP_ADDRESS));\r
2021 }\r
2022\r
2023 if (!Mode->UsingIpv6 && NewSubnetMask != NULL) {\r
2024 CopyMem (&Mode->SubnetMask, NewSubnetMask, sizeof (EFI_IP_ADDRESS));\r
2025 CopyMem (&Private->SubnetMask ,NewSubnetMask, sizeof (EFI_IP_ADDRESS));\r
2026 }\r
2027\r
0e7f6f50 2028 Status = PxeBcFlushStationIp (Private, NewStationIp, NewSubnetMask);\r
a3bcde70
HT
2029ON_EXIT:\r
2030 return Status;\r
2031}\r
2032\r
2033\r
2034/**\r
2035 Updates the contents of the cached DHCP and Discover packets.\r
2036\r
2037 The pointers to the new packets are used to update the contents of the cached\r
2038 packets in the EFI_PXE_BASE_CODE_MODE structure.\r
2039\r
2040 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
2041 @param[in] NewDhcpDiscoverValid Pointer to a value that will replace the current\r
2042 DhcpDiscoverValid field.\r
2043 @param[in] NewDhcpAckReceived Pointer to a value that will replace the current\r
2044 DhcpAckReceived field.\r
2045 @param[in] NewProxyOfferReceived Pointer to a value that will replace the current\r
2046 ProxyOfferReceived field.\r
2047 @param[in] NewPxeDiscoverValid Pointer to a value that will replace the current\r
2048 ProxyOfferReceived field.\r
2049 @param[in] NewPxeReplyReceived Pointer to a value that will replace the current\r
2050 PxeReplyReceived field.\r
2051 @param[in] NewPxeBisReplyReceived Pointer to a value that will replace the current\r
2052 PxeBisReplyReceived field.\r
2053 @param[in] NewDhcpDiscover Pointer to the new cached DHCP Discover packet contents.\r
2054 @param[in] NewDhcpAck Pointer to the new cached DHCP Ack packet contents.\r
2055 @param[in] NewProxyOffer Pointer to the new cached Proxy Offer packet contents.\r
2056 @param[in] NewPxeDiscover Pointer to the new cached PXE Discover packet contents.\r
2057 @param[in] NewPxeReply Pointer to the new cached PXE Reply packet contents.\r
2058 @param[in] NewPxeBisReply Pointer to the new cached PXE BIS Reply packet contents.\r
2059\r
2060 @retval EFI_SUCCESS The cached packet contents were updated.\r
2061 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.\r
2062 @retval EFI_INVALID_PARAMETER This is NULL or does not point to a valid\r
2063 EFI_PXE_BASE_CODE_PROTOCOL structure.\r
2064\r
2065**/\r
2066EFI_STATUS\r
2067EFIAPI\r
2068EfiPxeBcSetPackets (\r
2069 IN EFI_PXE_BASE_CODE_PROTOCOL *This,\r
2070 IN BOOLEAN *NewDhcpDiscoverValid OPTIONAL,\r
2071 IN BOOLEAN *NewDhcpAckReceived OPTIONAL,\r
2072 IN BOOLEAN *NewProxyOfferReceived OPTIONAL,\r
2073 IN BOOLEAN *NewPxeDiscoverValid OPTIONAL,\r
2074 IN BOOLEAN *NewPxeReplyReceived OPTIONAL,\r
2075 IN BOOLEAN *NewPxeBisReplyReceived OPTIONAL,\r
2076 IN EFI_PXE_BASE_CODE_PACKET *NewDhcpDiscover OPTIONAL,\r
2077 IN EFI_PXE_BASE_CODE_PACKET *NewDhcpAck OPTIONAL,\r
2078 IN EFI_PXE_BASE_CODE_PACKET *NewProxyOffer OPTIONAL,\r
2079 IN EFI_PXE_BASE_CODE_PACKET *NewPxeDiscover OPTIONAL,\r
2080 IN EFI_PXE_BASE_CODE_PACKET *NewPxeReply OPTIONAL,\r
2081 IN EFI_PXE_BASE_CODE_PACKET *NewPxeBisReply OPTIONAL\r
2082 )\r
2083{\r
2084 PXEBC_PRIVATE_DATA *Private;\r
2085 EFI_PXE_BASE_CODE_MODE *Mode;\r
2086\r
2087 if (This == NULL) {\r
2088 return EFI_INVALID_PARAMETER;\r
2089 }\r
2090\r
2091 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
2092 Mode = Private->PxeBc.Mode;\r
2093\r
2094 if (!Mode->Started) {\r
2095 return EFI_NOT_STARTED;\r
2096 }\r
2097\r
2098 if (NewDhcpDiscoverValid != NULL) {\r
2099 Mode->DhcpDiscoverValid = *NewDhcpDiscoverValid;\r
2100 }\r
2101\r
2102 if (NewDhcpAckReceived != NULL) {\r
2103 Mode->DhcpAckReceived = *NewDhcpAckReceived;\r
2104 }\r
2105\r
2106 if (NewProxyOfferReceived != NULL) {\r
2107 Mode->ProxyOfferReceived = *NewProxyOfferReceived;\r
2108 }\r
2109\r
2110 if (NewPxeDiscoverValid != NULL) {\r
2111 Mode->PxeDiscoverValid = *NewPxeDiscoverValid;\r
2112 }\r
2113\r
2114 if (NewPxeReplyReceived != NULL) {\r
2115 Mode->PxeReplyReceived = *NewPxeReplyReceived;\r
2116 }\r
2117\r
2118 if (NewPxeBisReplyReceived != NULL) {\r
2119 Mode->PxeBisReplyReceived = *NewPxeBisReplyReceived;\r
2120 }\r
2121\r
2122 if (NewDhcpDiscover != NULL) {\r
2123 CopyMem (&Mode->DhcpDiscover, NewDhcpDiscover, sizeof (EFI_PXE_BASE_CODE_PACKET));\r
2124 }\r
2125\r
2126 if (NewDhcpAck != NULL) {\r
2127 CopyMem (&Mode->DhcpAck, NewDhcpAck, sizeof (EFI_PXE_BASE_CODE_PACKET));\r
2128 }\r
2129\r
2130 if (NewProxyOffer != NULL) {\r
2131 CopyMem (&Mode->ProxyOffer, NewProxyOffer, sizeof (EFI_PXE_BASE_CODE_PACKET));\r
2132 }\r
2133\r
2134 if (NewPxeDiscover != NULL) {\r
2135 CopyMem (&Mode->PxeDiscover, NewPxeDiscover, sizeof (EFI_PXE_BASE_CODE_PACKET));\r
2136 }\r
2137\r
2138 if (NewPxeReply != NULL) {\r
2139 CopyMem (&Mode->PxeReply, NewPxeReply, sizeof (EFI_PXE_BASE_CODE_PACKET));\r
2140 }\r
2141\r
2142 if (NewPxeBisReply != NULL) {\r
2143 CopyMem (&Mode->PxeBisReply, NewPxeBisReply, sizeof (EFI_PXE_BASE_CODE_PACKET));\r
2144 }\r
2145\r
2146 return EFI_SUCCESS;\r
2147}\r
2148\r
2149EFI_PXE_BASE_CODE_PROTOCOL gPxeBcProtocolTemplate = {\r
2150 EFI_PXE_BASE_CODE_PROTOCOL_REVISION,\r
2151 EfiPxeBcStart,\r
2152 EfiPxeBcStop,\r
2153 EfiPxeBcDhcp,\r
2154 EfiPxeBcDiscover,\r
2155 EfiPxeBcMtftp,\r
2156 EfiPxeBcUdpWrite,\r
2157 EfiPxeBcUdpRead,\r
2158 EfiPxeBcSetIpFilter,\r
2159 EfiPxeBcArp,\r
2160 EfiPxeBcSetParameters,\r
2161 EfiPxeBcSetStationIP,\r
2162 EfiPxeBcSetPackets,\r
2163 NULL\r
2164};\r
2165\r
2166\r
2167/**\r
2168 Callback function that is invoked when the PXE Base Code Protocol is about to transmit, has\r
2169 received, or is waiting to receive a packet.\r
2170\r
2171 This function is invoked when the PXE Base Code Protocol is about to transmit, has received,\r
2172 or is waiting to receive a packet. Parameters Function and Received specify the type of event.\r
2173 Parameters PacketLen and Packet specify the packet that generated the event. If these fields\r
2174 are zero and NULL respectively, then this is a status update callback. If the operation specified\r
2175 by Function is to continue, then CALLBACK_STATUS_CONTINUE should be returned. If the operation\r
2176 specified by Function should be aborted, then CALLBACK_STATUS_ABORT should be returned. Due to\r
2177 the polling nature of UEFI device drivers, a callback function should not execute for more than 5 ms.\r
2178 The SetParameters() function must be called after a Callback Protocol is installed to enable the\r
2179 use of callbacks.\r
2180\r
2181 @param[in] This Pointer to the EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL instance.\r
2182 @param[in] Function The PXE Base Code Protocol function that is waiting for an event.\r
2183 @param[in] Received TRUE if the callback is being invoked due to a receive event. FALSE if\r
2184 the callback is being invoked due to a transmit event.\r
2185 @param[in] PacketLength The length, in bytes, of Packet. This field will have a value of zero if\r
2186 this is a wait for receive event.\r
2187 @param[in] PacketPtr If Received is TRUE, a pointer to the packet that was just received;\r
2188 otherwise a pointer to the packet that is about to be transmitted.\r
2189\r
2190 @retval EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE If Function specifies a continue operation.\r
2191 @retval EFI_PXE_BASE_CODE_CALLBACK_STATUS_ABORT If Function specifies an abort operation.\r
2192\r
2193**/\r
2194EFI_PXE_BASE_CODE_CALLBACK_STATUS\r
2195EFIAPI\r
2196EfiPxeLoadFileCallback (\r
2197 IN EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL *This,\r
2198 IN EFI_PXE_BASE_CODE_FUNCTION Function,\r
2199 IN BOOLEAN Received,\r
2200 IN UINT32 PacketLength,\r
2201 IN EFI_PXE_BASE_CODE_PACKET *PacketPtr OPTIONAL\r
2202 )\r
2203{\r
2204 EFI_INPUT_KEY Key;\r
2205 EFI_STATUS Status;\r
2206\r
2207 //\r
2208 // Catch Ctrl-C or ESC to abort.\r
2209 //\r
2210 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
2211\r
2212 if (!EFI_ERROR (Status)) {\r
2213\r
2214 if (Key.ScanCode == SCAN_ESC || Key.UnicodeChar == (0x1F & 'c')) {\r
2215\r
2216 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_ABORT;\r
2217 }\r
2218 }\r
2219 //\r
2220 // No print if receive packet\r
2221 //\r
2222 if (Received) {\r
2223 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE;\r
2224 }\r
2225 //\r
2226 // Print only for three functions\r
2227 //\r
2228 switch (Function) {\r
2229\r
2230 case EFI_PXE_BASE_CODE_FUNCTION_MTFTP:\r
2231 //\r
2232 // Print only for open MTFTP packets, not every MTFTP packets\r
2233 //\r
2234 if (PacketLength != 0 && PacketPtr != NULL) {\r
2235 if (PacketPtr->Raw[0x1C] != 0x00 || PacketPtr->Raw[0x1D] != 0x01) {\r
2236 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE;\r
2237 }\r
2238 }\r
2239 break;\r
2240\r
2241 case EFI_PXE_BASE_CODE_FUNCTION_DHCP:\r
2242 case EFI_PXE_BASE_CODE_FUNCTION_DISCOVER:\r
2243 break;\r
2244\r
2245 default:\r
2246 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE;\r
2247 }\r
2248\r
2249 if (PacketLength != 0 && PacketPtr != NULL) {\r
2250 //\r
2251 // Print '.' when transmit a packet\r
2252 //\r
2253 AsciiPrint (".");\r
2254 }\r
2255\r
2256 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE;\r
2257}\r
2258\r
2259EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL gPxeBcCallBackTemplate = {\r
2260 EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL_REVISION,\r
2261 EfiPxeLoadFileCallback\r
2262};\r
2263\r
2264\r
2265/**\r
2266 Causes the driver to load a specified file.\r
2267\r
2268 @param[in] This Protocol instance pointer.\r
2269 @param[in] FilePath The device specific path of the file to load.\r
2270 @param[in] BootPolicy If TRUE, indicates that the request originates from the\r
2271 boot manager is attempting to load FilePath as a boot\r
2272 selection. If FALSE, then FilePath must match an exact file\r
2273 to be loaded.\r
2274 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return\r
2275 code of EFI_SUCCESS, the amount of data transferred to\r
2276 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,\r
2277 the size of Buffer required to retrieve the requested file.\r
2278 @param[in] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,\r
2279 then no the size of the requested file is returned in\r
2280 BufferSize.\r
2281\r
2282 @retval EFI_SUCCESS The file was loaded.\r
2283 @retval EFI_UNSUPPORTED The device does not support the provided BootPolicy.\r
2284 @retval EFI_INVALID_PARAMETER FilePath is not a valid device path, or\r
2285 BufferSize is NULL.\r
2286 @retval EFI_NO_MEDIA No medium was present to load the file.\r
2287 @retval EFI_DEVICE_ERROR The file was not loaded due to a device error.\r
2288 @retval EFI_NO_RESPONSE The remote system did not respond.\r
2289 @retval EFI_NOT_FOUND The file was not found.\r
2290 @retval EFI_ABORTED The file load process was manually cancelled.\r
2291\r
2292**/\r
2293EFI_STATUS\r
2294EFIAPI\r
2295EfiPxeLoadFile (\r
2296 IN EFI_LOAD_FILE_PROTOCOL *This,\r
2297 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,\r
2298 IN BOOLEAN BootPolicy,\r
2299 IN OUT UINTN *BufferSize,\r
2300 IN VOID *Buffer OPTIONAL\r
2301 )\r
2302{\r
2303 PXEBC_PRIVATE_DATA *Private;\r
2304 PXEBC_VIRTUAL_NIC *VirtualNic;\r
2305 EFI_PXE_BASE_CODE_PROTOCOL *PxeBc;\r
2306 BOOLEAN UsingIpv6;\r
2307 EFI_STATUS Status;\r
2308 BOOLEAN MediaPresent;\r
2309\r
2310 VirtualNic = PXEBC_VIRTUAL_NIC_FROM_LOADFILE (This);\r
2311 Private = VirtualNic->Private;\r
2312 PxeBc = &Private->PxeBc;\r
2313 UsingIpv6 = FALSE;\r
2314 Status = EFI_DEVICE_ERROR;\r
2315\r
2316 if (This == NULL || BufferSize == NULL) {\r
2317 return EFI_INVALID_PARAMETER;\r
2318 }\r
2319\r
2320 //\r
2321 // Only support BootPolicy\r
2322 //\r
2323 if (!BootPolicy) {\r
2324 return EFI_UNSUPPORTED;\r
2325 }\r
2326\r
2327 //\r
2328 // Check media status before PXE start\r
2329 //\r
2330 MediaPresent = TRUE;\r
2331 NetLibDetectMedia (Private->Controller, &MediaPresent);\r
2332 if (!MediaPresent) {\r
2333 return EFI_NO_MEDIA;\r
2334 }\r
2335\r
2336 //\r
2337 // Check whether the virtual nic is using IPv6 or not.\r
2338 //\r
2339 if (VirtualNic == Private->Ip6Nic) {\r
2340 UsingIpv6 = TRUE;\r
2341 }\r
2342\r
2343 //\r
2344 // Start Pxe Base Code to initialize PXE boot.\r
2345 //\r
2346 Status = PxeBc->Start (PxeBc, UsingIpv6);\r
75dce340 2347 if (Status == EFI_ALREADY_STARTED && UsingIpv6 != PxeBc->Mode->UsingIpv6) {\r
2348 //\r
2349 // PxeBc protocol has already been started but not on the required IP version, restart it.\r
2350 //\r
2351 Status = PxeBc->Stop (PxeBc);\r
2352 if (!EFI_ERROR (Status)) {\r
2353 Status = PxeBc->Start (PxeBc, UsingIpv6);\r
2354 }\r
2355 }\r
a3bcde70
HT
2356 if (Status == EFI_SUCCESS || Status == EFI_ALREADY_STARTED) {\r
2357 Status = PxeBcLoadBootFile (Private, BufferSize, Buffer);\r
2358 }\r
2359\r
2360 if (Status != EFI_SUCCESS &&\r
2361 Status != EFI_UNSUPPORTED &&\r
2362 Status != EFI_BUFFER_TOO_SMALL) {\r
2363 //\r
2364 // There are three cases, which needn't stop pxebc here.\r
2365 // 1. success to download file.\r
2366 // 2. success to get file size.\r
2367 // 3. unsupported.\r
2368 //\r
2369 PxeBc->Stop (PxeBc);\r
2370 }\r
2371\r
2372 return Status;\r
2373}\r
2374\r
2375EFI_LOAD_FILE_PROTOCOL gLoadFileProtocolTemplate = { EfiPxeLoadFile };\r
2376\r