2013-04-22 23:02:49 +04:00
|
|
|
/*
|
|
|
|
* QEMU S390 bootmap interpreter
|
|
|
|
*
|
|
|
|
* Copyright (c) 2009 Alexander Graf <agraf@suse.de>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or (at
|
|
|
|
* your option) any later version. See the COPYING file in the top-level
|
|
|
|
* directory.
|
|
|
|
*/
|
|
|
|
|
2024-10-20 04:29:36 +03:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2013-04-22 23:02:49 +04:00
|
|
|
#include "s390-ccw.h"
|
2020-10-06 12:42:46 +03:00
|
|
|
#include "s390-arch.h"
|
2014-05-19 22:08:54 +04:00
|
|
|
#include "bootmap.h"
|
2014-05-19 22:10:27 +04:00
|
|
|
#include "virtio.h"
|
2017-07-12 15:49:47 +03:00
|
|
|
#include "bswap.h"
|
2013-04-22 23:02:49 +04:00
|
|
|
|
2014-05-19 22:12:43 +04:00
|
|
|
#ifdef DEBUG
|
2014-05-19 22:05:40 +04:00
|
|
|
/* #define DEBUG_FALLBACK */
|
2014-05-19 22:12:43 +04:00
|
|
|
#endif
|
2013-04-22 23:02:49 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG_FALLBACK
|
|
|
|
#define dputs(txt) \
|
2024-10-20 04:29:36 +03:00
|
|
|
do { printf("zipl: " txt); } while (0)
|
2013-04-22 23:02:49 +04:00
|
|
|
#else
|
|
|
|
#define dputs(fmt, ...) \
|
|
|
|
do { } while (0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Scratch space */
|
2014-05-19 22:12:43 +04:00
|
|
|
static uint8_t sec[MAX_SECTOR_SIZE*4] __attribute__((__aligned__(PAGE_SIZE)));
|
2013-04-22 23:02:49 +04:00
|
|
|
|
2018-03-06 09:18:01 +03:00
|
|
|
const uint8_t el_torito_magic[] = "EL TORITO SPECIFICATION"
|
|
|
|
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Match two CCWs located after PSW and eight filler bytes.
|
|
|
|
* From libmagic and arch/s390/kernel/head.S.
|
|
|
|
*/
|
|
|
|
const uint8_t linux_s390_magic[] = "\x02\x00\x00\x18\x60\x00\x00\x50\x02\x00"
|
|
|
|
"\x00\x68\x60\x00\x00\x50\x40\x40\x40\x40"
|
|
|
|
"\x40\x40\x40\x40";
|
|
|
|
|
|
|
|
static inline bool is_iso_vd_valid(IsoVolDesc *vd)
|
|
|
|
{
|
|
|
|
const uint8_t vol_desc_magic[] = "CD001";
|
|
|
|
|
|
|
|
return !memcmp(&vd->ident[0], vol_desc_magic, 5) &&
|
|
|
|
vd->version == 0x1 &&
|
|
|
|
vd->type <= VOL_DESC_TYPE_PARTITION;
|
|
|
|
}
|
|
|
|
|
2014-05-19 22:13:41 +04:00
|
|
|
/***********************************************************************
|
|
|
|
* IPL an ECKD DASD (CDL or LDL/CMS format)
|
|
|
|
*/
|
|
|
|
|
|
|
|
static unsigned char _bprs[8*1024]; /* guessed "max" ECKD sector size */
|
2015-01-22 15:44:26 +03:00
|
|
|
static const int max_bprs_entries = sizeof(_bprs) / sizeof(ExtEckdBlockPtr);
|
2018-02-23 18:43:14 +03:00
|
|
|
static uint8_t _s2[MAX_SECTOR_SIZE * 3] __attribute__((__aligned__(PAGE_SIZE)));
|
|
|
|
static void *s2_prev_blk = _s2;
|
|
|
|
static void *s2_cur_blk = _s2 + MAX_SECTOR_SIZE;
|
|
|
|
static void *s2_next_blk = _s2 + MAX_SECTOR_SIZE * 2;
|
2014-05-19 22:13:41 +04:00
|
|
|
|
2024-10-20 04:29:46 +03:00
|
|
|
static inline int verify_boot_info(BootInfo *bip)
|
2014-05-19 22:14:41 +04:00
|
|
|
{
|
2024-10-20 04:29:46 +03:00
|
|
|
if (!magic_match(bip->magic, ZIPL_MAGIC)) {
|
|
|
|
puts("No zIPL sig in BootInfo");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (bip->version != BOOT_INFO_VERSION) {
|
|
|
|
puts("Wrong zIPL version");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (bip->bp_type != BOOT_INFO_BP_TYPE_IPL) {
|
|
|
|
puts("DASD is not for IPL");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
if (bip->dev_type != BOOT_INFO_DEV_TYPE_ECKD) {
|
|
|
|
puts("DASD is not ECKD");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
if (bip->flags != BOOT_INFO_FLAGS_ARCH) {
|
|
|
|
puts("Not for this arch");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (!block_size_ok(bip->bp.ipl.bm_ptr.eckd.bptr.size)) {
|
|
|
|
puts("Bad block size in zIPL section of 1st record");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2014-05-19 22:14:41 +04:00
|
|
|
}
|
|
|
|
|
2023-02-21 20:45:48 +03:00
|
|
|
static void eckd_format_chs(ExtEckdBlockPtr *ptr, bool ldipl,
|
|
|
|
uint64_t *c,
|
|
|
|
uint64_t *h,
|
|
|
|
uint64_t *s)
|
|
|
|
{
|
|
|
|
if (ldipl) {
|
|
|
|
*c = ptr->ldptr.chs.cylinder;
|
|
|
|
*h = ptr->ldptr.chs.head;
|
|
|
|
*s = ptr->ldptr.chs.sector;
|
|
|
|
} else {
|
|
|
|
*c = ptr->bptr.chs.cylinder;
|
|
|
|
*h = ptr->bptr.chs.head;
|
|
|
|
*s = ptr->bptr.chs.sector;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static block_number_t eckd_chs_to_block(uint64_t c, uint64_t h, uint64_t s)
|
2014-05-19 22:13:41 +04:00
|
|
|
{
|
2014-08-29 13:01:39 +04:00
|
|
|
const uint64_t sectors = virtio_get_sectors();
|
|
|
|
const uint64_t heads = virtio_get_heads();
|
2023-02-21 20:45:48 +03:00
|
|
|
const uint64_t cylinder = c + ((h & 0xfff0) << 12);
|
|
|
|
const uint64_t head = h & 0x000f;
|
2014-08-29 13:01:39 +04:00
|
|
|
const block_number_t block = sectors * heads * cylinder
|
|
|
|
+ sectors * head
|
2023-02-21 20:45:48 +03:00
|
|
|
+ s - 1; /* block nr starts with zero */
|
2014-08-29 13:01:39 +04:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
2023-02-21 20:45:48 +03:00
|
|
|
static block_number_t eckd_block_num(EckdCHS *chs)
|
2014-08-29 13:01:39 +04:00
|
|
|
{
|
2023-02-21 20:45:48 +03:00
|
|
|
return eckd_chs_to_block(chs->cylinder, chs->head, chs->sector);
|
|
|
|
}
|
|
|
|
|
|
|
|
static block_number_t gen_eckd_block_num(ExtEckdBlockPtr *ptr, bool ldipl)
|
|
|
|
{
|
|
|
|
uint64_t cyl, head, sec;
|
|
|
|
eckd_format_chs(ptr, ldipl, &cyl, &head, &sec);
|
|
|
|
return eckd_chs_to_block(cyl, head, sec);
|
|
|
|
}
|
2014-05-19 22:13:41 +04:00
|
|
|
|
2023-02-21 20:45:48 +03:00
|
|
|
static bool eckd_valid_chs(uint64_t cyl, uint64_t head, uint64_t sector)
|
|
|
|
{
|
2014-05-19 22:13:41 +04:00
|
|
|
if (head >= virtio_get_heads()
|
2023-02-21 20:45:48 +03:00
|
|
|
|| sector > virtio_get_sectors()
|
|
|
|
|| sector <= 0) {
|
2014-05-19 22:13:41 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-29 13:01:39 +04:00
|
|
|
if (!virtio_guessed_disk_nature() &&
|
2023-02-21 20:45:48 +03:00
|
|
|
eckd_chs_to_block(cyl, head, sector) >= virtio_get_blocks()) {
|
2014-05-19 22:13:41 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-02-21 20:45:48 +03:00
|
|
|
static bool eckd_valid_address(ExtEckdBlockPtr *ptr, bool ldipl)
|
|
|
|
{
|
|
|
|
uint64_t cyl, head, sec;
|
|
|
|
eckd_format_chs(ptr, ldipl, &cyl, &head, &sec);
|
|
|
|
return eckd_valid_chs(cyl, head, sec);
|
|
|
|
}
|
|
|
|
|
|
|
|
static block_number_t load_eckd_segments(block_number_t blk, bool ldipl,
|
|
|
|
uint64_t *address)
|
2014-05-19 22:13:41 +04:00
|
|
|
{
|
|
|
|
block_number_t block_nr;
|
2023-02-21 20:45:48 +03:00
|
|
|
int j, rc, count;
|
2014-05-19 22:13:41 +04:00
|
|
|
BootMapPointer *bprs = (void *)_bprs;
|
|
|
|
bool more_data;
|
|
|
|
|
|
|
|
memset(_bprs, FREE_SPACE_FILLER, sizeof(_bprs));
|
2024-10-20 04:29:42 +03:00
|
|
|
if (virtio_read(blk, bprs)) {
|
|
|
|
puts("BPRS read failed");
|
|
|
|
return ERROR_BLOCK_NR;
|
|
|
|
}
|
2014-05-19 22:13:41 +04:00
|
|
|
|
|
|
|
do {
|
|
|
|
more_data = false;
|
|
|
|
for (j = 0;; j++) {
|
2023-02-21 20:45:48 +03:00
|
|
|
block_nr = gen_eckd_block_num(&bprs[j].xeckd, ldipl);
|
2014-05-19 22:13:41 +04:00
|
|
|
if (is_null_block_number(block_nr)) { /* end of chunk */
|
2024-10-20 04:29:42 +03:00
|
|
|
return NULL_BLOCK_NR;
|
2014-05-19 22:13:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* we need the updated blockno for the next indirect entry
|
|
|
|
* in the chain, but don't want to advance address
|
|
|
|
*/
|
|
|
|
if (j == (max_bprs_entries - 1)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-02-21 20:45:48 +03:00
|
|
|
/* List directed pointer does not store block size */
|
2024-10-20 04:29:42 +03:00
|
|
|
if (!ldipl && !block_size_ok(bprs[j].xeckd.bptr.size)) {
|
|
|
|
puts("Bad chunk block size");
|
|
|
|
return ERROR_BLOCK_NR;
|
|
|
|
}
|
2014-05-19 22:13:41 +04:00
|
|
|
|
2023-02-21 20:45:48 +03:00
|
|
|
if (!eckd_valid_address(&bprs[j].xeckd, ldipl)) {
|
|
|
|
/*
|
|
|
|
* If an invalid address is found during LD-IPL then break and
|
2024-10-20 04:29:42 +03:00
|
|
|
* retry as CCW-IPL, otherwise abort on error
|
2023-02-21 20:45:48 +03:00
|
|
|
*/
|
2024-10-20 04:29:42 +03:00
|
|
|
if (!ldipl) {
|
|
|
|
puts("Bad chunk ECKD address");
|
|
|
|
return ERROR_BLOCK_NR;
|
|
|
|
}
|
2023-02-21 20:45:48 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ldipl) {
|
|
|
|
count = bprs[j].xeckd.ldptr.count;
|
|
|
|
} else {
|
|
|
|
count = bprs[j].xeckd.bptr.count;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count == 0 && unused_space(&bprs[j + 1],
|
2014-05-19 22:13:41 +04:00
|
|
|
sizeof(EckdBlockPtr))) {
|
|
|
|
/* This is a "continue" pointer.
|
|
|
|
* This ptr should be the last one in the current
|
|
|
|
* script section.
|
|
|
|
* I.e. the next ptr must point to the unused memory area
|
|
|
|
*/
|
|
|
|
memset(_bprs, FREE_SPACE_FILLER, sizeof(_bprs));
|
2024-10-20 04:29:42 +03:00
|
|
|
if (virtio_read(block_nr, bprs)) {
|
|
|
|
puts("BPRS continuation read failed");
|
|
|
|
return ERROR_BLOCK_NR;
|
|
|
|
}
|
2014-05-19 22:13:41 +04:00
|
|
|
more_data = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load (count+1) blocks of code at (block_nr)
|
|
|
|
* to memory (address).
|
|
|
|
*/
|
2023-02-21 20:45:48 +03:00
|
|
|
rc = virtio_read_many(block_nr, (void *)(*address), count + 1);
|
2024-10-20 04:29:42 +03:00
|
|
|
if (rc != 0) {
|
|
|
|
puts("Code chunk read failed");
|
|
|
|
return ERROR_BLOCK_NR;
|
|
|
|
}
|
2014-05-19 22:13:41 +04:00
|
|
|
|
2023-02-21 20:45:48 +03:00
|
|
|
*address += (count + 1) * virtio_get_block_size();
|
2014-05-19 22:13:41 +04:00
|
|
|
}
|
|
|
|
} while (more_data);
|
|
|
|
return block_nr;
|
|
|
|
}
|
|
|
|
|
2018-02-23 18:43:14 +03:00
|
|
|
static bool find_zipl_boot_menu_banner(int *offset)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Menu banner starts with "zIPL" */
|
2020-09-24 11:59:24 +03:00
|
|
|
for (i = 0; i <= virtio_get_block_size() - 4; i++) {
|
2018-02-23 18:43:14 +03:00
|
|
|
if (magic_match(s2_cur_blk + i, ZIPL_MAGIC_EBCDIC)) {
|
|
|
|
*offset = i;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int eckd_get_boot_menu_index(block_number_t s1b_block_nr)
|
|
|
|
{
|
|
|
|
block_number_t cur_block_nr;
|
|
|
|
block_number_t prev_block_nr = 0;
|
|
|
|
block_number_t next_block_nr = 0;
|
|
|
|
EckdStage1b *s1b = (void *)sec;
|
|
|
|
int banner_offset;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Get Stage1b data */
|
|
|
|
memset(sec, FREE_SPACE_FILLER, sizeof(sec));
|
2024-10-20 04:29:42 +03:00
|
|
|
if (virtio_read(s1b_block_nr, s1b)) {
|
|
|
|
puts("Cannot read stage1b boot loader");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2018-02-23 18:43:14 +03:00
|
|
|
|
|
|
|
memset(_s2, FREE_SPACE_FILLER, sizeof(_s2));
|
|
|
|
|
|
|
|
/* Get Stage2 data */
|
|
|
|
for (i = 0; i < STAGE2_BLK_CNT_MAX; i++) {
|
|
|
|
cur_block_nr = eckd_block_num(&s1b->seek[i].chs);
|
|
|
|
|
2020-09-24 11:59:25 +03:00
|
|
|
if (!cur_block_nr || is_null_block_number(cur_block_nr)) {
|
2018-02-23 18:43:14 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-10-20 04:29:42 +03:00
|
|
|
if (virtio_read(cur_block_nr, s2_cur_blk)) {
|
|
|
|
puts("Cannot read stage2 boot loader");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2018-02-23 18:43:14 +03:00
|
|
|
|
|
|
|
if (find_zipl_boot_menu_banner(&banner_offset)) {
|
|
|
|
/*
|
|
|
|
* Load the adjacent blocks to account for the
|
|
|
|
* possibility of menu data spanning multiple blocks.
|
|
|
|
*/
|
|
|
|
if (prev_block_nr) {
|
2024-10-20 04:29:42 +03:00
|
|
|
if (virtio_read(prev_block_nr, s2_prev_blk)) {
|
|
|
|
puts("Cannot read stage2 boot loader");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2018-02-23 18:43:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (i + 1 < STAGE2_BLK_CNT_MAX) {
|
|
|
|
next_block_nr = eckd_block_num(&s1b->seek[i + 1].chs);
|
|
|
|
}
|
|
|
|
|
2021-04-16 10:47:36 +03:00
|
|
|
if (next_block_nr && !is_null_block_number(next_block_nr)) {
|
2024-10-20 04:29:42 +03:00
|
|
|
if (virtio_read(next_block_nr, s2_next_blk)) {
|
|
|
|
puts("Cannot read stage2 boot loader");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2018-02-23 18:43:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return menu_get_zipl_boot_index(s2_cur_blk + banner_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
prev_block_nr = cur_block_nr;
|
|
|
|
}
|
|
|
|
|
2024-10-20 04:29:36 +03:00
|
|
|
printf("No zipl boot menu data found. Booting default entry.");
|
2018-02-23 18:43:14 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-10-20 04:29:42 +03:00
|
|
|
static int run_eckd_boot_script(block_number_t bmt_block_nr,
|
2018-02-23 18:43:14 +03:00
|
|
|
block_number_t s1b_block_nr)
|
2014-05-19 22:13:41 +04:00
|
|
|
{
|
|
|
|
int i;
|
2016-04-18 14:45:32 +03:00
|
|
|
unsigned int loadparm = get_loadparm_index();
|
2014-05-19 22:13:41 +04:00
|
|
|
block_number_t block_nr;
|
|
|
|
uint64_t address;
|
2018-02-23 18:43:07 +03:00
|
|
|
BootMapTable *bmt = (void *)sec;
|
2014-05-19 22:13:41 +04:00
|
|
|
BootMapScript *bms = (void *)sec;
|
2023-02-21 20:45:48 +03:00
|
|
|
/* The S1B block number is NULL_BLOCK_NR if and only if it's an LD-IPL */
|
|
|
|
bool ldipl = (s1b_block_nr == NULL_BLOCK_NR);
|
2014-05-19 22:13:41 +04:00
|
|
|
|
2023-02-21 20:45:48 +03:00
|
|
|
if (menu_is_enabled_zipl() && !ldipl) {
|
2018-02-23 18:43:14 +03:00
|
|
|
loadparm = eckd_get_boot_menu_index(s1b_block_nr);
|
|
|
|
}
|
|
|
|
|
2016-04-18 14:45:32 +03:00
|
|
|
debug_print_int("loadparm", loadparm);
|
2024-10-20 04:29:42 +03:00
|
|
|
if (loadparm >= MAX_BOOT_ENTRIES) {
|
|
|
|
puts("loadparm value greater than max number of boot entries allowed");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2016-04-18 14:45:32 +03:00
|
|
|
|
2014-05-19 22:13:41 +04:00
|
|
|
memset(sec, FREE_SPACE_FILLER, sizeof(sec));
|
2024-10-20 04:29:42 +03:00
|
|
|
if (virtio_read(bmt_block_nr, sec)) {
|
|
|
|
puts("Cannot read Boot Map Table");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2014-05-19 22:13:41 +04:00
|
|
|
|
2023-02-21 20:45:48 +03:00
|
|
|
block_nr = gen_eckd_block_num(&bmt->entry[loadparm].xeckd, ldipl);
|
2024-10-20 04:29:42 +03:00
|
|
|
if (block_nr == NULL_BLOCK_NR) {
|
|
|
|
puts("Cannot find Boot Map Table Entry");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2014-05-19 22:13:41 +04:00
|
|
|
|
|
|
|
memset(sec, FREE_SPACE_FILLER, sizeof(sec));
|
2024-10-20 04:29:42 +03:00
|
|
|
if (virtio_read(block_nr, sec)) {
|
|
|
|
puts("Cannot read Boot Map Script");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2014-05-19 22:13:41 +04:00
|
|
|
|
2019-04-29 16:09:41 +03:00
|
|
|
for (i = 0; bms->entry[i].type == BOOT_SCRIPT_LOAD ||
|
|
|
|
bms->entry[i].type == BOOT_SCRIPT_SIGNATURE; i++) {
|
|
|
|
|
|
|
|
/* We don't support secure boot yet, so we skip signature entries */
|
|
|
|
if (bms->entry[i].type == BOOT_SCRIPT_SIGNATURE) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-05-19 22:13:41 +04:00
|
|
|
address = bms->entry[i].address.load_address;
|
2023-02-21 20:45:48 +03:00
|
|
|
block_nr = gen_eckd_block_num(&bms->entry[i].blkptr.xeckd, ldipl);
|
2014-05-19 22:13:41 +04:00
|
|
|
|
|
|
|
do {
|
2023-02-21 20:45:48 +03:00
|
|
|
block_nr = load_eckd_segments(block_nr, ldipl, &address);
|
2024-10-20 04:29:42 +03:00
|
|
|
if (block_nr == ERROR_BLOCK_NR) {
|
|
|
|
return ldipl ? 0 : -EIO;
|
|
|
|
}
|
|
|
|
} while (block_nr != NULL_BLOCK_NR);
|
2014-05-19 22:13:41 +04:00
|
|
|
}
|
|
|
|
|
2023-02-21 20:45:48 +03:00
|
|
|
if (ldipl && bms->entry[i].type != BOOT_SCRIPT_EXEC) {
|
|
|
|
/* Abort LD-IPL and retry as CCW-IPL */
|
2024-10-20 04:29:42 +03:00
|
|
|
return 0;
|
2023-02-21 20:45:48 +03:00
|
|
|
}
|
|
|
|
|
2024-10-20 04:29:42 +03:00
|
|
|
if (bms->entry[i].type != BOOT_SCRIPT_EXEC) {
|
|
|
|
puts("Unknown script entry type");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2024-10-20 04:29:46 +03:00
|
|
|
write_reset_psw(bms->entry[i].address.load_address);
|
|
|
|
jump_to_IPL_code(0);
|
2024-10-20 04:29:42 +03:00
|
|
|
return -1;
|
2014-05-19 22:13:41 +04:00
|
|
|
}
|
|
|
|
|
2024-10-20 04:29:42 +03:00
|
|
|
static int ipl_eckd_cdl(void)
|
2014-05-19 22:13:41 +04:00
|
|
|
{
|
|
|
|
XEckdMbr *mbr;
|
2018-02-23 18:43:09 +03:00
|
|
|
EckdCdlIpl2 *ipl2 = (void *)sec;
|
2014-05-19 22:13:41 +04:00
|
|
|
IplVolumeLabel *vlbl = (void *)sec;
|
2018-02-23 18:43:14 +03:00
|
|
|
block_number_t bmt_block_nr, s1b_block_nr;
|
2014-05-19 22:13:41 +04:00
|
|
|
|
|
|
|
/* we have just read the block #0 and recognized it as "IPL1" */
|
2024-10-20 04:29:36 +03:00
|
|
|
puts("CDL");
|
2014-05-19 22:13:41 +04:00
|
|
|
|
|
|
|
memset(sec, FREE_SPACE_FILLER, sizeof(sec));
|
2024-10-20 04:29:42 +03:00
|
|
|
if (virtio_read(1, ipl2)) {
|
|
|
|
puts("Cannot read IPL2 record at block 1");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2014-05-19 22:13:41 +04:00
|
|
|
|
2018-02-23 18:43:09 +03:00
|
|
|
mbr = &ipl2->mbr;
|
2020-07-28 19:14:50 +03:00
|
|
|
if (!magic_match(mbr, ZIPL_MAGIC)) {
|
2024-10-20 04:29:36 +03:00
|
|
|
puts("No zIPL section in IPL2 record.");
|
2024-10-20 04:29:42 +03:00
|
|
|
return 0;
|
2020-07-28 19:14:50 +03:00
|
|
|
}
|
|
|
|
if (!block_size_ok(mbr->blockptr.xeckd.bptr.size)) {
|
2024-10-20 04:29:36 +03:00
|
|
|
puts("Bad block size in zIPL section of IPL2 record.");
|
2024-10-20 04:29:42 +03:00
|
|
|
return 0;
|
2020-07-28 19:14:50 +03:00
|
|
|
}
|
2021-04-21 18:48:48 +03:00
|
|
|
if (mbr->dev_type != DEV_TYPE_ECKD) {
|
2024-10-20 04:29:36 +03:00
|
|
|
puts("Non-ECKD device type in zIPL section of IPL2 record.");
|
2024-10-20 04:29:42 +03:00
|
|
|
return 0;
|
2020-07-28 19:14:50 +03:00
|
|
|
}
|
2014-05-19 22:13:41 +04:00
|
|
|
|
2018-02-23 18:43:07 +03:00
|
|
|
/* save pointer to Boot Map Table */
|
2018-02-23 18:43:08 +03:00
|
|
|
bmt_block_nr = eckd_block_num(&mbr->blockptr.xeckd.bptr.chs);
|
2014-05-19 22:13:41 +04:00
|
|
|
|
2018-02-23 18:43:14 +03:00
|
|
|
/* save pointer to Stage1b Data */
|
|
|
|
s1b_block_nr = eckd_block_num(&ipl2->stage1.seek[0].chs);
|
|
|
|
|
2014-05-19 22:13:41 +04:00
|
|
|
memset(sec, FREE_SPACE_FILLER, sizeof(sec));
|
2024-10-20 04:29:42 +03:00
|
|
|
if (virtio_read(2, vlbl)) {
|
|
|
|
puts("Cannot read Volume Label at block 2");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2020-07-28 19:14:50 +03:00
|
|
|
if (!magic_match(vlbl->key, VOL1_MAGIC)) {
|
2024-10-20 04:29:36 +03:00
|
|
|
puts("Invalid magic of volume label block.");
|
2024-10-20 04:29:42 +03:00
|
|
|
return 0;
|
2020-07-28 19:14:50 +03:00
|
|
|
}
|
|
|
|
if (!magic_match(vlbl->f.key, VOL1_MAGIC)) {
|
2024-10-20 04:29:36 +03:00
|
|
|
puts("Invalid magic of volser block.");
|
2024-10-20 04:29:42 +03:00
|
|
|
return 0;
|
2020-07-28 19:14:50 +03:00
|
|
|
}
|
2014-05-19 22:13:41 +04:00
|
|
|
print_volser(vlbl->f.volser);
|
|
|
|
|
2024-10-20 04:29:42 +03:00
|
|
|
return run_eckd_boot_script(bmt_block_nr, s1b_block_nr);
|
2014-05-19 22:13:41 +04:00
|
|
|
}
|
|
|
|
|
2014-08-29 13:01:40 +04:00
|
|
|
static void print_eckd_ldl_msg(ECKD_IPL_mode_t mode)
|
2014-05-19 22:14:41 +04:00
|
|
|
{
|
|
|
|
LDL_VTOC *vlbl = (void *)sec; /* already read, 3rd block */
|
|
|
|
char msg[4] = { '?', '.', '\n', '\0' };
|
|
|
|
|
2024-10-20 04:29:36 +03:00
|
|
|
printf((mode == ECKD_CMS) ? "CMS" : "LDL");
|
|
|
|
printf(" version ");
|
2014-05-19 22:14:41 +04:00
|
|
|
switch (vlbl->LDL_version) {
|
|
|
|
case LDL1_VERSION:
|
|
|
|
msg[0] = '1';
|
|
|
|
break;
|
|
|
|
case LDL2_VERSION:
|
|
|
|
msg[0] = '2';
|
|
|
|
break;
|
|
|
|
default:
|
2020-06-24 10:52:23 +03:00
|
|
|
msg[0] = ebc2asc[vlbl->LDL_version];
|
2014-05-19 22:14:41 +04:00
|
|
|
msg[1] = '?';
|
|
|
|
break;
|
|
|
|
}
|
2024-10-20 04:29:36 +03:00
|
|
|
printf("%s", msg);
|
2014-05-19 22:14:41 +04:00
|
|
|
print_volser(vlbl->volser);
|
2014-08-29 13:01:40 +04:00
|
|
|
}
|
|
|
|
|
2024-10-20 04:29:42 +03:00
|
|
|
static int ipl_eckd_ldl(ECKD_IPL_mode_t mode)
|
2014-08-29 13:01:40 +04:00
|
|
|
{
|
2018-02-23 18:43:14 +03:00
|
|
|
block_number_t bmt_block_nr, s1b_block_nr;
|
2018-02-23 18:43:09 +03:00
|
|
|
EckdLdlIpl1 *ipl1 = (void *)sec;
|
2014-08-29 13:01:40 +04:00
|
|
|
|
|
|
|
if (mode != ECKD_LDL_UNLABELED) {
|
|
|
|
print_eckd_ldl_msg(mode);
|
|
|
|
}
|
2014-05-19 22:14:41 +04:00
|
|
|
|
|
|
|
/* DO NOT read BootMap pointer (only one, xECKD) at block #2 */
|
|
|
|
|
|
|
|
memset(sec, FREE_SPACE_FILLER, sizeof(sec));
|
2024-10-20 04:29:42 +03:00
|
|
|
if (virtio_read(0, sec)) {
|
|
|
|
puts("Cannot read block 0 to grab boot info.");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2014-08-29 13:01:40 +04:00
|
|
|
if (mode == ECKD_LDL_UNLABELED) {
|
2018-02-23 18:43:09 +03:00
|
|
|
if (!magic_match(ipl1->bip.magic, ZIPL_MAGIC)) {
|
2024-10-20 04:29:42 +03:00
|
|
|
return 0; /* not applicable layout */
|
2014-08-29 13:01:40 +04:00
|
|
|
}
|
2024-10-20 04:29:36 +03:00
|
|
|
puts("unlabeled LDL.");
|
2014-08-29 13:01:40 +04:00
|
|
|
}
|
2018-02-23 18:43:09 +03:00
|
|
|
verify_boot_info(&ipl1->bip);
|
2014-05-19 22:14:41 +04:00
|
|
|
|
2018-02-23 18:43:07 +03:00
|
|
|
/* save pointer to Boot Map Table */
|
2018-02-23 18:43:09 +03:00
|
|
|
bmt_block_nr = eckd_block_num(&ipl1->bip.bp.ipl.bm_ptr.eckd.bptr.chs);
|
2018-02-23 18:43:07 +03:00
|
|
|
|
2018-02-23 18:43:14 +03:00
|
|
|
/* save pointer to Stage1b Data */
|
|
|
|
s1b_block_nr = eckd_block_num(&ipl1->stage1.seek[0].chs);
|
|
|
|
|
2024-10-20 04:29:42 +03:00
|
|
|
return run_eckd_boot_script(bmt_block_nr, s1b_block_nr);
|
2014-05-19 22:14:41 +04:00
|
|
|
}
|
|
|
|
|
2023-02-21 20:45:48 +03:00
|
|
|
static block_number_t eckd_find_bmt(ExtEckdBlockPtr *ptr)
|
|
|
|
{
|
|
|
|
block_number_t blockno;
|
|
|
|
uint8_t tmp_sec[MAX_SECTOR_SIZE];
|
|
|
|
BootRecord *br;
|
|
|
|
|
|
|
|
blockno = gen_eckd_block_num(ptr, 0);
|
2024-10-20 04:29:42 +03:00
|
|
|
if (virtio_read(blockno, tmp_sec)) {
|
|
|
|
puts("Cannot read boot record");
|
|
|
|
return ERROR_BLOCK_NR;
|
|
|
|
}
|
2023-02-21 20:45:48 +03:00
|
|
|
br = (BootRecord *)tmp_sec;
|
|
|
|
if (!magic_match(br->magic, ZIPL_MAGIC)) {
|
|
|
|
/* If the boot record is invalid, return and try CCW-IPL instead */
|
|
|
|
return NULL_BLOCK_NR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return gen_eckd_block_num(&br->pgt.xeckd, 1);
|
|
|
|
}
|
|
|
|
|
2014-08-29 13:01:38 +04:00
|
|
|
static void print_eckd_msg(void)
|
|
|
|
{
|
|
|
|
char msg[] = "Using ECKD scheme (block size *****), ";
|
|
|
|
char *p = &msg[34], *q = &msg[30];
|
|
|
|
int n = virtio_get_block_size();
|
|
|
|
|
|
|
|
/* Fill in the block size and show up the message */
|
|
|
|
if (n > 0 && n <= 99999) {
|
|
|
|
while (n) {
|
|
|
|
*p-- = '0' + (n % 10);
|
|
|
|
n /= 10;
|
|
|
|
}
|
|
|
|
while (p >= q) {
|
|
|
|
*p-- = ' ';
|
|
|
|
}
|
|
|
|
}
|
2024-10-20 04:29:36 +03:00
|
|
|
printf("%s", msg);
|
2014-08-29 13:01:38 +04:00
|
|
|
}
|
|
|
|
|
2024-10-20 04:29:42 +03:00
|
|
|
static int ipl_eckd(void)
|
2015-10-30 19:28:48 +03:00
|
|
|
{
|
2023-02-21 20:45:48 +03:00
|
|
|
IplVolumeLabel *vlbl = (void *)sec;
|
|
|
|
LDL_VTOC *vtoc = (void *)sec;
|
|
|
|
block_number_t ldipl_bmt; /* Boot Map Table for List-Directed IPL */
|
2015-10-30 19:28:48 +03:00
|
|
|
|
|
|
|
print_eckd_msg();
|
|
|
|
|
2023-02-21 20:45:48 +03:00
|
|
|
/* Block 2 can contain either the CDL VOL1 label or the LDL VTOC */
|
2015-10-30 19:28:48 +03:00
|
|
|
memset(sec, FREE_SPACE_FILLER, sizeof(sec));
|
2024-10-20 04:29:42 +03:00
|
|
|
if (virtio_read(2, vlbl)) {
|
|
|
|
puts("Cannot read block 2");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2015-10-30 19:28:48 +03:00
|
|
|
|
2023-02-21 20:45:48 +03:00
|
|
|
/*
|
|
|
|
* First check for a list-directed-format pointer which would
|
|
|
|
* supersede the CCW pointer.
|
|
|
|
*/
|
|
|
|
if (eckd_valid_address((ExtEckdBlockPtr *)&vlbl->f.br, 0)) {
|
|
|
|
ldipl_bmt = eckd_find_bmt((ExtEckdBlockPtr *)&vlbl->f.br);
|
2024-10-20 04:29:42 +03:00
|
|
|
switch (ldipl_bmt) {
|
|
|
|
case ERROR_BLOCK_NR:
|
|
|
|
return -EIO;
|
|
|
|
case NULL_BLOCK_NR:
|
|
|
|
break; /* Invalid BMT but the device may still boot with CCW-IPL */
|
|
|
|
default:
|
2024-10-20 04:29:36 +03:00
|
|
|
puts("List-Directed");
|
2024-10-20 04:29:42 +03:00
|
|
|
/*
|
|
|
|
* LD-IPL does not use the S1B bock, just make it NULL_BLOCK_NR.
|
|
|
|
* In some failure cases retry IPL before aborting.
|
|
|
|
*/
|
|
|
|
if (run_eckd_boot_script(ldipl_bmt, NULL_BLOCK_NR)) {
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
/* Non-fatal error, retry as CCW-IPL */
|
2024-10-20 04:29:36 +03:00
|
|
|
printf("Retrying IPL ");
|
2023-02-21 20:45:48 +03:00
|
|
|
print_eckd_msg();
|
|
|
|
}
|
|
|
|
memset(sec, FREE_SPACE_FILLER, sizeof(sec));
|
2024-10-20 04:29:42 +03:00
|
|
|
if (virtio_read(2, vtoc)) {
|
|
|
|
puts("Cannot read block 2");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2015-10-30 19:28:48 +03:00
|
|
|
}
|
|
|
|
|
2023-02-21 20:45:48 +03:00
|
|
|
/* Not list-directed */
|
|
|
|
if (magic_match(vtoc->magic, VOL1_MAGIC)) {
|
2024-10-20 04:29:42 +03:00
|
|
|
if (ipl_eckd_cdl()) {
|
|
|
|
return -1;
|
|
|
|
}
|
2023-02-21 20:45:48 +03:00
|
|
|
}
|
2015-10-30 19:28:48 +03:00
|
|
|
|
2023-02-21 20:45:48 +03:00
|
|
|
if (magic_match(vtoc->magic, CMS1_MAGIC)) {
|
2024-10-20 04:29:42 +03:00
|
|
|
return ipl_eckd_ldl(ECKD_CMS);
|
2015-10-30 19:28:48 +03:00
|
|
|
}
|
2023-02-21 20:45:48 +03:00
|
|
|
if (magic_match(vtoc->magic, LNX1_MAGIC)) {
|
2024-10-20 04:29:42 +03:00
|
|
|
return ipl_eckd_ldl(ECKD_LDL);
|
2015-10-30 19:28:48 +03:00
|
|
|
}
|
|
|
|
|
2024-10-20 04:29:42 +03:00
|
|
|
if (ipl_eckd_ldl(ECKD_LDL_UNLABELED)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-10-30 19:28:48 +03:00
|
|
|
/*
|
|
|
|
* Ok, it is not a LDL by any means.
|
|
|
|
* It still might be a CDL with zero record keys for IPL1 and IPL2
|
|
|
|
*/
|
2024-10-20 04:29:42 +03:00
|
|
|
return ipl_eckd_cdl();
|
2015-10-30 19:28:48 +03:00
|
|
|
}
|
|
|
|
|
2014-05-19 22:12:43 +04:00
|
|
|
/***********************************************************************
|
|
|
|
* IPL a SCSI disk
|
|
|
|
*/
|
2013-04-22 23:02:49 +04:00
|
|
|
|
2024-10-20 04:29:43 +03:00
|
|
|
static int zipl_load_segment(ComponentEntry *entry)
|
2013-04-22 23:02:49 +04:00
|
|
|
{
|
2014-05-19 22:10:27 +04:00
|
|
|
const int max_entries = (MAX_SECTOR_SIZE / sizeof(ScsiBlockPtr));
|
2014-05-19 22:08:54 +04:00
|
|
|
ScsiBlockPtr *bprs = (void *)sec;
|
2014-06-18 16:16:46 +04:00
|
|
|
const int bprs_size = sizeof(sec);
|
2014-05-19 22:09:50 +04:00
|
|
|
block_number_t blockno;
|
2014-05-19 22:12:43 +04:00
|
|
|
uint64_t address;
|
2013-04-22 23:02:49 +04:00
|
|
|
int i;
|
2014-05-19 22:12:43 +04:00
|
|
|
char err_msg[] = "zIPL failed to read BPRS at 0xZZZZZZZZZZZZZZZZ";
|
|
|
|
char *blk_no = &err_msg[30]; /* where to print blockno in (those ZZs) */
|
2013-04-22 23:02:49 +04:00
|
|
|
|
|
|
|
blockno = entry->data.blockno;
|
2020-10-06 12:42:46 +03:00
|
|
|
address = entry->compdat.load_addr;
|
2013-04-22 23:02:49 +04:00
|
|
|
|
|
|
|
debug_print_int("loading segment at block", blockno);
|
|
|
|
debug_print_int("addr", address);
|
|
|
|
|
|
|
|
do {
|
2014-06-18 16:16:46 +04:00
|
|
|
memset(bprs, FREE_SPACE_FILLER, bprs_size);
|
2014-05-19 22:12:43 +04:00
|
|
|
fill_hex_val(blk_no, &blockno, sizeof(blockno));
|
2024-10-20 04:29:43 +03:00
|
|
|
if (virtio_read(blockno, bprs)) {
|
|
|
|
puts(err_msg);
|
|
|
|
return -EIO;
|
|
|
|
}
|
2013-04-22 23:02:49 +04:00
|
|
|
|
|
|
|
for (i = 0;; i++) {
|
2014-05-19 22:12:43 +04:00
|
|
|
uint64_t *cur_desc = (void *)&bprs[i];
|
2013-04-22 23:02:49 +04:00
|
|
|
|
|
|
|
blockno = bprs[i].blockno;
|
2014-05-19 22:05:40 +04:00
|
|
|
if (!blockno) {
|
2013-04-22 23:02:49 +04:00
|
|
|
break;
|
2014-05-19 22:05:40 +04:00
|
|
|
}
|
2013-04-22 23:02:49 +04:00
|
|
|
|
|
|
|
/* we need the updated blockno for the next indirect entry in the
|
|
|
|
chain, but don't want to advance address */
|
2014-05-19 22:05:40 +04:00
|
|
|
if (i == (max_entries - 1)) {
|
2013-04-22 23:02:49 +04:00
|
|
|
break;
|
2014-05-19 22:05:40 +04:00
|
|
|
}
|
2013-04-22 23:02:49 +04:00
|
|
|
|
2014-06-18 16:16:46 +04:00
|
|
|
if (bprs[i].blockct == 0 && unused_space(&bprs[i + 1],
|
2014-05-19 22:08:54 +04:00
|
|
|
sizeof(ScsiBlockPtr))) {
|
2014-06-18 16:16:46 +04:00
|
|
|
/* This is a "continue" pointer.
|
|
|
|
* This ptr is the last one in the current script section.
|
|
|
|
* I.e. the next ptr must point to the unused memory area.
|
|
|
|
* The blockno is not zero, so the upper loop must continue
|
|
|
|
* reading next section of BPRS.
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
}
|
2013-04-22 23:02:49 +04:00
|
|
|
address = virtio_load_direct(cur_desc[0], cur_desc[1], 0,
|
2014-05-19 22:05:40 +04:00
|
|
|
(void *)address);
|
2024-10-20 04:29:43 +03:00
|
|
|
if (!address) {
|
|
|
|
puts("zIPL load segment failed");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2013-04-22 23:02:49 +04:00
|
|
|
}
|
|
|
|
} while (blockno);
|
2024-10-20 04:29:43 +03:00
|
|
|
|
|
|
|
return 0;
|
2013-04-22 23:02:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Run a zipl program */
|
2024-10-20 04:29:43 +03:00
|
|
|
static int zipl_run(ScsiBlockPtr *pte)
|
2013-04-22 23:02:49 +04:00
|
|
|
{
|
2014-05-19 22:08:54 +04:00
|
|
|
ComponentHeader *header;
|
|
|
|
ComponentEntry *entry;
|
2014-05-19 22:10:27 +04:00
|
|
|
uint8_t tmp_sec[MAX_SECTOR_SIZE];
|
2013-04-22 23:02:49 +04:00
|
|
|
|
2024-10-20 04:29:43 +03:00
|
|
|
if (virtio_read(pte->blockno, tmp_sec)) {
|
|
|
|
puts("Cannot read header");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2014-05-19 22:08:54 +04:00
|
|
|
header = (ComponentHeader *)tmp_sec;
|
2013-04-22 23:02:49 +04:00
|
|
|
|
2024-10-20 04:29:43 +03:00
|
|
|
if (!magic_match(tmp_sec, ZIPL_MAGIC)) {
|
|
|
|
puts("No zIPL magic in header");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (header->type != ZIPL_COMP_HEADER_IPL) {
|
|
|
|
puts("Bad header type");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2013-04-22 23:02:49 +04:00
|
|
|
|
|
|
|
dputs("start loading images\n");
|
|
|
|
|
|
|
|
/* Load image(s) into RAM */
|
2014-05-19 22:08:54 +04:00
|
|
|
entry = (ComponentEntry *)(&header[1]);
|
2019-04-29 16:09:41 +03:00
|
|
|
while (entry->component_type == ZIPL_COMP_ENTRY_LOAD ||
|
|
|
|
entry->component_type == ZIPL_COMP_ENTRY_SIGNATURE) {
|
|
|
|
|
|
|
|
/* We don't support secure boot yet, so we skip signature entries */
|
|
|
|
if (entry->component_type == ZIPL_COMP_ENTRY_SIGNATURE) {
|
|
|
|
entry++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-10-20 04:29:43 +03:00
|
|
|
if (zipl_load_segment(entry)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-04-22 23:02:49 +04:00
|
|
|
|
|
|
|
entry++;
|
|
|
|
|
2024-10-20 04:29:43 +03:00
|
|
|
if ((uint8_t *)(&entry[1]) > (tmp_sec + MAX_SECTOR_SIZE)) {
|
|
|
|
puts("Wrong entry value");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2013-04-22 23:02:49 +04:00
|
|
|
}
|
|
|
|
|
2024-10-20 04:29:43 +03:00
|
|
|
if (entry->component_type != ZIPL_COMP_ENTRY_EXEC) {
|
|
|
|
puts("No EXEC entry");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2013-04-22 23:02:49 +04:00
|
|
|
|
|
|
|
/* should not return */
|
2020-10-06 12:42:48 +03:00
|
|
|
write_reset_psw(entry->compdat.load_psw);
|
|
|
|
jump_to_IPL_code(0);
|
2024-10-20 04:29:43 +03:00
|
|
|
return -1;
|
2013-04-22 23:02:49 +04:00
|
|
|
}
|
|
|
|
|
2024-10-20 04:29:43 +03:00
|
|
|
static int ipl_scsi(void)
|
2013-04-22 23:02:49 +04:00
|
|
|
{
|
2014-05-19 22:08:54 +04:00
|
|
|
ScsiMbr *mbr = (void *)sec;
|
2013-04-22 23:02:49 +04:00
|
|
|
int program_table_entries = 0;
|
2018-02-23 18:43:07 +03:00
|
|
|
BootMapTable *prog_table = (void *)sec;
|
2016-04-18 14:45:31 +03:00
|
|
|
unsigned int loadparm = get_loadparm_index();
|
2018-04-16 19:56:10 +03:00
|
|
|
bool valid_entries[MAX_BOOT_ENTRIES] = {false};
|
|
|
|
size_t i;
|
2013-04-22 23:02:49 +04:00
|
|
|
|
2015-10-30 19:28:48 +03:00
|
|
|
/* Grab the MBR */
|
|
|
|
memset(sec, FREE_SPACE_FILLER, sizeof(sec));
|
2024-10-20 04:29:43 +03:00
|
|
|
if (virtio_read(0, mbr)) {
|
|
|
|
puts("Cannot read block 0");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2015-10-30 19:28:48 +03:00
|
|
|
|
|
|
|
if (!magic_match(mbr->magic, ZIPL_MAGIC)) {
|
2024-10-20 04:29:43 +03:00
|
|
|
return 0;
|
2015-10-30 19:28:48 +03:00
|
|
|
}
|
2013-04-22 23:02:49 +04:00
|
|
|
|
2024-10-20 04:29:36 +03:00
|
|
|
puts("Using SCSI scheme.");
|
2015-09-17 13:41:36 +03:00
|
|
|
debug_print_int("MBR Version", mbr->version_id);
|
|
|
|
IPL_check(mbr->version_id == 1,
|
|
|
|
"Unknown MBR layout version, assuming version 1");
|
2018-02-23 18:43:07 +03:00
|
|
|
debug_print_int("program table", mbr->pt.blockno);
|
2024-10-20 04:29:43 +03:00
|
|
|
if (!mbr->pt.blockno) {
|
|
|
|
puts("No Program Table");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2013-04-22 23:02:49 +04:00
|
|
|
|
|
|
|
/* Parse the program table */
|
2024-10-20 04:29:43 +03:00
|
|
|
if (virtio_read(mbr->pt.blockno, sec)) {
|
|
|
|
puts("Error reading Program Table");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
if (!magic_match(sec, ZIPL_MAGIC)) {
|
|
|
|
puts("No zIPL magic in Program Table");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2013-04-22 23:02:49 +04:00
|
|
|
|
2018-04-16 19:56:10 +03:00
|
|
|
for (i = 0; i < MAX_BOOT_ENTRIES; i++) {
|
|
|
|
if (prog_table->entry[i].scsi.blockno) {
|
|
|
|
valid_entries[i] = true;
|
|
|
|
program_table_entries++;
|
2013-04-22 23:02:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
debug_print_int("program table entries", program_table_entries);
|
2024-10-20 04:29:43 +03:00
|
|
|
if (program_table_entries == 0) {
|
|
|
|
puts("Empty Program Table");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2013-04-22 23:02:49 +04:00
|
|
|
|
2018-02-23 18:43:19 +03:00
|
|
|
if (menu_is_enabled_enum()) {
|
2018-04-16 19:56:10 +03:00
|
|
|
loadparm = menu_get_enum_boot_index(valid_entries);
|
2018-02-23 18:43:19 +03:00
|
|
|
}
|
|
|
|
|
2018-02-23 18:43:07 +03:00
|
|
|
debug_print_int("loadparm", loadparm);
|
2024-10-20 04:29:43 +03:00
|
|
|
if (loadparm >= MAX_BOOT_ENTRIES) {
|
|
|
|
puts("loadparm value greater than max number of boot entries allowed");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2018-02-23 18:43:07 +03:00
|
|
|
|
2024-10-20 04:29:43 +03:00
|
|
|
return zipl_run(&prog_table->entry[loadparm].scsi);
|
2013-04-22 23:02:49 +04:00
|
|
|
}
|
2014-05-19 22:12:43 +04:00
|
|
|
|
2015-10-12 18:50:20 +03:00
|
|
|
/***********************************************************************
|
|
|
|
* IPL El Torito ISO9660 image or DVD
|
|
|
|
*/
|
|
|
|
|
|
|
|
static bool is_iso_bc_entry_compatible(IsoBcSection *s)
|
|
|
|
{
|
2015-10-12 18:50:20 +03:00
|
|
|
uint8_t *magic_sec = (uint8_t *)(sec + ISO_SECTOR_SIZE);
|
|
|
|
|
|
|
|
if (s->unused || !s->sector_count) {
|
|
|
|
return false;
|
|
|
|
}
|
2024-10-20 04:29:41 +03:00
|
|
|
if (virtio_read(bswap32(s->load_rba), magic_sec)) {
|
|
|
|
puts("Failed to read image sector 0");
|
|
|
|
return false;
|
|
|
|
}
|
2015-10-12 18:50:20 +03:00
|
|
|
|
|
|
|
/* Checking bytes 8 - 32 for S390 Linux magic */
|
2018-02-23 18:43:10 +03:00
|
|
|
return !memcmp(magic_sec + 8, linux_s390_magic, 24);
|
2015-10-12 18:50:20 +03:00
|
|
|
}
|
|
|
|
|
2015-10-12 18:50:20 +03:00
|
|
|
/* Location of the current sector of the directory */
|
|
|
|
static uint32_t sec_loc[ISO9660_MAX_DIR_DEPTH];
|
|
|
|
/* Offset in the current sector of the directory */
|
|
|
|
static uint32_t sec_offset[ISO9660_MAX_DIR_DEPTH];
|
|
|
|
/* Remained directory space in bytes */
|
|
|
|
static uint32_t dir_rem[ISO9660_MAX_DIR_DEPTH];
|
|
|
|
|
2024-10-20 04:29:41 +03:00
|
|
|
static inline long iso_get_file_size(uint32_t load_rba)
|
2015-10-12 18:50:20 +03:00
|
|
|
{
|
|
|
|
IsoVolDesc *vd = (IsoVolDesc *)sec;
|
|
|
|
IsoDirHdr *cur_record = &vd->vd.primary.rootdir;
|
|
|
|
uint8_t *temp = sec + ISO_SECTOR_SIZE;
|
|
|
|
int level = 0;
|
|
|
|
|
2024-10-20 04:29:41 +03:00
|
|
|
if (virtio_read(ISO_PRIMARY_VD_SECTOR, sec)) {
|
|
|
|
puts("Failed to read ISO primary descriptor");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2015-10-12 18:50:20 +03:00
|
|
|
sec_loc[0] = iso_733_to_u32(cur_record->ext_loc);
|
|
|
|
dir_rem[0] = 0;
|
|
|
|
sec_offset[0] = 0;
|
|
|
|
|
|
|
|
while (level >= 0) {
|
2024-10-20 04:29:41 +03:00
|
|
|
if (sec_offset[level] > ISO_SECTOR_SIZE) {
|
|
|
|
puts("Directory tree structure violation");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2015-10-12 18:50:20 +03:00
|
|
|
|
|
|
|
cur_record = (IsoDirHdr *)(temp + sec_offset[level]);
|
|
|
|
|
|
|
|
if (sec_offset[level] == 0) {
|
2024-10-20 04:29:41 +03:00
|
|
|
if (virtio_read(sec_loc[level], temp)) {
|
|
|
|
puts("Failed to read ISO directory");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2015-10-12 18:50:20 +03:00
|
|
|
if (dir_rem[level] == 0) {
|
|
|
|
/* Skip self and parent records */
|
|
|
|
dir_rem[level] = iso_733_to_u32(cur_record->data_len) -
|
|
|
|
cur_record->dr_len;
|
|
|
|
sec_offset[level] += cur_record->dr_len;
|
|
|
|
|
|
|
|
cur_record = (IsoDirHdr *)(temp + sec_offset[level]);
|
|
|
|
dir_rem[level] -= cur_record->dr_len;
|
|
|
|
sec_offset[level] += cur_record->dr_len;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cur_record->dr_len || sec_offset[level] == ISO_SECTOR_SIZE) {
|
|
|
|
/* Zero-padding and/or the end of current sector */
|
|
|
|
dir_rem[level] -= ISO_SECTOR_SIZE - sec_offset[level];
|
|
|
|
sec_offset[level] = 0;
|
|
|
|
sec_loc[level]++;
|
|
|
|
} else {
|
|
|
|
/* The directory record is valid */
|
|
|
|
if (load_rba == iso_733_to_u32(cur_record->ext_loc)) {
|
|
|
|
return iso_733_to_u32(cur_record->data_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
dir_rem[level] -= cur_record->dr_len;
|
|
|
|
sec_offset[level] += cur_record->dr_len;
|
|
|
|
|
|
|
|
if (cur_record->file_flags & 0x2) {
|
|
|
|
/* Subdirectory */
|
|
|
|
if (level == ISO9660_MAX_DIR_DEPTH - 1) {
|
2024-10-20 04:29:36 +03:00
|
|
|
puts("ISO-9660 directory depth limit exceeded");
|
2015-10-12 18:50:20 +03:00
|
|
|
} else {
|
|
|
|
level++;
|
|
|
|
sec_loc[level] = iso_733_to_u32(cur_record->ext_loc);
|
|
|
|
sec_offset[level] = 0;
|
|
|
|
dir_rem[level] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dir_rem[level] == 0) {
|
|
|
|
/* Nothing remaining */
|
|
|
|
level--;
|
2024-10-20 04:29:41 +03:00
|
|
|
if (virtio_read(sec_loc[level], temp)) {
|
|
|
|
puts("Failed to read ISO directory");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2015-10-12 18:50:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-12 18:50:20 +03:00
|
|
|
static void load_iso_bc_entry(IsoBcSection *load)
|
|
|
|
{
|
|
|
|
IsoBcSection s = *load;
|
|
|
|
/*
|
|
|
|
* According to spec, extent for each file
|
|
|
|
* is padded and ISO_SECTOR_SIZE bytes aligned
|
|
|
|
*/
|
|
|
|
uint32_t blks_to_load = bswap16(s.sector_count) >> ET_SECTOR_SHIFT;
|
2024-10-20 04:29:41 +03:00
|
|
|
long real_size = iso_get_file_size(bswap32(s.load_rba));
|
2015-10-12 18:50:20 +03:00
|
|
|
|
2024-10-20 04:29:41 +03:00
|
|
|
if (real_size > 0) {
|
2015-10-12 18:50:20 +03:00
|
|
|
/* Round up blocks to load */
|
|
|
|
blks_to_load = (real_size + ISO_SECTOR_SIZE - 1) / ISO_SECTOR_SIZE;
|
2024-10-20 04:29:36 +03:00
|
|
|
puts("ISO boot image size verified");
|
2015-10-12 18:50:20 +03:00
|
|
|
} else {
|
2024-10-20 04:29:36 +03:00
|
|
|
puts("ISO boot image size could not be verified");
|
2024-10-20 04:29:41 +03:00
|
|
|
if (real_size < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2015-10-12 18:50:20 +03:00
|
|
|
}
|
2015-10-12 18:50:20 +03:00
|
|
|
|
2024-10-20 04:29:41 +03:00
|
|
|
if (read_iso_boot_image(bswap32(s.load_rba),
|
2015-10-12 18:50:20 +03:00
|
|
|
(void *)((uint64_t)bswap16(s.load_segment)),
|
2024-10-20 04:29:41 +03:00
|
|
|
blks_to_load)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-10-12 18:50:20 +03:00
|
|
|
|
2018-04-20 12:30:42 +03:00
|
|
|
jump_to_low_kernel();
|
2015-10-12 18:50:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t find_iso_bc(void)
|
|
|
|
{
|
|
|
|
IsoVolDesc *vd = (IsoVolDesc *)sec;
|
|
|
|
uint32_t block_num = ISO_PRIMARY_VD_SECTOR;
|
|
|
|
|
|
|
|
if (virtio_read_many(block_num++, sec, 1)) {
|
|
|
|
/* If primary vd cannot be read, there is no boot catalog */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (is_iso_vd_valid(vd) && vd->type != VOL_DESC_TERMINATOR) {
|
|
|
|
if (vd->type == VOL_DESC_TYPE_BOOT) {
|
|
|
|
IsoVdElTorito *et = &vd->vd.boot;
|
|
|
|
|
2018-02-23 18:43:10 +03:00
|
|
|
if (!memcmp(&et->el_torito[0], el_torito_magic, 32)) {
|
2015-10-12 18:50:20 +03:00
|
|
|
return bswap32(et->bc_offset);
|
|
|
|
}
|
|
|
|
}
|
2024-10-20 04:29:41 +03:00
|
|
|
if (virtio_read(block_num++, sec)) {
|
|
|
|
puts("Failed to read ISO volume descriptor");
|
|
|
|
return 0;
|
|
|
|
}
|
2015-10-12 18:50:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-10-20 04:29:41 +03:00
|
|
|
static IsoBcSection *find_iso_bc_entry(uint32_t offset)
|
2015-10-12 18:50:20 +03:00
|
|
|
{
|
|
|
|
IsoBcEntry *e = (IsoBcEntry *)sec;
|
|
|
|
int i;
|
2016-04-18 14:45:36 +03:00
|
|
|
unsigned int loadparm = get_loadparm_index();
|
2015-10-12 18:50:20 +03:00
|
|
|
|
|
|
|
if (!offset) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2024-10-20 04:29:41 +03:00
|
|
|
if (virtio_read(offset, sec)) {
|
|
|
|
puts("Failed to read El Torito boot catalog");
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-10-12 18:50:20 +03:00
|
|
|
|
|
|
|
if (!is_iso_bc_valid(e)) {
|
|
|
|
/* The validation entry is mandatory */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Each entry has 32 bytes size, so one sector cannot contain > 64 entries.
|
|
|
|
* We consider only boot catalogs with no more than 64 entries.
|
|
|
|
*/
|
|
|
|
for (i = 1; i < ISO_BC_ENTRY_PER_SECTOR; i++) {
|
|
|
|
if (e[i].id == ISO_BC_BOOTABLE_SECTION) {
|
|
|
|
if (is_iso_bc_entry_compatible(&e[i].body.sect)) {
|
2016-04-18 14:45:36 +03:00
|
|
|
if (loadparm <= 1) {
|
|
|
|
/* found, default, or unspecified */
|
|
|
|
return &e[i].body.sect;
|
|
|
|
}
|
|
|
|
loadparm--;
|
2015-10-12 18:50:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2024-10-20 04:29:41 +03:00
|
|
|
static int ipl_iso_el_torito(void)
|
2015-10-12 18:50:20 +03:00
|
|
|
{
|
2024-10-20 04:29:41 +03:00
|
|
|
uint32_t offset = find_iso_bc();
|
|
|
|
if (!offset) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
IsoBcSection *s = find_iso_bc_entry(offset);
|
2015-10-12 18:50:20 +03:00
|
|
|
|
|
|
|
if (s) {
|
2024-10-20 04:29:41 +03:00
|
|
|
load_iso_bc_entry(s); /* only return in error */
|
|
|
|
return -1;
|
2015-10-12 18:50:20 +03:00
|
|
|
}
|
2024-10-20 04:29:41 +03:00
|
|
|
|
|
|
|
puts("No suitable boot entry found on ISO-9660 media!");
|
|
|
|
return -EIO;
|
2015-10-12 18:50:20 +03:00
|
|
|
}
|
|
|
|
|
2022-07-04 14:18:54 +03:00
|
|
|
/**
|
|
|
|
* Detect whether we're trying to boot from an .ISO image.
|
|
|
|
* These always have a signature string "CD001" at offset 0x8001.
|
|
|
|
*/
|
|
|
|
static bool has_iso_signature(void)
|
|
|
|
{
|
|
|
|
int blksize = virtio_get_block_size();
|
|
|
|
|
|
|
|
if (!blksize || virtio_read(0x8000 / blksize, sec)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !memcmp("CD001", &sec[1], 5);
|
|
|
|
}
|
|
|
|
|
2014-05-19 22:12:43 +04:00
|
|
|
/***********************************************************************
|
2015-10-30 19:28:48 +03:00
|
|
|
* Bus specific IPL sequences
|
2014-05-19 22:12:43 +04:00
|
|
|
*/
|
|
|
|
|
2024-10-20 04:29:42 +03:00
|
|
|
static int zipl_load_vblk(void)
|
2014-05-19 22:12:43 +04:00
|
|
|
{
|
2022-07-04 14:18:54 +03:00
|
|
|
int blksize = virtio_get_block_size();
|
|
|
|
|
|
|
|
if (blksize == VIRTIO_ISO_BLOCK_SIZE || has_iso_signature()) {
|
|
|
|
if (blksize != VIRTIO_ISO_BLOCK_SIZE) {
|
|
|
|
virtio_assume_iso9660();
|
|
|
|
}
|
2024-10-20 04:29:41 +03:00
|
|
|
if (ipl_iso_el_torito()) {
|
2024-10-20 04:29:42 +03:00
|
|
|
return 0;
|
2024-10-20 04:29:41 +03:00
|
|
|
}
|
2015-10-12 18:50:20 +03:00
|
|
|
}
|
|
|
|
|
2022-07-04 14:18:54 +03:00
|
|
|
if (blksize != VIRTIO_DASD_DEFAULT_BLOCK_SIZE) {
|
2024-10-20 04:29:36 +03:00
|
|
|
puts("Using guessed DASD geometry.");
|
2014-05-19 22:14:41 +04:00
|
|
|
virtio_assume_eckd();
|
|
|
|
}
|
2024-10-20 04:29:42 +03:00
|
|
|
return ipl_eckd();
|
2015-10-30 19:28:48 +03:00
|
|
|
}
|
|
|
|
|
2024-10-20 04:29:42 +03:00
|
|
|
static int zipl_load_vscsi(void)
|
2015-10-30 19:28:48 +03:00
|
|
|
{
|
|
|
|
if (virtio_get_block_size() == VIRTIO_ISO_BLOCK_SIZE) {
|
|
|
|
/* Is it an ISO image in non-CD drive? */
|
2024-10-20 04:29:41 +03:00
|
|
|
if (ipl_iso_el_torito()) {
|
2024-10-20 04:29:42 +03:00
|
|
|
return 0;
|
2024-10-20 04:29:41 +03:00
|
|
|
}
|
2014-05-19 22:14:41 +04:00
|
|
|
}
|
|
|
|
|
2024-10-20 04:29:36 +03:00
|
|
|
puts("Using guessed DASD geometry.");
|
2015-10-30 19:28:48 +03:00
|
|
|
virtio_assume_eckd();
|
2024-10-20 04:29:42 +03:00
|
|
|
return ipl_eckd();
|
2015-10-30 19:28:48 +03:00
|
|
|
}
|
2014-05-19 22:14:41 +04:00
|
|
|
|
2015-10-30 19:28:48 +03:00
|
|
|
/***********************************************************************
|
|
|
|
* IPL starts here
|
|
|
|
*/
|
|
|
|
|
|
|
|
void zipl_load(void)
|
|
|
|
{
|
2016-11-02 00:34:00 +03:00
|
|
|
VDev *vdev = virtio_get_device();
|
|
|
|
|
|
|
|
if (vdev->is_cdrom) {
|
2015-10-30 19:28:48 +03:00
|
|
|
ipl_iso_el_torito();
|
2024-10-20 04:29:46 +03:00
|
|
|
puts("Failed to IPL this ISO image!");
|
|
|
|
return;
|
2014-05-19 22:13:41 +04:00
|
|
|
}
|
2014-05-19 22:12:43 +04:00
|
|
|
|
2016-11-02 00:34:00 +03:00
|
|
|
if (virtio_get_device_type() == VIRTIO_ID_NET) {
|
2024-10-20 04:29:37 +03:00
|
|
|
netmain();
|
2024-10-20 04:29:46 +03:00
|
|
|
puts("Failed to IPL from this network!");
|
|
|
|
return;
|
2016-11-02 00:34:00 +03:00
|
|
|
}
|
|
|
|
|
2024-10-20 04:29:43 +03:00
|
|
|
if (ipl_scsi()) {
|
2024-10-20 04:29:46 +03:00
|
|
|
puts("Failed to IPL from this SCSI device!");
|
|
|
|
return;
|
2024-10-20 04:29:43 +03:00
|
|
|
}
|
2015-10-30 19:28:48 +03:00
|
|
|
|
|
|
|
switch (virtio_get_device_type()) {
|
|
|
|
case VIRTIO_ID_BLOCK:
|
|
|
|
zipl_load_vblk();
|
|
|
|
break;
|
|
|
|
case VIRTIO_ID_SCSI:
|
|
|
|
zipl_load_vscsi();
|
|
|
|
break;
|
|
|
|
default:
|
2024-10-20 04:29:46 +03:00
|
|
|
puts("Unknown IPL device type!");
|
|
|
|
return;
|
2015-10-30 19:28:48 +03:00
|
|
|
}
|
2014-08-29 13:01:40 +04:00
|
|
|
|
2024-10-20 04:29:46 +03:00
|
|
|
puts("zIPL load failed!");
|
2014-05-19 22:12:43 +04:00
|
|
|
}
|