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>
21 #include <crypto/hash.h>
22 #include <crypto/sha.h>
23 #include <linux/syscalls.h>
24 #include <linux/vmalloc.h>
25 #include "kexec_internal.h"
28 * Declare these symbols weak so that if architecture provides a purgatory,
29 * these will be overridden.
31 char __weak kexec_purgatory
[0];
32 size_t __weak kexec_purgatory_size
= 0;
34 static int kexec_calculate_store_digests(struct kimage
*image
);
36 static int copy_file_from_fd(int fd
, void **buf
, unsigned long *buf_len
)
38 struct fd f
= fdget(fd
);
47 ret
= vfs_getattr(&f
.file
->f_path
, &stat
);
51 if (stat
.size
> INT_MAX
) {
56 /* Don't hand 0 to vmalloc, it whines. */
62 *buf
= vmalloc(stat
.size
);
69 while (pos
< stat
.size
) {
70 bytes
= kernel_read(f
.file
, pos
, (char *)(*buf
) + pos
,
83 if (pos
!= stat
.size
) {
95 /* Architectures can provide this probe function */
96 int __weak
arch_kexec_kernel_image_probe(struct kimage
*image
, void *buf
,
97 unsigned long buf_len
)
102 void * __weak
arch_kexec_kernel_image_load(struct kimage
*image
)
104 return ERR_PTR(-ENOEXEC
);
107 int __weak
arch_kimage_file_post_load_cleanup(struct kimage
*image
)
112 #ifdef CONFIG_KEXEC_VERIFY_SIG
113 int __weak
arch_kexec_kernel_verify_sig(struct kimage
*image
, void *buf
,
114 unsigned long buf_len
)
116 return -EKEYREJECTED
;
120 /* Apply relocations of type RELA */
122 arch_kexec_apply_relocations_add(const Elf_Ehdr
*ehdr
, Elf_Shdr
*sechdrs
,
125 pr_err("RELA relocation unsupported.\n");
129 /* Apply relocations of type REL */
131 arch_kexec_apply_relocations(const Elf_Ehdr
*ehdr
, Elf_Shdr
*sechdrs
,
134 pr_err("REL relocation unsupported.\n");
139 * Free up memory used by kernel, initrd, and command line. This is temporary
140 * memory allocation which is not needed any more after these buffers have
141 * been loaded into separate segments and have been copied elsewhere.
143 void kimage_file_post_load_cleanup(struct kimage
*image
)
145 struct purgatory_info
*pi
= &image
->purgatory_info
;
147 vfree(image
->kernel_buf
);
148 image
->kernel_buf
= NULL
;
150 vfree(image
->initrd_buf
);
151 image
->initrd_buf
= NULL
;
153 kfree(image
->cmdline_buf
);
154 image
->cmdline_buf
= NULL
;
156 vfree(pi
->purgatory_buf
);
157 pi
->purgatory_buf
= NULL
;
162 /* See if architecture has anything to cleanup post load */
163 arch_kimage_file_post_load_cleanup(image
);
166 * Above call should have called into bootloader to free up
167 * any data stored in kimage->image_loader_data. It should
168 * be ok now to free it up.
170 kfree(image
->image_loader_data
);
171 image
->image_loader_data
= NULL
;
175 * In file mode list of segments is prepared by kernel. Copy relevant
176 * data from user space, do error checking, prepare segment list
179 kimage_file_prepare_segments(struct kimage
*image
, int kernel_fd
, int initrd_fd
,
180 const char __user
*cmdline_ptr
,
181 unsigned long cmdline_len
, unsigned flags
)
186 ret
= copy_file_from_fd(kernel_fd
, &image
->kernel_buf
,
187 &image
->kernel_buf_len
);
191 /* Call arch image probe handlers */
192 ret
= arch_kexec_kernel_image_probe(image
, image
->kernel_buf
,
193 image
->kernel_buf_len
);
198 #ifdef CONFIG_KEXEC_VERIFY_SIG
199 ret
= arch_kexec_kernel_verify_sig(image
, image
->kernel_buf
,
200 image
->kernel_buf_len
);
202 pr_debug("kernel signature verification failed.\n");
205 pr_debug("kernel signature verification successful.\n");
207 /* It is possible that there no initramfs is being loaded */
208 if (!(flags
& KEXEC_FILE_NO_INITRAMFS
)) {
209 ret
= copy_file_from_fd(initrd_fd
, &image
->initrd_buf
,
210 &image
->initrd_buf_len
);
216 image
->cmdline_buf
= kzalloc(cmdline_len
, GFP_KERNEL
);
217 if (!image
->cmdline_buf
) {
222 ret
= copy_from_user(image
->cmdline_buf
, cmdline_ptr
,
229 image
->cmdline_buf_len
= cmdline_len
;
231 /* command line should be a string with last byte null */
232 if (image
->cmdline_buf
[cmdline_len
- 1] != '\0') {
238 /* Call arch image load handlers */
239 ldata
= arch_kexec_kernel_image_load(image
);
242 ret
= PTR_ERR(ldata
);
246 image
->image_loader_data
= ldata
;
248 /* In case of error, free up all allocated memory in this function */
250 kimage_file_post_load_cleanup(image
);
255 kimage_file_alloc_init(struct kimage
**rimage
, int kernel_fd
,
256 int initrd_fd
, const char __user
*cmdline_ptr
,
257 unsigned long cmdline_len
, unsigned long flags
)
260 struct kimage
*image
;
261 bool kexec_on_panic
= flags
& KEXEC_FILE_ON_CRASH
;
263 image
= do_kimage_alloc_init();
267 image
->file_mode
= 1;
269 if (kexec_on_panic
) {
270 /* Enable special crash kernel control page alloc policy. */
271 image
->control_page
= crashk_res
.start
;
272 image
->type
= KEXEC_TYPE_CRASH
;
275 ret
= kimage_file_prepare_segments(image
, kernel_fd
, initrd_fd
,
276 cmdline_ptr
, cmdline_len
, flags
);
280 ret
= sanity_check_segment_list(image
);
282 goto out_free_post_load_bufs
;
285 image
->control_code_page
= kimage_alloc_control_pages(image
,
286 get_order(KEXEC_CONTROL_PAGE_SIZE
));
287 if (!image
->control_code_page
) {
288 pr_err("Could not allocate control_code_buffer\n");
289 goto out_free_post_load_bufs
;
292 if (!kexec_on_panic
) {
293 image
->swap_page
= kimage_alloc_control_pages(image
, 0);
294 if (!image
->swap_page
) {
295 pr_err("Could not allocate swap buffer\n");
296 goto out_free_control_pages
;
302 out_free_control_pages
:
303 kimage_free_page_list(&image
->control_pages
);
304 out_free_post_load_bufs
:
305 kimage_file_post_load_cleanup(image
);
311 SYSCALL_DEFINE5(kexec_file_load
, int, kernel_fd
, int, initrd_fd
,
312 unsigned long, cmdline_len
, const char __user
*, cmdline_ptr
,
313 unsigned long, flags
)
316 struct kimage
**dest_image
, *image
;
318 /* We only trust the superuser with rebooting the system. */
319 if (!capable(CAP_SYS_BOOT
) || kexec_load_disabled
)
322 /* Make sure we have a legal set of flags */
323 if (flags
!= (flags
& KEXEC_FILE_FLAGS
))
328 if (!mutex_trylock(&kexec_mutex
))
331 dest_image
= &kexec_image
;
332 if (flags
& KEXEC_FILE_ON_CRASH
)
333 dest_image
= &kexec_crash_image
;
335 if (flags
& KEXEC_FILE_UNLOAD
)
339 * In case of crash, new kernel gets loaded in reserved region. It is
340 * same memory where old crash kernel might be loaded. Free any
341 * current crash dump kernel before we corrupt it.
343 if (flags
& KEXEC_FILE_ON_CRASH
)
344 kimage_free(xchg(&kexec_crash_image
, NULL
));
346 ret
= kimage_file_alloc_init(&image
, kernel_fd
, initrd_fd
, cmdline_ptr
,
351 ret
= machine_kexec_prepare(image
);
355 ret
= kexec_calculate_store_digests(image
);
359 for (i
= 0; i
< image
->nr_segments
; i
++) {
360 struct kexec_segment
*ksegment
;
362 ksegment
= &image
->segment
[i
];
363 pr_debug("Loading segment %d: buf=0x%p bufsz=0x%zx mem=0x%lx memsz=0x%zx\n",
364 i
, ksegment
->buf
, ksegment
->bufsz
, ksegment
->mem
,
367 ret
= kimage_load_segment(image
, &image
->segment
[i
]);
372 kimage_terminate(image
);
375 * Free up any temporary buffers allocated which are not needed
376 * after image has been loaded
378 kimage_file_post_load_cleanup(image
);
380 image
= xchg(dest_image
, image
);
382 mutex_unlock(&kexec_mutex
);
387 static int locate_mem_hole_top_down(unsigned long start
, unsigned long end
,
388 struct kexec_buf
*kbuf
)
390 struct kimage
*image
= kbuf
->image
;
391 unsigned long temp_start
, temp_end
;
393 temp_end
= min(end
, kbuf
->buf_max
);
394 temp_start
= temp_end
- kbuf
->memsz
;
397 /* align down start */
398 temp_start
= temp_start
& (~(kbuf
->buf_align
- 1));
400 if (temp_start
< start
|| temp_start
< kbuf
->buf_min
)
403 temp_end
= temp_start
+ kbuf
->memsz
- 1;
406 * Make sure this does not conflict with any of existing
409 if (kimage_is_destination_range(image
, temp_start
, temp_end
)) {
410 temp_start
= temp_start
- PAGE_SIZE
;
414 /* We found a suitable memory range */
418 /* If we are here, we found a suitable memory range */
419 kbuf
->mem
= temp_start
;
421 /* Success, stop navigating through remaining System RAM ranges */
425 static int locate_mem_hole_bottom_up(unsigned long start
, unsigned long end
,
426 struct kexec_buf
*kbuf
)
428 struct kimage
*image
= kbuf
->image
;
429 unsigned long temp_start
, temp_end
;
431 temp_start
= max(start
, kbuf
->buf_min
);
434 temp_start
= ALIGN(temp_start
, kbuf
->buf_align
);
435 temp_end
= temp_start
+ kbuf
->memsz
- 1;
437 if (temp_end
> end
|| temp_end
> kbuf
->buf_max
)
440 * Make sure this does not conflict with any of existing
443 if (kimage_is_destination_range(image
, temp_start
, temp_end
)) {
444 temp_start
= temp_start
+ PAGE_SIZE
;
448 /* We found a suitable memory range */
452 /* If we are here, we found a suitable memory range */
453 kbuf
->mem
= temp_start
;
455 /* Success, stop navigating through remaining System RAM ranges */
459 static int locate_mem_hole_callback(u64 start
, u64 end
, void *arg
)
461 struct kexec_buf
*kbuf
= (struct kexec_buf
*)arg
;
462 unsigned long sz
= end
- start
+ 1;
464 /* Returning 0 will take to next memory range */
465 if (sz
< kbuf
->memsz
)
468 if (end
< kbuf
->buf_min
|| start
> kbuf
->buf_max
)
472 * Allocate memory top down with-in ram range. Otherwise bottom up
476 return locate_mem_hole_top_down(start
, end
, kbuf
);
477 return locate_mem_hole_bottom_up(start
, end
, kbuf
);
481 * Helper function for placing a buffer in a kexec segment. This assumes
482 * that kexec_mutex is held.
484 int kexec_add_buffer(struct kimage
*image
, char *buffer
, unsigned long bufsz
,
485 unsigned long memsz
, unsigned long buf_align
,
486 unsigned long buf_min
, unsigned long buf_max
,
487 bool top_down
, unsigned long *load_addr
)
490 struct kexec_segment
*ksegment
;
491 struct kexec_buf buf
, *kbuf
;
494 /* Currently adding segment this way is allowed only in file mode */
495 if (!image
->file_mode
)
498 if (image
->nr_segments
>= KEXEC_SEGMENT_MAX
)
502 * Make sure we are not trying to add buffer after allocating
503 * control pages. All segments need to be placed first before
504 * any control pages are allocated. As control page allocation
505 * logic goes through list of segments to make sure there are
506 * no destination overlaps.
508 if (!list_empty(&image
->control_pages
)) {
513 memset(&buf
, 0, sizeof(struct kexec_buf
));
516 kbuf
->buffer
= buffer
;
519 kbuf
->memsz
= ALIGN(memsz
, PAGE_SIZE
);
520 kbuf
->buf_align
= max(buf_align
, PAGE_SIZE
);
521 kbuf
->buf_min
= buf_min
;
522 kbuf
->buf_max
= buf_max
;
523 kbuf
->top_down
= top_down
;
525 /* Walk the RAM ranges and allocate a suitable range for the buffer */
526 if (image
->type
== KEXEC_TYPE_CRASH
)
527 ret
= walk_iomem_res("Crash kernel",
528 IORESOURCE_MEM
| IORESOURCE_BUSY
,
529 crashk_res
.start
, crashk_res
.end
, kbuf
,
530 locate_mem_hole_callback
);
532 ret
= walk_system_ram_res(0, -1, kbuf
,
533 locate_mem_hole_callback
);
535 /* A suitable memory range could not be found for buffer */
536 return -EADDRNOTAVAIL
;
539 /* Found a suitable memory range */
540 ksegment
= &image
->segment
[image
->nr_segments
];
541 ksegment
->kbuf
= kbuf
->buffer
;
542 ksegment
->bufsz
= kbuf
->bufsz
;
543 ksegment
->mem
= kbuf
->mem
;
544 ksegment
->memsz
= kbuf
->memsz
;
545 image
->nr_segments
++;
546 *load_addr
= ksegment
->mem
;
550 /* Calculate and store the digest of segments */
551 static int kexec_calculate_store_digests(struct kimage
*image
)
553 struct crypto_shash
*tfm
;
554 struct shash_desc
*desc
;
555 int ret
= 0, i
, j
, zero_buf_sz
, sha_region_sz
;
556 size_t desc_size
, nullsz
;
559 struct kexec_sha_region
*sha_regions
;
560 struct purgatory_info
*pi
= &image
->purgatory_info
;
562 zero_buf
= __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT
);
563 zero_buf_sz
= PAGE_SIZE
;
565 tfm
= crypto_alloc_shash("sha256", 0, 0);
571 desc_size
= crypto_shash_descsize(tfm
) + sizeof(*desc
);
572 desc
= kzalloc(desc_size
, GFP_KERNEL
);
578 sha_region_sz
= KEXEC_SEGMENT_MAX
* sizeof(struct kexec_sha_region
);
579 sha_regions
= vzalloc(sha_region_sz
);
586 ret
= crypto_shash_init(desc
);
588 goto out_free_sha_regions
;
590 digest
= kzalloc(SHA256_DIGEST_SIZE
, GFP_KERNEL
);
593 goto out_free_sha_regions
;
596 for (j
= i
= 0; i
< image
->nr_segments
; i
++) {
597 struct kexec_segment
*ksegment
;
599 ksegment
= &image
->segment
[i
];
601 * Skip purgatory as it will be modified once we put digest
604 if (ksegment
->kbuf
== pi
->purgatory_buf
)
607 ret
= crypto_shash_update(desc
, ksegment
->kbuf
,
613 * Assume rest of the buffer is filled with zero and
614 * update digest accordingly.
616 nullsz
= ksegment
->memsz
- ksegment
->bufsz
;
618 unsigned long bytes
= nullsz
;
620 if (bytes
> zero_buf_sz
)
622 ret
= crypto_shash_update(desc
, zero_buf
, bytes
);
631 sha_regions
[j
].start
= ksegment
->mem
;
632 sha_regions
[j
].len
= ksegment
->memsz
;
637 ret
= crypto_shash_final(desc
, digest
);
639 goto out_free_digest
;
640 ret
= kexec_purgatory_get_set_symbol(image
, "sha_regions",
641 sha_regions
, sha_region_sz
, 0);
643 goto out_free_digest
;
645 ret
= kexec_purgatory_get_set_symbol(image
, "sha256_digest",
646 digest
, SHA256_DIGEST_SIZE
, 0);
648 goto out_free_digest
;
653 out_free_sha_regions
:
663 /* Actually load purgatory. Lot of code taken from kexec-tools */
664 static int __kexec_load_purgatory(struct kimage
*image
, unsigned long min
,
665 unsigned long max
, int top_down
)
667 struct purgatory_info
*pi
= &image
->purgatory_info
;
668 unsigned long align
, buf_align
, bss_align
, buf_sz
, bss_sz
, bss_pad
;
669 unsigned long memsz
, entry
, load_addr
, curr_load_addr
, bss_addr
, offset
;
670 unsigned char *buf_addr
, *src
;
671 int i
, ret
= 0, entry_sidx
= -1;
672 const Elf_Shdr
*sechdrs_c
;
673 Elf_Shdr
*sechdrs
= NULL
;
674 void *purgatory_buf
= NULL
;
677 * sechdrs_c points to section headers in purgatory and are read
678 * only. No modifications allowed.
680 sechdrs_c
= (void *)pi
->ehdr
+ pi
->ehdr
->e_shoff
;
683 * We can not modify sechdrs_c[] and its fields. It is read only.
684 * Copy it over to a local copy where one can store some temporary
685 * data and free it at the end. We need to modify ->sh_addr and
686 * ->sh_offset fields to keep track of permanent and temporary
687 * locations of sections.
689 sechdrs
= vzalloc(pi
->ehdr
->e_shnum
* sizeof(Elf_Shdr
));
693 memcpy(sechdrs
, sechdrs_c
, pi
->ehdr
->e_shnum
* sizeof(Elf_Shdr
));
696 * We seem to have multiple copies of sections. First copy is which
697 * is embedded in kernel in read only section. Some of these sections
698 * will be copied to a temporary buffer and relocated. And these
699 * sections will finally be copied to their final destination at
702 * Use ->sh_offset to reflect section address in memory. It will
703 * point to original read only copy if section is not allocatable.
704 * Otherwise it will point to temporary copy which will be relocated.
706 * Use ->sh_addr to contain final address of the section where it
707 * will go during execution time.
709 for (i
= 0; i
< pi
->ehdr
->e_shnum
; i
++) {
710 if (sechdrs
[i
].sh_type
== SHT_NOBITS
)
713 sechdrs
[i
].sh_offset
= (unsigned long)pi
->ehdr
+
714 sechdrs
[i
].sh_offset
;
718 * Identify entry point section and make entry relative to section
721 entry
= pi
->ehdr
->e_entry
;
722 for (i
= 0; i
< pi
->ehdr
->e_shnum
; i
++) {
723 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
726 if (!(sechdrs
[i
].sh_flags
& SHF_EXECINSTR
))
729 /* Make entry section relative */
730 if (sechdrs
[i
].sh_addr
<= pi
->ehdr
->e_entry
&&
731 ((sechdrs
[i
].sh_addr
+ sechdrs
[i
].sh_size
) >
732 pi
->ehdr
->e_entry
)) {
734 entry
-= sechdrs
[i
].sh_addr
;
739 /* Determine how much memory is needed to load relocatable object. */
745 for (i
= 0; i
< pi
->ehdr
->e_shnum
; i
++) {
746 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
749 align
= sechdrs
[i
].sh_addralign
;
750 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
) {
751 if (buf_align
< align
)
753 buf_sz
= ALIGN(buf_sz
, align
);
754 buf_sz
+= sechdrs
[i
].sh_size
;
757 if (bss_align
< align
)
759 bss_sz
= ALIGN(bss_sz
, align
);
760 bss_sz
+= sechdrs
[i
].sh_size
;
764 /* Determine the bss padding required to align bss properly */
766 if (buf_sz
& (bss_align
- 1))
767 bss_pad
= bss_align
- (buf_sz
& (bss_align
- 1));
769 memsz
= buf_sz
+ bss_pad
+ bss_sz
;
771 /* Allocate buffer for purgatory */
772 purgatory_buf
= vzalloc(buf_sz
);
773 if (!purgatory_buf
) {
778 if (buf_align
< bss_align
)
779 buf_align
= bss_align
;
781 /* Add buffer to segment list */
782 ret
= kexec_add_buffer(image
, purgatory_buf
, buf_sz
, memsz
,
783 buf_align
, min
, max
, top_down
,
784 &pi
->purgatory_load_addr
);
788 /* Load SHF_ALLOC sections */
789 buf_addr
= purgatory_buf
;
790 load_addr
= curr_load_addr
= pi
->purgatory_load_addr
;
791 bss_addr
= load_addr
+ buf_sz
+ bss_pad
;
793 for (i
= 0; i
< pi
->ehdr
->e_shnum
; i
++) {
794 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
797 align
= sechdrs
[i
].sh_addralign
;
798 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
) {
799 curr_load_addr
= ALIGN(curr_load_addr
, align
);
800 offset
= curr_load_addr
- load_addr
;
801 /* We already modifed ->sh_offset to keep src addr */
802 src
= (char *) sechdrs
[i
].sh_offset
;
803 memcpy(buf_addr
+ offset
, src
, sechdrs
[i
].sh_size
);
805 /* Store load address and source address of section */
806 sechdrs
[i
].sh_addr
= curr_load_addr
;
809 * This section got copied to temporary buffer. Update
810 * ->sh_offset accordingly.
812 sechdrs
[i
].sh_offset
= (unsigned long)(buf_addr
+ offset
);
814 /* Advance to the next address */
815 curr_load_addr
+= sechdrs
[i
].sh_size
;
817 bss_addr
= ALIGN(bss_addr
, align
);
818 sechdrs
[i
].sh_addr
= bss_addr
;
819 bss_addr
+= sechdrs
[i
].sh_size
;
823 /* Update entry point based on load address of text section */
825 entry
+= sechdrs
[entry_sidx
].sh_addr
;
827 /* Make kernel jump to purgatory after shutdown */
828 image
->start
= entry
;
830 /* Used later to get/set symbol values */
831 pi
->sechdrs
= sechdrs
;
834 * Used later to identify which section is purgatory and skip it
837 pi
->purgatory_buf
= purgatory_buf
;
841 vfree(purgatory_buf
);
845 static int kexec_apply_relocations(struct kimage
*image
)
848 struct purgatory_info
*pi
= &image
->purgatory_info
;
849 Elf_Shdr
*sechdrs
= pi
->sechdrs
;
851 /* Apply relocations */
852 for (i
= 0; i
< pi
->ehdr
->e_shnum
; i
++) {
853 Elf_Shdr
*section
, *symtab
;
855 if (sechdrs
[i
].sh_type
!= SHT_RELA
&&
856 sechdrs
[i
].sh_type
!= SHT_REL
)
860 * For section of type SHT_RELA/SHT_REL,
861 * ->sh_link contains section header index of associated
862 * symbol table. And ->sh_info contains section header
863 * index of section to which relocations apply.
865 if (sechdrs
[i
].sh_info
>= pi
->ehdr
->e_shnum
||
866 sechdrs
[i
].sh_link
>= pi
->ehdr
->e_shnum
)
869 section
= &sechdrs
[sechdrs
[i
].sh_info
];
870 symtab
= &sechdrs
[sechdrs
[i
].sh_link
];
872 if (!(section
->sh_flags
& SHF_ALLOC
))
876 * symtab->sh_link contain section header index of associated
879 if (symtab
->sh_link
>= pi
->ehdr
->e_shnum
)
880 /* Invalid section number? */
884 * Respective architecture needs to provide support for applying
885 * relocations of type SHT_RELA/SHT_REL.
887 if (sechdrs
[i
].sh_type
== SHT_RELA
)
888 ret
= arch_kexec_apply_relocations_add(pi
->ehdr
,
890 else if (sechdrs
[i
].sh_type
== SHT_REL
)
891 ret
= arch_kexec_apply_relocations(pi
->ehdr
,
900 /* Load relocatable purgatory object and relocate it appropriately */
901 int kexec_load_purgatory(struct kimage
*image
, unsigned long min
,
902 unsigned long max
, int top_down
,
903 unsigned long *load_addr
)
905 struct purgatory_info
*pi
= &image
->purgatory_info
;
908 if (kexec_purgatory_size
<= 0)
911 if (kexec_purgatory_size
< sizeof(Elf_Ehdr
))
914 pi
->ehdr
= (Elf_Ehdr
*)kexec_purgatory
;
916 if (memcmp(pi
->ehdr
->e_ident
, ELFMAG
, SELFMAG
) != 0
917 || pi
->ehdr
->e_type
!= ET_REL
918 || !elf_check_arch(pi
->ehdr
)
919 || pi
->ehdr
->e_shentsize
!= sizeof(Elf_Shdr
))
922 if (pi
->ehdr
->e_shoff
>= kexec_purgatory_size
923 || (pi
->ehdr
->e_shnum
* sizeof(Elf_Shdr
) >
924 kexec_purgatory_size
- pi
->ehdr
->e_shoff
))
927 ret
= __kexec_load_purgatory(image
, min
, max
, top_down
);
931 ret
= kexec_apply_relocations(image
);
935 *load_addr
= pi
->purgatory_load_addr
;
939 vfree(pi
->purgatory_buf
);
943 static Elf_Sym
*kexec_purgatory_find_symbol(struct purgatory_info
*pi
,
952 if (!pi
->sechdrs
|| !pi
->ehdr
)
955 sechdrs
= pi
->sechdrs
;
958 for (i
= 0; i
< ehdr
->e_shnum
; i
++) {
959 if (sechdrs
[i
].sh_type
!= SHT_SYMTAB
)
962 if (sechdrs
[i
].sh_link
>= ehdr
->e_shnum
)
963 /* Invalid strtab section number */
965 strtab
= (char *)sechdrs
[sechdrs
[i
].sh_link
].sh_offset
;
966 syms
= (Elf_Sym
*)sechdrs
[i
].sh_offset
;
968 /* Go through symbols for a match */
969 for (k
= 0; k
< sechdrs
[i
].sh_size
/sizeof(Elf_Sym
); k
++) {
970 if (ELF_ST_BIND(syms
[k
].st_info
) != STB_GLOBAL
)
973 if (strcmp(strtab
+ syms
[k
].st_name
, name
) != 0)
976 if (syms
[k
].st_shndx
== SHN_UNDEF
||
977 syms
[k
].st_shndx
>= ehdr
->e_shnum
) {
978 pr_debug("Symbol: %s has bad section index %d.\n",
979 name
, syms
[k
].st_shndx
);
983 /* Found the symbol we are looking for */
991 void *kexec_purgatory_get_symbol_addr(struct kimage
*image
, const char *name
)
993 struct purgatory_info
*pi
= &image
->purgatory_info
;
997 sym
= kexec_purgatory_find_symbol(pi
, name
);
999 return ERR_PTR(-EINVAL
);
1001 sechdr
= &pi
->sechdrs
[sym
->st_shndx
];
1004 * Returns the address where symbol will finally be loaded after
1005 * kexec_load_segment()
1007 return (void *)(sechdr
->sh_addr
+ sym
->st_value
);
1011 * Get or set value of a symbol. If "get_value" is true, symbol value is
1012 * returned in buf otherwise symbol value is set based on value in buf.
1014 int kexec_purgatory_get_set_symbol(struct kimage
*image
, const char *name
,
1015 void *buf
, unsigned int size
, bool get_value
)
1019 struct purgatory_info
*pi
= &image
->purgatory_info
;
1022 sym
= kexec_purgatory_find_symbol(pi
, name
);
1026 if (sym
->st_size
!= size
) {
1027 pr_err("symbol %s size mismatch: expected %lu actual %u\n",
1028 name
, (unsigned long)sym
->st_size
, size
);
1032 sechdrs
= pi
->sechdrs
;
1034 if (sechdrs
[sym
->st_shndx
].sh_type
== SHT_NOBITS
) {
1035 pr_err("symbol %s is in a bss section. Cannot %s\n", name
,
1036 get_value
? "get" : "set");
1040 sym_buf
= (unsigned char *)sechdrs
[sym
->st_shndx
].sh_offset
+
1044 memcpy((void *)buf
, sym_buf
, size
);
1046 memcpy((void *)sym_buf
, buf
, size
);