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

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