radare2/libr/bin/p/bin_bios.c

141 lines
3.2 KiB
C
Raw Normal View History

/* radare - LGPL - Copyright 2013-2018 - 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>
2017-03-08 23:30:54 +01:00
static bool check_bytes(const ut8 *buf, ut64 length) {
if (buf && length > 0xffff && buf[0] != 0xcf && buf[0] != 0x7f) {
2017-03-08 23:30:54 +01:00
const ut32 ep = length - 0x10000 + 0xfff0; /* F000:FFF0 address */
/* hacky check to avoid detecting multidex or MZ bins as bios */
2017-03-08 23:30:54 +01:00
/* need better fix for this */
if (!memcmp (buf, "dex", 3) || !memcmp (buf, "MZ", 2)) {
return false;
2017-03-08 23:30:54 +01:00
}
/* Check if this a 'jmp' opcode */
if ((buf[ep] == 0xea) || (buf[ep] == 0xe9)) {
return true;
2017-03-08 23:30:54 +01:00
}
}
return false;
2017-03-08 23:30:54 +01:00
}
static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb) {
return check_bytes (buf, sz);
}
2017-10-31 17:24:53 +01:00
static bool load(RBinFile *bf) {
const ut8 *bytes = bf? r_buf_buffer (bf->buf): NULL;
ut64 sz = bf? r_buf_size (bf->buf): 0;
return check_bytes (bytes, sz);
2013-06-09 02:11:17 +02:00
}
2017-10-31 17:24:53 +01:00
static int destroy(RBinFile *bf) {
// r_bin_bios_free ((struct r_bin_bios_obj_t*)bf->o->bin_obj);
2015-09-14 02:08:31 +02:00
return true;
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;
2017-03-17 03:52:43 +01:00
if (!(ret = r_list_newf ((RListFree) 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;
2017-10-31 17:24:53 +01:00
ptr->paddr = bf->buf->length - 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 = bf->buf->length - 2 * ptr->size;
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",
2013-06-09 02:11:17 +02:00
.load = &load,
.load_bytes = &load_bytes,
2013-06-09 02:11:17 +02:00
.destroy = &destroy,
.check_bytes = &check_bytes,
2013-06-09 02:11:17 +02:00
.baddr = &baddr,
.entries = entries,
.sections = sections,
.strings = &strings,
.info = &info,
};
#ifndef CORELIB
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