This commit is contained in:
Kirill Zhumarin 2023-10-30 02:42:45 +02:00
parent 3e7606f644
commit e4b861bbf0
9 changed files with 12703 additions and 12370 deletions

View file

@ -18,8 +18,14 @@ add_executable(elfloader3
src/main.cpp
src/stubs.cpp
src/utils.cpp
src/swi/libc.cpp
src/SieFs.cpp
# SWI
src/swi.cpp
src/swi/libc.cpp
src/swi/fs.cpp
# vova7890 & Ganster elfloader
src/elfloader/debug.c
src/elfloader/env.c
src/elfloader/loadelf.c

34
src/SieFs.cpp Normal file
View file

@ -0,0 +1,34 @@
#include "SieFs.h"
#include <regex>
static std::map<std::string, std::string> m_drives;
void SieFs::mount(const std::string &drive, const std::string &path) {
m_drives[drive] = path;
}
std::string SieFs::path2sie(const std::string &path) {
return "";
}
std::string SieFs::sie2path(const std::string &uri) {
std::regex path_re("^([0-9a-f]):\\\\(.*?)$", std::regex_constants::ECMAScript | std::regex_constants::icase);
std::smatch m;
if (std::regex_match(uri, m, path_re)) {
auto drive = m[1].str();
auto path = m[2].str();
std::replace(path.begin(), path.end(), '\\', '/');
if (m_drives.find(drive) != m_drives.end()) {
std::string unix_path = m_drives[drive] + "/" + path;
fprintf(stderr, "sie2path: %s:%s | -> %s\n", drive.c_str(), path.c_str(), unix_path.c_str());
return unix_path;
}
}
fprintf(stderr, "Invalid siemens path: %s\n", uri.c_str());
abort();
return "";
}

10
src/SieFs.h Normal file
View file

@ -0,0 +1,10 @@
#pragma once
#include <string>
class SieFs {
public:
static void mount(const std::string &drive, const std::string &path);
static std::string path2sie(const std::string &path);
static std::string sie2path(const std::string &path);
};

View file

@ -7,81 +7,11 @@
#include "elfloader/loader.h"
#include "elfloader/env.h"
#include "stubs.h"
#include "swi.h"
#include "log.h"
#include "utils.h"
void swi_stub(int swi) {
fprintf(stderr, "Unk swi 0x%04X (%d) func called\n", swi, swi);
abort();
}
void loader_subproc_impl(void *func, void *p1) {
EP3_DEBUG("subproc called");
}
int *loader_library_impl() {
fprintf(stderr, "loader_library_impl\n");
return (int *) switab_functions;
}
extern "C" void SWI_MutexCreate() {
}
extern "C" void SWI_MutexDestroy() {
}
extern "C" void SWI_close(int FileHandler, unsigned int *ErrorNumber) {
fprintf(stderr, "_close(%d) %p\n", FileHandler, ErrorNumber);
}
extern "C" void SWI_elfclose(Elf32_Exec *ex) {
loader_elf_close(ex);
}
static void init_switab() {
// LIBC
switab_functions[0x0113] = (void *) SWI_strpbrk,
switab_functions[0x0113] = (void *) SWI_strcspn,
switab_functions[0x0114] = (void *) SWI_strncat,
switab_functions[0x0115] = (void *) SWI_strncmp,
switab_functions[0x0116] = (void *) SWI_strncpy,
switab_functions[0x0117] = (void *) SWI_strrchr,
switab_functions[0x0118] = (void *) SWI_strstr,
switab_functions[0x0119] = (void *) SWI_strtol,
switab_functions[0x011A] = (void *) SWI_strtoul,
switab_functions[0x011C] = (void *) SWI_memcmp,
switab_functions[0x011D] = (void *) SWI_zeromem,
switab_functions[0x011E] = (void *) SWI_memcpy,
switab_functions[0x0132] = (void *) SWI_memmove,
switab_functions[0x0162] = (void *) SWI_setjmp,
switab_functions[0x011B] = (void *) SWI_snprintf,
switab_functions[0x00BB] = (void *) SWI_memset,
switab_functions[0x0092] = (void *) SWI_calloc,
switab_functions[0x0085] = (void *) SWI_strcmpi,
switab_functions[0x0054] = (void *) SWI_StrToInt,
switab_functions[0x0014] = (void *) SWI_malloc,
switab_functions[0x0015] = (void *) SWI_free,
switab_functions[0x0015] = (void *) SWI_free,
switab_functions[0x8014] = (void *) SWI_malloc_adr,
switab_functions[0x8015] = (void *) SWI_mfree_adr,
switab_functions[0x0016] = (void *) SWI_sprintf,
switab_functions[0x0017] = (void *) SWI_strcat,
switab_functions[0x0018] = (void *) SWI_strchr,
switab_functions[0x0019] = (void *) SWI_strcmp,
switab_functions[0x001A] = (void *) SWI_strcpy,
switab_functions[0x001B] = (void *) SWI_strlen,
switab_functions[0x00D] = (void *) SWI_close;
switab_functions[0x190] = (void *) SWI_MutexCreate;
switab_functions[0x191] = (void *) SWI_MutexDestroy;
switab_functions[0x2EE] = (void *) SWI_elfclose;
}
extern int __executable_start;
#include "SieFs.h"
std::string normalizeLibraryPath(const std::string &library_path_env) {
std::vector<std::string> new_library_path_env;
@ -111,21 +41,27 @@ int main(int argc, char **argv) {
filename = std::filesystem::canonical(filename);
std::string self_dir = std::filesystem::canonical(std::filesystem::path(argv[0]).parent_path());
std::string library_path_env;
if (getenv("SDK_PATH")) {
library_path_env = strprintf("%s/lib/NSG:%s/lib", getenv("SDK_PATH"), getenv("SDK_PATH"));
library_path_env = strprintf("%s/lib/NSG;%s/lib", getenv("SDK_PATH"), getenv("SDK_PATH"));
} else if (getenv("EL3_LIBRARY_PATH")) {
library_path_env = getenv("EL3_LIBRARY_PATH");
} else {
std::string bindir = std::filesystem::canonical(std::filesystem::path(argv[0]).parent_path());
library_path_env = bindir + "/../sdk/lib/NSG;" + bindir + "/../sdk/lib";
library_path_env = self_dir + "/../sdk/lib/NSG;" + self_dir + "/../sdk/lib";
}
SieFs::mount("0", self_dir + "/rootfs/Data");
SieFs::mount("1", self_dir + "/rootfs/Cache");
SieFs::mount("2", self_dir + "/rootfs/Config");
SieFs::mount("4", "");
library_path_env = normalizeLibraryPath(library_path_env);
loader_setenv("LD_LIBRARY_PATH", library_path_env.c_str(), true);
LOGD("EL3_LIBRARY_PATH: %s\n", loader_getenv("LD_LIBRARY_PATH"));
init_switab();
loader_init_switab();
loader_set_debug(false);
loader_gdb_init();

File diff suppressed because it is too large Load diff

View file

@ -1,5 +0,0 @@
#pragma once
extern void *switab_functions[4096];
void swi_stub(int swi);

88
src/swi.cpp Normal file
View file

@ -0,0 +1,88 @@
#include "swi.h"
#include "elfloader/loader.h"
void loader_swi_stub(int swi) {
fprintf(stderr, "Unk swi 0x%04X (%d) func called\n", swi, swi);
abort();
}
int *loader_library_impl() {
return (int *) switab_functions;
}
void SWI_MutexCreate() {
}
void SWI_MutexDestroy() {
}
void SWI_elfclose(Elf32_Exec *ex) {
loader_elf_close(ex);
}
void SWI_ShowMSG(int flag, int msg) {
fprintf(stderr, "ShowMSG: %s\n", reinterpret_cast<char *>(msg));
}
void loader_init_switab() {
// UI
switab_functions[0x0148] = (void *) SWI_ShowMSG,
// LIBC
switab_functions[0x0113] = (void *) SWI_strpbrk,
switab_functions[0x0113] = (void *) SWI_strcspn,
switab_functions[0x0114] = (void *) SWI_strncat,
switab_functions[0x0115] = (void *) SWI_strncmp,
switab_functions[0x0116] = (void *) SWI_strncpy,
switab_functions[0x0117] = (void *) SWI_strrchr,
switab_functions[0x0118] = (void *) SWI_strstr,
switab_functions[0x0119] = (void *) SWI_strtol,
switab_functions[0x011A] = (void *) SWI_strtoul,
switab_functions[0x011C] = (void *) SWI_memcmp,
switab_functions[0x011D] = (void *) SWI_zeromem,
switab_functions[0x011E] = (void *) SWI_memcpy,
switab_functions[0x0132] = (void *) SWI_memmove,
switab_functions[0x0162] = (void *) SWI_setjmp,
switab_functions[0x011B] = (void *) SWI_snprintf,
switab_functions[0x00BB] = (void *) SWI_memset,
switab_functions[0x0092] = (void *) SWI_calloc,
switab_functions[0x0085] = (void *) SWI_strcmpi,
switab_functions[0x0054] = (void *) SWI_StrToInt,
switab_functions[0x0014] = (void *) SWI_malloc,
switab_functions[0x0015] = (void *) SWI_free,
switab_functions[0x0015] = (void *) SWI_free,
switab_functions[0x8014] = (void *) SWI_malloc_adr,
switab_functions[0x8015] = (void *) SWI_mfree_adr,
switab_functions[0x0016] = (void *) SWI_sprintf,
switab_functions[0x0017] = (void *) SWI_strcat,
switab_functions[0x0018] = (void *) SWI_strchr,
switab_functions[0x0019] = (void *) SWI_strcmp,
switab_functions[0x001A] = (void *) SWI_strcpy,
switab_functions[0x001B] = (void *) SWI_strlen,
// Filesystem
switab_functions[0x000A] = (void *) SWI_open,
switab_functions[0x000B] = (void *) SWI_read,
switab_functions[0x000C] = (void *) SWI_write,
switab_functions[0x000D] = (void *) SWI_close,
switab_functions[0x000E] = (void *) SWI_flush,
switab_functions[0x000F] = (void *) SWI_lseek,
switab_functions[0x0010] = (void *) SWI_mkdir,
switab_functions[0x0012] = (void *) SWI_GetFileAttrib,
switab_functions[0x0013] = (void *) SWI_SetFileAttrib,
switab_functions[0x003C] = (void *) SWI_setfilesize,
switab_functions[0x006B] = (void *) SWI_FindFirstFile,
switab_functions[0x006C] = (void *) SWI_FindNextFile,
switab_functions[0x006D] = (void *) SWI_FindClose,
switab_functions[0x008E] = (void *) SWI_fmove,
switab_functions[0x008F] = (void *) SWI_rmdir,
switab_functions[0x0090] = (void *) SWI_truncate,
switab_functions[0x0091] = (void *) SWI_isdir,
switab_functions[0x0084] = (void *) SWI_GetFileStats,
switab_functions[0x190] = (void *) SWI_MutexCreate;
switab_functions[0x191] = (void *) SWI_MutexDestroy;
switab_functions[0x2EE] = (void *) SWI_elfclose;
}

View file

@ -3,6 +3,14 @@
#include <cstdint>
#include <cstddef>
#define NEWSGOLD 1
// internal
extern void *switab_functions[4096];
void loader_init_switab();
void loader_swi_stub(int swi);
// libc
typedef int _jmp_buf[11];
@ -35,3 +43,81 @@ char *SWI_strchr(const char *param1, int param2);
int SWI_strcmp(const char *param1, const char *param2);
char *SWI_strcpy(char *dest, const char *source);
unsigned int SWI_strlen(const char *param1);
// Filesystem
// Permissions
#define P_WRITE 0x100
#define P_READ 0x80
// Access
#define A_ReadOnly 0
#define A_WriteOnly 1
#define A_ReadWrite 2
#define A_NoShare 4
#define A_Append 8
#define A_Exclusive 0x10
#define A_MMCStream 0x20
#define A_Create 0x100
#define A_Truncate 0x200
#define A_FailCreateOnExist 0x400
#define A_FailOnReopen 0x800
#define A_TXT 0x4000
#define A_BIN 0x8000
#pragma pack(1)
typedef struct {
#ifdef NEWSGOLD
char unk0[40];
unsigned int file_size;
short file_attr;
unsigned int create_date_time;
char unk1[2];
char folder_name[128];
char unk2;
char file_name[128];
char unk3[11];
short wsfolder_name[128+1];
short wsfile_name[128+1];
#else
char unk1[14];
short file_attr;
unsigned int create_date_time;
unsigned int file_size;
char unk2[16];
char folder_name[128];
char unk3;
char file_name[271];
#endif
} DIR_ENTRY;
#pragma pack()
typedef struct {
char unk1;
char unk2;
char unk3;
char unk4;
unsigned int size;
char unk5[28];
int file_attr;
} FSTATS;
int SWI_open(const char *cFileName, unsigned int iFileFlags, unsigned int iFileMode, unsigned int *ErrorNumber);
int SWI_read(int FileHandler, void *cBuffer, int iByteCount, unsigned int *ErrorNumber);
int SWI_write(int FileHandler, void const * cBuffer, int iByteCount, unsigned int *ErrorNumber);
int SWI_close(int FileHandler, unsigned int *ErrorNumber);
int SWI_flush(int stream, unsigned int *ErrorNumber);
long SWI_lseek(int FileHandler, unsigned int offset, unsigned int origin, unsigned int *ErrorNumber, unsigned int *ErrorNumber2);
int SWI_mkdir(const char * cFileName, unsigned int *ErrorNumber);
int SWI_GetFileAttrib(const char *cFileName, unsigned char *cAttribute, unsigned int *ErrorNumber);
int SWI_SetFileAttrib(const char *cFileName, unsigned char cAttribute, unsigned int *ErrorNumber);
int SWI_setfilesize(int FileHandler, unsigned int iNewFileSize, unsigned int *ErrorNumber);
int SWI_FindFirstFile(DIR_ENTRY *DIRENTRY, const char *mask, unsigned int *ErrorNumber);
int SWI_FindNextFile(DIR_ENTRY *DIRENTRY,unsigned int *ErrorNumber);
int SWI_FindClose(DIR_ENTRY *DIRENTRY,unsigned int *ErrorNumber);
int SWI_fmove(const char * SourceFileName, const char * DestFileName, unsigned int *ErrorNumber);
int SWI_rmdir(const char * cDirectory, unsigned int *ErrorNumber);
int SWI_truncate(int FileHandler, int length, int *errornumber);
int SWI_isdir(const char * cDirectory, unsigned int *ErrorNumber);
int SWI_GetFileStats(const char *cFileName, FSTATS * StatBuffer, unsigned int *errornumber);

178
src/swi/fs.cpp Normal file
View file

@ -0,0 +1,178 @@
#include "../swi.h"
#include "../SieFs.h"
#include <cstdio>
#include <cstdlib>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int SWI_open(const char *path, unsigned int f, unsigned int m, unsigned int *errp) {
int flags = 0;
mode_t mode = S_IRUSR | S_IWUSR;
if ((f & A_ReadOnly))
flags |= O_RDONLY;
if ((f & A_WriteOnly))
flags |= O_WRONLY;
if ((f & A_ReadWrite))
flags |= O_RDWR;
if ((f & A_Create))
flags |= O_CREAT;
if ((f & A_Exclusive))
flags |= O_EXCL;
if ((f & A_Truncate))
flags |= O_TRUNC;
if ((f & A_Append))
flags |= O_APPEND;
int ret = open(SieFs::sie2path(path).c_str(), flags, mode);
// fprintf(stderr, "open(%s) = %d\n", path, ret);
if (ret < 0) {
perror("[SWI] open");
if (errp)
*errp = 1;
} else {
if (errp)
*errp = 0;
}
return ret;
}
int SWI_read(int fd, void *buff, int count, unsigned int *errp) {
int ret = read(fd, buff, count);
if (ret < 0) {
perror("[SWI] read");
if (errp)
*errp = 1;
} else {
if (errp)
*errp = 0;
}
return ret;
}
int SWI_write(int fd, const void *buff, int count, unsigned int *errp) {
int ret = write(fd, buff, count);
if (ret < 0) {
perror("[SWI] write");
if (errp)
*errp = 1;
} else {
if (errp)
*errp = 0;
}
return ret;
}
int SWI_close(int fd, unsigned int *errp) {
int ret = close(fd);
// fprintf(stderr, "close(%d) = %d\n", fd, ret);
if (ret < 0) {
perror("[SWI] close");
if (errp)
*errp = 1;
} else {
if (errp)
*errp = 0;
}
return ret;
}
int SWI_flush(int fd, unsigned int *errp) {
int ret = fsync(fd);
if (ret < 0) {
perror("[SWI] flush");
if (errp)
*errp = 1;
} else {
if (errp)
*errp = 0;
}
return ret;
}
long SWI_lseek(int fd, unsigned int offset, unsigned int origin, unsigned int *errp, unsigned int *errp2) {
fprintf(stderr, "%s not implemented!\n", __func__);
abort();
return 0;
}
int SWI_mkdir(const char * cFileName, unsigned int *ErrorNumber) {
fprintf(stderr, "%s not implemented!\n", __func__);
abort();
return 0;
}
int SWI_GetFileAttrib(const char *cFileName, unsigned char *cAttribute, unsigned int *ErrorNumber) {
fprintf(stderr, "%s not implemented!\n", __func__);
abort();
return 0;
}
int SWI_SetFileAttrib(const char *cFileName, unsigned char cAttribute, unsigned int *ErrorNumber) {
fprintf(stderr, "%s not implemented!\n", __func__);
abort();
return 0;
}
int SWI_setfilesize(int FileHandler, unsigned int iNewFileSize, unsigned int *ErrorNumber) {
fprintf(stderr, "%s not implemented!\n", __func__);
abort();
return 0;
}
int SWI_FindFirstFile(DIR_ENTRY *DIRENTRY, const char *mask,unsigned int *ErrorNumber) {
fprintf(stderr, "%s not implemented!\n", __func__);
abort();
return 0;
}
int SWI_FindNextFile(DIR_ENTRY *DIRENTRY,unsigned int *ErrorNumber) {
fprintf(stderr, "%s not implemented!\n", __func__);
abort();
return 0;
}
int SWI_FindClose(DIR_ENTRY *DIRENTRY,unsigned int *ErrorNumber) {
fprintf(stderr, "%s not implemented!\n", __func__);
abort();
return 0;
}
int SWI_fmove(const char * SourceFileName, const char * DestFileName, unsigned int *ErrorNumber) {
fprintf(stderr, "%s not implemented!\n", __func__);
abort();
return 0;
}
int SWI_rmdir(const char * cDirectory, unsigned int *ErrorNumber) {
fprintf(stderr, "%s not implemented!\n", __func__);
abort();
return 0;
}
int SWI_truncate(int FileHandler, int length, int *errornumber) {
fprintf(stderr, "%s not implemented!\n", __func__);
abort();
return 0;
}
int SWI_isdir(const char * cDirectory, unsigned int *ErrorNumber) {
fprintf(stderr, "%s not implemented!\n", __func__);
abort();
return 0;
}
int SWI_GetFileStats(const char *cFileName, FSTATS * StatBuffer, unsigned int *errornumber) {
fprintf(stderr, "%s not implemented!\n", __func__);
abort();
return 0;
}