Rizin
unix-like reverse engineering framework and cli tools
bin_bios.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2013-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 #include "../i/private.h"
9 
10 static bool check_buffer(RzBuffer *buf) {
11  rz_return_val_if_fail(buf, false);
12 
13  ut64 sz = rz_buf_size(buf);
14  if (sz <= 0xffff) {
15  return false;
16  }
17 
18  ut8 b0;
19  if (!rz_buf_read8_at(buf, 0, &b0)) {
20  return false;
21  }
22 
23  if (b0 == 0xcf || b0 == 0x7f) {
24  return false;
25  }
26 
27  const ut32 ep = sz - 0x10000 + 0xfff0; /* F000:FFF0 address */
28  /* hacky check to avoid detecting multidex or MZ bins as bios */
29  /* need better fix for this */
30  ut8 tmp[3];
31  int r = rz_buf_read_at(buf, 0, tmp, sizeof(tmp));
32  if (r <= 0 || !memcmp(tmp, "dex", 3) || !memcmp(tmp, "MZ", 2)) {
33  return false;
34  }
35 
36  /* Check if this a 'jmp' opcode */
37  ut8 bep;
38  if (!rz_buf_read8_at(buf, ep, &bep)) {
39  return false;
40  }
41 
42  return bep == 0xea || bep == 0xe9;
43 }
44 
45 static bool load_buffer(RzBinFile *bf, RzBinObject *obj, RzBuffer *buf, Sdb *sdb) {
46  if (!check_buffer(buf)) {
47  return false;
48  }
49  obj->bin_obj = rz_buf_ref(buf);
50  return true;
51 }
52 
53 static void destroy(RzBinFile *bf) {
54  rz_buf_free(bf->o->bin_obj);
55 }
56 
57 static ut64 baddr(RzBinFile *bf) {
58  return 0;
59 }
60 
61 /* accelerate binary load */
62 static RzList *strings(RzBinFile *bf) {
63  return NULL;
64 }
65 
66 static RzBinInfo *info(RzBinFile *bf) {
67  RzBinInfo *ret = NULL;
68  if (!(ret = RZ_NEW0(RzBinInfo))) {
69  return NULL;
70  }
71  ret->lang = NULL;
72  ret->file = bf->file ? strdup(bf->file) : NULL;
73  ret->type = strdup("bios");
74  ret->bclass = strdup("1.0");
75  ret->rclass = strdup("bios");
76  ret->os = strdup("any");
77  ret->subsystem = strdup("unknown");
78  ret->machine = strdup("pc");
79  ret->arch = strdup("x86");
80  ret->has_va = 1;
81  ret->bits = 16;
82  ret->big_endian = 0;
83  ret->dbg_info = 0;
84  return ret;
85 }
86 
87 static RzList *sections(RzBinFile *bf) {
88  RzList *ret = NULL;
89  RzBinSection *ptr = NULL;
90  RzBuffer *obj = bf->o->bin_obj;
91 
93  return NULL;
94  }
95  // program headers is another section
96  if (!(ptr = RZ_NEW0(RzBinSection))) {
97  return ret;
98  }
99  ptr->name = strdup("bootblk"); // Maps to 0xF000:0000 segment
100  ptr->vsize = ptr->size = 0x10000;
101  ptr->paddr = rz_buf_size(bf->buf) - ptr->size;
102  ptr->vaddr = 0xf0000;
103  ptr->perm = RZ_PERM_RWX;
104  rz_list_append(ret, ptr);
105  // If image bigger than 128K - add one more section
106  if (bf->size >= 0x20000) {
107  if (!(ptr = RZ_NEW0(RzBinSection))) {
108  return ret;
109  }
110  ptr->name = strdup("_e000"); // Maps to 0xE000:0000 segment
111  ptr->vsize = ptr->size = 0x10000;
112  ptr->paddr = rz_buf_size(obj) - 2 * ptr->size;
113  ptr->vaddr = 0xe0000;
114  ptr->perm = RZ_PERM_RWX;
115  rz_list_append(ret, ptr);
116  }
117  return ret;
118 }
119 
120 static RzList *entries(RzBinFile *bf) {
121  RzList *ret;
122  RzBinAddr *ptr = NULL;
123  if (!(ret = rz_list_new())) {
124  return NULL;
125  }
126  ret->free = free;
127  if (!(ptr = RZ_NEW0(RzBinAddr))) {
128  return ret;
129  }
130  ptr->paddr = 0; // 0x70000;
131  ptr->vaddr = 0xffff0;
132  rz_list_append(ret, ptr);
133  return ret;
134 }
135 
137  .name = "bios",
138  .desc = "BIOS bin plugin",
139  .license = "LGPL",
140  .load_buffer = &load_buffer,
141  .destroy = &destroy,
142  .check_buffer = &check_buffer,
143  .baddr = &baddr,
144  .entries = entries,
146  .sections = sections,
147  .strings = &strings,
148  .info = &info,
149 };
150 
151 #ifndef RZ_PLUGIN_INCORE
154  .data = &rz_bin_plugin_bios,
156 };
157 #endif
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 check_buffer(RzBuffer *buf)
Definition: bin_bios.c:10
static bool load_buffer(RzBinFile *bf, RzBinObject *obj, RzBuffer *buf, Sdb *sdb)
Definition: bin_bios.c:45
RzBinPlugin rz_bin_plugin_bios
Definition: bin_bios.c:136
static RzList * strings(RzBinFile *bf)
Definition: bin_bios.c:62
static void destroy(RzBinFile *bf)
Definition: bin_bios.c:53
RZ_API RzLibStruct rizin_plugin
Definition: bin_bios.c:152
static RzBinInfo * info(RzBinFile *bf)
Definition: bin_bios.c:66
static ut64 baddr(RzBinFile *bf)
Definition: bin_bios.c:57
static RzList * entries(RzBinFile *bf)
Definition: bin_bios.c:120
static RzList * sections(RzBinFile *bf)
Definition: bin_bios.c:87
#define RZ_API
#define NULL
Definition: cris-opc.c:27
#define r
Definition: crypto_rc6.c:12
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
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")
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API RzBuffer * rz_buf_ref(RzBuffer *b)
Increment the reference count of the buffer.
Definition: buf.c:668
RZ_API bool rz_buf_read8_at(RzBuffer *b, ut64 addr, RZ_NONNULL RZ_OUT ut8 *result)
Read a byte at the specified address in the buffer.
Definition: buf.c:876
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 void rz_buf_free(RzBuffer *b)
Free all internal data hold by the buffer and the buffer.
Definition: buf.c:1253
RZ_API ut64 rz_buf_size(RZ_NONNULL RzBuffer *b)
Return the size of the buffer.
Definition: buf.c:1225
@ RZ_LIB_TYPE_BIN
Definition: rz_lib.h:75
void(* RzListFree)(void *ptr)
Definition: rz_list.h:11
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_PERM_RWX
Definition: rz_types.h:98
#define RZ_VERSION
Definition: rz_version.h:8
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
const char * lang
Definition: rz_bin.h:224
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
void * bin_obj
Definition: rz_bin.h:293
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)()