diff --git a/ui/xemu-hud.cc b/ui/xemu-hud.cc index 9ce5ce9db9..21f11405af 100644 --- a/ui/xemu-hud.cc +++ b/ui/xemu-hud.cc @@ -990,14 +990,11 @@ public: report.gl_renderer = (const char *)glGetString(GL_RENDERER); report.gl_version = (const char *)glGetString(GL_VERSION); report.gl_shading_language_version = (const char *)glGetString(GL_SHADING_LANGUAGE_VERSION); - struct xbe_info *xbe = xemu_get_xbe_info(); + struct xbe *xbe = xemu_get_xbe_info(); if (xbe != NULL) { - report.xbe_timestamp = xbe->timedate; - report.xbe_cert_timestamp = xbe->cert_timedate; - report.xbe_cert_title_id = xbe->cert_title_id; - report.xbe_cert_region = xbe->cert_region; - report.xbe_cert_disc_num = xbe->cert_disc_num; - report.xbe_cert_version = xbe->cert_version; + report.SetXbeData(xbe); + } else { + // FIXME: Show message if XBE could not be identified } } diff --git a/ui/xemu-reporting.cc b/ui/xemu-reporting.cc index 70ba999dfb..a4411612ed 100644 --- a/ui/xemu-reporting.cc +++ b/ui/xemu-reporting.cc @@ -1,9 +1,9 @@ +#include +#include #include #include "xemu-reporting.h" - #define CPPHTTPLIB_OPENSSL_SUPPORT 1 #include "httplib.h" - #include "json.hpp" using json = nlohmann::json; @@ -30,15 +30,9 @@ const std::string &CompatibilityReport::GetSerializedReport() {"gl_renderer", gl_renderer}, {"gl_version", gl_version}, {"gl_shading_language_version", gl_shading_language_version}, - {"memory", memory}, - {"xbe_timestamp", xbe_timestamp}, - {"xbe_cert_timestamp", xbe_cert_timestamp}, - {"xbe_cert_title_id", xbe_cert_title_id}, - {"xbe_cert_region", xbe_cert_region}, - {"xbe_cert_disc_num", xbe_cert_disc_num}, - {"xbe_cert_version", xbe_cert_version}, {"compat_rating", compat_rating}, {"compat_comments", compat_comments}, + {"xbe_headers", xbe_headers}, }; serialized = report.dump(2); return serialized; @@ -74,3 +68,15 @@ void CompatibilityReport::Send() fprintf(stderr, "%d\n", res->status); } + +void CompatibilityReport::SetXbeData(struct xbe *xbe) +{ + assert(xbe != NULL); + assert(xbe->headers != NULL); + assert(xbe->headers_len > 0); + + // base64 encode all XBE headers to be sent with the report + gchar *buf = g_base64_encode(xbe->headers, xbe->headers_len); + xbe_headers = buf; + g_free(buf); +} diff --git a/ui/xemu-reporting.h b/ui/xemu-reporting.h index 9b83df0ff3..f7765cfec6 100644 --- a/ui/xemu-reporting.h +++ b/ui/xemu-reporting.h @@ -25,6 +25,8 @@ #include #include +#include "xemu-xbe.h" + class CompatibilityReport { public: std::string token; @@ -39,15 +41,9 @@ public: std::string gl_renderer; std::string gl_version; std::string gl_shading_language_version; - std::string memory; - uint32_t xbe_timestamp; - uint32_t xbe_cert_timestamp; - uint32_t xbe_cert_title_id; - uint32_t xbe_cert_region; - uint32_t xbe_cert_disc_num; - uint32_t xbe_cert_version; std::string compat_rating; std::string compat_comments; + std::string xbe_headers; std::string serialized; @@ -56,6 +52,7 @@ public: ~CompatibilityReport(); void Send(); const std::string &GetSerializedReport(); + void SetXbeData(struct xbe *xbe); }; #endif diff --git a/xemu-xbe.c b/xemu-xbe.c index 05c1f2ca52..f394f887ca 100644 --- a/xemu-xbe.c +++ b/xemu-xbe.c @@ -1,3 +1,24 @@ +/* + * xemu XBE accessing + * + * Helper functions to get details about the currently running executable. + * + * Copyright (C) 2020 Matt Borgerson + * + * 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 . + */ + #include "xemu-xbe.h" #include "qemu/osdep.h" #include "hw/hw.h" @@ -6,10 +27,7 @@ #include "monitor/hmp-target.h" #include "sysemu/hw_accel.h" -// #include "base64/base64.h" -// #include "base64/base64.c" - -static int virt_to_phys(target_ulong virt_addr, hwaddr *phys_addr) +static int virt_to_phys(vaddr vaddr, hwaddr *phys_addr) { MemTxAttrs attrs; CPUState *cs; @@ -22,11 +40,11 @@ static int virt_to_phys(target_ulong virt_addr, hwaddr *phys_addr) cpu_synchronize_state(cs); - gpa = cpu_get_phys_page_attrs_debug(cs, virt_addr & TARGET_PAGE_MASK, &attrs); + gpa = cpu_get_phys_page_attrs_debug(cs, vaddr & TARGET_PAGE_MASK, &attrs); if (gpa == -1) { return 1; // Unmapped } else { - *phys_addr = gpa + (virt_addr & ~TARGET_PAGE_MASK); + *phys_addr = gpa + (vaddr & ~TARGET_PAGE_MASK); } return 0; @@ -44,31 +62,33 @@ static ssize_t virt_dma_memory_read(vaddr vaddr, void *buf, size_t len) } // Read contents from the page - size_t bytes_in_chunk = (len-num_bytes_read) & (TARGET_PAGE_SIZE-1); - // FIXME: Check return value - dma_memory_read(&address_space_memory, phys_addr, buf + num_bytes_read, bytes_in_chunk); + size_t num_bytes_to_read = MIN(len-num_bytes_read, TARGET_PAGE_SIZE-(phys_addr & ~TARGET_PAGE_MASK)); + num_bytes_to_read = MIN(num_bytes_to_read, 0x100); - num_bytes_read += bytes_in_chunk; + // FIXME: Check return value + dma_memory_read(&address_space_memory, phys_addr, buf + num_bytes_read, num_bytes_to_read); + + num_bytes_read += num_bytes_to_read; + fprintf(stderr, "read %d bytes\n", (int)num_bytes_to_read); } return num_bytes_read; } -// Get current XBE info -struct xbe_info *xemu_get_xbe_info(void) +struct xbe *xemu_get_xbe_info(void) { - vaddr xbe_hdr_addr_virt = 0x10000; + vaddr hdr_addr_virt = 0x10000; - static struct xbe_info xbe_info; + static struct xbe xbe = {0}; - if (xbe_info.headers) { - free(xbe_info.headers); - xbe_info.headers = NULL; + if (xbe.headers) { + free(xbe.headers); + xbe.headers = NULL; } // Get physical page of headers hwaddr hdr_addr_phys = 0; - if (virt_to_phys(xbe_hdr_addr_virt, &hdr_addr_phys) != 0) { + if (virt_to_phys(hdr_addr_virt, &hdr_addr_phys) != 0) { return NULL; } @@ -79,52 +99,35 @@ struct xbe_info *xemu_get_xbe_info(void) } // Determine full length of headers - xbe_info.headers_len = ldl_le_phys(&address_space_memory, + xbe.headers_len = ldl_le_phys(&address_space_memory, hdr_addr_phys + offsetof(struct xbe_header, m_sizeof_headers)); - if (xbe_info.headers_len > 4*TARGET_PAGE_SIZE) { + if (xbe.headers_len > 4*TARGET_PAGE_SIZE) { // Headers are unusually large return NULL; } - xbe_info.headers = malloc(xbe_info.headers_len); - assert(xbe_info.headers != NULL); + xbe.headers = malloc(xbe.headers_len); + assert(xbe.headers != NULL); // Read all XBE headers - ssize_t bytes_read = virt_dma_memory_read(xbe_hdr_addr_virt, - xbe_info.headers, - xbe_info.headers_len); - if (bytes_read != xbe_info.headers_len) { + ssize_t bytes_read = virt_dma_memory_read(hdr_addr_virt, + xbe.headers, + xbe.headers_len); + if (bytes_read != xbe.headers_len) { // Failed to read headers return NULL; } // Extract XBE header fields - xbe_info.xbe_hdr = (struct xbe_header *)xbe_info.headers; - xbe_info.timedate = ldl_le_p(&xbe_info.xbe_hdr->m_timedate); + xbe.header = (struct xbe_header *)xbe.headers; // Get certificate - uint32_t cert_addr_virt = ldl_le_p(&xbe_info.xbe_hdr->m_certificate_addr); - if ((cert_addr_virt == 0) || ((cert_addr_virt + sizeof(struct xbe_certificate)) > (xbe_hdr_addr_virt + xbe_info.headers_len))) { + 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))) { // Invalid certificate header (a valid certificate is expected for official titles) return NULL; } - - // Extract certificate fields - xbe_info.xbe_cert = (struct xbe_certificate *)(xbe_info.headers + cert_addr_virt - xbe_hdr_addr_virt); - xbe_info.cert_timedate = ldl_le_p(&xbe_info.xbe_cert->m_timedate); - xbe_info.cert_title_id = ldl_le_p(&xbe_info.xbe_cert->m_titleid); - xbe_info.cert_version = ldl_le_p(&xbe_info.xbe_cert->m_version); - xbe_info.cert_region = ldl_le_p(&xbe_info.xbe_cert->m_game_region); - xbe_info.cert_disc_num = ldl_le_p(&xbe_info.xbe_cert->m_disk_number); + xbe.cert = (struct xbe_certificate *)(xbe.headers + cert_addr_virt - hdr_addr_virt); -#if 0 - // Dump base64 version of headers - FILE *fd = fopen("dump2.bin", "wb"); - void *b64buf = malloc(xbe_info.headers_len*2); - int enc = base64_encode(xbe_info.headers, xbe_info.headers_len, b64buf); - fwrite(b64buf, 1, enc, fd); - free(b64buf); -#endif - - return &xbe_info; + return &xbe; } diff --git a/xemu-xbe.h b/xemu-xbe.h index bc8104c691..22313c5cc9 100644 --- a/xemu-xbe.h +++ b/xemu-xbe.h @@ -91,22 +91,14 @@ struct xbe_certificate }; #pragma pack() -struct xbe_info { - // Typically accessed fields extracted from heeaders - uint32_t timedate; - uint32_t cert_timedate; - uint32_t cert_title_id; - uint32_t cert_version; - uint32_t cert_region; - uint32_t cert_disc_num; - - // Full XBE headers, copied into a temporary buffer +struct xbe { + // Full XBE headers, copied into an allocated buffer uint8_t *headers; uint32_t headers_len; - // Pointer into `headers` (note: little-endian!) - struct xbe_header *xbe_hdr; - struct xbe_certificate *xbe_cert; + // Pointers into `headers` (note: little-endian!) + struct xbe_header *header; + struct xbe_certificate *cert; }; #ifdef __cplusplus @@ -114,7 +106,7 @@ extern "C" { #endif // Get current XBE info -struct xbe_info *xemu_get_xbe_info(void); +struct xbe *xemu_get_xbe_info(void); #ifdef __cplusplus }