1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * ipl/reipl/dump support for Linux on s390.
4 *
5 * Copyright IBM Corp. 2005, 2012
6 * Author(s): Michael Holzheu <holzheu@de.ibm.com>
7 * Heiko Carstens <heiko.carstens@de.ibm.com>
8 * Volker Sameske <sameske@de.ibm.com>
9 */
10
11 #include <linux/types.h>
12 #include <linux/export.h>
13 #include <linux/init.h>
14 #include <linux/device.h>
15 #include <linux/delay.h>
16 #include <linux/reboot.h>
17 #include <linux/ctype.h>
18 #include <linux/fs.h>
19 #include <linux/gfp.h>
20 #include <linux/crash_dump.h>
21 #include <linux/debug_locks.h>
22 #include <asm/diag.h>
23 #include <asm/ipl.h>
24 #include <asm/smp.h>
25 #include <asm/setup.h>
26 #include <asm/cpcmd.h>
27 #include <asm/ebcdic.h>
28 #include <asm/sclp.h>
29 #include <asm/checksum.h>
30 #include <asm/debug.h>
31 #include <asm/os_info.h>
32 #include <asm/sections.h>
33 #include <asm/boot_data.h>
34 #include "entry.h"
35
36 #define IPL_PARM_BLOCK_VERSION 0
37
38 #define IPL_UNKNOWN_STR "unknown"
39 #define IPL_CCW_STR "ccw"
40 #define IPL_FCP_STR "fcp"
41 #define IPL_FCP_DUMP_STR "fcp_dump"
42 #define IPL_NSS_STR "nss"
43
44 #define DUMP_CCW_STR "ccw"
45 #define DUMP_FCP_STR "fcp"
46 #define DUMP_NONE_STR "none"
47
48 /*
49 * Four shutdown trigger types are supported:
50 * - panic
51 * - halt
52 * - power off
53 * - reipl
54 * - restart
55 */
56 #define ON_PANIC_STR "on_panic"
57 #define ON_HALT_STR "on_halt"
58 #define ON_POFF_STR "on_poff"
59 #define ON_REIPL_STR "on_reboot"
60 #define ON_RESTART_STR "on_restart"
61
62 struct shutdown_action;
63 struct shutdown_trigger {
64 char *name;
65 struct shutdown_action *action;
66 };
67
68 /*
69 * The following shutdown action types are supported:
70 */
71 #define SHUTDOWN_ACTION_IPL_STR "ipl"
72 #define SHUTDOWN_ACTION_REIPL_STR "reipl"
73 #define SHUTDOWN_ACTION_DUMP_STR "dump"
74 #define SHUTDOWN_ACTION_VMCMD_STR "vmcmd"
75 #define SHUTDOWN_ACTION_STOP_STR "stop"
76 #define SHUTDOWN_ACTION_DUMP_REIPL_STR "dump_reipl"
77
78 struct shutdown_action {
79 char *name;
80 void (*fn) (struct shutdown_trigger *trigger);
81 int (*init) (void);
82 int init_rc;
83 };
84
ipl_type_str(enum ipl_type type)85 static char *ipl_type_str(enum ipl_type type)
86 {
87 switch (type) {
88 case IPL_TYPE_CCW:
89 return IPL_CCW_STR;
90 case IPL_TYPE_FCP:
91 return IPL_FCP_STR;
92 case IPL_TYPE_FCP_DUMP:
93 return IPL_FCP_DUMP_STR;
94 case IPL_TYPE_NSS:
95 return IPL_NSS_STR;
96 case IPL_TYPE_UNKNOWN:
97 default:
98 return IPL_UNKNOWN_STR;
99 }
100 }
101
102 enum dump_type {
103 DUMP_TYPE_NONE = 1,
104 DUMP_TYPE_CCW = 2,
105 DUMP_TYPE_FCP = 4,
106 };
107
dump_type_str(enum dump_type type)108 static char *dump_type_str(enum dump_type type)
109 {
110 switch (type) {
111 case DUMP_TYPE_NONE:
112 return DUMP_NONE_STR;
113 case DUMP_TYPE_CCW:
114 return DUMP_CCW_STR;
115 case DUMP_TYPE_FCP:
116 return DUMP_FCP_STR;
117 default:
118 return NULL;
119 }
120 }
121
122 int __bootdata_preserved(ipl_block_valid);
123 struct ipl_parameter_block __bootdata_preserved(ipl_block);
124 int __bootdata_preserved(ipl_secure_flag);
125
126 unsigned long __bootdata_preserved(ipl_cert_list_addr);
127 unsigned long __bootdata_preserved(ipl_cert_list_size);
128
129 unsigned long __bootdata(early_ipl_comp_list_addr);
130 unsigned long __bootdata(early_ipl_comp_list_size);
131
132 static int reipl_capabilities = IPL_TYPE_UNKNOWN;
133
134 static enum ipl_type reipl_type = IPL_TYPE_UNKNOWN;
135 static struct ipl_parameter_block *reipl_block_fcp;
136 static struct ipl_parameter_block *reipl_block_ccw;
137 static struct ipl_parameter_block *reipl_block_nss;
138 static struct ipl_parameter_block *reipl_block_actual;
139
140 static int dump_capabilities = DUMP_TYPE_NONE;
141 static enum dump_type dump_type = DUMP_TYPE_NONE;
142 static struct ipl_parameter_block *dump_block_fcp;
143 static struct ipl_parameter_block *dump_block_ccw;
144
145 static struct sclp_ipl_info sclp_ipl_info;
146
__diag308(unsigned long subcode,void * addr)147 static inline int __diag308(unsigned long subcode, void *addr)
148 {
149 register unsigned long _addr asm("0") = (unsigned long) addr;
150 register unsigned long _rc asm("1") = 0;
151
152 asm volatile(
153 " diag %0,%2,0x308\n"
154 "0: nopr %%r7\n"
155 EX_TABLE(0b,0b)
156 : "+d" (_addr), "+d" (_rc)
157 : "d" (subcode) : "cc", "memory");
158 return _rc;
159 }
160
diag308(unsigned long subcode,void * addr)161 int diag308(unsigned long subcode, void *addr)
162 {
163 if (IS_ENABLED(CONFIG_KASAN))
164 __arch_local_irq_stosm(0x04); /* enable DAT */
165 diag_stat_inc(DIAG_STAT_X308);
166 return __diag308(subcode, addr);
167 }
168 EXPORT_SYMBOL_GPL(diag308);
169
170 /* SYSFS */
171
172 #define IPL_ATTR_SHOW_FN(_prefix, _name, _format, args...) \
173 static ssize_t sys_##_prefix##_##_name##_show(struct kobject *kobj, \
174 struct kobj_attribute *attr, \
175 char *page) \
176 { \
177 return snprintf(page, PAGE_SIZE, _format, ##args); \
178 }
179
180 #define IPL_ATTR_CCW_STORE_FN(_prefix, _name, _ipl_blk) \
181 static ssize_t sys_##_prefix##_##_name##_store(struct kobject *kobj, \
182 struct kobj_attribute *attr, \
183 const char *buf, size_t len) \
184 { \
185 unsigned long long ssid, devno; \
186 \
187 if (sscanf(buf, "0.%llx.%llx\n", &ssid, &devno) != 2) \
188 return -EINVAL; \
189 \
190 if (ssid > __MAX_SSID || devno > __MAX_SUBCHANNEL) \
191 return -EINVAL; \
192 \
193 _ipl_blk.ssid = ssid; \
194 _ipl_blk.devno = devno; \
195 return len; \
196 }
197
198 #define DEFINE_IPL_CCW_ATTR_RW(_prefix, _name, _ipl_blk) \
199 IPL_ATTR_SHOW_FN(_prefix, _name, "0.%x.%04x\n", \
200 _ipl_blk.ssid, _ipl_blk.devno); \
201 IPL_ATTR_CCW_STORE_FN(_prefix, _name, _ipl_blk); \
202 static struct kobj_attribute sys_##_prefix##_##_name##_attr = \
203 __ATTR(_name, (S_IRUGO | S_IWUSR), \
204 sys_##_prefix##_##_name##_show, \
205 sys_##_prefix##_##_name##_store) \
206
207 #define DEFINE_IPL_ATTR_RO(_prefix, _name, _format, _value) \
208 IPL_ATTR_SHOW_FN(_prefix, _name, _format, _value) \
209 static struct kobj_attribute sys_##_prefix##_##_name##_attr = \
210 __ATTR(_name, S_IRUGO, sys_##_prefix##_##_name##_show, NULL)
211
212 #define DEFINE_IPL_ATTR_RW(_prefix, _name, _fmt_out, _fmt_in, _value) \
213 IPL_ATTR_SHOW_FN(_prefix, _name, _fmt_out, (unsigned long long) _value) \
214 static ssize_t sys_##_prefix##_##_name##_store(struct kobject *kobj, \
215 struct kobj_attribute *attr, \
216 const char *buf, size_t len) \
217 { \
218 unsigned long long value; \
219 if (sscanf(buf, _fmt_in, &value) != 1) \
220 return -EINVAL; \
221 _value = value; \
222 return len; \
223 } \
224 static struct kobj_attribute sys_##_prefix##_##_name##_attr = \
225 __ATTR(_name,(S_IRUGO | S_IWUSR), \
226 sys_##_prefix##_##_name##_show, \
227 sys_##_prefix##_##_name##_store)
228
229 #define DEFINE_IPL_ATTR_STR_RW(_prefix, _name, _fmt_out, _fmt_in, _value)\
230 IPL_ATTR_SHOW_FN(_prefix, _name, _fmt_out, _value) \
231 static ssize_t sys_##_prefix##_##_name##_store(struct kobject *kobj, \
232 struct kobj_attribute *attr, \
233 const char *buf, size_t len) \
234 { \
235 strncpy(_value, buf, sizeof(_value) - 1); \
236 strim(_value); \
237 return len; \
238 } \
239 static struct kobj_attribute sys_##_prefix##_##_name##_attr = \
240 __ATTR(_name,(S_IRUGO | S_IWUSR), \
241 sys_##_prefix##_##_name##_show, \
242 sys_##_prefix##_##_name##_store)
243
244 /*
245 * ipl section
246 */
247
get_ipl_type(void)248 static __init enum ipl_type get_ipl_type(void)
249 {
250 if (!ipl_block_valid)
251 return IPL_TYPE_UNKNOWN;
252
253 switch (ipl_block.pb0_hdr.pbt) {
254 case IPL_PBT_CCW:
255 return IPL_TYPE_CCW;
256 case IPL_PBT_FCP:
257 if (ipl_block.fcp.opt == IPL_PB0_FCP_OPT_DUMP)
258 return IPL_TYPE_FCP_DUMP;
259 else
260 return IPL_TYPE_FCP;
261 }
262 return IPL_TYPE_UNKNOWN;
263 }
264
265 struct ipl_info ipl_info;
266 EXPORT_SYMBOL_GPL(ipl_info);
267
ipl_type_show(struct kobject * kobj,struct kobj_attribute * attr,char * page)268 static ssize_t ipl_type_show(struct kobject *kobj, struct kobj_attribute *attr,
269 char *page)
270 {
271 return sprintf(page, "%s\n", ipl_type_str(ipl_info.type));
272 }
273
274 static struct kobj_attribute sys_ipl_type_attr = __ATTR_RO(ipl_type);
275
ipl_secure_show(struct kobject * kobj,struct kobj_attribute * attr,char * page)276 static ssize_t ipl_secure_show(struct kobject *kobj,
277 struct kobj_attribute *attr, char *page)
278 {
279 return sprintf(page, "%i\n", !!ipl_secure_flag);
280 }
281
282 static struct kobj_attribute sys_ipl_secure_attr =
283 __ATTR(secure, 0444, ipl_secure_show, NULL);
284
ipl_has_secure_show(struct kobject * kobj,struct kobj_attribute * attr,char * page)285 static ssize_t ipl_has_secure_show(struct kobject *kobj,
286 struct kobj_attribute *attr, char *page)
287 {
288 return sprintf(page, "%i\n", !!sclp.has_sipl);
289 }
290
291 static struct kobj_attribute sys_ipl_has_secure_attr =
292 __ATTR(has_secure, 0444, ipl_has_secure_show, NULL);
293
ipl_vm_parm_show(struct kobject * kobj,struct kobj_attribute * attr,char * page)294 static ssize_t ipl_vm_parm_show(struct kobject *kobj,
295 struct kobj_attribute *attr, char *page)
296 {
297 char parm[DIAG308_VMPARM_SIZE + 1] = {};
298
299 if (ipl_block_valid && (ipl_block.pb0_hdr.pbt == IPL_PBT_CCW))
300 ipl_block_get_ascii_vmparm(parm, sizeof(parm), &ipl_block);
301 return sprintf(page, "%s\n", parm);
302 }
303
304 static struct kobj_attribute sys_ipl_vm_parm_attr =
305 __ATTR(parm, S_IRUGO, ipl_vm_parm_show, NULL);
306
sys_ipl_device_show(struct kobject * kobj,struct kobj_attribute * attr,char * page)307 static ssize_t sys_ipl_device_show(struct kobject *kobj,
308 struct kobj_attribute *attr, char *page)
309 {
310 switch (ipl_info.type) {
311 case IPL_TYPE_CCW:
312 return sprintf(page, "0.%x.%04x\n", ipl_block.ccw.ssid,
313 ipl_block.ccw.devno);
314 case IPL_TYPE_FCP:
315 case IPL_TYPE_FCP_DUMP:
316 return sprintf(page, "0.0.%04x\n", ipl_block.fcp.devno);
317 default:
318 return 0;
319 }
320 }
321
322 static struct kobj_attribute sys_ipl_device_attr =
323 __ATTR(device, S_IRUGO, sys_ipl_device_show, NULL);
324
ipl_parameter_read(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t off,size_t count)325 static ssize_t ipl_parameter_read(struct file *filp, struct kobject *kobj,
326 struct bin_attribute *attr, char *buf,
327 loff_t off, size_t count)
328 {
329 return memory_read_from_buffer(buf, count, &off, &ipl_block,
330 ipl_block.hdr.len);
331 }
332 static struct bin_attribute ipl_parameter_attr =
333 __BIN_ATTR(binary_parameter, S_IRUGO, ipl_parameter_read, NULL,
334 PAGE_SIZE);
335
ipl_scp_data_read(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t off,size_t count)336 static ssize_t ipl_scp_data_read(struct file *filp, struct kobject *kobj,
337 struct bin_attribute *attr, char *buf,
338 loff_t off, size_t count)
339 {
340 unsigned int size = ipl_block.fcp.scp_data_len;
341 void *scp_data = &ipl_block.fcp.scp_data;
342
343 return memory_read_from_buffer(buf, count, &off, scp_data, size);
344 }
345 static struct bin_attribute ipl_scp_data_attr =
346 __BIN_ATTR(scp_data, S_IRUGO, ipl_scp_data_read, NULL, PAGE_SIZE);
347
348 static struct bin_attribute *ipl_fcp_bin_attrs[] = {
349 &ipl_parameter_attr,
350 &ipl_scp_data_attr,
351 NULL,
352 };
353
354 /* FCP ipl device attributes */
355
356 DEFINE_IPL_ATTR_RO(ipl_fcp, wwpn, "0x%016llx\n",
357 (unsigned long long)ipl_block.fcp.wwpn);
358 DEFINE_IPL_ATTR_RO(ipl_fcp, lun, "0x%016llx\n",
359 (unsigned long long)ipl_block.fcp.lun);
360 DEFINE_IPL_ATTR_RO(ipl_fcp, bootprog, "%lld\n",
361 (unsigned long long)ipl_block.fcp.bootprog);
362 DEFINE_IPL_ATTR_RO(ipl_fcp, br_lba, "%lld\n",
363 (unsigned long long)ipl_block.fcp.br_lba);
364
ipl_ccw_loadparm_show(struct kobject * kobj,struct kobj_attribute * attr,char * page)365 static ssize_t ipl_ccw_loadparm_show(struct kobject *kobj,
366 struct kobj_attribute *attr, char *page)
367 {
368 char loadparm[LOADPARM_LEN + 1] = {};
369
370 if (!sclp_ipl_info.is_valid)
371 return sprintf(page, "#unknown#\n");
372 memcpy(loadparm, &sclp_ipl_info.loadparm, LOADPARM_LEN);
373 EBCASC(loadparm, LOADPARM_LEN);
374 strim(loadparm);
375 return sprintf(page, "%s\n", loadparm);
376 }
377
378 static struct kobj_attribute sys_ipl_ccw_loadparm_attr =
379 __ATTR(loadparm, 0444, ipl_ccw_loadparm_show, NULL);
380
381 static struct attribute *ipl_fcp_attrs[] = {
382 &sys_ipl_type_attr.attr,
383 &sys_ipl_device_attr.attr,
384 &sys_ipl_fcp_wwpn_attr.attr,
385 &sys_ipl_fcp_lun_attr.attr,
386 &sys_ipl_fcp_bootprog_attr.attr,
387 &sys_ipl_fcp_br_lba_attr.attr,
388 &sys_ipl_ccw_loadparm_attr.attr,
389 &sys_ipl_secure_attr.attr,
390 &sys_ipl_has_secure_attr.attr,
391 NULL,
392 };
393
394 static struct attribute_group ipl_fcp_attr_group = {
395 .attrs = ipl_fcp_attrs,
396 .bin_attrs = ipl_fcp_bin_attrs,
397 };
398
399 /* CCW ipl device attributes */
400
401 static struct attribute *ipl_ccw_attrs_vm[] = {
402 &sys_ipl_type_attr.attr,
403 &sys_ipl_device_attr.attr,
404 &sys_ipl_ccw_loadparm_attr.attr,
405 &sys_ipl_vm_parm_attr.attr,
406 &sys_ipl_secure_attr.attr,
407 &sys_ipl_has_secure_attr.attr,
408 NULL,
409 };
410
411 static struct attribute *ipl_ccw_attrs_lpar[] = {
412 &sys_ipl_type_attr.attr,
413 &sys_ipl_device_attr.attr,
414 &sys_ipl_ccw_loadparm_attr.attr,
415 &sys_ipl_secure_attr.attr,
416 &sys_ipl_has_secure_attr.attr,
417 NULL,
418 };
419
420 static struct attribute_group ipl_ccw_attr_group_vm = {
421 .attrs = ipl_ccw_attrs_vm,
422 };
423
424 static struct attribute_group ipl_ccw_attr_group_lpar = {
425 .attrs = ipl_ccw_attrs_lpar
426 };
427
428 /* UNKNOWN ipl device attributes */
429
430 static struct attribute *ipl_unknown_attrs[] = {
431 &sys_ipl_type_attr.attr,
432 &sys_ipl_secure_attr.attr,
433 &sys_ipl_has_secure_attr.attr,
434 NULL,
435 };
436
437 static struct attribute_group ipl_unknown_attr_group = {
438 .attrs = ipl_unknown_attrs,
439 };
440
441 static struct kset *ipl_kset;
442
__ipl_run(void * unused)443 static void __ipl_run(void *unused)
444 {
445 __bpon();
446 diag308(DIAG308_LOAD_CLEAR, NULL);
447 }
448
ipl_run(struct shutdown_trigger * trigger)449 static void ipl_run(struct shutdown_trigger *trigger)
450 {
451 smp_call_ipl_cpu(__ipl_run, NULL);
452 }
453
ipl_init(void)454 static int __init ipl_init(void)
455 {
456 int rc;
457
458 ipl_kset = kset_create_and_add("ipl", NULL, firmware_kobj);
459 if (!ipl_kset) {
460 rc = -ENOMEM;
461 goto out;
462 }
463 switch (ipl_info.type) {
464 case IPL_TYPE_CCW:
465 if (MACHINE_IS_VM)
466 rc = sysfs_create_group(&ipl_kset->kobj,
467 &ipl_ccw_attr_group_vm);
468 else
469 rc = sysfs_create_group(&ipl_kset->kobj,
470 &ipl_ccw_attr_group_lpar);
471 break;
472 case IPL_TYPE_FCP:
473 case IPL_TYPE_FCP_DUMP:
474 rc = sysfs_create_group(&ipl_kset->kobj, &ipl_fcp_attr_group);
475 break;
476 default:
477 rc = sysfs_create_group(&ipl_kset->kobj,
478 &ipl_unknown_attr_group);
479 break;
480 }
481 out:
482 if (rc)
483 panic("ipl_init failed: rc = %i\n", rc);
484
485 return 0;
486 }
487
488 static struct shutdown_action __refdata ipl_action = {
489 .name = SHUTDOWN_ACTION_IPL_STR,
490 .fn = ipl_run,
491 .init = ipl_init,
492 };
493
494 /*
495 * reipl shutdown action: Reboot Linux on shutdown.
496 */
497
498 /* VM IPL PARM attributes */
reipl_generic_vmparm_show(struct ipl_parameter_block * ipb,char * page)499 static ssize_t reipl_generic_vmparm_show(struct ipl_parameter_block *ipb,
500 char *page)
501 {
502 char vmparm[DIAG308_VMPARM_SIZE + 1] = {};
503
504 ipl_block_get_ascii_vmparm(vmparm, sizeof(vmparm), ipb);
505 return sprintf(page, "%s\n", vmparm);
506 }
507
reipl_generic_vmparm_store(struct ipl_parameter_block * ipb,size_t vmparm_max,const char * buf,size_t len)508 static ssize_t reipl_generic_vmparm_store(struct ipl_parameter_block *ipb,
509 size_t vmparm_max,
510 const char *buf, size_t len)
511 {
512 int i, ip_len;
513
514 /* ignore trailing newline */
515 ip_len = len;
516 if ((len > 0) && (buf[len - 1] == '\n'))
517 ip_len--;
518
519 if (ip_len > vmparm_max)
520 return -EINVAL;
521
522 /* parm is used to store kernel options, check for common chars */
523 for (i = 0; i < ip_len; i++)
524 if (!(isalnum(buf[i]) || isascii(buf[i]) || isprint(buf[i])))
525 return -EINVAL;
526
527 memset(ipb->ccw.vm_parm, 0, DIAG308_VMPARM_SIZE);
528 ipb->ccw.vm_parm_len = ip_len;
529 if (ip_len > 0) {
530 ipb->ccw.vm_flags |= IPL_PB0_CCW_VM_FLAG_VP;
531 memcpy(ipb->ccw.vm_parm, buf, ip_len);
532 ASCEBC(ipb->ccw.vm_parm, ip_len);
533 } else {
534 ipb->ccw.vm_flags &= ~IPL_PB0_CCW_VM_FLAG_VP;
535 }
536
537 return len;
538 }
539
540 /* NSS wrapper */
reipl_nss_vmparm_show(struct kobject * kobj,struct kobj_attribute * attr,char * page)541 static ssize_t reipl_nss_vmparm_show(struct kobject *kobj,
542 struct kobj_attribute *attr, char *page)
543 {
544 return reipl_generic_vmparm_show(reipl_block_nss, page);
545 }
546
reipl_nss_vmparm_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t len)547 static ssize_t reipl_nss_vmparm_store(struct kobject *kobj,
548 struct kobj_attribute *attr,
549 const char *buf, size_t len)
550 {
551 return reipl_generic_vmparm_store(reipl_block_nss, 56, buf, len);
552 }
553
554 /* CCW wrapper */
reipl_ccw_vmparm_show(struct kobject * kobj,struct kobj_attribute * attr,char * page)555 static ssize_t reipl_ccw_vmparm_show(struct kobject *kobj,
556 struct kobj_attribute *attr, char *page)
557 {
558 return reipl_generic_vmparm_show(reipl_block_ccw, page);
559 }
560
reipl_ccw_vmparm_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t len)561 static ssize_t reipl_ccw_vmparm_store(struct kobject *kobj,
562 struct kobj_attribute *attr,
563 const char *buf, size_t len)
564 {
565 return reipl_generic_vmparm_store(reipl_block_ccw, 64, buf, len);
566 }
567
568 static struct kobj_attribute sys_reipl_nss_vmparm_attr =
569 __ATTR(parm, S_IRUGO | S_IWUSR, reipl_nss_vmparm_show,
570 reipl_nss_vmparm_store);
571 static struct kobj_attribute sys_reipl_ccw_vmparm_attr =
572 __ATTR(parm, S_IRUGO | S_IWUSR, reipl_ccw_vmparm_show,
573 reipl_ccw_vmparm_store);
574
575 /* FCP reipl device attributes */
576
reipl_fcp_scpdata_read(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t off,size_t count)577 static ssize_t reipl_fcp_scpdata_read(struct file *filp, struct kobject *kobj,
578 struct bin_attribute *attr,
579 char *buf, loff_t off, size_t count)
580 {
581 size_t size = reipl_block_fcp->fcp.scp_data_len;
582 void *scp_data = reipl_block_fcp->fcp.scp_data;
583
584 return memory_read_from_buffer(buf, count, &off, scp_data, size);
585 }
586
reipl_fcp_scpdata_write(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t off,size_t count)587 static ssize_t reipl_fcp_scpdata_write(struct file *filp, struct kobject *kobj,
588 struct bin_attribute *attr,
589 char *buf, loff_t off, size_t count)
590 {
591 size_t scpdata_len = count;
592 size_t padding;
593
594
595 if (off)
596 return -EINVAL;
597
598 memcpy(reipl_block_fcp->fcp.scp_data, buf, count);
599 if (scpdata_len % 8) {
600 padding = 8 - (scpdata_len % 8);
601 memset(reipl_block_fcp->fcp.scp_data + scpdata_len,
602 0, padding);
603 scpdata_len += padding;
604 }
605
606 reipl_block_fcp->hdr.len = IPL_BP_FCP_LEN + scpdata_len;
607 reipl_block_fcp->fcp.len = IPL_BP0_FCP_LEN + scpdata_len;
608 reipl_block_fcp->fcp.scp_data_len = scpdata_len;
609
610 return count;
611 }
612 static struct bin_attribute sys_reipl_fcp_scp_data_attr =
613 __BIN_ATTR(scp_data, (S_IRUGO | S_IWUSR), reipl_fcp_scpdata_read,
614 reipl_fcp_scpdata_write, DIAG308_SCPDATA_SIZE);
615
616 static struct bin_attribute *reipl_fcp_bin_attrs[] = {
617 &sys_reipl_fcp_scp_data_attr,
618 NULL,
619 };
620
621 DEFINE_IPL_ATTR_RW(reipl_fcp, wwpn, "0x%016llx\n", "%llx\n",
622 reipl_block_fcp->fcp.wwpn);
623 DEFINE_IPL_ATTR_RW(reipl_fcp, lun, "0x%016llx\n", "%llx\n",
624 reipl_block_fcp->fcp.lun);
625 DEFINE_IPL_ATTR_RW(reipl_fcp, bootprog, "%lld\n", "%lld\n",
626 reipl_block_fcp->fcp.bootprog);
627 DEFINE_IPL_ATTR_RW(reipl_fcp, br_lba, "%lld\n", "%lld\n",
628 reipl_block_fcp->fcp.br_lba);
629 DEFINE_IPL_ATTR_RW(reipl_fcp, device, "0.0.%04llx\n", "0.0.%llx\n",
630 reipl_block_fcp->fcp.devno);
631
reipl_get_ascii_loadparm(char * loadparm,struct ipl_parameter_block * ibp)632 static void reipl_get_ascii_loadparm(char *loadparm,
633 struct ipl_parameter_block *ibp)
634 {
635 memcpy(loadparm, ibp->common.loadparm, LOADPARM_LEN);
636 EBCASC(loadparm, LOADPARM_LEN);
637 loadparm[LOADPARM_LEN] = 0;
638 strim(loadparm);
639 }
640
reipl_generic_loadparm_show(struct ipl_parameter_block * ipb,char * page)641 static ssize_t reipl_generic_loadparm_show(struct ipl_parameter_block *ipb,
642 char *page)
643 {
644 char buf[LOADPARM_LEN + 1];
645
646 reipl_get_ascii_loadparm(buf, ipb);
647 return sprintf(page, "%s\n", buf);
648 }
649
reipl_generic_loadparm_store(struct ipl_parameter_block * ipb,const char * buf,size_t len)650 static ssize_t reipl_generic_loadparm_store(struct ipl_parameter_block *ipb,
651 const char *buf, size_t len)
652 {
653 int i, lp_len;
654
655 /* ignore trailing newline */
656 lp_len = len;
657 if ((len > 0) && (buf[len - 1] == '\n'))
658 lp_len--;
659 /* loadparm can have max 8 characters and must not start with a blank */
660 if ((lp_len > LOADPARM_LEN) || ((lp_len > 0) && (buf[0] == ' ')))
661 return -EINVAL;
662 /* loadparm can only contain "a-z,A-Z,0-9,SP,." */
663 for (i = 0; i < lp_len; i++) {
664 if (isalpha(buf[i]) || isdigit(buf[i]) || (buf[i] == ' ') ||
665 (buf[i] == '.'))
666 continue;
667 return -EINVAL;
668 }
669 /* initialize loadparm with blanks */
670 memset(ipb->common.loadparm, ' ', LOADPARM_LEN);
671 /* copy and convert to ebcdic */
672 memcpy(ipb->common.loadparm, buf, lp_len);
673 ASCEBC(ipb->common.loadparm, LOADPARM_LEN);
674 ipb->common.flags |= IPL_PB0_FLAG_LOADPARM;
675 return len;
676 }
677
678 /* FCP wrapper */
reipl_fcp_loadparm_show(struct kobject * kobj,struct kobj_attribute * attr,char * page)679 static ssize_t reipl_fcp_loadparm_show(struct kobject *kobj,
680 struct kobj_attribute *attr, char *page)
681 {
682 return reipl_generic_loadparm_show(reipl_block_fcp, page);
683 }
684
reipl_fcp_loadparm_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t len)685 static ssize_t reipl_fcp_loadparm_store(struct kobject *kobj,
686 struct kobj_attribute *attr,
687 const char *buf, size_t len)
688 {
689 return reipl_generic_loadparm_store(reipl_block_fcp, buf, len);
690 }
691
692 static struct kobj_attribute sys_reipl_fcp_loadparm_attr =
693 __ATTR(loadparm, S_IRUGO | S_IWUSR, reipl_fcp_loadparm_show,
694 reipl_fcp_loadparm_store);
695
696 static struct attribute *reipl_fcp_attrs[] = {
697 &sys_reipl_fcp_device_attr.attr,
698 &sys_reipl_fcp_wwpn_attr.attr,
699 &sys_reipl_fcp_lun_attr.attr,
700 &sys_reipl_fcp_bootprog_attr.attr,
701 &sys_reipl_fcp_br_lba_attr.attr,
702 &sys_reipl_fcp_loadparm_attr.attr,
703 NULL,
704 };
705
706 static struct attribute_group reipl_fcp_attr_group = {
707 .attrs = reipl_fcp_attrs,
708 .bin_attrs = reipl_fcp_bin_attrs,
709 };
710
711 /* CCW reipl device attributes */
712 DEFINE_IPL_CCW_ATTR_RW(reipl_ccw, device, reipl_block_ccw->ccw);
713
714 /* NSS wrapper */
reipl_nss_loadparm_show(struct kobject * kobj,struct kobj_attribute * attr,char * page)715 static ssize_t reipl_nss_loadparm_show(struct kobject *kobj,
716 struct kobj_attribute *attr, char *page)
717 {
718 return reipl_generic_loadparm_show(reipl_block_nss, page);
719 }
720
reipl_nss_loadparm_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t len)721 static ssize_t reipl_nss_loadparm_store(struct kobject *kobj,
722 struct kobj_attribute *attr,
723 const char *buf, size_t len)
724 {
725 return reipl_generic_loadparm_store(reipl_block_nss, buf, len);
726 }
727
728 /* CCW wrapper */
reipl_ccw_loadparm_show(struct kobject * kobj,struct kobj_attribute * attr,char * page)729 static ssize_t reipl_ccw_loadparm_show(struct kobject *kobj,
730 struct kobj_attribute *attr, char *page)
731 {
732 return reipl_generic_loadparm_show(reipl_block_ccw, page);
733 }
734
reipl_ccw_loadparm_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t len)735 static ssize_t reipl_ccw_loadparm_store(struct kobject *kobj,
736 struct kobj_attribute *attr,
737 const char *buf, size_t len)
738 {
739 return reipl_generic_loadparm_store(reipl_block_ccw, buf, len);
740 }
741
742 static struct kobj_attribute sys_reipl_ccw_loadparm_attr =
743 __ATTR(loadparm, S_IRUGO | S_IWUSR, reipl_ccw_loadparm_show,
744 reipl_ccw_loadparm_store);
745
746 static struct attribute *reipl_ccw_attrs_vm[] = {
747 &sys_reipl_ccw_device_attr.attr,
748 &sys_reipl_ccw_loadparm_attr.attr,
749 &sys_reipl_ccw_vmparm_attr.attr,
750 NULL,
751 };
752
753 static struct attribute *reipl_ccw_attrs_lpar[] = {
754 &sys_reipl_ccw_device_attr.attr,
755 &sys_reipl_ccw_loadparm_attr.attr,
756 NULL,
757 };
758
759 static struct attribute_group reipl_ccw_attr_group_vm = {
760 .name = IPL_CCW_STR,
761 .attrs = reipl_ccw_attrs_vm,
762 };
763
764 static struct attribute_group reipl_ccw_attr_group_lpar = {
765 .name = IPL_CCW_STR,
766 .attrs = reipl_ccw_attrs_lpar,
767 };
768
769
770 /* NSS reipl device attributes */
reipl_get_ascii_nss_name(char * dst,struct ipl_parameter_block * ipb)771 static void reipl_get_ascii_nss_name(char *dst,
772 struct ipl_parameter_block *ipb)
773 {
774 memcpy(dst, ipb->ccw.nss_name, NSS_NAME_SIZE);
775 EBCASC(dst, NSS_NAME_SIZE);
776 dst[NSS_NAME_SIZE] = 0;
777 }
778
reipl_nss_name_show(struct kobject * kobj,struct kobj_attribute * attr,char * page)779 static ssize_t reipl_nss_name_show(struct kobject *kobj,
780 struct kobj_attribute *attr, char *page)
781 {
782 char nss_name[NSS_NAME_SIZE + 1] = {};
783
784 reipl_get_ascii_nss_name(nss_name, reipl_block_nss);
785 return sprintf(page, "%s\n", nss_name);
786 }
787
reipl_nss_name_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t len)788 static ssize_t reipl_nss_name_store(struct kobject *kobj,
789 struct kobj_attribute *attr,
790 const char *buf, size_t len)
791 {
792 int nss_len;
793
794 /* ignore trailing newline */
795 nss_len = len;
796 if ((len > 0) && (buf[len - 1] == '\n'))
797 nss_len--;
798
799 if (nss_len > NSS_NAME_SIZE)
800 return -EINVAL;
801
802 memset(reipl_block_nss->ccw.nss_name, 0x40, NSS_NAME_SIZE);
803 if (nss_len > 0) {
804 reipl_block_nss->ccw.vm_flags |= IPL_PB0_CCW_VM_FLAG_NSS;
805 memcpy(reipl_block_nss->ccw.nss_name, buf, nss_len);
806 ASCEBC(reipl_block_nss->ccw.nss_name, nss_len);
807 EBC_TOUPPER(reipl_block_nss->ccw.nss_name, nss_len);
808 } else {
809 reipl_block_nss->ccw.vm_flags &= ~IPL_PB0_CCW_VM_FLAG_NSS;
810 }
811
812 return len;
813 }
814
815 static struct kobj_attribute sys_reipl_nss_name_attr =
816 __ATTR(name, S_IRUGO | S_IWUSR, reipl_nss_name_show,
817 reipl_nss_name_store);
818
819 static struct kobj_attribute sys_reipl_nss_loadparm_attr =
820 __ATTR(loadparm, S_IRUGO | S_IWUSR, reipl_nss_loadparm_show,
821 reipl_nss_loadparm_store);
822
823 static struct attribute *reipl_nss_attrs[] = {
824 &sys_reipl_nss_name_attr.attr,
825 &sys_reipl_nss_loadparm_attr.attr,
826 &sys_reipl_nss_vmparm_attr.attr,
827 NULL,
828 };
829
830 static struct attribute_group reipl_nss_attr_group = {
831 .name = IPL_NSS_STR,
832 .attrs = reipl_nss_attrs,
833 };
834
set_os_info_reipl_block(void)835 void set_os_info_reipl_block(void)
836 {
837 os_info_entry_add(OS_INFO_REIPL_BLOCK, reipl_block_actual,
838 reipl_block_actual->hdr.len);
839 }
840
841 /* reipl type */
842
reipl_set_type(enum ipl_type type)843 static int reipl_set_type(enum ipl_type type)
844 {
845 if (!(reipl_capabilities & type))
846 return -EINVAL;
847
848 switch(type) {
849 case IPL_TYPE_CCW:
850 reipl_block_actual = reipl_block_ccw;
851 break;
852 case IPL_TYPE_FCP:
853 reipl_block_actual = reipl_block_fcp;
854 break;
855 case IPL_TYPE_NSS:
856 reipl_block_actual = reipl_block_nss;
857 break;
858 default:
859 break;
860 }
861 reipl_type = type;
862 return 0;
863 }
864
reipl_type_show(struct kobject * kobj,struct kobj_attribute * attr,char * page)865 static ssize_t reipl_type_show(struct kobject *kobj,
866 struct kobj_attribute *attr, char *page)
867 {
868 return sprintf(page, "%s\n", ipl_type_str(reipl_type));
869 }
870
reipl_type_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t len)871 static ssize_t reipl_type_store(struct kobject *kobj,
872 struct kobj_attribute *attr,
873 const char *buf, size_t len)
874 {
875 int rc = -EINVAL;
876
877 if (strncmp(buf, IPL_CCW_STR, strlen(IPL_CCW_STR)) == 0)
878 rc = reipl_set_type(IPL_TYPE_CCW);
879 else if (strncmp(buf, IPL_FCP_STR, strlen(IPL_FCP_STR)) == 0)
880 rc = reipl_set_type(IPL_TYPE_FCP);
881 else if (strncmp(buf, IPL_NSS_STR, strlen(IPL_NSS_STR)) == 0)
882 rc = reipl_set_type(IPL_TYPE_NSS);
883 return (rc != 0) ? rc : len;
884 }
885
886 static struct kobj_attribute reipl_type_attr =
887 __ATTR(reipl_type, 0644, reipl_type_show, reipl_type_store);
888
889 static struct kset *reipl_kset;
890 static struct kset *reipl_fcp_kset;
891
__reipl_run(void * unused)892 static void __reipl_run(void *unused)
893 {
894 switch (reipl_type) {
895 case IPL_TYPE_CCW:
896 diag308(DIAG308_SET, reipl_block_ccw);
897 diag308(DIAG308_LOAD_CLEAR, NULL);
898 break;
899 case IPL_TYPE_FCP:
900 diag308(DIAG308_SET, reipl_block_fcp);
901 diag308(DIAG308_LOAD_CLEAR, NULL);
902 break;
903 case IPL_TYPE_NSS:
904 diag308(DIAG308_SET, reipl_block_nss);
905 diag308(DIAG308_LOAD_CLEAR, NULL);
906 break;
907 case IPL_TYPE_UNKNOWN:
908 diag308(DIAG308_LOAD_CLEAR, NULL);
909 break;
910 case IPL_TYPE_FCP_DUMP:
911 break;
912 }
913 disabled_wait();
914 }
915
reipl_run(struct shutdown_trigger * trigger)916 static void reipl_run(struct shutdown_trigger *trigger)
917 {
918 smp_call_ipl_cpu(__reipl_run, NULL);
919 }
920
reipl_block_ccw_init(struct ipl_parameter_block * ipb)921 static void reipl_block_ccw_init(struct ipl_parameter_block *ipb)
922 {
923 ipb->hdr.len = IPL_BP_CCW_LEN;
924 ipb->hdr.version = IPL_PARM_BLOCK_VERSION;
925 ipb->pb0_hdr.len = IPL_BP0_CCW_LEN;
926 ipb->pb0_hdr.pbt = IPL_PBT_CCW;
927 }
928
reipl_block_ccw_fill_parms(struct ipl_parameter_block * ipb)929 static void reipl_block_ccw_fill_parms(struct ipl_parameter_block *ipb)
930 {
931 /* LOADPARM */
932 /* check if read scp info worked and set loadparm */
933 if (sclp_ipl_info.is_valid)
934 memcpy(ipb->ccw.loadparm, &sclp_ipl_info.loadparm, LOADPARM_LEN);
935 else
936 /* read scp info failed: set empty loadparm (EBCDIC blanks) */
937 memset(ipb->ccw.loadparm, 0x40, LOADPARM_LEN);
938 ipb->ccw.flags = IPL_PB0_FLAG_LOADPARM;
939
940 /* VM PARM */
941 if (MACHINE_IS_VM && ipl_block_valid &&
942 (ipl_block.ccw.vm_flags & IPL_PB0_CCW_VM_FLAG_VP)) {
943
944 ipb->ccw.vm_flags |= IPL_PB0_CCW_VM_FLAG_VP;
945 ipb->ccw.vm_parm_len = ipl_block.ccw.vm_parm_len;
946 memcpy(ipb->ccw.vm_parm,
947 ipl_block.ccw.vm_parm, DIAG308_VMPARM_SIZE);
948 }
949 }
950
reipl_nss_init(void)951 static int __init reipl_nss_init(void)
952 {
953 int rc;
954
955 if (!MACHINE_IS_VM)
956 return 0;
957
958 reipl_block_nss = (void *) get_zeroed_page(GFP_KERNEL);
959 if (!reipl_block_nss)
960 return -ENOMEM;
961
962 rc = sysfs_create_group(&reipl_kset->kobj, &reipl_nss_attr_group);
963 if (rc)
964 return rc;
965
966 reipl_block_ccw_init(reipl_block_nss);
967 reipl_capabilities |= IPL_TYPE_NSS;
968 return 0;
969 }
970
reipl_ccw_init(void)971 static int __init reipl_ccw_init(void)
972 {
973 int rc;
974
975 reipl_block_ccw = (void *) get_zeroed_page(GFP_KERNEL);
976 if (!reipl_block_ccw)
977 return -ENOMEM;
978
979 rc = sysfs_create_group(&reipl_kset->kobj,
980 MACHINE_IS_VM ? &reipl_ccw_attr_group_vm
981 : &reipl_ccw_attr_group_lpar);
982 if (rc)
983 return rc;
984
985 reipl_block_ccw_init(reipl_block_ccw);
986 if (ipl_info.type == IPL_TYPE_CCW) {
987 reipl_block_ccw->ccw.ssid = ipl_block.ccw.ssid;
988 reipl_block_ccw->ccw.devno = ipl_block.ccw.devno;
989 reipl_block_ccw_fill_parms(reipl_block_ccw);
990 }
991
992 reipl_capabilities |= IPL_TYPE_CCW;
993 return 0;
994 }
995
reipl_fcp_init(void)996 static int __init reipl_fcp_init(void)
997 {
998 int rc;
999
1000 reipl_block_fcp = (void *) get_zeroed_page(GFP_KERNEL);
1001 if (!reipl_block_fcp)
1002 return -ENOMEM;
1003
1004 /* sysfs: create fcp kset for mixing attr group and bin attrs */
1005 reipl_fcp_kset = kset_create_and_add(IPL_FCP_STR, NULL,
1006 &reipl_kset->kobj);
1007 if (!reipl_fcp_kset) {
1008 free_page((unsigned long) reipl_block_fcp);
1009 return -ENOMEM;
1010 }
1011
1012 rc = sysfs_create_group(&reipl_fcp_kset->kobj, &reipl_fcp_attr_group);
1013 if (rc) {
1014 kset_unregister(reipl_fcp_kset);
1015 free_page((unsigned long) reipl_block_fcp);
1016 return rc;
1017 }
1018
1019 if (ipl_info.type == IPL_TYPE_FCP) {
1020 memcpy(reipl_block_fcp, &ipl_block, sizeof(ipl_block));
1021 /*
1022 * Fix loadparm: There are systems where the (SCSI) LOADPARM
1023 * is invalid in the SCSI IPL parameter block, so take it
1024 * always from sclp_ipl_info.
1025 */
1026 memcpy(reipl_block_fcp->fcp.loadparm, sclp_ipl_info.loadparm,
1027 LOADPARM_LEN);
1028 } else {
1029 reipl_block_fcp->hdr.len = IPL_BP_FCP_LEN;
1030 reipl_block_fcp->hdr.version = IPL_PARM_BLOCK_VERSION;
1031 reipl_block_fcp->fcp.len = IPL_BP0_FCP_LEN;
1032 reipl_block_fcp->fcp.pbt = IPL_PBT_FCP;
1033 reipl_block_fcp->fcp.opt = IPL_PB0_FCP_OPT_IPL;
1034 }
1035 reipl_capabilities |= IPL_TYPE_FCP;
1036 return 0;
1037 }
1038
reipl_type_init(void)1039 static int __init reipl_type_init(void)
1040 {
1041 enum ipl_type reipl_type = ipl_info.type;
1042 struct ipl_parameter_block *reipl_block;
1043 unsigned long size;
1044
1045 reipl_block = os_info_old_entry(OS_INFO_REIPL_BLOCK, &size);
1046 if (!reipl_block)
1047 goto out;
1048 /*
1049 * If we have an OS info reipl block, this will be used
1050 */
1051 if (reipl_block->pb0_hdr.pbt == IPL_PBT_FCP) {
1052 memcpy(reipl_block_fcp, reipl_block, size);
1053 reipl_type = IPL_TYPE_FCP;
1054 } else if (reipl_block->pb0_hdr.pbt == IPL_PBT_CCW) {
1055 memcpy(reipl_block_ccw, reipl_block, size);
1056 reipl_type = IPL_TYPE_CCW;
1057 }
1058 out:
1059 return reipl_set_type(reipl_type);
1060 }
1061
reipl_init(void)1062 static int __init reipl_init(void)
1063 {
1064 int rc;
1065
1066 reipl_kset = kset_create_and_add("reipl", NULL, firmware_kobj);
1067 if (!reipl_kset)
1068 return -ENOMEM;
1069 rc = sysfs_create_file(&reipl_kset->kobj, &reipl_type_attr.attr);
1070 if (rc) {
1071 kset_unregister(reipl_kset);
1072 return rc;
1073 }
1074 rc = reipl_ccw_init();
1075 if (rc)
1076 return rc;
1077 rc = reipl_fcp_init();
1078 if (rc)
1079 return rc;
1080 rc = reipl_nss_init();
1081 if (rc)
1082 return rc;
1083 return reipl_type_init();
1084 }
1085
1086 static struct shutdown_action __refdata reipl_action = {
1087 .name = SHUTDOWN_ACTION_REIPL_STR,
1088 .fn = reipl_run,
1089 .init = reipl_init,
1090 };
1091
1092 /*
1093 * dump shutdown action: Dump Linux on shutdown.
1094 */
1095
1096 /* FCP dump device attributes */
1097
1098 DEFINE_IPL_ATTR_RW(dump_fcp, wwpn, "0x%016llx\n", "%llx\n",
1099 dump_block_fcp->fcp.wwpn);
1100 DEFINE_IPL_ATTR_RW(dump_fcp, lun, "0x%016llx\n", "%llx\n",
1101 dump_block_fcp->fcp.lun);
1102 DEFINE_IPL_ATTR_RW(dump_fcp, bootprog, "%lld\n", "%lld\n",
1103 dump_block_fcp->fcp.bootprog);
1104 DEFINE_IPL_ATTR_RW(dump_fcp, br_lba, "%lld\n", "%lld\n",
1105 dump_block_fcp->fcp.br_lba);
1106 DEFINE_IPL_ATTR_RW(dump_fcp, device, "0.0.%04llx\n", "0.0.%llx\n",
1107 dump_block_fcp->fcp.devno);
1108
1109 static struct attribute *dump_fcp_attrs[] = {
1110 &sys_dump_fcp_device_attr.attr,
1111 &sys_dump_fcp_wwpn_attr.attr,
1112 &sys_dump_fcp_lun_attr.attr,
1113 &sys_dump_fcp_bootprog_attr.attr,
1114 &sys_dump_fcp_br_lba_attr.attr,
1115 NULL,
1116 };
1117
1118 static struct attribute_group dump_fcp_attr_group = {
1119 .name = IPL_FCP_STR,
1120 .attrs = dump_fcp_attrs,
1121 };
1122
1123 /* CCW dump device attributes */
1124 DEFINE_IPL_CCW_ATTR_RW(dump_ccw, device, dump_block_ccw->ccw);
1125
1126 static struct attribute *dump_ccw_attrs[] = {
1127 &sys_dump_ccw_device_attr.attr,
1128 NULL,
1129 };
1130
1131 static struct attribute_group dump_ccw_attr_group = {
1132 .name = IPL_CCW_STR,
1133 .attrs = dump_ccw_attrs,
1134 };
1135
1136 /* dump type */
1137
dump_set_type(enum dump_type type)1138 static int dump_set_type(enum dump_type type)
1139 {
1140 if (!(dump_capabilities & type))
1141 return -EINVAL;
1142 dump_type = type;
1143 return 0;
1144 }
1145
dump_type_show(struct kobject * kobj,struct kobj_attribute * attr,char * page)1146 static ssize_t dump_type_show(struct kobject *kobj,
1147 struct kobj_attribute *attr, char *page)
1148 {
1149 return sprintf(page, "%s\n", dump_type_str(dump_type));
1150 }
1151
dump_type_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t len)1152 static ssize_t dump_type_store(struct kobject *kobj,
1153 struct kobj_attribute *attr,
1154 const char *buf, size_t len)
1155 {
1156 int rc = -EINVAL;
1157
1158 if (strncmp(buf, DUMP_NONE_STR, strlen(DUMP_NONE_STR)) == 0)
1159 rc = dump_set_type(DUMP_TYPE_NONE);
1160 else if (strncmp(buf, DUMP_CCW_STR, strlen(DUMP_CCW_STR)) == 0)
1161 rc = dump_set_type(DUMP_TYPE_CCW);
1162 else if (strncmp(buf, DUMP_FCP_STR, strlen(DUMP_FCP_STR)) == 0)
1163 rc = dump_set_type(DUMP_TYPE_FCP);
1164 return (rc != 0) ? rc : len;
1165 }
1166
1167 static struct kobj_attribute dump_type_attr =
1168 __ATTR(dump_type, 0644, dump_type_show, dump_type_store);
1169
1170 static struct kset *dump_kset;
1171
diag308_dump(void * dump_block)1172 static void diag308_dump(void *dump_block)
1173 {
1174 diag308(DIAG308_SET, dump_block);
1175 while (1) {
1176 if (diag308(DIAG308_LOAD_NORMAL_DUMP, NULL) != 0x302)
1177 break;
1178 udelay_simple(USEC_PER_SEC);
1179 }
1180 }
1181
__dump_run(void * unused)1182 static void __dump_run(void *unused)
1183 {
1184 switch (dump_type) {
1185 case DUMP_TYPE_CCW:
1186 diag308_dump(dump_block_ccw);
1187 break;
1188 case DUMP_TYPE_FCP:
1189 diag308_dump(dump_block_fcp);
1190 break;
1191 default:
1192 break;
1193 }
1194 }
1195
dump_run(struct shutdown_trigger * trigger)1196 static void dump_run(struct shutdown_trigger *trigger)
1197 {
1198 if (dump_type == DUMP_TYPE_NONE)
1199 return;
1200 smp_send_stop();
1201 smp_call_ipl_cpu(__dump_run, NULL);
1202 }
1203
dump_ccw_init(void)1204 static int __init dump_ccw_init(void)
1205 {
1206 int rc;
1207
1208 dump_block_ccw = (void *) get_zeroed_page(GFP_KERNEL);
1209 if (!dump_block_ccw)
1210 return -ENOMEM;
1211 rc = sysfs_create_group(&dump_kset->kobj, &dump_ccw_attr_group);
1212 if (rc) {
1213 free_page((unsigned long)dump_block_ccw);
1214 return rc;
1215 }
1216 dump_block_ccw->hdr.len = IPL_BP_CCW_LEN;
1217 dump_block_ccw->hdr.version = IPL_PARM_BLOCK_VERSION;
1218 dump_block_ccw->ccw.len = IPL_BP0_CCW_LEN;
1219 dump_block_ccw->ccw.pbt = IPL_PBT_CCW;
1220 dump_capabilities |= DUMP_TYPE_CCW;
1221 return 0;
1222 }
1223
dump_fcp_init(void)1224 static int __init dump_fcp_init(void)
1225 {
1226 int rc;
1227
1228 if (!sclp_ipl_info.has_dump)
1229 return 0; /* LDIPL DUMP is not installed */
1230 dump_block_fcp = (void *) get_zeroed_page(GFP_KERNEL);
1231 if (!dump_block_fcp)
1232 return -ENOMEM;
1233 rc = sysfs_create_group(&dump_kset->kobj, &dump_fcp_attr_group);
1234 if (rc) {
1235 free_page((unsigned long)dump_block_fcp);
1236 return rc;
1237 }
1238 dump_block_fcp->hdr.len = IPL_BP_FCP_LEN;
1239 dump_block_fcp->hdr.version = IPL_PARM_BLOCK_VERSION;
1240 dump_block_fcp->fcp.len = IPL_BP0_FCP_LEN;
1241 dump_block_fcp->fcp.pbt = IPL_PBT_FCP;
1242 dump_block_fcp->fcp.opt = IPL_PB0_FCP_OPT_DUMP;
1243 dump_capabilities |= DUMP_TYPE_FCP;
1244 return 0;
1245 }
1246
dump_init(void)1247 static int __init dump_init(void)
1248 {
1249 int rc;
1250
1251 dump_kset = kset_create_and_add("dump", NULL, firmware_kobj);
1252 if (!dump_kset)
1253 return -ENOMEM;
1254 rc = sysfs_create_file(&dump_kset->kobj, &dump_type_attr.attr);
1255 if (rc) {
1256 kset_unregister(dump_kset);
1257 return rc;
1258 }
1259 rc = dump_ccw_init();
1260 if (rc)
1261 return rc;
1262 rc = dump_fcp_init();
1263 if (rc)
1264 return rc;
1265 dump_set_type(DUMP_TYPE_NONE);
1266 return 0;
1267 }
1268
1269 static struct shutdown_action __refdata dump_action = {
1270 .name = SHUTDOWN_ACTION_DUMP_STR,
1271 .fn = dump_run,
1272 .init = dump_init,
1273 };
1274
dump_reipl_run(struct shutdown_trigger * trigger)1275 static void dump_reipl_run(struct shutdown_trigger *trigger)
1276 {
1277 unsigned long ipib = (unsigned long) reipl_block_actual;
1278 unsigned int csum;
1279
1280 csum = (__force unsigned int)
1281 csum_partial(reipl_block_actual, reipl_block_actual->hdr.len, 0);
1282 mem_assign_absolute(S390_lowcore.ipib, ipib);
1283 mem_assign_absolute(S390_lowcore.ipib_checksum, csum);
1284 dump_run(trigger);
1285 }
1286
1287 static struct shutdown_action __refdata dump_reipl_action = {
1288 .name = SHUTDOWN_ACTION_DUMP_REIPL_STR,
1289 .fn = dump_reipl_run,
1290 };
1291
1292 /*
1293 * vmcmd shutdown action: Trigger vm command on shutdown.
1294 */
1295
1296 static char vmcmd_on_reboot[128];
1297 static char vmcmd_on_panic[128];
1298 static char vmcmd_on_halt[128];
1299 static char vmcmd_on_poff[128];
1300 static char vmcmd_on_restart[128];
1301
1302 DEFINE_IPL_ATTR_STR_RW(vmcmd, on_reboot, "%s\n", "%s\n", vmcmd_on_reboot);
1303 DEFINE_IPL_ATTR_STR_RW(vmcmd, on_panic, "%s\n", "%s\n", vmcmd_on_panic);
1304 DEFINE_IPL_ATTR_STR_RW(vmcmd, on_halt, "%s\n", "%s\n", vmcmd_on_halt);
1305 DEFINE_IPL_ATTR_STR_RW(vmcmd, on_poff, "%s\n", "%s\n", vmcmd_on_poff);
1306 DEFINE_IPL_ATTR_STR_RW(vmcmd, on_restart, "%s\n", "%s\n", vmcmd_on_restart);
1307
1308 static struct attribute *vmcmd_attrs[] = {
1309 &sys_vmcmd_on_reboot_attr.attr,
1310 &sys_vmcmd_on_panic_attr.attr,
1311 &sys_vmcmd_on_halt_attr.attr,
1312 &sys_vmcmd_on_poff_attr.attr,
1313 &sys_vmcmd_on_restart_attr.attr,
1314 NULL,
1315 };
1316
1317 static struct attribute_group vmcmd_attr_group = {
1318 .attrs = vmcmd_attrs,
1319 };
1320
1321 static struct kset *vmcmd_kset;
1322
vmcmd_run(struct shutdown_trigger * trigger)1323 static void vmcmd_run(struct shutdown_trigger *trigger)
1324 {
1325 char *cmd;
1326
1327 if (strcmp(trigger->name, ON_REIPL_STR) == 0)
1328 cmd = vmcmd_on_reboot;
1329 else if (strcmp(trigger->name, ON_PANIC_STR) == 0)
1330 cmd = vmcmd_on_panic;
1331 else if (strcmp(trigger->name, ON_HALT_STR) == 0)
1332 cmd = vmcmd_on_halt;
1333 else if (strcmp(trigger->name, ON_POFF_STR) == 0)
1334 cmd = vmcmd_on_poff;
1335 else if (strcmp(trigger->name, ON_RESTART_STR) == 0)
1336 cmd = vmcmd_on_restart;
1337 else
1338 return;
1339
1340 if (strlen(cmd) == 0)
1341 return;
1342 __cpcmd(cmd, NULL, 0, NULL);
1343 }
1344
vmcmd_init(void)1345 static int vmcmd_init(void)
1346 {
1347 if (!MACHINE_IS_VM)
1348 return -EOPNOTSUPP;
1349 vmcmd_kset = kset_create_and_add("vmcmd", NULL, firmware_kobj);
1350 if (!vmcmd_kset)
1351 return -ENOMEM;
1352 return sysfs_create_group(&vmcmd_kset->kobj, &vmcmd_attr_group);
1353 }
1354
1355 static struct shutdown_action vmcmd_action = {SHUTDOWN_ACTION_VMCMD_STR,
1356 vmcmd_run, vmcmd_init};
1357
1358 /*
1359 * stop shutdown action: Stop Linux on shutdown.
1360 */
1361
stop_run(struct shutdown_trigger * trigger)1362 static void stop_run(struct shutdown_trigger *trigger)
1363 {
1364 if (strcmp(trigger->name, ON_PANIC_STR) == 0 ||
1365 strcmp(trigger->name, ON_RESTART_STR) == 0)
1366 disabled_wait();
1367 smp_stop_cpu();
1368 }
1369
1370 static struct shutdown_action stop_action = {SHUTDOWN_ACTION_STOP_STR,
1371 stop_run, NULL};
1372
1373 /* action list */
1374
1375 static struct shutdown_action *shutdown_actions_list[] = {
1376 &ipl_action, &reipl_action, &dump_reipl_action, &dump_action,
1377 &vmcmd_action, &stop_action};
1378 #define SHUTDOWN_ACTIONS_COUNT (sizeof(shutdown_actions_list) / sizeof(void *))
1379
1380 /*
1381 * Trigger section
1382 */
1383
1384 static struct kset *shutdown_actions_kset;
1385
set_trigger(const char * buf,struct shutdown_trigger * trigger,size_t len)1386 static int set_trigger(const char *buf, struct shutdown_trigger *trigger,
1387 size_t len)
1388 {
1389 int i;
1390
1391 for (i = 0; i < SHUTDOWN_ACTIONS_COUNT; i++) {
1392 if (sysfs_streq(buf, shutdown_actions_list[i]->name)) {
1393 if (shutdown_actions_list[i]->init_rc) {
1394 return shutdown_actions_list[i]->init_rc;
1395 } else {
1396 trigger->action = shutdown_actions_list[i];
1397 return len;
1398 }
1399 }
1400 }
1401 return -EINVAL;
1402 }
1403
1404 /* on reipl */
1405
1406 static struct shutdown_trigger on_reboot_trigger = {ON_REIPL_STR,
1407 &reipl_action};
1408
on_reboot_show(struct kobject * kobj,struct kobj_attribute * attr,char * page)1409 static ssize_t on_reboot_show(struct kobject *kobj,
1410 struct kobj_attribute *attr, char *page)
1411 {
1412 return sprintf(page, "%s\n", on_reboot_trigger.action->name);
1413 }
1414
on_reboot_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t len)1415 static ssize_t on_reboot_store(struct kobject *kobj,
1416 struct kobj_attribute *attr,
1417 const char *buf, size_t len)
1418 {
1419 return set_trigger(buf, &on_reboot_trigger, len);
1420 }
1421 static struct kobj_attribute on_reboot_attr = __ATTR_RW(on_reboot);
1422
do_machine_restart(char * __unused)1423 static void do_machine_restart(char *__unused)
1424 {
1425 smp_send_stop();
1426 on_reboot_trigger.action->fn(&on_reboot_trigger);
1427 reipl_run(NULL);
1428 }
1429 void (*_machine_restart)(char *command) = do_machine_restart;
1430
1431 /* on panic */
1432
1433 static struct shutdown_trigger on_panic_trigger = {ON_PANIC_STR, &stop_action};
1434
on_panic_show(struct kobject * kobj,struct kobj_attribute * attr,char * page)1435 static ssize_t on_panic_show(struct kobject *kobj,
1436 struct kobj_attribute *attr, char *page)
1437 {
1438 return sprintf(page, "%s\n", on_panic_trigger.action->name);
1439 }
1440
on_panic_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t len)1441 static ssize_t on_panic_store(struct kobject *kobj,
1442 struct kobj_attribute *attr,
1443 const char *buf, size_t len)
1444 {
1445 return set_trigger(buf, &on_panic_trigger, len);
1446 }
1447 static struct kobj_attribute on_panic_attr = __ATTR_RW(on_panic);
1448
do_panic(void)1449 static void do_panic(void)
1450 {
1451 lgr_info_log();
1452 on_panic_trigger.action->fn(&on_panic_trigger);
1453 stop_run(&on_panic_trigger);
1454 }
1455
1456 /* on restart */
1457
1458 static struct shutdown_trigger on_restart_trigger = {ON_RESTART_STR,
1459 &stop_action};
1460
on_restart_show(struct kobject * kobj,struct kobj_attribute * attr,char * page)1461 static ssize_t on_restart_show(struct kobject *kobj,
1462 struct kobj_attribute *attr, char *page)
1463 {
1464 return sprintf(page, "%s\n", on_restart_trigger.action->name);
1465 }
1466
on_restart_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t len)1467 static ssize_t on_restart_store(struct kobject *kobj,
1468 struct kobj_attribute *attr,
1469 const char *buf, size_t len)
1470 {
1471 return set_trigger(buf, &on_restart_trigger, len);
1472 }
1473 static struct kobj_attribute on_restart_attr = __ATTR_RW(on_restart);
1474
__do_restart(void * ignore)1475 static void __do_restart(void *ignore)
1476 {
1477 __arch_local_irq_stosm(0x04); /* enable DAT */
1478 smp_send_stop();
1479 #ifdef CONFIG_CRASH_DUMP
1480 crash_kexec(NULL);
1481 #endif
1482 on_restart_trigger.action->fn(&on_restart_trigger);
1483 stop_run(&on_restart_trigger);
1484 }
1485
do_restart(void)1486 void do_restart(void)
1487 {
1488 tracing_off();
1489 debug_locks_off();
1490 lgr_info_log();
1491 smp_call_online_cpu(__do_restart, NULL);
1492 }
1493
1494 /* on halt */
1495
1496 static struct shutdown_trigger on_halt_trigger = {ON_HALT_STR, &stop_action};
1497
on_halt_show(struct kobject * kobj,struct kobj_attribute * attr,char * page)1498 static ssize_t on_halt_show(struct kobject *kobj,
1499 struct kobj_attribute *attr, char *page)
1500 {
1501 return sprintf(page, "%s\n", on_halt_trigger.action->name);
1502 }
1503
on_halt_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t len)1504 static ssize_t on_halt_store(struct kobject *kobj,
1505 struct kobj_attribute *attr,
1506 const char *buf, size_t len)
1507 {
1508 return set_trigger(buf, &on_halt_trigger, len);
1509 }
1510 static struct kobj_attribute on_halt_attr = __ATTR_RW(on_halt);
1511
do_machine_halt(void)1512 static void do_machine_halt(void)
1513 {
1514 smp_send_stop();
1515 on_halt_trigger.action->fn(&on_halt_trigger);
1516 stop_run(&on_halt_trigger);
1517 }
1518 void (*_machine_halt)(void) = do_machine_halt;
1519
1520 /* on power off */
1521
1522 static struct shutdown_trigger on_poff_trigger = {ON_POFF_STR, &stop_action};
1523
on_poff_show(struct kobject * kobj,struct kobj_attribute * attr,char * page)1524 static ssize_t on_poff_show(struct kobject *kobj,
1525 struct kobj_attribute *attr, char *page)
1526 {
1527 return sprintf(page, "%s\n", on_poff_trigger.action->name);
1528 }
1529
on_poff_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t len)1530 static ssize_t on_poff_store(struct kobject *kobj,
1531 struct kobj_attribute *attr,
1532 const char *buf, size_t len)
1533 {
1534 return set_trigger(buf, &on_poff_trigger, len);
1535 }
1536 static struct kobj_attribute on_poff_attr = __ATTR_RW(on_poff);
1537
do_machine_power_off(void)1538 static void do_machine_power_off(void)
1539 {
1540 smp_send_stop();
1541 on_poff_trigger.action->fn(&on_poff_trigger);
1542 stop_run(&on_poff_trigger);
1543 }
1544 void (*_machine_power_off)(void) = do_machine_power_off;
1545
1546 static struct attribute *shutdown_action_attrs[] = {
1547 &on_restart_attr.attr,
1548 &on_reboot_attr.attr,
1549 &on_panic_attr.attr,
1550 &on_halt_attr.attr,
1551 &on_poff_attr.attr,
1552 NULL,
1553 };
1554
1555 static struct attribute_group shutdown_action_attr_group = {
1556 .attrs = shutdown_action_attrs,
1557 };
1558
shutdown_triggers_init(void)1559 static void __init shutdown_triggers_init(void)
1560 {
1561 shutdown_actions_kset = kset_create_and_add("shutdown_actions", NULL,
1562 firmware_kobj);
1563 if (!shutdown_actions_kset)
1564 goto fail;
1565 if (sysfs_create_group(&shutdown_actions_kset->kobj,
1566 &shutdown_action_attr_group))
1567 goto fail;
1568 return;
1569 fail:
1570 panic("shutdown_triggers_init failed\n");
1571 }
1572
shutdown_actions_init(void)1573 static void __init shutdown_actions_init(void)
1574 {
1575 int i;
1576
1577 for (i = 0; i < SHUTDOWN_ACTIONS_COUNT; i++) {
1578 if (!shutdown_actions_list[i]->init)
1579 continue;
1580 shutdown_actions_list[i]->init_rc =
1581 shutdown_actions_list[i]->init();
1582 }
1583 }
1584
s390_ipl_init(void)1585 static int __init s390_ipl_init(void)
1586 {
1587 char str[8] = {0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40};
1588
1589 sclp_early_get_ipl_info(&sclp_ipl_info);
1590 /*
1591 * Fix loadparm: There are systems where the (SCSI) LOADPARM
1592 * returned by read SCP info is invalid (contains EBCDIC blanks)
1593 * when the system has been booted via diag308. In that case we use
1594 * the value from diag308, if available.
1595 *
1596 * There are also systems where diag308 store does not work in
1597 * case the system is booted from HMC. Fortunately in this case
1598 * READ SCP info provides the correct value.
1599 */
1600 if (memcmp(sclp_ipl_info.loadparm, str, sizeof(str)) == 0 && ipl_block_valid)
1601 memcpy(sclp_ipl_info.loadparm, ipl_block.ccw.loadparm, LOADPARM_LEN);
1602 shutdown_actions_init();
1603 shutdown_triggers_init();
1604 return 0;
1605 }
1606
1607 __initcall(s390_ipl_init);
1608
strncpy_skip_quote(char * dst,char * src,int n)1609 static void __init strncpy_skip_quote(char *dst, char *src, int n)
1610 {
1611 int sx, dx;
1612
1613 dx = 0;
1614 for (sx = 0; src[sx] != 0; sx++) {
1615 if (src[sx] == '"')
1616 continue;
1617 dst[dx++] = src[sx];
1618 if (dx >= n)
1619 break;
1620 }
1621 }
1622
vmcmd_on_reboot_setup(char * str)1623 static int __init vmcmd_on_reboot_setup(char *str)
1624 {
1625 if (!MACHINE_IS_VM)
1626 return 1;
1627 strncpy_skip_quote(vmcmd_on_reboot, str, 127);
1628 vmcmd_on_reboot[127] = 0;
1629 on_reboot_trigger.action = &vmcmd_action;
1630 return 1;
1631 }
1632 __setup("vmreboot=", vmcmd_on_reboot_setup);
1633
vmcmd_on_panic_setup(char * str)1634 static int __init vmcmd_on_panic_setup(char *str)
1635 {
1636 if (!MACHINE_IS_VM)
1637 return 1;
1638 strncpy_skip_quote(vmcmd_on_panic, str, 127);
1639 vmcmd_on_panic[127] = 0;
1640 on_panic_trigger.action = &vmcmd_action;
1641 return 1;
1642 }
1643 __setup("vmpanic=", vmcmd_on_panic_setup);
1644
vmcmd_on_halt_setup(char * str)1645 static int __init vmcmd_on_halt_setup(char *str)
1646 {
1647 if (!MACHINE_IS_VM)
1648 return 1;
1649 strncpy_skip_quote(vmcmd_on_halt, str, 127);
1650 vmcmd_on_halt[127] = 0;
1651 on_halt_trigger.action = &vmcmd_action;
1652 return 1;
1653 }
1654 __setup("vmhalt=", vmcmd_on_halt_setup);
1655
vmcmd_on_poff_setup(char * str)1656 static int __init vmcmd_on_poff_setup(char *str)
1657 {
1658 if (!MACHINE_IS_VM)
1659 return 1;
1660 strncpy_skip_quote(vmcmd_on_poff, str, 127);
1661 vmcmd_on_poff[127] = 0;
1662 on_poff_trigger.action = &vmcmd_action;
1663 return 1;
1664 }
1665 __setup("vmpoff=", vmcmd_on_poff_setup);
1666
on_panic_notify(struct notifier_block * self,unsigned long event,void * data)1667 static int on_panic_notify(struct notifier_block *self,
1668 unsigned long event, void *data)
1669 {
1670 do_panic();
1671 return NOTIFY_OK;
1672 }
1673
1674 static struct notifier_block on_panic_nb = {
1675 .notifier_call = on_panic_notify,
1676 .priority = INT_MIN,
1677 };
1678
setup_ipl(void)1679 void __init setup_ipl(void)
1680 {
1681 BUILD_BUG_ON(sizeof(struct ipl_parameter_block) != PAGE_SIZE);
1682
1683 ipl_info.type = get_ipl_type();
1684 switch (ipl_info.type) {
1685 case IPL_TYPE_CCW:
1686 ipl_info.data.ccw.dev_id.ssid = ipl_block.ccw.ssid;
1687 ipl_info.data.ccw.dev_id.devno = ipl_block.ccw.devno;
1688 break;
1689 case IPL_TYPE_FCP:
1690 case IPL_TYPE_FCP_DUMP:
1691 ipl_info.data.fcp.dev_id.ssid = 0;
1692 ipl_info.data.fcp.dev_id.devno = ipl_block.fcp.devno;
1693 ipl_info.data.fcp.wwpn = ipl_block.fcp.wwpn;
1694 ipl_info.data.fcp.lun = ipl_block.fcp.lun;
1695 break;
1696 case IPL_TYPE_NSS:
1697 case IPL_TYPE_UNKNOWN:
1698 /* We have no info to copy */
1699 break;
1700 }
1701 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
1702 }
1703
s390_reset_system(void)1704 void s390_reset_system(void)
1705 {
1706 /* Disable prefixing */
1707 set_prefix(0);
1708
1709 /* Disable lowcore protection */
1710 __ctl_clear_bit(0, 28);
1711 diag_dma_ops.diag308_reset();
1712 }
1713
1714 #ifdef CONFIG_KEXEC_FILE
1715
ipl_report_add_component(struct ipl_report * report,struct kexec_buf * kbuf,unsigned char flags,unsigned short cert)1716 int ipl_report_add_component(struct ipl_report *report, struct kexec_buf *kbuf,
1717 unsigned char flags, unsigned short cert)
1718 {
1719 struct ipl_report_component *comp;
1720
1721 comp = vzalloc(sizeof(*comp));
1722 if (!comp)
1723 return -ENOMEM;
1724 list_add_tail(&comp->list, &report->components);
1725
1726 comp->entry.addr = kbuf->mem;
1727 comp->entry.len = kbuf->memsz;
1728 comp->entry.flags = flags;
1729 comp->entry.certificate_index = cert;
1730
1731 report->size += sizeof(comp->entry);
1732
1733 return 0;
1734 }
1735
ipl_report_add_certificate(struct ipl_report * report,void * key,unsigned long addr,unsigned long len)1736 int ipl_report_add_certificate(struct ipl_report *report, void *key,
1737 unsigned long addr, unsigned long len)
1738 {
1739 struct ipl_report_certificate *cert;
1740
1741 cert = vzalloc(sizeof(*cert));
1742 if (!cert)
1743 return -ENOMEM;
1744 list_add_tail(&cert->list, &report->certificates);
1745
1746 cert->entry.addr = addr;
1747 cert->entry.len = len;
1748 cert->key = key;
1749
1750 report->size += sizeof(cert->entry);
1751 report->size += cert->entry.len;
1752
1753 return 0;
1754 }
1755
ipl_report_init(struct ipl_parameter_block * ipib)1756 struct ipl_report *ipl_report_init(struct ipl_parameter_block *ipib)
1757 {
1758 struct ipl_report *report;
1759
1760 report = vzalloc(sizeof(*report));
1761 if (!report)
1762 return ERR_PTR(-ENOMEM);
1763
1764 report->ipib = ipib;
1765 INIT_LIST_HEAD(&report->components);
1766 INIT_LIST_HEAD(&report->certificates);
1767
1768 report->size = ALIGN(ipib->hdr.len, 8);
1769 report->size += sizeof(struct ipl_rl_hdr);
1770 report->size += sizeof(struct ipl_rb_components);
1771 report->size += sizeof(struct ipl_rb_certificates);
1772
1773 return report;
1774 }
1775
ipl_report_finish(struct ipl_report * report)1776 void *ipl_report_finish(struct ipl_report *report)
1777 {
1778 struct ipl_report_certificate *cert;
1779 struct ipl_report_component *comp;
1780 struct ipl_rb_certificates *certs;
1781 struct ipl_parameter_block *ipib;
1782 struct ipl_rb_components *comps;
1783 struct ipl_rl_hdr *rl_hdr;
1784 void *buf, *ptr;
1785
1786 buf = vzalloc(report->size);
1787 if (!buf)
1788 goto out;
1789 ptr = buf;
1790
1791 memcpy(ptr, report->ipib, report->ipib->hdr.len);
1792 ipib = ptr;
1793 if (ipl_secure_flag)
1794 ipib->hdr.flags |= IPL_PL_FLAG_SIPL;
1795 ipib->hdr.flags |= IPL_PL_FLAG_IPLSR;
1796 ptr += report->ipib->hdr.len;
1797 ptr = PTR_ALIGN(ptr, 8);
1798
1799 rl_hdr = ptr;
1800 ptr += sizeof(*rl_hdr);
1801
1802 comps = ptr;
1803 comps->rbt = IPL_RBT_COMPONENTS;
1804 ptr += sizeof(*comps);
1805 list_for_each_entry(comp, &report->components, list) {
1806 memcpy(ptr, &comp->entry, sizeof(comp->entry));
1807 ptr += sizeof(comp->entry);
1808 }
1809 comps->len = ptr - (void *)comps;
1810
1811 certs = ptr;
1812 certs->rbt = IPL_RBT_CERTIFICATES;
1813 ptr += sizeof(*certs);
1814 list_for_each_entry(cert, &report->certificates, list) {
1815 memcpy(ptr, &cert->entry, sizeof(cert->entry));
1816 ptr += sizeof(cert->entry);
1817 }
1818 certs->len = ptr - (void *)certs;
1819 rl_hdr->len = ptr - (void *)rl_hdr;
1820
1821 list_for_each_entry(cert, &report->certificates, list) {
1822 memcpy(ptr, cert->key, cert->entry.len);
1823 ptr += cert->entry.len;
1824 }
1825
1826 BUG_ON(ptr > buf + report->size);
1827 out:
1828 return buf;
1829 }
1830
ipl_report_free(struct ipl_report * report)1831 int ipl_report_free(struct ipl_report *report)
1832 {
1833 struct ipl_report_component *comp, *ncomp;
1834 struct ipl_report_certificate *cert, *ncert;
1835
1836 list_for_each_entry_safe(comp, ncomp, &report->components, list)
1837 vfree(comp);
1838
1839 list_for_each_entry_safe(cert, ncert, &report->certificates, list)
1840 vfree(cert);
1841
1842 vfree(report);
1843
1844 return 0;
1845 }
1846
1847 #endif
1848