Rizin
unix-like reverse engineering framework and cli tools
bin_sms.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2015-2018 shengdi <github@sheng.my>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_bin.h>
5 
6 typedef struct gen_hdr {
10  ut8 ProductCode[2];
11  ut8 Version; // Low 4 bits version, Top 4 bits ProductCode
12  ut8 RegionRomSize; // Low 4 bits RomSize, Top 4 bits Region
14 
15 static ut32 cb = 0;
16 
17 static bool check_buffer(RzBuffer *b) {
18  ut32 *off, offs[] = { 0x2000, 0x4000, 0x8000, 0x9000, 0 };
19  ut8 signature[8];
20  for (off = (ut32 *)&offs; *off; off++) {
21  rz_buf_read_at(b, *off - 16, (ut8 *)&signature, 8);
22  if (!strncmp((const char *)signature, "TMR SEGA", 8)) {
23  cb = *off - 16;
24  return true; // int)(*off - 16);
25  }
26  if (*off == 0x8000) {
27  if (!strncmp((const char *)signature, "SDSC", 4)) {
28  cb = *off - 16;
29  return true; // (int)(*off - 16);
30  }
31  }
32  }
33  return false;
34 }
35 
36 static bool load_buffer(RzBinFile *bf, RzBinObject *obj, RzBuffer *buf, Sdb *sdb) {
37  return check_buffer(buf);
38 }
39 
40 static RzBinInfo *info(RzBinFile *bf) {
41  RzBinInfo *ret = RZ_NEW0(RzBinInfo);
42  if (!ret || !bf || !bf->buf) {
43  free(ret);
44  return NULL;
45  }
46  ret->file = strdup(bf->file);
47  ret->type = strdup("ROM");
48  ret->machine = strdup("SEGA MasterSystem");
49  ret->os = strdup("sms");
50  ret->arch = strdup("z80");
51  ret->has_va = 1;
52  ret->bits = 8;
53  if (!check_buffer(bf->buf)) {
54  eprintf("Cannot find magic SEGA copyright\n");
55  free(ret);
56  return NULL;
57  }
58  SMS_Header hdr = { { 0 } };
59  rz_buf_read_at(bf->buf, cb, (ut8 *)&hdr, sizeof(hdr));
60  hdr.CheckSum = rz_read_le16(&hdr.CheckSum);
61 
62  eprintf("Checksum: 0x%04x\n", (ut32)hdr.CheckSum); // use endian safe apis here
63  eprintf("ProductCode: %02d%02X%02X\n", (hdr.Version >> 4), hdr.ProductCode[1],
64  hdr.ProductCode[0]);
65  switch (hdr.RegionRomSize >> 4) {
66  case 3:
67  eprintf("Console: Sega Master System\n");
68  eprintf("Region: Japan\n");
69  break;
70  case 4:
71  eprintf("Console: Sega Master System\n");
72  eprintf("Region: Export\n");
73  break;
74  case 5:
75  eprintf("Console: Game Gear\n");
76  eprintf("Region: Japan\n");
77  break;
78  case 6:
79  eprintf("Console: Game Gear\n");
80  eprintf("Region: Export\n");
81  break;
82  case 7:
83  eprintf("Console: Game Gear\n");
84  eprintf("Region: International\n");
85  break;
86  }
87  int romsize = 0;
88  switch (hdr.RegionRomSize & 0xf) {
89  case 0xa: romsize = 8; break;
90  case 0xb: romsize = 16; break;
91  case 0xc: romsize = 32; break;
92  case 0xd: romsize = 48; break;
93  case 0xe: romsize = 64; break;
94  case 0xf: romsize = 128; break;
95  case 0x0: romsize = 256; break;
96  case 0x1: romsize = 512; break;
97  case 0x2: romsize = 1024; break;
98  }
99  eprintf("RomSize: %dKB\n", romsize);
100  return ret;
101 }
102 
104  .name = "sms",
105  .desc = "SEGA MasterSystem/GameGear",
106  .license = "LGPL3",
107  .load_buffer = &load_buffer,
108  .check_buffer = &check_buffer,
109  .info = &info,
110  .minstrlen = 10,
111  .strfilter = 'U'
112 };
113 
114 #ifndef RZ_PLUGIN_INCORE
117  .data = &rz_bin_plugin_sms,
119 };
120 #endif
static char * signature(RzBinFile *bf, bool json)
Definition: bin_pe.c:117
static bool load_buffer(RzBinFile *bf, RzBinObject *obj, RzBuffer *buf, Sdb *sdb)
Definition: bin_sms.c:36
RZ_API RzLibStruct rizin_plugin
Definition: bin_sms.c:115
static bool check_buffer(RzBuffer *b)
Definition: bin_sms.c:17
static RzBinInfo * info(RzBinFile *bf)
Definition: bin_sms.c:40
struct gen_hdr SMS_Header
static ut32 cb
Definition: bin_sms.c:15
RzBinPlugin rz_bin_plugin_sms
Definition: bin_sms.c:103
#define RZ_API
#define NULL
Definition: cris-opc.c:27
uint16_t ut16
uint32_t ut32
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
int off
Definition: pal.c:13
#define eprintf(x, y...)
Definition: rlcc.c:7
RZ_API st64 rz_buf_read_at(RZ_NONNULL RzBuffer *b, ut64 addr, RZ_NONNULL RZ_OUT ut8 *buf, ut64 len)
Read len bytes of the buffer at the specified address.
Definition: buf.c:1136
static ut16 rz_read_le16(const void *src)
Definition: rz_endian.h:206
@ RZ_LIB_TYPE_BIN
Definition: rz_lib.h:75
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_VERSION
Definition: rz_version.h:8
#define b(i)
Definition: sha256.c:42
Definition: bin_smd.c:6
ut8 ProductCode[14]
Definition: bin_smd.c:10
ut16 CheckSum
Definition: bin_smd.c:11
ut8 Version
Definition: bin_sms.c:11
ut8 ReservedWord[2]
Definition: bin_sms.c:8
ut8 HeaderID[8]
Definition: bin_sms.c:7
ut8 RegionRomSize
Definition: bin_sms.c:12
XX curplugin == o->plugin.
Definition: rz_bin.h:298
char * file
Definition: rz_bin.h:299
RzBuffer * buf
Definition: rz_bin.h:303
int has_va
Definition: rz_bin.h:228
char * type
Definition: rz_bin.h:211
char * os
Definition: rz_bin.h:219
char * machine
Definition: rz_bin.h:216
char * file
Definition: rz_bin.h:210
char * arch
Definition: rz_bin.h:214
char * name
Definition: rz_bin.h:509
char * version
Definition: rz_bin.h:512
Definition: sdb.h:63