| 1 | /* |
| 2 | * S390 virtio-ccw loading program |
| 3 | * |
| 4 | * Copyright (c) 2013 Alexander Graf <agraf@suse.de> |
| 5 | * |
| 6 | * This work is licensed under the terms of the GNU GPL, version 2 or (at |
| 7 | * your option) any later version. See the COPYING file in the top-level |
| 8 | * directory. |
| 9 | */ |
| 10 | |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <stdio.h> |
| 14 | #include "helper.h" |
| 15 | #include "s390-arch.h" |
| 16 | #include "s390-ccw.h" |
| 17 | #include "cio.h" |
| 18 | #include "virtio.h" |
| 19 | #include "virtio-scsi.h" |
| 20 | #include "dasd-ipl.h" |
| 21 | #include "clp.h" |
| 22 | #include "virtio-pci.h" |
| 23 | #include "secure-ipl.h" |
| 24 | |
| 25 | static SubChannelId blk_schid = { .one = 1 }; |
| 26 | static char loadparm_str[LOADPARM_LEN + 1]; |
| 27 | QemuIplParameters qipl; |
| 28 | /* Ensure that IPLB and IIRB are page aligned and sequential in memory */ |
| 29 | IplBlocks ipl_blocks __attribute__((__aligned__(PAGE_SIZE))); |
| 30 | IplParameterBlock *iplb; |
| 31 | bool have_iplb; |
| 32 | static uint16_t cutype; |
| 33 | LowCore *lowcore; /* Yes, this *is* a pointer to address 0 */ |
| 34 | ZiplBootMode boot_mode; |
| 35 | |
| 36 | #define LOADPARM_PROMPT "PROMPT " |
| 37 | #define LOADPARM_EMPTY " " |
| 38 | #define BOOT_MENU_FLAG_MASK (QIPL_FLAG_BM_OPTS_CMD | QIPL_FLAG_BM_OPTS_ZIPL) |
| 39 | |
| 40 | /* |
| 41 | * Principles of Operations (SA22-7832-09) chapter 17 requires that |
| 42 | * a subsystem-identification is at 184-187 and bytes 188-191 are zero |
| 43 | * after list-directed-IPL and ccw-IPL. |
| 44 | */ |
| 45 | void write_subsystem_identification(void) |
| 46 | { |
| 47 | if (cutype == CU_TYPE_VIRTIO && virtio_get_device_type() == VIRTIO_ID_NET) { |
| 48 | lowcore->subchannel_id = net_schid.sch_id; |
| 49 | lowcore->subchannel_nr = net_schid.sch_no; |
| 50 | } else { |
| 51 | lowcore->subchannel_id = blk_schid.sch_id; |
| 52 | lowcore->subchannel_nr = blk_schid.sch_no; |
| 53 | } |
| 54 | lowcore->io_int_parm = 0; |
| 55 | } |
| 56 | |
| 57 | void write_iplb_location(void) |
| 58 | { |
| 59 | if (virtio_is_supported(virtio_get_device()) && |
| 60 | virtio_get_device_type() != VIRTIO_ID_NET) { |
| 61 | lowcore->ptr_iplb = ptr2u32(iplb); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | static void copy_qipl(void) |
| 66 | { |
| 67 | QemuIplParameters *early_qipl = (QemuIplParameters *)QIPL_ADDRESS; |
| 68 | memcpy(&qipl, early_qipl, sizeof(QemuIplParameters)); |
| 69 | } |
| 70 | |
| 71 | unsigned int get_loadparm_index(void) |
| 72 | { |
| 73 | return atoi(loadparm_str); |
| 74 | } |
| 75 | |
| 76 | static int is_dev_possibly_bootable(int dev_no, int sch_no) |
| 77 | { |
| 78 | bool is_virtio; |
| 79 | Schib schib; |
| 80 | int r; |
| 81 | VDev *vdev = virtio_get_device(); |
| 82 | |
| 83 | blk_schid.sch_no = sch_no; |
| 84 | r = stsch_err(blk_schid, &schib); |
| 85 | if (r == 3 || r == -EIO) { |
| 86 | return -ENODEV; |
| 87 | } |
| 88 | if (!schib.pmcw.dnv) { |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | enable_subchannel(blk_schid); |
| 93 | cutype = cu_type(blk_schid); |
| 94 | if (cutype == CU_TYPE_UNKNOWN) { |
| 95 | return -EIO; |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | * Note: we always have to run virtio_is_supported() here to make |
| 100 | * sure that the vdev.senseid data gets pre-initialized correctly |
| 101 | */ |
| 102 | vdev->schid = blk_schid; |
| 103 | is_virtio = virtio_is_supported(vdev); |
| 104 | |
| 105 | /* No specific devno given, just return whether the device is possibly bootable */ |
| 106 | if (dev_no < 0) { |
| 107 | switch (cutype) { |
| 108 | case CU_TYPE_VIRTIO: |
| 109 | if (is_virtio) { |
| 110 | /* |
| 111 | * Skip net devices since no IPLB is created and therefore |
| 112 | * no network bootloader has been loaded |
| 113 | */ |
| 114 | if (virtio_get_device_type() != VIRTIO_ID_NET) { |
| 115 | return true; |
| 116 | } |
| 117 | } |
| 118 | return false; |
| 119 | case CU_TYPE_DASD_3990: |
| 120 | case CU_TYPE_DASD_2107: |
| 121 | return true; |
| 122 | default: |
| 123 | return false; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /* Caller asked for a specific devno */ |
| 128 | if (schib.pmcw.dev == dev_no) { |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * Find the subchannel connected to the given device (dev_no) and fill in the |
| 137 | * subchannel information block (schib) with the connected subchannel's info. |
| 138 | * NOTE: The global variable blk_schid is updated to contain the subchannel |
| 139 | * information. |
| 140 | * |
| 141 | * If the caller gives dev_no=-1 then the user did not specify a boot device. |
| 142 | * In this case we'll just use the first potentially bootable device we find. |
| 143 | */ |
| 144 | static bool find_subch(int dev_no) |
| 145 | { |
| 146 | int i, r; |
| 147 | |
| 148 | for (i = 0; i < 0x10000; i++) { |
| 149 | r = is_dev_possibly_bootable(dev_no, i); |
| 150 | if (r < 0) { |
| 151 | break; |
| 152 | } |
| 153 | if (r == true) { |
| 154 | return true; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | static bool find_fid(uint32_t fid) |
| 162 | { |
| 163 | ClpFhListEntry entry; |
| 164 | VDev *vdev = virtio_get_device(); |
| 165 | |
| 166 | if (find_pci_function(fid, &entry)) { |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | vdev->pci_fh = entry.fh; |
| 171 | vdev->vendor_id = entry.vendor_id; |
| 172 | virtio_pci_id2type(vdev, entry.device_id); |
| 173 | |
| 174 | return vdev->dev_type != 0; |
| 175 | } |
| 176 | |
| 177 | static void menu_setup(VDev *vdev) |
| 178 | { |
| 179 | if (memcmp(loadparm_str, LOADPARM_PROMPT, LOADPARM_LEN) == 0) { |
| 180 | menu_set_parms(QIPL_FLAG_BM_OPTS_CMD, 0); |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | /* If loadparm was set to any other value, then do not enable menu */ |
| 185 | if (memcmp(loadparm_str, LOADPARM_EMPTY, LOADPARM_LEN) != 0) { |
| 186 | menu_set_parms(qipl.qipl_flags & ~BOOT_MENU_FLAG_MASK, 0); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | switch (vdev->ipl_type) { |
| 191 | case S390_IPL_TYPE_CCW: |
| 192 | case S390_IPL_TYPE_PCI: |
| 193 | case S390_IPL_TYPE_QEMU_SCSI: |
| 194 | menu_set_parms(qipl.qipl_flags & BOOT_MENU_FLAG_MASK, |
| 195 | qipl.boot_menu_timeout); |
| 196 | /* fall through */ |
| 197 | default: |
| 198 | return; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Initialize the channel I/O subsystem so we can talk to our ipl/boot device. |
| 204 | */ |
| 205 | static void css_setup(void) |
| 206 | { |
| 207 | /* |
| 208 | * Unconditionally enable mss support. In every sane configuration this |
| 209 | * will succeed; and even if it doesn't, stsch_err() can handle it. |
| 210 | */ |
| 211 | enable_mss_facility(); |
| 212 | } |
| 213 | |
| 214 | /* |
| 215 | * Collect various pieces of information from the hypervisor/hardware that |
| 216 | * we'll use to determine exactly how we'll boot. |
| 217 | */ |
| 218 | static void boot_setup(void) |
| 219 | { |
| 220 | char lpmsg[] = "LOADPARM=[________]\n"; |
| 221 | VDev *vdev = virtio_get_device(); |
| 222 | |
| 223 | if (have_iplb && memcmp(iplb->loadparm, NO_LOADPARM, LOADPARM_LEN) != 0) { |
| 224 | ebcdic_to_ascii((char *) iplb->loadparm, loadparm_str, LOADPARM_LEN); |
| 225 | } else { |
| 226 | sclp_get_loadparm_ascii(loadparm_str); |
| 227 | } |
| 228 | |
| 229 | if (have_iplb) { |
| 230 | vdev->ipl_type = iplb->pbt; |
| 231 | menu_setup(vdev); |
| 232 | } else { |
| 233 | vdev->ipl_type = QEMU_DEFAULT_IPL; |
| 234 | } |
| 235 | |
| 236 | memcpy(lpmsg + 10, loadparm_str, 8); |
| 237 | puts(lpmsg); |
| 238 | |
| 239 | /* |
| 240 | * Clear out any potential S390EP magic (see jump_to_low_kernel()), |
| 241 | * so we don't taint our decision-making process during a reboot. |
| 242 | */ |
| 243 | memset((char *)S390EP, 0, 6); |
| 244 | } |
| 245 | |
| 246 | static bool find_boot_device(void) |
| 247 | { |
| 248 | VDev *vdev = virtio_get_device(); |
| 249 | bool found = false; |
| 250 | |
| 251 | switch (vdev->ipl_type) { |
| 252 | case S390_IPL_TYPE_CCW: |
| 253 | vdev->scsi_device_selected = false; |
| 254 | debug_print_int("device no. ", iplb->ccw.devno); |
| 255 | blk_schid.ssid = iplb->ccw.ssid & 0x3; |
| 256 | debug_print_int("ssid ", blk_schid.ssid); |
| 257 | found = find_subch(iplb->ccw.devno); |
| 258 | break; |
| 259 | case S390_IPL_TYPE_QEMU_SCSI: |
| 260 | vdev->scsi_device_selected = true; |
| 261 | vdev->selected_scsi_device.channel = iplb->scsi.channel; |
| 262 | vdev->selected_scsi_device.target = iplb->scsi.target; |
| 263 | vdev->selected_scsi_device.lun = iplb->scsi.lun; |
| 264 | blk_schid.ssid = iplb->scsi.ssid & 0x3; |
| 265 | found = find_subch(iplb->scsi.devno); |
| 266 | break; |
| 267 | case S390_IPL_TYPE_PCI: |
| 268 | found = find_fid(iplb->pci.fid); |
| 269 | break; |
| 270 | default: |
| 271 | puts("Unsupported IPLB"); |
| 272 | } |
| 273 | |
| 274 | return found; |
| 275 | } |
| 276 | |
| 277 | static int virtio_setup(void) |
| 278 | { |
| 279 | VDev *vdev = virtio_get_device(); |
| 280 | vdev->is_cdrom = false; |
| 281 | int ret; |
| 282 | |
| 283 | switch (vdev->dev_type) { |
| 284 | case VIRTIO_ID_NET: |
| 285 | puts("Network boot device detected"); |
| 286 | return 0; |
| 287 | case VIRTIO_ID_BLOCK: |
| 288 | ret = virtio_blk_setup_device(vdev); |
| 289 | break; |
| 290 | case VIRTIO_ID_SCSI: |
| 291 | ret = virtio_scsi_setup_device(vdev); |
| 292 | break; |
| 293 | default: |
| 294 | puts("\n! No IPL device available !\n"); |
| 295 | return -1; |
| 296 | } |
| 297 | |
| 298 | if (!ret && !virtio_ipl_disk_is_valid()) { |
| 299 | puts("No valid IPL device detected"); |
| 300 | return -ENODEV; |
| 301 | } |
| 302 | |
| 303 | return ret; |
| 304 | } |
| 305 | |
| 306 | static void ipl_ccw_device(void) |
| 307 | { |
| 308 | switch (cutype) { |
| 309 | case CU_TYPE_DASD_3990: |
| 310 | case CU_TYPE_DASD_2107: |
| 311 | dasd_ipl(blk_schid, cutype); |
| 312 | break; |
| 313 | case CU_TYPE_VIRTIO: |
| 314 | if (virtio_setup() == 0) { |
| 315 | zipl_load(); /* only return on error */ |
| 316 | virtio_reset(virtio_get_device()); |
| 317 | } |
| 318 | break; |
| 319 | default: |
| 320 | printf("Cannot boot CCW device with cu type 0x%X\n", cutype); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | static void ipl_pci_device(void) |
| 325 | { |
| 326 | VDev *vdev = virtio_get_device(); |
| 327 | vdev->is_cdrom = false; |
| 328 | vdev->scsi_device_selected = false; |
| 329 | |
| 330 | if (virtio_pci_setup_device()) { |
| 331 | return; |
| 332 | } |
| 333 | |
| 334 | switch (vdev->dev_type) { |
| 335 | case VIRTIO_ID_BLOCK: |
| 336 | if (virtio_setup() == 0) { |
| 337 | zipl_load(); /* only return on error */ |
| 338 | virtio_reset(virtio_get_device()); |
| 339 | } |
| 340 | break; |
| 341 | default: |
| 342 | printf("Cannot boot PCI device type 0x%X\n", vdev->dev_type); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | static void ipl_boot_device(void) |
| 347 | { |
| 348 | switch (virtio_get_device()->ipl_type) { |
| 349 | case S390_IPL_TYPE_QEMU_SCSI: |
| 350 | case S390_IPL_TYPE_CCW: |
| 351 | ipl_ccw_device(); |
| 352 | break; |
| 353 | case S390_IPL_TYPE_PCI: |
| 354 | ipl_pci_device(); |
| 355 | break; |
| 356 | default: |
| 357 | puts("Unrecognized IPL type!"); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | * No boot device has been specified, so we have to scan through the |
| 363 | * channels to find one. |
| 364 | */ |
| 365 | static void probe_boot_device(void) |
| 366 | { |
| 367 | int ssid, sch_no, ret; |
| 368 | |
| 369 | for (ssid = 0; ssid < 0x3; ssid++) { |
| 370 | blk_schid.ssid = ssid; |
| 371 | for (sch_no = 0; sch_no < 0x10000; sch_no++) { |
| 372 | ret = is_dev_possibly_bootable(-1, sch_no); |
| 373 | if (ret < 0) { |
| 374 | break; |
| 375 | } |
| 376 | if (ret == true) { |
| 377 | ipl_boot_device(); /* Only returns if unsuccessful */ |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | puts("Could not find a suitable boot device (none specified)"); |
| 383 | } |
| 384 | |
| 385 | void main(void) |
| 386 | { |
| 387 | int vcssb_len; |
| 388 | |
| 389 | iplb = &ipl_blocks.iplb; |
| 390 | |
| 391 | copy_qipl(); |
| 392 | sclp_setup(); |
| 393 | css_setup(); |
| 394 | have_iplb = store_iplb(iplb); |
| 395 | if (!have_iplb) { |
| 396 | boot_setup(); |
| 397 | probe_boot_device(); |
| 398 | } |
| 399 | |
| 400 | boot_mode = get_boot_mode(iplb->hdr_flags); |
| 401 | switch (boot_mode) { |
| 402 | case ZIPL_BOOT_MODE_SECURE: |
| 403 | case ZIPL_BOOT_MODE_SECURE_AUDIT: |
| 404 | if (!secure_ipl_supported()) { |
| 405 | panic("Unable to boot in secure/audit mode"); |
| 406 | } |
| 407 | |
| 408 | vcssb_len = zipl_secure_get_vcssb(); |
| 409 | if (vcssb_len == 0) { |
| 410 | panic("Failed to query certificate storage information!"); |
| 411 | } |
| 412 | |
| 413 | if (vcssb_len == VCSSB_NO_VC) { |
| 414 | panic("Need at least one certificate for secure boot!"); |
| 415 | } |
| 416 | break; |
| 417 | default: |
| 418 | break; |
| 419 | } |
| 420 | |
| 421 | while (have_iplb) { |
| 422 | boot_setup(); |
| 423 | if (have_iplb && find_boot_device()) { |
| 424 | ipl_boot_device(); |
| 425 | } |
| 426 | have_iplb = load_next_iplb(); |
| 427 | } |
| 428 | |
| 429 | panic("No suitable device for IPL. Halting..."); |
| 430 | |
| 431 | } |