1 /* radare - LGPL - 2015-2019 - a0rtega */
2 
3 #include <r_types.h>
4 #include <r_util.h>
5 #include <r_lib.h>
6 #include <r_bin.h>
7 #include <string.h>
8 
9 #include "../format/nin/nds.h"
10 
11 static struct nds_hdr loaded_header;
12 
check_buffer(RBuffer * b)13 static bool check_buffer(RBuffer *b) {
14 	ut8 ninlogohead[6];
15 	if (r_buf_read_at (b, 0xc0, ninlogohead, sizeof (ninlogohead)) == 6) {
16 		/* begin of nintendo logo =    \x24\xff\xae\x51\x69\x9a */
17 		if (!memcmp (ninlogohead, "\x24\xff\xae\x51\x69\x9a", 6)) {
18 			return true;
19 		}
20 		/* begin of Homebrew magic */
21 		if (!memcmp (ninlogohead, "\xC8\x60\x4F\xE2\x01\x70", 6)){
22 			return true;
23 		}
24 	}
25 	return false;
26 }
27 
load_buffer(RBinFile * bf,void ** bin_obj,RBuffer * b,ut64 loadaddr,Sdb * sdb)28 static bool load_buffer (RBinFile *bf, void **bin_obj, RBuffer *b, ut64 loadaddr, Sdb *sdb) {
29 	r_buf_read_at (b, 0, (ut8*)&loaded_header, sizeof (loaded_header));
30 	*bin_obj = &loaded_header;
31 	return (*bin_obj != NULL);
32 }
33 
baddr(RBinFile * bf)34 static ut64 baddr(RBinFile *bf) {
35 	return (ut64) loaded_header.arm9_ram_address;
36 }
37 
boffset(RBinFile * bf)38 static ut64 boffset(RBinFile *bf) {
39 	return 0LL;
40 }
41 
sections(RBinFile * bf)42 static RList *sections(RBinFile *bf) {
43 	RList *ret = NULL;
44 	RBinSection *ptr9 = NULL, *ptr7 = NULL;
45 
46 	if (!(ret = r_list_new ())) {
47 		return NULL;
48 	}
49 	if (!(ptr9 = R_NEW0 (RBinSection))) {
50 		r_list_free (ret);
51 		return NULL;
52 	}
53 	if (!(ptr7 = R_NEW0 (RBinSection))) {
54 		r_list_free (ret);
55 		free (ptr9);
56 		return NULL;
57 	}
58 
59 	ptr9->name = strdup ("arm9");
60 	ptr9->size = loaded_header.arm9_size;
61 	ptr9->vsize = loaded_header.arm9_size;
62 	ptr9->paddr = loaded_header.arm9_rom_offset;
63 	ptr9->vaddr = loaded_header.arm9_ram_address;
64 	ptr9->perm = r_str_rwx ("rwx");
65 	ptr9->add = true;
66 	r_list_append (ret, ptr9);
67 
68 	ptr7->name = strdup ("arm7");
69 	ptr7->size = loaded_header.arm7_size;
70 	ptr7->vsize = loaded_header.arm7_size;
71 	ptr7->paddr = loaded_header.arm7_rom_offset;
72 	ptr7->vaddr = loaded_header.arm7_ram_address;
73 	ptr7->perm = r_str_rwx ("rwx");
74 	ptr7->add = true;
75 	r_list_append (ret, ptr7);
76 
77 	return ret;
78 }
79 
entries(RBinFile * bf)80 static RList *entries(RBinFile *bf) {
81 	RList *ret = r_list_new ();
82 	RBinAddr *ptr9 = NULL, *ptr7 = NULL;
83 
84 	if (bf && bf->buf) {
85 		if (!ret) {
86 			return NULL;
87 		}
88 		ret->free = free;
89 		if (!(ptr9 = R_NEW0 (RBinAddr))) {
90 			r_list_free (ret);
91 			return NULL;
92 		}
93 		if (!(ptr7 = R_NEW0 (RBinAddr))) {
94 			r_list_free (ret);
95 			free (ptr9);
96 			return NULL;
97 		}
98 
99 		/* ARM9 entry point */
100 		ptr9->vaddr = loaded_header.arm9_entry_address;
101 		// ptr9->paddr = loaded_header.arm9_entry_address;
102 		r_list_append (ret, ptr9);
103 
104 		/* ARM7 entry point */
105 		ptr7->vaddr = loaded_header.arm7_entry_address;
106 		// ptr7->paddr = loaded_header.arm7_entry_address;
107 		r_list_append (ret, ptr7);
108 	}
109 	return ret;
110 }
111 
info(RBinFile * bf)112 static RBinInfo *info(RBinFile *bf) {
113 	r_return_val_if_fail (bf && bf->buf, NULL);
114 	RBinInfo *ret = R_NEW0 (RBinInfo);
115 	if (ret) {
116 		char *filepath = r_str_newf ("%.12s - %.4s",
117 			loaded_header.title, loaded_header.gamecode);
118 		ret->file = filepath;
119 		ret->type = strdup ("ROM");
120 		ret->machine = strdup ("Nintendo DS");
121 		ret->os = strdup ("nds");
122 		ret->arch = strdup ("arm");
123 		ret->has_va = true;
124 		ret->bits = 32;
125 	}
126 	return ret;
127 }
128 
129 RBinPlugin r_bin_plugin_ninds = {
130 	.name = "ninds",
131 	.desc = "Nintendo DS format r_bin plugin",
132 	.license = "LGPL3",
133 	.load_buffer = &load_buffer,
134 	.check_buffer = &check_buffer,
135 	.baddr = &baddr,
136 	.boffset = &boffset,
137 	.entries = &entries,
138 	.sections = &sections,
139 	.info = &info,
140 };
141 
142 #ifndef R2_PLUGIN_INCORE
143 R_API RLibStruct radare_plugin = {
144 	.type = R_LIB_TYPE_BIN,
145 	.data = &r_bin_plugin_ninds,
146 	.version = R2_VERSION
147 };
148 #endif
149