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

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