hw/arm/virt: fix max-cpus check
[qemu/ar7.git] / pc-bios / s390-ccw / bootmap.c
blob415508b27927ff533f89a14decadd92fb3d185b1
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 "s390-ccw.h"
12 #include "bootmap.h"
13 #include "virtio.h"
15 #ifdef DEBUG
16 /* #define DEBUG_FALLBACK */
17 #endif
19 #ifdef DEBUG_FALLBACK
20 #define dputs(txt) \
21 do { sclp_print("zipl: " txt); } while (0)
22 #else
23 #define dputs(fmt, ...) \
24 do { } while (0)
25 #endif
27 /* Scratch space */
28 static uint8_t sec[MAX_SECTOR_SIZE*4] __attribute__((__aligned__(PAGE_SIZE)));
30 typedef struct ResetInfo {
31 uint32_t ipl_mask;
32 uint32_t ipl_addr;
33 uint32_t ipl_continue;
34 } ResetInfo;
36 static ResetInfo save;
38 static void jump_to_IPL_2(void)
40 ResetInfo *current = 0;
42 void (*ipl)(void) = (void *) (uint64_t) current->ipl_continue;
43 *current = save;
44 ipl(); /* should not return */
47 static void jump_to_IPL_code(uint64_t address)
49 /* store the subsystem information _after_ the bootmap was loaded */
50 write_subsystem_identification();
52 * The IPL PSW is at address 0. We also must not overwrite the
53 * content of non-BIOS memory after we loaded the guest, so we
54 * save the original content and restore it in jump_to_IPL_2.
56 ResetInfo *current = 0;
58 save = *current;
59 current->ipl_addr = (uint32_t) (uint64_t) &jump_to_IPL_2;
60 current->ipl_continue = address & 0x7fffffff;
62 debug_print_int("set IPL addr to", current->ipl_continue);
64 /* Ensure the guest output starts fresh */
65 sclp_print("\n");
68 * HACK ALERT.
69 * We use the load normal reset to keep r15 unchanged. jump_to_IPL_2
70 * can then use r15 as its stack pointer.
72 asm volatile("lghi 1,1\n\t"
73 "diag 1,1,0x308\n\t"
74 : : : "1", "memory");
75 virtio_panic("\n! IPL returns !\n");
78 /***********************************************************************
79 * IPL an ECKD DASD (CDL or LDL/CMS format)
82 static unsigned char _bprs[8*1024]; /* guessed "max" ECKD sector size */
83 static const int max_bprs_entries = sizeof(_bprs) / sizeof(ExtEckdBlockPtr);
85 static inline void verify_boot_info(BootInfo *bip)
87 IPL_assert(magic_match(bip->magic, ZIPL_MAGIC), "No zIPL magic");
88 IPL_assert(bip->version == BOOT_INFO_VERSION, "Wrong zIPL version");
89 IPL_assert(bip->bp_type == BOOT_INFO_BP_TYPE_IPL, "DASD is not for IPL");
90 IPL_assert(bip->dev_type == BOOT_INFO_DEV_TYPE_ECKD, "DASD is not ECKD");
91 IPL_assert(bip->flags == BOOT_INFO_FLAGS_ARCH, "Not for this arch");
92 IPL_assert(block_size_ok(bip->bp.ipl.bm_ptr.eckd.bptr.size),
93 "Bad block size in zIPL section of the 1st record.");
96 static block_number_t eckd_block_num(BootMapPointer *p)
98 const uint64_t sectors = virtio_get_sectors();
99 const uint64_t heads = virtio_get_heads();
100 const uint64_t cylinder = p->eckd.cylinder
101 + ((p->eckd.head & 0xfff0) << 12);
102 const uint64_t head = p->eckd.head & 0x000f;
103 const block_number_t block = sectors * heads * cylinder
104 + sectors * head
105 + p->eckd.sector
106 - 1; /* block nr starts with zero */
107 return block;
110 static bool eckd_valid_address(BootMapPointer *p)
112 const uint64_t head = p->eckd.head & 0x000f;
114 if (head >= virtio_get_heads()
115 || p->eckd.sector > virtio_get_sectors()
116 || p->eckd.sector <= 0) {
117 return false;
120 if (!virtio_guessed_disk_nature() &&
121 eckd_block_num(p) >= virtio_get_blocks()) {
122 return false;
125 return true;
128 static block_number_t load_eckd_segments(block_number_t blk, uint64_t *address)
130 block_number_t block_nr;
131 int j, rc;
132 BootMapPointer *bprs = (void *)_bprs;
133 bool more_data;
135 memset(_bprs, FREE_SPACE_FILLER, sizeof(_bprs));
136 read_block(blk, bprs, "BPRS read failed");
138 do {
139 more_data = false;
140 for (j = 0;; j++) {
141 block_nr = eckd_block_num((void *)&(bprs[j].xeckd));
142 if (is_null_block_number(block_nr)) { /* end of chunk */
143 break;
146 /* we need the updated blockno for the next indirect entry
147 * in the chain, but don't want to advance address
149 if (j == (max_bprs_entries - 1)) {
150 break;
153 IPL_assert(block_size_ok(bprs[j].xeckd.bptr.size),
154 "bad chunk block size");
155 IPL_assert(eckd_valid_address(&bprs[j]), "bad chunk ECKD addr");
157 if ((bprs[j].xeckd.bptr.count == 0) && unused_space(&(bprs[j+1]),
158 sizeof(EckdBlockPtr))) {
159 /* This is a "continue" pointer.
160 * This ptr should be the last one in the current
161 * script section.
162 * I.e. the next ptr must point to the unused memory area
164 memset(_bprs, FREE_SPACE_FILLER, sizeof(_bprs));
165 read_block(block_nr, bprs, "BPRS continuation read failed");
166 more_data = true;
167 break;
170 /* Load (count+1) blocks of code at (block_nr)
171 * to memory (address).
173 rc = virtio_read_many(block_nr, (void *)(*address),
174 bprs[j].xeckd.bptr.count+1);
175 IPL_assert(rc == 0, "code chunk read failed");
177 *address += (bprs[j].xeckd.bptr.count+1) * virtio_get_block_size();
179 } while (more_data);
180 return block_nr;
183 static void run_eckd_boot_script(block_number_t mbr_block_nr)
185 int i;
186 block_number_t block_nr;
187 uint64_t address;
188 ScsiMbr *scsi_mbr = (void *)sec;
189 BootMapScript *bms = (void *)sec;
191 memset(sec, FREE_SPACE_FILLER, sizeof(sec));
192 read_block(mbr_block_nr, sec, "Cannot read MBR");
194 block_nr = eckd_block_num((void *)&(scsi_mbr->blockptr));
196 memset(sec, FREE_SPACE_FILLER, sizeof(sec));
197 read_block(block_nr, sec, "Cannot read Boot Map Script");
199 for (i = 0; bms->entry[i].type == BOOT_SCRIPT_LOAD; i++) {
200 address = bms->entry[i].address.load_address;
201 block_nr = eckd_block_num(&(bms->entry[i].blkptr));
203 do {
204 block_nr = load_eckd_segments(block_nr, &address);
205 } while (block_nr != -1);
208 IPL_assert(bms->entry[i].type == BOOT_SCRIPT_EXEC,
209 "Unknown script entry type");
210 jump_to_IPL_code(bms->entry[i].address.load_address); /* no return */
213 static void ipl_eckd_cdl(void)
215 XEckdMbr *mbr;
216 Ipl2 *ipl2 = (void *)sec;
217 IplVolumeLabel *vlbl = (void *)sec;
218 block_number_t block_nr;
220 /* we have just read the block #0 and recognized it as "IPL1" */
221 sclp_print("CDL\n");
223 memset(sec, FREE_SPACE_FILLER, sizeof(sec));
224 read_block(1, ipl2, "Cannot read IPL2 record at block 1");
226 mbr = &ipl2->u.x.mbr;
227 IPL_assert(magic_match(mbr, ZIPL_MAGIC), "No zIPL section in IPL2 record.");
228 IPL_assert(block_size_ok(mbr->blockptr.xeckd.bptr.size),
229 "Bad block size in zIPL section of IPL2 record.");
230 IPL_assert(mbr->dev_type == DEV_TYPE_ECKD,
231 "Non-ECKD device type in zIPL section of IPL2 record.");
233 /* save pointer to Boot Script */
234 block_nr = eckd_block_num((void *)&(mbr->blockptr));
236 memset(sec, FREE_SPACE_FILLER, sizeof(sec));
237 read_block(2, vlbl, "Cannot read Volume Label at block 2");
238 IPL_assert(magic_match(vlbl->key, VOL1_MAGIC),
239 "Invalid magic of volume label block");
240 IPL_assert(magic_match(vlbl->f.key, VOL1_MAGIC),
241 "Invalid magic of volser block");
242 print_volser(vlbl->f.volser);
244 run_eckd_boot_script(block_nr);
245 /* no return */
248 static void print_eckd_ldl_msg(ECKD_IPL_mode_t mode)
250 LDL_VTOC *vlbl = (void *)sec; /* already read, 3rd block */
251 char msg[4] = { '?', '.', '\n', '\0' };
253 sclp_print((mode == ECKD_CMS) ? "CMS" : "LDL");
254 sclp_print(" version ");
255 switch (vlbl->LDL_version) {
256 case LDL1_VERSION:
257 msg[0] = '1';
258 break;
259 case LDL2_VERSION:
260 msg[0] = '2';
261 break;
262 default:
263 msg[0] = vlbl->LDL_version;
264 msg[0] &= 0x0f; /* convert EBCDIC */
265 msg[0] |= 0x30; /* to ASCII (digit) */
266 msg[1] = '?';
267 break;
269 sclp_print(msg);
270 print_volser(vlbl->volser);
273 static void ipl_eckd_ldl(ECKD_IPL_mode_t mode)
275 block_number_t block_nr;
276 BootInfo *bip = (void *)(sec + 0x70); /* BootInfo is MBR for LDL */
278 if (mode != ECKD_LDL_UNLABELED) {
279 print_eckd_ldl_msg(mode);
282 /* DO NOT read BootMap pointer (only one, xECKD) at block #2 */
284 memset(sec, FREE_SPACE_FILLER, sizeof(sec));
285 read_block(0, sec, "Cannot read block 0 to grab boot info.");
286 if (mode == ECKD_LDL_UNLABELED) {
287 if (!magic_match(bip->magic, ZIPL_MAGIC)) {
288 return; /* not applicable layout */
290 sclp_print("unlabeled LDL.\n");
292 verify_boot_info(bip);
294 block_nr = eckd_block_num((void *)&(bip->bp.ipl.bm_ptr.eckd.bptr));
295 run_eckd_boot_script(block_nr);
296 /* no return */
299 static void print_eckd_msg(void)
301 char msg[] = "Using ECKD scheme (block size *****), ";
302 char *p = &msg[34], *q = &msg[30];
303 int n = virtio_get_block_size();
305 /* Fill in the block size and show up the message */
306 if (n > 0 && n <= 99999) {
307 while (n) {
308 *p-- = '0' + (n % 10);
309 n /= 10;
311 while (p >= q) {
312 *p-- = ' ';
315 sclp_print(msg);
318 /***********************************************************************
319 * IPL a SCSI disk
322 static void zipl_load_segment(ComponentEntry *entry)
324 const int max_entries = (MAX_SECTOR_SIZE / sizeof(ScsiBlockPtr));
325 ScsiBlockPtr *bprs = (void *)sec;
326 const int bprs_size = sizeof(sec);
327 block_number_t blockno;
328 uint64_t address;
329 int i;
330 char err_msg[] = "zIPL failed to read BPRS at 0xZZZZZZZZZZZZZZZZ";
331 char *blk_no = &err_msg[30]; /* where to print blockno in (those ZZs) */
333 blockno = entry->data.blockno;
334 address = entry->load_address;
336 debug_print_int("loading segment at block", blockno);
337 debug_print_int("addr", address);
339 do {
340 memset(bprs, FREE_SPACE_FILLER, bprs_size);
341 fill_hex_val(blk_no, &blockno, sizeof(blockno));
342 read_block(blockno, bprs, err_msg);
344 for (i = 0;; i++) {
345 uint64_t *cur_desc = (void *)&bprs[i];
347 blockno = bprs[i].blockno;
348 if (!blockno) {
349 break;
352 /* we need the updated blockno for the next indirect entry in the
353 chain, but don't want to advance address */
354 if (i == (max_entries - 1)) {
355 break;
358 if (bprs[i].blockct == 0 && unused_space(&bprs[i + 1],
359 sizeof(ScsiBlockPtr))) {
360 /* This is a "continue" pointer.
361 * This ptr is the last one in the current script section.
362 * I.e. the next ptr must point to the unused memory area.
363 * The blockno is not zero, so the upper loop must continue
364 * reading next section of BPRS.
366 break;
368 address = virtio_load_direct(cur_desc[0], cur_desc[1], 0,
369 (void *)address);
370 IPL_assert(address != -1, "zIPL load segment failed");
372 } while (blockno);
375 /* Run a zipl program */
376 static void zipl_run(ScsiBlockPtr *pte)
378 ComponentHeader *header;
379 ComponentEntry *entry;
380 uint8_t tmp_sec[MAX_SECTOR_SIZE];
382 read_block(pte->blockno, tmp_sec, "Cannot read header");
383 header = (ComponentHeader *)tmp_sec;
385 IPL_assert(magic_match(tmp_sec, ZIPL_MAGIC), "No zIPL magic");
386 IPL_assert(header->type == ZIPL_COMP_HEADER_IPL, "Bad header type");
388 dputs("start loading images\n");
390 /* Load image(s) into RAM */
391 entry = (ComponentEntry *)(&header[1]);
392 while (entry->component_type == ZIPL_COMP_ENTRY_LOAD) {
393 zipl_load_segment(entry);
395 entry++;
397 IPL_assert((uint8_t *)(&entry[1]) <= (tmp_sec + MAX_SECTOR_SIZE),
398 "Wrong entry value");
401 IPL_assert(entry->component_type == ZIPL_COMP_ENTRY_EXEC, "No EXEC entry");
403 /* should not return */
404 jump_to_IPL_code(entry->load_address);
407 static void ipl_scsi(void)
409 ScsiMbr *mbr = (void *)sec;
410 uint8_t *ns, *ns_end;
411 int program_table_entries = 0;
412 const int pte_len = sizeof(ScsiBlockPtr);
413 ScsiBlockPtr *prog_table_entry;
415 /* The 0-th block (MBR) was already read into sec[] */
417 sclp_print("Using SCSI scheme.\n");
418 debug_print_int("program table", mbr->blockptr.blockno);
420 /* Parse the program table */
421 read_block(mbr->blockptr.blockno, sec,
422 "Error reading Program Table");
424 IPL_assert(magic_match(sec, ZIPL_MAGIC), "No zIPL magic");
426 ns_end = sec + virtio_get_block_size();
427 for (ns = (sec + pte_len); (ns + pte_len) < ns_end; ns++) {
428 prog_table_entry = (ScsiBlockPtr *)ns;
429 if (!prog_table_entry->blockno) {
430 break;
433 program_table_entries++;
436 debug_print_int("program table entries", program_table_entries);
438 IPL_assert(program_table_entries != 0, "Empty Program Table");
440 /* Run the default entry */
442 prog_table_entry = (ScsiBlockPtr *)(sec + pte_len);
444 zipl_run(prog_table_entry); /* no return */
447 /***********************************************************************
448 * IPL El Torito ISO9660 image or DVD
451 static bool is_iso_bc_entry_compatible(IsoBcSection *s)
453 uint8_t *magic_sec = (uint8_t *)(sec + ISO_SECTOR_SIZE);
455 if (s->unused || !s->sector_count) {
456 return false;
458 read_iso_sector(bswap32(s->load_rba), magic_sec,
459 "Failed to read image sector 0");
461 /* Checking bytes 8 - 32 for S390 Linux magic */
462 return !_memcmp(magic_sec + 8, linux_s390_magic, 24);
465 /* Location of the current sector of the directory */
466 static uint32_t sec_loc[ISO9660_MAX_DIR_DEPTH];
467 /* Offset in the current sector of the directory */
468 static uint32_t sec_offset[ISO9660_MAX_DIR_DEPTH];
469 /* Remained directory space in bytes */
470 static uint32_t dir_rem[ISO9660_MAX_DIR_DEPTH];
472 static inline uint32_t iso_get_file_size(uint32_t load_rba)
474 IsoVolDesc *vd = (IsoVolDesc *)sec;
475 IsoDirHdr *cur_record = &vd->vd.primary.rootdir;
476 uint8_t *temp = sec + ISO_SECTOR_SIZE;
477 int level = 0;
479 read_iso_sector(ISO_PRIMARY_VD_SECTOR, sec,
480 "Failed to read ISO primary descriptor");
481 sec_loc[0] = iso_733_to_u32(cur_record->ext_loc);
482 dir_rem[0] = 0;
483 sec_offset[0] = 0;
485 while (level >= 0) {
486 IPL_assert(sec_offset[level] <= ISO_SECTOR_SIZE,
487 "Directory tree structure violation");
489 cur_record = (IsoDirHdr *)(temp + sec_offset[level]);
491 if (sec_offset[level] == 0) {
492 read_iso_sector(sec_loc[level], temp,
493 "Failed to read ISO directory");
494 if (dir_rem[level] == 0) {
495 /* Skip self and parent records */
496 dir_rem[level] = iso_733_to_u32(cur_record->data_len) -
497 cur_record->dr_len;
498 sec_offset[level] += cur_record->dr_len;
500 cur_record = (IsoDirHdr *)(temp + sec_offset[level]);
501 dir_rem[level] -= cur_record->dr_len;
502 sec_offset[level] += cur_record->dr_len;
503 continue;
507 if (!cur_record->dr_len || sec_offset[level] == ISO_SECTOR_SIZE) {
508 /* Zero-padding and/or the end of current sector */
509 dir_rem[level] -= ISO_SECTOR_SIZE - sec_offset[level];
510 sec_offset[level] = 0;
511 sec_loc[level]++;
512 } else {
513 /* The directory record is valid */
514 if (load_rba == iso_733_to_u32(cur_record->ext_loc)) {
515 return iso_733_to_u32(cur_record->data_len);
518 dir_rem[level] -= cur_record->dr_len;
519 sec_offset[level] += cur_record->dr_len;
521 if (cur_record->file_flags & 0x2) {
522 /* Subdirectory */
523 if (level == ISO9660_MAX_DIR_DEPTH - 1) {
524 sclp_print("ISO-9660 directory depth limit exceeded\n");
525 } else {
526 level++;
527 sec_loc[level] = iso_733_to_u32(cur_record->ext_loc);
528 sec_offset[level] = 0;
529 dir_rem[level] = 0;
530 continue;
535 if (dir_rem[level] == 0) {
536 /* Nothing remaining */
537 level--;
538 read_iso_sector(sec_loc[level], temp,
539 "Failed to read ISO directory");
543 return 0;
546 static void load_iso_bc_entry(IsoBcSection *load)
548 IsoBcSection s = *load;
550 * According to spec, extent for each file
551 * is padded and ISO_SECTOR_SIZE bytes aligned
553 uint32_t blks_to_load = bswap16(s.sector_count) >> ET_SECTOR_SHIFT;
554 uint32_t real_size = iso_get_file_size(bswap32(s.load_rba));
556 if (real_size) {
557 /* Round up blocks to load */
558 blks_to_load = (real_size + ISO_SECTOR_SIZE - 1) / ISO_SECTOR_SIZE;
559 sclp_print("ISO boot image size verified\n");
560 } else {
561 sclp_print("ISO boot image size could not be verified\n");
564 read_iso_boot_image(bswap32(s.load_rba),
565 (void *)((uint64_t)bswap16(s.load_segment)),
566 blks_to_load);
568 /* Trying to get PSW at zero address */
569 if (*((uint64_t *)0) & IPL_PSW_MASK) {
570 jump_to_IPL_code((*((uint64_t *)0)) & 0x7fffffff);
573 /* Try default linux start address */
574 jump_to_IPL_code(KERN_IMAGE_START);
577 static uint32_t find_iso_bc(void)
579 IsoVolDesc *vd = (IsoVolDesc *)sec;
580 uint32_t block_num = ISO_PRIMARY_VD_SECTOR;
582 if (virtio_read_many(block_num++, sec, 1)) {
583 /* If primary vd cannot be read, there is no boot catalog */
584 return 0;
587 while (is_iso_vd_valid(vd) && vd->type != VOL_DESC_TERMINATOR) {
588 if (vd->type == VOL_DESC_TYPE_BOOT) {
589 IsoVdElTorito *et = &vd->vd.boot;
591 if (!_memcmp(&et->el_torito[0], el_torito_magic, 32)) {
592 return bswap32(et->bc_offset);
595 read_iso_sector(block_num++, sec,
596 "Failed to read ISO volume descriptor");
599 return 0;
602 static IsoBcSection *find_iso_bc_entry(void)
604 IsoBcEntry *e = (IsoBcEntry *)sec;
605 uint32_t offset = find_iso_bc();
606 int i;
608 if (!offset) {
609 return NULL;
612 read_iso_sector(offset, sec, "Failed to read El Torito boot catalog");
614 if (!is_iso_bc_valid(e)) {
615 /* The validation entry is mandatory */
616 virtio_panic("No valid boot catalog found!\n");
617 return NULL;
621 * Each entry has 32 bytes size, so one sector cannot contain > 64 entries.
622 * We consider only boot catalogs with no more than 64 entries.
624 for (i = 1; i < ISO_BC_ENTRY_PER_SECTOR; i++) {
625 if (e[i].id == ISO_BC_BOOTABLE_SECTION) {
626 if (is_iso_bc_entry_compatible(&e[i].body.sect)) {
627 return &e[i].body.sect;
632 virtio_panic("No suitable boot entry found on ISO-9660 media!\n");
634 return NULL;
637 static void ipl_iso_el_torito(void)
639 IsoBcSection *s = find_iso_bc_entry();
641 if (s) {
642 load_iso_bc_entry(s);
643 /* no return */
647 /***********************************************************************
648 * IPL starts here
651 void zipl_load(void)
653 ScsiMbr *mbr = (void *)sec;
654 LDL_VTOC *vlbl = (void *)sec;
656 /* Grab the MBR */
657 memset(sec, FREE_SPACE_FILLER, sizeof(sec));
658 read_block(0, mbr, "Cannot read block 0");
660 dputs("checking magic\n");
662 if (magic_match(mbr->magic, ZIPL_MAGIC)) {
663 ipl_scsi(); /* no return */
666 /* Check if we can boot as ISO media */
667 if (virtio_guessed_disk_nature()) {
668 virtio_assume_iso9660();
670 ipl_iso_el_torito();
672 /* We have failed to follow the SCSI scheme, so */
673 if (virtio_guessed_disk_nature()) {
674 sclp_print("Using guessed DASD geometry.\n");
675 virtio_assume_eckd();
677 print_eckd_msg();
678 if (magic_match(mbr->magic, IPL1_MAGIC)) {
679 ipl_eckd_cdl(); /* no return */
682 /* LDL/CMS? */
683 memset(sec, FREE_SPACE_FILLER, sizeof(sec));
684 read_block(2, vlbl, "Cannot read block 2");
686 if (magic_match(vlbl->magic, CMS1_MAGIC)) {
687 ipl_eckd_ldl(ECKD_CMS); /* no return */
689 if (magic_match(vlbl->magic, LNX1_MAGIC)) {
690 ipl_eckd_ldl(ECKD_LDL); /* no return */
693 ipl_eckd_ldl(ECKD_LDL_UNLABELED); /* it still may return */
695 * Ok, it is not a LDL by any means.
696 * It still might be a CDL with zero record keys for IPL1 and IPL2
698 ipl_eckd_cdl();
700 virtio_panic("\n* this can never happen *\n");