xref: /openbmc/qemu/hw/s390x/ipl.c (revision 3588185b)
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"
19 
20 #define KERN_IMAGE_START                0x010000UL
21 #define KERN_PARM_AREA                  0x010480UL
22 #define INITRD_START                    0x800000UL
23 #define INITRD_PARM_START               0x010408UL
24 #define INITRD_PARM_SIZE                0x010410UL
25 #define PARMFILE_START                  0x001000UL
26 #define ZIPL_FILENAME                   "s390-zipl.rom"
27 #define ZIPL_IMAGE_START                0x009000UL
28 #define IPL_PSW_MASK                    (PSW_MASK_32 | PSW_MASK_64)
29 
30 #define TYPE_S390_IPL "s390-ipl"
31 #define S390_IPL(obj) \
32     OBJECT_CHECK(S390IPLState, (obj), TYPE_S390_IPL)
33 #if 0
34 #define S390_IPL_CLASS(klass) \
35     OBJECT_CLASS_CHECK(S390IPLState, (klass), TYPE_S390_IPL)
36 #define S390_IPL_GET_CLASS(obj) \
37     OBJECT_GET_CLASS(S390IPLState, (obj), TYPE_S390_IPL)
38 #endif
39 
40 typedef struct S390IPLClass {
41     /*< private >*/
42     SysBusDeviceClass parent_class;
43     /*< public >*/
44 
45     void (*parent_reset) (SysBusDevice *dev);
46 } S390IPLClass;
47 
48 typedef struct S390IPLState {
49     /*< private >*/
50     SysBusDevice parent_obj;
51     /*< public >*/
52 
53     char *kernel;
54     char *initrd;
55     char *cmdline;
56 } S390IPLState;
57 
58 
59 static void s390_ipl_cpu(uint64_t pswaddr)
60 {
61     CPUS390XState *env = &S390_CPU(qemu_get_cpu(0))->env;
62     env->psw.addr = pswaddr;
63     env->psw.mask = IPL_PSW_MASK;
64     s390_add_running_cpu(env);
65 }
66 
67 static int s390_ipl_init(SysBusDevice *dev)
68 {
69     S390IPLState *ipl = S390_IPL(dev);
70     ram_addr_t kernel_size = 0;
71 
72     if (!ipl->kernel) {
73         ram_addr_t bios_size = 0;
74         char *bios_filename;
75 
76         /* Load zipl bootloader */
77         if (bios_name == NULL) {
78             bios_name = ZIPL_FILENAME;
79         }
80 
81         bios_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
82         bios_size = load_image_targphys(bios_filename, ZIPL_IMAGE_START, 4096);
83         g_free(bios_filename);
84 
85         if ((long)bios_size < 0) {
86             hw_error("could not load bootloader '%s'\n", bios_name);
87         }
88 
89         if (bios_size > 4096) {
90             hw_error("stage1 bootloader is > 4k\n");
91         }
92         return 0;
93     } else {
94         kernel_size = load_elf(ipl->kernel, NULL, NULL, NULL, NULL,
95                                NULL, 1, ELF_MACHINE, 0);
96         if (kernel_size == -1UL) {
97             kernel_size = load_image_targphys(ipl->kernel, 0, ram_size);
98         }
99         if (kernel_size == -1UL) {
100             fprintf(stderr, "could not load kernel '%s'\n", ipl->kernel);
101             return -1;
102         }
103         /* we have to overwrite values in the kernel image, which are "rom" */
104         strcpy(rom_ptr(KERN_PARM_AREA), ipl->cmdline);
105     }
106     if (ipl->initrd) {
107         ram_addr_t initrd_offset, initrd_size;
108 
109         initrd_offset = INITRD_START;
110         while (kernel_size + 0x100000 > initrd_offset) {
111             initrd_offset += 0x100000;
112         }
113         initrd_size = load_image_targphys(ipl->initrd, initrd_offset,
114                                           ram_size - initrd_offset);
115         if (initrd_size == -1UL) {
116             fprintf(stderr, "qemu: could not load initrd '%s'\n", ipl->initrd);
117             exit(1);
118         }
119 
120         /* we have to overwrite values in the kernel image, which are "rom" */
121         stq_p(rom_ptr(INITRD_PARM_START), initrd_offset);
122         stq_p(rom_ptr(INITRD_PARM_SIZE), initrd_size);
123     }
124 
125     return 0;
126 }
127 
128 static Property s390_ipl_properties[] = {
129     DEFINE_PROP_STRING("kernel", S390IPLState, kernel),
130     DEFINE_PROP_STRING("initrd", S390IPLState, initrd),
131     DEFINE_PROP_STRING("cmdline", S390IPLState, cmdline),
132     DEFINE_PROP_END_OF_LIST(),
133 };
134 
135 static void s390_ipl_reset(DeviceState *dev)
136 {
137     S390IPLState *ipl = S390_IPL(dev);
138 
139     if (ipl->kernel) {
140         /*
141          * we can not rely on the ELF entry point, since up to 3.2 this
142          * value was 0x800 (the SALIPL loader) and it wont work. For
143          * all (Linux) cases 0x10000 (KERN_IMAGE_START) should be fine.
144          */
145         return s390_ipl_cpu(KERN_IMAGE_START);
146     } else {
147         return s390_ipl_cpu(ZIPL_IMAGE_START);
148     }
149 }
150 
151 static void s390_ipl_class_init(ObjectClass *klass, void *data)
152 {
153     DeviceClass *dc = DEVICE_CLASS(klass);
154     SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
155 
156     k->init = s390_ipl_init;
157     dc->props = s390_ipl_properties;
158     dc->reset = s390_ipl_reset;
159     dc->no_user = 1;
160 }
161 
162 static TypeInfo s390_ipl_info = {
163     .class_init = s390_ipl_class_init,
164     .parent = TYPE_SYS_BUS_DEVICE,
165     .name  = "s390-ipl",
166     .instance_size  = sizeof(S390IPLState),
167 };
168 
169 static void s390_ipl_register_types(void)
170 {
171     type_register_static(&s390_ipl_info);
172 }
173 
174 type_init(s390_ipl_register_types)
175