iPXE
httpblock.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /**
00027  * @file
00028  *
00029  * Hyper Text Transfer Protocol (HTTP) block device
00030  *
00031  */
00032 
00033 #include <stdint.h>
00034 #include <ipxe/uaccess.h>
00035 #include <ipxe/blocktrans.h>
00036 #include <ipxe/blockdev.h>
00037 #include <ipxe/acpi.h>
00038 #include <ipxe/http.h>
00039 
00040 /** Block size used for HTTP block device requests */
00041 #define HTTP_BLKSIZE 512
00042 
00043 /**
00044  * Read from block device
00045  *
00046  * @v http              HTTP transaction
00047  * @v data              Data interface
00048  * @v lba               Starting logical block address
00049  * @v count             Number of logical blocks
00050  * @v buffer            Data buffer
00051  * @v len               Length of data buffer
00052  * @ret rc              Return status code
00053  */
00054 int http_block_read ( struct http_transaction *http, struct interface *data,
00055                       uint64_t lba, unsigned int count, userptr_t buffer,
00056                       size_t len ) {
00057         struct http_request_range range;
00058         int rc;
00059 
00060         /* Sanity check */
00061         assert ( len == ( count * HTTP_BLKSIZE ) );
00062 
00063         /* Construct request range descriptor */
00064         range.start = ( lba * HTTP_BLKSIZE );
00065         range.len = len;
00066 
00067         /* Start a range request to retrieve the block(s) */
00068         if ( ( rc = http_open ( data, &http_get, http->uri, &range,
00069                                 NULL ) ) != 0 )
00070                 goto err_open;
00071 
00072         /* Insert block device translator */
00073         if ( ( rc = block_translate ( data, buffer, len ) ) != 0 ) {
00074                 DBGC ( http, "HTTP %p could not insert block translator: %s\n",
00075                        http, strerror ( rc ) );
00076                 goto err_translate;
00077         }
00078 
00079         return 0;
00080 
00081  err_translate:
00082         intf_restart ( data, rc );
00083  err_open:
00084         return rc;
00085 }
00086 
00087 /**
00088  * Read block device capacity
00089  *
00090  * @v control           Control interface
00091  * @v data              Data interface
00092  * @ret rc              Return status code
00093  */
00094 int http_block_read_capacity ( struct http_transaction *http,
00095                                struct interface *data ) {
00096         int rc;
00097 
00098         /* Start a HEAD request to retrieve the capacity */
00099         if ( ( rc = http_open ( data, &http_head, http->uri, NULL,
00100                                 NULL ) ) != 0 )
00101                 goto err_open;
00102 
00103         /* Insert block device translator */
00104         if ( ( rc = block_translate ( data, UNULL, HTTP_BLKSIZE ) ) != 0 ) {
00105                 DBGC ( http, "HTTP %p could not insert block translator: %s\n",
00106                        http, strerror ( rc ) );
00107                 goto err_translate;
00108         }
00109 
00110         return 0;
00111 
00112  err_translate:
00113         intf_restart ( data, rc );
00114  err_open:
00115         return rc;
00116 }