a4c8b3
diff -Naurp open-iscsi-2.0-870-rc1/utils/fwparam_ibft/fw_entry.c open-iscsi-2.0-870-rc1.work/utils/fwparam_ibft/fw_entry.c
a4c8b3
--- open-iscsi-2.0-870-rc1/utils/fwparam_ibft/fw_entry.c	2008-06-30 20:14:03.000000000 -0500
a4c8b3
+++ open-iscsi-2.0-870-rc1.work/utils/fwparam_ibft/fw_entry.c	2008-06-30 21:21:57.000000000 -0500
5a3872
@@ -29,7 +29,8 @@ int fw_get_entry(struct boot_context *co
5a3872
 
5a3872
 	ret = fwparam_ppc(context, filepath);
5a3872
 	if (ret)
5a3872
-		ret = fwparam_ibft(context, filepath);
5a3872
+		ret = fwparam_ibft_sysfs(context, filepath);
5a3872
+
5a3872
 	return ret;
5a3872
 }
5a3872
 
a4c8b3
diff -Naurp open-iscsi-2.0-870-rc1/utils/fwparam_ibft/fwparam_ibft.h open-iscsi-2.0-870-rc1.work/utils/fwparam_ibft/fwparam_ibft.h
a4c8b3
--- open-iscsi-2.0-870-rc1/utils/fwparam_ibft/fwparam_ibft.h	2008-06-30 20:14:03.000000000 -0500
a4c8b3
+++ open-iscsi-2.0-870-rc1.work/utils/fwparam_ibft/fwparam_ibft.h	2008-06-30 21:21:57.000000000 -0500
5a3872
@@ -153,6 +153,7 @@ extern int dev_count;
5a3872
 #define TARGET		"target"
5a3872
 
5a3872
 extern int fwparam_ibft(struct boot_context *context, const char *filepath);
5a3872
+extern int fwparam_ibft_sysfs(struct boot_context *context,
5a3872
+			const char *filepath);
5a3872
 extern int fwparam_ppc(struct boot_context *context, const char *filepath);
5a3872
-
5a3872
 #endif /* FWPARAM_IBFT_H_ */
a4c8b3
diff -Naurp open-iscsi-2.0-870-rc1/utils/fwparam_ibft/fwparam_ibft_sysfs.c open-iscsi-2.0-870-rc1.work/utils/fwparam_ibft/fwparam_ibft_sysfs.c
a4c8b3
--- open-iscsi-2.0-870-rc1/utils/fwparam_ibft/fwparam_ibft_sysfs.c	1969-12-31 18:00:00.000000000 -0600
a4c8b3
+++ open-iscsi-2.0-870-rc1.work/utils/fwparam_ibft/fwparam_ibft_sysfs.c	2008-06-30 21:21:57.000000000 -0500
5a3872
@@ -0,0 +1,271 @@
5a3872
+/*
5a3872
+ * Copyright (C) IBM Corporation. 2007
5a3872
+ * Author: Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
5a3872
+ *
5a3872
+ * This program is free software: you can redistribute it and/or modify
5a3872
+ * it under the terms of the GNU General Public License as published by
5a3872
+ * the Free Software Foundation, either version 2 of the License, or
5a3872
+ * (at your option) any later version.
5a3872
+ *
5a3872
+ * This program is distributed in the hope that it will be useful,
5a3872
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
5a3872
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5a3872
+ * GNU General Public License for more details.
5a3872
+ *
5a3872
+ * You should have received a copy of the GNU General Public License
5a3872
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
5a3872
+ */
5a3872
+
5a3872
+#define  _XOPEN_SOURCE 500
5a3872
+#include <ftw.h>
5a3872
+#include <stdio.h>
5a3872
+#include <stdlib.h>
5a3872
+#include <string.h>
5a3872
+#include <unistd.h>
5a3872
+#include <fcntl.h>
5a3872
+#include <errno.h>
5a3872
+#include "fwparam_ibft.h"
5a3872
+#include <fw_context.h>
5a3872
+
5a3872
+#define IBFT_MAX 255
5a3872
+#define IBFT_SYSFS_ROOT "/sys/firmware/ibft/"
5a3872
+
5a3872
+static char *target_list[IBFT_MAX];
5a3872
+static char *nic_list[IBFT_MAX];
5a3872
+static int nic_cnt;
5a3872
+static int tgt_cnt;
5a3872
+
5a3872
+/*
5a3872
+ * Helper routines.
5a3872
+ */
5a3872
+static int file_exist(const char *file)
5a3872
+{
5a3872
+
5a3872
+	struct stat bootpath_stat;
5a3872
+
5a3872
+	return !stat(file, &bootpath_stat);
5a3872
+}
5a3872
+
5a3872
+static int read_file(const char *file, char **contents)
5a3872
+{
5a3872
+	int error, fd, bytes_read;
5a3872
+	struct stat bootpath_stat;
5a3872
+
5a3872
+	error = stat(file, &bootpath_stat);
5a3872
+	if (error < 0) {
5a3872
+		fprintf(stderr, "(%s:%d) stat %s, %s\n", __FILE__, __LINE__,
5a3872
+			file, strerror(errno));
5a3872
+		return error;
5a3872
+	}
5a3872
+
5a3872
+	*contents = malloc(bootpath_stat.st_size);
5a3872
+	if (!*contents) {
5a3872
+		error = ENOMEM;
5a3872
+		fprintf(stderr, "(%s:%d) Could not allocate enough memory for "\
5a3872
+			"%s: %s (%d)\n",
5a3872
+			__FILE__, __LINE__, file, strerror(error), error);
5a3872
+		return errno;
5a3872
+	}
5a3872
+
5a3872
+	fd = open(file, O_RDONLY);
5a3872
+	if (fd < 0) {
5a3872
+		fprintf(stderr, "(%s:%d): Could not open %s: %s (%d)\n",
5a3872
+			__FILE__, __LINE__, file, strerror(errno), errno);
5a3872
+		free(*contents);
5a3872
+		return errno;
5a3872
+	}
5a3872
+
5a3872
+	bytes_read = read(fd, *contents, bootpath_stat.st_size);
5a3872
+	close(fd);
5a3872
+	if (bytes_read > bootpath_stat.st_size) {
5a3872
+		fprintf(stderr, "(%s:%d) Read more data in than expected for "\
5a3872
+			"%s: %s (%d)\n",
5a3872
+			__FILE__, __LINE__, file, strerror(EIO), EIO);
5a3872
+		free(*contents);
5a3872
+		return errno;
5a3872
+	}
5a3872
+	/* chop() implementation */
5a3872
+	if (*(*contents + (ssize_t)(bytes_read - 1))  == '\n')
5a3872
+		*(*contents + (ssize_t) (bytes_read - 1)) = 0;
5a3872
+
5a3872
+	return 0;
5a3872
+}
5a3872
+
5a3872
+static int read_data(const char *dir, const char *name, char *dst, ssize_t size)
5a3872
+{
5a3872
+	char *data = NULL;
5a3872
+	char file[FILENAMESZ];
5a3872
+	int rc = 0;
5a3872
+
5a3872
+	memset(file, 0, FILENAMESZ);
5a3872
+	strncat(file, dir, FILENAMESZ);
5a3872
+	strncat(file, name, FILENAMESZ);
5a3872
+
5a3872
+	if (file_exist(file))  {
5a3872
+		rc = read_file(file, &data);
5a3872
+		if (debug)
5a3872
+			fprintf(stderr, "(%s:%d) Read from %s:[%s]\n",
5a3872
+				__FILE__, __LINE__, file, data);
5a3872
+		if (!rc)
5a3872
+			memcpy(dst, data, size);
5a3872
+		free(data);
5a3872
+	}
5a3872
+
5a3872
+	return rc;
5a3872
+}
5a3872
+
5a3872
+static int read_int_data(const char *dir, const char *name, int *dst)
5a3872
+{
5a3872
+	int rc = 0;
5a3872
+	char contents[5]; /* The flag is a 1 byte value */
5a3872
+
5a3872
+	rc = read_data(dir, name, (char *)&contents, sizeof(contents));
5a3872
+	if (!rc)
5a3872
+		*dst = atoi(contents);
5a3872
+
5a3872
+	return rc;
5a3872
+}
5a3872
+
5a3872
+/*
5a3872
+ * Finds the etherrnetX and targetX under the sysfs directory.
5a3872
+ */
5a3872
+static int find_sysfs_dirs(const char *fpath, const struct stat *sb,
5a3872
+			   int tflag, struct FTW *ftw)
5a3872
+{
5a3872
+	if (tflag == FTW_D &&
5a3872
+		(strstr(fpath + ftw->base, "target")))
5a3872
+			target_list[tgt_cnt++] = strdup(fpath);
5a3872
+
5a3872
+	if (tflag == FTW_D &&
5a3872
+		(strstr(fpath + ftw->base, "ethernet")))
5a3872
+			nic_list[nic_cnt++] = strdup(fpath);
5a3872
+
5a3872
+	return 0;
5a3872
+}
5a3872
+
5a3872
+/*
5a3872
+ * Routines to fill in the context values.
5a3872
+ */
5a3872
+static int fill_nic_context(const char *dir, struct boot_context *context)
5a3872
+{
5a3872
+	int rc = 0;
5a3872
+
5a3872
+	rc |= read_data(dir, "/mac", context->mac, sizeof(context->mac));
5a3872
+	rc |= read_data(dir, "/vlan", context->vlan, sizeof(context->vlan));
5a3872
+	rc |= read_data(dir, "/ip-addr", context->ipaddr,
5a3872
+		sizeof(context->ipaddr));
5a3872
+	rc |= read_data(dir, "/mask", context->mask, sizeof(context->mask));
5a3872
+
5a3872
+	return rc;
5a3872
+}
5a3872
+
5a3872
+static int fill_initiator_context(const char *dir, struct boot_context *context)
5a3872
+{
5a3872
+	int rc = 0;
5a3872
+
5a3872
+	rc |= read_data(dir, "/initiator-name", context->initiatorname,
5a3872
+		sizeof(context->initiatorname));
5a3872
+	rc |= read_data(dir, "/isns-server", context->isid,
5a3872
+		sizeof(context->isid));
5a3872
+
5a3872
+	return rc;
5a3872
+}
5a3872
+static int fill_tgt_context(const char *dir, struct boot_context *context)
5a3872
+{
5a3872
+	int rc = 0;
5a3872
+
5a3872
+	rc |= read_data(dir, "/target-name", context->targetname,
5a3872
+		sizeof(context->targetname));
5a3872
+	rc |= read_data(dir, "/ip-addr", context->target_ipaddr,
5a3872
+		sizeof(context->target_ipaddr));
5a3872
+	rc |= read_int_data(dir, "/port", &context->target_port);
5a3872
+	rc |= read_data(dir, "/lun", context->lun,
5a3872
+		sizeof(context->lun));
5a3872
+	rc |= read_data(dir, "/chap-name", context->chap_name,
5a3872
+		sizeof(context->chap_name));
5a3872
+	rc |= read_data(dir, "/chap-secret", context->chap_password,
5a3872
+		sizeof(context->chap_password));
5a3872
+	rc |= read_data(dir, "/rev-chap-name", context->chap_name_in,
5a3872
+		sizeof(context->chap_name_in));
5a3872
+	rc |= read_data(dir, "/rev-chap-name-secret", context->chap_password_in,
5a3872
+		sizeof(context->chap_password_in));
5a3872
+
5a3872
+	return 0;
5a3872
+}
5a3872
+
5a3872
+#define IBFT_SYSFS_FLAG_NAME "/flags"
5a3872
+#define IBFT_SYSFS_FLAG_FW_SEL_BOOT 2
5a3872
+
5a3872
+static int find_boot_flag(char *list[], ssize_t size, int *boot_idx)
5a3872
+{
5a3872
+	int rc = -1;
5a3872
+	int i, flag = -1;
5a3872
+
5a3872
+	for (i = 0; i < size; i++, flag = -1) {
5a3872
+		rc = read_int_data(list[i], IBFT_SYSFS_FLAG_NAME, &flag;;
5a3872
+		if (flag & IBFT_SYSFS_FLAG_FW_SEL_BOOT) {
5a3872
+			*boot_idx = i;
5a3872
+			rc = 0;
5a3872
+			break;
5a3872
+		}
5a3872
+
5a3872
+	}
5a3872
+
5a3872
+	return rc;
5a3872
+}
5a3872
+
5a3872
+static void deallocate_lists(void)
5a3872
+{
5a3872
+	int i;
5a3872
+
5a3872
+	for (i = 0; i < nic_cnt; i++)
5a3872
+		free(nic_list[i]);
5a3872
+
5a3872
+	nic_cnt = 0;
5a3872
+	for (i = 0; i < tgt_cnt; i++)
5a3872
+		free(target_list[i]);
5a3872
+
5a3872
+	tgt_cnt = 0;
5a3872
+
5a3872
+}
5a3872
+
5a3872
+int fwparam_ibft_sysfs(struct boot_context *context, const char *filepath)
5a3872
+{
5a3872
+	char initiator_dir[FILENAMESZ];
5a3872
+	char *root_sysfs = NULL;
5a3872
+	int rc = 1;
5a3872
+	int nic_idx = -1, tgt_idx = -1;
5a3872
+
5a3872
+	if (filepath)
5a3872
+		root_sysfs = (char *)filepath;
5a3872
+	else
5a3872
+		root_sysfs = IBFT_SYSFS_ROOT;
5a3872
+
5a3872
+	memset(&initiator_dir, 0 , FILENAMESZ);
5a3872
+	strncat(initiator_dir, root_sysfs, FILENAMESZ);
5a3872
+	strncat(initiator_dir, "initiator", FILENAMESZ);
5a3872
+
5a3872
+	if (file_exist(initiator_dir)) {
5a3872
+
5a3872
+		/* Find the target's and the ethernet's */
5a3872
+		rc = nftw(root_sysfs, find_sysfs_dirs, 20, 1);
5a3872
+
5a3872
+		/* Find wihch target and which ethernet have
5a3872
+		the boot flag set. */
5a3872
+		rc = find_boot_flag(nic_list, nic_cnt, &nic_idx);
5a3872
+		if (rc)
5a3872
+			goto free;
5a3872
+
5a3872
+		rc = find_boot_flag(target_list, tgt_cnt, &tgt_idx);
5a3872
+		if (rc)
5a3872
+			goto free;
5a3872
+
5a3872
+		/* Fill in the context values */
5a3872
+		rc = fill_nic_context(nic_list[nic_idx], context);
5a3872
+		rc |= fill_tgt_context(target_list[tgt_idx], context);
5a3872
+		rc |= fill_initiator_context(initiator_dir, context);
5a3872
+	}
5a3872
+free:
5a3872
+	deallocate_lists();
5a3872
+	return rc;
5a3872
+}
a4c8b3
diff -Naurp open-iscsi-2.0-870-rc1/utils/fwparam_ibft/Makefile open-iscsi-2.0-870-rc1.work/utils/fwparam_ibft/Makefile
a4c8b3
--- open-iscsi-2.0-870-rc1/utils/fwparam_ibft/Makefile	2008-06-30 20:14:03.000000000 -0500
a4c8b3
+++ open-iscsi-2.0-870-rc1.work/utils/fwparam_ibft/Makefile	2008-06-30 21:22:44.000000000 -0500
a4c8b3
@@ -22,7 +22,7 @@
5a3872
 #
5a3872
 
a4c8b3
 OBJS := fwparam_ibft.o fw_entry.o
a4c8b3
-OBJS += prom_lex.o prom_parse.tab.o fwparam_ppc.o
a4c8b3
+OBJS += prom_lex.o prom_parse.tab.o fwparam_ppc.o fwparam_ibft_sysfs.o
a4c8b3
 CLEANFILES = $(OBJS) *.output *~
a4c8b3
 
a4c8b3
 OPTFLAGS ?= -O2 -g -fPIC