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