[http] Cleanly shut down potentially looped interfaces
[ipxe.git] / src / net / tcp / httpblock.c
1 /*
2 * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 /**
27 * @file
28 *
29 * Hyper Text Transfer Protocol (HTTP) block device
30 *
31 */
32
33 #include <stdint.h>
34 #include <ipxe/uaccess.h>
35 #include <ipxe/blocktrans.h>
36 #include <ipxe/blockdev.h>
37 #include <ipxe/acpi.h>
38 #include <ipxe/http.h>
39
40 /** Block size used for HTTP block device requests */
41 #define HTTP_BLKSIZE 512
42
43 /**
44 * Read from block device
45 *
46 * @v http HTTP transaction
47 * @v data Data interface
48 * @v lba Starting logical block address
49 * @v count Number of logical blocks
50 * @v buffer Data buffer
51 * @v len Length of data buffer
52 * @ret rc Return status code
53 */
54 int http_block_read ( struct http_transaction *http, struct interface *data,
55 uint64_t lba, unsigned int count, userptr_t buffer,
56 size_t len ) {
57 struct http_request_range range;
58 int rc;
59
60 /* Sanity check */
61 assert ( len == ( count * HTTP_BLKSIZE ) );
62
63 /* Construct request range descriptor */
64 range.start = ( lba * HTTP_BLKSIZE );
65 range.len = len;
66
67 /* Start a range request to retrieve the block(s) */
68 if ( ( rc = http_open ( data, &http_get, http->uri, &range,
69 NULL ) ) != 0 )
70 goto err_open;
71
72 /* Insert block device translator */
73 if ( ( rc = block_translate ( data, buffer, len ) ) != 0 ) {
74 DBGC ( http, "HTTP %p could not insert block translator: %s\n",
75 http, strerror ( rc ) );
76 goto err_translate;
77 }
78
79 return 0;
80
81 err_translate:
82 intf_restart ( data, rc );
83 err_open:
84 return rc;
85 }
86
87 /**
88 * Read block device capacity
89 *
90 * @v control Control interface
91 * @v data Data interface
92 * @ret rc Return status code
93 */
94 int http_block_read_capacity ( struct http_transaction *http,
95 struct interface *data ) {
96 int rc;
97
98 /* Start a HEAD request to retrieve the capacity */
99 if ( ( rc = http_open ( data, &http_head, http->uri, NULL,
100 NULL ) ) != 0 )
101 goto err_open;
102
103 /* Insert block device translator */
104 if ( ( rc = block_translate ( data, UNULL, HTTP_BLKSIZE ) ) != 0 ) {
105 DBGC ( http, "HTTP %p could not insert block translator: %s\n",
106 http, strerror ( rc ) );
107 goto err_translate;
108 }
109
110 return 0;
111
112 err_translate:
113 intf_restart ( data, rc );
114 err_open:
115 return rc;
116 }
117
118 /**
119 * Describe device in ACPI table
120 *
121 * @v http HTTP transaction
122 * @v acpi ACPI table
123 * @v len Length of ACPI table
124 * @ret rc Return status code
125 */
126 int http_acpi_describe ( struct http_transaction *http,
127 struct acpi_description_header *acpi, size_t len ) {
128
129 DBGC ( http, "HTTP %p cannot yet describe device in an ACPI table\n",
130 http );
131 ( void ) acpi;
132 ( void ) len;
133 return 0;
134 }