Rizin
unix-like reverse engineering framework and cli tools
bin_bootimg.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  BootImageObj
 

Macros

#define BOOT_MAGIC   "ANDROID!"
 
#define BOOT_MAGIC_SIZE   8
 
#define BOOT_NAME_SIZE   16
 
#define BOOT_ARGS_SIZE   512
 
#define BOOT_EXTRA_ARGS_SIZE   1024
 
#define ADD_REMAINDER(val, aln)   ((val) + ((aln) != 0 ? ((val) % (aln)) : 0))
 
#define ROUND_DOWN(val, aln)   ((aln) != 0 ? (((val) / (aln)) * (aln)) : (val))
 

Typedefs

typedef struct boot_img_hdr BootImage
 

Functions

 RZ_PACKED (struct boot_img_hdr { ut8 magic[BOOT_MAGIC_SIZE];ut32 kernel_size;ut32 kernel_addr;ut32 ramdisk_size;ut32 ramdisk_addr;ut32 second_size;ut32 second_addr;ut32 tags_addr;ut32 page_size;ut32 unused[2];ut8 name[BOOT_NAME_SIZE];ut8 cmdline[BOOT_ARGS_SIZE];ut32 id[8];ut8 extra_cmdline[BOOT_EXTRA_ARGS_SIZE];})
 
static int bootimg_header_load (BootImageObj *obj, Sdb *db)
 
static Sdbget_sdb (RzBinFile *bf)
 
static bool load_buffer (RzBinFile *bf, RzBinObject *obj, RzBuffer *buf, Sdb *sdb)
 
static void destroy (RzBinFile *bf)
 
static ut64 baddr (RzBinFile *bf)
 
static RzListstrings (RzBinFile *bf)
 
static RzBinInfoinfo (RzBinFile *bf)
 
static bool check_buffer (RzBuffer *buf)
 
static RzListentries (RzBinFile *bf)
 
static RzListsections (RzBinFile *bf)
 

Variables

RzBinPlugin rz_bin_plugin_bootimg
 
RZ_API RzLibStruct rizin_plugin
 

Macro Definition Documentation

◆ ADD_REMAINDER

#define ADD_REMAINDER (   val,
  aln 
)    ((val) + ((aln) != 0 ? ((val) % (aln)) : 0))

Definition at line 17 of file bin_bootimg.c.

◆ BOOT_ARGS_SIZE

#define BOOT_ARGS_SIZE   512

Definition at line 14 of file bin_bootimg.c.

◆ BOOT_EXTRA_ARGS_SIZE

#define BOOT_EXTRA_ARGS_SIZE   1024

Definition at line 15 of file bin_bootimg.c.

◆ BOOT_MAGIC

#define BOOT_MAGIC   "ANDROID!"

Definition at line 11 of file bin_bootimg.c.

◆ BOOT_MAGIC_SIZE

#define BOOT_MAGIC_SIZE   8

Definition at line 12 of file bin_bootimg.c.

◆ BOOT_NAME_SIZE

#define BOOT_NAME_SIZE   16

Definition at line 13 of file bin_bootimg.c.

◆ ROUND_DOWN

#define ROUND_DOWN (   val,
  aln 
)    ((aln) != 0 ? (((val) / (aln)) * (aln)) : (val))

Definition at line 18 of file bin_bootimg.c.

Typedef Documentation

◆ BootImage

typedef struct boot_img_hdr BootImage

Definition at line 1 of file bin_bootimg.c.

Function Documentation

◆ baddr()

static ut64 baddr ( RzBinFile bf)
static

Definition at line 114 of file bin_bootimg.c.

114  {
115  BootImageObj *bio = bf->o->bin_obj;
116  return bio ? bio->bi.kernel_addr : 0;
117 }
BootImage bi
Definition: bin_bootimg.c:47
RzBinObject * o
Definition: rz_bin.h:305
void * bin_obj
Definition: rz_bin.h:293

References BootImageObj::bi, rz_bin_object_t::bin_obj, and rz_bin_file_t::o.

◆ bootimg_header_load()

static int bootimg_header_load ( BootImageObj obj,
Sdb db 
)
static

Definition at line 51 of file bin_bootimg.c.

51  {
52  char *n;
53  int i;
54  if (rz_buf_size(obj->buf) < sizeof(BootImage)) {
55  return false;
56  }
57  // TODO make it endian-safe (void)rz_buf_fread_at (buf, 0, (ut8*)bi, "IIiiiiiiiiiiii", 1);
58  BootImage *bi = &obj->bi;
59  (void)rz_buf_read_at(obj->buf, 0, (ut8 *)bi, sizeof(BootImage));
60  if ((n = rz_str_ndup((char *)bi->name, BOOT_NAME_SIZE))) {
61  sdb_set(db, "name", n, 0);
62  free(n);
63  }
64  if ((n = rz_str_ndup((char *)bi->cmdline, BOOT_ARGS_SIZE))) {
65  sdb_set(db, "cmdline", n, 0);
66  free(n);
67  }
68  for (i = 0; i < 8; i++) {
69  sdb_num_set(db, "id", (ut64)bi->id[i], 0);
70  }
71  if ((n = rz_str_ndup((char *)bi->extra_cmdline, BOOT_EXTRA_ARGS_SIZE))) {
72  sdb_set(db, "extra_cmdline", n, 0);
73  free(n);
74  }
75  return true;
76 }
lzma_index ** i
Definition: index.h:629
#define BOOT_EXTRA_ARGS_SIZE
Definition: bin_bootimg.c:15
struct boot_img_hdr BootImage
Definition: bin_bootimg.c:9
#define BOOT_NAME_SIZE
Definition: bin_bootimg.c:13
#define BOOT_ARGS_SIZE
Definition: bin_bootimg.c:14
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
uint8_t ut8
Definition: lh5801.h:11
int n
Definition: mipsasm.c:19
RZ_API int sdb_num_set(Sdb *s, const char *key, ut64 v, ut32 cas)
Definition: num.c:25
RZ_API st64 rz_buf_read_at(RZ_NONNULL RzBuffer *b, ut64 addr, RZ_NONNULL RZ_OUT ut8 *buf, ut64 len)
Read len bytes of the buffer at the specified address.
Definition: buf.c:1136
RZ_API ut64 rz_buf_size(RZ_NONNULL RzBuffer *b)
Return the size of the buffer.
Definition: buf.c:1225
RZ_API char * rz_str_ndup(RZ_NULLABLE const char *ptr, int len)
Create new copy of string ptr limited to size len.
Definition: str.c:1006
RZ_API int sdb_set(Sdb *s, const char *key, const char *val, ut32 cas)
Definition: sdb.c:611
RzBuffer * buf
Definition: bin_bootimg.c:48
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References BootImageObj::bi, BOOT_ARGS_SIZE, BOOT_EXTRA_ARGS_SIZE, BOOT_NAME_SIZE, BootImageObj::buf, free(), i, n, rz_buf_read_at(), rz_buf_size(), rz_str_ndup(), sdb_num_set(), sdb_set(), and ut64().

Referenced by load_buffer().

◆ check_buffer()

static bool check_buffer ( RzBuffer buf)
static

Definition at line 149 of file bin_bootimg.c.

149  {
150  ut8 tmp[13];
151  int r = rz_buf_read_at(buf, 0, tmp, sizeof(tmp));
152  return r > 12 && !strncmp((const char *)tmp, "ANDROID!", 8);
153 }
#define r
Definition: crypto_rc6.c:12
voidpf void * buf
Definition: ioapi.h:138

References r, rz_buf_read_at(), and autogen_x86imm::tmp.

◆ destroy()

static void destroy ( RzBinFile bf)
static

Definition at line 108 of file bin_bootimg.c.

108  {
109  BootImageObj *bio = bf->o->bin_obj;
110  rz_buf_free(bio->buf);
111  RZ_FREE(bf->o->bin_obj);
112 }
RZ_API void rz_buf_free(RzBuffer *b)
Free all internal data hold by the buffer and the buffer.
Definition: buf.c:1253
#define RZ_FREE(x)
Definition: rz_types.h:369

References rz_bin_object_t::bin_obj, BootImageObj::buf, rz_bin_file_t::o, rz_buf_free(), and RZ_FREE.

◆ entries()

static RzList* entries ( RzBinFile bf)
static

Definition at line 155 of file bin_bootimg.c.

155  {
156  BootImageObj *bio = bf->o->bin_obj;
157  RzBinAddr *ptr = NULL;
158  if (!bio) {
159  return NULL;
160  }
161  BootImage *bi = &bio->bi;
162  RzList *ret;
163 
164  if (!(ret = rz_list_newf(free))) {
165  return NULL;
166  }
167  if (!(ptr = RZ_NEW0(RzBinAddr))) {
168  return ret;
169  }
170  ptr->paddr = bi->page_size;
171  ptr->vaddr = bi->kernel_addr;
172  rz_list_append(ret, ptr);
173  return ret;
174 }
#define NULL
Definition: cris-opc.c:27
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 BootImageObj::bi, rz_bin_object_t::bin_obj, free(), NULL, rz_bin_file_t::o, rz_bin_addr_t::paddr, rz_list_append(), rz_list_newf(), RZ_NEW0, and rz_bin_addr_t::vaddr.

◆ get_sdb()

static Sdb* get_sdb ( RzBinFile bf)
static

Definition at line 78 of file bin_bootimg.c.

78  {
79  RzBinObject *o = bf->o;
80  BootImageObj *ao;
81  if (!o) {
82  return NULL;
83  }
84  ao = o->bin_obj;
85  return ao ? ao->kv : NULL;
86 }

References rz_bin_object_t::bin_obj, BootImageObj::kv, NULL, and rz_bin_file_t::o.

◆ info()

static RzBinInfo* info ( RzBinFile bf)
static

Definition at line 123 of file bin_bootimg.c.

123  {
124  RzBinInfo *ret;
125  if (!bf || !bf->o || !bf->o->bin_obj) {
126  return NULL;
127  }
128  ret = RZ_NEW0(RzBinInfo);
129  if (!ret) {
130  return NULL;
131  }
132 
133  ret->lang = NULL;
134  ret->file = bf->file ? strdup(bf->file) : NULL;
135  ret->type = strdup("Android Boot Image");
136  ret->os = strdup("android");
137  ret->subsystem = strdup("unknown");
138  ret->machine = strdup("arm");
139  ret->arch = strdup("arm");
140  ret->has_va = 1;
141  ret->has_pi = 0;
142  ret->bits = 16;
143  ret->big_endian = 0;
144  ret->dbg_info = 0;
145  ret->rclass = strdup("image");
146  return ret;
147 }
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
char * os
Definition: rz_bin.h:219
char * subsystem
Definition: rz_bin.h:220
int has_pi
Definition: rz_bin.h:229
char * machine
Definition: rz_bin.h:216
const char * lang
Definition: rz_bin.h:224
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::big_endian, rz_bin_object_t::bin_obj, rz_bin_info_t::bits, rz_bin_info_t::dbg_info, rz_bin_info_t::file, rz_bin_file_t::file, rz_bin_info_t::has_pi, rz_bin_info_t::has_va, rz_bin_info_t::lang, rz_bin_info_t::machine, NULL, rz_bin_file_t::o, 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 buf,
Sdb sdb 
)
static

Definition at line 88 of file bin_bootimg.c.

88  {
90  if (!bio) {
91  return false;
92  }
93  bio->kv = sdb_new0();
94  if (!bio->kv) {
95  free(bio);
96  return false;
97  }
98  bio->buf = rz_buf_ref(buf);
99  if (!bootimg_header_load(bio, bio->kv)) {
100  free(bio);
101  return false;
102  }
103  sdb_ns_set(sdb, "info", bio->kv);
104  obj->bin_obj = bio;
105  return true;
106 }
static int bootimg_header_load(BootImageObj *obj, Sdb *db)
Definition: bin_bootimg.c:51
RZ_API int sdb_ns_set(Sdb *s, const char *name, Sdb *r)
Definition: ns.c:156
RZ_API RzBuffer * rz_buf_ref(RzBuffer *b)
Increment the reference count of the buffer.
Definition: buf.c:668
RZ_API Sdb * sdb_new0(void)
Definition: sdb.c:43

References rz_bin_object_t::bin_obj, bootimg_header_load(), BootImageObj::buf, free(), BootImageObj::kv, rz_buf_ref(), RZ_NEW0, sdb_new0(), and sdb_ns_set().

◆ RZ_PACKED()

RZ_PACKED ( struct boot_img_hdr { ut8 magic[BOOT_MAGIC_SIZE];ut32 kernel_size;ut32 kernel_addr;ut32 ramdisk_size;ut32 ramdisk_addr;ut32 second_size;ut32 second_addr;ut32 tags_addr;ut32 page_size;ut32 unused[2];ut8 name[BOOT_NAME_SIZE];ut8 cmdline[BOOT_ARGS_SIZE];ut32 id[8];ut8 extra_cmdline[BOOT_EXTRA_ARGS_SIZE];}  )

◆ sections()

static RzList* sections ( RzBinFile bf)
static

Definition at line 176 of file bin_bootimg.c.

176  {
177  BootImageObj *bio = bf->o->bin_obj;
178  if (!bio) {
179  return NULL;
180  }
181  BootImage *bi = &bio->bi;
182  RzList *ret = NULL;
183  RzBinSection *ptr = NULL;
184 
185  if (!(ret = rz_list_new())) {
186  return NULL;
187  }
188  ret->free = free;
189 
190  if (!(ptr = RZ_NEW0(RzBinSection))) {
191  return ret;
192  }
193  ptr->name = strdup("header");
194  ptr->size = sizeof(BootImage);
195  ptr->vsize = bi->page_size;
196  ptr->paddr = 0;
197  ptr->vaddr = 0;
198  ptr->perm = RZ_PERM_R; // r--
199  rz_list_append(ret, ptr);
200 
201  if (!(ptr = RZ_NEW0(RzBinSection))) {
202  return ret;
203  }
204  ptr->name = strdup("kernel");
205  ptr->size = bi->kernel_size;
206  ptr->vsize = ADD_REMAINDER(ptr->size, bi->page_size);
207  ptr->paddr = bi->page_size;
208  ptr->vaddr = bi->kernel_addr;
209  ptr->perm = RZ_PERM_R; // r--
210  rz_list_append(ret, ptr);
211 
212  if (bi->ramdisk_size > 0) {
213  ut64 base = bi->kernel_size + 2 * bi->page_size - 1;
214  if (!(ptr = RZ_NEW0(RzBinSection))) {
215  return ret;
216  }
217  ptr->name = strdup("ramdisk");
218  ptr->size = bi->ramdisk_size;
219  ptr->vsize = ADD_REMAINDER(bi->ramdisk_size, bi->page_size);
220  ptr->paddr = ROUND_DOWN(base, bi->page_size);
221  ptr->vaddr = bi->ramdisk_addr;
222  ptr->perm = RZ_PERM_RX; // r-x
223  rz_list_append(ret, ptr);
224  }
225 
226  if (bi->second_size > 0) {
227  ut64 base = bi->kernel_size + bi->ramdisk_size + 2 * bi->page_size - 1;
228  if (!(ptr = RZ_NEW0(RzBinSection))) {
229  return ret;
230  }
231  ptr->name = strdup("second");
232  ptr->size = bi->second_size;
233  ptr->vsize = ADD_REMAINDER(bi->second_size, bi->page_size);
234  ptr->paddr = ROUND_DOWN(base, bi->page_size);
235  ptr->vaddr = bi->second_addr;
236  ptr->perm = RZ_PERM_RX; // r-x
237  rz_list_append(ret, ptr);
238  }
239 
240  return ret;
241 }
#define ADD_REMAINDER(val, aln)
Definition: bin_bootimg.c:17
#define ROUND_DOWN(val, aln)
Definition: bin_bootimg.c:18
RZ_API RZ_OWN RzList * rz_list_new(void)
Returns a new initialized RzList pointer (free method is not initialized)
Definition: list.c:235
#define RZ_PERM_R
Definition: rz_types.h:93
#define RZ_PERM_RX
Definition: rz_types.h:97
char * name
Definition: rz_bin.h:619
RzListFree free
Definition: rz_list.h:21

References ADD_REMAINDER, BootImageObj::bi, rz_bin_object_t::bin_obj, rz_list_t::free, free(), rz_bin_section_t::name, NULL, rz_bin_file_t::o, rz_bin_section_t::paddr, rz_bin_section_t::perm, ROUND_DOWN, rz_list_append(), rz_list_new(), RZ_NEW0, RZ_PERM_R, RZ_PERM_RX, rz_bin_section_t::size, strdup(), ut64(), rz_bin_section_t::vaddr, and rz_bin_section_t::vsize.

◆ strings()

static RzList* strings ( RzBinFile bf)
static

Definition at line 119 of file bin_bootimg.c.

119  {
120  return NULL;
121 }

References NULL.

Variable Documentation

◆ rizin_plugin

RZ_API RzLibStruct rizin_plugin
Initial value:
= {
.type = RZ_LIB_TYPE_BIN,
}
RzBinPlugin rz_bin_plugin_bootimg
Definition: bin_bootimg.c:243
@ 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 260 of file bin_bootimg.c.

◆ rz_bin_plugin_bootimg

RzBinPlugin rz_bin_plugin_bootimg
Initial value:
= {
.name = "bootimg",
.desc = "Android Boot Image",
.license = "LGPL3",
.get_sdb = &get_sdb,
.load_buffer = &load_buffer,
.destroy = &destroy,
.check_buffer = &check_buffer,
.baddr = &baddr,
.sections = &sections,
.entries = entries,
.strings = &strings,
.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 Sdb * get_sdb(RzBinFile *bf)
Definition: bin_bootimg.c:78
static bool check_buffer(RzBuffer *buf)
Definition: bin_bootimg.c:149
static bool load_buffer(RzBinFile *bf, RzBinObject *obj, RzBuffer *buf, Sdb *sdb)
Definition: bin_bootimg.c:88
static RzList * strings(RzBinFile *bf)
Definition: bin_bootimg.c:119
static void destroy(RzBinFile *bf)
Definition: bin_bootimg.c:108
static RzBinInfo * info(RzBinFile *bf)
Definition: bin_bootimg.c:123
static ut64 baddr(RzBinFile *bf)
Definition: bin_bootimg.c:114
static RzList * entries(RzBinFile *bf)
Definition: bin_bootimg.c:155
static RzList * sections(RzBinFile *bf)
Definition: bin_bootimg.c:176

Definition at line 243 of file bin_bootimg.c.