GNU Linux-libre 6.8.7-gnu
[releases.git] / arch / x86 / kernel / kexec-bzimage64.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Kexec bzImage loader
4  *
5  * Copyright (C) 2014 Red Hat Inc.
6  * Authors:
7  *      Vivek Goyal <vgoyal@redhat.com>
8  */
9
10 #define pr_fmt(fmt)     "kexec-bzImage64: " fmt
11
12 #include <linux/string.h>
13 #include <linux/printk.h>
14 #include <linux/errno.h>
15 #include <linux/slab.h>
16 #include <linux/kexec.h>
17 #include <linux/kernel.h>
18 #include <linux/mm.h>
19 #include <linux/efi.h>
20 #include <linux/random.h>
21
22 #include <asm/bootparam.h>
23 #include <asm/setup.h>
24 #include <asm/crash.h>
25 #include <asm/efi.h>
26 #include <asm/e820/api.h>
27 #include <asm/kexec-bzimage64.h>
28
29 #define MAX_ELFCOREHDR_STR_LEN  30      /* elfcorehdr=0x<64bit-value> */
30
31 /*
32  * Defines lowest physical address for various segments. Not sure where
33  * exactly these limits came from. Current bzimage64 loader in kexec-tools
34  * uses these so I am retaining it. It can be changed over time as we gain
35  * more insight.
36  */
37 #define MIN_PURGATORY_ADDR      0x3000
38 #define MIN_BOOTPARAM_ADDR      0x3000
39 #define MIN_KERNEL_LOAD_ADDR    0x100000
40 #define MIN_INITRD_LOAD_ADDR    0x1000000
41
42 /*
43  * This is a place holder for all boot loader specific data structure which
44  * gets allocated in one call but gets freed much later during cleanup
45  * time. Right now there is only one field but it can grow as need be.
46  */
47 struct bzimage64_data {
48         /*
49          * Temporary buffer to hold bootparams buffer. This should be
50          * freed once the bootparam segment has been loaded.
51          */
52         void *bootparams_buf;
53 };
54
55 static int setup_initrd(struct boot_params *params,
56                 unsigned long initrd_load_addr, unsigned long initrd_len)
57 {
58         params->hdr.ramdisk_image = initrd_load_addr & 0xffffffffUL;
59         params->hdr.ramdisk_size = initrd_len & 0xffffffffUL;
60
61         params->ext_ramdisk_image = initrd_load_addr >> 32;
62         params->ext_ramdisk_size = initrd_len >> 32;
63
64         return 0;
65 }
66
67 static int setup_cmdline(struct kimage *image, struct boot_params *params,
68                          unsigned long bootparams_load_addr,
69                          unsigned long cmdline_offset, char *cmdline,
70                          unsigned long cmdline_len)
71 {
72         char *cmdline_ptr = ((char *)params) + cmdline_offset;
73         unsigned long cmdline_ptr_phys, len = 0;
74         uint32_t cmdline_low_32, cmdline_ext_32;
75
76         if (image->type == KEXEC_TYPE_CRASH) {
77                 len = sprintf(cmdline_ptr,
78                         "elfcorehdr=0x%lx ", image->elf_load_addr);
79         }
80         memcpy(cmdline_ptr + len, cmdline, cmdline_len);
81         cmdline_len += len;
82
83         cmdline_ptr[cmdline_len - 1] = '\0';
84
85         kexec_dprintk("Final command line is: %s\n", cmdline_ptr);
86         cmdline_ptr_phys = bootparams_load_addr + cmdline_offset;
87         cmdline_low_32 = cmdline_ptr_phys & 0xffffffffUL;
88         cmdline_ext_32 = cmdline_ptr_phys >> 32;
89
90         params->hdr.cmd_line_ptr = cmdline_low_32;
91         if (cmdline_ext_32)
92                 params->ext_cmd_line_ptr = cmdline_ext_32;
93
94         return 0;
95 }
96
97 static int setup_e820_entries(struct boot_params *params)
98 {
99         unsigned int nr_e820_entries;
100
101         nr_e820_entries = e820_table_kexec->nr_entries;
102
103         /* TODO: Pass entries more than E820_MAX_ENTRIES_ZEROPAGE in bootparams setup data */
104         if (nr_e820_entries > E820_MAX_ENTRIES_ZEROPAGE)
105                 nr_e820_entries = E820_MAX_ENTRIES_ZEROPAGE;
106
107         params->e820_entries = nr_e820_entries;
108         memcpy(&params->e820_table, &e820_table_kexec->entries, nr_e820_entries*sizeof(struct e820_entry));
109
110         return 0;
111 }
112
113 enum { RNG_SEED_LENGTH = 32 };
114
115 static void
116 setup_rng_seed(struct boot_params *params, unsigned long params_load_addr,
117                unsigned int rng_seed_setup_data_offset)
118 {
119         struct setup_data *sd = (void *)params + rng_seed_setup_data_offset;
120         unsigned long setup_data_phys;
121
122         if (!rng_is_initialized())
123                 return;
124
125         sd->type = SETUP_RNG_SEED;
126         sd->len = RNG_SEED_LENGTH;
127         get_random_bytes(sd->data, RNG_SEED_LENGTH);
128         setup_data_phys = params_load_addr + rng_seed_setup_data_offset;
129         sd->next = params->hdr.setup_data;
130         params->hdr.setup_data = setup_data_phys;
131 }
132
133 #ifdef CONFIG_EFI
134 static int setup_efi_info_memmap(struct boot_params *params,
135                                   unsigned long params_load_addr,
136                                   unsigned int efi_map_offset,
137                                   unsigned int efi_map_sz)
138 {
139         void *efi_map = (void *)params + efi_map_offset;
140         unsigned long efi_map_phys_addr = params_load_addr + efi_map_offset;
141         struct efi_info *ei = &params->efi_info;
142
143         if (!efi_map_sz)
144                 return 0;
145
146         efi_runtime_map_copy(efi_map, efi_map_sz);
147
148         ei->efi_memmap = efi_map_phys_addr & 0xffffffff;
149         ei->efi_memmap_hi = efi_map_phys_addr >> 32;
150         ei->efi_memmap_size = efi_map_sz;
151
152         return 0;
153 }
154
155 static int
156 prepare_add_efi_setup_data(struct boot_params *params,
157                        unsigned long params_load_addr,
158                        unsigned int efi_setup_data_offset)
159 {
160         unsigned long setup_data_phys;
161         struct setup_data *sd = (void *)params + efi_setup_data_offset;
162         struct efi_setup_data *esd = (void *)sd + sizeof(struct setup_data);
163
164         esd->fw_vendor = efi_fw_vendor;
165         esd->tables = efi_config_table;
166         esd->smbios = efi.smbios;
167
168         sd->type = SETUP_EFI;
169         sd->len = sizeof(struct efi_setup_data);
170
171         /* Add setup data */
172         setup_data_phys = params_load_addr + efi_setup_data_offset;
173         sd->next = params->hdr.setup_data;
174         params->hdr.setup_data = setup_data_phys;
175
176         return 0;
177 }
178
179 static int
180 setup_efi_state(struct boot_params *params, unsigned long params_load_addr,
181                 unsigned int efi_map_offset, unsigned int efi_map_sz,
182                 unsigned int efi_setup_data_offset)
183 {
184         struct efi_info *current_ei = &boot_params.efi_info;
185         struct efi_info *ei = &params->efi_info;
186
187         if (!efi_enabled(EFI_RUNTIME_SERVICES))
188                 return 0;
189
190         if (!current_ei->efi_memmap_size)
191                 return 0;
192
193         params->secure_boot = boot_params.secure_boot;
194         ei->efi_loader_signature = current_ei->efi_loader_signature;
195         ei->efi_systab = current_ei->efi_systab;
196         ei->efi_systab_hi = current_ei->efi_systab_hi;
197
198         ei->efi_memdesc_version = current_ei->efi_memdesc_version;
199         ei->efi_memdesc_size = efi_get_runtime_map_desc_size();
200
201         setup_efi_info_memmap(params, params_load_addr, efi_map_offset,
202                               efi_map_sz);
203         prepare_add_efi_setup_data(params, params_load_addr,
204                                    efi_setup_data_offset);
205         return 0;
206 }
207 #endif /* CONFIG_EFI */
208
209 static void
210 setup_ima_state(const struct kimage *image, struct boot_params *params,
211                 unsigned long params_load_addr,
212                 unsigned int ima_setup_data_offset)
213 {
214 #ifdef CONFIG_IMA_KEXEC
215         struct setup_data *sd = (void *)params + ima_setup_data_offset;
216         unsigned long setup_data_phys;
217         struct ima_setup_data *ima;
218
219         if (!image->ima_buffer_size)
220                 return;
221
222         sd->type = SETUP_IMA;
223         sd->len = sizeof(*ima);
224
225         ima = (void *)sd + sizeof(struct setup_data);
226         ima->addr = image->ima_buffer_addr;
227         ima->size = image->ima_buffer_size;
228
229         /* Add setup data */
230         setup_data_phys = params_load_addr + ima_setup_data_offset;
231         sd->next = params->hdr.setup_data;
232         params->hdr.setup_data = setup_data_phys;
233 #endif /* CONFIG_IMA_KEXEC */
234 }
235
236 static int
237 setup_boot_parameters(struct kimage *image, struct boot_params *params,
238                       unsigned long params_load_addr,
239                       unsigned int efi_map_offset, unsigned int efi_map_sz,
240                       unsigned int setup_data_offset)
241 {
242         unsigned int nr_e820_entries;
243         unsigned long long mem_k, start, end;
244         int i, ret = 0;
245
246         /* Get subarch from existing bootparams */
247         params->hdr.hardware_subarch = boot_params.hdr.hardware_subarch;
248
249         /* Copying screen_info will do? */
250         memcpy(&params->screen_info, &screen_info, sizeof(struct screen_info));
251
252         /* Fill in memsize later */
253         params->screen_info.ext_mem_k = 0;
254         params->alt_mem_k = 0;
255
256         /* Always fill in RSDP: it is either 0 or a valid value */
257         params->acpi_rsdp_addr = boot_params.acpi_rsdp_addr;
258
259         /* Default APM info */
260         memset(&params->apm_bios_info, 0, sizeof(params->apm_bios_info));
261
262         /* Default drive info */
263         memset(&params->hd0_info, 0, sizeof(params->hd0_info));
264         memset(&params->hd1_info, 0, sizeof(params->hd1_info));
265
266         if (image->type == KEXEC_TYPE_CRASH) {
267                 ret = crash_setup_memmap_entries(image, params);
268                 if (ret)
269                         return ret;
270         } else
271                 setup_e820_entries(params);
272
273         nr_e820_entries = params->e820_entries;
274
275         kexec_dprintk("E820 memmap:\n");
276         for (i = 0; i < nr_e820_entries; i++) {
277                 kexec_dprintk("%016llx-%016llx (%d)\n",
278                               params->e820_table[i].addr,
279                               params->e820_table[i].addr + params->e820_table[i].size - 1,
280                               params->e820_table[i].type);
281                 if (params->e820_table[i].type != E820_TYPE_RAM)
282                         continue;
283                 start = params->e820_table[i].addr;
284                 end = params->e820_table[i].addr + params->e820_table[i].size - 1;
285
286                 if ((start <= 0x100000) && end > 0x100000) {
287                         mem_k = (end >> 10) - (0x100000 >> 10);
288                         params->screen_info.ext_mem_k = mem_k;
289                         params->alt_mem_k = mem_k;
290                         if (mem_k > 0xfc00)
291                                 params->screen_info.ext_mem_k = 0xfc00; /* 64M*/
292                         if (mem_k > 0xffffffff)
293                                 params->alt_mem_k = 0xffffffff;
294                 }
295         }
296
297 #ifdef CONFIG_EFI
298         /* Setup EFI state */
299         setup_efi_state(params, params_load_addr, efi_map_offset, efi_map_sz,
300                         setup_data_offset);
301         setup_data_offset += sizeof(struct setup_data) +
302                         sizeof(struct efi_setup_data);
303 #endif
304
305         if (IS_ENABLED(CONFIG_IMA_KEXEC)) {
306                 /* Setup IMA log buffer state */
307                 setup_ima_state(image, params, params_load_addr,
308                                 setup_data_offset);
309                 setup_data_offset += sizeof(struct setup_data) +
310                                      sizeof(struct ima_setup_data);
311         }
312
313         /* Setup RNG seed */
314         setup_rng_seed(params, params_load_addr, setup_data_offset);
315
316         /* Setup EDD info */
317         memcpy(params->eddbuf, boot_params.eddbuf,
318                                 EDDMAXNR * sizeof(struct edd_info));
319         params->eddbuf_entries = boot_params.eddbuf_entries;
320
321         memcpy(params->edd_mbr_sig_buffer, boot_params.edd_mbr_sig_buffer,
322                EDD_MBR_SIG_MAX * sizeof(unsigned int));
323
324         return ret;
325 }
326
327 static int bzImage64_probe(const char *buf, unsigned long len)
328 {
329         int ret = -ENOEXEC;
330         struct setup_header *header;
331
332         /* kernel should be at least two sectors long */
333         if (len < 2 * 512) {
334                 pr_err("File is too short to be a bzImage\n");
335                 return ret;
336         }
337
338         header = (struct setup_header *)(buf + offsetof(struct boot_params, hdr));
339         if (memcmp((char *)&header->header, "HdrS", 4) != 0) {
340                 pr_err("Not a bzImage\n");
341                 return ret;
342         }
343
344         if (header->boot_flag != 0xAA55) {
345                 pr_err("No x86 boot sector present\n");
346                 return ret;
347         }
348
349         if (header->version < 0x020C) {
350                 pr_err("Must be at least protocol version 2.12\n");
351                 return ret;
352         }
353
354         if (!(header->loadflags & LOADED_HIGH)) {
355                 pr_err("zImage not a bzImage\n");
356                 return ret;
357         }
358
359         if (!(header->xloadflags & XLF_KERNEL_64)) {
360                 pr_err("Not a bzImage64. XLF_KERNEL_64 is not set.\n");
361                 return ret;
362         }
363
364         if (!(header->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G)) {
365                 pr_err("XLF_CAN_BE_LOADED_ABOVE_4G is not set.\n");
366                 return ret;
367         }
368
369         /*
370          * Can't handle 32bit EFI as it does not allow loading kernel
371          * above 4G. This should be handled by 32bit bzImage loader
372          */
373         if (efi_enabled(EFI_RUNTIME_SERVICES) && !efi_enabled(EFI_64BIT)) {
374                 pr_debug("EFI is 32 bit. Can't load kernel above 4G.\n");
375                 return ret;
376         }
377
378         if (!(header->xloadflags & XLF_5LEVEL) && pgtable_l5_enabled()) {
379                 pr_err("bzImage cannot handle 5-level paging mode.\n");
380                 return ret;
381         }
382
383         /* I've got a bzImage */
384         pr_debug("It's a relocatable bzImage64\n");
385         ret = 0;
386
387         return ret;
388 }
389
390 static void *bzImage64_load(struct kimage *image, char *kernel,
391                             unsigned long kernel_len, char *initrd,
392                             unsigned long initrd_len, char *cmdline,
393                             unsigned long cmdline_len)
394 {
395
396         struct setup_header *header;
397         int setup_sects, kern16_size, ret = 0;
398         unsigned long setup_header_size, params_cmdline_sz;
399         struct boot_params *params;
400         unsigned long bootparam_load_addr, kernel_load_addr, initrd_load_addr;
401         struct bzimage64_data *ldata;
402         struct kexec_entry64_regs regs64;
403         void *stack;
404         unsigned int setup_hdr_offset = offsetof(struct boot_params, hdr);
405         unsigned int efi_map_offset, efi_map_sz, efi_setup_data_offset;
406         struct kexec_buf kbuf = { .image = image, .buf_max = ULONG_MAX,
407                                   .top_down = true };
408         struct kexec_buf pbuf = { .image = image, .buf_min = MIN_PURGATORY_ADDR,
409                                   .buf_max = ULONG_MAX, .top_down = true };
410
411         header = (struct setup_header *)(kernel + setup_hdr_offset);
412         setup_sects = header->setup_sects;
413         if (setup_sects == 0)
414                 setup_sects = 4;
415
416         kern16_size = (setup_sects + 1) * 512;
417         if (kernel_len < kern16_size) {
418                 pr_err("bzImage truncated\n");
419                 return ERR_PTR(-ENOEXEC);
420         }
421
422         if (cmdline_len > header->cmdline_size) {
423                 pr_err("Kernel command line too long\n");
424                 return ERR_PTR(-EINVAL);
425         }
426
427         /*
428          * In case of crash dump, we will append elfcorehdr=<addr> to
429          * command line. Make sure it does not overflow
430          */
431         if (cmdline_len + MAX_ELFCOREHDR_STR_LEN > header->cmdline_size) {
432                 pr_err("Appending elfcorehdr=<addr> to command line exceeds maximum allowed length\n");
433                 return ERR_PTR(-EINVAL);
434         }
435
436         /* Allocate and load backup region */
437         if (image->type == KEXEC_TYPE_CRASH) {
438                 ret = crash_load_segments(image);
439                 if (ret)
440                         return ERR_PTR(ret);
441         }
442
443         /*
444          * Load purgatory. For 64bit entry point, purgatory  code can be
445          * anywhere.
446          */
447         ret = kexec_load_purgatory(image, &pbuf);
448         if (ret) {
449                 pr_err("Loading purgatory failed\n");
450                 return ERR_PTR(ret);
451         }
452
453         kexec_dprintk("Loaded purgatory at 0x%lx\n", pbuf.mem);
454
455
456         /*
457          * Load Bootparams and cmdline and space for efi stuff.
458          *
459          * Allocate memory together for multiple data structures so
460          * that they all can go in single area/segment and we don't
461          * have to create separate segment for each. Keeps things
462          * little bit simple
463          */
464         efi_map_sz = efi_get_runtime_map_size();
465         params_cmdline_sz = sizeof(struct boot_params) + cmdline_len +
466                                 MAX_ELFCOREHDR_STR_LEN;
467         params_cmdline_sz = ALIGN(params_cmdline_sz, 16);
468         kbuf.bufsz = params_cmdline_sz + ALIGN(efi_map_sz, 16) +
469                                 sizeof(struct setup_data) +
470                                 sizeof(struct efi_setup_data) +
471                                 sizeof(struct setup_data) +
472                                 RNG_SEED_LENGTH;
473
474         if (IS_ENABLED(CONFIG_IMA_KEXEC))
475                 kbuf.bufsz += sizeof(struct setup_data) +
476                               sizeof(struct ima_setup_data);
477
478         params = kzalloc(kbuf.bufsz, GFP_KERNEL);
479         if (!params)
480                 return ERR_PTR(-ENOMEM);
481         efi_map_offset = params_cmdline_sz;
482         efi_setup_data_offset = efi_map_offset + ALIGN(efi_map_sz, 16);
483
484         /* Copy setup header onto bootparams. Documentation/arch/x86/boot.rst */
485         setup_header_size = 0x0202 + kernel[0x0201] - setup_hdr_offset;
486
487         /* Is there a limit on setup header size? */
488         memcpy(&params->hdr, (kernel + setup_hdr_offset), setup_header_size);
489
490         kbuf.buffer = params;
491         kbuf.memsz = kbuf.bufsz;
492         kbuf.buf_align = 16;
493         kbuf.buf_min = MIN_BOOTPARAM_ADDR;
494         ret = kexec_add_buffer(&kbuf);
495         if (ret)
496                 goto out_free_params;
497         bootparam_load_addr = kbuf.mem;
498         kexec_dprintk("Loaded boot_param, command line and misc at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
499                       bootparam_load_addr, kbuf.bufsz, kbuf.memsz);
500
501         /* Load kernel */
502         kbuf.buffer = kernel + kern16_size;
503         kbuf.bufsz =  kernel_len - kern16_size;
504         kbuf.memsz = PAGE_ALIGN(header->init_size);
505         kbuf.buf_align = header->kernel_alignment;
506         kbuf.buf_min = MIN_KERNEL_LOAD_ADDR;
507         kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
508         ret = kexec_add_buffer(&kbuf);
509         if (ret)
510                 goto out_free_params;
511         kernel_load_addr = kbuf.mem;
512
513         kexec_dprintk("Loaded 64bit kernel at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
514                       kernel_load_addr, kbuf.bufsz, kbuf.memsz);
515
516         /* Load initrd high */
517         if (initrd) {
518                 kbuf.buffer = initrd;
519                 kbuf.bufsz = kbuf.memsz = initrd_len;
520                 kbuf.buf_align = PAGE_SIZE;
521                 kbuf.buf_min = MIN_INITRD_LOAD_ADDR;
522                 kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
523                 ret = kexec_add_buffer(&kbuf);
524                 if (ret)
525                         goto out_free_params;
526                 initrd_load_addr = kbuf.mem;
527
528                 kexec_dprintk("Loaded initrd at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
529                               initrd_load_addr, initrd_len, initrd_len);
530
531                 setup_initrd(params, initrd_load_addr, initrd_len);
532         }
533
534         setup_cmdline(image, params, bootparam_load_addr,
535                       sizeof(struct boot_params), cmdline, cmdline_len);
536
537         /* bootloader info. Do we need a separate ID for kexec kernel loader? */
538         params->hdr.type_of_loader = 0x0D << 4;
539         params->hdr.loadflags = 0;
540
541         /* Setup purgatory regs for entry */
542         ret = kexec_purgatory_get_set_symbol(image, "entry64_regs", &regs64,
543                                              sizeof(regs64), 1);
544         if (ret)
545                 goto out_free_params;
546
547         regs64.rbx = 0; /* Bootstrap Processor */
548         regs64.rsi = bootparam_load_addr;
549         regs64.rip = kernel_load_addr + 0x200;
550         stack = kexec_purgatory_get_symbol_addr(image, "stack_end");
551         if (IS_ERR(stack)) {
552                 pr_err("Could not find address of symbol stack_end\n");
553                 ret = -EINVAL;
554                 goto out_free_params;
555         }
556
557         regs64.rsp = (unsigned long)stack;
558         ret = kexec_purgatory_get_set_symbol(image, "entry64_regs", &regs64,
559                                              sizeof(regs64), 0);
560         if (ret)
561                 goto out_free_params;
562
563         ret = setup_boot_parameters(image, params, bootparam_load_addr,
564                                     efi_map_offset, efi_map_sz,
565                                     efi_setup_data_offset);
566         if (ret)
567                 goto out_free_params;
568
569         /* Allocate loader specific data */
570         ldata = kzalloc(sizeof(struct bzimage64_data), GFP_KERNEL);
571         if (!ldata) {
572                 ret = -ENOMEM;
573                 goto out_free_params;
574         }
575
576         /*
577          * Store pointer to params so that it could be freed after loading
578          * params segment has been loaded and contents have been copied
579          * somewhere else.
580          */
581         ldata->bootparams_buf = params;
582         return ldata;
583
584 out_free_params:
585         kfree(params);
586         return ERR_PTR(ret);
587 }
588
589 /* This cleanup function is called after various segments have been loaded */
590 static int bzImage64_cleanup(void *loader_data)
591 {
592         struct bzimage64_data *ldata = loader_data;
593
594         if (!ldata)
595                 return 0;
596
597         kfree(ldata->bootparams_buf);
598         ldata->bootparams_buf = NULL;
599
600         return 0;
601 }
602
603 const struct kexec_file_ops kexec_bzImage64_ops = {
604         .probe = bzImage64_probe,
605         .load = bzImage64_load,
606         .cleanup = bzImage64_cleanup,
607 #ifdef CONFIG_KEXEC_BZIMAGE_VERIFY_SIG
608         .verify_sig = kexec_kernel_verify_pe_sig,
609 #endif
610 };