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 <string.h> 12 #include <stdio.h> 13 #include "s390-ccw.h" 14 #include "s390-arch.h" 15 #include "bootmap.h" 16 #include "virtio.h" 17 #include "bswap.h" 18 19 #ifdef DEBUG 20 /* #define DEBUG_FALLBACK */ 21 #endif 22 23 #ifdef DEBUG_FALLBACK 24 #define dputs(txt) \ 25 do { printf("zipl: " txt); } while (0) 26 #else 27 #define dputs(fmt, ...) \ 28 do { } while (0) 29 #endif 30 31 /* Scratch space */ 32 static uint8_t sec[MAX_SECTOR_SIZE*4] __attribute__((__aligned__(PAGE_SIZE))); 33 34 const uint8_t el_torito_magic[] = "EL TORITO SPECIFICATION" 35 "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; 36 37 /* 38 * Match two CCWs located after PSW and eight filler bytes. 39 * From libmagic and arch/s390/kernel/head.S. 40 */ 41 const uint8_t linux_s390_magic[] = "\x02\x00\x00\x18\x60\x00\x00\x50\x02\x00" 42 "\x00\x68\x60\x00\x00\x50\x40\x40\x40\x40" 43 "\x40\x40\x40\x40"; 44 45 static inline bool is_iso_vd_valid(IsoVolDesc *vd) 46 { 47 const uint8_t vol_desc_magic[] = "CD001"; 48 49 return !memcmp(&vd->ident[0], vol_desc_magic, 5) && 50 vd->version == 0x1 && 51 vd->type <= VOL_DESC_TYPE_PARTITION; 52 } 53 54 /*********************************************************************** 55 * IPL an ECKD DASD (CDL or LDL/CMS format) 56 */ 57 58 static unsigned char _bprs[8*1024]; /* guessed "max" ECKD sector size */ 59 static const int max_bprs_entries = sizeof(_bprs) / sizeof(ExtEckdBlockPtr); 60 static uint8_t _s2[MAX_SECTOR_SIZE * 3] __attribute__((__aligned__(PAGE_SIZE))); 61 static void *s2_prev_blk = _s2; 62 static void *s2_cur_blk = _s2 + MAX_SECTOR_SIZE; 63 static void *s2_next_blk = _s2 + MAX_SECTOR_SIZE * 2; 64 65 static inline int verify_boot_info(BootInfo *bip) 66 { 67 if (!magic_match(bip->magic, ZIPL_MAGIC)) { 68 puts("No zIPL sig in BootInfo"); 69 return -EINVAL; 70 } 71 if (bip->version != BOOT_INFO_VERSION) { 72 puts("Wrong zIPL version"); 73 return -EINVAL; 74 } 75 if (bip->bp_type != BOOT_INFO_BP_TYPE_IPL) { 76 puts("DASD is not for IPL"); 77 return -ENODEV; 78 } 79 if (bip->dev_type != BOOT_INFO_DEV_TYPE_ECKD) { 80 puts("DASD is not ECKD"); 81 return -ENODEV; 82 } 83 if (bip->flags != BOOT_INFO_FLAGS_ARCH) { 84 puts("Not for this arch"); 85 return -EINVAL; 86 } 87 if (!block_size_ok(bip->bp.ipl.bm_ptr.eckd.bptr.size)) { 88 puts("Bad block size in zIPL section of 1st record"); 89 return -EINVAL; 90 } 91 92 return 0; 93 } 94 95 static void eckd_format_chs(ExtEckdBlockPtr *ptr, bool ldipl, 96 uint64_t *c, 97 uint64_t *h, 98 uint64_t *s) 99 { 100 if (ldipl) { 101 *c = ptr->ldptr.chs.cylinder; 102 *h = ptr->ldptr.chs.head; 103 *s = ptr->ldptr.chs.sector; 104 } else { 105 *c = ptr->bptr.chs.cylinder; 106 *h = ptr->bptr.chs.head; 107 *s = ptr->bptr.chs.sector; 108 } 109 } 110 111 static block_number_t eckd_chs_to_block(uint64_t c, uint64_t h, uint64_t s) 112 { 113 const uint64_t sectors = virtio_get_sectors(); 114 const uint64_t heads = virtio_get_heads(); 115 const uint64_t cylinder = c + ((h & 0xfff0) << 12); 116 const uint64_t head = h & 0x000f; 117 const block_number_t block = sectors * heads * cylinder 118 + sectors * head 119 + s - 1; /* block nr starts with zero */ 120 return block; 121 } 122 123 static block_number_t eckd_block_num(EckdCHS *chs) 124 { 125 return eckd_chs_to_block(chs->cylinder, chs->head, chs->sector); 126 } 127 128 static block_number_t gen_eckd_block_num(ExtEckdBlockPtr *ptr, bool ldipl) 129 { 130 uint64_t cyl, head, sec; 131 eckd_format_chs(ptr, ldipl, &cyl, &head, &sec); 132 return eckd_chs_to_block(cyl, head, sec); 133 } 134 135 static bool eckd_valid_chs(uint64_t cyl, uint64_t head, uint64_t sector) 136 { 137 if (head >= virtio_get_heads() 138 || sector > virtio_get_sectors() 139 || sector <= 0) { 140 return false; 141 } 142 143 if (!virtio_guessed_disk_nature() && 144 eckd_chs_to_block(cyl, head, sector) >= virtio_get_blocks()) { 145 return false; 146 } 147 148 return true; 149 } 150 151 static bool eckd_valid_address(ExtEckdBlockPtr *ptr, bool ldipl) 152 { 153 uint64_t cyl, head, sec; 154 eckd_format_chs(ptr, ldipl, &cyl, &head, &sec); 155 return eckd_valid_chs(cyl, head, sec); 156 } 157 158 static block_number_t load_eckd_segments(block_number_t blk, bool ldipl, 159 uint64_t *address) 160 { 161 block_number_t block_nr; 162 int j, rc, count; 163 BootMapPointer *bprs = (void *)_bprs; 164 bool more_data; 165 166 memset(_bprs, FREE_SPACE_FILLER, sizeof(_bprs)); 167 if (virtio_read(blk, bprs)) { 168 puts("BPRS read failed"); 169 return ERROR_BLOCK_NR; 170 } 171 172 do { 173 more_data = false; 174 for (j = 0;; j++) { 175 block_nr = gen_eckd_block_num(&bprs[j].xeckd, ldipl); 176 if (is_null_block_number(block_nr)) { /* end of chunk */ 177 return NULL_BLOCK_NR; 178 } 179 180 /* we need the updated blockno for the next indirect entry 181 * in the chain, but don't want to advance address 182 */ 183 if (j == (max_bprs_entries - 1)) { 184 break; 185 } 186 187 /* List directed pointer does not store block size */ 188 if (!ldipl && !block_size_ok(bprs[j].xeckd.bptr.size)) { 189 puts("Bad chunk block size"); 190 return ERROR_BLOCK_NR; 191 } 192 193 if (!eckd_valid_address(&bprs[j].xeckd, ldipl)) { 194 /* 195 * If an invalid address is found during LD-IPL then break and 196 * retry as CCW-IPL, otherwise abort on error 197 */ 198 if (!ldipl) { 199 puts("Bad chunk ECKD address"); 200 return ERROR_BLOCK_NR; 201 } 202 break; 203 } 204 205 if (ldipl) { 206 count = bprs[j].xeckd.ldptr.count; 207 } else { 208 count = bprs[j].xeckd.bptr.count; 209 } 210 211 if (count == 0 && unused_space(&bprs[j + 1], 212 sizeof(EckdBlockPtr))) { 213 /* This is a "continue" pointer. 214 * This ptr should be the last one in the current 215 * script section. 216 * I.e. the next ptr must point to the unused memory area 217 */ 218 memset(_bprs, FREE_SPACE_FILLER, sizeof(_bprs)); 219 if (virtio_read(block_nr, bprs)) { 220 puts("BPRS continuation read failed"); 221 return ERROR_BLOCK_NR; 222 } 223 more_data = true; 224 break; 225 } 226 227 /* Load (count+1) blocks of code at (block_nr) 228 * to memory (address). 229 */ 230 rc = virtio_read_many(block_nr, (void *)(*address), count + 1); 231 if (rc != 0) { 232 puts("Code chunk read failed"); 233 return ERROR_BLOCK_NR; 234 } 235 236 *address += (count + 1) * virtio_get_block_size(); 237 } 238 } while (more_data); 239 return block_nr; 240 } 241 242 static bool find_zipl_boot_menu_banner(int *offset) 243 { 244 int i; 245 246 /* Menu banner starts with "zIPL" */ 247 for (i = 0; i <= virtio_get_block_size() - 4; i++) { 248 if (magic_match(s2_cur_blk + i, ZIPL_MAGIC_EBCDIC)) { 249 *offset = i; 250 return true; 251 } 252 } 253 254 return false; 255 } 256 257 static int eckd_get_boot_menu_index(block_number_t s1b_block_nr) 258 { 259 block_number_t cur_block_nr; 260 block_number_t prev_block_nr = 0; 261 block_number_t next_block_nr = 0; 262 EckdStage1b *s1b = (void *)sec; 263 int banner_offset; 264 int i; 265 266 /* Get Stage1b data */ 267 memset(sec, FREE_SPACE_FILLER, sizeof(sec)); 268 if (virtio_read(s1b_block_nr, s1b)) { 269 puts("Cannot read stage1b boot loader"); 270 return -EIO; 271 } 272 273 memset(_s2, FREE_SPACE_FILLER, sizeof(_s2)); 274 275 /* Get Stage2 data */ 276 for (i = 0; i < STAGE2_BLK_CNT_MAX; i++) { 277 cur_block_nr = eckd_block_num(&s1b->seek[i].chs); 278 279 if (!cur_block_nr || is_null_block_number(cur_block_nr)) { 280 break; 281 } 282 283 if (virtio_read(cur_block_nr, s2_cur_blk)) { 284 puts("Cannot read stage2 boot loader"); 285 return -EIO; 286 } 287 288 if (find_zipl_boot_menu_banner(&banner_offset)) { 289 /* 290 * Load the adjacent blocks to account for the 291 * possibility of menu data spanning multiple blocks. 292 */ 293 if (prev_block_nr) { 294 if (virtio_read(prev_block_nr, s2_prev_blk)) { 295 puts("Cannot read stage2 boot loader"); 296 return -EIO; 297 } 298 } 299 300 if (i + 1 < STAGE2_BLK_CNT_MAX) { 301 next_block_nr = eckd_block_num(&s1b->seek[i + 1].chs); 302 } 303 304 if (next_block_nr && !is_null_block_number(next_block_nr)) { 305 if (virtio_read(next_block_nr, s2_next_blk)) { 306 puts("Cannot read stage2 boot loader"); 307 return -EIO; 308 } 309 } 310 311 return menu_get_zipl_boot_index(s2_cur_blk + banner_offset); 312 } 313 314 prev_block_nr = cur_block_nr; 315 } 316 317 printf("No zipl boot menu data found. Booting default entry."); 318 return 0; 319 } 320 321 static int run_eckd_boot_script(block_number_t bmt_block_nr, 322 block_number_t s1b_block_nr) 323 { 324 int i; 325 unsigned int loadparm = get_loadparm_index(); 326 block_number_t block_nr; 327 uint64_t address; 328 BootMapTable *bmt = (void *)sec; 329 BootMapScript *bms = (void *)sec; 330 /* The S1B block number is NULL_BLOCK_NR if and only if it's an LD-IPL */ 331 bool ldipl = (s1b_block_nr == NULL_BLOCK_NR); 332 333 if (menu_is_enabled_zipl() && !ldipl) { 334 loadparm = eckd_get_boot_menu_index(s1b_block_nr); 335 } 336 337 debug_print_int("loadparm", loadparm); 338 if (loadparm >= MAX_BOOT_ENTRIES) { 339 panic("loadparm value greater than max number of boot entries allowed"); 340 } 341 342 memset(sec, FREE_SPACE_FILLER, sizeof(sec)); 343 if (virtio_read(bmt_block_nr, sec)) { 344 puts("Cannot read Boot Map Table"); 345 return -EIO; 346 } 347 348 block_nr = gen_eckd_block_num(&bmt->entry[loadparm].xeckd, ldipl); 349 if (block_nr == NULL_BLOCK_NR) { 350 printf("The requested boot entry (%d) is invalid\n", loadparm); 351 panic("Invalid loadparm"); 352 } 353 354 memset(sec, FREE_SPACE_FILLER, sizeof(sec)); 355 if (virtio_read(block_nr, sec)) { 356 puts("Cannot read Boot Map Script"); 357 return -EIO; 358 } 359 360 for (i = 0; bms->entry[i].type == BOOT_SCRIPT_LOAD || 361 bms->entry[i].type == BOOT_SCRIPT_SIGNATURE; i++) { 362 363 /* We don't support secure boot yet, so we skip signature entries */ 364 if (bms->entry[i].type == BOOT_SCRIPT_SIGNATURE) { 365 continue; 366 } 367 368 address = bms->entry[i].address.load_address; 369 block_nr = gen_eckd_block_num(&bms->entry[i].blkptr.xeckd, ldipl); 370 371 do { 372 block_nr = load_eckd_segments(block_nr, ldipl, &address); 373 if (block_nr == ERROR_BLOCK_NR) { 374 return ldipl ? 0 : -EIO; 375 } 376 } while (block_nr != NULL_BLOCK_NR); 377 } 378 379 if (ldipl && bms->entry[i].type != BOOT_SCRIPT_EXEC) { 380 /* Abort LD-IPL and retry as CCW-IPL */ 381 return 0; 382 } 383 384 if (bms->entry[i].type != BOOT_SCRIPT_EXEC) { 385 puts("Unknown script entry type"); 386 return -EINVAL; 387 } 388 write_reset_psw(bms->entry[i].address.load_address); 389 jump_to_IPL_code(0); 390 return -1; 391 } 392 393 static int ipl_eckd_cdl(void) 394 { 395 XEckdMbr *mbr; 396 EckdCdlIpl2 *ipl2 = (void *)sec; 397 IplVolumeLabel *vlbl = (void *)sec; 398 block_number_t bmt_block_nr, s1b_block_nr; 399 400 /* we have just read the block #0 and recognized it as "IPL1" */ 401 puts("CDL"); 402 403 memset(sec, FREE_SPACE_FILLER, sizeof(sec)); 404 if (virtio_read(1, ipl2)) { 405 puts("Cannot read IPL2 record at block 1"); 406 return -EIO; 407 } 408 409 mbr = &ipl2->mbr; 410 if (!magic_match(mbr, ZIPL_MAGIC)) { 411 puts("No zIPL section in IPL2 record."); 412 return 0; 413 } 414 if (!block_size_ok(mbr->blockptr.xeckd.bptr.size)) { 415 puts("Bad block size in zIPL section of IPL2 record."); 416 return 0; 417 } 418 if (mbr->dev_type != DEV_TYPE_ECKD) { 419 puts("Non-ECKD device type in zIPL section of IPL2 record."); 420 return 0; 421 } 422 423 /* save pointer to Boot Map Table */ 424 bmt_block_nr = eckd_block_num(&mbr->blockptr.xeckd.bptr.chs); 425 426 /* save pointer to Stage1b Data */ 427 s1b_block_nr = eckd_block_num(&ipl2->stage1.seek[0].chs); 428 429 memset(sec, FREE_SPACE_FILLER, sizeof(sec)); 430 if (virtio_read(2, vlbl)) { 431 puts("Cannot read Volume Label at block 2"); 432 return -EIO; 433 } 434 if (!magic_match(vlbl->key, VOL1_MAGIC)) { 435 puts("Invalid magic of volume label block."); 436 return 0; 437 } 438 if (!magic_match(vlbl->f.key, VOL1_MAGIC)) { 439 puts("Invalid magic of volser block."); 440 return 0; 441 } 442 print_volser(vlbl->f.volser); 443 444 return run_eckd_boot_script(bmt_block_nr, s1b_block_nr); 445 } 446 447 static void print_eckd_ldl_msg(ECKD_IPL_mode_t mode) 448 { 449 LDL_VTOC *vlbl = (void *)sec; /* already read, 3rd block */ 450 char msg[4] = { '?', '.', '\n', '\0' }; 451 452 printf((mode == ECKD_CMS) ? "CMS" : "LDL"); 453 printf(" version "); 454 switch (vlbl->LDL_version) { 455 case LDL1_VERSION: 456 msg[0] = '1'; 457 break; 458 case LDL2_VERSION: 459 msg[0] = '2'; 460 break; 461 default: 462 msg[0] = ebc2asc[vlbl->LDL_version]; 463 msg[1] = '?'; 464 break; 465 } 466 printf("%s", msg); 467 print_volser(vlbl->volser); 468 } 469 470 static int ipl_eckd_ldl(ECKD_IPL_mode_t mode) 471 { 472 block_number_t bmt_block_nr, s1b_block_nr; 473 EckdLdlIpl1 *ipl1 = (void *)sec; 474 475 if (mode != ECKD_LDL_UNLABELED) { 476 print_eckd_ldl_msg(mode); 477 } 478 479 /* DO NOT read BootMap pointer (only one, xECKD) at block #2 */ 480 481 memset(sec, FREE_SPACE_FILLER, sizeof(sec)); 482 if (virtio_read(0, sec)) { 483 puts("Cannot read block 0 to grab boot info."); 484 return -EIO; 485 } 486 if (mode == ECKD_LDL_UNLABELED) { 487 if (!magic_match(ipl1->bip.magic, ZIPL_MAGIC)) { 488 return 0; /* not applicable layout */ 489 } 490 puts("unlabeled LDL."); 491 } 492 verify_boot_info(&ipl1->bip); 493 494 /* save pointer to Boot Map Table */ 495 bmt_block_nr = eckd_block_num(&ipl1->bip.bp.ipl.bm_ptr.eckd.bptr.chs); 496 497 /* save pointer to Stage1b Data */ 498 s1b_block_nr = eckd_block_num(&ipl1->stage1.seek[0].chs); 499 500 return run_eckd_boot_script(bmt_block_nr, s1b_block_nr); 501 } 502 503 static block_number_t eckd_find_bmt(ExtEckdBlockPtr *ptr) 504 { 505 block_number_t blockno; 506 uint8_t tmp_sec[MAX_SECTOR_SIZE]; 507 BootRecord *br; 508 509 blockno = gen_eckd_block_num(ptr, 0); 510 if (virtio_read(blockno, tmp_sec)) { 511 puts("Cannot read boot record"); 512 return ERROR_BLOCK_NR; 513 } 514 br = (BootRecord *)tmp_sec; 515 if (!magic_match(br->magic, ZIPL_MAGIC)) { 516 /* If the boot record is invalid, return and try CCW-IPL instead */ 517 return NULL_BLOCK_NR; 518 } 519 520 return gen_eckd_block_num(&br->pgt.xeckd, 1); 521 } 522 523 static void print_eckd_msg(void) 524 { 525 char msg[] = "Using ECKD scheme (block size *****), "; 526 char *p = &msg[34], *q = &msg[30]; 527 int n = virtio_get_block_size(); 528 529 /* Fill in the block size and show up the message */ 530 if (n > 0 && n <= 99999) { 531 while (n) { 532 *p-- = '0' + (n % 10); 533 n /= 10; 534 } 535 while (p >= q) { 536 *p-- = ' '; 537 } 538 } 539 printf("%s", msg); 540 } 541 542 static int ipl_eckd(void) 543 { 544 IplVolumeLabel *vlbl = (void *)sec; 545 LDL_VTOC *vtoc = (void *)sec; 546 block_number_t ldipl_bmt; /* Boot Map Table for List-Directed IPL */ 547 548 print_eckd_msg(); 549 550 /* Block 2 can contain either the CDL VOL1 label or the LDL VTOC */ 551 memset(sec, FREE_SPACE_FILLER, sizeof(sec)); 552 if (virtio_read(2, vlbl)) { 553 puts("Cannot read block 2"); 554 return -EIO; 555 } 556 557 /* 558 * First check for a list-directed-format pointer which would 559 * supersede the CCW pointer. 560 */ 561 if (eckd_valid_address((ExtEckdBlockPtr *)&vlbl->f.br, 0)) { 562 ldipl_bmt = eckd_find_bmt((ExtEckdBlockPtr *)&vlbl->f.br); 563 switch (ldipl_bmt) { 564 case ERROR_BLOCK_NR: 565 return -EIO; 566 case NULL_BLOCK_NR: 567 break; /* Invalid BMT but the device may still boot with CCW-IPL */ 568 default: 569 puts("List-Directed"); 570 /* 571 * LD-IPL does not use the S1B bock, just make it NULL_BLOCK_NR. 572 * In some failure cases retry IPL before aborting. 573 */ 574 if (run_eckd_boot_script(ldipl_bmt, NULL_BLOCK_NR)) { 575 return -EIO; 576 } 577 /* Non-fatal error, retry as CCW-IPL */ 578 printf("Retrying IPL "); 579 print_eckd_msg(); 580 } 581 memset(sec, FREE_SPACE_FILLER, sizeof(sec)); 582 if (virtio_read(2, vtoc)) { 583 puts("Cannot read block 2"); 584 return -EIO; 585 } 586 } 587 588 /* Not list-directed */ 589 if (magic_match(vtoc->magic, VOL1_MAGIC)) { 590 if (ipl_eckd_cdl()) { 591 return -1; 592 } 593 } 594 595 if (magic_match(vtoc->magic, CMS1_MAGIC)) { 596 return ipl_eckd_ldl(ECKD_CMS); 597 } 598 if (magic_match(vtoc->magic, LNX1_MAGIC)) { 599 return ipl_eckd_ldl(ECKD_LDL); 600 } 601 602 if (ipl_eckd_ldl(ECKD_LDL_UNLABELED)) { 603 return -1; 604 } 605 /* 606 * Ok, it is not a LDL by any means. 607 * It still might be a CDL with zero record keys for IPL1 and IPL2 608 */ 609 return ipl_eckd_cdl(); 610 } 611 612 /*********************************************************************** 613 * IPL a SCSI disk 614 */ 615 616 static int zipl_load_segment(ComponentEntry *entry) 617 { 618 const int max_entries = (MAX_SECTOR_SIZE / sizeof(ScsiBlockPtr)); 619 ScsiBlockPtr *bprs = (void *)sec; 620 const int bprs_size = sizeof(sec); 621 block_number_t blockno; 622 uint64_t address; 623 int i; 624 char err_msg[] = "zIPL failed to read BPRS at 0xZZZZZZZZZZZZZZZZ"; 625 char *blk_no = &err_msg[30]; /* where to print blockno in (those ZZs) */ 626 627 blockno = entry->data.blockno; 628 address = entry->compdat.load_addr; 629 630 debug_print_int("loading segment at block", blockno); 631 debug_print_int("addr", address); 632 633 do { 634 memset(bprs, FREE_SPACE_FILLER, bprs_size); 635 fill_hex_val(blk_no, &blockno, sizeof(blockno)); 636 if (virtio_read(blockno, bprs)) { 637 puts(err_msg); 638 return -EIO; 639 } 640 641 for (i = 0;; i++) { 642 uint64_t *cur_desc = (void *)&bprs[i]; 643 644 blockno = bprs[i].blockno; 645 if (!blockno) { 646 break; 647 } 648 649 /* we need the updated blockno for the next indirect entry in the 650 chain, but don't want to advance address */ 651 if (i == (max_entries - 1)) { 652 break; 653 } 654 655 if (bprs[i].blockct == 0 && unused_space(&bprs[i + 1], 656 sizeof(ScsiBlockPtr))) { 657 /* This is a "continue" pointer. 658 * This ptr is the last one in the current script section. 659 * I.e. the next ptr must point to the unused memory area. 660 * The blockno is not zero, so the upper loop must continue 661 * reading next section of BPRS. 662 */ 663 break; 664 } 665 address = virtio_load_direct(cur_desc[0], cur_desc[1], 0, 666 (void *)address); 667 if (!address) { 668 puts("zIPL load segment failed"); 669 return -EIO; 670 } 671 } 672 } while (blockno); 673 674 return 0; 675 } 676 677 /* Run a zipl program */ 678 static int zipl_run(ScsiBlockPtr *pte) 679 { 680 ComponentHeader *header; 681 ComponentEntry *entry; 682 uint8_t tmp_sec[MAX_SECTOR_SIZE]; 683 684 if (virtio_read(pte->blockno, tmp_sec)) { 685 puts("Cannot read header"); 686 return -EIO; 687 } 688 header = (ComponentHeader *)tmp_sec; 689 690 if (!magic_match(tmp_sec, ZIPL_MAGIC)) { 691 puts("No zIPL magic in header"); 692 return -EINVAL; 693 } 694 if (header->type != ZIPL_COMP_HEADER_IPL) { 695 puts("Bad header type"); 696 return -EINVAL; 697 } 698 699 dputs("start loading images\n"); 700 701 /* Load image(s) into RAM */ 702 entry = (ComponentEntry *)(&header[1]); 703 while (entry->component_type == ZIPL_COMP_ENTRY_LOAD || 704 entry->component_type == ZIPL_COMP_ENTRY_SIGNATURE) { 705 706 /* We don't support secure boot yet, so we skip signature entries */ 707 if (entry->component_type == ZIPL_COMP_ENTRY_SIGNATURE) { 708 entry++; 709 continue; 710 } 711 712 if (zipl_load_segment(entry)) { 713 return -1; 714 } 715 716 entry++; 717 718 if ((uint8_t *)(&entry[1]) > (tmp_sec + MAX_SECTOR_SIZE)) { 719 puts("Wrong entry value"); 720 return -EINVAL; 721 } 722 } 723 724 if (entry->component_type != ZIPL_COMP_ENTRY_EXEC) { 725 puts("No EXEC entry"); 726 return -EINVAL; 727 } 728 729 /* should not return */ 730 write_reset_psw(entry->compdat.load_psw); 731 jump_to_IPL_code(0); 732 return -1; 733 } 734 735 static int ipl_scsi(void) 736 { 737 ScsiMbr *mbr = (void *)sec; 738 int program_table_entries = 0; 739 BootMapTable *prog_table = (void *)sec; 740 unsigned int loadparm = get_loadparm_index(); 741 bool valid_entries[MAX_BOOT_ENTRIES] = {false}; 742 size_t i; 743 744 /* Grab the MBR */ 745 memset(sec, FREE_SPACE_FILLER, sizeof(sec)); 746 if (virtio_read(0, mbr)) { 747 puts("Cannot read block 0"); 748 return -EIO; 749 } 750 751 if (!magic_match(mbr->magic, ZIPL_MAGIC)) { 752 return 0; 753 } 754 755 puts("Using SCSI scheme."); 756 debug_print_int("MBR Version", mbr->version_id); 757 IPL_check(mbr->version_id == 1, 758 "Unknown MBR layout version, assuming version 1"); 759 debug_print_int("program table", mbr->pt.blockno); 760 if (!mbr->pt.blockno) { 761 puts("No Program Table"); 762 return -EINVAL; 763 } 764 765 /* Parse the program table */ 766 if (virtio_read(mbr->pt.blockno, sec)) { 767 puts("Error reading Program Table"); 768 return -EIO; 769 } 770 if (!magic_match(sec, ZIPL_MAGIC)) { 771 puts("No zIPL magic in Program Table"); 772 return -EINVAL; 773 } 774 775 for (i = 0; i < MAX_BOOT_ENTRIES; i++) { 776 if (prog_table->entry[i].scsi.blockno) { 777 valid_entries[i] = true; 778 program_table_entries++; 779 } 780 } 781 782 debug_print_int("program table entries", program_table_entries); 783 if (program_table_entries == 0) { 784 puts("Empty Program Table"); 785 return -EINVAL; 786 } 787 788 if (menu_is_enabled_enum()) { 789 loadparm = menu_get_enum_boot_index(valid_entries); 790 } 791 792 debug_print_int("loadparm", loadparm); 793 if (loadparm >= MAX_BOOT_ENTRIES) { 794 panic("loadparm value greater than max number of boot entries allowed"); 795 } 796 797 if (!valid_entries[loadparm]) { 798 printf("The requested boot entry (%d) is invalid\n", loadparm); 799 panic("Invalid loadparm"); 800 } 801 802 return zipl_run(&prog_table->entry[loadparm].scsi); 803 } 804 805 /*********************************************************************** 806 * IPL El Torito ISO9660 image or DVD 807 */ 808 809 static bool is_iso_bc_entry_compatible(IsoBcSection *s) 810 { 811 uint8_t *magic_sec = (uint8_t *)(sec + ISO_SECTOR_SIZE); 812 813 if (s->unused || !s->sector_count) { 814 return false; 815 } 816 if (virtio_read(bswap32(s->load_rba), magic_sec)) { 817 puts("Failed to read image sector 0"); 818 return false; 819 } 820 821 /* Checking bytes 8 - 32 for S390 Linux magic */ 822 return !memcmp(magic_sec + 8, linux_s390_magic, 24); 823 } 824 825 /* Location of the current sector of the directory */ 826 static uint32_t sec_loc[ISO9660_MAX_DIR_DEPTH]; 827 /* Offset in the current sector of the directory */ 828 static uint32_t sec_offset[ISO9660_MAX_DIR_DEPTH]; 829 /* Remained directory space in bytes */ 830 static uint32_t dir_rem[ISO9660_MAX_DIR_DEPTH]; 831 832 static inline long iso_get_file_size(uint32_t load_rba) 833 { 834 IsoVolDesc *vd = (IsoVolDesc *)sec; 835 IsoDirHdr *cur_record = &vd->vd.primary.rootdir; 836 uint8_t *temp = sec + ISO_SECTOR_SIZE; 837 int level = 0; 838 839 if (virtio_read(ISO_PRIMARY_VD_SECTOR, sec)) { 840 puts("Failed to read ISO primary descriptor"); 841 return -EIO; 842 } 843 844 sec_loc[0] = iso_733_to_u32(cur_record->ext_loc); 845 dir_rem[0] = 0; 846 sec_offset[0] = 0; 847 848 while (level >= 0) { 849 if (sec_offset[level] > ISO_SECTOR_SIZE) { 850 puts("Directory tree structure violation"); 851 return -EIO; 852 } 853 854 cur_record = (IsoDirHdr *)(temp + sec_offset[level]); 855 856 if (sec_offset[level] == 0) { 857 if (virtio_read(sec_loc[level], temp)) { 858 puts("Failed to read ISO directory"); 859 return -EIO; 860 } 861 if (dir_rem[level] == 0) { 862 /* Skip self and parent records */ 863 dir_rem[level] = iso_733_to_u32(cur_record->data_len) - 864 cur_record->dr_len; 865 sec_offset[level] += cur_record->dr_len; 866 867 cur_record = (IsoDirHdr *)(temp + sec_offset[level]); 868 dir_rem[level] -= cur_record->dr_len; 869 sec_offset[level] += cur_record->dr_len; 870 continue; 871 } 872 } 873 874 if (!cur_record->dr_len || sec_offset[level] == ISO_SECTOR_SIZE) { 875 /* Zero-padding and/or the end of current sector */ 876 dir_rem[level] -= ISO_SECTOR_SIZE - sec_offset[level]; 877 sec_offset[level] = 0; 878 sec_loc[level]++; 879 } else { 880 /* The directory record is valid */ 881 if (load_rba == iso_733_to_u32(cur_record->ext_loc)) { 882 return iso_733_to_u32(cur_record->data_len); 883 } 884 885 dir_rem[level] -= cur_record->dr_len; 886 sec_offset[level] += cur_record->dr_len; 887 888 if (cur_record->file_flags & 0x2) { 889 /* Subdirectory */ 890 if (level == ISO9660_MAX_DIR_DEPTH - 1) { 891 puts("ISO-9660 directory depth limit exceeded"); 892 } else { 893 level++; 894 sec_loc[level] = iso_733_to_u32(cur_record->ext_loc); 895 sec_offset[level] = 0; 896 dir_rem[level] = 0; 897 continue; 898 } 899 } 900 } 901 902 if (dir_rem[level] == 0) { 903 /* Nothing remaining */ 904 level--; 905 if (virtio_read(sec_loc[level], temp)) { 906 puts("Failed to read ISO directory"); 907 return -EIO; 908 } 909 } 910 } 911 912 return 0; 913 } 914 915 static void load_iso_bc_entry(IsoBcSection *load) 916 { 917 IsoBcSection s = *load; 918 /* 919 * According to spec, extent for each file 920 * is padded and ISO_SECTOR_SIZE bytes aligned 921 */ 922 uint32_t blks_to_load = bswap16(s.sector_count) >> ET_SECTOR_SHIFT; 923 long real_size = iso_get_file_size(bswap32(s.load_rba)); 924 925 if (real_size > 0) { 926 /* Round up blocks to load */ 927 blks_to_load = (real_size + ISO_SECTOR_SIZE - 1) / ISO_SECTOR_SIZE; 928 puts("ISO boot image size verified"); 929 } else { 930 puts("ISO boot image size could not be verified"); 931 if (real_size < 0) { 932 return; 933 } 934 } 935 936 if (read_iso_boot_image(bswap32(s.load_rba), 937 (void *)((uint64_t)bswap16(s.load_segment)), 938 blks_to_load)) { 939 return; 940 } 941 942 jump_to_low_kernel(); 943 } 944 945 static uint32_t find_iso_bc(void) 946 { 947 IsoVolDesc *vd = (IsoVolDesc *)sec; 948 uint32_t block_num = ISO_PRIMARY_VD_SECTOR; 949 950 if (virtio_read_many(block_num++, sec, 1)) { 951 /* If primary vd cannot be read, there is no boot catalog */ 952 return 0; 953 } 954 955 while (is_iso_vd_valid(vd) && vd->type != VOL_DESC_TERMINATOR) { 956 if (vd->type == VOL_DESC_TYPE_BOOT) { 957 IsoVdElTorito *et = &vd->vd.boot; 958 959 if (!memcmp(&et->el_torito[0], el_torito_magic, 32)) { 960 return bswap32(et->bc_offset); 961 } 962 } 963 if (virtio_read(block_num++, sec)) { 964 puts("Failed to read ISO volume descriptor"); 965 return 0; 966 } 967 } 968 969 return 0; 970 } 971 972 static IsoBcSection *find_iso_bc_entry(uint32_t offset) 973 { 974 IsoBcEntry *e = (IsoBcEntry *)sec; 975 int i; 976 unsigned int loadparm = get_loadparm_index(); 977 978 if (!offset) { 979 return NULL; 980 } 981 982 if (virtio_read(offset, sec)) { 983 puts("Failed to read El Torito boot catalog"); 984 return NULL; 985 } 986 987 if (!is_iso_bc_valid(e)) { 988 /* The validation entry is mandatory */ 989 return NULL; 990 } 991 992 /* 993 * Each entry has 32 bytes size, so one sector cannot contain > 64 entries. 994 * We consider only boot catalogs with no more than 64 entries. 995 */ 996 for (i = 1; i < ISO_BC_ENTRY_PER_SECTOR; i++) { 997 if (e[i].id == ISO_BC_BOOTABLE_SECTION) { 998 if (is_iso_bc_entry_compatible(&e[i].body.sect)) { 999 if (loadparm <= 1) { 1000 /* found, default, or unspecified */ 1001 return &e[i].body.sect; 1002 } 1003 loadparm--; 1004 } 1005 } 1006 } 1007 1008 return NULL; 1009 } 1010 1011 static int ipl_iso_el_torito(void) 1012 { 1013 uint32_t offset = find_iso_bc(); 1014 if (!offset) { 1015 return 0; 1016 } 1017 1018 IsoBcSection *s = find_iso_bc_entry(offset); 1019 1020 if (s) { 1021 load_iso_bc_entry(s); /* only return in error */ 1022 return -1; 1023 } 1024 1025 puts("No suitable boot entry found on ISO-9660 media!"); 1026 return -EIO; 1027 } 1028 1029 /** 1030 * Detect whether we're trying to boot from an .ISO image. 1031 * These always have a signature string "CD001" at offset 0x8001. 1032 */ 1033 static bool has_iso_signature(void) 1034 { 1035 int blksize = virtio_get_block_size(); 1036 1037 if (!blksize || virtio_read(0x8000 / blksize, sec)) { 1038 return false; 1039 } 1040 1041 return !memcmp("CD001", &sec[1], 5); 1042 } 1043 1044 /*********************************************************************** 1045 * Bus specific IPL sequences 1046 */ 1047 1048 static int zipl_load_vblk(void) 1049 { 1050 int blksize = virtio_get_block_size(); 1051 1052 if (blksize == VIRTIO_ISO_BLOCK_SIZE || has_iso_signature()) { 1053 if (blksize != VIRTIO_ISO_BLOCK_SIZE) { 1054 virtio_assume_iso9660(); 1055 } 1056 if (ipl_iso_el_torito()) { 1057 return 0; 1058 } 1059 } 1060 1061 if (blksize != VIRTIO_DASD_DEFAULT_BLOCK_SIZE) { 1062 puts("Using guessed DASD geometry."); 1063 virtio_assume_eckd(); 1064 } 1065 return ipl_eckd(); 1066 } 1067 1068 static int zipl_load_vscsi(void) 1069 { 1070 if (virtio_get_block_size() == VIRTIO_ISO_BLOCK_SIZE) { 1071 /* Is it an ISO image in non-CD drive? */ 1072 if (ipl_iso_el_torito()) { 1073 return 0; 1074 } 1075 } 1076 1077 puts("Using guessed DASD geometry."); 1078 virtio_assume_eckd(); 1079 return ipl_eckd(); 1080 } 1081 1082 /*********************************************************************** 1083 * IPL starts here 1084 */ 1085 1086 void zipl_load(void) 1087 { 1088 VDev *vdev = virtio_get_device(); 1089 1090 if (vdev->is_cdrom) { 1091 ipl_iso_el_torito(); 1092 puts("Failed to IPL this ISO image!"); 1093 return; 1094 } 1095 1096 if (virtio_get_device_type() == VIRTIO_ID_NET) { 1097 netmain(); 1098 puts("Failed to IPL from this network!"); 1099 return; 1100 } 1101 1102 if (ipl_scsi()) { 1103 puts("Failed to IPL from this SCSI device!"); 1104 return; 1105 } 1106 1107 switch (virtio_get_device_type()) { 1108 case VIRTIO_ID_BLOCK: 1109 zipl_load_vblk(); 1110 break; 1111 case VIRTIO_ID_SCSI: 1112 zipl_load_vscsi(); 1113 break; 1114 default: 1115 puts("Unknown IPL device type!"); 1116 return; 1117 } 1118 1119 puts("zIPL load failed!"); 1120 } 1121