Discussion:
[PATCH 1/3] kexec: Let slurp_file_len() return the number of bytes read
Geert Uytterhoeven
2013-10-15 17:50:58 UTC
Permalink
Add an optional output parameter to slurp_file_len() so it can return the
actual number of bytes read.

Signed-off-by: Geert Uytterhoeven <***@linux-m68k.org>
Acked-by: Dave Young <***@redhat.com>
---
kexec/arch/i386/crashdump-x86.c | 5 ++---
kexec/kexec.c | 4 +++-
kexec/kexec.h | 2 +-
3 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/kexec/arch/i386/crashdump-x86.c b/kexec/arch/i386/crashdump-x86.c
index e44fcebbd32b..e2da50b54c91 100644
--- a/kexec/arch/i386/crashdump-x86.c
+++ b/kexec/arch/i386/crashdump-x86.c
@@ -121,7 +121,7 @@ static int get_kernel_vaddr_and_size(struct kexec_info *UNUSED(info),
struct mem_ehdr ehdr;
struct mem_phdr *phdr, *end_phdr;
int align;
- unsigned long size;
+ off_t size;
uint32_t elf_flags = 0;

if (elf_info->machine != EM_X86_64)
@@ -131,8 +131,7 @@ static int get_kernel_vaddr_and_size(struct kexec_info *UNUSED(info),
return 0;

align = getpagesize();
- size = KCORE_ELF_HEADERS_SIZE;
- buf = slurp_file_len(kcore, size);
+ buf = slurp_file_len(kcore, KCORE_ELF_HEADERS_SIZE, &size);
if (!buf) {
fprintf(stderr, "Cannot read %s: %s\n", kcore, strerror(errno));
return -1;
diff --git a/kexec/kexec.c b/kexec/kexec.c
index 2ce570f7bb43..185c85bef342 100644
--- a/kexec/kexec.c
+++ b/kexec/kexec.c
@@ -537,7 +537,7 @@ char *slurp_file(const char *filename, off_t *r_size)
/* This functions reads either specified number of bytes from the file or
lesser if EOF is met. */

-char *slurp_file_len(const char *filename, off_t size)
+char *slurp_file_len(const char *filename, off_t size, off_t *nread)
{
int fd;
char *buf;
@@ -575,6 +575,8 @@ char *slurp_file_len(const char *filename, off_t size)
die("Close of %s failed: %s\n",
filename, strerror(errno));
}
+ if (nread)
+ *nread = progress;
return buf;
}

diff --git a/kexec/kexec.h b/kexec/kexec.h
index 2904e03c79b8..8d5b512e365f 100644
--- a/kexec/kexec.h
+++ b/kexec/kexec.h
@@ -237,7 +237,7 @@ extern void die(const char *fmt, ...)
extern void *xmalloc(size_t size);
extern void *xrealloc(void *ptr, size_t size);
extern char *slurp_file(const char *filename, off_t *r_size);
-extern char *slurp_file_len(const char *filename, off_t size);
+extern char *slurp_file_len(const char *filename, off_t size, off_t *nread);
extern char *slurp_decompress_file(const char *filename, off_t *r_size);
extern unsigned long virt_to_phys(unsigned long addr);
extern void add_segment(struct kexec_info *info,
--
1.7.9.5
Geert Uytterhoeven
2013-10-15 17:51:00 UTC
Permalink
Signed-off-by: Geert Uytterhoeven <***@linux-m68k.org>
---
v2:
- Fix handling of virtual and physical addresses, for machines where
memory doesn't start at zero,
- Print a warning if the kernel size exceeds 4 MiB, as current kernels
cannot handle that,
- Check struct bootversion at the start of the kernel, and print a
warning if it cannot be found or doesn't match,
- Replace literal 4096 by PAGE_SIZE,
- Handle removal of page zero at the ELF program segment level, as
m68kboot does,
- Remove -PAGE_SIZE for the ramdisk location now the bug in locate_hole()
is fixed,
- Use endian-correct types for bootinfo,
- Remove unused -? option handling, cfr. commit
bf9d0f055c791a26b2237b5a12b48ae1b7e0d550 ("kexec: Remove unused -?
option handling"),
- Use <asm/bootinfo.h> instead of our own definitions.
---
configure.ac | 2 +-
kexec/Makefile | 1 +
kexec/arch/m68k/Makefile | 14 ++
kexec/arch/m68k/bootinfo.c | 262 ++++++++++++++++++++++++++++++++
kexec/arch/m68k/bootinfo.h | 43 ++++++
kexec/arch/m68k/include/arch/options.h | 45 ++++++
kexec/arch/m68k/kexec-elf-m68k.c | 182 ++++++++++++++++++++++
kexec/arch/m68k/kexec-elf-rel-m68k.c | 37 +++++
kexec/arch/m68k/kexec-m68k.c | 104 +++++++++++++
kexec/arch/m68k/kexec-m68k.h | 9 ++
kexec/kexec-syscall.h | 7 +
11 files changed, 705 insertions(+), 1 deletion(-)
create mode 100644 kexec/arch/m68k/Makefile
create mode 100644 kexec/arch/m68k/bootinfo.c
create mode 100644 kexec/arch/m68k/bootinfo.h
create mode 100644 kexec/arch/m68k/include/arch/options.h
create mode 100644 kexec/arch/m68k/kexec-elf-m68k.c
create mode 100644 kexec/arch/m68k/kexec-elf-rel-m68k.c
create mode 100644 kexec/arch/m68k/kexec-m68k.c
create mode 100644 kexec/arch/m68k/kexec-m68k.h

diff --git a/configure.ac b/configure.ac
index 7b61dbf6f528..704d4f9c4f80 100644
--- a/configure.ac
+++ b/configure.ac
@@ -45,7 +45,7 @@ case $target_cpu in
cris|crisv32 )
ARCH="cris"
;;
- ia64|x86_64|alpha )
+ ia64|x86_64|alpha|m68k )
ARCH="$target_cpu"
;;
* )
diff --git a/kexec/Makefile b/kexec/Makefile
index 8a6138d5d703..ceb33af98696 100644
--- a/kexec/Makefile
+++ b/kexec/Makefile
@@ -72,6 +72,7 @@ include $(srcdir)/kexec/arch/alpha/Makefile
include $(srcdir)/kexec/arch/arm/Makefile
include $(srcdir)/kexec/arch/i386/Makefile
include $(srcdir)/kexec/arch/ia64/Makefile
+include $(srcdir)/kexec/arch/m68k/Makefile
include $(srcdir)/kexec/arch/mips/Makefile
include $(srcdir)/kexec/arch/cris/Makefile
include $(srcdir)/kexec/arch/ppc/Makefile
diff --git a/kexec/arch/m68k/Makefile b/kexec/arch/m68k/Makefile
new file mode 100644
index 000000000000..7b135649dd63
--- /dev/null
+++ b/kexec/arch/m68k/Makefile
@@ -0,0 +1,14 @@
+#
+# kexec m68k (linux booting linux)
+#
+m68k_KEXEC_SRCS = kexec/arch/m68k/kexec-m68k.c
+m68k_KEXEC_SRCS += kexec/arch/m68k/kexec-elf-m68k.c
+m68k_KEXEC_SRCS += kexec/arch/m68k/kexec-elf-rel-m68k.c
+m68k_KEXEC_SRCS += kexec/arch/m68k/bootinfo.c
+
+m68k_ADD_SEGMENT =
+m68k_VIRT_TO_PHYS =
+
+dist += kexec/arch/m68k/Makefile $(m68k_KEXEC_SRCS) \
+ kexec/arch/m68k/kexec-m68k.h \
+ kexec/arch/m68k/include/arch/options.h
diff --git a/kexec/arch/m68k/bootinfo.c b/kexec/arch/m68k/bootinfo.c
new file mode 100644
index 000000000000..18bf22649dc8
--- /dev/null
+++ b/kexec/arch/m68k/bootinfo.c
@@ -0,0 +1,262 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "../../kexec.h"
+
+#include "bootinfo.h"
+
+const char *bootinfo_file = DEFAULT_BOOTINFO_FILE;
+static struct bi_rec *bootinfo;
+static off_t bootinfo_size;
+
+static unsigned int num_memchunks;
+
+static struct bi_rec *bi_next(struct bi_rec *bi, uint16_t size)
+{
+ return (void *)((unsigned long)bi + size);
+}
+
+static struct bi_rec *bi_find(struct bi_rec *prev, uint16_t tag)
+{
+ struct bi_rec *bi = prev ? bi_next(prev, prev->size) : bootinfo;
+
+ for (bi = prev ? bi_next(prev, prev->size) : bootinfo;
+ bi->tag != BI_LAST; bi = bi_next(bi, bi->size))
+ if (bi->tag == tag)
+ return bi;
+ return NULL;
+}
+
+static void bi_remove(uint16_t tag)
+{
+ struct bi_rec *bi;
+ off_t rem;
+ uint16_t size;
+
+ bi = bootinfo;
+ rem = bootinfo_size;
+ while (1) {
+ if (bi->tag == BI_LAST)
+ break;
+
+ size = bi->size;
+ if (bi->tag == tag) {
+ memmove(bi, bi_next(bi, size), rem - size);
+ bootinfo_size -= size;
+ rem -= size;
+ continue;
+ }
+
+ bi = bi_next(bi, size);
+ rem -= size;
+ }
+}
+
+static struct bi_rec *bi_add(uint16_t tag, uint16_t size)
+{
+ struct bi_rec *bi;
+
+ /* Add 4-byte header and round up to multiple of 4 bytes */
+ size = _ALIGN_UP(4 + size, 4);
+
+ bootinfo = xrealloc(bootinfo, bootinfo_size + size);
+
+ /* Replace old sentinel by new record */
+ bi = bi_next(bootinfo, bootinfo_size - 2);
+ bootinfo_size += size;
+ memset(bi, 0, size);
+ bi->tag = tag;
+ bi->size = size;
+
+ /* Re-add sentinel */
+ bi_next(bi, size)->tag = BI_LAST;
+
+ return bi;
+}
+
+void bootinfo_load(void)
+{
+ struct bi_rec *bi;
+ off_t rem;
+ uint16_t tag, size;
+
+ dbgprintf("Loading bootinfo from %s\n", bootinfo_file);
+ bootinfo = (void *)slurp_file_len(bootinfo_file, MAX_BOOTINFO_SIZE,
+ &bootinfo_size);
+ if (!bootinfo)
+ die("No bootinfo\n");
+
+ bi = bootinfo;
+ rem = bootinfo_size;
+ while (1) {
+ if (rem < 2)
+ die("Unexpected end of bootinfo\n");
+
+ tag = bi->tag;
+ if (tag == BI_LAST) {
+ rem -= 2;
+ break;
+ }
+
+ if (rem < 4)
+ die("Unexpected end of bootinfo\n");
+
+ size = bi->size;
+ if (size < 4 || size % 4)
+ die("Invalid tag size\n");
+ if (rem < size)
+ die("Unexpected end of bootinfo\n");
+
+ if (tag == BI_MEMCHUNK)
+ num_memchunks++;
+
+ bi = bi_next(bi, size);
+ rem -= size;
+ }
+
+ if (rem)
+ die("Trailing data at end of bootinfo\n");
+}
+
+void bootinfo_print(void)
+{
+ struct bi_rec *bi = bootinfo;
+ uint16_t tag, size;
+
+ while (1) {
+ tag = bi->tag;
+ if (tag == BI_LAST) {
+ puts("BI_LAST");
+ break;
+ }
+
+ size = bi->size;
+ switch (tag) {
+ case BI_MACHTYPE:
+ printf("BI_MACHTYPE: 0x%08x\n", bi->machtype);
+ break;
+
+ case BI_MEMCHUNK:
+ printf("BI_MEMCHUNK: 0x%08x bytes at 0x%08x\n",
+ bi->mem_info.size, bi->mem_info.addr);
+ break;
+
+ case BI_RAMDISK:
+ printf("BI_RAMDISK: 0x%08x bytes at 0x%08x\n",
+ bi->mem_info.size, bi->mem_info.addr);
+ break;
+
+ case BI_COMMAND_LINE:
+ printf("BI_COMMAND_LINE: %s\n", bi->string);
+ break;
+
+ default:
+ printf("BI tag 0x%04x size %u\n", tag, size);
+ break;
+ }
+ bi = bi_next(bi, size);
+ }
+}
+
+int bootinfo_get_memory_ranges(struct memory_range **range)
+{
+ struct memory_range *ranges;
+ unsigned int i;
+ struct bi_rec *bi;
+
+ ranges = xmalloc(num_memchunks * sizeof(struct memory_range));
+ for (i = 0, bi = NULL;
+ i < num_memchunks && (bi = bi_find(bi, BI_MEMCHUNK)); i++) {
+ ranges[i].start = bi->mem_info.addr;
+ ranges[i].end = bi->mem_info.addr + bi->mem_info.size - 1;
+ ranges[i].type = RANGE_RAM;
+ }
+
+ *range = ranges;
+ return i;
+}
+
+void bootinfo_set_cmdline(const char *cmdline)
+{
+ struct bi_rec *bi;
+ uint16_t size;
+
+ /* Remove existing command line records */
+ bi_remove(BI_COMMAND_LINE);
+
+ if (!cmdline)
+ return;
+
+ /* Add new command line record */
+ size = strlen(cmdline) + 1;
+ bi = bi_add(BI_COMMAND_LINE, size);
+ memcpy(bi->string, cmdline, size);
+}
+
+void bootinfo_set_ramdisk(unsigned long ramdisk_addr,
+ unsigned long ramdisk_size)
+{
+ struct bi_rec *bi;
+
+ /* Remove existing ramdisk records */
+ bi_remove(BI_RAMDISK);
+
+ if (!ramdisk_size)
+ return;
+
+ /* Add new ramdisk record */
+ bi = bi_add(BI_RAMDISK, sizeof(bi->mem_info));
+ bi->mem_info.addr = ramdisk_addr;
+ bi->mem_info.size = ramdisk_size;
+}
+
+
+ /*
+ * Check the bootinfo version in the kernel image
+ * All failures are non-fatal, as kexec may be used to load
+ * non-Linux images
+ */
+
+void bootinfo_check_bootversion(const struct kexec_info *info)
+{
+ struct bi_rec *bi;
+ const struct bootversion *bv;
+ uint16_t major, minor;
+ unsigned int i;
+
+ bv = info->segment[0].buf;
+ if (bv->magic != BOOTINFOV_MAGIC) {
+ printf("WARNING: No bootversion in kernel image\n");
+ return;
+ }
+
+ bi = bi_find(NULL, BI_MACHTYPE);
+ if (!bi) {
+ printf("WARNING: No machtype in bootinfo\n");
+ return;
+ }
+
+ for (i = 0; bv->machversions[i].machtype != bi->machtype; i++)
+ if (!bv->machversions[i].machtype) {
+ printf("WARNING: Machtype 0x%08x not in kernel bootversion\n",
+ bi->machtype);
+ return;
+ }
+
+ major = BI_VERSION_MAJOR(bv->machversions[i].version);
+ minor = BI_VERSION_MINOR(bv->machversions[i].version);
+ dbgprintf("Kernel uses bootversion %u.%u\n", major, minor);
+ if (major != SUPPORTED_BOOTINFO_VERSION)
+ printf("WARNING: Kernel bootversion %u.%u is too %s for this kexec (expected %u.x)\n",
+ major, minor,
+ major < SUPPORTED_BOOTINFO_VERSION ? "old" : "new",
+ SUPPORTED_BOOTINFO_VERSION);
+}
+
+void add_bootinfo(struct kexec_info *info, unsigned long addr)
+{
+ add_buffer(info, bootinfo, bootinfo_size, bootinfo_size,
+ sizeof(void *), addr, 0x0fffffff, 1);
+}
diff --git a/kexec/arch/m68k/bootinfo.h b/kexec/arch/m68k/bootinfo.h
new file mode 100644
index 000000000000..b6f453de955f
--- /dev/null
+++ b/kexec/arch/m68k/bootinfo.h
@@ -0,0 +1,43 @@
+#include <asm/bootinfo.h>
+
+#define DEFAULT_BOOTINFO_FILE "/proc/bootinfo"
+#define MAX_BOOTINFO_SIZE 1536
+
+
+ /*
+ * Convenience overlay of several struct bi_record variants
+ */
+
+struct bi_rec {
+ __be16 tag;
+ __be16 size;
+ union {
+ __be32 data[0];
+ /* shorthands for the types we use */
+ __be32 machtype;
+ struct {
+ __be32 addr;
+ __be32 size;
+ } mem_info;
+ char string[0];
+ };
+};
+
+
+ /*
+ * We only support the "new" tagged bootinfo (v2)
+ */
+
+#define SUPPORTED_BOOTINFO_VERSION 2
+
+
+extern const char *bootinfo_file;
+
+extern void bootinfo_load(void);
+extern void bootinfo_print(void);
+extern int bootinfo_get_memory_ranges(struct memory_range **range);
+extern void bootinfo_set_cmdline(const char *cmdline);
+extern void bootinfo_set_ramdisk(unsigned long ramdisk_addr,
+ unsigned long ramdisk_size);
+extern void bootinfo_check_bootversion(const struct kexec_info *info);
+extern void add_bootinfo(struct kexec_info *info, unsigned long addr);
diff --git a/kexec/arch/m68k/include/arch/options.h b/kexec/arch/m68k/include/arch/options.h
new file mode 100644
index 000000000000..f279d54b265b
--- /dev/null
+++ b/kexec/arch/m68k/include/arch/options.h
@@ -0,0 +1,45 @@
+#ifndef KEXEC_ARCH_M68K_OPTIONS_H
+#define KEXEC_ARCH_M68K_OPTIONS_H
+
+#define OPT_ARCH_MAX (OPT_MAX+0)
+
+/* All 'local' loader options: */
+#define OPT_APPEND (OPT_ARCH_MAX+0)
+#define OPT_REUSE_CMDLINE (OPT_ARCH_MAX+1)
+#define OPT_RAMDISK (OPT_ARCH_MAX+2)
+#define OPT_BOOTINFO (OPT_ARCH_MAX+3)
+
+/* Options relevant to the architecture (excluding loader-specific ones),
+ * in this case none:
+ */
+#define KEXEC_ARCH_OPTIONS \
+ KEXEC_OPTIONS \
+
+#define KEXEC_ARCH_OPT_STR KEXEC_OPT_STR ""
+
+/* The following two #defines list ALL of the options added by all of the
+ * architecture's loaders.
+ * o main() uses this complete list to scan for its options, ignoring
+ * arch-specific/loader-specific ones.
+ * o Then, arch_process_options() uses this complete list to scan for its
+ * options, ignoring general/loader-specific ones.
+ * o Then, the file_type[n].load re-scans for options, using
+ * KEXEC_ARCH_OPTIONS plus its loader-specific options subset.
+ * Any unrecognised options cause an error here.
+ *
+ * This is done so that main()'s/arch_process_options()'s getopt_long() calls
+ * don't choose a kernel filename from random arguments to options they don't
+ * recognise -- as they now recognise (if not act upon) all possible options.
+ */
+#define KEXEC_ALL_OPTIONS \
+ KEXEC_ARCH_OPTIONS \
+ { "command-line", 1, NULL, OPT_APPEND }, \
+ { "append", 1, NULL, OPT_APPEND }, \
+ { "reuse-cmdline", 0, NULL, OPT_REUSE_CMDLINE }, \
+ { "ramdisk", 1, NULL, OPT_RAMDISK }, \
+ { "initrd", 1, NULL, OPT_RAMDISK }, \
+ { "bootinfo", 1, NULL, OPT_BOOTINFO },
+
+#define KEXEC_ALL_OPT_STR KEXEC_ARCH_OPT_STR
+
+#endif /* KEXEC_ARCH_M68K_OPTIONS_H */
diff --git a/kexec/arch/m68k/kexec-elf-m68k.c b/kexec/arch/m68k/kexec-elf-m68k.c
new file mode 100644
index 000000000000..8d00eb98539e
--- /dev/null
+++ b/kexec/arch/m68k/kexec-elf-m68k.c
@@ -0,0 +1,182 @@
+/*
+ * kexec-elf-m68k.c - kexec Elf loader for m68k
+ *
+ * Copyright (C) 2013 Geert Uytterhoeven
+ *
+ * This source code is licensed under the GNU General Public License,
+ * Version 2. See the file COPYING for more details.
+*/
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <elf.h>
+#include <boot/elf_boot.h>
+#include <ip_checksum.h>
+#include "../../kexec.h"
+#include "../../kexec-elf.h"
+#include "../../kexec-syscall.h"
+#include "kexec-m68k.h"
+#include "bootinfo.h"
+#include <arch/options.h>
+
+#define KiB * 1024
+#define MiB * 1024 KiB
+
+#define PAGE_SIZE 4 KiB
+
+
+int elf_m68k_probe(const char *buf, off_t len)
+{
+ struct mem_ehdr ehdr;
+ int result;
+ result = build_elf_exec_info(buf, len, &ehdr, 0);
+ if (result < 0)
+ goto out;
+
+ /* Verify the architecuture specific bits */
+ if (ehdr.e_machine != EM_68K) {
+ /* for a different architecture */
+ fprintf(stderr, "Not for this architecture.\n");
+ result = -1;
+ goto out;
+ }
+ result = 0;
+ out:
+ free_elf_info(&ehdr);
+ return result;
+}
+
+void elf_m68k_usage(void)
+{
+ printf(" --command-line=STRING Set the kernel command line to STRING\n"
+ " --append=STRING Set the kernel command line to STRING\n"
+ " --reuse-cmdline Use kernel command line from running system.\n"
+ " --ramdisk=FILE Use FILE as the kernel's initial ramdisk.\n"
+ " --initrd=FILE Use FILE as the kernel's initial ramdisk.\n"
+ " --bootinfo=FILE Use FILE as the kernel's bootinfo\n"
+ );
+}
+
+static unsigned long segment_end(const struct kexec_info *info, int i)
+{
+ return (unsigned long)info->segment[i].mem + info->segment[i].memsz - 1;
+}
+
+int elf_m68k_load(int argc, char **argv, const char *buf, off_t len,
+ struct kexec_info *info)
+{
+ struct mem_ehdr ehdr;
+ const char *cmdline = NULL, *ramdisk_file = NULL;
+ int opt, result, i;
+ unsigned long bootinfo_addr, ramdisk_addr = 0;
+ off_t ramdisk_size = 0;
+
+ /* See options.h if adding any more options. */
+ static const struct option options[] = {
+ KEXEC_ARCH_OPTIONS
+ { "command-line", 1, NULL, OPT_APPEND },
+ { "append", 1, NULL, OPT_APPEND },
+ { "reuse-cmdline", 0, NULL, OPT_REUSE_CMDLINE },
+ { "ramdisk", 1, NULL, OPT_RAMDISK },
+ { "initrd", 1, NULL, OPT_RAMDISK },
+ { "bootinfo", 1, NULL, OPT_BOOTINFO },
+ { 0, 0, NULL, 0 },
+ };
+
+ static const char short_options[] = KEXEC_ARCH_OPT_STR "d";
+
+ while ((opt = getopt_long(argc, argv, short_options, options, 0)) !=
+ -1) {
+ switch (opt) {
+ default:
+ /* Ignore core options */
+ if (opt < OPT_ARCH_MAX)
+ break;
+ case OPT_APPEND:
+ cmdline = optarg;
+ break;
+ case OPT_REUSE_CMDLINE:
+ cmdline = get_command_line();
+ break;
+ case OPT_RAMDISK:
+ ramdisk_file = optarg;
+ break;
+ case OPT_BOOTINFO:
+ break;
+ }
+ }
+
+ result = build_elf_exec_info(buf, len, &ehdr, 0);
+ if (result < 0)
+ die("ELF exec parse failed\n");
+
+ /* Fixup PT_LOAD segments that include the ELF header (offset zero) */
+ for (i = 0; i < ehdr.e_phnum; i++) {
+ struct mem_phdr *phdr;
+ phdr = &ehdr.e_phdr[i];
+ if (phdr->p_type != PT_LOAD || phdr->p_offset)
+ continue;
+
+ dbgprintf("Removing ELF header from segment %d\n", i);
+ phdr->p_paddr += PAGE_SIZE;
+ phdr->p_vaddr += PAGE_SIZE;
+ phdr->p_filesz -= PAGE_SIZE;
+ phdr->p_memsz -= PAGE_SIZE;
+ phdr->p_offset += PAGE_SIZE;
+ phdr->p_data += PAGE_SIZE;
+ }
+
+ /* Load the ELF data */
+ result = elf_exec_load(&ehdr, info);
+ if (result < 0)
+ die("ELF exec load failed\n");
+
+ info->entry = (void *)virt_to_phys(ehdr.e_entry);
+
+ /* Bootinfo must be stored right after the kernel */
+ bootinfo_addr = segment_end(info, info->nr_segments - 1) + 1;
+
+ /* Load ramdisk */
+ if (ramdisk_file) {
+ void *ramdisk = slurp_decompress_file(ramdisk_file,
+ &ramdisk_size);
+ /* Store ramdisk at top of first memory chunk */
+ ramdisk_addr = _ALIGN_DOWN(info->memory_range[0].end -
+ ramdisk_size + 1,
+ PAGE_SIZE);
+ if (!buf)
+ die("Ramdisk load failed\n");
+ add_buffer(info, ramdisk, ramdisk_size, ramdisk_size,
+ PAGE_SIZE, ramdisk_addr, info->memory_range[0].end,
+ 1);
+ }
+
+ /* Update and add bootinfo */
+ bootinfo_set_cmdline(cmdline);
+ bootinfo_set_ramdisk(ramdisk_addr, ramdisk_size);
+ if (kexec_debug)
+ bootinfo_print();
+ add_bootinfo(info, bootinfo_addr);
+
+ /*
+ * Check if the kernel (and bootinfo) exceed 4 MiB, as current kernels
+ * don't support that.
+ * As the segments are still unsorted, the bootinfo is located in the
+ * last segment.
+ */
+ if (segment_end(info, info->nr_segments - 1) >= virt_to_phys(4 MiB - 1))
+ printf("WARNING: Kernel is larger than 4 MiB\n");
+
+ /* Check struct bootversion at start of kernel */
+ bootinfo_check_bootversion(info);
+
+ return 0;
+}
diff --git a/kexec/arch/m68k/kexec-elf-rel-m68k.c b/kexec/arch/m68k/kexec-elf-rel-m68k.c
new file mode 100644
index 000000000000..fa12a1670149
--- /dev/null
+++ b/kexec/arch/m68k/kexec-elf-rel-m68k.c
@@ -0,0 +1,37 @@
+/*
+ * kexec-elf-rel-m68k.c - kexec Elf relocation routines
+ *
+ * Copyright (C) 2013 Geert Uytterhoeven
+ *
+ * This source code is licensed under the GNU General Public License,
+ * Version 2. See the file COPYING for more details.
+*/
+
+#include <stdio.h>
+#include <elf.h>
+#include "../../kexec.h"
+#include "../../kexec-elf.h"
+
+int machine_verify_elf_rel(struct mem_ehdr *ehdr)
+{
+ if (ehdr->ei_data != ELFDATA2MSB)
+ return 0;
+ if (ehdr->ei_class != ELFCLASS32)
+ return 0;
+ if (ehdr->e_machine != EM_68K)
+ return 0;
+ return 1;
+}
+
+void machine_apply_elf_rel(struct mem_ehdr *UNUSED(ehdr), unsigned long r_type,
+ void *UNUSED(location),
+ unsigned long UNUSED(address),
+ unsigned long UNUSED(value))
+{
+ switch (r_type) {
+ default:
+ die("Unknown rela relocation: %lu\n", r_type);
+ break;
+ }
+ return;
+}
diff --git a/kexec/arch/m68k/kexec-m68k.c b/kexec/arch/m68k/kexec-m68k.c
new file mode 100644
index 000000000000..372aa378f9a1
--- /dev/null
+++ b/kexec/arch/m68k/kexec-m68k.c
@@ -0,0 +1,104 @@
+/*
+ * kexec-m68k.c - kexec for m68k
+ *
+ * Copyright (C) 2013 Geert Uytterhoeven
+ *
+ * This source code is licensed under the GNU General Public License,
+ * Version 2. See the file COPYING for more details.
+ */
+
+#include <stddef.h>
+#include <stdio.h>
+#include <errno.h>
+#include <stdint.h>
+#include <string.h>
+#include <getopt.h>
+#include "../../kexec.h"
+#include "../../kexec-syscall.h"
+#include "kexec-m68k.h"
+#include "bootinfo.h"
+#include <arch/options.h>
+
+
+static unsigned long m68k_memoffset;
+
+
+/* Return a sorted list of memory ranges. */
+int get_memory_ranges(struct memory_range **range, int *ranges,
+ unsigned long kexec_flags)
+{
+ bootinfo_load();
+ *ranges = bootinfo_get_memory_ranges(range);
+ m68k_memoffset = (*range)[0].start;
+ return 0;
+}
+
+
+struct file_type file_type[] = {
+ {"elf-m68k", elf_m68k_probe, elf_m68k_load, elf_m68k_usage},
+};
+int file_types = sizeof(file_type) / sizeof(file_type[0]);
+
+void arch_usage(void)
+{
+}
+
+int arch_process_options(int argc, char **argv)
+{
+ static const struct option options[] = {
+ KEXEC_ALL_OPTIONS
+ { "bootinfo", 1, NULL, OPT_BOOTINFO },
+ { 0, 0, NULL, 0 },
+ };
+ static const char short_options[] = KEXEC_ALL_OPT_STR;
+ int opt;
+
+ opterr = 0; /* Don't complain about unrecognized options here */
+ while ((opt = getopt_long(argc, argv, short_options, options, 0)) !=
+ -1) {
+ switch (opt) {
+ default:
+ break;
+ case OPT_BOOTINFO:
+ bootinfo_file = optarg;
+ break;
+ }
+ }
+ /* Reset getopt for the next pass; called in other source modules */
+ opterr = 1;
+ optind = 1;
+ return 0;
+}
+
+const struct arch_map_entry arches[] = {
+ { "m68k", KEXEC_ARCH_68K },
+ { NULL, 0 },
+};
+
+int arch_compat_trampoline(struct kexec_info *UNUSED(info))
+{
+ return 0;
+}
+
+void arch_update_purgatory(struct kexec_info *UNUSED(info))
+{
+}
+
+int is_crashkernel_mem_reserved(void)
+{
+ return 0;
+}
+
+unsigned long virt_to_phys(unsigned long addr)
+{
+ return addr + m68k_memoffset;
+}
+
+/*
+ * add_segment() should convert base to a physical address on m68k,
+ * while the default is just to work with base as is */
+void add_segment(struct kexec_info *info, const void *buf, size_t bufsz,
+ unsigned long base, size_t memsz)
+{
+ add_segment_phys_virt(info, buf, bufsz, base, memsz, 1);
+}
diff --git a/kexec/arch/m68k/kexec-m68k.h b/kexec/arch/m68k/kexec-m68k.h
new file mode 100644
index 000000000000..99482c49db96
--- /dev/null
+++ b/kexec/arch/m68k/kexec-m68k.h
@@ -0,0 +1,9 @@
+#ifndef KEXEC_M68K_H
+#define KEXEC_M68K_H
+
+int elf_m68k_probe(const char *buf, off_t len);
+int elf_m68k_load(int argc, char **argv, const char *buf, off_t len,
+ struct kexec_info *info);
+void elf_m68k_usage(void);
+
+#endif /* KEXEC_M68K_H */
diff --git a/kexec/kexec-syscall.h b/kexec/kexec-syscall.h
index b56cb00feadc..6238044198e5 100644
--- a/kexec/kexec-syscall.h
+++ b/kexec/kexec-syscall.h
@@ -45,6 +45,9 @@
#if defined(__mips__)
#define __NR_kexec_load 4311
#endif
+#ifdef __m68k__
+#define __NR_kexec_load 313
+#endif
#ifndef __NR_kexec_load
#error Unknown processor architecture. Needs a kexec_load syscall number.
#endif
@@ -67,6 +70,7 @@ static inline long kexec_load(void *entry, unsigned long nr_segments,
*/
#define KEXEC_ARCH_DEFAULT ( 0 << 16)
#define KEXEC_ARCH_386 ( 3 << 16)
+#define KEXEC_ARCH_68K ( 4 << 16)
#define KEXEC_ARCH_X86_64 (62 << 16)
#define KEXEC_ARCH_PPC (20 << 16)
#define KEXEC_ARCH_PPC64 (21 << 16)
@@ -114,5 +118,8 @@ static inline long kexec_load(void *entry, unsigned long nr_segments,
#if defined(__mips__)
#define KEXEC_ARCH_NATIVE KEXEC_ARCH_MIPS
#endif
+#ifdef __m68k__
+#define KEXEC_ARCH_NATIVE KEXEC_ARCH_68K
+#endif

#endif /* KEXEC_SYSCALL_H */
--
1.7.9.5
Andreas Schwab
2014-09-22 20:04:59 UTC
Permalink
Signed-off-by: Andreas Schwab <***@linux-m68k.org>
---
kexec/arch/m68k/Makefile | 1 +
1 file changed, 1 insertion(+)

diff --git a/kexec/arch/m68k/Makefile b/kexec/arch/m68k/Makefile
index 7b13564..eeaacbd 100644
--- a/kexec/arch/m68k/Makefile
+++ b/kexec/arch/m68k/Makefile
@@ -10,5 +10,6 @@ m68k_ADD_SEGMENT =
m68k_VIRT_TO_PHYS =

dist += kexec/arch/m68k/Makefile $(m68k_KEXEC_SRCS) \
+ kexec/arch/m68k/bootinfo.h \
kexec/arch/m68k/kexec-m68k.h \
kexec/arch/m68k/include/arch/options.h
--
2.1.1
--
Andreas Schwab, ***@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5
"And now for something completely different."
Andreas Schwab
2014-10-12 19:37:17 UTC
Permalink
Signed-off-by: Andreas Schwab <***@linux-m68k.org>
---
kexec/arch/m68k/Makefile | 1 +
1 file changed, 1 insertion(+)

diff --git a/kexec/arch/m68k/Makefile b/kexec/arch/m68k/Makefile
index 7b13564..eeaacbd 100644
--- a/kexec/arch/m68k/Makefile
+++ b/kexec/arch/m68k/Makefile
@@ -10,5 +10,6 @@ m68k_ADD_SEGMENT =
m68k_VIRT_TO_PHYS =

dist += kexec/arch/m68k/Makefile $(m68k_KEXEC_SRCS) \
+ kexec/arch/m68k/bootinfo.h \
kexec/arch/m68k/kexec-m68k.h \
kexec/arch/m68k/include/arch/options.h
--
2.1.2
--
Andreas Schwab, ***@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5
"And now for something completely different."
Simon Horman
2014-10-14 05:06:40 UTC
Permalink
Thanks, applied. Sorry for missing this earlier.
Post by Andreas Schwab
---
kexec/arch/m68k/Makefile | 1 +
1 file changed, 1 insertion(+)
diff --git a/kexec/arch/m68k/Makefile b/kexec/arch/m68k/Makefile
index 7b13564..eeaacbd 100644
--- a/kexec/arch/m68k/Makefile
+++ b/kexec/arch/m68k/Makefile
@@ -10,5 +10,6 @@ m68k_ADD_SEGMENT =
m68k_VIRT_TO_PHYS =
dist += kexec/arch/m68k/Makefile $(m68k_KEXEC_SRCS) \
+ kexec/arch/m68k/bootinfo.h \
kexec/arch/m68k/kexec-m68k.h \
kexec/arch/m68k/include/arch/options.h
--
2.1.2
--
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5
"And now for something completely different."
Geert Uytterhoeven
2013-10-15 17:50:59 UTC
Permalink
Factor out the common part of slurp_file() and slurp_file_len() into
a new helper function slurp_fd().

Signed-off-by: Geert Uytterhoeven <***@linux-m68k.org>
Cc: Dave Young <***@redhat.com>
---
kexec/kexec.c | 92 +++++++++++++++++++++++++++------------------------------
1 file changed, 43 insertions(+), 49 deletions(-)

diff --git a/kexec/kexec.c b/kexec/kexec.c
index 185c85bef342..583a35a24a70 100644
--- a/kexec/kexec.c
+++ b/kexec/kexec.c
@@ -469,14 +469,46 @@ static int add_backup_segments(struct kexec_info *info,
return 0;
}

+static char *slurp_fd(int fd, const char *filename, off_t size, off_t *nread)
+{
+ char *buf;
+ off_t progress;
+ ssize_t result;
+
+ buf = xmalloc(size);
+ progress = 0;
+ while (progress < size) {
+ result = read(fd, buf + progress, size - progress);
+ if (result < 0) {
+ if ((errno == EINTR) || (errno == EAGAIN))
+ continue;
+ fprintf(stderr, "Read on %s failed: %s\n", filename,
+ strerror(errno));
+ free(buf);
+ close(fd);
+ return NULL;
+ }
+ if (result == 0)
+ /* EOF */
+ break;
+ progress += result;
+ }
+ result = close(fd);
+ if (result < 0)
+ die("Close of %s failed: %s\n", filename, strerror(errno));
+
+ if (nread)
+ *nread = progress;
+ return buf;
+}
+
char *slurp_file(const char *filename, off_t *r_size)
{
int fd;
char *buf;
- off_t size, progress, err;
+ off_t size, err, nread;
ssize_t result;
struct stat stats;
-

if (!filename) {
*r_size = 0;
@@ -512,25 +544,14 @@ char *slurp_file(const char *filename, off_t *r_size)
size = stats.st_size;
}

+ buf = slurp_fd(fd, filename, size, &nread);
+ if (!buf)
+ die("Cannot read %s", filename);
+
+ if (nread != size)
+ die("Read on %s ended before stat said it should\n", filename);
+
*r_size = size;
- buf = xmalloc(size);
- progress = 0;
- while(progress < size) {
- result = read(fd, buf + progress, size - progress);
- if (result < 0) {
- if ((errno == EINTR) || (errno == EAGAIN))
- continue;
- die("read on %s of %ld bytes failed: %s\n", filename,
- (size - progress)+ 0UL, strerror(errno));
- }
- if (result == 0)
- die("read on %s ended before stat said it should\n", filename);
- progress += result;
- }
- result = close(fd);
- if (result < 0) {
- die("Close of %s failed: %s\n", filename, strerror(errno));
- }
return buf;
}

@@ -540,9 +561,6 @@ char *slurp_file(const char *filename, off_t *r_size)
char *slurp_file_len(const char *filename, off_t size, off_t *nread)
{
int fd;
- char *buf;
- off_t progress;
- ssize_t result;

if (!filename)
return 0;
@@ -552,32 +570,8 @@ char *slurp_file_len(const char *filename, off_t size, off_t *nread)
strerror(errno));
return 0;
}
- buf = xmalloc(size);
- progress = 0;
- while(progress < size) {
- result = read(fd, buf + progress, size - progress);
- if (result < 0) {
- if ((errno == EINTR) || (errno == EAGAIN))
- continue;
- fprintf(stderr, "read on %s of %ld bytes failed: %s\n",
- filename, (size - progress)+ 0UL,
- strerror(errno));
- free(buf);
- return 0;
- }
- if (result == 0)
- /* EOF */
- break;
- progress += result;
- }
- result = close(fd);
- if (result < 0) {
- die("Close of %s failed: %s\n",
- filename, strerror(errno));
- }
- if (nread)
- *nread = progress;
- return buf;
+
+ return slurp_fd(fd, filename, size, nread);
}

char *slurp_decompress_file(const char *filename, off_t *r_size)
--
1.7.9.5
Simon Horman
2013-10-16 07:22:36 UTC
Permalink
kexec support for Linux/m68k (tools part)
This is a set of patches to add kexec support for m68k to kexec-tools.
- Kexec only, no kdump support yet (do you have enough RAM to keep a
crashdump kernel in memory at all times? ;-)
- [PATCH 1/3] kexec: Let slurp_file_len() return the number of bytes
- v2: no changes
- [PATCH 2/3] kexec: Extract slurp_fd()
- v2: new patch
- [PATCH 3/3] kexec: Add m68k support
- Fix handling of virtual and physical addresses, for machines where
memory doesn't start at zero,
- Print a warning if the kernel size exceeds 4 MiB, as current kernels
cannot handle that,
- Check struct bootversion at the start of the kernel, and print a
warning if it cannot be found or doesn't match,
- Replace literal 4096 by PAGE_SIZE,
- Handle removal of page zero at the ELF program segment level, as
m68kboot does,
- Remove -PAGE_SIZE for the ramdisk location now the bug in
locate_hole() is fixed,
- Use endian-correct types for bootinfo,
- Remove unused -? option handling, cfr. commit
bf9d0f055c791a26b2237b5a12b48ae1b7e0d550 ("kexec: Remove unused -?
option handling"),
- Use <asm/bootinfo.h> instead of our own definitions.
- Based on git://git.kernel.org/pub/scm/utils/kernel/kexec/kexec-tools.git
- Tagged bootinfo is read from /proc/bootinfo by default, but this can be
overridden using --bootinfo. No bootinfo editor is provided.
The kexec command will replace/delete command line and ramdisk tags in the
bootinfo.
Thanks,

I've taken a quick look over this and it seems reasonable to me.
However, I wonder if I should wait for review of the kernel-side
to be completed before applying the kexec-tools patches.

Let me know what you think.
Geert Uytterhoeven
2013-11-03 14:08:50 UTC
Permalink
Post by Simon Horman
I've taken a quick look over this and it seems reasonable to me.
Thanks!
Post by Simon Horman
However, I wonder if I should wait for review of the kernel-side
to be completed before applying the kexec-tools patches.
Let me know what you think.
The kexec-tools part seems to be stable. The issues to be sorted out are on
the kernel side.

However, it doesn't hurt to wait a little bit more to apply this. We m68k
people are patient and used to multi-year development ;-)

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- ***@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
Simon Horman
2013-11-05 07:05:13 UTC
Permalink
Post by Geert Uytterhoeven
Post by Simon Horman
I've taken a quick look over this and it seems reasonable to me.
Thanks!
Post by Simon Horman
However, I wonder if I should wait for review of the kernel-side
to be completed before applying the kexec-tools patches.
Let me know what you think.
The kexec-tools part seems to be stable. The issues to be sorted out are on
the kernel side.
However, it doesn't hurt to wait a little bit more to apply this. We m68k
people are patient and used to multi-year development ;-)
It is said that patience is a virtue :^)

As the kexec-tools portion of this change isn't useful without the
kernel portion I think I would like to wait before applying this series.
Please repost once the kernel changes have been merged or if you
would like them re-considered for any other reason.
Geert Uytterhoeven
2013-12-01 11:40:27 UTC
Permalink
Hi Simon,
Post by Simon Horman
Post by Geert Uytterhoeven
Post by Simon Horman
I've taken a quick look over this and it seems reasonable to me.
Thanks!
Post by Simon Horman
However, I wonder if I should wait for review of the kernel-side
to be completed before applying the kexec-tools patches.
Let me know what you think.
The kexec-tools part seems to be stable. The issues to be sorted out are on
the kernel side.
As the kexec-tools portion of this change isn't useful without the
kernel portion I think I would like to wait before applying this series.
Please repost once the kernel changes have been merged or if you
would like them re-considered for any other reason.
The kernel part is working now.

I didn't make any changes to kexec-tools for v3.
Do you want me to resend them anyway, or can you just take v2?

Thanks again!

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- ***@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
Simon Horman
2013-12-13 00:51:11 UTC
Permalink
Post by Geert Uytterhoeven
Hi Simon,
Post by Simon Horman
Post by Geert Uytterhoeven
Post by Simon Horman
I've taken a quick look over this and it seems reasonable to me.
Thanks!
Post by Simon Horman
However, I wonder if I should wait for review of the kernel-side
to be completed before applying the kexec-tools patches.
Let me know what you think.
The kexec-tools part seems to be stable. The issues to be sorted out are on
the kernel side.
As the kexec-tools portion of this change isn't useful without the
kernel portion I think I would like to wait before applying this series.
Please repost once the kernel changes have been merged or if you
would like them re-considered for any other reason.
The kernel part is working now.
I didn't make any changes to kexec-tools for v3.
Do you want me to resend them anyway, or can you just take v2?
Sorry for being slow, I will take v2.
Simon Horman
2013-12-13 00:53:22 UTC
Permalink
kexec support for Linux/m68k (tools part)
This is a set of patches to add kexec support for m68k to kexec-tools.
- Kexec only, no kdump support yet (do you have enough RAM to keep a
crashdump kernel in memory at all times? ;-)
Thanks, applied.
Geert Uytterhoeven
2013-12-13 08:03:45 UTC
Permalink
Hi Simon,
Post by Simon Horman
kexec support for Linux/m68k (tools part)
This is a set of patches to add kexec support for m68k to kexec-tools.
- Kexec only, no kdump support yet (do you have enough RAM to keep a
crashdump kernel in memory at all times? ;-)
Thanks, applied.
Thanks, I have queued the kernel counterpart for 3.14.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- ***@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
Simon Horman
2013-12-14 00:31:55 UTC
Permalink
Post by Geert Uytterhoeven
Hi Simon,
Post by Simon Horman
kexec support for Linux/m68k (tools part)
This is a set of patches to add kexec support for m68k to kexec-tools.
- Kexec only, no kdump support yet (do you have enough RAM to keep a
crashdump kernel in memory at all times? ;-)
Thanks, applied.
Thanks, I have queued the kernel counterpart for 3.14.
Great. It sounds like it would be ideal if a new kexec-tools release
coincided with the release of v3.14 of the kernel.
Loading...