file_load.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * powerpc code to implement the kexec_file_load syscall
  4. *
  5. * Copyright (C) 2004 Adam Litke ([email protected])
  6. * Copyright (C) 2004 IBM Corp.
  7. * Copyright (C) 2004,2005 Milton D Miller II, IBM Corporation
  8. * Copyright (C) 2005 R Sharada ([email protected])
  9. * Copyright (C) 2006 Mohan Kumar M ([email protected])
  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 <[email protected]>.
  15. */
  16. #include <linux/slab.h>
  17. #include <linux/kexec.h>
  18. #include <linux/of_fdt.h>
  19. #include <linux/libfdt.h>
  20. #include <asm/setup.h>
  21. #define SLAVE_CODE_SIZE 256 /* First 0x100 bytes */
  22. /**
  23. * setup_kdump_cmdline - Prepend "elfcorehdr=<addr> " to command line
  24. * of kdump kernel for exporting the core.
  25. * @image: Kexec image
  26. * @cmdline: Command line parameters to update.
  27. * @cmdline_len: Length of the cmdline parameters.
  28. *
  29. * kdump segment must be setup before calling this function.
  30. *
  31. * Returns new cmdline buffer for kdump kernel on success, NULL otherwise.
  32. */
  33. char *setup_kdump_cmdline(struct kimage *image, char *cmdline,
  34. unsigned long cmdline_len)
  35. {
  36. int elfcorehdr_strlen;
  37. char *cmdline_ptr;
  38. cmdline_ptr = kzalloc(COMMAND_LINE_SIZE, GFP_KERNEL);
  39. if (!cmdline_ptr)
  40. return NULL;
  41. elfcorehdr_strlen = sprintf(cmdline_ptr, "elfcorehdr=0x%lx ",
  42. image->elf_load_addr);
  43. if (elfcorehdr_strlen + cmdline_len > COMMAND_LINE_SIZE) {
  44. pr_err("Appending elfcorehdr=<addr> exceeds cmdline size\n");
  45. kfree(cmdline_ptr);
  46. return NULL;
  47. }
  48. memcpy(cmdline_ptr + elfcorehdr_strlen, cmdline, cmdline_len);
  49. // Ensure it's nul terminated
  50. cmdline_ptr[COMMAND_LINE_SIZE - 1] = '\0';
  51. return cmdline_ptr;
  52. }
  53. /**
  54. * setup_purgatory - initialize the purgatory's global variables
  55. * @image: kexec image.
  56. * @slave_code: Slave code for the purgatory.
  57. * @fdt: Flattened device tree for the next kernel.
  58. * @kernel_load_addr: Address where the kernel is loaded.
  59. * @fdt_load_addr: Address where the flattened device tree is loaded.
  60. *
  61. * Return: 0 on success, or negative errno on error.
  62. */
  63. int setup_purgatory(struct kimage *image, const void *slave_code,
  64. const void *fdt, unsigned long kernel_load_addr,
  65. unsigned long fdt_load_addr)
  66. {
  67. unsigned int *slave_code_buf, master_entry;
  68. int ret;
  69. slave_code_buf = kmalloc(SLAVE_CODE_SIZE, GFP_KERNEL);
  70. if (!slave_code_buf)
  71. return -ENOMEM;
  72. /* Get the slave code from the new kernel and put it in purgatory. */
  73. ret = kexec_purgatory_get_set_symbol(image, "purgatory_start",
  74. slave_code_buf, SLAVE_CODE_SIZE,
  75. true);
  76. if (ret) {
  77. kfree(slave_code_buf);
  78. return ret;
  79. }
  80. master_entry = slave_code_buf[0];
  81. memcpy(slave_code_buf, slave_code, SLAVE_CODE_SIZE);
  82. slave_code_buf[0] = master_entry;
  83. ret = kexec_purgatory_get_set_symbol(image, "purgatory_start",
  84. slave_code_buf, SLAVE_CODE_SIZE,
  85. false);
  86. kfree(slave_code_buf);
  87. ret = kexec_purgatory_get_set_symbol(image, "kernel", &kernel_load_addr,
  88. sizeof(kernel_load_addr), false);
  89. if (ret)
  90. return ret;
  91. ret = kexec_purgatory_get_set_symbol(image, "dt_offset", &fdt_load_addr,
  92. sizeof(fdt_load_addr), false);
  93. if (ret)
  94. return ret;
  95. return 0;
  96. }