kexec_file.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Load ELF vmlinux file for the kexec_file_load syscall.
  4. *
  5. * Copyright (c) 2019 Sven Schnelle <[email protected]>
  6. *
  7. */
  8. #include <linux/elf.h>
  9. #include <linux/kexec.h>
  10. #include <linux/libfdt.h>
  11. #include <linux/module.h>
  12. #include <linux/of_fdt.h>
  13. #include <linux/slab.h>
  14. #include <linux/types.h>
  15. static void *elf_load(struct kimage *image, char *kernel_buf,
  16. unsigned long kernel_len, char *initrd,
  17. unsigned long initrd_len, char *cmdline,
  18. unsigned long cmdline_len)
  19. {
  20. int ret, i;
  21. unsigned long kernel_load_addr;
  22. struct elfhdr ehdr;
  23. struct kexec_elf_info elf_info;
  24. struct kexec_buf kbuf = { .image = image, .buf_min = 0,
  25. .buf_max = -1UL, };
  26. ret = kexec_build_elf_info(kernel_buf, kernel_len, &ehdr, &elf_info);
  27. if (ret)
  28. goto out;
  29. ret = kexec_elf_load(image, &ehdr, &elf_info, &kbuf, &kernel_load_addr);
  30. if (ret)
  31. goto out;
  32. image->start = __pa(elf_info.ehdr->e_entry);
  33. for (i = 0; i < image->nr_segments; i++)
  34. image->segment[i].mem = __pa(image->segment[i].mem);
  35. pr_debug("Loaded the kernel at 0x%lx, entry at 0x%lx\n",
  36. kernel_load_addr, image->start);
  37. if (initrd != NULL) {
  38. kbuf.buffer = initrd;
  39. kbuf.bufsz = kbuf.memsz = initrd_len;
  40. kbuf.buf_align = PAGE_SIZE;
  41. kbuf.top_down = false;
  42. kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
  43. ret = kexec_add_buffer(&kbuf);
  44. if (ret)
  45. goto out;
  46. pr_debug("Loaded initrd at 0x%lx\n", kbuf.mem);
  47. image->arch.initrd_start = kbuf.mem;
  48. image->arch.initrd_end = kbuf.mem + initrd_len;
  49. }
  50. if (cmdline != NULL) {
  51. kbuf.buffer = cmdline;
  52. kbuf.bufsz = kbuf.memsz = ALIGN(cmdline_len, 8);
  53. kbuf.buf_align = PAGE_SIZE;
  54. kbuf.top_down = false;
  55. kbuf.buf_min = PAGE0->mem_free + PAGE_SIZE;
  56. kbuf.buf_max = kernel_load_addr;
  57. kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
  58. ret = kexec_add_buffer(&kbuf);
  59. if (ret)
  60. goto out;
  61. pr_debug("Loaded cmdline at 0x%lx\n", kbuf.mem);
  62. image->arch.cmdline = kbuf.mem;
  63. }
  64. out:
  65. return NULL;
  66. }
  67. const struct kexec_file_ops kexec_elf_ops = {
  68. .probe = kexec_elf_probe,
  69. .load = elf_load,
  70. };
  71. const struct kexec_file_ops * const kexec_file_loaders[] = {
  72. &kexec_elf_ops,
  73. NULL
  74. };