| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
#define _GNU_SOURCE |
| 29 |
#include <fcntl.h> |
| 30 |
#include <unistd.h> |
| 31 |
#include <stdlib.h> |
| 32 |
#include "kexec-arm64.h" |
| 33 |
#include <kexec-pe-zboot.h> |
| 34 |
#include "arch/options.h" |
| 35 |
|
| 36 |
static int kernel_fd = -1; |
| 37 |
static off_t decompressed_size; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
int pez_arm64_probe(const char *kernel_buf, off_t kernel_size) |
| 43 |
{ |
| 44 |
int ret = -1; |
| 45 |
const struct arm64_image_header *h; |
| 46 |
char *buf; |
| 47 |
off_t buf_sz; |
| 48 |
|
| 49 |
buf = (char *)kernel_buf; |
| 50 |
buf_sz = kernel_size; |
| 51 |
if (!buf) |
| 52 |
return -1; |
| 53 |
h = (const struct arm64_image_header *)buf; |
| 54 |
|
| 55 |
dbgprintf("%s: PROBE.\n", __func__); |
| 56 |
if (buf_sz < sizeof(struct arm64_image_header)) { |
| 57 |
dbgprintf("%s: Not large enough to be a PE image.\n", __func__); |
| 58 |
return -1; |
| 59 |
} |
| 60 |
if (!arm64_header_check_pe_sig(h)) { |
| 61 |
dbgprintf("%s: Not an PE image.\n", __func__); |
| 62 |
return -1; |
| 63 |
} |
| 64 |
|
| 65 |
if (buf_sz < sizeof(struct arm64_image_header) + h->pe_header) { |
| 66 |
dbgprintf("%s: PE image offset larger than image.\n", __func__); |
| 67 |
return -1; |
| 68 |
} |
| 69 |
|
| 70 |
if (memcmp(&buf[h->pe_header], |
| 71 |
arm64_pe_machtype, sizeof(arm64_pe_machtype))) { |
| 72 |
dbgprintf("%s: PE header doesn't match machine type.\n", __func__); |
| 73 |
return -1; |
| 74 |
} |
| 75 |
|
| 76 |
ret = pez_prepare(buf, buf_sz, &kernel_fd, &decompressed_size); |
| 77 |
|
| 78 |
if (!ret) { |
| 79 |
|
| 80 |
struct arm64_image_header hdr_check; |
| 81 |
if (read(kernel_fd, &hdr_check, sizeof(hdr_check)) != sizeof(hdr_check)) |
| 82 |
goto bad_header; |
| 83 |
|
| 84 |
lseek(kernel_fd, 0, SEEK_SET); |
| 85 |
|
| 86 |
if (!arm64_header_check_magic(&hdr_check)) { |
| 87 |
dbgprintf("%s: Bad arm64 image header.\n", __func__); |
| 88 |
goto bad_header; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return ret; |
| 93 |
bad_header: |
| 94 |
close(kernel_fd); |
| 95 |
free(buf); |
| 96 |
return -1; |
| 97 |
} |
| 98 |
|
| 99 |
int pez_arm64_load(int argc, char **argv, const char *buf, off_t len, |
| 100 |
struct kexec_info *info) |
| 101 |
{ |
| 102 |
if (kernel_fd > 0 && decompressed_size > 0) { |
| 103 |
char *kbuf; |
| 104 |
off_t nread; |
| 105 |
int fd; |
| 106 |
|
| 107 |
if (info->kernel_fd > 0) |
| 108 |
close(info->kernel_fd); |
| 109 |
info->kernel_fd = kernel_fd; |
| 110 |
fd = dup(kernel_fd); |
| 111 |
if (fd < 0) { |
| 112 |
dbgprintf("%s: dup fd failed.\n", __func__); |
| 113 |
return -1; |
| 114 |
} |
| 115 |
kbuf = slurp_fd(fd, NULL, decompressed_size, &nread); |
| 116 |
if (!kbuf || nread != decompressed_size) { |
| 117 |
dbgprintf("%s: slurp_fd failed.\n", __func__); |
| 118 |
return -1; |
| 119 |
} |
| 120 |
return image_arm64_load(argc, argv, kbuf, decompressed_size, info); |
| 121 |
} |
| 122 |
|
| 123 |
dbgprintf("%s: wrong kernel file descriptor.\n", __func__); |
| 124 |
return -1; |
| 125 |
} |
| 126 |
|
| 127 |
void pez_arm64_usage(void) |
| 128 |
{ |
| 129 |
printf( |
| 130 |
" An ARM64 vmlinuz, PE image of a compressed, little endian.\n" |
| 131 |
" kernel, built with ZBOOT enabled.\n\n"); |
| 132 |
} |