qemu/pc-bios/s390-ccw/bootmap.c
<<
>>
Prefs
   1/*
   2 * QEMU S390 bootmap interpreter
   3 *
   4 * Copyright (c) 2009 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 "s390-ccw.h"
  12#include "bootmap.h"
  13#include "virtio.h"
  14
  15#ifdef DEBUG
  16/* #define DEBUG_FALLBACK */
  17#endif
  18
  19#ifdef DEBUG_FALLBACK
  20#define dputs(txt) \
  21    do { sclp_print("zipl: " txt); } while (0)
  22#else
  23#define dputs(fmt, ...) \
  24    do { } while (0)
  25#endif
  26
  27/* Scratch space */
  28static uint8_t sec[MAX_SECTOR_SIZE*4] __attribute__((__aligned__(PAGE_SIZE)));
  29
  30typedef struct ResetInfo {
  31    uint32_t ipl_mask;
  32    uint32_t ipl_addr;
  33    uint32_t ipl_continue;
  34} ResetInfo;
  35
  36static ResetInfo save;
  37
  38static void jump_to_IPL_2(void)
  39{
  40    ResetInfo *current = 0;
  41
  42    void (*ipl)(void) = (void *) (uint64_t) current->ipl_continue;
  43    *current = save;
  44    ipl(); /* should not return */
  45}
  46
  47static void jump_to_IPL_code(uint64_t address)
  48{
  49    /* store the subsystem information _after_ the bootmap was loaded */
  50    write_subsystem_identification();
  51    /*
  52     * The IPL PSW is at address 0. We also must not overwrite the
  53     * content of non-BIOS memory after we loaded the guest, so we
  54     * save the original content and restore it in jump_to_IPL_2.
  55     */
  56    ResetInfo *current = 0;
  57
  58    save = *current;
  59    current->ipl_addr = (uint32_t) (uint64_t) &jump_to_IPL_2;
  60    current->ipl_continue = address & 0x7fffffff;
  61
  62    debug_print_int("set IPL addr to", current->ipl_continue);
  63
  64    /* Ensure the guest output starts fresh */
  65    sclp_print("\n");
  66
  67    /*
  68     * HACK ALERT.
  69     * We use the load normal reset to keep r15 unchanged. jump_to_IPL_2
  70     * can then use r15 as its stack pointer.
  71     */
  72    asm volatile("lghi 1,1\n\t"
  73                 "diag 1,1,0x308\n\t"
  74                 : : : "1", "memory");
  75    panic("\n! IPL returns !\n");
  76}
  77
  78/***********************************************************************
  79 * IPL an ECKD DASD (CDL or LDL/CMS format)
  80 */
  81
  82static unsigned char _bprs[8*1024]; /* guessed "max" ECKD sector size */
  83static const int max_bprs_entries = sizeof(_bprs) / sizeof(ExtEckdBlockPtr);
  84
  85static inline void verify_boot_info(BootInfo *bip)
  86{
  87    IPL_assert(magic_match(bip->magic, ZIPL_MAGIC), "No zIPL sig in BootInfo");
  88    IPL_assert(bip->version == BOOT_INFO_VERSION, "Wrong zIPL version");
  89    IPL_assert(bip->bp_type == BOOT_INFO_BP_TYPE_IPL, "DASD is not for IPL");
  90    IPL_assert(bip->dev_type == BOOT_INFO_DEV_TYPE_ECKD, "DASD is not ECKD");
  91    IPL_assert(bip->flags == BOOT_INFO_FLAGS_ARCH, "Not for this arch");
  92    IPL_assert(block_size_ok(bip->bp.ipl.bm_ptr.eckd.bptr.size),
  93               "Bad block size in zIPL section of the 1st record.");
  94}
  95
  96static block_number_t eckd_block_num(BootMapPointer *p)
  97{
  98    const uint64_t sectors = virtio_get_sectors();
  99    const uint64_t heads = virtio_get_heads();
 100    const uint64_t cylinder = p->eckd.cylinder
 101                            + ((p->eckd.head & 0xfff0) << 12);
 102    const uint64_t head = p->eckd.head & 0x000f;
 103    const block_number_t block = sectors * heads * cylinder
 104                               + sectors * head
 105                               + p->eckd.sector
 106                               - 1; /* block nr starts with zero */
 107    return block;
 108}
 109
 110static bool eckd_valid_address(BootMapPointer *p)
 111{
 112    const uint64_t head = p->eckd.head & 0x000f;
 113
 114    if (head >= virtio_get_heads()
 115        ||  p->eckd.sector > virtio_get_sectors()
 116        ||  p->eckd.sector <= 0) {
 117        return false;
 118    }
 119
 120    if (!virtio_guessed_disk_nature() &&
 121        eckd_block_num(p) >= virtio_get_blocks()) {
 122        return false;
 123    }
 124
 125    return true;
 126}
 127
 128static block_number_t load_eckd_segments(block_number_t blk, uint64_t *address)
 129{
 130    block_number_t block_nr;
 131    int j, rc;
 132    BootMapPointer *bprs = (void *)_bprs;
 133    bool more_data;
 134
 135    memset(_bprs, FREE_SPACE_FILLER, sizeof(_bprs));
 136    read_block(blk, bprs, "BPRS read failed");
 137
 138    do {
 139        more_data = false;
 140        for (j = 0;; j++) {
 141            block_nr = eckd_block_num((void *)&(bprs[j].xeckd));
 142            if (is_null_block_number(block_nr)) { /* end of chunk */
 143                break;
 144            }
 145
 146            /* we need the updated blockno for the next indirect entry
 147             * in the chain, but don't want to advance address
 148             */
 149            if (j == (max_bprs_entries - 1)) {
 150                break;
 151            }
 152
 153            IPL_assert(block_size_ok(bprs[j].xeckd.bptr.size),
 154                       "bad chunk block size");
 155            IPL_assert(eckd_valid_address(&bprs[j]), "bad chunk ECKD addr");
 156
 157            if ((bprs[j].xeckd.bptr.count == 0) && unused_space(&(bprs[j+1]),
 158                sizeof(EckdBlockPtr))) {
 159                /* This is a "continue" pointer.
 160                 * This ptr should be the last one in the current
 161                 * script section.
 162                 * I.e. the next ptr must point to the unused memory area
 163                 */
 164                memset(_bprs, FREE_SPACE_FILLER, sizeof(_bprs));
 165                read_block(block_nr, bprs, "BPRS continuation read failed");
 166                more_data = true;
 167                break;
 168            }
 169
 170            /* Load (count+1) blocks of code at (block_nr)
 171             * to memory (address).
 172             */
 173            rc = virtio_read_many(block_nr, (void *)(*address),
 174                                  bprs[j].xeckd.bptr.count+1);
 175            IPL_assert(rc == 0, "code chunk read failed");
 176
 177            *address += (bprs[j].xeckd.bptr.count+1) * virtio_get_block_size();
 178        }
 179    } while (more_data);
 180    return block_nr;
 181}
 182
 183static void run_eckd_boot_script(block_number_t mbr_block_nr)
 184{
 185    int i;
 186    block_number_t block_nr;
 187    uint64_t address;
 188    ScsiMbr *scsi_mbr = (void *)sec;
 189    BootMapScript *bms = (void *)sec;
 190
 191    memset(sec, FREE_SPACE_FILLER, sizeof(sec));
 192    read_block(mbr_block_nr, sec, "Cannot read MBR");
 193
 194    block_nr = eckd_block_num((void *)&(scsi_mbr->blockptr));
 195
 196    memset(sec, FREE_SPACE_FILLER, sizeof(sec));
 197    read_block(block_nr, sec, "Cannot read Boot Map Script");
 198
 199    for (i = 0; bms->entry[i].type == BOOT_SCRIPT_LOAD; i++) {
 200        address = bms->entry[i].address.load_address;
 201        block_nr = eckd_block_num(&(bms->entry[i].blkptr));
 202
 203        do {
 204            block_nr = load_eckd_segments(block_nr, &address);
 205        } while (block_nr != -1);
 206    }
 207
 208    IPL_assert(bms->entry[i].type == BOOT_SCRIPT_EXEC,
 209               "Unknown script entry type");
 210    jump_to_IPL_code(bms->entry[i].address.load_address); /* no return */
 211}
 212
 213static void ipl_eckd_cdl(void)
 214{
 215    XEckdMbr *mbr;
 216    Ipl2 *ipl2 = (void *)sec;
 217    IplVolumeLabel *vlbl = (void *)sec;
 218    block_number_t block_nr;
 219
 220    /* we have just read the block #0 and recognized it as "IPL1" */
 221    sclp_print("CDL\n");
 222
 223    memset(sec, FREE_SPACE_FILLER, sizeof(sec));
 224    read_block(1, ipl2, "Cannot read IPL2 record at block 1");
 225
 226    mbr = &ipl2->u.x.mbr;
 227    IPL_assert(magic_match(mbr, ZIPL_MAGIC), "No zIPL section in IPL2 record.");
 228    IPL_assert(block_size_ok(mbr->blockptr.xeckd.bptr.size),
 229               "Bad block size in zIPL section of IPL2 record.");
 230    IPL_assert(mbr->dev_type == DEV_TYPE_ECKD,
 231               "Non-ECKD device type in zIPL section of IPL2 record.");
 232
 233    /* save pointer to Boot Script */
 234    block_nr = eckd_block_num((void *)&(mbr->blockptr));
 235
 236    memset(sec, FREE_SPACE_FILLER, sizeof(sec));
 237    read_block(2, vlbl, "Cannot read Volume Label at block 2");
 238    IPL_assert(magic_match(vlbl->key, VOL1_MAGIC),
 239               "Invalid magic of volume label block");
 240    IPL_assert(magic_match(vlbl->f.key, VOL1_MAGIC),
 241               "Invalid magic of volser block");
 242    print_volser(vlbl->f.volser);
 243
 244    run_eckd_boot_script(block_nr);
 245    /* no return */
 246}
 247
 248static void print_eckd_ldl_msg(ECKD_IPL_mode_t mode)
 249{
 250    LDL_VTOC *vlbl = (void *)sec; /* already read, 3rd block */
 251    char msg[4] = { '?', '.', '\n', '\0' };
 252
 253    sclp_print((mode == ECKD_CMS) ? "CMS" : "LDL");
 254    sclp_print(" version ");
 255    switch (vlbl->LDL_version) {
 256    case LDL1_VERSION:
 257        msg[0] = '1';
 258        break;
 259    case LDL2_VERSION:
 260        msg[0] = '2';
 261        break;
 262    default:
 263        msg[0] = vlbl->LDL_version;
 264        msg[0] &= 0x0f; /* convert EBCDIC   */
 265        msg[0] |= 0x30; /* to ASCII (digit) */
 266        msg[1] = '?';
 267        break;
 268    }
 269    sclp_print(msg);
 270    print_volser(vlbl->volser);
 271}
 272
 273static void ipl_eckd_ldl(ECKD_IPL_mode_t mode)
 274{
 275    block_number_t block_nr;
 276    BootInfo *bip = (void *)(sec + 0x70); /* BootInfo is MBR for LDL */
 277
 278    if (mode != ECKD_LDL_UNLABELED) {
 279        print_eckd_ldl_msg(mode);
 280    }
 281
 282    /* DO NOT read BootMap pointer (only one, xECKD) at block #2 */
 283
 284    memset(sec, FREE_SPACE_FILLER, sizeof(sec));
 285    read_block(0, sec, "Cannot read block 0 to grab boot info.");
 286    if (mode == ECKD_LDL_UNLABELED) {
 287        if (!magic_match(bip->magic, ZIPL_MAGIC)) {
 288            return; /* not applicable layout */
 289        }
 290        sclp_print("unlabeled LDL.\n");
 291    }
 292    verify_boot_info(bip);
 293
 294    block_nr = eckd_block_num((void *)&(bip->bp.ipl.bm_ptr.eckd.bptr));
 295    run_eckd_boot_script(block_nr);
 296    /* no return */
 297}
 298
 299static void print_eckd_msg(void)
 300{
 301    char msg[] = "Using ECKD scheme (block size *****), ";
 302    char *p = &msg[34], *q = &msg[30];
 303    int n = virtio_get_block_size();
 304
 305    /* Fill in the block size and show up the message */
 306    if (n > 0 && n <= 99999) {
 307        while (n) {
 308            *p-- = '0' + (n % 10);
 309            n /= 10;
 310        }
 311        while (p >= q) {
 312            *p-- = ' ';
 313        }
 314    }
 315    sclp_print(msg);
 316}
 317
 318static void ipl_eckd(void)
 319{
 320    ScsiMbr *mbr = (void *)sec;
 321    LDL_VTOC *vlbl = (void *)sec;
 322
 323    print_eckd_msg();
 324
 325    /* Grab the MBR again */
 326    memset(sec, FREE_SPACE_FILLER, sizeof(sec));
 327    read_block(0, mbr, "Cannot read block 0 on DASD");
 328
 329    if (magic_match(mbr->magic, IPL1_MAGIC)) {
 330        ipl_eckd_cdl(); /* no return */
 331    }
 332
 333    /* LDL/CMS? */
 334    memset(sec, FREE_SPACE_FILLER, sizeof(sec));
 335    read_block(2, vlbl, "Cannot read block 2");
 336
 337    if (magic_match(vlbl->magic, CMS1_MAGIC)) {
 338        ipl_eckd_ldl(ECKD_CMS); /* no return */
 339    }
 340    if (magic_match(vlbl->magic, LNX1_MAGIC)) {
 341        ipl_eckd_ldl(ECKD_LDL); /* no return */
 342    }
 343
 344    ipl_eckd_ldl(ECKD_LDL_UNLABELED); /* it still may return */
 345    /*
 346     * Ok, it is not a LDL by any means.
 347     * It still might be a CDL with zero record keys for IPL1 and IPL2
 348     */
 349    ipl_eckd_cdl();
 350}
 351
 352/***********************************************************************
 353 * IPL a SCSI disk
 354 */
 355
 356static void zipl_load_segment(ComponentEntry *entry)
 357{
 358    const int max_entries = (MAX_SECTOR_SIZE / sizeof(ScsiBlockPtr));
 359    ScsiBlockPtr *bprs = (void *)sec;
 360    const int bprs_size = sizeof(sec);
 361    block_number_t blockno;
 362    uint64_t address;
 363    int i;
 364    char err_msg[] = "zIPL failed to read BPRS at 0xZZZZZZZZZZZZZZZZ";
 365    char *blk_no = &err_msg[30]; /* where to print blockno in (those ZZs) */
 366
 367    blockno = entry->data.blockno;
 368    address = entry->load_address;
 369
 370    debug_print_int("loading segment at block", blockno);
 371    debug_print_int("addr", address);
 372
 373    do {
 374        memset(bprs, FREE_SPACE_FILLER, bprs_size);
 375        fill_hex_val(blk_no, &blockno, sizeof(blockno));
 376        read_block(blockno, bprs, err_msg);
 377
 378        for (i = 0;; i++) {
 379            uint64_t *cur_desc = (void *)&bprs[i];
 380
 381            blockno = bprs[i].blockno;
 382            if (!blockno) {
 383                break;
 384            }
 385
 386            /* we need the updated blockno for the next indirect entry in the
 387               chain, but don't want to advance address */
 388            if (i == (max_entries - 1)) {
 389                break;
 390            }
 391
 392            if (bprs[i].blockct == 0 && unused_space(&bprs[i + 1],
 393                sizeof(ScsiBlockPtr))) {
 394                /* This is a "continue" pointer.
 395                 * This ptr is the last one in the current script section.
 396                 * I.e. the next ptr must point to the unused memory area.
 397                 * The blockno is not zero, so the upper loop must continue
 398                 * reading next section of BPRS.
 399                 */
 400                break;
 401            }
 402            address = virtio_load_direct(cur_desc[0], cur_desc[1], 0,
 403                                         (void *)address);
 404            IPL_assert(address != -1, "zIPL load segment failed");
 405        }
 406    } while (blockno);
 407}
 408
 409/* Run a zipl program */
 410static void zipl_run(ScsiBlockPtr *pte)
 411{
 412    ComponentHeader *header;
 413    ComponentEntry *entry;
 414    uint8_t tmp_sec[MAX_SECTOR_SIZE];
 415
 416    read_block(pte->blockno, tmp_sec, "Cannot read header");
 417    header = (ComponentHeader *)tmp_sec;
 418
 419    IPL_assert(magic_match(tmp_sec, ZIPL_MAGIC), "No zIPL magic in header");
 420    IPL_assert(header->type == ZIPL_COMP_HEADER_IPL, "Bad header type");
 421
 422    dputs("start loading images\n");
 423
 424    /* Load image(s) into RAM */
 425    entry = (ComponentEntry *)(&header[1]);
 426    while (entry->component_type == ZIPL_COMP_ENTRY_LOAD) {
 427        zipl_load_segment(entry);
 428
 429        entry++;
 430
 431        IPL_assert((uint8_t *)(&entry[1]) <= (tmp_sec + MAX_SECTOR_SIZE),
 432                   "Wrong entry value");
 433    }
 434
 435    IPL_assert(entry->component_type == ZIPL_COMP_ENTRY_EXEC, "No EXEC entry");
 436
 437    /* should not return */
 438    jump_to_IPL_code(entry->load_address);
 439}
 440
 441static void ipl_scsi(void)
 442{
 443    ScsiMbr *mbr = (void *)sec;
 444    uint8_t *ns, *ns_end;
 445    int program_table_entries = 0;
 446    const int pte_len = sizeof(ScsiBlockPtr);
 447    ScsiBlockPtr *prog_table_entry;
 448
 449    /* Grab the MBR */
 450    memset(sec, FREE_SPACE_FILLER, sizeof(sec));
 451    read_block(0, mbr, "Cannot read block 0");
 452
 453    if (!magic_match(mbr->magic, ZIPL_MAGIC)) {
 454        return;
 455    }
 456
 457    sclp_print("Using SCSI scheme.\n");
 458    debug_print_int("MBR Version", mbr->version_id);
 459    IPL_check(mbr->version_id == 1,
 460              "Unknown MBR layout version, assuming version 1");
 461    debug_print_int("program table", mbr->blockptr.blockno);
 462    IPL_assert(mbr->blockptr.blockno, "No Program Table");
 463
 464    /* Parse the program table */
 465    read_block(mbr->blockptr.blockno, sec,
 466               "Error reading Program Table");
 467
 468    IPL_assert(magic_match(sec, ZIPL_MAGIC), "No zIPL magic in PT");
 469
 470    ns_end = sec + virtio_get_block_size();
 471    for (ns = (sec + pte_len); (ns + pte_len) < ns_end; ns += pte_len) {
 472        prog_table_entry = (ScsiBlockPtr *)ns;
 473        if (!prog_table_entry->blockno) {
 474            break;
 475        }
 476
 477        program_table_entries++;
 478    }
 479
 480    debug_print_int("program table entries", program_table_entries);
 481
 482    IPL_assert(program_table_entries != 0, "Empty Program Table");
 483
 484    /* Run the default entry */
 485
 486    prog_table_entry = (ScsiBlockPtr *)(sec + pte_len);
 487
 488    zipl_run(prog_table_entry); /* no return */
 489}
 490
 491/***********************************************************************
 492 * IPL El Torito ISO9660 image or DVD
 493 */
 494
 495static bool is_iso_bc_entry_compatible(IsoBcSection *s)
 496{
 497    uint8_t *magic_sec = (uint8_t *)(sec + ISO_SECTOR_SIZE);
 498
 499    if (s->unused || !s->sector_count) {
 500        return false;
 501    }
 502    read_iso_sector(bswap32(s->load_rba), magic_sec,
 503                    "Failed to read image sector 0");
 504
 505    /* Checking bytes 8 - 32 for S390 Linux magic */
 506    return !_memcmp(magic_sec + 8, linux_s390_magic, 24);
 507}
 508
 509/* Location of the current sector of the directory */
 510static uint32_t sec_loc[ISO9660_MAX_DIR_DEPTH];
 511/* Offset in the current sector of the directory */
 512static uint32_t sec_offset[ISO9660_MAX_DIR_DEPTH];
 513/* Remained directory space in bytes */
 514static uint32_t dir_rem[ISO9660_MAX_DIR_DEPTH];
 515
 516static inline uint32_t iso_get_file_size(uint32_t load_rba)
 517{
 518    IsoVolDesc *vd = (IsoVolDesc *)sec;
 519    IsoDirHdr *cur_record = &vd->vd.primary.rootdir;
 520    uint8_t *temp = sec + ISO_SECTOR_SIZE;
 521    int level = 0;
 522
 523    read_iso_sector(ISO_PRIMARY_VD_SECTOR, sec,
 524                    "Failed to read ISO primary descriptor");
 525    sec_loc[0] = iso_733_to_u32(cur_record->ext_loc);
 526    dir_rem[0] = 0;
 527    sec_offset[0] = 0;
 528
 529    while (level >= 0) {
 530        IPL_assert(sec_offset[level] <= ISO_SECTOR_SIZE,
 531                   "Directory tree structure violation");
 532
 533        cur_record = (IsoDirHdr *)(temp + sec_offset[level]);
 534
 535        if (sec_offset[level] == 0) {
 536            read_iso_sector(sec_loc[level], temp,
 537                            "Failed to read ISO directory");
 538            if (dir_rem[level] == 0) {
 539                /* Skip self and parent records */
 540                dir_rem[level] = iso_733_to_u32(cur_record->data_len) -
 541                                 cur_record->dr_len;
 542                sec_offset[level] += cur_record->dr_len;
 543
 544                cur_record = (IsoDirHdr *)(temp + sec_offset[level]);
 545                dir_rem[level] -= cur_record->dr_len;
 546                sec_offset[level] += cur_record->dr_len;
 547                continue;
 548            }
 549        }
 550
 551        if (!cur_record->dr_len || sec_offset[level] == ISO_SECTOR_SIZE) {
 552            /* Zero-padding and/or the end of current sector */
 553            dir_rem[level] -= ISO_SECTOR_SIZE - sec_offset[level];
 554            sec_offset[level] = 0;
 555            sec_loc[level]++;
 556        } else {
 557            /* The directory record is valid */
 558            if (load_rba == iso_733_to_u32(cur_record->ext_loc)) {
 559                return iso_733_to_u32(cur_record->data_len);
 560            }
 561
 562            dir_rem[level] -= cur_record->dr_len;
 563            sec_offset[level] += cur_record->dr_len;
 564
 565            if (cur_record->file_flags & 0x2) {
 566                /* Subdirectory */
 567                if (level == ISO9660_MAX_DIR_DEPTH - 1) {
 568                    sclp_print("ISO-9660 directory depth limit exceeded\n");
 569                } else {
 570                    level++;
 571                    sec_loc[level] = iso_733_to_u32(cur_record->ext_loc);
 572                    sec_offset[level] = 0;
 573                    dir_rem[level] = 0;
 574                    continue;
 575                }
 576            }
 577        }
 578
 579        if (dir_rem[level] == 0) {
 580            /* Nothing remaining */
 581            level--;
 582            read_iso_sector(sec_loc[level], temp,
 583                            "Failed to read ISO directory");
 584        }
 585    }
 586
 587    return 0;
 588}
 589
 590static void load_iso_bc_entry(IsoBcSection *load)
 591{
 592    IsoBcSection s = *load;
 593    /*
 594     * According to spec, extent for each file
 595     * is padded and ISO_SECTOR_SIZE bytes aligned
 596     */
 597    uint32_t blks_to_load = bswap16(s.sector_count) >> ET_SECTOR_SHIFT;
 598    uint32_t real_size = iso_get_file_size(bswap32(s.load_rba));
 599
 600    if (real_size) {
 601        /* Round up blocks to load */
 602        blks_to_load = (real_size + ISO_SECTOR_SIZE - 1) / ISO_SECTOR_SIZE;
 603        sclp_print("ISO boot image size verified\n");
 604    } else {
 605        sclp_print("ISO boot image size could not be verified\n");
 606    }
 607
 608    read_iso_boot_image(bswap32(s.load_rba),
 609                        (void *)((uint64_t)bswap16(s.load_segment)),
 610                        blks_to_load);
 611
 612    /* Trying to get PSW at zero address */
 613    if (*((uint64_t *)0) & IPL_PSW_MASK) {
 614        jump_to_IPL_code((*((uint64_t *)0)) & 0x7fffffff);
 615    }
 616
 617    /* Try default linux start address */
 618    jump_to_IPL_code(KERN_IMAGE_START);
 619}
 620
 621static uint32_t find_iso_bc(void)
 622{
 623    IsoVolDesc *vd = (IsoVolDesc *)sec;
 624    uint32_t block_num = ISO_PRIMARY_VD_SECTOR;
 625
 626    if (virtio_read_many(block_num++, sec, 1)) {
 627        /* If primary vd cannot be read, there is no boot catalog */
 628        return 0;
 629    }
 630
 631    while (is_iso_vd_valid(vd) && vd->type != VOL_DESC_TERMINATOR) {
 632        if (vd->type == VOL_DESC_TYPE_BOOT) {
 633            IsoVdElTorito *et = &vd->vd.boot;
 634
 635            if (!_memcmp(&et->el_torito[0], el_torito_magic, 32)) {
 636                return bswap32(et->bc_offset);
 637            }
 638        }
 639        read_iso_sector(block_num++, sec,
 640                        "Failed to read ISO volume descriptor");
 641    }
 642
 643    return 0;
 644}
 645
 646static IsoBcSection *find_iso_bc_entry(void)
 647{
 648    IsoBcEntry *e = (IsoBcEntry *)sec;
 649    uint32_t offset = find_iso_bc();
 650    int i;
 651
 652    if (!offset) {
 653        return NULL;
 654    }
 655
 656    read_iso_sector(offset, sec, "Failed to read El Torito boot catalog");
 657
 658    if (!is_iso_bc_valid(e)) {
 659        /* The validation entry is mandatory */
 660        panic("No valid boot catalog found!\n");
 661        return NULL;
 662    }
 663
 664    /*
 665     * Each entry has 32 bytes size, so one sector cannot contain > 64 entries.
 666     * We consider only boot catalogs with no more than 64 entries.
 667     */
 668    for (i = 1; i < ISO_BC_ENTRY_PER_SECTOR; i++) {
 669        if (e[i].id == ISO_BC_BOOTABLE_SECTION) {
 670            if (is_iso_bc_entry_compatible(&e[i].body.sect)) {
 671                return &e[i].body.sect;
 672            }
 673        }
 674    }
 675
 676    panic("No suitable boot entry found on ISO-9660 media!\n");
 677
 678    return NULL;
 679}
 680
 681static void ipl_iso_el_torito(void)
 682{
 683    IsoBcSection *s = find_iso_bc_entry();
 684
 685    if (s) {
 686        load_iso_bc_entry(s);
 687        /* no return */
 688    }
 689}
 690
 691/***********************************************************************
 692 * Bus specific IPL sequences
 693 */
 694
 695static void zipl_load_vblk(void)
 696{
 697    if (virtio_guessed_disk_nature()) {
 698        virtio_assume_iso9660();
 699    }
 700    ipl_iso_el_torito();
 701
 702    if (virtio_guessed_disk_nature()) {
 703        sclp_print("Using guessed DASD geometry.\n");
 704        virtio_assume_eckd();
 705    }
 706    ipl_eckd();
 707}
 708
 709static void zipl_load_vscsi(void)
 710{
 711    if (virtio_get_block_size() == VIRTIO_ISO_BLOCK_SIZE) {
 712        /* Is it an ISO image in non-CD drive? */
 713        ipl_iso_el_torito();
 714    }
 715
 716    sclp_print("Using guessed DASD geometry.\n");
 717    virtio_assume_eckd();
 718    ipl_eckd();
 719}
 720
 721/***********************************************************************
 722 * IPL starts here
 723 */
 724
 725void zipl_load(void)
 726{
 727    if (virtio_get_device()->is_cdrom) {
 728        ipl_iso_el_torito();
 729        panic("\n! Cannot IPL this ISO image !\n");
 730    }
 731
 732    ipl_scsi();
 733
 734    switch (virtio_get_device_type()) {
 735    case VIRTIO_ID_BLOCK:
 736        zipl_load_vblk();
 737        break;
 738    case VIRTIO_ID_SCSI:
 739        zipl_load_vscsi();
 740        break;
 741    default:
 742        panic("\n! Unknown IPL device type !\n");
 743    }
 744
 745    panic("\n* this can never happen *\n");
 746}
 747