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 "exec/ram_addr.h" 18 #include "hw/loader.h" 19 #include "hw/sysbus.h" 20 #include "hw/s390x/virtio-ccw.h" 21 #include "hw/s390x/css.h" 22 #include "ipl.h" 23 24 #define KERN_IMAGE_START 0x010000UL 25 #define KERN_PARM_AREA 0x010480UL 26 #define INITRD_START 0x800000UL 27 #define INITRD_PARM_START 0x010408UL 28 #define INITRD_PARM_SIZE 0x010410UL 29 #define PARMFILE_START 0x001000UL 30 #define ZIPL_IMAGE_START 0x009000UL 31 #define IPL_PSW_MASK (PSW_MASK_32 | PSW_MASK_64) 32 33 #define TYPE_S390_IPL "s390-ipl" 34 #define S390_IPL(obj) \ 35 OBJECT_CHECK(S390IPLState, (obj), TYPE_S390_IPL) 36 #if 0 37 #define S390_IPL_CLASS(klass) \ 38 OBJECT_CLASS_CHECK(S390IPLState, (klass), TYPE_S390_IPL) 39 #define S390_IPL_GET_CLASS(obj) \ 40 OBJECT_GET_CLASS(S390IPLState, (obj), TYPE_S390_IPL) 41 #endif 42 43 typedef struct S390IPLClass { 44 /*< private >*/ 45 SysBusDeviceClass parent_class; 46 /*< public >*/ 47 48 void (*parent_reset) (SysBusDevice *dev); 49 } S390IPLClass; 50 51 typedef struct S390IPLState { 52 /*< private >*/ 53 SysBusDevice parent_obj; 54 uint64_t start_addr; 55 uint64_t bios_start_addr; 56 bool enforce_bios; 57 IplParameterBlock iplb; 58 bool iplb_valid; 59 bool reipl_requested; 60 61 /*< public >*/ 62 char *kernel; 63 char *initrd; 64 char *cmdline; 65 char *firmware; 66 uint8_t cssid; 67 uint8_t ssid; 68 uint16_t devno; 69 } S390IPLState; 70 71 static const VMStateDescription vmstate_iplb = { 72 .name = "ipl/iplb", 73 .version_id = 0, 74 .minimum_version_id = 0, 75 .fields = (VMStateField[]) { 76 VMSTATE_UINT8_ARRAY(reserved1, IplParameterBlock, 110), 77 VMSTATE_UINT16(devno, IplParameterBlock), 78 VMSTATE_UINT8_ARRAY(reserved2, IplParameterBlock, 88), 79 VMSTATE_END_OF_LIST() 80 } 81 }; 82 83 static const VMStateDescription vmstate_ipl = { 84 .name = "ipl", 85 .version_id = 0, 86 .minimum_version_id = 0, 87 .fields = (VMStateField[]) { 88 VMSTATE_UINT64(start_addr, S390IPLState), 89 VMSTATE_UINT64(bios_start_addr, S390IPLState), 90 VMSTATE_STRUCT(iplb, S390IPLState, 0, vmstate_iplb, IplParameterBlock), 91 VMSTATE_BOOL(iplb_valid, S390IPLState), 92 VMSTATE_UINT8(cssid, S390IPLState), 93 VMSTATE_UINT8(ssid, S390IPLState), 94 VMSTATE_UINT16(devno, S390IPLState), 95 VMSTATE_END_OF_LIST() 96 } 97 }; 98 99 static uint64_t bios_translate_addr(void *opaque, uint64_t srcaddr) 100 { 101 uint64_t dstaddr = *(uint64_t *) opaque; 102 /* 103 * Assuming that our s390-ccw.img was linked for starting at address 0, 104 * we can simply add the destination address for the final location 105 */ 106 return srcaddr + dstaddr; 107 } 108 109 static int s390_ipl_init(SysBusDevice *dev) 110 { 111 S390IPLState *ipl = S390_IPL(dev); 112 uint64_t pentry = KERN_IMAGE_START; 113 int kernel_size; 114 115 int bios_size; 116 char *bios_filename; 117 118 /* 119 * Always load the bios if it was enforced, 120 * even if an external kernel has been defined. 121 */ 122 if (!ipl->kernel || ipl->enforce_bios) { 123 uint64_t fwbase = (MIN(ram_size, 0x80000000U) - 0x200000) & ~0xffffUL; 124 125 if (bios_name == NULL) { 126 bios_name = ipl->firmware; 127 } 128 129 bios_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name); 130 if (bios_filename == NULL) { 131 hw_error("could not find stage1 bootloader\n"); 132 } 133 134 bios_size = load_elf(bios_filename, bios_translate_addr, &fwbase, 135 &ipl->bios_start_addr, NULL, NULL, 1, 136 ELF_MACHINE, 0); 137 if (bios_size > 0) { 138 /* Adjust ELF start address to final location */ 139 ipl->bios_start_addr += fwbase; 140 } else { 141 /* Try to load non-ELF file (e.g. s390-zipl.rom) */ 142 bios_size = load_image_targphys(bios_filename, ZIPL_IMAGE_START, 143 4096); 144 ipl->bios_start_addr = ZIPL_IMAGE_START; 145 if (bios_size > 4096) { 146 hw_error("stage1 bootloader is > 4k\n"); 147 } 148 } 149 g_free(bios_filename); 150 151 if (bios_size == -1) { 152 hw_error("could not load bootloader '%s'\n", bios_name); 153 } 154 155 /* default boot target is the bios */ 156 ipl->start_addr = ipl->bios_start_addr; 157 } 158 159 if (ipl->kernel) { 160 kernel_size = load_elf(ipl->kernel, NULL, NULL, &pentry, NULL, 161 NULL, 1, ELF_MACHINE, 0); 162 if (kernel_size < 0) { 163 kernel_size = load_image_targphys(ipl->kernel, 0, ram_size); 164 } 165 if (kernel_size < 0) { 166 fprintf(stderr, "could not load kernel '%s'\n", ipl->kernel); 167 return -1; 168 } 169 /* 170 * Is it a Linux kernel (starting at 0x10000)? If yes, we fill in the 171 * kernel parameters here as well. Note: For old kernels (up to 3.2) 172 * we can not rely on the ELF entry point - it was 0x800 (the SALIPL 173 * loader) and it won't work. For this case we force it to 0x10000, too. 174 */ 175 if (pentry == KERN_IMAGE_START || pentry == 0x800) { 176 ipl->start_addr = KERN_IMAGE_START; 177 /* Overwrite parameters in the kernel image, which are "rom" */ 178 strcpy(rom_ptr(KERN_PARM_AREA), ipl->cmdline); 179 } else { 180 ipl->start_addr = pentry; 181 } 182 183 if (ipl->initrd) { 184 ram_addr_t initrd_offset; 185 int initrd_size; 186 187 initrd_offset = INITRD_START; 188 while (kernel_size + 0x100000 > initrd_offset) { 189 initrd_offset += 0x100000; 190 } 191 initrd_size = load_image_targphys(ipl->initrd, initrd_offset, 192 ram_size - initrd_offset); 193 if (initrd_size == -1) { 194 fprintf(stderr, "qemu: could not load initrd '%s'\n", 195 ipl->initrd); 196 exit(1); 197 } 198 199 /* 200 * we have to overwrite values in the kernel image, 201 * which are "rom" 202 */ 203 stq_p(rom_ptr(INITRD_PARM_START), initrd_offset); 204 stq_p(rom_ptr(INITRD_PARM_SIZE), initrd_size); 205 } 206 } 207 return 0; 208 } 209 210 static Property s390_ipl_properties[] = { 211 DEFINE_PROP_STRING("kernel", S390IPLState, kernel), 212 DEFINE_PROP_STRING("initrd", S390IPLState, initrd), 213 DEFINE_PROP_STRING("cmdline", S390IPLState, cmdline), 214 DEFINE_PROP_STRING("firmware", S390IPLState, firmware), 215 DEFINE_PROP_BOOL("enforce_bios", S390IPLState, enforce_bios, false), 216 DEFINE_PROP_END_OF_LIST(), 217 }; 218 219 /* 220 * In addition to updating the iplstate, this function returns: 221 * - 0 if system was ipled with external kernel 222 * - -1 if no valid boot device was found 223 * - ccw id of the boot device otherwise 224 */ 225 static uint64_t s390_update_iplstate(CPUS390XState *env, S390IPLState *ipl) 226 { 227 DeviceState *dev_st; 228 229 if (ipl->iplb_valid) { 230 ipl->cssid = 0; 231 ipl->ssid = 0; 232 ipl->devno = ipl->iplb.devno; 233 goto out; 234 } 235 236 if (ipl->kernel) { 237 return 0; 238 } 239 240 dev_st = get_boot_device(0); 241 if (dev_st) { 242 VirtioCcwDevice *ccw_dev = (VirtioCcwDevice *) object_dynamic_cast( 243 OBJECT(qdev_get_parent_bus(dev_st)->parent), 244 TYPE_VIRTIO_CCW_DEVICE); 245 if (ccw_dev) { 246 ipl->cssid = ccw_dev->sch->cssid; 247 ipl->ssid = ccw_dev->sch->ssid; 248 ipl->devno = ccw_dev->sch->devno; 249 goto out; 250 } 251 } 252 253 return -1; 254 out: 255 return ipl->cssid << 24 | ipl->ssid << 16 | ipl->devno; 256 } 257 258 int s390_ipl_update_diag308(IplParameterBlock *iplb) 259 { 260 S390IPLState *ipl; 261 262 ipl = S390_IPL(object_resolve_path(TYPE_S390_IPL, NULL)); 263 if (ipl) { 264 ipl->iplb = *iplb; 265 ipl->iplb_valid = true; 266 return 0; 267 } 268 return -1; 269 } 270 271 IplParameterBlock *s390_ipl_get_iplb(void) 272 { 273 S390IPLState *ipl; 274 275 ipl = S390_IPL(object_resolve_path(TYPE_S390_IPL, NULL)); 276 if (!ipl || !ipl->iplb_valid) { 277 return NULL; 278 } 279 return &ipl->iplb; 280 } 281 282 void s390_reipl_request(void) 283 { 284 S390IPLState *ipl; 285 286 ipl = S390_IPL(object_resolve_path(TYPE_S390_IPL, NULL)); 287 ipl->reipl_requested = true; 288 qemu_system_reset_request(); 289 } 290 291 static void s390_ipl_reset(DeviceState *dev) 292 { 293 S390IPLState *ipl = S390_IPL(dev); 294 S390CPU *cpu = S390_CPU(qemu_get_cpu(0)); 295 CPUS390XState *env = &cpu->env; 296 297 env->psw.addr = ipl->start_addr; 298 env->psw.mask = IPL_PSW_MASK; 299 300 if (!ipl->reipl_requested) { 301 ipl->iplb_valid = false; 302 } 303 ipl->reipl_requested = false; 304 305 if (!ipl->kernel || ipl->iplb_valid) { 306 env->psw.addr = ipl->bios_start_addr; 307 env->regs[7] = s390_update_iplstate(env, ipl); 308 } 309 310 s390_cpu_set_state(CPU_STATE_OPERATING, cpu); 311 } 312 313 static void s390_ipl_class_init(ObjectClass *klass, void *data) 314 { 315 DeviceClass *dc = DEVICE_CLASS(klass); 316 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass); 317 318 k->init = s390_ipl_init; 319 dc->props = s390_ipl_properties; 320 dc->reset = s390_ipl_reset; 321 dc->vmsd = &vmstate_ipl; 322 } 323 324 static const TypeInfo s390_ipl_info = { 325 .class_init = s390_ipl_class_init, 326 .parent = TYPE_SYS_BUS_DEVICE, 327 .name = "s390-ipl", 328 .instance_size = sizeof(S390IPLState), 329 }; 330 331 static void s390_ipl_register_types(void) 332 { 333 type_register_static(&s390_ipl_info); 334 } 335 336 type_init(s390_ipl_register_types) 337