[ARM] 4736/1: Export atags to userspace and allow kexec to use customised atags

Currently, the atags used by kexec are fixed to the ones originally used
to boot the kernel. This is less than ideal as changing the commandline,
initrd and other options would be a useful feature.

This patch exports the atags used for the current kernel to userspace
through an "atags" file in procfs. The presence of the file is
controlled by its own Kconfig option and cleans up several ifdef blocks
into a separate file. The tags for the new kernel are assumed to be at
a fixed location before the kernel image itself. The location of the
tags used to boot the original kernel is unimportant and no longer
saved.

Based on a patch from Uli Luckas <u.luckas@road.de>

Signed-off-by: Richard Purdie <rpurdie@rpsys.net>
Acked-by: Uli Luckas <u.luckas@road.de>
Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>

authored by Richard Purdie and committed by Russell King 4cd9d6f7 ae9458d6

+110 -56
+7
arch/arm/Kconfig
··· 894 initially work for you. It may help to enable device hotplugging 895 support. 896 897 endmenu 898 899 if (ARCH_SA1100 || ARCH_INTEGRATOR || ARCH_OMAP || ARCH_IMX || ARCH_PXA)
··· 894 initially work for you. It may help to enable device hotplugging 895 support. 896 897 + config ATAGS_PROC 898 + bool "Export atags in procfs" 899 + default n 900 + help 901 + Should the atags used to boot the kernel be exported in an "atags" 902 + file in procfs. Useful with kexec. 903 + 904 endmenu 905 906 if (ARCH_SA1100 || ARCH_INTEGRATOR || ARCH_OMAP || ARCH_IMX || ARCH_PXA)
+1
arch/arm/kernel/Makefile
··· 20 obj-$(CONFIG_SMP) += smp.o 21 obj-$(CONFIG_KEXEC) += machine_kexec.o relocate_kernel.o 22 obj-$(CONFIG_KPROBES) += kprobes.o kprobes-decode.o 23 obj-$(CONFIG_OABI_COMPAT) += sys_oabi-compat.o 24 25 obj-$(CONFIG_CRUNCH) += crunch.o crunch-bits.o
··· 20 obj-$(CONFIG_SMP) += smp.o 21 obj-$(CONFIG_KEXEC) += machine_kexec.o relocate_kernel.o 22 obj-$(CONFIG_KPROBES) += kprobes.o kprobes-decode.o 23 + obj-$(CONFIG_ATAGS_PROC) += atags.o 24 obj-$(CONFIG_OABI_COMPAT) += sys_oabi-compat.o 25 26 obj-$(CONFIG_CRUNCH) += crunch.o crunch-bits.o
+86
arch/arm/kernel/atags.c
···
··· 1 + #include <linux/slab.h> 2 + #include <linux/kexec.h> 3 + #include <linux/proc_fs.h> 4 + #include <asm/setup.h> 5 + #include <asm/types.h> 6 + #include <asm/page.h> 7 + 8 + struct buffer { 9 + size_t size; 10 + char *data; 11 + }; 12 + static struct buffer tags_buffer; 13 + 14 + static int 15 + read_buffer(char* page, char** start, off_t off, int count, 16 + int* eof, void* data) 17 + { 18 + struct buffer *buffer = (struct buffer *)data; 19 + 20 + if (off >= buffer->size) { 21 + *eof = 1; 22 + return 0; 23 + } 24 + 25 + count = min((int) (buffer->size - off), count); 26 + 27 + memcpy(page, &buffer->data[off], count); 28 + 29 + return count; 30 + } 31 + 32 + 33 + static int 34 + create_proc_entries(void) 35 + { 36 + struct proc_dir_entry* tags_entry; 37 + 38 + tags_entry = create_proc_read_entry("atags", 0400, &proc_root, read_buffer, &tags_buffer); 39 + if (!tags_entry) 40 + return -ENOMEM; 41 + 42 + return 0; 43 + } 44 + 45 + 46 + static char __initdata atags_copy_buf[KEXEC_BOOT_PARAMS_SIZE]; 47 + static char __initdata *atags_copy; 48 + 49 + void __init save_atags(const struct tag *tags) 50 + { 51 + atags_copy = atags_copy_buf; 52 + memcpy(atags_copy, tags, KEXEC_BOOT_PARAMS_SIZE); 53 + } 54 + 55 + 56 + static int __init init_atags_procfs(void) 57 + { 58 + struct tag *tag; 59 + int error; 60 + 61 + if (!atags_copy) { 62 + printk(KERN_WARNING "Exporting ATAGs: No saved tags found\n"); 63 + return -EIO; 64 + } 65 + 66 + for (tag = (struct tag *) atags_copy; tag->hdr.size; tag = tag_next(tag)) 67 + ; 68 + 69 + tags_buffer.size = ((char *) tag - atags_copy) + sizeof(tag->hdr); 70 + tags_buffer.data = kmalloc(tags_buffer.size, GFP_KERNEL); 71 + if (tags_buffer.data == NULL) 72 + return -ENOMEM; 73 + memcpy(tags_buffer.data, atags_copy, tags_buffer.size); 74 + 75 + error = create_proc_entries(); 76 + if (error) { 77 + printk(KERN_ERR "Exporting ATAGs: not enough memory\n"); 78 + kfree(tags_buffer.data); 79 + tags_buffer.size = 0; 80 + tags_buffer.data = NULL; 81 + } 82 + 83 + return error; 84 + } 85 + 86 + arch_initcall(init_atags_procfs);
+5
arch/arm/kernel/atags.h
···
··· 1 + #ifdef CONFIG_ATAGS_PROC 2 + extern void save_atags(struct tag *tags); 3 + #else 4 + static inline void save_atags(struct tag *tags) { } 5 + #endif
+2
arch/arm/kernel/machine_kexec.c
··· 21 extern unsigned long kexec_start_address; 22 extern unsigned long kexec_indirection_page; 23 extern unsigned long kexec_mach_type; 24 25 /* 26 * Provide a dummy crash_notes definition while crash dump arrives to arm. ··· 63 kexec_start_address = image->start; 64 kexec_indirection_page = page_list; 65 kexec_mach_type = machine_arch_type; 66 67 /* copy our kernel relocation code to the control code page */ 68 memcpy(reboot_code_buffer,
··· 21 extern unsigned long kexec_start_address; 22 extern unsigned long kexec_indirection_page; 23 extern unsigned long kexec_mach_type; 24 + extern unsigned long kexec_boot_atags; 25 26 /* 27 * Provide a dummy crash_notes definition while crash dump arrives to arm. ··· 62 kexec_start_address = image->start; 63 kexec_indirection_page = page_list; 64 kexec_mach_type = machine_arch_type; 65 + kexec_boot_atags = image->start - KEXEC_ARM_ZIMAGE_OFFSET + KEXEC_ARM_ATAGS_OFFSET; 66 67 /* copy our kernel relocation code to the control code page */ 68 memcpy(reboot_code_buffer,
+4 -26
arch/arm/kernel/relocate_kernel.S
··· 7 .globl relocate_new_kernel 8 relocate_new_kernel: 9 10 - /* Move boot params back to where the kernel expects them */ 11 - 12 - ldr r0,kexec_boot_params_address 13 - teq r0,#0 14 - beq 8f 15 - 16 - ldr r1,kexec_boot_params_copy 17 - mov r6,#KEXEC_BOOT_PARAMS_SIZE/4 18 - 7: 19 - ldr r5,[r1],#4 20 - str r5,[r0],#4 21 - subs r6,r6,#1 22 - bne 7b 23 - 24 - 8: 25 - /* Boot params moved, now go on with the kernel */ 26 - 27 ldr r0,kexec_indirection_page 28 ldr r1,kexec_start_address 29 ··· 50 mov lr,r1 51 mov r0,#0 52 ldr r1,kexec_mach_type 53 - ldr r2,kexec_boot_params_address 54 mov pc,lr 55 56 .globl kexec_start_address ··· 65 kexec_mach_type: 66 .long 0x0 67 68 - /* phy addr where new kernel will expect to find boot params */ 69 - .globl kexec_boot_params_address 70 - kexec_boot_params_address: 71 - .long 0x0 72 - 73 - /* phy addr where old kernel put a copy of orig boot params */ 74 - .globl kexec_boot_params_copy 75 - kexec_boot_params_copy: 76 .long 0x0 77 78 relocate_new_kernel_end:
··· 7 .globl relocate_new_kernel 8 relocate_new_kernel: 9 10 ldr r0,kexec_indirection_page 11 ldr r1,kexec_start_address 12 ··· 67 mov lr,r1 68 mov r0,#0 69 ldr r1,kexec_mach_type 70 + ldr r2,kexec_boot_atags 71 mov pc,lr 72 73 .globl kexec_start_address ··· 82 kexec_mach_type: 83 .long 0x0 84 85 + /* phy addr of the atags for the new kernel */ 86 + .globl kexec_boot_atags 87 + kexec_boot_atags: 88 .long 0x0 89 90 relocate_new_kernel_end:
+2 -30
arch/arm/kernel/setup.c
··· 24 #include <linux/interrupt.h> 25 #include <linux/smp.h> 26 #include <linux/fs.h> 27 - #include <linux/kexec.h> 28 29 #include <asm/cpu.h> 30 #include <asm/elf.h> ··· 38 #include <asm/mach/time.h> 39 40 #include "compat.h" 41 42 #ifndef MEM_SIZE 43 #define MEM_SIZE (16*1024*1024) ··· 784 } 785 arch_initcall(customize_machine); 786 787 - #ifdef CONFIG_KEXEC 788 - 789 - /* Physical addr of where the boot params should be for this machine */ 790 - extern unsigned long kexec_boot_params_address; 791 - 792 - /* Physical addr of the buffer into which the boot params are copied */ 793 - extern unsigned long kexec_boot_params_copy; 794 - 795 - /* Pointer to the boot params buffer, for manipulation and display */ 796 - unsigned long kexec_boot_params; 797 - EXPORT_SYMBOL(kexec_boot_params); 798 - 799 - /* The buffer itself - make sure it is sized correctly */ 800 - static unsigned long kexec_boot_params_buf[(KEXEC_BOOT_PARAMS_SIZE + 3) / 4]; 801 - 802 - #endif 803 - 804 void __init setup_arch(char **cmdline_p) 805 { 806 struct tag *tags = (struct tag *)&init_tags; ··· 802 else if (mdesc->boot_params) 803 tags = phys_to_virt(mdesc->boot_params); 804 805 - #ifdef CONFIG_KEXEC 806 - kexec_boot_params_copy = virt_to_phys(kexec_boot_params_buf); 807 - kexec_boot_params = (unsigned long)kexec_boot_params_buf; 808 - if (__atags_pointer) { 809 - kexec_boot_params_address = __atags_pointer; 810 - memcpy((void *)kexec_boot_params, tags, KEXEC_BOOT_PARAMS_SIZE); 811 - } else if (mdesc->boot_params) { 812 - kexec_boot_params_address = mdesc->boot_params; 813 - memcpy((void *)kexec_boot_params, tags, KEXEC_BOOT_PARAMS_SIZE); 814 - } 815 - #endif 816 - 817 /* 818 * If we have the old style parameters, convert them to 819 * a tag list. ··· 817 if (tags->hdr.tag == ATAG_CORE) { 818 if (meminfo.nr_banks != 0) 819 squash_mem_tags(tags); 820 parse_tags(tags); 821 } 822
··· 24 #include <linux/interrupt.h> 25 #include <linux/smp.h> 26 #include <linux/fs.h> 27 28 #include <asm/cpu.h> 29 #include <asm/elf.h> ··· 39 #include <asm/mach/time.h> 40 41 #include "compat.h" 42 + #include "atags.h" 43 44 #ifndef MEM_SIZE 45 #define MEM_SIZE (16*1024*1024) ··· 784 } 785 arch_initcall(customize_machine); 786 787 void __init setup_arch(char **cmdline_p) 788 { 789 struct tag *tags = (struct tag *)&init_tags; ··· 819 else if (mdesc->boot_params) 820 tags = phys_to_virt(mdesc->boot_params); 821 822 /* 823 * If we have the old style parameters, convert them to 824 * a tag list. ··· 846 if (tags->hdr.tag == ATAG_CORE) { 847 if (meminfo.nr_banks != 0) 848 squash_mem_tags(tags); 849 + save_atags(tags); 850 parse_tags(tags); 851 } 852
+3
include/asm-arm/kexec.h
··· 16 17 #define KEXEC_BOOT_PARAMS_SIZE 1536 18 19 #ifndef __ASSEMBLY__ 20 21 struct kimage;
··· 16 17 #define KEXEC_BOOT_PARAMS_SIZE 1536 18 19 + #define KEXEC_ARM_ATAGS_OFFSET 0x1000 20 + #define KEXEC_ARM_ZIMAGE_OFFSET 0x8000 21 + 22 #ifndef __ASSEMBLY__ 23 24 struct kimage;