Add GameBoy support in RBin

- Can identify rom, romtype (mbc,sram, ...), arch, entrypoint and 2 rombanks
This commit is contained in:
pancake 2013-11-22 09:45:49 +01:00
parent ba3065fb99
commit fb654160e9
6 changed files with 302 additions and 1 deletions

144
libr/bin/format/nin/nin.h Normal file
View File

@ -0,0 +1,144 @@
/* radare - GPL3 - 2013 condret@runas-racer.com */
#include <r_types.h>
#include <string.h>
enum{
GB_SGB = 3
,GB_GBC = 0x80
};
enum{
GB_ROM
,GB_ROM_MBC1
,GB_ROM_MBC1_RAM
,GB_ROM_MBC1_RAM_BAT
,GB_ROM_MBC2 = 0x5
,GB_ROM_MBC2_BAT
,GB_ROM_RAM = 0x8
,GB_ROM_RAM_BAT
,GB_ROM_MMM0 = 0xb
,GB_ROM_MMM0_SRAM
,GB_ROM_MMM0_SRAM_BAT
,GB_ROM_MBC3_TIMER_BAT = 0xf
,GB_ROM_MBC3_TIMER_RAM_BAT
,GB_ROM_MBC3
,GB_ROM_MBC3_RAM
,GB_ROM_MBC3_RAM_BAT
,GB_ROM_MBC5 = 0x19
,GB_ROM_MBC5_RAM
,GB_ROM_MBC5_RAM_BAT
,GB_ROM_MBC5_RMBL
,GB_ROM_MBC5_RMBL_SRAM
,GB_ROM_MBC5_RMBL_SRAM_BAT
,GB_CAM
,GB_TAMA5 = 0xfd
,GB_HUC3
,GB_HUC1
};
enum{
GB_ROM_BANKS_2
,GB_ROM_BANKS_4
,GB_ROM_BANKS_8
,GB_ROM_BANKS_16
,GB_ROM_BANKS_32
,GB_ROM_BANKS_64
,GB_ROM_BANKS_128
,GB_ROM_BANKS_72 = 0x52
,GB_ROM_BANKS_80
,GB_ROM_BANKS_96
};
enum{
GB_NO_RAM
,GB_RAM_2
,GB_RAM_8
,GB_RAM_32
,GB_RAM_128
};
const ut8 lic[]={
0xce, 0xed, 0x66, 0x66, 0xcc, 0x0d, 0x00, 0x0b, 0x03, 0x73, 0x00,
0x83, 0x00, 0x0c, 0x00, 0x0d, 0x00, 0x08, 0x11, 0x1f, 0x88, 0x89,
0x00, 0x0e, 0xdc, 0xcc, 0x6e, 0xe6, 0xdd, 0xdd, 0xd9, 0x99, 0xbb,
0xbb, 0x67, 0x63, 0x6e, 0x0e, 0xec, 0xcc, 0xdd, 0xdc, 0x99, 0x9f,
0xbb, 0xb9, 0x33, 0x3e};
const char *gb_card_type_str[]={
"ROM",
"ROM+MBC1",
"ROM+MBC1+RAM",
"ROM+MBC1+RAM+BAT",
"XXX",
"ROM+MBC2",
"ROM+MBC2+BAT",
"XXX",
"ROM+RAM",
"ROM+RAM+BAT",
"XXX",
"ROM+MMM0",
"ROM+MMM0+SRAM",
"ROM+MMM0+SRAM+BAT",
"XXX",
"ROM+MBC3+TIMER+BAT",
"ROM+MBC3+TIMER+RAM+BAT",
"ROM+MBC3",
"ROM+MBC3+RAM",
"ROM+MBC3+RAM+BAT",
"TAMA5",
"HUC3",
"HUC1",
"XXX", //mbc4?
"XXX",
"XXX",
"ROM+MBC5",
"ROM+MBC5+RAM",
"ROM+MBC5+RAM+BAT",
"ROM+MBC5+RUMBLE",
"ROM+MBC5+RUMBLE+SRAM",
"ROM+MBC5+RUMBLE+SRAM+BAT",
"CAM"
};
void gb_add_cardtype(char *type, ut8 cardcode){
strcat(type,"\ncard\t");
switch(cardcode){
case GB_TAMA5:
case GB_HUC3:
case GB_HUC1:
strcat(type,gb_card_type_str[cardcode-240]);
break;
case 0x15:
case 0x16:
case 0x17:
strcat(type,"XXX");
break;
default:
if(cardcode>GB_CAM){
strcat(type,"XXX");
return;
}
strcat(type,gb_card_type_str[cardcode]);
break;
}
}
void gb_get_gbtype(char *type, ut8 foo, ut8 bar){
if(foo==GB_SGB)
{
strcpy (type, "SuperGameboy-Rom");
}
else
{
if(bar==GB_GBC)
{
strcpy (type, "GameboyColor-Rom");
}
else
{
strcpy (type, "Gameboy-Rom");
}
}
}

View File

@ -8,7 +8,7 @@ foo: all
ALL_TARGETS=
FORMATS=any.mk elf.mk elf64.mk pe.mk pe64.mk te.mk mach0.mk
FORMATS+=bios.mk mach064.mk fatmach0.mk dyldcache.mk java.mk
FORMATS+=dex.mk fs.mk
FORMATS+=dex.mk fs.mk ningb.mk
include $(FORMATS)
all: ${ALL_TARGETS}

146
libr/bin/p/bin_ningb.c Normal file
View File

@ -0,0 +1,146 @@
/* radare - GPL - 2013 - condret@runas-racer.com */
#include <r_types.h>
#include <r_util.h>
#include <r_lib.h>
#include <r_bin.h>
#include <string.h>
#include "../format/nin/nin.h"
static int check(RBinArch *arch);
static int load(RBinArch *arch) {
if(check(arch)) return R_TRUE;
return R_FALSE;
}
static int destroy(RBinArch *arch) {
r_buf_free (arch->buf);
arch->buf = NULL;
return R_TRUE;
}
static ut64 baddr(RBinArch *arch) {
return 0LL;
}
static RList* entries(RBinArch *arch) {
RList *ret;
RBinAddr *ptr = NULL;
ut8 init_jmp[4];
r_buf_read_at(arch->buf,0x100,init_jmp,4);
if (!(ret = r_list_new ()))
return NULL;
ret->free = free;
if (!(ptr = R_NEW (RBinAddr)))
return ret;
memset (ptr, '\0', sizeof (RBinAddr));
if(!init_jmp[1]==0xc3){ /* Function for this? */
ptr->offset = ptr->rva = 0x100;
}
else
{
ptr->offset = ptr->rva = init_jmp[3]*0x100+init_jmp[2];
}
r_list_append (ret, ptr);
return ret;
}
static RList* sections(RBinArch *arch){
ut8 banks;
RList *ret = NULL;
RBinSection *rombank0 = NULL;
if(!(ret = r_list_new()))
return NULL;
ret->free = free;
rombank0 = R_NEW0 (RBinSection);
strncpy(rombank0->name, "rombank0", R_BIN_SIZEOF_STRINGS);
r_buf_read_at(arch->buf,0x147,&banks,1);
rombank0->offset = 0;
rombank0->size = 0x4000;
rombank0->vsize = 0x4000;
rombank0->rva = 0;
rombank0->srwx = r_str_rwx("rx");
r_list_append(ret,rombank0);
if(banks==GB_ROM){ /* TODO(for condret): Function + switch + Ram Banks + Moar Banks!!! */
RBinSection *rombank1 = NULL;
rombank1 = R_NEW0 (RBinSection);
strncpy(rombank1->name, "rombank1", R_BIN_SIZEOF_STRINGS);
rombank1->offset = 0x4000;
rombank1->size = 0x4000;
rombank1->vsize = 0x4000;
rombank1->rva = 0x4000;
rombank1->srwx = r_str_rwx("rx");
r_list_append(ret,rombank1);
}
return ret;
}
static RBinInfo* info(RBinArch *arch) {
ut8 rom_header[76];
RBinInfo *ret = NULL;
if (!(ret = R_NEW (RBinInfo)))
return NULL;
memset (ret, '\0', sizeof (RBinInfo));
ret->lang = NULL;
r_buf_read_at(arch->buf,0x104,rom_header,76);
strncpy (ret->file, &rom_header[48], 16);
gb_get_gbtype(ret->type,rom_header[66],rom_header[63]);
gb_add_cardtype(ret->type,rom_header[67]); // XXX
strncpy (ret->machine, "Gameboy", sizeof (ret->machine)-1);
strncpy (ret->os, "any", sizeof (ret->os)-1);
strcpy (ret->arch, "gb");
ret->has_va = 1;
ret->bits = 8;
ret->big_endian = 0;
ret->dbg_info = 0;
return ret;
}
static int check(RBinArch *arch) {
ut8 lict[48];
r_buf_read_at(arch->buf,0x104,lict,48);
if(!memcmp(lict,lic,48)) return 1;
return 0;
}
struct r_bin_plugin_t r_bin_plugin_ningb = {
.name = "ningb",
.desc = "Gameboy format r_bin plugin",
.init = NULL,
.fini = NULL,
.load = &load,
.destroy = &destroy,
.check = &check,
.baddr = &baddr,
.binsym = NULL,
.entries = &entries,
.sections = &sections,
.symbols = NULL,
.imports = NULL,
.strings = NULL,
.info = &info,
.fields = NULL,
.libs = NULL,
.relocs = NULL,
.meta = NULL,
.create = NULL,
.write = NULL,
};
#ifndef CORELIB
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_BIN,
.data = &r_bin_plugin_ningb
};
#endif

9
libr/bin/p/ningb.mk Normal file
View File

@ -0,0 +1,9 @@
OBJ_NINGB=bin_ningb.o
STATIC_OBJ+=${OBJ_NINGB}
TARGET_NINGB=bin_ningb.${EXT_SO}
ALL_TARGETS+=${TARGET_NINGB}
${TARGET_NINGB}: ${OBJ_NINGB}
${CC} $(call libname,bin_ningb) ${CFLAGS} $(LDFLAGS) ${OBJ_NINGB}

View File

@ -359,6 +359,7 @@ extern RBinPlugin r_bin_plugin_java;
extern RBinPlugin r_bin_plugin_dex;
extern RBinPlugin r_bin_plugin_dummy;
extern RBinPlugin r_bin_plugin_rar;
extern RBinPlugin r_bin_plugin_ningb;
extern RBinXtrPlugin r_bin_xtr_plugin_fatmach0;
extern RBinXtrPlugin r_bin_xtr_plugin_dyldcache;

View File

@ -64,6 +64,7 @@ bin.pe64
bin.te
bin.mach0
bin.mach064
bin.ningb
bin_xtr.fatmach0
bin_xtr.dyldcache
bp.arm