Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Load ELF vmlinux file for the kexec_file_load syscall.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2004  Adam Litke (agl@us.ibm.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (C) 2004  IBM Corp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Copyright (C) 2005  R Sharada (sharada@in.ibm.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Copyright (C) 2006  Mohan Kumar M (mohan@in.ibm.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Copyright (C) 2016  IBM Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * Based on kexec-tools' kexec-elf-exec.c and kexec-elf-ppc64.c.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * Heavily modified for the kernel by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * Thiago Jung Bauermann <bauerman@linux.vnet.ibm.com>.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #define pr_fmt(fmt)	"kexec_elf: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/elf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/kexec.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/libfdt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/of_fdt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static void *elf64_load(struct kimage *image, char *kernel_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 			unsigned long kernel_len, char *initrd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 			unsigned long initrd_len, char *cmdline,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 			unsigned long cmdline_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	unsigned int fdt_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	unsigned long kernel_load_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	unsigned long initrd_load_addr = 0, fdt_load_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	void *fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	const void *slave_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	struct elfhdr ehdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	char *modified_cmdline = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	struct kexec_elf_info elf_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	struct kexec_buf kbuf = { .image = image, .buf_min = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 				  .buf_max = ppc64_rma_size };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	struct kexec_buf pbuf = { .image = image, .buf_min = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 				  .buf_max = ppc64_rma_size, .top_down = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 				  .mem = KEXEC_BUF_MEM_UNKNOWN };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	ret = kexec_build_elf_info(kernel_buf, kernel_len, &ehdr, &elf_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	if (image->type == KEXEC_TYPE_CRASH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		/* min & max buffer values for kdump case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		kbuf.buf_min = pbuf.buf_min = crashk_res.start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		kbuf.buf_max = pbuf.buf_max =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 				((crashk_res.end < ppc64_rma_size) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 				 crashk_res.end : (ppc64_rma_size - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	ret = kexec_elf_load(image, &ehdr, &elf_info, &kbuf, &kernel_load_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	pr_debug("Loaded the kernel at 0x%lx\n", kernel_load_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	ret = kexec_load_purgatory(image, &pbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		pr_err("Loading purgatory failed.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	pr_debug("Loaded purgatory at 0x%lx\n", pbuf.mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	/* Load additional segments needed for panic kernel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	if (image->type == KEXEC_TYPE_CRASH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		ret = load_crashdump_segments_ppc64(image, &kbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 			pr_err("Failed to load kdump kernel segments\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		/* Setup cmdline for kdump kernel case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		modified_cmdline = setup_kdump_cmdline(image, cmdline,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 						       cmdline_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		if (!modified_cmdline) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 			pr_err("Setting up cmdline for kdump kernel failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		cmdline = modified_cmdline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	if (initrd != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		kbuf.buffer = initrd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		kbuf.bufsz = kbuf.memsz = initrd_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		kbuf.buf_align = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		kbuf.top_down = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		ret = kexec_add_buffer(&kbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		initrd_load_addr = kbuf.mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		pr_debug("Loaded initrd at 0x%lx\n", initrd_load_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	fdt_size = kexec_fdt_totalsize_ppc64(image);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	fdt = kmalloc(fdt_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	if (!fdt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		pr_err("Not enough memory for the device tree.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	ret = fdt_open_into(initial_boot_params, fdt, fdt_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		pr_err("Error setting up the new device tree.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	ret = setup_new_fdt_ppc64(image, fdt, initrd_load_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 				  initrd_len, cmdline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	fdt_pack(fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	kbuf.buffer = fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	kbuf.bufsz = kbuf.memsz = fdt_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	kbuf.buf_align = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	kbuf.top_down = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	ret = kexec_add_buffer(&kbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	fdt_load_addr = kbuf.mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	pr_debug("Loaded device tree at 0x%lx\n", fdt_load_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	slave_code = elf_info.buffer + elf_info.proghdrs[0].p_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	ret = setup_purgatory_ppc64(image, slave_code, fdt, kernel_load_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 				    fdt_load_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		pr_err("Error setting up the purgatory.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	kfree(modified_cmdline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	kexec_free_elf_info(&elf_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	/* Make kimage_file_post_load_cleanup free the fdt buffer for us. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	return ret ? ERR_PTR(ret) : fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) const struct kexec_file_ops kexec_elf64_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	.probe = kexec_elf_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	.load = elf64_load,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) };