summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Pavone <pavone@retrodev.com>2017-03-29 00:29:44 -0700
committerMichael Pavone <pavone@retrodev.com>2017-03-29 00:29:44 -0700
commit60a2e7bf23dc0d4746807d565bfabe9a8c9fe417 (patch)
treec06868f5b36beb696d92400de632b496a86f4c80
parentee4dc11142649262163a85aad31775a02031b818 (diff)
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.
-rw-r--r--Makefile2
-rw-r--r--hash.c101
-rw-r--r--hash.h12
-rw-r--r--rom.db30
-rw-r--r--romdb.c17
-rw-r--r--util.c15
-rw-r--r--util.h2
7 files changed, 177 insertions, 2 deletions
diff --git a/Makefile b/Makefile
index 3bd1152..02d87f7 100644
--- a/Makefile
+++ b/Makefile
@@ -127,7 +127,7 @@ Z80OBJS=z80inst.o z80_to_x86.o
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
diff --git a/hash.c b/hash.c
new file mode 100644
index 0000000..efdd8c1
--- /dev/null
+++ b/hash.c
@@ -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;
+ }
+}
diff --git a/hash.h b/hash.h
new file mode 100644
index 0000000..784c4b7
--- /dev/null
+++ b/hash.h
@@ -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_
diff --git a/rom.db b/rom.db
index 8bb2177..223bc3c 100644
--- a/rom.db
+++ b/rom.db
@@ -549,6 +549,36 @@ ACLD012 {
}
}
+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 {
diff --git a/romdb.c b/romdb.c
index cf463fd..48a76a7 100644
--- a/romdb.c
+++ b/romdb.c
@@ -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 @@ void map_iter_fun(char *key, tern_val val, void *data)
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 @@ rom_info configure_rom(tern_node *rom_db, void *vrom, uint32_t rom_size, void *l
}
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)) {
diff --git a/util.c b/util.c
index 06342b3..8bb1184 100644
--- a/util.c
+++ b/util.c
@@ -189,6 +189,21 @@ char * split_keyval(char * text)
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
diff --git a/util.h b/util.h
index 39413e1..92b643a 100644
--- a/util.h
+++ b/util.h
@@ -32,6 +32,8 @@ long file_size(FILE * f);
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