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 */ 28 static uint8_t sec[MAX_SECTOR_SIZE*4] __attribute__((__aligned__(PAGE_SIZE))); 29 30 typedef struct ResetInfo { 31 uint32_t ipl_mask; 32 uint32_t ipl_addr; 33 uint32_t ipl_continue; 34 } ResetInfo; 35 36 static ResetInfo save; 37 38 static 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 47 static 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 virtio_panic("\n! IPL returns !\n"); 76 } 77 78 /*********************************************************************** 79 * IPL an ECKD DASD (CDL or LDL/CMS format) 80 */ 81 82 static unsigned char _bprs[8*1024]; /* guessed "max" ECKD sector size */ 83 static const int max_bprs_entries = sizeof(_bprs) / sizeof(ExtEckdBlockPtr); 84 85 static inline void verify_boot_info(BootInfo *bip) 86 { 87 IPL_assert(magic_match(bip->magic, ZIPL_MAGIC), "No zIPL magic"); 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 96 static 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 110 static 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 128 static 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 183 static 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 213 static 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 248 static 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 273 static 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 299 static 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 318 /*********************************************************************** 319 * IPL a SCSI disk 320 */ 321 322 static void zipl_load_segment(ComponentEntry *entry) 323 { 324 const int max_entries = (MAX_SECTOR_SIZE / sizeof(ScsiBlockPtr)); 325 ScsiBlockPtr *bprs = (void *)sec; 326 const int bprs_size = sizeof(sec); 327 block_number_t blockno; 328 uint64_t address; 329 int i; 330 char err_msg[] = "zIPL failed to read BPRS at 0xZZZZZZZZZZZZZZZZ"; 331 char *blk_no = &err_msg[30]; /* where to print blockno in (those ZZs) */ 332 333 blockno = entry->data.blockno; 334 address = entry->load_address; 335 336 debug_print_int("loading segment at block", blockno); 337 debug_print_int("addr", address); 338 339 do { 340 memset(bprs, FREE_SPACE_FILLER, bprs_size); 341 fill_hex_val(blk_no, &blockno, sizeof(blockno)); 342 read_block(blockno, bprs, err_msg); 343 344 for (i = 0;; i++) { 345 uint64_t *cur_desc = (void *)&bprs[i]; 346 347 blockno = bprs[i].blockno; 348 if (!blockno) { 349 break; 350 } 351 352 /* we need the updated blockno for the next indirect entry in the 353 chain, but don't want to advance address */ 354 if (i == (max_entries - 1)) { 355 break; 356 } 357 358 if (bprs[i].blockct == 0 && unused_space(&bprs[i + 1], 359 sizeof(ScsiBlockPtr))) { 360 /* This is a "continue" pointer. 361 * This ptr is the last one in the current script section. 362 * I.e. the next ptr must point to the unused memory area. 363 * The blockno is not zero, so the upper loop must continue 364 * reading next section of BPRS. 365 */ 366 break; 367 } 368 address = virtio_load_direct(cur_desc[0], cur_desc[1], 0, 369 (void *)address); 370 IPL_assert(address != -1, "zIPL load segment failed"); 371 } 372 } while (blockno); 373 } 374 375 /* Run a zipl program */ 376 static void zipl_run(ScsiBlockPtr *pte) 377 { 378 ComponentHeader *header; 379 ComponentEntry *entry; 380 uint8_t tmp_sec[MAX_SECTOR_SIZE]; 381 382 read_block(pte->blockno, tmp_sec, "Cannot read header"); 383 header = (ComponentHeader *)tmp_sec; 384 385 IPL_assert(magic_match(tmp_sec, ZIPL_MAGIC), "No zIPL magic"); 386 IPL_assert(header->type == ZIPL_COMP_HEADER_IPL, "Bad header type"); 387 388 dputs("start loading images\n"); 389 390 /* Load image(s) into RAM */ 391 entry = (ComponentEntry *)(&header[1]); 392 while (entry->component_type == ZIPL_COMP_ENTRY_LOAD) { 393 zipl_load_segment(entry); 394 395 entry++; 396 397 IPL_assert((uint8_t *)(&entry[1]) <= (tmp_sec + MAX_SECTOR_SIZE), 398 "Wrong entry value"); 399 } 400 401 IPL_assert(entry->component_type == ZIPL_COMP_ENTRY_EXEC, "No EXEC entry"); 402 403 /* should not return */ 404 jump_to_IPL_code(entry->load_address); 405 } 406 407 static void ipl_scsi(void) 408 { 409 ScsiMbr *mbr = (void *)sec; 410 uint8_t *ns, *ns_end; 411 int program_table_entries = 0; 412 const int pte_len = sizeof(ScsiBlockPtr); 413 ScsiBlockPtr *prog_table_entry; 414 415 /* The 0-th block (MBR) was already read into sec[] */ 416 417 sclp_print("Using SCSI scheme.\n"); 418 debug_print_int("program table", mbr->blockptr.blockno); 419 420 /* Parse the program table */ 421 read_block(mbr->blockptr.blockno, sec, 422 "Error reading Program Table"); 423 424 IPL_assert(magic_match(sec, ZIPL_MAGIC), "No zIPL magic"); 425 426 ns_end = sec + virtio_get_block_size(); 427 for (ns = (sec + pte_len); (ns + pte_len) < ns_end; ns++) { 428 prog_table_entry = (ScsiBlockPtr *)ns; 429 if (!prog_table_entry->blockno) { 430 break; 431 } 432 433 program_table_entries++; 434 } 435 436 debug_print_int("program table entries", program_table_entries); 437 438 IPL_assert(program_table_entries != 0, "Empty Program Table"); 439 440 /* Run the default entry */ 441 442 prog_table_entry = (ScsiBlockPtr *)(sec + pte_len); 443 444 zipl_run(prog_table_entry); /* no return */ 445 } 446 447 /*********************************************************************** 448 * IPL starts here 449 */ 450 451 void zipl_load(void) 452 { 453 ScsiMbr *mbr = (void *)sec; 454 LDL_VTOC *vlbl = (void *)sec; 455 456 /* Grab the MBR */ 457 memset(sec, FREE_SPACE_FILLER, sizeof(sec)); 458 read_block(0, mbr, "Cannot read block 0"); 459 460 dputs("checking magic\n"); 461 462 if (magic_match(mbr->magic, ZIPL_MAGIC)) { 463 ipl_scsi(); /* no return */ 464 } 465 466 /* We have failed to follow the SCSI scheme, so */ 467 if (virtio_guessed_disk_nature()) { 468 sclp_print("Using guessed DASD geometry.\n"); 469 virtio_assume_eckd(); 470 } 471 print_eckd_msg(); 472 if (magic_match(mbr->magic, IPL1_MAGIC)) { 473 ipl_eckd_cdl(); /* no return */ 474 } 475 476 /* LDL/CMS? */ 477 memset(sec, FREE_SPACE_FILLER, sizeof(sec)); 478 read_block(2, vlbl, "Cannot read block 2"); 479 480 if (magic_match(vlbl->magic, CMS1_MAGIC)) { 481 ipl_eckd_ldl(ECKD_CMS); /* no return */ 482 } 483 if (magic_match(vlbl->magic, LNX1_MAGIC)) { 484 ipl_eckd_ldl(ECKD_LDL); /* no return */ 485 } 486 487 ipl_eckd_ldl(ECKD_LDL_UNLABELED); /* it still may return */ 488 /* 489 * Ok, it is not a LDL by any means. 490 * It still might be a CDL with zero record keys for IPL1 and IPL2 491 */ 492 ipl_eckd_cdl(); 493 494 virtio_panic("\n* this can never happen *\n"); 495 } 496