Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _ASM_X86_BOOTPARAM_UTILS_H
0003 #define _ASM_X86_BOOTPARAM_UTILS_H
0004 
0005 #include <asm/bootparam.h>
0006 
0007 /*
0008  * This file is included from multiple environments.  Do not
0009  * add completing #includes to make it standalone.
0010  */
0011 
0012 /*
0013  * Deal with bootloaders which fail to initialize unknown fields in
0014  * boot_params to zero.  The list fields in this list are taken from
0015  * analysis of kexec-tools; if other broken bootloaders initialize a
0016  * different set of fields we will need to figure out how to disambiguate.
0017  *
0018  * Note: efi_info is commonly left uninitialized, but that field has a
0019  * private magic, so it is better to leave it unchanged.
0020  */
0021 
0022 #define sizeof_mbr(type, member) ({ sizeof(((type *)0)->member); })
0023 
0024 #define BOOT_PARAM_PRESERVE(struct_member)              \
0025     {                               \
0026         .start = offsetof(struct boot_params, struct_member),   \
0027         .len   = sizeof_mbr(struct boot_params, struct_member), \
0028     }
0029 
0030 struct boot_params_to_save {
0031     unsigned int start;
0032     unsigned int len;
0033 };
0034 
0035 static void sanitize_boot_params(struct boot_params *boot_params)
0036 {
0037     /* 
0038      * IMPORTANT NOTE TO BOOTLOADER AUTHORS: do not simply clear
0039      * this field.  The purpose of this field is to guarantee
0040      * compliance with the x86 boot spec located in
0041      * Documentation/x86/boot.rst .  That spec says that the
0042      * *whole* structure should be cleared, after which only the
0043      * portion defined by struct setup_header (boot_params->hdr)
0044      * should be copied in.
0045      *
0046      * If you're having an issue because the sentinel is set, you
0047      * need to change the whole structure to be cleared, not this
0048      * (or any other) individual field, or you will soon have
0049      * problems again.
0050      */
0051     if (boot_params->sentinel) {
0052         static struct boot_params scratch;
0053         char *bp_base = (char *)boot_params;
0054         char *save_base = (char *)&scratch;
0055         int i;
0056 
0057         const struct boot_params_to_save to_save[] = {
0058             BOOT_PARAM_PRESERVE(screen_info),
0059             BOOT_PARAM_PRESERVE(apm_bios_info),
0060             BOOT_PARAM_PRESERVE(tboot_addr),
0061             BOOT_PARAM_PRESERVE(ist_info),
0062             BOOT_PARAM_PRESERVE(hd0_info),
0063             BOOT_PARAM_PRESERVE(hd1_info),
0064             BOOT_PARAM_PRESERVE(sys_desc_table),
0065             BOOT_PARAM_PRESERVE(olpc_ofw_header),
0066             BOOT_PARAM_PRESERVE(efi_info),
0067             BOOT_PARAM_PRESERVE(alt_mem_k),
0068             BOOT_PARAM_PRESERVE(scratch),
0069             BOOT_PARAM_PRESERVE(e820_entries),
0070             BOOT_PARAM_PRESERVE(eddbuf_entries),
0071             BOOT_PARAM_PRESERVE(edd_mbr_sig_buf_entries),
0072             BOOT_PARAM_PRESERVE(edd_mbr_sig_buffer),
0073             BOOT_PARAM_PRESERVE(secure_boot),
0074             BOOT_PARAM_PRESERVE(hdr),
0075             BOOT_PARAM_PRESERVE(e820_table),
0076             BOOT_PARAM_PRESERVE(eddbuf),
0077             BOOT_PARAM_PRESERVE(cc_blob_address),
0078         };
0079 
0080         memset(&scratch, 0, sizeof(scratch));
0081 
0082         for (i = 0; i < ARRAY_SIZE(to_save); i++) {
0083             memcpy(save_base + to_save[i].start,
0084                    bp_base + to_save[i].start, to_save[i].len);
0085         }
0086 
0087         memcpy(boot_params, save_base, sizeof(*boot_params));
0088     }
0089 }
0090 
0091 #endif /* _ASM_X86_BOOTPARAM_UTILS_H */