2013-04-23 03:02:49 +08: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "s390-ccw.h"
|
2014-05-20 02:08:54 +08:00
|
|
|
#include "bootmap.h"
|
2014-05-20 02:10:27 +08:00
|
|
|
#include "virtio.h"
|
2013-04-23 03:02:49 +08:00
|
|
|
|
2014-05-20 02:05:40 +08:00
|
|
|
/* #define DEBUG_FALLBACK */
|
2013-04-23 03:02:49 +08:00
|
|
|
|
|
|
|
#ifdef DEBUG_FALLBACK
|
|
|
|
#define dputs(txt) \
|
|
|
|
do { sclp_print("zipl: " txt); } while (0)
|
|
|
|
#else
|
|
|
|
#define dputs(fmt, ...) \
|
|
|
|
do { } while (0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Scratch space */
|
2014-05-20 02:10:27 +08:00
|
|
|
static uint8_t sec[MAX_SECTOR_SIZE]
|
|
|
|
__attribute__((__aligned__(MAX_SECTOR_SIZE)));
|
2013-04-23 03:02:49 +08:00
|
|
|
|
2014-06-18 20:16:45 +08:00
|
|
|
typedef struct ResetInfo {
|
|
|
|
uint32_t ipl_mask;
|
|
|
|
uint32_t ipl_addr;
|
|
|
|
uint32_t ipl_continue;
|
|
|
|
} ResetInfo;
|
|
|
|
|
|
|
|
ResetInfo save;
|
|
|
|
|
|
|
|
static void jump_to_IPL_2(void)
|
|
|
|
{
|
|
|
|
ResetInfo *current = 0;
|
|
|
|
|
|
|
|
void (*ipl)(void) = (void *) (uint64_t) current->ipl_continue;
|
|
|
|
debug_print_addr("set IPL addr to", ipl);
|
|
|
|
|
|
|
|
/* Ensure the guest output starts fresh */
|
|
|
|
sclp_print("\n");
|
|
|
|
|
|
|
|
*current = save;
|
|
|
|
ipl(); /* should not return */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void jump_to_IPL_code(uint64_t address)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The IPL PSW is at address 0. We also must not overwrite the
|
|
|
|
* content of non-BIOS memory after we loaded the guest, so we
|
|
|
|
* save the original content and restore it in jump_to_IPL_2.
|
|
|
|
*/
|
|
|
|
ResetInfo *current = 0;
|
|
|
|
|
|
|
|
save = *current;
|
|
|
|
current->ipl_addr = (uint32_t) (uint64_t) &jump_to_IPL_2;
|
|
|
|
current->ipl_continue = address & 0x7fffffff;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* HACK ALERT.
|
|
|
|
* We use the load normal reset to keep r15 unchanged. jump_to_IPL_2
|
|
|
|
* can then use r15 as its stack pointer.
|
|
|
|
*/
|
|
|
|
asm volatile("lghi 1,1\n\t"
|
|
|
|
"diag 1,1,0x308\n\t"
|
|
|
|
: : : "1", "memory");
|
|
|
|
virtio_panic("\n! IPL returns !\n");
|
|
|
|
}
|
|
|
|
|
2013-04-23 03:02:49 +08:00
|
|
|
/* Check for ZIPL magic. Returns 0 if not matched. */
|
|
|
|
static int zipl_magic(uint8_t *ptr)
|
|
|
|
{
|
2014-05-20 02:05:40 +08:00
|
|
|
uint32_t *p = (void *)ptr;
|
|
|
|
uint32_t *z = (void *)ZIPL_MAGIC;
|
2013-04-23 03:02:49 +08:00
|
|
|
|
|
|
|
if (*p != *z) {
|
|
|
|
debug_print_int("invalid magic", *p);
|
|
|
|
virtio_panic("invalid magic");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-06-18 20:16:46 +08:00
|
|
|
static inline bool unused_space(const void *p, unsigned int size)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const unsigned char *m = p;
|
|
|
|
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
if (m[i] != FREE_SPACE_FILLER) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-20 02:08:54 +08:00
|
|
|
static int zipl_load_segment(ComponentEntry *entry)
|
2013-04-23 03:02:49 +08:00
|
|
|
{
|
2014-05-20 02:10:27 +08:00
|
|
|
const int max_entries = (MAX_SECTOR_SIZE / sizeof(ScsiBlockPtr));
|
2014-05-20 02:08:54 +08:00
|
|
|
ScsiBlockPtr *bprs = (void *)sec;
|
2014-06-18 20:16:46 +08:00
|
|
|
const int bprs_size = sizeof(sec);
|
2013-04-23 03:02:49 +08:00
|
|
|
uint64_t blockno;
|
|
|
|
long address;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
blockno = entry->data.blockno;
|
|
|
|
address = entry->load_address;
|
|
|
|
|
|
|
|
debug_print_int("loading segment at block", blockno);
|
|
|
|
debug_print_int("addr", address);
|
|
|
|
|
|
|
|
do {
|
2014-06-18 20:16:46 +08:00
|
|
|
memset(bprs, FREE_SPACE_FILLER, bprs_size);
|
2013-04-23 03:02:49 +08:00
|
|
|
if (virtio_read(blockno, (uint8_t *)bprs)) {
|
|
|
|
debug_print_int("failed reading bprs at", blockno);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0;; i++) {
|
2014-05-20 02:05:40 +08:00
|
|
|
u64 *cur_desc = (void *)&bprs[i];
|
2013-04-23 03:02:49 +08:00
|
|
|
|
|
|
|
blockno = bprs[i].blockno;
|
2014-05-20 02:05:40 +08:00
|
|
|
if (!blockno) {
|
2013-04-23 03:02:49 +08:00
|
|
|
break;
|
2014-05-20 02:05:40 +08:00
|
|
|
}
|
2013-04-23 03:02:49 +08:00
|
|
|
|
|
|
|
/* we need the updated blockno for the next indirect entry in the
|
|
|
|
chain, but don't want to advance address */
|
2014-05-20 02:05:40 +08:00
|
|
|
if (i == (max_entries - 1)) {
|
2013-04-23 03:02:49 +08:00
|
|
|
break;
|
2014-05-20 02:05:40 +08:00
|
|
|
}
|
2013-04-23 03:02:49 +08:00
|
|
|
|
2014-06-18 20:16:46 +08:00
|
|
|
if (bprs[i].blockct == 0 && unused_space(&bprs[i + 1],
|
2014-05-20 02:08:54 +08:00
|
|
|
sizeof(ScsiBlockPtr))) {
|
2014-06-18 20:16:46 +08: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-23 03:02:49 +08:00
|
|
|
address = virtio_load_direct(cur_desc[0], cur_desc[1], 0,
|
2014-05-20 02:05:40 +08:00
|
|
|
(void *)address);
|
|
|
|
if (address == -1) {
|
2013-04-23 03:02:49 +08:00
|
|
|
goto fail;
|
2014-05-20 02:05:40 +08:00
|
|
|
}
|
2013-04-23 03:02:49 +08:00
|
|
|
}
|
|
|
|
} while (blockno);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
sclp_print("failed loading segment\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Run a zipl program */
|
2014-05-20 02:08:54 +08:00
|
|
|
static int zipl_run(ScsiBlockPtr *pte)
|
2013-04-23 03:02:49 +08:00
|
|
|
{
|
2014-05-20 02:08:54 +08:00
|
|
|
ComponentHeader *header;
|
|
|
|
ComponentEntry *entry;
|
2014-05-20 02:10:27 +08:00
|
|
|
uint8_t tmp_sec[MAX_SECTOR_SIZE];
|
2013-04-23 03:02:49 +08:00
|
|
|
|
|
|
|
virtio_read(pte->blockno, tmp_sec);
|
2014-05-20 02:08:54 +08:00
|
|
|
header = (ComponentHeader *)tmp_sec;
|
2013-04-23 03:02:49 +08:00
|
|
|
|
|
|
|
if (!zipl_magic(tmp_sec)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (header->type != ZIPL_COMP_HEADER_IPL) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
dputs("start loading images\n");
|
|
|
|
|
|
|
|
/* Load image(s) into RAM */
|
2014-05-20 02:08:54 +08:00
|
|
|
entry = (ComponentEntry *)(&header[1]);
|
2013-04-23 03:02:49 +08:00
|
|
|
while (entry->component_type == ZIPL_COMP_ENTRY_LOAD) {
|
|
|
|
if (zipl_load_segment(entry) < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry++;
|
|
|
|
|
2014-05-20 02:10:27 +08:00
|
|
|
if ((uint8_t *)(&entry[1]) > (tmp_sec + MAX_SECTOR_SIZE)) {
|
2013-04-23 03:02:49 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry->component_type != ZIPL_COMP_ENTRY_EXEC) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* should not return */
|
2014-06-18 20:16:45 +08:00
|
|
|
jump_to_IPL_code(entry->load_address);
|
2013-04-23 03:02:49 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
sclp_print("failed running zipl\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int zipl_load(void)
|
|
|
|
{
|
2014-05-20 02:08:54 +08:00
|
|
|
ScsiMbr *mbr = (void *)sec;
|
2013-04-23 03:02:49 +08:00
|
|
|
uint8_t *ns, *ns_end;
|
|
|
|
int program_table_entries = 0;
|
2014-05-20 02:08:54 +08:00
|
|
|
const int pte_len = sizeof(ScsiBlockPtr);
|
|
|
|
ScsiBlockPtr *prog_table_entry;
|
2013-04-23 03:02:49 +08:00
|
|
|
const char *error = "";
|
|
|
|
|
|
|
|
/* Grab the MBR */
|
2014-05-20 02:05:40 +08:00
|
|
|
virtio_read(0, (void *)mbr);
|
2013-04-23 03:02:49 +08:00
|
|
|
|
|
|
|
dputs("checking magic\n");
|
|
|
|
|
|
|
|
if (!zipl_magic(mbr->magic)) {
|
|
|
|
error = "zipl_magic 1";
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug_print_int("program table", mbr->blockptr.blockno);
|
|
|
|
|
|
|
|
/* Parse the program table */
|
|
|
|
if (virtio_read(mbr->blockptr.blockno, sec)) {
|
|
|
|
error = "virtio_read";
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!zipl_magic(sec)) {
|
|
|
|
error = "zipl_magic 2";
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2014-05-20 02:10:27 +08:00
|
|
|
ns_end = sec + virtio_get_block_size();
|
2013-04-23 03:02:49 +08:00
|
|
|
for (ns = (sec + pte_len); (ns + pte_len) < ns_end; ns++) {
|
2014-05-20 02:08:54 +08:00
|
|
|
prog_table_entry = (ScsiBlockPtr *)ns;
|
2013-04-23 03:02:49 +08:00
|
|
|
if (!prog_table_entry->blockno) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
program_table_entries++;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug_print_int("program table entries", program_table_entries);
|
|
|
|
|
|
|
|
if (!program_table_entries) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Run the default entry */
|
|
|
|
|
2014-05-20 02:08:54 +08:00
|
|
|
prog_table_entry = (ScsiBlockPtr *)(sec + pte_len);
|
2013-04-23 03:02:49 +08:00
|
|
|
|
|
|
|
return zipl_run(prog_table_entry);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
sclp_print("failed loading zipl: ");
|
|
|
|
sclp_print(error);
|
|
|
|
sclp_print("\n");
|
|
|
|
return -1;
|
|
|
|
}
|