qxl: keep going if reaching guest bug on empty area
[qemu/ar7.git] / pc-bios / s390-ccw / bootmap.c
blob115d8bbac6f5d488f0f80e31526716f032de7565
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 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 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 starts here
451 void zipl_load(void)
453 ScsiMbr *mbr = (void *)sec;
454 LDL_VTOC *vlbl = (void *)sec;
456 /* Grab the MBR */
457 memset(sec, FREE_SPACE_FILLER, sizeof(sec));
458 read_block(0, mbr, "Cannot read block 0");
460 dputs("checking magic\n");
462 if (magic_match(mbr->magic, ZIPL_MAGIC)) {
463 ipl_scsi(); /* no return */
466 /* We have failed to follow the SCSI scheme, so */
467 if (virtio_guessed_disk_nature()) {
468 sclp_print("Using guessed DASD geometry.\n");
469 virtio_assume_eckd();
471 print_eckd_msg();
472 if (magic_match(mbr->magic, IPL1_MAGIC)) {
473 ipl_eckd_cdl(); /* no return */
476 /* LDL/CMS? */
477 memset(sec, FREE_SPACE_FILLER, sizeof(sec));
478 read_block(2, vlbl, "Cannot read block 2");
480 if (magic_match(vlbl->magic, CMS1_MAGIC)) {
481 ipl_eckd_ldl(ECKD_CMS); /* no return */
483 if (magic_match(vlbl->magic, LNX1_MAGIC)) {
484 ipl_eckd_ldl(ECKD_LDL); /* no return */
487 ipl_eckd_ldl(ECKD_LDL_UNLABELED); /* it still may return */
489 * Ok, it is not a LDL by any means.
490 * It still might be a CDL with zero record keys for IPL1 and IPL2
492 ipl_eckd_cdl();
494 virtio_panic("\n* this can never happen *\n");