xref: /qemu/pc-bios/s390-ccw/bootmap.c (revision d072cdf3)
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 "s390-ccw.h"
12 #include "bootmap.h"
13 #include "virtio.h"
14 
15 #ifdef DEBUG
16 /* #define DEBUG_FALLBACK */
17 #endif
18 
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
26 
27 /* Scratch space */
28 static uint8_t sec[MAX_SECTOR_SIZE*4] __attribute__((__aligned__(PAGE_SIZE)));
29 
30 typedef struct ResetInfo {
31     uint32_t ipl_mask;
32     uint32_t ipl_addr;
33     uint32_t ipl_continue;
34 } ResetInfo;
35 
36 ResetInfo save;
37 
38 static void jump_to_IPL_2(void)
39 {
40     ResetInfo *current = 0;
41 
42     void (*ipl)(void) = (void *) (uint64_t) current->ipl_continue;
43     debug_print_addr("set IPL addr to", ipl);
44 
45     /* Ensure the guest output starts fresh */
46     sclp_print("\n");
47 
48     *current = save;
49     ipl(); /* should not return */
50 }
51 
52 static void jump_to_IPL_code(uint64_t address)
53 {
54     /* store the subsystem information _after_ the bootmap was loaded */
55     write_subsystem_identification();
56     /*
57      * The IPL PSW is at address 0. We also must not overwrite the
58      * content of non-BIOS memory after we loaded the guest, so we
59      * save the original content and restore it in jump_to_IPL_2.
60      */
61     ResetInfo *current = 0;
62 
63     save = *current;
64     current->ipl_addr = (uint32_t) (uint64_t) &jump_to_IPL_2;
65     current->ipl_continue = address & 0x7fffffff;
66 
67     /*
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.
71      */
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");
76 }
77 
78 /***********************************************************************
79  * IPL an ECKD DASD (CDL or LDL/CMS format)
80  */
81 
82 static unsigned char _bprs[8*1024]; /* guessed "max" ECKD sector size */
83 const int max_bprs_entries = sizeof(_bprs) / sizeof(ExtEckdBlockPtr);
84 
85 static inline void verify_boot_info(BootInfo *bip)
86 {
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.");
94 }
95 
96 static bool eckd_valid_address(BootMapPointer *p)
97 {
98     const uint64_t cylinder = p->eckd.cylinder
99                             + ((p->eckd.head & 0xfff0) << 12);
100     const uint64_t head = p->eckd.head & 0x000f;
101 
102     if (head >= virtio_get_heads()
103         ||  p->eckd.sector > virtio_get_sectors()
104         ||  p->eckd.sector <= 0) {
105         return false;
106     }
107 
108     if (!virtio_guessed_disk_nature() && cylinder >= virtio_get_cylinders()) {
109         return false;
110     }
111 
112     return true;
113 }
114 
115 static block_number_t eckd_block_num(BootMapPointer *p)
116 {
117     const uint64_t sectors = virtio_get_sectors();
118     const uint64_t heads = virtio_get_heads();
119     const uint64_t cylinder = p->eckd.cylinder
120                             + ((p->eckd.head & 0xfff0) << 12);
121     const uint64_t head = p->eckd.head & 0x000f;
122     const block_number_t block = sectors * heads * cylinder
123                                + sectors * head
124                                + p->eckd.sector
125                                - 1; /* block nr starts with zero */
126     return block;
127 }
128 
129 static block_number_t load_eckd_segments(block_number_t blk, uint64_t *address)
130 {
131     block_number_t block_nr;
132     int j, rc;
133     BootMapPointer *bprs = (void *)_bprs;
134     bool more_data;
135 
136     memset(_bprs, FREE_SPACE_FILLER, sizeof(_bprs));
137     read_block(blk, bprs, "BPRS read failed");
138 
139     do {
140         more_data = false;
141         for (j = 0;; j++) {
142             block_nr = eckd_block_num((void *)&(bprs[j].xeckd));
143             if (is_null_block_number(block_nr)) { /* end of chunk */
144                 break;
145             }
146 
147             /* we need the updated blockno for the next indirect entry
148              * in the chain, but don't want to advance address
149              */
150             if (j == (max_bprs_entries - 1)) {
151                 break;
152             }
153 
154             IPL_assert(block_size_ok(bprs[j].xeckd.bptr.size),
155                        "bad chunk block size");
156             IPL_assert(eckd_valid_address(&bprs[j]), "bad chunk ECKD addr");
157 
158             if ((bprs[j].xeckd.bptr.count == 0) && unused_space(&(bprs[j+1]),
159                 sizeof(EckdBlockPtr))) {
160                 /* This is a "continue" pointer.
161                  * This ptr should be the last one in the current
162                  * script section.
163                  * I.e. the next ptr must point to the unused memory area
164                  */
165                 memset(_bprs, FREE_SPACE_FILLER, sizeof(_bprs));
166                 read_block(block_nr, bprs, "BPRS continuation read failed");
167                 more_data = true;
168                 break;
169             }
170 
171             /* Load (count+1) blocks of code at (block_nr)
172              * to memory (address).
173              */
174             rc = virtio_read_many(block_nr, (void *)(*address),
175                                   bprs[j].xeckd.bptr.count+1);
176             IPL_assert(rc == 0, "code chunk read failed");
177 
178             *address += (bprs[j].xeckd.bptr.count+1) * virtio_get_block_size();
179         }
180     } while (more_data);
181     return block_nr;
182 }
183 
184 static void run_eckd_boot_script(block_number_t mbr_block_nr)
185 {
186     int i;
187     block_number_t block_nr;
188     uint64_t address;
189     ScsiMbr *scsi_mbr = (void *)sec;
190     BootMapScript *bms = (void *)sec;
191 
192     memset(sec, FREE_SPACE_FILLER, sizeof(sec));
193     read_block(mbr_block_nr, sec, "Cannot read MBR");
194 
195     block_nr = eckd_block_num((void *)&(scsi_mbr->blockptr));
196 
197     memset(sec, FREE_SPACE_FILLER, sizeof(sec));
198     read_block(block_nr, sec, "Cannot read Boot Map Script");
199 
200     for (i = 0; bms->entry[i].type == BOOT_SCRIPT_LOAD; i++) {
201         address = bms->entry[i].address.load_address;
202         block_nr = eckd_block_num(&(bms->entry[i].blkptr));
203 
204         do {
205             block_nr = load_eckd_segments(block_nr, &address);
206         } while (block_nr != -1);
207     }
208 
209     IPL_assert(bms->entry[i].type == BOOT_SCRIPT_EXEC,
210                "Unknown script entry type");
211     jump_to_IPL_code(bms->entry[i].address.load_address); /* no return */
212 }
213 
214 static void ipl_eckd_cdl(void)
215 {
216     XEckdMbr *mbr;
217     Ipl2 *ipl2 = (void *)sec;
218     IplVolumeLabel *vlbl = (void *)sec;
219     block_number_t block_nr;
220 
221     /* we have just read the block #0 and recognized it as "IPL1" */
222     sclp_print("CDL\n");
223 
224     memset(sec, FREE_SPACE_FILLER, sizeof(sec));
225     read_block(1, ipl2, "Cannot read IPL2 record at block 1");
226     IPL_assert(magic_match(ipl2, IPL2_MAGIC), "No IPL2 record");
227 
228     mbr = &ipl2->u.x.mbr;
229     IPL_assert(magic_match(mbr, ZIPL_MAGIC), "No zIPL section in IPL2 record.");
230     IPL_assert(block_size_ok(mbr->blockptr.xeckd.bptr.size),
231                "Bad block size in zIPL section of IPL2 record.");
232     IPL_assert(mbr->dev_type == DEV_TYPE_ECKD,
233                "Non-ECKD device type in zIPL section of IPL2 record.");
234 
235     /* save pointer to Boot Script */
236     block_nr = eckd_block_num((void *)&(mbr->blockptr));
237 
238     memset(sec, FREE_SPACE_FILLER, sizeof(sec));
239     read_block(2, vlbl, "Cannot read Volume Label at block 2");
240     IPL_assert(magic_match(vlbl->key, VOL1_MAGIC),
241                "Invalid magic of volume label block");
242     IPL_assert(magic_match(vlbl->f.key, VOL1_MAGIC),
243                "Invalid magic of volser block");
244     print_volser(vlbl->f.volser);
245 
246     run_eckd_boot_script(block_nr);
247     /* no return */
248 }
249 
250 static void ipl_eckd_ldl(ECKD_IPL_mode_t mode)
251 {
252     LDL_VTOC *vlbl = (void *)sec; /* already read, 3rd block */
253     char msg[4] = { '?', '.', '\n', '\0' };
254     block_number_t block_nr;
255     BootInfo *bip;
256 
257     sclp_print((mode == ECKD_CMS) ? "CMS" : "LDL");
258     sclp_print(" version ");
259     switch (vlbl->LDL_version) {
260     case LDL1_VERSION:
261         msg[0] = '1';
262         break;
263     case LDL2_VERSION:
264         msg[0] = '2';
265         break;
266     default:
267         msg[0] = vlbl->LDL_version;
268         msg[0] &= 0x0f; /* convert EBCDIC   */
269         msg[0] |= 0x30; /* to ASCII (digit) */
270         msg[1] = '?';
271         break;
272     }
273     sclp_print(msg);
274     print_volser(vlbl->volser);
275 
276     /* DO NOT read BootMap pointer (only one, xECKD) at block #2 */
277 
278     memset(sec, FREE_SPACE_FILLER, sizeof(sec));
279     read_block(0, sec, "Cannot read block 0");
280     bip = (void *)(sec + 0x70); /* "boot info" is "eckd mbr" for LDL */
281     verify_boot_info(bip);
282 
283     block_nr = eckd_block_num((void *)&(bip->bp.ipl.bm_ptr.eckd.bptr));
284     run_eckd_boot_script(block_nr);
285     /* no return */
286 }
287 
288 static void ipl_eckd(ECKD_IPL_mode_t mode)
289 {
290     switch (mode) {
291     case ECKD_CDL:
292         ipl_eckd_cdl(); /* no return */
293     case ECKD_CMS:
294     case ECKD_LDL:
295         ipl_eckd_ldl(mode); /* no return */
296     default:
297         virtio_panic("\n! Unknown ECKD IPL mode !\n");
298     }
299 }
300 
301 /***********************************************************************
302  * IPL a SCSI disk
303  */
304 
305 static void zipl_load_segment(ComponentEntry *entry)
306 {
307     const int max_entries = (MAX_SECTOR_SIZE / sizeof(ScsiBlockPtr));
308     ScsiBlockPtr *bprs = (void *)sec;
309     const int bprs_size = sizeof(sec);
310     block_number_t blockno;
311     uint64_t address;
312     int i;
313     char err_msg[] = "zIPL failed to read BPRS at 0xZZZZZZZZZZZZZZZZ";
314     char *blk_no = &err_msg[30]; /* where to print blockno in (those ZZs) */
315 
316     blockno = entry->data.blockno;
317     address = entry->load_address;
318 
319     debug_print_int("loading segment at block", blockno);
320     debug_print_int("addr", address);
321 
322     do {
323         memset(bprs, FREE_SPACE_FILLER, bprs_size);
324         fill_hex_val(blk_no, &blockno, sizeof(blockno));
325         read_block(blockno, bprs, err_msg);
326 
327         for (i = 0;; i++) {
328             uint64_t *cur_desc = (void *)&bprs[i];
329 
330             blockno = bprs[i].blockno;
331             if (!blockno) {
332                 break;
333             }
334 
335             /* we need the updated blockno for the next indirect entry in the
336                chain, but don't want to advance address */
337             if (i == (max_entries - 1)) {
338                 break;
339             }
340 
341             if (bprs[i].blockct == 0 && unused_space(&bprs[i + 1],
342                 sizeof(ScsiBlockPtr))) {
343                 /* This is a "continue" pointer.
344                  * This ptr is the last one in the current script section.
345                  * I.e. the next ptr must point to the unused memory area.
346                  * The blockno is not zero, so the upper loop must continue
347                  * reading next section of BPRS.
348                  */
349                 break;
350             }
351             address = virtio_load_direct(cur_desc[0], cur_desc[1], 0,
352                                          (void *)address);
353             IPL_assert(address != -1, "zIPL load segment failed");
354         }
355     } while (blockno);
356 }
357 
358 /* Run a zipl program */
359 static void zipl_run(ScsiBlockPtr *pte)
360 {
361     ComponentHeader *header;
362     ComponentEntry *entry;
363     uint8_t tmp_sec[MAX_SECTOR_SIZE];
364 
365     read_block(pte->blockno, tmp_sec, "Cannot read header");
366     header = (ComponentHeader *)tmp_sec;
367 
368     IPL_assert(magic_match(tmp_sec, ZIPL_MAGIC), "No zIPL magic");
369     IPL_assert(header->type == ZIPL_COMP_HEADER_IPL, "Bad header type");
370 
371     dputs("start loading images\n");
372 
373     /* Load image(s) into RAM */
374     entry = (ComponentEntry *)(&header[1]);
375     while (entry->component_type == ZIPL_COMP_ENTRY_LOAD) {
376         zipl_load_segment(entry);
377 
378         entry++;
379 
380         IPL_assert((uint8_t *)(&entry[1]) <= (tmp_sec + MAX_SECTOR_SIZE),
381                    "Wrong entry value");
382     }
383 
384     IPL_assert(entry->component_type == ZIPL_COMP_ENTRY_EXEC, "No EXEC entry");
385 
386     /* should not return */
387     jump_to_IPL_code(entry->load_address);
388 }
389 
390 static void ipl_scsi(void)
391 {
392     ScsiMbr *mbr = (void *)sec;
393     uint8_t *ns, *ns_end;
394     int program_table_entries = 0;
395     const int pte_len = sizeof(ScsiBlockPtr);
396     ScsiBlockPtr *prog_table_entry;
397 
398     /* The 0-th block (MBR) was already read into sec[] */
399 
400     sclp_print("Using SCSI scheme.\n");
401     debug_print_int("program table", mbr->blockptr.blockno);
402 
403     /* Parse the program table */
404     read_block(mbr->blockptr.blockno, sec,
405                "Error reading Program Table");
406 
407     IPL_assert(magic_match(sec, ZIPL_MAGIC), "No zIPL magic");
408 
409     ns_end = sec + virtio_get_block_size();
410     for (ns = (sec + pte_len); (ns + pte_len) < ns_end; ns++) {
411         prog_table_entry = (ScsiBlockPtr *)ns;
412         if (!prog_table_entry->blockno) {
413             break;
414         }
415 
416         program_table_entries++;
417     }
418 
419     debug_print_int("program table entries", program_table_entries);
420 
421     IPL_assert(program_table_entries != 0, "Empty Program Table");
422 
423     /* Run the default entry */
424 
425     prog_table_entry = (ScsiBlockPtr *)(sec + pte_len);
426 
427     zipl_run(prog_table_entry); /* no return */
428 }
429 
430 /***********************************************************************
431  * IPL starts here
432  */
433 
434 void zipl_load(void)
435 {
436     ScsiMbr *mbr = (void *)sec;
437     LDL_VTOC *vlbl = (void *)sec;
438 
439     /* Grab the MBR */
440     memset(sec, FREE_SPACE_FILLER, sizeof(sec));
441     read_block(0, mbr, "Cannot read block 0");
442 
443     dputs("checking magic\n");
444 
445     if (magic_match(mbr->magic, ZIPL_MAGIC)) {
446         ipl_scsi(); /* no return */
447     }
448 
449     /* We have failed to follow the SCSI scheme, so */
450     sclp_print("Using ECKD scheme.\n");
451     if (virtio_guessed_disk_nature()) {
452         sclp_print("Using guessed DASD geometry.\n");
453         virtio_assume_eckd();
454     }
455 
456     if (magic_match(mbr->magic, IPL1_MAGIC)) {
457         ipl_eckd(ECKD_CDL); /* no return */
458     }
459 
460     /* LDL/CMS? */
461     memset(sec, FREE_SPACE_FILLER, sizeof(sec));
462     read_block(2, vlbl, "Cannot read block 2");
463 
464     if (magic_match(vlbl->magic, CMS1_MAGIC)) {
465         ipl_eckd(ECKD_CMS); /* no return */
466     }
467     if (magic_match(vlbl->magic, LNX1_MAGIC)) {
468         ipl_eckd(ECKD_LDL); /* no return */
469     }
470 
471     virtio_panic("\n* invalid MBR magic *\n");
472 }
473