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