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