2 * Load ELF vmlinux file for the kexec_file_load syscall.
4 * Copyright (C) 2004 Adam Litke (agl@us.ibm.com)
5 * Copyright (C) 2004 IBM Corp.
6 * Copyright (C) 2005 R Sharada (sharada@in.ibm.com)
7 * Copyright (C) 2006 Mohan Kumar M (mohan@in.ibm.com)
8 * Copyright (C) 2016 IBM Corporation
10 * Based on kexec-tools' kexec-elf-exec.c and kexec-elf-ppc64.c.
11 * Heavily modified for the kernel by
12 * Thiago Jung Bauermann <bauerman@linux.vnet.ibm.com>.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation (version 2 of the License).
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
24 #define pr_fmt(fmt) "kexec_elf: " fmt
26 #include <linux/elf.h>
27 #include <linux/kexec.h>
28 #include <linux/libfdt.h>
29 #include <linux/module.h>
30 #include <linux/of_fdt.h>
31 #include <linux/slab.h>
32 #include <linux/types.h>
34 #define PURGATORY_STACK_SIZE (16 * 1024)
36 #define elf_addr_to_cpu elf64_to_cpu
39 #define Elf_Rel Elf64_Rel
44 * Where the ELF binary contents are kept.
45 * Memory managed by the user of the struct.
49 const struct elfhdr
*ehdr
;
50 const struct elf_phdr
*proghdrs
;
51 struct elf_shdr
*sechdrs
;
54 static inline bool elf_is_elf_file(const struct elfhdr
*ehdr
)
56 return memcmp(ehdr
->e_ident
, ELFMAG
, SELFMAG
) == 0;
59 static uint64_t elf64_to_cpu(const struct elfhdr
*ehdr
, uint64_t value
)
61 if (ehdr
->e_ident
[EI_DATA
] == ELFDATA2LSB
)
62 value
= le64_to_cpu(value
);
63 else if (ehdr
->e_ident
[EI_DATA
] == ELFDATA2MSB
)
64 value
= be64_to_cpu(value
);
69 static uint16_t elf16_to_cpu(const struct elfhdr
*ehdr
, uint16_t value
)
71 if (ehdr
->e_ident
[EI_DATA
] == ELFDATA2LSB
)
72 value
= le16_to_cpu(value
);
73 else if (ehdr
->e_ident
[EI_DATA
] == ELFDATA2MSB
)
74 value
= be16_to_cpu(value
);
79 static uint32_t elf32_to_cpu(const struct elfhdr
*ehdr
, uint32_t value
)
81 if (ehdr
->e_ident
[EI_DATA
] == ELFDATA2LSB
)
82 value
= le32_to_cpu(value
);
83 else if (ehdr
->e_ident
[EI_DATA
] == ELFDATA2MSB
)
84 value
= be32_to_cpu(value
);
90 * elf_is_ehdr_sane - check that it is safe to use the ELF header
91 * @buf_len: size of the buffer in which the ELF file is loaded.
93 static bool elf_is_ehdr_sane(const struct elfhdr
*ehdr
, size_t buf_len
)
95 if (ehdr
->e_phnum
> 0 && ehdr
->e_phentsize
!= sizeof(struct elf_phdr
)) {
96 pr_debug("Bad program header size.\n");
98 } else if (ehdr
->e_shnum
> 0 &&
99 ehdr
->e_shentsize
!= sizeof(struct elf_shdr
)) {
100 pr_debug("Bad section header size.\n");
102 } else if (ehdr
->e_ident
[EI_VERSION
] != EV_CURRENT
||
103 ehdr
->e_version
!= EV_CURRENT
) {
104 pr_debug("Unknown ELF version.\n");
108 if (ehdr
->e_phoff
> 0 && ehdr
->e_phnum
> 0) {
112 * e_phnum is at most 65535 so calculating the size of the
113 * program header cannot overflow.
115 phdr_size
= sizeof(struct elf_phdr
) * ehdr
->e_phnum
;
117 /* Sanity check the program header table location. */
118 if (ehdr
->e_phoff
+ phdr_size
< ehdr
->e_phoff
) {
119 pr_debug("Program headers at invalid location.\n");
121 } else if (ehdr
->e_phoff
+ phdr_size
> buf_len
) {
122 pr_debug("Program headers truncated.\n");
127 if (ehdr
->e_shoff
> 0 && ehdr
->e_shnum
> 0) {
131 * e_shnum is at most 65536 so calculating
132 * the size of the section header cannot overflow.
134 shdr_size
= sizeof(struct elf_shdr
) * ehdr
->e_shnum
;
136 /* Sanity check the section header table location. */
137 if (ehdr
->e_shoff
+ shdr_size
< ehdr
->e_shoff
) {
138 pr_debug("Section headers at invalid location.\n");
140 } else if (ehdr
->e_shoff
+ shdr_size
> buf_len
) {
141 pr_debug("Section headers truncated.\n");
149 static int elf_read_ehdr(const char *buf
, size_t len
, struct elfhdr
*ehdr
)
151 struct elfhdr
*buf_ehdr
;
153 if (len
< sizeof(*buf_ehdr
)) {
154 pr_debug("Buffer is too small to hold ELF header.\n");
158 memset(ehdr
, 0, sizeof(*ehdr
));
159 memcpy(ehdr
->e_ident
, buf
, sizeof(ehdr
->e_ident
));
160 if (!elf_is_elf_file(ehdr
)) {
161 pr_debug("No ELF header magic.\n");
165 if (ehdr
->e_ident
[EI_CLASS
] != ELF_CLASS
) {
166 pr_debug("Not a supported ELF class.\n");
168 } else if (ehdr
->e_ident
[EI_DATA
] != ELFDATA2LSB
&&
169 ehdr
->e_ident
[EI_DATA
] != ELFDATA2MSB
) {
170 pr_debug("Not a supported ELF data format.\n");
174 buf_ehdr
= (struct elfhdr
*) buf
;
175 if (elf16_to_cpu(ehdr
, buf_ehdr
->e_ehsize
) != sizeof(*buf_ehdr
)) {
176 pr_debug("Bad ELF header size.\n");
180 ehdr
->e_type
= elf16_to_cpu(ehdr
, buf_ehdr
->e_type
);
181 ehdr
->e_machine
= elf16_to_cpu(ehdr
, buf_ehdr
->e_machine
);
182 ehdr
->e_version
= elf32_to_cpu(ehdr
, buf_ehdr
->e_version
);
183 ehdr
->e_entry
= elf_addr_to_cpu(ehdr
, buf_ehdr
->e_entry
);
184 ehdr
->e_phoff
= elf_addr_to_cpu(ehdr
, buf_ehdr
->e_phoff
);
185 ehdr
->e_shoff
= elf_addr_to_cpu(ehdr
, buf_ehdr
->e_shoff
);
186 ehdr
->e_flags
= elf32_to_cpu(ehdr
, buf_ehdr
->e_flags
);
187 ehdr
->e_phentsize
= elf16_to_cpu(ehdr
, buf_ehdr
->e_phentsize
);
188 ehdr
->e_phnum
= elf16_to_cpu(ehdr
, buf_ehdr
->e_phnum
);
189 ehdr
->e_shentsize
= elf16_to_cpu(ehdr
, buf_ehdr
->e_shentsize
);
190 ehdr
->e_shnum
= elf16_to_cpu(ehdr
, buf_ehdr
->e_shnum
);
191 ehdr
->e_shstrndx
= elf16_to_cpu(ehdr
, buf_ehdr
->e_shstrndx
);
193 return elf_is_ehdr_sane(ehdr
, len
) ? 0 : -ENOEXEC
;
197 * elf_is_phdr_sane - check that it is safe to use the program header
198 * @buf_len: size of the buffer in which the ELF file is loaded.
200 static bool elf_is_phdr_sane(const struct elf_phdr
*phdr
, size_t buf_len
)
203 if (phdr
->p_offset
+ phdr
->p_filesz
< phdr
->p_offset
) {
204 pr_debug("ELF segment location wraps around.\n");
206 } else if (phdr
->p_offset
+ phdr
->p_filesz
> buf_len
) {
207 pr_debug("ELF segment not in file.\n");
209 } else if (phdr
->p_paddr
+ phdr
->p_memsz
< phdr
->p_paddr
) {
210 pr_debug("ELF segment address wraps around.\n");
217 static int elf_read_phdr(const char *buf
, size_t len
, struct elf_info
*elf_info
,
220 /* Override the const in proghdrs, we are the ones doing the loading. */
221 struct elf_phdr
*phdr
= (struct elf_phdr
*) &elf_info
->proghdrs
[idx
];
223 struct elf_phdr
*buf_phdr
;
225 pbuf
= buf
+ elf_info
->ehdr
->e_phoff
+ (idx
* sizeof(*buf_phdr
));
226 buf_phdr
= (struct elf_phdr
*) pbuf
;
228 phdr
->p_type
= elf32_to_cpu(elf_info
->ehdr
, buf_phdr
->p_type
);
229 phdr
->p_offset
= elf_addr_to_cpu(elf_info
->ehdr
, buf_phdr
->p_offset
);
230 phdr
->p_paddr
= elf_addr_to_cpu(elf_info
->ehdr
, buf_phdr
->p_paddr
);
231 phdr
->p_vaddr
= elf_addr_to_cpu(elf_info
->ehdr
, buf_phdr
->p_vaddr
);
232 phdr
->p_flags
= elf32_to_cpu(elf_info
->ehdr
, buf_phdr
->p_flags
);
235 * The following fields have a type equivalent to Elf_Addr
236 * both in 32 bit and 64 bit ELF.
238 phdr
->p_filesz
= elf_addr_to_cpu(elf_info
->ehdr
, buf_phdr
->p_filesz
);
239 phdr
->p_memsz
= elf_addr_to_cpu(elf_info
->ehdr
, buf_phdr
->p_memsz
);
240 phdr
->p_align
= elf_addr_to_cpu(elf_info
->ehdr
, buf_phdr
->p_align
);
242 return elf_is_phdr_sane(phdr
, len
) ? 0 : -ENOEXEC
;
246 * elf_read_phdrs - read the program headers from the buffer
248 * This function assumes that the program header table was checked for sanity.
249 * Use elf_is_ehdr_sane() if it wasn't.
251 static int elf_read_phdrs(const char *buf
, size_t len
,
252 struct elf_info
*elf_info
)
255 const struct elfhdr
*ehdr
= elf_info
->ehdr
;
258 * e_phnum is at most 65535 so calculating the size of the
259 * program header cannot overflow.
261 phdr_size
= sizeof(struct elf_phdr
) * ehdr
->e_phnum
;
263 elf_info
->proghdrs
= kzalloc(phdr_size
, GFP_KERNEL
);
264 if (!elf_info
->proghdrs
)
267 for (i
= 0; i
< ehdr
->e_phnum
; i
++) {
270 ret
= elf_read_phdr(buf
, len
, elf_info
, i
);
272 kfree(elf_info
->proghdrs
);
273 elf_info
->proghdrs
= NULL
;
282 * elf_is_shdr_sane - check that it is safe to use the section header
283 * @buf_len: size of the buffer in which the ELF file is loaded.
285 static bool elf_is_shdr_sane(const struct elf_shdr
*shdr
, size_t buf_len
)
289 /* SHT_NULL headers have undefined values, so we can't check them. */
290 if (shdr
->sh_type
== SHT_NULL
)
293 /* Now verify sh_entsize */
294 switch (shdr
->sh_type
) {
296 size_ok
= shdr
->sh_entsize
== sizeof(Elf_Sym
);
299 size_ok
= shdr
->sh_entsize
== sizeof(Elf_Rela
);
302 size_ok
= shdr
->sh_entsize
== sizeof(Elf_Dyn
);
305 size_ok
= shdr
->sh_entsize
== sizeof(Elf_Rel
);
313 * This is a section whose entsize requirements
314 * I don't care about. If I don't know about
315 * the section I can't care about it's entsize
323 pr_debug("ELF section with wrong entry size.\n");
325 } else if (shdr
->sh_addr
+ shdr
->sh_size
< shdr
->sh_addr
) {
326 pr_debug("ELF section address wraps around.\n");
330 if (shdr
->sh_type
!= SHT_NOBITS
) {
331 if (shdr
->sh_offset
+ shdr
->sh_size
< shdr
->sh_offset
) {
332 pr_debug("ELF section location wraps around.\n");
334 } else if (shdr
->sh_offset
+ shdr
->sh_size
> buf_len
) {
335 pr_debug("ELF section not in file.\n");
343 static int elf_read_shdr(const char *buf
, size_t len
, struct elf_info
*elf_info
,
346 struct elf_shdr
*shdr
= &elf_info
->sechdrs
[idx
];
347 const struct elfhdr
*ehdr
= elf_info
->ehdr
;
349 struct elf_shdr
*buf_shdr
;
351 sbuf
= buf
+ ehdr
->e_shoff
+ idx
* sizeof(*buf_shdr
);
352 buf_shdr
= (struct elf_shdr
*) sbuf
;
354 shdr
->sh_name
= elf32_to_cpu(ehdr
, buf_shdr
->sh_name
);
355 shdr
->sh_type
= elf32_to_cpu(ehdr
, buf_shdr
->sh_type
);
356 shdr
->sh_addr
= elf_addr_to_cpu(ehdr
, buf_shdr
->sh_addr
);
357 shdr
->sh_offset
= elf_addr_to_cpu(ehdr
, buf_shdr
->sh_offset
);
358 shdr
->sh_link
= elf32_to_cpu(ehdr
, buf_shdr
->sh_link
);
359 shdr
->sh_info
= elf32_to_cpu(ehdr
, buf_shdr
->sh_info
);
362 * The following fields have a type equivalent to Elf_Addr
363 * both in 32 bit and 64 bit ELF.
365 shdr
->sh_flags
= elf_addr_to_cpu(ehdr
, buf_shdr
->sh_flags
);
366 shdr
->sh_size
= elf_addr_to_cpu(ehdr
, buf_shdr
->sh_size
);
367 shdr
->sh_addralign
= elf_addr_to_cpu(ehdr
, buf_shdr
->sh_addralign
);
368 shdr
->sh_entsize
= elf_addr_to_cpu(ehdr
, buf_shdr
->sh_entsize
);
370 return elf_is_shdr_sane(shdr
, len
) ? 0 : -ENOEXEC
;
374 * elf_read_shdrs - read the section headers from the buffer
376 * This function assumes that the section header table was checked for sanity.
377 * Use elf_is_ehdr_sane() if it wasn't.
379 static int elf_read_shdrs(const char *buf
, size_t len
,
380 struct elf_info
*elf_info
)
385 * e_shnum is at most 65536 so calculating
386 * the size of the section header cannot overflow.
388 shdr_size
= sizeof(struct elf_shdr
) * elf_info
->ehdr
->e_shnum
;
390 elf_info
->sechdrs
= kzalloc(shdr_size
, GFP_KERNEL
);
391 if (!elf_info
->sechdrs
)
394 for (i
= 0; i
< elf_info
->ehdr
->e_shnum
; i
++) {
397 ret
= elf_read_shdr(buf
, len
, elf_info
, i
);
399 kfree(elf_info
->sechdrs
);
400 elf_info
->sechdrs
= NULL
;
409 * elf_read_from_buffer - read ELF file and sets up ELF header and ELF info
410 * @buf: Buffer to read ELF file from.
411 * @len: Size of @buf.
412 * @ehdr: Pointer to existing struct which will be populated.
413 * @elf_info: Pointer to existing struct which will be populated.
415 * This function allows reading ELF files with different byte order than
416 * the kernel, byte-swapping the fields as needed.
419 * On success returns 0, and the caller should call elf_free_info(elf_info) to
420 * free the memory allocated for the section and program headers.
422 int elf_read_from_buffer(const char *buf
, size_t len
, struct elfhdr
*ehdr
,
423 struct elf_info
*elf_info
)
427 ret
= elf_read_ehdr(buf
, len
, ehdr
);
431 elf_info
->buffer
= buf
;
432 elf_info
->ehdr
= ehdr
;
433 if (ehdr
->e_phoff
> 0 && ehdr
->e_phnum
> 0) {
434 ret
= elf_read_phdrs(buf
, len
, elf_info
);
438 if (ehdr
->e_shoff
> 0 && ehdr
->e_shnum
> 0) {
439 ret
= elf_read_shdrs(buf
, len
, elf_info
);
441 kfree(elf_info
->proghdrs
);
450 * elf_free_info - free memory allocated by elf_read_from_buffer
452 void elf_free_info(struct elf_info
*elf_info
)
454 kfree(elf_info
->proghdrs
);
455 kfree(elf_info
->sechdrs
);
456 memset(elf_info
, 0, sizeof(*elf_info
));
459 * build_elf_exec_info - read ELF executable and check that we can use it
461 static int build_elf_exec_info(const char *buf
, size_t len
, struct elfhdr
*ehdr
,
462 struct elf_info
*elf_info
)
467 ret
= elf_read_from_buffer(buf
, len
, ehdr
, elf_info
);
471 /* Big endian vmlinux has type ET_DYN. */
472 if (ehdr
->e_type
!= ET_EXEC
&& ehdr
->e_type
!= ET_DYN
) {
473 pr_err("Not an ELF executable.\n");
475 } else if (!elf_info
->proghdrs
) {
476 pr_err("No ELF program header.\n");
480 for (i
= 0; i
< ehdr
->e_phnum
; i
++) {
482 * Kexec does not support loading interpreters.
483 * In addition this check keeps us from attempting
484 * to kexec ordinay executables.
486 if (elf_info
->proghdrs
[i
].p_type
== PT_INTERP
) {
487 pr_err("Requires an ELF interpreter.\n");
494 elf_free_info(elf_info
);
498 static int elf64_probe(const char *buf
, unsigned long len
)
501 struct elf_info elf_info
;
504 ret
= build_elf_exec_info(buf
, len
, &ehdr
, &elf_info
);
508 elf_free_info(&elf_info
);
510 return elf_check_arch(&ehdr
) ? 0 : -ENOEXEC
;
514 * elf_exec_load - load ELF executable image
515 * @lowest_load_addr: On return, will be the address where the first PT_LOAD
516 * section will be loaded in memory.
519 * 0 on success, negative value on failure.
521 static int elf_exec_load(struct kimage
*image
, struct elfhdr
*ehdr
,
522 struct elf_info
*elf_info
,
523 unsigned long *lowest_load_addr
)
525 unsigned long base
= 0, lowest_addr
= UINT_MAX
;
528 struct kexec_buf kbuf
= { .image
= image
, .buf_max
= ppc64_rma_size
,
531 /* Read in the PT_LOAD segments. */
532 for (i
= 0; i
< ehdr
->e_phnum
; i
++) {
533 unsigned long load_addr
;
535 const struct elf_phdr
*phdr
;
537 phdr
= &elf_info
->proghdrs
[i
];
538 if (phdr
->p_type
!= PT_LOAD
)
541 size
= phdr
->p_filesz
;
542 if (size
> phdr
->p_memsz
)
543 size
= phdr
->p_memsz
;
545 kbuf
.buffer
= (void *) elf_info
->buffer
+ phdr
->p_offset
;
547 kbuf
.memsz
= phdr
->p_memsz
;
548 kbuf
.buf_align
= phdr
->p_align
;
549 kbuf
.buf_min
= phdr
->p_paddr
+ base
;
550 ret
= kexec_add_buffer(&kbuf
);
553 load_addr
= kbuf
.mem
;
555 if (load_addr
< lowest_addr
)
556 lowest_addr
= load_addr
;
559 /* Update entry point to reflect new load address. */
560 ehdr
->e_entry
+= base
;
562 *lowest_load_addr
= lowest_addr
;
568 static void *elf64_load(struct kimage
*image
, char *kernel_buf
,
569 unsigned long kernel_len
, char *initrd
,
570 unsigned long initrd_len
, char *cmdline
,
571 unsigned long cmdline_len
)
574 unsigned int fdt_size
;
575 unsigned long kernel_load_addr
, purgatory_load_addr
;
576 unsigned long initrd_load_addr
= 0, fdt_load_addr
;
578 const void *slave_code
;
580 struct elf_info elf_info
;
581 struct kexec_buf kbuf
= { .image
= image
, .buf_min
= 0,
582 .buf_max
= ppc64_rma_size
};
584 ret
= build_elf_exec_info(kernel_buf
, kernel_len
, &ehdr
, &elf_info
);
588 ret
= elf_exec_load(image
, &ehdr
, &elf_info
, &kernel_load_addr
);
592 pr_debug("Loaded the kernel at 0x%lx\n", kernel_load_addr
);
594 ret
= kexec_load_purgatory(image
, 0, ppc64_rma_size
, true,
595 &purgatory_load_addr
);
597 pr_err("Loading purgatory failed.\n");
601 pr_debug("Loaded purgatory at 0x%lx\n", purgatory_load_addr
);
603 if (initrd
!= NULL
) {
604 kbuf
.buffer
= initrd
;
605 kbuf
.bufsz
= kbuf
.memsz
= initrd_len
;
606 kbuf
.buf_align
= PAGE_SIZE
;
607 kbuf
.top_down
= false;
608 ret
= kexec_add_buffer(&kbuf
);
611 initrd_load_addr
= kbuf
.mem
;
613 pr_debug("Loaded initrd at 0x%lx\n", initrd_load_addr
);
616 fdt_size
= fdt_totalsize(initial_boot_params
) * 2;
617 fdt
= kmalloc(fdt_size
, GFP_KERNEL
);
619 pr_err("Not enough memory for the device tree.\n");
623 ret
= fdt_open_into(initial_boot_params
, fdt
, fdt_size
);
625 pr_err("Error setting up the new device tree.\n");
630 ret
= setup_new_fdt(image
, fdt
, initrd_load_addr
, initrd_len
, cmdline
);
637 kbuf
.bufsz
= kbuf
.memsz
= fdt_size
;
638 kbuf
.buf_align
= PAGE_SIZE
;
639 kbuf
.top_down
= true;
640 ret
= kexec_add_buffer(&kbuf
);
643 fdt_load_addr
= kbuf
.mem
;
645 pr_debug("Loaded device tree at 0x%lx\n", fdt_load_addr
);
647 slave_code
= elf_info
.buffer
+ elf_info
.proghdrs
[0].p_offset
;
648 ret
= setup_purgatory(image
, slave_code
, fdt
, kernel_load_addr
,
651 pr_err("Error setting up the purgatory.\n");
654 elf_free_info(&elf_info
);
656 /* Make kimage_file_post_load_cleanup free the fdt buffer for us. */
657 return ret
? ERR_PTR(ret
) : fdt
;
660 struct kexec_file_ops kexec_elf64_ops
= {
661 .probe
= elf64_probe
,