Skip to content

Commit

Permalink
Merge pull request #510 from israpps/ioprp
Browse files Browse the repository at this point in the history
add romimg to tools & recipe for IOPRP Images
  • Loading branch information
rickgaiser authored Feb 23, 2024
2 parents f5db5e0 + 59af0e9 commit 08181ea
Show file tree
Hide file tree
Showing 12 changed files with 1,219 additions and 0 deletions.
9 changes: 9 additions & 0 deletions samples/Makefile.eeglobal_sample
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,12 @@ $(EE_ERL): $(EE_OBJS)
$(EE_LIB): $(EE_OBJS)
$(DIR_GUARD)
$(EE_AR) cru $(EE_LIB) $(EE_OBJS)

$(IOPRP_BIN): $(IOPRP_CONTENTS)
ifeq (_$(IOPRP_CONTENTS)_,__)
$(error Cannot generate IOPRP if 'IOPRP_CONTENTS' variable is empty)
else
$(DIR_GUARD)
romimg -c $@ $<
endif

1 change: 1 addition & 0 deletions tools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ SUBDIRS = \
bin2c \
ps2-irxgen \
ps2adpcm \
romimg \
# gensymtab

include $(PS2SDKSRC)/Defs.make
Expand Down
15 changes: 15 additions & 0 deletions tools/romimg/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# _____ ___ ____ ___ ____
# ____| | ____| | | |____|
# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
#-----------------------------------------------------------------------
# Copyright 2001-2022, ps2dev - http://www.ps2dev.org
# Licenced under Academic Free License version 2.0
# Review ps2sdk README & LICENSE files for further details.
# ROMIMG was made by @sp193

TOOLS_OBJS = main.o platform.o romimg.o SonyRX.o

include $(PS2SDKSRC)/Defs.make
include $(PS2SDKSRC)/tools/Rules.bin.make
include $(PS2SDKSRC)/tools/Rules.make
include $(PS2SDKSRC)/tools/Rules.release
122 changes: 122 additions & 0 deletions tools/romimg/src/ELF.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
typedef unsigned char u8;
typedef unsigned short int u16;
typedef unsigned int u32;

/* ELF-loading stuff */
#define ELF_MAGIC 0x464c457f
#define ELF_TYPE_IRX 0xFF80 /* SCE IOP Relocatable eXcutable file version 1.0 */
#define ELF_TYPE_ERX2 0xFF91 /* SCE EE Relocatable eXcutable file version 2.0 */
#define ELF_PT_LOAD 1

/*------------------------------*/
typedef struct
{
u8 ident[16]; /* Structure of a ELF header */
u16 type;
u16 machine;
u32 version;
u32 entry;
u32 phoff;
u32 shoff;
u32 flags;
u16 ehsize;
u16 phentsize;
u16 phnum;
u16 shentsize;
u16 shnum;
u16 shstrndx;
} elf_header_t;
/*------------------------------*/
typedef struct
{
u32 type; /* Structure of a header a sections in an ELF */
u32 offset;
void *vaddr;
u32 paddr;
u32 filesz;
u32 memsz;
u32 flags;
u32 align;
} elf_pheader_t;

typedef struct
{
u32 name;
u32 type;
u32 flags;
u32 addr;
u32 offset;
u32 size;
u32 link;
u32 info;
u32 addralign;
u32 entsize;
} elf_shdr_t;

typedef struct
{
u32 offset;
u32 info;
} elf_rel;

typedef struct
{
u32 offset;
u32 info;
u32 addend;
} elf_rela;

enum ELF_SHT_types {
SHT_NULL = 0,
SHT_PROGBITS,
SHT_SYMTAB,
SHT_STRTAB,
SHT_RELA,
SHT_HASH,
SHT_DYNAMIC,
SHT_NOTE,
SHT_NOBITS,
SHT_REL,
SHT_SHLIB,
SHT_DYNSYM
};

typedef struct iopmod_struct
{
u32 moduleinfo;
u32 entry;
u32 gp_value;
u32 text_size;
u32 data_size;
u32 bss_size;
u16 version;
char modname[];
} iopmod_t;

typedef struct eemod_struct
{
u32 moduleinfo;
u32 entry;
u32 gp_value;
u32 text_size;
u32 data_size;
u32 bss_size;
u32 ERX_lib_addr;
u32 ERX_lib_size;
u32 ERX_stub_addr;
u32 ERX_stub_size;
u16 version;
char modname[];
} eemod_t;

#define SHT_LOPROC 0x70000000
#define SHT_LOPROC_IOPMOD_TAB 0x80
#define SHT_LOPROC_EEMOD_TAB 0x90
#define SHT_HIPROC 0x7fffffff
#define SHT_LOUSER 0x80000000
#define SHT_HIUSER 0xffffffff

#define SHF_WRITE 0x1
#define SHF_ALLOC 0x2
#define SHF_EXECINSTR 0x4
#define SHF_MASKPROC 0xf0000000
90 changes: 90 additions & 0 deletions tools/romimg/src/SonyRX.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
SonyRX.c - Contains functions for handling Sony Relocatable eXecutable files (e.g. IRX and ERX files).
*/

#include <errno.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>

#include "ELF.h"
#include "SonyRX.h"

int IsSonyRXModule(const char *path)
{
FILE *file;
elf_header_t header;
elf_shdr_t SectionHeader;
int result;

result = 0;
if ((file = fopen(path, "rb")) != NULL) {
fread(&header, 1, sizeof(elf_header_t), file);
if (*(u32 *)header.ident == ELF_MAGIC && (header.type == ELF_TYPE_ERX2 || header.type == ELF_TYPE_IRX)) {
unsigned int i;
for (i = 0; i < header.shnum; i++) {
fseek(file, header.shoff + i * header.shentsize, SEEK_SET);
fread(&SectionHeader, 1, sizeof(elf_shdr_t), file);

if ((SectionHeader.type == (SHT_LOPROC | SHT_LOPROC_EEMOD_TAB)) || (SectionHeader.type == (SHT_LOPROC | SHT_LOPROC_IOPMOD_TAB))) {
result = 1;
break;
}
}
}

fclose(file);
}

return result;
}

int GetSonyRXModInfo(const char *path, char *description, unsigned int MaxLength, unsigned short int *version)
{
FILE *file;
int result;
elf_header_t header;
elf_shdr_t SectionHeader;

result = ENOENT;
if ((file = fopen(path, "rb")) != NULL) {
fread(&header, 1, sizeof(elf_header_t), file);
if (*(u32 *)header.ident == ELF_MAGIC && (header.type == ELF_TYPE_ERX2 || header.type == ELF_TYPE_IRX)) {
unsigned int i;
for (i = 0; i < header.shnum; i++) {
fseek(file, header.shoff + i * header.shentsize, SEEK_SET);
fread(&SectionHeader, 1, sizeof(elf_shdr_t), file);

if (SectionHeader.type == (SHT_LOPROC | SHT_LOPROC_EEMOD_TAB) || SectionHeader.type == (SHT_LOPROC | SHT_LOPROC_IOPMOD_TAB)) {
void *buffer;
if ((buffer = malloc(SectionHeader.size)) != NULL) {
fseek(file, SectionHeader.offset, SEEK_SET);
fread(buffer, 1, SectionHeader.size, file);

if (SectionHeader.type == (SHT_LOPROC | SHT_LOPROC_IOPMOD_TAB)) {
*version = ((iopmod_t *)buffer)->version;
strncpy(description, ((iopmod_t *)buffer)->modname, MaxLength - 1);
description[MaxLength - 1] = '\0';
} else if (SectionHeader.type == (SHT_LOPROC | SHT_LOPROC_EEMOD_TAB)) {
*version = ((eemod_t *)buffer)->version;
strncpy(description, ((eemod_t *)buffer)->modname, MaxLength - 1);
description[MaxLength - 1] = '\0';
}

result = 0;

free(buffer);
} else
result = ENOMEM;
break;
}
}
} else
result = EINVAL;

fclose(file);
} else
result = ENOENT;

return result;
}
2 changes: 2 additions & 0 deletions tools/romimg/src/SonyRX.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
int IsSonyRXModule(const char *path);
int GetSonyRXModInfo(const char *path, char *description, unsigned int MaxLength, unsigned short int *version);
18 changes: 18 additions & 0 deletions tools/romimg/src/dprintf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#define REDBOLD "\033[1;31m"
#define YELBOLD "\033[1;33m"
#define GRNBOLD "\033[1;32m"

#define GREEN "\033[0;32m"
#define YELLOW "\033[0;33m"
#define DEFCOL "\033[0m"
#include <stdio.h>
#define ERROR(fmt, x...) printf(REDBOLD "ERROR: " fmt DEFCOL, ##x)
#define WARNING(fmt, x...) printf(YELBOLD "WARNING: " fmt DEFCOL, ##x)
// #define DEBUG
#ifdef DEBUG
#define DPRINTF(fmt, x...) printf(REDBOLD "%s: " fmt DEFCOL, __func__, ##x)
#define PRINTLN() DPRINTF("%d\n", __LINE__)
#else
#define DPRINTF(x...)
#define PRINTLN()
#endif
Loading

0 comments on commit 08181ea

Please sign in to comment.