ipl_report.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/init.h>
  3. #include <linux/ctype.h>
  4. #include <asm/ebcdic.h>
  5. #include <asm/sclp.h>
  6. #include <asm/sections.h>
  7. #include <asm/boot_data.h>
  8. #include <asm/physmem_info.h>
  9. #include <uapi/asm/ipl.h>
  10. #include "boot.h"
  11. int __bootdata_preserved(ipl_secure_flag);
  12. unsigned long __bootdata_preserved(ipl_cert_list_addr);
  13. unsigned long __bootdata_preserved(ipl_cert_list_size);
  14. unsigned long __bootdata(early_ipl_comp_list_addr);
  15. unsigned long __bootdata(early_ipl_comp_list_size);
  16. static struct ipl_rb_certificates *certs;
  17. static struct ipl_rb_components *comps;
  18. static bool ipl_report_needs_saving;
  19. #define for_each_rb_entry(entry, rb) \
  20. for (entry = rb->entries; \
  21. (void *) entry + sizeof(*entry) <= (void *) rb + rb->len; \
  22. entry++)
  23. static unsigned long get_cert_comp_list_size(void)
  24. {
  25. struct ipl_rb_certificate_entry *cert;
  26. struct ipl_rb_component_entry *comp;
  27. /*
  28. * Find the length for the IPL report boot data
  29. */
  30. early_ipl_comp_list_size = 0;
  31. for_each_rb_entry(comp, comps)
  32. early_ipl_comp_list_size += sizeof(*comp);
  33. ipl_cert_list_size = 0;
  34. for_each_rb_entry(cert, certs)
  35. ipl_cert_list_size += sizeof(unsigned int) + cert->len;
  36. return ipl_cert_list_size + early_ipl_comp_list_size;
  37. }
  38. bool ipl_report_certs_intersects(unsigned long addr, unsigned long size,
  39. unsigned long *intersection_start)
  40. {
  41. struct ipl_rb_certificate_entry *cert;
  42. if (!ipl_report_needs_saving)
  43. return false;
  44. for_each_rb_entry(cert, certs) {
  45. if (intersects(addr, size, cert->addr, cert->len)) {
  46. *intersection_start = cert->addr;
  47. return true;
  48. }
  49. }
  50. return false;
  51. }
  52. static void copy_components_bootdata(void)
  53. {
  54. struct ipl_rb_component_entry *comp, *ptr;
  55. ptr = (struct ipl_rb_component_entry *) early_ipl_comp_list_addr;
  56. for_each_rb_entry(comp, comps)
  57. memcpy(ptr++, comp, sizeof(*ptr));
  58. }
  59. static void copy_certificates_bootdata(void)
  60. {
  61. struct ipl_rb_certificate_entry *cert;
  62. void *ptr;
  63. ptr = (void *) ipl_cert_list_addr;
  64. for_each_rb_entry(cert, certs) {
  65. *(unsigned int *) ptr = cert->len;
  66. ptr += sizeof(unsigned int);
  67. memcpy(ptr, (void *) cert->addr, cert->len);
  68. ptr += cert->len;
  69. }
  70. }
  71. int read_ipl_report(void)
  72. {
  73. struct ipl_pl_hdr *pl_hdr;
  74. struct ipl_rl_hdr *rl_hdr;
  75. struct ipl_rb_hdr *rb_hdr;
  76. unsigned long tmp;
  77. void *rl_end;
  78. /*
  79. * Check if there is a IPL report by looking at the copy
  80. * of the IPL parameter information block.
  81. */
  82. if (!ipl_block_valid ||
  83. !(ipl_block.hdr.flags & IPL_PL_FLAG_IPLSR))
  84. return -1;
  85. ipl_secure_flag = !!(ipl_block.hdr.flags & IPL_PL_FLAG_SIPL);
  86. /*
  87. * There is an IPL report, to find it load the pointer to the
  88. * IPL parameter information block from lowcore and skip past
  89. * the IPL parameter list, then align the address to a double
  90. * word boundary.
  91. */
  92. tmp = (unsigned long)get_lowcore()->ipl_parmblock_ptr;
  93. pl_hdr = (struct ipl_pl_hdr *) tmp;
  94. tmp = (tmp + pl_hdr->len + 7) & -8UL;
  95. rl_hdr = (struct ipl_rl_hdr *) tmp;
  96. /* Walk through the IPL report blocks in the IPL Report list */
  97. certs = NULL;
  98. comps = NULL;
  99. rl_end = (void *) rl_hdr + rl_hdr->len;
  100. rb_hdr = (void *) rl_hdr + sizeof(*rl_hdr);
  101. while ((void *) rb_hdr + sizeof(*rb_hdr) < rl_end &&
  102. (void *) rb_hdr + rb_hdr->len <= rl_end) {
  103. switch (rb_hdr->rbt) {
  104. case IPL_RBT_CERTIFICATES:
  105. certs = (struct ipl_rb_certificates *) rb_hdr;
  106. break;
  107. case IPL_RBT_COMPONENTS:
  108. comps = (struct ipl_rb_components *) rb_hdr;
  109. break;
  110. default:
  111. break;
  112. }
  113. rb_hdr = (void *) rb_hdr + rb_hdr->len;
  114. }
  115. /*
  116. * With either the component list or the certificate list
  117. * missing the kernel will stay ignorant of secure IPL.
  118. */
  119. if (!comps || !certs) {
  120. certs = NULL;
  121. return -1;
  122. }
  123. ipl_report_needs_saving = true;
  124. physmem_reserve(RR_IPLREPORT, (unsigned long)pl_hdr,
  125. (unsigned long)rl_end - (unsigned long)pl_hdr);
  126. return 0;
  127. }
  128. void save_ipl_cert_comp_list(void)
  129. {
  130. unsigned long size;
  131. if (!ipl_report_needs_saving)
  132. return;
  133. size = get_cert_comp_list_size();
  134. early_ipl_comp_list_addr = physmem_alloc_or_die(RR_CERT_COMP_LIST, size, sizeof(int));
  135. ipl_cert_list_addr = early_ipl_comp_list_addr + early_ipl_comp_list_size;
  136. copy_components_bootdata();
  137. copy_certificates_bootdata();
  138. physmem_free(RR_IPLREPORT);
  139. ipl_report_needs_saving = false;
  140. }