xref: /openbmc/qemu/pc-bios/s390-ccw/bootmap.h (revision 80631527)
1 /*
2  * QEMU S390 bootmap interpreter -- declarations
3  *
4  * Copyright 2014 IBM Corp.
5  * Author(s): Eugene (jno) Dvurechenski <jno@linux.vnet.ibm.com>
6  *
7  * This work is licensed under the terms of the GNU GPL, version 2 or (at
8  * your option) any later version. See the COPYING file in the top-level
9  * directory.
10  */
11 #ifndef _PC_BIOS_S390_CCW_BOOTMAP_H
12 #define _PC_BIOS_S390_CCW_BOOTMAP_H
13 
14 #include "s390-ccw.h"
15 #include "virtio.h"
16 
17 typedef uint64_t block_number_t;
18 #define NULL_BLOCK_NR 0xffffffffffffffffULL
19 #define ERROR_BLOCK_NR 0xfffffffffffffffeULL
20 
21 #define FREE_SPACE_FILLER '\xAA'
22 
23 typedef struct ScsiBlockPtr {
24     uint64_t blockno;
25     uint16_t size;
26     uint16_t blockct;
27     uint8_t reserved[4];
28 } __attribute__ ((packed)) ScsiBlockPtr;
29 
30 typedef struct FbaBlockPtr {
31     uint32_t blockno;
32     uint16_t size;
33     uint16_t blockct;
34 } __attribute__ ((packed)) FbaBlockPtr;
35 
36 typedef struct EckdCHS {
37     uint16_t cylinder;
38     uint16_t head;
39     uint8_t sector;
40 } __attribute__ ((packed)) EckdCHS;
41 
42 typedef struct EckdBlockPtr {
43     EckdCHS chs; /* cylinder/head/sector is an address of the block */
44     uint16_t size;
45     uint8_t count; /* (size_in_blocks-1);
46                     * it's 0 for TablePtr, ScriptPtr, and SectionPtr */
47 } __attribute__ ((packed)) EckdBlockPtr;
48 
49 typedef struct LdEckdCHS {
50     uint32_t cylinder;
51     uint8_t head;
52     uint8_t sector;
53 } __attribute__ ((packed)) LdEckdCHS;
54 
55 typedef struct LdEckdBlockPtr {
56     LdEckdCHS chs; /* cylinder/head/sector is an address of the block */
57     uint8_t reserved[4];
58     uint16_t count;
59     uint32_t pad;
60 } __attribute__ ((packed)) LdEckdBlockPtr;
61 
62 /* bptr is used for CCW type IPL, while ldptr is for list-directed IPL */
63 typedef union ExtEckdBlockPtr {
64     EckdBlockPtr bptr;
65     LdEckdBlockPtr ldptr;
66 } __attribute__ ((packed)) ExtEckdBlockPtr;
67 
68 typedef union BootMapPointer {
69     ScsiBlockPtr scsi;
70     FbaBlockPtr fba;
71     EckdBlockPtr eckd;
72     ExtEckdBlockPtr xeckd;
73 } __attribute__ ((packed)) BootMapPointer;
74 
75 typedef struct BootRecord {
76     uint8_t magic[4];
77     uint32_t version;
78     uint64_t res1;
79     BootMapPointer pgt;
80     uint8_t reserved[510 - 32];
81     uint16_t os_id;
82 } __attribute__ ((packed)) BootRecord;
83 
84 /* aka Program Table */
85 typedef struct BootMapTable {
86     uint8_t magic[4];
87     uint8_t reserved[12];
88     BootMapPointer entry[];
89 } __attribute__ ((packed)) BootMapTable;
90 
91 typedef union ComponentEntryData {
92     uint64_t load_psw;
93     uint64_t load_addr;
94 } ComponentEntryData;
95 
96 typedef struct ComponentEntry {
97     ScsiBlockPtr data;
98     uint8_t pad[7];
99     uint8_t component_type;
100     ComponentEntryData compdat;
101 } __attribute((packed)) ComponentEntry;
102 
103 typedef struct ComponentHeader {
104     uint8_t magic[4];   /* == "zIPL"                    */
105     uint8_t type;       /* == ZIPL_COMP_HEADER_*        */
106     uint8_t reserved[27];
107 } __attribute((packed)) ComponentHeader;
108 
109 typedef struct ScsiMbr {
110     uint8_t magic[4];
111     uint32_t version_id;
112     uint8_t reserved[8];
113     ScsiBlockPtr pt;   /* block pointer to program table */
114 } __attribute__ ((packed)) ScsiMbr;
115 
116 #define ZIPL_MAGIC              "zIPL"
117 #define ZIPL_MAGIC_EBCDIC       "\xa9\xc9\xd7\xd3"
118 #define IPL1_MAGIC "\xc9\xd7\xd3\xf1" /* == "IPL1" in EBCDIC */
119 #define IPL2_MAGIC "\xc9\xd7\xd3\xf2" /* == "IPL2" in EBCDIC */
120 #define VOL1_MAGIC "\xe5\xd6\xd3\xf1" /* == "VOL1" in EBCDIC */
121 #define LNX1_MAGIC "\xd3\xd5\xe7\xf1" /* == "LNX1" in EBCDIC */
122 #define CMS1_MAGIC "\xc3\xd4\xe2\xf1" /* == "CMS1" in EBCDIC */
123 
124 #define LDL1_VERSION '\x40' /* == ' ' in EBCDIC */
125 #define LDL2_VERSION '\xf2' /* == '2' in EBCDIC */
126 
127 #define ZIPL_COMP_HEADER_IPL    0x00
128 #define ZIPL_COMP_HEADER_DUMP   0x01
129 
130 #define ZIPL_COMP_ENTRY_EXEC      0x01
131 #define ZIPL_COMP_ENTRY_LOAD      0x02
132 #define ZIPL_COMP_ENTRY_SIGNATURE 0x03
133 
134 typedef struct XEckdMbr {
135     uint8_t magic[4];   /* == "xIPL"        */
136     uint8_t version;
137     uint8_t bp_type;
138     uint8_t dev_type;   /* == DEV_TYPE_*    */
139 #define DEV_TYPE_ECKD 0x00
140 #define DEV_TYPE_FBA  0x01
141     uint8_t flags;
142     BootMapPointer blockptr;
143     uint8_t reserved[8];
144 } __attribute__ ((packed)) XEckdMbr; /* see also BootInfo */
145 
146 typedef struct BootMapScriptEntry {
147     BootMapPointer blkptr;
148     uint8_t pad[7];
149     uint8_t type;   /* == BOOT_SCRIPT_* */
150 #define BOOT_SCRIPT_EXEC      0x01
151 #define BOOT_SCRIPT_LOAD      0x02
152 #define BOOT_SCRIPT_SIGNATURE 0x03
153     union {
154         uint64_t load_address;
155         uint64_t load_psw;
156     } address;
157 } __attribute__ ((packed)) BootMapScriptEntry;
158 
159 typedef struct BootMapScriptHeader {
160     uint32_t magic;
161     uint8_t type;
162 #define BOOT_SCRIPT_HDR_IPL 0x00
163     uint8_t reserved[27];
164 } __attribute__ ((packed)) BootMapScriptHeader;
165 
166 typedef struct BootMapScript {
167     BootMapScriptHeader header;
168     BootMapScriptEntry  entry[];
169 } __attribute__ ((packed)) BootMapScript;
170 
171 /*
172  * These aren't real VTOCs, but referred to this way in some docs.
173  * They are "volume labels" actually.
174  *
175  * Some structures looks similar to described above, but left
176  * separate as there is no indication that they are the same.
177  * So, the value definitions are left separate too.
178  */
179 typedef struct LDL_VTOC {       /* @ rec.3 cyl.0 trk.0 for ECKD */
180     char magic[4];              /* "LNX1", EBCDIC               */
181     char volser[6];             /* volser, EBCDIC               */
182     uint8_t reserved[69];       /* reserved, 0x40               */
183     uint8_t LDL_version;        /* 0x40 or 0xF2                 */
184     uint64_t formatted_blocks;  /* if LDL_version >= 0xF2       */
185 } __attribute__ ((packed)) LDL_VTOC;
186 
187 typedef struct format_date {
188     uint8_t YY;
189     uint8_t MM;
190     uint8_t DD;
191     uint8_t hh;
192     uint8_t mm;
193     uint8_t ss;
194 } __attribute__ ((packed)) format_date_t;
195 
196 typedef struct CMS_VTOC {       /* @ rec.3 cyl.0 trk.0 for ECKD */
197                                 /* @ blk.1 (zero based) for FBA */
198     char magic[4];              /* 'CMS1', EBCDIC               */
199     char volser[6];             /* volser, EBCDIC               */
200     uint16_t version;           /* = 0                          */
201     uint32_t block_size;        /* = 512, 1024, 2048, or 4096   */
202     uint32_t disk_origin;       /* = 4 or 5                     */
203     uint32_t blocks;            /* Number of usable cyls/blocks */
204     uint32_t formatted;         /* Max number of fmtd cyls/blks */
205     uint32_t CMS_blocks;        /* disk size in CMS blocks      */
206     uint32_t CMS_used;          /* Number of CMS blocks in use  */
207     uint32_t FST_size;          /* = 64, bytes                  */
208     uint32_t FST_per_CMS_blk;   /*                              */
209     format_date_t format_date;  /* YYMMDDhhmmss as 6 bytes      */
210     uint8_t reserved1[2];       /* = 0                          */
211     uint32_t offset;            /* disk offset when reserved    */
212     uint32_t next_hole;         /* block nr                     */
213     uint32_t HBLK_hole_offset;  /* >> HBLK data of next hole    */
214     uint32_t alloc_map_usr_off; /* >> user part of Alloc map    */
215     uint8_t reserved2[4];       /* = 0                          */
216     char shared_seg_name[8];    /*                              */
217 } __attribute__ ((packed)) CMS_VTOC;
218 
219 /* from zipl/include/boot.h */
220 typedef struct BootInfoBpIpl {
221     union {
222         ExtEckdBlockPtr eckd;
223         ScsiBlockPtr linr;
224     } bm_ptr;
225     uint8_t unused[16];
226 } __attribute__ ((packed)) BootInfoBpIpl;
227 
228 typedef struct EckdDumpParam {
229     uint32_t        start_blk;
230     uint32_t        end_blk;
231     uint16_t        blocksize;
232     uint8_t         num_heads;
233     uint8_t         bpt;
234     char            reserved[4];
235 } __attribute((packed, may_alias)) EckdDumpParam;
236 
237 typedef struct FbaDumpParam {
238     uint64_t        start_blk;
239     uint64_t        blockct;
240 } __attribute((packed)) FbaDumpParam;
241 
242 typedef struct BootInfoBpDump {
243     union {
244         EckdDumpParam eckd;
245         FbaDumpParam fba;
246     } param;
247     uint8_t         unused[16];
248 } __attribute__ ((packed)) BootInfoBpDump;
249 
250 typedef struct BootInfo {          /* @ 0x70, record #0    */
251     unsigned char magic[4]; /* = 'zIPL', ASCII      */
252     uint8_t version;        /* = 1                  */
253 #define BOOT_INFO_VERSION               1
254     uint8_t bp_type;        /* = 0                  */
255 #define BOOT_INFO_BP_TYPE_IPL           0x00
256 #define BOOT_INFO_BP_TYPE_DUMP          0x01
257     uint8_t dev_type;       /* = 0                  */
258 #define BOOT_INFO_DEV_TYPE_ECKD         0x00
259 #define BOOT_INFO_DEV_TYPE_FBA          0x01
260     uint8_t flags;          /* = 1                  */
261 #ifdef __s390x__
262 #define BOOT_INFO_FLAGS_ARCH            0x01
263 #else
264 #define BOOT_INFO_FLAGS_ARCH            0x00
265 #endif
266     union {
267         BootInfoBpDump dump;
268         BootInfoBpIpl ipl;
269     } bp;
270 } __attribute__ ((packed)) BootInfo; /* see also XEckdMbr   */
271 
272 /*
273  * Structs for IPL
274  */
275 #define STAGE2_BLK_CNT_MAX  24 /* Stage 1b can load up to 24 blocks */
276 
277 typedef struct EckdCdlIpl1 {
278     uint8_t key[4]; /* == "IPL1" */
279     uint8_t data[24];
280 } __attribute__((packed)) EckdCdlIpl1;
281 
282 typedef struct EckdSeekArg {
283     uint16_t pad;
284     EckdCHS chs;
285     uint8_t pad2;
286 } __attribute__ ((packed)) EckdSeekArg;
287 
288 typedef struct EckdStage1b {
289     uint8_t reserved[32 * STAGE2_BLK_CNT_MAX];
290     struct EckdSeekArg seek[STAGE2_BLK_CNT_MAX];
291     uint8_t unused[64];
292 } __attribute__ ((packed)) EckdStage1b;
293 
294 typedef struct EckdStage1 {
295     uint8_t reserved[72];
296     struct EckdSeekArg seek[2];
297 } __attribute__ ((packed)) EckdStage1;
298 
299 typedef struct EckdCdlIpl2 {
300     uint8_t key[4]; /* == "IPL2" */
301     struct EckdStage1 stage1;
302     XEckdMbr mbr;
303     uint8_t reserved[24];
304 } __attribute__((packed)) EckdCdlIpl2;
305 
306 typedef struct EckdLdlIpl1 {
307     uint8_t reserved[24];
308     struct EckdStage1 stage1;
309     BootInfo bip; /* BootInfo is MBR for LDL */
310 } __attribute__((packed)) EckdLdlIpl1;
311 
312 typedef struct IplVolumeLabel {
313     unsigned char key[4]; /* == "VOL1" */
314     union {
315         unsigned char data[80];
316         struct {
317             unsigned char key[4]; /* == "VOL1" */
318             unsigned char volser[6];
319             unsigned char reserved[64];
320             EckdCHS br; /* Location of Boot Record for list-directed IPL */
321         } f;
322     };
323 } __attribute__((packed)) IplVolumeLabel;
324 
325 typedef enum {
326     ECKD_NO_IPL,
327     ECKD_CMS,
328     ECKD_LDL,
329     ECKD_LDL_UNLABELED,
330 } ECKD_IPL_mode_t;
331 
332 /* utility code below */
333 
print_volser(const void * volser)334 static inline void print_volser(const void *volser)
335 {
336     char ascii[8];
337 
338     ebcdic_to_ascii((char *)volser, ascii, 6);
339     ascii[6] = '\0';
340     printf("VOLSER=[%s]\n", ascii);
341 }
342 
unused_space(const void * p,size_t size)343 static inline bool unused_space(const void *p, size_t size)
344 {
345     size_t i;
346     const unsigned char *m = p;
347 
348     for (i = 0; i < size; i++) {
349         if (m[i] != FREE_SPACE_FILLER) {
350             return false;
351         }
352     }
353     return true;
354 }
355 
is_null_block_number(block_number_t x)356 static inline bool is_null_block_number(block_number_t x)
357 {
358     return x == NULL_BLOCK_NR;
359 }
360 
read_block(block_number_t blockno,void * buffer,const char * errmsg)361 static inline void read_block(block_number_t blockno,
362                               void *buffer,
363                               const char *errmsg)
364 {
365     IPL_assert(virtio_read(blockno, buffer) == 0, errmsg);
366 }
367 
block_size_ok(uint32_t block_size)368 static inline bool block_size_ok(uint32_t block_size)
369 {
370     return block_size == virtio_get_block_size();
371 }
372 
magic_match(const void * data,const void * magic)373 static inline bool magic_match(const void *data, const void *magic)
374 {
375     return *((uint32_t *)data) == *((uint32_t *)magic);
376 }
377 
iso_733_to_u32(uint64_t x)378 static inline uint32_t iso_733_to_u32(uint64_t x)
379 {
380     return (uint32_t)x;
381 }
382 
383 #define ISO_SECTOR_SIZE 2048
384 /* El Torito specifies boot image size in 512 byte blocks */
385 #define ET_SECTOR_SHIFT 2
386 
387 #define ISO_PRIMARY_VD_SECTOR 16
388 
read_iso_boot_image(uint32_t block_offset,void * load_addr,uint32_t blks_to_load)389 static inline int read_iso_boot_image(uint32_t block_offset, void *load_addr,
390                                        uint32_t blks_to_load)
391 {
392     if (virtio_read_many(block_offset, load_addr, blks_to_load)) {
393         puts("Failed to read boot image!");
394         return -1;
395     }
396     return 0;
397 }
398 
399 #define ISO9660_MAX_DIR_DEPTH 8
400 
401 typedef struct IsoDirHdr {
402     uint8_t dr_len;
403     uint8_t ear_len;
404     uint64_t ext_loc;
405     uint64_t data_len;
406     uint8_t recording_datetime[7];
407     uint8_t file_flags;
408     uint8_t file_unit_size;
409     uint8_t gap_size;
410     uint32_t vol_seqnum;
411     uint8_t fileid_len;
412 } __attribute__((packed)) IsoDirHdr;
413 
414 typedef struct IsoVdElTorito {
415     uint8_t el_torito[32]; /* must contain el_torito_magic value */
416     uint8_t unused0[32];
417     uint32_t bc_offset;
418     uint8_t unused1[1974];
419 } __attribute__((packed)) IsoVdElTorito;
420 
421 typedef struct IsoVdPrimary {
422     uint8_t unused1;
423     uint8_t sys_id[32];
424     uint8_t vol_id[32];
425     uint8_t unused2[8];
426     uint64_t vol_space_size;
427     uint8_t unused3[32];
428     uint32_t vol_set_size;
429     uint32_t vol_seqnum;
430     uint32_t log_block_size;
431     uint64_t path_table_size;
432     uint32_t l_path_table;
433     uint32_t opt_l_path_table;
434     uint32_t m_path_table;
435     uint32_t opt_m_path_table;
436     IsoDirHdr rootdir;
437     uint8_t root_null;
438     uint8_t reserved2[1858];
439 } __attribute__((packed)) IsoVdPrimary;
440 
441 typedef struct IsoVolDesc {
442     uint8_t type;
443     uint8_t ident[5];
444     uint8_t version;
445     union {
446         IsoVdElTorito boot;
447         IsoVdPrimary primary;
448     } vd;
449 } __attribute__((packed)) IsoVolDesc;
450 
451 #define VOL_DESC_TYPE_BOOT 0
452 #define VOL_DESC_TYPE_PRIMARY 1
453 #define VOL_DESC_TYPE_SUPPLEMENT 2
454 #define VOL_DESC_TYPE_PARTITION 3
455 #define VOL_DESC_TERMINATOR 255
456 
457 typedef struct IsoBcValid {
458     uint8_t platform_id;
459     uint16_t reserved;
460     uint8_t id[24];
461     uint16_t checksum;
462     uint8_t key[2];
463 } __attribute__((packed)) IsoBcValid;
464 
465 typedef struct IsoBcSection {
466     uint8_t boot_type;
467     uint16_t load_segment;
468     uint8_t sys_type;
469     uint8_t unused;
470     uint16_t sector_count;
471     uint32_t load_rba;
472     uint8_t selection[20];
473 } __attribute__((packed)) IsoBcSection;
474 
475 typedef struct IsoBcHdr {
476     uint8_t platform_id;
477     uint16_t sect_num;
478     uint8_t id[28];
479 } __attribute__((packed)) IsoBcHdr;
480 
481 typedef struct IsoBcEntry {
482     uint8_t id;
483     union {
484         IsoBcValid valid; /* id == 0x01 */
485         IsoBcSection sect; /* id == 0x88 || id == 0x0 */
486         IsoBcHdr hdr; /* id == 0x90 || id == 0x91 */
487     } body;
488 } __attribute__((packed)) IsoBcEntry;
489 
490 #define ISO_BC_ENTRY_PER_SECTOR (ISO_SECTOR_SIZE / sizeof(IsoBcEntry))
491 #define ISO_BC_HDR_VALIDATION 0x01
492 #define ISO_BC_BOOTABLE_SECTION 0x88
493 #define ISO_BC_MAGIC_55 0x55
494 #define ISO_BC_MAGIC_AA 0xaa
495 #define ISO_BC_PLATFORM_X86 0x0
496 #define ISO_BC_PLATFORM_PPC 0x1
497 #define ISO_BC_PLATFORM_MAC 0x2
498 
is_iso_bc_valid(IsoBcEntry * e)499 static inline bool is_iso_bc_valid(IsoBcEntry *e)
500 {
501     IsoBcValid *v = &e->body.valid;
502 
503     if (e->id != ISO_BC_HDR_VALIDATION) {
504         return false;
505     }
506 
507     if (v->platform_id != ISO_BC_PLATFORM_X86 &&
508         v->platform_id != ISO_BC_PLATFORM_PPC &&
509         v->platform_id != ISO_BC_PLATFORM_MAC) {
510         return false;
511     }
512 
513     return v->key[0] == ISO_BC_MAGIC_55 &&
514            v->key[1] == ISO_BC_MAGIC_AA &&
515            v->reserved == 0x0;
516 }
517 
518 #endif /* _PC_BIOS_S390_CCW_BOOTMAP_H */
519