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