]> git.proxmox.com Git - mirror_qemu.git/blame - hw/s390x/ipl.c
s390x/ipl: support diagnose 308 subcodes 5 and 6
[mirror_qemu.git] / hw / s390x / ipl.c
CommitLineData
e674a49a
CB
1/*
2 * bootloader support
3 *
4 * Copyright IBM, Corp. 2012
5 *
6 * Authors:
7 * Christian Borntraeger <borntraeger@de.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2 or (at your
10 * option) any later version. See the COPYING file in the top-level directory.
11 *
12 */
13
14#include "sysemu/sysemu.h"
15#include "cpu.h"
16#include "elf.h"
17#include "hw/loader.h"
18#include "hw/sysbus.h"
ba1509c0
DD
19#include "hw/s390x/virtio-ccw.h"
20#include "hw/s390x/css.h"
df75a4e2 21#include "ipl.h"
e674a49a
CB
22
23#define KERN_IMAGE_START 0x010000UL
24#define KERN_PARM_AREA 0x010480UL
25#define INITRD_START 0x800000UL
26#define INITRD_PARM_START 0x010408UL
27#define INITRD_PARM_SIZE 0x010410UL
28#define PARMFILE_START 0x001000UL
e674a49a
CB
29#define ZIPL_IMAGE_START 0x009000UL
30#define IPL_PSW_MASK (PSW_MASK_32 | PSW_MASK_64)
31
32#define TYPE_S390_IPL "s390-ipl"
33#define S390_IPL(obj) \
34 OBJECT_CHECK(S390IPLState, (obj), TYPE_S390_IPL)
35#if 0
36#define S390_IPL_CLASS(klass) \
37 OBJECT_CLASS_CHECK(S390IPLState, (klass), TYPE_S390_IPL)
38#define S390_IPL_GET_CLASS(obj) \
39 OBJECT_GET_CLASS(S390IPLState, (obj), TYPE_S390_IPL)
40#endif
41
42typedef struct S390IPLClass {
43 /*< private >*/
44 SysBusDeviceClass parent_class;
45 /*< public >*/
46
47 void (*parent_reset) (SysBusDevice *dev);
48} S390IPLClass;
49
50typedef struct S390IPLState {
51 /*< private >*/
52 SysBusDevice parent_obj;
74ad2d22 53 uint64_t start_addr;
f0180f91
FZ
54 uint64_t bios_start_addr;
55 bool enforce_bios;
df75a4e2
FZ
56 IplParameterBlock iplb;
57 bool iplb_valid;
e674a49a 58
74ad2d22 59 /*< public >*/
e674a49a
CB
60 char *kernel;
61 char *initrd;
62 char *cmdline;
d0249ce5 63 char *firmware;
df75a4e2
FZ
64 uint8_t cssid;
65 uint8_t ssid;
66 uint16_t devno;
e674a49a
CB
67} S390IPLState;
68
69
e674a49a
CB
70static int s390_ipl_init(SysBusDevice *dev)
71{
72 S390IPLState *ipl = S390_IPL(dev);
7691993c 73 uint64_t pentry = KERN_IMAGE_START;
376827d4 74 int kernel_size;
e674a49a 75
f0180f91
FZ
76 int bios_size;
77 char *bios_filename;
e674a49a 78
f0180f91
FZ
79 /*
80 * Always load the bios if it was enforced,
81 * even if an external kernel has been defined.
82 */
83 if (!ipl->kernel || ipl->enforce_bios) {
e674a49a 84 if (bios_name == NULL) {
d0249ce5 85 bios_name = ipl->firmware;
e674a49a
CB
86 }
87
88 bios_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
1f7de853
DD
89 if (bios_filename == NULL) {
90 hw_error("could not find stage1 bootloader\n");
91 }
92
f0180f91
FZ
93 bios_size = load_elf(bios_filename, NULL, NULL, &ipl->bios_start_addr,
94 NULL, NULL, 1, ELF_MACHINE, 0);
0a1bec8a 95 if (bios_size < 0) {
33259956
AG
96 bios_size = load_image_targphys(bios_filename, ZIPL_IMAGE_START,
97 4096);
f0180f91 98 ipl->bios_start_addr = ZIPL_IMAGE_START;
33259956
AG
99 if (bios_size > 4096) {
100 hw_error("stage1 bootloader is > 4k\n");
101 }
102 }
e674a49a
CB
103 g_free(bios_filename);
104
376827d4 105 if (bios_size == -1) {
e674a49a
CB
106 hw_error("could not load bootloader '%s'\n", bios_name);
107 }
7691993c 108
f0180f91
FZ
109 /* default boot target is the bios */
110 ipl->start_addr = ipl->bios_start_addr;
e674a49a 111 }
7691993c 112
f0180f91
FZ
113 if (ipl->kernel) {
114 kernel_size = load_elf(ipl->kernel, NULL, NULL, &pentry, NULL,
115 NULL, 1, ELF_MACHINE, 0);
116 if (kernel_size < 0) {
117 kernel_size = load_image_targphys(ipl->kernel, 0, ram_size);
118 }
119 if (kernel_size < 0) {
120 fprintf(stderr, "could not load kernel '%s'\n", ipl->kernel);
121 return -1;
e674a49a 122 }
f0180f91
FZ
123 /*
124 * Is it a Linux kernel (starting at 0x10000)? If yes, we fill in the
125 * kernel parameters here as well. Note: For old kernels (up to 3.2)
126 * we can not rely on the ELF entry point - it was 0x800 (the SALIPL
127 * loader) and it won't work. For this case we force it to 0x10000, too.
128 */
129 if (pentry == KERN_IMAGE_START || pentry == 0x800) {
130 ipl->start_addr = KERN_IMAGE_START;
131 /* Overwrite parameters in the kernel image, which are "rom" */
132 strcpy(rom_ptr(KERN_PARM_AREA), ipl->cmdline);
133 } else {
134 ipl->start_addr = pentry;
e674a49a
CB
135 }
136
f0180f91
FZ
137 if (ipl->initrd) {
138 ram_addr_t initrd_offset;
139 int initrd_size;
140
141 initrd_offset = INITRD_START;
142 while (kernel_size + 0x100000 > initrd_offset) {
143 initrd_offset += 0x100000;
144 }
145 initrd_size = load_image_targphys(ipl->initrd, initrd_offset,
146 ram_size - initrd_offset);
147 if (initrd_size == -1) {
148 fprintf(stderr, "qemu: could not load initrd '%s'\n",
149 ipl->initrd);
150 exit(1);
151 }
e674a49a 152
f0180f91
FZ
153 /*
154 * we have to overwrite values in the kernel image,
155 * which are "rom"
156 */
157 stq_p(rom_ptr(INITRD_PARM_START), initrd_offset);
158 stq_p(rom_ptr(INITRD_PARM_SIZE), initrd_size);
159 }
160 }
e674a49a
CB
161 return 0;
162}
163
164static Property s390_ipl_properties[] = {
165 DEFINE_PROP_STRING("kernel", S390IPLState, kernel),
166 DEFINE_PROP_STRING("initrd", S390IPLState, initrd),
167 DEFINE_PROP_STRING("cmdline", S390IPLState, cmdline),
d0249ce5 168 DEFINE_PROP_STRING("firmware", S390IPLState, firmware),
f0180f91 169 DEFINE_PROP_BOOL("enforce_bios", S390IPLState, enforce_bios, false),
e674a49a
CB
170 DEFINE_PROP_END_OF_LIST(),
171};
172
df75a4e2
FZ
173/*
174 * In addition to updating the iplstate, this function returns:
175 * - 0 if system was ipled with external kernel
176 * - -1 if no valid boot device was found
177 * - ccw id of the boot device otherwise
178 */
179static uint64_t s390_update_iplstate(CPUS390XState *env, S390IPLState *ipl)
180{
181 DeviceState *dev_st;
182
183 if (ipl->iplb_valid) {
184 ipl->cssid = 0;
185 ipl->ssid = 0;
186 ipl->devno = ipl->iplb.devno;
187 goto out;
188 }
189
190 if (ipl->kernel) {
191 return 0;
192 }
193
194 dev_st = get_boot_device(0);
195 if (dev_st) {
196 VirtioCcwDevice *ccw_dev = (VirtioCcwDevice *) object_dynamic_cast(
197 OBJECT(qdev_get_parent_bus(dev_st)->parent),
198 TYPE_VIRTIO_CCW_DEVICE);
199 if (ccw_dev) {
200 ipl->cssid = ccw_dev->sch->cssid;
201 ipl->ssid = ccw_dev->sch->ssid;
202 ipl->devno = ccw_dev->sch->devno;
203 goto out;
204 }
205 }
206
207 return -1;
208out:
209 return ipl->cssid << 24 | ipl->ssid << 16 | ipl->devno;
210}
211
212int s390_ipl_update_diag308(IplParameterBlock *iplb)
213{
214 S390IPLState *ipl;
215
216 ipl = S390_IPL(object_resolve_path(TYPE_S390_IPL, NULL));
217 if (ipl) {
218 ipl->iplb = *iplb;
219 ipl->iplb_valid = true;
220 return 0;
221 }
222 return -1;
223}
224
225IplParameterBlock *s390_ipl_get_iplb(void)
226{
227 S390IPLState *ipl;
228
229 ipl = S390_IPL(object_resolve_path(TYPE_S390_IPL, NULL));
230 if (!ipl || !ipl->iplb_valid) {
231 return NULL;
232 }
233 return &ipl->iplb;
234}
235
e674a49a
CB
236static void s390_ipl_reset(DeviceState *dev)
237{
238 S390IPLState *ipl = S390_IPL(dev);
2c4c71ee
DD
239 S390CPU *cpu = S390_CPU(qemu_get_cpu(0));
240 CPUS390XState *env = &cpu->env;
e674a49a 241
2c4c71ee
DD
242 env->psw.addr = ipl->start_addr;
243 env->psw.mask = IPL_PSW_MASK;
ba1509c0 244
df75a4e2
FZ
245 if (!ipl->kernel || ipl->iplb_valid) {
246 env->psw.addr = ipl->bios_start_addr;
247 env->regs[7] = s390_update_iplstate(env, ipl);
ba1509c0
DD
248 }
249
eb24f7c6 250 s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
e674a49a
CB
251}
252
253static void s390_ipl_class_init(ObjectClass *klass, void *data)
254{
255 DeviceClass *dc = DEVICE_CLASS(klass);
256 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
257
258 k->init = s390_ipl_init;
259 dc->props = s390_ipl_properties;
260 dc->reset = s390_ipl_reset;
e674a49a
CB
261}
262
49973ebc 263static const TypeInfo s390_ipl_info = {
e674a49a
CB
264 .class_init = s390_ipl_class_init,
265 .parent = TYPE_SYS_BUS_DEVICE,
266 .name = "s390-ipl",
267 .instance_size = sizeof(S390IPLState),
268};
269
270static void s390_ipl_register_types(void)
271{
272 type_register_static(&s390_ipl_info);
273}
274
275type_init(s390_ipl_register_types)