Rizin
unix-like reverse engineering framework and cli tools
bin_ningb.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2013-2017 condret <condr3t@protonmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_types.h>
5 #include <rz_util.h>
6 #include <rz_lib.h>
7 #include <rz_bin.h>
8 #include <string.h>
9 #include "../format/nin/nin.h"
10 
11 static bool check_buffer(RzBuffer *b) {
12  ut8 lict[sizeof(lic)];
13  if (rz_buf_read_at(b, 0x104, lict, sizeof(lict)) == sizeof(lict)) {
14  return !memcmp(lict, lic, sizeof(lict));
15  }
16  return false;
17 }
18 
19 static bool load_buffer(RzBinFile *bf, RzBinObject *obj, RzBuffer *buf, Sdb *sdb) {
20  return check_buffer(buf);
21 }
22 
23 static ut64 baddr(RzBinFile *bf) {
24  return 0LL;
25 }
26 
28  if (type == RZ_BIN_SPECIAL_SYMBOL_MAIN && bf && bf->buf) {
29  ut8 init_jmp[4];
30  RzBinAddr *ret = RZ_NEW0(RzBinAddr);
31  if (!ret) {
32  return NULL;
33  }
34  rz_buf_read_at(bf->buf, 0x100, init_jmp, 4);
35  if (init_jmp[1] == 0xc3) {
36  ret->paddr = ret->vaddr = init_jmp[3] * 0x100 + init_jmp[2];
37  return ret;
38  }
39  free(ret);
40  }
41  return NULL;
42 }
43 
44 static RzList *entries(RzBinFile *bf) {
45  RzList *ret = rz_list_new();
46  RzBinAddr *ptr = NULL;
47 
48  if (bf && bf->buf != NULL) {
49  if (!ret) {
50  return NULL;
51  }
52  ret->free = free;
53  if (!(ptr = RZ_NEW0(RzBinAddr))) {
54  return ret;
55  }
56  ptr->paddr = ptr->vaddr = ptr->hpaddr = 0x100;
57  rz_list_append(ret, ptr);
58  }
59  return ret;
60 }
61 
62 static RzList *sections(RzBinFile *bf) {
63  if (!bf || !bf->buf) {
64  return NULL;
65  }
67  if (!ret) {
68  return NULL;
69  }
70  ut8 bank_id;
71  rz_buf_read_at(bf->buf, 0x148, &bank_id, 1);
72  int banks_count = gb_get_rombanks(bank_id);
73  for (size_t i = 0; i < banks_count; i++) {
75  section->name = rz_str_newf("rombank%02x", (unsigned int)i);
76  section->paddr = i * 0x4000;
77  section->vaddr = i ? (i * 0x10000 - 0xc000) : 0;
78  section->size = section->vsize = 0x4000;
79  section->perm = rz_str_rwx("rx");
80  rz_list_append(ret, section);
81  }
82  return ret;
83 }
84 
85 static RzList *symbols(RzBinFile *bf) {
86  RzList *ret = NULL;
87  RzBinSymbol *ptr[13];
88  int i;
90  return NULL;
91  }
92 
93  for (i = 0; i < 8; i++) {
94  if (!(ptr[i] = RZ_NEW0(RzBinSymbol))) {
95  ret->free(ret);
96  return NULL;
97  }
98  ptr[i]->name = rz_str_newf("rst_%i", i * 8);
99  ptr[i]->paddr = ptr[i]->vaddr = i * 8;
100  ptr[i]->size = 1;
101  ptr[i]->ordinal = i;
102  rz_list_append(ret, ptr[i]);
103  }
104 
105  if (!(ptr[8] = RZ_NEW0(RzBinSymbol))) {
106  return ret;
107  }
108 
109  ptr[8]->name = strdup("Interrupt_Vblank");
110  ptr[8]->paddr = ptr[8]->vaddr = 64;
111  ptr[8]->size = 1;
112  ptr[8]->ordinal = 8;
113  rz_list_append(ret, ptr[8]);
114 
115  if (!(ptr[9] = RZ_NEW0(RzBinSymbol))) {
116  return ret;
117  }
118 
119  ptr[9]->name = strdup("Interrupt_LCDC-Status");
120  ptr[9]->paddr = ptr[9]->vaddr = 72;
121  ptr[9]->size = 1;
122  ptr[9]->ordinal = 9;
123  rz_list_append(ret, ptr[9]);
124 
125  if (!(ptr[10] = RZ_NEW0(RzBinSymbol))) {
126  return ret;
127  }
128 
129  ptr[10]->name = strdup("Interrupt_Timer-Overflow");
130  ptr[10]->paddr = ptr[10]->vaddr = 80;
131  ptr[10]->size = 1;
132  ptr[10]->ordinal = 10;
133  rz_list_append(ret, ptr[10]);
134 
135  if (!(ptr[11] = RZ_NEW0(RzBinSymbol))) {
136  return ret;
137  }
138 
139  ptr[11]->name = strdup("Interrupt_Serial-Transfere");
140  ptr[11]->paddr = ptr[11]->vaddr = 88;
141  ptr[11]->size = 1;
142  ptr[11]->ordinal = 11;
143  rz_list_append(ret, ptr[11]);
144 
145  if (!(ptr[12] = RZ_NEW0(RzBinSymbol))) {
146  return ret;
147  }
148 
149  ptr[12]->name = strdup("Interrupt_Joypad");
150  ptr[12]->paddr = ptr[12]->vaddr = 96;
151  ptr[12]->size = 1;
152  ptr[12]->ordinal = 12;
153  rz_list_append(ret, ptr[12]);
154 
155  return ret;
156 }
157 
158 static RzBinInfo *info(RzBinFile *bf) {
159  ut8 rom_header[76];
160  RzBinInfo *ret = RZ_NEW0(RzBinInfo);
161  if (!ret || !bf || !bf->buf) {
162  free(ret);
163  return NULL;
164  }
165  rz_buf_read_at(bf->buf, 0x104, rom_header, 76);
166  ret->file = rz_str_ndup((const char *)&rom_header[48], 16);
167  ret->type = malloc(128);
168  ret->type[0] = 0;
169  gb_get_gbtype(ret->type, rom_header[66], rom_header[63]);
170  gb_add_cardtype(ret->type, rom_header[67]); // XXX
171  ret->machine = strdup("Gameboy");
172  ret->os = strdup("any");
173  ret->arch = strdup("gb");
174  ret->has_va = true;
175  ret->bits = 16;
176  ret->big_endian = 0;
177  ret->dbg_info = 0;
178  return ret;
179 }
180 
182  RzList *ret;
183  RzBinMem *m, *n;
184  if (!(ret = rz_list_new())) {
185  return NULL;
186  }
187  ret->free = free;
188  if (!(m = RZ_NEW0(RzBinMem))) {
189  rz_list_free(ret);
190  return NULL;
191  }
192  m->name = strdup("fastram");
193  m->addr = 0xff80LL;
194  m->size = 0x80;
195  m->perms = rz_str_rwx("rwx");
196  rz_list_append(ret, m);
197 
198  if (!(m = RZ_NEW0(RzBinMem))) {
199  return ret;
200  }
201  m->name = strdup("ioports");
202  m->addr = 0xff00LL;
203  m->size = 0x4c;
204  m->perms = rz_str_rwx("rwx");
205  rz_list_append(ret, m);
206 
207  if (!(m = RZ_NEW0(RzBinMem))) {
208  return ret;
209  }
210  m->name = strdup("oam");
211  m->addr = 0xfe00LL;
212  m->size = 0xa0;
213  m->perms = rz_str_rwx("rwx");
214  rz_list_append(ret, m);
215 
216  if (!(m = RZ_NEW0(RzBinMem))) {
217  return ret;
218  }
219  m->name = strdup("videoram");
220  m->addr = 0x8000LL;
221  m->size = 0x2000;
222  m->perms = rz_str_rwx("rwx");
223  rz_list_append(ret, m);
224 
225  if (!(m = RZ_NEW0(RzBinMem))) {
226  return ret;
227  }
228  m->name = strdup("iram");
229  m->addr = 0xc000LL;
230  m->size = 0x2000;
231  m->perms = rz_str_rwx("rwx");
232  rz_list_append(ret, m);
233  if (!(m->mirrors = rz_list_new())) {
234  return ret;
235  }
236  if (!(n = RZ_NEW0(RzBinMem))) {
237  rz_list_free(m->mirrors);
238  m->mirrors = NULL;
239  return ret;
240  }
241  n->name = strdup("iram_echo");
242  n->addr = 0xe000LL;
243  n->size = 0x1e00;
244  n->perms = rz_str_rwx("rx");
245  rz_list_append(m->mirrors, n);
246 
247  return ret;
248 }
249 
251  .name = "ningb",
252  .desc = "Gameboy format rz_bin plugin",
253  .license = "LGPL3",
254  .load_buffer = &load_buffer,
255  .check_buffer = &check_buffer,
256  .baddr = &baddr,
257  .binsym = &binsym,
258  .entries = &entries,
260  .sections = &sections,
261  .symbols = &symbols,
262  .info = &info,
263  .mem = &mem,
264 };
265 
266 #ifndef RZ_PLUGIN_INCORE
269  .data = &rz_bin_plugin_ningb,
271 };
272 #endif
lzma_index ** i
Definition: index.h:629
RZ_API void rz_bin_symbol_free(RzBinSymbol *sym)
Definition: bin.c:175
RZ_API void rz_bin_section_free(RzBinSection *bs)
Definition: bin.c:1116
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 *buf, Sdb *sdb)
Definition: bin_ningb.c:19
static RzList * symbols(RzBinFile *bf)
Definition: bin_ningb.c:85
static RzBinAddr * binsym(RzBinFile *bf, RzBinSpecialSymbol type)
Definition: bin_ningb.c:27
RZ_API RzLibStruct rizin_plugin
Definition: bin_ningb.c:267
static bool check_buffer(RzBuffer *b)
Definition: bin_ningb.c:11
static RzBinInfo * info(RzBinFile *bf)
Definition: bin_ningb.c:158
static ut64 baddr(RzBinFile *bf)
Definition: bin_ningb.c:23
static RzList * entries(RzBinFile *bf)
Definition: bin_ningb.c:44
RzList * mem(RzBinFile *bf)
Definition: bin_ningb.c:181
RzBinPlugin rz_bin_plugin_ningb
Definition: bin_ningb.c:250
static RzList * sections(RzBinFile *bf)
Definition: bin_ningb.c:62
#define RZ_API
#define NULL
Definition: cris-opc.c:27
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
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
RZ_API RZ_OWN RzList * rz_list_new(void)
Returns a new initialized RzList pointer (free method is not initialized)
Definition: list.c:235
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
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
void * malloc(size_t size)
Definition: malloc.c:123
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 n
Definition: mipsasm.c:19
int type
Definition: mipsasm.c:17
void gb_get_gbtype(char *type, ut8 foo, ut8 bar)
Definition: nin.h:154
const ut8 lic[]
Definition: nin.h:62
int gb_get_rombanks(ut8 id)
Definition: nin.h:128
void gb_add_cardtype(char *type, ut8 cardcode)
Definition: nin.h:105
RzBinSpecialSymbol
Definition: rz_bin.h:136
@ RZ_BIN_SPECIAL_SYMBOL_MAIN
Definition: rz_bin.h:139
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_LIB_TYPE_BIN
Definition: rz_lib.h:75
void(* RzListFree)(void *ptr)
Definition: rz_list.h:11
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
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
RZ_API int rz_str_rwx(const char *str)
Definition: str.c:318
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_VERSION
Definition: rz_version.h:8
#define b(i)
Definition: sha256.c:42
ut64 vaddr
Definition: rz_bin.h:186
ut64 paddr
Definition: rz_bin.h:187
ut64 hpaddr
Definition: rz_bin.h:189
XX curplugin == o->plugin.
Definition: rz_bin.h:298
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
ut64 dbg_info
Definition: rz_bin.h:240
char * arch
Definition: rz_bin.h:214
int big_endian
Definition: rz_bin.h:235
char * name
Definition: rz_bin.h:509
char * version
Definition: rz_bin.h:512
char * name
Definition: rz_bin.h:675
ut32 ordinal
Definition: rz_bin.h:692
RzListFree free
Definition: rz_list.h:21
Definition: sdb.h:63
uint32_t size
ut64(WINAPI *w32_GetEnabledXStateFeatures)()