Skip to content

Commit

Permalink
system: Implement replace ACCOUNT_ID, also move pfs parts
Browse files Browse the repository at this point in the history
it will automatically replace ACCOUNT_ID value

Resolve #75
  • Loading branch information
d3m3vilurr committed Apr 22, 2018
1 parent b6b788d commit 1cc426e
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 54 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Expand Up @@ -47,6 +47,7 @@ add_executable(${PROJECT_NAME}.elf
src/config.c
src/util.c
src/main.c
src/system.c
src/vita_sqlite.c

sqlite3/sqlite3.c
Expand All @@ -71,6 +72,7 @@ target_link_libraries(${PROJECT_NAME}.elf
-lSceTouch_stub
-lScePgf_stub
-lSceShellSvc_stub
-lSceRegistryMgr_stub
-ltaihen_stub
-lVitaShellUser_stub_weak

Expand Down
48 changes: 0 additions & 48 deletions src/file.c
Expand Up @@ -4,8 +4,6 @@
#include <psp2/io/stat.h>
#include <psp2/io/fcntl.h>
#include <psp2/io/dirent.h>
#include <psp2/appmgr.h>
#include <vitashell_user.h>

#include "common.h"
#include "file.h"
Expand Down Expand Up @@ -276,49 +274,3 @@ int copydir(const char *src, const char *dest, void (*callback)()) {
return 1;
}

// below codes are part of vitashell
char pfs_mount_point[MAX_MOUNT_POINT_LENGTH];
int known_pfs_ids[] = {
0x6E,
0x12E,
0x12F,
0x3ED,
};

int pfs_mount(const char *path) {
char klicensee[0x10];
//char license_buf[0x200];
ShellMountIdArgs args;

memset(klicensee, 0, sizeof(klicensee));

args.process_titleid = "SAVEMGR00";
args.path = path;
args.desired_mount_point = NULL;
args.klicensee = klicensee;
args.mount_point = pfs_mount_point;

int i;
for (i = 0; i < sizeof(known_pfs_ids) / sizeof(int); i++) {
args.id = known_pfs_ids[i];

int res = shellUserMountById(&args);
if (res >= 0)
return res;
}

return sceAppMgrGameDataMount(path, 0, 0, pfs_mount_point);
}

int pfs_unmount() {
if (pfs_mount_point[0] == 0)
return -1;

int res = sceAppMgrUmount(pfs_mount_point);
if (res >= 0) {
memset(pfs_mount_point, 0, sizeof(pfs_mount_point));
//memset(pfs_mounted_path, 0, sizeof(pfs_mounted_path));
}

return res;
}
6 changes: 0 additions & 6 deletions src/file.h
Expand Up @@ -10,10 +10,4 @@ int file_count(char *path, int check_blacklist);
int copydir(const char *src, const char *dest, void (*callback)());

int copyfile(char *src, char *dest);

// below codes are part of vitashell
#define MAX_MOUNT_POINT_LENGTH 16

int pfs_mount(const char *path);
int pfs_unmount();
#endif
10 changes: 10 additions & 0 deletions src/main.c
Expand Up @@ -24,6 +24,7 @@
#include "display.h"
#include "input.h"
#include "util.h"
#include "system.h"

vita2d_pgf* font;
SceUID kernel_modid = -1;
Expand Down Expand Up @@ -972,8 +973,17 @@ int copy_slot_to_savedata(appinfo *info, int slot) {
goto exit;
}

char *sfo_path = calloc(sizeof(char), 1);
aprintf(&sfo_path, "%s/sce_sys/param.sfo", dest);
printf("sfo_path %s\n", sfo_path);
if (exists(sfo_path)) {
change_accountid(sfo_path, get_accountid());
}
free(sfo_path);

exit:
pfs_unmount();

unlock_psbutton();
if (src) {
free(src);
Expand Down
135 changes: 135 additions & 0 deletions src/system.c
@@ -0,0 +1,135 @@
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <psp2/registrymgr.h>
#include <psp2/appmgr.h>
#include <vitashell_user.h>

#include "system.h"
#include "util.h"

#define SFO_MAGIC 0x46535000 // \x00PSF
#define MAX_MOUNT_POINT_LENGTH 16

struct sfo_header {
uint32_t magic;
uint32_t version;
uint32_t key_table_offset;
uint32_t data_table_offset;
uint32_t entries;
};

struct sfo_index {
uint16_t key_offset;
uint16_t param_format;
uint32_t param_length;
uint32_t param_max_length;
uint32_t data_offset;
};

uint8_t g_aid_loaded = 0;
uint64_t g_aid;

char pfs_mount_point[MAX_MOUNT_POINT_LENGTH];
int known_pfs_ids[] = {
0x6E,
0x12E,
0x12F,
0x3ED,
};

// below codes use part of vitashell codeset
uint64_t get_accountid() {
if (g_aid_loaded) {
return g_aid;
}
if (sceRegMgrGetKeyBin("/CONFIG/NP", "account_id", &g_aid, sizeof(uint64_t)) < 0) {
return 0;
}
g_aid_loaded = 1;
return g_aid;
}

int8_t change_accountid(const char *sfo_path, const uint64_t aid) {
FILE *f = fopen(sfo_path, "r+b");

struct sfo_header hdr = {0};
fread(&hdr, sizeof(struct sfo_header), 1, f);
if (hdr.magic != SFO_MAGIC) {
printf("magic mismatch\n");
fclose(f);
return -1;
}
uint32_t data_offset = -1;
for (int i = 0; i < hdr.entries; i++) {
fseek(f, sizeof(struct sfo_header) + sizeof(struct sfo_index) * i, SEEK_SET);
struct sfo_index idx = {0};
fread(&idx, sizeof(struct sfo_index), 1, f);
char key[64];
fseek(f, hdr.key_table_offset + idx.key_offset, SEEK_SET);
fread(&key, sizeof(char), 64, f);
if (strncmp(key, "ACCOUNT_ID", 10) != 0) {
continue;
}
data_offset = hdr.data_table_offset + idx.data_offset;
break;
}
if (data_offset == -1) {
printf("not exist ACCOUNT_ID\n");
fclose(f);
return -2;
}

uint64_t old_aid;
fseek(f, data_offset, SEEK_SET);
fread(&old_aid, sizeof(uint64_t), 1, f);
if (old_aid == aid) {
fclose(f);
printf("aid is already same\n");
return 1;
}

printf("replace ACCOUNT_ID - %08x to %08x\n", old_aid, aid);
fseek(f, data_offset, SEEK_SET);
fwrite(&aid, sizeof(uint64_t), 1, f);
fclose(f);
return 0;
}

int pfs_mount(const char *path) {
char klicensee[0x10];
//char license_buf[0x200];
ShellMountIdArgs args;

memset(klicensee, 0, sizeof(klicensee));

args.process_titleid = "SAVEMGR00";
args.path = path;
args.desired_mount_point = NULL;
args.klicensee = klicensee;
args.mount_point = pfs_mount_point;

int i;
for (i = 0; i < sizeof(known_pfs_ids) / sizeof(int); i++) {
args.id = known_pfs_ids[i];

int res = shellUserMountById(&args);
if (res >= 0)
return res;
}

return sceAppMgrGameDataMount(path, 0, 0, pfs_mount_point);
}

int pfs_unmount() {
if (pfs_mount_point[0] == 0)
return -1;

int res = sceAppMgrUmount(pfs_mount_point);
if (res >= 0) {
memset(pfs_mount_point, 0, sizeof(pfs_mount_point));
//memset(pfs_mounted_path, 0, sizeof(pfs_mounted_path));
}

return res;
}
10 changes: 10 additions & 0 deletions src/system.h
@@ -0,0 +1,10 @@
#ifndef __SYSTEM_H__
#define __SYSTEM_H__

// below codes use part of vitashell codeset
uint64_t get_accountid();
int8_t change_accountid(const char *sfo_path, const uint64_t aid);

int pfs_mount(const char *path);
int pfs_unmount();
#endif

0 comments on commit 1cc426e

Please sign in to comment.