MIPS: use kexec to load ELF linux images
Subsystems (3)
v2 cover MIPS: use kexec to load ELF linux images
This patchseries introduces a suitable way for loading ELF linux kernel images on MIPS. You can load normal vmlinux images or compressed self-extractable vmlinuz images.
The patchseries and additional patch with pre-compiled demo vmlinux image for MIPS Malta can be obtained here:
https://github.com/frantony/barebox/tree/20161205.malta-bootm-vmlinux
Please see this commit message
https://github.com/frantony/barebox/commit/23b24122a4bd1d59266ae585364c792dc9140ecb
for instructions.
The code for actual ELF loading and relocation is imported
from
kexec-tools (git://git.kernel.org/pub/scm/utils/kernel/kexec/kexec-tools)
and
linux kernel (http://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git).
There is no common practice on passing cmdline to the linux kernel on MIPS machines. We have to realize it's own cmdline passing routine for every MIPS machine supported by barebox. The 'MIPS: malta: enable kexec' patch
demonstrates how to do it for MIPS Malta.
To separate common ELF handling routines and machine-specific cmdline handling the 'reboot()' call is introduced.
The common code checks and loads ELF-file, the MIPS-specific code arrange ELF segments in the appropriate memory places and tunes relocator; next reboot() is called and machine-specific code works on cmdline passing.
The 'import initial kexec stuff' contains some codes stealed from kexec-tools as-is. These codes have some checkpatch.pl issues.
Antony Pavlov (7):
MIPS: add virt_to_phys() and phys_to_virt()
resource: add create_resource() helper function
import initial kexec stuff
filetype: add ELF type
bootm: add kexec ELF support
MIPS: add kexec ELF loading support
MIPS: malta: enable kexec
Peter Mamonov (1):
MIPS: c-r4k: add support for secondary cache
Changes since RFC v1 (http://lists.infradead.org/pipermail/barebox/2014-April/018651.html):
* add 64 bit ELF support by Peter Mamonov;
* update relocate_new_kernel arguments passing mechanism;
* test with linux-4.9-rc8.
TODOs:
* add DT and initrd passing support.
arch/mips/Kconfig | 1 + arch/mips/include/asm/cacheops.h | 10 + arch/mips/include/asm/elf.h | 8 +- arch/mips/include/asm/io.h | 35 ++ arch/mips/lib/Makefile | 3 + arch/mips/lib/c-r4k.c | 19 +- arch/mips/lib/kexec-mips.c | 171 +++++++++ arch/mips/lib/relocate_kernel.S | 97 +++++ arch/mips/mach-malta/Makefile | 1 + arch/mips/mach-malta/reboot.c | 104 ++++++ commands/Kconfig | 7 + common/Kconfig | 3 + common/filetype.c | 5 + common/resource.c | 15 + include/filetype.h | 1 + include/linux/ioport.h | 2 + include/linux/reboot.h | 14 + lib/Makefile | 1 + lib/kexec/Makefile | 4 + lib/kexec/kexec-bootm-elf.c | 37 ++ lib/kexec/kexec-elf-exec.c | 82 ++++ lib/kexec/kexec-elf.c | 783 +++++++++++++++++++++++++++++++++++++++ lib/kexec/kexec-elf.h | 86 +++++ lib/kexec/kexec.c | 151 ++++++++ lib/kexec/kexec.h | 89 +++++ 25 files changed, 1722 insertions(+), 7 deletions(-) create mode 100644 arch/mips/lib/kexec-mips.c create mode 100644 arch/mips/lib/relocate_kernel.S create mode 100644 arch/mips/mach-malta/reboot.c create mode 100644 include/linux/reboot.h create mode 100644 lib/kexec/Makefile create mode 100644 lib/kexec/kexec-bootm-elf.c create mode 100644 lib/kexec/kexec-elf-exec.c create mode 100644 lib/kexec/kexec-elf.c create mode 100644 lib/kexec/kexec-elf.h create mode 100644 lib/kexec/kexec.c create mode 100644 lib/kexec/kexec.h
Signature · 7 lines
2.10.2 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
RFC v2 2/8 MIPS: c-r4k: add support for secondary cache
From: Peter Mamonov <[email protected]>
--- arch/mips/include/asm/cacheops.h | 10 ++++++++++ arch/mips/lib/c-r4k.c | 19 +++++++++++++------ 2 files changed, 23 insertions(+), 6 deletions(-)
arch/mips/include/asm/cacheops.h +10 -0
@@ -33,4 +33,14 @@#define Hit_Invalidate_D (Cache_D | Hit_Invalidate)#define Hit_Writeback_Inv_D (Cache_D | Hit_Writeback_Inv) +/* +* R4000SC and R4400SC-specific cacheops +*/ +#define Cache_SD 0x03 + +#define Index_Writeback_Inv_SD (Cache_SD | Index_Writeback_Inv) +#define Index_Store_Tag_SD (Cache_SD | Index_Store_Tag) +#define Hit_Invalidate_SD (Cache_SD | Hit_Invalidate) +#define Hit_Writeback_Inv_SD (Cache_SD | Hit_Writeback_Inv) +#endif /* __ASM_CACHEOPS_H */
arch/mips/lib/c-r4k.c +13 -6
@@ -46,7 +46,9 @@ static inline void blast_##pfx##cache##_range(unsigned long start, \ } __BUILD_BLAST_CACHE_RANGE(d, dcache, Hit_Writeback_Inv_D) +__BUILD_BLAST_CACHE_RANGE(s, scache, Hit_Writeback_Inv_SD) __BUILD_BLAST_CACHE_RANGE(inv_d, dcache, Hit_Invalidate_D) +__BUILD_BLAST_CACHE_RANGE(inv_s, scache, Hit_Invalidate_SD)void flush_cache_all (void ) {
@@ -54,7 +56,7 @@ void flush_cache_all(void)unsigned long lsize;unsigned long addr;unsigned long aend; -unsigned int icache_size, dcache_size; +unsigned int icache_size, dcache_size, scache_size; dcache_size = c->dcache.waysize * c->dcache.ways; lsize = c->dcache.linesz;
@@ -68,21 +70,26 @@ void flush_cache_all(void)for (addr =KSEG0 ; addr <= aend; addr += lsize)cache_op (Index_Invalidate_I, addr); -/* secondatory cache skipped */ +if (c->scache.flags &MIPS_CACHE_NOT_PRESENT ) +return ; + + scache_size = c->scache.waysize * c->scache.ways; + lsize = c->scache.linesz; + aend = (KSEG0 + scache_size -1 ) & ~(lsize -1 ); +for (addr =KSEG0 ; addr <= aend; addr += lsize) +cache_op (Index_Writeback_Inv_SD, addr); }void dma_flush_range (unsigned long start,unsigned long end) {blast_dcache_range (start, end); - -/* secondatory cache skipped */ +blast_scache_range (start, end); }void dma_inv_range (unsigned long start,unsigned long end) {blast_inv_dcache_range (start, end); - -/* secondatory cache skipped */ +blast_inv_scache_range (start, end); }void r4k_cache_init (void );
Signature · 7 lines
2.10.2 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
RFC v2 1/8 MIPS: add virt_to_phys() and phys_to_virt()
In reply to Antony Pavlov--- arch/mips/include/asm/io.h | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+)
arch/mips/include/asm/io.h +35 -0
@@ -12,11 +12,46 @@#include <linux/compiler.h> #include <asm/types.h> +#include <asm/addrspace.h> #include <asm/byteorder.h> void dma_flush_range (unsigned long ,unsigned long );void dma_inv_range (unsigned long ,unsigned long ); +/* +* virt_to_phys - map virtual addresses to physical +* @address: address to remap +* +* The returned physical address is the physical (CPU) mapping for +* the memory address given. It is only valid to use this function on +* addresses directly mapped or allocated via kmalloc. +* +* This function does not give bus mappings for DMA transfers. In +* almost all conceivable cases a device driver should not be using +* this function +*/ +static inline unsigned long virt_to_phys (const void *address) +{ +return (unsigned long )address &0x1fffffff ; +} + +/* +* phys_to_virt - map physical address to virtual +* @address: address to remap +* +* The returned virtual address is a current CPU mapping for +* the memory address given. It is only valid to use this function on +* addresses that have a kernel mapping +* +* This function does not handle bus mappings for DMA transfers. In +* almost all conceivable cases a device driver should not be using +* this function +*/ +static inline void *phys_to_virt (unsigned long address) +{ +return (void *)(KSEG0 | (address &0x1fffffff )); +} +#define IO_SPACE_LIMIT 0/*****************************************************************************/
Signature · 7 lines
2.10.2 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
RFC v2 3/8 resource: add create_resource() helper function
In reply to Antony Pavlov--- common/resource.c | 15 +++++++++++++++ include/linux/ioport.h | 2 ++ 2 files changed, 17 insertions(+)
common/resource.c +15 -0
@@ -150,3 +150,18 @@ struct resource *request_ioport_region(const char *name,return res; } + +struct resource *create_resource (const char *name, +resource_size_t start,resource_size_t end) +{ +struct resource *t; + + t =xzalloc (sizeof *t); +INIT_LIST_HEAD (&t->children); + t->parent =NULL ; + t->name =xstrdup (name); + t->start = start; + t->end = end; + +return t; +}
include/linux/ioport.h +2 -0
@@ -140,6 +140,8 @@ struct resource *__request_region(struct resource *parent, resource_size_t size);int release_region (struct resource *res); +struct resource *create_resource (const char *name, +resource_size_t start,resource_size_t end);extern struct resource iomem_resource;extern struct resource ioport_resource;
Signature · 7 lines
2.10.2 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
RFC v2 5/8 filetype: add ELF type
In reply to Antony Pavlov--- common/filetype.c | 5 +++++ include/filetype.h | 1 + 2 files changed, 6 insertions(+)
common/filetype.c +5 -0
@@ -25,6 +25,7 @@#include <errno.h> #include <envfs.h> #include <disks.h> +#include <elf.h> struct filetype_str {const char *name;/* human readable filetype */
@@ -63,6 +64,7 @@ static const struct filetype_str filetype_str[] = { [filetype_exe] = { "MS-DOS executable", "exe" }, [filetype_mxs_bootstream] = { "Freescale MXS bootstream", "mxsbs" }, [filetype_socfpga_xload] = { "SoCFPGA prebootloader image", "socfpga-xload" }, + [filetype_elf] = { "ELF", "elf" }, };const char *file_type_to_string (enum filetype f)
@@ -327,6 +329,9 @@ enum filetype file_detect_type(const void *_buf, size_t bufsize) buf[7] == 0x47530000)return filetype_ch_image_be; +if (strncmp (buf8,ELFMAG ,4 ) ==0 ) +return filetype_elf; +return filetype_unknown; }
include/filetype.h +1 -0
@@ -38,6 +38,7 @@ enum filetype { filetype_xz_compressed, filetype_mxs_bootstream, filetype_socfpga_xload, + filetype_elf, filetype_max, };
Signature · 7 lines
2.10.2 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
RFC v2 8/8 MIPS: malta: enable kexec
In reply to Antony Pavlov--- arch/mips/Kconfig | 1 + arch/mips/mach-malta/Makefile | 1 + arch/mips/mach-malta/reboot.c | 104 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+)
arch/mips/Kconfig +1 -0
@@ -48,6 +48,7 @@ config MACH_MIPS_MALTA select HAS_DEBUG_LL select GPIOLIB select HW_HAS_PCI + select HAS_KEXEC config MACH_MIPS_AR231X bool "Atheros ar231x-based boards"
arch/mips/mach-malta/Makefile +1 -0
@@ -1,2 +1,3 @@ obj-y += reset.o obj-$(CONFIG_PCI) += pci.o +obj-$(CONFIG_KEXEC) += reboot.o
arch/mips/mach-malta/reboot.c new file +104 -0
104 lines not on this page · raw message
Signature · 7 lines
2.10.2 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
RFC v2 6/8 bootm: add kexec ELF support
In reply to Antony PavlovAlso introduce reboot() for starting already loaded via kexec ELF segments.
--- include/linux/reboot.h | 14 ++++++++++++++ lib/kexec/Makefile | 1 + lib/kexec/kexec-bootm-elf.c | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+)
include/linux/reboot.h new file +14 -0
@@ -0,0 +1,14 @@ +#ifndef _LINUX_REBOOT_H +#define _LINUX_REBOOT_H + +/* +* Commands accepted by the _reboot() system call. +* +* KEXEC Restart system using a previously loaded Linux kernel +*/ + +#define LINUX_REBOOT_CMD_KEXEC 0x45584543 + +extern int reboot (int cmd); + +#endif /* _LINUX_REBOOT_H */
lib/kexec/Makefile +1 -0
@@ -1,3 +1,4 @@ obj-y += kexec.o obj-y += kexec-elf.o obj-y += kexec-elf-exec.o +obj-y += kexec-bootm-elf.o
lib/kexec/kexec-bootm-elf.c new file +37 -0
@@ -0,0 +1,37 @@ +#include <bootm.h> +#include <init.h> +#include <binfmt.h> +#include <errno.h> +#include <linux/reboot.h> +#include <environment.h> + +#include "kexec.h" + +static int do_bootm_elf (struct image_data *data) +{ +kexec_load_file (data->os_file,0 ); +setenv ("global.bootm.image" , data->os_file); +reboot (LINUX_REBOOT_CMD_KEXEC ); + +return -ERESTARTSYS ; +} + +static struct image_handler elf_handler = { + .name ="ELF" , + .bootm = do_bootm_elf, + .filetype = filetype_elf, +}; + +static struct binfmt_hook binfmt_elf_hook = { + .type = filetype_elf, + .exec ="bootm" , +}; + +static int elf_register_image_handler (void ) +{ +register_image_handler (&elf_handler); +binfmt_register (&binfmt_elf_hook); + +return 0 ; +} +late_initcall (elf_register_image_handler);
Signature · 7 lines
2.10.2 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
RFC v2 7/8 MIPS: add kexec ELF loading support
In reply to Antony Pavlov--- arch/mips/include/asm/elf.h | 8 +- arch/mips/lib/Makefile | 3 + arch/mips/lib/kexec-mips.c | 171 ++++++++++++++++++++++++++++++++++++++++ arch/mips/lib/relocate_kernel.S | 97 +++++++++++++++++++++++ 4 files changed, 278 insertions(+), 1 deletion(-)
arch/mips/include/asm/elf.h +7 -1
@@ -17,12 +17,18 @@#ifndef ELF_ARCH +/* Legal values for e_machine (architecture). */ + +#define EM_MIPS 8/* MIPS R3000 big-endian */ +#define EM_MIPS_RS4_BE 10/* MIPS R4000 big-endian */ +#ifdef CONFIG_32BIT /* * This is used to ensure we don't load something for the wrong architecture. */ -#define elf_check_arch (hdr) \ +#define elf_check_arch (x) ((x)->e_machine == EM_MIPS) +/* * These are used to set parameters in the core dumps. */
arch/mips/lib/Makefile +3 -0
@@ -18,4 +18,7 @@ obj-$(CONFIG_CPU_MIPS64) += c-r4k.o obj-$(CONFIG_CMD_MIPS_CPUINFO) += cpuinfo.o obj-$(CONFIG_CMD_BOOTM) += bootm.o +obj-$(CONFIG_KEXEC) += kexec-mips.o +obj-$(CONFIG_KEXEC) += relocate_kernel.o + pbl-y += ashldi3.o
arch/mips/lib/kexec-mips.c new file +171 -0
171 lines not on this page · raw message
arch/mips/lib/relocate_kernel.S new file +97 -0
97 lines not on this page · raw message
Signature · 7 lines
2.10.2 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
RFC v2 4/8 import initial kexec stuff
In reply to Antony Pavlov--- commands/Kconfig | 7 + common/Kconfig | 3 + lib/Makefile | 1 + lib/kexec/Makefile | 3 + lib/kexec/kexec-elf-exec.c | 82 +++++ lib/kexec/kexec-elf.c | 783 +++++++++++++++++++++++++++++++++++++++++++++ lib/kexec/kexec-elf.h | 86 +++++ lib/kexec/kexec.c | 151 +++++++++ lib/kexec/kexec.h | 89 ++++++ 9 files changed, 1205 insertions(+)
commands/Kconfig +7 -0
@@ -480,6 +480,13 @@ config CMD_SAVES Save S-Record file to serial line with offset OFFS and length LEN. +config KEXEC + bool + prompt "bootm ELF image support" + depends on CMD_BOOTM && HAS_KEXEC + help + Support using ELF Images. + config CMD_UIMAGE select UIMAGE tristate
common/Kconfig +3 -0
@@ -1160,3 +1160,6 @@ config HAS_DEBUG_LL config DDR_SPD bool select CRC16 + +config HAS_KEXEC + bool
lib/Makefile +1 -0
@@ -59,3 +59,4 @@ obj-y += reed_solomon/ obj-$(CONFIG_RATP) += ratp.o obj-y += list_sort.o obj-y += int_sqrt.o +obj-$(CONFIG_KEXEC) += kexec/
lib/kexec/Makefile new file +3 -0
@@ -0,0 +1,3 @@ +obj-y += kexec.o +obj-y += kexec-elf.o +obj-y += kexec-elf-exec.o
lib/kexec/kexec-elf-exec.c new file +82 -0
82 lines not on this page · raw message
lib/kexec/kexec-elf.c new file +783 -0
783 lines not on this page · raw message
lib/kexec/kexec-elf.h new file +86 -0
86 lines not on this page · raw message
lib/kexec/kexec.c new file +151 -0
151 lines not on this page · raw message
lib/kexec/kexec.h new file +89 -0
89 lines not on this page · raw message
Signature · 7 lines
2.10.2 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Re: [RFC v2 4/8] import initial kexec stuff
Quoted 2 lines · Antony Pavlov
Signed-off-by Antony Pavlov <[email protected]>Signed-off-by Peter Mamonov <[email protected]>
The commit message could be a bit more verbose and at least mention where this code is derived from and from which version.
Quoted 26 lines
lib/kexec/kexec-elf.c new file +26
-0+ +static int build_mem_ehdr (const char *buf,off_t len,struct mem_ehdr *ehdr) +{ +unsigned char e_ident[EI_NIDENT ]; +int result; + +memset (ehdr,0 ,sizeof (*ehdr)); + +if ((size_t )len <sizeof (e_ident)) { +printf ("Buffer is too small to hold ELF e_ident\n" ); + +return -1 ; + } + +memcpy (e_ident, buf,sizeof (e_ident)); + + ehdr->ei_class = e_ident[EI_CLASS ]; + ehdr->ei_data = e_ident[EI_DATA ]; +if ( (ehdr->ei_class !=ELFCLASS32 ) && + (ehdr->ei_class !=ELFCLASS64 )) + { +printf ("Not a supported ELF class\n" ); +return -1 ; + } + + if ( (ehdr->ei_data != ELFDATA2LSB) &&
lib/kexec/kexec-elf.c line 180
Quoted 10 lines
lib/kexec/kexec-elf.c new file +10
-0+ (ehdr->ei_data != ELFDATA2MSB)) + { +printf ("Not a supported ELF data format\n" ); +return -1 ; + } + + result =-1 ; +if (ehdr->ei_class ==ELFCLASS32 ) { + result =build_mem_elf32_ehdr (buf, len, ehdr); + }
The opening braces are sometimes on the same line and sometimees on a new line. Is this from the original code?
Quoted 14 lines
lib/kexec/kexec-elf.c new file +14
-0+ +/* Now walk and normalize the notes */ + ehdr->e_note =xmalloc (sizeof (*ehdr->e_note) * ehdr->e_notenum); +for (i =0 , note = note_start; note < note_end; + note += note_size, i++) { +const unsigned char *name, *desc; +ElfNN_Nhdr hdr; +read_nhdr (ehdr, &hdr, note); + note_size =sizeof (hdr); + name = note + note_size; + note_size += (hdr.n_namesz +3 ) & ~3 ; + desc = note + note_size; + note_size += (hdr.n_descsz +3 ) & ~3 ; +
lib/kexec/kexec-elf.c line 576
Quoted 10 lines
lib/kexec/kexec-elf.c new file +10
-0+if ((hdr.n_namesz !=0 ) && (name[hdr.n_namesz -1 ] !='\0' )) { +/* If note name string is not null terminated, just +* warn user about it and continue processing. This +* allows us to parse /proc/kcore on older kernels +* where /proc/kcore elf notes were not null +* terminated. It has been fixed in 2.6.19. +*/ +printf ("Warning: Elf Note name is not null " +"terminated\n" ); + }
Is this relevant for barebox? We do not parse /proc/kcore.
lib/kexec/kexec-elf.c line 586
Quoted 10 lines
lib/kexec/kexec-elf.c new file +10
-0+ ehdr->e_note[i].n_type = hdr.n_type; + ehdr->e_note[i].n_name = (char *)name; + ehdr->e_note[i].n_desc = desc; + ehdr->e_note[i].n_descsz = hdr.n_descsz; + + } + +return 0 ; +} +
Sascha
Signature · 9 lines
Pengutronix e.K. | | Industrial Linux Solutions | http://www.pengutronix.de/ | Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Re: [RFC v2 6/8] bootm: add kexec ELF support
In reply to Antony PavlovQuoted 72 lines · Antony Pavlov
Also introduce
reboot()for starting already loaded via kexec ELF segments.Signed-off-by Antony Pavlov <[email protected]>--- include/linux/reboot.h | 14 ++++++++++++++ lib/kexec/Makefile | 1 + lib/kexec/kexec-bootm-elf.c | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+)include/linux/reboot.h new file +14
-0@@ -0,0 +1,14 @@ +#ifndef _LINUX_REBOOT_H +#define _LINUX_REBOOT_H + +/* +* Commands accepted by the _reboot() system call. +* +* KEXEC Restart system using a previously loaded Linux kernel +*/ + +#define LINUX_REBOOT_CMD_KEXEC 0x45584543 + +extern int reboot (int cmd); + +#endif /* _LINUX_REBOOT_H */ lib/kexec/Makefile +1
-0@@ -1,3 +1,4 @@ obj-y += kexec.o obj-y += kexec-elf.o obj-y += kexec-elf-exec.o +obj-y += kexec-bootm-elf.olib/kexec/kexec-bootm-elf.c new file +27
-0@@ -0,0 +1,37 @@ +#include <bootm.h> +#include <init.h> +#include <binfmt.h> +#include <errno.h> +#include <linux/reboot.h> +#include <environment.h> + +#include "kexec.h" + +static int do_bootm_elf (struct image_data *data) +{ +kexec_load_file (data->os_file,0 ); +setenv ("global.bootm.image" , data->os_file); +reboot (LINUX_REBOOT_CMD_KEXEC ); + +return -ERESTARTSYS ; +} + +static struct image_handler elf_handler = { + .name ="ELF" , + .bootm = do_bootm_elf, + .filetype = filetype_elf, +}; + +static struct binfmt_hook binfmt_elf_hook = { + .type = filetype_elf, + .exec ="bootm" ,
lib/kexec/kexec-bootm-elf.c line 37
Quoted 10 lines
lib/kexec/kexec-bootm-elf.c new file +10
-0+}; + +static int elf_register_image_handler (void ) +{ +register_image_handler (&elf_handler); +binfmt_register (&binfmt_elf_hook); + +return 0 ; +} +late_initcall (elf_register_image_handler);
The code needed to actually start a kexec kernel is architecture specific, so the registration of the ELF handler should be done by this architecture specific code. Then you also don't need this awkward reboot() stuff. I'm thinking of a
int bootm_register_kexec_handler(int (*do_bootm)(struct image_data *data));
to be called by MIPS or malta specific code.
Sascha
Signature · 9 lines
Pengutronix e.K. | | Industrial Linux Solutions | http://www.pengutronix.de/ | Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Re: [RFC v2 4/8] import initial kexec stuff
In reply to Sascha HauerQuoted 38 lines · On Wed, 7 Dec 2016 20:47:55 +0100 Sascha Hauer <[email protected]> wrote:
Quoted 2 lines · Antony Pavlov
Signed-off-by Antony Pavlov <[email protected]>Signed-off-by Peter Mamonov <[email protected]>The commit message could be a bit more verbose and at least mention where this code is derived from and from which version.
Quoted 31 lines
lib/kexec/kexec-elf.c new file +31
-0+ +static int build_mem_ehdr (const char *buf,off_t len,struct mem_ehdr *ehdr) +{ +unsigned char e_ident[EI_NIDENT ]; +int result; + +memset (ehdr,0 ,sizeof (*ehdr)); + +if ((size_t )len <sizeof (e_ident)) { +printf ("Buffer is too small to hold ELF e_ident\n" ); + +return -1 ; + } + +memcpy (e_ident, buf,sizeof (e_ident)); + + ehdr->ei_class = e_ident[EI_CLASS ]; + ehdr->ei_data = e_ident[EI_DATA ]; +if ( (ehdr->ei_class !=ELFCLASS32 ) && + (ehdr->ei_class !=ELFCLASS64 )) + { +printf ("Not a supported ELF class\n" ); +return -1 ; + } + +if ( (ehdr->ei_data !=ELFDATA2LSB ) && + (ehdr->ei_data !=ELFDATA2MSB )) + { +printf ("Not a supported ELF data format\n" ); +return -1 ; + }
Quoted 8 lines
lib/kexec/kexec-elf.c line 180
Quoted 5 lines
lib/kexec/kexec-elf.c new file +5
-0+ + result =-1 ; +if (ehdr->ei_class ==ELFCLASS32 ) { + result =build_mem_elf32_ehdr (buf, len, ehdr); + }The opening braces are sometimes on the same line and sometimees on a new line. Is this from the original code?
Yes, original code has some formatting problems. I have noted that fact in RFC v2 0/0 message.
Quoted 18 lines
Quoted 18 lines
lib/kexec/kexec-elf.c new file +18
-0+ +/* Now walk and normalize the notes */ + ehdr->e_note =xmalloc (sizeof (*ehdr->e_note) * ehdr->e_notenum); +for (i =0 , note = note_start; note < note_end; + note += note_size, i++) { +const unsigned char *name, *desc; +ElfNN_Nhdr hdr; +read_nhdr (ehdr, &hdr, note); + note_size =sizeof (hdr); + name = note + note_size; + note_size += (hdr.n_namesz +3 ) & ~3 ; + desc = note + note_size; + note_size += (hdr.n_descsz +3 ) & ~3 ; + +if ((hdr.n_namesz !=0 ) && (name[hdr.n_namesz -1 ] !='\0' )) { +/* If note name string is not null terminated, just +* warn user about it and continue processing. This +* allows us to parse /proc/kcore on older kernels
Quoted 8 lines
lib/kexec/kexec-elf.c line 576
Quoted 6 lines
lib/kexec/kexec-elf.c new file +6
-0+* where /proc/kcore elf notes were not null +* terminated. It has been fixed in 2.6.19. +*/ +printf ("Warning: Elf Note name is not null " +"terminated\n" ); + }Is this relevant for barebox? We do not parse
/proc/kcore.
Good shot! I'll drop this code.
Quoted 19 lines
lib/kexec/kexec-elf.c line 586
Quoted 11 lines
lib/kexec/kexec-elf.c new file +10
-0+ ehdr->e_note[i].n_type = hdr.n_type; + ehdr->e_note[i].n_name = (char *)name; + ehdr->e_note[i].n_desc = desc; + ehdr->e_note[i].n_descsz = hdr.n_descsz; + + } + +return 0 ; +} +Sascha
Signature · 4 lines
Pengutronix e.K. | | Industrial Linux Solutions | http://www.pengutronix.de/ | Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
Signature · 7 lines
Best regards, Antony Pavlov _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Re: [RFC v2 6/8] bootm: add kexec ELF support
In reply to Sascha HauerQuoted 89 lines · On Wed, 7 Dec 2016 20:59:04 +0100 Sascha Hauer <[email protected]> wrote:
Quoted 72 lines · Antony Pavlov
Also introduce
reboot()for starting already loaded via kexec ELF segments.Signed-off-by Antony Pavlov <[email protected]>--- include/linux/reboot.h | 14 ++++++++++++++ lib/kexec/Makefile | 1 + lib/kexec/kexec-bootm-elf.c | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+)include/linux/reboot.h new file +14
-0@@ -0,0 +1,14 @@ +#ifndef _LINUX_REBOOT_H +#define _LINUX_REBOOT_H + +/* +* Commands accepted by the _reboot() system call. +* +* KEXEC Restart system using a previously loaded Linux kernel +*/ + +#define LINUX_REBOOT_CMD_KEXEC 0x45584543 + +extern int reboot (int cmd); + +#endif /* _LINUX_REBOOT_H */ lib/kexec/Makefile +1
-0@@ -1,3 +1,4 @@ obj-y += kexec.o obj-y += kexec-elf.o obj-y += kexec-elf-exec.o +obj-y += kexec-bootm-elf.olib/kexec/kexec-bootm-elf.c new file +27
-0@@ -0,0 +1,37 @@ +#include <bootm.h> +#include <init.h> +#include <binfmt.h> +#include <errno.h> +#include <linux/reboot.h> +#include <environment.h> + +#include "kexec.h" + +static int do_bootm_elf (struct image_data *data) +{ +kexec_load_file (data->os_file,0 ); +setenv ("global.bootm.image" , data->os_file); +reboot (LINUX_REBOOT_CMD_KEXEC ); + +return -ERESTARTSYS ; +} + +static struct image_handler elf_handler = { + .name ="ELF" , + .bootm = do_bootm_elf, + .filetype = filetype_elf, +}; + +static struct binfmt_hook binfmt_elf_hook = { + .type = filetype_elf, + .exec ="bootm" ,lib/kexec/kexec-bootm-elf.c line 37
Quoted 10 lines
lib/kexec/kexec-bootm-elf.c new file +10
-0+}; + +static int elf_register_image_handler (void ) +{ +register_image_handler (&elf_handler); +binfmt_register (&binfmt_elf_hook); + +return 0 ; +} +late_initcall (elf_register_image_handler);The code needed to actually start a kexec kernel is architecture specific, so the registration of the ELF handler should be done by this architecture specific code. Then you also don't need this awkward
reboot()stuff. I'm thinking of a
Quoted 4 lines
int
bootm_register_kexec_handler(int (*do_bootm)(struct image_data *data));to be called by MIPS or malta specific code.
Moreover current RFC v2 kexec patchseries contains too many redundant code (e.g. kexec uses it's own filetype-like infrastructure).
I agree. Current barebox kexec infrastructure is used only for MIPS so I can move some code from lib/ to arch/mips.
Signature · 7 lines
Best regards, Antony Pavlov _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox