Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial support for RBin->RIO redirections ##bin #23109

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dist/plugins-cfg/plugins.def.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ arch.xtensa
arch.z80
asm.null
bin.any
bin.uf2
bin.art
bin.avr
bin.bf
Expand Down
5 changes: 2 additions & 3 deletions libr/bin/p/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ FORMATS=any.mk elf.mk elf64.mk pe.mk pe64.mk te.mk mach0.mk
FORMATS+=bios.mk mach064.mk dyldcache.mk xnu_kernelcache.mk java.mk
FORMATS+=dex.mk fs.mk ningb.mk coff.mk xcoff64.mk ningba.mk xbe.mk zimg.mk
FORMATS+=omf.mk cgc.mk dol.mk rel.mk nes.mk mbn.mk psxexe.mk
FORMATS+=vsf.mk nin3ds.mk bflt.mk wasm.mk sfc.mk
FORMATS+=mdmp.mk z64.mk qnx.mk prg.mk dmp64.mk
FORMATS+=xtac.mk
FORMATS+=vsf.mk nin3ds.mk bflt.mk wasm.mk sfc.mk uf2.mk
FORMATS+=mdmp.mk z64.mk qnx.mk prg.mk dmp64.mk xtac.mk

FORMATS+=xtr_dyldcache.mk
FORMATS+=xtr_fatmach0.mk
Expand Down
52 changes: 52 additions & 0 deletions libr/bin/p/bin_uf2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* radare - MIT - 2024 - pancake */

#include <r_bin.h>

static bool check(RBinFile *bf, RBuffer *b) {
if (r_buf_size (b) > 0x10) {
ut8 buf[5] ={0};
r_buf_read_at (b, 0, buf, sizeof (buf));
return !memcmp (buf, "UF2\n", 4);
}
return false;
}

static bool load(RBinFile *bf, RBuffer *b, ut64 loadaddr) {
return check (bf, b);
}

static RBinInfo *info(RBinFile *bf) {
RBinInfo *ret = R_NEW0 (RBinInfo);
if (!ret) {
return NULL;
}
ret->file = strdup (bf->file);
ret->type = strdup ("io"); // requires IO redirection to work
ret->machine = strdup ("Microsoft UF2"); // XXX
ret->bclass = strdup ("uf2://");
ret->os = strdup ("hw"); // aka baremetal
ret->arch = strdup ("arm");
ret->bits = 32;
ret->has_va = 1;
ret->big_endian = 1;
return ret;
}

RBinPlugin r_bin_plugin_uf2 = {
.meta = {
.name = "uf2",
.desc = "Microsoft Unified Firmware v2",
.license = "MIT",
},
.load = &load,
.check = &check,
.info = &info,
};

#ifndef R2_PLUGIN_INCORE
R_API RLibStruct radare_plugin = {
.type = R_LIB_TYPE_BIN,
.data = &r_bin_plugin_uf2,
.version = R2_VERSION
};
#endif
10 changes: 10 additions & 0 deletions libr/bin/p/uf2.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
OBJ_UF2=bin_uf2.o

STATIC_OBJ+=${OBJ_UF2}
TARGET_UF2=bin_uf2.${EXT_SO}

ALL_TARGETS+=${TARGET_UF2}

${TARGET_UF2}: ${OBJ_UF2}
${CC} $(call libname,bin_uf2) -shared ${CFLAGS} \
-o ${TARGET_UF2} ${OBJ_UF2} $(LINK) $(LDFLAGS)
46 changes: 31 additions & 15 deletions libr/core/cfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ static bool setbpint(RCore *r, const char *mode, const char *sym) {
// XXX - need to handle index selection during debugging
static int r_core_file_load_for_debug(RCore *r, ut64 baseaddr, R_NULLABLE const char *filenameuri) {
RIODesc *desc = r->io->desc;
RBinFile *binfile = NULL;
RBinFile *bf = NULL;
RBinPlugin *plugin;
int xtr_idx = 0; // if 0, load all if xtr is used

Expand Down Expand Up @@ -412,9 +412,9 @@ static int r_core_file_load_for_debug(RCore *r, ut64 baseaddr, R_NULLABLE const
setbpint (r, "dbg.libs", "sym._dlclose");
#endif
}
binfile = r_bin_cur (r->bin);
r_core_bin_set_env (r, binfile);
plugin = r_bin_file_cur_plugin (binfile);
bf = r_bin_cur (r->bin);
r_core_bin_set_env (r, bf);
plugin = r_bin_file_cur_plugin (bf);
const char *plugin_name = plugin? plugin->meta.name: "";
if (!strcmp (plugin_name, "any")) {
// set use of raw strings
Expand All @@ -423,11 +423,11 @@ static int r_core_file_load_for_debug(RCore *r, ut64 baseaddr, R_NULLABLE const
// get bin.str.min
r->bin->minstrlen = r_config_get_i (r->config, "bin.str.min");
r->bin->maxstrbuf = r_config_get_i (r->config, "bin.str.maxbuf");
} else if (binfile) {
} else if (bf) {
RBinObject *obj = r_bin_cur_object (r->bin);
RBinInfo *info = obj? obj->info: NULL;
if (plugin && info) {
r_core_bin_set_arch_bits (r, binfile->file, info->arch, info->bits);
r_core_bin_set_arch_bits (r, bf->file, info->arch, info->bits);
}
}
if (!strcmp (plugin_name, "dex")) {
Expand Down Expand Up @@ -456,13 +456,27 @@ static int r_core_file_load_for_io_plugin(RCore *r, ut64 baseaddr, ut64 loadaddr
R_CRITICAL_LEAVE (r);
return false;
}
RBinFile *binfile = r_bin_cur (r->bin);
if (r_core_bin_set_env (r, binfile)) {
RBinFile *bf = r_bin_cur (r->bin);
if (r_core_bin_set_env (r, bf)) {
if (r->anal->verbose && !sdb_const_get (r->anal->sdb_cc, "default.cc", 0)) {
R_LOG_WARN ("No calling convention defined for this file, analysis may be inaccurate");
}
}
plugin = r_bin_file_cur_plugin (binfile);
if (bf) {
const char *bclass = R_UNWRAP4 (bf, bo, info, bclass);
if (bclass && strstr (bclass, "://")) {
// perform a redirection!
char *uri = r_str_newf ("%s%s\n", bclass, bf->file);
r_core_cmdf (r, "ob-*");
r_core_cmdf (r, "o %s", uri);
// r_core_cmdf (r, "o-%d", fd); // XXX segfault
free (uri);
// r_io_close_fd (r->io, fd);
// r_bin_file_close (r->bin, bf);
return false;
}
}
plugin = r_bin_file_cur_plugin (bf);
if (plugin && !strcmp (plugin->meta.name, "any")) {
RBinObject *obj = r_bin_cur_object (r->bin);
RBinInfo *info = obj? obj->info: NULL;
Expand All @@ -472,22 +486,21 @@ static int r_core_file_load_for_io_plugin(RCore *r, ut64 baseaddr, ut64 loadaddr
}
info->bits = r->rasm->config->bits;
// set use of raw strings
r_core_bin_set_arch_bits (r, binfile->file, info->arch, info->bits);
r_core_bin_set_arch_bits (r, bf->file, info->arch, info->bits);
// r_config_set_i (r->config, "io.va", false);
// r_config_set_b (r->config, "bin.str.raw", true);
// get bin.str.min
r->bin->minstrlen = r_config_get_i (r->config, "bin.str.min");
r->bin->maxstrbuf = r_config_get_i (r->config, "bin.str.maxbuf");
} else if (binfile) {
} else if (bf) {
RBinObject *obj = r_bin_cur_object (r->bin);
RBinInfo *info = obj? obj->info: NULL;
if (!info) {
R_CRITICAL_LEAVE (r);
return false;
}
if (plugin) {
r_core_bin_set_arch_bits (r, binfile->file,
info->arch, info->bits);
r_core_bin_set_arch_bits (r, bf->file, info->arch, info->bits);
}
}

Expand Down Expand Up @@ -657,6 +670,7 @@ R_API bool r_core_bin_load(RCore *r, const char *filenameuri, ut64 baddr) {
r->bin->maxstrbuf = r_config_get_i (r->config, "bin.str.maxbuf");
R_CRITICAL_LEAVE (r);
if (desc && is_io_load) {
int desc_fd = desc->fd;
// TODO? necessary to restore the desc back?
// Fix to select pid before trying to load the binary
if ((desc->plugin && desc->plugin->isdbg) || r_config_get_b (r->config, "cfg.debug")) {
Expand All @@ -666,12 +680,14 @@ R_API bool r_core_bin_load(RCore *r, const char *filenameuri, ut64 baddr) {
r_core_file_open (r, filenameuri, 0, baddr);
}
r_core_file_load_for_io_plugin (r, baddr, 0LL);
desc = r->io->desc;
}
r_io_use_fd (r->io, desc->fd);
r_io_use_fd (r->io, desc_fd);
} else if (desc != NULL) {
r_io_use_fd (r->io, desc->fd);
r_io_map_add (r->io, desc->fd, R_PERM_RWX, 0LL, 0, r_io_desc_size (desc));
}
desc = r->io->desc;
RBinFile *binfile = r_bin_cur (r->bin);
if (r->bin->cur && r->bin->cur->bo && r->bin->cur->bo->plugin && r->bin->cur->bo->plugin->strfilter) {
char msg[2];
Expand All @@ -692,7 +708,7 @@ R_API bool r_core_bin_load(RCore *r, const char *filenameuri, ut64 baddr) {
r_core_cmd (r, cmd_load, 0);
}

if (plugin && plugin->meta.name) {
if (desc && plugin && plugin->meta.name) {
if (!strcmp (plugin->meta.name, "any")) {
ut64 size = (desc->name && (r_str_startswith (desc->name, "rap") && strstr (desc->name, "://")))
? UT64_MAX : r_io_desc_size (desc);
Expand Down
1 change: 1 addition & 0 deletions libr/include/r_bin.h
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,7 @@ extern RBinPlugin r_bin_plugin_lua;
extern RBinPlugin r_bin_plugin_xtac;
extern RBinPlugin r_bin_plugin_pdp11;
extern RBinPlugin r_bin_plugin_pcap;
extern RBinPlugin r_bin_plugin_uf2;
extern RBinPlugin r_bin_plugin_io;

#ifdef __cplusplus
Expand Down
Loading