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