pc-bios/s390-ccw: fix off-by-one error
[qemu/ar7.git] / pc-bios / s390-ccw / bootmap.c
blob767bb612dbd7038cb90c33446646890d658e99e0
1 /*
2 * QEMU S390 bootmap interpreter
4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
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 */
11 #include "libc.h"
12 #include "s390-ccw.h"
13 #include "bootmap.h"
14 #include "virtio.h"
15 #include "bswap.h"
17 #ifdef DEBUG
18 /* #define DEBUG_FALLBACK */
19 #endif
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
29 /* Scratch space */
30 static uint8_t sec[MAX_SECTOR_SIZE*4] __attribute__((__aligned__(PAGE_SIZE)));
32 const uint8_t el_torito_magic[] = "EL TORITO SPECIFICATION"
33 "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
36 * Match two CCWs located after PSW and eight filler bytes.
37 * From libmagic and arch/s390/kernel/head.S.
39 const uint8_t linux_s390_magic[] = "\x02\x00\x00\x18\x60\x00\x00\x50\x02\x00"
40 "\x00\x68\x60\x00\x00\x50\x40\x40\x40\x40"
41 "\x40\x40\x40\x40";
43 static inline bool is_iso_vd_valid(IsoVolDesc *vd)
45 const uint8_t vol_desc_magic[] = "CD001";
47 return !memcmp(&vd->ident[0], vol_desc_magic, 5) &&
48 vd->version == 0x1 &&
49 vd->type <= VOL_DESC_TYPE_PARTITION;
52 /***********************************************************************
53 * IPL an ECKD DASD (CDL or LDL/CMS format)
56 static unsigned char _bprs[8*1024]; /* guessed "max" ECKD sector size */
57 static const int max_bprs_entries = sizeof(_bprs) / sizeof(ExtEckdBlockPtr);
58 static uint8_t _s2[MAX_SECTOR_SIZE * 3] __attribute__((__aligned__(PAGE_SIZE)));
59 static void *s2_prev_blk = _s2;
60 static void *s2_cur_blk = _s2 + MAX_SECTOR_SIZE;
61 static void *s2_next_blk = _s2 + MAX_SECTOR_SIZE * 2;
63 static inline void verify_boot_info(BootInfo *bip)
65 IPL_assert(magic_match(bip->magic, ZIPL_MAGIC), "No zIPL sig in BootInfo");
66 IPL_assert(bip->version == BOOT_INFO_VERSION, "Wrong zIPL version");
67 IPL_assert(bip->bp_type == BOOT_INFO_BP_TYPE_IPL, "DASD is not for IPL");
68 IPL_assert(bip->dev_type == BOOT_INFO_DEV_TYPE_ECKD, "DASD is not ECKD");
69 IPL_assert(bip->flags == BOOT_INFO_FLAGS_ARCH, "Not for this arch");
70 IPL_assert(block_size_ok(bip->bp.ipl.bm_ptr.eckd.bptr.size),
71 "Bad block size in zIPL section of the 1st record.");
74 static block_number_t eckd_block_num(EckdCHS *chs)
76 const uint64_t sectors = virtio_get_sectors();
77 const uint64_t heads = virtio_get_heads();
78 const uint64_t cylinder = chs->cylinder
79 + ((chs->head & 0xfff0) << 12);
80 const uint64_t head = chs->head & 0x000f;
81 const block_number_t block = sectors * heads * cylinder
82 + sectors * head
83 + chs->sector
84 - 1; /* block nr starts with zero */
85 return block;
88 static bool eckd_valid_address(BootMapPointer *p)
90 const uint64_t head = p->eckd.chs.head & 0x000f;
92 if (head >= virtio_get_heads()
93 || p->eckd.chs.sector > virtio_get_sectors()
94 || p->eckd.chs.sector <= 0) {
95 return false;
98 if (!virtio_guessed_disk_nature() &&
99 eckd_block_num(&p->eckd.chs) >= virtio_get_blocks()) {
100 return false;
103 return true;
106 static block_number_t load_eckd_segments(block_number_t blk, uint64_t *address)
108 block_number_t block_nr;
109 int j, rc;
110 BootMapPointer *bprs = (void *)_bprs;
111 bool more_data;
113 memset(_bprs, FREE_SPACE_FILLER, sizeof(_bprs));
114 read_block(blk, bprs, "BPRS read failed");
116 do {
117 more_data = false;
118 for (j = 0;; j++) {
119 block_nr = eckd_block_num(&bprs[j].xeckd.bptr.chs);
120 if (is_null_block_number(block_nr)) { /* end of chunk */
121 break;
124 /* we need the updated blockno for the next indirect entry
125 * in the chain, but don't want to advance address
127 if (j == (max_bprs_entries - 1)) {
128 break;
131 IPL_assert(block_size_ok(bprs[j].xeckd.bptr.size),
132 "bad chunk block size");
133 IPL_assert(eckd_valid_address(&bprs[j]), "bad chunk ECKD addr");
135 if ((bprs[j].xeckd.bptr.count == 0) && unused_space(&(bprs[j+1]),
136 sizeof(EckdBlockPtr))) {
137 /* This is a "continue" pointer.
138 * This ptr should be the last one in the current
139 * script section.
140 * I.e. the next ptr must point to the unused memory area
142 memset(_bprs, FREE_SPACE_FILLER, sizeof(_bprs));
143 read_block(block_nr, bprs, "BPRS continuation read failed");
144 more_data = true;
145 break;
148 /* Load (count+1) blocks of code at (block_nr)
149 * to memory (address).
151 rc = virtio_read_many(block_nr, (void *)(*address),
152 bprs[j].xeckd.bptr.count+1);
153 IPL_assert(rc == 0, "code chunk read failed");
155 *address += (bprs[j].xeckd.bptr.count+1) * virtio_get_block_size();
157 } while (more_data);
158 return block_nr;
161 static bool find_zipl_boot_menu_banner(int *offset)
163 int i;
165 /* Menu banner starts with "zIPL" */
166 for (i = 0; i <= virtio_get_block_size() - 4; i++) {
167 if (magic_match(s2_cur_blk + i, ZIPL_MAGIC_EBCDIC)) {
168 *offset = i;
169 return true;
173 return false;
176 static int eckd_get_boot_menu_index(block_number_t s1b_block_nr)
178 block_number_t cur_block_nr;
179 block_number_t prev_block_nr = 0;
180 block_number_t next_block_nr = 0;
181 EckdStage1b *s1b = (void *)sec;
182 int banner_offset;
183 int i;
185 /* Get Stage1b data */
186 memset(sec, FREE_SPACE_FILLER, sizeof(sec));
187 read_block(s1b_block_nr, s1b, "Cannot read stage1b boot loader");
189 memset(_s2, FREE_SPACE_FILLER, sizeof(_s2));
191 /* Get Stage2 data */
192 for (i = 0; i < STAGE2_BLK_CNT_MAX; i++) {
193 cur_block_nr = eckd_block_num(&s1b->seek[i].chs);
195 if (!cur_block_nr) {
196 break;
199 read_block(cur_block_nr, s2_cur_blk, "Cannot read stage2 boot loader");
201 if (find_zipl_boot_menu_banner(&banner_offset)) {
203 * Load the adjacent blocks to account for the
204 * possibility of menu data spanning multiple blocks.
206 if (prev_block_nr) {
207 read_block(prev_block_nr, s2_prev_blk,
208 "Cannot read stage2 boot loader");
211 if (i + 1 < STAGE2_BLK_CNT_MAX) {
212 next_block_nr = eckd_block_num(&s1b->seek[i + 1].chs);
215 if (next_block_nr) {
216 read_block(next_block_nr, s2_next_blk,
217 "Cannot read stage2 boot loader");
220 return menu_get_zipl_boot_index(s2_cur_blk + banner_offset);
223 prev_block_nr = cur_block_nr;
226 sclp_print("No zipl boot menu data found. Booting default entry.");
227 return 0;
230 static void run_eckd_boot_script(block_number_t bmt_block_nr,
231 block_number_t s1b_block_nr)
233 int i;
234 unsigned int loadparm = get_loadparm_index();
235 block_number_t block_nr;
236 uint64_t address;
237 BootMapTable *bmt = (void *)sec;
238 BootMapScript *bms = (void *)sec;
240 if (menu_is_enabled_zipl()) {
241 loadparm = eckd_get_boot_menu_index(s1b_block_nr);
244 debug_print_int("loadparm", loadparm);
245 IPL_assert(loadparm < MAX_BOOT_ENTRIES, "loadparm value greater than"
246 " maximum number of boot entries allowed");
248 memset(sec, FREE_SPACE_FILLER, sizeof(sec));
249 read_block(bmt_block_nr, sec, "Cannot read Boot Map Table");
251 block_nr = eckd_block_num(&bmt->entry[loadparm].xeckd.bptr.chs);
252 IPL_assert(block_nr != -1, "Cannot find Boot Map Table Entry");
254 memset(sec, FREE_SPACE_FILLER, sizeof(sec));
255 read_block(block_nr, sec, "Cannot read Boot Map Script");
257 for (i = 0; bms->entry[i].type == BOOT_SCRIPT_LOAD ||
258 bms->entry[i].type == BOOT_SCRIPT_SIGNATURE; i++) {
260 /* We don't support secure boot yet, so we skip signature entries */
261 if (bms->entry[i].type == BOOT_SCRIPT_SIGNATURE) {
262 continue;
265 address = bms->entry[i].address.load_address;
266 block_nr = eckd_block_num(&bms->entry[i].blkptr.xeckd.bptr.chs);
268 do {
269 block_nr = load_eckd_segments(block_nr, &address);
270 } while (block_nr != -1);
273 IPL_assert(bms->entry[i].type == BOOT_SCRIPT_EXEC,
274 "Unknown script entry type");
275 jump_to_IPL_code(bms->entry[i].address.load_address); /* no return */
278 static void ipl_eckd_cdl(void)
280 XEckdMbr *mbr;
281 EckdCdlIpl2 *ipl2 = (void *)sec;
282 IplVolumeLabel *vlbl = (void *)sec;
283 block_number_t bmt_block_nr, s1b_block_nr;
285 /* we have just read the block #0 and recognized it as "IPL1" */
286 sclp_print("CDL\n");
288 memset(sec, FREE_SPACE_FILLER, sizeof(sec));
289 read_block(1, ipl2, "Cannot read IPL2 record at block 1");
291 mbr = &ipl2->mbr;
292 if (!magic_match(mbr, ZIPL_MAGIC)) {
293 sclp_print("No zIPL section in IPL2 record.\n");
294 return;
296 if (!block_size_ok(mbr->blockptr.xeckd.bptr.size)) {
297 sclp_print("Bad block size in zIPL section of IPL2 record.\n");
298 return;
300 if (!mbr->dev_type == DEV_TYPE_ECKD) {
301 sclp_print("Non-ECKD device type in zIPL section of IPL2 record.\n");
302 return;
305 /* save pointer to Boot Map Table */
306 bmt_block_nr = eckd_block_num(&mbr->blockptr.xeckd.bptr.chs);
308 /* save pointer to Stage1b Data */
309 s1b_block_nr = eckd_block_num(&ipl2->stage1.seek[0].chs);
311 memset(sec, FREE_SPACE_FILLER, sizeof(sec));
312 read_block(2, vlbl, "Cannot read Volume Label at block 2");
313 if (!magic_match(vlbl->key, VOL1_MAGIC)) {
314 sclp_print("Invalid magic of volume label block.\n");
315 return;
317 if (!magic_match(vlbl->f.key, VOL1_MAGIC)) {
318 sclp_print("Invalid magic of volser block.\n");
319 return;
321 print_volser(vlbl->f.volser);
323 run_eckd_boot_script(bmt_block_nr, s1b_block_nr);
324 /* no return */
327 static void print_eckd_ldl_msg(ECKD_IPL_mode_t mode)
329 LDL_VTOC *vlbl = (void *)sec; /* already read, 3rd block */
330 char msg[4] = { '?', '.', '\n', '\0' };
332 sclp_print((mode == ECKD_CMS) ? "CMS" : "LDL");
333 sclp_print(" version ");
334 switch (vlbl->LDL_version) {
335 case LDL1_VERSION:
336 msg[0] = '1';
337 break;
338 case LDL2_VERSION:
339 msg[0] = '2';
340 break;
341 default:
342 msg[0] = ebc2asc[vlbl->LDL_version];
343 msg[1] = '?';
344 break;
346 sclp_print(msg);
347 print_volser(vlbl->volser);
350 static void ipl_eckd_ldl(ECKD_IPL_mode_t mode)
352 block_number_t bmt_block_nr, s1b_block_nr;
353 EckdLdlIpl1 *ipl1 = (void *)sec;
355 if (mode != ECKD_LDL_UNLABELED) {
356 print_eckd_ldl_msg(mode);
359 /* DO NOT read BootMap pointer (only one, xECKD) at block #2 */
361 memset(sec, FREE_SPACE_FILLER, sizeof(sec));
362 read_block(0, sec, "Cannot read block 0 to grab boot info.");
363 if (mode == ECKD_LDL_UNLABELED) {
364 if (!magic_match(ipl1->bip.magic, ZIPL_MAGIC)) {
365 return; /* not applicable layout */
367 sclp_print("unlabeled LDL.\n");
369 verify_boot_info(&ipl1->bip);
371 /* save pointer to Boot Map Table */
372 bmt_block_nr = eckd_block_num(&ipl1->bip.bp.ipl.bm_ptr.eckd.bptr.chs);
374 /* save pointer to Stage1b Data */
375 s1b_block_nr = eckd_block_num(&ipl1->stage1.seek[0].chs);
377 run_eckd_boot_script(bmt_block_nr, s1b_block_nr);
378 /* no return */
381 static void print_eckd_msg(void)
383 char msg[] = "Using ECKD scheme (block size *****), ";
384 char *p = &msg[34], *q = &msg[30];
385 int n = virtio_get_block_size();
387 /* Fill in the block size and show up the message */
388 if (n > 0 && n <= 99999) {
389 while (n) {
390 *p-- = '0' + (n % 10);
391 n /= 10;
393 while (p >= q) {
394 *p-- = ' ';
397 sclp_print(msg);
400 static void ipl_eckd(void)
402 XEckdMbr *mbr = (void *)sec;
403 LDL_VTOC *vlbl = (void *)sec;
405 print_eckd_msg();
407 /* Grab the MBR again */
408 memset(sec, FREE_SPACE_FILLER, sizeof(sec));
409 read_block(0, mbr, "Cannot read block 0 on DASD");
411 if (magic_match(mbr->magic, IPL1_MAGIC)) {
412 ipl_eckd_cdl(); /* only returns in case of error */
413 return;
416 /* LDL/CMS? */
417 memset(sec, FREE_SPACE_FILLER, sizeof(sec));
418 read_block(2, vlbl, "Cannot read block 2");
420 if (magic_match(vlbl->magic, CMS1_MAGIC)) {
421 ipl_eckd_ldl(ECKD_CMS); /* no return */
423 if (magic_match(vlbl->magic, LNX1_MAGIC)) {
424 ipl_eckd_ldl(ECKD_LDL); /* no return */
427 ipl_eckd_ldl(ECKD_LDL_UNLABELED); /* it still may return */
429 * Ok, it is not a LDL by any means.
430 * It still might be a CDL with zero record keys for IPL1 and IPL2
432 ipl_eckd_cdl();
435 /***********************************************************************
436 * IPL a SCSI disk
439 static void zipl_load_segment(ComponentEntry *entry)
441 const int max_entries = (MAX_SECTOR_SIZE / sizeof(ScsiBlockPtr));
442 ScsiBlockPtr *bprs = (void *)sec;
443 const int bprs_size = sizeof(sec);
444 block_number_t blockno;
445 uint64_t address;
446 int i;
447 char err_msg[] = "zIPL failed to read BPRS at 0xZZZZZZZZZZZZZZZZ";
448 char *blk_no = &err_msg[30]; /* where to print blockno in (those ZZs) */
450 blockno = entry->data.blockno;
451 address = entry->load_address;
453 debug_print_int("loading segment at block", blockno);
454 debug_print_int("addr", address);
456 do {
457 memset(bprs, FREE_SPACE_FILLER, bprs_size);
458 fill_hex_val(blk_no, &blockno, sizeof(blockno));
459 read_block(blockno, bprs, err_msg);
461 for (i = 0;; i++) {
462 uint64_t *cur_desc = (void *)&bprs[i];
464 blockno = bprs[i].blockno;
465 if (!blockno) {
466 break;
469 /* we need the updated blockno for the next indirect entry in the
470 chain, but don't want to advance address */
471 if (i == (max_entries - 1)) {
472 break;
475 if (bprs[i].blockct == 0 && unused_space(&bprs[i + 1],
476 sizeof(ScsiBlockPtr))) {
477 /* This is a "continue" pointer.
478 * This ptr is the last one in the current script section.
479 * I.e. the next ptr must point to the unused memory area.
480 * The blockno is not zero, so the upper loop must continue
481 * reading next section of BPRS.
483 break;
485 address = virtio_load_direct(cur_desc[0], cur_desc[1], 0,
486 (void *)address);
487 IPL_assert(address != -1, "zIPL load segment failed");
489 } while (blockno);
492 /* Run a zipl program */
493 static void zipl_run(ScsiBlockPtr *pte)
495 ComponentHeader *header;
496 ComponentEntry *entry;
497 uint8_t tmp_sec[MAX_SECTOR_SIZE];
499 read_block(pte->blockno, tmp_sec, "Cannot read header");
500 header = (ComponentHeader *)tmp_sec;
502 IPL_assert(magic_match(tmp_sec, ZIPL_MAGIC), "No zIPL magic in header");
503 IPL_assert(header->type == ZIPL_COMP_HEADER_IPL, "Bad header type");
505 dputs("start loading images\n");
507 /* Load image(s) into RAM */
508 entry = (ComponentEntry *)(&header[1]);
509 while (entry->component_type == ZIPL_COMP_ENTRY_LOAD ||
510 entry->component_type == ZIPL_COMP_ENTRY_SIGNATURE) {
512 /* We don't support secure boot yet, so we skip signature entries */
513 if (entry->component_type == ZIPL_COMP_ENTRY_SIGNATURE) {
514 entry++;
515 continue;
518 zipl_load_segment(entry);
520 entry++;
522 IPL_assert((uint8_t *)(&entry[1]) <= (tmp_sec + MAX_SECTOR_SIZE),
523 "Wrong entry value");
526 IPL_assert(entry->component_type == ZIPL_COMP_ENTRY_EXEC, "No EXEC entry");
528 /* should not return */
529 jump_to_IPL_code(entry->load_address);
532 static void ipl_scsi(void)
534 ScsiMbr *mbr = (void *)sec;
535 int program_table_entries = 0;
536 BootMapTable *prog_table = (void *)sec;
537 unsigned int loadparm = get_loadparm_index();
538 bool valid_entries[MAX_BOOT_ENTRIES] = {false};
539 size_t i;
541 /* Grab the MBR */
542 memset(sec, FREE_SPACE_FILLER, sizeof(sec));
543 read_block(0, mbr, "Cannot read block 0");
545 if (!magic_match(mbr->magic, ZIPL_MAGIC)) {
546 return;
549 sclp_print("Using SCSI scheme.\n");
550 debug_print_int("MBR Version", mbr->version_id);
551 IPL_check(mbr->version_id == 1,
552 "Unknown MBR layout version, assuming version 1");
553 debug_print_int("program table", mbr->pt.blockno);
554 IPL_assert(mbr->pt.blockno, "No Program Table");
556 /* Parse the program table */
557 read_block(mbr->pt.blockno, sec, "Error reading Program Table");
558 IPL_assert(magic_match(sec, ZIPL_MAGIC), "No zIPL magic in PT");
560 for (i = 0; i < MAX_BOOT_ENTRIES; i++) {
561 if (prog_table->entry[i].scsi.blockno) {
562 valid_entries[i] = true;
563 program_table_entries++;
567 debug_print_int("program table entries", program_table_entries);
568 IPL_assert(program_table_entries != 0, "Empty Program Table");
570 if (menu_is_enabled_enum()) {
571 loadparm = menu_get_enum_boot_index(valid_entries);
574 debug_print_int("loadparm", loadparm);
575 IPL_assert(loadparm < MAX_BOOT_ENTRIES, "loadparm value greater than"
576 " maximum number of boot entries allowed");
578 zipl_run(&prog_table->entry[loadparm].scsi); /* no return */
581 /***********************************************************************
582 * IPL El Torito ISO9660 image or DVD
585 static bool is_iso_bc_entry_compatible(IsoBcSection *s)
587 uint8_t *magic_sec = (uint8_t *)(sec + ISO_SECTOR_SIZE);
589 if (s->unused || !s->sector_count) {
590 return false;
592 read_iso_sector(bswap32(s->load_rba), magic_sec,
593 "Failed to read image sector 0");
595 /* Checking bytes 8 - 32 for S390 Linux magic */
596 return !memcmp(magic_sec + 8, linux_s390_magic, 24);
599 /* Location of the current sector of the directory */
600 static uint32_t sec_loc[ISO9660_MAX_DIR_DEPTH];
601 /* Offset in the current sector of the directory */
602 static uint32_t sec_offset[ISO9660_MAX_DIR_DEPTH];
603 /* Remained directory space in bytes */
604 static uint32_t dir_rem[ISO9660_MAX_DIR_DEPTH];
606 static inline uint32_t iso_get_file_size(uint32_t load_rba)
608 IsoVolDesc *vd = (IsoVolDesc *)sec;
609 IsoDirHdr *cur_record = &vd->vd.primary.rootdir;
610 uint8_t *temp = sec + ISO_SECTOR_SIZE;
611 int level = 0;
613 read_iso_sector(ISO_PRIMARY_VD_SECTOR, sec,
614 "Failed to read ISO primary descriptor");
615 sec_loc[0] = iso_733_to_u32(cur_record->ext_loc);
616 dir_rem[0] = 0;
617 sec_offset[0] = 0;
619 while (level >= 0) {
620 IPL_assert(sec_offset[level] <= ISO_SECTOR_SIZE,
621 "Directory tree structure violation");
623 cur_record = (IsoDirHdr *)(temp + sec_offset[level]);
625 if (sec_offset[level] == 0) {
626 read_iso_sector(sec_loc[level], temp,
627 "Failed to read ISO directory");
628 if (dir_rem[level] == 0) {
629 /* Skip self and parent records */
630 dir_rem[level] = iso_733_to_u32(cur_record->data_len) -
631 cur_record->dr_len;
632 sec_offset[level] += cur_record->dr_len;
634 cur_record = (IsoDirHdr *)(temp + sec_offset[level]);
635 dir_rem[level] -= cur_record->dr_len;
636 sec_offset[level] += cur_record->dr_len;
637 continue;
641 if (!cur_record->dr_len || sec_offset[level] == ISO_SECTOR_SIZE) {
642 /* Zero-padding and/or the end of current sector */
643 dir_rem[level] -= ISO_SECTOR_SIZE - sec_offset[level];
644 sec_offset[level] = 0;
645 sec_loc[level]++;
646 } else {
647 /* The directory record is valid */
648 if (load_rba == iso_733_to_u32(cur_record->ext_loc)) {
649 return iso_733_to_u32(cur_record->data_len);
652 dir_rem[level] -= cur_record->dr_len;
653 sec_offset[level] += cur_record->dr_len;
655 if (cur_record->file_flags & 0x2) {
656 /* Subdirectory */
657 if (level == ISO9660_MAX_DIR_DEPTH - 1) {
658 sclp_print("ISO-9660 directory depth limit exceeded\n");
659 } else {
660 level++;
661 sec_loc[level] = iso_733_to_u32(cur_record->ext_loc);
662 sec_offset[level] = 0;
663 dir_rem[level] = 0;
664 continue;
669 if (dir_rem[level] == 0) {
670 /* Nothing remaining */
671 level--;
672 read_iso_sector(sec_loc[level], temp,
673 "Failed to read ISO directory");
677 return 0;
680 static void load_iso_bc_entry(IsoBcSection *load)
682 IsoBcSection s = *load;
684 * According to spec, extent for each file
685 * is padded and ISO_SECTOR_SIZE bytes aligned
687 uint32_t blks_to_load = bswap16(s.sector_count) >> ET_SECTOR_SHIFT;
688 uint32_t real_size = iso_get_file_size(bswap32(s.load_rba));
690 if (real_size) {
691 /* Round up blocks to load */
692 blks_to_load = (real_size + ISO_SECTOR_SIZE - 1) / ISO_SECTOR_SIZE;
693 sclp_print("ISO boot image size verified\n");
694 } else {
695 sclp_print("ISO boot image size could not be verified\n");
698 read_iso_boot_image(bswap32(s.load_rba),
699 (void *)((uint64_t)bswap16(s.load_segment)),
700 blks_to_load);
702 jump_to_low_kernel();
705 static uint32_t find_iso_bc(void)
707 IsoVolDesc *vd = (IsoVolDesc *)sec;
708 uint32_t block_num = ISO_PRIMARY_VD_SECTOR;
710 if (virtio_read_many(block_num++, sec, 1)) {
711 /* If primary vd cannot be read, there is no boot catalog */
712 return 0;
715 while (is_iso_vd_valid(vd) && vd->type != VOL_DESC_TERMINATOR) {
716 if (vd->type == VOL_DESC_TYPE_BOOT) {
717 IsoVdElTorito *et = &vd->vd.boot;
719 if (!memcmp(&et->el_torito[0], el_torito_magic, 32)) {
720 return bswap32(et->bc_offset);
723 read_iso_sector(block_num++, sec,
724 "Failed to read ISO volume descriptor");
727 return 0;
730 static IsoBcSection *find_iso_bc_entry(void)
732 IsoBcEntry *e = (IsoBcEntry *)sec;
733 uint32_t offset = find_iso_bc();
734 int i;
735 unsigned int loadparm = get_loadparm_index();
737 if (!offset) {
738 return NULL;
741 read_iso_sector(offset, sec, "Failed to read El Torito boot catalog");
743 if (!is_iso_bc_valid(e)) {
744 /* The validation entry is mandatory */
745 panic("No valid boot catalog found!\n");
746 return NULL;
750 * Each entry has 32 bytes size, so one sector cannot contain > 64 entries.
751 * We consider only boot catalogs with no more than 64 entries.
753 for (i = 1; i < ISO_BC_ENTRY_PER_SECTOR; i++) {
754 if (e[i].id == ISO_BC_BOOTABLE_SECTION) {
755 if (is_iso_bc_entry_compatible(&e[i].body.sect)) {
756 if (loadparm <= 1) {
757 /* found, default, or unspecified */
758 return &e[i].body.sect;
760 loadparm--;
765 panic("No suitable boot entry found on ISO-9660 media!\n");
767 return NULL;
770 static void ipl_iso_el_torito(void)
772 IsoBcSection *s = find_iso_bc_entry();
774 if (s) {
775 load_iso_bc_entry(s);
776 /* no return */
780 /***********************************************************************
781 * Bus specific IPL sequences
784 static void zipl_load_vblk(void)
786 if (virtio_guessed_disk_nature()) {
787 virtio_assume_iso9660();
789 ipl_iso_el_torito();
791 if (virtio_guessed_disk_nature()) {
792 sclp_print("Using guessed DASD geometry.\n");
793 virtio_assume_eckd();
795 ipl_eckd();
798 static void zipl_load_vscsi(void)
800 if (virtio_get_block_size() == VIRTIO_ISO_BLOCK_SIZE) {
801 /* Is it an ISO image in non-CD drive? */
802 ipl_iso_el_torito();
805 sclp_print("Using guessed DASD geometry.\n");
806 virtio_assume_eckd();
807 ipl_eckd();
810 /***********************************************************************
811 * IPL starts here
814 void zipl_load(void)
816 VDev *vdev = virtio_get_device();
818 if (vdev->is_cdrom) {
819 ipl_iso_el_torito();
820 panic("\n! Cannot IPL this ISO image !\n");
823 if (virtio_get_device_type() == VIRTIO_ID_NET) {
824 jump_to_IPL_code(vdev->netboot_start_addr);
827 ipl_scsi();
829 switch (virtio_get_device_type()) {
830 case VIRTIO_ID_BLOCK:
831 zipl_load_vblk();
832 break;
833 case VIRTIO_ID_SCSI:
834 zipl_load_vscsi();
835 break;
836 default:
837 panic("\n! Unknown IPL device type !\n");
840 sclp_print("zIPL load failed.\n");