Rizin
unix-like reverse engineering framework and cli tools
bin_nin3ds.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2018-2019 a0rtega
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 
10 #include "nin/n3ds.h"
11 
12 static struct n3ds_firm_hdr loaded_header;
13 
14 static bool check_buffer(RzBuffer *b) {
15  ut8 magic[4];
16  rz_buf_read_at(b, 0, magic, sizeof(magic));
17  return (!memcmp(magic, "FIRM", 4));
18 }
19 
20 static bool load_buffer(RzBinFile *bf, RzBinObject *obj, RzBuffer *b, Sdb *sdb) {
21  if (rz_buf_read_at(b, 0, (ut8 *)&loaded_header, sizeof(loaded_header)) == sizeof(loaded_header)) {
22  obj->bin_obj = &loaded_header;
23  return true;
24  }
25  return false;
26 }
27 
28 static RzList *sections(RzBinFile *bf) {
29  RzList *ret = NULL;
30  RzBinSection *sections[4] = {
31  NULL, NULL, NULL, NULL
32  };
33  int i, corrupt = false;
34 
35  if (!(ret = rz_list_new())) {
36  return NULL;
37  }
38 
39  /* FIRM has always 4 sections, normally the 4th section is not used */
40  for (i = 0; i < 4; i++) {
41  /* Check if section is used */
42  if (loaded_header.sections[i].size) {
44  /* Firmware Type ('0'=ARM9/'1'=ARM11) */
45  if (loaded_header.sections[i].type == 0x0) {
46  sections[i]->name = strdup("arm9");
47  } else if (loaded_header.sections[i].type == 0x1) {
48  sections[i]->name = strdup("arm11");
49  } else {
50  corrupt = true;
51  break;
52  }
53  sections[i]->size = loaded_header.sections[i].size;
54  sections[i]->vsize = loaded_header.sections[i].size;
55  sections[i]->paddr = loaded_header.sections[i].offset;
56  sections[i]->vaddr = loaded_header.sections[i].address;
57  sections[i]->perm = rz_str_rwx("rwx");
58  }
59  }
60 
61  /* Append sections or free them if file is corrupt to avoid memory leaks */
62  for (i = 0; i < 4; i++) {
63  if (sections[i]) {
64  if (corrupt) {
65  free(sections[i]);
66  } else {
67  rz_list_append(ret, sections[i]);
68  }
69  }
70  }
71  if (corrupt) {
72  rz_list_free(ret);
73  return NULL;
74  }
75 
76  return ret;
77 }
78 
79 static RzList *entries(RzBinFile *bf) {
80  RzList *ret = rz_list_new();
81  RzBinAddr *ptr9 = NULL, *ptr11 = NULL;
82 
83  if (bf && bf->buf) {
84  if (!ret) {
85  return NULL;
86  }
87  ret->free = free;
88  if (!(ptr9 = RZ_NEW0(RzBinAddr))) {
89  rz_list_free(ret);
90  return NULL;
91  }
92  if (!(ptr11 = RZ_NEW0(RzBinAddr))) {
93  rz_list_free(ret);
94  free(ptr9);
95  return NULL;
96  }
97 
98  /* ARM9 entry point */
99  ptr9->vaddr = loaded_header.arm9_ep;
100  rz_list_append(ret, ptr9);
101 
102  /* ARM11 entry point */
103  ptr11->vaddr = loaded_header.arm11_ep;
104  rz_list_append(ret, ptr11);
105  }
106  return ret;
107 }
108 
109 static RzBinInfo *info(RzBinFile *bf) {
110  RzBinInfo *ret = RZ_NEW0(RzBinInfo);
111  if (!ret) {
112  return NULL;
113  }
114 
115  if (!bf || !bf->buf) {
116  free(ret);
117  return NULL;
118  }
119 
120  ret->type = strdup("FIRM");
121  ret->machine = strdup("Nintendo 3DS");
122  ret->os = strdup("n3ds");
123  ret->arch = strdup("arm");
124  ret->has_va = true;
125  ret->bits = 32;
126 
127  return ret;
128 }
129 
131  .name = "nin3ds",
132  .desc = "Nintendo 3DS FIRM format rz_bin plugin",
133  .license = "LGPL3",
134  .load_buffer = &load_buffer,
135  .check_buffer = &check_buffer,
136  .entries = &entries,
138  .sections = &sections,
139  .info = &info,
140 };
141 
142 #ifndef RZ_PLUGIN_INCORE
145  .data = &rz_bin_plugin_nin3ds,
147 };
148 #endif
lzma_index ** i
Definition: index.h:629
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_nin3ds.c:20
RzBinPlugin rz_bin_plugin_nin3ds
Definition: bin_nin3ds.c:130
static struct n3ds_firm_hdr loaded_header
Definition: bin_nin3ds.c:12
RZ_API RzLibStruct rizin_plugin
Definition: bin_nin3ds.c:143
static bool check_buffer(RzBuffer *b)
Definition: bin_nin3ds.c:14
static RzBinInfo * info(RzBinFile *bf)
Definition: bin_nin3ds.c:109
static RzList * entries(RzBinFile *bf)
Definition: bin_nin3ds.c:79
static RzList * sections(RzBinFile *bf)
Definition: bin_nin3ds.c:28
#define RZ_API
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
uint8_t ut8
Definition: lh5801.h:11
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_LIB_TYPE_BIN
Definition: rz_lib.h:75
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
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 * arch
Definition: rz_bin.h:214
void * bin_obj
Definition: rz_bin.h:293
char * name
Definition: rz_bin.h:509
char * version
Definition: rz_bin.h:512
RzListFree free
Definition: rz_list.h:21
Definition: sdb.h:63