iPXE
blockdev.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2010 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 #include <errno.h>
00027 #include <ipxe/interface.h>
00028 #include <ipxe/blockdev.h>
00029 
00030 /** @file
00031  *
00032  * Block devices
00033  *
00034  */
00035 
00036 /**
00037  * Read from block device
00038  *
00039  * @v control           Control interface
00040  * @v data              Data interface
00041  * @v lba               Starting logical block address
00042  * @v count             Number of logical blocks
00043  * @v buffer            Data buffer
00044  * @v len               Length of data buffer
00045  * @ret rc              Return status code
00046  */
00047 int block_read ( struct interface *control, struct interface *data,
00048                  uint64_t lba, unsigned int count,
00049                  userptr_t buffer, size_t len ) {
00050         struct interface *dest;
00051         block_read_TYPE ( void * ) *op =
00052                 intf_get_dest_op ( control, block_read, &dest );
00053         void *object = intf_object ( dest );
00054         int rc;
00055 
00056         if ( op ) {
00057                 rc = op ( object, data, lba, count, buffer, len );
00058         } else {
00059                 /* Default is to fail to issue the command */
00060                 rc = -EOPNOTSUPP;
00061         }
00062 
00063         intf_put ( dest );
00064         return rc;
00065 }
00066 
00067 /**
00068  * Write to block device
00069  *
00070  * @v control           Control interface
00071  * @v data              Data interface
00072  * @v lba               Starting logical block address
00073  * @v count             Number of logical blocks
00074  * @v buffer            Data buffer
00075  * @v len               Length of data buffer
00076  * @ret rc              Return status code
00077  */
00078 int block_write ( struct interface *control, struct interface *data,
00079                   uint64_t lba, unsigned int count,
00080                   userptr_t buffer, size_t len ) {
00081         struct interface *dest;
00082         block_write_TYPE ( void * ) *op =
00083                 intf_get_dest_op ( control, block_write, &dest );
00084         void *object = intf_object ( dest );
00085         int rc;
00086 
00087         if ( op ) {
00088                 rc = op ( object, data, lba, count, buffer, len );
00089         } else {
00090                 /* Default is to fail to issue the command */
00091                 rc = -EOPNOTSUPP;
00092         }
00093 
00094         intf_put ( dest );
00095         return rc;
00096 }
00097 
00098 /**
00099  * Read block device capacity
00100  *
00101  * @v control           Control interface
00102  * @v data              Data interface
00103  * @ret rc              Return status code
00104  */
00105 int block_read_capacity ( struct interface *control, struct interface *data ) {
00106         struct interface *dest;
00107         block_read_capacity_TYPE ( void * ) *op =
00108                 intf_get_dest_op ( control, block_read_capacity, &dest );
00109         void *object = intf_object ( dest );
00110         int rc;
00111 
00112         if ( op ) {
00113                 rc = op ( object, data );
00114         } else {
00115                 /* Default is to fail to issue the command */
00116                 rc = -EOPNOTSUPP;
00117         }
00118 
00119         intf_put ( dest );
00120         return rc;
00121 }
00122 
00123 /**
00124  * Report block device capacity
00125  *
00126  * @v intf              Interface
00127  * @v capacity          Block device capacity
00128  */
00129 void block_capacity ( struct interface *intf,
00130                       struct block_device_capacity *capacity ) {
00131         struct interface *dest;
00132         block_capacity_TYPE ( void * ) *op =
00133                 intf_get_dest_op ( intf, block_capacity, &dest );
00134         void *object = intf_object ( dest );
00135 
00136         if ( op ) {
00137                 op ( object, capacity );
00138         } else {
00139                 /* Default is to do nothing */
00140         }
00141 
00142         intf_put ( dest );
00143 }