You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

blockdev.c 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (C) 2010 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. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <errno.h>
  25. #include <ipxe/interface.h>
  26. #include <ipxe/blockdev.h>
  27. /** @file
  28. *
  29. * Block devices
  30. *
  31. */
  32. /**
  33. * Read from block device
  34. *
  35. * @v control Control interface
  36. * @v data Data interface
  37. * @v lba Starting logical block address
  38. * @v count Number of logical blocks
  39. * @v buffer Data buffer
  40. * @v len Length of data buffer
  41. * @ret rc Return status code
  42. */
  43. int block_read ( struct interface *control, struct interface *data,
  44. uint64_t lba, unsigned int count,
  45. userptr_t buffer, size_t len ) {
  46. struct interface *dest;
  47. block_read_TYPE ( void * ) *op =
  48. intf_get_dest_op ( control, block_read, &dest );
  49. void *object = intf_object ( dest );
  50. int rc;
  51. if ( op ) {
  52. rc = op ( object, data, lba, count, buffer, len );
  53. } else {
  54. /* Default is to fail to issue the command */
  55. rc = -EOPNOTSUPP;
  56. }
  57. intf_put ( dest );
  58. return rc;
  59. }
  60. /**
  61. * Write to block device
  62. *
  63. * @v control Control interface
  64. * @v data Data interface
  65. * @v lba Starting logical block address
  66. * @v count Number of logical blocks
  67. * @v buffer Data buffer
  68. * @v len Length of data buffer
  69. * @ret rc Return status code
  70. */
  71. int block_write ( struct interface *control, struct interface *data,
  72. uint64_t lba, unsigned int count,
  73. userptr_t buffer, size_t len ) {
  74. struct interface *dest;
  75. block_write_TYPE ( void * ) *op =
  76. intf_get_dest_op ( control, block_write, &dest );
  77. void *object = intf_object ( dest );
  78. int rc;
  79. if ( op ) {
  80. rc = op ( object, data, lba, count, buffer, len );
  81. } else {
  82. /* Default is to fail to issue the command */
  83. rc = -EOPNOTSUPP;
  84. }
  85. intf_put ( dest );
  86. return rc;
  87. }
  88. /**
  89. * Read block device capacity
  90. *
  91. * @v control Control interface
  92. * @v data Data interface
  93. * @ret rc Return status code
  94. */
  95. int block_read_capacity ( struct interface *control, struct interface *data ) {
  96. struct interface *dest;
  97. block_read_capacity_TYPE ( void * ) *op =
  98. intf_get_dest_op ( control, block_read_capacity, &dest );
  99. void *object = intf_object ( dest );
  100. int rc;
  101. if ( op ) {
  102. rc = op ( object, data );
  103. } else {
  104. /* Default is to fail to issue the command */
  105. rc = -EOPNOTSUPP;
  106. }
  107. intf_put ( dest );
  108. return rc;
  109. }
  110. /**
  111. * Report block device capacity
  112. *
  113. * @v intf Interface
  114. * @v capacity Block device capacity
  115. */
  116. void block_capacity ( struct interface *intf,
  117. struct block_device_capacity *capacity ) {
  118. struct interface *dest;
  119. block_capacity_TYPE ( void * ) *op =
  120. intf_get_dest_op ( intf, block_capacity, &dest );
  121. void *object = intf_object ( dest );
  122. if ( op ) {
  123. op ( object, capacity );
  124. } else {
  125. /* Default is to do nothing */
  126. }
  127. intf_put ( dest );
  128. }