OSDN Git Service

s390: allow overriding facilities via command line
[uclinux-h8/linux.git] / arch / s390 / boot / ipl_parm.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/init.h>
3 #include <linux/ctype.h>
4 #include <asm/ebcdic.h>
5 #include <asm/sclp.h>
6 #include <asm/sections.h>
7 #include <asm/boot_data.h>
8 #include <asm/facility.h>
9 #include "boot.h"
10
11 char __bootdata(early_command_line)[COMMAND_LINE_SIZE];
12 struct ipl_parameter_block __bootdata(early_ipl_block);
13 int __bootdata(early_ipl_block_valid);
14
15 unsigned long __bootdata(memory_end);
16 int __bootdata(memory_end_set);
17 int __bootdata(noexec_disabled);
18
19 static inline int __diag308(unsigned long subcode, void *addr)
20 {
21         register unsigned long _addr asm("0") = (unsigned long)addr;
22         register unsigned long _rc asm("1") = 0;
23         unsigned long reg1, reg2;
24         psw_t old = S390_lowcore.program_new_psw;
25
26         asm volatile(
27                 "       epsw    %0,%1\n"
28                 "       st      %0,%[psw_pgm]\n"
29                 "       st      %1,%[psw_pgm]+4\n"
30                 "       larl    %0,1f\n"
31                 "       stg     %0,%[psw_pgm]+8\n"
32                 "       diag    %[addr],%[subcode],0x308\n"
33                 "1:     nopr    %%r7\n"
34                 : "=&d" (reg1), "=&a" (reg2),
35                   [psw_pgm] "=Q" (S390_lowcore.program_new_psw),
36                   [addr] "+d" (_addr), "+d" (_rc)
37                 : [subcode] "d" (subcode)
38                 : "cc", "memory");
39         S390_lowcore.program_new_psw = old;
40         return _rc;
41 }
42
43 void store_ipl_parmblock(void)
44 {
45         int rc;
46
47         rc = __diag308(DIAG308_STORE, &early_ipl_block);
48         if (rc == DIAG308_RC_OK &&
49             early_ipl_block.hdr.version <= IPL_MAX_SUPPORTED_VERSION)
50                 early_ipl_block_valid = 1;
51 }
52
53 static size_t scpdata_length(const char *buf, size_t count)
54 {
55         while (count) {
56                 if (buf[count - 1] != '\0' && buf[count - 1] != ' ')
57                         break;
58                 count--;
59         }
60         return count;
61 }
62
63 static size_t ipl_block_get_ascii_scpdata(char *dest, size_t size,
64                                           const struct ipl_parameter_block *ipb)
65 {
66         size_t count;
67         size_t i;
68         int has_lowercase;
69
70         count = min(size - 1, scpdata_length(ipb->ipl_info.fcp.scp_data,
71                                              ipb->ipl_info.fcp.scp_data_len));
72         if (!count)
73                 goto out;
74
75         has_lowercase = 0;
76         for (i = 0; i < count; i++) {
77                 if (!isascii(ipb->ipl_info.fcp.scp_data[i])) {
78                         count = 0;
79                         goto out;
80                 }
81                 if (!has_lowercase && islower(ipb->ipl_info.fcp.scp_data[i]))
82                         has_lowercase = 1;
83         }
84
85         if (has_lowercase)
86                 memcpy(dest, ipb->ipl_info.fcp.scp_data, count);
87         else
88                 for (i = 0; i < count; i++)
89                         dest[i] = tolower(ipb->ipl_info.fcp.scp_data[i]);
90 out:
91         dest[count] = '\0';
92         return count;
93 }
94
95 static void append_ipl_block_parm(void)
96 {
97         char *parm, *delim;
98         size_t len, rc = 0;
99
100         len = strlen(early_command_line);
101
102         delim = early_command_line + len;    /* '\0' character position */
103         parm = early_command_line + len + 1; /* append right after '\0' */
104
105         switch (early_ipl_block.hdr.pbt) {
106         case DIAG308_IPL_TYPE_CCW:
107                 rc = ipl_block_get_ascii_vmparm(
108                         parm, COMMAND_LINE_SIZE - len - 1, &early_ipl_block);
109                 break;
110         case DIAG308_IPL_TYPE_FCP:
111                 rc = ipl_block_get_ascii_scpdata(
112                         parm, COMMAND_LINE_SIZE - len - 1, &early_ipl_block);
113                 break;
114         }
115         if (rc) {
116                 if (*parm == '=')
117                         memmove(early_command_line, parm + 1, rc);
118                 else
119                         *delim = ' '; /* replace '\0' with space */
120         }
121 }
122
123 static inline int has_ebcdic_char(const char *str)
124 {
125         int i;
126
127         for (i = 0; str[i]; i++)
128                 if (str[i] & 0x80)
129                         return 1;
130         return 0;
131 }
132
133 void setup_boot_command_line(void)
134 {
135         COMMAND_LINE[ARCH_COMMAND_LINE_SIZE - 1] = 0;
136         /* convert arch command line to ascii if necessary */
137         if (has_ebcdic_char(COMMAND_LINE))
138                 EBCASC(COMMAND_LINE, ARCH_COMMAND_LINE_SIZE);
139         /* copy arch command line */
140         strcpy(early_command_line, strim(COMMAND_LINE));
141
142         /* append IPL PARM data to the boot command line */
143         if (early_ipl_block_valid)
144                 append_ipl_block_parm();
145 }
146
147 static void modify_facility(unsigned long nr, bool clear)
148 {
149         if (clear)
150                 __clear_facility(nr, S390_lowcore.stfle_fac_list);
151         else
152                 __set_facility(nr, S390_lowcore.stfle_fac_list);
153 }
154
155 static void modify_fac_list(char *str)
156 {
157         unsigned long val, endval;
158         char *endp;
159         bool clear;
160
161         while (*str) {
162                 clear = false;
163                 if (*str == '!') {
164                         clear = true;
165                         str++;
166                 }
167                 val = simple_strtoull(str, &endp, 0);
168                 if (str == endp)
169                         break;
170                 str = endp;
171                 if (*str == '-') {
172                         str++;
173                         endval = simple_strtoull(str, &endp, 0);
174                         if (str == endp)
175                                 break;
176                         str = endp;
177                         while (val <= endval) {
178                                 modify_facility(val, clear);
179                                 val++;
180                         }
181                 } else {
182                         modify_facility(val, clear);
183                 }
184                 if (*str != ',')
185                         break;
186                 str++;
187         }
188 }
189
190 static char command_line_buf[COMMAND_LINE_SIZE] __section(.data);
191 void parse_boot_command_line(void)
192 {
193         char *param, *val;
194         bool enabled;
195         char *args;
196         int rc;
197
198         args = strcpy(command_line_buf, early_command_line);
199         while (*args) {
200                 args = next_arg(args, &param, &val);
201
202                 if (!strcmp(param, "mem")) {
203                         memory_end = memparse(val, NULL);
204                         memory_end_set = 1;
205                 }
206
207                 if (!strcmp(param, "noexec")) {
208                         rc = kstrtobool(val, &enabled);
209                         if (!rc && !enabled)
210                                 noexec_disabled = 1;
211                 }
212
213                 if (!strcmp(param, "facilities"))
214                         modify_fac_list(val);
215         }
216 }
217
218 void setup_memory_end(void)
219 {
220 #ifdef CONFIG_CRASH_DUMP
221         if (!OLDMEM_BASE && early_ipl_block_valid &&
222             early_ipl_block.hdr.pbt == DIAG308_IPL_TYPE_FCP &&
223             early_ipl_block.ipl_info.fcp.opt == DIAG308_IPL_OPT_DUMP) {
224                 if (!sclp_early_get_hsa_size(&memory_end) && memory_end)
225                         memory_end_set = 1;
226         }
227 #endif
228 }