ipl_report.c 4.7 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 <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_START && INITRD_SIZE &&
  50. intersects(INITRD_START, INITRD_SIZE, safe_addr, size))
  51. safe_addr = INITRD_START + INITRD_SIZE;
  52. for_each_rb_entry(comp, comps)
  53. if (intersects(safe_addr, size, comp->addr, comp->len)) {
  54. safe_addr = comp->addr + comp->len;
  55. goto repeat;
  56. }
  57. for_each_rb_entry(cert, certs)
  58. if (intersects(safe_addr, size, cert->addr, cert->len)) {
  59. safe_addr = cert->addr + cert->len;
  60. goto repeat;
  61. }
  62. early_ipl_comp_list_addr = safe_addr;
  63. ipl_cert_list_addr = safe_addr + early_ipl_comp_list_size;
  64. return safe_addr + size;
  65. }
  66. static void copy_components_bootdata(struct ipl_rb_components *comps)
  67. {
  68. struct ipl_rb_component_entry *comp, *ptr;
  69. ptr = (struct ipl_rb_component_entry *) early_ipl_comp_list_addr;
  70. for_each_rb_entry(comp, comps)
  71. memcpy(ptr++, comp, sizeof(*ptr));
  72. }
  73. static void copy_certificates_bootdata(struct ipl_rb_certificates *certs)
  74. {
  75. struct ipl_rb_certificate_entry *cert;
  76. void *ptr;
  77. ptr = (void *) ipl_cert_list_addr;
  78. for_each_rb_entry(cert, certs) {
  79. *(unsigned int *) ptr = cert->len;
  80. ptr += sizeof(unsigned int);
  81. memcpy(ptr, (void *) cert->addr, cert->len);
  82. ptr += cert->len;
  83. }
  84. }
  85. unsigned long read_ipl_report(unsigned long safe_addr)
  86. {
  87. struct ipl_rb_certificates *certs;
  88. struct ipl_rb_components *comps;
  89. struct ipl_pl_hdr *pl_hdr;
  90. struct ipl_rl_hdr *rl_hdr;
  91. struct ipl_rb_hdr *rb_hdr;
  92. unsigned long tmp;
  93. void *rl_end;
  94. /*
  95. * Check if there is a IPL report by looking at the copy
  96. * of the IPL parameter information block.
  97. */
  98. if (!ipl_block_valid ||
  99. !(ipl_block.hdr.flags & IPL_PL_FLAG_IPLSR))
  100. return safe_addr;
  101. ipl_secure_flag = !!(ipl_block.hdr.flags & IPL_PL_FLAG_SIPL);
  102. /*
  103. * There is an IPL report, to find it load the pointer to the
  104. * IPL parameter information block from lowcore and skip past
  105. * the IPL parameter list, then align the address to a double
  106. * word boundary.
  107. */
  108. tmp = (unsigned long) S390_lowcore.ipl_parmblock_ptr;
  109. pl_hdr = (struct ipl_pl_hdr *) tmp;
  110. tmp = (tmp + pl_hdr->len + 7) & -8UL;
  111. rl_hdr = (struct ipl_rl_hdr *) tmp;
  112. /* Walk through the IPL report blocks in the IPL Report list */
  113. certs = NULL;
  114. comps = NULL;
  115. rl_end = (void *) rl_hdr + rl_hdr->len;
  116. rb_hdr = (void *) rl_hdr + sizeof(*rl_hdr);
  117. while ((void *) rb_hdr + sizeof(*rb_hdr) < rl_end &&
  118. (void *) rb_hdr + rb_hdr->len <= rl_end) {
  119. switch (rb_hdr->rbt) {
  120. case IPL_RBT_CERTIFICATES:
  121. certs = (struct ipl_rb_certificates *) rb_hdr;
  122. break;
  123. case IPL_RBT_COMPONENTS:
  124. comps = (struct ipl_rb_components *) rb_hdr;
  125. break;
  126. default:
  127. break;
  128. }
  129. rb_hdr = (void *) rb_hdr + rb_hdr->len;
  130. }
  131. /*
  132. * With either the component list or the certificate list
  133. * missing the kernel will stay ignorant of secure IPL.
  134. */
  135. if (!comps || !certs)
  136. return safe_addr;
  137. /*
  138. * Copy component and certificate list to a safe area
  139. * where the decompressed kernel can find them.
  140. */
  141. safe_addr = find_bootdata_space(comps, certs, safe_addr);
  142. copy_components_bootdata(comps);
  143. copy_certificates_bootdata(certs);
  144. return safe_addr;
  145. }