Skip to contentSkip to conversation

ARM: read kernel size from zImage

PATCH Applied Russell King 2 participants 3 messages Oct 23, 2017 – Nov 1, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 2/2 ARM: read kernel size from zImage

Read the new extension data which tells the boot agent about the requirements for booting the kernel image, such as how much RAM will be consumed by the kernel through decompression and booting. This is necessary to control the placement of the DTB and compressed RAM disk to avoid these objects being corrupted.

---
 kexec/arch/arm/kexec-zImage-arm.c | 51 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 50 insertions(+), 1 deletion(-)
kexec/arch/arm/kexec-zImage-arm.c +50 -1
index c6ecb04..a8c40cb 100644
@@ -34,6 +34,15 @@ struct zimage_header {
 #define ZIMAGE_MAGIC cpu_to_le32(0x016f2818)
 	uint32_t start;
 	uint32_t end;
+	uint32_t endian;
+
+	/* Extension to the data passed to the boot agent.  The offset
+	 * points at a tagged table following a similar format to the
+	 * ATAGs.
+	 */
+	uint32_t magic2;
+#define ZIMAGE_MAGIC2 (0x45454545)
+	uint32_t extension_tag_offset;
 };
 
 struct android_image {
@@ -114,6 +123,17 @@ struct tag {
 #define byte_size(t)    ((t)->hdr.size << 2)
 #define tag_size(type)  ((sizeof(struct tag_header) + sizeof(struct type) + 3) >> 2)
 
+struct zimage_tag {
+	struct tag_header hdr;
+	union {
+#define ZIMAGE_TAG_KRNL_SIZE cpu_to_le32(0x5a534c4b)
+		struct zimage_krnl_size {
+			uint32_t size_ptr;
+			uint32_t bss_size;
+		} krnl_size;
+	} u;
+};
+
 int zImage_arm_probe(const char *UNUSED(buf), off_t UNUSED(len))
 {
 	/* 
@@ -434,7 +454,7 @@ int zImage_arm_load(int argc, char **argv, const char *buf, off_t len,
 	if (dtb_file)
 		dtb_buf = slurp_file(dtb_file, &dtb_length);
 
-	if (len > 0x34) {
+	if (len > sizeof(struct zimage_header)) {
 		const struct zimage_header *hdr;
 		off_t size;
 
@@ -460,6 +480,35 @@ int zImage_arm_load(int argc, char **argv, const char *buf, off_t len,
 			if (size < len)
 				len = size;
 		}
+
+		/* Do we have an extension table? */
+		if (hdr->magic2 == ZIMAGE_MAGIC2 && !kexec_arm_image_size) {
+			uint32_t offset = hdr->extension_tag_offset;
+			uint32_t max = len - sizeof(struct tag_header);
+			struct zimage_tag *tag;
+
+			dbgprintf("zImage has tags\n");
+
+			for (offset = hdr->extension_tag_offset;
+			     (tag = (void *)(buf + offset)) != NULL &&
+			     offset < max && byte_size(tag) &&
+				offset + byte_size(tag) < len;
+			     offset += byte_size(tag)) {
+				dbgprintf("  offset 0x%08x tag 0x%08x size %u\n",
+					  offset, tag->hdr.tag, byte_size(tag));
+				if (tag->hdr.tag == ZIMAGE_TAG_KRNL_SIZE) {
+					uint32_t *p = (void *)buf +
+						tag->u.krnl_size.size_ptr;
+
+					kexec_arm_image_size =
+						get_unaligned(p) +
+						tag->u.krnl_size.bss_size;
+				}
+			}
+
+			dbgprintf("kernel image size: 0x%08x\n",
+				  kexec_arm_image_size);
+		}
 	}
 
 	/* Handle android images, 2048 is the minimum page size */
Signature · 12 lines
2.7.4


-- 
RMK's Patch system: http://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line in suburbia: sync at 8.8Mbps down 630kbps up
According to speedtest.net: 8.21Mbps down 510kbps up

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Applied
#1 lore raw
Quoted 8 lines · Russell King

Read the new extension data which tells the boot agent about the requirements for booting the kernel image, such as how much RAM will be consumed by the kernel through decompression and booting. This is necessary to control the placement of the DTB and compressed RAM disk to avoid these objects being corrupted.

Thanks, applied.

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#2 lore raw
Quoted 11 lines · Simon Horman
Quoted 8 lines · Russell King

Read the new extension data which tells the boot agent about the requirements for booting the kernel image, such as how much RAM will be consumed by the kernel through decompression and booting. This is necessary to control the placement of the DTB and compressed RAM disk to avoid these objects being corrupted.

Thanks, applied.

Thanks, since I rebased my tree, I've realised that wasn't quite the patch I intended, but it'll do for now.

Building the latest tree gives me a new warning:

gcc -g -O2 -fno-strict-aliasing -Wall -Wstrict-prototypes -I./include -I./util_lib/include -Iinclude/ -I./kexec/libfdt -D_FILE_OFFSET_BITS=64 -I./kexec/arch/arm/include -c -MD -o kexec/kexec.o kexec/kexec.c kexec/kexec.c: In function ■print_crashkernel_region_size■:
kexec/kexec.c:1232:9: warning: format ■%lu■ expects argument of type ■long unsigned int■, but argument 2 has type ■uint64_t {aka long long unsigned int}■ [-Wformat=]

  printf("%lu\n", (start != end) ? (end - start + 1) : 0UL);
         ^

caused by:

commit 76b31203222a9833f424e98a134603c2f840c82b

Author: Daniel Kiper <[email protected]>
Date:   Fri Feb 17 16:47:25 2017 -0600

kexec: Add option to get crash kernel region size

On 32-bit architectures, "%lu" expects an unsigned long, but uint64_t is an unsigned long long. On 64-bit architectures, "%lu" also expects an unsigned long, but uint64_t is unsigned long, so there's no warning.

The problem for ARM here is that a 64-bit value is passed as an even-odd register pair, so for the above printf, that would be r2, r3. However, "%lu" will expect the value in the first register following the format pointer, that being r1. So we end up printing garbage here.

Either we use "%llu" and cast the value to an unsigned long long (needlessly widening on 64-bit arches), or "%lu" and cast to unsigned long (truncating) or we need a definition of the format to be used for uint64_t types (tends to be messy.)

Signature · 6 lines
Russell King

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec