Subject: [PATCH 03/18] Implement command-line processing

This is a backport of

    commit 3964c98eaca162ac1672327ad113b26d77dae0a6
    Author: HATAYAMA Daisuke <d.hatayama@jp.fujitsu.com>
    Date:   Fri Dec 16 10:34:52 2011 +0900
    
    [PATCH v2 03/14] Implement command-line processing
    
    Introduce new long option --diskset=VMCORE, which is used for
    specifying multiple VMCORE(s) generated on sadump diskset
    configuration. --diskset and --split options can be specified at the
    same time.
    
    Signed-off-by: HATAYAMA Daisuke <d.hatayama@jp.fujitsu.com>
---
diff -Nrup kexec-tools-2.0.0.old/makedumpfile-1.3.5/diskdump_mod.h kexec-tools-2.0.0/makedumpfile-1.3.5/diskdump_mod.h
--- kexec-tools-2.0.0.old/makedumpfile-1.3.5/diskdump_mod.h	2012-04-04 03:05:43.377528858 -0400
+++ kexec-tools-2.0.0/makedumpfile-1.3.5/diskdump_mod.h	2012-04-04 03:37:55.814462119 -0400
@@ -13,6 +13,8 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
+#ifndef _DISKDUMP_MOD_H
+#define _DISKDUMP_MOD_H
 
 #include <elf.h>
 
@@ -94,3 +96,5 @@ typedef struct page_desc {
 #define PAGE_VALID		(0x1)	/* flags */
 #define DISKDUMP_VALID_PAGE(flags)	((flags) & PAGE_VALID)
 
+#endif  /* DISKDUMP_MOD_H */
+
diff -Nrup kexec-tools-2.0.0.old/makedumpfile-1.3.5/makedumpfile.c kexec-tools-2.0.0/makedumpfile-1.3.5/makedumpfile.c
--- kexec-tools-2.0.0.old/makedumpfile-1.3.5/makedumpfile.c	2012-04-04 03:05:43.378528929 -0400
+++ kexec-tools-2.0.0/makedumpfile-1.3.5/makedumpfile.c	2012-04-04 03:22:07.374560292 -0400
@@ -14,6 +14,7 @@
  * GNU General Public License for more details.
  */
 #include "makedumpfile.h"
+#include "sadump_info.h"
 
 struct symbol_table	symbol_table;
 struct size_table	size_table;
@@ -10287,20 +10288,34 @@ check_param_for_creating_dumpfile(int ar
 	if (info->name_filterconfig && !info->name_vmlinux)
 		return FALSE;
 
+	if (info->flag_sadump_diskset && !sadump_is_supported_arch())
+		return FALSE;
+
 	if ((argc == optind + 2) && !info->flag_flatten
-				 && !info->flag_split) {
+				 && !info->flag_split
+				 && !info->flag_sadump_diskset) {
 		/*
 		 * Parameters for creating the dumpfile from vmcore.
 		 */
 		info->name_memory   = argv[optind];
 		info->name_dumpfile = argv[optind+1];
 
-	} else if ((argc > optind + 2) && info->flag_split) {
+	} else if (info->flag_split && (info->flag_sadump_diskset
+					? (argc >= optind + 2)
+					: (argc > optind + 2))) {
+		int num_vmcore;
+
 		/*
 		 * Parameters for creating multiple dumpfiles from vmcore.
 		 */
-		info->num_dumpfile = argc - optind - 1;
-		info->name_memory  = argv[optind];
+		if (info->flag_sadump_diskset) {
+			num_vmcore = 0;
+			info->name_memory = sadump_head_disk_name_memory();
+		} else {
+			num_vmcore = 1;
+			info->name_memory = argv[optind];
+		}
+		info->num_dumpfile = argc - optind - num_vmcore;
 
 		if (info->flag_elf_dumpfile) {
 			MSG("Options for splitting dumpfile cannot be used with Elf format.\n");
@@ -10313,7 +10328,15 @@ check_param_for_creating_dumpfile(int ar
 			return FALSE;
 		}
 		for (i = 0; i < info->num_dumpfile; i++)
-			SPLITTING_DUMPFILE(i) = argv[optind + 1 + i];
+			SPLITTING_DUMPFILE(i) = argv[optind + num_vmcore + i];
+
+	} else if ((argc == optind + 1) && !info->flag_split
+					&& info->flag_sadump_diskset) {
+		info->name_dumpfile = argv[optind];
+		info->name_memory = sadump_head_disk_name_memory();
+
+		DEBUG_MSG("name_dumpfile: %s\n", info->name_dumpfile);
+		DEBUG_MSG("name_memory: %s\n", info->name_memory);
 
 	} else if ((argc == optind + 1) && info->flag_flatten) {
 		/*
@@ -10381,6 +10404,7 @@ static struct option longopts[] = {
 	{"dump-dmesg", no_argument, NULL, 'M'}, 
 	{"config", required_argument, NULL, 'C'},
 	{"help", no_argument, NULL, 'h'},
+	{"diskset", required_argument, NULL, 'k'},
 	{0, 0, 0, 0}
 };
 
@@ -10443,6 +10467,11 @@ main(int argc, char *argv[])
 			info->flag_read_vmcoreinfo = 1;
 			info->name_vmcoreinfo = optarg;
 			break;
+		case 'k':
+			if (!sadump_add_diskset_info(optarg))
+				goto out;
+			info->flag_sadump_diskset = 1;
+			break;
 		case 'm':
 			message_level = atoi(optarg);
 			break;
diff -Nrup kexec-tools-2.0.0.old/makedumpfile-1.3.5/makedumpfile.conf.5 kexec-tools-2.0.0/makedumpfile-1.3.5/makedumpfile.conf.5
--- kexec-tools-2.0.0.old/makedumpfile-1.3.5/makedumpfile.conf.5	2012-04-04 03:05:43.378528929 -0400
+++ kexec-tools-2.0.0/makedumpfile-1.3.5/makedumpfile.conf.5	2012-04-04 03:39:14.640479278 -0400
@@ -1,4 +1,4 @@
-.TH MAKEDUMPFILE.CONF 5 "16 November 2010" "makedumpfile v1.3.7" "Linux System Administrator's Manual"
+.TH MAKEDUMPFILE.CONF 5 "11 November 2009" "makedumpfile v1.3.5" "Linux System Administrator's Manual"
 .SH NAME
 makedumpfile.conf \- The filter configuration file for makedumpfile(8).
 .SH DESCRIPTION
diff -Nrup kexec-tools-2.0.0.old/makedumpfile-1.3.5/makedumpfile.h kexec-tools-2.0.0/makedumpfile-1.3.5/makedumpfile.h
--- kexec-tools-2.0.0.old/makedumpfile-1.3.5/makedumpfile.h	2012-04-04 03:05:43.378528929 -0400
+++ kexec-tools-2.0.0/makedumpfile-1.3.5/makedumpfile.h	2012-04-04 03:39:08.894464182 -0400
@@ -13,6 +13,8 @@
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  */
+#ifndef _MAKEDUMPFILE_H
+#define _MAKEDUMPFILE_H
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -1484,3 +1486,5 @@ int get_xen_info_ia64(void);
 #define kvtop_xen(X)	FALSE
 #define get_xen_info_arch(X) FALSE
 #endif	/* s390x */
+
+#endif /* MAKEDUMPFILE_H */
diff -Nrup kexec-tools-2.0.0.old/makedumpfile-1.3.5/Makefile kexec-tools-2.0.0/makedumpfile-1.3.5/Makefile
--- kexec-tools-2.0.0.old/makedumpfile-1.3.5/Makefile	2012-04-04 03:05:43.377528858 -0400
+++ kexec-tools-2.0.0/makedumpfile-1.3.5/Makefile	2012-04-04 03:30:08.181464287 -0400
@@ -22,9 +22,9 @@ CFLAGS += -m64
 CFLAGS_ARCH += -m64
 endif
 
-SRC	= makedumpfile.c makedumpfile.h diskdump_mod.h sadump_mod.h
-SRC_ARCH = x86.c x86_64.c ia64.c ppc64.c s390x.c
-OBJ_ARCH = x86.o x86_64.o ia64.o ppc64.o s390x.o
+SRC	= makedumpfile.c makedumpfile.h diskdump_mod.h sadump_mod.h sadump_info.h
+SRC_ARCH = x86.c x86_64.c ia64.c ppc64.c s390x.c sadump_info.c
+OBJ_ARCH = x86.o x86_64.o ia64.o ppc64.o s390x.o sadump_info.o
 
 all: makedumpfile
 
diff -Nrup kexec-tools-2.0.0.old/makedumpfile-1.3.5/sadump_info.c kexec-tools-2.0.0/makedumpfile-1.3.5/sadump_info.c
--- kexec-tools-2.0.0.old/makedumpfile-1.3.5/sadump_info.c	1969-12-31 19:00:00.000000000 -0500
+++ kexec-tools-2.0.0/makedumpfile-1.3.5/sadump_info.c	2012-04-04 03:32:30.807466502 -0400
@@ -0,0 +1,77 @@
+/*
+ * sadump_info.c
+ *
+ * Created by: HATAYAMA, Daisuke <d.hatayama@jp.fujitsu.com>
+ *
+ * Copyright (C) 2011  FUJITSU LIMITED
+ * Copyright (C) 2011  NEC Corporation
+ *
+ * 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.
+ */
+#if defined(__x86__) || defined(__x86_64__)
+
+#include "makedumpfile.h"
+
+struct sadump_diskset_info {
+	char *name_memory;
+	int fd_memory;
+	struct sadump_part_header *sph_memory;
+	unsigned long data_offset;
+};
+
+struct sadump_info {
+	struct sadump_part_header *sph_memory;
+	struct sadump_header *sh_memory;
+	struct sadump_disk_set_header *sdh_memory;
+	struct sadump_media_header *smh_memory;
+	struct sadump_diskset_info *diskset_info;
+	int num_disks;
+	unsigned long sub_hdr_offset;
+	uint32_t smram_cpu_state_size;
+	unsigned long data_offset;
+};
+
+static struct sadump_info sadump_info = {};
+static struct sadump_info *si = &sadump_info;
+
+int
+sadump_add_diskset_info(char *name_memory)
+{
+	si->num_disks++;
+
+	si->diskset_info =
+		realloc(si->diskset_info,
+			si->num_disks*sizeof(struct sadump_diskset_info));
+	if (!si->diskset_info) {
+		ERRMSG("Can't allocate memory for sadump_diskset_info. %s\n",
+		       strerror(errno));
+		return FALSE;
+	}
+
+	si->diskset_info[si->num_disks - 1].name_memory = name_memory;
+
+	return TRUE;
+}
+
+char *
+sadump_head_disk_name_memory(void)
+{
+	return si->diskset_info[0].name_memory;
+}
+
+void
+free_sadump_info(void)
+{
+	if (si->diskset_info)
+		free(si->diskset_info);
+}
+
+#endif /* defined(__x86__) && defined(__x86_64__) */
diff -Nrup kexec-tools-2.0.0.old/makedumpfile-1.3.5/sadump_info.h kexec-tools-2.0.0/makedumpfile-1.3.5/sadump_info.h
--- kexec-tools-2.0.0.old/makedumpfile-1.3.5/sadump_info.h	1969-12-31 19:00:00.000000000 -0500
+++ kexec-tools-2.0.0/makedumpfile-1.3.5/sadump_info.h	2012-04-04 03:08:56.129529479 -0400
@@ -0,0 +1,61 @@
+/*
+ * sadump_info.h
+ *
+ * Created by: HATAYAMA, Daisuke <d.hatayama@jp.fujitsu.com>
+ *
+ * Copyright (C) 2011  FUJITSU LIMITED
+ * Copyright (C) 2011  NEC Corporation
+ *
+ * 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.
+ */
+
+#ifndef _SADUMP_INFO_H
+#define _SADUMP_INFO_H
+
+#include "makedumpfile.h"
+
+#if defined(__x86__) || defined(__x86_64__)
+
+int sadump_add_diskset_info(char *name_memory);
+char *sadump_head_disk_name_memory(void);
+void free_sadump_info(void);
+
+static inline int sadump_is_supported_arch(void)
+{
+	return TRUE;
+}
+
+#else
+
+static inline int sadump_add_diskset_info(char *name_memory)
+{
+	return TRUE;
+}
+
+static inline char *
+sadump_head_disk_name_memory(void)
+{
+	return NULL;
+}
+
+static inline void free_sadump_info(void)
+{
+	return;
+}
+
+static inline int sadump_is_supported_arch(void)
+{
+	return FALSE;
+}
+
+#endif
+
+#endif /* _SADUMP_INFO_H */
