1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/init.h>
3 #include <linux/ctype.h>
4 #include <asm/ebcdic.h>
6 #include <asm/sections.h>
7 #include <asm/boot_data.h>
8 #include <asm/physmem_info.h>
9 #include <uapi/asm/ipl.h>
12 int __bootdata_preserved(ipl_secure_flag);
14 unsigned long __bootdata_preserved(ipl_cert_list_addr);
15 unsigned long __bootdata_preserved(ipl_cert_list_size);
17 unsigned long __bootdata(early_ipl_comp_list_addr);
18 unsigned long __bootdata(early_ipl_comp_list_size);
20 static struct ipl_rb_certificates *certs;
21 static struct ipl_rb_components *comps;
22 static bool ipl_report_needs_saving;
24 #define for_each_rb_entry(entry, rb) \
25 for (entry = rb->entries; \
26 (void *) entry + sizeof(*entry) <= (void *) rb + rb->len; \
29 static unsigned long get_cert_comp_list_size(void)
31 struct ipl_rb_certificate_entry *cert;
32 struct ipl_rb_component_entry *comp;
36 * Find the length for the IPL report boot data
38 early_ipl_comp_list_size = 0;
39 for_each_rb_entry(comp, comps)
40 early_ipl_comp_list_size += sizeof(*comp);
41 ipl_cert_list_size = 0;
42 for_each_rb_entry(cert, certs)
43 ipl_cert_list_size += sizeof(unsigned int) + cert->len;
44 return ipl_cert_list_size + early_ipl_comp_list_size;
47 bool ipl_report_certs_intersects(unsigned long addr, unsigned long size,
48 unsigned long *intersection_start)
50 struct ipl_rb_certificate_entry *cert;
52 if (!ipl_report_needs_saving)
55 for_each_rb_entry(cert, certs) {
56 if (intersects(addr, size, cert->addr, cert->len)) {
57 *intersection_start = cert->addr;
64 static void copy_components_bootdata(void)
66 struct ipl_rb_component_entry *comp, *ptr;
68 ptr = (struct ipl_rb_component_entry *) early_ipl_comp_list_addr;
69 for_each_rb_entry(comp, comps)
70 memcpy(ptr++, comp, sizeof(*ptr));
73 static void copy_certificates_bootdata(void)
75 struct ipl_rb_certificate_entry *cert;
78 ptr = (void *) ipl_cert_list_addr;
79 for_each_rb_entry(cert, certs) {
80 *(unsigned int *) ptr = cert->len;
81 ptr += sizeof(unsigned int);
82 memcpy(ptr, (void *) cert->addr, cert->len);
87 int read_ipl_report(void)
89 struct ipl_pl_hdr *pl_hdr;
90 struct ipl_rl_hdr *rl_hdr;
91 struct ipl_rb_hdr *rb_hdr;
96 * Check if there is a IPL report by looking at the copy
97 * of the IPL parameter information block.
99 if (!ipl_block_valid ||
100 !(ipl_block.hdr.flags & IPL_PL_FLAG_IPLSR))
102 ipl_secure_flag = !!(ipl_block.hdr.flags & IPL_PL_FLAG_SIPL);
104 * There is an IPL report, to find it load the pointer to the
105 * IPL parameter information block from lowcore and skip past
106 * the IPL parameter list, then align the address to a double
109 tmp = (unsigned long) S390_lowcore.ipl_parmblock_ptr;
110 pl_hdr = (struct ipl_pl_hdr *) tmp;
111 tmp = (tmp + pl_hdr->len + 7) & -8UL;
112 rl_hdr = (struct ipl_rl_hdr *) tmp;
113 /* Walk through the IPL report blocks in the IPL Report list */
116 rl_end = (void *) rl_hdr + rl_hdr->len;
117 rb_hdr = (void *) rl_hdr + sizeof(*rl_hdr);
118 while ((void *) rb_hdr + sizeof(*rb_hdr) < rl_end &&
119 (void *) rb_hdr + rb_hdr->len <= rl_end) {
121 switch (rb_hdr->rbt) {
122 case IPL_RBT_CERTIFICATES:
123 certs = (struct ipl_rb_certificates *) rb_hdr;
125 case IPL_RBT_COMPONENTS:
126 comps = (struct ipl_rb_components *) rb_hdr;
132 rb_hdr = (void *) rb_hdr + rb_hdr->len;
136 * With either the component list or the certificate list
137 * missing the kernel will stay ignorant of secure IPL.
139 if (!comps || !certs) {
144 ipl_report_needs_saving = true;
145 physmem_reserve(RR_IPLREPORT, (unsigned long)pl_hdr,
146 (unsigned long)rl_end - (unsigned long)pl_hdr);
150 void save_ipl_cert_comp_list(void)
154 if (!ipl_report_needs_saving)
157 size = get_cert_comp_list_size();
158 early_ipl_comp_list_addr = physmem_alloc_top_down(RR_CERT_COMP_LIST, size, sizeof(int));
159 ipl_cert_list_addr = early_ipl_comp_list_addr + early_ipl_comp_list_size;
161 copy_components_bootdata();
162 copy_certificates_bootdata();
163 physmem_free(RR_IPLREPORT);
164 ipl_report_needs_saving = false;