changeset 1305:5ceb316c479a

Allow games to be specified in ROM DB via sha1 instead of product ID. Added a new ROM DB memory map device type fixed for emulating simple fixed value copy protection registers. Used those two features to support Ya Se Chuan Shuo via a ROM DB entry.
author Michael Pavone <pavone@retrodev.com>
date Wed, 29 Mar 2017 00:29:44 -0700
parents 5b90d7669eee
children 1e0c63e1cf07
files Makefile hash.c hash.h rom.db romdb.c util.c util.h
diffstat 7 files changed, 177 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Tue Mar 28 09:39:54 2017 -0700
+++ b/Makefile	Wed Mar 29 00:29:44 2017 -0700
@@ -127,7 +127,7 @@
 AUDIOOBJS=ym2612.o psg.o wave.o
 CONFIGOBJS=config.o tern.o util.o
 
-MAINOBJS=blastem.o system.o genesis.o debug.o gdb_remote.o vdp.o render_sdl.o ppm.o io.o romdb.o menu.o xband.o realtec.o $(TERMINAL) $(CONFIGOBJS) gst.o $(M68KOBJS) $(TRANSOBJS) $(AUDIOOBJS)
+MAINOBJS=blastem.o system.o genesis.o debug.o gdb_remote.o vdp.o render_sdl.o ppm.o io.o romdb.o hash.o menu.o xband.o realtec.o $(TERMINAL) $(CONFIGOBJS) gst.o $(M68KOBJS) $(TRANSOBJS) $(AUDIOOBJS)
 
 ifeq ($(CPU),x86_64)
 CFLAGS+=-DX86_64 -m64
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hash.c	Wed Mar 29 00:29:44 2017 -0700
@@ -0,0 +1,101 @@
+#include <stdint.h>
+#include <string.h>
+
+//NOTE: This is only intended for use in file identification
+//Please do not use this in a cryptographic setting as no attempts have been
+//made at avoiding side channel attacks
+
+static uint32_t rotleft(uint32_t val, uint32_t shift)
+{
+	return val << shift | val >> (32-shift);
+}
+
+static void sha1_step(uint32_t *state, uint32_t f, uint32_t k, uint32_t w)
+{
+	uint32_t tmp = rotleft(state[0], 5) + f + state[4] + k + w;
+	state[4] = state[3];
+	state[3] = state[2];
+	state[2] = rotleft(state[1], 30);
+	state[1] = state[0];
+	state[0] = tmp;
+}
+
+static void sha1_chunk(uint8_t *chunk, uint32_t *hash)
+{
+	uint32_t state[5], w[80];
+	memcpy(state, hash, sizeof(state));
+	for (uint32_t src = 0; src < 64; src += 4)
+	{
+		w[src >> 2] = chunk[src] << 24 | chunk[src+1] << 16 | chunk[src+2] << 8 | chunk[src+3];
+	}
+	for (uint32_t cur = 16; cur < 80; cur++)
+	{
+		w[cur] = rotleft(w[cur-3] ^ w[cur-8] ^ w[cur-14] ^ w[cur-16], 1);
+	}
+	for (uint32_t cur = 0; cur < 20; cur++)
+	{
+		sha1_step(state, (state[1] & state[2]) | ((~state[1]) & state[3]), 0x5A827999, w[cur]);
+	}
+	for (uint32_t cur = 20; cur < 40; cur++)
+	{
+		sha1_step(state, state[1] ^ state[2] ^ state[3], 0x6ED9EBA1, w[cur]);
+	}
+	for (uint32_t cur = 40; cur < 60; cur++)
+	{
+		sha1_step(state, (state[1] & state[2]) | (state[1] & state[3]) | (state[2] & state[3]), 0x8F1BBCDC, w[cur]);
+	}
+	for (uint32_t cur = 60; cur < 80; cur++)
+	{
+		sha1_step(state, state[1] ^ state[2] ^ state[3], 0xCA62C1D6, w[cur]);
+	}
+	for (uint32_t i = 0; i < 5; i++)
+	{
+		hash[i] += state[i];
+	}
+}
+
+void sha1(uint8_t *data, uint64_t size, uint8_t *out)
+{
+	uint32_t hash[5] = {0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0};
+	uint8_t last[128];
+	uint32_t last_size = 0;
+	if ((size & 63) != 0) {
+		for (uint32_t src = size - (size & 63); src < size; src++)
+		{
+			last[last_size++] = data[src];
+		}
+	}
+	uint64_t bitsize = size * 8;
+	size -= last_size;
+	last[last_size++] = 0x80;
+	while ((last_size & 63) != 56)
+	{
+		last[last_size++] = 0;
+	}
+	
+	last[last_size++] = bitsize >> 56;
+	last[last_size++] = bitsize >> 48;
+	last[last_size++] = bitsize >> 40;
+	last[last_size++] = bitsize >> 32;
+	last[last_size++] = bitsize >> 24;
+	last[last_size++] = bitsize >> 16;
+	last[last_size++] = bitsize >> 8;
+	last[last_size++] = bitsize;
+	
+	for (uint64_t cur = 0; cur < size; cur += 64)
+	{
+		sha1_chunk(data + cur, hash);
+	}
+	for (uint64_t cur = 0; cur < last_size; cur += 64)
+	{
+		sha1_chunk(last + cur, hash);
+	}
+	for (uint32_t cur = 0; cur < 20; cur += 4)
+	{
+		uint32_t val = hash[cur >> 2];
+		out[cur] = val >> 24;
+		out[cur+1] = val >> 16;
+		out[cur+2] = val >> 8;
+		out[cur+3] = val;
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hash.h	Wed Mar 29 00:29:44 2017 -0700
@@ -0,0 +1,12 @@
+#ifndef HASH_H_
+#define HASH_H_
+
+#include <stdint.h>
+
+//NOTE: This is only intended for use in file identification
+//Please do not use this in a cryptographic setting as no attempts have been
+//made at avoiding side channel attacks
+
+void sha1(uint8_t *data, uint64_t size, uint8_t *out);
+
+#endif //HASH_H_
--- a/rom.db	Tue Mar 28 09:39:54 2017 -0700
+++ b/rom.db	Wed Mar 29 00:29:44 2017 -0700
@@ -549,6 +549,36 @@
 	}
 }
 
+8fe0806427e123717ba20478ab1410c25fa942e6 {
+	name Ya Se Chuan Shuo
+	map {
+		0 {
+			device ROM
+			last 3FFFFF
+		}
+		400000 {
+			device fixed
+			value 6300
+			last 400001
+		}
+		400002 {
+			device fixed
+			value 9800
+			last 400003
+		}
+		400004 {
+			device fixed
+			value C900
+			last 400005
+		}
+		400006 {
+			device fixed
+			value 1800
+			last 400007
+		}
+	}
+}
+
 #This entry is used by the GUI ROM
 BlstMenu {
 	map {
--- a/romdb.c	Tue Mar 28 09:39:54 2017 -0700
+++ b/romdb.c	Wed Mar 29 00:29:44 2017 -0700
@@ -3,6 +3,7 @@
 #include "config.h"
 #include "romdb.h"
 #include "util.h"
+#include "hash.h"
 #include "genesis.h"
 #include "menu.h"
 #include "xband.h"
@@ -856,6 +857,12 @@
 		map->mask = 0xFF;
 		map->write_16 = menu_write_w;
 		map->read_16 = menu_read_w;
+	} else if (!strcmp(dtype, "fixed")) {
+		uint16_t *value =  malloc(2);
+		map->buffer = value;
+		map->mask = 0;
+		map->flags = MMAP_READ;
+		*value = strtol(tern_find_ptr_default(node, "value", "0"), NULL, 16);
 	} else {
 		fatal_error("Invalid device type %s for ROM DB map entry %d with address %s\n", dtype, state->index, key);
 	}
@@ -877,7 +884,15 @@
 
 	}
 	printf("Product ID: %s\n", product_id);
-	tern_node * entry = tern_find_ptr(rom_db, product_id);
+	uint8_t raw_hash[20];
+	sha1(vrom, rom_size, raw_hash);
+	uint8_t hex_hash[41];
+	bin_to_hex(hex_hash, raw_hash, 20);
+	printf("SHA1: %s\n", hex_hash);
+	tern_node * entry = tern_find_ptr(rom_db, hex_hash);
+	if (!entry) {
+		entry = tern_find_ptr(rom_db, product_id);
+	}
 	if (!entry) {
 		puts("Not found in ROM DB, examining header\n");
 		if (xband_detect(rom, rom_size)) {
--- a/util.c	Tue Mar 28 09:39:54 2017 -0700
+++ b/util.c	Wed Mar 29 00:29:44 2017 -0700
@@ -189,6 +189,21 @@
 	return text+1;
 }
 
+void bin_to_hex(uint8_t *output, uint8_t *input, uint64_t size)
+{
+	while (size)
+	{
+		uint8_t digit = *input >> 4;
+		digit += digit > 9 ? 'a' - 0xa : '0';
+		*(output++) = digit;
+		digit = *(input++) & 0xF;
+		digit += digit > 9 ? 'a' - 0xa : '0';
+		*(output++) = digit;
+		size--;
+	}
+	*(output++) = 0;
+}
+
 char is_path_sep(char c)
 {
 #ifdef _WIN32
--- a/util.h	Tue Mar 28 09:39:54 2017 -0700
+++ b/util.h	Wed Mar 29 00:29:44 2017 -0700
@@ -32,6 +32,8 @@
 char * strip_ws(char * text);
 //Inserts a null after the first word, returns a pointer to the second word
 char * split_keyval(char * text);
+//Takes a binary byte buffer and produces a lowercase hex string
+void bin_to_hex(uint8_t *output, uint8_t *input, uint64_t size);
 //Determines whether a character is a valid path separator for the current platform
 char is_path_sep(char c);
 //Determines whether a path is considered an absolute path on the current platform