Use siaddr in DHCP packet, if zero, use option 54 instead.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / UefiPxeBcDxe / PxeBcDhcp.c
CommitLineData
dc361cc5 1/** @file\r
f737cfb9 2 Support for PxeBc dhcp functions.\r
3 \r
6d3ea23f 4Copyright (c) 2007 - 2009, Intel Corporation.<BR> \r
dc361cc5 5All rights reserved. This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
dc361cc5 13**/\r
14\r
15\r
16#include "PxeBcImpl.h"\r
17\r
18//\r
19// This is a map from the interested DHCP4 option tags' index to the tag value.\r
20//\r
21UINT8 mInterestedDhcp4Tags[PXEBC_DHCP4_TAG_INDEX_MAX] = {\r
22 PXEBC_DHCP4_TAG_BOOTFILE_LEN,\r
23 PXEBC_DHCP4_TAG_VENDOR,\r
24 PXEBC_DHCP4_TAG_OVERLOAD,\r
25 PXEBC_DHCP4_TAG_MSG_TYPE,\r
26 PXEBC_DHCP4_TAG_SERVER_ID,\r
27 PXEBC_DHCP4_TAG_CLASS_ID,\r
28 PXEBC_DHCP4_TAG_BOOTFILE\r
29};\r
30\r
31\r
32/**\r
f737cfb9 33 This function initialize the DHCP4 message instance.\r
dc361cc5 34\r
f737cfb9 35 This function will pad each item of dhcp4 message packet.\r
36 \r
37 @param Seed Pointer to the message instance of the DHCP4 packet.\r
38 @param Udp4 Pointer to the EFI_UDP4_PROTOCOL instance.\r
dc361cc5 39\r
dc361cc5 40**/\r
41VOID\r
42PxeBcInitSeedPacket (\r
43 IN EFI_DHCP4_PACKET *Seed,\r
44 IN EFI_UDP4_PROTOCOL *Udp4\r
45 )\r
46{\r
47 EFI_SIMPLE_NETWORK_MODE Mode;\r
48 EFI_DHCP4_HEADER *Header;\r
49\r
50 Udp4->GetModeData (Udp4, NULL, NULL, NULL, &Mode);\r
51\r
52 Seed->Size = sizeof (EFI_DHCP4_PACKET);\r
53 Seed->Length = sizeof (Seed->Dhcp4);\r
54\r
55 Header = &Seed->Dhcp4.Header;\r
56\r
e48e37fc 57 ZeroMem (Header, sizeof (EFI_DHCP4_HEADER));\r
dc361cc5 58 Header->OpCode = PXEBC_DHCP4_OPCODE_REQUEST;\r
59 Header->HwType = Mode.IfType;\r
60 Header->HwAddrLen = (UINT8) Mode.HwAddressSize;\r
e48e37fc 61 CopyMem (Header->ClientHwAddr, &Mode.CurrentAddress, Header->HwAddrLen);\r
dc361cc5 62\r
63 Seed->Dhcp4.Magik = PXEBC_DHCP4_MAGIC;\r
64 Seed->Dhcp4.Option[0] = PXEBC_DHCP4_TAG_EOP;\r
65}\r
66\r
67\r
68/**\r
f737cfb9 69 Copy the DCHP4 packet from srouce to destination.\r
dc361cc5 70\r
f737cfb9 71 @param Dst Pointer to the EFI_DHCP4_PROTOCOL instance.\r
72 @param Src Pointer to the EFI_DHCP4_PROTOCOL instance.\r
dc361cc5 73\r
dc361cc5 74**/\r
75VOID\r
76PxeBcCopyEfiDhcp4Packet (\r
77 IN EFI_DHCP4_PACKET *Dst,\r
78 IN EFI_DHCP4_PACKET *Src\r
79 )\r
80{\r
81 ASSERT (Dst->Size >= Src->Length);\r
82\r
e48e37fc 83 CopyMem (&Dst->Dhcp4, &Src->Dhcp4, Src->Length);\r
dc361cc5 84 Dst->Length = Src->Length;\r
85}\r
86\r
87\r
88/**\r
f737cfb9 89 Copy the dhcp4 packet to the PxeBc private data and parse the dhcp4 packet.\r
dc361cc5 90\r
f737cfb9 91 @param Private Pointer to PxeBc private data.\r
92 @param OfferIndex Index of cached packets as complements of pxe mode data,\r
93 the index is maximum offer number.\r
dc361cc5 94\r
dc361cc5 95**/\r
96VOID\r
97PxeBcCopyProxyOffer (\r
98 IN PXEBC_PRIVATE_DATA *Private,\r
99 IN UINT32 OfferIndex\r
100 )\r
101{\r
102 EFI_PXE_BASE_CODE_MODE *Mode;\r
103 EFI_DHCP4_PACKET *Offer;\r
104\r
105 ASSERT (OfferIndex < Private->NumOffers);\r
890986ca 106 ASSERT (OfferIndex < PXEBC_MAX_OFFER_NUM);\r
dc361cc5 107\r
108 Mode = Private->PxeBc.Mode;\r
109 Offer = &Private->Dhcp4Offers[OfferIndex].Packet.Offer;\r
110\r
111 PxeBcCopyEfiDhcp4Packet (&Private->ProxyOffer.Packet.Offer, Offer);\r
e48e37fc 112 CopyMem (&Mode->ProxyOffer, &Offer->Dhcp4, Offer->Length);\r
dc361cc5 113 Mode->ProxyOfferReceived = TRUE;\r
114\r
115 PxeBcParseCachedDhcpPacket (&Private->ProxyOffer);\r
116}\r
117\r
118\r
119/**\r
120 Parse the cached dhcp packet.\r
121\r
f737cfb9 122 @param CachedPacket Pointer to cached dhcp packet.\r
dc361cc5 123\r
f737cfb9 124 @retval TRUE Succeed to parse and validation.\r
125 @retval FALSE Fail to parse or validation.\r
dc361cc5 126\r
127**/\r
128BOOLEAN\r
129PxeBcParseCachedDhcpPacket (\r
130 IN PXEBC_CACHED_DHCP4_PACKET *CachedPacket\r
131 )\r
132{\r
133 EFI_DHCP4_PACKET *Offer;\r
134 EFI_DHCP4_PACKET_OPTION **Options;\r
135 EFI_DHCP4_PACKET_OPTION *Option;\r
136 UINT8 OfferType;\r
137 UINTN Index;\r
138\r
139 CachedPacket->IsPxeOffer = FALSE;\r
e48e37fc 140 ZeroMem (CachedPacket->Dhcp4Option, sizeof (CachedPacket->Dhcp4Option));\r
141 ZeroMem (&CachedPacket->PxeVendorOption, sizeof (CachedPacket->PxeVendorOption));\r
dc361cc5 142\r
143 Offer = &CachedPacket->Packet.Offer;\r
144 Options = CachedPacket->Dhcp4Option;\r
145\r
146 //\r
147 // Parse interested dhcp options and store their pointers in CachedPacket->Dhcp4Option.\r
148 //\r
149 for (Index = 0; Index < PXEBC_DHCP4_TAG_INDEX_MAX; Index++) {\r
150 Options[Index] = PxeBcParseExtendOptions (\r
151 Offer->Dhcp4.Option,\r
152 GET_OPTION_BUFFER_LEN (Offer),\r
153 mInterestedDhcp4Tags[Index]\r
154 );\r
155 }\r
156\r
157 //\r
158 // Check whether is an offer with PXEClient or not.\r
159 //\r
160 Option = Options[PXEBC_DHCP4_TAG_INDEX_CLASS_ID];\r
161 if ((Option != NULL) && (Option->Length >= 9) &&\r
e48e37fc 162 (CompareMem (Option->Data, DEFAULT_CLASS_ID_DATA, 9) == 0)) {\r
dc361cc5 163\r
164 CachedPacket->IsPxeOffer = TRUE;\r
165 }\r
166\r
167 //\r
168 // Parse pxe vendor options and store their content/pointers in CachedPacket->PxeVendorOption.\r
169 //\r
170 Option = Options[PXEBC_DHCP4_TAG_INDEX_VENDOR];\r
171 if (CachedPacket->IsPxeOffer && (Option != NULL)) {\r
172\r
173 if (!PxeBcParseVendorOptions (Option, &CachedPacket->PxeVendorOption)) {\r
174 return FALSE;\r
175 }\r
176 }\r
177\r
178 //\r
179 // Check whether bootfilename/serverhostname overloaded (See details in dhcp spec).\r
180 // If overloaded, parse this buffer as nested dhcp options, or just parse bootfilename/\r
181 // serverhostname option.\r
182 //\r
183 Option = Options[PXEBC_DHCP4_TAG_INDEX_OVERLOAD];\r
184 if ((Option != NULL) && (Option->Data[0] & PXEBC_DHCP4_OVERLOAD_FILE)) {\r
185\r
186 Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] = PxeBcParseExtendOptions (\r
187 (UINT8 *) Offer->Dhcp4.Header.BootFileName,\r
188 sizeof (Offer->Dhcp4.Header.BootFileName),\r
189 PXEBC_DHCP4_TAG_BOOTFILE\r
190 );\r
191\r
192 } else if ((Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] == NULL) &&\r
193 (Offer->Dhcp4.Header.BootFileName[0] != 0)) {\r
194 //\r
195 // If the bootfile is not present and bootfilename is present in dhcp packet, just parse it.\r
196 // And do not count dhcp option header, or else will destory the serverhostname.\r
197 //\r
198 Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] = (EFI_DHCP4_PACKET_OPTION *) (&Offer->Dhcp4.Header.BootFileName[0] -\r
f3f2e05d 199 OFFSET_OF (EFI_DHCP4_PACKET_OPTION, Data[0]));\r
dc361cc5 200\r
201 }\r
202\r
203 //\r
204 // Determine offer type of the dhcp packet.\r
205 //\r
206 Option = Options[PXEBC_DHCP4_TAG_INDEX_MSG_TYPE];\r
207 if ((Option == NULL) || (Option->Data[0] == 0)) {\r
208 //\r
209 // It's a bootp offer\r
210 //\r
211 Option = CachedPacket->Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_BOOTFILE];\r
212 if (Option == NULL) {\r
213 //\r
214 // bootp offer without bootfilename, discard it.\r
215 //\r
216 return FALSE;\r
217 }\r
218\r
219 OfferType = DHCP4_PACKET_TYPE_BOOTP;\r
220\r
221 } else {\r
222\r
223 if (IS_VALID_DISCOVER_VENDOR_OPTION (CachedPacket->PxeVendorOption.BitMap)) {\r
224 //\r
225 // It's a pxe10 offer with PXEClient and discover vendor option.\r
226 //\r
227 OfferType = DHCP4_PACKET_TYPE_PXE10;\r
228 } else if (IS_VALID_MTFTP_VENDOR_OPTION (CachedPacket->PxeVendorOption.BitMap)) {\r
229 //\r
230 // It's a wfm11a offer with PXEClient and mtftp vendor option, and\r
231 // return false since mtftp not supported currently.\r
232 //\r
233 return FALSE;\r
dc361cc5 234 } else {\r
235 //\r
236 // If the binl offer with only PXEClient.\r
237 //\r
238 OfferType = (UINT8) ((CachedPacket->IsPxeOffer) ? DHCP4_PACKET_TYPE_BINL : DHCP4_PACKET_TYPE_DHCP_ONLY);\r
239 }\r
240 }\r
241\r
242 CachedPacket->OfferType = OfferType;\r
243\r
244 return TRUE;\r
245}\r
246\r
247\r
248/**\r
f737cfb9 249 Offer dhcp service with a BINL dhcp offer.\r
dc361cc5 250\r
f737cfb9 251 @param Private Pointer to PxeBc private data.\r
252 @param Index Index of cached packets as complements of pxe mode data,\r
253 the index is maximum offer number.\r
dc361cc5 254\r
f737cfb9 255 @retval TRUE Offer the service successfully under priority BINL.\r
256 @retval FALSE Boot Service failed, parse cached dhcp packet failed or this \r
257 BINL ack cannot find options set or bootfile name specified.\r
258 \r
dc361cc5 259**/\r
260BOOLEAN\r
261PxeBcTryBinl (\r
262 IN PXEBC_PRIVATE_DATA *Private,\r
263 IN UINT32 Index\r
264 )\r
265{\r
266 EFI_DHCP4_PACKET *Offer;\r
267 EFI_IP_ADDRESS ServerIp;\r
268 EFI_STATUS Status;\r
269 PXEBC_CACHED_DHCP4_PACKET *CachedPacket;\r
270 EFI_DHCP4_PACKET *Reply;\r
271\r
894d038a 272 ASSERT (Index < PXEBC_MAX_OFFER_NUM);\r
dc361cc5 273 ASSERT (Private->Dhcp4Offers[Index].OfferType == DHCP4_PACKET_TYPE_BINL);\r
274\r
275 Offer = &Private->Dhcp4Offers[Index].Packet.Offer;\r
319075ff 276\r
277 //\r
2ce5c88a 278 // Use siaddr(next server) in DHCPOFFER packet header, if zero, use option 54(server identifier)\r
279 // in DHCPOFFER packet.\r
280 // (It does not comply with PXE Spec, Ver2.1)\r
319075ff 281 //\r
2ce5c88a 282 if (EFI_IP4_EQUAL (&Offer->Dhcp4.Header.ServerAddr.Addr, &mZeroIp4Addr)) {\r
e48e37fc 283 CopyMem (\r
dc361cc5 284 &ServerIp.Addr[0],\r
285 Private->Dhcp4Offers[Index].Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_SERVER_ID]->Data,\r
286 sizeof (EFI_IPv4_ADDRESS)\r
287 );\r
288 } else {\r
319075ff 289 CopyMem (\r
290 &ServerIp.Addr[0], \r
291 &Offer->Dhcp4.Header.ServerAddr, \r
292 sizeof (EFI_IPv4_ADDRESS)\r
293 );\r
294 }\r
295 if (ServerIp.Addr[0] == 0) {\r
296 return FALSE;\r
dc361cc5 297 }\r
298\r
299 CachedPacket = &Private->ProxyOffer;\r
300 Reply = &CachedPacket->Packet.Offer;\r
301\r
302 Status = PxeBcDiscvBootService (\r
303 Private,\r
304 0,\r
305 NULL,\r
306 FALSE,\r
307 &ServerIp,\r
308 0,\r
309 NULL,\r
310 FALSE,\r
311 Reply\r
312 );\r
313 if (EFI_ERROR (Status)) {\r
314 return FALSE;\r
315 }\r
316\r
317 if (!PxeBcParseCachedDhcpPacket (CachedPacket)) {\r
318 return FALSE;\r
319 }\r
320\r
321 if ((CachedPacket->OfferType != DHCP4_PACKET_TYPE_PXE10) &&\r
322 (CachedPacket->Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] == NULL)) {\r
323 //\r
324 // This BINL ack doesn't have discovery options set or bootfile name\r
325 // specified.\r
326 //\r
327 return FALSE;\r
328 }\r
329\r
330 Private->PxeBc.Mode->ProxyOfferReceived = TRUE;\r
e48e37fc 331 CopyMem (&Private->PxeBc.Mode->ProxyOffer, &Reply->Dhcp4, Reply->Length);\r
dc361cc5 332\r
333 return TRUE;\r
334}\r
335\r
336\r
337/**\r
f737cfb9 338 Offer dhcp service for each proxy with a BINL dhcp offer.\r
dc361cc5 339\r
f737cfb9 340 @param Private Pointer to PxeBc private data\r
341 @param OfferIndex Pointer to the index of cached packets as complements of \r
342 pxe mode data, the index is maximum offer number.\r
dc361cc5 343\r
f737cfb9 344 @return If there is no service needed offer return FALSE, otherwise TRUE.\r
dc361cc5 345\r
346**/\r
347BOOLEAN\r
348PxeBcTryBinlProxy (\r
349 IN PXEBC_PRIVATE_DATA *Private,\r
350 OUT UINT32 *OfferIndex\r
351 )\r
352{\r
353 UINT32 Index;\r
354\r
355 for (Index = 0; Index < Private->ProxyIndex[DHCP4_PACKET_TYPE_BINL]; Index++) {\r
356\r
357 *OfferIndex = Private->BinlIndex[Index];\r
358 //\r
359 // Try this BINL proxy offer\r
360 //\r
361 if (PxeBcTryBinl (Private, *OfferIndex)) {\r
362 return TRUE;\r
363 }\r
364 }\r
365\r
366 return FALSE;\r
367}\r
368\r
369\r
370/**\r
f737cfb9 371 This function is to check the selected proxy offer (include BINL dhcp offer and\r
372 DHCP_ONLY offer ) and set the flag and copy the DHCP packets to the Pxe base code\r
373 mode structure.\r
dc361cc5 374\r
f737cfb9 375 @param Private Pointer to PxeBc private data.\r
dc361cc5 376\r
f737cfb9 377 @retval EFI_SUCCESS Operational successful.\r
378 @retval EFI_NO_RESPONSE Offer dhcp service failed.\r
dc361cc5 379\r
380**/\r
381EFI_STATUS\r
382PxeBcCheckSelectedOffer (\r
383 IN PXEBC_PRIVATE_DATA *Private\r
384 )\r
385{\r
386 PXEBC_CACHED_DHCP4_PACKET *SelectedOffer;\r
387 EFI_DHCP4_PACKET_OPTION **Options;\r
388 UINT32 Index;\r
389 EFI_DHCP4_PACKET *Offer;\r
390 UINT32 ProxyOfferIndex;\r
391 EFI_STATUS Status;\r
392 EFI_PXE_BASE_CODE_MODE *Mode;\r
393 EFI_DHCP4_PACKET *Ack;\r
394\r
395 ASSERT (Private->SelectedOffer != 0);\r
396\r
397 Status = EFI_SUCCESS;\r
398 SelectedOffer = &Private->Dhcp4Offers[Private->SelectedOffer - 1];\r
399 Options = SelectedOffer->Dhcp4Option;\r
400\r
401 if (SelectedOffer->OfferType == DHCP4_PACKET_TYPE_BINL) {\r
402 //\r
403 // The addresses are acquired from a BINL dhcp offer, try BINL to get\r
404 // the bootfile name\r
405 //\r
406 if (!PxeBcTryBinl (Private, Private->SelectedOffer - 1)) {\r
407 Status = EFI_NO_RESPONSE;\r
408 }\r
05c0e3cb 409 } else if (SelectedOffer->OfferType == DHCP4_PACKET_TYPE_DHCP_ONLY) {\r
dc361cc5 410 //\r
05c0e3cb 411 // The selected offer to finish the D.O.R.A. is a DHCP only offer, we need \r
412 // try proxy offers if there are some, othewise the bootfile name must be \r
413 // set in this DHCP only offer.\r
dc361cc5 414 //\r
05c0e3cb 415 if (Private->GotProxyOffer) {\r
dc361cc5 416 //\r
05c0e3cb 417 // Get rid of the compiler warning.\r
dc361cc5 418 //\r
05c0e3cb 419 ProxyOfferIndex = 0;\r
420 if (Private->SortOffers) {\r
dc361cc5 421 //\r
05c0e3cb 422 // The offers are sorted before selecting, the proxy offer type must be\r
423 // already determined.\r
dc361cc5 424 //\r
05c0e3cb 425 ASSERT (Private->ProxyIndex[Private->ProxyOfferType] > 0);\r
426\r
427 if (Private->ProxyOfferType == DHCP4_PACKET_TYPE_BINL) {\r
428 //\r
429 // We buffer all received BINL proxy offers, try them all one by one\r
430 //\r
431 if (!PxeBcTryBinlProxy (Private, &ProxyOfferIndex)) {\r
432 Status = EFI_NO_RESPONSE;\r
433 }\r
434 } else {\r
435 //\r
436 // For other types, only one proxy offer is buffered.\r
437 //\r
438 ProxyOfferIndex = Private->ProxyIndex[Private->ProxyOfferType] - 1;\r
dc361cc5 439 }\r
440 } else {\r
441 //\r
05c0e3cb 442 // The proxy offer type is not determined, choose proxy offer in the \r
443 // received order.\r
dc361cc5 444 //\r
05c0e3cb 445 Status = EFI_NO_RESPONSE;\r
dc361cc5 446\r
05c0e3cb 447 for (Index = 0; Index < Private->NumOffers; Index++) {\r
dc361cc5 448\r
05c0e3cb 449 Offer = &Private->Dhcp4Offers[Index].Packet.Offer;\r
450 if (!IS_PROXY_DHCP_OFFER (Offer)) {\r
dc361cc5 451 //\r
05c0e3cb 452 // Skip non proxy dhcp offers.\r
dc361cc5 453 //\r
454 continue;\r
455 }\r
dc361cc5 456\r
05c0e3cb 457 if (Private->Dhcp4Offers[Index].OfferType == DHCP4_PACKET_TYPE_BINL) {\r
458 //\r
459 // Try BINL\r
460 //\r
461 if (!PxeBcTryBinl (Private, Index)) {\r
462 //\r
463 // Failed, skip to the next offer\r
464 //\r
465 continue;\r
466 }\r
467 }\r
468\r
469 Private->ProxyOfferType = Private->Dhcp4Offers[Index].OfferType;\r
470 ProxyOfferIndex = Index;\r
471 Status = EFI_SUCCESS;\r
472 break;\r
473 }\r
dc361cc5 474 }\r
dc361cc5 475\r
05c0e3cb 476 if (!EFI_ERROR (Status) && (Private->ProxyOfferType != DHCP4_PACKET_TYPE_BINL)) {\r
477 //\r
478 // Copy the proxy offer to Mode and set the flag\r
479 //\r
480 PxeBcCopyProxyOffer (Private, ProxyOfferIndex);\r
481 }\r
482 } else {\r
dc361cc5 483 //\r
05c0e3cb 484 // No proxy offer is received, the bootfile name MUST be set.\r
dc361cc5 485 //\r
05c0e3cb 486 ASSERT (Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] != NULL);\r
dc361cc5 487 }\r
488 }\r
489\r
490 if (!EFI_ERROR (Status)) {\r
491 //\r
492 // Everything is OK, set the flag and copy the DHCP packets.\r
493 //\r
494 Mode = Private->PxeBc.Mode;\r
495 Offer = &SelectedOffer->Packet.Offer;\r
496\r
497 //\r
498 // The discover packet is already copied, just set flag here.\r
499 //\r
500 Mode->DhcpDiscoverValid = TRUE;\r
501\r
502 Ack = &Private->Dhcp4Ack.Packet.Ack;\r
503 if (SelectedOffer->OfferType == DHCP4_PACKET_TYPE_BOOTP) {\r
504 //\r
505 // Other type of ACK is already cached. Bootp is special that we should\r
506 // use the bootp reply as the ACK and put it into the DHCP_ONLY buffer.\r
507 //\r
508 PxeBcCopyEfiDhcp4Packet (&Private->Dhcp4Ack.Packet.Ack, Offer);\r
509 }\r
510\r
511 PxeBcParseCachedDhcpPacket (&Private->Dhcp4Ack);\r
512\r
513 Mode->DhcpAckReceived = TRUE;\r
514\r
515 //\r
516 // Copy the dhcp ack.\r
517 //\r
e48e37fc 518 CopyMem (&Mode->DhcpAck, &Ack->Dhcp4, Ack->Length);\r
dc361cc5 519 }\r
520\r
521 return Status;\r
522}\r
523\r
524\r
525/**\r
f737cfb9 526 Cache the Dhcp4 packet offer, Parse and validate each option of the packet.\r
dc361cc5 527\r
f737cfb9 528 @param Private Pointer to PxeBc private data.\r
529 @param RcvdOffer Pointer to the received Dhcp proxy offer packet.\r
dc361cc5 530\r
dc361cc5 531**/\r
532VOID\r
533PxeBcCacheDhcpOffer (\r
534 IN PXEBC_PRIVATE_DATA *Private,\r
535 IN EFI_DHCP4_PACKET *RcvdOffer\r
536 )\r
537{\r
538 PXEBC_CACHED_DHCP4_PACKET *CachedOffer;\r
539 EFI_DHCP4_PACKET *Offer;\r
540 UINT8 OfferType;\r
541\r
542 CachedOffer = &Private->Dhcp4Offers[Private->NumOffers];\r
543 Offer = &CachedOffer->Packet.Offer;\r
544\r
545 //\r
546 // Cache the orignal dhcp packet\r
547 //\r
548 PxeBcCopyEfiDhcp4Packet (Offer, RcvdOffer);\r
549\r
550 //\r
551 // Parse and validate the options (including dhcp option and vendor option)\r
552 //\r
553 if (!PxeBcParseCachedDhcpPacket (CachedOffer)) {\r
554 return ;\r
555 }\r
556\r
557 OfferType = CachedOffer->OfferType;\r
894d038a 558 ASSERT (OfferType < DHCP4_PACKET_TYPE_MAX);\r
dc361cc5 559\r
560 if (OfferType == DHCP4_PACKET_TYPE_BOOTP) {\r
561\r
562 if (Private->BootpIndex != 0) {\r
563 //\r
564 // Only cache the first bootp offer, discard others.\r
565 //\r
566 return ;\r
567 } else {\r
568 //\r
569 // Take as a dhcp only offer, but record index specifically.\r
570 //\r
571 Private->BootpIndex = Private->NumOffers + 1;\r
572 }\r
573 } else {\r
574\r
575 if (IS_PROXY_DHCP_OFFER (Offer)) {\r
576 //\r
577 // It's a proxy dhcp offer with no your address, including pxe10, wfm11a or binl offer.\r
578 //\r
05c0e3cb 579 Private->GotProxyOffer = TRUE;\r
580\r
dc361cc5 581 if (OfferType == DHCP4_PACKET_TYPE_BINL) {\r
582 //\r
583 // Cache all binl offers.\r
584 //\r
585 Private->BinlIndex[Private->ProxyIndex[DHCP4_PACKET_TYPE_BINL]] = Private->NumOffers;\r
586 Private->ProxyIndex[DHCP4_PACKET_TYPE_BINL]++;\r
587 } else if (Private->ProxyIndex[OfferType] != 0) {\r
588 //\r
589 // Only cache the first pxe10/wfm11a offers each, discard the others.\r
590 //\r
591 return ;\r
592 } else {\r
593 //\r
594 // Record index of the proxy dhcp offer with type other than binl.\r
595 //\r
596 Private->ProxyIndex[OfferType] = Private->NumOffers + 1;\r
597 }\r
598 } else {\r
599 //\r
600 // It's a dhcp offer with your address.\r
601 //\r
894d038a 602 ASSERT (Private->ServerCount[OfferType] < PXEBC_MAX_OFFER_NUM);\r
dc361cc5 603 Private->OfferIndex[OfferType][Private->ServerCount[OfferType]] = Private->NumOffers;\r
604 Private->ServerCount[OfferType]++;\r
605 }\r
606 }\r
607\r
608 //\r
609 // Count the accepted offers.\r
610 //\r
611 Private->NumOffers++;\r
612}\r
613\r
614\r
615/**\r
f737cfb9 616 Select the specified proxy offer, such as BINL, DHCP_ONLY and so on.\r
617 If the proxy does not exist, try offers with bootfile.\r
618 \r
619 @param Private Pointer to PxeBc private data.\r
dc361cc5 620\r
dc361cc5 621**/\r
622VOID\r
623PxeBcSelectOffer (\r
624 IN PXEBC_PRIVATE_DATA *Private\r
625 )\r
626{\r
627 UINT32 Index;\r
628 UINT32 OfferIndex;\r
629 EFI_DHCP4_PACKET *Offer;\r
dc361cc5 630\r
631 Private->SelectedOffer = 0;\r
632\r
633 if (Private->SortOffers) {\r
634 //\r
635 // Select offer according to the priority\r
636 //\r
637 if (Private->ServerCount[DHCP4_PACKET_TYPE_PXE10] > 0) {\r
638 //\r
639 // DHCP with PXE10\r
640 //\r
641 Private->SelectedOffer = Private->OfferIndex[DHCP4_PACKET_TYPE_PXE10][0] + 1;\r
642\r
643 } else if (Private->ServerCount[DHCP4_PACKET_TYPE_WFM11A] > 0) {\r
644 //\r
645 // DHCP with WfM\r
646 //\r
647 Private->SelectedOffer = Private->OfferIndex[DHCP4_PACKET_TYPE_WFM11A][0] + 1;\r
648\r
649 } else if ((Private->ProxyIndex[DHCP4_PACKET_TYPE_PXE10] > 0) &&\r
650 (Private->ServerCount[DHCP4_PACKET_TYPE_DHCP_ONLY] > 0)\r
651 ) {\r
652 //\r
653 // DHCP only and proxy DHCP with PXE10\r
654 //\r
655 Private->SelectedOffer = Private->OfferIndex[DHCP4_PACKET_TYPE_DHCP_ONLY][0] + 1;\r
656 Private->ProxyOfferType = DHCP4_PACKET_TYPE_PXE10;\r
657\r
658 } else if ((Private->ProxyIndex[DHCP4_PACKET_TYPE_WFM11A] > 0) &&\r
659 (Private->ServerCount[DHCP4_PACKET_TYPE_DHCP_ONLY] > 0)\r
660 ) {\r
661 //\r
662 // DHCP only and proxy DHCP with WfM\r
663 //\r
664 Private->SelectedOffer = Private->OfferIndex[DHCP4_PACKET_TYPE_DHCP_ONLY][0] + 1;\r
665 Private->ProxyOfferType = DHCP4_PACKET_TYPE_WFM11A;\r
666\r
667 } else if (Private->ServerCount[DHCP4_PACKET_TYPE_BINL] > 0) {\r
668 //\r
669 // DHCP with BINL\r
670 //\r
671 Private->SelectedOffer = Private->OfferIndex[DHCP4_PACKET_TYPE_BINL][0] + 1;\r
672\r
673 } else if ((Private->ProxyIndex[DHCP4_PACKET_TYPE_BINL] > 0) &&\r
674 (Private->ServerCount[DHCP4_PACKET_TYPE_DHCP_ONLY] > 0)\r
675 ) {\r
676 //\r
677 // DHCP only and proxy DHCP with BINL\r
678 //\r
679 Private->SelectedOffer = Private->OfferIndex[DHCP4_PACKET_TYPE_DHCP_ONLY][0] + 1;\r
680 Private->ProxyOfferType = DHCP4_PACKET_TYPE_BINL;\r
681\r
682 } else {\r
683 //\r
684 // Try offers with bootfile\r
685 //\r
686 for (Index = 0; Index < Private->ServerCount[DHCP4_PACKET_TYPE_DHCP_ONLY]; Index++) {\r
687 //\r
688 // Select the first DHCP only offer with bootfile\r
689 //\r
690 OfferIndex = Private->OfferIndex[DHCP4_PACKET_TYPE_DHCP_ONLY][Index];\r
691 if (Private->Dhcp4Offers[OfferIndex].Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] != NULL) {\r
692 Private->SelectedOffer = OfferIndex + 1;\r
693 break;\r
694 }\r
695 }\r
696\r
697 if (Private->SelectedOffer == 0) {\r
698 //\r
699 // Select the Bootp reply with bootfile if any\r
700 //\r
701 Private->SelectedOffer = Private->BootpIndex;\r
702 }\r
703 }\r
704 } else {\r
705 //\r
706 // Try the offers in the received order.\r
707 //\r
dc361cc5 708 for (Index = 0; Index < Private->NumOffers; Index++) {\r
709\r
710 Offer = &Private->Dhcp4Offers[Index].Packet.Offer;\r
711\r
712 if (IS_PROXY_DHCP_OFFER (Offer)) {\r
713 //\r
714 // Skip proxy offers\r
715 //\r
716 continue;\r
717 }\r
718\r
719 if ((Private->Dhcp4Offers[Index].OfferType == DHCP4_PACKET_TYPE_DHCP_ONLY) &&\r
05c0e3cb 720 ((!Private->GotProxyOffer) && (Private->Dhcp4Offers[Index].Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] == NULL))) {\r
dc361cc5 721 //\r
722 // DHCP only offer but no proxy offer received and no bootfile option in this offer\r
723 //\r
724 continue;\r
725 }\r
726\r
727 Private->SelectedOffer = Index + 1;\r
728 break;\r
729 }\r
730 }\r
731}\r
732\r
733\r
734/**\r
f737cfb9 735 Callback routine.\r
736 \r
737 EFI_DHCP4_CALLBACK is provided by the consumer of the EFI DHCPv4 Protocol driver\r
738 to intercept events that occurred in the configuration process. This structure\r
739 provides advanced control of each state transition of the DHCP process. The\r
740 returned status code determines the behavior of the EFI DHCPv4 Protocol driver.\r
741 There are three possible returned values, which are described in the following\r
742 table.\r
743\r
744 @param This Pointer to the EFI DHCPv4 Protocol instance that is used to\r
745 configure this callback function.\r
746 @param Context Pointer to the context that is initialized by\r
747 EFI_DHCP4_PROTOCOL.Configure().\r
748 @param CurrentState The current operational state of the EFI DHCPv4 Protocol\r
749 driver.\r
750 @param Dhcp4Event The event that occurs in the current state, which usually means a\r
751 state transition.\r
752 @param Packet The DHCP packet that is going to be sent or already received.\r
753 @param NewPacket The packet that is used to replace the above Packet.\r
754\r
755 @retval EFI_SUCCESS Tells the EFI DHCPv4 Protocol driver to continue the DHCP process.\r
756 @retval EFI_NOT_READY Only used in the Dhcp4Selecting state. The EFI DHCPv4 Protocol\r
757 driver will continue to wait for more DHCPOFFER packets until the retry\r
758 timeout expires.\r
759 @retval EFI_ABORTED Tells the EFI DHCPv4 Protocol driver to abort the current process and\r
760 return to the Dhcp4Init or Dhcp4InitReboot state.\r
dc361cc5 761\r
762**/\r
763EFI_STATUS\r
6d3ea23f 764EFIAPI\r
dc361cc5 765PxeBcDhcpCallBack (\r
766 IN EFI_DHCP4_PROTOCOL * This,\r
767 IN VOID *Context,\r
768 IN EFI_DHCP4_STATE CurrentState,\r
769 IN EFI_DHCP4_EVENT Dhcp4Event,\r
770 IN EFI_DHCP4_PACKET * Packet OPTIONAL,\r
771 OUT EFI_DHCP4_PACKET **NewPacket OPTIONAL\r
772 )\r
773{\r
774 PXEBC_PRIVATE_DATA *Private;\r
775 EFI_PXE_BASE_CODE_MODE *Mode;\r
776 EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL *Callback;\r
777 EFI_DHCP4_PACKET_OPTION *MaxMsgSize;\r
778 UINT16 Value;\r
779 EFI_STATUS Status;\r
780 BOOLEAN Received;\r
982a9eae 781 CHAR8 *SystemSerialNumber;\r
782 EFI_DHCP4_HEADER *DhcpHeader;\r
dc361cc5 783\r
784 if ((Dhcp4Event != Dhcp4RcvdOffer) &&\r
785 (Dhcp4Event != Dhcp4SelectOffer) &&\r
786 (Dhcp4Event != Dhcp4SendDiscover) &&\r
982a9eae 787 (Dhcp4Event != Dhcp4RcvdAck) &&\r
788 (Dhcp4Event != Dhcp4SendRequest)) {\r
dc361cc5 789 return EFI_SUCCESS;\r
790 }\r
791\r
792 Private = (PXEBC_PRIVATE_DATA *) Context;\r
793 Mode = Private->PxeBc.Mode;\r
794 Callback = Private->PxeBcCallback;\r
795\r
796 //\r
797 // Override the Maximum DHCP Message Size.\r
798 //\r
799 MaxMsgSize = PxeBcParseExtendOptions (\r
800 Packet->Dhcp4.Option,\r
801 GET_OPTION_BUFFER_LEN (Packet),\r
802 PXEBC_DHCP4_TAG_MAXMSG\r
803 );\r
804 if (MaxMsgSize != NULL) {\r
805 Value = HTONS (PXEBC_DHCP4_MAX_PACKET_SIZE);\r
e48e37fc 806 CopyMem (MaxMsgSize->Data, &Value, sizeof (Value));\r
dc361cc5 807 }\r
808\r
809 if ((Dhcp4Event != Dhcp4SelectOffer) && (Callback != NULL)) {\r
810 Received = (BOOLEAN) ((Dhcp4Event == Dhcp4RcvdOffer) || (Dhcp4Event == Dhcp4RcvdAck));\r
811 Status = Callback->Callback (\r
812 Callback,\r
813 Private->Function,\r
814 Received,\r
815 Packet->Length,\r
816 (EFI_PXE_BASE_CODE_PACKET *) &Packet->Dhcp4\r
817 );\r
818 if (Status != EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE) {\r
819 return EFI_ABORTED;\r
820 }\r
821 }\r
822\r
823 Status = EFI_SUCCESS;\r
824\r
825 switch (Dhcp4Event) {\r
826\r
827 case Dhcp4SendDiscover:\r
982a9eae 828 case Dhcp4SendRequest:\r
829 if (Mode->SendGUID) {\r
830 //\r
831 // send the system GUID instead of the MAC address as the hardware address\r
832 // in the DHCP packet header.\r
833 //\r
834 DhcpHeader = &Packet->Dhcp4.Header;\r
835\r
836 if (EFI_ERROR (GetSmbiosSystemGuidAndSerialNumber ((EFI_GUID *) DhcpHeader->ClientHwAddr, &SystemSerialNumber))) {\r
837 //\r
838 // GUID not yet set - send all 0xff's to show programable (via SetVariable)\r
839 // SetMem(DHCPV4_OPTIONS_BUFFER.DhcpPlatformId.Guid, sizeof(EFI_GUID), 0xff);\r
840 // GUID not yet set - send all 0's to show not programable\r
841 //\r
842 ZeroMem (DhcpHeader->ClientHwAddr, sizeof (EFI_GUID));\r
843 }\r
844\r
845 DhcpHeader->HwAddrLen = sizeof (EFI_GUID);\r
846 }\r
847\r
848 if (Dhcp4Event == Dhcp4SendDiscover) {\r
849 //\r
850 // Cache the dhcp discover packet, of which some information will be used later.\r
851 //\r
852 CopyMem (Mode->DhcpDiscover.Raw, &Packet->Dhcp4, Packet->Length);\r
853 }\r
dc361cc5 854\r
855 break;\r
856\r
857 case Dhcp4RcvdOffer:\r
858 Status = EFI_NOT_READY;\r
859 if (Private->NumOffers < PXEBC_MAX_OFFER_NUM) {\r
860 //\r
861 // Cache the dhcp offers in Private->Dhcp4Offers[]\r
862 //\r
863 PxeBcCacheDhcpOffer (Private, Packet);\r
864 }\r
865\r
866 break;\r
867\r
868 case Dhcp4SelectOffer:\r
869 //\r
870 // Select an offer, if succeeded, Private->SelectedOffer points to\r
871 // the index of the selected one.\r
872 //\r
873 PxeBcSelectOffer (Private);\r
874\r
875 if (Private->SelectedOffer == 0) {\r
876 Status = EFI_ABORTED;\r
877 } else {\r
878 *NewPacket = &Private->Dhcp4Offers[Private->SelectedOffer - 1].Packet.Offer;\r
879 }\r
880\r
881 break;\r
882\r
883 case Dhcp4RcvdAck:\r
884 //\r
885 // Cache Ack\r
886 //\r
887 ASSERT (Private->SelectedOffer != 0);\r
888\r
889 PxeBcCopyEfiDhcp4Packet (&Private->Dhcp4Ack.Packet.Ack, Packet);\r
890 break;\r
12f3a142 891\r
892 default:\r
893 break;\r
dc361cc5 894 }\r
895\r
896 return Status;\r
897}\r
898\r
899\r
900/**\r
f737cfb9 901 Initialize the DHCP options and build the option list.\r
dc361cc5 902\r
f737cfb9 903 @param Private Pointer to PxeBc private data.\r
904 @param OptList Pointer to a DHCP option list.\r
905 \r
906 @param IsDhcpDiscover Discover dhcp option or not. \r
dc361cc5 907\r
f737cfb9 908 @return The index item number of the option list.\r
dc361cc5 909\r
910**/\r
911UINT32\r
912PxeBcBuildDhcpOptions (\r
913 IN PXEBC_PRIVATE_DATA *Private,\r
914 IN EFI_DHCP4_PACKET_OPTION **OptList,\r
915 IN BOOLEAN IsDhcpDiscover\r
916 )\r
917{\r
918 UINT32 Index;\r
919 PXEBC_DHCP4_OPTION_ENTRY OptEnt;\r
920 UINT16 Value;\r
921 CHAR8 *SystemSerialNumber;\r
922\r
923 Index = 0;\r
924 OptList[0] = (EFI_DHCP4_PACKET_OPTION *) Private->OptionBuffer;\r
925\r
926 if (!IsDhcpDiscover) {\r
927 //\r
928 // Append message type.\r
929 //\r
930 OptList[Index]->OpCode = PXEBC_DHCP4_TAG_MSG_TYPE;\r
931 OptList[Index]->Length = 1;\r
932 OptEnt.Mesg = (PXEBC_DHCP4_OPTION_MESG *) OptList[Index]->Data;\r
933 OptEnt.Mesg->Type = PXEBC_DHCP4_MSG_TYPE_REQUEST;\r
934 Index++;\r
935 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);\r
936\r
937 //\r
938 // Append max message size.\r
939 //\r
940 OptList[Index]->OpCode = PXEBC_DHCP4_TAG_MAXMSG;\r
941 OptList[Index]->Length = sizeof (PXEBC_DHCP4_OPTION_MAX_MESG_SIZE);\r
942 OptEnt.MaxMesgSize = (PXEBC_DHCP4_OPTION_MAX_MESG_SIZE *) OptList[Index]->Data;\r
943 Value = NTOHS (PXEBC_DHCP4_MAX_PACKET_SIZE);\r
e48e37fc 944 CopyMem (&OptEnt.MaxMesgSize->Size, &Value, sizeof (UINT16));\r
dc361cc5 945 Index++;\r
946 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);\r
947 }\r
948 //\r
949 // Parameter request list option.\r
950 //\r
951 OptList[Index]->OpCode = PXEBC_DHCP4_TAG_PARA_LIST;\r
952 OptList[Index]->Length = 35;\r
953 OptEnt.Para = (PXEBC_DHCP4_OPTION_PARA *) OptList[Index]->Data;\r
954 OptEnt.Para->ParaList[0] = PXEBC_DHCP4_TAG_NETMASK;\r
955 OptEnt.Para->ParaList[1] = PXEBC_DHCP4_TAG_TIME_OFFSET;\r
956 OptEnt.Para->ParaList[2] = PXEBC_DHCP4_TAG_ROUTER;\r
957 OptEnt.Para->ParaList[3] = PXEBC_DHCP4_TAG_TIME_SERVER;\r
958 OptEnt.Para->ParaList[4] = PXEBC_DHCP4_TAG_NAME_SERVER;\r
959 OptEnt.Para->ParaList[5] = PXEBC_DHCP4_TAG_DNS_SERVER;\r
960 OptEnt.Para->ParaList[6] = PXEBC_DHCP4_TAG_HOSTNAME;\r
961 OptEnt.Para->ParaList[7] = PXEBC_DHCP4_TAG_BOOTFILE_LEN;\r
962 OptEnt.Para->ParaList[8] = PXEBC_DHCP4_TAG_DOMAINNAME;\r
963 OptEnt.Para->ParaList[9] = PXEBC_DHCP4_TAG_ROOTPATH;\r
964 OptEnt.Para->ParaList[10] = PXEBC_DHCP4_TAG_EXTEND_PATH;\r
965 OptEnt.Para->ParaList[11] = PXEBC_DHCP4_TAG_EMTU;\r
966 OptEnt.Para->ParaList[12] = PXEBC_DHCP4_TAG_TTL;\r
967 OptEnt.Para->ParaList[13] = PXEBC_DHCP4_TAG_BROADCAST;\r
968 OptEnt.Para->ParaList[14] = PXEBC_DHCP4_TAG_NIS_DOMAIN;\r
969 OptEnt.Para->ParaList[15] = PXEBC_DHCP4_TAG_NIS_SERVER;\r
970 OptEnt.Para->ParaList[16] = PXEBC_DHCP4_TAG_NTP_SERVER;\r
971 OptEnt.Para->ParaList[17] = PXEBC_DHCP4_TAG_VENDOR;\r
972 OptEnt.Para->ParaList[18] = PXEBC_DHCP4_TAG_REQUEST_IP;\r
973 OptEnt.Para->ParaList[19] = PXEBC_DHCP4_TAG_LEASE;\r
974 OptEnt.Para->ParaList[20] = PXEBC_DHCP4_TAG_SERVER_ID;\r
975 OptEnt.Para->ParaList[21] = PXEBC_DHCP4_TAG_T1;\r
976 OptEnt.Para->ParaList[22] = PXEBC_DHCP4_TAG_T2;\r
977 OptEnt.Para->ParaList[23] = PXEBC_DHCP4_TAG_CLASS_ID;\r
978 OptEnt.Para->ParaList[24] = PXEBC_DHCP4_TAG_TFTP;\r
979 OptEnt.Para->ParaList[25] = PXEBC_DHCP4_TAG_BOOTFILE;\r
980 OptEnt.Para->ParaList[26] = PXEBC_PXE_DHCP4_TAG_UUID;\r
981 OptEnt.Para->ParaList[27] = 0x80;\r
982 OptEnt.Para->ParaList[28] = 0x81;\r
983 OptEnt.Para->ParaList[29] = 0x82;\r
984 OptEnt.Para->ParaList[30] = 0x83;\r
985 OptEnt.Para->ParaList[31] = 0x84;\r
986 OptEnt.Para->ParaList[32] = 0x85;\r
987 OptEnt.Para->ParaList[33] = 0x86;\r
988 OptEnt.Para->ParaList[34] = 0x87;\r
989 Index++;\r
990 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);\r
991\r
992 //\r
993 // Append UUID/Guid-based client identifier option\r
994 //\r
995 OptList[Index]->OpCode = PXEBC_PXE_DHCP4_TAG_UUID;\r
996 OptList[Index]->Length = sizeof (PXEBC_DHCP4_OPTION_UUID);\r
997 OptEnt.Uuid = (PXEBC_DHCP4_OPTION_UUID *) OptList[Index]->Data;\r
998 OptEnt.Uuid->Type = 0;\r
999 Index++;\r
1000 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);\r
1001\r
1002 if (EFI_ERROR (GetSmbiosSystemGuidAndSerialNumber ((EFI_GUID *) OptEnt.Uuid->Guid, &SystemSerialNumber))) {\r
1003 //\r
1004 // GUID not yet set - send all 0xff's to show programable (via SetVariable)\r
1005 // SetMem(DHCPV4_OPTIONS_BUFFER.DhcpPlatformId.Guid, sizeof(EFI_GUID), 0xff);\r
1006 // GUID not yet set - send all 0's to show not programable\r
1007 //\r
1008 ZeroMem (OptEnt.Uuid->Guid, sizeof (EFI_GUID));\r
1009 }\r
1010\r
1011 //\r
1012 // Append client network device interface option\r
1013 //\r
1014 OptList[Index]->OpCode = PXEBC_PXE_DHCP4_TAG_UNDI;\r
1015 OptList[Index]->Length = sizeof (PXEBC_DHCP4_OPTION_UNDI);\r
1016 OptEnt.Undi = (PXEBC_DHCP4_OPTION_UNDI *) OptList[Index]->Data;\r
169a3461 1017 if (Private->Nii != NULL) {\r
1018 OptEnt.Undi->Type = Private->Nii->Type;\r
1019 OptEnt.Undi->MajorVer = Private->Nii->MajorVer;\r
1020 OptEnt.Undi->MinorVer = Private->Nii->MinorVer;\r
1021 } else {\r
1022 OptEnt.Undi->Type = DEFAULT_UNDI_TYPE;\r
1023 OptEnt.Undi->MajorVer = DEFAULT_UNDI_MAJOR;\r
1024 OptEnt.Undi->MinorVer = DEFAULT_UNDI_MINOR;\r
1025 }\r
dc361cc5 1026\r
1027 Index++;\r
1028 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);\r
1029\r
1030 //\r
1031 // Append client system architecture option\r
1032 //\r
1033 OptList[Index]->OpCode = PXEBC_PXE_DHCP4_TAG_ARCH;\r
1034 OptList[Index]->Length = sizeof (PXEBC_DHCP4_OPTION_ARCH);\r
1035 OptEnt.Arch = (PXEBC_DHCP4_OPTION_ARCH *) OptList[Index]->Data;\r
1036 Value = HTONS (SYS_ARCH);\r
e48e37fc 1037 CopyMem (&OptEnt.Arch->Type, &Value, sizeof (UINT16));\r
dc361cc5 1038 Index++;\r
1039 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);\r
1040\r
1041 //\r
1042 // Append client system architecture option\r
1043 //\r
1044 OptList[Index]->OpCode = PXEBC_DHCP4_TAG_CLASS_ID;\r
1045 OptList[Index]->Length = sizeof (PXEBC_DHCP4_OPTION_CLID);\r
1046 OptEnt.Clid = (PXEBC_DHCP4_OPTION_CLID *) OptList[Index]->Data;\r
e48e37fc 1047 CopyMem (OptEnt.Clid, DEFAULT_CLASS_ID_DATA, sizeof (PXEBC_DHCP4_OPTION_CLID));\r
dc361cc5 1048 CvtNum (SYS_ARCH, OptEnt.Clid->ArchitectureType, sizeof (OptEnt.Clid->ArchitectureType));\r
169a3461 1049\r
1050 if (Private->Nii != NULL) {\r
1051 // \r
1052 // If NII protocol exists, update DHCP option data\r
1053 //\r
1054 CopyMem (OptEnt.Clid->InterfaceName, Private->Nii->StringId, sizeof (OptEnt.Clid->InterfaceName));\r
1055 CvtNum (Private->Nii->MajorVer, OptEnt.Clid->UndiMajor, sizeof (OptEnt.Clid->UndiMajor));\r
1056 CvtNum (Private->Nii->MinorVer, OptEnt.Clid->UndiMinor, sizeof (OptEnt.Clid->UndiMinor));\r
1057 }\r
1058\r
dc361cc5 1059 Index++;\r
1060\r
1061 return Index;\r
1062}\r
1063\r
1064\r
1065/**\r
f737cfb9 1066 Discover the boot of service and initialize the vendor option if exists.\r
1067\r
1068 @param Private Pointer to PxeBc private data.\r
1069 @param Type PxeBc option boot item type\r
1070 @param Layer PxeBc option boot item layer\r
1071 @param UseBis Use BIS or not\r
1072 @param DestIp Ip address for server \r
1073 @param IpCount The total count of the server ip address \r
1074 @param SrvList Server list\r
1075 @param IsDiscv Discover the vendor or not\r
1076 @param Reply The dhcp4 packet of Pxe reply\r
1077\r
1078 @retval EFI_SUCCESS Operation succeeds.\r
1079 @retval EFI_OUT_OF_RESOURCES Allocate memory pool failed.\r
1080 @retval EFI_NOT_FOUND There is no vendor option exists.\r
1081 @retval EFI_TIMEOUT Send Pxe Discover time out. \r
1082 \r
dc361cc5 1083**/\r
1084EFI_STATUS\r
1085PxeBcDiscvBootService (\r
1086 IN PXEBC_PRIVATE_DATA * Private,\r
1087 IN UINT16 Type,\r
1088 IN UINT16 *Layer,\r
1089 IN BOOLEAN UseBis,\r
1090 IN EFI_IP_ADDRESS * DestIp,\r
1091 IN UINT16 IpCount,\r
1092 IN EFI_PXE_BASE_CODE_SRVLIST * SrvList,\r
1093 IN BOOLEAN IsDiscv,\r
1094 OUT EFI_DHCP4_PACKET * Reply OPTIONAL\r
1095 )\r
1096{\r
1097 EFI_PXE_BASE_CODE_UDP_PORT Sport;\r
1098 EFI_PXE_BASE_CODE_MODE *Mode;\r
1099 EFI_DHCP4_PROTOCOL *Dhcp4;\r
1100 EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN Token;\r
1101 BOOLEAN IsBCast;\r
1102 EFI_STATUS Status;\r
1103 UINT16 RepIndex;\r
1104 UINT16 SrvIndex;\r
1105 UINT16 TryIndex;\r
1106 EFI_DHCP4_LISTEN_POINT ListenPoint;\r
1107 EFI_DHCP4_PACKET *Response;\r
1108 EFI_DHCP4_PACKET_OPTION *OptList[PXEBC_DHCP4_MAX_OPTION_NUM];\r
1109 UINT32 OptCount;\r
1110 EFI_DHCP4_PACKET_OPTION *PxeOpt;\r
1111 PXEBC_OPTION_BOOT_ITEM *PxeBootItem;\r
1112 UINT8 VendorOptLen;\r
982a9eae 1113 CHAR8 *SystemSerialNumber;\r
1114 EFI_DHCP4_HEADER *DhcpHeader;\r
434ce3fe 1115 UINT32 Xid;\r
982a9eae 1116\r
dc361cc5 1117 Mode = Private->PxeBc.Mode;\r
1118 Dhcp4 = Private->Dhcp4;\r
1119 Status = EFI_SUCCESS;\r
1120\r
1121 ZeroMem (&Token, sizeof (EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN));\r
1122\r
1123 if (DestIp == NULL) {\r
1124 Sport = PXEBC_DHCP4_S_PORT;\r
1125 IsBCast = TRUE;\r
1126 } else {\r
1127 Sport = PXEBC_BS_DISCOVER_PORT;\r
1128 IsBCast = FALSE;\r
1129 }\r
1130\r
1131 if (!UseBis && Layer != NULL) {\r
1132 *Layer &= EFI_PXE_BASE_CODE_BOOT_LAYER_MASK;\r
1133 }\r
1134\r
1135 OptCount = PxeBcBuildDhcpOptions (Private, OptList, FALSE);\r
1136\r
1137 if (IsDiscv) {\r
944bf8db 1138 ASSERT (Layer != NULL);\r
dc361cc5 1139 //\r
1140 // Add vendor option of PXE_BOOT_ITEM\r
1141 //\r
1142 VendorOptLen = (sizeof (EFI_DHCP4_PACKET_OPTION) - 1) * 2 + sizeof (PXEBC_OPTION_BOOT_ITEM) + 1;\r
e48e37fc 1143 OptList[OptCount] = AllocatePool (VendorOptLen);\r
dc361cc5 1144 if (OptList[OptCount] == NULL) {\r
1145 return EFI_OUT_OF_RESOURCES;\r
1146 }\r
1147\r
1148 OptList[OptCount]->OpCode = PXEBC_DHCP4_TAG_VENDOR;\r
1149 OptList[OptCount]->Length = (UINT8) (VendorOptLen - 2);\r
1150 PxeOpt = (EFI_DHCP4_PACKET_OPTION *) OptList[OptCount]->Data;\r
1151 PxeOpt->OpCode = PXEBC_VENDOR_TAG_BOOT_ITEM;\r
1152 PxeOpt->Length = sizeof (PXEBC_OPTION_BOOT_ITEM);\r
1153 PxeBootItem = (PXEBC_OPTION_BOOT_ITEM *) PxeOpt->Data;\r
1154 PxeBootItem->Type = HTONS (Type);\r
1155 PxeBootItem->Layer = HTONS (*Layer);\r
1156 PxeOpt->Data[PxeOpt->Length] = PXEBC_DHCP4_TAG_EOP;\r
1157\r
1158 OptCount++;\r
1159 }\r
1160\r
1161 Status = Dhcp4->Build (Dhcp4, &Private->SeedPacket, 0, NULL, OptCount, OptList, &Token.Packet);\r
1162\r
1163 if (IsDiscv) {\r
e48e37fc 1164 gBS->FreePool (OptList[OptCount - 1]);\r
dc361cc5 1165 }\r
1166\r
1167 if (EFI_ERROR (Status)) {\r
1168 return Status;\r
1169 }\r
1170\r
982a9eae 1171 DhcpHeader = &Token.Packet->Dhcp4.Header;\r
1172 if (Mode->SendGUID) {\r
1173 if (EFI_ERROR (GetSmbiosSystemGuidAndSerialNumber ((EFI_GUID *) DhcpHeader->ClientHwAddr, &SystemSerialNumber))) {\r
1174 //\r
1175 // GUID not yet set - send all 0's to show not programable\r
1176 //\r
1177 ZeroMem (DhcpHeader->ClientHwAddr, sizeof (EFI_GUID));\r
1178 }\r
1179\r
1180 DhcpHeader->HwAddrLen = sizeof (EFI_GUID);\r
1181 }\r
434ce3fe 1182 \r
1183 Xid = NET_RANDOM (NetRandomInitSeed ());\r
1184 Token.Packet->Dhcp4.Header.Xid = HTONL(Xid);\r
1185 Token.Packet->Dhcp4.Header.Reserved = HTONS((IsBCast) ? 0x8000 : 0);\r
e48e37fc 1186 CopyMem (&Token.Packet->Dhcp4.Header.ClientAddr, &Private->StationIp, sizeof (EFI_IPv4_ADDRESS));\r
dc361cc5 1187\r
1188 Token.RemotePort = Sport;\r
1189\r
434ce3fe 1190 if (IsBCast) {\r
e48e37fc 1191 SetMem (&Token.RemoteAddress, sizeof (EFI_IPv4_ADDRESS), 0xff);\r
dc361cc5 1192 } else {\r
e48e37fc 1193 CopyMem (&Token.RemoteAddress, DestIp, sizeof (EFI_IPv4_ADDRESS));\r
dc361cc5 1194 }\r
1195\r
e48e37fc 1196 CopyMem (&Token.GatewayAddress, &Private->GatewayIp, sizeof (EFI_IPv4_ADDRESS));\r
dc361cc5 1197\r
1198 if (!IsBCast) {\r
1199 Token.ListenPointCount = 1;\r
1200 Token.ListenPoints = &ListenPoint;\r
1201 Token.ListenPoints[0].ListenPort = PXEBC_BS_DISCOVER_PORT;\r
e48e37fc 1202 CopyMem (&Token.ListenPoints[0].ListenAddress, &Private->StationIp, sizeof(EFI_IPv4_ADDRESS));\r
1203 CopyMem (&Token.ListenPoints[0].SubnetMask, &Private->SubnetMask, sizeof(EFI_IPv4_ADDRESS));\r
dc361cc5 1204 }\r
1205 //\r
1206 // Send Pxe Discover\r
1207 //\r
1208 for (TryIndex = 1; TryIndex <= PXEBC_BOOT_REQUEST_RETRIES; TryIndex++) {\r
1209\r
434ce3fe 1210 Token.TimeoutValue = (UINT16) (PXEBC_BOOT_REQUEST_TIMEOUT * TryIndex);\r
1211 Token.Packet->Dhcp4.Header.Seconds = (UINT16) (PXEBC_BOOT_REQUEST_TIMEOUT * (TryIndex - 1));\r
dc361cc5 1212\r
1213 Status = Dhcp4->TransmitReceive (Dhcp4, &Token);\r
1214\r
1215 if (Token.Status != EFI_TIMEOUT) {\r
1216 break;\r
1217 }\r
1218 }\r
1219\r
1220 if (!EFI_ERROR (Status)) {\r
1221 //\r
1222 // Find Pxe Reply\r
1223 //\r
1224 RepIndex = 0;\r
1225 SrvIndex = 0;\r
1226 Response = Token.ResponseList;\r
1227\r
1228 while (RepIndex < Token.ResponseCount) {\r
1229\r
1230 while (SrvIndex < IpCount) {\r
1231\r
1232 if (SrvList[SrvIndex].AcceptAnyResponse) {\r
1233 break;\r
1234 }\r
1235\r
1236 if ((SrvList[SrvIndex].Type == Type) && EFI_IP4_EQUAL (&(Response->Dhcp4.Header.ServerAddr), &(Private->ServerIp))) {\r
1237 break;\r
1238 }\r
1239\r
1240 SrvIndex++;\r
1241 }\r
1242\r
1243 if ((IpCount != SrvIndex) || (IpCount == 0)) {\r
1244 break;\r
1245 }\r
1246\r
1247 SrvIndex = 0;\r
1248 RepIndex++;\r
1249\r
1250 Response = (EFI_DHCP4_PACKET *) ((UINT8 *) Response + Response->Size);\r
1251 }\r
1252\r
1253 if (RepIndex < Token.ResponseCount) {\r
1254\r
1255 if (Reply != NULL) {\r
1256 PxeBcCopyEfiDhcp4Packet (Reply, Response);\r
1257 }\r
1258\r
1259 if (IsDiscv) {\r
e48e37fc 1260 CopyMem (&(Mode->PxeDiscover), &(Token.Packet->Dhcp4), Token.Packet->Length);\r
dc361cc5 1261 Mode->PxeDiscoverValid = TRUE;\r
1262\r
e48e37fc 1263 CopyMem (Mode->PxeReply.Raw, &Response->Dhcp4, Response->Length);\r
dc361cc5 1264 Mode->PxeReplyReceived = TRUE;\r
1265 }\r
1266 } else {\r
1267 Status = EFI_NOT_FOUND;\r
1268 }\r
1269\r
1270 //\r
1271 // free the responselist\r
1272 //\r
e48e37fc 1273 gBS->FreePool (Token.ResponseList);\r
dc361cc5 1274 }\r
1275 //\r
1276 // Free the dhcp packet\r
1277 //\r
e48e37fc 1278 gBS->FreePool (Token.Packet);\r
dc361cc5 1279\r
1280 return Status;\r
1281}\r
1282\r
1283\r
1284/**\r
f737cfb9 1285 Parse interested dhcp options.\r
dc361cc5 1286\r
f737cfb9 1287 @param Buffer Pointer to the dhcp options packet.\r
1288 @param Length The length of the dhcp options.\r
1289 @param OptTag The option OpCode.\r
dc361cc5 1290\r
f737cfb9 1291 @return NULL if the buffer length is 0 and OpCode is not \r
1292 PXEBC_DHCP4_TAG_EOP, or the pointer to the buffer.\r
dc361cc5 1293\r
1294**/\r
1295EFI_DHCP4_PACKET_OPTION *\r
1296PxeBcParseExtendOptions (\r
1297 IN UINT8 *Buffer,\r
1298 IN UINT32 Length,\r
1299 IN UINT8 OptTag\r
1300 )\r
1301{\r
1302 EFI_DHCP4_PACKET_OPTION *Option;\r
1303 UINT32 Offset;\r
1304\r
1305 Option = (EFI_DHCP4_PACKET_OPTION *) Buffer;\r
1306 Offset = 0;\r
1307\r
1308 while (Offset < Length && Option->OpCode != PXEBC_DHCP4_TAG_EOP) {\r
1309\r
1310 if (Option->OpCode == OptTag) {\r
1311\r
1312 return Option;\r
1313 }\r
1314\r
1315 if (Option->OpCode == PXEBC_DHCP4_TAG_PAD) {\r
1316 Offset++;\r
1317 } else {\r
1318 Offset += Option->Length + 2;\r
1319 }\r
1320\r
1321 Option = (EFI_DHCP4_PACKET_OPTION *) (Buffer + Offset);\r
1322 }\r
1323\r
1324 return NULL;\r
1325}\r
1326\r
1327\r
1328/**\r
1329 This function is to parse and check vendor options.\r
1330\r
1331 @param Dhcp4Option Pointer to dhcp options\r
1332 @param VendorOption Pointer to vendor options\r
1333\r
f737cfb9 1334 @return TRUE if valid for vendor options, or FALSE.\r
dc361cc5 1335\r
1336**/\r
1337BOOLEAN\r
1338PxeBcParseVendorOptions (\r
1339 IN EFI_DHCP4_PACKET_OPTION *Dhcp4Option,\r
1340 IN PXEBC_VENDOR_OPTION *VendorOption\r
1341 )\r
1342{\r
1343 UINT32 *BitMap;\r
1344 UINT8 VendorOptionLen;\r
1345 EFI_DHCP4_PACKET_OPTION *PxeOption;\r
1346 UINT8 Offset;\r
1347\r
1348 BitMap = VendorOption->BitMap;\r
1349 VendorOptionLen = Dhcp4Option->Length;\r
1350 PxeOption = (EFI_DHCP4_PACKET_OPTION *) &Dhcp4Option->Data[0];\r
1351 Offset = 0;\r
1352\r
1353 while ((Offset < VendorOptionLen) && (PxeOption->OpCode != PXEBC_DHCP4_TAG_EOP)) {\r
1354 //\r
1355 // Parse every Vendor Option and set its BitMap\r
1356 //\r
1357 switch (PxeOption->OpCode) {\r
1358\r
1359 case PXEBC_VENDOR_TAG_MTFTP_IP:\r
1360\r
e48e37fc 1361 CopyMem (&VendorOption->MtftpIp, PxeOption->Data, sizeof (EFI_IPv4_ADDRESS));\r
dc361cc5 1362 break;\r
1363\r
1364 case PXEBC_VENDOR_TAG_MTFTP_CPORT:\r
1365\r
e48e37fc 1366 CopyMem (&VendorOption->MtftpCPort, PxeOption->Data, sizeof (VendorOption->MtftpCPort));\r
dc361cc5 1367 break;\r
1368\r
1369 case PXEBC_VENDOR_TAG_MTFTP_SPORT:\r
1370\r
e48e37fc 1371 CopyMem (&VendorOption->MtftpSPort, PxeOption->Data, sizeof (VendorOption->MtftpSPort));\r
dc361cc5 1372 break;\r
1373\r
1374 case PXEBC_VENDOR_TAG_MTFTP_TIMEOUT:\r
1375\r
1376 VendorOption->MtftpTimeout = *PxeOption->Data;\r
1377 break;\r
1378\r
1379 case PXEBC_VENDOR_TAG_MTFTP_DELAY:\r
1380\r
1381 VendorOption->MtftpDelay = *PxeOption->Data;\r
1382 break;\r
1383\r
1384 case PXEBC_VENDOR_TAG_DISCOVER_CTRL:\r
1385\r
1386 VendorOption->DiscoverCtrl = *PxeOption->Data;\r
1387 break;\r
1388\r
1389 case PXEBC_VENDOR_TAG_DISCOVER_MCAST:\r
1390\r
e48e37fc 1391 CopyMem (&VendorOption->DiscoverMcastIp, PxeOption->Data, sizeof (EFI_IPv4_ADDRESS));\r
dc361cc5 1392 break;\r
1393\r
1394 case PXEBC_VENDOR_TAG_BOOT_SERVERS:\r
1395\r
1396 VendorOption->BootSvrLen = PxeOption->Length;\r
1397 VendorOption->BootSvr = (PXEBC_BOOT_SVR_ENTRY *) PxeOption->Data;\r
1398 break;\r
1399\r
1400 case PXEBC_VENDOR_TAG_BOOT_MENU:\r
1401\r
1402 VendorOption->BootMenuLen = PxeOption->Length;\r
1403 VendorOption->BootMenu = (PXEBC_BOOT_MENU_ENTRY *) PxeOption->Data;\r
1404 break;\r
1405\r
1406 case PXEBC_VENDOR_TAG_MENU_PROMPT:\r
1407\r
1408 VendorOption->MenuPromptLen = PxeOption->Length;\r
1409 VendorOption->MenuPrompt = (PXEBC_MENU_PROMPT *) PxeOption->Data;\r
1410 break;\r
1411\r
1412 case PXEBC_VENDOR_TAG_MCAST_ALLOC:\r
1413\r
e48e37fc 1414 CopyMem (&VendorOption->McastIpBase, PxeOption->Data, sizeof (EFI_IPv4_ADDRESS));\r
1415 CopyMem (&VendorOption->McastIpBlock, PxeOption->Data + 4, sizeof (VendorOption->McastIpBlock));\r
1416 CopyMem (&VendorOption->McastIpRange, PxeOption->Data + 6, sizeof (VendorOption->McastIpRange));\r
dc361cc5 1417 break;\r
1418\r
1419 case PXEBC_VENDOR_TAG_CREDENTIAL_TYPES:\r
1420\r
1421 VendorOption->CredTypeLen = PxeOption->Length;\r
1422 VendorOption->CredType = (UINT32 *) PxeOption->Data;\r
1423 break;\r
1424\r
1425 case PXEBC_VENDOR_TAG_BOOT_ITEM:\r
1426\r
e48e37fc 1427 CopyMem (&VendorOption->BootSrvType, PxeOption->Data, sizeof (VendorOption->BootSrvType));\r
1428 CopyMem (&VendorOption->BootSrvLayer, PxeOption->Data + 2, sizeof (VendorOption->BootSrvLayer));\r
dc361cc5 1429 break;\r
1430 }\r
1431\r
1432 SET_VENDOR_OPTION_BIT_MAP (BitMap, PxeOption->OpCode);\r
1433\r
1434 if (PxeOption->OpCode == PXEBC_DHCP4_TAG_PAD) {\r
1435 Offset++;\r
1436 } else {\r
1437 Offset = (UINT8) (Offset + PxeOption->Length + 2);\r
1438 }\r
1439\r
1440 PxeOption = (EFI_DHCP4_PACKET_OPTION *) (Dhcp4Option->Data + Offset);\r
1441 }\r
1442\r
1443 //\r
1444 // FixMe, return falas if invalid of any vendor option\r
1445 //\r
1446\r
1447 return TRUE;\r
1448}\r
1449\r
1450\r
1451/**\r
f737cfb9 1452 This function display boot item detail.\r
dc361cc5 1453\r
f737cfb9 1454 If the length of the boot item string over 70 Char, just display 70 Char. \r
1455 \r
1456 @param Str Pointer to a string (boot item string).\r
1457 @param Len The length of string.\r
dc361cc5 1458\r
dc361cc5 1459**/\r
1460VOID\r
1461PxeBcDisplayBootItem (\r
1462 IN UINT8 *Str,\r
1463 IN UINT8 Len\r
1464 )\r
1465{\r
1466 UINT8 Tmp;\r
1467\r
1468 Len = (UINT8) MIN (70, Len);\r
1469 Tmp = Str[Len];\r
1470 Str[Len] = 0;\r
1471 AsciiPrint ("%a \n", Str);\r
1472 Str[Len] = Tmp;\r
1473}\r
1474\r
1475\r
1476/**\r
f737cfb9 1477 Choose the boot prompt.\r
dc361cc5 1478\r
f737cfb9 1479 @param Private Pointer to PxeBc private data.\r
dc361cc5 1480\r
f737cfb9 1481 @retval EFI_SUCCESS Select boot prompt done.\r
1482 @retval EFI_TIMEOUT Select boot prompt time out. \r
1483 @retval EFI_NOT_FOUND The proxy offer is not Pxe10.\r
1484 @retval EFI_ABORTED User cancel the operation.\r
1485 @retval EFI_NOT_READY Read the input key from the keybroad has not finish.\r
1486 \r
dc361cc5 1487**/\r
1488EFI_STATUS\r
1489PxeBcSelectBootPrompt (\r
1490 IN PXEBC_PRIVATE_DATA *Private\r
1491 )\r
1492{\r
1493 PXEBC_CACHED_DHCP4_PACKET *Packet;\r
1494 PXEBC_VENDOR_OPTION *VendorOpt;\r
1495 EFI_EVENT TimeoutEvent;\r
1496 EFI_EVENT DescendEvent;\r
1497 EFI_INPUT_KEY InputKey;\r
1498 EFI_STATUS Status;\r
1499 UINT8 Timeout;\r
1500 UINT8 *Prompt;\r
1501 UINT8 PromptLen;\r
1502 INT32 SecCol;\r
1503 INT32 SecRow;\r
1504\r
1505 TimeoutEvent = NULL;\r
1506 DescendEvent = NULL;\r
1507\r
1508 if (Private->PxeBc.Mode->ProxyOfferReceived) {\r
1509\r
1510 Packet = &Private->ProxyOffer;\r
1511 } else {\r
1512\r
1513 Packet = &Private->Dhcp4Ack;\r
1514 }\r
1515\r
1516 if (Packet->OfferType != DHCP4_PACKET_TYPE_PXE10) {\r
1517 return EFI_NOT_FOUND;\r
1518 }\r
1519\r
1520 VendorOpt = &Packet->PxeVendorOption;\r
1521\r
1522 if (!IS_VALID_BOOT_PROMPT (VendorOpt->BitMap)) {\r
1523 return EFI_SUCCESS;\r
1524 }\r
1525\r
1526 Timeout = VendorOpt->MenuPrompt->Timeout;\r
1527 Prompt = VendorOpt->MenuPrompt->Prompt;\r
1528 PromptLen = (UINT8) (VendorOpt->MenuPromptLen - 1);\r
1529\r
1530 if (Timeout == 0) {\r
1531 return EFI_SUCCESS;\r
1532 }\r
1533\r
1534 if (Timeout == 255) {\r
1535 return EFI_TIMEOUT;\r
1536 }\r
1537\r
1538 Status = gBS->CreateEvent (\r
1539 EVT_TIMER,\r
1540 TPL_CALLBACK,\r
1541 NULL,\r
1542 NULL,\r
1543 &TimeoutEvent\r
1544 );\r
1545\r
1546 if (EFI_ERROR (Status)) {\r
1547 return Status;\r
1548 }\r
1549\r
1550 Status = gBS->SetTimer (\r
1551 TimeoutEvent,\r
1552 TimerRelative,\r
1553 Timeout * TICKS_PER_SECOND\r
1554 );\r
1555\r
1556 if (EFI_ERROR (Status)) {\r
1557 goto ON_EXIT;\r
1558 }\r
1559\r
1560 Status = gBS->CreateEvent (\r
1561 EVT_TIMER,\r
1562 TPL_CALLBACK,\r
1563 NULL,\r
1564 NULL,\r
1565 &DescendEvent\r
1566 );\r
1567\r
1568 if (EFI_ERROR (Status)) {\r
1569 goto ON_EXIT;\r
1570 }\r
1571\r
1572 Status = gBS->SetTimer (\r
1573 DescendEvent,\r
1574 TimerPeriodic,\r
1575 TICKS_PER_SECOND\r
1576 );\r
1577\r
1578 if (EFI_ERROR (Status)) {\r
1579 goto ON_EXIT;\r
1580 }\r
1581\r
1582 SecCol = gST->ConOut->Mode->CursorColumn;\r
1583 SecRow = gST->ConOut->Mode->CursorRow;\r
1584\r
1585 PxeBcDisplayBootItem (Prompt, PromptLen);\r
1586\r
1587 gST->ConOut->SetCursorPosition (gST->ConOut, SecCol + PromptLen, SecRow);\r
1588 AsciiPrint ("(%d) ", Timeout--);\r
1589\r
1590 while (EFI_ERROR (gBS->CheckEvent (TimeoutEvent))) {\r
1591\r
1592 if (!EFI_ERROR (gBS->CheckEvent (DescendEvent))) {\r
1593 gST->ConOut->SetCursorPosition (gST->ConOut, SecCol + PromptLen, SecRow);\r
1594 AsciiPrint ("(%d) ", Timeout--);\r
1595 }\r
1596\r
1597 if (gST->ConIn->ReadKeyStroke (gST->ConIn, &InputKey) == EFI_NOT_READY) {\r
1598\r
1599 gBS->Stall (10 * TICKS_PER_MS);\r
1600 continue;\r
1601 }\r
1602\r
1603 if (InputKey.ScanCode == 0) {\r
1604\r
1605 switch (InputKey.UnicodeChar) {\r
1606 case CTRL ('c'):\r
1607 Status = EFI_ABORTED;\r
1608 break;\r
1609\r
1610 case CTRL ('m'):\r
1611 case 'm':\r
1612 case 'M':\r
1613 Status = EFI_TIMEOUT;\r
1614 break;\r
1615\r
1616 default:\r
1617 continue;\r
1618 }\r
1619 } else {\r
1620\r
1621 switch (InputKey.ScanCode) {\r
1622 case SCAN_F8:\r
1623 Status = EFI_TIMEOUT;\r
1624 break;\r
1625\r
1626 case SCAN_ESC:\r
1627 Status = EFI_ABORTED;\r
1628 break;\r
1629\r
1630 default:\r
1631 continue;\r
1632 }\r
1633 }\r
1634\r
1635 break;\r
1636 }\r
1637\r
1638 gST->ConOut->SetCursorPosition (gST->ConOut, 0 , SecRow + 1);\r
1639\r
1640ON_EXIT:\r
1641\r
1642 if (DescendEvent != NULL) {\r
1643 gBS->CloseEvent (DescendEvent);\r
1644 }\r
1645\r
1646 if (TimeoutEvent != NULL) {\r
1647 gBS->CloseEvent (TimeoutEvent);\r
1648 }\r
1649\r
1650 return Status;\r
1651}\r
1652\r
1653\r
1654/**\r
f737cfb9 1655 Select the boot menu.\r
dc361cc5 1656\r
f737cfb9 1657 @param Private Pointer to PxeBc private data.\r
1658 @param Type The type of the menu.\r
1659 @param UseDefaultItem Use default item or not.\r
1660 \r
1661 @retval EFI_ABORTED User cancel operation.\r
1662 @retval EFI_SUCCESS Select the boot menu success.\r
1663 @retval EFI_NOT_READY Read the input key from the keybroad has not finish. \r
dc361cc5 1664\r
1665**/\r
1666EFI_STATUS\r
1667PxeBcSelectBootMenu (\r
1668 IN PXEBC_PRIVATE_DATA *Private,\r
1669 OUT UINT16 *Type,\r
1670 IN BOOLEAN UseDefaultItem\r
1671 )\r
1672{\r
1673 PXEBC_CACHED_DHCP4_PACKET *Packet;\r
1674 PXEBC_VENDOR_OPTION *VendorOpt;\r
1675 EFI_INPUT_KEY InputKey;\r
1676 UINT8 MenuSize;\r
1677 UINT8 MenuNum;\r
1678 INT32 TopRow;\r
1679 UINT16 Select;\r
1680 UINT16 LastSelect;\r
1681 UINT8 Index;\r
1682 BOOLEAN Finish;\r
1683 CHAR8 Blank[70];\r
1684 PXEBC_BOOT_MENU_ENTRY *MenuItem;\r
1685 PXEBC_BOOT_MENU_ENTRY *MenuArray[PXEBC_MAX_MENU_NUM];\r
1686\r
1687 Finish = FALSE;\r
1688 Select = 1;\r
1689 Index = 0;\r
1690 *Type = 0;\r
1691\r
1692 if (Private->PxeBc.Mode->ProxyOfferReceived) {\r
1693\r
1694 Packet = &Private->ProxyOffer;\r
1695 } else {\r
1696\r
1697 Packet = &Private->Dhcp4Ack;\r
1698 }\r
1699\r
1700 ASSERT (Packet->OfferType == DHCP4_PACKET_TYPE_PXE10);\r
1701\r
1702 VendorOpt = &Packet->PxeVendorOption;\r
1703\r
1704 if (!IS_VALID_BOOT_MENU (VendorOpt->BitMap)) {\r
1705 return EFI_SUCCESS;\r
1706 }\r
1707\r
1708 SetMem (Blank, sizeof(Blank), ' ');\r
1709\r
1710 MenuSize = VendorOpt->BootMenuLen;\r
1711 MenuItem = VendorOpt->BootMenu;\r
1712\r
894d038a 1713 if (MenuSize == 0) {\r
1714 return EFI_NOT_READY;\r
1715 }\r
1716\r
dc361cc5 1717 while (MenuSize > 0) {\r
7b0ae7e8 1718 MenuArray[Index++] = MenuItem;\r
dc361cc5 1719 MenuSize = (UINT8) (MenuSize - (MenuItem->DescLen + 3));\r
1720 MenuItem = (PXEBC_BOOT_MENU_ENTRY *) ((UINT8 *) MenuItem + MenuItem->DescLen + 3);\r
7b0ae7e8 1721 if (Index >= PXEBC_MAX_MENU_NUM) {\r
894d038a 1722 break;\r
1723 }\r
dc361cc5 1724 }\r
1725\r
1726 if (UseDefaultItem) {\r
894d038a 1727 *Type = MenuArray[0]->Type;\r
58bba940 1728 *Type = NTOHS (*Type);\r
dc361cc5 1729 return EFI_SUCCESS;\r
1730 }\r
1731\r
1732 MenuNum = Index;\r
1733\r
1734 for (Index = 0; Index < MenuNum; Index++) {\r
1735 PxeBcDisplayBootItem (MenuArray[Index]->DescStr, MenuArray[Index]->DescLen);\r
1736 }\r
1737\r
1738 TopRow = gST->ConOut->Mode->CursorRow - MenuNum;\r
1739\r
1740 do {\r
7b0ae7e8 1741 ASSERT (Select < PXEBC_MAX_MENU_NUM);\r
dc361cc5 1742 //\r
1743 // highlight selected row\r
1744 //\r
1745 gST->ConOut->SetAttribute (gST->ConOut, EFI_TEXT_ATTR (EFI_BLACK, EFI_LIGHTGRAY));\r
1746 gST->ConOut->SetCursorPosition (gST->ConOut, 0, TopRow + Select);\r
1747 Blank[MenuArray[Select]->DescLen] = 0;\r
1748 AsciiPrint ("%a\r", Blank);\r
1749 PxeBcDisplayBootItem (MenuArray[Select]->DescStr, MenuArray[Select]->DescLen);\r
1750 gST->ConOut->SetCursorPosition (gST->ConOut, 0, TopRow + MenuNum);\r
1751 LastSelect = Select;\r
1752\r
1753 while (gST->ConIn->ReadKeyStroke (gST->ConIn, &InputKey) == EFI_NOT_READY) {\r
1754 gBS->Stall (10 * TICKS_PER_MS);\r
1755 }\r
1756\r
1757 if (!InputKey.ScanCode) {\r
1758 switch (InputKey.UnicodeChar) {\r
1759 case CTRL ('c'):\r
1760 InputKey.ScanCode = SCAN_ESC;\r
1761 break;\r
1762\r
1763 case CTRL ('j'): /* linefeed */\r
1764 case CTRL ('m'): /* return */\r
1765 Finish = TRUE;\r
1766 break;\r
1767\r
1768 case CTRL ('i'): /* tab */\r
1769 case ' ':\r
1770 case 'd':\r
1771 case 'D':\r
1772 InputKey.ScanCode = SCAN_DOWN;\r
1773 break;\r
1774\r
1775 case CTRL ('h'): /* backspace */\r
1776 case 'u':\r
1777 case 'U':\r
1778 InputKey.ScanCode = SCAN_UP;\r
1779 break;\r
1780\r
1781 default:\r
1782 InputKey.ScanCode = 0;\r
1783 }\r
1784 }\r
1785\r
1786 switch (InputKey.ScanCode) {\r
1787 case SCAN_LEFT:\r
1788 case SCAN_UP:\r
f737cfb9 1789 if (Select > 0) {\r
dc361cc5 1790 --Select;\r
1791 }\r
1792\r
1793 break;\r
1794\r
1795 case SCAN_DOWN:\r
1796 case SCAN_RIGHT:\r
1797 if (++Select == MenuNum) {\r
1798 --Select;\r
1799 }\r
1800\r
1801 break;\r
1802\r
1803 case SCAN_PAGE_UP:\r
1804 case SCAN_HOME:\r
1805 Select = 0;\r
1806 break;\r
1807\r
1808 case SCAN_PAGE_DOWN:\r
1809 case SCAN_END:\r
1810 Select = (UINT16) (MenuNum - 1);\r
1811 break;\r
1812\r
1813 case SCAN_ESC:\r
1814 return EFI_ABORTED;\r
1815 }\r
1816\r
1817 /* unhighlight last selected row */\r
1818 gST->ConOut->SetAttribute (gST->ConOut, EFI_TEXT_ATTR (EFI_LIGHTGRAY, EFI_BLACK));\r
1819 gST->ConOut->SetCursorPosition (gST->ConOut, 0, TopRow + LastSelect);\r
1820 Blank[MenuArray[LastSelect]->DescLen] = 0;\r
1821 AsciiPrint ("%a\r", Blank);\r
1822 PxeBcDisplayBootItem (MenuArray[LastSelect]->DescStr, MenuArray[LastSelect]->DescLen);\r
1823 gST->ConOut->SetCursorPosition (gST->ConOut, 0, TopRow + MenuNum);\r
1824 } while (!Finish);\r
1825\r
1826 //\r
1827 // Swap the byte order\r
1828 //\r
58bba940 1829 CopyMem (Type, &MenuArray[Select]->Type, sizeof (UINT16));\r
1830 *Type = NTOHS (*Type);\r
dc361cc5 1831\r
1832 return EFI_SUCCESS;\r
1833}\r
1834\r