summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZhaofeng Li <hello@zhaofeng.li>2021-09-13 20:51:38 -0700
committerSimon Horman <horms@verge.net.au>2021-09-14 13:20:31 +0200
commitbfaebfb3ac740345eafda251a871638014983ba7 (patch)
treeea9b436021d42aa238bb4ba82c66cc651c7bd891
parent091f9e91bb24c475f7e09347de71cce811a2b314 (diff)
downloadkexec-tools-bfaebfb3ac740345eafda251a871638014983ba7.tar.gz
x86: Consolidate elf_x86_probe routines
Signed-off-by: Zhaofeng Li <hello@zhaofeng.li> Signed-off-by: Simon Horman <horms@verge.net.au>
-rw-r--r--kexec/arch/i386/kexec-elf-x86.c44
-rw-r--r--kexec/arch/i386/kexec-x86.h1
-rw-r--r--kexec/arch/x86_64/kexec-elf-x86_64.c28
3 files changed, 40 insertions, 33 deletions
diff --git a/kexec/arch/i386/kexec-elf-x86.c b/kexec/arch/i386/kexec-elf-x86.c
index fedf031c..8eba242a 100644
--- a/kexec/arch/i386/kexec-elf-x86.c
+++ b/kexec/arch/i386/kexec-elf-x86.c
@@ -42,7 +42,7 @@
static const int probe_debug = 0;
-int elf_x86_probe(const char *buf, off_t len)
+int elf_x86_any_probe(const char *buf, off_t len, enum coretype arch)
{
struct mem_ehdr ehdr;
@@ -56,20 +56,50 @@ int elf_x86_probe(const char *buf, off_t len)
}
/* Verify the architecuture specific bits */
- if ((ehdr.e_machine != EM_386) && (ehdr.e_machine != EM_486)) {
- /* for a different architecture */
- if (probe_debug) {
- fprintf(stderr, "Not i386 ELF executable\n");
+ switch (arch) {
+ case CORE_TYPE_ELF32:
+ if ((ehdr.e_machine != EM_386) && (ehdr.e_machine != EM_486)) {
+ if (probe_debug)
+ fprintf(stderr, "Not i386 ELF executable\n");
+ result = -1;
+ goto out;
}
- result = -1;
- goto out;
+ break;
+
+ case CORE_TYPE_ELF64:
+ if (ehdr.e_machine != EM_X86_64) {
+ if (probe_debug)
+ fprintf(stderr, "Not x86_64 ELF executable\n");
+ result = -1;
+ goto out;
+ }
+ break;
+
+ case CORE_TYPE_UNDEF:
+ default:
+ if (
+ (ehdr.e_machine != EM_386) &&
+ (ehdr.e_machine != EM_486) &&
+ (ehdr.e_machine != EM_X86_64)
+ ) {
+ if (probe_debug)
+ fprintf(stderr, "Not i386 or x86_64 ELF executable\n");
+ result = -1;
+ goto out;
+ }
+ break;
}
+
result = 0;
out:
free_elf_info(&ehdr);
return result;
}
+int elf_x86_probe(const char *buf, off_t len) {
+ return elf_x86_any_probe(buf, len, CORE_TYPE_ELF32);
+}
+
void elf_x86_usage(void)
{
printf( " --command-line=STRING Set the kernel command line to STRING\n"
diff --git a/kexec/arch/i386/kexec-x86.h b/kexec/arch/i386/kexec-x86.h
index 0f941dfe..71e43297 100644
--- a/kexec/arch/i386/kexec-x86.h
+++ b/kexec/arch/i386/kexec-x86.h
@@ -66,6 +66,7 @@ void multiboot2_x86_usage(void);
int multiboot2_x86_probe(const char *buf, off_t buf_len);
int elf_x86_probe(const char *buf, off_t len);
+int elf_x86_any_probe(const char *buf, off_t len, enum coretype arch);
int elf_x86_load(int argc, char **argv, const char *buf, off_t len,
struct kexec_info *info);
void elf_x86_usage(void);
diff --git a/kexec/arch/x86_64/kexec-elf-x86_64.c b/kexec/arch/x86_64/kexec-elf-x86_64.c
index ad223119..7f9540a8 100644
--- a/kexec/arch/x86_64/kexec-elf-x86_64.c
+++ b/kexec/arch/x86_64/kexec-elf-x86_64.c
@@ -37,37 +37,13 @@
#include "../../kexec-elf-boot.h"
#include "../i386/x86-linux-setup.h"
#include "kexec-x86_64.h"
+#include "../i386/kexec-x86.h"
#include "../i386/crashdump-x86.h"
#include <arch/options.h>
-static const int probe_debug = 0;
-
int elf_x86_64_probe(const char *buf, off_t len)
{
-
- struct mem_ehdr ehdr;
- int result;
- result = build_elf_exec_info(buf, len, &ehdr, 0);
- if (result < 0) {
- if (probe_debug) {
- fprintf(stderr, "Not an ELF executable\n");
- }
- goto out;
- }
-
- /* Verify the architecuture specific bits */
- if (ehdr.e_machine != EM_X86_64) {
- /* for a different architecture */
- if (probe_debug) {
- fprintf(stderr, "Not x86_64 ELF executable\n");
- }
- result = -1;
- goto out;
- }
- result = 0;
- out:
- free_elf_info(&ehdr);
- return result;
+ return elf_x86_any_probe(buf, len, CORE_TYPE_ELF64);
}
void elf_x86_64_usage(void)