Rizin
unix-like reverse engineering framework and cli tools
bin_mbn.c File Reference
#include <rz_types.h>
#include <rz_util.h>
#include <rz_lib.h>
#include <rz_bin.h>

Go to the source code of this file.

Classes

struct  sbl_header
 

Typedefs

typedef struct sbl_header SblHeader
 

Functions

static bool check_buffer (RzBuffer *b)
 
static bool load_buffer (RzBinFile *bf, RzBinObject *obj, RzBuffer *b, Sdb *sdb)
 
static ut64 baddr (RzBinFile *bf)
 
static RzListentries (RzBinFile *bf)
 
static RzListsections (RzBinFile *bf)
 
static RzBinInfoinfo (RzBinFile *bf)
 
static ut64 size (RzBinFile *bf)
 

Variables

static SblHeader sb = { 0 }
 
RzBinPlugin rz_bin_plugin_mbn
 
RZ_API RzLibStruct rizin_plugin
 

Typedef Documentation

◆ SblHeader

typedef struct sbl_header SblHeader

Function Documentation

◆ baddr()

static ut64 baddr ( RzBinFile bf)
static

Definition at line 82 of file bin_mbn.c.

82  {
83  return sb.vaddr; // XXX
84 }
static SblHeader sb
Definition: bin_mbn.c:26
ut32 vaddr
Definition: bin_mbn.c:16

References sb, and sbl_header::vaddr.

◆ check_buffer()

static bool check_buffer ( RzBuffer b)
static

Definition at line 28 of file bin_mbn.c.

28  {
29  rz_return_val_if_fail(b, false);
30  ut64 bufsz = rz_buf_size(b);
31  if (sizeof(SblHeader) < bufsz) {
32  int ret = rz_buf_fread_at(b, 0, (ut8 *)&sb, "10i", 1);
33  if (!ret) {
34  return false;
35  }
36 #if 0
37  eprintf ("V=%d\n", sb.version);
38  eprintf ("PA=0x%08x sz=0x%x\n", sb.paddr, sb.psize);
39  eprintf ("VA=0x%08x sz=0x%x\n", sb.vaddr, sb.psize);
40  eprintf ("CODE=0x%08x\n", sb.code_pa + sb.vaddr + 40);
41  eprintf ("SIGN=0x%08x sz=0x%x\n", sb.sign_va, sb.sign_sz);
42  if (sb.cert_sz > 0) {
43  eprintf ("CERT=0x%08x sz=0x%x\n", sb.cert_va, sb.cert_sz);
44  } else {
45  eprintf ("No certificate found.\n");
46  }
47 #endif
48  if (sb.version != 3) { // NAND
49  return false;
50  }
51  if (sb.paddr + sizeof(SblHeader) > bufsz) { // NAND
52  return false;
53  }
54  if (sb.vaddr < 0x100 || sb.psize > bufsz) { // NAND
55  return false;
56  }
57  if (sb.cert_va < sb.vaddr) {
58  return false;
59  }
60  if (sb.cert_sz >= 0xf0000) {
61  return false;
62  }
63  if (sb.sign_va < sb.vaddr) {
64  return false;
65  }
66  if (sb.sign_sz >= 0xf0000) {
67  return false;
68  }
69  if (sb.load_index < 1 || sb.load_index > 0x40) {
70  return false; // should be 0x19 ?
71  }
72  // TODO: Add more checks here
73  return true;
74  }
75  return false;
76 }
uint8_t ut8
Definition: lh5801.h:11
#define eprintf(x, y...)
Definition: rlcc.c:7
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API st64 rz_buf_fread_at(RZ_NONNULL RzBuffer *b, ut64 addr, RZ_NONNULL ut8 *buf, RZ_NONNULL const char *fmt, int n)
...
Definition: buf.c:1001
RZ_API ut64 rz_buf_size(RZ_NONNULL RzBuffer *b)
Return the size of the buffer.
Definition: buf.c:1225
#define b(i)
Definition: sha256.c:42
ut32 sign_va
Definition: bin_mbn.c:19
ut32 code_pa
Definition: bin_mbn.c:18
ut32 paddr
Definition: bin_mbn.c:15
ut32 sign_sz
Definition: bin_mbn.c:20
ut32 cert_va
Definition: bin_mbn.c:21
ut32 version
Definition: bin_mbn.c:14
ut32 load_index
Definition: bin_mbn.c:13
ut32 cert_sz
Definition: bin_mbn.c:22
ut32 psize
Definition: bin_mbn.c:17
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References b, sbl_header::cert_sz, sbl_header::cert_va, sbl_header::code_pa, eprintf, sbl_header::load_index, sbl_header::paddr, sbl_header::psize, rz_buf_fread_at(), rz_buf_size(), rz_return_val_if_fail, sb, sbl_header::sign_sz, sbl_header::sign_va, ut64(), sbl_header::vaddr, and sbl_header::version.

Referenced by load_buffer().

◆ entries()

static RzList* entries ( RzBinFile bf)
static

Definition at line 86 of file bin_mbn.c.

86  {
87  RzList *ret = rz_list_newf(free);
88  ;
89  if (ret) {
90  RzBinAddr *ptr = RZ_NEW0(RzBinAddr);
91  if (ptr) {
92  ptr->paddr = 40 + sb.code_pa;
93  ptr->vaddr = 40 + sb.code_pa + sb.vaddr;
94  rz_list_append(ret, ptr);
95  }
96  }
97  return ret;
98 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
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_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
#define RZ_NEW0(x)
Definition: rz_types.h:284
ut64 vaddr
Definition: rz_bin.h:186
ut64 paddr
Definition: rz_bin.h:187

References sbl_header::code_pa, free(), rz_bin_addr_t::paddr, rz_list_append(), rz_list_newf(), RZ_NEW0, sb, sbl_header::vaddr, and rz_bin_addr_t::vaddr.

◆ info()

static RzBinInfo* info ( RzBinFile bf)
static

Definition at line 156 of file bin_mbn.c.

156  {
157  RzBinInfo *ret = NULL;
158  const int bits = 16;
159  if (!(ret = RZ_NEW0(RzBinInfo))) {
160  return NULL;
161  }
162  ret->file = strdup(bf->file);
163  ret->bclass = strdup("bootloader");
164  ret->rclass = strdup("mbn");
165  ret->os = strdup("MBN");
166  ret->arch = strdup("arm");
167  ret->machine = strdup(ret->arch);
168  ret->subsystem = strdup("mbn");
169  ret->type = strdup("sbl"); // secondary boot loader
170  ret->bits = bits;
171  ret->has_va = true;
172  ret->has_crypto = true; // must be false if there' no sign or cert sections
173  ret->has_pi = false;
174  ret->has_nx = false;
175  ret->big_endian = false;
176  ret->dbg_info = false;
177  return ret;
178 }
int bits(struct state *s, int need)
Definition: blast.c:72
#define NULL
Definition: cris-opc.c:27
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")
char * file
Definition: rz_bin.h:299
int has_va
Definition: rz_bin.h:228
char * type
Definition: rz_bin.h:211
int has_crypto
Definition: rz_bin.h:233
char * os
Definition: rz_bin.h:219
char * subsystem
Definition: rz_bin.h:220
int has_pi
Definition: rz_bin.h:229
int has_nx
Definition: rz_bin.h:234
char * machine
Definition: rz_bin.h:216
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

References rz_bin_info_t::arch, rz_bin_info_t::bclass, rz_bin_info_t::big_endian, rz_bin_info_t::bits, bits(), rz_bin_info_t::dbg_info, rz_bin_info_t::file, rz_bin_file_t::file, rz_bin_info_t::has_crypto, rz_bin_info_t::has_nx, rz_bin_info_t::has_pi, rz_bin_info_t::has_va, rz_bin_info_t::machine, NULL, rz_bin_info_t::os, rz_bin_info_t::rclass, RZ_NEW0, strdup(), rz_bin_info_t::subsystem, and rz_bin_info_t::type.

◆ load_buffer()

static bool load_buffer ( RzBinFile bf,
RzBinObject obj,
RzBuffer b,
Sdb sdb 
)
static

Definition at line 78 of file bin_mbn.c.

78  {
79  return check_buffer(b);
80 }
static bool check_buffer(RzBuffer *b)
Definition: bin_mbn.c:28

References b, and check_buffer().

◆ sections()

static RzList* sections ( RzBinFile bf)
static

Definition at line 100 of file bin_mbn.c.

100  {
101  RzBinSection *ptr = NULL;
102  RzList *ret = NULL;
103  int rc;
104 
105  if (!(ret = rz_list_new())) {
106  return NULL;
107  }
108  ret->free = free;
109  rc = rz_buf_fread_at(bf->buf, 0, (ut8 *)&sb, "10i", 1);
110  if (!rc) {
111  rz_list_free(ret);
112  return false;
113  }
114 
115  // add text segment
116  if (!(ptr = RZ_NEW0(RzBinSection))) {
117  return ret;
118  }
119  ptr->name = strdup("text");
120  ptr->size = sb.psize;
121  ptr->vsize = sb.psize;
122  ptr->paddr = sb.paddr + 40;
123  ptr->vaddr = sb.vaddr;
124  ptr->perm = RZ_PERM_RX; // r-x
125  ptr->has_strings = true;
126  rz_list_append(ret, ptr);
127 
128  if (!(ptr = RZ_NEW0(RzBinSection))) {
129  return ret;
130  }
131  ptr->name = strdup("sign");
132  ptr->size = sb.sign_sz;
133  ptr->vsize = sb.sign_sz;
134  ptr->paddr = sb.sign_va - sb.vaddr;
135  ptr->vaddr = sb.sign_va;
136  ptr->perm = RZ_PERM_R; // r--
137  ptr->has_strings = true;
138  rz_list_append(ret, ptr);
139 
140  if (sb.cert_sz && sb.cert_va > sb.vaddr) {
141  if (!(ptr = RZ_NEW0(RzBinSection))) {
142  return ret;
143  }
144  ptr->name = strdup("cert");
145  ptr->size = sb.cert_sz;
146  ptr->vsize = sb.cert_sz;
147  ptr->paddr = sb.cert_va - sb.vaddr;
148  ptr->vaddr = sb.cert_va;
149  ptr->perm = RZ_PERM_R; // r--
150  ptr->has_strings = true;
151  rz_list_append(ret, ptr);
152  }
153  return ret;
154 }
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 void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
#define RZ_PERM_R
Definition: rz_types.h:93
#define RZ_PERM_RX
Definition: rz_types.h:97
RzBuffer * buf
Definition: rz_bin.h:303
char * name
Definition: rz_bin.h:619
bool has_strings
Definition: rz_bin.h:632
RzListFree free
Definition: rz_list.h:21

References rz_bin_file_t::buf, sbl_header::cert_sz, sbl_header::cert_va, rz_list_t::free, free(), rz_bin_section_t::has_strings, rz_bin_section_t::name, NULL, sbl_header::paddr, rz_bin_section_t::paddr, rz_bin_section_t::perm, sbl_header::psize, rz_buf_fread_at(), rz_list_append(), rz_list_free(), rz_list_new(), RZ_NEW0, RZ_PERM_R, RZ_PERM_RX, sb, sbl_header::sign_sz, sbl_header::sign_va, rz_bin_section_t::size, strdup(), sbl_header::vaddr, rz_bin_section_t::vaddr, and rz_bin_section_t::vsize.

◆ size()

static ut64 size ( RzBinFile bf)
static

Definition at line 180 of file bin_mbn.c.

180  {
181  return sizeof(SblHeader) + sb.psize;
182 }
struct sbl_header SblHeader

References sbl_header::psize, and sb.

Variable Documentation

◆ rizin_plugin

RZ_API RzLibStruct rizin_plugin
Initial value:
= {
.type = RZ_LIB_TYPE_BIN,
}
RzBinPlugin rz_bin_plugin_mbn
Definition: bin_mbn.c:184
@ RZ_LIB_TYPE_BIN
Definition: rz_lib.h:75
#define RZ_VERSION
Definition: rz_version.h:8
char * version
Definition: rz_bin.h:512

Definition at line 200 of file bin_mbn.c.

◆ rz_bin_plugin_mbn

RzBinPlugin rz_bin_plugin_mbn
Initial value:
= {
.name = "mbn",
.desc = "MBN/SBL bootloader things",
.license = "LGPL3",
.minstrlen = 10,
.load_buffer = &load_buffer,
.size = &size,
.check_buffer = &check_buffer,
.baddr = &baddr,
.entries = &entries,
.sections = &sections,
.info = &info,
}
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 load_buffer(RzBinFile *bf, RzBinObject *obj, RzBuffer *b, Sdb *sdb)
Definition: bin_mbn.c:78
static ut64 size(RzBinFile *bf)
Definition: bin_mbn.c:180
static RzBinInfo * info(RzBinFile *bf)
Definition: bin_mbn.c:156
static ut64 baddr(RzBinFile *bf)
Definition: bin_mbn.c:82
static RzList * entries(RzBinFile *bf)
Definition: bin_mbn.c:86
static RzList * sections(RzBinFile *bf)
Definition: bin_mbn.c:100

Definition at line 184 of file bin_mbn.c.

◆ sb

SblHeader sb = { 0 }
static

Definition at line 26 of file bin_mbn.c.

Referenced by __op_refs(), __strbuf_append_col_aligned(), __strbuf_append_col_aligned_fancy(), _zip_stdio_op_stat(), analysis_mask_avr(), apprentice_sort(), argv_get_help(), avr_AAAAAbbb(), avr_AAdddddAAAA(), avr_AArrrrrAAAA(), avr_dddcrrr(), avr_ddddcccc(), avr_dddddcbbb(), avr_dddddcccc(), avr_dddddcccc_load32(), avr_dddddcccc_store32(), avr_dddddcccc_x(), avr_dddddcccc_xm(), avr_dddddcccc_xp(), avr_dddddcccc_y(), avr_dddddcccc_ym(), avr_dddddcccc_yp(), avr_dddddcccc_z(), avr_dddddcccc_zm(), avr_dddddcccc_zp(), avr_ddddrrrr(), avr_ddddrrrr_2x(), avr_disassembler(), avr_KKddKKKK(), avr_kkkddddkkkk_load16(), avr_kkkddddkkkk_store16(), avr_KKKKcccc(), avr_KKKKddddKKKK(), avr_kkkkkccck(), avr_kkkkkkkccc(), avr_kkkkkkkkkkkk(), avr_op(), avr_qcqqcdddddcqqq_y(), avr_qcqqcdddddcqqq_z(), avr_qcqqcrrrrrcqqq_y(), avr_qcqqcrrrrrcqqq_z(), avr_rdddddrrrr(), avr_rrrrrcbbb(), avr_rrrrrcccc_x(), avr_rrrrrcccc_xm(), avr_rrrrrcccc_xp(), avr_rrrrrcccc_y(), avr_rrrrrcccc_ym(), avr_rrrrrcccc_yp(), avr_rrrrrcccc_z(), avr_rrrrrcccc_zm(), avr_rrrrrcccc_zp(), avr_spmz(), avr_unique(), baddr(), byte_stringify(), bytecode_snprint(), calc_padding_len(), check_buffer(), class_stringify(), class_stringify_addr(), close_optionals(), clusterLoadConfig(), clusterSaveConfig(), cmd_print_fromage(), cmd_pxr(), compareSize(), decode_array(), decode_buffer(), DEFINE_HANDLE_TS_FCN_AND_SYMBOL(), demangle_any(), demangle_class_object(), demangle_method(), demangle_object_with_type(), demangle_type(), dex_resolve_proto_id(), diff_unified_append_data(), diff_unified_append_ranges(), diff_unified_lines_hl(), do_handle_ts_unescape_arg(), do_print_child_help(), drain(), ds_print_cmt_il(), dump_element(), entries(), entry_stringify(), field_stringify(), field_stringify_addr(), file_fsmagic(), file_or_fd(), fill_args(), fill_argv_modes_help_strbuf(), fill_children_chars(), fill_colored_args(), fill_details(), fill_details_cb(), fill_details_do(), fill_details_static(), fill_modes_children_chars(), fill_usage_strbuf(), fill_wrapped_comment(), flirt_pat_parse_line(), gdb_to_rz_profile(), group_get_help(), handle_skip_next_instruction(), hex_disasm_with_templates(), il_op_effect_resolve(), il_op_pure_resolve(), il_opdmp_bitv(), il_opdmp_blk(), il_opdmp_bool_false(), il_opdmp_bool_true(), il_opdmp_cast(), il_opdmp_goto(), il_opdmp_let(), il_opdmp_load(), il_opdmp_loadw(), il_opdmp_seq(), il_opdmp_seq_inner(), il_opdmp_set(), il_opdmp_store(), il_opdmp_storew(), il_opdmp_var(), import_stringify(), java_field_access_flags_readable(), java_method_access_flags_readable(), lang_byte_array_asm(), lang_byte_array_bash(), lang_byte_array_c_cpp(), lang_byte_array_golang(), lang_byte_array_java(), lang_byte_array_kotlin(), lang_byte_array_nodejs(), lang_byte_array_objective_c_cpp(), lang_byte_array_rizin(), libs_stringify(), line_sample_cmp(), line_stringify(), lua54_disasm(), mk_reg_str(), pad(), parse(), parse_localvar(), parsed_args_iterateargs(), print_addr(), print_and_check_il(), print_byte(), print_child_help(), print_cursor_l(), print_cursor_r(), print_section(), prompt_add_file(), prompt_add_offset(), prompt_add_section(), read_to_strbuf(), replace_directives_for(), rz_analysis_cc_del(), rz_analysis_cc_get(), rz_analysis_cc_set_error(), rz_analysis_cc_set_self(), rz_analysis_data_to_string(), rz_analysis_var_get_constraints_readable(), rz_analysis_var_global_get_constraints_readable(), rz_asn1_print_hexdump_padded(), rz_asn1_print_padded(), rz_asn1_to_string(), rz_axml_decode(), rz_bin_dex_access_flags_readable(), rz_bin_java_class_access_flags_readable(), rz_bin_java_class_as_source_code(), rz_bin_java_class_as_text(), rz_bin_java_class_const_pool_as_text(), rz_bin_java_class_fields_as_text(), rz_bin_java_class_interfaces_as_text(), rz_bin_java_class_methods_as_text(), rz_cmd_get_help_json(), rz_cmd_get_help_strbuf(), rz_cmd_javac_handler(), rz_cmd_javaf_handler(), rz_cmd_javai_handler(), rz_cmd_javam_handler(), rz_cmd_javap_handler(), rz_cmd_javas_handler(), rz_cmd_parsed_args_argstr(), rz_cmd_parsed_args_execstr(), rz_cons_isatty(), rz_core_analysis_all_vars_display(), rz_core_analysis_il_step_with_events(), rz_core_analysis_var_display(), rz_core_bin_sections_mapping_print(), rz_core_print_hexdump_byline_str(), rz_core_print_string_c_cpp(), rz_core_visual_view_rop(), rz_diff_unified_text(), rz_il_event_stringify(), rz_il_op_effect_stringify(), rz_il_op_pure_stringify(), rz_io_zip_alloc_zipfileobj(), rz_io_zip_create_new_file(), rz_io_zip_get_by_file_idx(), rz_io_zip_get_files(), rz_io_zip_slurp_file(), rz_parse_pseudocode(), rz_pkcs7_cms_to_string(), rz_print_addr(), rz_print_byte(), rz_print_hexdiff_str(), rz_print_hexdump_str(), rz_print_section_str(), rz_protobuf_decode(), rz_pseudo_convert(), rz_run_get_environ_profile(), rz_sign_flirt_write_string_pattern_to_buffer(), rz_str_array_join(), rz_str_format_msvc_argv(), rz_str_list_join(), rz_str_stringify_raw_buffer(), rz_str_trim_lines(), rz_str_version(), rz_str_widget_list(), rz_strbuf_append(), rz_strbuf_append_n(), rz_strbuf_appendf(), rz_strbuf_drain(), rz_strbuf_drain_nofree(), rz_strbuf_equals(), rz_strbuf_fini(), rz_strbuf_free(), rz_strbuf_get(), rz_strbuf_getbin(), rz_strbuf_init(), rz_strbuf_initf(), rz_strbuf_is_empty(), rz_strbuf_length(), rz_strbuf_prepend(), rz_strbuf_reserve(), rz_strbuf_set(), rz_strbuf_setbin(), rz_strbuf_setf(), rz_strbuf_setptr(), rz_strbuf_slice(), rz_strbuf_vappendf(), rz_strbuf_vsetf(), rz_table_tocsv(), rz_table_tofancystring(), rz_table_tosimplestring(), rz_x509_certificate_dump(), rz_x509_crl_to_string(), rz_x509_crlentry_dump(), rz_x509_extensions_dump(), rz_x509_name_dump(), rz_x509_signedinfo_dump(), rz_x509_subjectpublickeyinfo_dump(), rz_x509_tbscertificate_dump(), rz_x509_validity_dump(), section_stringify(), section_stringify_addr(), sections(), shifted_reg64_append(), show_config_options(), showfile(), sigdb_entry_cmp(), size(), strbuf_append(), strbuf_append_calc(), strbuf_free(), strbuf_rev_append_char(), strbuf_rev_prepend_char(), string_stringify(), string_stringify_addr(), symbol_stringify(), symbol_stringify_addr(), ut64_to_hex(), uv__tty_is_slave(), walk_namespace(), wasm_dis(), zip_source_file_common_new(), and zstat().