Blame SOURCES/kexec-tools-2.0.8-makedumpfile-sadump-Change-bit-order.patch

02c297
From 5f15256acab27859ececcfda1b882e8c49597697 Mon Sep 17 00:00:00 2001
02c297
From: HATAYAMA Daisuke <d.hatayama@jp.fujitsu.com>
02c297
Date: Tue, 20 Oct 2015 16:08:01 +0900
02c297
Subject: [PATCH 1/2] [PATCH 1/2] sadump: Change bit order.
02c297
02c297
sadump formats associate each bit in a bitmap with a physical
02c297
page in reverse order with the kdump-compressed format.
02c297
02c297
We had not detected this bug for considerably long term because
02c297
bitmaps in sadump formats consist mostly of 0x00 and 0xff excluding a
02c297
very limited amount of memory space for firmware.
02c297
02c297
Signed-off-by: Takao Indoh <indou.takao@jp.fujitsu.com>
02c297
Signed-off-by: HATAYAMA Daisuke <d.hatayama@jp.fujitsu.com>
02c297
---
02c297
 makedumpfile-1.5.7/sadump_info.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
02c297
 1 file changed, 73 insertions(+), 2 deletions(-)
02c297
02c297
diff --git a/makedumpfile-1.5.7/sadump_info.c b/makedumpfile-1.5.7/sadump_info.c
02c297
index 23275e2..4740683 100644
02c297
--- a/makedumpfile-1.5.7/sadump_info.c
02c297
+++ b/makedumpfile-1.5.7/sadump_info.c
02c297
@@ -85,6 +85,7 @@ struct sadump_info {
02c297
 	unsigned long long backup_offset;
02c297
 	int kdump_backed_up;
02c297
 	mdf_pfn_t max_mapnr;
02c297
+	struct dump_bitmap *ram_bitmap;
02c297
 };
02c297
 
02c297
 static char *guid_to_str(efi_guid_t *guid, char *buf, size_t buflen);
02c297
@@ -127,6 +128,35 @@ static int get_registers(int cpu, struct elf_prstatus *prstatus);
02c297
 static struct sadump_info sadump_info = {};
02c297
 static struct sadump_info *si = &sadump_info;
02c297
 
02c297
+static inline int
02c297
+sadump_is_on(char *bitmap, mdf_pfn_t i)
02c297
+{
02c297
+	return bitmap[i >> 3] & (1 << (7 - (i & 7)));
02c297
+}
02c297
+
02c297
+static inline int
02c297
+sadump_is_dumpable(struct dump_bitmap *bitmap, mdf_pfn_t pfn)
02c297
+{
02c297
+	off_t offset;
02c297
+
02c297
+	if (pfn == 0 || bitmap->no_block != pfn/PFN_BUFBITMAP) {
02c297
+		offset = bitmap->offset + BUFSIZE_BITMAP*(pfn/PFN_BUFBITMAP);
02c297
+		lseek(bitmap->fd, offset, SEEK_SET);
02c297
+		read(bitmap->fd, bitmap->buf, BUFSIZE_BITMAP);
02c297
+		if (pfn == 0)
02c297
+			bitmap->no_block = 0;
02c297
+		else
02c297
+			bitmap->no_block = pfn / PFN_BUFBITMAP;
02c297
+	}
02c297
+	return sadump_is_on(bitmap->buf, pfn % PFN_BUFBITMAP);
02c297
+}
02c297
+
02c297
+static inline int
02c297
+sadump_is_ram(mdf_pfn_t pfn)
02c297
+{
02c297
+	return sadump_is_dumpable(si->ram_bitmap, pfn);
02c297
+}
02c297
+
02c297
 int
02c297
 check_and_get_sadump_header_info(char *filename)
02c297
 {
02c297
@@ -161,6 +191,21 @@ check_and_get_sadump_header_info(char *filename)
02c297
 	return TRUE;
02c297
 }
02c297
 
02c297
+static void
02c297
+reverse_bit(char *buf, int len)
02c297
+{
02c297
+	int i;
02c297
+	unsigned char c;
02c297
+
02c297
+	for (i = 0; i < len; i++) {
02c297
+		c = buf[i];
02c297
+		c = ((c & 0x55) << 1) | ((c & 0xaa) >> 1); /* Swap 1bit */
02c297
+		c = ((c & 0x33) << 2) | ((c & 0xcc) >> 2); /* Swap 2bit */
02c297
+		c = (c << 4) | (c >> 4); /* Swap 4bit */
02c297
+		buf[i] = c;
02c297
+	}
02c297
+}
02c297
+
02c297
 int
02c297
 sadump_copy_1st_bitmap_from_memory(void)
02c297
 {
02c297
@@ -189,6 +234,14 @@ sadump_copy_1st_bitmap_from_memory(void)
02c297
 			       info->name_memory, strerror(errno));
02c297
 			return FALSE;
02c297
 		}
02c297
+		/*
02c297
+		 * sadump formats associate each bit in a bitmap with
02c297
+		 * a physical page in reverse order with the
02c297
+		 * kdump-compressed format. We need to change bit
02c297
+		 * order to reuse bitmaps in sadump formats in the
02c297
+		 * kdump-compressed format.
02c297
+		 */
02c297
+		reverse_bit(buf, sizeof(buf));
02c297
 		if (write(info->bitmap1->fd, buf, sizeof(buf)) != sizeof(buf)) {
02c297
 			ERRMSG("Can't write the bitmap(%s). %s\n",
02c297
 			       info->bitmap1->file_name, strerror(errno));
02c297
@@ -808,6 +861,19 @@ sadump_initialize_bitmap_memory(void)
02c297
 	info->bitmap_memory = bmp;
02c297
 	si->block_table = block_table;
02c297
 
02c297
+	bmp = malloc(sizeof(struct dump_bitmap));
02c297
+	if (bmp == NULL) {
02c297
+		ERRMSG("Can't allocate memory for the memory-bitmap. %s\n",
02c297
+		       strerror(errno));
02c297
+		return FALSE;
02c297
+	}
02c297
+	bmp->fd = info->fd_memory;
02c297
+	bmp->file_name = info->name_memory;
02c297
+	bmp->no_block = -1;
02c297
+	memset(bmp->buf, 0, BUFSIZE_BITMAP);
02c297
+	bmp->offset = si->sub_hdr_offset + sh->block_size * sh->sub_hdr_size;
02c297
+	si->ram_bitmap = bmp;
02c297
+
02c297
 	return TRUE;
02c297
 }
02c297
 
02c297
@@ -977,7 +1043,12 @@ readpage_sadump(unsigned long long paddr, void *bufptr)
02c297
 	if (pfn >= si->max_mapnr)
02c297
 		return FALSE;
02c297
 
02c297
-	if (!is_dumpable(info->bitmap_memory, pfn)) {
02c297
+	if (!sadump_is_ram(pfn)) {
02c297
+		ERRMSG("pfn(%llx) is not ram.\n", pfn);
02c297
+		return FALSE;
02c297
+	}
02c297
+
02c297
+	if (!sadump_is_dumpable(info->bitmap_memory, pfn)) {
02c297
 		ERRMSG("pfn(%llx) is excluded from %s.\n", pfn,
02c297
 		       info->name_memory);
02c297
 		return FALSE;
02c297
@@ -1142,7 +1213,7 @@ pfn_to_block(mdf_pfn_t pfn)
02c297
 		block = 0;
02c297
 
02c297
 	for (p = section * SADUMP_PF_SECTION_NUM; p < pfn; ++p)
02c297
-		if (is_dumpable(info->bitmap_memory, p))
02c297
+		if (sadump_is_dumpable(info->bitmap_memory, p))
02c297
 			block++;
02c297
 
02c297
 	return block;
02c297
-- 
02c297
2.1.0
02c297