|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * kexec_file for LoongArch |
| 4 | + * |
| 5 | + * Author: Youling Tang <tangyouling@kylinos.cn> |
| 6 | + * Copyright (C) 2025 KylinSoft Corporation. |
| 7 | + * |
| 8 | + * Most code is derived from LoongArch port of kexec-tools |
| 9 | + */ |
| 10 | + |
| 11 | +#define pr_fmt(fmt) "kexec_file: " fmt |
| 12 | + |
| 13 | +#include <linux/ioport.h> |
| 14 | +#include <linux/kernel.h> |
| 15 | +#include <linux/kexec.h> |
| 16 | +#include <linux/memblock.h> |
| 17 | +#include <linux/slab.h> |
| 18 | +#include <linux/string.h> |
| 19 | +#include <linux/types.h> |
| 20 | +#include <linux/vmalloc.h> |
| 21 | +#include <asm/bootinfo.h> |
| 22 | + |
| 23 | +const struct kexec_file_ops * const kexec_file_loaders[] = { |
| 24 | + NULL |
| 25 | +}; |
| 26 | + |
| 27 | +int arch_kimage_file_post_load_cleanup(struct kimage *image) |
| 28 | +{ |
| 29 | + vfree(image->elf_headers); |
| 30 | + image->elf_headers = NULL; |
| 31 | + image->elf_headers_sz = 0; |
| 32 | + |
| 33 | + return kexec_image_post_load_cleanup_default(image); |
| 34 | +} |
| 35 | + |
| 36 | +/* Add the "kexec_file" command line parameter to command line. */ |
| 37 | +static void cmdline_add_loader(unsigned long *cmdline_tmplen, char *modified_cmdline) |
| 38 | +{ |
| 39 | + int loader_strlen; |
| 40 | + |
| 41 | + loader_strlen = sprintf(modified_cmdline + (*cmdline_tmplen), "kexec_file "); |
| 42 | + *cmdline_tmplen += loader_strlen; |
| 43 | +} |
| 44 | + |
| 45 | +/* Add the "initrd=start,size" command line parameter to command line. */ |
| 46 | +static void cmdline_add_initrd(struct kimage *image, unsigned long *cmdline_tmplen, |
| 47 | + char *modified_cmdline, unsigned long initrd) |
| 48 | +{ |
| 49 | + int initrd_strlen; |
| 50 | + |
| 51 | + initrd_strlen = sprintf(modified_cmdline + (*cmdline_tmplen), "initrd=0x%lx,0x%lx ", |
| 52 | + initrd, image->initrd_buf_len); |
| 53 | + *cmdline_tmplen += initrd_strlen; |
| 54 | +} |
| 55 | + |
| 56 | +/* |
| 57 | + * Try to add the initrd to the image. If it is not possible to find valid |
| 58 | + * locations, this function will undo changes to the image and return non zero. |
| 59 | + */ |
| 60 | +int load_other_segments(struct kimage *image, |
| 61 | + unsigned long kernel_load_addr, unsigned long kernel_size, |
| 62 | + char *initrd, unsigned long initrd_len, char *cmdline, unsigned long cmdline_len) |
| 63 | +{ |
| 64 | + int ret = 0; |
| 65 | + unsigned long cmdline_tmplen = 0; |
| 66 | + unsigned long initrd_load_addr = 0; |
| 67 | + unsigned long orig_segments = image->nr_segments; |
| 68 | + char *modified_cmdline = NULL; |
| 69 | + struct kexec_buf kbuf; |
| 70 | + |
| 71 | + kbuf.image = image; |
| 72 | + /* Don't allocate anything below the kernel */ |
| 73 | + kbuf.buf_min = kernel_load_addr + kernel_size; |
| 74 | + |
| 75 | + modified_cmdline = kzalloc(COMMAND_LINE_SIZE, GFP_KERNEL); |
| 76 | + if (!modified_cmdline) |
| 77 | + return -EINVAL; |
| 78 | + |
| 79 | + cmdline_add_loader(&cmdline_tmplen, modified_cmdline); |
| 80 | + /* Ensure it's null terminated */ |
| 81 | + modified_cmdline[COMMAND_LINE_SIZE - 1] = '\0'; |
| 82 | + |
| 83 | + /* Load initrd */ |
| 84 | + if (initrd) { |
| 85 | + kbuf.buffer = initrd; |
| 86 | + kbuf.bufsz = initrd_len; |
| 87 | + kbuf.mem = KEXEC_BUF_MEM_UNKNOWN; |
| 88 | + kbuf.memsz = initrd_len; |
| 89 | + kbuf.buf_align = 0; |
| 90 | + /* within 1GB-aligned window of up to 32GB in size */ |
| 91 | + kbuf.buf_max = round_down(kernel_load_addr, SZ_1G) + (unsigned long)SZ_1G * 32; |
| 92 | + kbuf.top_down = false; |
| 93 | + |
| 94 | + ret = kexec_add_buffer(&kbuf); |
| 95 | + if (ret < 0) |
| 96 | + goto out_err; |
| 97 | + initrd_load_addr = kbuf.mem; |
| 98 | + |
| 99 | + kexec_dprintk("Loaded initrd at 0x%lx bufsz=0x%lx memsz=0x%lx\n", |
| 100 | + initrd_load_addr, kbuf.bufsz, kbuf.memsz); |
| 101 | + |
| 102 | + /* Add the initrd=start,size parameter to the command line */ |
| 103 | + cmdline_add_initrd(image, &cmdline_tmplen, modified_cmdline, initrd_load_addr); |
| 104 | + } |
| 105 | + |
| 106 | + if (cmdline_len + cmdline_tmplen > COMMAND_LINE_SIZE) { |
| 107 | + pr_err("Appending command line exceeds COMMAND_LINE_SIZE\n"); |
| 108 | + ret = -EINVAL; |
| 109 | + goto out_err; |
| 110 | + } |
| 111 | + |
| 112 | + memcpy(modified_cmdline + cmdline_tmplen, cmdline, cmdline_len); |
| 113 | + cmdline = modified_cmdline; |
| 114 | + image->arch.cmdline_ptr = (unsigned long)cmdline; |
| 115 | + |
| 116 | + return 0; |
| 117 | + |
| 118 | +out_err: |
| 119 | + image->nr_segments = orig_segments; |
| 120 | + kfree(modified_cmdline); |
| 121 | + return ret; |
| 122 | +} |
0 commit comments