xemu/xemu-xbe.c

137 lines
3.9 KiB
C
Raw Permalink Normal View History

2020-04-26 23:49:45 +00:00
/*
* xemu XBE accessing
*
* Helper functions to get details about the currently running executable.
*
2021-03-03 10:24:24 +00:00
* Copyright (C) 2020-2021 Matt Borgerson
2020-04-26 23:49:45 +00:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2020-04-07 08:10:59 +00:00
#include "xemu-xbe.h"
#include "qemu/osdep.h"
#include "hw/hw.h"
#include "hw/i386/pc.h"
#include "hw/pci/pci.h"
#include "sysemu/hw_accel.h"
#include "cpu.h"
2020-04-07 08:10:59 +00:00
2020-04-26 23:49:45 +00:00
static int virt_to_phys(vaddr vaddr, hwaddr *phys_addr)
2020-04-07 08:10:59 +00:00
{
MemTxAttrs attrs;
CPUState *cs;
hwaddr gpa;
cs = qemu_get_cpu(0);
if (!cs) {
return 1; // No cpu
}
cpu_synchronize_state(cs);
2020-04-26 23:49:45 +00:00
gpa = cpu_get_phys_page_attrs_debug(cs, vaddr & TARGET_PAGE_MASK, &attrs);
2020-04-07 08:10:59 +00:00
if (gpa == -1) {
return 1; // Unmapped
} else {
2020-04-26 23:49:45 +00:00
*phys_addr = gpa + (vaddr & ~TARGET_PAGE_MASK);
2020-04-07 08:10:59 +00:00
}
return 0;
}
2020-04-26 11:46:40 +00:00
static ssize_t virt_dma_memory_read(vaddr vaddr, void *buf, size_t len)
{
size_t num_bytes_read = 0;
while (num_bytes_read < len) {
// Get physical page for this offset
hwaddr phys_addr = 0;
2020-04-27 23:25:08 +00:00
if (virt_to_phys(vaddr + num_bytes_read, &phys_addr) != 0) {
2020-04-26 11:46:40 +00:00
return -1;
}
// Read contents from the page
2020-04-27 23:25:08 +00:00
size_t bytes_remaining_in_page = TARGET_PAGE_SIZE - (phys_addr & ~TARGET_PAGE_MASK);
size_t num_bytes_to_read = MIN(len - num_bytes_read, bytes_remaining_in_page);
2020-04-26 23:49:45 +00:00
2020-04-26 11:46:40 +00:00
// FIXME: Check return value
2020-04-27 23:25:08 +00:00
dma_memory_read(&address_space_memory,
phys_addr,
buf + num_bytes_read,
2023-01-23 07:30:40 +00:00
num_bytes_to_read,
MEMTXATTRS_UNSPECIFIED);
2020-04-26 11:46:40 +00:00
2020-04-26 23:49:45 +00:00
num_bytes_read += num_bytes_to_read;
2020-04-26 11:46:40 +00:00
}
return num_bytes_read;
}
2020-04-26 23:49:45 +00:00
struct xbe *xemu_get_xbe_info(void)
2020-04-07 08:10:59 +00:00
{
2020-04-26 23:49:45 +00:00
vaddr hdr_addr_virt = 0x10000;
2020-04-26 11:46:40 +00:00
2020-04-26 23:49:45 +00:00
static struct xbe xbe = {0};
2020-04-07 08:10:59 +00:00
2020-04-26 23:49:45 +00:00
if (xbe.headers) {
free(xbe.headers);
xbe.headers = NULL;
2020-04-26 11:46:40 +00:00
}
// Get physical page of headers
hwaddr hdr_addr_phys = 0;
2020-04-26 23:49:45 +00:00
if (virt_to_phys(hdr_addr_virt, &hdr_addr_phys) != 0) {
2020-04-07 08:10:59 +00:00
return NULL;
}
2020-04-26 11:46:40 +00:00
// Check `XBEH` signature
uint32_t sig = ldl_le_phys(&address_space_memory, hdr_addr_phys);
2020-04-07 08:10:59 +00:00
if (sig != 0x48454258) {
return NULL;
}
2020-04-26 11:46:40 +00:00
// Determine full length of headers
2020-04-26 23:49:45 +00:00
xbe.headers_len = ldl_le_phys(&address_space_memory,
2020-04-26 11:46:40 +00:00
hdr_addr_phys + offsetof(struct xbe_header, m_sizeof_headers));
if (xbe.headers_len > 8*TARGET_PAGE_SIZE) {
2020-04-26 11:46:40 +00:00
// Headers are unusually large
2020-04-07 08:10:59 +00:00
return NULL;
}
2020-04-26 23:49:45 +00:00
xbe.headers = malloc(xbe.headers_len);
assert(xbe.headers != NULL);
2021-03-03 10:24:24 +00:00
2020-04-26 11:46:40 +00:00
// Read all XBE headers
2020-04-26 23:49:45 +00:00
ssize_t bytes_read = virt_dma_memory_read(hdr_addr_virt,
xbe.headers,
xbe.headers_len);
if (bytes_read != xbe.headers_len) {
2020-04-26 11:46:40 +00:00
// Failed to read headers
2020-04-07 08:10:59 +00:00
return NULL;
}
2020-04-26 11:46:40 +00:00
// Extract XBE header fields
2020-04-26 23:49:45 +00:00
xbe.header = (struct xbe_header *)xbe.headers;
2020-04-07 08:10:59 +00:00
2020-04-26 11:46:40 +00:00
// Get certificate
2020-04-26 23:49:45 +00:00
vaddr cert_addr_virt = ldl_le_p(&xbe.header->m_certificate_addr);
if ((cert_addr_virt == 0) || ((cert_addr_virt + sizeof(struct xbe_certificate)) > (hdr_addr_virt + xbe.headers_len))) {
2020-04-26 11:46:40 +00:00
// Invalid certificate header (a valid certificate is expected for official titles)
return NULL;
2020-04-07 08:10:59 +00:00
}
2020-04-26 23:49:45 +00:00
xbe.cert = (struct xbe_certificate *)(xbe.headers + cert_addr_virt - hdr_addr_virt);
return &xbe;
2020-04-07 08:10:59 +00:00
}