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