Rizin
unix-like reverse engineering framework and cli tools
bin_psxexe.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2015-2018 Dax89 <trogu.davide@gmail.com>
2 // SPDX-FileCopyrightText: 2015-2018 pancake <pancake@nopcode.org>
3 // SPDX-License-Identifier: LGPL-3.0-only
4 
5 #include <string.h>
6 #include <rz_types.h>
7 #include <rz_lib.h>
8 #include <rz_bin.h>
9 #include "../i/private.h"
10 #include "psxexe/psxexe.h"
11 
12 static bool check_buffer(RzBuffer *b) {
13  ut8 magic[PSXEXE_ID_LEN];
14  if (rz_buf_read_at(b, 0, magic, sizeof(magic)) == PSXEXE_ID_LEN) {
15  return !memcmp(magic, PSXEXE_ID, PSXEXE_ID_LEN);
16  }
17  return false;
18 }
19 
20 static bool load_buffer(RzBinFile *bf, RzBinObject *obj, RzBuffer *b, Sdb *sdb) {
21  return check_buffer(b);
22 }
23 
24 static RzBinInfo *info(RzBinFile *bf) {
25  RzBinInfo *ret = NULL;
26  psxexe_header psxheader;
27 
28  if (rz_buf_read_at(bf->buf, 0, (ut8 *)&psxheader, sizeof(psxexe_header)) < sizeof(psxexe_header)) {
29  RZ_LOG_ERROR("Truncated Header\n");
30  return NULL;
31  }
32 
33  if (!(ret = RZ_NEW0(RzBinInfo))) {
34  return NULL;
35  }
36 
37  ret->file = strdup(bf->file);
38  ret->type = strdup("Sony PlayStation 1 Executable");
39  ret->machine = strdup("Sony PlayStation 1");
40  ret->os = strdup("psx");
41  ret->arch = strdup("mips");
42  ret->bits = 32;
43  ret->has_va = true;
44  return ret;
45 }
46 
47 static RzList *sections(RzBinFile *bf) {
48  RzList *ret = NULL;
49  RzBinSection *sect = NULL;
50  psxexe_header psxheader;
51  ut64 sz = 0;
52 
53  if (!(ret = rz_list_new())) {
54  return NULL;
55  }
56 
57  if (!(sect = RZ_NEW0(RzBinSection))) {
58  rz_list_free(ret);
59  return NULL;
60  }
61 
62  if (rz_buf_fread_at(bf->buf, 0, (ut8 *)&psxheader, "8c17i", 1) < sizeof(psxexe_header)) {
63  RZ_LOG_ERROR("Truncated Header\n");
64  free(sect);
65  rz_list_free(ret);
66  return NULL;
67  }
68 
69  sz = rz_buf_size(bf->buf);
70 
71  sect->name = strdup("TEXT");
73  sect->size = sz - PSXEXE_TEXTSECTION_OFFSET;
74  sect->vaddr = psxheader.t_addr;
75  sect->vsize = psxheader.t_size;
76  sect->perm = RZ_PERM_RX;
77  sect->has_strings = true;
78 
79  rz_list_append(ret, sect);
80  return ret;
81 }
82 
83 static RzList *entries(RzBinFile *bf) {
84  RzList *ret = NULL;
85  RzBinAddr *addr = NULL;
86  psxexe_header psxheader;
87 
88  if (!(ret = rz_list_new())) {
89  return NULL;
90  }
91 
92  if (!(addr = RZ_NEW0(RzBinAddr))) {
93  rz_list_free(ret);
94  return NULL;
95  }
96 
97  if (rz_buf_fread_at(bf->buf, 0, (ut8 *)&psxheader, "8c17i", 1) < sizeof(psxexe_header)) {
98  RZ_LOG_ERROR("Truncated Header\n");
99  rz_list_free(ret);
100  free(addr);
101  return NULL;
102  }
103 
104  addr->paddr = (psxheader.pc0 - psxheader.t_addr) + PSXEXE_TEXTSECTION_OFFSET;
105  addr->vaddr = psxheader.pc0;
106 
107  rz_list_append(ret, addr);
108  return ret;
109 }
110 
111 static RzList *strings(RzBinFile *bf) {
112  // hardcode minstrlen = 20
113  return rz_bin_file_strings(bf, 20, true);
114 }
115 
117  .name = "psxexe",
118  .desc = "Sony PlayStation 1 Executable",
119  .license = "LGPL3",
120  .load_buffer = &load_buffer,
121  .check_buffer = &check_buffer,
122  .info = &info,
124  .sections = &sections,
125  .entries = &entries,
126  .strings = &strings,
127 };
128 
129 #ifndef RZ_PLUGIN_INCORE
132  .data = &rz_bin_plugin_psxexe,
134 };
135 #endif
RZ_API RZ_OWN RzList * rz_bin_file_strings(RZ_NONNULL RzBinFile *bf, size_t min_length, bool raw_strings)
Generates a RzList struct containing RzBinString from a given RzBinFile.
Definition: bfile_string.c:325
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_psxexe.c:20
static RzList * strings(RzBinFile *bf)
Definition: bin_psxexe.c:111
RZ_API RzLibStruct rizin_plugin
Definition: bin_psxexe.c:130
static bool check_buffer(RzBuffer *b)
Definition: bin_psxexe.c:12
static RzBinInfo * info(RzBinFile *bf)
Definition: bin_psxexe.c:24
RzBinPlugin rz_bin_plugin_psxexe
Definition: bin_psxexe.c:116
static RzList * entries(RzBinFile *bf)
Definition: bin_psxexe.c:83
static RzList * sections(RzBinFile *bf)
Definition: bin_psxexe.c:47
#define RZ_API
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
uint8_t ut8
Definition: lh5801.h:11
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 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
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
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")
#define PSXEXE_ID
Definition: psxexe.h:7
#define PSXEXE_ID_LEN
Definition: psxexe.h:8
#define PSXEXE_TEXTSECTION_OFFSET
Definition: psxexe.h:9
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 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
@ RZ_LIB_TYPE_BIN
Definition: rz_lib.h:75
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_PERM_RX
Definition: rz_types.h:97
#define RZ_VERSION
Definition: rz_version.h:8
#define b(i)
Definition: sha256.c:42
ut32 pc0
Definition: psxexe.h:17
ut32 t_addr
Definition: psxexe.h:19
ut32 t_size
Definition: psxexe.h:20
XX curplugin == o->plugin.
Definition: rz_bin.h:298
char * file
Definition: rz_bin.h:299
RzBuffer * buf
Definition: rz_bin.h:303
int has_va
Definition: rz_bin.h:228
char * type
Definition: rz_bin.h:211
char * os
Definition: rz_bin.h:219
char * machine
Definition: rz_bin.h:216
char * file
Definition: rz_bin.h:210
char * arch
Definition: rz_bin.h:214
char * name
Definition: rz_bin.h:509
char * version
Definition: rz_bin.h:512
char * name
Definition: rz_bin.h:619
bool has_strings
Definition: rz_bin.h:632
Definition: sdb.h:63
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int addr
Definition: z80asm.c:58