1
2
3
4
5
6
7
8
9
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
17typedef uint64_t block_number_t;
18#define NULL_BLOCK_NR 0xffffffffffffffffULL
19
20#define FREE_SPACE_FILLER '\xAA'
21
22typedef struct ScsiBlockPtr {
23 uint64_t blockno;
24 uint16_t size;
25 uint16_t blockct;
26 uint8_t reserved[4];
27} __attribute__ ((packed)) ScsiBlockPtr;
28
29typedef struct FbaBlockPtr {
30 uint32_t blockno;
31 uint16_t size;
32 uint16_t blockct;
33} __attribute__ ((packed)) FbaBlockPtr;
34
35typedef struct EckdBlockPtr {
36 uint16_t cylinder;
37 uint16_t head;
38 uint8_t sector;
39 uint16_t size;
40 uint8_t count;
41
42} __attribute__ ((packed)) EckdBlockPtr;
43
44typedef struct ExtEckdBlockPtr {
45 EckdBlockPtr bptr;
46 uint8_t reserved[8];
47} __attribute__ ((packed)) ExtEckdBlockPtr;
48
49typedef union BootMapPointer {
50 ScsiBlockPtr scsi;
51 FbaBlockPtr fba;
52 EckdBlockPtr eckd;
53 ExtEckdBlockPtr xeckd;
54} __attribute__ ((packed)) BootMapPointer;
55
56typedef 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
63typedef struct ComponentHeader {
64 uint8_t magic[4];
65 uint8_t type;
66 uint8_t reserved[27];
67} __attribute((packed)) ComponentHeader;
68
69typedef 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"
78#define IPL2_MAGIC "\xc9\xd7\xd3\xf2"
79#define VOL1_MAGIC "\xe5\xd6\xd3\xf1"
80#define LNX1_MAGIC "\xd3\xd5\xe7\xf1"
81#define CMS1_MAGIC "\xc3\xd4\xe2\xf1"
82
83#define LDL1_VERSION '\x40'
84#define LDL2_VERSION '\xf2'
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
92typedef struct XEckdMbr {
93 uint8_t magic[4];
94 uint8_t version;
95 uint8_t bp_type;
96 uint8_t 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;
103
104typedef struct BootMapScriptEntry {
105 BootMapPointer blkptr;
106 uint8_t pad[7];
107 uint8_t type;
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
116typedef 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
123typedef struct BootMapScript {
124 BootMapScriptHeader header;
125 BootMapScriptEntry entry[0];
126} __attribute__ ((packed)) BootMapScript;
127
128
129
130
131
132
133
134
135
136typedef struct LDL_VTOC {
137 char magic[4];
138 char volser[6];
139 uint8_t reserved[69];
140 uint8_t LDL_version;
141 uint64_t formatted_blocks;
142} __attribute__ ((packed)) LDL_VTOC;
143
144typedef 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
153typedef struct CMS_VTOC {
154
155 char magic[4];
156 char volser[6];
157 uint16_t version;
158 uint32_t block_size;
159 uint32_t disk_origin;
160 uint32_t blocks;
161 uint32_t formatted;
162 uint32_t CMS_blocks;
163 uint32_t CMS_used;
164 uint32_t FST_size;
165 uint32_t FST_per_CMS_blk;
166 format_date_t format_date;
167 uint8_t reserved1[2];
168 uint32_t offset;
169 uint32_t next_hole;
170 uint32_t HBLK_hole_offset;
171 uint32_t alloc_map_usr_off;
172 uint8_t reserved2[4];
173 char shared_seg_name[8];
174} __attribute__ ((packed)) CMS_VTOC;
175
176
177typedef struct BootInfoBpIpl {
178 union {
179 ExtEckdBlockPtr eckd;
180 ScsiBlockPtr linr;
181 } bm_ptr;
182 uint8_t unused[16];
183} __attribute__ ((packed)) BootInfoBpIpl;
184
185typedef 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
194typedef struct FbaDumpParam {
195 uint64_t start_blk;
196 uint64_t blockct;
197} __attribute((packed)) FbaDumpParam;
198
199typedef struct BootInfoBpDump {
200 union {
201 EckdDumpParam eckd;
202 FbaDumpParam fba;
203 } param;
204 uint8_t unused[16];
205} __attribute__ ((packed)) BootInfoBpDump;
206
207typedef struct BootInfo {
208 unsigned char magic[4];
209 uint8_t version;
210#define BOOT_INFO_VERSION 1
211 uint8_t bp_type;
212#define BOOT_INFO_BP_TYPE_IPL 0x00
213#define BOOT_INFO_BP_TYPE_DUMP 0x01
214 uint8_t dev_type;
215#define BOOT_INFO_DEV_TYPE_ECKD 0x00
216#define BOOT_INFO_DEV_TYPE_FBA 0x01
217 uint8_t flags;
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;
228
229typedef struct Ipl1 {
230 unsigned char key[4];
231 unsigned char data[24];
232} __attribute__((packed)) Ipl1;
233
234typedef struct Ipl2 {
235 unsigned char key[4];
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
246typedef struct IplVolumeLabel {
247 unsigned char key[4];
248 union {
249 unsigned char data[80];
250 struct {
251 unsigned char key[4];
252 unsigned char volser[6];
253 unsigned char reserved[6];
254 } f;
255 };
256} __attribute__((packed)) IplVolumeLabel;
257
258typedef enum {
259 ECKD_NO_IPL,
260 ECKD_CMS,
261 ECKD_LDL,
262 ECKD_LDL_UNLABELED,
263} ECKD_IPL_mode_t;
264
265
266
267static inline void IPL_assert(bool term, const char *message)
268{
269 if (!term) {
270 sclp_print("\n! ");
271 sclp_print(message);
272 virtio_panic(" !\n");
273 }
274}
275
276static const unsigned char ebc2asc[256] =
277
278 "................................"
279 "................................"
280 " ...........<(+|&.........!$*);."
281 "-/.........,%_>?.........`:#@'=\""
282 ".abcdefghi.......jklmnopqr......"
283 "..stuvwxyz......................"
284 ".ABCDEFGHI.......JKLMNOPQR......"
285 "..STUVWXYZ......0123456789......";
286
287static inline void ebcdic_to_ascii(const char *src,
288 char *dst,
289 unsigned int size)
290{
291 unsigned int i;
292 for (i = 0; i < size; i++) {
293 unsigned c = src[i];
294 dst[i] = ebc2asc[c];
295 }
296}
297
298static inline void print_volser(const void *volser)
299{
300 char ascii[8];
301
302 ebcdic_to_ascii((char *)volser, ascii, 6);
303 ascii[6] = '\0';
304 sclp_print("VOLSER=[");
305 sclp_print(ascii);
306 sclp_print("]\n");
307}
308
309static inline bool unused_space(const void *p, size_t size)
310{
311 size_t i;
312 const unsigned char *m = p;
313
314 for (i = 0; i < size; i++) {
315 if (m[i] != FREE_SPACE_FILLER) {
316 return false;
317 }
318 }
319 return true;
320}
321
322static inline bool is_null_block_number(block_number_t x)
323{
324 return x == NULL_BLOCK_NR;
325}
326
327static inline void read_block(block_number_t blockno,
328 void *buffer,
329 const char *errmsg)
330{
331 IPL_assert(virtio_read(blockno, buffer) == 0, errmsg);
332}
333
334static inline bool block_size_ok(uint32_t block_size)
335{
336 return block_size == virtio_get_block_size();
337}
338
339static inline bool magic_match(const void *data, const void *magic)
340{
341 return *((uint32_t *)data) == *((uint32_t *)magic);
342}
343
344static inline int _memcmp(const void *s1, const void *s2, size_t n)
345{
346 int i;
347 const uint8_t *p1 = s1, *p2 = s2;
348
349 for (i = 0; i < n; i++) {
350 if (p1[i] != p2[i]) {
351 return p1[i] > p2[i] ? 1 : -1;
352 }
353 }
354
355 return 0;
356}
357
358
359
360
361static inline uint16_t bswap16(uint16_t x)
362{
363 return ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8);
364}
365
366static inline uint32_t bswap32(uint32_t x)
367{
368 return ((x & 0x000000ffU) << 24) | ((x & 0x0000ff00U) << 8) |
369 ((x & 0x00ff0000U) >> 8) | ((x & 0xff000000U) >> 24);
370}
371
372static inline uint64_t bswap64(uint64_t x)
373{
374 return ((x & 0x00000000000000ffULL) << 56) |
375 ((x & 0x000000000000ff00ULL) << 40) |
376 ((x & 0x0000000000ff0000ULL) << 24) |
377 ((x & 0x00000000ff000000ULL) << 8) |
378 ((x & 0x000000ff00000000ULL) >> 8) |
379 ((x & 0x0000ff0000000000ULL) >> 24) |
380 ((x & 0x00ff000000000000ULL) >> 40) |
381 ((x & 0xff00000000000000ULL) >> 56);
382}
383
384static inline uint32_t iso_733_to_u32(uint64_t x)
385{
386 return (uint32_t)x;
387}
388
389#define ISO_SECTOR_SIZE 2048
390
391#define ET_SECTOR_SHIFT 2
392#define KERN_IMAGE_START 0x010000UL
393#define PSW_MASK_64 0x0000000100000000ULL
394#define PSW_MASK_32 0x0000000080000000ULL
395#define IPL_PSW_MASK (PSW_MASK_32 | PSW_MASK_64)
396
397#define ISO_PRIMARY_VD_SECTOR 16
398
399static inline void read_iso_sector(uint32_t block_offset, void *buf,
400 const char *errmsg)
401{
402 IPL_assert(virtio_read_many(block_offset, buf, 1) == 0, errmsg);
403}
404
405static inline void read_iso_boot_image(uint32_t block_offset, void *load_addr,
406 uint32_t blks_to_load)
407{
408 IPL_assert(virtio_read_many(block_offset, load_addr, blks_to_load) == 0,
409 "Failed to read boot image!");
410}
411
412const uint8_t el_torito_magic[] = "EL TORITO SPECIFICATION"
413 "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
414
415#define ISO9660_MAX_DIR_DEPTH 8
416
417typedef struct IsoDirHdr {
418 uint8_t dr_len;
419 uint8_t ear_len;
420 uint64_t ext_loc;
421 uint64_t data_len;
422 uint8_t recording_datetime[7];
423 uint8_t file_flags;
424 uint8_t file_unit_size;
425 uint8_t gap_size;
426 uint32_t vol_seqnum;
427 uint8_t fileid_len;
428} __attribute__((packed)) IsoDirHdr;
429
430typedef struct IsoVdElTorito {
431 uint8_t el_torito[32];
432 uint8_t unused0[32];
433 uint32_t bc_offset;
434 uint8_t unused1[1974];
435} __attribute__((packed)) IsoVdElTorito;
436
437typedef struct IsoVdPrimary {
438 uint8_t unused1;
439 uint8_t sys_id[32];
440 uint8_t vol_id[32];
441 uint8_t unused2[8];
442 uint64_t vol_space_size;
443 uint8_t unused3[32];
444 uint32_t vol_set_size;
445 uint32_t vol_seqnum;
446 uint32_t log_block_size;
447 uint64_t path_table_size;
448 uint32_t l_path_table;
449 uint32_t opt_l_path_table;
450 uint32_t m_path_table;
451 uint32_t opt_m_path_table;
452 IsoDirHdr rootdir;
453 uint8_t root_null;
454 uint8_t reserved2[1858];
455} __attribute__((packed)) IsoVdPrimary;
456
457typedef struct IsoVolDesc {
458 uint8_t type;
459 uint8_t ident[5];
460 uint8_t version;
461 union {
462 IsoVdElTorito boot;
463 IsoVdPrimary primary;
464 } vd;
465} __attribute__((packed)) IsoVolDesc;
466
467const uint8_t vol_desc_magic[] = "CD001";
468#define VOL_DESC_TYPE_BOOT 0
469#define VOL_DESC_TYPE_PRIMARY 1
470#define VOL_DESC_TYPE_SUPPLEMENT 2
471#define VOL_DESC_TYPE_PARTITION 3
472#define VOL_DESC_TERMINATOR 255
473
474static inline bool is_iso_vd_valid(IsoVolDesc *vd)
475{
476 return !_memcmp(&vd->ident[0], vol_desc_magic, 5) &&
477 vd->version == 0x1 &&
478 vd->type <= VOL_DESC_TYPE_PARTITION;
479}
480
481typedef struct IsoBcValid {
482 uint8_t platform_id;
483 uint16_t reserved;
484 uint8_t id[24];
485 uint16_t checksum;
486 uint8_t key[2];
487} __attribute__((packed)) IsoBcValid;
488
489typedef struct IsoBcSection {
490 uint8_t boot_type;
491 uint16_t load_segment;
492 uint8_t sys_type;
493 uint8_t unused;
494 uint16_t sector_count;
495 uint32_t load_rba;
496 uint8_t selection[20];
497} __attribute__((packed)) IsoBcSection;
498
499typedef struct IsoBcHdr {
500 uint8_t platform_id;
501 uint16_t sect_num;
502 uint8_t id[28];
503} __attribute__((packed)) IsoBcHdr;
504
505
506
507
508
509const uint8_t linux_s390_magic[] = "\x02\x00\x00\x18\x60\x00\x00\x50\x02\x00"
510 "\x00\x68\x60\x00\x00\x50\x40\x40\x40\x40"
511 "\x40\x40\x40\x40";
512
513typedef struct IsoBcEntry {
514 uint8_t id;
515 union {
516 IsoBcValid valid;
517 IsoBcSection sect;
518 IsoBcHdr hdr;
519 } body;
520} __attribute__((packed)) IsoBcEntry;
521
522#define ISO_BC_ENTRY_PER_SECTOR (ISO_SECTOR_SIZE / sizeof(IsoBcEntry))
523#define ISO_BC_HDR_VALIDATION 0x01
524#define ISO_BC_BOOTABLE_SECTION 0x88
525#define ISO_BC_MAGIC_55 0x55
526#define ISO_BC_MAGIC_AA 0xaa
527#define ISO_BC_PLATFORM_X86 0x0
528#define ISO_BC_PLATFORM_PPC 0x1
529#define ISO_BC_PLATFORM_MAC 0x2
530
531static inline bool is_iso_bc_valid(IsoBcEntry *e)
532{
533 IsoBcValid *v = &e->body.valid;
534
535 if (e->id != ISO_BC_HDR_VALIDATION) {
536 return false;
537 }
538
539 if (v->platform_id != ISO_BC_PLATFORM_X86 &&
540 v->platform_id != ISO_BC_PLATFORM_PPC &&
541 v->platform_id != ISO_BC_PLATFORM_MAC) {
542 return false;
543 }
544
545 return v->key[0] == ISO_BC_MAGIC_55 &&
546 v->key[1] == ISO_BC_MAGIC_AA &&
547 v->reserved == 0x0;
548}
549
550#endif
551