Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ibft.c 14KB

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