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/ethernet.h>
41 #include <gpxe/dhcp.h>
42 #include <gpxe/iscsi.h>
43 #include <gpxe/ibft.h>
47 * iSCSI boot firmware table
49 * The information in this file is derived from the document "iSCSI
50 * Boot Firmware Table (iBFT)" as published by IBM at
52 * ftp://ftp.software.ibm.com/systems/support/system_x_pdf/ibm_iscsi_boot_firmware_table_v1.02.pdf
56 #define ibftab __use_data16 ( ibftab )
57 /** The iBFT used by gPXE */
58 struct gpxe_ibft
__data16 ( ibftab
) = {
63 .signature
= IBFT_SIG
,
64 .length
= sizeof ( ibftab
),
67 .oem_table_id
= "gPXE",
72 .structure_id
= IBFT_STRUCTURE_ID_CONTROL
,
74 .length
= sizeof ( ibftab
.table
.control
),
77 .initiator
= offsetof ( typeof ( ibftab
), initiator
),
78 .nic_0
= offsetof ( typeof ( ibftab
), nic
),
79 .target_0
= offsetof ( typeof ( ibftab
), target
),
82 /* iSCSI initiator information */
85 .structure_id
= IBFT_STRUCTURE_ID_INITIATOR
,
87 .length
= sizeof ( ibftab
.initiator
),
88 .flags
= ( IBFT_FL_INITIATOR_BLOCK_VALID
|
89 IBFT_FL_INITIATOR_FIRMWARE_BOOT_SELECTED
),
95 .structure_id
= IBFT_STRUCTURE_ID_NIC
,
97 .length
= sizeof ( ibftab
.nic
),
98 .flags
= ( IBFT_FL_NIC_BLOCK_VALID
|
99 IBFT_FL_NIC_FIRMWARE_BOOT_SELECTED
),
102 /* iSCSI target information */
105 .structure_id
= IBFT_STRUCTURE_ID_TARGET
,
107 .length
= sizeof ( ibftab
.target
),
108 .flags
= ( IBFT_FL_TARGET_BLOCK_VALID
|
109 IBFT_FL_TARGET_FIRMWARE_BOOT_SELECTED
),
115 * Fill in an IP address field within iBFT
117 * @v ipaddr IP address field
120 static void ibft_set_ipaddr ( struct ibft_ipaddr
*ipaddr
, struct in_addr in
) {
121 memset ( ipaddr
, 0, sizeof ( ipaddr
) );
124 ipaddr
->ones
= 0xffff;
129 * Fill in an IP address within iBFT from configuration setting
131 * @v ipaddr IP address field
132 * @v setting Configuration setting
133 * @v tag DHCP option tag
135 static void ibft_set_ipaddr_option ( struct ibft_ipaddr
*ipaddr
,
136 struct setting
*setting
) {
137 struct in_addr in
= { 0 };
138 fetch_ipv4_setting ( NULL
, setting
, &in
);
139 ibft_set_ipaddr ( ipaddr
, in
);
143 * Read IP address from iBFT (for debugging)
145 * @v strings iBFT string block descriptor
146 * @v string String field
147 * @ret ipaddr IP address string
149 static const char * ibft_ipaddr ( struct ibft_ipaddr
*ipaddr
) {
150 return inet_ntoa ( ipaddr
->in
);
154 * Allocate a string within iBFT
156 * @v strings iBFT string block descriptor
157 * @v string String field to fill in
158 * @v len Length of string to allocate (excluding NUL)
159 * @ret rc Return status code
161 static int ibft_alloc_string ( struct ibft_string_block
*strings
,
162 struct ibft_string
*string
, size_t len
) {
164 unsigned int remaining
;
166 dest
= ( ( ( char * ) strings
->table
) + strings
->offset
);
167 remaining
= ( strings
->table
->acpi
.length
- strings
->offset
);
168 if ( len
>= remaining
)
171 string
->offset
= strings
->offset
;
172 string
->length
= len
;
173 strings
->offset
+= ( len
+ 1 );
178 * Fill in a string field within iBFT
180 * @v strings iBFT string block descriptor
181 * @v string String field
182 * @v data String to fill in, or NULL
183 * @ret rc Return status code
185 static int ibft_set_string ( struct ibft_string_block
*strings
,
186 struct ibft_string
*string
, const char *data
) {
193 if ( ( rc
= ibft_alloc_string ( strings
, string
,
194 strlen ( data
) ) ) != 0 )
196 dest
= ( ( ( char * ) strings
->table
) + string
->offset
);
197 strcpy ( dest
, data
);
203 * Fill in a string field within iBFT from configuration setting
205 * @v strings iBFT string block descriptor
206 * @v string String field
207 * @v setting Configuration setting
208 * @ret rc Return status code
210 static int ibft_set_string_option ( struct ibft_string_block
*strings
,
211 struct ibft_string
*string
,
212 struct setting
*setting
) {
217 len
= fetch_setting_len ( NULL
, setting
);
224 if ( ( rc
= ibft_alloc_string ( strings
, string
, len
) ) != 0 )
226 dest
= ( ( ( char * ) strings
->table
) + string
->offset
);
227 fetch_string_setting ( NULL
, setting
, dest
, ( len
+ 1 ) );
232 * Read string from iBFT (for debugging)
234 * @v strings iBFT string block descriptor
235 * @v string String field
236 * @ret data String content (or "<empty>")
238 static const char * ibft_string ( struct ibft_string_block
*strings
,
239 struct ibft_string
*string
) {
240 return ( string
->offset
?
241 ( ( ( char * ) strings
->table
) + string
->offset
) : NULL
);
245 * Fill in NIC portion of iBFT
247 * @v nic NIC portion of iBFT
248 * @v strings iBFT string block descriptor
249 * @v netdev Network device
250 * @ret rc Return status code
252 static int ibft_fill_nic ( struct ibft_nic
*nic
,
253 struct ibft_string_block
*strings
,
254 struct net_device
*netdev
) {
255 struct ll_protocol
*ll_protocol
= netdev
->ll_protocol
;
256 struct in_addr netmask_addr
= { 0 };
257 unsigned int netmask_count
= 0;
260 /* Extract values from DHCP configuration */
261 ibft_set_ipaddr_option ( &nic
->ip_address
, &ip_setting
);
262 DBG ( "iBFT NIC IP = %s\n", ibft_ipaddr ( &nic
->ip_address
) );
263 ibft_set_ipaddr_option ( &nic
->gateway
, &gateway_setting
);
264 DBG ( "iBFT NIC gateway = %s\n", ibft_ipaddr ( &nic
->gateway
) );
265 ibft_set_ipaddr_option ( &nic
->dns
[0], &dns_setting
);
266 DBG ( "iBFT NIC DNS = %s\n", ibft_ipaddr ( &nic
->dns
[0] ) );
267 if ( ( rc
= ibft_set_string_option ( strings
, &nic
->hostname
,
268 &hostname_setting
) ) != 0 )
270 DBG ( "iBFT NIC hostname = %s\n",
271 ibft_string ( strings
, &nic
->hostname
) );
273 /* Derive subnet mask prefix from subnet mask */
274 fetch_ipv4_setting ( NULL
, &netmask_setting
, &netmask_addr
);
275 while ( netmask_addr
.s_addr
) {
276 if ( netmask_addr
.s_addr
& 0x1 )
278 netmask_addr
.s_addr
>>= 1;
280 nic
->subnet_mask_prefix
= netmask_count
;
281 DBG ( "iBFT NIC subnet = /%d\n", nic
->subnet_mask_prefix
);
283 /* Extract values from net-device configuration */
284 if ( ( rc
= ll_protocol
->eth_addr ( netdev
->ll_addr
,
285 nic
->mac_address
) ) != 0 ) {
286 DBG ( "Could not determine iBFT MAC: %s\n", strerror ( rc
) );
289 DBG ( "iBFT NIC MAC = %s\n", eth_ntoa ( nic
->mac_address
) );
290 nic
->pci_bus_dev_func
= netdev
->dev
->desc
.location
;
291 DBG ( "iBFT NIC PCI = %04x\n", nic
->pci_bus_dev_func
);
297 * Fill in Initiator portion of iBFT
299 * @v initiator Initiator portion of iBFT
300 * @v strings iBFT string block descriptor
301 * @ret rc Return status code
303 static int ibft_fill_initiator ( struct ibft_initiator
*initiator
,
304 struct ibft_string_block
*strings
) {
305 const char *initiator_iqn
= iscsi_initiator_iqn();
308 if ( ( rc
= ibft_set_string ( strings
, &initiator
->initiator_name
,
309 initiator_iqn
) ) != 0 )
311 DBG ( "iBFT initiator hostname = %s\n",
312 ibft_string ( strings
, &initiator
->initiator_name
) );
318 * Fill in Target CHAP portion of iBFT
320 * @v target Target portion of iBFT
321 * @v strings iBFT string block descriptor
322 * @v iscsi iSCSI session
323 * @ret rc Return status code
325 static int ibft_fill_target_chap ( struct ibft_target
*target
,
326 struct ibft_string_block
*strings
,
327 struct iscsi_session
*iscsi
) {
330 if ( ! ( iscsi
->status
& ISCSI_STATUS_AUTH_FORWARD_REQUIRED
) )
333 assert ( iscsi
->initiator_username
);
334 assert ( iscsi
->initiator_password
);
336 target
->chap_type
= IBFT_CHAP_ONE_WAY
;
337 if ( ( rc
= ibft_set_string ( strings
, &target
->chap_name
,
338 iscsi
->initiator_username
) ) != 0 )
340 DBG ( "iBFT target username = %s\n",
341 ibft_string ( strings
, &target
->chap_name
) );
342 if ( ( rc
= ibft_set_string ( strings
, &target
->chap_secret
,
343 iscsi
->initiator_password
) ) != 0 )
345 DBG ( "iBFT target password = <redacted>\n" );
351 * Fill in Target Reverse CHAP portion of iBFT
353 * @v target Target portion of iBFT
354 * @v strings iBFT string block descriptor
355 * @v iscsi iSCSI session
356 * @ret rc Return status code
358 static int ibft_fill_target_reverse_chap ( struct ibft_target
*target
,
359 struct ibft_string_block
*strings
,
360 struct iscsi_session
*iscsi
) {
363 if ( ! ( iscsi
->status
& ISCSI_STATUS_AUTH_REVERSE_REQUIRED
) )
366 assert ( iscsi
->initiator_username
);
367 assert ( iscsi
->initiator_password
);
368 assert ( iscsi
->target_username
);
369 assert ( iscsi
->target_password
);
371 target
->chap_type
= IBFT_CHAP_MUTUAL
;
372 if ( ( rc
= ibft_set_string ( strings
, &target
->reverse_chap_name
,
373 iscsi
->target_username
) ) != 0 )
375 DBG ( "iBFT target reverse username = %s\n",
376 ibft_string ( strings
, &target
->chap_name
) );
377 if ( ( rc
= ibft_set_string ( strings
, &target
->reverse_chap_secret
,
378 iscsi
->target_password
) ) != 0 )
380 DBG ( "iBFT target reverse password = <redacted>\n" );
386 * Fill in Target portion of iBFT
388 * @v target Target portion of iBFT
389 * @v strings iBFT string block descriptor
390 * @v iscsi iSCSI session
391 * @ret rc Return status code
393 static int ibft_fill_target ( struct ibft_target
*target
,
394 struct ibft_string_block
*strings
,
395 struct iscsi_session
*iscsi
) {
396 struct sockaddr_in
*sin_target
=
397 ( struct sockaddr_in
* ) &iscsi
->target_sockaddr
;
400 /* Fill in Target values */
401 ibft_set_ipaddr ( &target
->ip_address
, sin_target
->sin_addr
);
402 DBG ( "iBFT target IP = %s\n", ibft_ipaddr ( &target
->ip_address
) );
403 target
->socket
= ntohs ( sin_target
->sin_port
);
404 DBG ( "iBFT target port = %d\n", target
->socket
);
405 if ( ( rc
= ibft_set_string ( strings
, &target
->target_name
,
406 iscsi
->target_iqn
) ) != 0 )
408 DBG ( "iBFT target name = %s\n",
409 ibft_string ( strings
, &target
->target_name
) );
410 if ( ( rc
= ibft_fill_target_chap ( target
, strings
, iscsi
) ) != 0 )
412 if ( ( rc
= ibft_fill_target_reverse_chap ( target
, strings
,
420 * Fill in all variable portions of iBFT
422 * @v netdev Network device
423 * @v initiator_iqn Initiator IQN
424 * @v st_target Target socket address
425 * @v target_iqn Target IQN
426 * @ret rc Return status code
429 int ibft_fill_data ( struct net_device
*netdev
,
430 struct iscsi_session
*iscsi
) {
431 struct ibft_string_block strings
= {
432 .table
= &ibftab
.table
,
433 .offset
= offsetof ( typeof ( ibftab
), strings
),
437 /* Fill in NIC, Initiator and Target portions */
438 if ( ( rc
= ibft_fill_nic ( &ibftab
.nic
, &strings
, netdev
) ) != 0 )
440 if ( ( rc
= ibft_fill_initiator ( &ibftab
.initiator
,
443 if ( ( rc
= ibft_fill_target ( &ibftab
.target
, &strings
,
447 /* Update checksum */
448 acpi_fix_checksum ( &ibftab
.table
.acpi
);