1
2
3
4
5
6
7
8
9
10
11
12#define pr_fmt(fmt) "kexec-bzImage64: " fmt
13
14#include <linux/string.h>
15#include <linux/printk.h>
16#include <linux/errno.h>
17#include <linux/slab.h>
18#include <linux/kexec.h>
19#include <linux/kernel.h>
20#include <linux/mm.h>
21#include <linux/efi.h>
22#include <linux/verification.h>
23
24#include <asm/bootparam.h>
25#include <asm/setup.h>
26#include <asm/crash.h>
27#include <asm/efi.h>
28#include <asm/e820/api.h>
29#include <asm/kexec-bzimage64.h>
30
31#define MAX_ELFCOREHDR_STR_LEN 30
32
33
34
35
36
37
38
39#define MIN_PURGATORY_ADDR 0x3000
40#define MIN_BOOTPARAM_ADDR 0x3000
41#define MIN_KERNEL_LOAD_ADDR 0x100000
42#define MIN_INITRD_LOAD_ADDR 0x1000000
43
44
45
46
47
48
49struct bzimage64_data {
50
51
52
53
54 void *bootparams_buf;
55};
56
57static int setup_initrd(struct boot_params *params,
58 unsigned long initrd_load_addr, unsigned long initrd_len)
59{
60 params->hdr.ramdisk_image = initrd_load_addr & 0xffffffffUL;
61 params->hdr.ramdisk_size = initrd_len & 0xffffffffUL;
62
63 params->ext_ramdisk_image = initrd_load_addr >> 32;
64 params->ext_ramdisk_size = initrd_len >> 32;
65
66 return 0;
67}
68
69static int setup_cmdline(struct kimage *image, struct boot_params *params,
70 unsigned long bootparams_load_addr,
71 unsigned long cmdline_offset, char *cmdline,
72 unsigned long cmdline_len)
73{
74 char *cmdline_ptr = ((char *)params) + cmdline_offset;
75 unsigned long cmdline_ptr_phys, len = 0;
76 uint32_t cmdline_low_32, cmdline_ext_32;
77
78 if (image->type == KEXEC_TYPE_CRASH) {
79 len = sprintf(cmdline_ptr,
80 "elfcorehdr=0x%lx ", image->arch.elf_load_addr);
81 }
82 memcpy(cmdline_ptr + len, cmdline, cmdline_len);
83 cmdline_len += len;
84
85 cmdline_ptr[cmdline_len - 1] = '\0';
86
87 pr_debug("Final command line is: %s\n", cmdline_ptr);
88 cmdline_ptr_phys = bootparams_load_addr + cmdline_offset;
89 cmdline_low_32 = cmdline_ptr_phys & 0xffffffffUL;
90 cmdline_ext_32 = cmdline_ptr_phys >> 32;
91
92 params->hdr.cmd_line_ptr = cmdline_low_32;
93 if (cmdline_ext_32)
94 params->ext_cmd_line_ptr = cmdline_ext_32;
95
96 return 0;
97}
98
99static int setup_e820_entries(struct boot_params *params)
100{
101 unsigned int nr_e820_entries;
102
103 nr_e820_entries = e820_table_kexec->nr_entries;
104
105
106 if (nr_e820_entries > E820_MAX_ENTRIES_ZEROPAGE)
107 nr_e820_entries = E820_MAX_ENTRIES_ZEROPAGE;
108
109 params->e820_entries = nr_e820_entries;
110 memcpy(¶ms->e820_table, &e820_table_kexec->entries, nr_e820_entries*sizeof(struct e820_entry));
111
112 return 0;
113}
114
115#ifdef CONFIG_EFI
116static int setup_efi_info_memmap(struct boot_params *params,
117 unsigned long params_load_addr,
118 unsigned int efi_map_offset,
119 unsigned int efi_map_sz)
120{
121 void *efi_map = (void *)params + efi_map_offset;
122 unsigned long efi_map_phys_addr = params_load_addr + efi_map_offset;
123 struct efi_info *ei = ¶ms->efi_info;
124
125 if (!efi_map_sz)
126 return 0;
127
128 efi_runtime_map_copy(efi_map, efi_map_sz);
129
130 ei->efi_memmap = efi_map_phys_addr & 0xffffffff;
131 ei->efi_memmap_hi = efi_map_phys_addr >> 32;
132 ei->efi_memmap_size = efi_map_sz;
133
134 return 0;
135}
136
137static int
138prepare_add_efi_setup_data(struct boot_params *params,
139 unsigned long params_load_addr,
140 unsigned int efi_setup_data_offset)
141{
142 unsigned long setup_data_phys;
143 struct setup_data *sd = (void *)params + efi_setup_data_offset;
144 struct efi_setup_data *esd = (void *)sd + sizeof(struct setup_data);
145
146 esd->fw_vendor = efi.fw_vendor;
147 esd->runtime = efi.runtime;
148 esd->tables = efi.config_table;
149 esd->smbios = efi.smbios;
150
151 sd->type = SETUP_EFI;
152 sd->len = sizeof(struct efi_setup_data);
153
154
155 setup_data_phys = params_load_addr + efi_setup_data_offset;
156 sd->next = params->hdr.setup_data;
157 params->hdr.setup_data = setup_data_phys;
158
159 return 0;
160}
161
162static int
163setup_efi_state(struct boot_params *params, unsigned long params_load_addr,
164 unsigned int efi_map_offset, unsigned int efi_map_sz,
165 unsigned int efi_setup_data_offset)
166{
167 struct efi_info *current_ei = &boot_params.efi_info;
168 struct efi_info *ei = ¶ms->efi_info;
169
170 if (!efi_enabled(EFI_RUNTIME_SERVICES))
171 return 0;
172
173 if (!current_ei->efi_memmap_size)
174 return 0;
175
176 params->secure_boot = boot_params.secure_boot;
177 ei->efi_loader_signature = current_ei->efi_loader_signature;
178 ei->efi_systab = current_ei->efi_systab;
179 ei->efi_systab_hi = current_ei->efi_systab_hi;
180
181 ei->efi_memdesc_version = current_ei->efi_memdesc_version;
182 ei->efi_memdesc_size = efi_get_runtime_map_desc_size();
183
184 setup_efi_info_memmap(params, params_load_addr, efi_map_offset,
185 efi_map_sz);
186 prepare_add_efi_setup_data(params, params_load_addr,
187 efi_setup_data_offset);
188 return 0;
189}
190#endif
191
192static int
193setup_boot_parameters(struct kimage *image, struct boot_params *params,
194 unsigned long params_load_addr,
195 unsigned int efi_map_offset, unsigned int efi_map_sz,
196 unsigned int efi_setup_data_offset)
197{
198 unsigned int nr_e820_entries;
199 unsigned long long mem_k, start, end;
200 int i, ret = 0;
201
202
203 params->hdr.hardware_subarch = boot_params.hdr.hardware_subarch;
204
205
206 memcpy(¶ms->screen_info, &screen_info, sizeof(struct screen_info));
207
208
209 params->screen_info.ext_mem_k = 0;
210 params->alt_mem_k = 0;
211
212
213 params->acpi_rsdp_addr = boot_params.acpi_rsdp_addr;
214
215
216 memset(¶ms->apm_bios_info, 0, sizeof(params->apm_bios_info));
217
218
219 memset(¶ms->hd0_info, 0, sizeof(params->hd0_info));
220 memset(¶ms->hd1_info, 0, sizeof(params->hd1_info));
221
222 if (image->type == KEXEC_TYPE_CRASH) {
223 ret = crash_setup_memmap_entries(image, params);
224 if (ret)
225 return ret;
226 } else
227 setup_e820_entries(params);
228
229 nr_e820_entries = params->e820_entries;
230
231 for (i = 0; i < nr_e820_entries; i++) {
232 if (params->e820_table[i].type != E820_TYPE_RAM)
233 continue;
234 start = params->e820_table[i].addr;
235 end = params->e820_table[i].addr + params->e820_table[i].size - 1;
236
237 if ((start <= 0x100000) && end > 0x100000) {
238 mem_k = (end >> 10) - (0x100000 >> 10);
239 params->screen_info.ext_mem_k = mem_k;
240 params->alt_mem_k = mem_k;
241 if (mem_k > 0xfc00)
242 params->screen_info.ext_mem_k = 0xfc00;
243 if (mem_k > 0xffffffff)
244 params->alt_mem_k = 0xffffffff;
245 }
246 }
247
248#ifdef CONFIG_EFI
249
250 setup_efi_state(params, params_load_addr, efi_map_offset, efi_map_sz,
251 efi_setup_data_offset);
252#endif
253
254 memcpy(params->eddbuf, boot_params.eddbuf,
255 EDDMAXNR * sizeof(struct edd_info));
256 params->eddbuf_entries = boot_params.eddbuf_entries;
257
258 memcpy(params->edd_mbr_sig_buffer, boot_params.edd_mbr_sig_buffer,
259 EDD_MBR_SIG_MAX * sizeof(unsigned int));
260
261 return ret;
262}
263
264static int bzImage64_probe(const char *buf, unsigned long len)
265{
266 int ret = -ENOEXEC;
267 struct setup_header *header;
268
269
270 if (len < 2 * 512) {
271 pr_err("File is too short to be a bzImage\n");
272 return ret;
273 }
274
275 header = (struct setup_header *)(buf + offsetof(struct boot_params, hdr));
276 if (memcmp((char *)&header->header, "HdrS", 4) != 0) {
277 pr_err("Not a bzImage\n");
278 return ret;
279 }
280
281 if (header->boot_flag != 0xAA55) {
282 pr_err("No x86 boot sector present\n");
283 return ret;
284 }
285
286 if (header->version < 0x020C) {
287 pr_err("Must be at least protocol version 2.12\n");
288 return ret;
289 }
290
291 if (!(header->loadflags & LOADED_HIGH)) {
292 pr_err("zImage not a bzImage\n");
293 return ret;
294 }
295
296 if (!(header->xloadflags & XLF_KERNEL_64)) {
297 pr_err("Not a bzImage64. XLF_KERNEL_64 is not set.\n");
298 return ret;
299 }
300
301 if (!(header->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G)) {
302 pr_err("XLF_CAN_BE_LOADED_ABOVE_4G is not set.\n");
303 return ret;
304 }
305
306
307
308
309
310 if (efi_enabled(EFI_RUNTIME_SERVICES) && !efi_enabled(EFI_64BIT)) {
311 pr_debug("EFI is 32 bit. Can't load kernel above 4G.\n");
312 return ret;
313 }
314
315
316 pr_debug("It's a relocatable bzImage64\n");
317 ret = 0;
318
319 return ret;
320}
321
322static void *bzImage64_load(struct kimage *image, char *kernel,
323 unsigned long kernel_len, char *initrd,
324 unsigned long initrd_len, char *cmdline,
325 unsigned long cmdline_len)
326{
327
328 struct setup_header *header;
329 int setup_sects, kern16_size, ret = 0;
330 unsigned long setup_header_size, params_cmdline_sz;
331 struct boot_params *params;
332 unsigned long bootparam_load_addr, kernel_load_addr, initrd_load_addr;
333 struct bzimage64_data *ldata;
334 struct kexec_entry64_regs regs64;
335 void *stack;
336 unsigned int setup_hdr_offset = offsetof(struct boot_params, hdr);
337 unsigned int efi_map_offset, efi_map_sz, efi_setup_data_offset;
338 struct kexec_buf kbuf = { .image = image, .buf_max = ULONG_MAX,
339 .top_down = true };
340 struct kexec_buf pbuf = { .image = image, .buf_min = MIN_PURGATORY_ADDR,
341 .buf_max = ULONG_MAX, .top_down = true };
342
343 header = (struct setup_header *)(kernel + setup_hdr_offset);
344 setup_sects = header->setup_sects;
345 if (setup_sects == 0)
346 setup_sects = 4;
347
348 kern16_size = (setup_sects + 1) * 512;
349 if (kernel_len < kern16_size) {
350 pr_err("bzImage truncated\n");
351 return ERR_PTR(-ENOEXEC);
352 }
353
354 if (cmdline_len > header->cmdline_size) {
355 pr_err("Kernel command line too long\n");
356 return ERR_PTR(-EINVAL);
357 }
358
359
360
361
362
363 if (cmdline_len + MAX_ELFCOREHDR_STR_LEN > header->cmdline_size) {
364 pr_debug("Appending elfcorehdr=<addr> to command line exceeds maximum allowed length\n");
365 return ERR_PTR(-EINVAL);
366 }
367
368
369 if (image->type == KEXEC_TYPE_CRASH) {
370 ret = crash_load_segments(image);
371 if (ret)
372 return ERR_PTR(ret);
373 }
374
375
376
377
378
379 ret = kexec_load_purgatory(image, &pbuf);
380 if (ret) {
381 pr_err("Loading purgatory failed\n");
382 return ERR_PTR(ret);
383 }
384
385 pr_debug("Loaded purgatory at 0x%lx\n", pbuf.mem);
386
387
388
389
390
391
392
393
394
395
396 efi_map_sz = efi_get_runtime_map_size();
397 params_cmdline_sz = sizeof(struct boot_params) + cmdline_len +
398 MAX_ELFCOREHDR_STR_LEN;
399 params_cmdline_sz = ALIGN(params_cmdline_sz, 16);
400 kbuf.bufsz = params_cmdline_sz + ALIGN(efi_map_sz, 16) +
401 sizeof(struct setup_data) +
402 sizeof(struct efi_setup_data);
403
404 params = kzalloc(kbuf.bufsz, GFP_KERNEL);
405 if (!params)
406 return ERR_PTR(-ENOMEM);
407 efi_map_offset = params_cmdline_sz;
408 efi_setup_data_offset = efi_map_offset + ALIGN(efi_map_sz, 16);
409
410
411 setup_header_size = 0x0202 + kernel[0x0201] - setup_hdr_offset;
412
413
414 memcpy(¶ms->hdr, (kernel + setup_hdr_offset), setup_header_size);
415
416 kbuf.buffer = params;
417 kbuf.memsz = kbuf.bufsz;
418 kbuf.buf_align = 16;
419 kbuf.buf_min = MIN_BOOTPARAM_ADDR;
420 ret = kexec_add_buffer(&kbuf);
421 if (ret)
422 goto out_free_params;
423 bootparam_load_addr = kbuf.mem;
424 pr_debug("Loaded boot_param, command line and misc at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
425 bootparam_load_addr, kbuf.bufsz, kbuf.bufsz);
426
427
428 kbuf.buffer = kernel + kern16_size;
429 kbuf.bufsz = kernel_len - kern16_size;
430 kbuf.memsz = PAGE_ALIGN(header->init_size);
431 kbuf.buf_align = header->kernel_alignment;
432 kbuf.buf_min = MIN_KERNEL_LOAD_ADDR;
433 kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
434 ret = kexec_add_buffer(&kbuf);
435 if (ret)
436 goto out_free_params;
437 kernel_load_addr = kbuf.mem;
438
439 pr_debug("Loaded 64bit kernel at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
440 kernel_load_addr, kbuf.bufsz, kbuf.memsz);
441
442
443 if (initrd) {
444 kbuf.buffer = initrd;
445 kbuf.bufsz = kbuf.memsz = initrd_len;
446 kbuf.buf_align = PAGE_SIZE;
447 kbuf.buf_min = MIN_INITRD_LOAD_ADDR;
448 kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
449 ret = kexec_add_buffer(&kbuf);
450 if (ret)
451 goto out_free_params;
452 initrd_load_addr = kbuf.mem;
453
454 pr_debug("Loaded initrd at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
455 initrd_load_addr, initrd_len, initrd_len);
456
457 setup_initrd(params, initrd_load_addr, initrd_len);
458 }
459
460 setup_cmdline(image, params, bootparam_load_addr,
461 sizeof(struct boot_params), cmdline, cmdline_len);
462
463
464 params->hdr.type_of_loader = 0x0D << 4;
465 params->hdr.loadflags = 0;
466
467
468 ret = kexec_purgatory_get_set_symbol(image, "entry64_regs", ®s64,
469 sizeof(regs64), 1);
470 if (ret)
471 goto out_free_params;
472
473 regs64.rbx = 0;
474 regs64.rsi = bootparam_load_addr;
475 regs64.rip = kernel_load_addr + 0x200;
476 stack = kexec_purgatory_get_symbol_addr(image, "stack_end");
477 if (IS_ERR(stack)) {
478 pr_err("Could not find address of symbol stack_end\n");
479 ret = -EINVAL;
480 goto out_free_params;
481 }
482
483 regs64.rsp = (unsigned long)stack;
484 ret = kexec_purgatory_get_set_symbol(image, "entry64_regs", ®s64,
485 sizeof(regs64), 0);
486 if (ret)
487 goto out_free_params;
488
489 ret = setup_boot_parameters(image, params, bootparam_load_addr,
490 efi_map_offset, efi_map_sz,
491 efi_setup_data_offset);
492 if (ret)
493 goto out_free_params;
494
495
496 ldata = kzalloc(sizeof(struct bzimage64_data), GFP_KERNEL);
497 if (!ldata) {
498 ret = -ENOMEM;
499 goto out_free_params;
500 }
501
502
503
504
505
506
507 ldata->bootparams_buf = params;
508 return ldata;
509
510out_free_params:
511 kfree(params);
512 return ERR_PTR(ret);
513}
514
515
516static int bzImage64_cleanup(void *loader_data)
517{
518 struct bzimage64_data *ldata = loader_data;
519
520 if (!ldata)
521 return 0;
522
523 kfree(ldata->bootparams_buf);
524 ldata->bootparams_buf = NULL;
525
526 return 0;
527}
528
529#ifdef CONFIG_KEXEC_BZIMAGE_VERIFY_SIG
530static int bzImage64_verify_sig(const char *kernel, unsigned long kernel_len)
531{
532 int ret;
533
534 ret = verify_pefile_signature(kernel, kernel_len,
535 VERIFY_USE_SECONDARY_KEYRING,
536 VERIFYING_KEXEC_PE_SIGNATURE);
537 if (ret == -ENOKEY && IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING)) {
538 ret = verify_pefile_signature(kernel, kernel_len,
539 VERIFY_USE_PLATFORM_KEYRING,
540 VERIFYING_KEXEC_PE_SIGNATURE);
541 }
542 return ret;
543}
544#endif
545
546const struct kexec_file_ops kexec_bzImage64_ops = {
547 .probe = bzImage64_probe,
548 .load = bzImage64_load,
549 .cleanup = bzImage64_cleanup,
550#ifdef CONFIG_KEXEC_BZIMAGE_VERIFY_SIG
551 .verify_sig = bzImage64_verify_sig,
552#endif
553};
554