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
35 #include <gpxe/acpi.h>
37 #include <gpxe/netdevice.h>
38 #include <gpxe/dhcp.h>
39 #include <gpxe/iscsi.h>
40 #include <gpxe/ibft.h>
44 * iSCSI boot firmware table
46 * The information in this file is derived from the document "iSCSI
47 * Boot Firmware Table (iBFT)" as published by IBM at
49 * ftp://ftp.software.ibm.com/systems/support/system_x_pdf/ibm_iscsi_boot_firmware_table_v1.02.pdf
53 #define ibftab __use_data16 ( ibftab )
54 /** The iBFT used by gPXE */
55 struct gpxe_ibft
__data16 ( ibftab
) = {
60 .signature
= IBFT_SIG
,
61 .length
= sizeof ( ibftab
),
64 .oem_table_id
= "gPXE",
69 .structure_id
= IBFT_STRUCTURE_ID_CONTROL
,
71 .length
= sizeof ( ibftab
.table
.control
),
74 .initiator
= offsetof ( typeof ( ibftab
), initiator
),
75 .nic_0
= offsetof ( typeof ( ibftab
), nic
),
76 .target_0
= offsetof ( typeof ( ibftab
), target
),
79 /* iSCSI initiator information */
82 .structure_id
= IBFT_STRUCTURE_ID_INITIATOR
,
84 .length
= sizeof ( ibftab
.initiator
),
85 .flags
= ( IBFT_FL_INITIATOR_BLOCK_VALID
|
86 IBFT_FL_INITIATOR_FIRMWARE_BOOT_SELECTED
),
92 .structure_id
= IBFT_STRUCTURE_ID_NIC
,
94 .length
= sizeof ( ibftab
.nic
),
95 .flags
= ( IBFT_FL_NIC_BLOCK_VALID
|
96 IBFT_FL_NIC_FIRMWARE_BOOT_SELECTED
),
99 /* iSCSI target information */
102 .structure_id
= IBFT_STRUCTURE_ID_TARGET
,
104 .length
= sizeof ( ibftab
.target
),
105 .flags
= ( IBFT_FL_TARGET_BLOCK_VALID
|
106 IBFT_FL_TARGET_FIRMWARE_BOOT_SELECTED
),
112 * Fill in an IP address field within iBFT
114 * @v ipaddr IP address field
117 static void ibft_set_ipaddr ( struct ibft_ipaddr
*ipaddr
, struct in_addr in
) {
118 memset ( ipaddr
, 0, sizeof ( ipaddr
) );
121 ipaddr
->ones
= 0xffff;
126 * Fill in an IP address within iBFT from DHCP option
128 * @v ipaddr IP address field
129 * @v tag DHCP option tag
131 static void ibft_set_ipaddr_option ( struct ibft_ipaddr
*ipaddr
,
134 find_global_dhcp_ipv4_option ( tag
, &in
);
135 ibft_set_ipaddr ( ipaddr
, in
);
139 * Fill in a string field within iBFT
141 * @v strings iBFT string block descriptor
142 * @v string String field
143 * @v data String to fill in
144 * @v len Length of string to fill in
145 * @ret rc Return status code
147 static int ibft_set_string ( struct ibft_string_block
*strings
,
148 struct ibft_string
*string
,
149 const void *data
, size_t len
) {
152 unsigned int remaining
;
154 dest
= ( ( ( char * ) strings
->table
) + strings
->offset
);
155 end
= ( ( ( char * ) strings
->table
) + strings
->table
->acpi
.length
);
156 remaining
= ( end
- dest
);
158 if ( len
>= remaining
)
161 memcpy ( dest
, data
, len
);
164 string
->offset
= strings
->offset
;
165 string
->length
= len
;
166 strings
->offset
+= ( len
+ 1 );
171 * Fill in a string field within iBFT from DHCP option
173 * @v strings iBFT string block descriptor
174 * @v string String field
175 * @v tag DHCP option tag
176 * @ret rc Return status code
178 static int ibft_set_string_option ( struct ibft_string_block
*strings
,
179 struct ibft_string
*string
,
181 struct dhcp_option
*option
;
183 option
= find_global_dhcp_option ( tag
);
190 return ibft_set_string ( strings
, string
, option
->data
.string
,
195 * Fill in NIC portion of iBFT
197 * @v nic NIC portion of iBFT
198 * @v strings iBFT string block descriptor
199 * @v netdev Network device
200 * @ret rc Return status code
202 static int ibft_fill_nic ( struct ibft_nic
*nic
,
203 struct ibft_string_block
*strings
,
204 struct net_device
*netdev
) {
205 struct in_addr netmask_addr
;
206 unsigned int netmask_count
= 0;
209 /* Extract values from DHCP configuration */
210 ibft_set_ipaddr_option ( &nic
->ip_address
, DHCP_EB_YIADDR
);
211 ibft_set_ipaddr_option ( &nic
->gateway
, DHCP_ROUTERS
);
212 ibft_set_ipaddr_option ( &nic
->dns
[0], DHCP_DNS_SERVERS
);
213 if ( ( rc
= ibft_set_string_option ( strings
, &nic
->hostname
,
214 DHCP_HOST_NAME
) ) != 0 )
217 /* Derive subnet mask prefix from subnet mask */
218 find_global_dhcp_ipv4_option ( DHCP_SUBNET_MASK
, &netmask_addr
);
219 while ( netmask_addr
.s_addr
) {
220 if ( netmask_addr
.s_addr
& 0x1 )
222 netmask_addr
.s_addr
>>= 1;
224 nic
->subnet_mask_prefix
= netmask_count
;
226 /* Extract values from net-device configuration */
227 memcpy ( nic
->mac_address
, netdev
->ll_addr
,
228 sizeof ( nic
->mac_address
) );
229 nic
->pci_bus_dev_func
= netdev
->dev
->desc
.location
;
235 * Fill in Initiator portion of iBFT
237 * @v initiator Initiator portion of iBFT
238 * @v strings iBFT string block descriptor
239 * @ret rc Return status code
241 static int ibft_fill_initiator ( struct ibft_initiator
*initiator
,
242 struct ibft_string_block
*strings
) {
243 const char *initiator_iqn
= iscsi_initiator_iqn();
246 if ( ( rc
= ibft_set_string ( strings
, &initiator
->initiator_name
,
248 strlen ( initiator_iqn
) ) ) != 0)
255 * Fill in Target portion of iBFT
257 * @v target Target portion of iBFT
258 * @v strings iBFT string block descriptor
259 * @v iscsi iSCSI session
260 * @ret rc Return status code
262 static int ibft_fill_target ( struct ibft_target
*target
,
263 struct ibft_string_block
*strings
,
264 struct iscsi_session
*iscsi
) {
265 struct sockaddr_in
*sin_target
=
266 ( struct sockaddr_in
* ) &iscsi
->target_sockaddr
;
269 /* Fill in Target values */
270 ibft_set_ipaddr ( &target
->ip_address
, sin_target
->sin_addr
);
271 target
->socket
= ntohs ( sin_target
->sin_port
);
272 if ( ( rc
= ibft_set_string ( strings
, &target
->target_name
,
274 strlen ( iscsi
->target_iqn
) ) ) != 0 )
276 if ( iscsi
->username
) {
277 if ( ( rc
= ibft_set_string ( strings
, &target
->chap_name
,
279 strlen ( iscsi
->username
) ))!=0)
282 if ( iscsi
->password
) {
283 if ( ( rc
= ibft_set_string ( strings
, &target
->chap_secret
,
285 strlen ( iscsi
->password
) ))!=0)
287 target
->chap_type
= IBFT_CHAP_ONE_WAY
;
294 * Fill in all variable portions of iBFT
296 * @v netdev Network device
297 * @v initiator_iqn Initiator IQN
298 * @v st_target Target socket address
299 * @v target_iqn Target IQN
300 * @ret rc Return status code
303 int ibft_fill_data ( struct net_device
*netdev
,
304 struct iscsi_session
*iscsi
) {
305 struct ibft_string_block strings
= {
306 .table
= &ibftab
.table
,
307 .offset
= offsetof ( typeof ( ibftab
), strings
),
311 /* Fill in NIC, Initiator and Target portions */
312 if ( ( rc
= ibft_fill_nic ( &ibftab
.nic
, &strings
, netdev
) ) != 0 )
314 if ( ( rc
= ibft_fill_initiator ( &ibftab
.initiator
,
317 if ( ( rc
= ibft_fill_target ( &ibftab
.target
, &strings
,
321 /* Update checksum */
322 acpi_fix_checksum ( &ibftab
.table
.acpi
);