Debian Codegraph @

kexec/arch/arm64/kexec-vmlinuz-arm64.c

raw symbols SCIP
1 /*
2 * ARM64 PE compressed Image (vmlinuz, ZBOOT) support.
3 *
4 * Several distros use 'make zinstall' rule inside
5 * 'arch/arm64/boot/Makefile' to install the arm64
6 * ZBOOT compressed file inside the boot destination
7 * directory (for e.g. /boot).
8 *
9 * Currently we cannot use kexec_file_load() to load vmlinuz
10 * PE images that self decompress.
11 *
12 * To support ZBOOT, we should:
13 * a). Copy the compressed contents of vmlinuz to a temporary file.
14 * b). Decompress (gunzip-decompress) the contents inside the
15 * temporary file.
16 * c). Validate the resulting image and write it back to the
17 * temporary file.
18 * d). Pass the 'fd' of the temporary file to the kernel space.
19 *
20 * Note this, module doesn't provide a _load() function instead
21 * relying on image_arm64_load() to load the resulting decompressed
22 * image.
23 *
24 * So basically the kernel space still gets a decompressed
25 * kernel image to load via kexec-tools.
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 /* Returns:
40 * -1 : in case of error/invalid format (not a valid PE+compressed ZBOOT format.
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 /* validate the arm64 specific header */
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 }