s390-ccw: interactive boot menu for scsi
[qemu.git] / pc-bios / s390-ccw / main.c
blob9d9f8cf4d3471f65c96773aa4b12c47f46c45e5b
1 /*
2 * S390 virtio-ccw loading program
4 * Copyright (c) 2013 Alexander Graf <agraf@suse.de>
6 * This work is licensed under the terms of the GNU GPL, version 2 or (at
7 * your option) any later version. See the COPYING file in the top-level
8 * directory.
9 */
11 #include "libc.h"
12 #include "s390-ccw.h"
13 #include "virtio.h"
15 char stack[PAGE_SIZE * 8] __attribute__((__aligned__(PAGE_SIZE)));
16 static SubChannelId blk_schid = { .one = 1 };
17 IplParameterBlock iplb __attribute__((__aligned__(PAGE_SIZE)));
18 static char loadparm[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
19 QemuIplParameters qipl;
21 #define LOADPARM_PROMPT "PROMPT "
22 #define LOADPARM_EMPTY "........"
23 #define BOOT_MENU_FLAG_MASK (QIPL_FLAG_BM_OPTS_CMD | QIPL_FLAG_BM_OPTS_ZIPL)
26 * Priniciples of Operations (SA22-7832-09) chapter 17 requires that
27 * a subsystem-identification is at 184-187 and bytes 188-191 are zero
28 * after list-directed-IPL and ccw-IPL.
30 void write_subsystem_identification(void)
32 SubChannelId *schid = (SubChannelId *) 184;
33 uint32_t *zeroes = (uint32_t *) 188;
35 *schid = blk_schid;
36 *zeroes = 0;
39 void panic(const char *string)
41 sclp_print(string);
42 disabled_wait();
43 while (1) { }
46 unsigned int get_loadparm_index(void)
48 return atoui(loadparm);
51 static bool find_dev(Schib *schib, int dev_no)
53 int i, r;
55 for (i = 0; i < 0x10000; i++) {
56 blk_schid.sch_no = i;
57 r = stsch_err(blk_schid, schib);
58 if ((r == 3) || (r == -EIO)) {
59 break;
61 if (!schib->pmcw.dnv) {
62 continue;
64 if (!virtio_is_supported(blk_schid)) {
65 continue;
67 /* Skip net devices since no IPLB is created and therefore no
68 * no network bootloader has been loaded
70 if (virtio_get_device_type() == VIRTIO_ID_NET && dev_no < 0) {
71 continue;
73 if ((dev_no < 0) || (schib->pmcw.dev == dev_no)) {
74 return true;
78 return false;
81 static void menu_setup(void)
83 if (memcmp(loadparm, LOADPARM_PROMPT, 8) == 0) {
84 menu_set_parms(QIPL_FLAG_BM_OPTS_CMD, 0);
85 return;
88 /* If loadparm was set to any other value, then do not enable menu */
89 if (memcmp(loadparm, LOADPARM_EMPTY, 8) != 0) {
90 return;
93 switch (iplb.pbt) {
94 case S390_IPL_TYPE_CCW:
95 case S390_IPL_TYPE_QEMU_SCSI:
96 menu_set_parms(qipl.qipl_flags & BOOT_MENU_FLAG_MASK,
97 qipl.boot_menu_timeout);
98 return;
102 static void virtio_setup(void)
104 Schib schib;
105 int ssid;
106 bool found = false;
107 uint16_t dev_no;
108 char ldp[] = "LOADPARM=[________]\n";
109 VDev *vdev = virtio_get_device();
110 QemuIplParameters *early_qipl = (QemuIplParameters *)QIPL_ADDRESS;
113 * We unconditionally enable mss support. In every sane configuration,
114 * this will succeed; and even if it doesn't, stsch_err() can deal
115 * with the consequences.
117 enable_mss_facility();
119 sclp_get_loadparm_ascii(loadparm);
120 memcpy(ldp + 10, loadparm, 8);
121 sclp_print(ldp);
123 memcpy(&qipl, early_qipl, sizeof(QemuIplParameters));
125 if (store_iplb(&iplb)) {
126 switch (iplb.pbt) {
127 case S390_IPL_TYPE_CCW:
128 dev_no = iplb.ccw.devno;
129 debug_print_int("device no. ", dev_no);
130 blk_schid.ssid = iplb.ccw.ssid & 0x3;
131 debug_print_int("ssid ", blk_schid.ssid);
132 found = find_dev(&schib, dev_no);
133 break;
134 case S390_IPL_TYPE_QEMU_SCSI:
135 vdev->scsi_device_selected = true;
136 vdev->selected_scsi_device.channel = iplb.scsi.channel;
137 vdev->selected_scsi_device.target = iplb.scsi.target;
138 vdev->selected_scsi_device.lun = iplb.scsi.lun;
139 blk_schid.ssid = iplb.scsi.ssid & 0x3;
140 found = find_dev(&schib, iplb.scsi.devno);
141 break;
142 default:
143 panic("List-directed IPL not supported yet!\n");
145 menu_setup();
146 } else {
147 for (ssid = 0; ssid < 0x3; ssid++) {
148 blk_schid.ssid = ssid;
149 found = find_dev(&schib, -1);
150 if (found) {
151 break;
156 IPL_assert(found, "No virtio device found");
158 if (virtio_get_device_type() == VIRTIO_ID_NET) {
159 sclp_print("Network boot device detected\n");
160 vdev->netboot_start_addr = qipl.netboot_start_addr;
161 } else {
162 virtio_blk_setup_device(blk_schid);
164 IPL_assert(virtio_ipl_disk_is_valid(), "No valid IPL device detected");
168 int main(void)
170 sclp_setup();
171 virtio_setup();
173 zipl_load(); /* no return */
175 panic("Failed to load OS from hard disk\n");
176 return 0; /* make compiler happy */