bootparam_utils.h 2.8 KB

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