Rizin
unix-like reverse engineering framework and cli tools
bin_menuet.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2016-2019 pancake <pancake@nopcode.org>
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 
9 #define MENUET_VERSION(x) x[7]
10 
11 #if 0
12  db 'MENUET00' ; 8 byte id
13  dd 38 ; required os
14  dd START ; program start
15  dd I_END ; image size
16  dd 0x100000 ; reguired amount of memory
17  dd 0x00000000 ; reserved=no extended header
18 
19  org 0x0
20  db 'MENUET01' ; 8 byte id
21  dd 1 ; header version
22  dd START ; program start
23  dd I_END ; program image size
24  dd 0x1000 ; required amount of memory
25  dd 0x1000 ; esp
26  dd 0, 0 ; no parameters, no path
27 
28  0 db 'MENUET02'
29  8 dd 0x01
30  12 dd __start
31  16 dd __iend
32  20 dd __bssend
33  24 dd __stack
34  28 dd __cmdline
35  32 dd __pgmname
36  36 dd 0x0; tls map
37  40 dd __idata_start; секция .import
38  44 dd __idata_end
39  48 dd main
40 
41  db 'MENUET02'
42  dd 1
43  dd start
44  dd i_end
45  dd mem
46  dd mem
47  dd cmdline
48  dd path
49  dd 0
50 
51 #endif
52 
53 static bool check_buffer(RzBuffer *b) {
54  ut8 buf[8];
55  if (rz_buf_read_at(b, 0, buf, sizeof(buf)) != sizeof(buf)) {
56  return false;
57  }
58  if (rz_buf_size(b) >= 32 && !memcmp(buf, "MENUET0", 7)) {
59  switch (buf[7]) {
60  case '0':
61  case '1':
62  case '2':
63  return true;
64  }
65  RZ_LOG_ERROR("Unsupported MENUET version header\n");
66  }
67  return false;
68 }
69 
70 static bool load_buffer(RzBinFile *bf, RzBinObject *obj, RzBuffer *b, Sdb *sdb) {
71  return check_buffer(b);
72 }
73 
74 static ut64 baddr(RzBinFile *bf) {
75  return 0; // 0x800000;
76 }
77 
78 static ut64 menuetEntry(const ut8 *buf, int buf_size) {
79  switch (MENUET_VERSION(buf)) {
80  case '0': return rz_read_ble32(buf + 12, false);
81  case '1': return rz_read_ble32(buf + 12, false);
82  case '2': return rz_read_ble32(buf + 44, false);
83  }
84  return UT64_MAX;
85 }
86 
87 static RzList *entries(RzBinFile *bf) {
88  RzList *ret;
89  ut8 buf[64] = { 0 };
90  RzBinAddr *ptr = NULL;
91  const int buf_size = RZ_MIN(sizeof(buf), rz_buf_size(bf->buf));
92 
93  rz_buf_read_at(bf->buf, 0, buf, buf_size);
95  if (entry == UT64_MAX) {
96  return NULL;
97  }
98  if (!(ret = rz_list_new())) {
99  return NULL;
100  }
101  ret->free = free;
102  if ((ptr = RZ_NEW0(RzBinAddr))) {
103  ptr->paddr = rz_read_ble32(buf + 12, false);
104  ptr->vaddr = ptr->paddr + baddr(bf);
105  rz_list_append(ret, ptr);
106  }
107  return ret;
108 }
109 
110 static RzList *sections(RzBinFile *bf) {
111  RzList *ret = NULL;
112  RzBinSection *ptr = NULL;
113  ut8 buf[64] = { 0 };
114  const int buf_size = RZ_MIN(sizeof(buf), rz_buf_size(bf->buf));
115 
116  rz_buf_read_at(bf->buf, 0, buf, buf_size);
117  if (!bf->o->info) {
118  return NULL;
119  }
120 
121  if (!(ret = rz_list_newf(free))) {
122  return NULL;
123  }
124  // add text segment
125  if (!(ptr = RZ_NEW0(RzBinSection))) {
126  return ret;
127  }
128  ptr->name = strdup("text");
129  ptr->size = rz_read_ble32(buf + 16, false);
130  ptr->vsize = ptr->size + (ptr->size % 4096);
131  ptr->paddr = rz_read_ble32(buf + 12, false);
132  ptr->vaddr = ptr->paddr + baddr(bf);
133  ptr->perm = RZ_PERM_RX; // r-x
134  rz_list_append(ret, ptr);
135 
136  if (MENUET_VERSION(buf)) {
137  /* add data section */
138  if (!(ptr = RZ_NEW0(RzBinSection))) {
139  return ret;
140  }
141  ptr->name = strdup("idata");
142  const ut32 idata_start = rz_read_ble32(buf + 40, false);
143  const ut32 idata_end = rz_read_ble32(buf + 44, false);
144  ptr->size = idata_end - idata_start;
145  ptr->vsize = ptr->size + (ptr->size % 4096);
146  ptr->paddr = rz_read_ble32(buf + 40, false);
147  ptr->vaddr = ptr->paddr + baddr(bf);
148  ptr->perm = RZ_PERM_R; // r--
149  rz_list_append(ret, ptr);
150  }
151 
152  return ret;
153 }
154 
155 static RzBinInfo *info(RzBinFile *bf) {
156  RzBinInfo *ret = RZ_NEW0(RzBinInfo);
157  if (ret) {
158  ret->file = strdup(bf->file);
159  ret->bclass = strdup("program");
160  ret->rclass = strdup("menuet");
161  ret->os = strdup("MenuetOS");
162  ret->arch = strdup("x86");
163  ret->machine = strdup(ret->arch);
164  ret->subsystem = strdup("kolibri");
165  ret->type = strdup("EXEC");
166  ret->bits = 32;
167  ret->has_va = true;
168  ret->big_endian = 0;
169  ret->dbg_info = 0;
170  ret->dbg_info = 0;
171  }
172  return ret;
173 }
174 
175 static ut64 size(RzBinFile *bf) {
176  ut8 buf[4] = { 0 };
177  if (!bf->o->info) {
178  bf->o->info = info(bf);
179  }
180  if (!bf->o->info) {
181  return 0;
182  }
183  rz_buf_read_at(bf->buf, 16, buf, 4);
184  return (ut64)rz_read_ble32(buf, false);
185 }
186 
187 #if !RZ_BIN_P9
188 
189 /* inspired in http://www.phreedom.org/solar/code/tinype/tiny.97/tiny.asm */
190 static RzBuffer *create(RzBin *bin, const ut8 *code, int codelen, const ut8 *data, int datalen, RzBinArchOptions *opt) {
192 #define B(x, y) rz_buf_append_bytes(buf, (const ut8 *)(x), y)
193 #define D(x) rz_buf_append_ut32(buf, x)
194  B("MENUET01", 8);
195  D(1); // header version
196  D(32); // program start
197  D(0x1000); // program image size
198  D(0x1000); // ESP
199  D(0); // no parameters
200  D(0); // no path
201  B(code, codelen);
202  return buf;
203 }
204 
206  .name = "menuet",
207  .desc = "Menuet/KolibriOS bin plugin",
208  .license = "LGPL3",
209  .load_buffer = &load_buffer,
210  .size = &size,
211  .check_buffer = &check_buffer,
212  .baddr = &baddr,
213  .entries = &entries,
215  .sections = &sections,
216  .info = &info,
217  .create = &create,
218 };
219 
220 #ifndef RZ_PLUGIN_INCORE
223  .data = &rz_bin_plugin_menuet,
225 };
226 #endif
227 #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_menuet.c:70
static RzBuffer * create(RzBin *bin, const ut8 *code, int codelen, const ut8 *data, int datalen, RzBinArchOptions *opt)
Definition: bin_menuet.c:190
static ut64 size(RzBinFile *bf)
Definition: bin_menuet.c:175
#define MENUET_VERSION(x)
Definition: bin_menuet.c:9
RZ_API RzLibStruct rizin_plugin
Definition: bin_menuet.c:221
static bool check_buffer(RzBuffer *b)
Definition: bin_menuet.c:53
RzBinPlugin rz_bin_plugin_menuet
Definition: bin_menuet.c:205
static RzBinInfo * info(RzBinFile *bf)
Definition: bin_menuet.c:155
#define B(x, y)
static ut64 baddr(RzBinFile *bf)
Definition: bin_menuet.c:74
static RzList * entries(RzBinFile *bf)
Definition: bin_menuet.c:87
static RzList * sections(RzBinFile *bf)
Definition: bin_menuet.c:110
static ut64 menuetEntry(const ut8 *buf, int buf_size)
Definition: bin_menuet.c:78
#define D(x)
#define RZ_API
#define NULL
Definition: cris-opc.c:27
static static fork const void static count static fd const char const char static newpath const char static path const char path
Definition: sflib.h:35
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void start
Definition: sflib.h:133
static int buf_size
Definition: debug_qnx.c:35
uint32_t ut32
size_t map(int syms, int left, int len)
Definition: enough.c:237
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void uLong size
Definition: ioapi.h:138
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
void * mem
Definition: libc.cpp:91
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
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")
@ reserved
Definition: lm32_isa.h:94
#define header(is_bt, len_min, ret_op)
int main(int argc, char **argv)
Definition: rz-bb.c:29
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 RZ_OWN RzBuffer * rz_buf_new_with_bytes(RZ_NULLABLE RZ_BORROW const ut8 *bytes, ut64 len)
Creates a new buffer with a bytes array.
Definition: buf.c:465
RZ_API ut64 rz_buf_size(RZ_NONNULL RzBuffer *b)
Return the size of the buffer.
Definition: buf.c:1225
static ut32 rz_read_ble32(const void *src, bool big_endian)
Definition: rz_endian.h:497
@ RZ_LIB_TYPE_BIN
Definition: rz_lib.h:75
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
#define RZ_PERM_R
Definition: rz_types.h:93
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_PERM_RX
Definition: rz_types.h:97
#define RZ_MIN(x, y)
#define UT64_MAX
Definition: rz_types_base.h:86
#define RZ_VERSION
Definition: rz_version.h:8
#define b(i)
Definition: sha256.c:42
Definition: malloc.c:26
Definition: inftree9.h:24
Definition: zipcmp.c:77
ut64 vaddr
Definition: rz_bin.h:186
ut64 paddr
Definition: rz_bin.h:187
XX curplugin == o->plugin.
Definition: rz_bin.h:298
RzBinObject * o
Definition: rz_bin.h:305
char * file
Definition: rz_bin.h:299
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 * subsystem
Definition: rz_bin.h:220
char * machine
Definition: rz_bin.h:216
char * bclass
Definition: rz_bin.h:212
char * file
Definition: rz_bin.h:210
ut64 dbg_info
Definition: rz_bin.h:240
char * rclass
Definition: rz_bin.h:213
char * arch
Definition: rz_bin.h:214
int big_endian
Definition: rz_bin.h:235
RzBinInfo * info
Definition: rz_bin.h:287
char * name
Definition: rz_bin.h:509
char * version
Definition: rz_bin.h:512
char * name
Definition: rz_bin.h:619
RzListFree free
Definition: rz_list.h:21
Definition: sdb.h:63
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static const z80_opcode dd[]
Definition: z80_tab.h:844