radare2/libr/bin/p/bin_bios.c

150 lines
3.3 KiB
C
Raw Normal View History

/* radare - LGPL - Copyright 2013-2019 - pancake */
2013-06-09 02:11:17 +02:00
#include <r_types.h>
#include <r_util.h>
#include <r_lib.h>
#include <r_bin.h>
#include "../i/private.h"
2013-06-09 02:11:17 +02:00
static bool check_buffer(RBuffer *buf) {
r_return_val_if_fail (buf, false);
ut64 sz = r_buf_size (buf);
if (sz <= 0xffff) {
return false;
}
ut8 b0 = r_buf_read8_at (buf, 0);
if (b0 == 0xcf || b0 == 0x7f) {
return false;
2017-03-08 23:30:54 +01:00
}
const ut32 ep = sz - 0x10000 + 0xfff0; /* F000:FFF0 address */
/* hacky check to avoid detecting multidex or MZ bins as bios */
/* need better fix for this */
ut8 tmp[3];
int r = r_buf_read_at (buf, 0, tmp, sizeof (tmp));
if (r <= 0 || !memcmp (tmp, "dex", 3) || !memcmp (tmp, "MZ", 2)) {
return false;
}
/* Check if this a 'jmp' opcode */
ut8 bep = r_buf_read8_at (buf, ep);
return bep == 0xea || bep == 0xe9;
2017-03-08 23:30:54 +01:00
}
static bool load_buffer(RBinFile *bf, void **bin_obj, RBuffer *buf, ut64 loadaddr, Sdb *sdb) {
if (!check_buffer (buf)) {
return false;
}
*bin_obj = r_buf_ref (buf);
return true;
}
static void destroy(RBinFile *bf) {
r_buf_free (bf->o->bin_obj);
2013-06-09 02:11:17 +02:00
}
2017-10-31 17:24:53 +01:00
static ut64 baddr(RBinFile *bf) {
2013-06-09 02:11:17 +02:00
return 0;
}
/* accelerate binary load */
2017-10-31 17:24:53 +01:00
static RList *strings(RBinFile *bf) {
2013-06-09 02:11:17 +02:00
return NULL;
}
2017-10-31 17:24:53 +01:00
static RBinInfo *info(RBinFile *bf) {
2013-06-09 02:11:17 +02:00
RBinInfo *ret = NULL;
if (!(ret = R_NEW0 (RBinInfo))) {
2013-06-09 02:11:17 +02:00
return NULL;
}
2013-06-09 02:11:17 +02:00
ret->lang = NULL;
2017-10-31 17:24:53 +01:00
ret->file = bf->file? strdup (bf->file): NULL;
2015-03-25 14:49:33 +01:00
ret->type = strdup ("bios");
ret->bclass = strdup ("1.0");
ret->rclass = strdup ("bios");
ret->os = strdup ("any");
ret->subsystem = strdup ("unknown");
ret->machine = strdup ("pc");
ret->arch = strdup ("x86");
2013-06-09 02:11:17 +02:00
ret->has_va = 1;
ret->bits = 16;
ret->big_endian = 0;
ret->dbg_info = 0;
return ret;
}
2017-10-31 17:24:53 +01:00
static RList *sections(RBinFile *bf) {
2013-06-09 02:11:17 +02:00
RList *ret = NULL;
RBinSection *ptr = NULL;
RBuffer *obj = bf->o->bin_obj;
2013-06-09 02:11:17 +02:00
if (!(ret = r_list_newf ((RListFree) r_bin_section_free))) {
2013-06-09 02:11:17 +02:00
return NULL;
}
2013-06-09 02:11:17 +02:00
// program headers is another section
if (!(ptr = R_NEW0 (RBinSection))) {
2013-06-09 02:11:17 +02:00
return ret;
}
ptr->name = strdup ("bootblk"); // Maps to 0xF000:0000 segment
2013-06-09 02:11:17 +02:00
ptr->vsize = ptr->size = 0x10000;
ptr->paddr = r_buf_size (bf->buf) - ptr->size;
ptr->vaddr = 0xf0000;
ptr->perm = R_PERM_RWX;
2015-12-12 22:27:17 +01:00
ptr->add = true;
2013-06-09 02:11:17 +02:00
r_list_append (ret, ptr);
// If image bigger than 128K - add one more section
if (bf->size >= 0x20000) {
2018-10-16 21:04:28 +08:00
if (!(ptr = R_NEW0 (RBinSection))) {
return ret;
}
ptr->name = strdup ("_e000"); // Maps to 0xE000:0000 segment
2018-10-16 21:04:28 +08:00
ptr->vsize = ptr->size = 0x10000;
ptr->paddr = r_buf_size (obj) - 2 * ptr->size;
2018-10-16 21:04:28 +08:00
ptr->vaddr = 0xe0000;
ptr->perm = R_PERM_RWX;
ptr->add = true;
r_list_append (ret, ptr);
}
2013-06-09 02:11:17 +02:00
return ret;
}
2017-10-31 17:24:53 +01:00
static RList *entries(RBinFile *bf) {
2013-06-09 02:11:17 +02:00
RList *ret;
RBinAddr *ptr = NULL;
if (!(ret = r_list_new ())) {
2013-06-09 02:11:17 +02:00
return NULL;
}
2013-06-09 02:11:17 +02:00
ret->free = free;
if (!(ptr = R_NEW0 (RBinAddr))) {
2013-06-09 02:11:17 +02:00
return ret;
}
ptr->paddr = 0; // 0x70000;
ptr->vaddr = 0xffff0;
2013-06-09 02:11:17 +02:00
r_list_append (ret, ptr);
return ret;
}
RBinPlugin r_bin_plugin_bios = {
2013-06-09 02:11:17 +02:00
.name = "bios",
2014-03-19 10:33:00 +01:00
.desc = "BIOS bin plugin",
.license = "LGPL",
.load_buffer = &load_buffer,
2013-06-09 02:11:17 +02:00
.destroy = &destroy,
.check_buffer = &check_buffer,
2013-06-09 02:11:17 +02:00
.baddr = &baddr,
.entries = entries,
.sections = sections,
.strings = &strings,
.info = &info,
};
2019-06-13 19:12:51 +02:00
#ifndef R2_PLUGIN_INCORE
2018-09-15 23:52:12 +03:00
R_API RLibStruct radare_plugin = {
2013-06-09 02:11:17 +02:00
.type = R_LIB_TYPE_BIN,
2015-07-12 16:04:10 +02:00
.data = &r_bin_plugin_bios,
.version = R2_VERSION
2013-06-09 02:11:17 +02:00
};
#endif