Rizin
unix-like reverse engineering framework and cli tools
bin_z64.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2018-2019 lowlyw <cutlassc91@gmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 /*
5  * info comes from here.
6  * https://github.com/mikeryan/n64dev
7  * http://en64.shoutwiki.com/wiki/N64_Memory
8  */
9 
10 #include <rz_types.h>
11 #include <rz_util.h>
12 #include <rz_lib.h>
13 #include <rz_bin.h>
14 #include <rz_io.h>
15 #include <rz_cons.h>
16 
17 #define N64_ROM_START 0x1000
18 
19 // starting at 0
20 /*
21 0000h (1 byte): initial PI_BSB_DOM1_LAT_REG value (0x80)
22 0001h (1 byte): initial PI_BSB_DOM1_PGS_REG value (0x37)
23 0002h (1 byte): initial PI_BSB_DOM1_PWD_REG value (0x12)
24 0003h (1 byte): initial PI_BSB_DOM1_PGS_REG value (0x40)
25 0004h - 0007h (1 dword): ClockRate
26 0008h - 000Bh (1 dword): Program Counter (PC)
27 000Ch - 000Fh (1 dword): Release
28 0010h - 0013h (1 dword): CRC1
29 0014h - 0017h (1 dword): CRC2
30 0018h - 001Fh (2 dwords): Unknown (0x0000000000000000)
31 0020h - 0033h (20 bytes): Image name
32  Padded with 0x00 or spaces (0x20)
33 0034h - 0037h (1 dword): Unknown (0x00000000)
34 0038h - 003Bh (1 dword): Manufacturer ID
35  0x0000004E = Nintendo ('N')
36 003Ch - 003Dh (1 word): Cartridge ID
37 003Eh - 003Fh (1 word): Country code
38  0x4400 = Germany ('D')
39  0x4500 = USA ('E')
40  0x4A00 = Japan ('J')
41  0x5000 = Europe ('P')
42  0x5500 = Australia ('U')
43 0040h - 0FFFh (1008 dwords): Boot code
44 */
45 typedef struct {
46  ut8 x1; /* initial PI_BSB_DOM1_LAT_REG value */
47  ut8 x2; /* initial PI_BSB_DOM1_PGS_REG value */
48  ut8 x3; /* initial PI_BSB_DOM1_PWD_REG value */
49  ut8 x4; /* initial PI_BSB_DOM1_RLS_REG value */
56  char Name[20];
60  ut8 ManufacturerID; // 0x0000004E ('N') ?
64  // BOOT CODE?
65 } N64Header;
66 
68 
69 static ut64 baddr(RzBinFile *bf) {
71 }
72 
73 static bool check_buffer(RzBuffer *b) {
74  ut8 magic[4];
75  if (rz_buf_size(b) < N64_ROM_START) {
76  return false;
77  }
78  (void)rz_buf_read_at(b, 0, magic, sizeof(magic));
79  return !memcmp(magic, "\x80\x37\x12\x40", 4);
80 }
81 
82 static bool load_buffer(RzBinFile *bf, RzBinObject *obj, RzBuffer *b, Sdb *sdb) {
83  if (check_buffer(b)) {
84  ut8 buf[sizeof(N64Header)] = { 0 };
85  rz_buf_read_at(b, 0, buf, sizeof(buf));
86  obj->bin_obj = memcpy(&n64_header, buf, sizeof(N64Header));
87  return true;
88  }
89  return false;
90 }
91 
92 static RzList *entries(RzBinFile *bf) {
93  RzList /*<RzBinAddr>*/ *ret = rz_list_newf(free);
94  if (!ret) {
95  return NULL;
96  }
97  RzBinAddr *ptr = RZ_NEW0(RzBinAddr);
98  if (ptr) {
99  ptr->paddr = N64_ROM_START;
100  ptr->vaddr = baddr(bf);
101  rz_list_append(ret, ptr);
102  }
103  return ret;
104 }
105 
106 static RzList *sections(RzBinFile *bf) {
107  RzList /*<RzBinSection>*/ *ret = rz_list_new();
108  if (!ret) {
109  return NULL;
110  }
112  if (!text) {
113  rz_list_free(ret);
114  return NULL;
115  }
116  text->name = strdup("text");
117  text->size = rz_buf_size(bf->buf) - N64_ROM_START;
118  text->vsize = text->size;
119  text->paddr = N64_ROM_START;
120  text->vaddr = baddr(bf);
121  text->perm = RZ_PERM_RX;
122  rz_list_append(ret, text);
123  return ret;
124 }
125 
126 static ut64 boffset(RzBinFile *bf) {
127  return 0LL;
128 }
129 
130 static RzBinInfo *info(RzBinFile *bf) {
131  char GameName[21] = { 0 };
132  RzBinInfo *ret = RZ_NEW0(RzBinInfo);
133  if (!ret) {
134  return NULL;
135  }
136  memcpy(GameName, n64_header.Name, sizeof(n64_header.Name));
137  ret->file = rz_str_newf("%s (%c)", GameName, n64_header.CountryCode);
138  ret->os = strdup("n64");
139  ret->arch = strdup("mips");
140  ret->machine = strdup("Nintendo 64");
141  ret->type = strdup("ROM");
142  ret->bits = 64;
143  ret->has_va = true;
144  ret->big_endian = true;
145  return ret;
146 }
147 
148 #if !RZ_BIN_Z64
149 
151  .name = "z64",
152  .desc = "Nintendo 64 binaries big endian rz_bin plugin",
153  .license = "LGPL3",
154  .load_buffer = &load_buffer,
155  .check_buffer = &check_buffer,
156  .baddr = baddr,
157  .boffset = &boffset,
158  .entries = &entries,
160  .sections = &sections,
161  .info = &info
162 };
163 
164 #ifndef RZ_PLUGIN_INCORE
167  .data = &rz_bin_plugin_z64,
169 };
170 #endif
171 #endif
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_z64.c:82
static ut64 boffset(RzBinFile *bf)
Definition: bin_z64.c:126
#define N64_ROM_START
Definition: bin_z64.c:17
RZ_API RzLibStruct rizin_plugin
Definition: bin_z64.c:165
static bool check_buffer(RzBuffer *b)
Definition: bin_z64.c:73
static RzBinInfo * info(RzBinFile *bf)
Definition: bin_z64.c:130
static ut64 baddr(RzBinFile *bf)
Definition: bin_z64.c:69
static N64Header n64_header
Definition: bin_z64.c:67
static RzList * entries(RzBinFile *bf)
Definition: bin_z64.c:92
static RzList * sections(RzBinFile *bf)
Definition: bin_z64.c:106
RzBinPlugin rz_bin_plugin_z64
Definition: bin_z64.c:150
#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
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
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
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")
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
static ut32 rz_read_be32(const void *src)
Definition: rz_endian.h:87
@ RZ_LIB_TYPE_BIN
Definition: rz_lib.h:75
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_PERM_RX
Definition: rz_types.h:97
#define RZ_VERSION
Definition: rz_version.h:8
#define b(i)
Definition: sha256.c:42
ut16 CartridgeID
Definition: bin_z64.c:61
ut8 x3
Definition: bin_z64.c:48
ut32 BootAddress
Definition: bin_z64.c:51
ut8 UNK4
Definition: bin_z64.c:59
ut8 x4
Definition: bin_z64.c:49
ut32 Release
Definition: bin_z64.c:52
ut8 ManufacturerID
Definition: bin_z64.c:60
ut64 UNK1
Definition: bin_z64.c:55
ut8 x2
Definition: bin_z64.c:47
ut8 UNK5
Definition: bin_z64.c:63
ut32 UNK2
Definition: bin_z64.c:57
char Name[20]
Definition: bin_z64.c:56
char CountryCode
Definition: bin_z64.c:62
ut32 CRC1
Definition: bin_z64.c:53
ut32 ClockRate
Definition: bin_z64.c:50
ut32 CRC2
Definition: bin_z64.c:54
ut16 UNK3
Definition: bin_z64.c:58
ut8 x1
Definition: bin_z64.c:46
ut64 vaddr
Definition: rz_bin.h:186
ut64 paddr
Definition: rz_bin.h:187
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
char * arch
Definition: rz_bin.h:214
int big_endian
Definition: rz_bin.h:235
void * bin_obj
Definition: rz_bin.h:293
char * name
Definition: rz_bin.h:509
char * version
Definition: rz_bin.h:512
Definition: sdb.h:63
ut64(WINAPI *w32_GetEnabledXStateFeatures)()