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