2018-09-21 02:16:54 +02:00
|
|
|
/* 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) {
|
2017-03-12 11:43:21 +01:00
|
|
|
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 */
|
2019-01-17 05:15:32 -03:00
|
|
|
/* hacky check to avoid detecting multidex or MZ bins as bios */
|
2017-03-08 23:30:54 +01:00
|
|
|
/* need better fix for this */
|
2019-01-17 05:15:32 -03:00
|
|
|
if (!memcmp (buf, "dex", 3) || !memcmp (buf, "MZ", 2)) {
|
2018-09-21 02:16:54 +02:00
|
|
|
return false;
|
2017-03-08 23:30:54 +01:00
|
|
|
}
|
|
|
|
/* Check if this a 'jmp' opcode */
|
|
|
|
if ((buf[ep] == 0xea) || (buf[ep] == 0xe9)) {
|
2018-09-21 02:16:54 +02:00
|
|
|
return true;
|
2017-03-08 23:30:54 +01:00
|
|
|
}
|
|
|
|
}
|
2018-09-21 02:16:54 +02:00
|
|
|
return false;
|
2017-03-08 23:30:54 +01:00
|
|
|
}
|
|
|
|
|
2018-10-18 08:10:34 -03:00
|
|
|
static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb) {
|
|
|
|
return check_bytes (buf, sz);
|
2014-05-08 18:35:04 -05:00
|
|
|
}
|
|
|
|
|
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;
|
2014-05-08 18:35:04 -05:00
|
|
|
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;
|
2017-03-12 11:43:21 +01:00
|
|
|
if (!(ret = R_NEW0 (RBinInfo))) {
|
2013-06-09 02:11:17 +02:00
|
|
|
return NULL;
|
2017-03-12 11:43:21 +01:00
|
|
|
}
|
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;
|
2017-03-08 23:16:46 +01:00
|
|
|
}
|
2013-06-09 02:11:17 +02:00
|
|
|
// program headers is another section
|
2017-03-08 23:16:46 +01:00
|
|
|
if (!(ptr = R_NEW0 (RBinSection))) {
|
2013-06-09 02:11:17 +02:00
|
|
|
return ret;
|
2017-03-08 23:16:46 +01:00
|
|
|
}
|
2018-11-05 11:52:40 +01:00
|
|
|
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;
|
2014-05-21 03:18:00 +02:00
|
|
|
ptr->vaddr = 0xf0000;
|
2018-09-21 02:16:54 +02:00
|
|
|
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);
|
2018-10-16 23:47:29 +08:00
|
|
|
// 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;
|
|
|
|
}
|
2018-11-05 11:52:40 +01:00
|
|
|
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;
|
2017-03-12 11:43:21 +01:00
|
|
|
if (!(ret = r_list_new ())) {
|
2013-06-09 02:11:17 +02:00
|
|
|
return NULL;
|
2017-03-12 11:43:21 +01:00
|
|
|
}
|
2013-06-09 02:11:17 +02:00
|
|
|
ret->free = free;
|
2017-03-12 11:43:21 +01:00
|
|
|
if (!(ptr = R_NEW0 (RBinAddr))) {
|
2013-06-09 02:11:17 +02:00
|
|
|
return ret;
|
2017-03-12 11:43:21 +01:00
|
|
|
}
|
|
|
|
ptr->paddr = 0; // 0x70000;
|
2014-09-28 03:27:22 +02:00
|
|
|
ptr->vaddr = 0xffff0;
|
2013-06-09 02:11:17 +02:00
|
|
|
r_list_append (ret, ptr);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-15 00:21:37 +01:00
|
|
|
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",
|
2013-12-10 04:19:04 +01:00
|
|
|
.license = "LGPL",
|
2013-06-09 02:11:17 +02:00
|
|
|
.load = &load,
|
2014-05-08 18:35:04 -05:00
|
|
|
.load_bytes = &load_bytes,
|
2013-06-09 02:11:17 +02:00
|
|
|
.destroy = &destroy,
|
2014-05-08 14:08:21 -05:00
|
|
|
.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
|