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 EckdCHS {
36 uint16_t cylinder;
37 uint16_t head;
38 uint8_t sector;
39} __attribute__ ((packed)) EckdCHS;
40
41typedef struct EckdBlockPtr {
42 EckdCHS chs;
43 uint16_t size;
44 uint8_t count;
45
46} __attribute__ ((packed)) EckdBlockPtr;
47
48typedef struct ExtEckdBlockPtr {
49 EckdBlockPtr bptr;
50 uint8_t reserved[8];
51} __attribute__ ((packed)) ExtEckdBlockPtr;
52
53typedef 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
63typedef struct BootMapTable {
64 uint8_t magic[4];
65 uint8_t reserved[12];
66 BootMapPointer entry[];
67} __attribute__ ((packed)) BootMapTable;
68
69typedef 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
76typedef struct ComponentHeader {
77 uint8_t magic[4];
78 uint8_t type;
79 uint8_t reserved[27];
80} __attribute((packed)) ComponentHeader;
81
82typedef struct ScsiMbr {
83 uint8_t magic[4];
84 uint32_t version_id;
85 uint8_t reserved[8];
86 ScsiBlockPtr pt;
87} __attribute__ ((packed)) ScsiMbr;
88
89#define ZIPL_MAGIC "zIPL"
90#define ZIPL_MAGIC_EBCDIC "\xa9\xc9\xd7\xd3"
91#define IPL1_MAGIC "\xc9\xd7\xd3\xf1"
92#define IPL2_MAGIC "\xc9\xd7\xd3\xf2"
93#define VOL1_MAGIC "\xe5\xd6\xd3\xf1"
94#define LNX1_MAGIC "\xd3\xd5\xe7\xf1"
95#define CMS1_MAGIC "\xc3\xd4\xe2\xf1"
96
97#define LDL1_VERSION '\x40'
98#define LDL2_VERSION '\xf2'
99
100#define ZIPL_COMP_HEADER_IPL 0x00
101#define ZIPL_COMP_HEADER_DUMP 0x01
102
103#define ZIPL_COMP_ENTRY_LOAD 0x02
104#define ZIPL_COMP_ENTRY_EXEC 0x01
105
106typedef struct XEckdMbr {
107 uint8_t magic[4];
108 uint8_t version;
109 uint8_t bp_type;
110 uint8_t dev_type;
111#define DEV_TYPE_ECKD 0x00
112#define DEV_TYPE_FBA 0x01
113 uint8_t flags;
114 BootMapPointer blockptr;
115 uint8_t reserved[8];
116} __attribute__ ((packed)) XEckdMbr;
117
118typedef struct BootMapScriptEntry {
119 BootMapPointer blkptr;
120 uint8_t pad[7];
121 uint8_t type;
122#define BOOT_SCRIPT_EXEC 0x01
123#define BOOT_SCRIPT_LOAD 0x02
124 union {
125 uint64_t load_address;
126 uint64_t load_psw;
127 } address;
128} __attribute__ ((packed)) BootMapScriptEntry;
129
130typedef struct BootMapScriptHeader {
131 uint32_t magic;
132 uint8_t type;
133#define BOOT_SCRIPT_HDR_IPL 0x00
134 uint8_t reserved[27];
135} __attribute__ ((packed)) BootMapScriptHeader;
136
137typedef struct BootMapScript {
138 BootMapScriptHeader header;
139 BootMapScriptEntry entry[0];
140} __attribute__ ((packed)) BootMapScript;
141
142
143
144
145
146
147
148
149
150typedef struct LDL_VTOC {
151 char magic[4];
152 char volser[6];
153 uint8_t reserved[69];
154 uint8_t LDL_version;
155 uint64_t formatted_blocks;
156} __attribute__ ((packed)) LDL_VTOC;
157
158typedef struct format_date {
159 uint8_t YY;
160 uint8_t MM;
161 uint8_t DD;
162 uint8_t hh;
163 uint8_t mm;
164 uint8_t ss;
165} __attribute__ ((packed)) format_date_t;
166
167typedef struct CMS_VTOC {
168
169 char magic[4];
170 char volser[6];
171 uint16_t version;
172 uint32_t block_size;
173 uint32_t disk_origin;
174 uint32_t blocks;
175 uint32_t formatted;
176 uint32_t CMS_blocks;
177 uint32_t CMS_used;
178 uint32_t FST_size;
179 uint32_t FST_per_CMS_blk;
180 format_date_t format_date;
181 uint8_t reserved1[2];
182 uint32_t offset;
183 uint32_t next_hole;
184 uint32_t HBLK_hole_offset;
185 uint32_t alloc_map_usr_off;
186 uint8_t reserved2[4];
187 char shared_seg_name[8];
188} __attribute__ ((packed)) CMS_VTOC;
189
190
191typedef struct BootInfoBpIpl {
192 union {
193 ExtEckdBlockPtr eckd;
194 ScsiBlockPtr linr;
195 } bm_ptr;
196 uint8_t unused[16];
197} __attribute__ ((packed)) BootInfoBpIpl;
198
199typedef struct EckdDumpParam {
200 uint32_t start_blk;
201 uint32_t end_blk;
202 uint16_t blocksize;
203 uint8_t num_heads;
204 uint8_t bpt;
205 char reserved[4];
206} __attribute((packed, may_alias)) EckdDumpParam;
207
208typedef struct FbaDumpParam {
209 uint64_t start_blk;
210 uint64_t blockct;
211} __attribute((packed)) FbaDumpParam;
212
213typedef struct BootInfoBpDump {
214 union {
215 EckdDumpParam eckd;
216 FbaDumpParam fba;
217 } param;
218 uint8_t unused[16];
219} __attribute__ ((packed)) BootInfoBpDump;
220
221typedef struct BootInfo {
222 unsigned char magic[4];
223 uint8_t version;
224#define BOOT_INFO_VERSION 1
225 uint8_t bp_type;
226#define BOOT_INFO_BP_TYPE_IPL 0x00
227#define BOOT_INFO_BP_TYPE_DUMP 0x01
228 uint8_t dev_type;
229#define BOOT_INFO_DEV_TYPE_ECKD 0x00
230#define BOOT_INFO_DEV_TYPE_FBA 0x01
231 uint8_t flags;
232#ifdef __s390x__
233#define BOOT_INFO_FLAGS_ARCH 0x01
234#else
235#define BOOT_INFO_FLAGS_ARCH 0x00
236#endif
237 union {
238 BootInfoBpDump dump;
239 BootInfoBpIpl ipl;
240 } bp;
241} __attribute__ ((packed)) BootInfo;
242
243
244
245
246#define STAGE2_BLK_CNT_MAX 24
247
248typedef struct EckdCdlIpl1 {
249 uint8_t key[4];
250 uint8_t data[24];
251} __attribute__((packed)) EckdCdlIpl1;
252
253typedef struct EckdSeekArg {
254 uint16_t pad;
255 EckdCHS chs;
256 uint8_t pad2;
257} __attribute__ ((packed)) EckdSeekArg;
258
259typedef struct EckdStage1b {
260 uint8_t reserved[32 * STAGE2_BLK_CNT_MAX];
261 struct EckdSeekArg seek[STAGE2_BLK_CNT_MAX];
262 uint8_t unused[64];
263} __attribute__ ((packed)) EckdStage1b;
264
265typedef struct EckdStage1 {
266 uint8_t reserved[72];
267 struct EckdSeekArg seek[2];
268} __attribute__ ((packed)) EckdStage1;
269
270typedef struct EckdCdlIpl2 {
271 uint8_t key[4];
272 struct EckdStage1 stage1;
273 XEckdMbr mbr;
274 uint8_t reserved[24];
275} __attribute__((packed)) EckdCdlIpl2;
276
277typedef struct EckdLdlIpl1 {
278 uint8_t reserved[24];
279 struct EckdStage1 stage1;
280 BootInfo bip;
281} __attribute__((packed)) EckdLdlIpl1;
282
283typedef struct IplVolumeLabel {
284 unsigned char key[4];
285 union {
286 unsigned char data[80];
287 struct {
288 unsigned char key[4];
289 unsigned char volser[6];
290 unsigned char reserved[6];
291 } f;
292 };
293} __attribute__((packed)) IplVolumeLabel;
294
295typedef enum {
296 ECKD_NO_IPL,
297 ECKD_CMS,
298 ECKD_LDL,
299 ECKD_LDL_UNLABELED,
300} ECKD_IPL_mode_t;
301
302
303
304static inline void print_volser(const void *volser)
305{
306 char ascii[8];
307
308 ebcdic_to_ascii((char *)volser, ascii, 6);
309 ascii[6] = '\0';
310 sclp_print("VOLSER=[");
311 sclp_print(ascii);
312 sclp_print("]\n");
313}
314
315static inline bool unused_space(const void *p, size_t size)
316{
317 size_t i;
318 const unsigned char *m = p;
319
320 for (i = 0; i < size; i++) {
321 if (m[i] != FREE_SPACE_FILLER) {
322 return false;
323 }
324 }
325 return true;
326}
327
328static inline bool is_null_block_number(block_number_t x)
329{
330 return x == NULL_BLOCK_NR;
331}
332
333static inline void read_block(block_number_t blockno,
334 void *buffer,
335 const char *errmsg)
336{
337 IPL_assert(virtio_read(blockno, buffer) == 0, errmsg);
338}
339
340static inline bool block_size_ok(uint32_t block_size)
341{
342 return block_size == virtio_get_block_size();
343}
344
345static inline bool magic_match(const void *data, const void *magic)
346{
347 return *((uint32_t *)data) == *((uint32_t *)magic);
348}
349
350static inline uint32_t iso_733_to_u32(uint64_t x)
351{
352 return (uint32_t)x;
353}
354
355#define ISO_SECTOR_SIZE 2048
356
357#define ET_SECTOR_SHIFT 2
358#define KERN_IMAGE_START 0x010000UL
359#define PSW_MASK_64 0x0000000100000000ULL
360#define PSW_MASK_32 0x0000000080000000ULL
361#define IPL_PSW_MASK (PSW_MASK_32 | PSW_MASK_64)
362
363#define ISO_PRIMARY_VD_SECTOR 16
364
365static inline void read_iso_sector(uint32_t block_offset, void *buf,
366 const char *errmsg)
367{
368 IPL_assert(virtio_read_many(block_offset, buf, 1) == 0, errmsg);
369}
370
371static inline void read_iso_boot_image(uint32_t block_offset, void *load_addr,
372 uint32_t blks_to_load)
373{
374 IPL_assert(virtio_read_many(block_offset, load_addr, blks_to_load) == 0,
375 "Failed to read boot image!");
376}
377
378#define ISO9660_MAX_DIR_DEPTH 8
379
380typedef struct IsoDirHdr {
381 uint8_t dr_len;
382 uint8_t ear_len;
383 uint64_t ext_loc;
384 uint64_t data_len;
385 uint8_t recording_datetime[7];
386 uint8_t file_flags;
387 uint8_t file_unit_size;
388 uint8_t gap_size;
389 uint32_t vol_seqnum;
390 uint8_t fileid_len;
391} __attribute__((packed)) IsoDirHdr;
392
393typedef struct IsoVdElTorito {
394 uint8_t el_torito[32];
395 uint8_t unused0[32];
396 uint32_t bc_offset;
397 uint8_t unused1[1974];
398} __attribute__((packed)) IsoVdElTorito;
399
400typedef struct IsoVdPrimary {
401 uint8_t unused1;
402 uint8_t sys_id[32];
403 uint8_t vol_id[32];
404 uint8_t unused2[8];
405 uint64_t vol_space_size;
406 uint8_t unused3[32];
407 uint32_t vol_set_size;
408 uint32_t vol_seqnum;
409 uint32_t log_block_size;
410 uint64_t path_table_size;
411 uint32_t l_path_table;
412 uint32_t opt_l_path_table;
413 uint32_t m_path_table;
414 uint32_t opt_m_path_table;
415 IsoDirHdr rootdir;
416 uint8_t root_null;
417 uint8_t reserved2[1858];
418} __attribute__((packed)) IsoVdPrimary;
419
420typedef struct IsoVolDesc {
421 uint8_t type;
422 uint8_t ident[5];
423 uint8_t version;
424 union {
425 IsoVdElTorito boot;
426 IsoVdPrimary primary;
427 } vd;
428} __attribute__((packed)) IsoVolDesc;
429
430#define VOL_DESC_TYPE_BOOT 0
431#define VOL_DESC_TYPE_PRIMARY 1
432#define VOL_DESC_TYPE_SUPPLEMENT 2
433#define VOL_DESC_TYPE_PARTITION 3
434#define VOL_DESC_TERMINATOR 255
435
436typedef struct IsoBcValid {
437 uint8_t platform_id;
438 uint16_t reserved;
439 uint8_t id[24];
440 uint16_t checksum;
441 uint8_t key[2];
442} __attribute__((packed)) IsoBcValid;
443
444typedef struct IsoBcSection {
445 uint8_t boot_type;
446 uint16_t load_segment;
447 uint8_t sys_type;
448 uint8_t unused;
449 uint16_t sector_count;
450 uint32_t load_rba;
451 uint8_t selection[20];
452} __attribute__((packed)) IsoBcSection;
453
454typedef struct IsoBcHdr {
455 uint8_t platform_id;
456 uint16_t sect_num;
457 uint8_t id[28];
458} __attribute__((packed)) IsoBcHdr;
459
460typedef struct IsoBcEntry {
461 uint8_t id;
462 union {
463 IsoBcValid valid;
464 IsoBcSection sect;
465 IsoBcHdr hdr;
466 } body;
467} __attribute__((packed)) IsoBcEntry;
468
469#define ISO_BC_ENTRY_PER_SECTOR (ISO_SECTOR_SIZE / sizeof(IsoBcEntry))
470#define ISO_BC_HDR_VALIDATION 0x01
471#define ISO_BC_BOOTABLE_SECTION 0x88
472#define ISO_BC_MAGIC_55 0x55
473#define ISO_BC_MAGIC_AA 0xaa
474#define ISO_BC_PLATFORM_X86 0x0
475#define ISO_BC_PLATFORM_PPC 0x1
476#define ISO_BC_PLATFORM_MAC 0x2
477
478static inline bool is_iso_bc_valid(IsoBcEntry *e)
479{
480 IsoBcValid *v = &e->body.valid;
481
482 if (e->id != ISO_BC_HDR_VALIDATION) {
483 return false;
484 }
485
486 if (v->platform_id != ISO_BC_PLATFORM_X86 &&
487 v->platform_id != ISO_BC_PLATFORM_PPC &&
488 v->platform_id != ISO_BC_PLATFORM_MAC) {
489 return false;
490 }
491
492 return v->key[0] == ISO_BC_MAGIC_55 &&
493 v->key[1] == ISO_BC_MAGIC_AA &&
494 v->reserved == 0x0;
495}
496
497#endif
498