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