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