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 FILE_LICENCE ( BSD2 );
29 
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <byteswap.h>
35 #include <realmode.h>
36 #include <gpxe/pci.h>
37 #include <gpxe/acpi.h>
38 #include <gpxe/in.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>
44 
45 /** @file
46  *
47  * iSCSI boot firmware table
48  *
49  * The information in this file is derived from the document "iSCSI
50  * Boot Firmware Table (iBFT)" as published by IBM at
51  *
52  * ftp://ftp.software.ibm.com/systems/support/system_x_pdf/ibm_iscsi_boot_firmware_table_v1.02.pdf
53  *
54  */
55 
56 #define ibftab __use_data16 ( ibftab )
57 /** The iBFT used by gPXE */
58 struct gpxe_ibft __data16 ( ibftab ) = {
59 	/* Table header */
60 	.table = {
61 		/* ACPI header */
62 		.acpi = {
63 			.signature = IBFT_SIG,
64 			.length = sizeof ( ibftab ),
65 			.revision = 1,
66 			.oem_id = "FENSYS",
67 			.oem_table_id = "gPXE",
68 		},
69 		/* Control block */
70 		.control = {
71 			.header = {
72 				.structure_id = IBFT_STRUCTURE_ID_CONTROL,
73 				.version = 1,
74 				.length = sizeof ( ibftab.table.control ),
75 				.flags = 0,
76 			},
77 			.initiator = offsetof ( typeof ( ibftab ), initiator ),
78 			.nic_0 = offsetof ( typeof ( ibftab ), nic ),
79 			.target_0 = offsetof ( typeof ( ibftab ), target ),
80 		},
81 	},
82 	/* iSCSI initiator information */
83 	.initiator = {
84 		.header = {
85 			.structure_id = IBFT_STRUCTURE_ID_INITIATOR,
86 			.version = 1,
87 			.length = sizeof ( ibftab.initiator ),
88 			.flags = ( IBFT_FL_INITIATOR_BLOCK_VALID |
89 				   IBFT_FL_INITIATOR_FIRMWARE_BOOT_SELECTED ),
90 		},
91 	},
92 	/* NIC information */
93 	.nic = {
94 		.header = {
95 			.structure_id = IBFT_STRUCTURE_ID_NIC,
96 			.version = 1,
97 			.length = sizeof ( ibftab.nic ),
98 			.flags = ( IBFT_FL_NIC_BLOCK_VALID |
99 				   IBFT_FL_NIC_FIRMWARE_BOOT_SELECTED ),
100 		},
101 	},
102 	/* iSCSI target information */
103 	.target = {
104 		.header = {
105 			.structure_id = IBFT_STRUCTURE_ID_TARGET,
106 			.version = 1,
107 			.length = sizeof ( ibftab.target ),
108 			.flags = ( IBFT_FL_TARGET_BLOCK_VALID |
109 				   IBFT_FL_TARGET_FIRMWARE_BOOT_SELECTED ),
110 		},
111 	},
112 };
113 
114 /**
115  * Fill in an IP address field within iBFT
116  *
117  * @v ipaddr		IP address field
118  * @v in		IPv4 address
119  */
ibft_set_ipaddr(struct ibft_ipaddr * ipaddr,struct in_addr in)120 static void ibft_set_ipaddr ( struct ibft_ipaddr *ipaddr, struct in_addr in ) {
121 	memset ( ipaddr, 0, sizeof ( ipaddr ) );
122 	if ( in.s_addr ) {
123 		ipaddr->in = in;
124 		ipaddr->ones = 0xffff;
125 	}
126 }
127 
128 /**
129  * Fill in an IP address within iBFT from configuration setting
130  *
131  * @v ipaddr		IP address field
132  * @v setting		Configuration setting
133  * @v tag		DHCP option tag
134  */
ibft_set_ipaddr_option(struct ibft_ipaddr * ipaddr,struct setting * setting)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 );
140 }
141 
142 /**
143  * Read IP address from iBFT (for debugging)
144  *
145  * @v strings		iBFT string block descriptor
146  * @v string		String field
147  * @ret ipaddr		IP address string
148  */
ibft_ipaddr(struct ibft_ipaddr * ipaddr)149 static const char * ibft_ipaddr ( struct ibft_ipaddr *ipaddr ) {
150 	return inet_ntoa ( ipaddr->in );
151 }
152 
153 /**
154  * Allocate a string within iBFT
155  *
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
160  */
ibft_alloc_string(struct ibft_string_block * strings,struct ibft_string * string,size_t len)161 static int ibft_alloc_string ( struct ibft_string_block *strings,
162 			       struct ibft_string *string, size_t len ) {
163 	char *dest;
164 	unsigned int remaining;
165 
166 	dest = ( ( ( char * ) strings->table ) + strings->offset );
167 	remaining = ( strings->table->acpi.length - strings->offset );
168 	if ( len >= remaining )
169 		return -ENOMEM;
170 
171 	string->offset = strings->offset;
172 	string->length = len;
173 	strings->offset += ( len + 1 );
174 	return 0;
175 }
176 
177 /**
178  * Fill in a string field within iBFT
179  *
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
184  */
ibft_set_string(struct ibft_string_block * strings,struct ibft_string * string,const char * data)185 static int ibft_set_string ( struct ibft_string_block *strings,
186 			     struct ibft_string *string, const char *data ) {
187 	char *dest;
188 	int rc;
189 
190 	if ( ! data )
191 		return 0;
192 
193 	if ( ( rc = ibft_alloc_string ( strings, string,
194 					strlen ( data ) ) ) != 0 )
195 		return rc;
196 	dest = ( ( ( char * ) strings->table ) + string->offset );
197 	strcpy ( dest, data );
198 
199 	return 0;
200 }
201 
202 /**
203  * Fill in a string field within iBFT from configuration setting
204  *
205  * @v strings		iBFT string block descriptor
206  * @v string		String field
207  * @v setting		Configuration setting
208  * @ret rc		Return status code
209  */
ibft_set_string_option(struct ibft_string_block * strings,struct ibft_string * string,struct setting * setting)210 static int ibft_set_string_option ( struct ibft_string_block *strings,
211 				    struct ibft_string *string,
212 				    struct setting *setting ) {
213 	int len;
214 	char *dest;
215 	int rc;
216 
217 	len = fetch_setting_len ( NULL, setting );
218 	if ( len < 0 ) {
219 		string->offset = 0;
220 		string->length = 0;
221 		return 0;
222 	}
223 
224 	if ( ( rc = ibft_alloc_string ( strings, string, len ) ) != 0 )
225 		return rc;
226 	dest = ( ( ( char * ) strings->table ) + string->offset );
227 	fetch_string_setting ( NULL, setting, dest, ( len + 1 ) );
228 	return 0;
229 }
230 
231 /**
232  * Read string from iBFT (for debugging)
233  *
234  * @v strings		iBFT string block descriptor
235  * @v string		String field
236  * @ret data		String content (or "<empty>")
237  */
ibft_string(struct ibft_string_block * strings,struct ibft_string * string)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 );
242 }
243 
244 /**
245  * Fill in NIC portion of iBFT
246  *
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
251  */
ibft_fill_nic(struct ibft_nic * nic,struct ibft_string_block * strings,struct net_device * netdev)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;
258 	int rc;
259 
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 )
269 		return rc;
270 	DBG ( "iBFT NIC hostname = %s\n",
271 	      ibft_string ( strings, &nic->hostname ) );
272 
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 )
277 			netmask_count++;
278 		netmask_addr.s_addr >>= 1;
279 	}
280 	nic->subnet_mask_prefix = netmask_count;
281 	DBG ( "iBFT NIC subnet = /%d\n", nic->subnet_mask_prefix );
282 
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 ) );
287 		return rc;
288 	}
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 );
292 
293 	return 0;
294 }
295 
296 /**
297  * Fill in Initiator portion of iBFT
298  *
299  * @v initiator		Initiator portion of iBFT
300  * @v strings		iBFT string block descriptor
301  * @ret rc		Return status code
302  */
ibft_fill_initiator(struct ibft_initiator * initiator,struct ibft_string_block * strings)303 static int ibft_fill_initiator ( struct ibft_initiator *initiator,
304 				 struct ibft_string_block *strings ) {
305 	const char *initiator_iqn = iscsi_initiator_iqn();
306 	int rc;
307 
308 	if ( ( rc = ibft_set_string ( strings, &initiator->initiator_name,
309 				      initiator_iqn ) ) != 0 )
310 		return rc;
311 	DBG ( "iBFT initiator hostname = %s\n",
312 	      ibft_string ( strings, &initiator->initiator_name ) );
313 
314 	return 0;
315 }
316 
317 /**
318  * Fill in Target CHAP portion of iBFT
319  *
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
324  */
ibft_fill_target_chap(struct ibft_target * target,struct ibft_string_block * strings,struct iscsi_session * iscsi)325 static int ibft_fill_target_chap ( struct ibft_target *target,
326 				   struct ibft_string_block *strings,
327 				   struct iscsi_session *iscsi ) {
328 	int rc;
329 
330 	if ( ! ( iscsi->status & ISCSI_STATUS_AUTH_FORWARD_REQUIRED ) )
331 		return 0;
332 
333 	assert ( iscsi->initiator_username );
334 	assert ( iscsi->initiator_password );
335 
336 	target->chap_type = IBFT_CHAP_ONE_WAY;
337 	if ( ( rc = ibft_set_string ( strings, &target->chap_name,
338 				      iscsi->initiator_username ) ) != 0 )
339 		return rc;
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 )
344 		return rc;
345 	DBG ( "iBFT target password = <redacted>\n" );
346 
347 	return 0;
348 }
349 
350 /**
351  * Fill in Target Reverse CHAP portion of iBFT
352  *
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
357  */
ibft_fill_target_reverse_chap(struct ibft_target * target,struct ibft_string_block * strings,struct iscsi_session * iscsi)358 static int ibft_fill_target_reverse_chap ( struct ibft_target *target,
359 					   struct ibft_string_block *strings,
360 					   struct iscsi_session *iscsi ) {
361 	int rc;
362 
363 	if ( ! ( iscsi->status & ISCSI_STATUS_AUTH_REVERSE_REQUIRED ) )
364 		return 0;
365 
366 	assert ( iscsi->initiator_username );
367 	assert ( iscsi->initiator_password );
368 	assert ( iscsi->target_username );
369 	assert ( iscsi->target_password );
370 
371 	target->chap_type = IBFT_CHAP_MUTUAL;
372 	if ( ( rc = ibft_set_string ( strings, &target->reverse_chap_name,
373 				      iscsi->target_username ) ) != 0 )
374 		return rc;
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 )
379 		return rc;
380 	DBG ( "iBFT target reverse password = <redacted>\n" );
381 
382 	return 0;
383 }
384 
385 /**
386  * Fill in Target portion of iBFT
387  *
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
392  */
ibft_fill_target(struct ibft_target * target,struct ibft_string_block * strings,struct iscsi_session * iscsi)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;
398 	int rc;
399 
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 )
407 		return rc;
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 )
411 		return rc;
412 	if ( ( rc = ibft_fill_target_reverse_chap ( target, strings,
413 						    iscsi ) ) != 0 )
414 		return rc;
415 
416 	return 0;
417 }
418 
419 /**
420  * Fill in all variable portions of iBFT
421  *
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
427  *
428  */
ibft_fill_data(struct net_device * netdev,struct iscsi_session * iscsi)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 ),
434 	};
435 	int rc;
436 
437 	/* Fill in NIC, Initiator and Target portions */
438 	if ( ( rc = ibft_fill_nic ( &ibftab.nic, &strings, netdev ) ) != 0 )
439 		return rc;
440 	if ( ( rc = ibft_fill_initiator ( &ibftab.initiator,
441 					  &strings ) ) != 0 )
442 		return rc;
443 	if ( ( rc = ibft_fill_target ( &ibftab.target, &strings,
444 				       iscsi ) ) != 0 )
445 		return rc;
446 
447 	/* Update checksum */
448 	acpi_fix_checksum ( &ibftab.table.acpi );
449 
450 	return 0;
451 }
452