2 * kexec: kexec_file_load system call
4 * Copyright (C) 2014 Red Hat Inc.
6 * Vivek Goyal <vgoyal@redhat.com>
8 * This source code is licensed under the GNU General Public License,
9 * Version 2. See the file COPYING for more details.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/capability.h>
16 #include <linux/file.h>
17 #include <linux/slab.h>
18 #include <linux/kexec.h>
19 #include <linux/mutex.h>
20 #include <linux/list.h>
22 #include <crypto/hash.h>
23 #include <crypto/sha.h>
24 #include <linux/syscalls.h>
25 #include <linux/vmalloc.h>
26 #include "kexec_internal.h"
29 * Declare these symbols weak so that if architecture provides a purgatory,
30 * these will be overridden.
32 char __weak kexec_purgatory[0];
33 size_t __weak kexec_purgatory_size = 0;
35 static int kexec_calculate_store_digests(struct kimage *image);
37 /* Architectures can provide this probe function */
38 int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
39 unsigned long buf_len)
44 void * __weak arch_kexec_kernel_image_load(struct kimage *image)
46 return ERR_PTR(-ENOEXEC);
49 int __weak arch_kimage_file_post_load_cleanup(struct kimage *image)
54 #ifdef CONFIG_KEXEC_VERIFY_SIG
55 int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf,
56 unsigned long buf_len)
63 * Free up memory used by kernel, initrd, and command line. This is temporary
64 * memory allocation which is not needed any more after these buffers have
65 * been loaded into separate segments and have been copied elsewhere.
67 void kimage_file_post_load_cleanup(struct kimage *image)
69 struct purgatory_info *pi = &image->purgatory_info;
71 vfree(image->kernel_buf);
72 image->kernel_buf = NULL;
74 vfree(image->initrd_buf);
75 image->initrd_buf = NULL;
77 kfree(image->cmdline_buf);
78 image->cmdline_buf = NULL;
80 vfree(pi->purgatory_buf);
81 pi->purgatory_buf = NULL;
86 /* See if architecture has anything to cleanup post load */
87 arch_kimage_file_post_load_cleanup(image);
90 * Above call should have called into bootloader to free up
91 * any data stored in kimage->image_loader_data. It should
92 * be ok now to free it up.
94 kfree(image->image_loader_data);
95 image->image_loader_data = NULL;
99 * In file mode list of segments is prepared by kernel. Copy relevant
100 * data from user space, do error checking, prepare segment list
103 kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,
104 const char __user *cmdline_ptr,
105 unsigned long cmdline_len, unsigned flags)
111 ret = kernel_read_file_from_fd(kernel_fd, &image->kernel_buf,
112 &size, INT_MAX, READING_KEXEC_IMAGE);
115 image->kernel_buf_len = size;
117 /* Call arch image probe handlers */
118 ret = arch_kexec_kernel_image_probe(image, image->kernel_buf,
119 image->kernel_buf_len);
123 #ifdef CONFIG_KEXEC_VERIFY_SIG
124 ret = arch_kexec_kernel_verify_sig(image, image->kernel_buf,
125 image->kernel_buf_len);
127 pr_debug("kernel signature verification failed.\n");
130 pr_debug("kernel signature verification successful.\n");
132 /* It is possible that there no initramfs is being loaded */
133 if (!(flags & KEXEC_FILE_NO_INITRAMFS)) {
134 ret = kernel_read_file_from_fd(initrd_fd, &image->initrd_buf,
136 READING_KEXEC_INITRAMFS);
139 image->initrd_buf_len = size;
143 image->cmdline_buf = kzalloc(cmdline_len, GFP_KERNEL);
144 if (!image->cmdline_buf) {
149 ret = copy_from_user(image->cmdline_buf, cmdline_ptr,
156 image->cmdline_buf_len = cmdline_len;
158 /* command line should be a string with last byte null */
159 if (image->cmdline_buf[cmdline_len - 1] != '\0') {
165 /* Call arch image load handlers */
166 ldata = arch_kexec_kernel_image_load(image);
169 ret = PTR_ERR(ldata);
173 image->image_loader_data = ldata;
175 /* In case of error, free up all allocated memory in this function */
177 kimage_file_post_load_cleanup(image);
182 kimage_file_alloc_init(struct kimage **rimage, int kernel_fd,
183 int initrd_fd, const char __user *cmdline_ptr,
184 unsigned long cmdline_len, unsigned long flags)
187 struct kimage *image;
188 bool kexec_on_panic = flags & KEXEC_FILE_ON_CRASH;
190 image = do_kimage_alloc_init();
194 image->file_mode = 1;
196 if (kexec_on_panic) {
197 /* Enable special crash kernel control page alloc policy. */
198 image->control_page = crashk_res.start;
199 image->type = KEXEC_TYPE_CRASH;
202 ret = kimage_file_prepare_segments(image, kernel_fd, initrd_fd,
203 cmdline_ptr, cmdline_len, flags);
207 ret = sanity_check_segment_list(image);
209 goto out_free_post_load_bufs;
212 image->control_code_page = kimage_alloc_control_pages(image,
213 get_order(KEXEC_CONTROL_PAGE_SIZE));
214 if (!image->control_code_page) {
215 pr_err("Could not allocate control_code_buffer\n");
216 goto out_free_post_load_bufs;
219 if (!kexec_on_panic) {
220 image->swap_page = kimage_alloc_control_pages(image, 0);
221 if (!image->swap_page) {
222 pr_err("Could not allocate swap buffer\n");
223 goto out_free_control_pages;
229 out_free_control_pages:
230 kimage_free_page_list(&image->control_pages);
231 out_free_post_load_bufs:
232 kimage_file_post_load_cleanup(image);
238 SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd,
239 unsigned long, cmdline_len, const char __user *, cmdline_ptr,
240 unsigned long, flags)
243 struct kimage **dest_image, *image;
245 /* We only trust the superuser with rebooting the system. */
246 if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
249 /* Make sure we have a legal set of flags */
250 if (flags != (flags & KEXEC_FILE_FLAGS))
255 if (!mutex_trylock(&kexec_mutex))
258 dest_image = &kexec_image;
259 if (flags & KEXEC_FILE_ON_CRASH) {
260 dest_image = &kexec_crash_image;
261 if (kexec_crash_image)
262 arch_kexec_unprotect_crashkres();
265 if (flags & KEXEC_FILE_UNLOAD)
269 * In case of crash, new kernel gets loaded in reserved region. It is
270 * same memory where old crash kernel might be loaded. Free any
271 * current crash dump kernel before we corrupt it.
273 if (flags & KEXEC_FILE_ON_CRASH)
274 kimage_free(xchg(&kexec_crash_image, NULL));
276 ret = kimage_file_alloc_init(&image, kernel_fd, initrd_fd, cmdline_ptr,
281 ret = machine_kexec_prepare(image);
285 ret = kexec_calculate_store_digests(image);
289 for (i = 0; i < image->nr_segments; i++) {
290 struct kexec_segment *ksegment;
292 ksegment = &image->segment[i];
293 pr_debug("Loading segment %d: buf=0x%p bufsz=0x%zx mem=0x%lx memsz=0x%zx\n",
294 i, ksegment->buf, ksegment->bufsz, ksegment->mem,
297 ret = kimage_load_segment(image, &image->segment[i]);
302 kimage_terminate(image);
305 * Free up any temporary buffers allocated which are not needed
306 * after image has been loaded
308 kimage_file_post_load_cleanup(image);
310 image = xchg(dest_image, image);
312 if ((flags & KEXEC_FILE_ON_CRASH) && kexec_crash_image)
313 arch_kexec_protect_crashkres();
315 mutex_unlock(&kexec_mutex);
320 static int locate_mem_hole_top_down(unsigned long start, unsigned long end,
321 struct kexec_buf *kbuf)
323 struct kimage *image = kbuf->image;
324 unsigned long temp_start, temp_end;
326 temp_end = min(end, kbuf->buf_max);
327 temp_start = temp_end - kbuf->memsz;
330 /* align down start */
331 temp_start = temp_start & (~(kbuf->buf_align - 1));
333 if (temp_start < start || temp_start < kbuf->buf_min)
336 temp_end = temp_start + kbuf->memsz - 1;
339 * Make sure this does not conflict with any of existing
342 if (kimage_is_destination_range(image, temp_start, temp_end)) {
343 temp_start = temp_start - PAGE_SIZE;
347 /* We found a suitable memory range */
351 /* If we are here, we found a suitable memory range */
352 kbuf->mem = temp_start;
354 /* Success, stop navigating through remaining System RAM ranges */
358 static int locate_mem_hole_bottom_up(unsigned long start, unsigned long end,
359 struct kexec_buf *kbuf)
361 struct kimage *image = kbuf->image;
362 unsigned long temp_start, temp_end;
364 temp_start = max(start, kbuf->buf_min);
367 temp_start = ALIGN(temp_start, kbuf->buf_align);
368 temp_end = temp_start + kbuf->memsz - 1;
370 if (temp_end > end || temp_end > kbuf->buf_max)
373 * Make sure this does not conflict with any of existing
376 if (kimage_is_destination_range(image, temp_start, temp_end)) {
377 temp_start = temp_start + PAGE_SIZE;
381 /* We found a suitable memory range */
385 /* If we are here, we found a suitable memory range */
386 kbuf->mem = temp_start;
388 /* Success, stop navigating through remaining System RAM ranges */
392 static int locate_mem_hole_callback(u64 start, u64 end, void *arg)
394 struct kexec_buf *kbuf = (struct kexec_buf *)arg;
395 unsigned long sz = end - start + 1;
397 /* Returning 0 will take to next memory range */
398 if (sz < kbuf->memsz)
401 if (end < kbuf->buf_min || start > kbuf->buf_max)
405 * Allocate memory top down with-in ram range. Otherwise bottom up
409 return locate_mem_hole_top_down(start, end, kbuf);
410 return locate_mem_hole_bottom_up(start, end, kbuf);
414 * Helper function for placing a buffer in a kexec segment. This assumes
415 * that kexec_mutex is held.
417 int kexec_add_buffer(struct kimage *image, char *buffer, unsigned long bufsz,
418 unsigned long memsz, unsigned long buf_align,
419 unsigned long buf_min, unsigned long buf_max,
420 bool top_down, unsigned long *load_addr)
423 struct kexec_segment *ksegment;
424 struct kexec_buf buf, *kbuf;
427 /* Currently adding segment this way is allowed only in file mode */
428 if (!image->file_mode)
431 if (image->nr_segments >= KEXEC_SEGMENT_MAX)
435 * Make sure we are not trying to add buffer after allocating
436 * control pages. All segments need to be placed first before
437 * any control pages are allocated. As control page allocation
438 * logic goes through list of segments to make sure there are
439 * no destination overlaps.
441 if (!list_empty(&image->control_pages)) {
446 memset(&buf, 0, sizeof(struct kexec_buf));
449 kbuf->buffer = buffer;
452 kbuf->memsz = ALIGN(memsz, PAGE_SIZE);
453 kbuf->buf_align = max(buf_align, PAGE_SIZE);
454 kbuf->buf_min = buf_min;
455 kbuf->buf_max = buf_max;
456 kbuf->top_down = top_down;
458 /* Walk the RAM ranges and allocate a suitable range for the buffer */
459 if (image->type == KEXEC_TYPE_CRASH)
460 ret = walk_iomem_res_desc(crashk_res.desc,
461 IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY,
462 crashk_res.start, crashk_res.end, kbuf,
463 locate_mem_hole_callback);
465 ret = walk_system_ram_res(0, -1, kbuf,
466 locate_mem_hole_callback);
468 /* A suitable memory range could not be found for buffer */
469 return -EADDRNOTAVAIL;
472 /* Found a suitable memory range */
473 ksegment = &image->segment[image->nr_segments];
474 ksegment->kbuf = kbuf->buffer;
475 ksegment->bufsz = kbuf->bufsz;
476 ksegment->mem = kbuf->mem;
477 ksegment->memsz = kbuf->memsz;
478 image->nr_segments++;
479 *load_addr = ksegment->mem;
483 /* Calculate and store the digest of segments */
484 static int kexec_calculate_store_digests(struct kimage *image)
486 struct crypto_shash *tfm;
487 struct shash_desc *desc;
488 int ret = 0, i, j, zero_buf_sz, sha_region_sz;
489 size_t desc_size, nullsz;
492 struct kexec_sha_region *sha_regions;
493 struct purgatory_info *pi = &image->purgatory_info;
495 zero_buf = __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT);
496 zero_buf_sz = PAGE_SIZE;
498 tfm = crypto_alloc_shash("sha256", 0, 0);
504 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
505 desc = kzalloc(desc_size, GFP_KERNEL);
511 sha_region_sz = KEXEC_SEGMENT_MAX * sizeof(struct kexec_sha_region);
512 sha_regions = vzalloc(sha_region_sz);
521 ret = crypto_shash_init(desc);
523 goto out_free_sha_regions;
525 digest = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
528 goto out_free_sha_regions;
531 for (j = i = 0; i < image->nr_segments; i++) {
532 struct kexec_segment *ksegment;
534 ksegment = &image->segment[i];
536 * Skip purgatory as it will be modified once we put digest
539 if (ksegment->kbuf == pi->purgatory_buf)
542 ret = crypto_shash_update(desc, ksegment->kbuf,
548 * Assume rest of the buffer is filled with zero and
549 * update digest accordingly.
551 nullsz = ksegment->memsz - ksegment->bufsz;
553 unsigned long bytes = nullsz;
555 if (bytes > zero_buf_sz)
557 ret = crypto_shash_update(desc, zero_buf, bytes);
566 sha_regions[j].start = ksegment->mem;
567 sha_regions[j].len = ksegment->memsz;
572 ret = crypto_shash_final(desc, digest);
574 goto out_free_digest;
575 ret = kexec_purgatory_get_set_symbol(image, "sha_regions",
576 sha_regions, sha_region_sz, 0);
578 goto out_free_digest;
580 ret = kexec_purgatory_get_set_symbol(image, "sha256_digest",
581 digest, SHA256_DIGEST_SIZE, 0);
583 goto out_free_digest;
588 out_free_sha_regions:
598 /* Actually load purgatory. Lot of code taken from kexec-tools */
599 static int __kexec_load_purgatory(struct kimage *image, unsigned long min,
600 unsigned long max, int top_down)
602 struct purgatory_info *pi = &image->purgatory_info;
603 unsigned long align, buf_align, bss_align, buf_sz, bss_sz, bss_pad;
604 unsigned long memsz, entry, load_addr, curr_load_addr, bss_addr, offset;
605 unsigned char *buf_addr, *src;
606 int i, ret = 0, entry_sidx = -1;
607 const Elf_Shdr *sechdrs_c;
608 Elf_Shdr *sechdrs = NULL;
609 void *purgatory_buf = NULL;
612 * sechdrs_c points to section headers in purgatory and are read
613 * only. No modifications allowed.
615 sechdrs_c = (void *)pi->ehdr + pi->ehdr->e_shoff;
618 * We can not modify sechdrs_c[] and its fields. It is read only.
619 * Copy it over to a local copy where one can store some temporary
620 * data and free it at the end. We need to modify ->sh_addr and
621 * ->sh_offset fields to keep track of permanent and temporary
622 * locations of sections.
624 sechdrs = vzalloc(pi->ehdr->e_shnum * sizeof(Elf_Shdr));
628 memcpy(sechdrs, sechdrs_c, pi->ehdr->e_shnum * sizeof(Elf_Shdr));
631 * We seem to have multiple copies of sections. First copy is which
632 * is embedded in kernel in read only section. Some of these sections
633 * will be copied to a temporary buffer and relocated. And these
634 * sections will finally be copied to their final destination at
637 * Use ->sh_offset to reflect section address in memory. It will
638 * point to original read only copy if section is not allocatable.
639 * Otherwise it will point to temporary copy which will be relocated.
641 * Use ->sh_addr to contain final address of the section where it
642 * will go during execution time.
644 for (i = 0; i < pi->ehdr->e_shnum; i++) {
645 if (sechdrs[i].sh_type == SHT_NOBITS)
648 sechdrs[i].sh_offset = (unsigned long)pi->ehdr +
649 sechdrs[i].sh_offset;
653 * Identify entry point section and make entry relative to section
656 entry = pi->ehdr->e_entry;
657 for (i = 0; i < pi->ehdr->e_shnum; i++) {
658 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
661 if (!(sechdrs[i].sh_flags & SHF_EXECINSTR))
664 /* Make entry section relative */
665 if (sechdrs[i].sh_addr <= pi->ehdr->e_entry &&
666 ((sechdrs[i].sh_addr + sechdrs[i].sh_size) >
667 pi->ehdr->e_entry)) {
669 entry -= sechdrs[i].sh_addr;
674 /* Determine how much memory is needed to load relocatable object. */
680 for (i = 0; i < pi->ehdr->e_shnum; i++) {
681 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
684 align = sechdrs[i].sh_addralign;
685 if (sechdrs[i].sh_type != SHT_NOBITS) {
686 if (buf_align < align)
688 buf_sz = ALIGN(buf_sz, align);
689 buf_sz += sechdrs[i].sh_size;
692 if (bss_align < align)
694 bss_sz = ALIGN(bss_sz, align);
695 bss_sz += sechdrs[i].sh_size;
699 /* Determine the bss padding required to align bss properly */
701 if (buf_sz & (bss_align - 1))
702 bss_pad = bss_align - (buf_sz & (bss_align - 1));
704 memsz = buf_sz + bss_pad + bss_sz;
706 /* Allocate buffer for purgatory */
707 purgatory_buf = vzalloc(buf_sz);
708 if (!purgatory_buf) {
713 if (buf_align < bss_align)
714 buf_align = bss_align;
716 /* Add buffer to segment list */
717 ret = kexec_add_buffer(image, purgatory_buf, buf_sz, memsz,
718 buf_align, min, max, top_down,
719 &pi->purgatory_load_addr);
723 /* Load SHF_ALLOC sections */
724 buf_addr = purgatory_buf;
725 load_addr = curr_load_addr = pi->purgatory_load_addr;
726 bss_addr = load_addr + buf_sz + bss_pad;
728 for (i = 0; i < pi->ehdr->e_shnum; i++) {
729 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
732 align = sechdrs[i].sh_addralign;
733 if (sechdrs[i].sh_type != SHT_NOBITS) {
734 curr_load_addr = ALIGN(curr_load_addr, align);
735 offset = curr_load_addr - load_addr;
736 /* We already modifed ->sh_offset to keep src addr */
737 src = (char *) sechdrs[i].sh_offset;
738 memcpy(buf_addr + offset, src, sechdrs[i].sh_size);
740 /* Store load address and source address of section */
741 sechdrs[i].sh_addr = curr_load_addr;
744 * This section got copied to temporary buffer. Update
745 * ->sh_offset accordingly.
747 sechdrs[i].sh_offset = (unsigned long)(buf_addr + offset);
749 /* Advance to the next address */
750 curr_load_addr += sechdrs[i].sh_size;
752 bss_addr = ALIGN(bss_addr, align);
753 sechdrs[i].sh_addr = bss_addr;
754 bss_addr += sechdrs[i].sh_size;
758 /* Update entry point based on load address of text section */
760 entry += sechdrs[entry_sidx].sh_addr;
762 /* Make kernel jump to purgatory after shutdown */
763 image->start = entry;
765 /* Used later to get/set symbol values */
766 pi->sechdrs = sechdrs;
769 * Used later to identify which section is purgatory and skip it
772 pi->purgatory_buf = purgatory_buf;
776 vfree(purgatory_buf);
780 static int kexec_apply_relocations(struct kimage *image)
783 struct purgatory_info *pi = &image->purgatory_info;
784 Elf_Shdr *sechdrs = pi->sechdrs;
786 /* Apply relocations */
787 for (i = 0; i < pi->ehdr->e_shnum; i++) {
788 Elf_Shdr *section, *symtab;
790 if (sechdrs[i].sh_type != SHT_RELA &&
791 sechdrs[i].sh_type != SHT_REL)
795 * For section of type SHT_RELA/SHT_REL,
796 * ->sh_link contains section header index of associated
797 * symbol table. And ->sh_info contains section header
798 * index of section to which relocations apply.
800 if (sechdrs[i].sh_info >= pi->ehdr->e_shnum ||
801 sechdrs[i].sh_link >= pi->ehdr->e_shnum)
804 section = &sechdrs[sechdrs[i].sh_info];
805 symtab = &sechdrs[sechdrs[i].sh_link];
807 if (!(section->sh_flags & SHF_ALLOC))
811 * symtab->sh_link contain section header index of associated
814 if (symtab->sh_link >= pi->ehdr->e_shnum)
815 /* Invalid section number? */
819 * Respective architecture needs to provide support for applying
820 * relocations of type SHT_RELA/SHT_REL.
822 if (sechdrs[i].sh_type == SHT_RELA)
823 ret = arch_kexec_apply_relocations_add(pi->ehdr,
825 else if (sechdrs[i].sh_type == SHT_REL)
826 ret = arch_kexec_apply_relocations(pi->ehdr,
835 /* Load relocatable purgatory object and relocate it appropriately */
836 int kexec_load_purgatory(struct kimage *image, unsigned long min,
837 unsigned long max, int top_down,
838 unsigned long *load_addr)
840 struct purgatory_info *pi = &image->purgatory_info;
843 if (kexec_purgatory_size <= 0)
846 if (kexec_purgatory_size < sizeof(Elf_Ehdr))
849 pi->ehdr = (Elf_Ehdr *)kexec_purgatory;
851 if (memcmp(pi->ehdr->e_ident, ELFMAG, SELFMAG) != 0
852 || pi->ehdr->e_type != ET_REL
853 || !elf_check_arch(pi->ehdr)
854 || pi->ehdr->e_shentsize != sizeof(Elf_Shdr))
857 if (pi->ehdr->e_shoff >= kexec_purgatory_size
858 || (pi->ehdr->e_shnum * sizeof(Elf_Shdr) >
859 kexec_purgatory_size - pi->ehdr->e_shoff))
862 ret = __kexec_load_purgatory(image, min, max, top_down);
866 ret = kexec_apply_relocations(image);
870 *load_addr = pi->purgatory_load_addr;
876 vfree(pi->purgatory_buf);
877 pi->purgatory_buf = NULL;
881 static Elf_Sym *kexec_purgatory_find_symbol(struct purgatory_info *pi,
890 if (!pi->sechdrs || !pi->ehdr)
893 sechdrs = pi->sechdrs;
896 for (i = 0; i < ehdr->e_shnum; i++) {
897 if (sechdrs[i].sh_type != SHT_SYMTAB)
900 if (sechdrs[i].sh_link >= ehdr->e_shnum)
901 /* Invalid strtab section number */
903 strtab = (char *)sechdrs[sechdrs[i].sh_link].sh_offset;
904 syms = (Elf_Sym *)sechdrs[i].sh_offset;
906 /* Go through symbols for a match */
907 for (k = 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) {
908 if (ELF_ST_BIND(syms[k].st_info) != STB_GLOBAL)
911 if (strcmp(strtab + syms[k].st_name, name) != 0)
914 if (syms[k].st_shndx == SHN_UNDEF ||
915 syms[k].st_shndx >= ehdr->e_shnum) {
916 pr_debug("Symbol: %s has bad section index %d.\n",
917 name, syms[k].st_shndx);
921 /* Found the symbol we are looking for */
929 void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name)
931 struct purgatory_info *pi = &image->purgatory_info;
935 sym = kexec_purgatory_find_symbol(pi, name);
937 return ERR_PTR(-EINVAL);
939 sechdr = &pi->sechdrs[sym->st_shndx];
942 * Returns the address where symbol will finally be loaded after
943 * kexec_load_segment()
945 return (void *)(sechdr->sh_addr + sym->st_value);
949 * Get or set value of a symbol. If "get_value" is true, symbol value is
950 * returned in buf otherwise symbol value is set based on value in buf.
952 int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
953 void *buf, unsigned int size, bool get_value)
957 struct purgatory_info *pi = &image->purgatory_info;
960 sym = kexec_purgatory_find_symbol(pi, name);
964 if (sym->st_size != size) {
965 pr_err("symbol %s size mismatch: expected %lu actual %u\n",
966 name, (unsigned long)sym->st_size, size);
970 sechdrs = pi->sechdrs;
972 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
973 pr_err("symbol %s is in a bss section. Cannot %s\n", name,
974 get_value ? "get" : "set");
978 sym_buf = (unsigned char *)sechdrs[sym->st_shndx].sh_offset +
982 memcpy((void *)buf, sym_buf, size);
984 memcpy((void *)sym_buf, buf, size);