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

Go to the source code of this file.

Classes

struct  gen_hdr
 
struct  gen_vect
 

Typedefs

typedef struct gen_hdr SMD_Header
 
typedef struct gen_vect SMD_Vectors
 

Functions

static ut64 baddr (RzBinFile *bf)
 
static bool check_buffer (RzBuffer *b)
 
static bool load_buffer (RzBinFile *bf, RzBinObject *obj, RzBuffer *b, Sdb *sdb)
 
static RzBinInfoinfo (RzBinFile *bf)
 
static void addsym (RzList *ret, const char *name, ut64 addr)
 
static void showstr (const char *str, const ut8 *s, int len)
 
static RzListsymbols (RzBinFile *bf)
 
static RzListsections (RzBinFile *bf)
 
static RzListentries (RzBinFile *bf)
 
static RzListstrings (RzBinFile *bf)
 

Variables

RzBinPlugin rz_bin_plugin_smd
 
RZ_API RzLibStruct rizin_plugin
 

Typedef Documentation

◆ SMD_Header

typedef struct gen_hdr SMD_Header

◆ SMD_Vectors

typedef struct gen_vect SMD_Vectors

Function Documentation

◆ addsym()

static void addsym ( RzList ret,
const char *  name,
ut64  addr 
)
static

Definition at line 131 of file bin_smd.c.

131  {
133  if (!ptr) {
134  return;
135  }
136  ptr->name = strdup(name ? name : "");
137  ptr->paddr = ptr->vaddr = addr;
138  ptr->size = 0;
139  ptr->ordinal = 0;
140  rz_list_append(ret, ptr);
141 }
RZ_API RZ_BORROW RzListIter * rz_list_append(RZ_NONNULL RzList *list, void *data)
Appends at the end of the list a new element.
Definition: list.c:288
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 RZ_NEW0(x)
Definition: rz_types.h:284
Definition: z80asm.h:102
char * name
Definition: rz_bin.h:675
ut32 ordinal
Definition: rz_bin.h:692
static int addr
Definition: z80asm.c:58

References addr, rz_bin_symbol_t::name, rz_bin_symbol_t::ordinal, rz_bin_symbol_t::paddr, rz_list_append(), RZ_NEW0, rz_bin_symbol_t::size, strdup(), and rz_bin_symbol_t::vaddr.

Referenced by symbols().

◆ baddr()

static ut64 baddr ( RzBinFile bf)
static

Definition at line 95 of file bin_smd.c.

95  {
96  return 0;
97 }

Referenced by sections().

◆ check_buffer()

static bool check_buffer ( RzBuffer b)
static

Definition at line 99 of file bin_smd.c.

99  {
100  if (rz_buf_size(b) > 0x190) {
101  ut8 buf[4];
102  rz_buf_read_at(b, 0x100, buf, sizeof(buf));
103  return !memcmp(buf, "SEGA", 4);
104  }
105  return false;
106 }
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
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
RZ_API ut64 rz_buf_size(RZ_NONNULL RzBuffer *b)
Return the size of the buffer.
Definition: buf.c:1225
#define b(i)
Definition: sha256.c:42

References b, rz_buf_read_at(), and rz_buf_size().

Referenced by load_buffer().

◆ entries()

static RzList* entries ( RzBinFile bf)
static

Definition at line 296 of file bin_smd.c.

296  { // Should be 3 offsets pointed by NMI, RESET, IRQ after mapping && default = 1st CHR
297  RzList *ret;
298  RzBinAddr *ptr = NULL;
299  if (!(ret = rz_list_new())) {
300  return NULL;
301  }
302  if (!(ptr = RZ_NEW0(RzBinAddr))) {
303  return ret;
304  }
305  if (bf->size < sizeof(SMD_Vectors)) {
306  eprintf("ERR: binfile too small!\n");
307  ptr->paddr = ptr->vaddr = 0x100 + sizeof(SMD_Header);
308  rz_list_append(ret, ptr);
309  } else {
310  SMD_Vectors vectors;
311  rz_buf_read_at(bf->buf, 0, (ut8 *)&vectors, sizeof(vectors));
312  ptr->paddr = ptr->vaddr = rz_read_be32(&vectors.Reset);
313  rz_list_append(ret, ptr);
314  }
315  return ret;
316 }
struct gen_hdr SMD_Header
#define NULL
Definition: cris-opc.c:27
RZ_API RZ_OWN RzList * rz_list_new(void)
Returns a new initialized RzList pointer (free method is not initialized)
Definition: list.c:235
#define eprintf(x, y...)
Definition: rlcc.c:7
static ut32 rz_read_be32(const void *src)
Definition: rz_endian.h:87
ut32 Reset
Definition: bin_smd.c:27
ut64 vaddr
Definition: rz_bin.h:186
ut64 paddr
Definition: rz_bin.h:187
RzBuffer * buf
Definition: rz_bin.h:303

References rz_bin_file_t::buf, eprintf, NULL, rz_bin_addr_t::paddr, gen_vect::Reset, rz_buf_read_at(), rz_list_append(), rz_list_new(), RZ_NEW0, rz_read_be32(), rz_bin_file_t::size, and rz_bin_addr_t::vaddr.

◆ info()

static RzBinInfo* info ( RzBinFile bf)
static

Definition at line 112 of file bin_smd.c.

112  {
113  RzBinInfo *ret = NULL;
114  if (!(ret = RZ_NEW0(RzBinInfo))) {
115  return NULL;
116  }
117  ret->file = strdup(bf->file);
118  ret->type = strdup("ROM");
119  ret->machine = strdup("Sega Megadrive");
120  ut8 tmp[32];
121  rz_buf_read_at(bf->buf, 0x100, tmp, sizeof(tmp));
122  ret->bclass = rz_str_ndup((char *)tmp, 32);
123  ret->os = strdup("smd");
124  ret->arch = strdup("m68k");
125  ret->bits = 16;
126  ret->has_va = 1;
127  ret->big_endian = 1;
128  return ret;
129 }
RZ_API char * rz_str_ndup(RZ_NULLABLE const char *ptr, int len)
Create new copy of string ptr limited to size len.
Definition: str.c:1006
char * file
Definition: rz_bin.h:299
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 * bclass
Definition: rz_bin.h:212
char * file
Definition: rz_bin.h:210
char * arch
Definition: rz_bin.h:214
int big_endian
Definition: rz_bin.h:235

References rz_bin_info_t::arch, rz_bin_info_t::bclass, rz_bin_info_t::big_endian, rz_bin_info_t::bits, rz_bin_file_t::buf, rz_bin_info_t::file, rz_bin_file_t::file, rz_bin_info_t::has_va, rz_bin_info_t::machine, NULL, rz_bin_info_t::os, rz_buf_read_at(), RZ_NEW0, rz_str_ndup(), strdup(), autogen_x86imm::tmp, and rz_bin_info_t::type.

◆ load_buffer()

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

Definition at line 108 of file bin_smd.c.

108  {
109  return check_buffer(b);
110 }
static bool check_buffer(RzBuffer *b)
Definition: bin_smd.c:99

References b, and check_buffer().

◆ sections()

static RzList* sections ( RzBinFile bf)
static

Definition at line 255 of file bin_smd.c.

255  {
256  RzList *ret = NULL;
257  if (!(ret = rz_list_new())) {
258  return NULL;
259  }
260  RzBinSection *ptr;
261  if (!(ptr = RZ_NEW0(RzBinSection))) {
262  return ret;
263  }
264  ptr->name = strdup("vtable");
265  ptr->paddr = ptr->vaddr = 0;
266  ptr->size = ptr->vsize = 0x100;
267  ptr->perm = RZ_PERM_R;
268  rz_list_append(ret, ptr);
269 
270  if (!(ptr = RZ_NEW0(RzBinSection))) {
271  return ret;
272  }
273  ptr->name = strdup("header");
274  ptr->paddr = ptr->vaddr = 0x100;
275  ptr->size = ptr->vsize = sizeof(SMD_Header);
276  ptr->perm = RZ_PERM_R;
277  rz_list_append(ret, ptr);
278 
279  if (!(ptr = RZ_NEW0(RzBinSection))) {
280  return ret;
281  }
282  ptr->name = strdup("text");
283  ptr->paddr = ptr->vaddr = 0x100 + sizeof(SMD_Header);
284  {
285  SMD_Header hdr = { { 0 } };
286  rz_buf_read_at(bf->buf, 0x100, (ut8 *)&hdr, sizeof(hdr));
288  ptr->vaddr += baddr;
289  }
290  ptr->size = ptr->vsize = rz_buf_size(bf->buf) - ptr->paddr;
291  ptr->perm = RZ_PERM_RX;
292  rz_list_append(ret, ptr);
293  return ret;
294 }
static ut64 baddr(RzBinFile *bf)
Definition: bin_smd.c:95
#define RZ_PERM_R
Definition: rz_types.h:93
#define RZ_PERM_RX
Definition: rz_types.h:97
Definition: bin_smd.c:6
ut32 RomStart
Definition: bin_smd.c:13
char * name
Definition: rz_bin.h:619
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References baddr(), rz_bin_file_t::buf, rz_bin_section_t::name, NULL, rz_bin_section_t::paddr, rz_bin_section_t::perm, gen_hdr::RomStart, rz_buf_read_at(), rz_buf_size(), rz_list_append(), rz_list_new(), RZ_NEW0, RZ_PERM_R, RZ_PERM_RX, rz_read_be32(), rz_bin_section_t::size, strdup(), ut64(), rz_bin_section_t::vaddr, and rz_bin_section_t::vsize.

◆ showstr()

static void showstr ( const char *  str,
const ut8 s,
int  len 
)
static

Definition at line 143 of file bin_smd.c.

143  {
144  char *msg = rz_str_ndup((const char *)s, len);
145  eprintf("%s: %s\n", str, msg);
146  free(msg);
147 }
size_t len
Definition: 6502dis.c:15
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
static RzSocket * s
Definition: rtr.c:28
static struct sockaddr static addrlen static backlog const void msg
Definition: sfsocketcall.h:119

References eprintf, free(), len, msg, rz_str_ndup(), s, and cmd_descs_generate::str.

Referenced by symbols().

◆ strings()

static RzList* strings ( RzBinFile bf)
static

Definition at line 318 of file bin_smd.c.

318  {
319  return rz_bin_file_strings(bf, 0, false);
320 }
RZ_API RZ_OWN RzList * rz_bin_file_strings(RZ_NONNULL RzBinFile *bf, size_t min_length, bool raw_strings)
Generates a RzList struct containing RzBinString from a given RzBinFile.
Definition: bfile_string.c:325

References rz_bin_file_strings().

◆ symbols()

static RzList* symbols ( RzBinFile bf)
static

Definition at line 149 of file bin_smd.c.

149  {
150  RzList *ret = NULL;
151  const char *name = NULL;
152  int i;
153 
154  if (!(ret = rz_list_newf((RzListFree)rz_bin_symbol_free))) {
155  return NULL;
156  }
157  SMD_Header hdr;
158  int left = rz_buf_read_at(bf->buf, 0x100, (ut8 *)&hdr, sizeof(hdr));
159  if (left < sizeof(SMD_Header)) {
160  return NULL;
161  }
162  // TODO: store all this stuff in SDB
163  addsym(ret, "rom_start", rz_read_be32(&hdr.RomStart));
164  addsym(ret, "rom_end", rz_read_be32(&hdr.RomEnd));
165  addsym(ret, "ram_start", rz_read_be32(&hdr.RamStart));
166  addsym(ret, "ram_end", rz_read_be32(&hdr.RamEnd));
167  showstr("Copyright", hdr.CopyRights, 32);
168  showstr("DomesticName", hdr.DomesticName, 48);
169  showstr("OverseasName", hdr.OverseasName, 48);
170  showstr("ProductCode", hdr.ProductCode, 14);
171  eprintf("Checksum: 0x%04x\n", (ut32)hdr.CheckSum);
172  showstr("Peripherials", hdr.Peripherials, 16);
173  showstr("SramCode", hdr.SramCode, 12);
174  showstr("ModemCode", hdr.ModemCode, 12);
175  showstr("CountryCode", hdr.CountryCode, 16);
176  ut32 vtable[64];
177  rz_buf_read_at(bf->buf, 0, (ut8 *)&vtable, sizeof(ut32) * 64);
178  /* parse vtable */
179  for (i = 0; i < 64; i++) {
180  switch (i) {
181  case 0: name = "SSP"; break;
182  case 1: name = "Reset"; break;
183  case 2: name = "BusErr"; break;
184  case 3: name = "AdrErr"; break;
185  case 4: name = "InvOpCode"; break;
186  case 5: name = "DivBy0"; break;
187  case 6: name = "Check"; break;
188  case 7: name = "TrapV"; break;
189  case 8: name = "GPF"; break;
190  case 9: name = "Trace"; break;
191  case 10: name = "Reserv0"; break;
192  case 11: name = "Reserv1"; break;
193  case 12: name = "Reserv2"; break;
194  case 13: name = "Reserv3"; break;
195  case 14: name = "Reserv4"; break;
196  case 15: name = "BadInt"; break;
197  case 16: name = "Reserv10"; break;
198  case 17: name = "Reserv11"; break;
199  case 18: name = "Reserv12"; break;
200  case 19: name = "Reserv13"; break;
201  case 20: name = "Reserv14"; break;
202  case 21: name = "Reserv15"; break;
203  case 22: name = "Reserv16"; break;
204  case 23: name = "Reserv17"; break;
205  case 24: name = "BadIRQ"; break;
206  case 25: name = "IRQ1"; break;
207  case 26: name = "EXT"; break;
208  case 27: name = "IRQ3"; break;
209  case 28: name = "HBLANK"; break;
210  case 29: name = "IRQ5"; break;
211  case 30: name = "VBLANK"; break;
212  case 31: name = "IRQ7"; break;
213  case 32: name = "Trap0"; break;
214  case 33: name = "Trap1"; break;
215  case 34: name = "Trap2"; break;
216  case 35: name = "Trap3"; break;
217  case 36: name = "Trap4"; break;
218  case 37: name = "Trap5"; break;
219  case 38: name = "Trap6"; break;
220  case 39: name = "Trap7"; break;
221  case 40: name = "Trap8"; break;
222  case 41: name = "Trap9"; break;
223  case 42: name = "Trap10"; break;
224  case 43: name = "Trap11"; break;
225  case 44: name = "Trap12"; break;
226  case 45: name = "Trap13"; break;
227  case 46: name = "Trap14"; break;
228  case 47: name = "Trap15"; break;
229  case 48: name = "Reserv30"; break;
230  case 49: name = "Reserv31"; break;
231  case 50: name = "Reserv32"; break;
232  case 51: name = "Reserv33"; break;
233  case 52: name = "Reserv34"; break;
234  case 53: name = "Reserv35"; break;
235  case 54: name = "Reserv36"; break;
236  case 55: name = "Reserv37"; break;
237  case 56: name = "Reserv38"; break;
238  case 57: name = "Reserv39"; break;
239  case 58: name = "Reserv3A"; break;
240  case 59: name = "Reserv3B"; break;
241  case 60: name = "Reserv3C"; break;
242  case 61: name = "Reserv3D"; break;
243  case 62: name = "Reserv3E"; break;
244  case 63: name = "Reserv3F"; break;
245  default: name = NULL;
246  }
247  if (name && vtable[i]) {
248  ut32 addr = rz_read_be32(&vtable[i]);
249  addsym(ret, name, addr);
250  }
251  }
252  return ret;
253 }
lzma_index ** i
Definition: index.h:629
RZ_API void rz_bin_symbol_free(RzBinSymbol *sym)
Definition: bin.c:175
static void showstr(const char *str, const ut8 *s, int len)
Definition: bin_smd.c:143
static void addsym(RzList *ret, const char *name, ut64 addr)
Definition: bin_smd.c:131
uint32_t ut32
RZ_API RZ_OWN RzList * rz_list_newf(RzListFree f)
Returns a new initialized RzList pointer and sets the free method.
Definition: list.c:248
void(* RzListFree)(void *ptr)
Definition: rz_list.h:11
ut8 ProductCode[14]
Definition: bin_smd.c:10
ut16 CheckSum
Definition: bin_smd.c:11
ut8 ModemCode[12]
Definition: bin_smd.c:18
ut8 DomesticName[48]
Definition: bin_smd.c:8
ut8 Peripherials[16]
Definition: bin_smd.c:12
ut8 CopyRights[32]
Definition: bin_smd.c:7
ut32 RomEnd
Definition: bin_smd.c:14
ut8 CountryCode[16]
Definition: bin_smd.c:20
ut32 RamStart
Definition: bin_smd.c:15
ut8 SramCode[12]
Definition: bin_smd.c:17
ut8 OverseasName[48]
Definition: bin_smd.c:9
ut32 RamEnd
Definition: bin_smd.c:16

References addr, addsym(), rz_bin_file_t::buf, gen_hdr::CheckSum, gen_hdr::CopyRights, gen_hdr::CountryCode, gen_hdr::DomesticName, eprintf, i, gen_hdr::ModemCode, NULL, gen_hdr::OverseasName, gen_hdr::Peripherials, gen_hdr::ProductCode, gen_hdr::RamEnd, gen_hdr::RamStart, gen_hdr::RomEnd, gen_hdr::RomStart, rz_bin_symbol_free(), rz_buf_read_at(), rz_list_newf(), rz_read_be32(), showstr(), and gen_hdr::SramCode.

Variable Documentation

◆ rizin_plugin

RZ_API RzLibStruct rizin_plugin
Initial value:
= {
.type = RZ_LIB_TYPE_BIN,
}
RzBinPlugin rz_bin_plugin_smd
Definition: bin_smd.c:322
@ 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 340 of file bin_smd.c.

◆ rz_bin_plugin_smd

RzBinPlugin rz_bin_plugin_smd
Initial value:
= {
.name = "smd",
.desc = "SEGA Genesis/Megadrive",
.license = "LGPL3",
.load_buffer = &load_buffer,
.check_buffer = &check_buffer,
.baddr = &baddr,
.entries = &entries,
.sections = &sections,
.symbols = &symbols,
.strings = &strings,
.info = &info,
.minstrlen = 10,
.strfilter = 'U'
}
RZ_API RZ_OWN RzList * rz_bin_maps_of_file_sections(RZ_NONNULL RzBinFile *binfile)
Create a list of RzBinMap from RzBinSections queried from the given file.
Definition: bin.c:1040
static bool load_buffer(RzBinFile *bf, RzBinObject *obj, RzBuffer *b, Sdb *sdb)
Definition: bin_smd.c:108
static RzList * symbols(RzBinFile *bf)
Definition: bin_smd.c:149
static RzList * strings(RzBinFile *bf)
Definition: bin_smd.c:318
static RzBinInfo * info(RzBinFile *bf)
Definition: bin_smd.c:112
static RzList * entries(RzBinFile *bf)
Definition: bin_smd.c:296
static RzList * sections(RzBinFile *bf)
Definition: bin_smd.c:255

Definition at line 322 of file bin_smd.c.