]>
Commit | Line | Data |
---|---|---|
d933e70a JW |
1 | /** @file\r |
2 | Implementation of the boot file download function.\r | |
3 | \r | |
221463c2 | 4 | Copyright (c) 2015 - 2017, Intel Corporation. All rights reserved.<BR>\r |
90f658c4 | 5 | (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r |
d933e70a JW |
6 | This program and the accompanying materials are licensed and made available under \r |
7 | the terms and conditions of the BSD License that accompanies this distribution. \r | |
8 | 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 "HttpBootDxe.h"\r | |
17 | \r | |
18 | /**\r | |
19 | Update the IP and URL device path node to include the boot resource information.\r | |
20 | \r | |
21 | @param[in] Private The pointer to the driver's private data.\r | |
22 | \r | |
23 | @retval EFI_SUCCESS Device patch successfully updated.\r | |
24 | @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.\r | |
25 | @retval Others Unexpected error happened.\r | |
26 | \r | |
27 | **/\r | |
28 | EFI_STATUS\r | |
29 | HttpBootUpdateDevicePath (\r | |
30 | IN HTTP_BOOT_PRIVATE_DATA *Private\r | |
31 | )\r | |
32 | {\r | |
33 | EFI_DEV_PATH *Node;\r | |
34 | EFI_DEVICE_PATH_PROTOCOL *TmpDevicePath;\r | |
35 | EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r | |
36 | UINTN Length;\r | |
37 | EFI_STATUS Status;\r | |
38 | \r | |
39 | TmpDevicePath = NULL;\r | |
40 | \r | |
41 | //\r | |
42 | // Update the IP node with DHCP assigned information.\r | |
43 | //\r | |
44 | if (!Private->UsingIpv6) {\r | |
45 | Node = AllocateZeroPool (sizeof (IPv4_DEVICE_PATH));\r | |
46 | if (Node == NULL) {\r | |
47 | return EFI_OUT_OF_RESOURCES;\r | |
48 | }\r | |
49 | Node->Ipv4.Header.Type = MESSAGING_DEVICE_PATH;\r | |
50 | Node->Ipv4.Header.SubType = MSG_IPv4_DP;\r | |
51 | SetDevicePathNodeLength (Node, sizeof (IPv4_DEVICE_PATH));\r | |
52 | CopyMem (&Node->Ipv4.LocalIpAddress, &Private->StationIp, sizeof (EFI_IPv4_ADDRESS));\r | |
53 | Node->Ipv4.RemotePort = Private->Port;\r | |
54 | Node->Ipv4.Protocol = EFI_IP_PROTO_TCP;\r | |
55 | Node->Ipv4.StaticIpAddress = FALSE;\r | |
56 | CopyMem (&Node->Ipv4.GatewayIpAddress, &Private->GatewayIp, sizeof (EFI_IPv4_ADDRESS));\r | |
57 | CopyMem (&Node->Ipv4.SubnetMask, &Private->SubnetMask, sizeof (EFI_IPv4_ADDRESS));\r | |
b659408b ZL |
58 | } else {\r |
59 | Node = AllocateZeroPool (sizeof (IPv6_DEVICE_PATH));\r | |
60 | if (Node == NULL) {\r | |
d933e70a JW |
61 | return EFI_OUT_OF_RESOURCES;\r |
62 | }\r | |
b659408b ZL |
63 | Node->Ipv6.Header.Type = MESSAGING_DEVICE_PATH;\r |
64 | Node->Ipv6.Header.SubType = MSG_IPv6_DP;\r | |
65 | SetDevicePathNodeLength (Node, sizeof (IPv6_DEVICE_PATH));\r | |
66 | Node->Ipv6.PrefixLength = IP6_PREFIX_LENGTH;\r | |
67 | Node->Ipv6.RemotePort = Private->Port;\r | |
68 | Node->Ipv6.Protocol = EFI_IP_PROTO_TCP; \r | |
69 | Node->Ipv6.IpAddressOrigin = 0;\r | |
70 | CopyMem (&Node->Ipv6.LocalIpAddress, &Private->StationIp.v6, sizeof (EFI_IPv6_ADDRESS));\r | |
71 | CopyMem (&Node->Ipv6.RemoteIpAddress, &Private->ServerIp.v6, sizeof (EFI_IPv6_ADDRESS));\r | |
72 | CopyMem (&Node->Ipv6.GatewayIpAddress, &Private->GatewayIp.v6, sizeof (EFI_IPv6_ADDRESS));\r | |
73 | }\r | |
74 | \r | |
75 | TmpDevicePath = AppendDevicePathNode (Private->ParentDevicePath, (EFI_DEVICE_PATH_PROTOCOL*) Node);\r | |
76 | FreePool (Node);\r | |
77 | if (TmpDevicePath == NULL) {\r | |
78 | return EFI_OUT_OF_RESOURCES;\r | |
d933e70a JW |
79 | }\r |
80 | \r | |
81 | //\r | |
82 | // Update the URI node with the boot file URI.\r | |
83 | //\r | |
84 | Length = sizeof (EFI_DEVICE_PATH_PROTOCOL) + AsciiStrSize (Private->BootFileUri);\r | |
85 | Node = AllocatePool (Length);\r | |
86 | if (Node == NULL) {\r | |
87 | FreePool (TmpDevicePath);\r | |
88 | return EFI_OUT_OF_RESOURCES;\r | |
89 | }\r | |
90 | Node->DevPath.Type = MESSAGING_DEVICE_PATH;\r | |
91 | Node->DevPath.SubType = MSG_URI_DP;\r | |
92 | SetDevicePathNodeLength (Node, Length);\r | |
93 | CopyMem ((UINT8*) Node + sizeof (EFI_DEVICE_PATH_PROTOCOL), Private->BootFileUri, AsciiStrSize (Private->BootFileUri));\r | |
94 | \r | |
95 | NewDevicePath = AppendDevicePathNode (TmpDevicePath, (EFI_DEVICE_PATH_PROTOCOL*) Node);\r | |
96 | FreePool (Node);\r | |
97 | FreePool (TmpDevicePath);\r | |
98 | if (NewDevicePath == NULL) {\r | |
99 | return EFI_OUT_OF_RESOURCES;\r | |
100 | }\r | |
101 | \r | |
b659408b ZL |
102 | if (!Private->UsingIpv6) {\r |
103 | //\r | |
104 | // Reinstall the device path protocol of the child handle.\r | |
105 | //\r | |
106 | Status = gBS->ReinstallProtocolInterface (\r | |
107 | Private->Ip4Nic->Controller,\r | |
108 | &gEfiDevicePathProtocolGuid,\r | |
109 | Private->Ip4Nic->DevicePath,\r | |
110 | NewDevicePath\r | |
111 | );\r | |
112 | if (EFI_ERROR (Status)) {\r | |
113 | return Status;\r | |
114 | }\r | |
115 | \r | |
116 | FreePool (Private->Ip4Nic->DevicePath);\r | |
117 | Private->Ip4Nic->DevicePath = NewDevicePath;\r | |
118 | } else {\r | |
119 | //\r | |
120 | // Reinstall the device path protocol of the child handle.\r | |
121 | //\r | |
122 | Status = gBS->ReinstallProtocolInterface (\r | |
123 | Private->Ip6Nic->Controller,\r | |
124 | &gEfiDevicePathProtocolGuid,\r | |
125 | Private->Ip6Nic->DevicePath,\r | |
126 | NewDevicePath\r | |
127 | );\r | |
128 | if (EFI_ERROR (Status)) {\r | |
129 | return Status;\r | |
130 | }\r | |
131 | FreePool (Private->Ip6Nic->DevicePath);\r | |
132 | Private->Ip6Nic->DevicePath = NewDevicePath;\r | |
d933e70a JW |
133 | }\r |
134 | \r | |
d933e70a JW |
135 | return EFI_SUCCESS;\r |
136 | }\r | |
137 | \r | |
138 | /**\r | |
139 | Parse the boot file URI information from the selected Dhcp4 offer packet.\r | |
140 | \r | |
141 | @param[in] Private The pointer to the driver's private data.\r | |
142 | \r | |
143 | @retval EFI_SUCCESS Successfully parsed out all the boot information.\r | |
144 | @retval Others Failed to parse out the boot information.\r | |
145 | \r | |
146 | **/\r | |
147 | EFI_STATUS\r | |
b659408b | 148 | HttpBootDhcp4ExtractUriInfo (\r |
d933e70a JW |
149 | IN HTTP_BOOT_PRIVATE_DATA *Private\r |
150 | )\r | |
151 | {\r | |
152 | HTTP_BOOT_DHCP4_PACKET_CACHE *SelectOffer;\r | |
153 | HTTP_BOOT_DHCP4_PACKET_CACHE *HttpOffer;\r | |
154 | UINT32 SelectIndex;\r | |
155 | UINT32 ProxyIndex;\r | |
156 | EFI_DHCP4_PACKET_OPTION *Option;\r | |
157 | EFI_STATUS Status;\r | |
158 | \r | |
159 | ASSERT (Private != NULL);\r | |
160 | ASSERT (Private->SelectIndex != 0);\r | |
161 | SelectIndex = Private->SelectIndex - 1;\r | |
162 | ASSERT (SelectIndex < HTTP_BOOT_OFFER_MAX_NUM);\r | |
163 | \r | |
164 | Status = EFI_SUCCESS;\r | |
165 | \r | |
166 | //\r | |
167 | // SelectOffer contains the IP address configuration and name server configuration.\r | |
168 | // HttpOffer contains the boot file URL.\r | |
169 | //\r | |
170 | SelectOffer = &Private->OfferBuffer[SelectIndex].Dhcp4;\r | |
fa848a40 FS |
171 | if (Private->FilePathUri == NULL) {\r |
172 | //\r | |
173 | // In Corporate environment, we need a HttpOffer.\r | |
174 | //\r | |
175 | if ((SelectOffer->OfferType == HttpOfferTypeDhcpIpUri) || \r | |
176 | (SelectOffer->OfferType == HttpOfferTypeDhcpIpUriDns) ||\r | |
177 | (SelectOffer->OfferType == HttpOfferTypeDhcpNameUriDns)) {\r | |
178 | HttpOffer = SelectOffer;\r | |
179 | } else {\r | |
180 | ASSERT (Private->SelectProxyType != HttpOfferTypeMax);\r | |
181 | ProxyIndex = Private->OfferIndex[Private->SelectProxyType][0];\r | |
182 | HttpOffer = &Private->OfferBuffer[ProxyIndex].Dhcp4;\r | |
183 | }\r | |
184 | Private->BootFileUriParser = HttpOffer->UriParser;\r | |
185 | Private->BootFileUri = (CHAR8*) HttpOffer->OptList[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data;\r | |
d933e70a | 186 | } else {\r |
fa848a40 FS |
187 | //\r |
188 | // In Home environment the BootFileUri comes from the FilePath.\r | |
189 | //\r | |
190 | Private->BootFileUriParser = Private->FilePathUriParser;\r | |
191 | Private->BootFileUri = Private->FilePathUri;\r | |
d933e70a JW |
192 | }\r |
193 | \r | |
221463c2 JW |
194 | //\r |
195 | // Check the URI scheme.\r | |
196 | //\r | |
197 | Status = HttpBootCheckUriScheme (Private->BootFileUri);\r | |
198 | if (EFI_ERROR (Status)) {\r | |
199 | DEBUG ((EFI_D_ERROR, "HttpBootDhcp4ExtractUriInfo: %r.\n", Status));\r | |
200 | return Status;\r | |
201 | }\r | |
202 | \r | |
d933e70a JW |
203 | //\r |
204 | // Configure the default DNS server if server assigned.\r | |
205 | //\r | |
fa848a40 FS |
206 | if ((SelectOffer->OfferType == HttpOfferTypeDhcpNameUriDns) || \r |
207 | (SelectOffer->OfferType == HttpOfferTypeDhcpDns) ||\r | |
208 | (SelectOffer->OfferType == HttpOfferTypeDhcpIpUriDns)) {\r | |
d933e70a JW |
209 | Option = SelectOffer->OptList[HTTP_BOOT_DHCP4_TAG_INDEX_DNS_SERVER];\r |
210 | ASSERT (Option != NULL);\r | |
211 | Status = HttpBootRegisterIp4Dns (\r | |
212 | Private,\r | |
213 | Option->Length,\r | |
214 | Option->Data\r | |
215 | );\r | |
216 | if (EFI_ERROR (Status)) {\r | |
217 | return Status;\r | |
218 | }\r | |
219 | }\r | |
220 | \r | |
221 | //\r | |
222 | // Extract the port from URL, and use default HTTP port 80 if not provided.\r | |
223 | //\r | |
224 | Status = HttpUrlGetPort (\r | |
fa848a40 FS |
225 | Private->BootFileUri,\r |
226 | Private->BootFileUriParser,\r | |
d933e70a JW |
227 | &Private->Port\r |
228 | );\r | |
229 | if (EFI_ERROR (Status) || Private->Port == 0) {\r | |
230 | Private->Port = 80;\r | |
231 | }\r | |
232 | \r | |
d933e70a JW |
233 | //\r |
234 | // All boot informations are valid here.\r | |
235 | //\r | |
d933e70a JW |
236 | \r |
237 | //\r | |
238 | // Update the device path to include the IP and boot URI information.\r | |
239 | //\r | |
240 | Status = HttpBootUpdateDevicePath (Private);\r | |
241 | \r | |
242 | return Status;\r | |
243 | }\r | |
244 | \r | |
b659408b ZL |
245 | /**\r |
246 | Parse the boot file URI information from the selected Dhcp6 offer packet.\r | |
247 | \r | |
248 | @param[in] Private The pointer to the driver's private data.\r | |
249 | \r | |
250 | @retval EFI_SUCCESS Successfully parsed out all the boot information.\r | |
251 | @retval Others Failed to parse out the boot information.\r | |
252 | \r | |
253 | **/\r | |
254 | EFI_STATUS\r | |
255 | HttpBootDhcp6ExtractUriInfo (\r | |
256 | IN HTTP_BOOT_PRIVATE_DATA *Private\r | |
257 | )\r | |
258 | {\r | |
259 | HTTP_BOOT_DHCP6_PACKET_CACHE *SelectOffer;\r | |
260 | HTTP_BOOT_DHCP6_PACKET_CACHE *HttpOffer;\r | |
261 | UINT32 SelectIndex;\r | |
262 | UINT32 ProxyIndex;\r | |
263 | EFI_DHCP6_PACKET_OPTION *Option;\r | |
264 | EFI_IPv6_ADDRESS IpAddr;\r | |
265 | CHAR8 *HostName;\r | |
b9679cd7 | 266 | UINTN HostNameSize;\r |
b659408b ZL |
267 | CHAR16 *HostNameStr;\r |
268 | EFI_STATUS Status;\r | |
269 | \r | |
270 | ASSERT (Private != NULL);\r | |
271 | ASSERT (Private->SelectIndex != 0);\r | |
272 | SelectIndex = Private->SelectIndex - 1;\r | |
273 | ASSERT (SelectIndex < HTTP_BOOT_OFFER_MAX_NUM);\r | |
274 | \r | |
275 | Status = EFI_SUCCESS;\r | |
276 | HostName = NULL;\r | |
277 | //\r | |
278 | // SelectOffer contains the IP address configuration and name server configuration.\r | |
279 | // HttpOffer contains the boot file URL.\r | |
280 | //\r | |
281 | SelectOffer = &Private->OfferBuffer[SelectIndex].Dhcp6;\r | |
fa848a40 FS |
282 | if (Private->FilePathUri == NULL) {\r |
283 | //\r | |
284 | // In Corporate environment, we need a HttpOffer.\r | |
285 | //\r | |
286 | if ((SelectOffer->OfferType == HttpOfferTypeDhcpIpUri) ||\r | |
287 | (SelectOffer->OfferType == HttpOfferTypeDhcpIpUriDns) ||\r | |
288 | (SelectOffer->OfferType == HttpOfferTypeDhcpNameUriDns)) {\r | |
289 | HttpOffer = SelectOffer;\r | |
290 | } else {\r | |
291 | ASSERT (Private->SelectProxyType != HttpOfferTypeMax);\r | |
292 | ProxyIndex = Private->OfferIndex[Private->SelectProxyType][0];\r | |
293 | HttpOffer = &Private->OfferBuffer[ProxyIndex].Dhcp6;\r | |
294 | }\r | |
295 | Private->BootFileUriParser = HttpOffer->UriParser;\r | |
296 | Private->BootFileUri = (CHAR8*) HttpOffer->OptList[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL]->Data;\r | |
b659408b | 297 | } else {\r |
fa848a40 FS |
298 | //\r |
299 | // In Home environment the BootFileUri comes from the FilePath.\r | |
300 | //\r | |
301 | Private->BootFileUriParser = Private->FilePathUriParser;\r | |
302 | Private->BootFileUri = Private->FilePathUri;\r | |
b659408b ZL |
303 | }\r |
304 | \r | |
305 | //\r | |
221463c2 JW |
306 | // Check the URI scheme.\r |
307 | //\r | |
308 | Status = HttpBootCheckUriScheme (Private->BootFileUri);\r | |
309 | if (EFI_ERROR (Status)) {\r | |
310 | DEBUG ((EFI_D_ERROR, "HttpBootDhcp6ExtractUriInfo: %r.\n", Status));\r | |
311 | return Status;\r | |
312 | }\r | |
313 | \r | |
314 | //\r | |
b659408b ZL |
315 | // Set the Local station address to IP layer.\r |
316 | //\r | |
317 | Status = HttpBootSetIp6Address (Private);\r | |
318 | if (EFI_ERROR (Status)) {\r | |
319 | return Status;\r | |
320 | }\r | |
08c6239d JW |
321 | \r |
322 | //\r | |
323 | // Register the IPv6 gateway address to the network device.\r | |
324 | //\r | |
325 | Status = HttpBootSetIp6Gateway (Private);\r | |
326 | if (EFI_ERROR (Status)) {\r | |
327 | return Status;\r | |
328 | }\r | |
b659408b ZL |
329 | \r |
330 | //\r | |
331 | // Configure the default DNS server if server assigned.\r | |
332 | //\r | |
fa848a40 FS |
333 | if ((SelectOffer->OfferType == HttpOfferTypeDhcpNameUriDns) || \r |
334 | (SelectOffer->OfferType == HttpOfferTypeDhcpDns) ||\r | |
335 | (SelectOffer->OfferType == HttpOfferTypeDhcpIpUriDns)) {\r | |
b659408b ZL |
336 | Option = SelectOffer->OptList[HTTP_BOOT_DHCP6_IDX_DNS_SERVER];\r |
337 | ASSERT (Option != NULL);\r | |
338 | Status = HttpBootSetIp6Dns (\r | |
339 | Private,\r | |
340 | HTONS (Option->OpLen),\r | |
341 | Option->Data\r | |
342 | );\r | |
343 | if (EFI_ERROR (Status)) {\r | |
344 | return Status;\r | |
345 | }\r | |
346 | }\r | |
347 | \r | |
348 | //\r | |
349 | // Extract the HTTP server Ip frome URL. This is used to Check route table \r | |
350 | // whether can send message to HTTP Server Ip through the GateWay.\r | |
351 | //\r | |
352 | Status = HttpUrlGetIp6 (\r | |
fa848a40 FS |
353 | Private->BootFileUri,\r |
354 | Private->BootFileUriParser,\r | |
b659408b ZL |
355 | &IpAddr\r |
356 | );\r | |
357 | \r | |
358 | if (EFI_ERROR (Status)) {\r | |
359 | //\r | |
360 | // The Http server address is expressed by Name Ip, so perform DNS resolution\r | |
361 | //\r | |
362 | Status = HttpUrlGetHostName (\r | |
fa848a40 FS |
363 | Private->BootFileUri,\r |
364 | Private->BootFileUriParser,\r | |
b659408b ZL |
365 | &HostName\r |
366 | );\r | |
367 | if (EFI_ERROR (Status)) {\r | |
368 | return Status;\r | |
369 | }\r | |
b9679cd7 SZ |
370 | \r |
371 | HostNameSize = AsciiStrSize (HostName);\r | |
372 | HostNameStr = AllocateZeroPool (HostNameSize * sizeof (CHAR16));\r | |
b659408b ZL |
373 | if (HostNameStr == NULL) {\r |
374 | Status = EFI_OUT_OF_RESOURCES;\r | |
375 | goto Error;\r | |
376 | }\r | |
377 | \r | |
b9679cd7 | 378 | AsciiStrToUnicodeStrS (HostName, HostNameStr, HostNameSize);\r |
b659408b ZL |
379 | Status = HttpBootDns (Private, HostNameStr, &IpAddr);\r |
380 | FreePool (HostNameStr);\r | |
381 | if (EFI_ERROR (Status)) {\r | |
382 | goto Error;\r | |
383 | } \r | |
384 | } \r | |
385 | \r | |
08c6239d | 386 | CopyMem (&Private->ServerIp.v6, &IpAddr, sizeof (EFI_IPv6_ADDRESS));\r |
b659408b ZL |
387 | \r |
388 | //\r | |
389 | // Extract the port from URL, and use default HTTP port 80 if not provided.\r | |
390 | //\r | |
391 | Status = HttpUrlGetPort (\r | |
fa848a40 FS |
392 | Private->BootFileUri,\r |
393 | Private->BootFileUriParser,\r | |
b659408b ZL |
394 | &Private->Port\r |
395 | );\r | |
396 | if (EFI_ERROR (Status) || Private->Port == 0) {\r | |
397 | Private->Port = 80;\r | |
398 | }\r | |
399 | \r | |
b659408b ZL |
400 | //\r |
401 | // All boot informations are valid here.\r | |
402 | //\r | |
95b5c32f | 403 | \r |
b659408b ZL |
404 | //\r |
405 | // Update the device path to include the IP and boot URI information.\r | |
406 | //\r | |
407 | Status = HttpBootUpdateDevicePath (Private);\r | |
408 | \r | |
409 | Error:\r | |
410 | \r | |
411 | if (HostName != NULL) {\r | |
412 | FreePool (HostName);\r | |
413 | }\r | |
414 | \r | |
415 | return Status;\r | |
416 | }\r | |
417 | \r | |
418 | \r | |
d933e70a JW |
419 | /**\r |
420 | Discover all the boot information for boot file.\r | |
421 | \r | |
422 | @param[in, out] Private The pointer to the driver's private data.\r | |
423 | \r | |
424 | @retval EFI_SUCCESS Successfully obtained all the boot information .\r | |
425 | @retval Others Failed to retrieve the boot information.\r | |
426 | \r | |
427 | **/\r | |
428 | EFI_STATUS\r | |
429 | HttpBootDiscoverBootInfo (\r | |
430 | IN OUT HTTP_BOOT_PRIVATE_DATA *Private\r | |
431 | )\r | |
432 | {\r | |
433 | EFI_STATUS Status;\r | |
434 | \r | |
435 | //\r | |
436 | // Start D.O.R.A/S.A.R.R exchange to acquire station ip address and\r | |
437 | // other Http boot information.\r | |
438 | //\r | |
439 | Status = HttpBootDhcp (Private);\r | |
440 | if (EFI_ERROR (Status)) {\r | |
441 | return Status;\r | |
442 | }\r | |
443 | \r | |
444 | if (!Private->UsingIpv6) {\r | |
b659408b | 445 | Status = HttpBootDhcp4ExtractUriInfo (Private);\r |
d933e70a | 446 | } else {\r |
b659408b | 447 | Status = HttpBootDhcp6ExtractUriInfo (Private);\r |
d933e70a JW |
448 | }\r |
449 | \r | |
450 | return Status;\r | |
451 | }\r | |
452 | \r | |
95b5c32f FS |
453 | /**\r |
454 | HttpIo Callback function which will be invoked when specified HTTP_IO_CALLBACK_EVENT happened.\r | |
455 | \r | |
456 | @param[in] EventType Indicate the Event type that occurs in the current callback.\r | |
457 | @param[in] Message HTTP message which will be send to, or just received from HTTP server.\r | |
458 | @param[in] Context The Callback Context pointer.\r | |
459 | \r | |
460 | @retval EFI_SUCCESS Tells the HttpIo to continue the HTTP process.\r | |
461 | @retval Others Tells the HttpIo to abort the current HTTP process.\r | |
462 | **/\r | |
463 | EFI_STATUS\r | |
464 | EFIAPI\r | |
465 | HttpBootHttpIoCallback (\r | |
466 | IN HTTP_IO_CALLBACK_EVENT EventType,\r | |
467 | IN EFI_HTTP_MESSAGE *Message,\r | |
468 | IN VOID *Context\r | |
469 | )\r | |
470 | {\r | |
471 | HTTP_BOOT_PRIVATE_DATA *Private;\r | |
472 | EFI_STATUS Status;\r | |
473 | Private = (HTTP_BOOT_PRIVATE_DATA *) Context;\r | |
474 | if (Private->HttpBootCallback != NULL) {\r | |
475 | Status = Private->HttpBootCallback->Callback (\r | |
476 | Private->HttpBootCallback,\r | |
477 | EventType == HttpIoRequest ? HttpBootHttpRequest : HttpBootHttpResponse,\r | |
478 | EventType == HttpIoRequest ? FALSE : TRUE,\r | |
479 | sizeof (EFI_HTTP_MESSAGE),\r | |
480 | (VOID *) Message\r | |
481 | );\r | |
482 | return Status;\r | |
483 | }\r | |
484 | return EFI_SUCCESS;\r | |
485 | }\r | |
486 | \r | |
d933e70a JW |
487 | /**\r |
488 | Create a HttpIo instance for the file download.\r | |
489 | \r | |
490 | @param[in] Private The pointer to the driver's private data.\r | |
491 | \r | |
492 | @retval EFI_SUCCESS Successfully created.\r | |
493 | @retval Others Failed to create HttpIo.\r | |
494 | \r | |
495 | **/\r | |
496 | EFI_STATUS\r | |
497 | HttpBootCreateHttpIo (\r | |
498 | IN HTTP_BOOT_PRIVATE_DATA *Private\r | |
499 | )\r | |
500 | {\r | |
501 | HTTP_IO_CONFIG_DATA ConfigData;\r | |
502 | EFI_STATUS Status;\r | |
75372581 | 503 | EFI_HANDLE ImageHandle;\r |
d933e70a JW |
504 | \r |
505 | ASSERT (Private != NULL);\r | |
506 | \r | |
507 | ZeroMem (&ConfigData, sizeof (HTTP_IO_CONFIG_DATA));\r | |
508 | if (!Private->UsingIpv6) {\r | |
b659408b | 509 | ConfigData.Config4.HttpVersion = HttpVersion11;\r |
d933e70a JW |
510 | ConfigData.Config4.RequestTimeOut = HTTP_BOOT_REQUEST_TIMEOUT;\r |
511 | IP4_COPY_ADDRESS (&ConfigData.Config4.LocalIp, &Private->StationIp.v4);\r | |
512 | IP4_COPY_ADDRESS (&ConfigData.Config4.SubnetMask, &Private->SubnetMask.v4);\r | |
75372581 | 513 | ImageHandle = Private->Ip4Nic->ImageHandle;\r |
d933e70a | 514 | } else {\r |
b659408b ZL |
515 | ConfigData.Config6.HttpVersion = HttpVersion11;\r |
516 | ConfigData.Config6.RequestTimeOut = HTTP_BOOT_REQUEST_TIMEOUT;\r | |
517 | IP6_COPY_ADDRESS (&ConfigData.Config6.LocalIp, &Private->StationIp.v6);\r | |
75372581 | 518 | ImageHandle = Private->Ip6Nic->ImageHandle;\r |
d933e70a JW |
519 | }\r |
520 | \r | |
521 | Status = HttpIoCreateIo (\r | |
75372581 | 522 | ImageHandle,\r |
d933e70a JW |
523 | Private->Controller,\r |
524 | Private->UsingIpv6 ? IP_VERSION_6 : IP_VERSION_4,\r | |
525 | &ConfigData,\r | |
95b5c32f FS |
526 | HttpBootHttpIoCallback,\r |
527 | (VOID *) Private,\r | |
d933e70a JW |
528 | &Private->HttpIo\r |
529 | );\r | |
530 | if (EFI_ERROR (Status)) {\r | |
531 | return Status;\r | |
532 | }\r | |
533 | \r | |
534 | Private->HttpCreated = TRUE;\r | |
535 | return EFI_SUCCESS;\r | |
536 | }\r | |
537 | \r | |
d933e70a JW |
538 | /**\r |
539 | Release all the resource of a cache item.\r | |
540 | \r | |
541 | @param[in] Cache The pointer to the cache item.\r | |
542 | \r | |
543 | **/\r | |
544 | VOID\r | |
545 | HttpBootFreeCache (\r | |
546 | IN HTTP_BOOT_CACHE_CONTENT *Cache\r | |
547 | )\r | |
548 | {\r | |
549 | UINTN Index;\r | |
550 | LIST_ENTRY *Entry;\r | |
551 | LIST_ENTRY *NextEntry;\r | |
552 | HTTP_BOOT_ENTITY_DATA *EntityData;\r | |
553 | \r | |
554 | if (Cache != NULL) {\r | |
555 | //\r | |
556 | // Free the request data\r | |
557 | //\r | |
558 | if (Cache->RequestData != NULL) {\r | |
559 | if (Cache->RequestData->Url != NULL) {\r | |
560 | FreePool (Cache->RequestData->Url);\r | |
561 | }\r | |
562 | FreePool (Cache->RequestData);\r | |
563 | }\r | |
564 | \r | |
565 | //\r | |
566 | // Free the response header\r | |
567 | //\r | |
568 | if (Cache->ResponseData != NULL) {\r | |
569 | if (Cache->ResponseData->Headers != NULL) {\r | |
570 | for (Index = 0; Index < Cache->ResponseData->HeaderCount; Index++) {\r | |
571 | FreePool (Cache->ResponseData->Headers[Index].FieldName);\r | |
572 | FreePool (Cache->ResponseData->Headers[Index].FieldValue);\r | |
573 | }\r | |
574 | FreePool (Cache->ResponseData->Headers);\r | |
575 | }\r | |
576 | }\r | |
577 | \r | |
578 | //\r | |
579 | // Free the response body\r | |
580 | //\r | |
581 | NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Cache->EntityDataList) {\r | |
582 | EntityData = NET_LIST_USER_STRUCT (Entry, HTTP_BOOT_ENTITY_DATA, Link);\r | |
583 | if (EntityData->Block != NULL) {\r | |
584 | FreePool (EntityData->Block);\r | |
585 | }\r | |
586 | RemoveEntryList (&EntityData->Link);\r | |
587 | FreePool (EntityData);\r | |
588 | }\r | |
589 | \r | |
590 | FreePool (Cache);\r | |
591 | }\r | |
592 | }\r | |
593 | \r | |
594 | /**\r | |
595 | Clean up all cached data.\r | |
596 | \r | |
597 | @param[in] Private The pointer to the driver's private data.\r | |
598 | \r | |
599 | **/\r | |
600 | VOID\r | |
601 | HttpBootFreeCacheList (\r | |
602 | IN HTTP_BOOT_PRIVATE_DATA *Private\r | |
603 | )\r | |
604 | {\r | |
605 | LIST_ENTRY *Entry;\r | |
606 | LIST_ENTRY *NextEntry;\r | |
607 | HTTP_BOOT_CACHE_CONTENT *Cache;\r | |
608 | \r | |
609 | NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Private->CacheList) {\r | |
610 | Cache = NET_LIST_USER_STRUCT (Entry, HTTP_BOOT_CACHE_CONTENT, Link);\r | |
611 | RemoveEntryList (&Cache->Link);\r | |
612 | HttpBootFreeCache (Cache);\r | |
613 | }\r | |
614 | }\r | |
615 | \r | |
7552c24e FS |
616 | /**\r |
617 | Get the file content from cached data.\r | |
618 | \r | |
619 | @param[in] Private The pointer to the driver's private data.\r | |
620 | @param[in] Uri Uri of the file to be retrieved from cache.\r | |
621 | @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return\r | |
622 | code of EFI_SUCCESS, the amount of data transferred to\r | |
623 | Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,\r | |
624 | the size of Buffer required to retrieve the requested file.\r | |
625 | @param[out] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,\r | |
626 | then the size of the requested file is returned in\r | |
627 | BufferSize.\r | |
587d204c | 628 | @param[out] ImageType The image type of the downloaded file.\r |
7552c24e FS |
629 | \r |
630 | @retval EFI_SUCCESS Successfully created.\r | |
631 | @retval Others Failed to create HttpIo.\r | |
632 | \r | |
633 | **/\r | |
634 | EFI_STATUS\r | |
635 | HttpBootGetFileFromCache (\r | |
636 | IN HTTP_BOOT_PRIVATE_DATA *Private,\r | |
637 | IN CHAR16 *Uri,\r | |
638 | IN OUT UINTN *BufferSize,\r | |
587d204c FS |
639 | OUT UINT8 *Buffer,\r |
640 | OUT HTTP_BOOT_IMAGE_TYPE *ImageType\r | |
7552c24e FS |
641 | )\r |
642 | {\r | |
643 | LIST_ENTRY *Entry;\r | |
644 | LIST_ENTRY *Entry2;\r | |
645 | HTTP_BOOT_CACHE_CONTENT *Cache;\r | |
646 | HTTP_BOOT_ENTITY_DATA *EntityData;\r | |
647 | UINTN CopyedSize;\r | |
648 | \r | |
587d204c | 649 | if (Uri == NULL || BufferSize == 0 || Buffer == NULL || ImageType == NULL) {\r |
7552c24e FS |
650 | return EFI_INVALID_PARAMETER;\r |
651 | }\r | |
652 | \r | |
7552c24e FS |
653 | NET_LIST_FOR_EACH (Entry, &Private->CacheList) {\r |
654 | Cache = NET_LIST_USER_STRUCT (Entry, HTTP_BOOT_CACHE_CONTENT, Link);\r | |
655 | //\r | |
656 | // Compare the URI to see whether we already have a cache for this file.\r | |
657 | //\r | |
658 | if ((Cache->RequestData != NULL) &&\r | |
659 | (Cache->RequestData->Url != NULL) &&\r | |
660 | (StrCmp (Uri, Cache->RequestData->Url) == 0)) \r | |
661 | {\r | |
662 | //\r | |
587d204c FS |
663 | // Hit in cache, record image type.\r |
664 | //\r | |
665 | *ImageType = Cache->ImageType;\r | |
666 | \r | |
667 | //\r | |
668 | // Check buffer size.\r | |
7552c24e FS |
669 | //\r |
670 | if (*BufferSize < Cache->EntityLength) {\r | |
671 | *BufferSize = Cache->EntityLength;\r | |
672 | return EFI_BUFFER_TOO_SMALL;\r | |
673 | }\r | |
674 | \r | |
675 | //\r | |
676 | // Fill data to buffer.\r | |
677 | //\r | |
678 | CopyedSize = 0;\r | |
679 | NET_LIST_FOR_EACH (Entry2, &Cache->EntityDataList) {\r | |
680 | EntityData = NET_LIST_USER_STRUCT (Entry2, HTTP_BOOT_ENTITY_DATA, Link);\r | |
681 | if (*BufferSize > CopyedSize) {\r | |
682 | CopyMem (\r | |
683 | Buffer + CopyedSize,\r | |
684 | EntityData->DataStart,\r | |
685 | MIN (EntityData->DataLength, *BufferSize - CopyedSize)\r | |
686 | );\r | |
687 | CopyedSize += MIN (EntityData->DataLength, *BufferSize - CopyedSize);\r | |
688 | }\r | |
689 | }\r | |
690 | *BufferSize = CopyedSize;\r | |
7552c24e FS |
691 | return EFI_SUCCESS;\r |
692 | }\r | |
693 | }\r | |
694 | \r | |
695 | return EFI_NOT_FOUND;\r | |
696 | }\r | |
697 | \r | |
d933e70a JW |
698 | /**\r |
699 | A callback function to intercept events during message parser.\r | |
700 | \r | |
701 | This function will be invoked during HttpParseMessageBody() with various events type. An error\r | |
702 | return status of the callback function will cause the HttpParseMessageBody() aborted.\r | |
703 | \r | |
704 | @param[in] EventType Event type of this callback call.\r | |
705 | @param[in] Data A pointer to data buffer.\r | |
706 | @param[in] Length Length in bytes of the Data.\r | |
707 | @param[in] Context Callback context set by HttpInitMsgParser().\r | |
708 | \r | |
709 | @retval EFI_SUCCESS Continue to parser the message body.\r | |
710 | @retval Others Abort the parse.\r | |
711 | \r | |
712 | **/\r | |
713 | EFI_STATUS\r | |
714 | EFIAPI\r | |
715 | HttpBootGetBootFileCallback (\r | |
716 | IN HTTP_BODY_PARSE_EVENT EventType,\r | |
717 | IN CHAR8 *Data,\r | |
718 | IN UINTN Length,\r | |
719 | IN VOID *Context\r | |
720 | )\r | |
721 | {\r | |
722 | HTTP_BOOT_CALLBACK_DATA *CallbackData;\r | |
723 | HTTP_BOOT_ENTITY_DATA *NewEntityData;\r | |
95b5c32f FS |
724 | EFI_STATUS Status;\r |
725 | EFI_HTTP_BOOT_CALLBACK_PROTOCOL *HttpBootCallback;\r | |
d933e70a JW |
726 | \r |
727 | //\r | |
728 | // We only care about the entity data.\r | |
729 | //\r | |
730 | if (EventType != BodyParseEventOnData) {\r | |
731 | return EFI_SUCCESS;\r | |
732 | }\r | |
733 | \r | |
734 | CallbackData = (HTTP_BOOT_CALLBACK_DATA *) Context;\r | |
95b5c32f FS |
735 | HttpBootCallback = CallbackData->Private->HttpBootCallback;\r |
736 | if (HttpBootCallback != NULL) {\r | |
737 | Status = HttpBootCallback->Callback (\r | |
738 | HttpBootCallback,\r | |
739 | HttpBootHttpEntityBody,\r | |
740 | TRUE,\r | |
741 | (UINT32)Length,\r | |
742 | Data\r | |
743 | );\r | |
744 | if (EFI_ERROR (Status)) {\r | |
745 | return Status;\r | |
746 | }\r | |
747 | }\r | |
d933e70a JW |
748 | //\r |
749 | // Copy data if caller has provided a buffer.\r | |
750 | //\r | |
751 | if (CallbackData->BufferSize > CallbackData->CopyedSize) {\r | |
752 | CopyMem (\r | |
753 | CallbackData->Buffer + CallbackData->CopyedSize,\r | |
754 | Data,\r | |
755 | MIN (Length, CallbackData->BufferSize - CallbackData->CopyedSize)\r | |
756 | );\r | |
757 | CallbackData->CopyedSize += MIN (Length, CallbackData->BufferSize - CallbackData->CopyedSize);\r | |
758 | }\r | |
759 | \r | |
7fd71047 FS |
760 | //\r |
761 | // The caller doesn't provide a buffer, save the block into cache list.\r | |
762 | //\r | |
763 | if (CallbackData->Cache != NULL) {\r | |
764 | NewEntityData = AllocatePool (sizeof (HTTP_BOOT_ENTITY_DATA));\r | |
765 | if (NewEntityData == NULL) {\r | |
766 | return EFI_OUT_OF_RESOURCES;\r | |
767 | }\r | |
768 | if (CallbackData->NewBlock) {\r | |
769 | NewEntityData->Block = CallbackData->Block;\r | |
770 | CallbackData->Block = NULL;\r | |
771 | }\r | |
772 | NewEntityData->DataLength = Length;\r | |
773 | NewEntityData->DataStart = (UINT8*) Data;\r | |
774 | InsertTailList (&CallbackData->Cache->EntityDataList, &NewEntityData->Link);\r | |
775 | }\r | |
d933e70a JW |
776 | return EFI_SUCCESS;\r |
777 | }\r | |
778 | \r | |
779 | /**\r | |
780 | This function download the boot file by using UEFI HTTP protocol.\r | |
781 | \r | |
782 | @param[in] Private The pointer to the driver's private data.\r | |
783 | @param[in] HeaderOnly Only request the response header, it could save a lot of time if\r | |
784 | the caller only want to know the size of the requested file.\r | |
785 | @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return\r | |
786 | code of EFI_SUCCESS, the amount of data transferred to\r | |
787 | Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,\r | |
788 | the size of Buffer required to retrieve the requested file.\r | |
789 | @param[out] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,\r | |
790 | then the size of the requested file is returned in\r | |
791 | BufferSize.\r | |
587d204c | 792 | @param[out] ImageType The image type of the downloaded file.\r |
d933e70a JW |
793 | \r |
794 | @retval EFI_SUCCESS The file was loaded.\r | |
795 | @retval EFI_INVALID_PARAMETER BufferSize is NULL or Buffer Size is not NULL but Buffer is NULL.\r | |
796 | @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources\r | |
797 | @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry.\r | |
798 | BufferSize has been updated with the size needed to complete\r | |
799 | the request.\r | |
800 | @retval Others Unexpected error happened.\r | |
801 | \r | |
802 | **/\r | |
803 | EFI_STATUS\r | |
804 | HttpBootGetBootFile (\r | |
805 | IN HTTP_BOOT_PRIVATE_DATA *Private,\r | |
806 | IN BOOLEAN HeaderOnly,\r | |
807 | IN OUT UINTN *BufferSize,\r | |
587d204c FS |
808 | OUT UINT8 *Buffer,\r |
809 | OUT HTTP_BOOT_IMAGE_TYPE *ImageType\r | |
d933e70a JW |
810 | )\r |
811 | {\r | |
812 | EFI_STATUS Status;\r | |
072289f4 | 813 | EFI_HTTP_STATUS_CODE StatusCode;\r |
d933e70a JW |
814 | CHAR8 *HostName;\r |
815 | EFI_HTTP_REQUEST_DATA *RequestData;\r | |
ef422fc5 PA |
816 | HTTP_IO_RESPONSE_DATA *ResponseData;\r |
817 | HTTP_IO_RESPONSE_DATA ResponseBody;\r | |
d933e70a JW |
818 | HTTP_IO *HttpIo;\r |
819 | HTTP_IO_HEADER *HttpIoHeader;\r | |
820 | VOID *Parser;\r | |
821 | HTTP_BOOT_CALLBACK_DATA Context;\r | |
822 | UINTN ContentLength;\r | |
823 | HTTP_BOOT_CACHE_CONTENT *Cache;\r | |
824 | UINT8 *Block;\r | |
b9679cd7 | 825 | UINTN UrlSize;\r |
d933e70a | 826 | CHAR16 *Url;\r |
7552c24e FS |
827 | BOOLEAN IdentityMode;\r |
828 | UINTN ReceivedSize;\r | |
d933e70a JW |
829 | \r |
830 | ASSERT (Private != NULL);\r | |
831 | ASSERT (Private->HttpCreated);\r | |
832 | \r | |
587d204c | 833 | if (BufferSize == NULL || ImageType == NULL) {\r |
d933e70a JW |
834 | return EFI_INVALID_PARAMETER;\r |
835 | }\r | |
836 | \r | |
837 | if (*BufferSize != 0 && Buffer == NULL) {\r | |
838 | return EFI_INVALID_PARAMETER;\r | |
839 | }\r | |
840 | \r | |
841 | //\r | |
842 | // First, check whether we already cached the requested Uri.\r | |
843 | //\r | |
b9679cd7 SZ |
844 | UrlSize = AsciiStrSize (Private->BootFileUri);\r |
845 | Url = AllocatePool (UrlSize * sizeof (CHAR16));\r | |
d933e70a JW |
846 | if (Url == NULL) {\r |
847 | return EFI_OUT_OF_RESOURCES;\r | |
848 | }\r | |
b9679cd7 | 849 | AsciiStrToUnicodeStrS (Private->BootFileUri, Url, UrlSize);\r |
d933e70a | 850 | if (!HeaderOnly) {\r |
587d204c | 851 | Status = HttpBootGetFileFromCache (Private, Url, BufferSize, Buffer, ImageType);\r |
d933e70a JW |
852 | if (Status != EFI_NOT_FOUND) {\r |
853 | FreePool (Url);\r | |
854 | return Status;\r | |
855 | }\r | |
856 | }\r | |
857 | \r | |
858 | //\r | |
859 | // Not found in cache, try to download it through HTTP.\r | |
860 | //\r | |
861 | \r | |
862 | //\r | |
7fd71047 | 863 | // 1. Create a temp cache item for the requested URI if caller doesn't provide buffer.\r |
d933e70a JW |
864 | //\r |
865 | Cache = NULL;\r | |
7fd71047 | 866 | if ((!HeaderOnly) && (*BufferSize == 0)) {\r |
d933e70a JW |
867 | Cache = AllocateZeroPool (sizeof (HTTP_BOOT_CACHE_CONTENT));\r |
868 | if (Cache == NULL) {\r | |
869 | Status = EFI_OUT_OF_RESOURCES;\r | |
870 | goto ERROR_1;\r | |
871 | }\r | |
587d204c | 872 | Cache->ImageType = ImageTypeMax;\r |
d933e70a JW |
873 | InitializeListHead (&Cache->EntityDataList);\r |
874 | }\r | |
875 | \r | |
876 | //\r | |
877 | // 2. Send HTTP request message.\r | |
878 | //\r | |
879 | \r | |
880 | //\r | |
881 | // 2.1 Build HTTP header for the request, 3 header is needed to download a boot file:\r | |
882 | // Host\r | |
883 | // Accept\r | |
884 | // User-Agent\r | |
885 | //\r | |
886 | HttpIoHeader = HttpBootCreateHeader (3);\r | |
887 | if (HttpIoHeader == NULL) {\r | |
888 | Status = EFI_OUT_OF_RESOURCES;\r | |
889 | goto ERROR_2;\r | |
890 | }\r | |
891 | \r | |
892 | //\r | |
893 | // Add HTTP header field 1: Host\r | |
894 | //\r | |
895 | HostName = NULL;\r | |
896 | Status = HttpUrlGetHostName (\r | |
897 | Private->BootFileUri,\r | |
898 | Private->BootFileUriParser,\r | |
899 | &HostName\r | |
900 | );\r | |
901 | if (EFI_ERROR (Status)) {\r | |
902 | goto ERROR_3;\r | |
903 | }\r | |
904 | Status = HttpBootSetHeader (\r | |
905 | HttpIoHeader,\r | |
90f658c4 | 906 | HTTP_HEADER_HOST,\r |
d933e70a JW |
907 | HostName\r |
908 | );\r | |
909 | FreePool (HostName);\r | |
910 | if (EFI_ERROR (Status)) {\r | |
911 | goto ERROR_3;\r | |
912 | }\r | |
913 | \r | |
914 | //\r | |
915 | // Add HTTP header field 2: Accept\r | |
916 | //\r | |
917 | Status = HttpBootSetHeader (\r | |
918 | HttpIoHeader,\r | |
90f658c4 | 919 | HTTP_HEADER_ACCEPT,\r |
d933e70a JW |
920 | "*/*"\r |
921 | );\r | |
922 | if (EFI_ERROR (Status)) {\r | |
923 | goto ERROR_3;\r | |
924 | }\r | |
925 | \r | |
926 | //\r | |
927 | // Add HTTP header field 3: User-Agent\r | |
928 | //\r | |
929 | Status = HttpBootSetHeader (\r | |
930 | HttpIoHeader,\r | |
90f658c4 | 931 | HTTP_HEADER_USER_AGENT,\r |
d933e70a JW |
932 | HTTP_USER_AGENT_EFI_HTTP_BOOT\r |
933 | );\r | |
934 | if (EFI_ERROR (Status)) {\r | |
935 | goto ERROR_3;\r | |
936 | }\r | |
937 | \r | |
938 | //\r | |
939 | // 2.2 Build the rest of HTTP request info.\r | |
940 | //\r | |
941 | RequestData = AllocatePool (sizeof (EFI_HTTP_REQUEST_DATA));\r | |
942 | if (RequestData == NULL) {\r | |
943 | Status = EFI_OUT_OF_RESOURCES;\r | |
944 | goto ERROR_3;\r | |
945 | }\r | |
946 | RequestData->Method = HeaderOnly ? HttpMethodHead : HttpMethodGet;\r | |
947 | RequestData->Url = Url;\r | |
d933e70a JW |
948 | \r |
949 | //\r | |
950 | // 2.3 Record the request info in a temp cache item.\r | |
951 | //\r | |
7fd71047 | 952 | if (Cache != NULL) {\r |
d933e70a JW |
953 | Cache->RequestData = RequestData;\r |
954 | }\r | |
955 | \r | |
956 | //\r | |
957 | // 2.4 Send out the request to HTTP server.\r | |
958 | //\r | |
959 | HttpIo = &Private->HttpIo;\r | |
960 | Status = HttpIoSendRequest (\r | |
961 | HttpIo,\r | |
962 | RequestData,\r | |
963 | HttpIoHeader->HeaderCount,\r | |
964 | HttpIoHeader->Headers,\r | |
965 | 0,\r | |
966 | NULL\r | |
967 | );\r | |
968 | if (EFI_ERROR (Status)) {\r | |
969 | goto ERROR_4;\r | |
970 | }\r | |
971 | \r | |
972 | //\r | |
973 | // 3. Receive HTTP response message.\r | |
974 | //\r | |
975 | \r | |
976 | //\r | |
977 | // 3.1 First step, use zero BodyLength to only receive the response headers.\r | |
978 | //\r | |
ef422fc5 | 979 | ResponseData = AllocateZeroPool (sizeof(HTTP_IO_RESPONSE_DATA));\r |
d933e70a JW |
980 | if (ResponseData == NULL) {\r |
981 | Status = EFI_OUT_OF_RESOURCES;\r | |
982 | goto ERROR_4;\r | |
983 | }\r | |
984 | Status = HttpIoRecvResponse (\r | |
985 | &Private->HttpIo,\r | |
986 | TRUE,\r | |
987 | ResponseData\r | |
988 | );\r | |
072289f4 ZL |
989 | if (EFI_ERROR (Status) || EFI_ERROR (ResponseData->Status)) {\r |
990 | if (EFI_ERROR (ResponseData->Status)) {\r | |
991 | StatusCode = HttpIo->RspToken.Message->Data.Response->StatusCode;\r | |
992 | HttpBootPrintErrorMessage (StatusCode);\r | |
993 | Status = ResponseData->Status;\r | |
994 | }\r | |
d933e70a JW |
995 | goto ERROR_5;\r |
996 | }\r | |
997 | \r | |
587d204c FS |
998 | //\r |
999 | // Check the image type according to server's response.\r | |
1000 | //\r | |
1001 | Status = HttpBootCheckImageType (\r | |
1002 | Private->BootFileUri,\r | |
1003 | Private->BootFileUriParser,\r | |
1004 | ResponseData->HeaderCount,\r | |
1005 | ResponseData->Headers,\r | |
1006 | ImageType\r | |
1007 | );\r | |
1008 | if (EFI_ERROR (Status)) {\r | |
1009 | goto ERROR_5;\r | |
1010 | }\r | |
1011 | \r | |
d933e70a JW |
1012 | //\r |
1013 | // 3.2 Cache the response header.\r | |
1014 | //\r | |
7fd71047 | 1015 | if (Cache != NULL) {\r |
d933e70a | 1016 | Cache->ResponseData = ResponseData;\r |
587d204c | 1017 | Cache->ImageType = *ImageType;\r |
d933e70a JW |
1018 | }\r |
1019 | \r | |
1020 | //\r | |
1021 | // 3.3 Init a message-body parser from the header information.\r | |
1022 | //\r | |
1023 | Parser = NULL;\r | |
1024 | Context.NewBlock = FALSE;\r | |
1025 | Context.Block = NULL;\r | |
1026 | Context.CopyedSize = 0;\r | |
1027 | Context.Buffer = Buffer;\r | |
1028 | Context.BufferSize = *BufferSize;\r | |
1029 | Context.Cache = Cache;\r | |
95b5c32f | 1030 | Context.Private = Private;\r |
d933e70a JW |
1031 | Status = HttpInitMsgParser (\r |
1032 | HeaderOnly? HttpMethodHead : HttpMethodGet,\r | |
1033 | ResponseData->Response.StatusCode,\r | |
1034 | ResponseData->HeaderCount,\r | |
1035 | ResponseData->Headers,\r | |
1036 | HttpBootGetBootFileCallback,\r | |
1037 | (VOID*) &Context,\r | |
1038 | &Parser\r | |
1039 | );\r | |
1040 | if (EFI_ERROR (Status)) {\r | |
1041 | goto ERROR_6;\r | |
1042 | }\r | |
1043 | \r | |
1044 | //\r | |
1045 | // 3.4 Continue to receive and parse message-body if needed.\r | |
1046 | //\r | |
7fd71047 | 1047 | Block = NULL;\r |
d933e70a | 1048 | if (!HeaderOnly) {\r |
7552c24e FS |
1049 | //\r |
1050 | // 3.4.1, check whether we are in identity transfer-coding.\r | |
1051 | //\r | |
1052 | ContentLength = 0;\r | |
1053 | Status = HttpGetEntityLength (Parser, &ContentLength);\r | |
1054 | if (!EFI_ERROR (Status)) {\r | |
1055 | IdentityMode = TRUE;\r | |
1056 | } else {\r | |
1057 | IdentityMode = FALSE;\r | |
1058 | }\r | |
1059 | \r | |
1060 | //\r | |
1061 | // 3.4.2, start the message-body download, the identity and chunked transfer-coding\r | |
1062 | // is handled in different path here.\r | |
1063 | //\r | |
ef422fc5 | 1064 | ZeroMem (&ResponseBody, sizeof (HTTP_IO_RESPONSE_DATA));\r |
7552c24e | 1065 | if (IdentityMode) {\r |
d933e70a | 1066 | //\r |
7552c24e FS |
1067 | // In identity transfer-coding there is no need to parse the message body,\r |
1068 | // just download the message body to the user provided buffer directly.\r | |
d933e70a | 1069 | //\r |
7552c24e FS |
1070 | ReceivedSize = 0;\r |
1071 | while (ReceivedSize < ContentLength) {\r | |
1072 | ResponseBody.Body = (CHAR8*) Buffer + ReceivedSize;\r | |
1073 | ResponseBody.BodyLength = *BufferSize - ReceivedSize;\r | |
1074 | Status = HttpIoRecvResponse (\r | |
1075 | &Private->HttpIo,\r | |
1076 | FALSE,\r | |
1077 | &ResponseBody\r | |
1078 | );\r | |
7570696c JW |
1079 | if (EFI_ERROR (Status) || EFI_ERROR (ResponseBody.Status)) {\r |
1080 | if (EFI_ERROR (ResponseBody.Status)) {\r | |
1081 | Status = ResponseBody.Status;\r | |
1082 | }\r | |
7fd71047 FS |
1083 | goto ERROR_6;\r |
1084 | }\r | |
7552c24e | 1085 | ReceivedSize += ResponseBody.BodyLength;\r |
95b5c32f FS |
1086 | if (Private->HttpBootCallback != NULL) {\r |
1087 | Status = Private->HttpBootCallback->Callback (\r | |
1088 | Private->HttpBootCallback,\r | |
1089 | HttpBootHttpEntityBody,\r | |
1090 | TRUE,\r | |
1091 | (UINT32)ResponseBody.BodyLength,\r | |
1092 | ResponseBody.Body\r | |
1093 | );\r | |
1094 | if (EFI_ERROR (Status)) {\r | |
1095 | goto ERROR_6;\r | |
1096 | }\r | |
1097 | }\r | |
d933e70a | 1098 | }\r |
7552c24e | 1099 | } else {\r |
d933e70a | 1100 | //\r |
7552c24e FS |
1101 | // In "chunked" transfer-coding mode, so we need to parse the received\r |
1102 | // data to get the real entity content.\r | |
d933e70a | 1103 | //\r |
7552c24e FS |
1104 | Block = NULL;\r |
1105 | while (!HttpIsMessageComplete (Parser)) {\r | |
1106 | //\r | |
1107 | // Allocate a buffer in Block to hold the message-body.\r | |
1108 | // If caller provides a buffer, this Block will be reused in every HttpIoRecvResponse().\r | |
1109 | // Otherwise a buffer, the buffer in Block will be cached and we should allocate a new before\r | |
1110 | // every HttpIoRecvResponse().\r | |
1111 | //\r | |
1112 | if (Block == NULL || Context.BufferSize == 0) {\r | |
1113 | Block = AllocatePool (HTTP_BOOT_BLOCK_SIZE);\r | |
1114 | if (Block == NULL) {\r | |
1115 | Status = EFI_OUT_OF_RESOURCES;\r | |
1116 | goto ERROR_6;\r | |
1117 | }\r | |
1118 | Context.NewBlock = TRUE;\r | |
1119 | Context.Block = Block;\r | |
1120 | } else {\r | |
1121 | Context.NewBlock = FALSE;\r | |
1122 | }\r | |
1123 | \r | |
1124 | ResponseBody.Body = (CHAR8*) Block;\r | |
1125 | ResponseBody.BodyLength = HTTP_BOOT_BLOCK_SIZE;\r | |
1126 | Status = HttpIoRecvResponse (\r | |
1127 | &Private->HttpIo,\r | |
1128 | FALSE,\r | |
1129 | &ResponseBody\r | |
1130 | );\r | |
7570696c JW |
1131 | if (EFI_ERROR (Status) || EFI_ERROR (ResponseBody.Status)) {\r |
1132 | if (EFI_ERROR (ResponseBody.Status)) {\r | |
1133 | Status = ResponseBody.Status;\r | |
1134 | }\r | |
7552c24e FS |
1135 | goto ERROR_6;\r |
1136 | }\r | |
1137 | \r | |
1138 | //\r | |
1139 | // Parse the new received block of the message-body, the block will be saved in cache.\r | |
1140 | //\r | |
1141 | Status = HttpParseMessageBody (\r | |
1142 | Parser,\r | |
1143 | ResponseBody.BodyLength,\r | |
1144 | ResponseBody.Body\r | |
1145 | );\r | |
1146 | if (EFI_ERROR (Status)) {\r | |
1147 | goto ERROR_6;\r | |
1148 | }\r | |
d933e70a JW |
1149 | }\r |
1150 | }\r | |
1151 | }\r | |
7552c24e | 1152 | \r |
d933e70a | 1153 | //\r |
7552c24e | 1154 | // 3.5 Message-body receive & parse is completed, we should be able to get the file size now.\r |
d933e70a JW |
1155 | //\r |
1156 | Status = HttpGetEntityLength (Parser, &ContentLength);\r | |
1157 | if (EFI_ERROR (Status)) {\r | |
1158 | goto ERROR_6;\r | |
1159 | }\r | |
1160 | \r | |
1161 | if (*BufferSize < ContentLength) {\r | |
1162 | Status = EFI_BUFFER_TOO_SMALL;\r | |
a93786ae GL |
1163 | } else {\r |
1164 | Status = EFI_SUCCESS;\r | |
d933e70a JW |
1165 | }\r |
1166 | *BufferSize = ContentLength;\r | |
1167 | \r | |
1168 | //\r | |
1169 | // 4. Save the cache item to driver's cache list and return.\r | |
1170 | //\r | |
7fd71047 | 1171 | if (Cache != NULL) {\r |
d933e70a JW |
1172 | Cache->EntityLength = ContentLength;\r |
1173 | InsertTailList (&Private->CacheList, &Cache->Link);\r | |
1174 | }\r | |
1175 | \r | |
1176 | if (Parser != NULL) {\r | |
1177 | HttpFreeMsgParser (Parser);\r | |
1178 | }\r | |
1179 | \r | |
a93786ae | 1180 | return Status;\r |
d933e70a JW |
1181 | \r |
1182 | ERROR_6:\r | |
1183 | if (Parser != NULL) {\r | |
1184 | HttpFreeMsgParser (Parser);\r | |
1185 | }\r | |
1186 | if (Context.Block != NULL) {\r | |
1187 | FreePool (Context.Block);\r | |
1188 | }\r | |
1189 | HttpBootFreeCache (Cache);\r | |
1190 | \r | |
1191 | ERROR_5:\r | |
1192 | if (ResponseData != NULL) {\r | |
1193 | FreePool (ResponseData);\r | |
1194 | }\r | |
1195 | ERROR_4:\r | |
1196 | if (RequestData != NULL) {\r | |
1197 | FreePool (RequestData);\r | |
1198 | }\r | |
1199 | ERROR_3:\r | |
1200 | HttpBootFreeHeader (HttpIoHeader);\r | |
1201 | ERROR_2:\r | |
1202 | if (Cache != NULL) {\r | |
1203 | FreePool (Cache);\r | |
1204 | }\r | |
1205 | ERROR_1:\r | |
1206 | if (Url != NULL) {\r | |
1207 | FreePool (Url);\r | |
1208 | }\r | |
1209 | \r | |
1210 | return Status;\r | |
1211 | }\r | |
fa848a40 | 1212 | \r |