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.

ibft.c 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright Fen Systems Ltd. 2007. Portions of this code are derived
  3. * from IBM Corporation Sample Programs. Copyright IBM Corporation
  4. * 2004, 2007. All rights reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person
  7. * obtaining a copy of this software and associated documentation
  8. * files (the "Software"), to deal in the Software without
  9. * restriction, including without limitation the rights to use, copy,
  10. * modify, merge, publish, distribute, sublicense, and/or sell copies
  11. * of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  21. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  22. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. *
  26. */
  27. #include <stdint.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <errno.h>
  31. #include <byteswap.h>
  32. #include <realmode.h>
  33. #include <gpxe/pci.h>
  34. #include <gpxe/acpi.h>
  35. #include <gpxe/in.h>
  36. #include <gpxe/netdevice.h>
  37. #include <gpxe/dhcp.h>
  38. #include <gpxe/iscsi.h>
  39. #include <gpxe/ibft.h>
  40. /** @file
  41. *
  42. * iSCSI boot firmware table
  43. *
  44. * The information in this file is derived from the document "iSCSI
  45. * Boot Firmware Table (iBFT)" as published by IBM at
  46. *
  47. * ftp://ftp.software.ibm.com/systems/support/system_x_pdf/ibm_iscsi_boot_firmware_table_v1.02.pdf
  48. *
  49. */
  50. #define ibftab __use_data16 ( ibftab )
  51. /** The iBFT used by gPXE */
  52. struct gpxe_ibft __data16 ( ibftab ) = {
  53. /* Table header */
  54. .table = {
  55. /* ACPI header */
  56. .acpi = {
  57. .signature = IBFT_SIG,
  58. .length = sizeof ( ibftab ),
  59. .revision = 1,
  60. .oem_id = "FENSYS",
  61. .oem_table_id = "gPXE",
  62. },
  63. /* Control block */
  64. .control = {
  65. .header = {
  66. .structure_id = IBFT_STRUCTURE_ID_CONTROL,
  67. .version = 1,
  68. .length = sizeof ( ibftab.table.control ),
  69. .flags = 0,
  70. },
  71. .initiator = offsetof ( typeof ( ibftab ), initiator ),
  72. .nic_0 = offsetof ( typeof ( ibftab ), nic ),
  73. .target_0 = offsetof ( typeof ( ibftab ), target ),
  74. },
  75. },
  76. /* iSCSI initiator information */
  77. .initiator = {
  78. .header = {
  79. .structure_id = IBFT_STRUCTURE_ID_INITIATOR,
  80. .version = 1,
  81. .length = sizeof ( ibftab.initiator ),
  82. .flags = ( IBFT_FL_INITIATOR_BLOCK_VALID |
  83. IBFT_FL_INITIATOR_FIRMWARE_BOOT_SELECTED ),
  84. },
  85. },
  86. /* NIC information */
  87. .nic = {
  88. .header = {
  89. .structure_id = IBFT_STRUCTURE_ID_NIC,
  90. .version = 1,
  91. .length = sizeof ( ibftab.nic ),
  92. .flags = ( IBFT_FL_NIC_BLOCK_VALID |
  93. IBFT_FL_NIC_FIRMWARE_BOOT_SELECTED ),
  94. },
  95. },
  96. /* iSCSI target information */
  97. .target = {
  98. .header = {
  99. .structure_id = IBFT_STRUCTURE_ID_TARGET,
  100. .version = 1,
  101. .length = sizeof ( ibftab.target ),
  102. .flags = ( IBFT_FL_TARGET_BLOCK_VALID |
  103. IBFT_FL_TARGET_FIRMWARE_BOOT_SELECTED ),
  104. },
  105. },
  106. };
  107. /**
  108. * Fill in an IP address field within iBFT
  109. *
  110. * @v ipaddr IP address field
  111. * @v in IPv4 address
  112. */
  113. static void ibft_set_ipaddr ( struct ibft_ipaddr *ipaddr, struct in_addr in ) {
  114. memset ( ipaddr, 0, sizeof ( ipaddr ) );
  115. if ( in.s_addr ) {
  116. ipaddr->in = in;
  117. ipaddr->ones = 0xffff;
  118. }
  119. }
  120. /**
  121. * Fill in an IP address within iBFT from DHCP option
  122. *
  123. * @v ipaddr IP address field
  124. * @v tag DHCP option tag
  125. */
  126. static void ibft_set_ipaddr_option ( struct ibft_ipaddr *ipaddr,
  127. unsigned int tag ) {
  128. struct in_addr in;
  129. find_global_dhcp_ipv4_option ( tag, &in );
  130. ibft_set_ipaddr ( ipaddr, in );
  131. }
  132. /**
  133. * Fill in a string field within iBFT
  134. *
  135. * @v strings iBFT string block descriptor
  136. * @v string String field
  137. * @v data String to fill in
  138. * @v len Length of string to fill in
  139. * @ret rc Return status code
  140. */
  141. static int ibft_set_string ( struct ibft_string_block *strings,
  142. struct ibft_string *string,
  143. const void *data, size_t len ) {
  144. char *dest;
  145. char *end;
  146. unsigned int remaining;
  147. dest = ( ( ( char * ) strings->table ) + strings->offset );
  148. end = ( ( ( char * ) strings->table ) + strings->table->acpi.length );
  149. remaining = ( end - dest );
  150. if ( len >= remaining )
  151. return -ENOMEM;
  152. memcpy ( dest, data, len );
  153. dest[len] = '\0';
  154. string->offset = strings->offset;
  155. string->length = len;
  156. strings->offset += ( len + 1 );
  157. return 0;
  158. }
  159. /**
  160. * Fill in a string field within iBFT from DHCP option
  161. *
  162. * @v strings iBFT string block descriptor
  163. * @v string String field
  164. * @v tag DHCP option tag
  165. * @ret rc Return status code
  166. */
  167. static int ibft_set_string_option ( struct ibft_string_block *strings,
  168. struct ibft_string *string,
  169. unsigned int tag ) {
  170. struct dhcp_option *option;
  171. option = find_global_dhcp_option ( tag );
  172. if ( ! option ) {
  173. string->offset = 0;
  174. string->length = 0;
  175. return 0;
  176. }
  177. return ibft_set_string ( strings, string, option->data.string,
  178. option->len );
  179. }
  180. /**
  181. * Fill in NIC portion of iBFT
  182. *
  183. * @v nic NIC portion of iBFT
  184. * @v strings iBFT string block descriptor
  185. * @v netdev Network device
  186. * @ret rc Return status code
  187. */
  188. static int ibft_fill_nic ( struct ibft_nic *nic,
  189. struct ibft_string_block *strings,
  190. struct net_device *netdev ) {
  191. struct in_addr netmask_addr;
  192. unsigned int netmask_count = 0;
  193. int rc;
  194. /* Extract values from DHCP configuration */
  195. ibft_set_ipaddr_option ( &nic->ip_address, DHCP_EB_YIADDR );
  196. ibft_set_ipaddr_option ( &nic->gateway, DHCP_ROUTERS );
  197. ibft_set_ipaddr_option ( &nic->dns[0], DHCP_DNS_SERVERS );
  198. if ( ( rc = ibft_set_string_option ( strings, &nic->hostname,
  199. DHCP_HOST_NAME ) ) != 0 )
  200. return rc;
  201. /* Derive subnet mask prefix from subnet mask */
  202. find_global_dhcp_ipv4_option ( DHCP_SUBNET_MASK, &netmask_addr );
  203. while ( netmask_addr.s_addr ) {
  204. if ( netmask_addr.s_addr & 0x1 )
  205. netmask_count++;
  206. netmask_addr.s_addr >>= 1;
  207. }
  208. nic->subnet_mask_prefix = netmask_count;
  209. /* Extract values from net-device configuration */
  210. memcpy ( nic->mac_address, netdev->ll_addr,
  211. sizeof ( nic->mac_address ) );
  212. nic->pci_bus_dev_func = netdev->dev->desc.location;
  213. return 0;
  214. }
  215. /**
  216. * Fill in Initiator portion of iBFT
  217. *
  218. * @v initiator Initiator portion of iBFT
  219. * @v strings iBFT string block descriptor
  220. * @ret rc Return status code
  221. */
  222. static int ibft_fill_initiator ( struct ibft_initiator *initiator,
  223. struct ibft_string_block *strings ) {
  224. const char *initiator_iqn = iscsi_initiator_iqn();
  225. int rc;
  226. if ( ( rc = ibft_set_string ( strings, &initiator->initiator_name,
  227. initiator_iqn,
  228. strlen ( initiator_iqn ) ) ) != 0)
  229. return rc;
  230. return 0;
  231. }
  232. /**
  233. * Fill in Target portion of iBFT
  234. *
  235. * @v target Target portion of iBFT
  236. * @v strings iBFT string block descriptor
  237. * @v iscsi iSCSI session
  238. * @ret rc Return status code
  239. */
  240. static int ibft_fill_target ( struct ibft_target *target,
  241. struct ibft_string_block *strings,
  242. struct iscsi_session *iscsi ) {
  243. struct sockaddr_in *sin_target =
  244. ( struct sockaddr_in * ) &iscsi->target_sockaddr;
  245. int rc;
  246. /* Fill in Target values */
  247. ibft_set_ipaddr ( &target->ip_address, sin_target->sin_addr );
  248. target->socket = ntohs ( sin_target->sin_port );
  249. if ( ( rc = ibft_set_string ( strings, &target->target_name,
  250. iscsi->target_iqn,
  251. strlen ( iscsi->target_iqn ) ) ) != 0 )
  252. return rc;
  253. if ( iscsi->username ) {
  254. if ( ( rc = ibft_set_string ( strings, &target->chap_name,
  255. iscsi->username,
  256. strlen ( iscsi->username ) ))!=0)
  257. return rc;
  258. }
  259. if ( iscsi->password ) {
  260. if ( ( rc = ibft_set_string ( strings, &target->chap_secret,
  261. iscsi->password,
  262. strlen ( iscsi->password ) ))!=0)
  263. return rc;
  264. target->chap_type = IBFT_CHAP_ONE_WAY;
  265. }
  266. return 0;
  267. }
  268. /**
  269. * Fill in all variable portions of iBFT
  270. *
  271. * @v netdev Network device
  272. * @v initiator_iqn Initiator IQN
  273. * @v st_target Target socket address
  274. * @v target_iqn Target IQN
  275. * @ret rc Return status code
  276. *
  277. */
  278. int ibft_fill_data ( struct net_device *netdev,
  279. struct iscsi_session *iscsi ) {
  280. struct ibft_string_block strings = {
  281. .table = &ibftab.table,
  282. .offset = offsetof ( typeof ( ibftab ), strings ),
  283. };
  284. int rc;
  285. /* Fill in NIC, Initiator and Target portions */
  286. if ( ( rc = ibft_fill_nic ( &ibftab.nic, &strings, netdev ) ) != 0 )
  287. return rc;
  288. if ( ( rc = ibft_fill_initiator ( &ibftab.initiator,
  289. &strings ) ) != 0 )
  290. return rc;
  291. if ( ( rc = ibft_fill_target ( &ibftab.target, &strings,
  292. iscsi ) ) != 0 )
  293. return rc;
  294. /* Update checksum */
  295. acpi_fix_checksum ( &ibftab.table.acpi );
  296. return 0;
  297. }