ipl_report.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 <uapi/asm/ipl.h>
  9. #include "boot.h"
  10. int __bootdata_preserved(ipl_secure_flag);
  11. unsigned long __bootdata_preserved(ipl_cert_list_addr);
  12. unsigned long __bootdata_preserved(ipl_cert_list_size);
  13. unsigned long __bootdata(early_ipl_comp_list_addr);
  14. unsigned long __bootdata(early_ipl_comp_list_size);
  15. #define for_each_rb_entry(entry, rb) \
  16. for (entry = rb->entries; \
  17. (void *) entry + sizeof(*entry) <= (void *) rb + rb->len; \
  18. entry++)
  19. static inline bool intersects(unsigned long addr0, unsigned long size0,
  20. unsigned long addr1, unsigned long size1)
  21. {
  22. return addr0 + size0 > addr1 && addr1 + size1 > addr0;
  23. }
  24. static unsigned long find_bootdata_space(struct ipl_rb_components *comps,
  25. struct ipl_rb_certificates *certs,
  26. unsigned long safe_addr)
  27. {
  28. struct ipl_rb_certificate_entry *cert;
  29. struct ipl_rb_component_entry *comp;
  30. size_t size;
  31. /*
  32. * Find the length for the IPL report boot data
  33. */
  34. early_ipl_comp_list_size = 0;
  35. for_each_rb_entry(comp, comps)
  36. early_ipl_comp_list_size += sizeof(*comp);
  37. ipl_cert_list_size = 0;
  38. for_each_rb_entry(cert, certs)
  39. ipl_cert_list_size += sizeof(unsigned int) + cert->len;
  40. size = ipl_cert_list_size + early_ipl_comp_list_size;
  41. /*
  42. * Start from safe_addr to find a free memory area large
  43. * enough for the IPL report boot data. This area is used
  44. * for ipl_cert_list_addr/ipl_cert_list_size and
  45. * early_ipl_comp_list_addr/early_ipl_comp_list_size. It must
  46. * not overlap with any component or any certificate.
  47. */
  48. repeat:
  49. if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && initrd_data.start && initrd_data.size &&
  50. intersects(initrd_data.start, initrd_data.size, safe_addr, size))
  51. safe_addr = initrd_data.start + initrd_data.size;
  52. if (intersects(safe_addr, size, (unsigned long)comps, comps->len)) {
  53. safe_addr = (unsigned long)comps + comps->len;
  54. goto repeat;
  55. }
  56. for_each_rb_entry(comp, comps)
  57. if (intersects(safe_addr, size, comp->addr, comp->len)) {
  58. safe_addr = comp->addr + comp->len;
  59. goto repeat;
  60. }
  61. if (intersects(safe_addr, size, (unsigned long)certs, certs->len)) {
  62. safe_addr = (unsigned long)certs + certs->len;
  63. goto repeat;
  64. }
  65. for_each_rb_entry(cert, certs)
  66. if (intersects(safe_addr, size, cert->addr, cert->len)) {
  67. safe_addr = cert->addr + cert->len;
  68. goto repeat;
  69. }
  70. early_ipl_comp_list_addr = safe_addr;
  71. ipl_cert_list_addr = safe_addr + early_ipl_comp_list_size;
  72. return safe_addr + size;
  73. }
  74. static void copy_components_bootdata(struct ipl_rb_components *comps)
  75. {
  76. struct ipl_rb_component_entry *comp, *ptr;
  77. ptr = (struct ipl_rb_component_entry *) early_ipl_comp_list_addr;
  78. for_each_rb_entry(comp, comps)
  79. memcpy(ptr++, comp, sizeof(*ptr));
  80. }
  81. static void copy_certificates_bootdata(struct ipl_rb_certificates *certs)
  82. {
  83. struct ipl_rb_certificate_entry *cert;
  84. void *ptr;
  85. ptr = (void *) ipl_cert_list_addr;
  86. for_each_rb_entry(cert, certs) {
  87. *(unsigned int *) ptr = cert->len;
  88. ptr += sizeof(unsigned int);
  89. memcpy(ptr, (void *) cert->addr, cert->len);
  90. ptr += cert->len;
  91. }
  92. }
  93. unsigned long read_ipl_report(unsigned long safe_addr)
  94. {
  95. struct ipl_rb_certificates *certs;
  96. struct ipl_rb_components *comps;
  97. struct ipl_pl_hdr *pl_hdr;
  98. struct ipl_rl_hdr *rl_hdr;
  99. struct ipl_rb_hdr *rb_hdr;
  100. unsigned long tmp;
  101. void *rl_end;
  102. /*
  103. * Check if there is a IPL report by looking at the copy
  104. * of the IPL parameter information block.
  105. */
  106. if (!ipl_block_valid ||
  107. !(ipl_block.hdr.flags & IPL_PL_FLAG_IPLSR))
  108. return safe_addr;
  109. ipl_secure_flag = !!(ipl_block.hdr.flags & IPL_PL_FLAG_SIPL);
  110. /*
  111. * There is an IPL report, to find it load the pointer to the
  112. * IPL parameter information block from lowcore and skip past
  113. * the IPL parameter list, then align the address to a double
  114. * word boundary.
  115. */
  116. tmp = (unsigned long) S390_lowcore.ipl_parmblock_ptr;
  117. pl_hdr = (struct ipl_pl_hdr *) tmp;
  118. tmp = (tmp + pl_hdr->len + 7) & -8UL;
  119. rl_hdr = (struct ipl_rl_hdr *) tmp;
  120. /* Walk through the IPL report blocks in the IPL Report list */
  121. certs = NULL;
  122. comps = NULL;
  123. rl_end = (void *) rl_hdr + rl_hdr->len;
  124. rb_hdr = (void *) rl_hdr + sizeof(*rl_hdr);
  125. while ((void *) rb_hdr + sizeof(*rb_hdr) < rl_end &&
  126. (void *) rb_hdr + rb_hdr->len <= rl_end) {
  127. switch (rb_hdr->rbt) {
  128. case IPL_RBT_CERTIFICATES:
  129. certs = (struct ipl_rb_certificates *) rb_hdr;
  130. break;
  131. case IPL_RBT_COMPONENTS:
  132. comps = (struct ipl_rb_components *) rb_hdr;
  133. break;
  134. default:
  135. break;
  136. }
  137. rb_hdr = (void *) rb_hdr + rb_hdr->len;
  138. }
  139. /*
  140. * With either the component list or the certificate list
  141. * missing the kernel will stay ignorant of secure IPL.
  142. */
  143. if (!comps || !certs)
  144. return safe_addr;
  145. /*
  146. * Copy component and certificate list to a safe area
  147. * where the decompressed kernel can find them.
  148. */
  149. safe_addr = find_bootdata_space(comps, certs, safe_addr);
  150. copy_components_bootdata(comps);
  151. copy_certificates_bootdata(certs);
  152. return safe_addr;
  153. }