|
@@ -0,0 +1,325 @@
|
|
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.
|
|
5
|
+ *
|
|
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:
|
|
13
|
+ *
|
|
14
|
+ * The above copyright notice and this permission notice shall be
|
|
15
|
+ * included in all copies or substantial portions of the Software.
|
|
16
|
+ *
|
|
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.
|
|
25
|
+ *
|
|
26
|
+ */
|
|
27
|
+
|
|
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>
|
|
41
|
+
|
|
42
|
+/** @file
|
|
43
|
+ *
|
|
44
|
+ * iSCSI boot firmware table
|
|
45
|
+ *
|
|
46
|
+ * The information in this file is derived from the document "iSCSI
|
|
47
|
+ * Boot Firmware Table (iBFT)" as published by IBM at
|
|
48
|
+ *
|
|
49
|
+ * ftp://ftp.software.ibm.com/systems/support/system_x_pdf/ibm_iscsi_boot_firmware_table_v1.02.pdf
|
|
50
|
+ *
|
|
51
|
+ */
|
|
52
|
+
|
|
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",
|
|
65
|
+ },
|
|
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,
|
|
73
|
+ },
|
|
74
|
+ .initiator = offsetof ( typeof ( ibftab ), initiator ),
|
|
75
|
+ .nic_0 = offsetof ( typeof ( ibftab ), nic ),
|
|
76
|
+ .target_0 = offsetof ( typeof ( ibftab ), target ),
|
|
77
|
+ },
|
|
78
|
+ },
|
|
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 ),
|
|
87
|
+ },
|
|
88
|
+ },
|
|
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 ),
|
|
97
|
+ },
|
|
98
|
+ },
|
|
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 ),
|
|
107
|
+ },
|
|
108
|
+ },
|
|
109
|
+};
|
|
110
|
+
|
|
111
|
+/**
|
|
112
|
+ * Fill in an IP address field within iBFT
|
|
113
|
+ *
|
|
114
|
+ * @v ipaddr IP address field
|
|
115
|
+ * @v in IPv4 address
|
|
116
|
+ */
|
|
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;
|
|
122
|
+ }
|
|
123
|
+}
|
|
124
|
+
|
|
125
|
+/**
|
|
126
|
+ * Fill in an IP address within iBFT from DHCP option
|
|
127
|
+ *
|
|
128
|
+ * @v ipaddr IP address field
|
|
129
|
+ * @v tag DHCP option tag
|
|
130
|
+ */
|
|
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 );
|
|
136
|
+}
|
|
137
|
+
|
|
138
|
+/**
|
|
139
|
+ * Fill in a string field within iBFT
|
|
140
|
+ *
|
|
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
|
|
146
|
+ */
|
|
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;
|
|
153
|
+
|
|
154
|
+ dest = ( ( ( char * ) strings->table ) + strings->offset );
|
|
155
|
+ end = ( ( ( char * ) strings->table ) + strings->table->acpi.length );
|
|
156
|
+ remaining = ( end - dest );
|
|
157
|
+
|
|
158
|
+ if ( len >= remaining )
|
|
159
|
+ return -ENOMEM;
|
|
160
|
+
|
|
161
|
+ memcpy ( dest, data, len );
|
|
162
|
+ dest[len] = '\0';
|
|
163
|
+
|
|
164
|
+ string->offset = strings->offset;
|
|
165
|
+ string->length = len;
|
|
166
|
+ strings->offset += ( len + 1 );
|
|
167
|
+ return 0;
|
|
168
|
+}
|
|
169
|
+
|
|
170
|
+/**
|
|
171
|
+ * Fill in a string field within iBFT from DHCP option
|
|
172
|
+ *
|
|
173
|
+ * @v strings iBFT string block descriptor
|
|
174
|
+ * @v string String field
|
|
175
|
+ * @v tag DHCP option tag
|
|
176
|
+ * @ret rc Return status code
|
|
177
|
+ */
|
|
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;
|
|
182
|
+
|
|
183
|
+ option = find_global_dhcp_option ( tag );
|
|
184
|
+ if ( ! option ) {
|
|
185
|
+ string->offset = 0;
|
|
186
|
+ string->length = 0;
|
|
187
|
+ return 0;
|
|
188
|
+ }
|
|
189
|
+
|
|
190
|
+ return ibft_set_string ( strings, string, option->data.string,
|
|
191
|
+ option->len );
|
|
192
|
+}
|
|
193
|
+
|
|
194
|
+/**
|
|
195
|
+ * Fill in NIC portion of iBFT
|
|
196
|
+ *
|
|
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
|
|
201
|
+ */
|
|
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;
|
|
208
|
+
|
|
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;
|
|
216
|
+
|
|
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;
|
|
223
|
+ }
|
|
224
|
+ nic->subnet_mask_prefix = netmask_count;
|
|
225
|
+
|
|
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;
|
|
230
|
+
|
|
231
|
+ return 0;
|
|
232
|
+}
|
|
233
|
+
|
|
234
|
+/**
|
|
235
|
+ * Fill in Initiator portion of iBFT
|
|
236
|
+ *
|
|
237
|
+ * @v initiator Initiator portion of iBFT
|
|
238
|
+ * @v strings iBFT string block descriptor
|
|
239
|
+ * @ret rc Return status code
|
|
240
|
+ */
|
|
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;
|
|
245
|
+
|
|
246
|
+ if ( ( rc = ibft_set_string ( strings, &initiator->initiator_name,
|
|
247
|
+ initiator_iqn,
|
|
248
|
+ strlen ( initiator_iqn ) ) ) != 0)
|
|
249
|
+ return rc;
|
|
250
|
+
|
|
251
|
+ return 0;
|
|
252
|
+}
|
|
253
|
+
|
|
254
|
+/**
|
|
255
|
+ * Fill in Target portion of iBFT
|
|
256
|
+ *
|
|
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
|
|
261
|
+ */
|
|
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;
|
|
268
|
+
|
|
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;
|
|
281
|
+ }
|
|
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;
|
|
288
|
+ }
|
|
289
|
+
|
|
290
|
+ return 0;
|
|
291
|
+}
|
|
292
|
+
|
|
293
|
+/**
|
|
294
|
+ * Fill in all variable portions of iBFT
|
|
295
|
+ *
|
|
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
|
|
301
|
+ *
|
|
302
|
+ */
|
|
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 ),
|
|
308
|
+ };
|
|
309
|
+ int rc;
|
|
310
|
+
|
|
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;
|
|
320
|
+
|
|
321
|
+ /* Update checksum */
|
|
322
|
+ acpi_fix_checksum ( &ibftab.table.acpi );
|
|
323
|
+
|
|
324
|
+ return 0;
|
|
325
|
+}
|