Rizin
unix-like reverse engineering framework and cli tools
bin_sms.c File Reference
#include <rz_bin.h>

Go to the source code of this file.

Classes

struct  gen_hdr
 

Typedefs

typedef struct gen_hdr SMS_Header
 

Functions

static bool check_buffer (RzBuffer *b)
 
static bool load_buffer (RzBinFile *bf, RzBinObject *obj, RzBuffer *buf, Sdb *sdb)
 
static RzBinInfoinfo (RzBinFile *bf)
 

Variables

static ut32 cb = 0
 
RzBinPlugin rz_bin_plugin_sms
 
RZ_API RzLibStruct rizin_plugin
 

Typedef Documentation

◆ SMS_Header

typedef struct gen_hdr SMS_Header

Function Documentation

◆ check_buffer()

static bool check_buffer ( RzBuffer b)
static

Definition at line 17 of file bin_sms.c.

17  {
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 }
static char * signature(RzBinFile *bf, bool json)
Definition: bin_pe.c:117
static ut32 cb
Definition: bin_sms.c:15
uint32_t ut32
uint8_t ut8
Definition: lh5801.h:11
int off
Definition: pal.c:13
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
#define b(i)
Definition: sha256.c:42

References b, cb, off, rz_buf_read_at(), and signature().

Referenced by info(), and load_buffer().

◆ info()

static RzBinInfo* info ( RzBinFile bf)
static

Definition at line 40 of file bin_sms.c.

40  {
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 }
static bool check_buffer(RzBuffer *b)
Definition: bin_sms.c:17
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
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")
#define eprintf(x, y...)
Definition: rlcc.c:7
static ut16 rz_read_le16(const void *src)
Definition: rz_endian.h:206
#define RZ_NEW0(x)
Definition: rz_types.h:284
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 RegionRomSize
Definition: bin_sms.c:12
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

References rz_bin_info_t::arch, rz_bin_info_t::bits, rz_bin_file_t::buf, cb, check_buffer(), gen_hdr::CheckSum, eprintf, rz_bin_info_t::file, rz_bin_file_t::file, free(), rz_bin_info_t::has_va, rz_bin_info_t::machine, NULL, rz_bin_info_t::os, gen_hdr::ProductCode, gen_hdr::RegionRomSize, rz_buf_read_at(), RZ_NEW0, rz_read_le16(), strdup(), rz_bin_info_t::type, and gen_hdr::Version.

◆ load_buffer()

static bool load_buffer ( RzBinFile bf,
RzBinObject obj,
RzBuffer buf,
Sdb sdb 
)
static

Definition at line 36 of file bin_sms.c.

36  {
37  return check_buffer(buf);
38 }
voidpf void * buf
Definition: ioapi.h:138

References check_buffer().

Variable Documentation

◆ cb

ut32 cb = 0
static

Definition at line 15 of file bin_sms.c.

Referenced by check_buffer(), and info().

◆ rizin_plugin

RZ_API RzLibStruct rizin_plugin
Initial value:
= {
.type = RZ_LIB_TYPE_BIN,
}
RzBinPlugin rz_bin_plugin_sms
Definition: bin_sms.c:103
@ RZ_LIB_TYPE_BIN
Definition: rz_lib.h:75
#define RZ_VERSION
Definition: rz_version.h:8
char * version
Definition: rz_bin.h:512

Definition at line 115 of file bin_sms.c.

◆ rz_bin_plugin_sms

RzBinPlugin rz_bin_plugin_sms
Initial value:
= {
.name = "sms",
.desc = "SEGA MasterSystem/GameGear",
.license = "LGPL3",
.load_buffer = &load_buffer,
.check_buffer = &check_buffer,
.info = &info,
.minstrlen = 10,
.strfilter = 'U'
}
static bool load_buffer(RzBinFile *bf, RzBinObject *obj, RzBuffer *buf, Sdb *sdb)
Definition: bin_sms.c:36
static RzBinInfo * info(RzBinFile *bf)
Definition: bin_sms.c:40

Definition at line 103 of file bin_sms.c.