Added netdev_nullify to natsemi_remove()
[gpxe.git] / src / core / ibft.c
blob5f033672400f09dc675fbb667096700cf3a1ee6f
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.
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
24 * SOFTWARE.
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <byteswap.h>
33 #include <realmode.h>
34 #include <gpxe/pci.h>
35 #include <gpxe/acpi.h>
36 #include <gpxe/in.h>
37 #include <gpxe/netdevice.h>
38 #include <gpxe/dhcp.h>
39 #include <gpxe/iscsi.h>
40 #include <gpxe/ibft.h>
42 /** @file
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 ) = {
56 /* Table header */
57 .table = {
58 /* ACPI header */
59 .acpi = {
60 .signature = IBFT_SIG,
61 .length = sizeof ( ibftab ),
62 .revision = 1,
63 .oem_id = "FENSYS",
64 .oem_table_id = "gPXE",
66 /* Control block */
67 .control = {
68 .header = {
69 .structure_id = IBFT_STRUCTURE_ID_CONTROL,
70 .version = 1,
71 .length = sizeof ( ibftab.table.control ),
72 .flags = 0,
74 .initiator = offsetof ( typeof ( ibftab ), initiator ),
75 .nic_0 = offsetof ( typeof ( ibftab ), nic ),
76 .target_0 = offsetof ( typeof ( ibftab ), target ),
79 /* iSCSI initiator information */
80 .initiator = {
81 .header = {
82 .structure_id = IBFT_STRUCTURE_ID_INITIATOR,
83 .version = 1,
84 .length = sizeof ( ibftab.initiator ),
85 .flags = ( IBFT_FL_INITIATOR_BLOCK_VALID |
86 IBFT_FL_INITIATOR_FIRMWARE_BOOT_SELECTED ),
89 /* NIC information */
90 .nic = {
91 .header = {
92 .structure_id = IBFT_STRUCTURE_ID_NIC,
93 .version = 1,
94 .length = sizeof ( ibftab.nic ),
95 .flags = ( IBFT_FL_NIC_BLOCK_VALID |
96 IBFT_FL_NIC_FIRMWARE_BOOT_SELECTED ),
99 /* iSCSI target information */
100 .target = {
101 .header = {
102 .structure_id = IBFT_STRUCTURE_ID_TARGET,
103 .version = 1,
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
115 * @v in IPv4 address
117 static void ibft_set_ipaddr ( struct ibft_ipaddr *ipaddr, struct in_addr in ) {
118 memset ( ipaddr, 0, sizeof ( ipaddr ) );
119 if ( in.s_addr ) {
120 ipaddr->in = in;
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,
132 unsigned int tag ) {
133 struct in_addr in;
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 ) {
150 char *dest;
151 char *end;
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 )
159 return -ENOMEM;
161 memcpy ( dest, data, len );
162 dest[len] = '\0';
164 string->offset = strings->offset;
165 string->length = len;
166 strings->offset += ( len + 1 );
167 return 0;
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,
180 unsigned int tag ) {
181 struct dhcp_option *option;
183 option = find_global_dhcp_option ( tag );
184 if ( ! option ) {
185 string->offset = 0;
186 string->length = 0;
187 return 0;
190 return ibft_set_string ( strings, string, option->data.string,
191 option->len );
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;
207 int rc;
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 )
215 return rc;
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 )
221 netmask_count++;
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;
231 return 0;
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();
244 int rc;
246 if ( ( rc = ibft_set_string ( strings, &initiator->initiator_name,
247 initiator_iqn,
248 strlen ( initiator_iqn ) ) ) != 0)
249 return rc;
251 return 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;
267 int rc;
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,
273 iscsi->target_iqn,
274 strlen ( iscsi->target_iqn ) ) ) != 0 )
275 return rc;
276 if ( iscsi->username ) {
277 if ( ( rc = ibft_set_string ( strings, &target->chap_name,
278 iscsi->username,
279 strlen ( iscsi->username ) ))!=0)
280 return rc;
282 if ( iscsi->password ) {
283 if ( ( rc = ibft_set_string ( strings, &target->chap_secret,
284 iscsi->password,
285 strlen ( iscsi->password ) ))!=0)
286 return rc;
287 target->chap_type = IBFT_CHAP_ONE_WAY;
290 return 0;
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 ),
309 int rc;
311 /* Fill in NIC, Initiator and Target portions */
312 if ( ( rc = ibft_fill_nic ( &ibftab.nic, &strings, netdev ) ) != 0 )
313 return rc;
314 if ( ( rc = ibft_fill_initiator ( &ibftab.initiator,
315 &strings ) ) != 0 )
316 return rc;
317 if ( ( rc = ibft_fill_target ( &ibftab.target, &strings,
318 iscsi ) ) != 0 )
319 return rc;
321 /* Update checksum */
322 acpi_fix_checksum ( &ibftab.table.acpi );
324 return 0;