]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/UefiPxeBcDxe/PxeBcDhcp6.c
1.Fix a icc build break
[mirror_edk2.git] / NetworkPkg / UefiPxeBcDxe / PxeBcDhcp6.c
1 /** @file
2 Functions implementation related with DHCPv6 for UefiPxeBc Driver.
3
4 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
5
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "PxeBcImpl.h"
17
18
19 /**
20 Parse out a DHCPv6 option by OptTag, and find the position in buffer.
21
22 @param[in] Buffer The pointer to the option buffer.
23 @param[in] Length Length of the option buffer.
24 @param[in] OptTag The required option tag.
25
26 @retval NULL Failed to parse the required option.
27 @retval Others The postion of the required option in buffer.
28
29 **/
30 EFI_DHCP6_PACKET_OPTION *
31 PxeBcParseDhcp6Options (
32 IN UINT8 *Buffer,
33 IN UINT32 Length,
34 IN UINT16 OptTag
35 )
36 {
37 EFI_DHCP6_PACKET_OPTION *Option;
38 UINT32 Offset;
39
40 Option = (EFI_DHCP6_PACKET_OPTION *) Buffer;
41 Offset = 0;
42
43 //
44 // OpLen and OpCode here are both stored in network order.
45 //
46 while (Offset < Length) {
47
48 if (NTOHS (Option->OpCode) == OptTag) {
49
50 return Option;
51 }
52
53 Offset += (NTOHS(Option->OpLen) + 4);
54 Option = (EFI_DHCP6_PACKET_OPTION *) (Buffer + Offset);
55 }
56
57 return NULL;
58 }
59
60
61 /**
62 Build the options buffer for the DHCPv6 request packet.
63
64 @param[in] Private The pointer to PxeBc private data.
65 @param[out] OptList The pointer to the option pointer array.
66 @param[in] Buffer The pointer to the buffer to contain the option list.
67
68 @return Index The count of the built-in options.
69
70 **/
71 UINT32
72 PxeBcBuildDhcp6Options (
73 IN PXEBC_PRIVATE_DATA *Private,
74 OUT EFI_DHCP6_PACKET_OPTION **OptList,
75 IN UINT8 *Buffer
76 )
77 {
78 PXEBC_DHCP6_OPTION_ENTRY OptEnt;
79 UINT32 Index;
80 UINT16 Value;
81
82 Index = 0;
83 OptList[0] = (EFI_DHCP6_PACKET_OPTION *) Buffer;
84
85 //
86 // Append client option request option
87 //
88 OptList[Index]->OpCode = HTONS (PXEBC_DHCP6_OPT_ORO);
89 OptList[Index]->OpLen = HTONS (4);
90 OptEnt.Oro = (PXEBC_DHCP6_OPTION_ORO *) OptList[Index]->Data;
91 OptEnt.Oro->OpCode[0] = HTONS(PXEBC_DHCP6_OPT_BOOT_FILE_URL);
92 OptEnt.Oro->OpCode[1] = HTONS(PXEBC_DHCP6_OPT_BOOT_FILE_PARAM);
93 Index++;
94 OptList[Index] = GET_NEXT_DHCP6_OPTION (OptList[Index - 1]);
95
96 //
97 // Append client network device interface option
98 //
99 OptList[Index]->OpCode = HTONS (PXEBC_DHCP6_OPT_UNDI);
100 OptList[Index]->OpLen = HTONS ((UINT16)3);
101 OptEnt.Undi = (PXEBC_DHCP6_OPTION_UNDI *) OptList[Index]->Data;
102
103 if (Private->Nii != NULL) {
104 OptEnt.Undi->Type = Private->Nii->Type;
105 OptEnt.Undi->MajorVer = Private->Nii->MajorVer;
106 OptEnt.Undi->MinorVer = Private->Nii->MinorVer;
107 } else {
108 OptEnt.Undi->Type = DEFAULT_UNDI_TYPE;
109 OptEnt.Undi->MajorVer = DEFAULT_UNDI_MAJOR;
110 OptEnt.Undi->MinorVer = DEFAULT_UNDI_MINOR;
111 }
112
113 Index++;
114 OptList[Index] = GET_NEXT_DHCP6_OPTION (OptList[Index - 1]);
115
116 //
117 // Append client system architecture option
118 //
119 OptList[Index]->OpCode = HTONS (PXEBC_DHCP6_OPT_ARCH);
120 OptList[Index]->OpLen = HTONS ((UINT16) sizeof (PXEBC_DHCP6_OPTION_ARCH));
121 OptEnt.Arch = (PXEBC_DHCP6_OPTION_ARCH *) OptList[Index]->Data;
122 Value = HTONS (EFI_PXE_CLIENT_SYSTEM_ARCHITECTURE);
123 CopyMem (&OptEnt.Arch->Type, &Value, sizeof (UINT16));
124 Index++;
125 OptList[Index] = GET_NEXT_DHCP6_OPTION (OptList[Index - 1]);
126
127 //
128 // Append vendor class option to store the PXE class identifier.
129 //
130 OptList[Index]->OpCode = HTONS (PXEBC_DHCP6_OPT_VENDOR_CLASS);
131 OptList[Index]->OpLen = HTONS ((UINT16) sizeof (PXEBC_DHCP6_OPTION_VENDOR_CLASS));
132 OptEnt.VendorClass = (PXEBC_DHCP6_OPTION_VENDOR_CLASS *) OptList[Index]->Data;
133 OptEnt.VendorClass->Vendor = HTONL (PXEBC_DHCP6_ENTERPRISE_NUM);
134 OptEnt.VendorClass->ClassLen = HTONS ((UINT16) sizeof (PXEBC_CLASS_ID));
135 CopyMem (
136 &OptEnt.VendorClass->ClassId,
137 DEFAULT_CLASS_ID_DATA,
138 sizeof (PXEBC_CLASS_ID)
139 );
140 PxeBcUintnToAscDecWithFormat (
141 EFI_PXE_CLIENT_SYSTEM_ARCHITECTURE,
142 OptEnt.VendorClass->ClassId.ArchitectureType,
143 sizeof (OptEnt.VendorClass->ClassId.ArchitectureType)
144 );
145
146 if (Private->Nii != NULL) {
147 CopyMem (
148 OptEnt.VendorClass->ClassId.InterfaceName,
149 Private->Nii->StringId,
150 sizeof (OptEnt.VendorClass->ClassId.InterfaceName)
151 );
152 PxeBcUintnToAscDecWithFormat (
153 Private->Nii->MajorVer,
154 OptEnt.VendorClass->ClassId.UndiMajor,
155 sizeof (OptEnt.VendorClass->ClassId.UndiMajor)
156 );
157 PxeBcUintnToAscDecWithFormat (
158 Private->Nii->MinorVer,
159 OptEnt.VendorClass->ClassId.UndiMinor,
160 sizeof (OptEnt.VendorClass->ClassId.UndiMinor)
161 );
162 }
163
164 Index++;
165
166 return Index;
167 }
168
169
170 /**
171 Cache the DHCPv6 packet.
172
173 @param[in] Dst The pointer to the cache buffer for DHCPv6 packet.
174 @param[in] Src The pointer to the DHCPv6 packet to be cached.
175
176 **/
177 VOID
178 PxeBcCacheDhcp6Packet (
179 IN EFI_DHCP6_PACKET *Dst,
180 IN EFI_DHCP6_PACKET *Src
181 )
182 {
183 ASSERT (Dst->Size >= Src->Length);
184
185 CopyMem (&Dst->Dhcp6, &Src->Dhcp6, Src->Length);
186 Dst->Length = Src->Length;
187 }
188
189
190 /**
191 Free all the nodes in the list for boot file.
192
193 @param[in] Head The pointer to the head of list.
194
195 **/
196 VOID
197 PxeBcFreeBootFileOption (
198 IN LIST_ENTRY *Head
199 )
200 {
201 LIST_ENTRY *Entry;
202 LIST_ENTRY *NextEntry;
203 PXEBC_DHCP6_OPTION_NODE *Node;
204
205 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, Head) {
206 Node = NET_LIST_USER_STRUCT (Entry, PXEBC_DHCP6_OPTION_NODE, Link);
207 RemoveEntryList (Entry);
208 FreePool (Node);
209 }
210 }
211
212
213 /**
214 Parse the Boot File URL option.
215
216 @param[out] FileName The pointer to the boot file name.
217 @param[in, out] SrvAddr The pointer to the boot server address.
218 @param[in] BootFile The pointer to the boot file URL option data.
219 @param[in] Length The length of the boot file URL option data.
220
221 @retval EFI_ABORTED User cancel operation.
222 @retval EFI_SUCCESS Selected the boot menu successfully.
223 @retval EFI_NOT_READY Read the input key from the keybroad has not finish.
224
225 **/
226 EFI_STATUS
227 PxeBcExtractBootFileUrl (
228 OUT UINT8 **FileName,
229 IN OUT EFI_IPv6_ADDRESS *SrvAddr,
230 IN CHAR8 *BootFile,
231 IN UINT16 Length
232 )
233 {
234 UINT16 PrefixLen;
235 UINT8 *BootFileNamePtr;
236 UINT8 *BootFileName;
237 UINT16 BootFileNameLen;
238 CHAR8 *TmpStr;
239 CHAR8 TmpChar;
240 CHAR8 *ServerAddressOption;
241 CHAR8 *ServerAddress;
242 EFI_STATUS Status;
243
244 //
245 // The format of the Boot File URL option is:
246 //
247 // 0 1 2 3
248 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
249 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
250 // | OPT_BOOTFILE_URL | option-len |
251 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
252 // | |
253 // . bootfile-url (variable length) .
254 // | |
255 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
256 //
257
258 //
259 // Based upon RFC 5970 and UEFI 2.3 Errata D specification, bootfile-url format
260 // is tftp://[SERVER_ADDRESS]/BOOTFILE_NAME
261 // As an example where the BOOTFILE_NAME is the EFI loader and
262 // SERVER_ADDRESS is the ASCII encoding of an IPV6 address.
263 //
264 PrefixLen = (UINT16) AsciiStrLen (PXEBC_DHCP6_BOOT_FILE_URL_PREFIX);
265
266 if (Length <= PrefixLen ||
267 CompareMem (BootFile, PXEBC_DHCP6_BOOT_FILE_URL_PREFIX, PrefixLen) != 0) {
268 return EFI_NOT_FOUND;
269 }
270
271 BootFile = BootFile + PrefixLen;
272 Length = (UINT16) (Length - PrefixLen);
273
274 TmpStr = (CHAR8 *) AllocateZeroPool (Length + 1);
275 if (TmpStr == NULL) {
276 return EFI_OUT_OF_RESOURCES;
277 }
278
279 CopyMem (TmpStr, BootFile, Length);
280 TmpStr[Length] = '\0';
281
282 //
283 // Get the part of SERVER_ADDRESS string.
284 //
285 ServerAddressOption = TmpStr;
286 if (*ServerAddressOption != PXEBC_ADDR_START_DELIMITER) {
287 FreePool (TmpStr);
288 return EFI_INVALID_PARAMETER;
289 }
290
291 ServerAddressOption ++;
292 ServerAddress = ServerAddressOption;
293 while (*ServerAddress != '\0' && *ServerAddress != PXEBC_ADDR_END_DELIMITER) {
294 ServerAddress++;
295 }
296
297 if (*ServerAddress != PXEBC_ADDR_END_DELIMITER) {
298 FreePool (TmpStr);
299 return EFI_INVALID_PARAMETER;
300 }
301
302 *ServerAddress = '\0';
303
304 //
305 // Convert the string of server address to Ipv6 address format and store it.
306 //
307 Status = NetLibAsciiStrToIp6 (ServerAddressOption, SrvAddr);
308 if (EFI_ERROR (Status)) {
309 FreePool (TmpStr);
310 return Status;
311 }
312
313 //
314 // Get the part of BOOTFILE_NAME string.
315 //
316 BootFileNamePtr = (UINT8*)((UINTN)ServerAddress + 1);
317 if (*BootFileNamePtr != PXEBC_TFTP_URL_SEPARATOR) {
318 FreePool (TmpStr);
319 return EFI_INVALID_PARAMETER;
320 }
321
322 ++BootFileNamePtr;
323 BootFileNameLen = (UINT16)(Length - (UINT16) ((UINTN)BootFileNamePtr - (UINTN)TmpStr) + 1);
324 if (BootFileNameLen != 0 || FileName != NULL) {
325 //
326 // Extract boot file name from URL.
327 //
328 BootFileName = (UINT8 *) AllocateZeroPool (BootFileNameLen);
329 if (BootFileName == NULL) {
330 FreePool (TmpStr);
331 return EFI_OUT_OF_RESOURCES;
332 }
333 *FileName = BootFileName;
334
335 //
336 // Decode percent-encoding in boot file name.
337 //
338 while (*BootFileNamePtr != '\0') {
339 if (*BootFileNamePtr == '%') {
340 TmpChar = *(BootFileNamePtr+ 3);
341 *(BootFileNamePtr+ 3) = '\0';
342 *BootFileName = (UINT8) AsciiStrHexToUintn ((CHAR8*)(BootFileNamePtr + 1));
343 BootFileName++;
344 *(BootFileNamePtr+ 3) = TmpChar;
345 BootFileNamePtr += 3;
346 } else {
347 *BootFileName = *BootFileNamePtr;
348 BootFileName++;
349 BootFileNamePtr++;
350 }
351 }
352 *BootFileName = '\0';
353 }
354
355 FreePool (TmpStr);
356
357 return EFI_SUCCESS;
358 }
359
360
361 /**
362 Parse the Boot File Parameter option.
363
364 @param[in] BootFilePara The pointer to boot file parameter option data.
365 @param[out] BootFileSize The pointer to the parsed boot file size.
366
367 @retval EFI_SUCCESS Successfully obtained the boot file size from parameter option.
368 @retval EFI_NOT_FOUND Failed to extract the boot file size from parameter option.
369
370 **/
371 EFI_STATUS
372 PxeBcExtractBootFileParam (
373 IN CHAR8 *BootFilePara,
374 OUT UINT16 *BootFileSize
375 )
376 {
377 UINT16 Length;
378 UINT8 Index;
379 UINT8 Digit;
380 UINT32 Size;
381
382 CopyMem (&Length, BootFilePara, sizeof (UINT16));
383 Length = NTOHS (Length);
384
385 //
386 // The BootFile Size should be 1~5 byte ASCII strings
387 //
388 if (Length < 1 || Length > 5) {
389 return EFI_NOT_FOUND;
390 }
391
392 //
393 // Extract the value of BootFile Size.
394 //
395 BootFilePara = BootFilePara + sizeof (UINT16);
396 Size = 0;
397 for (Index = 0; Index < Length; Index++) {
398 if (EFI_ERROR (PxeBcUniHexToUint8 (&Digit, *(BootFilePara + Index)))) {
399 return EFI_NOT_FOUND;
400 }
401
402 Size = (Size + Digit) * 10;
403 }
404
405 Size = Size / 10;
406 if (Size > PXEBC_DHCP6_MAX_BOOT_FILE_SIZE) {
407 return EFI_NOT_FOUND;
408 }
409
410 *BootFileSize = (UINT16) Size;
411 return EFI_SUCCESS;
412 }
413
414
415 /**
416 Parse the cached DHCPv6 packet, including all the options.
417
418 @param[in] Cache6 The pointer to a cached DHCPv6 packet.
419
420 @retval EFI_SUCCESS Parsed the DHCPv6 packet successfully.
421 @retval EFI_DEVICE_ERROR Failed to parse and invalid the packet.
422
423 **/
424 EFI_STATUS
425 PxeBcParseDhcp6Packet (
426 IN PXEBC_DHCP6_PACKET_CACHE *Cache6
427 )
428 {
429 EFI_DHCP6_PACKET *Offer;
430 EFI_DHCP6_PACKET_OPTION **Options;
431 EFI_DHCP6_PACKET_OPTION *Option;
432 PXEBC_OFFER_TYPE OfferType;
433 BOOLEAN IsProxyOffer;
434 BOOLEAN IsPxeOffer;
435 UINT32 Offset;
436 UINT32 Length;
437 UINT32 EnterpriseNum;
438
439 IsProxyOffer = TRUE;
440 IsPxeOffer = FALSE;
441 Offer = &Cache6->Packet.Offer;
442 Options = Cache6->OptList;
443
444 ZeroMem (Cache6->OptList, sizeof (Cache6->OptList));
445
446 Option = (EFI_DHCP6_PACKET_OPTION *) (Offer->Dhcp6.Option);
447 Offset = 0;
448 Length = GET_DHCP6_OPTION_SIZE (Offer);
449
450 //
451 // OpLen and OpCode here are both stored in network order, since they are from original packet.
452 //
453 while (Offset < Length) {
454
455 if (NTOHS (Option->OpCode) == PXEBC_DHCP6_OPT_IA_NA) {
456 Options[PXEBC_DHCP6_IDX_IA_NA] = Option;
457 } else if (NTOHS (Option->OpCode) == PXEBC_DHCP6_OPT_BOOT_FILE_URL) {
458 //
459 // The server sends this option to inform the client about an URL to a boot file.
460 //
461 Options[PXEBC_DHCP6_IDX_BOOT_FILE_URL] = Option;
462 } else if (NTOHS (Option->OpCode) == PXEBC_DHCP6_OPT_BOOT_FILE_PARAM) {
463 Options[PXEBC_DHCP6_IDX_BOOT_FILE_PARAM] = Option;
464 } else if (NTOHS (Option->OpCode) == PXEBC_DHCP6_OPT_VENDOR_CLASS) {
465 Options[PXEBC_DHCP6_IDX_VENDOR_CLASS] = Option;
466 }
467
468 Offset += (NTOHS (Option->OpLen) + 4);
469 Option = (EFI_DHCP6_PACKET_OPTION *) (Offer->Dhcp6.Option + Offset);
470 }
471
472 //
473 // The offer with assigned client address is a proxy offer.
474 // An ia_na option, embeded with valid ia_addr option and a status_code of success.
475 //
476 Option = Options[PXEBC_DHCP6_IDX_IA_NA];
477 if (Option != NULL) {
478 Option = PxeBcParseDhcp6Options (
479 Option->Data + 12,
480 NTOHS (Option->OpLen),
481 PXEBC_DHCP6_OPT_STATUS_CODE
482 );
483 if ((Option != NULL && Option->Data[0] == 0) || (Option == NULL)) {
484 IsProxyOffer = FALSE;
485 }
486 }
487
488 //
489 // The offer with "PXEClient" is a pxe offer.
490 //
491 Option = Options[PXEBC_DHCP6_IDX_VENDOR_CLASS];
492 EnterpriseNum = HTONL(PXEBC_DHCP6_ENTERPRISE_NUM);
493
494 if (Option != NULL &&
495 NTOHS(Option->OpLen) >= 13 &&
496 CompareMem (Option->Data, &EnterpriseNum, sizeof (UINT32)) == 0 &&
497 CompareMem (&Option->Data[6], DEFAULT_CLASS_ID_DATA, 9) == 0) {
498 IsPxeOffer = TRUE;
499 }
500
501 //
502 // Determine offer type of the dhcp6 packet.
503 //
504 if (IsPxeOffer) {
505 //
506 // It's a binl offer only with PXEClient.
507 //
508 OfferType = IsProxyOffer ? PxeOfferTypeProxyBinl : PxeOfferTypeDhcpBinl;
509 } else {
510 //
511 // It's a dhcp only offer, which is a pure dhcp6 offer packet.
512 //
513 OfferType = PxeOfferTypeDhcpOnly;
514 }
515
516 Cache6->OfferType = OfferType;
517
518 return EFI_SUCCESS;
519 }
520
521
522 /**
523 Cache the DHCPv6 ack packet, and parse it on demand.
524
525 @param[in] Private The pointer to PxeBc private data.
526 @param[in] Ack The pointer to the DHCPv6 ack packet.
527 @param[in] Verified If TRUE, parse the ACK packet and store info into mode data.
528
529 **/
530 VOID
531 PxeBcCopyDhcp6Ack (
532 IN PXEBC_PRIVATE_DATA *Private,
533 IN EFI_DHCP6_PACKET *Ack,
534 IN BOOLEAN Verified
535 )
536 {
537 EFI_PXE_BASE_CODE_MODE *Mode;
538
539 Mode = Private->PxeBc.Mode;
540
541 PxeBcCacheDhcp6Packet (&Private->DhcpAck.Dhcp6.Packet.Ack, Ack);
542
543 if (Verified) {
544 //
545 // Parse the ack packet and store it into mode data if needed.
546 //
547 PxeBcParseDhcp6Packet (&Private->DhcpAck.Dhcp6);
548 CopyMem (&Mode->DhcpAck.Dhcpv6, &Ack->Dhcp6, Ack->Length);
549 Mode->DhcpAckReceived = TRUE;
550 }
551 }
552
553
554 /**
555 Cache the DHCPv6 proxy offer packet according to the received order.
556
557 @param[in] Private The pointer to PxeBc private data.
558 @param[in] OfferIndex The received order of offer packets.
559
560 **/
561 VOID
562 PxeBcCopyDhcp6Proxy (
563 IN PXEBC_PRIVATE_DATA *Private,
564 IN UINT32 OfferIndex
565 )
566 {
567 EFI_PXE_BASE_CODE_MODE *Mode;
568 EFI_DHCP6_PACKET *Offer;
569
570 ASSERT (OfferIndex < Private->OfferNum);
571 ASSERT (OfferIndex < PXEBC_OFFER_MAX_NUM);
572
573 Mode = Private->PxeBc.Mode;
574 Offer = &Private->OfferBuffer[OfferIndex].Dhcp6.Packet.Offer;
575
576 //
577 // Cache the proxy offer packet and parse it.
578 //
579 PxeBcCacheDhcp6Packet (&Private->ProxyOffer.Dhcp6.Packet.Offer, Offer);
580 PxeBcParseDhcp6Packet (&Private->ProxyOffer.Dhcp6);
581
582 //
583 // Store this packet into mode data.
584 //
585 CopyMem (&Mode->ProxyOffer.Dhcpv6, &Offer->Dhcp6, Offer->Length);
586 Mode->ProxyOfferReceived = TRUE;
587 }
588
589 /**
590 Seek the address of the first byte of the option header.
591
592 @param[in] Buf The pointer to the buffer.
593 @param[in] SeekLen The length to seek.
594 @param[in] OptType The option type.
595
596 @retval NULL If it failed to seek the option.
597 @retval others The position to the option.
598
599 **/
600 UINT8 *
601 PxeBcDhcp6SeekOption (
602 IN UINT8 *Buf,
603 IN UINT32 SeekLen,
604 IN UINT16 OptType
605 )
606 {
607 UINT8 *Cursor;
608 UINT8 *Option;
609 UINT16 DataLen;
610 UINT16 OpCode;
611
612 Option = NULL;
613 Cursor = Buf;
614
615 while (Cursor < Buf + SeekLen) {
616 OpCode = ReadUnaligned16 ((UINT16 *) Cursor);
617 if (OpCode == HTONS (OptType)) {
618 Option = Cursor;
619 break;
620 }
621 DataLen = NTOHS (ReadUnaligned16 ((UINT16 *) (Cursor + 2)));
622 Cursor += (DataLen + 4);
623 }
624
625 return Option;
626 }
627
628
629 /**
630 Build and send out the request packet for the bootfile, and parse the reply.
631
632 @param[in] Private The pointer to PxeBc private data.
633 @param[in] Index PxeBc option boot item type.
634
635 @retval EFI_SUCCESS Successfully discovered the boot file.
636 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
637 @retval EFI_NOT_FOUND Can't get the PXE reply packet.
638 @retval Others Failed to discover the boot file.
639
640 **/
641 EFI_STATUS
642 PxeBcRequestBootService (
643 IN PXEBC_PRIVATE_DATA *Private,
644 IN UINT32 Index
645 )
646 {
647 EFI_PXE_BASE_CODE_UDP_PORT SrcPort;
648 EFI_PXE_BASE_CODE_UDP_PORT DestPort;
649 EFI_PXE_BASE_CODE_MODE *Mode;
650 EFI_PXE_BASE_CODE_PROTOCOL *PxeBc;
651 EFI_PXE_BASE_CODE_DHCPV6_PACKET *Discover;
652 UINTN DiscoverLen;
653 EFI_DHCP6_PACKET *Request;
654 UINTN RequestLen;
655 EFI_DHCP6_PACKET *Reply;
656 UINT8 *RequestOpt;
657 UINT8 *DiscoverOpt;
658 UINTN ReadSize;
659 UINT16 OpFlags;
660 UINT16 OpCode;
661 UINT16 OpLen;
662 EFI_STATUS Status;
663 EFI_DHCP6_PACKET *ProxyOffer;
664 UINT8 *Option;
665
666 PxeBc = &Private->PxeBc;
667 Mode = PxeBc->Mode;
668 Request = Private->Dhcp6Request;
669 ProxyOffer = &Private->OfferBuffer[Index].Dhcp6.Packet.Offer;
670 SrcPort = PXEBC_BS_DISCOVER_PORT;
671 DestPort = PXEBC_BS_DISCOVER_PORT;
672 OpFlags = 0;
673
674 if (Request == NULL) {
675 return EFI_DEVICE_ERROR;
676 }
677
678 Discover = AllocateZeroPool (sizeof (EFI_PXE_BASE_CODE_DHCPV6_PACKET));
679 if (Discover == NULL) {
680 return EFI_OUT_OF_RESOURCES;
681 }
682
683 //
684 // Build the request packet by the cached request packet before.
685 //
686 Discover->TransactionId = ProxyOffer->Dhcp6.Header.TransactionId;
687 Discover->MessageType = Request->Dhcp6.Header.MessageType;
688 RequestOpt = Request->Dhcp6.Option;
689 DiscoverOpt = Discover->DhcpOptions;
690 DiscoverLen = sizeof (EFI_DHCP6_HEADER);
691 RequestLen = DiscoverLen;
692
693 //
694 // Find Server ID Option from ProxyOffer.
695 //
696 Option = PxeBcDhcp6SeekOption (
697 ProxyOffer->Dhcp6.Option,
698 ProxyOffer->Length - 4,
699 PXEBC_DHCP6_OPT_SERVER_ID
700 );
701 if (Option == NULL) {
702 return EFI_NOT_FOUND;
703 }
704
705 //
706 // Add Server ID Option.
707 //
708 OpLen = NTOHS (((EFI_DHCP6_PACKET_OPTION *) Option)->OpLen);
709 CopyMem (DiscoverOpt, Option, OpLen + 4);
710 DiscoverOpt += (OpLen + 4);
711 DiscoverLen += (OpLen + 4);
712
713 while (RequestLen < Request->Length) {
714 OpCode = NTOHS (((EFI_DHCP6_PACKET_OPTION *) RequestOpt)->OpCode);
715 OpLen = NTOHS (((EFI_DHCP6_PACKET_OPTION *) RequestOpt)->OpLen);
716 if (OpCode != EFI_DHCP6_IA_TYPE_NA &&
717 OpCode != EFI_DHCP6_IA_TYPE_TA &&
718 OpCode != PXEBC_DHCP6_OPT_SERVER_ID
719 ) {
720 //
721 // Copy all the options except IA option and Server ID
722 //
723 CopyMem (DiscoverOpt, RequestOpt, OpLen + 4);
724 DiscoverOpt += (OpLen + 4);
725 DiscoverLen += (OpLen + 4);
726 }
727 RequestOpt += (OpLen + 4);
728 RequestLen += (OpLen + 4);
729 }
730
731 //
732 // Update Elapsed option in the package
733 //
734 Option = PxeBcDhcp6SeekOption (
735 Discover->DhcpOptions,
736 (UINT32)(RequestLen - 4),
737 PXEBC_DHCP6_OPT_ELAPSED_TIME
738 );
739 if (Option != NULL) {
740 CalcElapsedTime (Private);
741 WriteUnaligned16 ((UINT16*)(Option + 4), HTONS((UINT16) Private->ElapsedTime));
742 }
743
744 Status = PxeBc->UdpWrite (
745 PxeBc,
746 OpFlags,
747 &Private->ServerIp,
748 &DestPort,
749 NULL,
750 &Private->StationIp,
751 &SrcPort,
752 NULL,
753 NULL,
754 &DiscoverLen,
755 (VOID *) Discover
756 );
757
758 if (EFI_ERROR (Status)) {
759 return Status;
760 }
761
762 //
763 // Cache the right PXE reply packet here, set valid flag later.
764 // Especially for PXE discover packet, store it into mode data here.
765 //
766 Reply = &Private->ProxyOffer.Dhcp6.Packet.Offer;
767 ReadSize = (UINTN) Reply->Size;
768
769 //
770 // Start Udp6Read instance
771 //
772 Status = Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData);
773 if (EFI_ERROR (Status)) {
774 return Status;
775 }
776
777 Status = PxeBc->UdpRead (
778 PxeBc,
779 OpFlags,
780 &Private->StationIp,
781 &SrcPort,
782 &Private->ServerIp,
783 &DestPort,
784 NULL,
785 NULL,
786 &ReadSize,
787 (VOID *) &Reply->Dhcp6
788 );
789 //
790 // Stop Udp6Read instance
791 //
792 Private->Udp6Read->Configure (Private->Udp6Read, NULL);
793
794 if (EFI_ERROR (Status)) {
795 return Status;
796 }
797
798 //
799 // Update length
800 //
801 Reply->Length = (UINT32) ReadSize;
802
803 return EFI_SUCCESS;
804 }
805
806
807 /**
808 Retry to request bootfile name by the BINL offer.
809
810 @param[in] Private The pointer to PxeBc private data.
811 @param[in] Index The received order of offer packets.
812
813 @retval EFI_SUCCESS Successfully retried a request for the bootfile name.
814 @retval EFI_DEVICE_ERROR Failed to retry the bootfile name.
815
816 **/
817 EFI_STATUS
818 PxeBcRetryDhcp6Binl (
819 IN PXEBC_PRIVATE_DATA *Private,
820 IN UINT32 Index
821 )
822 {
823 EFI_PXE_BASE_CODE_MODE *Mode;
824 PXEBC_DHCP6_PACKET_CACHE *Offer;
825 PXEBC_DHCP6_PACKET_CACHE *Cache6;
826 EFI_STATUS Status;
827
828 ASSERT (Index < PXEBC_OFFER_MAX_NUM);
829 ASSERT (Private->OfferBuffer[Index].Dhcp6.OfferType == PxeOfferTypeDhcpBinl ||
830 Private->OfferBuffer[Index].Dhcp6.OfferType == PxeOfferTypeProxyBinl);
831
832 Mode = Private->PxeBc.Mode;
833 Private->IsDoDiscover = FALSE;
834 Offer = &Private->OfferBuffer[Index].Dhcp6;
835
836 ASSERT (Offer->OptList[PXEBC_DHCP6_IDX_BOOT_FILE_URL] != NULL);
837 //
838 // Parse out the next server address from the last offer, and store it
839 //
840 Status = PxeBcExtractBootFileUrl (
841 &Private->BootFileName,
842 &Private->ServerIp.v6,
843 (CHAR8 *) (Offer->OptList[PXEBC_DHCP6_IDX_BOOT_FILE_URL]->Data),
844 NTOHS (Offer->OptList[PXEBC_DHCP6_IDX_BOOT_FILE_URL]->OpLen)
845 );
846 if (EFI_ERROR (Status)) {
847 return Status;
848 }
849
850 //
851 // Retry Dhcp6Binl again for the bootfile, and the reply cached into Private->ProxyOffer.
852 //
853 Status = PxeBcRequestBootService (Private, Index);
854
855 if (EFI_ERROR (Status)) {
856 return Status;
857 }
858
859 Cache6 = &Private->ProxyOffer.Dhcp6;
860 Status = PxeBcParseDhcp6Packet (Cache6);
861 if (EFI_ERROR (Status)) {
862 return Status;
863 }
864
865 if (Cache6->OfferType != PxeOfferTypeProxyPxe10 &&
866 Cache6->OfferType != PxeOfferTypeProxyWfm11a &&
867 Cache6->OptList[PXEBC_DHCP6_IDX_BOOT_FILE_URL] == NULL) {
868 //
869 // This BINL ack doesn't have discovery option set or multicast option set
870 // or bootfile name specified.
871 //
872 return EFI_DEVICE_ERROR;
873 }
874
875 Mode->ProxyOfferReceived = TRUE;
876 CopyMem (
877 &Mode->ProxyOffer.Dhcpv6,
878 &Cache6->Packet.Offer.Dhcp6,
879 Cache6->Packet.Offer.Length
880 );
881
882 return EFI_SUCCESS;
883 }
884
885
886 /**
887 Cache all the received DHCPv6 offers, and set OfferIndex and OfferCount.
888
889 @param[in] Private The pointer to PXEBC_PRIVATE_DATA.
890 @param[in] RcvdOffer The pointer to the received offer packet.
891
892 **/
893 VOID
894 PxeBcCacheDhcp6Offer (
895 IN PXEBC_PRIVATE_DATA *Private,
896 IN EFI_DHCP6_PACKET *RcvdOffer
897 )
898 {
899 PXEBC_DHCP6_PACKET_CACHE *Cache6;
900 EFI_DHCP6_PACKET *Offer;
901 PXEBC_OFFER_TYPE OfferType;
902
903 Cache6 = &Private->OfferBuffer[Private->OfferNum].Dhcp6;
904 Offer = &Cache6->Packet.Offer;
905
906 //
907 // Cache the content of DHCPv6 packet firstly.
908 //
909 PxeBcCacheDhcp6Packet (Offer, RcvdOffer);
910
911 //
912 // Validate the DHCPv6 packet, and parse the options and offer type.
913 //
914 if (EFI_ERROR (PxeBcParseDhcp6Packet (Cache6))) {
915 return ;
916 }
917
918 //
919 // Determine whether cache the current offer by type, and record OfferIndex and OfferCount.
920 //
921 OfferType = Cache6->OfferType;
922 ASSERT (OfferType < PxeOfferTypeMax);
923 ASSERT (Private->OfferCount[OfferType] < PXEBC_OFFER_MAX_NUM);
924
925 if (IS_PROXY_OFFER (OfferType)) {
926 //
927 // It's a proxy offer without yiaddr, including PXE10, WFM11a or BINL offer.
928 //
929 Private->IsProxyRecved = TRUE;
930
931 if (OfferType == PxeOfferTypeProxyBinl) {
932 //
933 // Cache all proxy BINL offers.
934 //
935 Private->OfferIndex[OfferType][Private->OfferCount[OfferType]] = Private->OfferNum;
936 Private->OfferCount[OfferType]++;
937 } else if (Private->OfferCount[OfferType] > 0) {
938 //
939 // Only cache the first PXE10/WFM11a offer, and discard the others.
940 //
941 Private->OfferIndex[OfferType][0] = Private->OfferNum;
942 Private->OfferCount[OfferType] = 1;
943 } else {
944 return;
945 }
946 } else {
947 //
948 // It's a DHCPv6 offer with yiaddr, and cache them all.
949 //
950 Private->OfferIndex[OfferType][Private->OfferCount[OfferType]] = Private->OfferNum;
951 Private->OfferCount[OfferType]++;
952 }
953
954 Private->OfferNum++;
955 }
956
957
958 /**
959 Select an DHCPv6 offer, and record SelectIndex and SelectProxyType.
960
961 @param[in] Private The pointer to PXEBC_PRIVATE_DATA.
962
963 **/
964 VOID
965 PxeBcSelectDhcp6Offer (
966 IN PXEBC_PRIVATE_DATA *Private
967 )
968 {
969 UINT32 Index;
970 UINT32 OfferIndex;
971 PXEBC_OFFER_TYPE OfferType;
972
973 Private->SelectIndex = 0;
974
975 if (Private->IsOfferSorted) {
976 //
977 // Select offer by default policy.
978 //
979 if (Private->OfferCount[PxeOfferTypeDhcpPxe10] > 0) {
980 //
981 // 1. DhcpPxe10 offer
982 //
983 Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpPxe10][0] + 1;
984
985 } else if (Private->OfferCount[PxeOfferTypeDhcpWfm11a] > 0) {
986 //
987 // 2. DhcpWfm11a offer
988 //
989 Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpWfm11a][0] + 1;
990
991 } else if (Private->OfferCount[PxeOfferTypeDhcpOnly] > 0 &&
992 Private->OfferCount[PxeOfferTypeProxyPxe10] > 0) {
993 //
994 // 3. DhcpOnly offer and ProxyPxe10 offer.
995 //
996 Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpOnly][0] + 1;
997 Private->SelectProxyType = PxeOfferTypeProxyPxe10;
998
999 } else if (Private->OfferCount[PxeOfferTypeDhcpOnly] > 0 &&
1000 Private->OfferCount[PxeOfferTypeProxyWfm11a] > 0) {
1001 //
1002 // 4. DhcpOnly offer and ProxyWfm11a offer.
1003 //
1004 Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpOnly][0] + 1;
1005 Private->SelectProxyType = PxeOfferTypeProxyWfm11a;
1006
1007 } else if (Private->OfferCount[PxeOfferTypeDhcpBinl] > 0) {
1008 //
1009 // 5. DhcpBinl offer.
1010 //
1011 Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpBinl][0] + 1;
1012
1013 } else if (Private->OfferCount[PxeOfferTypeDhcpOnly] > 0 &&
1014 Private->OfferCount[PxeOfferTypeProxyBinl] > 0) {
1015 //
1016 // 6. DhcpOnly offer and ProxyBinl offer.
1017 //
1018 Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpOnly][0] + 1;
1019 Private->SelectProxyType = PxeOfferTypeProxyBinl;
1020
1021 } else {
1022 //
1023 // 7. DhcpOnly offer with bootfilename.
1024 //
1025 for (Index = 0; Index < Private->OfferCount[PxeOfferTypeDhcpOnly]; Index++) {
1026 OfferIndex = Private->OfferIndex[PxeOfferTypeDhcpOnly][Index];
1027 if (Private->OfferBuffer[OfferIndex].Dhcp6.OptList[PXEBC_DHCP6_IDX_BOOT_FILE_URL] != NULL) {
1028 Private->SelectIndex = OfferIndex + 1;
1029 break;
1030 }
1031 }
1032 }
1033 } else {
1034 //
1035 // Select offer by received order.
1036 //
1037 for (Index = 0; Index < Private->OfferNum; Index++) {
1038
1039 OfferType = Private->OfferBuffer[Index].Dhcp6.OfferType;
1040
1041 if (IS_PROXY_OFFER (OfferType)) {
1042 //
1043 // Skip proxy offers
1044 //
1045 continue;
1046 }
1047
1048 if (!Private->IsProxyRecved &&
1049 OfferType == PxeOfferTypeDhcpOnly &&
1050 Private->OfferBuffer[Index].Dhcp6.OptList[PXEBC_DHCP6_IDX_BOOT_FILE_URL] == NULL) {
1051 //
1052 // Skip if DhcpOnly offer without any other proxy offers or bootfilename.
1053 //
1054 continue;
1055 }
1056
1057 Private->SelectIndex = Index + 1;
1058 break;
1059 }
1060 }
1061 }
1062
1063
1064 /**
1065 Handle the DHCPv6 offer packet.
1066
1067 @param[in] Private The pointer to PXEBC_PRIVATE_DATA.
1068
1069 @retval EFI_SUCCESS Handled the DHCPv6 offer packet successfully.
1070 @retval EFI_NO_RESPONSE No response to the following request packet.
1071
1072 **/
1073 EFI_STATUS
1074 PxeBcHandleDhcp6Offer (
1075 IN PXEBC_PRIVATE_DATA *Private
1076 )
1077 {
1078 PXEBC_DHCP6_PACKET_CACHE *Cache6;
1079 EFI_STATUS Status;
1080 PXEBC_OFFER_TYPE OfferType;
1081 UINT32 ProxyIndex;
1082 UINT32 SelectIndex;
1083 UINT32 Index;
1084
1085 ASSERT (Private->SelectIndex > 0);
1086 SelectIndex = (UINT32) (Private->SelectIndex - 1);
1087 ASSERT (SelectIndex < PXEBC_OFFER_MAX_NUM);
1088 Cache6 = &Private->OfferBuffer[SelectIndex].Dhcp6;
1089 Status = EFI_SUCCESS;
1090
1091 if (Cache6->OfferType == PxeOfferTypeDhcpBinl) {
1092 //
1093 // DhcpBinl offer is selected, so need try to request bootfilename by this offer.
1094 //
1095 if (EFI_ERROR (PxeBcRetryDhcp6Binl (Private, SelectIndex))) {
1096 Status = EFI_NO_RESPONSE;
1097 }
1098 } else if (Cache6->OfferType == PxeOfferTypeDhcpOnly) {
1099
1100 if (Private->IsProxyRecved) {
1101 //
1102 // DhcpOnly offer is selected, so need try to request bootfilename.
1103 //
1104 ProxyIndex = 0;
1105 if (Private->IsOfferSorted) {
1106 //
1107 // The proxy offer should be determined if select by default policy.
1108 // IsOfferSorted means all offers are labeled by OfferIndex.
1109 //
1110 ASSERT (Private->OfferCount[Private->SelectProxyType] > 0);
1111
1112 if (Private->SelectProxyType == PxeOfferTypeProxyBinl) {
1113 //
1114 // Try all the cached ProxyBinl offer one by one to request bootfilename.
1115 //
1116 for (Index = 0; Index < Private->OfferCount[Private->SelectProxyType]; Index++) {
1117
1118 ProxyIndex = Private->OfferIndex[Private->SelectProxyType][Index];
1119 if (!EFI_ERROR (PxeBcRetryDhcp6Binl (Private, ProxyIndex))) {
1120 break;
1121 }
1122 }
1123 if (Index == Private->OfferCount[Private->SelectProxyType]) {
1124 Status = EFI_NO_RESPONSE;
1125 }
1126 } else {
1127 //
1128 // For other proxy offers (pxe10 or wfm11a), only one is buffered.
1129 //
1130 ProxyIndex = Private->OfferIndex[Private->SelectProxyType][0];
1131 }
1132 } else {
1133 //
1134 // The proxy offer should not be determined if select by received order.
1135 //
1136 Status = EFI_NO_RESPONSE;
1137
1138 for (Index = 0; Index < Private->OfferNum; Index++) {
1139
1140 OfferType = Private->OfferBuffer[Index].Dhcp6.OfferType;
1141
1142 if (!IS_PROXY_OFFER (OfferType)) {
1143 //
1144 // Skip non proxy dhcp offers.
1145 //
1146 continue;
1147 }
1148
1149 if (OfferType == PxeOfferTypeProxyBinl) {
1150 //
1151 // Try all the cached ProxyBinl offer one by one to request bootfilename.
1152 //
1153 if (EFI_ERROR (PxeBcRetryDhcp6Binl (Private, Index))) {
1154 continue;
1155 }
1156 }
1157
1158 Private->SelectProxyType = OfferType;
1159 ProxyIndex = Index;
1160 Status = EFI_SUCCESS;
1161 break;
1162 }
1163 }
1164
1165 if (!EFI_ERROR (Status) && Private->SelectProxyType != PxeOfferTypeProxyBinl) {
1166 //
1167 // Success to try to request by a ProxyPxe10 or ProxyWfm11a offer, copy and parse it.
1168 //
1169 PxeBcCopyDhcp6Proxy (Private, ProxyIndex);
1170 }
1171 } else {
1172 //
1173 // Othewise, the bootfilename must be included in DhcpOnly offer.
1174 //
1175 ASSERT (Cache6->OptList[PXEBC_DHCP6_IDX_BOOT_FILE_URL] != NULL);
1176 }
1177 }
1178
1179 if (!EFI_ERROR (Status)) {
1180 //
1181 // All PXE boot information is ready by now.
1182 //
1183 PxeBcCopyDhcp6Ack (Private, &Private->DhcpAck.Dhcp6.Packet.Ack, TRUE);
1184 Private->PxeBc.Mode->DhcpDiscoverValid = TRUE;
1185 }
1186
1187 return Status;
1188 }
1189
1190
1191 /**
1192 Unregister the address by Ip6Config protocol.
1193
1194 @param[in] Private The pointer to PXEBC_PRIVATE_DATA.
1195
1196 **/
1197 VOID
1198 PxeBcUnregisterIp6Address (
1199 IN PXEBC_PRIVATE_DATA *Private
1200 )
1201 {
1202 if (Private->Ip6Policy != PXEBC_IP6_POLICY_MAX) {
1203 //
1204 // PXE driver change the policy of IP6 driver, it's a chance to recover.
1205 // Keep the point and there is no enough requirements to do recovery.
1206 //
1207 }
1208 }
1209
1210
1211 /**
1212 Register the ready address by Ip6Config protocol.
1213
1214 @param[in] Private The pointer to PXEBC_PRIVATE_DATA.
1215 @param[in] Address The pointer to the ready address.
1216
1217 @retval EFI_SUCCESS Registered the address succesfully.
1218 @retval Others Failed to register the address.
1219
1220 **/
1221 EFI_STATUS
1222 PxeBcRegisterIp6Address (
1223 IN PXEBC_PRIVATE_DATA *Private,
1224 IN EFI_IPv6_ADDRESS *Address
1225 )
1226 {
1227 EFI_IP6_PROTOCOL *Ip6;
1228 EFI_IP6_CONFIG_PROTOCOL *Ip6Cfg;
1229 EFI_IP6_CONFIG_POLICY Policy;
1230 EFI_IP6_CONFIG_MANUAL_ADDRESS CfgAddr;
1231 UINTN DataSize;
1232 EFI_EVENT TimeOutEvt;
1233 EFI_EVENT MappedEvt;
1234 EFI_STATUS Status;
1235
1236 Status = EFI_SUCCESS;
1237 TimeOutEvt = NULL;
1238 MappedEvt = NULL;
1239 DataSize = sizeof (EFI_IP6_CONFIG_POLICY);
1240 Ip6Cfg = Private->Ip6Cfg;
1241 Ip6 = Private->Ip6;
1242
1243 ZeroMem (&CfgAddr, sizeof (EFI_IP6_CONFIG_MANUAL_ADDRESS));
1244 CopyMem (&CfgAddr.Address, Address, sizeof (EFI_IPv6_ADDRESS));
1245
1246 //
1247 // Get and store the current policy of IP6 driver.
1248 //
1249 Status = Ip6Cfg->GetData (
1250 Ip6Cfg,
1251 Ip6ConfigDataTypePolicy,
1252 &DataSize,
1253 &Private->Ip6Policy
1254 );
1255 if (EFI_ERROR (Status)) {
1256 goto ON_EXIT;
1257 }
1258
1259 //
1260 // There is no channel between IP6 and PXE driver about address setting,
1261 // so it has to set the new address by Ip6ConfigProtocol manually.
1262 //
1263 Policy = Ip6ConfigPolicyManual;
1264 Status = Ip6Cfg->SetData (
1265 Ip6Cfg,
1266 Ip6ConfigDataTypePolicy,
1267 sizeof(EFI_IP6_CONFIG_POLICY),
1268 &Policy
1269 );
1270 if (EFI_ERROR (Status)) {
1271 //
1272 // There is no need to recover later.
1273 //
1274 Private->Ip6Policy = PXEBC_IP6_POLICY_MAX;
1275 goto ON_EXIT;
1276 }
1277
1278 //
1279 // Create a timer as setting address timeout event since DAD in IP6 driver.
1280 //
1281 Status = gBS->CreateEvent (
1282 EVT_TIMER,
1283 TPL_CALLBACK,
1284 NULL,
1285 NULL,
1286 &TimeOutEvt
1287 );
1288 if (EFI_ERROR (Status)) {
1289 goto ON_EXIT;
1290 }
1291
1292 //
1293 // Create a notify event to set address flag when DAD if IP6 driver succeeded.
1294 //
1295 Status = gBS->CreateEvent (
1296 EVT_NOTIFY_SIGNAL,
1297 TPL_NOTIFY,
1298 PxeBcCommonNotify,
1299 &Private->IsAddressOk,
1300 &MappedEvt
1301 );
1302 if (EFI_ERROR (Status)) {
1303 goto ON_EXIT;
1304 }
1305
1306 Status = Ip6Cfg->RegisterDataNotify (
1307 Ip6Cfg,
1308 Ip6ConfigDataTypeManualAddress,
1309 MappedEvt
1310 );
1311 if (EFI_ERROR(Status)) {
1312 goto ON_EXIT;
1313 }
1314
1315 Status = Ip6Cfg->SetData (
1316 Ip6Cfg,
1317 Ip6ConfigDataTypeManualAddress,
1318 sizeof(EFI_IP6_CONFIG_MANUAL_ADDRESS),
1319 &CfgAddr
1320 );
1321 if (EFI_ERROR(Status) && Status != EFI_NOT_READY) {
1322 goto ON_EXIT;
1323 }
1324
1325 //
1326 // Start the 5 secondes timer to wait for setting address.
1327 //
1328 Status = EFI_NO_MAPPING;
1329 gBS->SetTimer (TimeOutEvt, TimerRelative, PXEBC_DHCP6_MAPPING_TIMEOUT);
1330
1331 while (EFI_ERROR (gBS->CheckEvent (TimeOutEvt))) {
1332 Ip6->Poll (Ip6);
1333 if (Private->IsAddressOk) {
1334 Status = EFI_SUCCESS;
1335 break;
1336 }
1337 }
1338
1339 ON_EXIT:
1340 if (MappedEvt != NULL) {
1341 Ip6Cfg->UnregisterDataNotify (
1342 Ip6Cfg,
1343 Ip6ConfigDataTypeManualAddress,
1344 MappedEvt
1345 );
1346 gBS->CloseEvent (MappedEvt);
1347 }
1348 if (TimeOutEvt != NULL) {
1349 gBS->CloseEvent (TimeOutEvt);
1350 }
1351 return Status;
1352 }
1353
1354
1355 /**
1356 EFI_DHCP6_CALLBACK is provided by the consumer of the EFI DHCPv6 Protocol driver
1357 to intercept events that occurred in the configuration process.
1358
1359 @param[in] This The pointer to the EFI DHCPv6 Protocol.
1360 @param[in] Context The pointer to the context set by EFI_DHCP6_PROTOCOL.Configure().
1361 @param[in] CurrentState The current operational state of the EFI DHCPv Protocol driver.
1362 @param[in] Dhcp6Event The event that occurs in the current state, which usually means a
1363 state transition.
1364 @param[in] Packet The DHCPv6 packet that is going to be sent or was already received.
1365 @param[out] NewPacket The packet that is used to replace the Packet above.
1366
1367 @retval EFI_SUCCESS Told the EFI DHCPv6 Protocol driver to continue the DHCP process.
1368 @retval EFI_NOT_READY Only used in the Dhcp6Selecting state. The EFI DHCPv6 Protocol
1369 driver will continue to wait for more packets.
1370 @retval EFI_ABORTED Told the EFI DHCPv6 Protocol driver to abort the current process.
1371
1372 **/
1373 EFI_STATUS
1374 EFIAPI
1375 PxeBcDhcp6CallBack (
1376 IN EFI_DHCP6_PROTOCOL *This,
1377 IN VOID *Context,
1378 IN EFI_DHCP6_STATE CurrentState,
1379 IN EFI_DHCP6_EVENT Dhcp6Event,
1380 IN EFI_DHCP6_PACKET *Packet,
1381 OUT EFI_DHCP6_PACKET **NewPacket OPTIONAL
1382 )
1383 {
1384 PXEBC_PRIVATE_DATA *Private;
1385 EFI_PXE_BASE_CODE_MODE *Mode;
1386 EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL *Callback;
1387 EFI_DHCP6_PACKET *SelectAd;
1388 EFI_STATUS Status;
1389 BOOLEAN Received;
1390
1391 if ((Dhcp6Event != Dhcp6RcvdAdvertise) &&
1392 (Dhcp6Event != Dhcp6SelectAdvertise) &&
1393 (Dhcp6Event != Dhcp6SendSolicit) &&
1394 (Dhcp6Event != Dhcp6SendRequest) &&
1395 (Dhcp6Event != Dhcp6RcvdReply)) {
1396 return EFI_SUCCESS;
1397 }
1398
1399 ASSERT (Packet != NULL);
1400
1401 Private = (PXEBC_PRIVATE_DATA *) Context;
1402 Mode = Private->PxeBc.Mode;
1403 Callback = Private->PxeBcCallback;
1404
1405 //
1406 // Callback to user when any traffic ocurred if has.
1407 //
1408 if (Dhcp6Event != Dhcp6SelectAdvertise && Callback != NULL) {
1409 Received = (BOOLEAN) (Dhcp6Event == Dhcp6RcvdAdvertise || Dhcp6Event == Dhcp6RcvdReply);
1410 Status = Callback->Callback (
1411 Callback,
1412 Private->Function,
1413 Received,
1414 Packet->Length,
1415 (EFI_PXE_BASE_CODE_PACKET *) &Packet->Dhcp6
1416 );
1417 if (Status != EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE) {
1418 return EFI_ABORTED;
1419 }
1420 }
1421
1422 Status = EFI_SUCCESS;
1423
1424 switch (Dhcp6Event) {
1425
1426 case Dhcp6SendSolicit:
1427 //
1428 // Record the first Solicate msg time
1429 //
1430 if (Private->SolicitTimes == 0) {
1431 CalcElapsedTime (Private);
1432 Private->SolicitTimes++;
1433 }
1434 //
1435 // Cache the dhcp discover packet to mode data directly.
1436 //
1437 CopyMem (&Mode->DhcpDiscover.Dhcpv4, &Packet->Dhcp6, Packet->Length);
1438 break;
1439
1440 case Dhcp6RcvdAdvertise:
1441 Status = EFI_NOT_READY;
1442 if (Private->OfferNum < PXEBC_OFFER_MAX_NUM) {
1443 //
1444 // Cache the dhcp offers to OfferBuffer[] for select later, and record
1445 // the OfferIndex and OfferCount.
1446 //
1447 PxeBcCacheDhcp6Offer (Private, Packet);
1448 }
1449 break;
1450
1451 case Dhcp6SendRequest:
1452 //
1453 // Store the request packet as seed packet for discover.
1454 //
1455 if (Private->Dhcp6Request != NULL) {
1456 FreePool (Private->Dhcp6Request);
1457 }
1458 Private->Dhcp6Request = AllocateZeroPool (Packet->Size);
1459 if (Private->Dhcp6Request != NULL) {
1460 CopyMem (Private->Dhcp6Request, Packet, Packet->Size);
1461 }
1462 break;
1463
1464 case Dhcp6SelectAdvertise:
1465 //
1466 // Select offer by the default policy or by order, and record the SelectIndex
1467 // and SelectProxyType.
1468 //
1469 PxeBcSelectDhcp6Offer (Private);
1470
1471 if (Private->SelectIndex == 0) {
1472 Status = EFI_ABORTED;
1473 } else {
1474 ASSERT (NewPacket != NULL);
1475 SelectAd = &Private->OfferBuffer[Private->SelectIndex - 1].Dhcp6.Packet.Offer;
1476 *NewPacket = AllocateZeroPool (SelectAd->Size);
1477 ASSERT (*NewPacket != NULL);
1478 CopyMem (*NewPacket, SelectAd, SelectAd->Size);
1479 }
1480 break;
1481
1482 case Dhcp6RcvdReply:
1483 //
1484 // Cache the dhcp ack to Private->Dhcp6Ack, but it's not the final ack in mode data
1485 // without verification.
1486 //
1487 ASSERT (Private->SelectIndex != 0);
1488 PxeBcCopyDhcp6Ack (Private, Packet, FALSE);
1489 break;
1490
1491 default:
1492 ASSERT (0);
1493 }
1494
1495 return Status;
1496 }
1497
1498
1499 /**
1500 Build and send out the request packet for the bootfile, and parse the reply.
1501
1502 @param[in] Private The pointer to PxeBc private data.
1503 @param[in] Type PxeBc option boot item type.
1504 @param[in] Layer The pointer to option boot item layer.
1505 @param[in] UseBis Use BIS or not.
1506 @param[in] DestIp The pointer to the server address.
1507
1508 @retval EFI_SUCCESS Successfully discovered the boot file.
1509 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
1510 @retval EFI_NOT_FOUND Can't get the PXE reply packet.
1511 @retval Others Failed to discover the boot file.
1512
1513 **/
1514 EFI_STATUS
1515 PxeBcDhcp6Discover (
1516 IN PXEBC_PRIVATE_DATA *Private,
1517 IN UINT16 Type,
1518 IN UINT16 *Layer,
1519 IN BOOLEAN UseBis,
1520 IN EFI_IP_ADDRESS *DestIp
1521 )
1522 {
1523 EFI_PXE_BASE_CODE_UDP_PORT SrcPort;
1524 EFI_PXE_BASE_CODE_UDP_PORT DestPort;
1525 EFI_PXE_BASE_CODE_MODE *Mode;
1526 EFI_PXE_BASE_CODE_PROTOCOL *PxeBc;
1527 EFI_PXE_BASE_CODE_DHCPV6_PACKET *Discover;
1528 UINTN DiscoverLen;
1529 EFI_DHCP6_PACKET *Request;
1530 UINTN RequestLen;
1531 EFI_DHCP6_PACKET *Reply;
1532 UINT8 *RequestOpt;
1533 UINT8 *DiscoverOpt;
1534 UINTN ReadSize;
1535 UINT16 OpFlags;
1536 UINT16 OpCode;
1537 UINT16 OpLen;
1538 UINT32 Xid;
1539 EFI_STATUS Status;
1540
1541 PxeBc = &Private->PxeBc;
1542 Mode = PxeBc->Mode;
1543 Request = Private->Dhcp6Request;
1544 SrcPort = PXEBC_BS_DISCOVER_PORT;
1545 DestPort = PXEBC_BS_DISCOVER_PORT;
1546 OpFlags = 0;
1547
1548 if (!UseBis && Layer != NULL) {
1549 *Layer &= EFI_PXE_BASE_CODE_BOOT_LAYER_MASK;
1550 }
1551
1552 if (Request == NULL) {
1553 return EFI_DEVICE_ERROR;
1554 }
1555
1556 Discover = AllocateZeroPool (sizeof (EFI_PXE_BASE_CODE_DHCPV6_PACKET));
1557 if (Discover == NULL) {
1558 return EFI_OUT_OF_RESOURCES;
1559 }
1560
1561 //
1562 // Build the discover packet by the cached request packet before.
1563 //
1564 Xid = NET_RANDOM (NetRandomInitSeed ());
1565 Discover->TransactionId = HTONL (Xid);
1566 Discover->MessageType = Request->Dhcp6.Header.MessageType;
1567 RequestOpt = Request->Dhcp6.Option;
1568 DiscoverOpt = Discover->DhcpOptions;
1569 DiscoverLen = sizeof (EFI_DHCP6_HEADER);
1570 RequestLen = DiscoverLen;
1571
1572 while (RequestLen < Request->Length) {
1573 OpCode = NTOHS (((EFI_DHCP6_PACKET_OPTION *) RequestOpt)->OpCode);
1574 OpLen = NTOHS (((EFI_DHCP6_PACKET_OPTION *) RequestOpt)->OpLen);
1575 if (OpCode != EFI_DHCP6_IA_TYPE_NA &&
1576 OpCode != EFI_DHCP6_IA_TYPE_TA) {
1577 //
1578 // Copy all the options except IA option.
1579 //
1580 CopyMem (DiscoverOpt, RequestOpt, OpLen + 4);
1581 DiscoverOpt += (OpLen + 4);
1582 DiscoverLen += (OpLen + 4);
1583 }
1584 RequestOpt += (OpLen + 4);
1585 RequestLen += (OpLen + 4);
1586 }
1587
1588 Status = PxeBc->UdpWrite (
1589 PxeBc,
1590 OpFlags,
1591 &Private->ServerIp,
1592 &DestPort,
1593 NULL,
1594 &Private->StationIp,
1595 &SrcPort,
1596 NULL,
1597 NULL,
1598 &DiscoverLen,
1599 (VOID *) Discover
1600 );
1601 if (EFI_ERROR (Status)) {
1602 return Status;
1603 }
1604
1605 //
1606 // Cache the right PXE reply packet here, set valid flag later.
1607 // Especially for PXE discover packet, store it into mode data here.
1608 //
1609 if (Private->IsDoDiscover) {
1610 CopyMem (&Mode->PxeDiscover.Dhcpv6, Discover, DiscoverLen);
1611 Reply = &Private->PxeReply.Dhcp6.Packet.Ack;
1612 } else {
1613 Reply = &Private->ProxyOffer.Dhcp6.Packet.Offer;
1614 }
1615 ReadSize = (UINTN) Reply->Size;
1616
1617 Status = PxeBc->UdpRead (
1618 PxeBc,
1619 OpFlags,
1620 &Private->StationIp,
1621 &SrcPort,
1622 &Private->ServerIp,
1623 &DestPort,
1624 NULL,
1625 NULL,
1626 &ReadSize,
1627 (VOID *) &Reply->Dhcp6
1628 );
1629 if (EFI_ERROR (Status)) {
1630 return Status;
1631 }
1632
1633 return EFI_SUCCESS;
1634 }
1635
1636
1637 /**
1638 Start the DHCPv6 S.A.R.R. process to acquire the IPv6 address and other PXE boot information.
1639
1640 @param[in] Private The pointer to PxeBc private data.
1641 @param[in] Dhcp6 The pointer to the EFI_DHCP6_PROTOCOL
1642
1643 @retval EFI_SUCCESS The S.A.R.R. process successfully finished.
1644 @retval Others Failed to finish the S.A.R.R. process.
1645
1646 **/
1647 EFI_STATUS
1648 PxeBcDhcp6Sarr (
1649 IN PXEBC_PRIVATE_DATA *Private,
1650 IN EFI_DHCP6_PROTOCOL *Dhcp6
1651 )
1652 {
1653 EFI_PXE_BASE_CODE_MODE *PxeMode;
1654 EFI_DHCP6_CONFIG_DATA Config;
1655 EFI_DHCP6_MODE_DATA Mode;
1656 EFI_DHCP6_RETRANSMISSION *Retransmit;
1657 EFI_DHCP6_PACKET_OPTION *OptList[PXEBC_DHCP6_OPTION_MAX_NUM];
1658 UINT8 Buffer[PXEBC_DHCP6_OPTION_MAX_SIZE];
1659 UINT32 OptCount;
1660 EFI_STATUS Status;
1661
1662 Status = EFI_SUCCESS;
1663 PxeMode = Private->PxeBc.Mode;
1664
1665 //
1666 // Build option list for the request packet.
1667 //
1668 OptCount = PxeBcBuildDhcp6Options (Private, OptList, Buffer);
1669 ASSERT (OptCount> 0);
1670
1671 Retransmit = AllocateZeroPool (sizeof (EFI_DHCP6_RETRANSMISSION));
1672 if (Retransmit == NULL) {
1673 return EFI_OUT_OF_RESOURCES;
1674 }
1675
1676 ZeroMem (&Mode, sizeof (EFI_DHCP6_MODE_DATA));
1677 ZeroMem (&Config, sizeof (EFI_DHCP6_CONFIG_DATA));
1678
1679 Config.OptionCount = OptCount;
1680 Config.OptionList = OptList;
1681 Config.Dhcp6Callback = PxeBcDhcp6CallBack;
1682 Config.CallbackContext = Private;
1683 Config.IaInfoEvent = NULL;
1684 Config.RapidCommit = FALSE;
1685 Config.ReconfigureAccept = FALSE;
1686 Config.IaDescriptor.IaId = 1;
1687 Config.IaDescriptor.Type = EFI_DHCP6_IA_TYPE_NA;
1688 Config.SolicitRetransmission = Retransmit;
1689 Retransmit->Irt = 4;
1690 Retransmit->Mrc = 4;
1691 Retransmit->Mrt = 32;
1692 Retransmit->Mrd = 60;
1693
1694 //
1695 // Configure the DHCPv6 instance for PXE boot.
1696 //
1697 Status = Dhcp6->Configure (Dhcp6, &Config);
1698 if (EFI_ERROR (Status)) {
1699 FreePool (Retransmit);
1700 return Status;
1701 }
1702
1703 //
1704 // Initialize the record fields for DHCPv6 offer in private data.
1705 //
1706 Private->IsProxyRecved = FALSE;
1707 Private->OfferNum = 0;
1708 Private->SelectIndex = 0;
1709 ZeroMem (Private->OfferCount, sizeof (Private->OfferCount));
1710 ZeroMem (Private->OfferIndex, sizeof (Private->OfferIndex));
1711
1712
1713 //
1714 // Start DHCPv6 S.A.R.R. process to acquire IPv6 address.
1715 //
1716 Status = Dhcp6->Start (Dhcp6);
1717 if (EFI_ERROR (Status)) {
1718 if (Status == EFI_ICMP_ERROR) {
1719 PxeMode->IcmpErrorReceived = TRUE;
1720 }
1721 Dhcp6->Configure (Dhcp6, NULL);
1722 return Status;
1723 }
1724
1725 //
1726 // Get the acquired IPv6 address and store them.
1727 //
1728 Status = Dhcp6->GetModeData (Dhcp6, &Mode, NULL);
1729 if (EFI_ERROR (Status)) {
1730 Dhcp6->Stop (Dhcp6);
1731 return Status;
1732 }
1733
1734 ASSERT (Mode.Ia->State == Dhcp6Bound);
1735 CopyMem (&Private->StationIp.v6, &Mode.Ia->IaAddress[0].IpAddress, sizeof (EFI_IPv6_ADDRESS));
1736 CopyMem (&PxeMode->StationIp.v6, &Private->StationIp.v6, sizeof (EFI_IPv6_ADDRESS));
1737
1738 Status = PxeBcRegisterIp6Address (Private, &Private->StationIp.v6);
1739 if (EFI_ERROR (Status)) {
1740 Dhcp6->Stop (Dhcp6);
1741 return Status;
1742 }
1743
1744 Status = PxeBcFlushStaionIp (Private, &Private->StationIp, NULL);
1745 if (EFI_ERROR (Status)) {
1746 PxeBcUnregisterIp6Address (Private);
1747 Dhcp6->Stop (Dhcp6);
1748 return Status;
1749 }
1750
1751 //
1752 // Check the selected offer whether BINL retry is needed.
1753 //
1754 Status = PxeBcHandleDhcp6Offer (Private);
1755 if (EFI_ERROR (Status)) {
1756 PxeBcUnregisterIp6Address (Private);
1757 Dhcp6->Stop (Dhcp6);
1758 return Status;
1759 }
1760
1761 AsciiPrint ("\n Station IP address is ");
1762
1763 PxeBcShowIp6Addr (&Private->StationIp.v6);
1764
1765 return EFI_SUCCESS;
1766 }
1767