GNU Linux-libre 4.9.337-gnu1
[releases.git] / arch / x86 / include / asm / bootparam_utils.h
1 #ifndef _ASM_X86_BOOTPARAM_UTILS_H
2 #define _ASM_X86_BOOTPARAM_UTILS_H
3
4 #include <asm/bootparam.h>
5
6 /*
7  * This file is included from multiple environments.  Do not
8  * add completing #includes to make it standalone.
9  */
10
11 /*
12  * Deal with bootloaders which fail to initialize unknown fields in
13  * boot_params to zero.  The list fields in this list are taken from
14  * analysis of kexec-tools; if other broken bootloaders initialize a
15  * different set of fields we will need to figure out how to disambiguate.
16  *
17  * Note: efi_info is commonly left uninitialized, but that field has a
18  * private magic, so it is better to leave it unchanged.
19  */
20
21 #define sizeof_mbr(type, member) ({ sizeof(((type *)0)->member); })
22
23 #define BOOT_PARAM_PRESERVE(struct_member)                              \
24         {                                                               \
25                 .start = offsetof(struct boot_params, struct_member),   \
26                 .len   = sizeof_mbr(struct boot_params, struct_member), \
27         }
28
29 struct boot_params_to_save {
30         unsigned int start;
31         unsigned int len;
32 };
33
34 static void sanitize_boot_params(struct boot_params *boot_params)
35 {
36         /* 
37          * IMPORTANT NOTE TO BOOTLOADER AUTHORS: do not simply clear
38          * this field.  The purpose of this field is to guarantee
39          * compliance with the x86 boot spec located in
40          * Documentation/x86/boot.txt .  That spec says that the
41          * *whole* structure should be cleared, after which only the
42          * portion defined by struct setup_header (boot_params->hdr)
43          * should be copied in.
44          *
45          * If you're having an issue because the sentinel is set, you
46          * need to change the whole structure to be cleared, not this
47          * (or any other) individual field, or you will soon have
48          * problems again.
49          */
50         if (boot_params->sentinel) {
51                 /* fields in boot_params are left uninitialized, clear them */
52                 static struct boot_params scratch;
53                 char *bp_base = (char *)boot_params;
54                 char *save_base = (char *)&scratch;
55                 int i;
56
57                 const struct boot_params_to_save to_save[] = {
58                         BOOT_PARAM_PRESERVE(screen_info),
59                         BOOT_PARAM_PRESERVE(apm_bios_info),
60                         BOOT_PARAM_PRESERVE(tboot_addr),
61                         BOOT_PARAM_PRESERVE(ist_info),
62                         BOOT_PARAM_PRESERVE(hd0_info),
63                         BOOT_PARAM_PRESERVE(hd1_info),
64                         BOOT_PARAM_PRESERVE(sys_desc_table),
65                         BOOT_PARAM_PRESERVE(olpc_ofw_header),
66                         BOOT_PARAM_PRESERVE(efi_info),
67                         BOOT_PARAM_PRESERVE(alt_mem_k),
68                         BOOT_PARAM_PRESERVE(scratch),
69                         BOOT_PARAM_PRESERVE(e820_entries),
70                         BOOT_PARAM_PRESERVE(eddbuf_entries),
71                         BOOT_PARAM_PRESERVE(edd_mbr_sig_buf_entries),
72                         BOOT_PARAM_PRESERVE(edd_mbr_sig_buffer),
73                         BOOT_PARAM_PRESERVE(hdr),
74                         BOOT_PARAM_PRESERVE(e820_map),
75                         BOOT_PARAM_PRESERVE(eddbuf),
76                 };
77
78                 memset(&scratch, 0, sizeof(scratch));
79
80                 for (i = 0; i < ARRAY_SIZE(to_save); i++) {
81                         memcpy(save_base + to_save[i].start,
82                                bp_base + to_save[i].start, to_save[i].len);
83                 }
84
85                 memcpy(boot_params, save_base, sizeof(*boot_params));
86         }
87 }
88
89 #endif /* _ASM_X86_BOOTPARAM_UTILS_H */