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.
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:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
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
28 FILE_LICENCE ( BSD2 );
37 #include <gpxe/acpi.h>
39 #include <gpxe/netdevice.h>
40 #include <gpxe/dhcp.h>
41 #include <gpxe/iscsi.h>
42 #include <gpxe/ibft.h>
46 * iSCSI boot firmware table
48 * The information in this file is derived from the document "iSCSI
49 * Boot Firmware Table (iBFT)" as published by IBM at
51 * ftp://ftp.software.ibm.com/systems/support/system_x_pdf/ibm_iscsi_boot_firmware_table_v1.02.pdf
55 #define ibftab __use_data16 ( ibftab )
56 /** The iBFT used by gPXE */
57 struct gpxe_ibft __data16 ( ibftab ) = {
62 .signature = IBFT_SIG,
63 .length = sizeof ( ibftab ),
66 .oem_table_id = "gPXE",
71 .structure_id = IBFT_STRUCTURE_ID_CONTROL,
73 .length = sizeof ( ibftab.table.control ),
76 .initiator = offsetof ( typeof ( ibftab ), initiator ),
77 .nic_0 = offsetof ( typeof ( ibftab ), nic ),
78 .target_0 = offsetof ( typeof ( ibftab ), target ),
81 /* iSCSI initiator information */
84 .structure_id = IBFT_STRUCTURE_ID_INITIATOR,
86 .length = sizeof ( ibftab.initiator ),
87 .flags = ( IBFT_FL_INITIATOR_BLOCK_VALID |
88 IBFT_FL_INITIATOR_FIRMWARE_BOOT_SELECTED ),
94 .structure_id = IBFT_STRUCTURE_ID_NIC,
96 .length = sizeof ( ibftab.nic ),
97 .flags = ( IBFT_FL_NIC_BLOCK_VALID |
98 IBFT_FL_NIC_FIRMWARE_BOOT_SELECTED ),
101 /* iSCSI target information */
104 .structure_id = IBFT_STRUCTURE_ID_TARGET,
106 .length = sizeof ( ibftab.target ),
107 .flags = ( IBFT_FL_TARGET_BLOCK_VALID |
108 IBFT_FL_TARGET_FIRMWARE_BOOT_SELECTED ),
114 * Fill in an IP address field within iBFT
116 * @v ipaddr IP address field
119 static void ibft_set_ipaddr ( struct ibft_ipaddr *ipaddr, struct in_addr in ) {
120 memset ( ipaddr, 0, sizeof ( ipaddr ) );
123 ipaddr->ones = 0xffff;
128 * Fill in an IP address within iBFT from configuration setting
130 * @v ipaddr IP address field
131 * @v setting Configuration setting
132 * @v tag DHCP option tag
134 static void ibft_set_ipaddr_option ( struct ibft_ipaddr *ipaddr,
135 struct setting *setting ) {
136 struct in_addr in = { 0 };
137 fetch_ipv4_setting ( NULL, setting, &in );
138 ibft_set_ipaddr ( ipaddr, in );
142 * Read IP address from iBFT (for debugging)
144 * @v strings iBFT string block descriptor
145 * @v string String field
146 * @ret ipaddr IP address string
148 static const char * ibft_ipaddr ( struct ibft_ipaddr *ipaddr ) {
149 return inet_ntoa ( ipaddr->in );
153 * Allocate a string within iBFT
155 * @v strings iBFT string block descriptor
156 * @v string String field to fill in
157 * @v len Length of string to allocate (excluding NUL)
158 * @ret rc Return status code
160 static int ibft_alloc_string ( struct ibft_string_block *strings,
161 struct ibft_string *string, size_t len ) {
163 unsigned int remaining;
165 dest = ( ( ( char * ) strings->table ) + strings->offset );
166 remaining = ( strings->table->acpi.length - strings->offset );
167 if ( len >= remaining )
170 string->offset = strings->offset;
171 string->length = len;
172 strings->offset += ( len + 1 );
177 * Fill in a string field within iBFT
179 * @v strings iBFT string block descriptor
180 * @v string String field
181 * @v data String to fill in, or NULL
182 * @ret rc Return status code
184 static int ibft_set_string ( struct ibft_string_block *strings,
185 struct ibft_string *string, const char *data ) {
192 if ( ( rc = ibft_alloc_string ( strings, string,
193 strlen ( data ) ) ) != 0 )
195 dest = ( ( ( char * ) strings->table ) + string->offset );
196 strcpy ( dest, data );
202 * Fill in a string field within iBFT from configuration setting
204 * @v strings iBFT string block descriptor
205 * @v string String field
206 * @v setting Configuration setting
207 * @ret rc Return status code
209 static int ibft_set_string_option ( struct ibft_string_block *strings,
210 struct ibft_string *string,
211 struct setting *setting ) {
216 len = fetch_setting_len ( NULL, setting );
223 if ( ( rc = ibft_alloc_string ( strings, string, len ) ) != 0 )
225 dest = ( ( ( char * ) strings->table ) + string->offset );
226 fetch_string_setting ( NULL, setting, dest, ( len + 1 ) );
231 * Read string from iBFT (for debugging)
233 * @v strings iBFT string block descriptor
234 * @v string String field
235 * @ret data String content (or "<empty>")
237 static const char * ibft_string ( struct ibft_string_block *strings,
238 struct ibft_string *string ) {
239 return ( ( ( char * ) strings->table ) + string->offset );
243 * Fill in NIC portion of iBFT
245 * @v nic NIC portion of iBFT
246 * @v strings iBFT string block descriptor
247 * @v netdev Network device
248 * @ret rc Return status code
250 static int ibft_fill_nic ( struct ibft_nic *nic,
251 struct ibft_string_block *strings,
252 struct net_device *netdev ) {
253 struct in_addr netmask_addr = { 0 };
254 unsigned int netmask_count = 0;
257 /* Extract values from DHCP configuration */
258 ibft_set_ipaddr_option ( &nic->ip_address, &ip_setting );
259 DBG ( "iBFT NIC IP = %s\n", ibft_ipaddr ( &nic->ip_address ) );
260 ibft_set_ipaddr_option ( &nic->gateway, &gateway_setting );
261 DBG ( "iBFT NIC gateway = %s\n", ibft_ipaddr ( &nic->gateway ) );
262 ibft_set_ipaddr_option ( &nic->dns[0], &dns_setting );
263 DBG ( "iBFT NIC DNS = %s\n", ibft_ipaddr ( &nic->dns[0] ) );
264 if ( ( rc = ibft_set_string_option ( strings, &nic->hostname,
265 &hostname_setting ) ) != 0 )
267 DBG ( "iBFT NIC hostname = %s\n",
268 ibft_string ( strings, &nic->hostname ) );
270 /* Derive subnet mask prefix from subnet mask */
271 fetch_ipv4_setting ( NULL, &netmask_setting, &netmask_addr );
272 while ( netmask_addr.s_addr ) {
273 if ( netmask_addr.s_addr & 0x1 )
275 netmask_addr.s_addr >>= 1;
277 nic->subnet_mask_prefix = netmask_count;
278 DBG ( "iBFT NIC subnet = /%d\n", nic->subnet_mask_prefix );
280 /* Extract values from net-device configuration */
281 memcpy ( nic->mac_address, netdev->ll_addr,
282 sizeof ( nic->mac_address ) );
283 DBG ( "iBFT NIC MAC = %s\n",
284 netdev->ll_protocol->ntoa ( nic->mac_address ) );
285 nic->pci_bus_dev_func = netdev->dev->desc.location;
286 DBG ( "iBFT NIC PCI = %04x\n", nic->pci_bus_dev_func );
292 * Fill in Initiator portion of iBFT
294 * @v initiator Initiator portion of iBFT
295 * @v strings iBFT string block descriptor
296 * @ret rc Return status code
298 static int ibft_fill_initiator ( struct ibft_initiator *initiator,
299 struct ibft_string_block *strings ) {
300 const char *initiator_iqn = iscsi_initiator_iqn();
303 if ( ( rc = ibft_set_string ( strings, &initiator->initiator_name,
304 initiator_iqn ) ) != 0 )
306 DBG ( "iBFT initiator hostname = %s\n",
307 ibft_string ( strings, &initiator->initiator_name ) );
313 * Fill in Target CHAP portion of iBFT
315 * @v target Target portion of iBFT
316 * @v strings iBFT string block descriptor
317 * @v iscsi iSCSI session
318 * @ret rc Return status code
320 static int ibft_fill_target_chap ( struct ibft_target *target,
321 struct ibft_string_block *strings,
322 struct iscsi_session *iscsi ) {
325 if ( ! ( iscsi->status & ISCSI_STATUS_AUTH_FORWARD_REQUIRED ) )
328 assert ( iscsi->initiator_username );
329 assert ( iscsi->initiator_password );
331 target->chap_type = IBFT_CHAP_ONE_WAY;
332 if ( ( rc = ibft_set_string ( strings, &target->chap_name,
333 iscsi->initiator_username ) ) != 0 )
335 DBG ( "iBFT target username = %s\n",
336 ibft_string ( strings, &target->chap_name ) );
337 if ( ( rc = ibft_set_string ( strings, &target->chap_secret,
338 iscsi->initiator_password ) ) != 0 )
340 DBG ( "iBFT target password = <redacted>\n" );
346 * Fill in Target Reverse CHAP portion of iBFT
348 * @v target Target portion of iBFT
349 * @v strings iBFT string block descriptor
350 * @v iscsi iSCSI session
351 * @ret rc Return status code
353 static int ibft_fill_target_reverse_chap ( struct ibft_target *target,
354 struct ibft_string_block *strings,
355 struct iscsi_session *iscsi ) {
358 if ( ! ( iscsi->status & ISCSI_STATUS_AUTH_REVERSE_REQUIRED ) )
361 assert ( iscsi->initiator_username );
362 assert ( iscsi->initiator_password );
363 assert ( iscsi->target_username );
364 assert ( iscsi->target_password );
366 target->chap_type = IBFT_CHAP_MUTUAL;
367 if ( ( rc = ibft_set_string ( strings, &target->reverse_chap_name,
368 iscsi->target_username ) ) != 0 )
370 DBG ( "iBFT target reverse username = %s\n",
371 ibft_string ( strings, &target->chap_name ) );
372 if ( ( rc = ibft_set_string ( strings, &target->reverse_chap_secret,
373 iscsi->target_password ) ) != 0 )
375 DBG ( "iBFT target reverse password = <redacted>\n" );
381 * Fill in Target portion of iBFT
383 * @v target Target portion of iBFT
384 * @v strings iBFT string block descriptor
385 * @v iscsi iSCSI session
386 * @ret rc Return status code
388 static int ibft_fill_target ( struct ibft_target *target,
389 struct ibft_string_block *strings,
390 struct iscsi_session *iscsi ) {
391 struct sockaddr_in *sin_target =
392 ( struct sockaddr_in * ) &iscsi->target_sockaddr;
395 /* Fill in Target values */
396 ibft_set_ipaddr ( &target->ip_address, sin_target->sin_addr );
397 DBG ( "iBFT target IP = %s\n", ibft_ipaddr ( &target->ip_address ) );
398 target->socket = ntohs ( sin_target->sin_port );
399 DBG ( "iBFT target port = %d\n", target->socket );
400 if ( ( rc = ibft_set_string ( strings, &target->target_name,
401 iscsi->target_iqn ) ) != 0 )
403 DBG ( "iBFT target name = %s\n",
404 ibft_string ( strings, &target->target_name ) );
405 if ( ( rc = ibft_fill_target_chap ( target, strings, iscsi ) ) != 0 )
407 if ( ( rc = ibft_fill_target_reverse_chap ( target, strings,
415 * Fill in all variable portions of iBFT
417 * @v netdev Network device
418 * @v initiator_iqn Initiator IQN
419 * @v st_target Target socket address
420 * @v target_iqn Target IQN
421 * @ret rc Return status code
424 int ibft_fill_data ( struct net_device *netdev,
425 struct iscsi_session *iscsi ) {
426 struct ibft_string_block strings = {
427 .table = &ibftab.table,
428 .offset = offsetof ( typeof ( ibftab ), strings ),
432 /* Fill in NIC, Initiator and Target portions */
433 if ( ( rc = ibft_fill_nic ( &ibftab.nic, &strings, netdev ) ) != 0 )
435 if ( ( rc = ibft_fill_initiator ( &ibftab.initiator,
438 if ( ( rc = ibft_fill_target ( &ibftab.target, &strings,
442 /* Update checksum */
443 acpi_fix_checksum ( &ibftab.table.acpi );