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