]>
Commit | Line | Data |
---|---|---|
1 | /** @file\r | |
2 | Functions implementation related with DHCPv4 for UefiPxeBc Driver.\r | |
3 | \r | |
4 | Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>\r | |
5 | \r | |
6 | This program and the accompanying materials\r | |
7 | are licensed and made available under the terms and conditions of the BSD License\r | |
8 | which accompanies this distribution. The full text of the license may be found at\r | |
9 | http://opensource.org/licenses/bsd-license.php.\r | |
10 | \r | |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
13 | \r | |
14 | **/\r | |
15 | \r | |
16 | #include "PxeBcImpl.h"\r | |
17 | \r | |
18 | //\r | |
19 | // This is a map from the interested DHCP4 option tags' index to the tag value.\r | |
20 | //\r | |
21 | UINT8 mInterestedDhcp4Tags[PXEBC_DHCP4_TAG_INDEX_MAX] = {\r | |
22 | DHCP4_TAG_BOOTFILE_LEN,\r | |
23 | DHCP4_TAG_VENDOR,\r | |
24 | DHCP4_TAG_OVERLOAD,\r | |
25 | DHCP4_TAG_MSG_TYPE,\r | |
26 | DHCP4_TAG_SERVER_ID,\r | |
27 | DHCP4_TAG_VENDOR_CLASS_ID,\r | |
28 | DHCP4_TAG_BOOTFILE\r | |
29 | };\r | |
30 | \r | |
31 | //\r | |
32 | // There are 4 times retries with the value of 4, 8, 16 and 32, refers to PXE2.1 spec.\r | |
33 | //\r | |
34 | UINT32 mPxeDhcpTimeout[4] = {4, 8, 16, 32};\r | |
35 | \r | |
36 | \r | |
37 | /**\r | |
38 | Parse a certain dhcp4 option by OptTag in Buffer, and return with start pointer.\r | |
39 | \r | |
40 | @param[in] Buffer Pointer to the option buffer.\r | |
41 | @param[in] Length Length of the option buffer.\r | |
42 | @param[in] OptTag Tag of the required option.\r | |
43 | \r | |
44 | @retval NULL Failed to find the required option.\r | |
45 | @retval Others The position of the required option.\r | |
46 | \r | |
47 | **/\r | |
48 | EFI_DHCP4_PACKET_OPTION *\r | |
49 | PxeBcParseDhcp4Options (\r | |
50 | IN UINT8 *Buffer,\r | |
51 | IN UINT32 Length,\r | |
52 | IN UINT8 OptTag\r | |
53 | )\r | |
54 | {\r | |
55 | EFI_DHCP4_PACKET_OPTION *Option;\r | |
56 | UINT32 Offset;\r | |
57 | \r | |
58 | Option = (EFI_DHCP4_PACKET_OPTION *) Buffer;\r | |
59 | Offset = 0;\r | |
60 | \r | |
61 | while (Offset < Length && Option->OpCode != DHCP4_TAG_EOP) {\r | |
62 | \r | |
63 | if (Option->OpCode == OptTag) {\r | |
64 | //\r | |
65 | // Found the required option.\r | |
66 | //\r | |
67 | return Option;\r | |
68 | }\r | |
69 | \r | |
70 | //\r | |
71 | // Skip the current option to the next.\r | |
72 | //\r | |
73 | if (Option->OpCode == DHCP4_TAG_PAD) {\r | |
74 | Offset++;\r | |
75 | } else {\r | |
76 | Offset += Option->Length + 2;\r | |
77 | }\r | |
78 | \r | |
79 | Option = (EFI_DHCP4_PACKET_OPTION *) (Buffer + Offset);\r | |
80 | }\r | |
81 | \r | |
82 | return NULL;\r | |
83 | }\r | |
84 | \r | |
85 | \r | |
86 | /**\r | |
87 | Parse the PXE vender options and extract the information from them.\r | |
88 | \r | |
89 | @param[in] Dhcp4Option Pointer to vendor options in buffer.\r | |
90 | @param[in] VendorOption Pointer to structure to store information in vendor options.\r | |
91 | \r | |
92 | **/\r | |
93 | VOID\r | |
94 | PxeBcParseVendorOptions (\r | |
95 | IN EFI_DHCP4_PACKET_OPTION *Dhcp4Option,\r | |
96 | IN PXEBC_VENDOR_OPTION *VendorOption\r | |
97 | )\r | |
98 | {\r | |
99 | UINT32 *BitMap;\r | |
100 | UINT8 VendorOptionLen;\r | |
101 | EFI_DHCP4_PACKET_OPTION *PxeOption;\r | |
102 | UINT8 Offset;\r | |
103 | \r | |
104 | BitMap = VendorOption->BitMap;\r | |
105 | VendorOptionLen = Dhcp4Option->Length;\r | |
106 | PxeOption = (EFI_DHCP4_PACKET_OPTION *) &Dhcp4Option->Data[0];\r | |
107 | Offset = 0;\r | |
108 | \r | |
109 | ASSERT (PxeOption != NULL);\r | |
110 | \r | |
111 | while ((Offset < VendorOptionLen) && (PxeOption->OpCode != DHCP4_TAG_EOP)) {\r | |
112 | //\r | |
113 | // Parse all the interesting PXE vendor options one by one.\r | |
114 | //\r | |
115 | switch (PxeOption->OpCode) {\r | |
116 | \r | |
117 | case PXEBC_VENDOR_TAG_MTFTP_IP:\r | |
118 | \r | |
119 | CopyMem (&VendorOption->MtftpIp, PxeOption->Data, sizeof (EFI_IPv4_ADDRESS));\r | |
120 | break;\r | |
121 | \r | |
122 | case PXEBC_VENDOR_TAG_MTFTP_CPORT:\r | |
123 | \r | |
124 | CopyMem (&VendorOption->MtftpCPort, PxeOption->Data, sizeof (VendorOption->MtftpCPort));\r | |
125 | break;\r | |
126 | \r | |
127 | case PXEBC_VENDOR_TAG_MTFTP_SPORT:\r | |
128 | \r | |
129 | CopyMem (&VendorOption->MtftpSPort, PxeOption->Data, sizeof (VendorOption->MtftpSPort));\r | |
130 | break;\r | |
131 | \r | |
132 | case PXEBC_VENDOR_TAG_MTFTP_TIMEOUT:\r | |
133 | \r | |
134 | VendorOption->MtftpTimeout = *PxeOption->Data;\r | |
135 | break;\r | |
136 | \r | |
137 | case PXEBC_VENDOR_TAG_MTFTP_DELAY:\r | |
138 | \r | |
139 | VendorOption->MtftpDelay = *PxeOption->Data;\r | |
140 | break;\r | |
141 | \r | |
142 | case PXEBC_VENDOR_TAG_DISCOVER_CTRL:\r | |
143 | \r | |
144 | VendorOption->DiscoverCtrl = *PxeOption->Data;\r | |
145 | break;\r | |
146 | \r | |
147 | case PXEBC_VENDOR_TAG_DISCOVER_MCAST:\r | |
148 | \r | |
149 | CopyMem (&VendorOption->DiscoverMcastIp, PxeOption->Data, sizeof (EFI_IPv4_ADDRESS));\r | |
150 | break;\r | |
151 | \r | |
152 | case PXEBC_VENDOR_TAG_BOOT_SERVERS:\r | |
153 | \r | |
154 | VendorOption->BootSvrLen = PxeOption->Length;\r | |
155 | VendorOption->BootSvr = (PXEBC_BOOT_SVR_ENTRY *) PxeOption->Data;\r | |
156 | break;\r | |
157 | \r | |
158 | case PXEBC_VENDOR_TAG_BOOT_MENU:\r | |
159 | \r | |
160 | VendorOption->BootMenuLen = PxeOption->Length;\r | |
161 | VendorOption->BootMenu = (PXEBC_BOOT_MENU_ENTRY *) PxeOption->Data;\r | |
162 | break;\r | |
163 | \r | |
164 | case PXEBC_VENDOR_TAG_MENU_PROMPT:\r | |
165 | \r | |
166 | VendorOption->MenuPromptLen = PxeOption->Length;\r | |
167 | VendorOption->MenuPrompt = (PXEBC_MENU_PROMPT *) PxeOption->Data;\r | |
168 | break;\r | |
169 | \r | |
170 | case PXEBC_VENDOR_TAG_MCAST_ALLOC:\r | |
171 | \r | |
172 | CopyMem (&VendorOption->McastIpBase, PxeOption->Data, sizeof (EFI_IPv4_ADDRESS));\r | |
173 | CopyMem (&VendorOption->McastIpBlock, PxeOption->Data + 4, sizeof (VendorOption->McastIpBlock));\r | |
174 | CopyMem (&VendorOption->McastIpRange, PxeOption->Data + 6, sizeof (VendorOption->McastIpRange));\r | |
175 | break;\r | |
176 | \r | |
177 | case PXEBC_VENDOR_TAG_CREDENTIAL_TYPES:\r | |
178 | \r | |
179 | VendorOption->CredTypeLen = PxeOption->Length;\r | |
180 | VendorOption->CredType = (UINT32 *) PxeOption->Data;\r | |
181 | break;\r | |
182 | \r | |
183 | case PXEBC_VENDOR_TAG_BOOT_ITEM:\r | |
184 | \r | |
185 | CopyMem (&VendorOption->BootSrvType, PxeOption->Data, sizeof (VendorOption->BootSrvType));\r | |
186 | CopyMem (&VendorOption->BootSrvLayer, PxeOption->Data + 2, sizeof (VendorOption->BootSrvLayer));\r | |
187 | break;\r | |
188 | \r | |
189 | default:\r | |
190 | //\r | |
191 | // Not interesting PXE vendor options.\r | |
192 | //\r | |
193 | break;\r | |
194 | }\r | |
195 | \r | |
196 | //\r | |
197 | // Set the bit map for the special PXE options.\r | |
198 | //\r | |
199 | SET_VENDOR_OPTION_BIT_MAP (BitMap, PxeOption->OpCode);\r | |
200 | \r | |
201 | //\r | |
202 | // Continue to the next option.\r | |
203 | //\r | |
204 | if (PxeOption->OpCode == DHCP4_TAG_PAD) {\r | |
205 | Offset++;\r | |
206 | } else {\r | |
207 | Offset = (UINT8) (Offset + PxeOption->Length + 2);\r | |
208 | }\r | |
209 | \r | |
210 | PxeOption = (EFI_DHCP4_PACKET_OPTION *) (Dhcp4Option->Data + Offset);\r | |
211 | }\r | |
212 | }\r | |
213 | \r | |
214 | \r | |
215 | /**\r | |
216 | Build the options buffer for the DHCPv4 request packet.\r | |
217 | \r | |
218 | @param[in] Private Pointer to PxeBc private data.\r | |
219 | @param[out] OptList Pointer to the option pointer array.\r | |
220 | @param[in] Buffer Pointer to the buffer to contain the option list.\r | |
221 | @param[in] NeedMsgType If TRUE, it is necessary to include the Msg type option.\r | |
222 | Otherwise, it is not necessary.\r | |
223 | \r | |
224 | @return Index The count of the built-in options.\r | |
225 | \r | |
226 | **/\r | |
227 | UINT32\r | |
228 | PxeBcBuildDhcp4Options (\r | |
229 | IN PXEBC_PRIVATE_DATA *Private,\r | |
230 | OUT EFI_DHCP4_PACKET_OPTION **OptList,\r | |
231 | IN UINT8 *Buffer,\r | |
232 | IN BOOLEAN NeedMsgType\r | |
233 | )\r | |
234 | {\r | |
235 | UINT32 Index;\r | |
236 | PXEBC_DHCP4_OPTION_ENTRY OptEnt;\r | |
237 | UINT16 Value;\r | |
238 | \r | |
239 | Index = 0;\r | |
240 | OptList[0] = (EFI_DHCP4_PACKET_OPTION *) Buffer;\r | |
241 | \r | |
242 | if (NeedMsgType) {\r | |
243 | //\r | |
244 | // Append message type.\r | |
245 | //\r | |
246 | OptList[Index]->OpCode = DHCP4_TAG_MSG_TYPE;\r | |
247 | OptList[Index]->Length = 1;\r | |
248 | OptEnt.Mesg = (PXEBC_DHCP4_OPTION_MESG *) OptList[Index]->Data;\r | |
249 | OptEnt.Mesg->Type = PXEBC_DHCP4_MSG_TYPE_REQUEST;\r | |
250 | Index++;\r | |
251 | OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);\r | |
252 | \r | |
253 | //\r | |
254 | // Append max message size.\r | |
255 | //\r | |
256 | OptList[Index]->OpCode = DHCP4_TAG_MAXMSG;\r | |
257 | OptList[Index]->Length = (UINT8) sizeof (PXEBC_DHCP4_OPTION_MAX_MESG_SIZE);\r | |
258 | OptEnt.MaxMesgSize = (PXEBC_DHCP4_OPTION_MAX_MESG_SIZE *) OptList[Index]->Data;\r | |
259 | Value = NTOHS (PXEBC_DHCP4_PACKET_MAX_SIZE);\r | |
260 | CopyMem (&OptEnt.MaxMesgSize->Size, &Value, sizeof (UINT16));\r | |
261 | Index++;\r | |
262 | OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);\r | |
263 | }\r | |
264 | \r | |
265 | //\r | |
266 | // Append parameter request list option.\r | |
267 | //\r | |
268 | OptList[Index]->OpCode = DHCP4_TAG_PARA_LIST;\r | |
269 | OptList[Index]->Length = 35;\r | |
270 | OptEnt.Para = (PXEBC_DHCP4_OPTION_PARA *) OptList[Index]->Data;\r | |
271 | OptEnt.Para->ParaList[0] = DHCP4_TAG_NETMASK;\r | |
272 | OptEnt.Para->ParaList[1] = DHCP4_TAG_TIME_OFFSET;\r | |
273 | OptEnt.Para->ParaList[2] = DHCP4_TAG_ROUTER;\r | |
274 | OptEnt.Para->ParaList[3] = DHCP4_TAG_TIME_SERVER;\r | |
275 | OptEnt.Para->ParaList[4] = DHCP4_TAG_NAME_SERVER;\r | |
276 | OptEnt.Para->ParaList[5] = DHCP4_TAG_DNS_SERVER;\r | |
277 | OptEnt.Para->ParaList[6] = DHCP4_TAG_HOSTNAME;\r | |
278 | OptEnt.Para->ParaList[7] = DHCP4_TAG_BOOTFILE_LEN;\r | |
279 | OptEnt.Para->ParaList[8] = DHCP4_TAG_DOMAINNAME;\r | |
280 | OptEnt.Para->ParaList[9] = DHCP4_TAG_ROOTPATH;\r | |
281 | OptEnt.Para->ParaList[10] = DHCP4_TAG_EXTEND_PATH;\r | |
282 | OptEnt.Para->ParaList[11] = DHCP4_TAG_EMTU;\r | |
283 | OptEnt.Para->ParaList[12] = DHCP4_TAG_TTL;\r | |
284 | OptEnt.Para->ParaList[13] = DHCP4_TAG_BROADCAST;\r | |
285 | OptEnt.Para->ParaList[14] = DHCP4_TAG_NIS_DOMAIN;\r | |
286 | OptEnt.Para->ParaList[15] = DHCP4_TAG_NIS_SERVER;\r | |
287 | OptEnt.Para->ParaList[16] = DHCP4_TAG_NTP_SERVER;\r | |
288 | OptEnt.Para->ParaList[17] = DHCP4_TAG_VENDOR;\r | |
289 | OptEnt.Para->ParaList[18] = DHCP4_TAG_REQUEST_IP;\r | |
290 | OptEnt.Para->ParaList[19] = DHCP4_TAG_LEASE;\r | |
291 | OptEnt.Para->ParaList[20] = DHCP4_TAG_SERVER_ID;\r | |
292 | OptEnt.Para->ParaList[21] = DHCP4_TAG_T1;\r | |
293 | OptEnt.Para->ParaList[22] = DHCP4_TAG_T2;\r | |
294 | OptEnt.Para->ParaList[23] = DHCP4_TAG_VENDOR_CLASS_ID;\r | |
295 | OptEnt.Para->ParaList[24] = DHCP4_TAG_TFTP;\r | |
296 | OptEnt.Para->ParaList[25] = DHCP4_TAG_BOOTFILE;\r | |
297 | OptEnt.Para->ParaList[26] = DHCP4_TAG_UUID;\r | |
298 | OptEnt.Para->ParaList[27] = 0x80;\r | |
299 | OptEnt.Para->ParaList[28] = 0x81;\r | |
300 | OptEnt.Para->ParaList[29] = 0x82;\r | |
301 | OptEnt.Para->ParaList[30] = 0x83;\r | |
302 | OptEnt.Para->ParaList[31] = 0x84;\r | |
303 | OptEnt.Para->ParaList[32] = 0x85;\r | |
304 | OptEnt.Para->ParaList[33] = 0x86;\r | |
305 | OptEnt.Para->ParaList[34] = 0x87;\r | |
306 | Index++;\r | |
307 | OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);\r | |
308 | \r | |
309 | //\r | |
310 | // Append UUID/Guid-based client identifier option\r | |
311 | //\r | |
312 | OptList[Index]->OpCode = DHCP4_TAG_UUID;\r | |
313 | OptList[Index]->Length = (UINT8) sizeof (PXEBC_DHCP4_OPTION_UUID);\r | |
314 | OptEnt.Uuid = (PXEBC_DHCP4_OPTION_UUID *) OptList[Index]->Data;\r | |
315 | OptEnt.Uuid->Type = 0;\r | |
316 | Index++;\r | |
317 | OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);\r | |
318 | \r | |
319 | if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID *) OptEnt.Uuid->Guid))) {\r | |
320 | //\r | |
321 | // Zero the Guid to indicate NOT programable if failed to get system Guid.\r | |
322 | //\r | |
323 | ZeroMem (OptEnt.Uuid->Guid, sizeof (EFI_GUID));\r | |
324 | }\r | |
325 | \r | |
326 | //\r | |
327 | // Append client network device interface option\r | |
328 | //\r | |
329 | OptList[Index]->OpCode = DHCP4_TAG_UNDI;\r | |
330 | OptList[Index]->Length = (UINT8) sizeof (PXEBC_DHCP4_OPTION_UNDI);\r | |
331 | OptEnt.Undi = (PXEBC_DHCP4_OPTION_UNDI *) OptList[Index]->Data;\r | |
332 | \r | |
333 | if (Private->Nii != NULL) {\r | |
334 | OptEnt.Undi->Type = Private->Nii->Type;\r | |
335 | OptEnt.Undi->MajorVer = Private->Nii->MajorVer;\r | |
336 | OptEnt.Undi->MinorVer = Private->Nii->MinorVer;\r | |
337 | } else {\r | |
338 | OptEnt.Undi->Type = DEFAULT_UNDI_TYPE;\r | |
339 | OptEnt.Undi->MajorVer = DEFAULT_UNDI_MAJOR;\r | |
340 | OptEnt.Undi->MinorVer = DEFAULT_UNDI_MINOR;\r | |
341 | }\r | |
342 | \r | |
343 | Index++;\r | |
344 | OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);\r | |
345 | \r | |
346 | //\r | |
347 | // Append client system architecture option\r | |
348 | //\r | |
349 | OptList[Index]->OpCode = DHCP4_TAG_ARCH;\r | |
350 | OptList[Index]->Length = (UINT8) sizeof (PXEBC_DHCP4_OPTION_ARCH);\r | |
351 | OptEnt.Arch = (PXEBC_DHCP4_OPTION_ARCH *) OptList[Index]->Data;\r | |
352 | Value = HTONS (EFI_PXE_CLIENT_SYSTEM_ARCHITECTURE);\r | |
353 | CopyMem (&OptEnt.Arch->Type, &Value, sizeof (UINT16));\r | |
354 | Index++;\r | |
355 | OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);\r | |
356 | \r | |
357 | //\r | |
358 | // Append vendor class identify option\r | |
359 | //\r | |
360 | OptList[Index]->OpCode = DHCP4_TAG_VENDOR_CLASS_ID;\r | |
361 | OptList[Index]->Length = (UINT8) sizeof (PXEBC_DHCP4_OPTION_CLID);\r | |
362 | OptEnt.Clid = (PXEBC_DHCP4_OPTION_CLID *) OptList[Index]->Data;\r | |
363 | CopyMem (\r | |
364 | OptEnt.Clid,\r | |
365 | DEFAULT_CLASS_ID_DATA,\r | |
366 | sizeof (PXEBC_DHCP4_OPTION_CLID)\r | |
367 | );\r | |
368 | PxeBcUintnToAscDecWithFormat (\r | |
369 | EFI_PXE_CLIENT_SYSTEM_ARCHITECTURE,\r | |
370 | OptEnt.Clid->ArchitectureType,\r | |
371 | sizeof (OptEnt.Clid->ArchitectureType)\r | |
372 | );\r | |
373 | \r | |
374 | if (Private->Nii != NULL) {\r | |
375 | CopyMem (OptEnt.Clid->InterfaceName, Private->Nii->StringId, sizeof (OptEnt.Clid->InterfaceName));\r | |
376 | PxeBcUintnToAscDecWithFormat (Private->Nii->MajorVer, OptEnt.Clid->UndiMajor, sizeof (OptEnt.Clid->UndiMajor));\r | |
377 | PxeBcUintnToAscDecWithFormat (Private->Nii->MinorVer, OptEnt.Clid->UndiMinor, sizeof (OptEnt.Clid->UndiMinor));\r | |
378 | }\r | |
379 | \r | |
380 | Index++;\r | |
381 | \r | |
382 | return Index;\r | |
383 | }\r | |
384 | \r | |
385 | \r | |
386 | /**\r | |
387 | Create a template DHCPv4 packet as a seed.\r | |
388 | \r | |
389 | @param[out] Seed Pointer to the seed packet.\r | |
390 | @param[in] Udp4 Pointer to EFI_UDP4_PROTOCOL.\r | |
391 | \r | |
392 | **/\r | |
393 | VOID\r | |
394 | PxeBcSeedDhcp4Packet (\r | |
395 | OUT EFI_DHCP4_PACKET *Seed,\r | |
396 | IN EFI_UDP4_PROTOCOL *Udp4\r | |
397 | )\r | |
398 | {\r | |
399 | EFI_SIMPLE_NETWORK_MODE Mode;\r | |
400 | EFI_DHCP4_HEADER *Header;\r | |
401 | \r | |
402 | //\r | |
403 | // Get IfType and HwAddressSize from SNP mode data.\r | |
404 | //\r | |
405 | Udp4->GetModeData (Udp4, NULL, NULL, NULL, &Mode);\r | |
406 | \r | |
407 | Seed->Size = sizeof (EFI_DHCP4_PACKET);\r | |
408 | Seed->Length = sizeof (Seed->Dhcp4);\r | |
409 | Header = &Seed->Dhcp4.Header;\r | |
410 | ZeroMem (Header, sizeof (EFI_DHCP4_HEADER));\r | |
411 | Header->OpCode = PXEBC_DHCP4_OPCODE_REQUEST;\r | |
412 | Header->HwType = Mode.IfType;\r | |
413 | Header->HwAddrLen = (UINT8) Mode.HwAddressSize;\r | |
414 | CopyMem (Header->ClientHwAddr, &Mode.CurrentAddress, Header->HwAddrLen);\r | |
415 | \r | |
416 | Seed->Dhcp4.Magik = PXEBC_DHCP4_MAGIC;\r | |
417 | Seed->Dhcp4.Option[0] = DHCP4_TAG_EOP;\r | |
418 | }\r | |
419 | \r | |
420 | \r | |
421 | /**\r | |
422 | Cache the DHCPv4 packet.\r | |
423 | \r | |
424 | @param[in] Dst Pointer to the cache buffer for DHCPv4 packet.\r | |
425 | @param[in] Src Pointer to the DHCPv4 packet to be cached.\r | |
426 | \r | |
427 | @retval EFI_SUCCESS Packet is copied.\r | |
428 | @retval EFI_BUFFER_TOO_SMALL Cache buffer is not big enough to hold the packet.\r | |
429 | \r | |
430 | **/\r | |
431 | EFI_STATUS\r | |
432 | PxeBcCacheDhcp4Packet (\r | |
433 | IN EFI_DHCP4_PACKET *Dst,\r | |
434 | IN EFI_DHCP4_PACKET *Src\r | |
435 | )\r | |
436 | {\r | |
437 | if (Dst->Size < Src->Length) {\r | |
438 | return EFI_BUFFER_TOO_SMALL;\r | |
439 | }\r | |
440 | \r | |
441 | CopyMem (&Dst->Dhcp4, &Src->Dhcp4, Src->Length);\r | |
442 | Dst->Length = Src->Length;\r | |
443 | \r | |
444 | return EFI_SUCCESS;\r | |
445 | }\r | |
446 | \r | |
447 | \r | |
448 | /**\r | |
449 | Parse the cached DHCPv4 packet, including all the options.\r | |
450 | \r | |
451 | @param[in] Cache4 Pointer to cached DHCPv4 packet.\r | |
452 | \r | |
453 | @retval EFI_SUCCESS Parsed the DHCPv4 packet successfully.\r | |
454 | @retval EFI_DEVICE_ERROR Failed to parse and invalid packet.\r | |
455 | \r | |
456 | **/\r | |
457 | EFI_STATUS\r | |
458 | PxeBcParseDhcp4Packet (\r | |
459 | IN PXEBC_DHCP4_PACKET_CACHE *Cache4\r | |
460 | )\r | |
461 | {\r | |
462 | EFI_DHCP4_PACKET *Offer;\r | |
463 | EFI_DHCP4_PACKET_OPTION **Options;\r | |
464 | EFI_DHCP4_PACKET_OPTION *Option;\r | |
465 | PXEBC_OFFER_TYPE OfferType;\r | |
466 | UINTN Index;\r | |
467 | BOOLEAN IsProxyOffer;\r | |
468 | BOOLEAN IsPxeOffer;\r | |
469 | UINT8 *Ptr8;\r | |
470 | BOOLEAN FileFieldOverloaded;\r | |
471 | \r | |
472 | IsProxyOffer = FALSE;\r | |
473 | IsPxeOffer = FALSE;\r | |
474 | FileFieldOverloaded = FALSE;\r | |
475 | \r | |
476 | ZeroMem (Cache4->OptList, sizeof (Cache4->OptList));\r | |
477 | ZeroMem (&Cache4->VendorOpt, sizeof (Cache4->VendorOpt));\r | |
478 | \r | |
479 | Offer = &Cache4->Packet.Offer;\r | |
480 | Options = Cache4->OptList;\r | |
481 | \r | |
482 | //\r | |
483 | // Parse DHCPv4 options in this offer, and store the pointers.\r | |
484 | // First, try to parse DHCPv4 options from the DHCP optional parameters field.\r | |
485 | //\r | |
486 | for (Index = 0; Index < PXEBC_DHCP4_TAG_INDEX_MAX; Index++) {\r | |
487 | Options[Index] = PxeBcParseDhcp4Options (\r | |
488 | Offer->Dhcp4.Option,\r | |
489 | GET_OPTION_BUFFER_LEN (Offer),\r | |
490 | mInterestedDhcp4Tags[Index]\r | |
491 | );\r | |
492 | }\r | |
493 | //\r | |
494 | // Second, Check if bootfilename and serverhostname is overloaded to carry DHCP options refers to rfc-2132. \r | |
495 | // If yes, try to parse options from the BootFileName field, then ServerName field.\r | |
496 | //\r | |
497 | Option = Options[PXEBC_DHCP4_TAG_INDEX_OVERLOAD];\r | |
498 | if (Option != NULL) {\r | |
499 | if ((Option->Data[0] & PXEBC_DHCP4_OVERLOAD_FILE) != 0) {\r | |
500 | FileFieldOverloaded = TRUE;\r | |
501 | for (Index = 0; Index < PXEBC_DHCP4_TAG_INDEX_MAX; Index++) {\r | |
502 | if (Options[Index] == NULL) {\r | |
503 | Options[Index] = PxeBcParseDhcp4Options (\r | |
504 | (UINT8 *) Offer->Dhcp4.Header.BootFileName,\r | |
505 | sizeof (Offer->Dhcp4.Header.BootFileName),\r | |
506 | mInterestedDhcp4Tags[Index]\r | |
507 | );\r | |
508 | }\r | |
509 | }\r | |
510 | }\r | |
511 | if ((Option->Data[0] & PXEBC_DHCP4_OVERLOAD_SERVER_NAME) != 0) {\r | |
512 | for (Index = 0; Index < PXEBC_DHCP4_TAG_INDEX_MAX; Index++) {\r | |
513 | if (Options[Index] == NULL) {\r | |
514 | Options[Index] = PxeBcParseDhcp4Options (\r | |
515 | (UINT8 *) Offer->Dhcp4.Header.ServerName,\r | |
516 | sizeof (Offer->Dhcp4.Header.ServerName),\r | |
517 | mInterestedDhcp4Tags[Index]\r | |
518 | );\r | |
519 | }\r | |
520 | }\r | |
521 | }\r | |
522 | }\r | |
523 | \r | |
524 | //\r | |
525 | // The offer with zero "yiaddr" is a proxy offer.\r | |
526 | //\r | |
527 | if (Offer->Dhcp4.Header.YourAddr.Addr[0] == 0) {\r | |
528 | IsProxyOffer = TRUE;\r | |
529 | }\r | |
530 | \r | |
531 | //\r | |
532 | // The offer with "PXEClient" is a PXE offer.\r | |
533 | //\r | |
534 | Option = Options[PXEBC_DHCP4_TAG_INDEX_CLASS_ID];\r | |
535 | if ((Option != NULL) && (Option->Length >= 9) &&\r | |
536 | (CompareMem (Option->Data, DEFAULT_CLASS_ID_DATA, 9) == 0)) {\r | |
537 | IsPxeOffer = TRUE;\r | |
538 | }\r | |
539 | \r | |
540 | //\r | |
541 | // Parse PXE vendor options in this offer, and store the contents/pointers.\r | |
542 | //\r | |
543 | Option = Options[PXEBC_DHCP4_TAG_INDEX_VENDOR];\r | |
544 | if (IsPxeOffer && Option != NULL) {\r | |
545 | PxeBcParseVendorOptions (Option, &Cache4->VendorOpt);\r | |
546 | }\r | |
547 | \r | |
548 | //\r | |
549 | // Parse PXE boot file name:\r | |
550 | // According to PXE spec, boot file name should be read from DHCP option 67 (bootfile name) if present.\r | |
551 | // Otherwise, read from boot file field in DHCP header.\r | |
552 | //\r | |
553 | if (Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] != NULL) {\r | |
554 | //\r | |
555 | // RFC 2132, Section 9.5 does not strictly state Bootfile name (option 67) is null\r | |
556 | // terminated string. So force to append null terminated character at the end of string.\r | |
557 | //\r | |
558 | Ptr8 = (UINT8*)&Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE]->Data[0];\r | |
559 | Ptr8 += Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE]->Length;\r | |
560 | if (*(Ptr8 - 1) != '\0') {\r | |
561 | *Ptr8 = '\0';\r | |
562 | }\r | |
563 | } else if (!FileFieldOverloaded && Offer->Dhcp4.Header.BootFileName[0] != 0) {\r | |
564 | //\r | |
565 | // If the bootfile is not present and bootfilename is present in DHCPv4 packet, just parse it.\r | |
566 | // Do not count dhcp option header here, or else will destroy the serverhostname.\r | |
567 | //\r | |
568 | Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] = (EFI_DHCP4_PACKET_OPTION *)\r | |
569 | (&Offer->Dhcp4.Header.BootFileName[0] -\r | |
570 | OFFSET_OF (EFI_DHCP4_PACKET_OPTION, Data[0]));\r | |
571 | \r | |
572 | }\r | |
573 | \r | |
574 | //\r | |
575 | // Determine offer type of the DHCPv4 packet.\r | |
576 | //\r | |
577 | Option = Options[PXEBC_DHCP4_TAG_INDEX_MSG_TYPE];\r | |
578 | if (Option == NULL || Option->Data[0] == 0) {\r | |
579 | //\r | |
580 | // It's a Bootp offer.\r | |
581 | //\r | |
582 | OfferType = PxeOfferTypeBootp;\r | |
583 | \r | |
584 | Option = Cache4->OptList[PXEBC_DHCP4_TAG_INDEX_BOOTFILE];\r | |
585 | if (Option == NULL) {\r | |
586 | //\r | |
587 | // If the Bootp offer without bootfilename, discard it.\r | |
588 | //\r | |
589 | return EFI_DEVICE_ERROR;\r | |
590 | }\r | |
591 | } else {\r | |
592 | \r | |
593 | if (IS_VALID_DISCOVER_VENDOR_OPTION (Cache4->VendorOpt.BitMap)) {\r | |
594 | //\r | |
595 | // It's a PXE10 offer with PXEClient and discover vendor option.\r | |
596 | //\r | |
597 | OfferType = IsProxyOffer ? PxeOfferTypeProxyPxe10 : PxeOfferTypeDhcpPxe10;\r | |
598 | } else if (IS_VALID_MTFTP_VENDOR_OPTION (Cache4->VendorOpt.BitMap)) {\r | |
599 | //\r | |
600 | // It's a WFM11a offer with PXEClient and mtftp vendor option.\r | |
601 | // But multi-cast download is not supported currently, so discard it.\r | |
602 | //\r | |
603 | return EFI_DEVICE_ERROR;\r | |
604 | } else if (IsPxeOffer) {\r | |
605 | //\r | |
606 | // It's a BINL offer only with PXEClient.\r | |
607 | //\r | |
608 | OfferType = IsProxyOffer ? PxeOfferTypeProxyBinl : PxeOfferTypeDhcpBinl;\r | |
609 | } else {\r | |
610 | //\r | |
611 | // It's a DHCPv4 only offer, which is a pure DHCPv4 offer packet.\r | |
612 | //\r | |
613 | OfferType = PxeOfferTypeDhcpOnly;\r | |
614 | }\r | |
615 | }\r | |
616 | \r | |
617 | Cache4->OfferType = OfferType;\r | |
618 | \r | |
619 | return EFI_SUCCESS;\r | |
620 | }\r | |
621 | \r | |
622 | \r | |
623 | /**\r | |
624 | Cache the DHCPv4 ack packet, and parse it on demand.\r | |
625 | \r | |
626 | @param[in] Private Pointer to PxeBc private data.\r | |
627 | @param[in] Ack Pointer to the DHCPv4 ack packet.\r | |
628 | @param[in] Verified If TRUE, parse the ACK packet and store info into mode data.\r | |
629 | \r | |
630 | @retval EFI_SUCCESS Cache and parse the packet successfully.\r | |
631 | @retval EFI_BUFFER_TOO_SMALL Cache buffer is not big enough to hold the packet.\r | |
632 | \r | |
633 | **/\r | |
634 | EFI_STATUS\r | |
635 | PxeBcCopyDhcp4Ack (\r | |
636 | IN PXEBC_PRIVATE_DATA *Private,\r | |
637 | IN EFI_DHCP4_PACKET *Ack,\r | |
638 | IN BOOLEAN Verified\r | |
639 | )\r | |
640 | {\r | |
641 | EFI_PXE_BASE_CODE_MODE *Mode;\r | |
642 | EFI_STATUS Status;\r | |
643 | \r | |
644 | Mode = Private->PxeBc.Mode;\r | |
645 | \r | |
646 | Status = PxeBcCacheDhcp4Packet (&Private->DhcpAck.Dhcp4.Packet.Ack, Ack);\r | |
647 | if (EFI_ERROR (Status)) {\r | |
648 | return Status;\r | |
649 | }\r | |
650 | \r | |
651 | if (Verified) {\r | |
652 | //\r | |
653 | // Parse the ack packet and store it into mode data if needed.\r | |
654 | //\r | |
655 | PxeBcParseDhcp4Packet (&Private->DhcpAck.Dhcp4);\r | |
656 | CopyMem (&Mode->DhcpAck.Dhcpv4, &Ack->Dhcp4, Ack->Length);\r | |
657 | Mode->DhcpAckReceived = TRUE;\r | |
658 | }\r | |
659 | \r | |
660 | return EFI_SUCCESS;\r | |
661 | }\r | |
662 | \r | |
663 | \r | |
664 | /**\r | |
665 | Cache the DHCPv4 proxy offer packet according to the received order.\r | |
666 | \r | |
667 | @param[in] Private Pointer to PxeBc private data.\r | |
668 | @param[in] OfferIndex The received order of offer packets.\r | |
669 | \r | |
670 | @retval EFI_SUCCESS Cache and parse the packet successfully.\r | |
671 | @retval EFI_BUFFER_TOO_SMALL Cache buffer is not big enough to hold the packet.\r | |
672 | \r | |
673 | **/\r | |
674 | EFI_STATUS\r | |
675 | PxeBcCopyProxyOffer (\r | |
676 | IN PXEBC_PRIVATE_DATA *Private,\r | |
677 | IN UINT32 OfferIndex\r | |
678 | )\r | |
679 | {\r | |
680 | EFI_PXE_BASE_CODE_MODE *Mode;\r | |
681 | EFI_DHCP4_PACKET *Offer;\r | |
682 | EFI_STATUS Status;\r | |
683 | \r | |
684 | ASSERT (OfferIndex < Private->OfferNum);\r | |
685 | ASSERT (OfferIndex < PXEBC_OFFER_MAX_NUM);\r | |
686 | \r | |
687 | Mode = Private->PxeBc.Mode;\r | |
688 | Offer = &Private->OfferBuffer[OfferIndex].Dhcp4.Packet.Offer;\r | |
689 | \r | |
690 | //\r | |
691 | // Cache the proxy offer packet and parse it.\r | |
692 | //\r | |
693 | Status = PxeBcCacheDhcp4Packet (&Private->ProxyOffer.Dhcp4.Packet.Offer, Offer);\r | |
694 | if (EFI_ERROR(Status)) {\r | |
695 | return Status;\r | |
696 | }\r | |
697 | \r | |
698 | PxeBcParseDhcp4Packet (&Private->ProxyOffer.Dhcp4);\r | |
699 | \r | |
700 | //\r | |
701 | // Store this packet into mode data.\r | |
702 | //\r | |
703 | CopyMem (&Mode->ProxyOffer.Dhcpv4, &Offer->Dhcp4, Offer->Length);\r | |
704 | Mode->ProxyOfferReceived = TRUE;\r | |
705 | \r | |
706 | return EFI_SUCCESS;\r | |
707 | }\r | |
708 | \r | |
709 | \r | |
710 | /**\r | |
711 | Retry to request bootfile name by the BINL offer.\r | |
712 | \r | |
713 | @param[in] Private Pointer to PxeBc private data.\r | |
714 | @param[in] Index The received order of offer packets.\r | |
715 | \r | |
716 | @retval EFI_SUCCESS Successfully retried to request bootfile name.\r | |
717 | @retval EFI_DEVICE_ERROR Failed to retry bootfile name.\r | |
718 | \r | |
719 | **/\r | |
720 | EFI_STATUS\r | |
721 | PxeBcRetryBinlOffer (\r | |
722 | IN PXEBC_PRIVATE_DATA *Private,\r | |
723 | IN UINT32 Index\r | |
724 | )\r | |
725 | {\r | |
726 | EFI_DHCP4_PACKET *Offer;\r | |
727 | EFI_IP_ADDRESS ServerIp;\r | |
728 | EFI_STATUS Status;\r | |
729 | PXEBC_DHCP4_PACKET_CACHE *Cache4;\r | |
730 | EFI_DHCP4_PACKET *Reply;\r | |
731 | \r | |
732 | ASSERT (Index < PXEBC_OFFER_MAX_NUM);\r | |
733 | ASSERT (Private->OfferBuffer[Index].Dhcp4.OfferType == PxeOfferTypeDhcpBinl ||\r | |
734 | Private->OfferBuffer[Index].Dhcp4.OfferType == PxeOfferTypeProxyBinl);\r | |
735 | \r | |
736 | Offer = &Private->OfferBuffer[Index].Dhcp4.Packet.Offer;\r | |
737 | \r | |
738 | //\r | |
739 | // Prefer to siaddr in header as next server address. If it's zero, then use option 54.\r | |
740 | //\r | |
741 | if (Offer->Dhcp4.Header.ServerAddr.Addr[0] == 0) {\r | |
742 | CopyMem (\r | |
743 | &ServerIp.Addr[0],\r | |
744 | Private->OfferBuffer[Index].Dhcp4.OptList[PXEBC_DHCP4_TAG_INDEX_SERVER_ID]->Data,\r | |
745 | sizeof (EFI_IPv4_ADDRESS)\r | |
746 | );\r | |
747 | } else {\r | |
748 | CopyMem (\r | |
749 | &ServerIp.Addr[0],\r | |
750 | &Offer->Dhcp4.Header.ServerAddr,\r | |
751 | sizeof (EFI_IPv4_ADDRESS)\r | |
752 | );\r | |
753 | }\r | |
754 | \r | |
755 | Private->IsDoDiscover = FALSE;\r | |
756 | Cache4 = &Private->ProxyOffer.Dhcp4;\r | |
757 | Reply = &Cache4->Packet.Offer;\r | |
758 | \r | |
759 | //\r | |
760 | // Send another request packet for bootfile name.\r | |
761 | //\r | |
762 | Status = PxeBcDhcp4Discover (\r | |
763 | Private,\r | |
764 | 0,\r | |
765 | NULL,\r | |
766 | FALSE,\r | |
767 | &ServerIp,\r | |
768 | 0,\r | |
769 | NULL\r | |
770 | );\r | |
771 | if (EFI_ERROR (Status)) {\r | |
772 | return Status;\r | |
773 | }\r | |
774 | \r | |
775 | //\r | |
776 | // Parse the reply for the last request packet.\r | |
777 | //\r | |
778 | Status = PxeBcParseDhcp4Packet (Cache4);\r | |
779 | if (EFI_ERROR (Status)) {\r | |
780 | return Status;\r | |
781 | }\r | |
782 | \r | |
783 | if (Cache4->OfferType != PxeOfferTypeProxyPxe10 &&\r | |
784 | Cache4->OfferType != PxeOfferTypeProxyWfm11a &&\r | |
785 | Cache4->OptList[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] == NULL) {\r | |
786 | //\r | |
787 | // This BINL ack doesn't have discovery option set or multicast option set\r | |
788 | // or bootfile name specified.\r | |
789 | //\r | |
790 | return EFI_DEVICE_ERROR;\r | |
791 | }\r | |
792 | \r | |
793 | //\r | |
794 | // Store the reply into mode data.\r | |
795 | //\r | |
796 | Private->PxeBc.Mode->ProxyOfferReceived = TRUE;\r | |
797 | CopyMem (&Private->PxeBc.Mode->ProxyOffer.Dhcpv4, &Reply->Dhcp4, Reply->Length);\r | |
798 | \r | |
799 | return EFI_SUCCESS;\r | |
800 | }\r | |
801 | \r | |
802 | \r | |
803 | /**\r | |
804 | Cache all the received DHCPv4 offers, and set OfferIndex and OfferCount.\r | |
805 | \r | |
806 | @param[in] Private Pointer to PxeBc private data.\r | |
807 | @param[in] RcvdOffer Pointer to the received offer packet.\r | |
808 | \r | |
809 | @retval EFI_SUCCESS Cache and parse the packet successfully.\r | |
810 | @retval Others Operation failed.\r | |
811 | \r | |
812 | **/\r | |
813 | EFI_STATUS\r | |
814 | PxeBcCacheDhcp4Offer (\r | |
815 | IN PXEBC_PRIVATE_DATA *Private,\r | |
816 | IN EFI_DHCP4_PACKET *RcvdOffer\r | |
817 | )\r | |
818 | {\r | |
819 | PXEBC_DHCP4_PACKET_CACHE *Cache4;\r | |
820 | EFI_DHCP4_PACKET *Offer;\r | |
821 | PXEBC_OFFER_TYPE OfferType;\r | |
822 | EFI_STATUS Status;\r | |
823 | \r | |
824 | ASSERT (Private->OfferNum < PXEBC_OFFER_MAX_NUM);\r | |
825 | Cache4 = &Private->OfferBuffer[Private->OfferNum].Dhcp4;\r | |
826 | Offer = &Cache4->Packet.Offer;\r | |
827 | \r | |
828 | //\r | |
829 | // Cache the content of DHCPv4 packet firstly.\r | |
830 | //\r | |
831 | Status = PxeBcCacheDhcp4Packet (Offer, RcvdOffer);\r | |
832 | if (EFI_ERROR(Status)) {\r | |
833 | return Status;\r | |
834 | }\r | |
835 | \r | |
836 | //\r | |
837 | // Validate the DHCPv4 packet, and parse the options and offer type.\r | |
838 | //\r | |
839 | if (EFI_ERROR (PxeBcParseDhcp4Packet (Cache4))) {\r | |
840 | return EFI_ABORTED;\r | |
841 | }\r | |
842 | \r | |
843 | //\r | |
844 | // Determine whether cache the current offer by type, and record OfferIndex and OfferCount.\r | |
845 | //\r | |
846 | OfferType = Cache4->OfferType;\r | |
847 | ASSERT (OfferType < PxeOfferTypeMax);\r | |
848 | \r | |
849 | if (OfferType == PxeOfferTypeBootp) {\r | |
850 | //\r | |
851 | // It's a Bootp offer, only cache the first one, and discard the others.\r | |
852 | //\r | |
853 | if (Private->OfferCount[OfferType] == 0) {\r | |
854 | Private->OfferIndex[OfferType][0] = Private->OfferNum;\r | |
855 | Private->OfferCount[OfferType] = 1;\r | |
856 | } else {\r | |
857 | return EFI_ABORTED;\r | |
858 | }\r | |
859 | } else {\r | |
860 | ASSERT (Private->OfferCount[OfferType] < PXEBC_OFFER_MAX_NUM);\r | |
861 | if (IS_PROXY_DHCP_OFFER (Offer)) {\r | |
862 | //\r | |
863 | // It's a proxy offer without yiaddr, including PXE10, WFM11a or BINL offer.\r | |
864 | //\r | |
865 | Private->IsProxyRecved = TRUE;\r | |
866 | \r | |
867 | if (OfferType == PxeOfferTypeProxyBinl) {\r | |
868 | //\r | |
869 | // Cache all proxy BINL offers.\r | |
870 | //\r | |
871 | Private->OfferIndex[OfferType][Private->OfferCount[OfferType]] = Private->OfferNum;\r | |
872 | Private->OfferCount[OfferType]++;\r | |
873 | } else if (Private->OfferCount[OfferType] > 0) {\r | |
874 | //\r | |
875 | // Only cache the first PXE10/WFM11a offer, and discard the others.\r | |
876 | //\r | |
877 | Private->OfferIndex[OfferType][0] = Private->OfferNum;\r | |
878 | Private->OfferCount[OfferType] = 1;\r | |
879 | } else {\r | |
880 | return EFI_ABORTED;\r | |
881 | }\r | |
882 | } else {\r | |
883 | //\r | |
884 | // It's a DHCPv4 offer with yiaddr, and cache them all.\r | |
885 | //\r | |
886 | Private->OfferIndex[OfferType][Private->OfferCount[OfferType]] = Private->OfferNum;\r | |
887 | Private->OfferCount[OfferType]++;\r | |
888 | }\r | |
889 | }\r | |
890 | \r | |
891 | Private->OfferNum++;\r | |
892 | \r | |
893 | return EFI_SUCCESS;\r | |
894 | }\r | |
895 | \r | |
896 | \r | |
897 | /**\r | |
898 | Select an DHCPv4 offer, and record SelectIndex and SelectProxyType.\r | |
899 | \r | |
900 | @param[in] Private Pointer to PxeBc private data.\r | |
901 | \r | |
902 | **/\r | |
903 | VOID\r | |
904 | PxeBcSelectDhcp4Offer (\r | |
905 | IN PXEBC_PRIVATE_DATA *Private\r | |
906 | )\r | |
907 | {\r | |
908 | UINT32 Index;\r | |
909 | UINT32 OfferIndex;\r | |
910 | EFI_DHCP4_PACKET *Offer;\r | |
911 | \r | |
912 | Private->SelectIndex = 0;\r | |
913 | \r | |
914 | if (Private->IsOfferSorted) {\r | |
915 | //\r | |
916 | // Select offer by default policy.\r | |
917 | //\r | |
918 | if (Private->OfferCount[PxeOfferTypeDhcpPxe10] > 0) {\r | |
919 | //\r | |
920 | // 1. DhcpPxe10 offer\r | |
921 | //\r | |
922 | Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpPxe10][0] + 1;\r | |
923 | \r | |
924 | } else if (Private->OfferCount[PxeOfferTypeDhcpWfm11a] > 0) {\r | |
925 | //\r | |
926 | // 2. DhcpWfm11a offer\r | |
927 | //\r | |
928 | Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpWfm11a][0] + 1;\r | |
929 | \r | |
930 | } else if (Private->OfferCount[PxeOfferTypeDhcpOnly] > 0 &&\r | |
931 | Private->OfferCount[PxeOfferTypeProxyPxe10] > 0) {\r | |
932 | //\r | |
933 | // 3. DhcpOnly offer and ProxyPxe10 offer.\r | |
934 | //\r | |
935 | Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpOnly][0] + 1;\r | |
936 | Private->SelectProxyType = PxeOfferTypeProxyPxe10;\r | |
937 | \r | |
938 | } else if (Private->OfferCount[PxeOfferTypeDhcpOnly] > 0 &&\r | |
939 | Private->OfferCount[PxeOfferTypeProxyWfm11a] > 0) {\r | |
940 | //\r | |
941 | // 4. DhcpOnly offer and ProxyWfm11a offer.\r | |
942 | //\r | |
943 | Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpOnly][0] + 1;\r | |
944 | Private->SelectProxyType = PxeOfferTypeProxyWfm11a;\r | |
945 | \r | |
946 | } else if (Private->OfferCount[PxeOfferTypeDhcpBinl] > 0) {\r | |
947 | //\r | |
948 | // 5. DhcpBinl offer.\r | |
949 | //\r | |
950 | Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpBinl][0] + 1;\r | |
951 | \r | |
952 | } else if (Private->OfferCount[PxeOfferTypeDhcpOnly] > 0 &&\r | |
953 | Private->OfferCount[PxeOfferTypeProxyBinl] > 0) {\r | |
954 | //\r | |
955 | // 6. DhcpOnly offer and ProxyBinl offer.\r | |
956 | //\r | |
957 | Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpOnly][0] + 1;\r | |
958 | Private->SelectProxyType = PxeOfferTypeProxyBinl;\r | |
959 | \r | |
960 | } else {\r | |
961 | //\r | |
962 | // 7. DhcpOnly offer with bootfilename.\r | |
963 | //\r | |
964 | for (Index = 0; Index < Private->OfferCount[PxeOfferTypeDhcpOnly]; Index++) {\r | |
965 | OfferIndex = Private->OfferIndex[PxeOfferTypeDhcpOnly][Index];\r | |
966 | if (Private->OfferBuffer[OfferIndex].Dhcp4.OptList[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] != NULL) {\r | |
967 | Private->SelectIndex = OfferIndex + 1;\r | |
968 | break;\r | |
969 | }\r | |
970 | }\r | |
971 | //\r | |
972 | // 8. Bootp offer with bootfilename.\r | |
973 | //\r | |
974 | OfferIndex = Private->OfferIndex[PxeOfferTypeBootp][0];\r | |
975 | if (Private->SelectIndex == 0 &&\r | |
976 | Private->OfferCount[PxeOfferTypeBootp] > 0 &&\r | |
977 | Private->OfferBuffer[OfferIndex].Dhcp4.OptList[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] != NULL) {\r | |
978 | Private->SelectIndex = OfferIndex + 1;\r | |
979 | }\r | |
980 | }\r | |
981 | } else {\r | |
982 | //\r | |
983 | // Select offer by received order.\r | |
984 | //\r | |
985 | for (Index = 0; Index < Private->OfferNum; Index++) {\r | |
986 | \r | |
987 | Offer = &Private->OfferBuffer[Index].Dhcp4.Packet.Offer;\r | |
988 | \r | |
989 | if (IS_PROXY_DHCP_OFFER (Offer)) {\r | |
990 | //\r | |
991 | // Skip proxy offers\r | |
992 | //\r | |
993 | continue;\r | |
994 | }\r | |
995 | \r | |
996 | if (!Private->IsProxyRecved &&\r | |
997 | Private->OfferBuffer[Index].Dhcp4.OfferType == PxeOfferTypeDhcpOnly &&\r | |
998 | Private->OfferBuffer[Index].Dhcp4.OptList[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] == NULL) {\r | |
999 | //\r | |
1000 | // Skip if DhcpOnly offer without any other proxy offers or bootfilename.\r | |
1001 | //\r | |
1002 | continue;\r | |
1003 | }\r | |
1004 | \r | |
1005 | //\r | |
1006 | // Record the index of the select offer.\r | |
1007 | //\r | |
1008 | Private->SelectIndex = Index + 1;\r | |
1009 | break;\r | |
1010 | }\r | |
1011 | }\r | |
1012 | }\r | |
1013 | \r | |
1014 | \r | |
1015 | /**\r | |
1016 | Handle the DHCPv4 offer packet.\r | |
1017 | \r | |
1018 | @param[in] Private Pointer to PxeBc private data.\r | |
1019 | \r | |
1020 | @retval EFI_SUCCESS Handled the DHCPv4 offer packet successfully.\r | |
1021 | @retval EFI_NO_RESPONSE No response to the following request packet.\r | |
1022 | @retval EFI_NOT_FOUND No boot filename received.\r | |
1023 | @retval EFI_BUFFER_TOO_SMALL Can't cache the offer pacet.\r | |
1024 | \r | |
1025 | **/\r | |
1026 | EFI_STATUS\r | |
1027 | PxeBcHandleDhcp4Offer (\r | |
1028 | IN PXEBC_PRIVATE_DATA *Private\r | |
1029 | )\r | |
1030 | {\r | |
1031 | PXEBC_DHCP4_PACKET_CACHE *Cache4;\r | |
1032 | EFI_DHCP4_PACKET_OPTION **Options;\r | |
1033 | UINT32 Index;\r | |
1034 | EFI_DHCP4_PACKET *Offer;\r | |
1035 | PXEBC_OFFER_TYPE OfferType;\r | |
1036 | UINT32 ProxyIndex;\r | |
1037 | UINT32 SelectIndex;\r | |
1038 | EFI_STATUS Status;\r | |
1039 | EFI_PXE_BASE_CODE_MODE *Mode;\r | |
1040 | EFI_DHCP4_PACKET *Ack;\r | |
1041 | \r | |
1042 | ASSERT (Private->SelectIndex > 0);\r | |
1043 | SelectIndex = (UINT32) (Private->SelectIndex - 1);\r | |
1044 | ASSERT (SelectIndex < PXEBC_OFFER_MAX_NUM);\r | |
1045 | Cache4 = &Private->OfferBuffer[SelectIndex].Dhcp4;\r | |
1046 | Options = Cache4->OptList;\r | |
1047 | Status = EFI_SUCCESS;\r | |
1048 | \r | |
1049 | if (Cache4->OfferType == PxeOfferTypeDhcpBinl) {\r | |
1050 | //\r | |
1051 | // DhcpBinl offer is selected, so need try to request bootfilename by this offer.\r | |
1052 | //\r | |
1053 | if (EFI_ERROR (PxeBcRetryBinlOffer (Private, SelectIndex))) {\r | |
1054 | Status = EFI_NO_RESPONSE;\r | |
1055 | }\r | |
1056 | } else if (Cache4->OfferType == PxeOfferTypeDhcpOnly) {\r | |
1057 | \r | |
1058 | if (Private->IsProxyRecved) {\r | |
1059 | //\r | |
1060 | // DhcpOnly offer is selected, so need try to request bootfile name.\r | |
1061 | //\r | |
1062 | ProxyIndex = 0;\r | |
1063 | if (Private->IsOfferSorted) {\r | |
1064 | //\r | |
1065 | // The proxy offer should be determined if select by default policy.\r | |
1066 | // IsOfferSorted means all offers are labeled by OfferIndex.\r | |
1067 | //\r | |
1068 | ASSERT (Private->SelectProxyType < PxeOfferTypeMax);\r | |
1069 | ASSERT (Private->OfferCount[Private->SelectProxyType] > 0);\r | |
1070 | \r | |
1071 | if (Private->SelectProxyType == PxeOfferTypeProxyBinl) {\r | |
1072 | //\r | |
1073 | // Try all the cached ProxyBinl offer one by one to request bootfile name.\r | |
1074 | //\r | |
1075 | for (Index = 0; Index < Private->OfferCount[Private->SelectProxyType]; Index++) {\r | |
1076 | ASSERT (Index < PXEBC_OFFER_MAX_NUM);\r | |
1077 | ProxyIndex = Private->OfferIndex[Private->SelectProxyType][Index];\r | |
1078 | if (!EFI_ERROR (PxeBcRetryBinlOffer (Private, ProxyIndex))) {\r | |
1079 | break;\r | |
1080 | }\r | |
1081 | }\r | |
1082 | if (Index == Private->OfferCount[Private->SelectProxyType]) {\r | |
1083 | Status = EFI_NO_RESPONSE;\r | |
1084 | }\r | |
1085 | } else {\r | |
1086 | //\r | |
1087 | // For other proxy offers, only one is buffered.\r | |
1088 | //\r | |
1089 | ProxyIndex = Private->OfferIndex[Private->SelectProxyType][0];\r | |
1090 | }\r | |
1091 | } else {\r | |
1092 | //\r | |
1093 | // The proxy offer should not be determined if select by received order.\r | |
1094 | //\r | |
1095 | Status = EFI_NO_RESPONSE;\r | |
1096 | \r | |
1097 | for (Index = 0; Index < Private->OfferNum; Index++) {\r | |
1098 | ASSERT (Index < PXEBC_OFFER_MAX_NUM);\r | |
1099 | Offer = &Private->OfferBuffer[Index].Dhcp4.Packet.Offer;\r | |
1100 | OfferType = Private->OfferBuffer[Index].Dhcp4.OfferType;\r | |
1101 | if (!IS_PROXY_DHCP_OFFER (Offer)) {\r | |
1102 | //\r | |
1103 | // Skip non proxy DHCPv4 offers.\r | |
1104 | //\r | |
1105 | continue;\r | |
1106 | }\r | |
1107 | \r | |
1108 | if (OfferType == PxeOfferTypeProxyBinl) {\r | |
1109 | //\r | |
1110 | // Try all the cached ProxyBinl offer one by one to request bootfile name.\r | |
1111 | //\r | |
1112 | if (EFI_ERROR (PxeBcRetryBinlOffer (Private, Index))) {\r | |
1113 | continue;\r | |
1114 | }\r | |
1115 | }\r | |
1116 | \r | |
1117 | Private->SelectProxyType = OfferType;\r | |
1118 | ProxyIndex = Index;\r | |
1119 | Status = EFI_SUCCESS;\r | |
1120 | break;\r | |
1121 | }\r | |
1122 | }\r | |
1123 | \r | |
1124 | if (!EFI_ERROR (Status) && Private->SelectProxyType != PxeOfferTypeProxyBinl) {\r | |
1125 | //\r | |
1126 | // Success to try to request by a ProxyPxe10 or ProxyWfm11a offer, copy and parse it.\r | |
1127 | //\r | |
1128 | Status = PxeBcCopyProxyOffer (Private, ProxyIndex);\r | |
1129 | }\r | |
1130 | } else {\r | |
1131 | //\r | |
1132 | // Othewise, the bootfile name must be included in DhcpOnly offer.\r | |
1133 | //\r | |
1134 | if (Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] == NULL) {\r | |
1135 | Status = EFI_NOT_FOUND;\r | |
1136 | }\r | |
1137 | }\r | |
1138 | }\r | |
1139 | \r | |
1140 | if (!EFI_ERROR (Status)) {\r | |
1141 | //\r | |
1142 | // All PXE boot information is ready by now.\r | |
1143 | //\r | |
1144 | Mode = Private->PxeBc.Mode;\r | |
1145 | Offer = &Cache4->Packet.Offer;\r | |
1146 | Ack = &Private->DhcpAck.Dhcp4.Packet.Ack;\r | |
1147 | if (Cache4->OfferType == PxeOfferTypeBootp) {\r | |
1148 | //\r | |
1149 | // Bootp is a special case that only 2 packets involved instead of 4. So the bootp's reply\r | |
1150 | // should be taken as ack.\r | |
1151 | //\r | |
1152 | Ack = Offer;\r | |
1153 | }\r | |
1154 | \r | |
1155 | Status = PxeBcCopyDhcp4Ack (Private, Ack, TRUE);\r | |
1156 | if (EFI_ERROR (Status)) {\r | |
1157 | return Status;\r | |
1158 | }\r | |
1159 | Mode->DhcpDiscoverValid = TRUE;\r | |
1160 | }\r | |
1161 | \r | |
1162 | return Status;\r | |
1163 | }\r | |
1164 | \r | |
1165 | \r | |
1166 | /**\r | |
1167 | EFI_DHCP4_CALLBACK is provided by the consumer of the EFI DHCPv4 Protocol driver\r | |
1168 | to intercept events that occurred in the configuration process.\r | |
1169 | \r | |
1170 | @param[in] This Pointer to the EFI DHCPv4 Protocol.\r | |
1171 | @param[in] Context Pointer to the context set by EFI_DHCP4_PROTOCOL.Configure().\r | |
1172 | @param[in] CurrentState The current operational state of the EFI DHCPv4 Protocol driver.\r | |
1173 | @param[in] Dhcp4Event The event that occurs in the current state, which usually means a\r | |
1174 | state transition.\r | |
1175 | @param[in] Packet The DHCPv4 packet that is going to be sent or already received.\r | |
1176 | @param[out] NewPacket The packet that is used to replace the above Packet.\r | |
1177 | \r | |
1178 | @retval EFI_SUCCESS Tells the EFI DHCPv4 Protocol driver to continue the DHCP process.\r | |
1179 | @retval EFI_NOT_READY Only used in the Dhcp4Selecting state. The EFI DHCPv4 Protocol\r | |
1180 | driver will continue to wait for more DHCPOFFER packets until the\r | |
1181 | retry timeout expires.\r | |
1182 | @retval EFI_ABORTED Tells the EFI DHCPv4 Protocol driver to abort the current process\r | |
1183 | and return to the Dhcp4Init or Dhcp4InitReboot state.\r | |
1184 | \r | |
1185 | **/\r | |
1186 | EFI_STATUS\r | |
1187 | EFIAPI\r | |
1188 | PxeBcDhcp4CallBack (\r | |
1189 | IN EFI_DHCP4_PROTOCOL *This,\r | |
1190 | IN VOID *Context,\r | |
1191 | IN EFI_DHCP4_STATE CurrentState,\r | |
1192 | IN EFI_DHCP4_EVENT Dhcp4Event,\r | |
1193 | IN EFI_DHCP4_PACKET *Packet OPTIONAL,\r | |
1194 | OUT EFI_DHCP4_PACKET **NewPacket OPTIONAL\r | |
1195 | )\r | |
1196 | {\r | |
1197 | PXEBC_PRIVATE_DATA *Private;\r | |
1198 | EFI_PXE_BASE_CODE_MODE *Mode;\r | |
1199 | EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL *Callback;\r | |
1200 | EFI_DHCP4_PACKET_OPTION *MaxMsgSize;\r | |
1201 | UINT16 Value;\r | |
1202 | EFI_STATUS Status;\r | |
1203 | BOOLEAN Received;\r | |
1204 | \r | |
1205 | if ((Dhcp4Event != Dhcp4RcvdOffer) &&\r | |
1206 | (Dhcp4Event != Dhcp4SelectOffer) &&\r | |
1207 | (Dhcp4Event != Dhcp4SendDiscover) &&\r | |
1208 | (Dhcp4Event != Dhcp4RcvdAck)) {\r | |
1209 | return EFI_SUCCESS;\r | |
1210 | }\r | |
1211 | \r | |
1212 | Private = (PXEBC_PRIVATE_DATA *) Context;\r | |
1213 | Mode = Private->PxeBc.Mode;\r | |
1214 | Callback = Private->PxeBcCallback;\r | |
1215 | \r | |
1216 | //\r | |
1217 | // Override the Maximum DHCP Message Size.\r | |
1218 | //\r | |
1219 | MaxMsgSize = PxeBcParseDhcp4Options (\r | |
1220 | Packet->Dhcp4.Option,\r | |
1221 | GET_OPTION_BUFFER_LEN (Packet),\r | |
1222 | DHCP4_TAG_MAXMSG\r | |
1223 | );\r | |
1224 | if (MaxMsgSize != NULL) {\r | |
1225 | Value = HTONS (PXEBC_DHCP4_PACKET_MAX_SIZE);\r | |
1226 | CopyMem (MaxMsgSize->Data, &Value, sizeof (Value));\r | |
1227 | }\r | |
1228 | \r | |
1229 | //\r | |
1230 | // Callback to user if any packets sent or received.\r | |
1231 | //\r | |
1232 | if (Dhcp4Event != Dhcp4SelectOffer && Callback != NULL) {\r | |
1233 | Received = (BOOLEAN) (Dhcp4Event == Dhcp4RcvdOffer || Dhcp4Event == Dhcp4RcvdAck);\r | |
1234 | Status = Callback->Callback (\r | |
1235 | Callback,\r | |
1236 | Private->Function,\r | |
1237 | Received,\r | |
1238 | Packet->Length,\r | |
1239 | (EFI_PXE_BASE_CODE_PACKET *) &Packet->Dhcp4\r | |
1240 | );\r | |
1241 | if (Status != EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE) {\r | |
1242 | return EFI_ABORTED;\r | |
1243 | }\r | |
1244 | }\r | |
1245 | \r | |
1246 | Status = EFI_SUCCESS;\r | |
1247 | \r | |
1248 | switch (Dhcp4Event) {\r | |
1249 | \r | |
1250 | case Dhcp4SendDiscover:\r | |
1251 | if (Packet->Length > PXEBC_DHCP4_PACKET_MAX_SIZE) {\r | |
1252 | //\r | |
1253 | // If the to be sent packet exceeds the maximum length, abort the DHCP process.\r | |
1254 | //\r | |
1255 | Status = EFI_ABORTED;\r | |
1256 | break;\r | |
1257 | }\r | |
1258 | \r | |
1259 | //\r | |
1260 | // Cache the DHCPv4 discover packet to mode data directly.\r | |
1261 | // It need to check SendGuid as well as Dhcp4SendRequest.\r | |
1262 | //\r | |
1263 | CopyMem (&Mode->DhcpDiscover.Dhcpv4, &Packet->Dhcp4, Packet->Length);\r | |
1264 | \r | |
1265 | case Dhcp4SendRequest:\r | |
1266 | if (Packet->Length > PXEBC_DHCP4_PACKET_MAX_SIZE) {\r | |
1267 | //\r | |
1268 | // If the to be sent packet exceeds the maximum length, abort the DHCP process.\r | |
1269 | //\r | |
1270 | Status = EFI_ABORTED;\r | |
1271 | break;\r | |
1272 | }\r | |
1273 | \r | |
1274 | if (Mode->SendGUID) {\r | |
1275 | //\r | |
1276 | // Send the system Guid instead of the MAC address as the hardware address if required.\r | |
1277 | //\r | |
1278 | if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID *) Packet->Dhcp4.Header.ClientHwAddr))) {\r | |
1279 | //\r | |
1280 | // Zero the Guid to indicate NOT programable if failed to get system Guid.\r | |
1281 | //\r | |
1282 | ZeroMem (Packet->Dhcp4.Header.ClientHwAddr, sizeof (EFI_GUID));\r | |
1283 | }\r | |
1284 | Packet->Dhcp4.Header.HwAddrLen = (UINT8) sizeof (EFI_GUID);\r | |
1285 | }\r | |
1286 | break;\r | |
1287 | \r | |
1288 | case Dhcp4RcvdOffer:\r | |
1289 | Status = EFI_NOT_READY;\r | |
1290 | if (Packet->Length > PXEBC_DHCP4_PACKET_MAX_SIZE) {\r | |
1291 | //\r | |
1292 | // Ignore the incoming packets which exceed the maximum length.\r | |
1293 | //\r | |
1294 | break;\r | |
1295 | }\r | |
1296 | if (Private->OfferNum < PXEBC_OFFER_MAX_NUM) {\r | |
1297 | //\r | |
1298 | // Cache the DHCPv4 offers to OfferBuffer[] for select later, and record\r | |
1299 | // the OfferIndex and OfferCount.\r | |
1300 | // If error happens, just ignore this packet and continue to wait more offer.\r | |
1301 | //\r | |
1302 | PxeBcCacheDhcp4Offer (Private, Packet);\r | |
1303 | }\r | |
1304 | break;\r | |
1305 | \r | |
1306 | case Dhcp4SelectOffer:\r | |
1307 | //\r | |
1308 | // Select offer by the default policy or by order, and record the SelectIndex\r | |
1309 | // and SelectProxyType.\r | |
1310 | //\r | |
1311 | PxeBcSelectDhcp4Offer (Private);\r | |
1312 | \r | |
1313 | if (Private->SelectIndex == 0) {\r | |
1314 | Status = EFI_ABORTED;\r | |
1315 | } else {\r | |
1316 | *NewPacket = &Private->OfferBuffer[Private->SelectIndex - 1].Dhcp4.Packet.Offer;\r | |
1317 | }\r | |
1318 | break;\r | |
1319 | \r | |
1320 | case Dhcp4RcvdAck:\r | |
1321 | //\r | |
1322 | // Cache the DHCPv4 ack to Private->Dhcp4Ack, but it's not the final ack in mode data\r | |
1323 | // without verification.\r | |
1324 | //\r | |
1325 | ASSERT (Private->SelectIndex != 0);\r | |
1326 | \r | |
1327 | Status = PxeBcCopyDhcp4Ack (Private, Packet, FALSE);\r | |
1328 | if (EFI_ERROR (Status)) {\r | |
1329 | Status = EFI_ABORTED;\r | |
1330 | }\r | |
1331 | break;\r | |
1332 | \r | |
1333 | default:\r | |
1334 | break;\r | |
1335 | }\r | |
1336 | \r | |
1337 | return Status;\r | |
1338 | }\r | |
1339 | \r | |
1340 | \r | |
1341 | /**\r | |
1342 | Build and send out the request packet for the bootfile, and parse the reply.\r | |
1343 | \r | |
1344 | @param[in] Private Pointer to PxeBc private data.\r | |
1345 | @param[in] Type PxeBc option boot item type.\r | |
1346 | @param[in] Layer Pointer to option boot item layer.\r | |
1347 | @param[in] UseBis Use BIS or not.\r | |
1348 | @param[in] DestIp Pointer to the server address.\r | |
1349 | @param[in] IpCount The total count of the server address.\r | |
1350 | @param[in] SrvList Pointer to EFI_PXE_BASE_CODE_SRVLIST.\r | |
1351 | \r | |
1352 | @retval EFI_SUCCESS Successfully discovered boot file.\r | |
1353 | @retval EFI_OUT_OF_RESOURCES Failed to allocate resource.\r | |
1354 | @retval EFI_NOT_FOUND Can't get the PXE reply packet.\r | |
1355 | @retval Others Failed to discover boot file.\r | |
1356 | \r | |
1357 | **/\r | |
1358 | EFI_STATUS\r | |
1359 | PxeBcDhcp4Discover (\r | |
1360 | IN PXEBC_PRIVATE_DATA *Private,\r | |
1361 | IN UINT16 Type,\r | |
1362 | IN UINT16 *Layer,\r | |
1363 | IN BOOLEAN UseBis,\r | |
1364 | IN EFI_IP_ADDRESS *DestIp,\r | |
1365 | IN UINT16 IpCount,\r | |
1366 | IN EFI_PXE_BASE_CODE_SRVLIST *SrvList\r | |
1367 | )\r | |
1368 | {\r | |
1369 | EFI_PXE_BASE_CODE_UDP_PORT Sport;\r | |
1370 | EFI_PXE_BASE_CODE_MODE *Mode;\r | |
1371 | EFI_DHCP4_PROTOCOL *Dhcp4;\r | |
1372 | EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN Token;\r | |
1373 | BOOLEAN IsBCast;\r | |
1374 | EFI_STATUS Status;\r | |
1375 | UINT16 RepIndex;\r | |
1376 | UINT16 SrvIndex;\r | |
1377 | UINT16 TryIndex;\r | |
1378 | EFI_DHCP4_LISTEN_POINT ListenPoint;\r | |
1379 | EFI_DHCP4_PACKET *Response;\r | |
1380 | UINT8 Buffer[PXEBC_DHCP4_OPTION_MAX_SIZE];\r | |
1381 | EFI_DHCP4_PACKET_OPTION *OptList[PXEBC_DHCP4_OPTION_MAX_NUM];\r | |
1382 | UINT32 OptCount;\r | |
1383 | EFI_DHCP4_PACKET_OPTION *PxeOpt;\r | |
1384 | PXEBC_OPTION_BOOT_ITEM *PxeBootItem;\r | |
1385 | UINT8 VendorOptLen;\r | |
1386 | UINT32 Xid;\r | |
1387 | \r | |
1388 | Mode = Private->PxeBc.Mode;\r | |
1389 | Dhcp4 = Private->Dhcp4;\r | |
1390 | Status = EFI_SUCCESS;\r | |
1391 | \r | |
1392 | ZeroMem (&Token, sizeof (EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN));\r | |
1393 | \r | |
1394 | //\r | |
1395 | // Use broadcast if destination address not specified.\r | |
1396 | //\r | |
1397 | if (DestIp == NULL) {\r | |
1398 | Sport = PXEBC_DHCP4_S_PORT;\r | |
1399 | IsBCast = TRUE;\r | |
1400 | } else {\r | |
1401 | Sport = PXEBC_BS_DISCOVER_PORT;\r | |
1402 | IsBCast = FALSE;\r | |
1403 | }\r | |
1404 | \r | |
1405 | if (!UseBis && Layer != NULL) {\r | |
1406 | *Layer &= EFI_PXE_BASE_CODE_BOOT_LAYER_MASK;\r | |
1407 | }\r | |
1408 | \r | |
1409 | //\r | |
1410 | // Build all the options for the request packet.\r | |
1411 | //\r | |
1412 | OptCount = PxeBcBuildDhcp4Options (Private, OptList, Buffer, TRUE);\r | |
1413 | \r | |
1414 | if (Private->IsDoDiscover) {\r | |
1415 | //\r | |
1416 | // Add vendor option of PXE_BOOT_ITEM\r | |
1417 | //\r | |
1418 | VendorOptLen = (UINT8) ((sizeof (EFI_DHCP4_PACKET_OPTION) - 1) * 2 + sizeof (PXEBC_OPTION_BOOT_ITEM) + 1);\r | |
1419 | OptList[OptCount] = AllocateZeroPool (VendorOptLen);\r | |
1420 | if (OptList[OptCount] == NULL) {\r | |
1421 | return EFI_OUT_OF_RESOURCES;\r | |
1422 | }\r | |
1423 | \r | |
1424 | OptList[OptCount]->OpCode = DHCP4_TAG_VENDOR;\r | |
1425 | OptList[OptCount]->Length = (UINT8) (VendorOptLen - 2);\r | |
1426 | PxeOpt = (EFI_DHCP4_PACKET_OPTION *) OptList[OptCount]->Data;\r | |
1427 | PxeOpt->OpCode = PXEBC_VENDOR_TAG_BOOT_ITEM;\r | |
1428 | PxeOpt->Length = (UINT8) sizeof (PXEBC_OPTION_BOOT_ITEM);\r | |
1429 | PxeBootItem = (PXEBC_OPTION_BOOT_ITEM *) PxeOpt->Data;\r | |
1430 | PxeBootItem->Type = HTONS (Type);\r | |
1431 | PxeOpt->Data[PxeOpt->Length] = DHCP4_TAG_EOP;\r | |
1432 | \r | |
1433 | if (Layer != NULL) {\r | |
1434 | PxeBootItem->Layer = HTONS (*Layer);\r | |
1435 | }\r | |
1436 | \r | |
1437 | OptCount++;\r | |
1438 | }\r | |
1439 | \r | |
1440 | //\r | |
1441 | // Build the request packet with seed packet and option list.\r | |
1442 | //\r | |
1443 | Status = Dhcp4->Build (\r | |
1444 | Dhcp4,\r | |
1445 | &Private->SeedPacket,\r | |
1446 | 0,\r | |
1447 | NULL,\r | |
1448 | OptCount,\r | |
1449 | OptList,\r | |
1450 | &Token.Packet\r | |
1451 | );\r | |
1452 | //\r | |
1453 | // Free the vendor option of PXE_BOOT_ITEM.\r | |
1454 | //\r | |
1455 | if (Private->IsDoDiscover) {\r | |
1456 | FreePool (OptList[OptCount - 1]);\r | |
1457 | }\r | |
1458 | \r | |
1459 | if (EFI_ERROR (Status)) {\r | |
1460 | return Status;\r | |
1461 | }\r | |
1462 | \r | |
1463 | if (Mode->SendGUID) {\r | |
1464 | if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID *) Token.Packet->Dhcp4.Header.ClientHwAddr))) {\r | |
1465 | //\r | |
1466 | // Zero the Guid to indicate NOT programable if failed to get system Guid.\r | |
1467 | //\r | |
1468 | ZeroMem (Token.Packet->Dhcp4.Header.ClientHwAddr, sizeof (EFI_GUID));\r | |
1469 | }\r | |
1470 | Token.Packet->Dhcp4.Header.HwAddrLen = (UINT8) sizeof (EFI_GUID);\r | |
1471 | }\r | |
1472 | \r | |
1473 | //\r | |
1474 | // Set fields of the token for the request packet.\r | |
1475 | //\r | |
1476 | Xid = NET_RANDOM (NetRandomInitSeed ());\r | |
1477 | Token.Packet->Dhcp4.Header.Xid = HTONL (Xid);\r | |
1478 | Token.Packet->Dhcp4.Header.Reserved = HTONS ((UINT16) ((IsBCast) ? 0x8000 : 0x0));\r | |
1479 | CopyMem (&Token.Packet->Dhcp4.Header.ClientAddr, &Private->StationIp, sizeof (EFI_IPv4_ADDRESS));\r | |
1480 | \r | |
1481 | Token.RemotePort = Sport;\r | |
1482 | \r | |
1483 | if (IsBCast) {\r | |
1484 | SetMem (&Token.RemoteAddress, sizeof (EFI_IPv4_ADDRESS), 0xff);\r | |
1485 | } else {\r | |
1486 | CopyMem (&Token.RemoteAddress, DestIp, sizeof (EFI_IPv4_ADDRESS));\r | |
1487 | }\r | |
1488 | \r | |
1489 | CopyMem (&Token.GatewayAddress, &Private->GatewayIp, sizeof (EFI_IPv4_ADDRESS));\r | |
1490 | \r | |
1491 | if (!IsBCast) {\r | |
1492 | Token.ListenPointCount = 1;\r | |
1493 | Token.ListenPoints = &ListenPoint;\r | |
1494 | Token.ListenPoints[0].ListenPort = PXEBC_BS_DISCOVER_PORT;\r | |
1495 | CopyMem (&Token.ListenPoints[0].ListenAddress, &Private->StationIp, sizeof(EFI_IPv4_ADDRESS));\r | |
1496 | CopyMem (&Token.ListenPoints[0].SubnetMask, &Private->SubnetMask, sizeof(EFI_IPv4_ADDRESS));\r | |
1497 | }\r | |
1498 | \r | |
1499 | //\r | |
1500 | // Send out the request packet to discover the bootfile.\r | |
1501 | //\r | |
1502 | for (TryIndex = 1; TryIndex <= PXEBC_BOOT_REQUEST_RETRIES; TryIndex++) {\r | |
1503 | \r | |
1504 | Token.TimeoutValue = (UINT16) (PXEBC_BOOT_REQUEST_TIMEOUT * TryIndex);\r | |
1505 | Token.Packet->Dhcp4.Header.Seconds = (UINT16) (PXEBC_BOOT_REQUEST_TIMEOUT * (TryIndex - 1));\r | |
1506 | \r | |
1507 | Status = Dhcp4->TransmitReceive (Dhcp4, &Token);\r | |
1508 | if (Token.Status != EFI_TIMEOUT) {\r | |
1509 | break;\r | |
1510 | }\r | |
1511 | }\r | |
1512 | \r | |
1513 | if (TryIndex > PXEBC_BOOT_REQUEST_RETRIES) {\r | |
1514 | //\r | |
1515 | // No server response our PXE request\r | |
1516 | //\r | |
1517 | Status = EFI_TIMEOUT;\r | |
1518 | }\r | |
1519 | \r | |
1520 | if (!EFI_ERROR (Status)) {\r | |
1521 | \r | |
1522 | RepIndex = 0;\r | |
1523 | SrvIndex = 0;\r | |
1524 | Response = Token.ResponseList;\r | |
1525 | //\r | |
1526 | // Find the right PXE Reply according to server address.\r | |
1527 | //\r | |
1528 | while (RepIndex < Token.ResponseCount) {\r | |
1529 | if (Response->Length > PXEBC_DHCP4_PACKET_MAX_SIZE) {\r | |
1530 | SrvIndex = 0;\r | |
1531 | RepIndex++;\r | |
1532 | Response = (EFI_DHCP4_PACKET *) ((UINT8 *) Response + Response->Size);\r | |
1533 | continue;\r | |
1534 | }\r | |
1535 | \r | |
1536 | while (SrvIndex < IpCount) {\r | |
1537 | if (SrvList[SrvIndex].AcceptAnyResponse) {\r | |
1538 | break;\r | |
1539 | }\r | |
1540 | if ((SrvList[SrvIndex].Type == Type) &&\r | |
1541 | EFI_IP4_EQUAL (&Response->Dhcp4.Header.ServerAddr, &SrvList[SrvIndex].IpAddr)) {\r | |
1542 | break;\r | |
1543 | }\r | |
1544 | SrvIndex++;\r | |
1545 | }\r | |
1546 | \r | |
1547 | if ((IpCount != SrvIndex) || (IpCount == 0)) {\r | |
1548 | break;\r | |
1549 | }\r | |
1550 | \r | |
1551 | SrvIndex = 0;\r | |
1552 | RepIndex++;\r | |
1553 | Response = (EFI_DHCP4_PACKET *) ((UINT8 *) Response + Response->Size);\r | |
1554 | }\r | |
1555 | \r | |
1556 | if (RepIndex < Token.ResponseCount) {\r | |
1557 | //\r | |
1558 | // Cache the right PXE reply packet here, set valid flag later.\r | |
1559 | // Especially for PXE discover packet, store it into mode data here.\r | |
1560 | //\r | |
1561 | if (Private->IsDoDiscover) {\r | |
1562 | Status = PxeBcCacheDhcp4Packet (&Private->PxeReply.Dhcp4.Packet.Ack, Response);\r | |
1563 | if (EFI_ERROR(Status)) {\r | |
1564 | goto ON_EXIT;\r | |
1565 | }\r | |
1566 | CopyMem (&Mode->PxeDiscover, &Token.Packet->Dhcp4, Token.Packet->Length);\r | |
1567 | } else {\r | |
1568 | Status = PxeBcCacheDhcp4Packet (&Private->ProxyOffer.Dhcp4.Packet.Offer, Response);\r | |
1569 | if (EFI_ERROR(Status)) {\r | |
1570 | goto ON_EXIT;\r | |
1571 | }\r | |
1572 | }\r | |
1573 | } else {\r | |
1574 | //\r | |
1575 | // Not found the right PXE reply packet.\r | |
1576 | //\r | |
1577 | Status = EFI_NOT_FOUND;\r | |
1578 | }\r | |
1579 | }\r | |
1580 | ON_EXIT:\r | |
1581 | \r | |
1582 | if (Token.ResponseList != NULL) {\r | |
1583 | FreePool (Token.ResponseList);\r | |
1584 | }\r | |
1585 | if (Token.Packet != NULL) {\r | |
1586 | FreePool (Token.Packet);\r | |
1587 | }\r | |
1588 | return Status;\r | |
1589 | }\r | |
1590 | \r | |
1591 | /**\r | |
1592 | Switch the Ip4 policy to static.\r | |
1593 | \r | |
1594 | @param[in] Private The pointer to PXEBC_PRIVATE_DATA.\r | |
1595 | \r | |
1596 | @retval EFI_SUCCESS The policy is already configured to static.\r | |
1597 | @retval Others Other error as indicated..\r | |
1598 | \r | |
1599 | **/\r | |
1600 | EFI_STATUS\r | |
1601 | PxeBcSetIp4Policy ( \r | |
1602 | IN PXEBC_PRIVATE_DATA *Private\r | |
1603 | )\r | |
1604 | {\r | |
1605 | EFI_STATUS Status;\r | |
1606 | EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2;\r | |
1607 | EFI_IP4_CONFIG2_POLICY Policy;\r | |
1608 | UINTN DataSize;\r | |
1609 | \r | |
1610 | Ip4Config2 = Private->Ip4Config2;\r | |
1611 | DataSize = sizeof (EFI_IP4_CONFIG2_POLICY);\r | |
1612 | Status = Ip4Config2->GetData (\r | |
1613 | Ip4Config2,\r | |
1614 | Ip4Config2DataTypePolicy,\r | |
1615 | &DataSize,\r | |
1616 | &Policy\r | |
1617 | );\r | |
1618 | if (EFI_ERROR (Status)) {\r | |
1619 | return Status;\r | |
1620 | }\r | |
1621 | \r | |
1622 | if (Policy != Ip4Config2PolicyStatic) {\r | |
1623 | Policy = Ip4Config2PolicyStatic;\r | |
1624 | Status= Ip4Config2->SetData (\r | |
1625 | Ip4Config2,\r | |
1626 | Ip4Config2DataTypePolicy,\r | |
1627 | sizeof (EFI_IP4_CONFIG2_POLICY),\r | |
1628 | &Policy\r | |
1629 | );\r | |
1630 | if (EFI_ERROR (Status)) {\r | |
1631 | return Status;\r | |
1632 | } \r | |
1633 | }\r | |
1634 | \r | |
1635 | return EFI_SUCCESS;\r | |
1636 | }\r | |
1637 | \r | |
1638 | /**\r | |
1639 | Start the D.O.R.A DHCPv4 process to acquire the IPv4 address and other PXE boot information.\r | |
1640 | \r | |
1641 | @param[in] Private Pointer to PxeBc private data.\r | |
1642 | @param[in] Dhcp4 Pointer to the EFI_DHCP4_PROTOCOL\r | |
1643 | \r | |
1644 | @retval EFI_SUCCESS The D.O.R.A process successfully finished.\r | |
1645 | @retval Others Failed to finish the D.O.R.A process.\r | |
1646 | \r | |
1647 | **/\r | |
1648 | EFI_STATUS\r | |
1649 | PxeBcDhcp4Dora (\r | |
1650 | IN PXEBC_PRIVATE_DATA *Private,\r | |
1651 | IN EFI_DHCP4_PROTOCOL *Dhcp4\r | |
1652 | )\r | |
1653 | {\r | |
1654 | EFI_PXE_BASE_CODE_MODE *PxeMode;\r | |
1655 | EFI_DHCP4_CONFIG_DATA Config;\r | |
1656 | EFI_DHCP4_MODE_DATA Mode;\r | |
1657 | EFI_DHCP4_PACKET_OPTION *OptList[PXEBC_DHCP4_OPTION_MAX_NUM];\r | |
1658 | UINT8 Buffer[PXEBC_DHCP4_OPTION_MAX_SIZE];\r | |
1659 | UINT32 OptCount;\r | |
1660 | EFI_STATUS Status;\r | |
1661 | \r | |
1662 | ASSERT (Dhcp4 != NULL);\r | |
1663 | \r | |
1664 | Status = EFI_SUCCESS;\r | |
1665 | PxeMode = Private->PxeBc.Mode;\r | |
1666 | \r | |
1667 | //\r | |
1668 | // Build option list for the request packet.\r | |
1669 | //\r | |
1670 | OptCount = PxeBcBuildDhcp4Options (Private, OptList, Buffer, FALSE);\r | |
1671 | ASSERT (OptCount> 0);\r | |
1672 | \r | |
1673 | ZeroMem (&Mode, sizeof (EFI_DHCP4_MODE_DATA));\r | |
1674 | ZeroMem (&Config, sizeof (EFI_DHCP4_CONFIG_DATA));\r | |
1675 | \r | |
1676 | Config.OptionCount = OptCount;\r | |
1677 | Config.OptionList = OptList;\r | |
1678 | Config.Dhcp4Callback = PxeBcDhcp4CallBack;\r | |
1679 | Config.CallbackContext = Private;\r | |
1680 | Config.DiscoverTryCount = PXEBC_DHCP_RETRIES;\r | |
1681 | Config.DiscoverTimeout = mPxeDhcpTimeout;\r | |
1682 | \r | |
1683 | //\r | |
1684 | // Configure the DHCPv4 instance for PXE boot.\r | |
1685 | //\r | |
1686 | Status = Dhcp4->Configure (Dhcp4, &Config);\r | |
1687 | if (EFI_ERROR (Status)) {\r | |
1688 | goto ON_EXIT;\r | |
1689 | }\r | |
1690 | \r | |
1691 | //\r | |
1692 | // Initialize the record fields for DHCPv4 offer in private data.\r | |
1693 | //\r | |
1694 | Private->IsProxyRecved = FALSE;\r | |
1695 | Private->OfferNum = 0;\r | |
1696 | ZeroMem (Private->OfferCount, sizeof (Private->OfferCount));\r | |
1697 | ZeroMem (Private->OfferIndex, sizeof (Private->OfferIndex));\r | |
1698 | \r | |
1699 | //\r | |
1700 | // Start DHCPv4 D.O.R.A. process to acquire IPv4 address. This may \r | |
1701 | // have already been done, thus do not leave in error if the return\r | |
1702 | // code is EFI_ALREADY_STARTED.\r | |
1703 | //\r | |
1704 | Status = Dhcp4->Start (Dhcp4, NULL);\r | |
1705 | if (EFI_ERROR (Status) && Status != EFI_ALREADY_STARTED) {\r | |
1706 | if (Status == EFI_ICMP_ERROR) {\r | |
1707 | PxeMode->IcmpErrorReceived = TRUE;\r | |
1708 | }\r | |
1709 | goto ON_EXIT;\r | |
1710 | }\r | |
1711 | \r | |
1712 | //\r | |
1713 | // Get the acquired IPv4 address and store them.\r | |
1714 | //\r | |
1715 | Status = Dhcp4->GetModeData (Dhcp4, &Mode);\r | |
1716 | if (EFI_ERROR (Status)) {\r | |
1717 | goto ON_EXIT;\r | |
1718 | }\r | |
1719 | \r | |
1720 | ASSERT (Mode.State == Dhcp4Bound);\r | |
1721 | \r | |
1722 | CopyMem (&Private->StationIp, &Mode.ClientAddress, sizeof (EFI_IPv4_ADDRESS));\r | |
1723 | CopyMem (&Private->SubnetMask, &Mode.SubnetMask, sizeof (EFI_IPv4_ADDRESS));\r | |
1724 | CopyMem (&Private->GatewayIp, &Mode.RouterAddress, sizeof (EFI_IPv4_ADDRESS));\r | |
1725 | CopyMem (&PxeMode->StationIp, &Private->StationIp, sizeof (EFI_IPv4_ADDRESS));\r | |
1726 | CopyMem (&PxeMode->SubnetMask, &Private->SubnetMask, sizeof (EFI_IPv4_ADDRESS));\r | |
1727 | \r | |
1728 | Status = PxeBcFlushStationIp (Private, &Private->StationIp, &Private->SubnetMask);\r | |
1729 | if (EFI_ERROR (Status)) {\r | |
1730 | goto ON_EXIT;\r | |
1731 | }\r | |
1732 | \r | |
1733 | //\r | |
1734 | // Check the selected offer whether BINL retry is needed.\r | |
1735 | //\r | |
1736 | Status = PxeBcHandleDhcp4Offer (Private);\r | |
1737 | \r | |
1738 | AsciiPrint ("\n Station IP address is ");\r | |
1739 | \r | |
1740 | PxeBcShowIp4Addr (&Private->StationIp.v4);\r | |
1741 | AsciiPrint ("\n");\r | |
1742 | \r | |
1743 | ON_EXIT:\r | |
1744 | if (EFI_ERROR (Status)) {\r | |
1745 | Dhcp4->Stop (Dhcp4);\r | |
1746 | Dhcp4->Configure (Dhcp4, NULL);\r | |
1747 | } else {\r | |
1748 | ZeroMem (&Config, sizeof (EFI_DHCP4_CONFIG_DATA));\r | |
1749 | Dhcp4->Configure (Dhcp4, &Config);\r | |
1750 | Private->IsAddressOk = TRUE;\r | |
1751 | }\r | |
1752 | \r | |
1753 | return Status;\r | |
1754 | }\r |