xref: /openbmc/linux/arch/s390/boot/ipl_parm.c (revision 84cc6674)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/init.h>
4 #include <linux/ctype.h>
5 #include <linux/pgtable.h>
6 #include <asm/ebcdic.h>
7 #include <asm/sclp.h>
8 #include <asm/sections.h>
9 #include <asm/boot_data.h>
10 #include <asm/facility.h>
11 #include <asm/setup.h>
12 #include <asm/uv.h>
13 #include "boot.h"
14 
15 struct parmarea parmarea __section(".parmarea") = {
16 	.kernel_version		= (unsigned long)kernel_version,
17 	.max_command_line_size	= COMMAND_LINE_SIZE,
18 	.command_line		= "root=/dev/ram0 ro",
19 };
20 
21 char __bootdata(early_command_line)[COMMAND_LINE_SIZE];
22 int __bootdata(noexec_disabled);
23 
24 unsigned int __bootdata_preserved(zlib_dfltcc_support) = ZLIB_DFLTCC_FULL;
25 struct ipl_parameter_block __bootdata_preserved(ipl_block);
26 int __bootdata_preserved(ipl_block_valid);
27 
28 unsigned long vmalloc_size = VMALLOC_DEFAULT_SIZE;
29 unsigned long memory_limit;
30 int vmalloc_size_set;
31 int kaslr_enabled;
32 
33 static inline int __diag308(unsigned long subcode, void *addr)
34 {
35 	unsigned long reg1, reg2;
36 	union register_pair r1;
37 	psw_t old;
38 
39 	r1.even = (unsigned long) addr;
40 	r1.odd	= 0;
41 	asm volatile(
42 		"	mvc	0(16,%[psw_old]),0(%[psw_pgm])\n"
43 		"	epsw	%[reg1],%[reg2]\n"
44 		"	st	%[reg1],0(%[psw_pgm])\n"
45 		"	st	%[reg2],4(%[psw_pgm])\n"
46 		"	larl	%[reg1],1f\n"
47 		"	stg	%[reg1],8(%[psw_pgm])\n"
48 		"	diag	%[r1],%[subcode],0x308\n"
49 		"1:	mvc	0(16,%[psw_pgm]),0(%[psw_old])\n"
50 		: [r1] "+&d" (r1.pair),
51 		  [reg1] "=&d" (reg1),
52 		  [reg2] "=&a" (reg2),
53 		  "+Q" (S390_lowcore.program_new_psw),
54 		  "=Q" (old)
55 		: [subcode] "d" (subcode),
56 		  [psw_old] "a" (&old),
57 		  [psw_pgm] "a" (&S390_lowcore.program_new_psw)
58 		: "cc", "memory");
59 	return r1.odd;
60 }
61 
62 void store_ipl_parmblock(void)
63 {
64 	int rc;
65 
66 	rc = __diag308(DIAG308_STORE, &ipl_block);
67 	if (rc == DIAG308_RC_OK &&
68 	    ipl_block.hdr.version <= IPL_MAX_SUPPORTED_VERSION)
69 		ipl_block_valid = 1;
70 }
71 
72 bool is_ipl_block_dump(void)
73 {
74 	if (ipl_block.pb0_hdr.pbt == IPL_PBT_FCP &&
75 	    ipl_block.fcp.opt == IPL_PB0_FCP_OPT_DUMP)
76 		return true;
77 	if (ipl_block.pb0_hdr.pbt == IPL_PBT_NVME &&
78 	    ipl_block.nvme.opt == IPL_PB0_NVME_OPT_DUMP)
79 		return true;
80 	if (ipl_block.pb0_hdr.pbt == IPL_PBT_ECKD &&
81 	    ipl_block.eckd.opt == IPL_PB0_ECKD_OPT_DUMP)
82 		return true;
83 	return false;
84 }
85 
86 static size_t scpdata_length(const u8 *buf, size_t count)
87 {
88 	while (count) {
89 		if (buf[count - 1] != '\0' && buf[count - 1] != ' ')
90 			break;
91 		count--;
92 	}
93 	return count;
94 }
95 
96 static size_t ipl_block_get_ascii_scpdata(char *dest, size_t size,
97 					  const struct ipl_parameter_block *ipb)
98 {
99 	const __u8 *scp_data;
100 	__u32 scp_data_len;
101 	int has_lowercase;
102 	size_t count = 0;
103 	size_t i;
104 
105 	switch (ipb->pb0_hdr.pbt) {
106 	case IPL_PBT_FCP:
107 		scp_data_len = ipb->fcp.scp_data_len;
108 		scp_data = ipb->fcp.scp_data;
109 		break;
110 	case IPL_PBT_NVME:
111 		scp_data_len = ipb->nvme.scp_data_len;
112 		scp_data = ipb->nvme.scp_data;
113 		break;
114 	case IPL_PBT_ECKD:
115 		scp_data_len = ipb->eckd.scp_data_len;
116 		scp_data = ipb->eckd.scp_data;
117 		break;
118 
119 	default:
120 		goto out;
121 	}
122 
123 	count = min(size - 1, scpdata_length(scp_data, scp_data_len));
124 	if (!count)
125 		goto out;
126 
127 	has_lowercase = 0;
128 	for (i = 0; i < count; i++) {
129 		if (!isascii(scp_data[i])) {
130 			count = 0;
131 			goto out;
132 		}
133 		if (!has_lowercase && islower(scp_data[i]))
134 			has_lowercase = 1;
135 	}
136 
137 	if (has_lowercase)
138 		memcpy(dest, scp_data, count);
139 	else
140 		for (i = 0; i < count; i++)
141 			dest[i] = tolower(scp_data[i]);
142 out:
143 	dest[count] = '\0';
144 	return count;
145 }
146 
147 static void append_ipl_block_parm(void)
148 {
149 	char *parm, *delim;
150 	size_t len, rc = 0;
151 
152 	len = strlen(early_command_line);
153 
154 	delim = early_command_line + len;    /* '\0' character position */
155 	parm = early_command_line + len + 1; /* append right after '\0' */
156 
157 	switch (ipl_block.pb0_hdr.pbt) {
158 	case IPL_PBT_CCW:
159 		rc = ipl_block_get_ascii_vmparm(
160 			parm, COMMAND_LINE_SIZE - len - 1, &ipl_block);
161 		break;
162 	case IPL_PBT_FCP:
163 	case IPL_PBT_NVME:
164 	case IPL_PBT_ECKD:
165 		rc = ipl_block_get_ascii_scpdata(
166 			parm, COMMAND_LINE_SIZE - len - 1, &ipl_block);
167 		break;
168 	}
169 	if (rc) {
170 		if (*parm == '=')
171 			memmove(early_command_line, parm + 1, rc);
172 		else
173 			*delim = ' '; /* replace '\0' with space */
174 	}
175 }
176 
177 static inline int has_ebcdic_char(const char *str)
178 {
179 	int i;
180 
181 	for (i = 0; str[i]; i++)
182 		if (str[i] & 0x80)
183 			return 1;
184 	return 0;
185 }
186 
187 void setup_boot_command_line(void)
188 {
189 	parmarea.command_line[COMMAND_LINE_SIZE - 1] = 0;
190 	/* convert arch command line to ascii if necessary */
191 	if (has_ebcdic_char(parmarea.command_line))
192 		EBCASC(parmarea.command_line, COMMAND_LINE_SIZE);
193 	/* copy arch command line */
194 	strcpy(early_command_line, strim(parmarea.command_line));
195 
196 	/* append IPL PARM data to the boot command line */
197 	if (!is_prot_virt_guest() && ipl_block_valid)
198 		append_ipl_block_parm();
199 }
200 
201 static void modify_facility(unsigned long nr, bool clear)
202 {
203 	if (clear)
204 		__clear_facility(nr, stfle_fac_list);
205 	else
206 		__set_facility(nr, stfle_fac_list);
207 }
208 
209 static void check_cleared_facilities(void)
210 {
211 	unsigned long als[] = { FACILITIES_ALS };
212 	int i;
213 
214 	for (i = 0; i < ARRAY_SIZE(als); i++) {
215 		if ((stfle_fac_list[i] & als[i]) != als[i]) {
216 			sclp_early_printk("Warning: The Linux kernel requires facilities cleared via command line option\n");
217 			print_missing_facilities();
218 			break;
219 		}
220 	}
221 }
222 
223 static void modify_fac_list(char *str)
224 {
225 	unsigned long val, endval;
226 	char *endp;
227 	bool clear;
228 
229 	while (*str) {
230 		clear = false;
231 		if (*str == '!') {
232 			clear = true;
233 			str++;
234 		}
235 		val = simple_strtoull(str, &endp, 0);
236 		if (str == endp)
237 			break;
238 		str = endp;
239 		if (*str == '-') {
240 			str++;
241 			endval = simple_strtoull(str, &endp, 0);
242 			if (str == endp)
243 				break;
244 			str = endp;
245 			while (val <= endval) {
246 				modify_facility(val, clear);
247 				val++;
248 			}
249 		} else {
250 			modify_facility(val, clear);
251 		}
252 		if (*str != ',')
253 			break;
254 		str++;
255 	}
256 	check_cleared_facilities();
257 }
258 
259 static char command_line_buf[COMMAND_LINE_SIZE];
260 void parse_boot_command_line(void)
261 {
262 	char *param, *val;
263 	bool enabled;
264 	char *args;
265 	int rc;
266 
267 	kaslr_enabled = IS_ENABLED(CONFIG_RANDOMIZE_BASE);
268 	args = strcpy(command_line_buf, early_command_line);
269 	while (*args) {
270 		args = next_arg(args, &param, &val);
271 
272 		if (!strcmp(param, "mem") && val)
273 			memory_limit = round_down(memparse(val, NULL), PAGE_SIZE);
274 
275 		if (!strcmp(param, "vmalloc") && val) {
276 			vmalloc_size = round_up(memparse(val, NULL), PAGE_SIZE);
277 			vmalloc_size_set = 1;
278 		}
279 
280 		if (!strcmp(param, "dfltcc") && val) {
281 			if (!strcmp(val, "off"))
282 				zlib_dfltcc_support = ZLIB_DFLTCC_DISABLED;
283 			else if (!strcmp(val, "on"))
284 				zlib_dfltcc_support = ZLIB_DFLTCC_FULL;
285 			else if (!strcmp(val, "def_only"))
286 				zlib_dfltcc_support = ZLIB_DFLTCC_DEFLATE_ONLY;
287 			else if (!strcmp(val, "inf_only"))
288 				zlib_dfltcc_support = ZLIB_DFLTCC_INFLATE_ONLY;
289 			else if (!strcmp(val, "always"))
290 				zlib_dfltcc_support = ZLIB_DFLTCC_FULL_DEBUG;
291 		}
292 
293 		if (!strcmp(param, "noexec")) {
294 			rc = kstrtobool(val, &enabled);
295 			if (!rc && !enabled)
296 				noexec_disabled = 1;
297 		}
298 
299 		if (!strcmp(param, "facilities") && val)
300 			modify_fac_list(val);
301 
302 		if (!strcmp(param, "nokaslr"))
303 			kaslr_enabled = 0;
304 
305 #if IS_ENABLED(CONFIG_KVM)
306 		if (!strcmp(param, "prot_virt")) {
307 			rc = kstrtobool(val, &enabled);
308 			if (!rc && enabled)
309 				prot_virt_host = 1;
310 		}
311 #endif
312 	}
313 }
314