xref: /openbmc/linux/arch/s390/boot/ipl_parm.c (revision 87fd22e0)
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 	return false;
81 }
82 
83 static size_t scpdata_length(const u8 *buf, size_t count)
84 {
85 	while (count) {
86 		if (buf[count - 1] != '\0' && buf[count - 1] != ' ')
87 			break;
88 		count--;
89 	}
90 	return count;
91 }
92 
93 static size_t ipl_block_get_ascii_scpdata(char *dest, size_t size,
94 					  const struct ipl_parameter_block *ipb)
95 {
96 	const __u8 *scp_data;
97 	__u32 scp_data_len;
98 	int has_lowercase;
99 	size_t count = 0;
100 	size_t i;
101 
102 	switch (ipb->pb0_hdr.pbt) {
103 	case IPL_PBT_FCP:
104 		scp_data_len = ipb->fcp.scp_data_len;
105 		scp_data = ipb->fcp.scp_data;
106 		break;
107 	case IPL_PBT_NVME:
108 		scp_data_len = ipb->nvme.scp_data_len;
109 		scp_data = ipb->nvme.scp_data;
110 		break;
111 	case IPL_PBT_ECKD:
112 		scp_data_len = ipb->eckd.scp_data_len;
113 		scp_data = ipb->eckd.scp_data;
114 		break;
115 
116 	default:
117 		goto out;
118 	}
119 
120 	count = min(size - 1, scpdata_length(scp_data, scp_data_len));
121 	if (!count)
122 		goto out;
123 
124 	has_lowercase = 0;
125 	for (i = 0; i < count; i++) {
126 		if (!isascii(scp_data[i])) {
127 			count = 0;
128 			goto out;
129 		}
130 		if (!has_lowercase && islower(scp_data[i]))
131 			has_lowercase = 1;
132 	}
133 
134 	if (has_lowercase)
135 		memcpy(dest, scp_data, count);
136 	else
137 		for (i = 0; i < count; i++)
138 			dest[i] = tolower(scp_data[i]);
139 out:
140 	dest[count] = '\0';
141 	return count;
142 }
143 
144 static void append_ipl_block_parm(void)
145 {
146 	char *parm, *delim;
147 	size_t len, rc = 0;
148 
149 	len = strlen(early_command_line);
150 
151 	delim = early_command_line + len;    /* '\0' character position */
152 	parm = early_command_line + len + 1; /* append right after '\0' */
153 
154 	switch (ipl_block.pb0_hdr.pbt) {
155 	case IPL_PBT_CCW:
156 		rc = ipl_block_get_ascii_vmparm(
157 			parm, COMMAND_LINE_SIZE - len - 1, &ipl_block);
158 		break;
159 	case IPL_PBT_FCP:
160 	case IPL_PBT_NVME:
161 	case IPL_PBT_ECKD:
162 		rc = ipl_block_get_ascii_scpdata(
163 			parm, COMMAND_LINE_SIZE - len - 1, &ipl_block);
164 		break;
165 	}
166 	if (rc) {
167 		if (*parm == '=')
168 			memmove(early_command_line, parm + 1, rc);
169 		else
170 			*delim = ' '; /* replace '\0' with space */
171 	}
172 }
173 
174 static inline int has_ebcdic_char(const char *str)
175 {
176 	int i;
177 
178 	for (i = 0; str[i]; i++)
179 		if (str[i] & 0x80)
180 			return 1;
181 	return 0;
182 }
183 
184 void setup_boot_command_line(void)
185 {
186 	parmarea.command_line[COMMAND_LINE_SIZE - 1] = 0;
187 	/* convert arch command line to ascii if necessary */
188 	if (has_ebcdic_char(parmarea.command_line))
189 		EBCASC(parmarea.command_line, COMMAND_LINE_SIZE);
190 	/* copy arch command line */
191 	strcpy(early_command_line, strim(parmarea.command_line));
192 
193 	/* append IPL PARM data to the boot command line */
194 	if (!is_prot_virt_guest() && ipl_block_valid)
195 		append_ipl_block_parm();
196 }
197 
198 static void modify_facility(unsigned long nr, bool clear)
199 {
200 	if (clear)
201 		__clear_facility(nr, stfle_fac_list);
202 	else
203 		__set_facility(nr, stfle_fac_list);
204 }
205 
206 static void check_cleared_facilities(void)
207 {
208 	unsigned long als[] = { FACILITIES_ALS };
209 	int i;
210 
211 	for (i = 0; i < ARRAY_SIZE(als); i++) {
212 		if ((stfle_fac_list[i] & als[i]) != als[i]) {
213 			sclp_early_printk("Warning: The Linux kernel requires facilities cleared via command line option\n");
214 			print_missing_facilities();
215 			break;
216 		}
217 	}
218 }
219 
220 static void modify_fac_list(char *str)
221 {
222 	unsigned long val, endval;
223 	char *endp;
224 	bool clear;
225 
226 	while (*str) {
227 		clear = false;
228 		if (*str == '!') {
229 			clear = true;
230 			str++;
231 		}
232 		val = simple_strtoull(str, &endp, 0);
233 		if (str == endp)
234 			break;
235 		str = endp;
236 		if (*str == '-') {
237 			str++;
238 			endval = simple_strtoull(str, &endp, 0);
239 			if (str == endp)
240 				break;
241 			str = endp;
242 			while (val <= endval) {
243 				modify_facility(val, clear);
244 				val++;
245 			}
246 		} else {
247 			modify_facility(val, clear);
248 		}
249 		if (*str != ',')
250 			break;
251 		str++;
252 	}
253 	check_cleared_facilities();
254 }
255 
256 static char command_line_buf[COMMAND_LINE_SIZE];
257 void parse_boot_command_line(void)
258 {
259 	char *param, *val;
260 	bool enabled;
261 	char *args;
262 	int rc;
263 
264 	kaslr_enabled = IS_ENABLED(CONFIG_RANDOMIZE_BASE);
265 	args = strcpy(command_line_buf, early_command_line);
266 	while (*args) {
267 		args = next_arg(args, &param, &val);
268 
269 		if (!strcmp(param, "mem") && val)
270 			memory_limit = round_down(memparse(val, NULL), PAGE_SIZE);
271 
272 		if (!strcmp(param, "vmalloc") && val) {
273 			vmalloc_size = round_up(memparse(val, NULL), PAGE_SIZE);
274 			vmalloc_size_set = 1;
275 		}
276 
277 		if (!strcmp(param, "dfltcc") && val) {
278 			if (!strcmp(val, "off"))
279 				zlib_dfltcc_support = ZLIB_DFLTCC_DISABLED;
280 			else if (!strcmp(val, "on"))
281 				zlib_dfltcc_support = ZLIB_DFLTCC_FULL;
282 			else if (!strcmp(val, "def_only"))
283 				zlib_dfltcc_support = ZLIB_DFLTCC_DEFLATE_ONLY;
284 			else if (!strcmp(val, "inf_only"))
285 				zlib_dfltcc_support = ZLIB_DFLTCC_INFLATE_ONLY;
286 			else if (!strcmp(val, "always"))
287 				zlib_dfltcc_support = ZLIB_DFLTCC_FULL_DEBUG;
288 		}
289 
290 		if (!strcmp(param, "noexec")) {
291 			rc = kstrtobool(val, &enabled);
292 			if (!rc && !enabled)
293 				noexec_disabled = 1;
294 		}
295 
296 		if (!strcmp(param, "facilities") && val)
297 			modify_fac_list(val);
298 
299 		if (!strcmp(param, "nokaslr"))
300 			kaslr_enabled = 0;
301 
302 #if IS_ENABLED(CONFIG_KVM)
303 		if (!strcmp(param, "prot_virt")) {
304 			rc = kstrtobool(val, &enabled);
305 			if (!rc && enabled)
306 				prot_virt_host = 1;
307 		}
308 #endif
309 	}
310 }
311