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