commit 318863586af9278c82bd8543b8891002ce76d485
Author: Hannes Reinecke <hare@suse.de>
Date:   Mon Mar 10 11:21:49 2008 +0100

    Add compability program fwparam_ibft
    
    We still need the fwparam_ibft program as the fw discovery won't
    give us details about the network settings.
    
    Signed-off-by: Hannes Reinecke <hare@suse.de>

diff --git a/usr/Makefile b/usr/Makefile
index 0e54cf6..42a8788 100644
--- a/usr/Makefile
+++ b/usr/Makefile
@@ -42,19 +42,19 @@ COMMON_SRCS =  $(ISCSI_LIB_SRCS)
 # core initiator files
 INITIATOR_SRCS = initiator.o scsi.o actor.o mgmt_ipc.o isns.o transport.o
 # fw boot files
-FW_BOOT_SRCS = $(wildcard ../utils/fwparam_ibft/*.o)
+FWPARAM_LIB = ../utils/fwparam_ibft/libfwparam.a
 
 all: $(PROGRAMS)
 
 iscsid: $(COMMON_SRCS) $(IPC_OBJ) $(INITIATOR_SRCS) iscsid.o
 	$(CC) $(CFLAGS) $^ -o $@
 
-iscsiadm: $(COMMON_SRCS) $(FW_BOOT_SRCS) strings.o discovery.o iscsiadm.o
-	$(CC) $(CFLAGS) $^ -o $@
+iscsiadm: $(COMMON_SRCS) strings.o discovery.o iscsiadm.o
+	$(CC) $(CFLAGS) $^ -o $@ $(FWPARAM_LIB)
 
-iscsistart: $(IPC_OBJ) $(ISCSI_LIB_SRCS) $(INITIATOR_SRCS) $(FW_BOOT_SRCS) \
+iscsistart: $(COMMON_SRCS) $(IPC_OBJ) $(INITIATOR_SRCS) \
 		iscsistart.o statics.o
-	$(CC) $(CFLAGS) -static $^ -o $@
+	$(CC) $(CFLAGS) -static $^ -o $@ $(FWPARAM_LIB)
 
 clean:
 	rm -f *.o $(PROGRAMS) .depend
diff --git a/utils/fwparam_ibft/Makefile b/utils/fwparam_ibft/Makefile
index 6d7d00a..1414f06 100644
--- a/utils/fwparam_ibft/Makefile
+++ b/utils/fwparam_ibft/Makefile
@@ -21,6 +21,9 @@
 #	    "Prasanna Mumbai" <mumbai.prasanna@gmail.com>
 #
 
+PROG = fwparam_ibft
+MAIN = fwparam_main.o
+LIB = libfwparam.a
 OBJS := fwparam_ibft.o fw_entry.o
 OBJS += prom_lex.o prom_parse.tab.o fwparam_ppc.o
 CLEANFILES = $(OBJS) $(GENFILES) *.output *~
@@ -39,7 +42,10 @@ OPTFLAGS ?= -O2 -g -fPIC
 WARNFLAGS ?= -Wall -Wstrict-prototypes
 CFLAGS += $(OPTFLAGS) $(WARNFLAGS) -I../../include
 
-all: $(OBJS)
+all: $(LIB) $(PROG)
+
+$(LIB): $(OBJS)
+	$(AR) rcv $(LIB) $(OBJS)
 
 clean:
 	rm -f *.o $(CLEANFILES) .depend
@@ -54,6 +60,9 @@ $(GENFILES): Makefile
 
 $(OBJS): prom_parse.tab.h prom_parse.h fwparam_ibft.h
 
+$(PROG): $(MAIN) $(LIB)
+	gcc $(CFLAGS) -o $@ $< libfwparam.a
+
 depend:
 	gcc $(CFLAGS) -M `ls *.c` > .depend
 
diff --git a/utils/fwparam_ibft/fwparam_ibft.c b/utils/fwparam_ibft/fwparam_ibft.c
index dcc63c3..4397f94 100644
--- a/utils/fwparam_ibft/fwparam_ibft.c
+++ b/utils/fwparam_ibft/fwparam_ibft.c
@@ -319,7 +319,7 @@ dump_ibft(void *ibft_loc, struct boot_context *context)
 	struct ibft_initiator *initiator = NULL;
 	struct ibft_nic *nic0 = NULL, *nic1 = NULL;
 	struct ibft_tgt *tgt0 = NULL, *tgt1 = NULL;
-	char ipbuf[32];
+	char buf[32];
 
 	control = ibft_loc + sizeof(*ibft_hdr);
 	CHECK_HDR(control, control);
@@ -359,6 +359,28 @@ dump_ibft(void *ibft_loc, struct boot_context *context)
 		CHECK_HDR(tgt1, target);
 	}
 
+	if (!context) {
+		snprintf(buf, sizeof(buf), "iSCSI_INITIATOR_");
+
+		if (initiator && (initiator->hdr.flags &
+				  INIT_FLAG_FW_SEL_BOOT))
+			dump_initiator_prefix(ibft_loc, initiator, buf);
+
+		if (nic0 && (nic0->hdr.flags & INIT_FLAG_FW_SEL_BOOT))
+			dump_nic_prefix(ibft_loc, nic0, buf);
+		else if (nic1 && (nic1->hdr.flags & INIT_FLAG_FW_SEL_BOOT))
+			dump_nic_prefix(ibft_loc, nic1, buf);
+
+		snprintf(buf, sizeof(buf), "iSCSI_TARGET_");
+
+		if (tgt0 && (tgt0->hdr.flags & INIT_FLAG_FW_SEL_BOOT))
+			dump_tgt_prefix(ibft_loc, tgt0, buf);
+		else if (tgt1 && (tgt1->hdr.flags & INIT_FLAG_FW_SEL_BOOT))
+			dump_tgt_prefix(ibft_loc, tgt1, buf);
+
+		return 0;
+	}
+
 	strncpy(context->initiatorname,
 		(char *)ibft_loc+initiator->initiator_name_off,
 		initiator->initiator_name_len + 1);
@@ -367,10 +389,10 @@ dump_ibft(void *ibft_loc, struct boot_context *context)
 		strncpy((char *)context->targetname,
 			(char *)(ibft_loc+tgt0->tgt_name_off),
 			tgt0->tgt_name_len);
-		format_ipaddr(ipbuf, sizeof(ipbuf),
+		format_ipaddr(buf, sizeof(buf),
 			      tgt0->ip_addr);
-		strncpy((char *)context->target_ipaddr, ipbuf,
-			sizeof(ipbuf));
+		strncpy((char *)context->target_ipaddr, buf,
+			sizeof(buf));
 		context->target_port = tgt0->port;
 		strncpy(context->chap_name,
 			(char *)(ibft_loc + tgt0->chap_name_off),
@@ -389,10 +411,10 @@ dump_ibft(void *ibft_loc, struct boot_context *context)
 		strncpy((char *)context->targetname,
 			(char *)(ibft_loc+tgt1->tgt_name_off),
 			tgt1->tgt_name_len);
-		format_ipaddr(ipbuf, sizeof(ipbuf),
+		format_ipaddr(buf, sizeof(buf),
 			      tgt1->ip_addr);
-		strncpy((char *)context->target_ipaddr,ipbuf,
-			sizeof(ipbuf));
+		strncpy((char *)context->target_ipaddr,buf,
+			sizeof(buf));
 		context->target_port = tgt1->port;
 		strncpy(context->chap_name,
 			(char *)(ibft_loc + tgt1->chap_name_off),
@@ -411,7 +433,7 @@ dump_ibft(void *ibft_loc, struct boot_co
 	return 0;
 }
 
-char *search_ibft(unsigned char *start, int length)
+char *search_ibft(unsigned char *start, int start_addr, int length)
 {
 	unsigned char *cur_ptr, *rom_end;
 	struct ibft_table_hdr *ibft_hdr;
@@ -426,35 +448,51 @@ char *search_ibft(unsigned char *start, 
 			continue;
 		}
 		memcpy(&rom_size, cur_ptr + 2, 1);
-		/* Don't search past the end of the ROM BIOS block */
-		rom_end = cur_ptr + (rom_size * 512) - strlen(iBFTSTR);
-		while (cur_ptr < rom_end) {
-			if (!memcmp(cur_ptr, iBFTSTR,strlen(iBFTSTR)))
-				break;
-			cur_ptr++;
-		}
 
-		if (cur_ptr == rom_end) {
-			cur_ptr += strlen(iBFTSTR);
-			continue;
-		}
+		if (debug > 1)
+			fprintf(stderr, "Found rom at %x of size %d\n",
+				((int)(cur_ptr - start) + start_addr),
+				rom_size * 512);
 
-		ibft_hdr = (struct ibft_table_hdr *)cur_ptr;
-		/* Make sure it's correct version. */
-		if (ibft_hdr->revision != iBFT_REV) {
-			cur_ptr = rom_end + strlen(iBFTSTR);
+		if (rom_size == 0) {
+			/* Skip empty rom areas */
+			cur_ptr += 512;
 			continue;
 		}
 
-		/* Make sure that length is valid. */
-		if ((cur_ptr + ibft_hdr->length) <= (start + length)) {
-			/* Let verify the checksum */
-			for (i = 0, check_sum = 0; i < ibft_hdr->length; i++)
-				check_sum += cur_ptr[i];
+		/* Don't search past the end of rom area */
+		rom_end = (cur_ptr + (rom_size * 512)) - strlen(iBFTSTR);
 
-			if (check_sum == 0)
-				return (char *)cur_ptr;
+		while (cur_ptr < rom_end) {
+			if (memcmp(cur_ptr, iBFTSTR,strlen(iBFTSTR))) {
+				cur_ptr++;
+				continue;
+			}
+
+			if (debug > 1)
+				fprintf(stderr, "Found iBFT table at %x\n",
+					(int)(cur_ptr - start) + start_addr);
+
+			ibft_hdr = (struct ibft_table_hdr *)cur_ptr;
+
+			/* Make sure it's correct version. */
+			if (ibft_hdr->revision != iBFT_REV) {
+				cur_ptr = rom_end;
+				continue;
+			}
+
+			/* Make sure that length is valid. */
+			if ((cur_ptr + ibft_hdr->length) <= (start + length)) {
+				/* Let verify the checksum */
+				for (i = 0, check_sum = 0; i < ibft_hdr->length; i++)
+					check_sum += cur_ptr[i];
+
+				if (check_sum == 0)
+					return (char *)cur_ptr;
+			}
+			cur_ptr = rom_end;
 		}
+		cur_ptr += strlen(iBFTSTR);
 	}
 	return NULL;
 }
@@ -505,7 +543,7 @@ fwparam_ibft(struct boot_context *contex
 		goto done;
 	}
 
-	ibft_loc = search_ibft((unsigned char *)filebuf, end_search);
+	ibft_loc = search_ibft((unsigned char *)filebuf, start, end_search);
 	if (ibft_loc)
 		ret = dump_ibft(ibft_loc, context);
 	else {
diff --git a/utils/fwparam_ibft/fwparam_main.c b/utils/fwparam_ibft/fwparam_main.c
new file mode 100644
index 0000000..3a91b52
--- /dev/null
+++ b/utils/fwparam_ibft/fwparam_main.c
@@ -0,0 +1,73 @@
+/*
+ * 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, write to the Free Software
+ * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
+ * USA.
+ *
+ * Copyright (C) IBM Corporation, 2006
+ *
+ * Authors:	Patrick Mansfield <patmans@us.ibm.com>
+ *		Mike Anderson	<andmike@us.ibm.com>
+ *		Hannes Reinecke <hare@suse.de>
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include "fwparam_ibft.h"
+#include "fw_context.h"
+
+extern int debug;
+
+int
+main (int argc, char **argv)
+{
+	int option, ret;
+	char *progname, *filebuf = NULL;
+
+	progname = argv[0];
+
+	while (1) {
+		option = getopt(argc, argv, "f:vhb");
+		if (option == -1)
+			break;
+		switch (option) {
+		case 'b':
+			/* Ignored for compability */
+			break;
+		case 'f':
+			filebuf = optarg;
+			break;
+		case 'v':
+			debug++;
+			break;
+		default:
+			fprintf(stderr, "Unknown or bad option '%c'\n", option);
+		case 'h':
+			printf("Usage: %s OPTIONS\n"
+			       "-b print only fw boot selected sections\n"
+			       "-f file_to_search (default /dev/mem)\n"
+			       "-v verbose\n",
+			       progname);
+			exit(1);
+		}
+	}
+
+	ret = fwparam_ibft(NULL, filebuf);
+
+	exit(ret);
+}
