Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
masagrator committed Aug 18, 2022
1 parent 7afae4e commit ff7d56f
Show file tree
Hide file tree
Showing 185 changed files with 13,489 additions and 2 deletions.
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2020 The Skyline Project
Copyright (c) 2021 MasaGratoR

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
16 changes: 16 additions & 0 deletions Makefile
@@ -0,0 +1,16 @@
.PHONY: all clean skyline

NAME := $(shell basename $(CURDIR))
NAME_LOWER := $(shell echo $(NAME) | tr A-Z a-z)

BUILD_DIR := build

MAKE_NSO := nso.mk

all: skyline

skyline:
$(MAKE) all -f $(MAKE_NSO) MAKE_NSO=$(MAKE_NSO) BUILD=$(BUILD_DIR) TARGET=$(NAME)

clean:
$(MAKE) clean -f $(MAKE_NSO)
56 changes: 54 additions & 2 deletions README.md
@@ -1,2 +1,54 @@
# PortalNXSideLoader
Portal Collection File Sideloader


# Portal Collection File Sideloader

# Installation
From releases download:
- for Portal 1:
> Portal-NXSideLoader.zip
- for Portal 2:
>Without network patches:
> - Portal2-NXSideLoader.zip
>
> With network patches:
> - Portal2-NoWeb-NXSideLoader.zip
Put `atmosphere` folder to root of sdcard (yes, your CFW won't be deleted...)

# Informations for mod makers

Few files in `nxcontent` may not be supported as they are preloaded with separate functions. I needed to add specific support for one function so `rom_boot_params.txt` could be loaded. If there is any file it's not working and you want it to work, write na issue.

# How this works?

Devs redesigned whole cstdio to use game.zip as filesystem.
Portal games to open most files are using function called `fopen_nx()`. To read this file - `fread_nx()`, etc.
All functions are cross compatible with cstdio, so solution was pretty easy:
1. Hook `fopen_nx()`
2. Detect if passed file path exists on SD card
3. If it exists, redirect call to `fopen()` with correct path starting with `rom:/`

There were 2 issues with this solution:
- not all files are using this function. It seems there is not many of them and only important one in my opinion was `rom_boot_params.txt` so I have hooked function reading this file and redesigned it to load file from SD card.
- fopen_nx() path load is ignoring case + has priority to check first if file is inside `nxcontent` folder, and if not checks root of zip. Inside hook remade this check + used `tolower()` for file path since all files and folders names inside zip are lower case.

# Compilation

You need standard devkitpro install with Switch-dev.

Patch `main.npdm` from exefs with this, otherwise plugin will crash:
https://github.com/skyline-dev/skyline/blob/master/scripts/patchNpdm.py

To compile it for Portal 1 use command
```
make PORTAL="-DPORTAL"
```

for Portal 2
```
make PORTAL="-DPORTAL2"
```
with internet patches to allow debugging via network that has blocked access to Nintendo servers
```
make PORTAL="-DPORTAL2 -DPDEBUG"
```
14 changes: 14 additions & 0 deletions exported.txt
@@ -0,0 +1,14 @@
{
global:
__custom_init;
__custom_fini;
skyline_tcp_send_raw;
getRegionAddress;
A64HookFunction;
A64InlineHook;
sky_memcpy;
get_program_id;
get_plugin_addresses;

local: *;
};
67 changes: 67 additions & 0 deletions include/ModuleObject.hpp
@@ -0,0 +1,67 @@
#pragma once

#include <assert.h>
#include <elf.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

namespace rtld {
struct ModuleObject {
private:
// ResolveSymbols internals
inline void ResolveSymbolRelAbsolute(Elf64_Rel* entry);
inline void ResolveSymbolRelaAbsolute(Elf64_Rela* entry);
inline void ResolveSymbolRelJumpSlot(Elf64_Rel* entry, bool do_lazy_got_init);
inline void ResolveSymbolRelaJumpSlot(Elf64_Rela* entry, bool do_lazy_got_init);

public:
struct ModuleObject* next;
struct ModuleObject* prev;
union {
Elf64_Rel* rel;
Elf64_Rela* rela;
void* raw;
} rela_or_rel_plt;
union {
Elf64_Rel* rel;
Elf64_Rela* rela;
} rela_or_rel;
uint64_t module_base;
Elf64_Dyn* dynamic;
bool is_rela;
uint64_t rela_or_rel_plt_size;
void (*dt_init)(void);
void (*dt_fini)(void);
uint32_t* hash_bucket;
uint32_t* hash_chain;
char* dynstr;
Elf64_Sym* dynsym;
uint64_t dynstr_size;
void** got;
uint64_t rela_dyn_size;
uint64_t rel_dyn_size;
uint64_t rel_count;
uint64_t rela_count;
uint64_t hash_nchain_value;
uint64_t hash_nbucket_value;
uint64_t got_stub_ptr;
#ifdef __RTLD_6XX__
uint64_t soname_idx;
uint64_t nro_size;
bool cannot_revert_symbols;
#endif

void Initialize(uint64_t aslr_base, Elf64_Dyn* dynamic);
void Relocate();
Elf64_Sym* GetSymbolByName(const char* name);
void ResolveSymbols(bool do_lazy_got_init);
bool TryResolveSymbol(Elf64_Addr* target_symbol_address, Elf64_Sym* symbol);
};

#ifdef __RTLD_6XX__
static_assert(sizeof(ModuleObject) == 0xD0, "ModuleObject size isn't valid");
#else
static_assert(sizeof(ModuleObject) == 0xB8, "ModuleObject size isn't valid");
#endif
} // namespace rtld
23 changes: 23 additions & 0 deletions include/alloc.h
@@ -0,0 +1,23 @@
/**
* @file alloc.h
* @brief Allocation functions.
*/

#pragma once

#include "types.h"

#ifdef __cplusplus
extern "C" {
#endif

void* malloc(size_t size);
void free(void* src);
void* calloc(u64 num, u64 size);
void* realloc(void* ptr, u64 size);
void* aligned_alloc(u64 alignment, u64 size);
u64 malloc_usable_size(void* ptr);

#ifdef __cplusplus
}
#endif
37 changes: 37 additions & 0 deletions include/curl.h
@@ -0,0 +1,37 @@
/**
* @file curl.h
* @brief CURL implementation.
*/

#pragma once

#include "types.h"

typedef u32 CURLCode; // basic code for getting results from functions

#ifdef __cplusplus
extern "C" {
#endif

typedef void CURL;
enum CURLINFO {};
enum CURLoption {};

// GLOBAL
CURLcode curl_global_init(s64 flags);

// EASY
CURL* curl_easy_init();
CURLcode curl_easy_setopt(CURL* curl, CURLoption, ...);
CURLcode curl_easy_perform(CURL* curl);
void curl_easy_cleanup(CURL* curl);
CURLcode curl_easy_getinfo(CURL* curl, CURLINFO info, ...);
CURL* curl_easy_duphandle(CURL* curl);
void curl_easy_reset(CURL* curl);
CURLcode curl_easy_pause(CURL* curl, s32 mask);
CURLcode curl_easy_recv(CURL* curl, void* buffer, u64 bufferLength, u64*);
CURLcode curl_easy_send(CURL* curl, void const* buffer, u64 bufferLength, u64*);

#ifdef __cplusplus
}
#endif
21 changes: 21 additions & 0 deletions include/cxa.h
@@ -0,0 +1,21 @@
/**
* @file cxa.h
* @brief One-Time Constru(X)tion API.
*/

#pragma once

#include "types.h"

#ifdef __cplusplus
extern "C" {
#endif

extern s32 __cxa_guard_acquire(u32* guard);
extern void __cxa_guard_release(u32* guard);
extern void __cxa_pure_virtual();
void __cxa_atexit();

#ifdef __cplusplus
}
#endif
12 changes: 12 additions & 0 deletions include/endianess.h
@@ -0,0 +1,12 @@
/**
* @file endianess.h
* @brief Functions for reversing data in between the two endianesses.
*/

#pragma once

#include "types.h"

u16 swap16(u16 x) { return (x << 8) | (x >> 8); }

u32 swap32(u32 x) { return (x << 24) | ((x << 8) & 0xFF0000) | ((x >> 24) & 0xFF) | ((x >> 8) & 0xFF00); }
23 changes: 23 additions & 0 deletions include/macros.h
@@ -0,0 +1,23 @@
/**
* @file macros.h
* @brief Various macros defined from IDA Pro.
*/

#pragma once

#include "types.h"

// https://github.com/joxeankoret/tahh/blob/master/comodo/defs.h

#define __CASSERT_N0__(l) COMPILE_TIME_ASSERT_##l
#define __CASSERT_N1__(l) __CASSERT_N0__(l)
#define CASSERT(cnd) typedef char __CASSERT_N1__(__LINE__)[(cnd) ? 1 : -1]

#define LODWORD(x) (*((u32*)&(x)))

template <class T>
bool is_mul_ok(T count, T elsize) {
CASSERT((T)(-1) > 0);
if (elsize == 0 || count == 0) return true;
return count <= ((T)(-1)) / elsize;
}
36 changes: 36 additions & 0 deletions include/main.hpp
@@ -0,0 +1,36 @@
#pragma once

#include <map>
#include <string>

#include "ModuleObject.hpp"
#include "alloc.h"
#include "mem.h"
#include "nn/crypto.h"
#include "nn/diag.h"
#include "nn/err.h"
#include "nn/fs.h"
#include "nn/nn.h"
#include "nn/oe.h"
#include "nn/os.hpp"
#include "nn/prepo.h"
#include "nn/ro.h"
#include "nvn/pfnc.h"
#include "operator.h"
#include "skyline/inlinehook/And64InlineHook.hpp"
#include "types.h"

#ifdef __cplusplus
extern "C" {
#endif

#include "skyline/nx/kernel/virtmem.h"
#include "skyline/nx/runtime/env.h"

#ifdef __cplusplus
};
#endif

extern nn::os::EventType romMountedEvent;

extern "C" void skyline_init();
22 changes: 22 additions & 0 deletions include/mem.h
@@ -0,0 +1,22 @@
/**
* @file mem.h
* @brief Memory functions.
*/

#pragma once

#include "types.h"

#ifdef __cplusplus
extern "C" {
#endif

void* memset(void* src, int val, u64 num);
void* memcpy(void* dest, void const* src, u64 count);
void* memmove(void* dest, const void* src, u64 count);
void* memalign(size_t alignment, size_t size);
void* memmem(void* needle, size_t needleLen, void* haystack, size_t haystackLen);

#ifdef __cplusplus
}
#endif

0 comments on commit ff7d56f

Please sign in to comment.