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

Add iP command to dsc ##io #23090

Merged
merged 4 commits into from
Jul 4, 2024
Merged
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
121 changes: 121 additions & 0 deletions libr/io/p/io_dsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ typedef struct {
RIO *io_backref;
RIODscSlices slices;
ut64 total_size;
ut64 last_seek;
} RIODscObject;

typedef enum {
Expand Down Expand Up @@ -254,6 +255,123 @@ static ut64 __lseek_dsc(RIO *io, RIODesc *fd, ut64 offset, int whence) {
return r_io_dsc_object_seek (io, (RIODscObject *)fd->data, offset, whence);
}

static char *__system(RIO *io, RIODesc *fd, const char *command) {
r_return_val_if_fail (io && fd && fd->data && command, NULL);
RIODscObject *dsc = (RIODscObject*) fd->data;

if (r_str_startswith (command, "iP")) {
PJ *pj = pj_new ();
if (!pj) {
return NULL;
}

ut64 size = 8;
if (command[2] == ' ') {
size = r_num_math (NULL, command + 3);
}

ut64 paddr = dsc->last_seek;

RList * slices = r_io_dsc_object_get_slices_by_range (dsc, paddr, size);
if (!slices) {
return NULL;
}

RListIter * iter;
RIODscTrimmedSlice * trimmed;

pj_a (pj);

r_list_foreach (slices, iter, trimmed) {
RList * infos = r_io_dsc_slice_get_rebase_infos_by_range (trimmed->slice, trimmed->seek, trimmed->count);
if (!infos) {
return NULL;
}

RListIter * iter;
RIODscTrimmedRebaseInfo * trimmed_info;

r_list_foreach (infos, iter, trimmed_info) {
ut64 remaining_size = trimmed_info->count;
ut64 cursor = 0;
while (remaining_size > 0) {
ut8 raw_value_buf[8];
bool got_raw_value;
ut64 off_local = trimmed_info->off_local + cursor;
RIO_FREAD_AT_INTO_DIRECT (trimmed->slice->fd, off_local, &raw_value_buf, 8, got_raw_value);
remaining_size -= 8;
cursor += 8;
if (!got_raw_value) {
R_LOG_ERROR ("reading raw pointer");
break;
}

ut64 raw_value = r_read_le64 (raw_value_buf);

pj_o (pj);

char * tmp = r_str_newf ("0x%"PFMT64x, trimmed->slice->start + off_local);
pj_ks (pj, "paddr", tmp);
free (tmp);

tmp = r_str_newf ("0x%"PFMT64x, raw_value);
pj_ks (pj, "raw", tmp);
free (tmp);

tmp = r_str_newf ("v%d", trimmed_info->info->info->version);
pj_ks (pj, "format", tmp);
free (tmp);

switch (trimmed_info->info->info->version) {
case 1:
case 2:
case 4:
break;
case 3:
if (R_IS_PTR_AUTHENTICATED (raw_value)) {
bool has_diversity = (raw_value & (1ULL << 48)) != 0;
pj_kb (pj, "has_diversity", has_diversity);
if (has_diversity) {
ut64 diversity = (raw_value >> 32) & 0xFFFF;
pj_kn (pj, "diversity", diversity);
}
ut64 key = (raw_value >> 49) & 3;
const char * names[4] = { "ia", "ib", "da", "db" };
pj_ks (pj, "key", names[key]);
}
break;
case 5:
if (R_IS_PTR_AUTHENTICATED (raw_value)) {
bool has_diversity = (raw_value & (1ULL << 50)) != 0;
pj_kb (pj, "has_diversity", has_diversity);
if (has_diversity) {
ut64 diversity = (raw_value >> 34) & 0xFFFF;
pj_kn (pj, "diversity", diversity);
}
ut64 key = (raw_value >> 51) & 1;
const char * names[2] = { "ia", "da" };
pj_ks (pj, "key", names[key]);
}
break;
default:
R_LOG_ERROR ("Unsupported rebase info version %d", trimmed_info->info->info->version);
}
pj_end (pj);
}
}
r_list_free (infos);
}

r_list_free (slices);

pj_end (pj);

return pj_drain (pj);
}

return NULL;
}

static RIODscObject *r_io_dsc_object_new(RIO *io, const char *filename, int perm, int mode) {
r_return_val_if_fail (io && filename, NULL);

Expand Down Expand Up @@ -707,6 +825,8 @@ static ut64 r_io_dsc_object_seek(RIO *io, RIODscObject *dsc, ut64 offset, int wh

io->off = off_local + slice->start;

dsc->last_seek = io->off;

return io->off;
}

Expand Down Expand Up @@ -1611,6 +1731,7 @@ RIOPlugin r_io_plugin_dsc = {
.read = __read,
.check = __check,
.seek = __lseek_dsc,
.system = __system,
#if R2__UNIX__
.is_blockdevice = __is_blockdevice,
#endif
Expand Down
Loading