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 ( string->offset ?
240 ( ( ( char * ) strings->table ) + string->offset ) : NULL );
244 * Fill in NIC portion of iBFT
246 * @v nic NIC portion of iBFT
247 * @v strings iBFT string block descriptor
248 * @v netdev Network device
249 * @ret rc Return status code
251 static int ibft_fill_nic ( struct ibft_nic *nic,
252 struct ibft_string_block *strings,
253 struct net_device *netdev ) {
254 struct in_addr netmask_addr = { 0 };
255 unsigned int netmask_count = 0;
258 /* Extract values from DHCP configuration */
259 ibft_set_ipaddr_option ( &nic->ip_address, &ip_setting );
260 DBG ( "iBFT NIC IP = %s\n", ibft_ipaddr ( &nic->ip_address ) );
261 ibft_set_ipaddr_option ( &nic->gateway, &gateway_setting );
262 DBG ( "iBFT NIC gateway = %s\n", ibft_ipaddr ( &nic->gateway ) );
263 ibft_set_ipaddr_option ( &nic->dns[0], &dns_setting );
264 DBG ( "iBFT NIC DNS = %s\n", ibft_ipaddr ( &nic->dns[0] ) );
265 if ( ( rc = ibft_set_string_option ( strings, &nic->hostname,
266 &hostname_setting ) ) != 0 )
268 DBG ( "iBFT NIC hostname = %s\n",
269 ibft_string ( strings, &nic->hostname ) );
271 /* Derive subnet mask prefix from subnet mask */
272 fetch_ipv4_setting ( NULL, &netmask_setting, &netmask_addr );
273 while ( netmask_addr.s_addr ) {
274 if ( netmask_addr.s_addr & 0x1 )
276 netmask_addr.s_addr >>= 1;
278 nic->subnet_mask_prefix = netmask_count;
279 DBG ( "iBFT NIC subnet = /%d\n", nic->subnet_mask_prefix );
281 /* Extract values from net-device configuration */
282 memcpy ( nic->mac_address, netdev->ll_addr,
283 sizeof ( nic->mac_address ) );
284 DBG ( "iBFT NIC MAC = %s\n",
285 netdev->ll_protocol->ntoa ( nic->mac_address ) );
286 nic->pci_bus_dev_func = netdev->dev->desc.location;
287 DBG ( "iBFT NIC PCI = %04x\n", nic->pci_bus_dev_func );
293 * Fill in Initiator portion of iBFT
295 * @v initiator Initiator portion of iBFT
296 * @v strings iBFT string block descriptor
297 * @ret rc Return status code
299 static int ibft_fill_initiator ( struct ibft_initiator *initiator,
300 struct ibft_string_block *strings ) {
301 const char *initiator_iqn = iscsi_initiator_iqn();
304 if ( ( rc = ibft_set_string ( strings, &initiator->initiator_name,
305 initiator_iqn ) ) != 0 )
307 DBG ( "iBFT initiator hostname = %s\n",
308 ibft_string ( strings, &initiator->initiator_name ) );
314 * Fill in Target CHAP portion of iBFT
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
321 static int ibft_fill_target_chap ( struct ibft_target *target,
322 struct ibft_string_block *strings,
323 struct iscsi_session *iscsi ) {
326 if ( ! ( iscsi->status & ISCSI_STATUS_AUTH_FORWARD_REQUIRED ) )
329 assert ( iscsi->initiator_username );
330 assert ( iscsi->initiator_password );
332 target->chap_type = IBFT_CHAP_ONE_WAY;
333 if ( ( rc = ibft_set_string ( strings, &target->chap_name,
334 iscsi->initiator_username ) ) != 0 )
336 DBG ( "iBFT target username = %s\n",
337 ibft_string ( strings, &target->chap_name ) );
338 if ( ( rc = ibft_set_string ( strings, &target->chap_secret,
339 iscsi->initiator_password ) ) != 0 )
341 DBG ( "iBFT target password = <redacted>\n" );
347 * Fill in Target Reverse CHAP portion of iBFT
349 * @v target Target portion of iBFT
350 * @v strings iBFT string block descriptor
351 * @v iscsi iSCSI session
352 * @ret rc Return status code
354 static int ibft_fill_target_reverse_chap ( struct ibft_target *target,
355 struct ibft_string_block *strings,
356 struct iscsi_session *iscsi ) {
359 if ( ! ( iscsi->status & ISCSI_STATUS_AUTH_REVERSE_REQUIRED ) )
362 assert ( iscsi->initiator_username );
363 assert ( iscsi->initiator_password );
364 assert ( iscsi->target_username );
365 assert ( iscsi->target_password );
367 target->chap_type = IBFT_CHAP_MUTUAL;
368 if ( ( rc = ibft_set_string ( strings, &target->reverse_chap_name,
369 iscsi->target_username ) ) != 0 )
371 DBG ( "iBFT target reverse username = %s\n",
372 ibft_string ( strings, &target->chap_name ) );
373 if ( ( rc = ibft_set_string ( strings, &target->reverse_chap_secret,
374 iscsi->target_password ) ) != 0 )
376 DBG ( "iBFT target reverse password = <redacted>\n" );
382 * Fill in Target portion of iBFT
384 * @v target Target portion of iBFT
385 * @v strings iBFT string block descriptor
386 * @v iscsi iSCSI session
387 * @ret rc Return status code
389 static int ibft_fill_target ( struct ibft_target *target,
390 struct ibft_string_block *strings,
391 struct iscsi_session *iscsi ) {
392 struct sockaddr_in *sin_target =
393 ( struct sockaddr_in * ) &iscsi->target_sockaddr;
396 /* Fill in Target values */
397 ibft_set_ipaddr ( &target->ip_address, sin_target->sin_addr );
398 DBG ( "iBFT target IP = %s\n", ibft_ipaddr ( &target->ip_address ) );
399 target->socket = ntohs ( sin_target->sin_port );
400 DBG ( "iBFT target port = %d\n", target->socket );
401 if ( ( rc = ibft_set_string ( strings, &target->target_name,
402 iscsi->target_iqn ) ) != 0 )
404 DBG ( "iBFT target name = %s\n",
405 ibft_string ( strings, &target->target_name ) );
406 if ( ( rc = ibft_fill_target_chap ( target, strings, iscsi ) ) != 0 )
408 if ( ( rc = ibft_fill_target_reverse_chap ( target, strings,
416 * Fill in all variable portions of iBFT
418 * @v netdev Network device
419 * @v initiator_iqn Initiator IQN
420 * @v st_target Target socket address
421 * @v target_iqn Target IQN
422 * @ret rc Return status code
425 int ibft_fill_data ( struct net_device *netdev,
426 struct iscsi_session *iscsi ) {
427 struct ibft_string_block strings = {
428 .table = &ibftab.table,
429 .offset = offsetof ( typeof ( ibftab ), strings ),
433 /* Fill in NIC, Initiator and Target portions */
434 if ( ( rc = ibft_fill_nic ( &ibftab.nic, &strings, netdev ) ) != 0 )
436 if ( ( rc = ibft_fill_initiator ( &ibftab.initiator,
439 if ( ( rc = ibft_fill_target ( &ibftab.target, &strings,
443 /* Update checksum */
444 acpi_fix_checksum ( &ibftab.table.acpi );