summaryrefslogtreecommitdiffstats
path: root/kexec/arch/mips
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 02:56:35 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 02:56:35 +0000
commiteba0cfa6b0bef4f2e73c8630a7efa3944df8b0f8 (patch)
tree74c37eede1f0634cc5de1c63c934edaa1630c6bc /kexec/arch/mips
parentInitial commit. (diff)
downloadkexec-tools-upstream.tar.xz
kexec-tools-upstream.zip
Adding upstream version 1:2.0.27.upstream/1%2.0.27upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'kexec/arch/mips')
-rw-r--r--kexec/arch/mips/Makefile29
-rw-r--r--kexec/arch/mips/crashdump-mips.c408
-rw-r--r--kexec/arch/mips/crashdump-mips.h30
-rw-r--r--kexec/arch/mips/include/arch/options.h43
-rw-r--r--kexec/arch/mips/kexec-elf-mips.c242
-rw-r--r--kexec/arch/mips/kexec-elf-rel-mips.c46
-rw-r--r--kexec/arch/mips/kexec-mips.c191
-rw-r--r--kexec/arch/mips/kexec-mips.h30
8 files changed, 1019 insertions, 0 deletions
diff --git a/kexec/arch/mips/Makefile b/kexec/arch/mips/Makefile
new file mode 100644
index 0000000..1fe7886
--- /dev/null
+++ b/kexec/arch/mips/Makefile
@@ -0,0 +1,29 @@
+#
+# kexec mips (linux booting linux)
+#
+mips_KEXEC_SRCS = kexec/arch/mips/kexec-mips.c
+mips_KEXEC_SRCS += kexec/arch/mips/kexec-elf-mips.c
+mips_KEXEC_SRCS += kexec/arch/mips/kexec-elf-rel-mips.c
+mips_KEXEC_SRCS += kexec/arch/mips/crashdump-mips.c
+
+mips_FS2DT = kexec/fs2dt.c
+mips_FS2DT_INCLUDE = \
+ -include $(srcdir)/kexec/arch/mips/crashdump-mips.h \
+ -include $(srcdir)/kexec/arch/mips/kexec-mips.h
+
+mips_DT_OPS += kexec/dt-ops.c
+
+include $(srcdir)/kexec/libfdt/Makefile.libfdt
+
+libfdt_SRCS += $(LIBFDT_SRCS:%=kexec/libfdt/%)
+mips_CPPFLAGS += -I$(srcdir)/kexec/libfdt
+mips_KEXEC_SRCS += $(libfdt_SRCS)
+
+mips_ADD_BUFFER =
+mips_ADD_SEGMENT =
+mips_VIRT_TO_PHYS =
+
+dist += kexec/arch/mips/Makefile $(mips_KEXEC_SRCS) \
+ kexec/arch/mips/kexec-mips.h \
+ kexec/arch/mips/crashdump-mips.h \
+ kexec/arch/mips/include/arch/options.h
diff --git a/kexec/arch/mips/crashdump-mips.c b/kexec/arch/mips/crashdump-mips.c
new file mode 100644
index 0000000..00f4335
--- /dev/null
+++ b/kexec/arch/mips/crashdump-mips.c
@@ -0,0 +1,408 @@
+/*
+ * kexec: Linux boots Linux
+ *
+ * 2005 (C) IBM Corporation.
+ * 2008 (C) MontaVista Software, Inc.
+ *
+ * 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 (version 2 of the License).
+ *
+ * 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, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <limits.h>
+#include <inttypes.h>
+#include <elf.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include "../../kexec.h"
+#include "../../kexec-elf.h"
+#include "../../kexec-syscall.h"
+#include "../../crashdump.h"
+#include "kexec-mips.h"
+#include "crashdump-mips.h"
+#include "unused.h"
+
+/* Stores a sorted list of RAM memory ranges for which to create elf headers.
+ * A separate program header is created for backup region */
+static struct memory_range crash_memory_range[CRASH_MAX_MEMORY_RANGES];
+
+/* Not used currently but required by generic fs2dt code */
+struct memory_ranges usablemem_rgns;
+
+/* Memory region reserved for storing panic kernel and other data. */
+static struct memory_range crash_reserved_mem;
+
+/* Read kernel physical load addr from the file returned by proc_iomem()
+ * (Kernel Code) and store in kexec_info */
+static int get_kernel_paddr(struct crash_elf_info *elf_info)
+{
+ uint64_t start;
+
+ if (xen_present()) /* Kernel not entity mapped under Xen */
+ return 0;
+
+ if (parse_iomem_single("Kernel code\n", &start, NULL) == 0) {
+ elf_info->kern_paddr_start = start;
+ dbgprintf("kernel load physical addr start = 0x%" PRIu64 "\n", start);
+ return 0;
+ }
+
+ fprintf(stderr, "Cannot determine kernel physical load addr\n");
+ return -1;
+}
+
+static int get_kernel_vaddr_and_size(struct crash_elf_info *elf_info,
+ unsigned long start_offset)
+{
+ uint64_t end;
+
+ if (!elf_info->kern_paddr_start)
+ return -1;
+
+ elf_info->kern_vaddr_start = elf_info->kern_paddr_start |
+ start_offset;
+ /* If "Kernel bss" exists, the kernel ends there, else fall
+ * through and say that it ends at "Kernel data" */
+ if (parse_iomem_single("Kernel bss\n", NULL, &end) == 0 ||
+ parse_iomem_single("Kernel data\n", NULL, &end) == 0) {
+ elf_info->kern_size = end - elf_info->kern_paddr_start;
+ dbgprintf("kernel_vaddr= 0x%llx paddr %llx\n",
+ elf_info->kern_vaddr_start,
+ elf_info->kern_paddr_start);
+ dbgprintf("kernel size = 0x%lx\n", elf_info->kern_size);
+ return 0;
+ }
+ fprintf(stderr, "Cannot determine kernel virtual load addr and size\n");
+ return -1;
+}
+
+/* Removes crash reserve region from list of memory chunks for whom elf program
+ * headers have to be created. Assuming crash reserve region to be a single
+ * continuous area fully contained inside one of the memory chunks */
+static int exclude_crash_reserve_region(int *nr_ranges)
+{
+ int i, j, tidx = -1;
+ unsigned long long cstart, cend;
+ struct memory_range temp_region = {
+ .start = 0,
+ .end = 0
+ };
+
+ /* Crash reserved region. */
+ cstart = crash_reserved_mem.start;
+ cend = crash_reserved_mem.end;
+
+ for (i = 0; i < (*nr_ranges); i++) {
+ unsigned long long mstart, mend;
+ mstart = crash_memory_range[i].start;
+ mend = crash_memory_range[i].end;
+ if (cstart < mend && cend > mstart) {
+ if (cstart != mstart && cend != mend) {
+ /* Split memory region */
+ crash_memory_range[i].end = cstart - 1;
+ temp_region.start = cend + 1;
+ temp_region.end = mend;
+ temp_region.type = RANGE_RAM;
+ tidx = i+1;
+ } else if (cstart != mstart)
+ crash_memory_range[i].end = cstart - 1;
+ else
+ crash_memory_range[i].start = cend + 1;
+ }
+ }
+ /* Insert split memory region, if any. */
+ if (tidx >= 0) {
+ if (*nr_ranges == CRASH_MAX_MEMORY_RANGES) {
+ /* No space to insert another element. */
+ fprintf(stderr, "Error: Number of crash memory ranges"
+ " excedeed the max limit\n");
+ return -1;
+ }
+ for (j = (*nr_ranges - 1); j >= tidx; j--)
+ crash_memory_range[j+1] = crash_memory_range[j];
+ crash_memory_range[tidx].start = temp_region.start;
+ crash_memory_range[tidx].end = temp_region.end;
+ crash_memory_range[tidx].type = temp_region.type;
+ (*nr_ranges)++;
+ }
+ return 0;
+}
+/* Reads the appropriate file and retrieves the SYSTEM RAM regions for whom to
+ * create Elf headers. Keeping it separate from get_memory_ranges() as
+ * requirements are different in the case of normal kexec and crashdumps.
+ *
+ * Normal kexec needs to look at all of available physical memory irrespective
+ * of the fact how much of it is being used by currently running kernel.
+ * Crashdumps need to have access to memory regions actually being used by
+ * running kernel. Expecting a different file/data structure than /proc/iomem
+ * to look into down the line. May be something like /proc/kernelmem or may
+ * be zone data structures exported from kernel.
+ */
+static int get_crash_memory_ranges(struct memory_range **range, int *ranges)
+{
+ const char iomem[] = "/proc/iomem";
+ int memory_ranges = 0;
+ char line[MAX_LINE];
+ FILE *fp;
+ unsigned long long start, end;
+
+ fp = fopen(iomem, "r");
+ if (!fp) {
+ fprintf(stderr, "Cannot open %s: %s\n",
+ iomem, strerror(errno));
+ return -1;
+ }
+ /* Separate segment for backup region */
+ crash_memory_range[0].start = BACKUP_SRC_START;
+ crash_memory_range[0].end = BACKUP_SRC_END;
+ crash_memory_range[0].type = RANGE_RAM;
+ memory_ranges++;
+
+ while (fgets(line, sizeof(line), fp) != 0) {
+ char *str;
+ int type, consumed, count;
+ if (memory_ranges >= CRASH_MAX_MEMORY_RANGES)
+ break;
+ count = sscanf(line, "%llx-%llx : %n",
+ &start, &end, &consumed);
+ if (count != 2)
+ continue;
+ str = line + consumed;
+
+ /* Only Dumping memory of type System RAM. */
+ if (memcmp(str, "System RAM\n", 11) == 0) {
+ type = RANGE_RAM;
+ } else if (memcmp(str, "Crash kernel\n", 13) == 0) {
+ /* Reserved memory region. New kernel can
+ * use this region to boot into. */
+ crash_reserved_mem.start = start;
+ crash_reserved_mem.end = end;
+ crash_reserved_mem.type = RANGE_RAM;
+ continue;
+ } else
+ continue;
+
+ if (start == BACKUP_SRC_START && end >= (BACKUP_SRC_END + 1))
+ start = BACKUP_SRC_END + 1;
+
+ crash_memory_range[memory_ranges].start = start;
+ crash_memory_range[memory_ranges].end = end;
+ crash_memory_range[memory_ranges].type = type;
+ memory_ranges++;
+
+ /* Segregate linearly mapped region. */
+ if (MAXMEM && (MAXMEM - 1) >= start && (MAXMEM - 1) <= end) {
+ crash_memory_range[memory_ranges - 1].end = MAXMEM - 1;
+
+ /* Add segregated region. */
+ crash_memory_range[memory_ranges].start = MAXMEM;
+ crash_memory_range[memory_ranges].end = end;
+ crash_memory_range[memory_ranges].type = type;
+ memory_ranges++;
+ }
+ }
+ fclose(fp);
+
+ if (exclude_crash_reserve_region(&memory_ranges) < 0)
+ return -1;
+
+ *range = crash_memory_range;
+ *ranges = memory_ranges;
+ return 0;
+}
+
+/* Adds the appropriate mem= options to command line, indicating the
+ * memory region the new kernel can use to boot into. */
+static int cmdline_add_mem(char *cmdline, unsigned long addr,
+ unsigned long size)
+{
+ int cmdlen, len;
+ char str[50], *ptr;
+
+ addr = addr/1024;
+ size = size/1024;
+ ptr = str;
+ strcpy(str, " mem=");
+ ptr += strlen(str);
+ ultoa(size, ptr);
+ strcat(str, "K@");
+ ptr = str + strlen(str);
+ ultoa(addr, ptr);
+ strcat(str, "K");
+ len = strlen(str);
+ cmdlen = strlen(cmdline) + len;
+ if (cmdlen > (COMMAND_LINE_SIZE - 1))
+ die("Command line overflow\n");
+ strcat(cmdline, str);
+
+ return 0;
+}
+
+/* Adds the elfcorehdr= command line parameter to command line. */
+static int cmdline_add_elfcorehdr(char *cmdline, unsigned long addr)
+{
+ int cmdlen, len, align = 1024;
+ char str[30], *ptr;
+
+ /* Passing in elfcorehdr=xxxK format. Saves space required in cmdline.
+ * Ensure 1K alignment*/
+ if (addr%align)
+ return -1;
+ addr = addr/align;
+ ptr = str;
+ strcpy(str, " elfcorehdr=");
+ ptr += strlen(str);
+ ultoa(addr, ptr);
+ strcat(str, "K");
+ len = strlen(str);
+ cmdlen = strlen(cmdline) + len;
+ if (cmdlen > (COMMAND_LINE_SIZE - 1))
+ die("Command line overflow\n");
+ strcat(cmdline, str);
+ return 0;
+}
+
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+# define ELFDATALOCAL ELFDATA2LSB
+#elif __BYTE_ORDER == __BIG_ENDIAN
+# define ELFDATALOCAL ELFDATA2MSB
+#else
+# error Unknown byte order
+#endif
+
+static struct crash_elf_info elf_info64 = {
+ class: ELFCLASS64,
+ data : ELFDATALOCAL,
+ machine : EM_MIPS,
+ page_offset : PAGE_OFFSET,
+ lowmem_limit : 0, /* 0 == no limit */
+};
+
+static struct crash_elf_info elf_info32 = {
+ class: ELFCLASS32,
+ data : ELFDATALOCAL,
+ machine : EM_MIPS,
+ page_offset : PAGE_OFFSET,
+ lowmem_limit : MAXMEM,
+};
+
+static int patch_elf_info(void)
+{
+ const char cpuinfo[] = "/proc/cpuinfo";
+ char line[MAX_LINE];
+ FILE *fp;
+
+ fp = fopen(cpuinfo, "r");
+ if (!fp) {
+ fprintf(stderr, "Cannot open %s: %s\n",
+ cpuinfo, strerror(errno));
+ return -1;
+ }
+ while (fgets(line, sizeof(line), fp) != 0) {
+ if (strncmp(line, "cpu model", 9) == 0) {
+ /* OCTEON uses a different page_offset. */
+ if (strstr(line, "Octeon"))
+ elf_info64.page_offset = OCTEON_PAGE_OFFSET;
+ /* LOONGSON uses a different page_offset. */
+ else if (strstr(line, "Loongson"))
+ elf_info64.page_offset = LOONGSON_PAGE_OFFSET;
+ break;
+ }
+ }
+ fclose(fp);
+ return 0;
+}
+
+/* Loads additional segments in case of a panic kernel is being loaded.
+ * One segment for backup region, another segment for storing elf headers
+ * for crash memory image.
+ */
+int load_crashdump_segments(struct kexec_info *info, char* mod_cmdline,
+ unsigned long UNUSED(max_addr),
+ unsigned long UNUSED(min_base))
+{
+ void *tmp;
+ unsigned long sz, elfcorehdr;
+ int nr_ranges, align = 1024;
+ struct memory_range *mem_range;
+ crash_create_elf_headers_func crash_create = crash_create_elf32_headers;
+ struct crash_elf_info *elf_info = &elf_info32;
+ unsigned long start_offset = 0x80000000UL;
+
+ if (patch_elf_info())
+ return -1;
+
+ if (arch_options.core_header_type == CORE_TYPE_ELF64) {
+ elf_info = &elf_info64;
+ crash_create = crash_create_elf64_headers;
+ start_offset = (unsigned long)0xffffffff80000000UL;
+ }
+
+ if (get_kernel_paddr(elf_info))
+ return -1;
+
+ if (get_kernel_vaddr_and_size(elf_info, start_offset))
+ return -1;
+
+ if (get_crash_memory_ranges(&mem_range, &nr_ranges) < 0)
+ return -1;
+
+ info->backup_src_start = BACKUP_SRC_START;
+ info->backup_src_size = BACKUP_SRC_SIZE;
+ /* Create a backup region segment to store backup data*/
+ sz = _ALIGN(BACKUP_SRC_SIZE, align);
+ tmp = xmalloc(sz);
+ memset(tmp, 0, sz);
+ info->backup_start = add_buffer(info, tmp, sz, sz, align,
+ crash_reserved_mem.start,
+ crash_reserved_mem.end, -1);
+
+ if (crash_create(info, elf_info, crash_memory_range, nr_ranges,
+ &tmp, &sz, ELF_CORE_HEADER_ALIGN) < 0) {
+ free(tmp);
+ return -1;
+ }
+
+ elfcorehdr = add_buffer(info, tmp, sz, sz, align,
+ crash_reserved_mem.start,
+ crash_reserved_mem.end, -1);
+
+ /*
+ * backup segment is after elfcorehdr, so use elfcorehdr as top of
+ * kernel's available memory
+ */
+ cmdline_add_mem(mod_cmdline, crash_reserved_mem.start,
+ crash_reserved_mem.end - crash_reserved_mem.start + 1);
+ cmdline_add_elfcorehdr(mod_cmdline, elfcorehdr);
+
+ dbgprintf("CRASH MEMORY RANGES:\n");
+ dbgprintf("%016Lx-%016Lx\n", crash_reserved_mem.start,
+ crash_reserved_mem.end);
+ return 0;
+}
+
+int is_crashkernel_mem_reserved(void)
+{
+ uint64_t start, end;
+
+ return parse_iomem_single("Crash kernel\n", &start, &end) == 0 ?
+ (start != end) : 0;
+}
+
+int get_crash_kernel_load_range(uint64_t *start, uint64_t *end)
+{
+ return parse_iomem_single("Crash kernel\n", start, end);
+}
diff --git a/kexec/arch/mips/crashdump-mips.h b/kexec/arch/mips/crashdump-mips.h
new file mode 100644
index 0000000..55c9925
--- /dev/null
+++ b/kexec/arch/mips/crashdump-mips.h
@@ -0,0 +1,30 @@
+#ifndef CRASHDUMP_MIPS_H
+#define CRASHDUMP_MIPS_H
+
+struct kexec_info;
+int load_crashdump_segments(struct kexec_info *info, char *mod_cmdline,
+ unsigned long max_addr, unsigned long min_base);
+#ifdef __mips64
+#define PAGE_OFFSET 0xa800000000000000ULL
+#define MAXMEM 0
+#else
+#define PAGE_OFFSET 0x80000000
+#define MAXMEM 0x80000000
+#endif
+#define __pa(x) ((unsigned long)(X) & 0x7fffffff)
+
+#define LOONGSON_PAGE_OFFSET 0xffffffff80000000ULL
+#define OCTEON_PAGE_OFFSET 0x8000000000000000ULL
+
+#define CRASH_MAX_MEMMAP_NR (KEXEC_MAX_SEGMENTS + 1)
+#define CRASH_MAX_MEMORY_RANGES (MAX_MEMORY_RANGES + 2)
+
+#define COMMAND_LINE_SIZE 512
+
+/* Backup Region, First 1M of System RAM. */
+#define BACKUP_SRC_START 0x00000000
+#define BACKUP_SRC_END 0x000fffff
+#define BACKUP_SRC_SIZE (BACKUP_SRC_END - BACKUP_SRC_START + 1)
+
+extern struct arch_options_t arch_options;
+#endif /* CRASHDUMP_MIPS_H */
diff --git a/kexec/arch/mips/include/arch/options.h b/kexec/arch/mips/include/arch/options.h
new file mode 100644
index 0000000..ba2f346
--- /dev/null
+++ b/kexec/arch/mips/include/arch/options.h
@@ -0,0 +1,43 @@
+#ifndef KEXEC_ARCH_MIPS_OPTIONS_H
+#define KEXEC_ARCH_MIPS_OPTIONS_H
+
+#define OPT_ARCH_MAX (OPT_MAX+0)
+#define OPT_APPEND (OPT_ARCH_MAX+0)
+#define OPT_DTB (OPT_ARCH_MAX+1)
+#define OPT_RAMDISK (OPT_ARCH_MAX+2)
+#define OPT_REUSE_CMDLINE (OPT_ARCH_MAX+3)
+
+/* Options relevant to the architecture (excluding loader-specific ones),
+ * in this case none:
+ */
+#define KEXEC_ARCH_OPTIONS \
+ KEXEC_OPTIONS \
+ {"command-line", 1, 0, OPT_APPEND}, \
+ {"append", 1, 0, OPT_APPEND}, \
+ {"dtb", 1, 0, OPT_DTB }, \
+ {"initrd", 1, 0, OPT_RAMDISK }, \
+ { "reuse-cmdline", 0, NULL, OPT_REUSE_CMDLINE },
+
+
+#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
+
+#define KEXEC_ALL_OPT_STR KEXEC_ARCH_OPT_STR
+
+#endif /* KEXEC_ARCH_MIPS_OPTIONS_H */
diff --git a/kexec/arch/mips/kexec-elf-mips.c b/kexec/arch/mips/kexec-elf-mips.c
new file mode 100644
index 0000000..230d806
--- /dev/null
+++ b/kexec/arch/mips/kexec-elf-mips.c
@@ -0,0 +1,242 @@
+/*
+ * kexec-elf-mips.c - kexec Elf loader for mips
+ * Copyright (C) 2007 Francesco Chiechi, Alessandro Rubini
+ * Copyright (C) 2007 Tvblob s.r.l.
+ *
+ * derived from ../ppc/kexec-elf-ppc.c
+ * Copyright (C) 2004 Albert Herranz
+ *
+ * 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-mips.h"
+#include "crashdump-mips.h"
+#include <arch/options.h>
+#include "../../fs2dt.h"
+#include "../../dt-ops.h"
+
+static const int probe_debug = 0;
+
+#define BOOTLOADER "kexec"
+#define UPSZ(X) _ALIGN_UP(sizeof(X), 4)
+
+#define CMDLINE_PREFIX "kexec "
+static char cmdline_buf[COMMAND_LINE_SIZE] = CMDLINE_PREFIX;
+
+/* Adds initrd parameters to command line. */
+static int cmdline_add_initrd(char *cmdline, unsigned long addr, char *new_para)
+{
+ int cmdlen, len;
+ char str[30], *ptr;
+
+ ptr = str;
+ strcpy(str, new_para);
+ ptr += strlen(str);
+ ultoa(addr, ptr);
+ len = strlen(str);
+ cmdlen = strlen(cmdline) + len;
+ if (cmdlen > (COMMAND_LINE_SIZE - 1))
+ die("Command line overflow\n");
+ strcat(cmdline, str);
+
+ return 0;
+}
+
+/* add initrd to cmdline to compatible with previous platforms. */
+static int patch_initrd_info(char *cmdline, unsigned long base,
+ unsigned long size)
+{
+ const char cpuinfo[] = "/proc/cpuinfo";
+ char line[MAX_LINE];
+ FILE *fp;
+ unsigned long page_offset = PAGE_OFFSET;
+
+ fp = fopen(cpuinfo, "r");
+ if (!fp) {
+ fprintf(stderr, "Cannot open %s: %s\n",
+ cpuinfo, strerror(errno));
+ return -1;
+ }
+ while (fgets(line, sizeof(line), fp) != 0) {
+ if (strncmp(line, "cpu model", 9) == 0) {
+ if (strstr(line, "Loongson")) {
+ /* LOONGSON64 uses a different page_offset. */
+ if (arch_options.core_header_type ==
+ CORE_TYPE_ELF64)
+ page_offset = LOONGSON_PAGE_OFFSET;
+ cmdline_add_initrd(cmdline,
+ page_offset + base, " rd_start=");
+ cmdline_add_initrd(cmdline, size, " rd_size=");
+ break;
+ }
+ }
+ }
+ fclose(fp);
+ return 0;
+}
+
+int elf_mips_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_MIPS) {
+ /* for a different architecture */
+ if (probe_debug) {
+ fprintf(stderr, "Not for this architecture.\n");
+ }
+ result = -1;
+ goto out;
+ }
+ result = 0;
+ out:
+ free_elf_info(&ehdr);
+ return result;
+}
+
+void elf_mips_usage(void)
+{
+}
+
+int elf_mips_load(int argc, char **argv, const char *buf, off_t len,
+ struct kexec_info *info)
+{
+ struct mem_ehdr ehdr;
+ int command_line_len = 0;
+ char *crash_cmdline;
+ int result;
+ unsigned long cmdline_addr;
+ size_t i;
+ off_t dtb_length;
+ char *dtb_buf;
+ char *initrd_buf = NULL;
+ unsigned long long kernel_addr = 0, kernel_size = 0;
+ unsigned long pagesize = getpagesize();
+
+ /* Need to append some command line parameters internally in case of
+ * taking crash dumps.
+ */
+ if (info->kexec_flags & KEXEC_ON_CRASH) {
+ crash_cmdline = xmalloc(COMMAND_LINE_SIZE);
+ memset((void *)crash_cmdline, 0, COMMAND_LINE_SIZE);
+ } else
+ crash_cmdline = NULL;
+
+ result = build_elf_exec_info(buf, len, &ehdr, 0);
+ if (result < 0)
+ die("ELF exec parse failed\n");
+
+ /* Read in the PT_LOAD segments and remove CKSEG0 mask from address*/
+ 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_paddr = virt_to_phys(phdr->p_paddr);
+ kernel_addr = phdr->p_paddr;
+ kernel_size = phdr->p_memsz;
+ }
+ }
+
+ /* 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);
+
+ if (arch_options.command_line)
+ command_line_len = strlen(arch_options.command_line) + 1;
+
+ if (info->kexec_flags & KEXEC_ON_CRASH) {
+ result = load_crashdump_segments(info, crash_cmdline,
+ 0, 0);
+ if (result < 0) {
+ free(crash_cmdline);
+ return -1;
+ }
+ }
+
+ if (arch_options.command_line)
+ strncat(cmdline_buf, arch_options.command_line, command_line_len);
+ if (crash_cmdline)
+ {
+ strncat(cmdline_buf, crash_cmdline,
+ sizeof(crash_cmdline) -
+ strlen(crash_cmdline) - 1);
+ free(crash_cmdline);
+ }
+
+ if (info->kexec_flags & KEXEC_ON_CRASH)
+ /* In case of crashdump segment[0] is kernel.
+ * Put cmdline just after it. */
+ cmdline_addr = (unsigned long)info->segment[0].mem +
+ info->segment[0].memsz;
+ else
+ cmdline_addr = 0;
+
+ /* MIPS systems that have been converted to use device tree
+ * passed through UHI will use commandline in the DTB and
+ * the DTB passed as a separate buffer. Note that
+ * CMDLINE_PREFIX is skipped here intentionally, as it is
+ * used only in the legacy method */
+
+ if (arch_options.dtb_file) {
+ dtb_buf = slurp_file(arch_options.dtb_file, &dtb_length);
+ } else {
+ create_flatten_tree(&dtb_buf, &dtb_length, cmdline_buf + strlen(CMDLINE_PREFIX));
+ }
+
+ if (arch_options.initrd_file) {
+ initrd_buf = slurp_file(arch_options.initrd_file, &initrd_size);
+
+ /* Create initrd entries in dtb - although at this time
+ * they would not point to the correct location */
+ dtb_set_initrd(&dtb_buf, &dtb_length, (off_t)initrd_buf, (off_t)initrd_buf + initrd_size);
+
+ initrd_base = add_buffer(info, initrd_buf, initrd_size,
+ initrd_size, sizeof(void *),
+ _ALIGN_UP(kernel_addr + kernel_size + dtb_length,
+ pagesize), 0x0fffffff, 1);
+
+ /* Now that the buffer for initrd is prepared, update the dtb
+ * with an appropriate location */
+ dtb_set_initrd(&dtb_buf, &dtb_length, initrd_base, initrd_base + initrd_size);
+
+ /* Add the initrd parameters to cmdline */
+ patch_initrd_info(cmdline_buf, initrd_base, initrd_size);
+ }
+ /* This is a legacy method for commandline passing used
+ * currently by Octeon CPUs only */
+ add_buffer(info, cmdline_buf, sizeof(cmdline_buf),
+ sizeof(cmdline_buf), sizeof(void *),
+ cmdline_addr, 0x0fffffff, 1);
+
+ add_buffer(info, dtb_buf, dtb_length, dtb_length, 0,
+ _ALIGN_UP(kernel_addr + kernel_size, pagesize),
+ 0x0fffffff, 1);
+
+ return 0;
+}
+
diff --git a/kexec/arch/mips/kexec-elf-rel-mips.c b/kexec/arch/mips/kexec-elf-rel-mips.c
new file mode 100644
index 0000000..5bc22d5
--- /dev/null
+++ b/kexec/arch/mips/kexec-elf-rel-mips.c
@@ -0,0 +1,46 @@
+/*
+ * kexec-elf-rel-mips.c - kexec Elf relocation routines
+ * Copyright (C) 2007 Francesco Chiechi, Alessandro Rubini
+ * Copyright (C) 2007 Tvblob s.r.l.
+ *
+ * derived from ../ppc/kexec-elf-rel-ppc.c
+ * Copyright (C) 2004 Albert Herranz
+ *
+ * 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_MIPS) {
+ return 0;
+ }
+ return 1;
+}
+
+void machine_apply_elf_rel(struct mem_ehdr *UNUSED(ehdr),
+ struct mem_sym *UNUSED(sym),
+ 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/mips/kexec-mips.c b/kexec/arch/mips/kexec-mips.c
new file mode 100644
index 0000000..d8cbea8
--- /dev/null
+++ b/kexec/arch/mips/kexec-mips.c
@@ -0,0 +1,191 @@
+/*
+ * kexec-mips.c - kexec for mips
+ * Copyright (C) 2007 Francesco Chiechi, Alessandro Rubini
+ * Copyright (C) 2007 Tvblob s.r.l.
+ *
+ * derived from ../ppc/kexec-ppc.c
+ * Copyright (C) 2004, 2005 Albert Herranz
+ *
+ * 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-mips.h"
+#include <arch/options.h>
+
+/* Currently not used but required by top-level fs2dt code */
+off_t initrd_base = 0;
+off_t initrd_size = 0;
+
+static struct memory_range memory_range[MAX_MEMORY_RANGES];
+
+/* Return a sorted list of memory ranges. */
+int get_memory_ranges(struct memory_range **range, int *ranges,
+ unsigned long UNUSED(kexec_flags))
+{
+ int memory_ranges = 0;
+
+ const char iomem[] = "/proc/iomem";
+ char line[MAX_LINE];
+ FILE *fp;
+ unsigned long long start, end;
+ char *str;
+ int type, consumed, count;
+
+ fp = fopen(iomem, "r");
+ if (!fp) {
+ fprintf(stderr, "Cannot open %s: %s\n", iomem, strerror(errno));
+ return -1;
+ }
+ while (fgets(line, sizeof(line), fp) != 0) {
+ if (memory_ranges >= MAX_MEMORY_RANGES)
+ break;
+ count = sscanf(line, "%llx-%llx : %n", &start, &end, &consumed);
+ if (count != 2)
+ continue;
+ str = line + consumed;
+ end = end + 1;
+ if (memcmp(str, "System RAM\n", 11) == 0) {
+ type = RANGE_RAM;
+ } else if (memcmp(str, "reserved\n", 9) == 0) {
+ type = RANGE_RESERVED;
+ } else {
+ continue;
+ }
+ if (memory_ranges > 0 &&
+ memory_range[memory_ranges - 1].end == start &&
+ memory_range[memory_ranges - 1].type == type) {
+ memory_range[memory_ranges - 1].end = end;
+ } else {
+ memory_range[memory_ranges].start = start;
+ memory_range[memory_ranges].end = end;
+ memory_range[memory_ranges].type = type;
+ memory_ranges++;
+ }
+ }
+ fclose(fp);
+ *range = memory_range;
+ *ranges = memory_ranges;
+ return 0;
+}
+
+struct file_type file_type[] = {
+ {"elf-mips", elf_mips_probe, elf_mips_load, elf_mips_usage},
+};
+int file_types = sizeof(file_type) / sizeof(file_type[0]);
+
+void arch_usage(void)
+{
+ printf(
+ " --command-line=STRING Set the kernel command line to STRING.\n"
+ " --append=STRING Set the kernel command line to STRING.\n"
+ " --dtb=FILE Use FILE as the device tree blob.\n"
+ " --initrd=FILE Use FILE as initial ramdisk.\n"
+ " --reuse-cmdline Use kernel command line from running system.\n"
+ );
+}
+
+struct arch_options_t arch_options = {
+#ifdef __mips64
+ .core_header_type = CORE_TYPE_ELF64,
+#else
+ .core_header_type = CORE_TYPE_ELF32,
+#endif
+};
+
+int arch_process_options(int argc, char **argv)
+{
+ static const struct option options[] = {
+ KEXEC_ARCH_OPTIONS
+ { 0 },
+ };
+ static const char short_options[] = KEXEC_ARCH_OPT_STR;
+ int opt;
+ char *cmdline = NULL;
+ const char *append = NULL;
+
+ while ((opt = getopt_long(argc, argv, short_options,
+ options, 0)) != -1) {
+ switch (opt) {
+ case OPT_APPEND:
+ append = optarg;
+ break;
+ case OPT_REUSE_CMDLINE:
+ cmdline = get_command_line();
+ break;
+ case OPT_DTB:
+ arch_options.dtb_file = optarg;
+ break;
+ case OPT_RAMDISK:
+ arch_options.initrd_file = optarg;
+ break;
+ default:
+ break;
+ }
+ }
+
+ arch_options.command_line = concat_cmdline(cmdline, append);
+
+ dbgprintf("%s:%d: command_line: %s\n", __func__, __LINE__,
+ arch_options.command_line);
+ dbgprintf("%s:%d: initrd: %s\n", __func__, __LINE__,
+ arch_options.initrd_file);
+ dbgprintf("%s:%d: dtb: %s\n", __func__, __LINE__,
+ arch_options.dtb_file);
+
+ return 0;
+}
+
+const struct arch_map_entry arches[] = {
+ /* For compatibility with older patches
+ * use KEXEC_ARCH_DEFAULT instead of KEXEC_ARCH_MIPS here.
+ */
+ { "mips", KEXEC_ARCH_MIPS },
+ { "mips64", KEXEC_ARCH_MIPS },
+ { NULL, 0 },
+};
+
+int arch_compat_trampoline(struct kexec_info *UNUSED(info))
+{
+
+ return 0;
+}
+
+void arch_update_purgatory(struct kexec_info *UNUSED(info))
+{
+}
+
+unsigned long virt_to_phys(unsigned long addr)
+{
+ return addr & 0x7fffffff;
+}
+
+/*
+ * add_segment() should convert base to a physical address on mips,
+ * 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, virt_to_phys(base), memsz, 1);
+}
+
+/*
+ * add_buffer() should convert base to a physical address on mips,
+ * while the default is just to work with base as is */
+unsigned long add_buffer(struct kexec_info *info, const void *buf,
+ unsigned long bufsz, unsigned long memsz,
+ unsigned long buf_align, unsigned long buf_min,
+ unsigned long buf_max, int buf_end)
+{
+ return add_buffer_phys_virt(info, buf, bufsz, memsz, buf_align,
+ buf_min, buf_max, buf_end, 1);
+}
+
diff --git a/kexec/arch/mips/kexec-mips.h b/kexec/arch/mips/kexec-mips.h
new file mode 100644
index 0000000..222c815
--- /dev/null
+++ b/kexec/arch/mips/kexec-mips.h
@@ -0,0 +1,30 @@
+#ifndef KEXEC_MIPS_H
+#define KEXEC_MIPS_H
+
+#include <sys/types.h>
+
+#define BOOT_BLOCK_VERSION 17
+#define BOOT_BLOCK_LAST_COMP_VERSION 16
+
+#define MAX_MEMORY_RANGES 64
+#define MAX_LINE 160
+
+#define CORE_TYPE_ELF32 1
+#define CORE_TYPE_ELF64 2
+
+int elf_mips_probe(const char *buf, off_t len);
+int elf_mips_load(int argc, char **argv, const char *buf, off_t len,
+ struct kexec_info *info);
+void elf_mips_usage(void);
+
+struct arch_options_t {
+ char *command_line;
+ char *dtb_file;
+ char *initrd_file;
+ int core_header_type;
+};
+
+extern struct memory_ranges usablemem_rgns;
+extern off_t initrd_base, initrd_size;
+
+#endif /* KEXEC_MIPS_H */