4 * Copyright IBM, Corp. 2012
7 * Christian Borntraeger <borntraeger@de.ibm.com>
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.
14 #include "sysemu/sysemu.h"
17 #include "hw/loader.h"
18 #include "hw/sysbus.h"
19 #include "hw/s390x/virtio-ccw.h"
20 #include "hw/s390x/css.h"
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
28 #define ZIPL_IMAGE_START 0x009000UL
29 #define IPL_PSW_MASK (PSW_MASK_32 | PSW_MASK_64)
31 #define TYPE_S390_IPL "s390-ipl"
32 #define S390_IPL(obj) \
33 OBJECT_CHECK(S390IPLState, (obj), TYPE_S390_IPL)
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)
41 typedef struct S390IPLClass
{
43 SysBusDeviceClass parent_class
;
46 void (*parent_reset
) (SysBusDevice
*dev
);
49 typedef struct S390IPLState
{
51 SysBusDevice parent_obj
;
62 static int s390_ipl_init(SysBusDevice
*dev
)
64 S390IPLState
*ipl
= S390_IPL(dev
);
71 /* Load zipl bootloader */
72 if (bios_name
== NULL
) {
73 bios_name
= ipl
->firmware
;
76 bios_filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, bios_name
);
77 if (bios_filename
== NULL
) {
78 hw_error("could not find stage1 bootloader\n");
81 bios_size
= load_elf(bios_filename
, NULL
, NULL
, &ipl
->start_addr
, NULL
,
82 NULL
, 1, ELF_MACHINE
, 0);
84 bios_size
= load_image_targphys(bios_filename
, ZIPL_IMAGE_START
,
86 ipl
->start_addr
= ZIPL_IMAGE_START
;
87 if (bios_size
> 4096) {
88 hw_error("stage1 bootloader is > 4k\n");
91 g_free(bios_filename
);
93 if (bios_size
== -1) {
94 hw_error("could not load bootloader '%s'\n", bios_name
);
98 uint64_t pentry
= KERN_IMAGE_START
;
99 kernel_size
= load_elf(ipl
->kernel
, NULL
, NULL
, &pentry
, NULL
,
100 NULL
, 1, ELF_MACHINE
, 0);
101 if (kernel_size
< 0) {
102 kernel_size
= load_image_targphys(ipl
->kernel
, 0, ram_size
);
104 if (kernel_size
< 0) {
105 fprintf(stderr
, "could not load kernel '%s'\n", ipl
->kernel
);
109 * Is it a Linux kernel (starting at 0x10000)? If yes, we fill in the
110 * kernel parameters here as well. Note: For old kernels (up to 3.2)
111 * we can not rely on the ELF entry point - it was 0x800 (the SALIPL
112 * loader) and it won't work. For this case we force it to 0x10000, too.
114 if (pentry
== KERN_IMAGE_START
|| pentry
== 0x800) {
115 ipl
->start_addr
= KERN_IMAGE_START
;
116 /* Overwrite parameters in the kernel image, which are "rom" */
117 strcpy(rom_ptr(KERN_PARM_AREA
), ipl
->cmdline
);
119 ipl
->start_addr
= pentry
;
123 ram_addr_t initrd_offset
;
126 initrd_offset
= INITRD_START
;
127 while (kernel_size
+ 0x100000 > initrd_offset
) {
128 initrd_offset
+= 0x100000;
130 initrd_size
= load_image_targphys(ipl
->initrd
, initrd_offset
,
131 ram_size
- initrd_offset
);
132 if (initrd_size
== -1) {
133 fprintf(stderr
, "qemu: could not load initrd '%s'\n", ipl
->initrd
);
137 /* we have to overwrite values in the kernel image, which are "rom" */
138 stq_p(rom_ptr(INITRD_PARM_START
), initrd_offset
);
139 stq_p(rom_ptr(INITRD_PARM_SIZE
), initrd_size
);
145 static Property s390_ipl_properties
[] = {
146 DEFINE_PROP_STRING("kernel", S390IPLState
, kernel
),
147 DEFINE_PROP_STRING("initrd", S390IPLState
, initrd
),
148 DEFINE_PROP_STRING("cmdline", S390IPLState
, cmdline
),
149 DEFINE_PROP_STRING("firmware", S390IPLState
, firmware
),
150 DEFINE_PROP_END_OF_LIST(),
153 static void s390_ipl_reset(DeviceState
*dev
)
155 S390IPLState
*ipl
= S390_IPL(dev
);
156 S390CPU
*cpu
= S390_CPU(qemu_get_cpu(0));
157 CPUS390XState
*env
= &cpu
->env
;
159 env
->psw
.addr
= ipl
->start_addr
;
160 env
->psw
.mask
= IPL_PSW_MASK
;
163 /* Tell firmware, if there is a preferred boot device */
165 DeviceState
*dev_st
= get_boot_device(0);
167 VirtioCcwDevice
*ccw_dev
= (VirtioCcwDevice
*) object_dynamic_cast(
168 OBJECT(qdev_get_parent_bus(dev_st
)->parent
),
169 TYPE_VIRTIO_CCW_DEVICE
);
172 env
->regs
[7] = ccw_dev
->sch
->cssid
<< 24 |
173 ccw_dev
->sch
->ssid
<< 16 |
179 s390_cpu_set_state(CPU_STATE_OPERATING
, cpu
);
182 static void s390_ipl_class_init(ObjectClass
*klass
, void *data
)
184 DeviceClass
*dc
= DEVICE_CLASS(klass
);
185 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
187 k
->init
= s390_ipl_init
;
188 dc
->props
= s390_ipl_properties
;
189 dc
->reset
= s390_ipl_reset
;
192 static const TypeInfo s390_ipl_info
= {
193 .class_init
= s390_ipl_class_init
,
194 .parent
= TYPE_SYS_BUS_DEVICE
,
196 .instance_size
= sizeof(S390IPLState
),
199 static void s390_ipl_register_types(void)
201 type_register_static(&s390_ipl_info
);
204 type_init(s390_ipl_register_types
)