]> git.proxmox.com Git - mirror_qemu.git/blame - hw/s390x/ipl.c
s390x/ipl: always load the bios for ccw machine
[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"
e674a49a
CB
21
22#define KERN_IMAGE_START 0x010000UL
23#define KERN_PARM_AREA 0x010480UL
24#define INITRD_START 0x800000UL
25#define INITRD_PARM_START 0x010408UL
26#define INITRD_PARM_SIZE 0x010410UL
27#define PARMFILE_START 0x001000UL
e674a49a
CB
28#define ZIPL_IMAGE_START 0x009000UL
29#define IPL_PSW_MASK (PSW_MASK_32 | PSW_MASK_64)
30
31#define TYPE_S390_IPL "s390-ipl"
32#define S390_IPL(obj) \
33 OBJECT_CHECK(S390IPLState, (obj), TYPE_S390_IPL)
34#if 0
35#define S390_IPL_CLASS(klass) \
36 OBJECT_CLASS_CHECK(S390IPLState, (klass), TYPE_S390_IPL)
37#define S390_IPL_GET_CLASS(obj) \
38 OBJECT_GET_CLASS(S390IPLState, (obj), TYPE_S390_IPL)
39#endif
40
41typedef struct S390IPLClass {
42 /*< private >*/
43 SysBusDeviceClass parent_class;
44 /*< public >*/
45
46 void (*parent_reset) (SysBusDevice *dev);
47} S390IPLClass;
48
49typedef struct S390IPLState {
50 /*< private >*/
51 SysBusDevice parent_obj;
74ad2d22 52 uint64_t start_addr;
f0180f91
FZ
53 uint64_t bios_start_addr;
54 bool enforce_bios;
e674a49a 55
74ad2d22 56 /*< public >*/
e674a49a
CB
57 char *kernel;
58 char *initrd;
59 char *cmdline;
d0249ce5 60 char *firmware;
e674a49a
CB
61} S390IPLState;
62
63
e674a49a
CB
64static int s390_ipl_init(SysBusDevice *dev)
65{
66 S390IPLState *ipl = S390_IPL(dev);
7691993c 67 uint64_t pentry = KERN_IMAGE_START;
376827d4 68 int kernel_size;
e674a49a 69
f0180f91
FZ
70 int bios_size;
71 char *bios_filename;
e674a49a 72
f0180f91
FZ
73 /*
74 * Always load the bios if it was enforced,
75 * even if an external kernel has been defined.
76 */
77 if (!ipl->kernel || ipl->enforce_bios) {
e674a49a 78 if (bios_name == NULL) {
d0249ce5 79 bios_name = ipl->firmware;
e674a49a
CB
80 }
81
82 bios_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
1f7de853
DD
83 if (bios_filename == NULL) {
84 hw_error("could not find stage1 bootloader\n");
85 }
86
f0180f91
FZ
87 bios_size = load_elf(bios_filename, NULL, NULL, &ipl->bios_start_addr,
88 NULL, NULL, 1, ELF_MACHINE, 0);
0a1bec8a 89 if (bios_size < 0) {
33259956
AG
90 bios_size = load_image_targphys(bios_filename, ZIPL_IMAGE_START,
91 4096);
f0180f91 92 ipl->bios_start_addr = ZIPL_IMAGE_START;
33259956
AG
93 if (bios_size > 4096) {
94 hw_error("stage1 bootloader is > 4k\n");
95 }
96 }
e674a49a
CB
97 g_free(bios_filename);
98
376827d4 99 if (bios_size == -1) {
e674a49a
CB
100 hw_error("could not load bootloader '%s'\n", bios_name);
101 }
7691993c 102
f0180f91
FZ
103 /* default boot target is the bios */
104 ipl->start_addr = ipl->bios_start_addr;
e674a49a 105 }
7691993c 106
f0180f91
FZ
107 if (ipl->kernel) {
108 kernel_size = load_elf(ipl->kernel, NULL, NULL, &pentry, NULL,
109 NULL, 1, ELF_MACHINE, 0);
110 if (kernel_size < 0) {
111 kernel_size = load_image_targphys(ipl->kernel, 0, ram_size);
112 }
113 if (kernel_size < 0) {
114 fprintf(stderr, "could not load kernel '%s'\n", ipl->kernel);
115 return -1;
e674a49a 116 }
f0180f91
FZ
117 /*
118 * Is it a Linux kernel (starting at 0x10000)? If yes, we fill in the
119 * kernel parameters here as well. Note: For old kernels (up to 3.2)
120 * we can not rely on the ELF entry point - it was 0x800 (the SALIPL
121 * loader) and it won't work. For this case we force it to 0x10000, too.
122 */
123 if (pentry == KERN_IMAGE_START || pentry == 0x800) {
124 ipl->start_addr = KERN_IMAGE_START;
125 /* Overwrite parameters in the kernel image, which are "rom" */
126 strcpy(rom_ptr(KERN_PARM_AREA), ipl->cmdline);
127 } else {
128 ipl->start_addr = pentry;
e674a49a
CB
129 }
130
f0180f91
FZ
131 if (ipl->initrd) {
132 ram_addr_t initrd_offset;
133 int initrd_size;
134
135 initrd_offset = INITRD_START;
136 while (kernel_size + 0x100000 > initrd_offset) {
137 initrd_offset += 0x100000;
138 }
139 initrd_size = load_image_targphys(ipl->initrd, initrd_offset,
140 ram_size - initrd_offset);
141 if (initrd_size == -1) {
142 fprintf(stderr, "qemu: could not load initrd '%s'\n",
143 ipl->initrd);
144 exit(1);
145 }
e674a49a 146
f0180f91
FZ
147 /*
148 * we have to overwrite values in the kernel image,
149 * which are "rom"
150 */
151 stq_p(rom_ptr(INITRD_PARM_START), initrd_offset);
152 stq_p(rom_ptr(INITRD_PARM_SIZE), initrd_size);
153 }
154 }
e674a49a
CB
155 return 0;
156}
157
158static Property s390_ipl_properties[] = {
159 DEFINE_PROP_STRING("kernel", S390IPLState, kernel),
160 DEFINE_PROP_STRING("initrd", S390IPLState, initrd),
161 DEFINE_PROP_STRING("cmdline", S390IPLState, cmdline),
d0249ce5 162 DEFINE_PROP_STRING("firmware", S390IPLState, firmware),
f0180f91 163 DEFINE_PROP_BOOL("enforce_bios", S390IPLState, enforce_bios, false),
e674a49a
CB
164 DEFINE_PROP_END_OF_LIST(),
165};
166
167static void s390_ipl_reset(DeviceState *dev)
168{
169 S390IPLState *ipl = S390_IPL(dev);
2c4c71ee
DD
170 S390CPU *cpu = S390_CPU(qemu_get_cpu(0));
171 CPUS390XState *env = &cpu->env;
e674a49a 172
2c4c71ee
DD
173 env->psw.addr = ipl->start_addr;
174 env->psw.mask = IPL_PSW_MASK;
ba1509c0
DD
175
176 if (!ipl->kernel) {
5c8ded6e
CB
177 /* Tell firmware, if there is a preferred boot device */
178 env->regs[7] = -1;
ba1509c0 179 DeviceState *dev_st = get_boot_device(0);
5c8ded6e
CB
180 if (dev_st) {
181 VirtioCcwDevice *ccw_dev = (VirtioCcwDevice *) object_dynamic_cast(
182 OBJECT(qdev_get_parent_bus(dev_st)->parent),
183 TYPE_VIRTIO_CCW_DEVICE);
184
185 if (ccw_dev) {
186 env->regs[7] = ccw_dev->sch->cssid << 24 |
187 ccw_dev->sch->ssid << 16 |
188 ccw_dev->sch->devno;
189 }
ba1509c0
DD
190 }
191 }
192
eb24f7c6 193 s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
e674a49a
CB
194}
195
196static void s390_ipl_class_init(ObjectClass *klass, void *data)
197{
198 DeviceClass *dc = DEVICE_CLASS(klass);
199 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
200
201 k->init = s390_ipl_init;
202 dc->props = s390_ipl_properties;
203 dc->reset = s390_ipl_reset;
e674a49a
CB
204}
205
49973ebc 206static const TypeInfo s390_ipl_info = {
e674a49a
CB
207 .class_init = s390_ipl_class_init,
208 .parent = TYPE_SYS_BUS_DEVICE,
209 .name = "s390-ipl",
210 .instance_size = sizeof(S390IPLState),
211};
212
213static void s390_ipl_register_types(void)
214{
215 type_register_static(&s390_ipl_info);
216}
217
218type_init(s390_ipl_register_types)