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 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. FILE_LICENCE ( BSD2 );
  28. #include <stdint.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <errno.h>
  32. #include <byteswap.h>
  33. #include <ipxe/pci.h>
  34. #include <ipxe/acpi.h>
  35. #include <ipxe/in.h>
  36. #include <ipxe/netdevice.h>
  37. #include <ipxe/ethernet.h>
  38. #include <ipxe/vlan.h>
  39. #include <ipxe/dhcp.h>
  40. #include <ipxe/iscsi.h>
  41. #include <ipxe/ibft.h>
  42. /** @file
  43. *
  44. * iSCSI boot firmware table
  45. *
  46. * The information in this file is derived from the document "iSCSI
  47. * Boot Firmware Table (iBFT)" as published by IBM at
  48. *
  49. * ftp://ftp.software.ibm.com/systems/support/system_x_pdf/ibm_iscsi_boot_firmware_table_v1.02.pdf
  50. *
  51. */
  52. /**
  53. * An iBFT created by iPXE
  54. *
  55. */
  56. struct ipxe_ibft {
  57. /** The fixed section */
  58. struct ibft_table table;
  59. /** The Initiator section */
  60. struct ibft_initiator initiator __attribute__ (( aligned ( 16 ) ));
  61. /** The NIC section */
  62. struct ibft_nic nic __attribute__ (( aligned ( 16 ) ));
  63. /** The Target section */
  64. struct ibft_target target __attribute__ (( aligned ( 16 ) ));
  65. /** Strings block */
  66. char strings[0];
  67. } __attribute__ (( packed, aligned ( 16 ) ));
  68. /**
  69. * iSCSI string block descriptor
  70. *
  71. * This is an internal structure that we use to keep track of the
  72. * allocation of string data.
  73. */
  74. struct ibft_strings {
  75. /** The iBFT containing these strings */
  76. struct ibft_table *table;
  77. /** Offset of first free byte within iBFT */
  78. size_t offset;
  79. /** Total length of the iBFT */
  80. size_t len;
  81. };
  82. /**
  83. * Fill in an IP address field within iBFT
  84. *
  85. * @v ipaddr IP address field
  86. * @v in IPv4 address
  87. */
  88. static void ibft_set_ipaddr ( struct ibft_ipaddr *ipaddr, struct in_addr in ) {
  89. memset ( ipaddr, 0, sizeof ( *ipaddr ) );
  90. if ( in.s_addr ) {
  91. ipaddr->in = in;
  92. ipaddr->ones = 0xffff;
  93. }
  94. }
  95. /**
  96. * Fill in an IP address within iBFT from configuration setting
  97. *
  98. * @v settings Parent settings block, or NULL
  99. * @v ipaddr IP address field
  100. * @v setting Configuration setting
  101. * @v count Maximum number of IP addresses
  102. */
  103. static void ibft_set_ipaddr_setting ( struct settings *settings,
  104. struct ibft_ipaddr *ipaddr,
  105. const struct setting *setting,
  106. unsigned int count ) {
  107. struct in_addr in[count];
  108. unsigned int i;
  109. fetch_ipv4_array_setting ( settings, setting, in, count );
  110. for ( i = 0 ; i < count ; i++ ) {
  111. ibft_set_ipaddr ( &ipaddr[i], in[i] );
  112. }
  113. }
  114. /**
  115. * Read IP address from iBFT (for debugging)
  116. *
  117. * @v strings iBFT string block descriptor
  118. * @v string String field
  119. * @ret ipaddr IP address string
  120. */
  121. static const char * ibft_ipaddr ( struct ibft_ipaddr *ipaddr ) {
  122. return inet_ntoa ( ipaddr->in );
  123. }
  124. /**
  125. * Allocate a string within iBFT
  126. *
  127. * @v strings iBFT string block descriptor
  128. * @v string String field to fill in
  129. * @v len Length of string to allocate (excluding NUL)
  130. * @ret dest String destination, or NULL
  131. */
  132. static char * ibft_alloc_string ( struct ibft_strings *strings,
  133. struct ibft_string *string, size_t len ) {
  134. if ( ( strings->offset + len ) >= strings->len )
  135. return NULL;
  136. string->offset = cpu_to_le16 ( strings->offset );
  137. string->len = cpu_to_le16 ( len );
  138. strings->offset += ( len + 1 );
  139. return ( ( ( char * ) strings->table ) + string->offset );
  140. }
  141. /**
  142. * Fill in a string field within iBFT
  143. *
  144. * @v strings iBFT string block descriptor
  145. * @v string String field
  146. * @v data String to fill in, or NULL
  147. * @ret rc Return status code
  148. */
  149. static int ibft_set_string ( struct ibft_strings *strings,
  150. struct ibft_string *string, const char *data ) {
  151. char *dest;
  152. if ( ! data )
  153. return 0;
  154. dest = ibft_alloc_string ( strings, string, strlen ( data ) );
  155. if ( ! dest )
  156. return -ENOBUFS;
  157. strcpy ( dest, data );
  158. return 0;
  159. }
  160. /**
  161. * Fill in a string field within iBFT from configuration setting
  162. *
  163. * @v settings Parent settings block, or NULL
  164. * @v strings iBFT string block descriptor
  165. * @v string String field
  166. * @v setting Configuration setting
  167. * @ret rc Return status code
  168. */
  169. static int ibft_set_string_setting ( struct settings *settings,
  170. struct ibft_strings *strings,
  171. struct ibft_string *string,
  172. const struct setting *setting ) {
  173. struct settings *origin;
  174. struct setting fetched;
  175. int len;
  176. char *dest;
  177. len = fetch_setting ( settings, setting, &origin, &fetched, NULL, 0 );
  178. if ( len < 0 ) {
  179. string->offset = 0;
  180. string->len = 0;
  181. return 0;
  182. }
  183. dest = ibft_alloc_string ( strings, string, len );
  184. if ( ! dest )
  185. return -ENOBUFS;
  186. fetch_string_setting ( origin, &fetched, dest, ( len + 1 ));
  187. return 0;
  188. }
  189. /**
  190. * Read string from iBFT (for debugging)
  191. *
  192. * @v strings iBFT string block descriptor
  193. * @v string String field
  194. * @ret data String content (or "<empty>")
  195. */
  196. static const char * ibft_string ( struct ibft_strings *strings,
  197. struct ibft_string *string ) {
  198. return ( string->offset ?
  199. ( ( ( char * ) strings->table ) + string->offset ) : NULL );
  200. }
  201. /**
  202. * Fill in NIC portion of iBFT
  203. *
  204. * @v nic NIC portion of iBFT
  205. * @v strings iBFT string block descriptor
  206. * @v netdev Network device
  207. * @ret rc Return status code
  208. */
  209. static int ibft_fill_nic ( struct ibft_nic *nic,
  210. struct ibft_strings *strings,
  211. struct net_device *netdev ) {
  212. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  213. struct in_addr netmask_addr = { 0 };
  214. unsigned int netmask_count = 0;
  215. struct settings *parent = netdev_settings ( netdev );
  216. struct settings *origin;
  217. int rc;
  218. /* Fill in common header */
  219. nic->header.structure_id = IBFT_STRUCTURE_ID_NIC;
  220. nic->header.version = 1;
  221. nic->header.length = cpu_to_le16 ( sizeof ( *nic ) );
  222. nic->header.flags = ( IBFT_FL_NIC_BLOCK_VALID |
  223. IBFT_FL_NIC_FIRMWARE_BOOT_SELECTED );
  224. /* Determine origin of IP address */
  225. fetch_setting ( parent, &ip_setting, &origin, NULL, NULL, 0 );
  226. nic->origin = ( ( origin == parent ) ?
  227. IBFT_NIC_ORIGIN_MANUAL : IBFT_NIC_ORIGIN_DHCP );
  228. DBG ( "iBFT NIC origin = %d\n", nic->origin );
  229. /* Extract values from configuration settings */
  230. ibft_set_ipaddr_setting ( parent, &nic->ip_address, &ip_setting, 1 );
  231. DBG ( "iBFT NIC IP = %s\n", ibft_ipaddr ( &nic->ip_address ) );
  232. ibft_set_ipaddr_setting ( parent, &nic->gateway, &gateway_setting, 1 );
  233. DBG ( "iBFT NIC gateway = %s\n", ibft_ipaddr ( &nic->gateway ) );
  234. ibft_set_ipaddr_setting ( NULL, &nic->dns[0], &dns_setting,
  235. ( sizeof ( nic->dns ) /
  236. sizeof ( nic->dns[0] ) ) );
  237. DBG ( "iBFT NIC DNS = %s", ibft_ipaddr ( &nic->dns[0] ) );
  238. DBG ( ", %s\n", ibft_ipaddr ( &nic->dns[1] ) );
  239. if ( ( rc = ibft_set_string_setting ( NULL, strings, &nic->hostname,
  240. &hostname_setting ) ) != 0 )
  241. return rc;
  242. DBG ( "iBFT NIC hostname = %s\n",
  243. ibft_string ( strings, &nic->hostname ) );
  244. /* Derive subnet mask prefix from subnet mask */
  245. fetch_ipv4_setting ( parent, &netmask_setting, &netmask_addr );
  246. while ( netmask_addr.s_addr ) {
  247. if ( netmask_addr.s_addr & 0x1 )
  248. netmask_count++;
  249. netmask_addr.s_addr >>= 1;
  250. }
  251. nic->subnet_mask_prefix = netmask_count;
  252. DBG ( "iBFT NIC subnet = /%d\n", nic->subnet_mask_prefix );
  253. /* Extract values from net-device configuration */
  254. nic->vlan = cpu_to_le16 ( vlan_tag ( netdev ) );
  255. DBG ( "iBFT NIC VLAN = %02x\n", le16_to_cpu ( nic->vlan ) );
  256. if ( ( rc = ll_protocol->eth_addr ( netdev->ll_addr,
  257. nic->mac_address ) ) != 0 ) {
  258. DBG ( "Could not determine iBFT MAC: %s\n", strerror ( rc ) );
  259. return rc;
  260. }
  261. DBG ( "iBFT NIC MAC = %s\n", eth_ntoa ( nic->mac_address ) );
  262. nic->pci_bus_dev_func = cpu_to_le16 ( netdev->dev->desc.location );
  263. DBG ( "iBFT NIC PCI = %04x\n", le16_to_cpu ( nic->pci_bus_dev_func ) );
  264. return 0;
  265. }
  266. /**
  267. * Fill in Initiator portion of iBFT
  268. *
  269. * @v initiator Initiator portion of iBFT
  270. * @v strings iBFT string block descriptor
  271. * @v iscsi iSCSI session
  272. * @ret rc Return status code
  273. */
  274. static int ibft_fill_initiator ( struct ibft_initiator *initiator,
  275. struct ibft_strings *strings,
  276. struct iscsi_session *iscsi ) {
  277. int rc;
  278. /* Fill in common header */
  279. initiator->header.structure_id = IBFT_STRUCTURE_ID_INITIATOR;
  280. initiator->header.version = 1;
  281. initiator->header.length = cpu_to_le16 ( sizeof ( *initiator ) );
  282. initiator->header.flags = ( IBFT_FL_INITIATOR_BLOCK_VALID |
  283. IBFT_FL_INITIATOR_FIRMWARE_BOOT_SELECTED );
  284. /* Fill in hostname */
  285. if ( ( rc = ibft_set_string ( strings, &initiator->initiator_name,
  286. iscsi->initiator_iqn ) ) != 0 )
  287. return rc;
  288. DBG ( "iBFT initiator hostname = %s\n",
  289. ibft_string ( strings, &initiator->initiator_name ) );
  290. return 0;
  291. }
  292. /**
  293. * Fill in Target CHAP portion of iBFT
  294. *
  295. * @v target Target portion of iBFT
  296. * @v strings iBFT string block descriptor
  297. * @v iscsi iSCSI session
  298. * @ret rc Return status code
  299. */
  300. static int ibft_fill_target_chap ( struct ibft_target *target,
  301. struct ibft_strings *strings,
  302. struct iscsi_session *iscsi ) {
  303. int rc;
  304. if ( ! ( iscsi->status & ISCSI_STATUS_AUTH_FORWARD_REQUIRED ) )
  305. return 0;
  306. assert ( iscsi->initiator_username );
  307. assert ( iscsi->initiator_password );
  308. target->chap_type = IBFT_CHAP_ONE_WAY;
  309. if ( ( rc = ibft_set_string ( strings, &target->chap_name,
  310. iscsi->initiator_username ) ) != 0 )
  311. return rc;
  312. DBG ( "iBFT target username = %s\n",
  313. ibft_string ( strings, &target->chap_name ) );
  314. if ( ( rc = ibft_set_string ( strings, &target->chap_secret,
  315. iscsi->initiator_password ) ) != 0 )
  316. return rc;
  317. DBG ( "iBFT target password = <redacted>\n" );
  318. return 0;
  319. }
  320. /**
  321. * Fill in Target Reverse CHAP portion of iBFT
  322. *
  323. * @v target Target portion of iBFT
  324. * @v strings iBFT string block descriptor
  325. * @v iscsi iSCSI session
  326. * @ret rc Return status code
  327. */
  328. static int ibft_fill_target_reverse_chap ( struct ibft_target *target,
  329. struct ibft_strings *strings,
  330. struct iscsi_session *iscsi ) {
  331. int rc;
  332. if ( ! ( iscsi->status & ISCSI_STATUS_AUTH_REVERSE_REQUIRED ) )
  333. return 0;
  334. assert ( iscsi->initiator_username );
  335. assert ( iscsi->initiator_password );
  336. assert ( iscsi->target_username );
  337. assert ( iscsi->target_password );
  338. target->chap_type = IBFT_CHAP_MUTUAL;
  339. if ( ( rc = ibft_set_string ( strings, &target->reverse_chap_name,
  340. iscsi->target_username ) ) != 0 )
  341. return rc;
  342. DBG ( "iBFT target reverse username = %s\n",
  343. ibft_string ( strings, &target->chap_name ) );
  344. if ( ( rc = ibft_set_string ( strings, &target->reverse_chap_secret,
  345. iscsi->target_password ) ) != 0 )
  346. return rc;
  347. DBG ( "iBFT target reverse password = <redacted>\n" );
  348. return 0;
  349. }
  350. /**
  351. * Fill in Target portion of iBFT
  352. *
  353. * @v target Target portion of iBFT
  354. * @v strings iBFT string block descriptor
  355. * @v iscsi iSCSI session
  356. * @ret rc Return status code
  357. */
  358. static int ibft_fill_target ( struct ibft_target *target,
  359. struct ibft_strings *strings,
  360. struct iscsi_session *iscsi ) {
  361. struct sockaddr_in *sin_target =
  362. ( struct sockaddr_in * ) &iscsi->target_sockaddr;
  363. int rc;
  364. /* Fill in common header */
  365. target->header.structure_id = IBFT_STRUCTURE_ID_TARGET;
  366. target->header.version = 1;
  367. target->header.length = cpu_to_le16 ( sizeof ( *target ) );
  368. target->header.flags = ( IBFT_FL_TARGET_BLOCK_VALID |
  369. IBFT_FL_TARGET_FIRMWARE_BOOT_SELECTED );
  370. /* Fill in Target values */
  371. ibft_set_ipaddr ( &target->ip_address, sin_target->sin_addr );
  372. DBG ( "iBFT target IP = %s\n", ibft_ipaddr ( &target->ip_address ) );
  373. target->socket = cpu_to_le16 ( ntohs ( sin_target->sin_port ) );
  374. DBG ( "iBFT target port = %d\n", target->socket );
  375. memcpy ( &target->boot_lun, &iscsi->lun, sizeof ( target->boot_lun ) );
  376. DBG ( "iBFT target boot LUN = " SCSI_LUN_FORMAT "\n",
  377. SCSI_LUN_DATA ( target->boot_lun ) );
  378. if ( ( rc = ibft_set_string ( strings, &target->target_name,
  379. iscsi->target_iqn ) ) != 0 )
  380. return rc;
  381. DBG ( "iBFT target name = %s\n",
  382. ibft_string ( strings, &target->target_name ) );
  383. if ( ( rc = ibft_fill_target_chap ( target, strings, iscsi ) ) != 0 )
  384. return rc;
  385. if ( ( rc = ibft_fill_target_reverse_chap ( target, strings,
  386. iscsi ) ) != 0 )
  387. return rc;
  388. return 0;
  389. }
  390. /**
  391. * Fill in iBFT
  392. *
  393. * @v iscsi iSCSI session
  394. * @v acpi ACPI table
  395. * @v len Length of ACPI table
  396. * @ret rc Return status code
  397. */
  398. int ibft_describe ( struct iscsi_session *iscsi,
  399. struct acpi_description_header *acpi,
  400. size_t len ) {
  401. struct ipxe_ibft *ibft =
  402. container_of ( acpi, struct ipxe_ibft, table.acpi );
  403. struct ibft_strings strings = {
  404. .table = &ibft->table,
  405. .offset = offsetof ( typeof ( *ibft ), strings ),
  406. .len = len,
  407. };
  408. struct net_device *netdev;
  409. int rc;
  410. /* Ugly hack. Now that we have a generic interface mechanism
  411. * that can support ioctls, we can potentially eliminate this.
  412. */
  413. netdev = last_opened_netdev();
  414. if ( ! netdev ) {
  415. DBGC ( iscsi, "iSCSI %p cannot guess network device\n",
  416. iscsi );
  417. return -ENODEV;
  418. }
  419. /* Fill in ACPI header */
  420. ibft->table.acpi.signature = cpu_to_le32 ( IBFT_SIG );
  421. ibft->table.acpi.length = cpu_to_le32 ( len );
  422. ibft->table.acpi.revision = 1;
  423. /* Fill in Control block */
  424. ibft->table.control.header.structure_id = IBFT_STRUCTURE_ID_CONTROL;
  425. ibft->table.control.header.version = 1;
  426. ibft->table.control.header.length =
  427. cpu_to_le16 ( sizeof ( ibft->table.control ) );
  428. ibft->table.control.initiator =
  429. cpu_to_le16 ( offsetof ( typeof ( *ibft ), initiator ) );
  430. ibft->table.control.nic_0 =
  431. cpu_to_le16 ( offsetof ( typeof ( *ibft ), nic ) );
  432. ibft->table.control.target_0 =
  433. cpu_to_le16 ( offsetof ( typeof ( *ibft ), target ) );
  434. /* Fill in NIC, Initiator and Target blocks */
  435. if ( ( rc = ibft_fill_nic ( &ibft->nic, &strings, netdev ) ) != 0 )
  436. return rc;
  437. if ( ( rc = ibft_fill_initiator ( &ibft->initiator, &strings,
  438. iscsi ) ) != 0 )
  439. return rc;
  440. if ( ( rc = ibft_fill_target ( &ibft->target, &strings,
  441. iscsi ) ) != 0 )
  442. return rc;
  443. return 0;
  444. }