elf_64.c 4.3 KB

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