Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 configuration setting
  122. *
  123. * @v ipaddr IP address field
  124. * @v setting Configuration setting
  125. * @v tag DHCP option tag
  126. */
  127. static void ibft_set_ipaddr_option ( struct ibft_ipaddr *ipaddr,
  128. struct setting *setting ) {
  129. struct in_addr in = { 0 };
  130. fetch_ipv4_setting ( NULL, setting, &in );
  131. ibft_set_ipaddr ( ipaddr, in );
  132. }
  133. /**
  134. * Allocate a string within iBFT
  135. *
  136. * @v strings iBFT string block descriptor
  137. * @v string String field to fill in
  138. * @v len Length of string to allocate (excluding NUL)
  139. * @ret rc Return status code
  140. */
  141. static int ibft_alloc_string ( struct ibft_string_block *strings,
  142. struct ibft_string *string, size_t len ) {
  143. char *dest;
  144. unsigned int remaining;
  145. dest = ( ( ( char * ) strings->table ) + strings->offset );
  146. remaining = ( strings->table->acpi.length - strings->offset );
  147. if ( len >= remaining )
  148. return -ENOMEM;
  149. string->offset = strings->offset;
  150. string->length = len;
  151. strings->offset += ( len + 1 );
  152. return 0;
  153. }
  154. /**
  155. * Fill in a string field within iBFT
  156. *
  157. * @v strings iBFT string block descriptor
  158. * @v string String field
  159. * @v data String to fill in, or NULL
  160. * @ret rc Return status code
  161. */
  162. static int ibft_set_string ( struct ibft_string_block *strings,
  163. struct ibft_string *string, const char *data ) {
  164. char *dest;
  165. int rc;
  166. if ( ! data )
  167. return 0;
  168. if ( ( rc = ibft_alloc_string ( strings, string,
  169. strlen ( data ) ) ) != 0 )
  170. return rc;
  171. dest = ( ( ( char * ) strings->table ) + string->offset );
  172. strcpy ( dest, data );
  173. return 0;
  174. }
  175. /**
  176. * Fill in a string field within iBFT from configuration setting
  177. *
  178. * @v strings iBFT string block descriptor
  179. * @v string String field
  180. * @v setting Configuration setting
  181. * @ret rc Return status code
  182. */
  183. static int ibft_set_string_option ( struct ibft_string_block *strings,
  184. struct ibft_string *string,
  185. struct setting *setting ) {
  186. int len;
  187. char *dest;
  188. int rc;
  189. len = fetch_setting_len ( NULL, setting );
  190. if ( len < 0 ) {
  191. string->offset = 0;
  192. string->length = 0;
  193. return 0;
  194. }
  195. if ( ( rc = ibft_alloc_string ( strings, string, len ) ) != 0 )
  196. return rc;
  197. dest = ( ( ( char * ) strings->table ) + string->offset );
  198. fetch_string_setting ( NULL, setting, dest, ( len + 1 ) );
  199. return 0;
  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_string_block *strings,
  211. struct net_device *netdev ) {
  212. struct in_addr netmask_addr = { 0 };
  213. unsigned int netmask_count = 0;
  214. int rc;
  215. /* Extract values from DHCP configuration */
  216. ibft_set_ipaddr_option ( &nic->ip_address, &ip_setting );
  217. ibft_set_ipaddr_option ( &nic->gateway, &gateway_setting );
  218. ibft_set_ipaddr_option ( &nic->dns[0], &dns_setting );
  219. if ( ( rc = ibft_set_string_option ( strings, &nic->hostname,
  220. &hostname_setting ) ) != 0 )
  221. return rc;
  222. /* Derive subnet mask prefix from subnet mask */
  223. fetch_ipv4_setting ( NULL, &netmask_setting, &netmask_addr );
  224. while ( netmask_addr.s_addr ) {
  225. if ( netmask_addr.s_addr & 0x1 )
  226. netmask_count++;
  227. netmask_addr.s_addr >>= 1;
  228. }
  229. nic->subnet_mask_prefix = netmask_count;
  230. /* Extract values from net-device configuration */
  231. memcpy ( nic->mac_address, netdev->ll_addr,
  232. sizeof ( nic->mac_address ) );
  233. nic->pci_bus_dev_func = netdev->dev->desc.location;
  234. return 0;
  235. }
  236. /**
  237. * Fill in Initiator portion of iBFT
  238. *
  239. * @v initiator Initiator portion of iBFT
  240. * @v strings iBFT string block descriptor
  241. * @ret rc Return status code
  242. */
  243. static int ibft_fill_initiator ( struct ibft_initiator *initiator,
  244. struct ibft_string_block *strings ) {
  245. const char *initiator_iqn = iscsi_initiator_iqn();
  246. int rc;
  247. if ( ( rc = ibft_set_string ( strings, &initiator->initiator_name,
  248. initiator_iqn ) ) != 0 )
  249. return rc;
  250. return 0;
  251. }
  252. /**
  253. * Fill in Target CHAP portion of iBFT
  254. *
  255. * @v target Target portion of iBFT
  256. * @v strings iBFT string block descriptor
  257. * @v iscsi iSCSI session
  258. * @ret rc Return status code
  259. */
  260. static int ibft_fill_target_chap ( struct ibft_target *target,
  261. struct ibft_string_block *strings,
  262. struct iscsi_session *iscsi ) {
  263. int rc;
  264. if ( ! iscsi->initiator_username )
  265. return 0;
  266. assert ( iscsi->initiator_password );
  267. target->chap_type = IBFT_CHAP_ONE_WAY;
  268. if ( ( rc = ibft_set_string ( strings, &target->chap_name,
  269. iscsi->initiator_username ) ) != 0 )
  270. return rc;
  271. if ( ( rc = ibft_set_string ( strings, &target->chap_secret,
  272. iscsi->initiator_password ) ) != 0 )
  273. return rc;
  274. return 0;
  275. }
  276. /**
  277. * Fill in Target Reverse CHAP portion of iBFT
  278. *
  279. * @v target Target portion of iBFT
  280. * @v strings iBFT string block descriptor
  281. * @v iscsi iSCSI session
  282. * @ret rc Return status code
  283. */
  284. static int ibft_fill_target_reverse_chap ( struct ibft_target *target,
  285. struct ibft_string_block *strings,
  286. struct iscsi_session *iscsi ) {
  287. int rc;
  288. if ( ! iscsi->target_username )
  289. return 0;
  290. assert ( iscsi->target_password );
  291. assert ( iscsi->initiator_username );
  292. assert ( iscsi->initiator_password );
  293. target->chap_type = IBFT_CHAP_MUTUAL;
  294. if ( ( rc = ibft_set_string ( strings, &target->reverse_chap_name,
  295. iscsi->target_username ) ) != 0 )
  296. return rc;
  297. if ( ( rc = ibft_set_string ( strings, &target->reverse_chap_secret,
  298. iscsi->target_password ) ) != 0 )
  299. return rc;
  300. return 0;
  301. }
  302. /**
  303. * Fill in Target portion of iBFT
  304. *
  305. * @v target Target portion of iBFT
  306. * @v strings iBFT string block descriptor
  307. * @v iscsi iSCSI session
  308. * @ret rc Return status code
  309. */
  310. static int ibft_fill_target ( struct ibft_target *target,
  311. struct ibft_string_block *strings,
  312. struct iscsi_session *iscsi ) {
  313. struct sockaddr_in *sin_target =
  314. ( struct sockaddr_in * ) &iscsi->target_sockaddr;
  315. int rc;
  316. /* Fill in Target values */
  317. ibft_set_ipaddr ( &target->ip_address, sin_target->sin_addr );
  318. target->socket = ntohs ( sin_target->sin_port );
  319. if ( ( rc = ibft_set_string ( strings, &target->target_name,
  320. iscsi->target_iqn ) ) != 0 )
  321. return rc;
  322. if ( ( rc = ibft_fill_target_chap ( target, strings, iscsi ) ) != 0 )
  323. return rc;
  324. if ( ( rc = ibft_fill_target_reverse_chap ( target, strings,
  325. iscsi ) ) != 0 )
  326. return rc;
  327. return 0;
  328. }
  329. /**
  330. * Fill in all variable portions of iBFT
  331. *
  332. * @v netdev Network device
  333. * @v initiator_iqn Initiator IQN
  334. * @v st_target Target socket address
  335. * @v target_iqn Target IQN
  336. * @ret rc Return status code
  337. *
  338. */
  339. int ibft_fill_data ( struct net_device *netdev,
  340. struct iscsi_session *iscsi ) {
  341. struct ibft_string_block strings = {
  342. .table = &ibftab.table,
  343. .offset = offsetof ( typeof ( ibftab ), strings ),
  344. };
  345. int rc;
  346. /* Fill in NIC, Initiator and Target portions */
  347. if ( ( rc = ibft_fill_nic ( &ibftab.nic, &strings, netdev ) ) != 0 )
  348. return rc;
  349. if ( ( rc = ibft_fill_initiator ( &ibftab.initiator,
  350. &strings ) ) != 0 )
  351. return rc;
  352. if ( ( rc = ibft_fill_target ( &ibftab.target, &strings,
  353. iscsi ) ) != 0 )
  354. return rc;
  355. /* Update checksum */
  356. acpi_fix_checksum ( &ibftab.table.acpi );
  357. return 0;
  358. }