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 <uapi/asm/ipl.h>
11 int __bootdata_preserved(ipl_secure_flag);
13 unsigned long __bootdata_preserved(ipl_cert_list_addr);
14 unsigned long __bootdata_preserved(ipl_cert_list_size);
16 unsigned long __bootdata(early_ipl_comp_list_addr);
17 unsigned long __bootdata(early_ipl_comp_list_size);
19 #define for_each_rb_entry(entry, rb) \
20 for (entry = rb->entries; \
21 (void *) entry + sizeof(*entry) <= (void *) rb + rb->len; \
24 static inline bool intersects(unsigned long addr0, unsigned long size0,
25 unsigned long addr1, unsigned long size1)
27 return addr0 + size0 > addr1 && addr1 + size1 > addr0;
30 static unsigned long find_bootdata_space(struct ipl_rb_components *comps,
31 struct ipl_rb_certificates *certs,
32 unsigned long safe_addr)
34 struct ipl_rb_certificate_entry *cert;
35 struct ipl_rb_component_entry *comp;
39 * Find the length for the IPL report boot data
41 early_ipl_comp_list_size = 0;
42 for_each_rb_entry(comp, comps)
43 early_ipl_comp_list_size += sizeof(*comp);
44 ipl_cert_list_size = 0;
45 for_each_rb_entry(cert, certs)
46 ipl_cert_list_size += sizeof(unsigned int) + cert->len;
47 size = ipl_cert_list_size + early_ipl_comp_list_size;
50 * Start from safe_addr to find a free memory area large
51 * enough for the IPL report boot data. This area is used
52 * for ipl_cert_list_addr/ipl_cert_list_size and
53 * early_ipl_comp_list_addr/early_ipl_comp_list_size. It must
54 * not overlap with any component or any certificate.
57 if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && INITRD_START && INITRD_SIZE &&
58 intersects(INITRD_START, INITRD_SIZE, safe_addr, size))
59 safe_addr = INITRD_START + INITRD_SIZE;
60 if (intersects(safe_addr, size, (unsigned long)comps, comps->len)) {
61 safe_addr = (unsigned long)comps + comps->len;
64 for_each_rb_entry(comp, comps)
65 if (intersects(safe_addr, size, comp->addr, comp->len)) {
66 safe_addr = comp->addr + comp->len;
69 if (intersects(safe_addr, size, (unsigned long)certs, certs->len)) {
70 safe_addr = (unsigned long)certs + certs->len;
73 for_each_rb_entry(cert, certs)
74 if (intersects(safe_addr, size, cert->addr, cert->len)) {
75 safe_addr = cert->addr + cert->len;
78 early_ipl_comp_list_addr = safe_addr;
79 ipl_cert_list_addr = safe_addr + early_ipl_comp_list_size;
81 return safe_addr + size;
84 static void copy_components_bootdata(struct ipl_rb_components *comps)
86 struct ipl_rb_component_entry *comp, *ptr;
88 ptr = (struct ipl_rb_component_entry *) early_ipl_comp_list_addr;
89 for_each_rb_entry(comp, comps)
90 memcpy(ptr++, comp, sizeof(*ptr));
93 static void copy_certificates_bootdata(struct ipl_rb_certificates *certs)
95 struct ipl_rb_certificate_entry *cert;
98 ptr = (void *) ipl_cert_list_addr;
99 for_each_rb_entry(cert, certs) {
100 *(unsigned int *) ptr = cert->len;
101 ptr += sizeof(unsigned int);
102 memcpy(ptr, (void *) cert->addr, cert->len);
107 unsigned long read_ipl_report(unsigned long safe_addr)
109 struct ipl_rb_certificates *certs;
110 struct ipl_rb_components *comps;
111 struct ipl_pl_hdr *pl_hdr;
112 struct ipl_rl_hdr *rl_hdr;
113 struct ipl_rb_hdr *rb_hdr;
118 * Check if there is a IPL report by looking at the copy
119 * of the IPL parameter information block.
121 if (!ipl_block_valid ||
122 !(ipl_block.hdr.flags & IPL_PL_FLAG_IPLSR))
124 ipl_secure_flag = !!(ipl_block.hdr.flags & IPL_PL_FLAG_SIPL);
126 * There is an IPL report, to find it load the pointer to the
127 * IPL parameter information block from lowcore and skip past
128 * the IPL parameter list, then align the address to a double
131 tmp = (unsigned long) S390_lowcore.ipl_parmblock_ptr;
132 pl_hdr = (struct ipl_pl_hdr *) tmp;
133 tmp = (tmp + pl_hdr->len + 7) & -8UL;
134 rl_hdr = (struct ipl_rl_hdr *) tmp;
135 /* Walk through the IPL report blocks in the IPL Report list */
138 rl_end = (void *) rl_hdr + rl_hdr->len;
139 rb_hdr = (void *) rl_hdr + sizeof(*rl_hdr);
140 while ((void *) rb_hdr + sizeof(*rb_hdr) < rl_end &&
141 (void *) rb_hdr + rb_hdr->len <= rl_end) {
143 switch (rb_hdr->rbt) {
144 case IPL_RBT_CERTIFICATES:
145 certs = (struct ipl_rb_certificates *) rb_hdr;
147 case IPL_RBT_COMPONENTS:
148 comps = (struct ipl_rb_components *) rb_hdr;
154 rb_hdr = (void *) rb_hdr + rb_hdr->len;
158 * With either the component list or the certificate list
159 * missing the kernel will stay ignorant of secure IPL.
161 if (!comps || !certs)
165 * Copy component and certificate list to a safe area
166 * where the decompressed kernel can find them.
168 safe_addr = find_bootdata_space(comps, certs, safe_addr);
169 copy_components_bootdata(comps);
170 copy_certificates_bootdata(certs);