machine_kexec_file.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * kexec_file for arm64
  4. *
  5. * Copyright (C) 2018 Linaro Limited
  6. * Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
  7. *
  8. * Most code is derived from arm64 port of kexec-tools
  9. */
  10. #define pr_fmt(fmt) "kexec_file: " fmt
  11. #include <linux/ioport.h>
  12. #include <linux/kernel.h>
  13. #include <linux/kexec.h>
  14. #include <linux/libfdt.h>
  15. #include <linux/memblock.h>
  16. #include <linux/of.h>
  17. #include <linux/of_fdt.h>
  18. #include <linux/slab.h>
  19. #include <linux/string.h>
  20. #include <linux/types.h>
  21. #include <linux/vmalloc.h>
  22. const struct kexec_file_ops * const kexec_file_loaders[] = {
  23. &kexec_image_ops,
  24. NULL
  25. };
  26. int arch_kimage_file_post_load_cleanup(struct kimage *image)
  27. {
  28. kvfree(image->arch.dtb);
  29. image->arch.dtb = NULL;
  30. vfree(image->elf_headers);
  31. image->elf_headers = NULL;
  32. image->elf_headers_sz = 0;
  33. return kexec_image_post_load_cleanup_default(image);
  34. }
  35. #ifdef CONFIG_CRASH_DUMP
  36. static int prepare_elf_headers(void **addr, unsigned long *sz)
  37. {
  38. struct crash_mem *cmem;
  39. unsigned int nr_ranges;
  40. int ret;
  41. u64 i;
  42. phys_addr_t start, end;
  43. nr_ranges = 2; /* for exclusion of crashkernel region */
  44. for_each_mem_range(i, &start, &end)
  45. nr_ranges++;
  46. cmem = kmalloc(struct_size(cmem, ranges, nr_ranges), GFP_KERNEL);
  47. if (!cmem)
  48. return -ENOMEM;
  49. cmem->max_nr_ranges = nr_ranges;
  50. cmem->nr_ranges = 0;
  51. for_each_mem_range(i, &start, &end) {
  52. cmem->ranges[cmem->nr_ranges].start = start;
  53. cmem->ranges[cmem->nr_ranges].end = end - 1;
  54. cmem->nr_ranges++;
  55. }
  56. /* Exclude crashkernel region */
  57. ret = crash_exclude_mem_range(cmem, crashk_res.start, crashk_res.end);
  58. if (ret)
  59. goto out;
  60. if (crashk_low_res.end) {
  61. ret = crash_exclude_mem_range(cmem, crashk_low_res.start, crashk_low_res.end);
  62. if (ret)
  63. goto out;
  64. }
  65. ret = crash_prepare_elf64_headers(cmem, true, addr, sz);
  66. out:
  67. kfree(cmem);
  68. return ret;
  69. }
  70. #endif
  71. /*
  72. * Tries to add the initrd and DTB to the image. If it is not possible to find
  73. * valid locations, this function will undo changes to the image and return non
  74. * zero.
  75. */
  76. int load_other_segments(struct kimage *image,
  77. unsigned long kernel_load_addr,
  78. unsigned long kernel_size,
  79. char *initrd, unsigned long initrd_len,
  80. char *cmdline)
  81. {
  82. struct kexec_buf kbuf;
  83. void *dtb = NULL;
  84. unsigned long initrd_load_addr = 0, dtb_len,
  85. orig_segments = image->nr_segments;
  86. int ret = 0;
  87. kbuf.image = image;
  88. /* not allocate anything below the kernel */
  89. kbuf.buf_min = kernel_load_addr + kernel_size;
  90. #ifdef CONFIG_CRASH_DUMP
  91. /* load elf core header */
  92. void *headers;
  93. unsigned long headers_sz;
  94. if (image->type == KEXEC_TYPE_CRASH) {
  95. ret = prepare_elf_headers(&headers, &headers_sz);
  96. if (ret) {
  97. pr_err("Preparing elf core header failed\n");
  98. goto out_err;
  99. }
  100. kbuf.buffer = headers;
  101. kbuf.bufsz = headers_sz;
  102. kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
  103. kbuf.memsz = headers_sz;
  104. kbuf.buf_align = SZ_64K; /* largest supported page size */
  105. kbuf.buf_max = ULONG_MAX;
  106. kbuf.top_down = true;
  107. ret = kexec_add_buffer(&kbuf);
  108. if (ret) {
  109. vfree(headers);
  110. goto out_err;
  111. }
  112. image->elf_headers = headers;
  113. image->elf_load_addr = kbuf.mem;
  114. image->elf_headers_sz = headers_sz;
  115. kexec_dprintk("Loaded elf core header at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
  116. image->elf_load_addr, kbuf.bufsz, kbuf.memsz);
  117. }
  118. #endif
  119. /* load initrd */
  120. if (initrd) {
  121. kbuf.buffer = initrd;
  122. kbuf.bufsz = initrd_len;
  123. kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
  124. kbuf.memsz = initrd_len;
  125. kbuf.buf_align = 0;
  126. /* within 1GB-aligned window of up to 32GB in size */
  127. kbuf.buf_max = round_down(kernel_load_addr, SZ_1G)
  128. + (unsigned long)SZ_1G * 32;
  129. kbuf.top_down = false;
  130. ret = kexec_add_buffer(&kbuf);
  131. if (ret)
  132. goto out_err;
  133. initrd_load_addr = kbuf.mem;
  134. kexec_dprintk("Loaded initrd at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
  135. initrd_load_addr, kbuf.bufsz, kbuf.memsz);
  136. }
  137. /* load dtb */
  138. dtb = of_kexec_alloc_and_setup_fdt(image, initrd_load_addr,
  139. initrd_len, cmdline, 0);
  140. if (!dtb) {
  141. pr_err("Preparing for new dtb failed\n");
  142. ret = -EINVAL;
  143. goto out_err;
  144. }
  145. /* trim it */
  146. fdt_pack(dtb);
  147. dtb_len = fdt_totalsize(dtb);
  148. kbuf.buffer = dtb;
  149. kbuf.bufsz = dtb_len;
  150. kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
  151. kbuf.memsz = dtb_len;
  152. /* not across 2MB boundary */
  153. kbuf.buf_align = SZ_2M;
  154. kbuf.buf_max = ULONG_MAX;
  155. kbuf.top_down = true;
  156. ret = kexec_add_buffer(&kbuf);
  157. if (ret)
  158. goto out_err;
  159. image->arch.dtb = dtb;
  160. image->arch.dtb_mem = kbuf.mem;
  161. kexec_dprintk("Loaded dtb at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
  162. kbuf.mem, kbuf.bufsz, kbuf.memsz);
  163. return 0;
  164. out_err:
  165. image->nr_segments = orig_segments;
  166. kvfree(dtb);
  167. return ret;
  168. }