ipl_report.c 4.2 KB

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