root/arch/powerpc/kernel/machine_kexec_file_64.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. arch_kexec_kernel_image_probe
  2. setup_purgatory
  3. delete_fdt_mem_rsv
  4. setup_new_fdt

   1 // SPDX-License-Identifier: GPL-2.0-only
   2 /*
   3  * ppc64 code to implement the kexec_file_load syscall
   4  *
   5  * Copyright (C) 2004  Adam Litke (agl@us.ibm.com)
   6  * Copyright (C) 2004  IBM Corp.
   7  * Copyright (C) 2004,2005  Milton D Miller II, IBM Corporation
   8  * Copyright (C) 2005  R Sharada (sharada@in.ibm.com)
   9  * Copyright (C) 2006  Mohan Kumar M (mohan@in.ibm.com)
  10  * Copyright (C) 2016  IBM Corporation
  11  *
  12  * Based on kexec-tools' kexec-elf-ppc64.c, fs2dt.c.
  13  * Heavily modified for the kernel by
  14  * Thiago Jung Bauermann <bauerman@linux.vnet.ibm.com>.
  15  */
  16 
  17 #include <linux/slab.h>
  18 #include <linux/kexec.h>
  19 #include <linux/of_fdt.h>
  20 #include <linux/libfdt.h>
  21 #include <asm/ima.h>
  22 
  23 #define SLAVE_CODE_SIZE         256
  24 
  25 const struct kexec_file_ops * const kexec_file_loaders[] = {
  26         &kexec_elf64_ops,
  27         NULL
  28 };
  29 
  30 int arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
  31                                   unsigned long buf_len)
  32 {
  33         /* We don't support crash kernels yet. */
  34         if (image->type == KEXEC_TYPE_CRASH)
  35                 return -EOPNOTSUPP;
  36 
  37         return kexec_image_probe_default(image, buf, buf_len);
  38 }
  39 
  40 /**
  41  * setup_purgatory - initialize the purgatory's global variables
  42  * @image:              kexec image.
  43  * @slave_code:         Slave code for the purgatory.
  44  * @fdt:                Flattened device tree for the next kernel.
  45  * @kernel_load_addr:   Address where the kernel is loaded.
  46  * @fdt_load_addr:      Address where the flattened device tree is loaded.
  47  *
  48  * Return: 0 on success, or negative errno on error.
  49  */
  50 int setup_purgatory(struct kimage *image, const void *slave_code,
  51                     const void *fdt, unsigned long kernel_load_addr,
  52                     unsigned long fdt_load_addr)
  53 {
  54         unsigned int *slave_code_buf, master_entry;
  55         int ret;
  56 
  57         slave_code_buf = kmalloc(SLAVE_CODE_SIZE, GFP_KERNEL);
  58         if (!slave_code_buf)
  59                 return -ENOMEM;
  60 
  61         /* Get the slave code from the new kernel and put it in purgatory. */
  62         ret = kexec_purgatory_get_set_symbol(image, "purgatory_start",
  63                                              slave_code_buf, SLAVE_CODE_SIZE,
  64                                              true);
  65         if (ret) {
  66                 kfree(slave_code_buf);
  67                 return ret;
  68         }
  69 
  70         master_entry = slave_code_buf[0];
  71         memcpy(slave_code_buf, slave_code, SLAVE_CODE_SIZE);
  72         slave_code_buf[0] = master_entry;
  73         ret = kexec_purgatory_get_set_symbol(image, "purgatory_start",
  74                                              slave_code_buf, SLAVE_CODE_SIZE,
  75                                              false);
  76         kfree(slave_code_buf);
  77 
  78         ret = kexec_purgatory_get_set_symbol(image, "kernel", &kernel_load_addr,
  79                                              sizeof(kernel_load_addr), false);
  80         if (ret)
  81                 return ret;
  82         ret = kexec_purgatory_get_set_symbol(image, "dt_offset", &fdt_load_addr,
  83                                              sizeof(fdt_load_addr), false);
  84         if (ret)
  85                 return ret;
  86 
  87         return 0;
  88 }
  89 
  90 /**
  91  * delete_fdt_mem_rsv - delete memory reservation with given address and size
  92  *
  93  * Return: 0 on success, or negative errno on error.
  94  */
  95 int delete_fdt_mem_rsv(void *fdt, unsigned long start, unsigned long size)
  96 {
  97         int i, ret, num_rsvs = fdt_num_mem_rsv(fdt);
  98 
  99         for (i = 0; i < num_rsvs; i++) {
 100                 uint64_t rsv_start, rsv_size;
 101 
 102                 ret = fdt_get_mem_rsv(fdt, i, &rsv_start, &rsv_size);
 103                 if (ret) {
 104                         pr_err("Malformed device tree.\n");
 105                         return -EINVAL;
 106                 }
 107 
 108                 if (rsv_start == start && rsv_size == size) {
 109                         ret = fdt_del_mem_rsv(fdt, i);
 110                         if (ret) {
 111                                 pr_err("Error deleting device tree reservation.\n");
 112                                 return -EINVAL;
 113                         }
 114 
 115                         return 0;
 116                 }
 117         }
 118 
 119         return -ENOENT;
 120 }
 121 
 122 /*
 123  * setup_new_fdt - modify /chosen and memory reservation for the next kernel
 124  * @image:              kexec image being loaded.
 125  * @fdt:                Flattened device tree for the next kernel.
 126  * @initrd_load_addr:   Address where the next initrd will be loaded.
 127  * @initrd_len:         Size of the next initrd, or 0 if there will be none.
 128  * @cmdline:            Command line for the next kernel, or NULL if there will
 129  *                      be none.
 130  *
 131  * Return: 0 on success, or negative errno on error.
 132  */
 133 int setup_new_fdt(const struct kimage *image, void *fdt,
 134                   unsigned long initrd_load_addr, unsigned long initrd_len,
 135                   const char *cmdline)
 136 {
 137         int ret, chosen_node;
 138         const void *prop;
 139 
 140         /* Remove memory reservation for the current device tree. */
 141         ret = delete_fdt_mem_rsv(fdt, __pa(initial_boot_params),
 142                                  fdt_totalsize(initial_boot_params));
 143         if (ret == 0)
 144                 pr_debug("Removed old device tree reservation.\n");
 145         else if (ret != -ENOENT)
 146                 return ret;
 147 
 148         chosen_node = fdt_path_offset(fdt, "/chosen");
 149         if (chosen_node == -FDT_ERR_NOTFOUND) {
 150                 chosen_node = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"),
 151                                               "chosen");
 152                 if (chosen_node < 0) {
 153                         pr_err("Error creating /chosen.\n");
 154                         return -EINVAL;
 155                 }
 156         } else if (chosen_node < 0) {
 157                 pr_err("Malformed device tree: error reading /chosen.\n");
 158                 return -EINVAL;
 159         }
 160 
 161         /* Did we boot using an initrd? */
 162         prop = fdt_getprop(fdt, chosen_node, "linux,initrd-start", NULL);
 163         if (prop) {
 164                 uint64_t tmp_start, tmp_end, tmp_size;
 165 
 166                 tmp_start = fdt64_to_cpu(*((const fdt64_t *) prop));
 167 
 168                 prop = fdt_getprop(fdt, chosen_node, "linux,initrd-end", NULL);
 169                 if (!prop) {
 170                         pr_err("Malformed device tree.\n");
 171                         return -EINVAL;
 172                 }
 173                 tmp_end = fdt64_to_cpu(*((const fdt64_t *) prop));
 174 
 175                 /*
 176                  * kexec reserves exact initrd size, while firmware may
 177                  * reserve a multiple of PAGE_SIZE, so check for both.
 178                  */
 179                 tmp_size = tmp_end - tmp_start;
 180                 ret = delete_fdt_mem_rsv(fdt, tmp_start, tmp_size);
 181                 if (ret == -ENOENT)
 182                         ret = delete_fdt_mem_rsv(fdt, tmp_start,
 183                                                  round_up(tmp_size, PAGE_SIZE));
 184                 if (ret == 0)
 185                         pr_debug("Removed old initrd reservation.\n");
 186                 else if (ret != -ENOENT)
 187                         return ret;
 188 
 189                 /* If there's no new initrd, delete the old initrd's info. */
 190                 if (initrd_len == 0) {
 191                         ret = fdt_delprop(fdt, chosen_node,
 192                                           "linux,initrd-start");
 193                         if (ret) {
 194                                 pr_err("Error deleting linux,initrd-start.\n");
 195                                 return -EINVAL;
 196                         }
 197 
 198                         ret = fdt_delprop(fdt, chosen_node, "linux,initrd-end");
 199                         if (ret) {
 200                                 pr_err("Error deleting linux,initrd-end.\n");
 201                                 return -EINVAL;
 202                         }
 203                 }
 204         }
 205 
 206         if (initrd_len) {
 207                 ret = fdt_setprop_u64(fdt, chosen_node,
 208                                       "linux,initrd-start",
 209                                       initrd_load_addr);
 210                 if (ret < 0)
 211                         goto err;
 212 
 213                 /* initrd-end is the first address after the initrd image. */
 214                 ret = fdt_setprop_u64(fdt, chosen_node, "linux,initrd-end",
 215                                       initrd_load_addr + initrd_len);
 216                 if (ret < 0)
 217                         goto err;
 218 
 219                 ret = fdt_add_mem_rsv(fdt, initrd_load_addr, initrd_len);
 220                 if (ret) {
 221                         pr_err("Error reserving initrd memory: %s\n",
 222                                fdt_strerror(ret));
 223                         return -EINVAL;
 224                 }
 225         }
 226 
 227         if (cmdline != NULL) {
 228                 ret = fdt_setprop_string(fdt, chosen_node, "bootargs", cmdline);
 229                 if (ret < 0)
 230                         goto err;
 231         } else {
 232                 ret = fdt_delprop(fdt, chosen_node, "bootargs");
 233                 if (ret && ret != -FDT_ERR_NOTFOUND) {
 234                         pr_err("Error deleting bootargs.\n");
 235                         return -EINVAL;
 236                 }
 237         }
 238 
 239         ret = setup_ima_buffer(image, fdt, chosen_node);
 240         if (ret) {
 241                 pr_err("Error setting up the new device tree.\n");
 242                 return ret;
 243         }
 244 
 245         ret = fdt_setprop(fdt, chosen_node, "linux,booted-from-kexec", NULL, 0);
 246         if (ret)
 247                 goto err;
 248 
 249         return 0;
 250 
 251 err:
 252         pr_err("Error setting up the new device tree.\n");
 253         return -EINVAL;
 254 }

/* [<][>][^][v][top][bottom][index][help] */