summaryrefslogtreecommitdiff
path: root/utils/fwparam_ibft/fwparam_ppc.c
blob: da9d76ed97d4bff9bc112132e28a962c6d591f7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/*
 * Copyright (C) IBM Corporation. 2007
 * Author: Doug Maxey <dwm@austin.ibm.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#define	 _XOPEN_SOURCE 500
#include <ftw.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

#include "fwparam.h"
#include "fw_context.h"
#include "iscsi_obp.h"
#include "prom_parse.h"
#include "sysdeps.h"
#include "iscsi_err.h"

void* yy_scan_string(const char *str);
int yyparse(struct ofw_dev *ofwdev);

#define BOOTPATH "/chosen/bootpath"
#define DT_TOP "/proc/device-tree"
#define LOCAL_MAC_FILE "/local-mac-address"

static int devtree_offset;
static char *bootpath_val;
static int bytes_read;
#define OFWDEV_MAX (10)
static struct ofw_dev *ofwdevs[OFWDEV_MAX];
static char *niclist[OFWDEV_MAX];
static int nic_count;
static int debug;
static int dev_count;

static void cp_param(char *dest, const char *name, struct ofw_dev *dev,
		     enum obp_param item, int len)
{
	if (dev->param[item])
		strlcpy(dest, dev->param[item]->val, len);
}

static void cp_int_param(int *dest, const char *name, struct ofw_dev *dev,
			 enum obp_param item)
{
	if (dev->param[item])
		*dest = strtol(dev->param[item]->val, NULL, 10);
}

static char *find_devtree(const char *filename)
{
	char *devtree = strdup(filename);
	char *chop_at;
	struct stat dt_stat;
	int error;

	/*
	 * What is the path to the device-tree?	 The only valid
	 * directories to locate the property are under /aliases or
	 * /chosen.
	 */

	if (!devtree)
		return NULL;

	chop_at = strstr(devtree, "/chosen");
	if (!chop_at)
		chop_at = strstr(devtree, "/aliases");

	if (!chop_at) {
		char *vdev = malloc(strlen(filename) + strlen("/vdevice") + 1);

		/*
		 * test to see if there is /vdevice dir
		 */
		if (vdev) {
			sprintf(vdev, "%s%s", filename, "/vdevice");
			error = stat(vdev, &dt_stat);
			free(vdev);
			if (error) {
				free(devtree);
				return NULL;
			}
		}
	} else
		devtree[chop_at - devtree] = 0;

	if (devtree)
		devtree_offset = strlen(devtree);

	return devtree;
}

/*
 * Take the path to the property under chosen, and swizzle to make that
 * the base for the device path discovered.
 */
static int locate_mac(const char *devtree, struct ofw_dev *ofwdev)
{
	int error = 0;
	int mac_path_len = strlen(ofwdev->dev_path) + strlen(LOCAL_MAC_FILE) +
		2;
	char *mac_file;
	int mac_fd;

	mac_path_len += strlen(devtree);
	mac_file = malloc(mac_path_len);
	if (!mac_file) {
		error = ENOMEM;
		fprintf(stderr, "%s: malloc , %s\n", __func__,
			strerror(errno));
		goto lpm_bail;
	}

	snprintf(mac_file, mac_path_len, "%s%s%s", devtree, ofwdev->dev_path,
		 LOCAL_MAC_FILE);
	mac_fd = open(mac_file, O_RDONLY);
	if (mac_fd < 0) {
		error = errno;
		fprintf(stderr, "%s: open %s, %s\n", __func__, mac_file,
			strerror(errno));
		free(mac_file);
		goto lpm_bail;
	}

	bytes_read = read(mac_fd, ofwdev->mac, 6);
	if (bytes_read != 6) {
		error = EIO;
		fprintf(stderr, "%s: read %s, %s\n", __func__, mac_file,
			strerror(errno));
	}
	free(mac_file);
	close(mac_fd);

lpm_bail:
	return error;
}

const char *obp_qual_set(struct ofw_dev *ofwdev, const char *qual)
{
	if (!strcmp("bootp", qual))
		ofwdev->quals[ofwdev->qual_count++] = OBP_QUAL_BOOTP;
	else if (!strcmp("dhcpv6", qual))
		ofwdev->quals[ofwdev->qual_count++] = OBP_QUAL_DHCPV6;
	else if (!strcmp("ipv6", qual))
		ofwdev->quals[ofwdev->qual_count++] = OBP_QUAL_IPV6;
	else if (!strcmp("iscsi", qual)) {
		ofwdev->type = OFW_DT_ISCSI;
		ofwdev->quals[ofwdev->qual_count++] = OBP_QUAL_ISCSI;
	} else if (!strcmp("ping", qual))
		ofwdev->quals[ofwdev->qual_count++] = OBP_QUAL_PING;
	else
		printf("%s: %s UNKNOWN\n", __func__, qual);
	return qual;
}

void add_obp_parm(struct ofw_dev *ofwdev, enum obp_param parm, const char *str)
{
	int psz = sizeof(struct ofw_obp_param) + strlen(str);

	ofwdev->param[parm] = malloc(psz);
	if (ofwdev->param[parm] == NULL) {
		printf("%s: ENOMEM!\n", __func__);
		return;
	}
	memset(ofwdev->param[parm], 0, psz);
	ofwdev->param[parm]->len = psz;
	strcpy(ofwdev->param[parm]->val, str);
}

void obp_parm_addr(struct ofw_dev *ofwdev, const char *parm, const char *addr)
{
	if (!strcmp("ciaddr", parm))
		add_obp_parm(ofwdev, OBP_PARAM_CIADDR, addr);
	else if (!strcmp("dhcp", parm))
		add_obp_parm(ofwdev, OBP_PARAM_DHCP, addr);
	else if (!strcmp("giaddr", parm))
		add_obp_parm(ofwdev, OBP_PARAM_GIADDR, addr);
	else if (!strcmp("isns", parm))
		add_obp_parm(ofwdev, OBP_PARAM_ISNS, addr);
	else if (!strcmp("siaddr", parm))
		add_obp_parm(ofwdev, OBP_PARAM_SIADDR, addr);
	else if (!strcmp("slp", parm))
		add_obp_parm(ofwdev, OBP_PARAM_SLP, addr);
	else if (!strcmp("subnet-mask", parm))
		add_obp_parm(ofwdev, OBP_PARAM_SUBNET_MASK, addr);
	else
		printf("%s: %s UNKNOWN\n", __func__, parm);
}

void obp_parm_iqn(struct ofw_dev *ofwdev, const char *parm, const char *iqn)
{
	if (!strcmp("itname", parm))
		add_obp_parm(ofwdev, OBP_PARAM_ITNAME, iqn);
	else if (!strcmp("iname", parm))
		add_obp_parm(ofwdev, OBP_PARAM_INAME, iqn);
	else
		printf("%s: %s UNKNOWN\n", __func__, parm);
}

void obp_parm_hexnum(struct ofw_dev *ofwdev, const char *parm,
		     const char *numstr)
{
	if (!strcmp("bootp-retries", parm))
		add_obp_parm(ofwdev, OBP_PARAM_BOOTP_RETRIES, numstr);
	else if (!strcmp("tftp-retries", parm))
		add_obp_parm(ofwdev, OBP_PARAM_TFTP_RETRIES, numstr);
	else if (!strcmp("iport", parm))
		add_obp_parm(ofwdev, OBP_PARAM_IPORT, numstr);
	else if (!strcmp("ilun", parm))
		add_obp_parm(ofwdev, OBP_PARAM_ILUN, numstr);
	else if (!strcmp("isid", parm))
		add_obp_parm(ofwdev, OBP_PARAM_ISID, numstr);
	else
		printf("%s: %s UNKNOWN <%s>\n", __func__, parm, numstr);
}

void obp_parm_str(struct ofw_dev *ofwdev, const char *parm, const char *str)
{
	if (!strcmp("filename", parm))
		add_obp_parm(ofwdev, OBP_PARAM_FILENAME, str);
	else if (!strcmp("ichapid", parm))
		add_obp_parm(ofwdev, OBP_PARAM_ICHAPID, str);
	else if (!strcmp("ichappw", parm))
		add_obp_parm(ofwdev, OBP_PARAM_ICHAPPW, str);
	else if (!strcmp("chapid", parm))
		add_obp_parm(ofwdev, OBP_PARAM_CHAPID, str);
	else if (!strcmp("chappw", parm))
		add_obp_parm(ofwdev, OBP_PARAM_CHAPPW, str);
	else
		printf("%s: %s UNKNOWN <%s>\n", __func__, parm, str);
}

void yyerror(struct ofw_dev *ofwdev, const char *msg)
{
	fprintf(stderr, "%s: error in <%s> at l%d.c%d\n", "fwparam_ppc",
		ofwdev->prop_path, yylloc.last_line, yylloc.last_column);
}

static int parse_params(const char *buf, struct ofw_dev *ofwdev)
{
	int error = 0;
#if YYDEBUG
	yydebug = 1;
#endif


	if (yy_scan_string(buf))
		error = yyparse(ofwdev);

	return error;
}

static int find_file(const char *filename)
{
	int error, fd;
	struct stat bootpath_stat;

	error = stat(filename, &bootpath_stat);
	if (error < 0) {
		fprintf(stderr, "%s: stat %s, %s\n", __func__, filename,
			strerror(errno));
		return error;
	}

	bootpath_val = malloc(bootpath_stat.st_size);
	if (!bootpath_val) {
		error = ENOMEM;
		fprintf(stderr, "%s: Could not open %s: %s (%d)\n",
			__func__, filename, strerror(error), error);
		return -1;
	}

	fd = open(filename, O_RDONLY);
	if (fd < 0) {
		fprintf(stderr, "%s: Could not open %s: %s (%d)\n",
			__func__, filename, strerror(errno), errno);
		free(bootpath_val);
		bootpath_val = NULL;
		return -1;
	}

	bytes_read = read(fd, bootpath_val, bootpath_stat.st_size);
	close(fd);
	if (bytes_read != bootpath_stat.st_size) {
		fprintf(stderr, "%s: Failed to read %s: %s (%d)\n",
			__func__, filename, strerror(EIO), EIO);
		free(bootpath_val);
		bootpath_val = NULL;
		return -1;
	}

	return 1;
}

static int find_nics(const char *fpath, const struct stat *sb, int tflag,
		     struct FTW *ftw)
{
	if (tflag == FTW_D &&
	    (strstr(fpath + ftw->base, "iscsi-toe") ||
	     strstr(fpath + ftw->base, "ethernet"))) {

		if (nic_count < OFWDEV_MAX)
			niclist[nic_count++] = strdup(fpath + devtree_offset);

	}
	return 0;
}

static int nic_cmp(const void *a, const void *b)
{
	return strcmp(a, b);
}

static int find_initiator(const char *fpath, const struct stat *sb, int tflag,
			  struct FTW *ftw)
{
	struct ofw_dev *dev;

	if (tflag == FTW_F && (strstr(fpath + ftw->base,
				      "/aliases/iscsi-disk"))) {

		if (dev_count < OFWDEV_MAX) {
			dev = calloc(sizeof(struct ofw_dev), 1);
			if (!dev)
				return -ENOMEM;

			dev->prop_path = strdup(fpath + devtree_offset);
			if (!dev->prop_path) {
				free(dev);
				return errno;
			}
			ofwdevs[dev_count++] = dev;
		}
	}
	return 0;
}

static int loop_devs(const char *devtree)
{
	int error;
	int i;
	char prefix[256];

	nic_count = 0;
	error = nftw(devtree, find_nics, 20, 0);
	if (error)
		return error;

	/*
	 * Sort the nics into "natural" order.	The proc fs
	 * device-tree has them in somewhat random, or reversed order.
	 */
	qsort(niclist, nic_count, sizeof(char *), nic_cmp);

	snprintf(prefix, sizeof(prefix), "%s/%s", devtree, "aliases");
	dev_count = 0;
	error = nftw(prefix, find_initiator, 20, 0);
	if (error)
		return error;

	for (i = 0; i < dev_count; i++) {
		snprintf(prefix, sizeof(prefix), "%s%s", devtree,
			 ofwdevs[i]->prop_path);
		if (find_file(prefix) > 0) {
			error = parse_params(bootpath_val, ofwdevs[i]);
			if (!error)
				error = locate_mac(devtree, ofwdevs[i]);

			free(bootpath_val);
			bootpath_val = NULL;
		}
	}
	return error;
}

#define set_context(fld,abrv,item)					\
	cp_param(context->fld, (abrv), ofwdev, (item), sizeof(context->fld))
#define set_int_context(fld,abrv,item)					    \
	cp_int_param(&context->fld, (abrv), ofwdev, (item))

static void fill_context(struct boot_context *context, struct ofw_dev *ofwdev)
{
	int ndx;

	memset(context, 0, sizeof(*context));

	set_context(initiatorname, "NAME", OBP_PARAM_ITNAME);

	snprintf(context->mac, sizeof(context->mac),
		 "%02x:%02x:%02x:%02x:%02x:%02x",
		 ofwdev->mac[0], ofwdev->mac[1], ofwdev->mac[2],
		 ofwdev->mac[3], ofwdev->mac[4], ofwdev->mac[5]);

	/*
	 * nic parameters
	 */
	for (ndx = 0; ndx < nic_count; ndx++) {
		if (!strcmp(niclist[ndx], ofwdev->dev_path)) {
			snprintf(context->iface, sizeof(context->iface),
				 "eth%d", ndx);
			break;
		}
	}

	set_context(ipaddr, "IPADDR", OBP_PARAM_CIADDR);
	set_context(mask, "MASK", OBP_PARAM_SUBNET_MASK);

	/*
	 * target parameters
	 */
	set_context(target_ipaddr, "IPADDR", OBP_PARAM_SIADDR);
	set_int_context(target_port, "PORT", OBP_PARAM_IPORT);
	set_context(lun, "LUN", OBP_PARAM_ILUN);
	set_context(targetname, "NAME", OBP_PARAM_INAME);
	set_context(isid, "ISID", OBP_PARAM_ISID);

	/*
	 * chap stuff is always associated with the target
	 */
	set_context(chap_name, "CHAP_NAME", OBP_PARAM_ICHAPID);
	set_context(chap_password, "CHAP_PASSWORD", OBP_PARAM_ICHAPPW);
	set_context(chap_name_in, "CHAP_NAME_IN", OBP_PARAM_CHAPID);
	set_context(chap_password_in, "CHAP_PASSWORD_IN", OBP_PARAM_CHAPPW);

}

int fwparam_ppc_boot_info(struct boot_context *context)
{
	char filename[FILENAMESZ];
	int error;
	char *devtree;
	int i;

	/*
	 * For powerpc, our operations are fundamentally to locate
	 * either the one boot target (the singleton disk), or to find
	 * the nics that support iscsi boot.  The only nics in IBM
	 * systems that can support iscsi are the ones that provide
	 * the appropriate FCODE with a load method.
	 */
	memset(filename, 0, FILENAMESZ);
	snprintf(filename, FILENAMESZ, "%s%s", DT_TOP, BOOTPATH);

	if (debug)
		fprintf(stderr, "%s: file:%s; debug:%d\n", __func__, filename,
			debug);

	devtree = find_devtree(filename);
	if (!devtree)
		return ISCSI_ERR_INVAL;

	/*
	 * Always search the device-tree to find the capable nic devices.
	 */
	error = loop_devs(devtree);
	if (error)
		goto free_devtree;

	if (find_file(filename) < 1) {
		error = ISCSI_ERR_NO_OBJS_FOUND;
		goto free_devtree;
	} else {
		if (debug)
			printf("%s:\n%s\n\n", filename, bootpath_val);
		/*
		 * We find *almost* everything we need in the
		 * bootpath, save the mac-address.
		 */

		if (!strstr(bootpath_val, "iscsi")) {
			error = ISCSI_ERR_INVAL;
			goto free_bootpath_val;
		}
		ofwdevs[0] = calloc(1, sizeof(struct ofw_dev));
		if (!ofwdevs[0]) {
			error = ISCSI_ERR_NOMEM;
			goto free_bootpath_val;
		}

		error = parse_params(bootpath_val, ofwdevs[0]);
		if (!error)
			error = locate_mac(devtree, ofwdevs[0]);
		if (!error) {
			if (!context)
				error = ISCSI_ERR_NOMEM;
			else
				fill_context(context, ofwdevs[0]);
		}
		free(ofwdevs[0]);
	}

free_bootpath_val:
	free(bootpath_val);
	bootpath_val = NULL;

free_devtree:
	free(devtree);
	for (i = 0; i < dev_count; i++)
		if (ofwdevs[i])
			free(ofwdevs[i]);

	return error;
}

/*
 * Due to lack of time this is just fwparam_ppc_boot_info which
 * adds the target used for boot to the list. It does not add
 * all possible targets (IBM please add).
 */
int fwparam_ppc_get_targets(struct list_head *list)
{
	char filename[FILENAMESZ];
	struct boot_context *context;
	int error;
	char *devtree;
	int i;

	/*
	 * For powerpc, our operations are fundamentally to locate
	 * either the one boot target (the singleton disk), or to find
	 * the nics that support iscsi boot.  The only nics in IBM
	 * systems that can support iscsi are the ones that provide
	 * the appropriate FCODE with a load method.
	 */
	memset(filename, 0, FILENAMESZ);
	snprintf(filename, FILENAMESZ, "%s%s", DT_TOP, BOOTPATH);

	if (debug)
		fprintf(stderr, "%s: file:%s; debug:%d\n", __func__, filename,
			debug);

	devtree = find_devtree(filename);
	if (!devtree)
		return ISCSI_ERR_INVAL;

	/*
	 * Always search the device-tree to find the capable nic devices.
	 */
	error = loop_devs(devtree);
	if (error)
		goto free_devtree;

	if (find_file(filename) < 1) {
		error = ISCSI_ERR_NO_OBJS_FOUND;
		goto free_devtree;
	} else {
		if (debug)
			printf("%s:\n%s\n\n", filename, bootpath_val);
		/*
		 * We find *almost* everything we need in the
		 * bootpath, save the mac-address.
		 */

		if (!strstr(bootpath_val, "iscsi")) {
			error = ISCSI_ERR_INVAL;
			goto free_bootpath_val;
		}
		ofwdevs[0] = calloc(1, sizeof(struct ofw_dev));
		if (!ofwdevs[0]) {
			error = ISCSI_ERR_NOMEM;
			goto free_bootpath_val;
		}

		error = parse_params(bootpath_val, ofwdevs[0]);
		if (!error)
			error = locate_mac(devtree, ofwdevs[0]);
		if (!error) {
			context = calloc(1, sizeof(*context));
			if (!context)
				error = ISCSI_ERR_NOMEM;
			else {
				fill_context(context, ofwdevs[0]);
				list_add_tail(&context->list, list);
			}
		}
		free(ofwdevs[0]);
	}
free_bootpath_val:
	free(bootpath_val);
	bootpath_val = NULL;

free_devtree:
	free(devtree);
	for (i = 0; i < dev_count; i++)
		if (ofwdevs[i])
			free(ofwdevs[i]);

	return error;
}