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