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