mirror of
https://github.com/DarkMatterCore/nxdumptool.git
synced 2024-11-26 04:02:11 +00:00
More changes.
This commit is contained in:
parent
c83858311b
commit
d3ad9d84b6
3 changed files with 91 additions and 65 deletions
|
@ -23,17 +23,24 @@
|
||||||
#include "service_guard.h"
|
#include "service_guard.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
|
#define GAMECARD_READ_BUFFER_SIZE 0x800000 /* 8 MiB */
|
||||||
|
|
||||||
#define GAMECARD_ACCESS_WAIT_TIME 3 /* Seconds */
|
#define GAMECARD_ACCESS_WAIT_TIME 3 /* Seconds */
|
||||||
|
|
||||||
#define GAMECARD_UPDATE_TID (u64)0x0100000000000816
|
#define GAMECARD_UPDATE_TID (u64)0x0100000000000816
|
||||||
|
|
||||||
#define GAMECARD_READ_BUFFER_SIZE 0x800000 /* 8 MiB */
|
|
||||||
|
|
||||||
#define GAMECARD_ECC_BLOCK_SIZE 0x200
|
#define GAMECARD_ECC_BLOCK_SIZE 0x200
|
||||||
#define GAMECARD_ECC_DATA_SIZE 0x24
|
#define GAMECARD_ECC_DATA_SIZE 0x24
|
||||||
|
|
||||||
#define GAMECARD_STORAGE_AREA_NAME(x) ((x) == GameCardStorageArea_Normal ? "normal" : ((x) == GameCardStorageArea_Secure ? "secure" : "none"))
|
#define GAMECARD_STORAGE_AREA_NAME(x) ((x) == GameCardStorageArea_Normal ? "normal" : ((x) == GameCardStorageArea_Secure ? "secure" : "none"))
|
||||||
|
|
||||||
|
#define GAMECARD_CAPACITY_1GiB (u64)0x40000000
|
||||||
|
#define GAMECARD_CAPACITY_2GiB (u64)0x80000000
|
||||||
|
#define GAMECARD_CAPACITY_4GiB (u64)0x100000000
|
||||||
|
#define GAMECARD_CAPACITY_8GiB (u64)0x200000000
|
||||||
|
#define GAMECARD_CAPACITY_16GiB (u64)0x400000000
|
||||||
|
#define GAMECARD_CAPACITY_32GiB (u64)0x800000000
|
||||||
|
|
||||||
/* Type definitions. */
|
/* Type definitions. */
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
|
@ -67,6 +74,7 @@ static u8 *g_gameCardReadBuf = NULL;
|
||||||
|
|
||||||
static GameCardHeader g_gameCardHeader = {0};
|
static GameCardHeader g_gameCardHeader = {0};
|
||||||
static u64 g_gameCardStorageNormalAreaSize = 0, g_gameCardStorageSecureAreaSize = 0;
|
static u64 g_gameCardStorageNormalAreaSize = 0, g_gameCardStorageSecureAreaSize = 0;
|
||||||
|
static u64 g_gameCardCapacity = 0;
|
||||||
|
|
||||||
static u8 *g_gameCardHfsRootHeader = NULL; /// GameCardHashFileSystemHeader + GameCardHashFileSystemEntry + Name Table.
|
static u8 *g_gameCardHfsRootHeader = NULL; /// GameCardHashFileSystemHeader + GameCardHashFileSystemEntry + Name Table.
|
||||||
static GameCardHashFileSystemPartitionInfo *g_gameCardHfsPartitions = NULL;
|
static GameCardHashFileSystemPartitionInfo *g_gameCardHfsPartitions = NULL;
|
||||||
|
@ -89,7 +97,8 @@ static bool gamecardOpenStorageArea(u8 area);
|
||||||
static bool gamecardReadStorageArea(void *out, u64 out_size, u64 offset, bool lock);
|
static bool gamecardReadStorageArea(void *out, u64 out_size, u64 offset, bool lock);
|
||||||
static void gamecardCloseStorageArea(void);
|
static void gamecardCloseStorageArea(void);
|
||||||
|
|
||||||
static bool gamecardGetSizesFromStorageAreas(void);
|
static bool gamecardGetStorageAreasSizes(void);
|
||||||
|
static inline u64 gamecardGetCapacityFromRomSizeValue(u8 rom_size);
|
||||||
|
|
||||||
/* Service guard used to generate thread-safe initialize + exit functions. */
|
/* Service guard used to generate thread-safe initialize + exit functions. */
|
||||||
/* I'm using this here even though this actually isn't a service but who cares, it gets the job done. */
|
/* I'm using this here even though this actually isn't a service but who cares, it gets the job done. */
|
||||||
|
@ -97,10 +106,13 @@ NX_GENERATE_SERVICE_GUARD(gamecard);
|
||||||
|
|
||||||
bool gamecardIsReady(void)
|
bool gamecardIsReady(void)
|
||||||
{
|
{
|
||||||
|
bool ret = false;
|
||||||
|
|
||||||
mtx_lock(&g_gameCardSharedDataMutex);
|
mtx_lock(&g_gameCardSharedDataMutex);
|
||||||
bool status = (g_gameCardInserted && g_gameCardInfoLoaded);
|
ret = (g_gameCardInserted && g_gameCardInfoLoaded);
|
||||||
mtx_unlock(&g_gameCardSharedDataMutex);
|
mtx_unlock(&g_gameCardSharedDataMutex);
|
||||||
return status;
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool gamecardRead(void *out, u64 out_size, u64 offset)
|
bool gamecardRead(void *out, u64 out_size, u64 offset)
|
||||||
|
@ -123,7 +135,7 @@ bool gamecardGetHeader(GameCardHeader *out)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool gamecardGetTotalRomSize(u64 *out)
|
bool gamecardGetTotalSize(u64 *out)
|
||||||
{
|
{
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
|
|
||||||
|
@ -138,7 +150,7 @@ bool gamecardGetTotalRomSize(u64 *out)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool gamecardGetTrimmedRomSize(u64 *out)
|
bool gamecardGetTrimmedSize(u64 *out)
|
||||||
{
|
{
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
|
|
||||||
|
@ -153,6 +165,21 @@ bool gamecardGetTrimmedRomSize(u64 *out)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool gamecardGetRomCapacity(u64 *out)
|
||||||
|
{
|
||||||
|
bool ret = false;
|
||||||
|
|
||||||
|
mtx_lock(&g_gameCardSharedDataMutex);
|
||||||
|
if (g_gameCardInserted && g_gameCardInfoLoaded && out)
|
||||||
|
{
|
||||||
|
*out = g_gameCardCapacity;
|
||||||
|
ret = true;
|
||||||
|
}
|
||||||
|
mtx_unlock(&g_gameCardSharedDataMutex);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
bool gamecardGetCertificate(FsGameCardCertificate *out)
|
bool gamecardGetCertificate(FsGameCardCertificate *out)
|
||||||
{
|
{
|
||||||
Result rc = 0;
|
Result rc = 0;
|
||||||
|
@ -418,7 +445,7 @@ static void gamecardLoadInfo(void)
|
||||||
|
|
||||||
/* Retrieve gamecard storage area sizes */
|
/* Retrieve gamecard storage area sizes */
|
||||||
/* gamecardReadStorageArea() actually checks if the storage area sizes are greater than zero, so we must first perform this step */
|
/* gamecardReadStorageArea() actually checks if the storage area sizes are greater than zero, so we must first perform this step */
|
||||||
if (!gamecardGetSizesFromStorageAreas())
|
if (!gamecardGetStorageAreasSizes())
|
||||||
{
|
{
|
||||||
LOGFILE("Failed to retrieve gamecard storage area sizes!");
|
LOGFILE("Failed to retrieve gamecard storage area sizes!");
|
||||||
goto out;
|
goto out;
|
||||||
|
@ -438,18 +465,19 @@ static void gamecardLoadInfo(void)
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Get gamecard capacity */
|
||||||
|
g_gameCardCapacity = gamecardGetCapacityFromRomSizeValue(g_gameCardHeader.rom_size);
|
||||||
|
if (!g_gameCardCapacity)
|
||||||
|
{
|
||||||
|
LOGFILE("Invalid gamecard capacity value! (0x%02X)", g_gameCardHeader.rom_size);
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
if (utilsGetCustomFirmwareType() == UtilsCustomFirmwareType_SXOS)
|
if (utilsGetCustomFirmwareType() == UtilsCustomFirmwareType_SXOS)
|
||||||
{
|
{
|
||||||
/* The total size for the secure storage area is maxed out under SX OS */
|
/* The total size for the secure storage area is maxed out under SX OS */
|
||||||
/* Let's try to calculate it manually */
|
/* Let's try to calculate it manually */
|
||||||
u64 capacity = gamecardGetCapacityFromHeader(&g_gameCardHeader);
|
g_gameCardStorageSecureAreaSize = ((g_gameCardCapacity - ((g_gameCardCapacity / GAMECARD_ECC_BLOCK_SIZE) * GAMECARD_ECC_DATA_SIZE)) - g_gameCardStorageNormalAreaSize);
|
||||||
if (!capacity)
|
|
||||||
{
|
|
||||||
LOGFILE("Invalid gamecard capacity value! (0x%02X)", g_gameCardHeader.rom_size);
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
g_gameCardStorageSecureAreaSize = ((capacity - ((capacity / GAMECARD_ECC_BLOCK_SIZE) * GAMECARD_ECC_DATA_SIZE)) - g_gameCardStorageNormalAreaSize);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Allocate memory for the root hash FS header */
|
/* Allocate memory for the root hash FS header */
|
||||||
|
@ -552,9 +580,12 @@ out:
|
||||||
static void gamecardFreeInfo(void)
|
static void gamecardFreeInfo(void)
|
||||||
{
|
{
|
||||||
memset(&g_gameCardHeader, 0, sizeof(GameCardHeader));
|
memset(&g_gameCardHeader, 0, sizeof(GameCardHeader));
|
||||||
|
|
||||||
g_gameCardStorageNormalAreaSize = 0;
|
g_gameCardStorageNormalAreaSize = 0;
|
||||||
g_gameCardStorageSecureAreaSize = 0;
|
g_gameCardStorageSecureAreaSize = 0;
|
||||||
|
|
||||||
|
g_gameCardCapacity = 0;
|
||||||
|
|
||||||
if (g_gameCardHfsRootHeader)
|
if (g_gameCardHfsRootHeader)
|
||||||
{
|
{
|
||||||
if (g_gameCardHfsPartitions)
|
if (g_gameCardHfsPartitions)
|
||||||
|
@ -759,7 +790,7 @@ static void gamecardCloseStorageArea(void)
|
||||||
g_gameCardStorageCurrentArea = GameCardStorageArea_None;
|
g_gameCardStorageCurrentArea = GameCardStorageArea_None;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool gamecardGetSizesFromStorageAreas(void)
|
static bool gamecardGetStorageAreasSizes(void)
|
||||||
{
|
{
|
||||||
if (!g_gameCardInserted)
|
if (!g_gameCardInserted)
|
||||||
{
|
{
|
||||||
|
@ -800,3 +831,34 @@ static bool gamecardGetSizesFromStorageAreas(void)
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline u64 gamecardGetCapacityFromRomSizeValue(u8 rom_size)
|
||||||
|
{
|
||||||
|
u64 capacity = 0;
|
||||||
|
|
||||||
|
switch(rom_size)
|
||||||
|
{
|
||||||
|
case GameCardRomSize_1GiB:
|
||||||
|
capacity = GAMECARD_CAPACITY_1GiB;
|
||||||
|
break;
|
||||||
|
case GameCardRomSize_2GiB:
|
||||||
|
capacity = GAMECARD_CAPACITY_2GiB;
|
||||||
|
break;
|
||||||
|
case GameCardRomSize_4GiB:
|
||||||
|
capacity = GAMECARD_CAPACITY_4GiB;
|
||||||
|
break;
|
||||||
|
case GameCardRomSize_8GiB:
|
||||||
|
capacity = GAMECARD_CAPACITY_8GiB;
|
||||||
|
break;
|
||||||
|
case GameCardRomSize_16GiB:
|
||||||
|
capacity = GAMECARD_CAPACITY_16GiB;
|
||||||
|
break;
|
||||||
|
case GameCardRomSize_32GiB:
|
||||||
|
capacity = GAMECARD_CAPACITY_32GiB;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return capacity;
|
||||||
|
}
|
||||||
|
|
|
@ -38,12 +38,12 @@ typedef struct {
|
||||||
} GameCardKeyFlags;
|
} GameCardKeyFlags;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
GameCardRomSize_1GB = 0xFA,
|
GameCardRomSize_1GiB = 0xFA,
|
||||||
GameCardRomSize_2GB = 0xF8,
|
GameCardRomSize_2GiB = 0xF8,
|
||||||
GameCardRomSize_4GB = 0xF0,
|
GameCardRomSize_4GiB = 0xF0,
|
||||||
GameCardRomSize_8GB = 0xE0,
|
GameCardRomSize_8GiB = 0xE0,
|
||||||
GameCardRomSize_16GB = 0xE1,
|
GameCardRomSize_16GiB = 0xE1,
|
||||||
GameCardRomSize_32GB = 0xE2
|
GameCardRomSize_32GiB = 0xE2
|
||||||
} GameCardRomSize;
|
} GameCardRomSize;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
@ -144,51 +144,15 @@ bool gamecardIsReady(void);
|
||||||
|
|
||||||
/// Used to read data from the inserted gamecard.
|
/// Used to read data from the inserted gamecard.
|
||||||
/// All required handles, changes between normal <-> secure storage areas and proper offset calculations are managed internally.
|
/// All required handles, changes between normal <-> secure storage areas and proper offset calculations are managed internally.
|
||||||
/// offset + out_size should never exceed the value returned by gamecardGetTotalRomSize().
|
/// offset + out_size should never exceed the value returned by gamecardGetTotalSize().
|
||||||
bool gamecardRead(void *out, u64 out_size, u64 offset);
|
bool gamecardRead(void *out, u64 out_size, u64 offset);
|
||||||
|
|
||||||
/// Miscellaneous functions.
|
/// Miscellaneous functions.
|
||||||
bool gamecardGetHeader(GameCardHeader *out);
|
bool gamecardGetHeader(GameCardHeader *out);
|
||||||
bool gamecardGetTotalRomSize(u64 *out);
|
bool gamecardGetTotalSize(u64 *out);
|
||||||
bool gamecardGetTrimmedRomSize(u64 *out);
|
bool gamecardGetTrimmedSize(u64 *out);
|
||||||
|
bool gamecardGetRomCapacity(u64 *out); ///< Not the same as gamecardGetTotalSize().
|
||||||
bool gamecardGetCertificate(FsGameCardCertificate *out);
|
bool gamecardGetCertificate(FsGameCardCertificate *out);
|
||||||
bool gamecardGetBundledFirmwareUpdateVersion(u32 *out);
|
bool gamecardGetBundledFirmwareUpdateVersion(u32 *out);
|
||||||
|
|
||||||
static inline u64 gamecardGetCapacityFromRomSizeValue(u8 rom_size)
|
|
||||||
{
|
|
||||||
u64 capacity = 0;
|
|
||||||
|
|
||||||
switch(rom_size)
|
|
||||||
{
|
|
||||||
case GameCardRomSize_1GB:
|
|
||||||
capacity = (u64)0x40000000;
|
|
||||||
break;
|
|
||||||
case GameCardRomSize_2GB:
|
|
||||||
capacity = (u64)0x80000000;
|
|
||||||
break;
|
|
||||||
case GameCardRomSize_4GB:
|
|
||||||
capacity = (u64)0x100000000;
|
|
||||||
break;
|
|
||||||
case GameCardRomSize_8GB:
|
|
||||||
capacity = (u64)0x200000000;
|
|
||||||
break;
|
|
||||||
case GameCardRomSize_16GB:
|
|
||||||
capacity = (u64)0x400000000;
|
|
||||||
break;
|
|
||||||
case GameCardRomSize_32GB:
|
|
||||||
capacity = (u64)0x800000000;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return capacity;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline u64 gamecardGetCapacityFromHeader(GameCardHeader *header)
|
|
||||||
{
|
|
||||||
if (!header) return 0;
|
|
||||||
return gamecardGetCapacityFromRomSizeValue(header->rom_size);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif /* __GAMECARD_H__ */
|
#endif /* __GAMECARD_H__ */
|
||||||
|
|
|
@ -87,7 +87,7 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
consoleUpdate(NULL);
|
consoleUpdate(NULL);
|
||||||
|
|
||||||
if (gamecardGetTotalRomSize(&total_size))
|
if (gamecardGetTotalSize(&total_size))
|
||||||
{
|
{
|
||||||
printf("total_size: 0x%lX\n", total_size);
|
printf("total_size: 0x%lX\n", total_size);
|
||||||
} else {
|
} else {
|
||||||
|
@ -96,7 +96,7 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
consoleUpdate(NULL);
|
consoleUpdate(NULL);
|
||||||
|
|
||||||
if (gamecardGetTrimmedRomSize(&trimmed_size))
|
if (gamecardGetTrimmedSize(&trimmed_size))
|
||||||
{
|
{
|
||||||
printf("trimmed_size: 0x%lX\n", trimmed_size);
|
printf("trimmed_size: 0x%lX\n", trimmed_size);
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue