xref: /openbmc/qemu/pc-bios/s390-ccw/main.c (revision f28d0dfd)
1 /*
2  * S390 virtio-ccw loading program
3  *
4  * Copyright (c) 2013 Alexander Graf <agraf@suse.de>
5  *
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  */
10 
11 #include "libc.h"
12 #include "s390-ccw.h"
13 #include "virtio.h"
14 
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_str[LOADPARM_LEN + 1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
19 QemuIplParameters qipl;
20 
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)
24 
25 /*
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.
29  */
30 void write_subsystem_identification(void)
31 {
32     SubChannelId *schid = (SubChannelId *) 184;
33     uint32_t *zeroes = (uint32_t *) 188;
34 
35     *schid = blk_schid;
36     *zeroes = 0;
37 }
38 
39 void panic(const char *string)
40 {
41     sclp_print(string);
42     disabled_wait();
43     while (1) { }
44 }
45 
46 unsigned int get_loadparm_index(void)
47 {
48     return atoui(loadparm_str);
49 }
50 
51 static bool find_dev(Schib *schib, int dev_no)
52 {
53     int i, r;
54 
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;
60         }
61         if (!schib->pmcw.dnv) {
62             continue;
63         }
64         if (!virtio_is_supported(blk_schid)) {
65             continue;
66         }
67         /* Skip net devices since no IPLB is created and therefore no
68          * no network bootloader has been loaded
69          */
70         if (virtio_get_device_type() == VIRTIO_ID_NET && dev_no < 0) {
71             continue;
72         }
73         if ((dev_no < 0) || (schib->pmcw.dev == dev_no)) {
74             return true;
75         }
76     }
77 
78     return false;
79 }
80 
81 static void menu_setup(void)
82 {
83     if (memcmp(loadparm_str, LOADPARM_PROMPT, LOADPARM_LEN) == 0) {
84         menu_set_parms(QIPL_FLAG_BM_OPTS_CMD, 0);
85         return;
86     }
87 
88     /* If loadparm was set to any other value, then do not enable menu */
89     if (memcmp(loadparm_str, LOADPARM_EMPTY, LOADPARM_LEN) != 0) {
90         return;
91     }
92 
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;
99     }
100 }
101 
102 static void virtio_setup(void)
103 {
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;
111 
112     /*
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.
116      */
117     enable_mss_facility();
118 
119     sclp_get_loadparm_ascii(loadparm_str);
120     memcpy(ldp + 10, loadparm_str, LOADPARM_LEN);
121     sclp_print(ldp);
122 
123     memcpy(&qipl, early_qipl, sizeof(QemuIplParameters));
124 
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");
144         }
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;
152             }
153         }
154     }
155 
156     IPL_assert(found, "No virtio device found");
157 
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);
163 
164         IPL_assert(virtio_ipl_disk_is_valid(), "No valid IPL device detected");
165     }
166 }
167 
168 int main(void)
169 {
170     sclp_setup();
171     virtio_setup();
172 
173     zipl_load(); /* no return */
174 
175     panic("Failed to load OS from hard disk\n");
176     return 0; /* make compiler happy */
177 }
178