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
is_iso_vd_valid(IsoVolDesc * vd)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
verify_boot_info(BootInfo * bip)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
eckd_format_chs(ExtEckdBlockPtr * ptr,bool ldipl,uint64_t * c,uint64_t * h,uint64_t * s)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
eckd_chs_to_block(uint64_t c,uint64_t h,uint64_t s)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
eckd_block_num(EckdCHS * chs)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
gen_eckd_block_num(ExtEckdBlockPtr * ptr,bool ldipl)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
eckd_valid_chs(uint64_t cyl,uint64_t head,uint64_t sector)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
eckd_valid_address(ExtEckdBlockPtr * ptr,bool ldipl)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
load_eckd_segments(block_number_t blk,bool ldipl,uint64_t * address)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
find_zipl_boot_menu_banner(int * offset)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
eckd_get_boot_menu_index(block_number_t s1b_block_nr)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
run_eckd_boot_script(block_number_t bmt_block_nr,block_number_t s1b_block_nr)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 puts("loadparm value greater than max number of boot entries allowed");
340 return -EINVAL;
341 }
342
343 memset(sec, FREE_SPACE_FILLER, sizeof(sec));
344 if (virtio_read(bmt_block_nr, sec)) {
345 puts("Cannot read Boot Map Table");
346 return -EIO;
347 }
348
349 block_nr = gen_eckd_block_num(&bmt->entry[loadparm].xeckd, ldipl);
350 if (block_nr == NULL_BLOCK_NR) {
351 puts("Cannot find Boot Map Table Entry");
352 return -EIO;
353 }
354
355 memset(sec, FREE_SPACE_FILLER, sizeof(sec));
356 if (virtio_read(block_nr, sec)) {
357 puts("Cannot read Boot Map Script");
358 return -EIO;
359 }
360
361 for (i = 0; bms->entry[i].type == BOOT_SCRIPT_LOAD ||
362 bms->entry[i].type == BOOT_SCRIPT_SIGNATURE; i++) {
363
364 /* We don't support secure boot yet, so we skip signature entries */
365 if (bms->entry[i].type == BOOT_SCRIPT_SIGNATURE) {
366 continue;
367 }
368
369 address = bms->entry[i].address.load_address;
370 block_nr = gen_eckd_block_num(&bms->entry[i].blkptr.xeckd, ldipl);
371
372 do {
373 block_nr = load_eckd_segments(block_nr, ldipl, &address);
374 if (block_nr == ERROR_BLOCK_NR) {
375 return ldipl ? 0 : -EIO;
376 }
377 } while (block_nr != NULL_BLOCK_NR);
378 }
379
380 if (ldipl && bms->entry[i].type != BOOT_SCRIPT_EXEC) {
381 /* Abort LD-IPL and retry as CCW-IPL */
382 return 0;
383 }
384
385 if (bms->entry[i].type != BOOT_SCRIPT_EXEC) {
386 puts("Unknown script entry type");
387 return -EINVAL;
388 }
389 write_reset_psw(bms->entry[i].address.load_address);
390 jump_to_IPL_code(0);
391 return -1;
392 }
393
ipl_eckd_cdl(void)394 static int ipl_eckd_cdl(void)
395 {
396 XEckdMbr *mbr;
397 EckdCdlIpl2 *ipl2 = (void *)sec;
398 IplVolumeLabel *vlbl = (void *)sec;
399 block_number_t bmt_block_nr, s1b_block_nr;
400
401 /* we have just read the block #0 and recognized it as "IPL1" */
402 puts("CDL");
403
404 memset(sec, FREE_SPACE_FILLER, sizeof(sec));
405 if (virtio_read(1, ipl2)) {
406 puts("Cannot read IPL2 record at block 1");
407 return -EIO;
408 }
409
410 mbr = &ipl2->mbr;
411 if (!magic_match(mbr, ZIPL_MAGIC)) {
412 puts("No zIPL section in IPL2 record.");
413 return 0;
414 }
415 if (!block_size_ok(mbr->blockptr.xeckd.bptr.size)) {
416 puts("Bad block size in zIPL section of IPL2 record.");
417 return 0;
418 }
419 if (mbr->dev_type != DEV_TYPE_ECKD) {
420 puts("Non-ECKD device type in zIPL section of IPL2 record.");
421 return 0;
422 }
423
424 /* save pointer to Boot Map Table */
425 bmt_block_nr = eckd_block_num(&mbr->blockptr.xeckd.bptr.chs);
426
427 /* save pointer to Stage1b Data */
428 s1b_block_nr = eckd_block_num(&ipl2->stage1.seek[0].chs);
429
430 memset(sec, FREE_SPACE_FILLER, sizeof(sec));
431 if (virtio_read(2, vlbl)) {
432 puts("Cannot read Volume Label at block 2");
433 return -EIO;
434 }
435 if (!magic_match(vlbl->key, VOL1_MAGIC)) {
436 puts("Invalid magic of volume label block.");
437 return 0;
438 }
439 if (!magic_match(vlbl->f.key, VOL1_MAGIC)) {
440 puts("Invalid magic of volser block.");
441 return 0;
442 }
443 print_volser(vlbl->f.volser);
444
445 return run_eckd_boot_script(bmt_block_nr, s1b_block_nr);
446 }
447
print_eckd_ldl_msg(ECKD_IPL_mode_t mode)448 static void print_eckd_ldl_msg(ECKD_IPL_mode_t mode)
449 {
450 LDL_VTOC *vlbl = (void *)sec; /* already read, 3rd block */
451 char msg[4] = { '?', '.', '\n', '\0' };
452
453 printf((mode == ECKD_CMS) ? "CMS" : "LDL");
454 printf(" version ");
455 switch (vlbl->LDL_version) {
456 case LDL1_VERSION:
457 msg[0] = '1';
458 break;
459 case LDL2_VERSION:
460 msg[0] = '2';
461 break;
462 default:
463 msg[0] = ebc2asc[vlbl->LDL_version];
464 msg[1] = '?';
465 break;
466 }
467 printf("%s", msg);
468 print_volser(vlbl->volser);
469 }
470
ipl_eckd_ldl(ECKD_IPL_mode_t mode)471 static int ipl_eckd_ldl(ECKD_IPL_mode_t mode)
472 {
473 block_number_t bmt_block_nr, s1b_block_nr;
474 EckdLdlIpl1 *ipl1 = (void *)sec;
475
476 if (mode != ECKD_LDL_UNLABELED) {
477 print_eckd_ldl_msg(mode);
478 }
479
480 /* DO NOT read BootMap pointer (only one, xECKD) at block #2 */
481
482 memset(sec, FREE_SPACE_FILLER, sizeof(sec));
483 if (virtio_read(0, sec)) {
484 puts("Cannot read block 0 to grab boot info.");
485 return -EIO;
486 }
487 if (mode == ECKD_LDL_UNLABELED) {
488 if (!magic_match(ipl1->bip.magic, ZIPL_MAGIC)) {
489 return 0; /* not applicable layout */
490 }
491 puts("unlabeled LDL.");
492 }
493 verify_boot_info(&ipl1->bip);
494
495 /* save pointer to Boot Map Table */
496 bmt_block_nr = eckd_block_num(&ipl1->bip.bp.ipl.bm_ptr.eckd.bptr.chs);
497
498 /* save pointer to Stage1b Data */
499 s1b_block_nr = eckd_block_num(&ipl1->stage1.seek[0].chs);
500
501 return run_eckd_boot_script(bmt_block_nr, s1b_block_nr);
502 }
503
eckd_find_bmt(ExtEckdBlockPtr * ptr)504 static block_number_t eckd_find_bmt(ExtEckdBlockPtr *ptr)
505 {
506 block_number_t blockno;
507 uint8_t tmp_sec[MAX_SECTOR_SIZE];
508 BootRecord *br;
509
510 blockno = gen_eckd_block_num(ptr, 0);
511 if (virtio_read(blockno, tmp_sec)) {
512 puts("Cannot read boot record");
513 return ERROR_BLOCK_NR;
514 }
515 br = (BootRecord *)tmp_sec;
516 if (!magic_match(br->magic, ZIPL_MAGIC)) {
517 /* If the boot record is invalid, return and try CCW-IPL instead */
518 return NULL_BLOCK_NR;
519 }
520
521 return gen_eckd_block_num(&br->pgt.xeckd, 1);
522 }
523
print_eckd_msg(void)524 static void print_eckd_msg(void)
525 {
526 char msg[] = "Using ECKD scheme (block size *****), ";
527 char *p = &msg[34], *q = &msg[30];
528 int n = virtio_get_block_size();
529
530 /* Fill in the block size and show up the message */
531 if (n > 0 && n <= 99999) {
532 while (n) {
533 *p-- = '0' + (n % 10);
534 n /= 10;
535 }
536 while (p >= q) {
537 *p-- = ' ';
538 }
539 }
540 printf("%s", msg);
541 }
542
ipl_eckd(void)543 static int ipl_eckd(void)
544 {
545 IplVolumeLabel *vlbl = (void *)sec;
546 LDL_VTOC *vtoc = (void *)sec;
547 block_number_t ldipl_bmt; /* Boot Map Table for List-Directed IPL */
548
549 print_eckd_msg();
550
551 /* Block 2 can contain either the CDL VOL1 label or the LDL VTOC */
552 memset(sec, FREE_SPACE_FILLER, sizeof(sec));
553 if (virtio_read(2, vlbl)) {
554 puts("Cannot read block 2");
555 return -EIO;
556 }
557
558 /*
559 * First check for a list-directed-format pointer which would
560 * supersede the CCW pointer.
561 */
562 if (eckd_valid_address((ExtEckdBlockPtr *)&vlbl->f.br, 0)) {
563 ldipl_bmt = eckd_find_bmt((ExtEckdBlockPtr *)&vlbl->f.br);
564 switch (ldipl_bmt) {
565 case ERROR_BLOCK_NR:
566 return -EIO;
567 case NULL_BLOCK_NR:
568 break; /* Invalid BMT but the device may still boot with CCW-IPL */
569 default:
570 puts("List-Directed");
571 /*
572 * LD-IPL does not use the S1B bock, just make it NULL_BLOCK_NR.
573 * In some failure cases retry IPL before aborting.
574 */
575 if (run_eckd_boot_script(ldipl_bmt, NULL_BLOCK_NR)) {
576 return -EIO;
577 }
578 /* Non-fatal error, retry as CCW-IPL */
579 printf("Retrying IPL ");
580 print_eckd_msg();
581 }
582 memset(sec, FREE_SPACE_FILLER, sizeof(sec));
583 if (virtio_read(2, vtoc)) {
584 puts("Cannot read block 2");
585 return -EIO;
586 }
587 }
588
589 /* Not list-directed */
590 if (magic_match(vtoc->magic, VOL1_MAGIC)) {
591 if (ipl_eckd_cdl()) {
592 return -1;
593 }
594 }
595
596 if (magic_match(vtoc->magic, CMS1_MAGIC)) {
597 return ipl_eckd_ldl(ECKD_CMS);
598 }
599 if (magic_match(vtoc->magic, LNX1_MAGIC)) {
600 return ipl_eckd_ldl(ECKD_LDL);
601 }
602
603 if (ipl_eckd_ldl(ECKD_LDL_UNLABELED)) {
604 return -1;
605 }
606 /*
607 * Ok, it is not a LDL by any means.
608 * It still might be a CDL with zero record keys for IPL1 and IPL2
609 */
610 return ipl_eckd_cdl();
611 }
612
613 /***********************************************************************
614 * IPL a SCSI disk
615 */
616
zipl_load_segment(ComponentEntry * entry)617 static int zipl_load_segment(ComponentEntry *entry)
618 {
619 const int max_entries = (MAX_SECTOR_SIZE / sizeof(ScsiBlockPtr));
620 ScsiBlockPtr *bprs = (void *)sec;
621 const int bprs_size = sizeof(sec);
622 block_number_t blockno;
623 uint64_t address;
624 int i;
625 char err_msg[] = "zIPL failed to read BPRS at 0xZZZZZZZZZZZZZZZZ";
626 char *blk_no = &err_msg[30]; /* where to print blockno in (those ZZs) */
627
628 blockno = entry->data.blockno;
629 address = entry->compdat.load_addr;
630
631 debug_print_int("loading segment at block", blockno);
632 debug_print_int("addr", address);
633
634 do {
635 memset(bprs, FREE_SPACE_FILLER, bprs_size);
636 fill_hex_val(blk_no, &blockno, sizeof(blockno));
637 if (virtio_read(blockno, bprs)) {
638 puts(err_msg);
639 return -EIO;
640 }
641
642 for (i = 0;; i++) {
643 uint64_t *cur_desc = (void *)&bprs[i];
644
645 blockno = bprs[i].blockno;
646 if (!blockno) {
647 break;
648 }
649
650 /* we need the updated blockno for the next indirect entry in the
651 chain, but don't want to advance address */
652 if (i == (max_entries - 1)) {
653 break;
654 }
655
656 if (bprs[i].blockct == 0 && unused_space(&bprs[i + 1],
657 sizeof(ScsiBlockPtr))) {
658 /* This is a "continue" pointer.
659 * This ptr is the last one in the current script section.
660 * I.e. the next ptr must point to the unused memory area.
661 * The blockno is not zero, so the upper loop must continue
662 * reading next section of BPRS.
663 */
664 break;
665 }
666 address = virtio_load_direct(cur_desc[0], cur_desc[1], 0,
667 (void *)address);
668 if (!address) {
669 puts("zIPL load segment failed");
670 return -EIO;
671 }
672 }
673 } while (blockno);
674
675 return 0;
676 }
677
678 /* Run a zipl program */
zipl_run(ScsiBlockPtr * pte)679 static int zipl_run(ScsiBlockPtr *pte)
680 {
681 ComponentHeader *header;
682 ComponentEntry *entry;
683 uint8_t tmp_sec[MAX_SECTOR_SIZE];
684
685 if (virtio_read(pte->blockno, tmp_sec)) {
686 puts("Cannot read header");
687 return -EIO;
688 }
689 header = (ComponentHeader *)tmp_sec;
690
691 if (!magic_match(tmp_sec, ZIPL_MAGIC)) {
692 puts("No zIPL magic in header");
693 return -EINVAL;
694 }
695 if (header->type != ZIPL_COMP_HEADER_IPL) {
696 puts("Bad header type");
697 return -EINVAL;
698 }
699
700 dputs("start loading images\n");
701
702 /* Load image(s) into RAM */
703 entry = (ComponentEntry *)(&header[1]);
704 while (entry->component_type == ZIPL_COMP_ENTRY_LOAD ||
705 entry->component_type == ZIPL_COMP_ENTRY_SIGNATURE) {
706
707 /* We don't support secure boot yet, so we skip signature entries */
708 if (entry->component_type == ZIPL_COMP_ENTRY_SIGNATURE) {
709 entry++;
710 continue;
711 }
712
713 if (zipl_load_segment(entry)) {
714 return -1;
715 }
716
717 entry++;
718
719 if ((uint8_t *)(&entry[1]) > (tmp_sec + MAX_SECTOR_SIZE)) {
720 puts("Wrong entry value");
721 return -EINVAL;
722 }
723 }
724
725 if (entry->component_type != ZIPL_COMP_ENTRY_EXEC) {
726 puts("No EXEC entry");
727 return -EINVAL;
728 }
729
730 /* should not return */
731 write_reset_psw(entry->compdat.load_psw);
732 jump_to_IPL_code(0);
733 return -1;
734 }
735
ipl_scsi(void)736 static int ipl_scsi(void)
737 {
738 ScsiMbr *mbr = (void *)sec;
739 int program_table_entries = 0;
740 BootMapTable *prog_table = (void *)sec;
741 unsigned int loadparm = get_loadparm_index();
742 bool valid_entries[MAX_BOOT_ENTRIES] = {false};
743 size_t i;
744
745 /* Grab the MBR */
746 memset(sec, FREE_SPACE_FILLER, sizeof(sec));
747 if (virtio_read(0, mbr)) {
748 puts("Cannot read block 0");
749 return -EIO;
750 }
751
752 if (!magic_match(mbr->magic, ZIPL_MAGIC)) {
753 return 0;
754 }
755
756 puts("Using SCSI scheme.");
757 debug_print_int("MBR Version", mbr->version_id);
758 IPL_check(mbr->version_id == 1,
759 "Unknown MBR layout version, assuming version 1");
760 debug_print_int("program table", mbr->pt.blockno);
761 if (!mbr->pt.blockno) {
762 puts("No Program Table");
763 return -EINVAL;
764 }
765
766 /* Parse the program table */
767 if (virtio_read(mbr->pt.blockno, sec)) {
768 puts("Error reading Program Table");
769 return -EIO;
770 }
771 if (!magic_match(sec, ZIPL_MAGIC)) {
772 puts("No zIPL magic in Program Table");
773 return -EINVAL;
774 }
775
776 for (i = 0; i < MAX_BOOT_ENTRIES; i++) {
777 if (prog_table->entry[i].scsi.blockno) {
778 valid_entries[i] = true;
779 program_table_entries++;
780 }
781 }
782
783 debug_print_int("program table entries", program_table_entries);
784 if (program_table_entries == 0) {
785 puts("Empty Program Table");
786 return -EINVAL;
787 }
788
789 if (menu_is_enabled_enum()) {
790 loadparm = menu_get_enum_boot_index(valid_entries);
791 }
792
793 debug_print_int("loadparm", loadparm);
794 if (loadparm >= MAX_BOOT_ENTRIES) {
795 puts("loadparm value greater than max number of boot entries allowed");
796 return -EINVAL;
797 }
798
799 return zipl_run(&prog_table->entry[loadparm].scsi);
800 }
801
802 /***********************************************************************
803 * IPL El Torito ISO9660 image or DVD
804 */
805
is_iso_bc_entry_compatible(IsoBcSection * s)806 static bool is_iso_bc_entry_compatible(IsoBcSection *s)
807 {
808 uint8_t *magic_sec = (uint8_t *)(sec + ISO_SECTOR_SIZE);
809
810 if (s->unused || !s->sector_count) {
811 return false;
812 }
813 if (virtio_read(bswap32(s->load_rba), magic_sec)) {
814 puts("Failed to read image sector 0");
815 return false;
816 }
817
818 /* Checking bytes 8 - 32 for S390 Linux magic */
819 return !memcmp(magic_sec + 8, linux_s390_magic, 24);
820 }
821
822 /* Location of the current sector of the directory */
823 static uint32_t sec_loc[ISO9660_MAX_DIR_DEPTH];
824 /* Offset in the current sector of the directory */
825 static uint32_t sec_offset[ISO9660_MAX_DIR_DEPTH];
826 /* Remained directory space in bytes */
827 static uint32_t dir_rem[ISO9660_MAX_DIR_DEPTH];
828
iso_get_file_size(uint32_t load_rba)829 static inline long iso_get_file_size(uint32_t load_rba)
830 {
831 IsoVolDesc *vd = (IsoVolDesc *)sec;
832 IsoDirHdr *cur_record = &vd->vd.primary.rootdir;
833 uint8_t *temp = sec + ISO_SECTOR_SIZE;
834 int level = 0;
835
836 if (virtio_read(ISO_PRIMARY_VD_SECTOR, sec)) {
837 puts("Failed to read ISO primary descriptor");
838 return -EIO;
839 }
840
841 sec_loc[0] = iso_733_to_u32(cur_record->ext_loc);
842 dir_rem[0] = 0;
843 sec_offset[0] = 0;
844
845 while (level >= 0) {
846 if (sec_offset[level] > ISO_SECTOR_SIZE) {
847 puts("Directory tree structure violation");
848 return -EIO;
849 }
850
851 cur_record = (IsoDirHdr *)(temp + sec_offset[level]);
852
853 if (sec_offset[level] == 0) {
854 if (virtio_read(sec_loc[level], temp)) {
855 puts("Failed to read ISO directory");
856 return -EIO;
857 }
858 if (dir_rem[level] == 0) {
859 /* Skip self and parent records */
860 dir_rem[level] = iso_733_to_u32(cur_record->data_len) -
861 cur_record->dr_len;
862 sec_offset[level] += cur_record->dr_len;
863
864 cur_record = (IsoDirHdr *)(temp + sec_offset[level]);
865 dir_rem[level] -= cur_record->dr_len;
866 sec_offset[level] += cur_record->dr_len;
867 continue;
868 }
869 }
870
871 if (!cur_record->dr_len || sec_offset[level] == ISO_SECTOR_SIZE) {
872 /* Zero-padding and/or the end of current sector */
873 dir_rem[level] -= ISO_SECTOR_SIZE - sec_offset[level];
874 sec_offset[level] = 0;
875 sec_loc[level]++;
876 } else {
877 /* The directory record is valid */
878 if (load_rba == iso_733_to_u32(cur_record->ext_loc)) {
879 return iso_733_to_u32(cur_record->data_len);
880 }
881
882 dir_rem[level] -= cur_record->dr_len;
883 sec_offset[level] += cur_record->dr_len;
884
885 if (cur_record->file_flags & 0x2) {
886 /* Subdirectory */
887 if (level == ISO9660_MAX_DIR_DEPTH - 1) {
888 puts("ISO-9660 directory depth limit exceeded");
889 } else {
890 level++;
891 sec_loc[level] = iso_733_to_u32(cur_record->ext_loc);
892 sec_offset[level] = 0;
893 dir_rem[level] = 0;
894 continue;
895 }
896 }
897 }
898
899 if (dir_rem[level] == 0) {
900 /* Nothing remaining */
901 level--;
902 if (virtio_read(sec_loc[level], temp)) {
903 puts("Failed to read ISO directory");
904 return -EIO;
905 }
906 }
907 }
908
909 return 0;
910 }
911
load_iso_bc_entry(IsoBcSection * load)912 static void load_iso_bc_entry(IsoBcSection *load)
913 {
914 IsoBcSection s = *load;
915 /*
916 * According to spec, extent for each file
917 * is padded and ISO_SECTOR_SIZE bytes aligned
918 */
919 uint32_t blks_to_load = bswap16(s.sector_count) >> ET_SECTOR_SHIFT;
920 long real_size = iso_get_file_size(bswap32(s.load_rba));
921
922 if (real_size > 0) {
923 /* Round up blocks to load */
924 blks_to_load = (real_size + ISO_SECTOR_SIZE - 1) / ISO_SECTOR_SIZE;
925 puts("ISO boot image size verified");
926 } else {
927 puts("ISO boot image size could not be verified");
928 if (real_size < 0) {
929 return;
930 }
931 }
932
933 if (read_iso_boot_image(bswap32(s.load_rba),
934 (void *)((uint64_t)bswap16(s.load_segment)),
935 blks_to_load)) {
936 return;
937 }
938
939 jump_to_low_kernel();
940 }
941
find_iso_bc(void)942 static uint32_t find_iso_bc(void)
943 {
944 IsoVolDesc *vd = (IsoVolDesc *)sec;
945 uint32_t block_num = ISO_PRIMARY_VD_SECTOR;
946
947 if (virtio_read_many(block_num++, sec, 1)) {
948 /* If primary vd cannot be read, there is no boot catalog */
949 return 0;
950 }
951
952 while (is_iso_vd_valid(vd) && vd->type != VOL_DESC_TERMINATOR) {
953 if (vd->type == VOL_DESC_TYPE_BOOT) {
954 IsoVdElTorito *et = &vd->vd.boot;
955
956 if (!memcmp(&et->el_torito[0], el_torito_magic, 32)) {
957 return bswap32(et->bc_offset);
958 }
959 }
960 if (virtio_read(block_num++, sec)) {
961 puts("Failed to read ISO volume descriptor");
962 return 0;
963 }
964 }
965
966 return 0;
967 }
968
find_iso_bc_entry(uint32_t offset)969 static IsoBcSection *find_iso_bc_entry(uint32_t offset)
970 {
971 IsoBcEntry *e = (IsoBcEntry *)sec;
972 int i;
973 unsigned int loadparm = get_loadparm_index();
974
975 if (!offset) {
976 return NULL;
977 }
978
979 if (virtio_read(offset, sec)) {
980 puts("Failed to read El Torito boot catalog");
981 return NULL;
982 }
983
984 if (!is_iso_bc_valid(e)) {
985 /* The validation entry is mandatory */
986 return NULL;
987 }
988
989 /*
990 * Each entry has 32 bytes size, so one sector cannot contain > 64 entries.
991 * We consider only boot catalogs with no more than 64 entries.
992 */
993 for (i = 1; i < ISO_BC_ENTRY_PER_SECTOR; i++) {
994 if (e[i].id == ISO_BC_BOOTABLE_SECTION) {
995 if (is_iso_bc_entry_compatible(&e[i].body.sect)) {
996 if (loadparm <= 1) {
997 /* found, default, or unspecified */
998 return &e[i].body.sect;
999 }
1000 loadparm--;
1001 }
1002 }
1003 }
1004
1005 return NULL;
1006 }
1007
ipl_iso_el_torito(void)1008 static int ipl_iso_el_torito(void)
1009 {
1010 uint32_t offset = find_iso_bc();
1011 if (!offset) {
1012 return 0;
1013 }
1014
1015 IsoBcSection *s = find_iso_bc_entry(offset);
1016
1017 if (s) {
1018 load_iso_bc_entry(s); /* only return in error */
1019 return -1;
1020 }
1021
1022 puts("No suitable boot entry found on ISO-9660 media!");
1023 return -EIO;
1024 }
1025
1026 /**
1027 * Detect whether we're trying to boot from an .ISO image.
1028 * These always have a signature string "CD001" at offset 0x8001.
1029 */
has_iso_signature(void)1030 static bool has_iso_signature(void)
1031 {
1032 int blksize = virtio_get_block_size();
1033
1034 if (!blksize || virtio_read(0x8000 / blksize, sec)) {
1035 return false;
1036 }
1037
1038 return !memcmp("CD001", &sec[1], 5);
1039 }
1040
1041 /***********************************************************************
1042 * Bus specific IPL sequences
1043 */
1044
zipl_load_vblk(void)1045 static int zipl_load_vblk(void)
1046 {
1047 int blksize = virtio_get_block_size();
1048
1049 if (blksize == VIRTIO_ISO_BLOCK_SIZE || has_iso_signature()) {
1050 if (blksize != VIRTIO_ISO_BLOCK_SIZE) {
1051 virtio_assume_iso9660();
1052 }
1053 if (ipl_iso_el_torito()) {
1054 return 0;
1055 }
1056 }
1057
1058 if (blksize != VIRTIO_DASD_DEFAULT_BLOCK_SIZE) {
1059 puts("Using guessed DASD geometry.");
1060 virtio_assume_eckd();
1061 }
1062 return ipl_eckd();
1063 }
1064
zipl_load_vscsi(void)1065 static int zipl_load_vscsi(void)
1066 {
1067 if (virtio_get_block_size() == VIRTIO_ISO_BLOCK_SIZE) {
1068 /* Is it an ISO image in non-CD drive? */
1069 if (ipl_iso_el_torito()) {
1070 return 0;
1071 }
1072 }
1073
1074 puts("Using guessed DASD geometry.");
1075 virtio_assume_eckd();
1076 return ipl_eckd();
1077 }
1078
1079 /***********************************************************************
1080 * IPL starts here
1081 */
1082
zipl_load(void)1083 void zipl_load(void)
1084 {
1085 VDev *vdev = virtio_get_device();
1086
1087 if (vdev->is_cdrom) {
1088 ipl_iso_el_torito();
1089 puts("Failed to IPL this ISO image!");
1090 return;
1091 }
1092
1093 if (virtio_get_device_type() == VIRTIO_ID_NET) {
1094 netmain();
1095 puts("Failed to IPL from this network!");
1096 return;
1097 }
1098
1099 if (ipl_scsi()) {
1100 puts("Failed to IPL from this SCSI device!");
1101 return;
1102 }
1103
1104 switch (virtio_get_device_type()) {
1105 case VIRTIO_ID_BLOCK:
1106 zipl_load_vblk();
1107 break;
1108 case VIRTIO_ID_SCSI:
1109 zipl_load_vscsi();
1110 break;
1111 default:
1112 puts("Unknown IPL device type!");
1113 return;
1114 }
1115
1116 puts("zIPL load failed!");
1117 }
1118