2018-05-13 17:53:55 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <errno.h>
|
2018-07-19 20:07:53 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include "nxfs.h"
|
2018-08-18 16:59:33 +00:00
|
|
|
#include "mc.h"
|
2018-05-13 17:53:55 +00:00
|
|
|
#include "gpt.h"
|
|
|
|
#include "se.h"
|
2018-07-19 20:07:53 +00:00
|
|
|
#include "utils.h"
|
|
|
|
#include "sdmmc/sdmmc.h"
|
2018-05-13 17:53:55 +00:00
|
|
|
|
|
|
|
static bool g_ahb_redirect_enabled = false;
|
2018-07-19 20:07:53 +00:00
|
|
|
static bool g_sd_device_initialized = false;
|
|
|
|
static bool g_emmc_device_initialized = false;
|
2018-05-13 17:53:55 +00:00
|
|
|
|
2018-07-19 20:07:53 +00:00
|
|
|
static sdmmc_t g_sd_sdmmc = {0};
|
|
|
|
static sdmmc_t g_emmc_sdmmc = {0};
|
2018-06-04 17:17:23 +00:00
|
|
|
|
2018-07-19 20:07:53 +00:00
|
|
|
static sdmmc_device_t g_sd_device = {0};
|
|
|
|
static sdmmc_device_t g_emmc_device = {0};
|
2018-05-13 17:53:55 +00:00
|
|
|
|
|
|
|
typedef struct mmc_partition_info_t {
|
2018-07-19 20:07:53 +00:00
|
|
|
sdmmc_device_t *device;
|
|
|
|
SdmmcControllerNum controller;
|
|
|
|
SdmmcPartitionNum partition;
|
2018-05-13 17:53:55 +00:00
|
|
|
} mmc_partition_info_t;
|
|
|
|
|
2018-07-19 20:07:53 +00:00
|
|
|
static mmc_partition_info_t g_sd_mmcpart = {&g_sd_device, SDMMC_1, SDMMC_PARTITION_USER};
|
|
|
|
static mmc_partition_info_t g_emmc_boot0_mmcpart = {&g_emmc_device, SDMMC_4, SDMMC_PARTITION_BOOT0};
|
|
|
|
static mmc_partition_info_t g_emmc_boot1_mmcpart = {&g_emmc_device, SDMMC_4, SDMMC_PARTITION_BOOT1};
|
|
|
|
static mmc_partition_info_t g_emmc_user_mmcpart = {&g_emmc_device, SDMMC_4, SDMMC_PARTITION_USER};
|
|
|
|
|
|
|
|
SdmmcPartitionNum g_current_emmc_partition = SDMMC_PARTITION_INVALID;
|
|
|
|
|
2018-05-13 17:53:55 +00:00
|
|
|
static int mmc_partition_initialize(device_partition_t *devpart) {
|
|
|
|
mmc_partition_info_t *mmcpart = (mmc_partition_info_t *)devpart->device_struct;
|
2018-07-19 20:07:53 +00:00
|
|
|
|
2018-05-13 17:53:55 +00:00
|
|
|
if (devpart->read_cipher != NULL || devpart->write_cipher != NULL) {
|
|
|
|
devpart->crypto_work_buffer = memalign(16, devpart->sector_size * 16);
|
|
|
|
if (devpart->crypto_work_buffer == NULL) {
|
|
|
|
return ENOMEM;
|
|
|
|
} else {
|
|
|
|
devpart->crypto_work_buffer_num_sectors = devpart->sector_size * 16;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
devpart->crypto_work_buffer = NULL;
|
|
|
|
devpart->crypto_work_buffer_num_sectors = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!g_ahb_redirect_enabled) {
|
|
|
|
mc_enable_ahb_redirect();
|
|
|
|
g_ahb_redirect_enabled = true;
|
|
|
|
}
|
|
|
|
|
2018-07-19 20:07:53 +00:00
|
|
|
if (mmcpart->device == &g_sd_device) {
|
|
|
|
if (!g_sd_device_initialized) {
|
|
|
|
int rc = sdmmc_device_sd_init(mmcpart->device, &g_sd_sdmmc, SDMMC_BUS_WIDTH_4BIT, SDMMC_SPEED_SDR104) ? 0 : EIO;
|
|
|
|
if (rc)
|
2018-05-17 17:53:42 +00:00
|
|
|
return rc;
|
2018-07-19 20:07:53 +00:00
|
|
|
g_sd_device_initialized = true;
|
2018-05-13 17:53:55 +00:00
|
|
|
}
|
2018-05-17 17:53:42 +00:00
|
|
|
devpart->initialized = true;
|
|
|
|
return 0;
|
2018-07-19 20:07:53 +00:00
|
|
|
} else if (mmcpart->device == &g_emmc_device) {
|
|
|
|
if (!g_emmc_device_initialized) {
|
|
|
|
int rc = sdmmc_device_mmc_init(mmcpart->device, &g_emmc_sdmmc, SDMMC_BUS_WIDTH_8BIT, SDMMC_SPEED_HS400) ? 0 : EIO;
|
|
|
|
if (rc)
|
2018-05-17 17:53:42 +00:00
|
|
|
return rc;
|
2018-07-19 20:07:53 +00:00
|
|
|
g_emmc_device_initialized = true;
|
2018-05-13 17:53:55 +00:00
|
|
|
}
|
2018-05-17 17:53:42 +00:00
|
|
|
devpart->initialized = true;
|
|
|
|
return 0;
|
2018-05-13 17:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mmc_partition_finalize(device_partition_t *devpart) {
|
|
|
|
free(devpart->crypto_work_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mmc_partition_read(device_partition_t *devpart, void *dst, uint64_t sector, uint64_t num_sectors) {
|
|
|
|
mmc_partition_info_t *mmcpart = (mmc_partition_info_t *)devpart->device_struct;
|
2018-07-19 20:07:53 +00:00
|
|
|
|
|
|
|
if ((mmcpart->device == &g_emmc_device) && (g_current_emmc_partition != mmcpart->partition)) {
|
|
|
|
if (!sdmmc_mmc_select_partition(mmcpart->device, mmcpart->partition))
|
|
|
|
return EIO;
|
|
|
|
g_current_emmc_partition = mmcpart->partition;
|
2018-05-13 17:53:55 +00:00
|
|
|
}
|
2018-07-19 20:07:53 +00:00
|
|
|
|
|
|
|
return sdmmc_device_read(mmcpart->device, (uint32_t)(devpart->start_sector + sector), (uint32_t)num_sectors, dst) ? 0 : EIO;
|
2018-05-13 17:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int mmc_partition_write(device_partition_t *devpart, const void *src, uint64_t sector, uint64_t num_sectors) {
|
|
|
|
mmc_partition_info_t *mmcpart = (mmc_partition_info_t *)devpart->device_struct;
|
2018-07-19 20:07:53 +00:00
|
|
|
|
|
|
|
if ((mmcpart->device == &g_emmc_device) && (g_current_emmc_partition != mmcpart->partition)) {
|
|
|
|
if (!sdmmc_mmc_select_partition(mmcpart->device, mmcpart->partition))
|
|
|
|
return EIO;
|
|
|
|
g_current_emmc_partition = mmcpart->partition;
|
2018-05-13 17:53:55 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 20:07:53 +00:00
|
|
|
return sdmmc_device_write(mmcpart->device, (uint32_t)(devpart->start_sector + sector), (uint32_t)num_sectors, (void *)src) ? 0 : EIO;
|
2018-05-13 17:53:55 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 20:07:53 +00:00
|
|
|
static int nxfs_bis_crypto_decrypt(device_partition_t *devpart, uint64_t sector, uint64_t num_sectors) {
|
2018-05-17 13:15:29 +00:00
|
|
|
unsigned int keyslot_a = 4; /* These keyslots are never used by exosphere, and should be safe. */
|
|
|
|
unsigned int keyslot_b = 5;
|
|
|
|
size_t size = num_sectors * devpart->sector_size;
|
|
|
|
switch (devpart->crypto_mode) {
|
|
|
|
case DevicePartitionCryptoMode_Ctr:
|
|
|
|
set_aes_keyslot(keyslot_a, devpart->keys[0], 0x10);
|
|
|
|
se_aes_ctr_crypt(keyslot_a, devpart->crypto_work_buffer, size, devpart->crypto_work_buffer, size, devpart->iv, 0x10);
|
|
|
|
return 0;
|
|
|
|
case DevicePartitionCryptoMode_Xts:
|
|
|
|
set_aes_keyslot(keyslot_a, devpart->keys[0], 0x10);
|
|
|
|
set_aes_keyslot(keyslot_b, devpart->keys[1], 0x10);
|
2018-05-17 13:15:55 +00:00
|
|
|
se_aes_128_xts_nintendo_decrypt(keyslot_a, keyslot_b, sector, devpart->crypto_work_buffer, devpart->crypto_work_buffer, size, devpart->sector_size);
|
2018-05-17 13:15:29 +00:00
|
|
|
return 0;
|
|
|
|
case DevicePartitionCryptoMode_None:
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
2018-05-13 17:53:55 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 20:07:53 +00:00
|
|
|
static int nxfs_bis_crypto_encrypt(device_partition_t *devpart, uint64_t sector, uint64_t num_sectors) {
|
2018-05-17 13:15:29 +00:00
|
|
|
unsigned int keyslot_a = 4; /* These keyslots are never used by exosphere, and should be safe. */
|
|
|
|
unsigned int keyslot_b = 5;
|
|
|
|
size_t size = num_sectors * devpart->sector_size;
|
|
|
|
switch (devpart->crypto_mode) {
|
|
|
|
case DevicePartitionCryptoMode_Ctr:
|
|
|
|
set_aes_keyslot(keyslot_a, devpart->keys[0], 0x10);
|
|
|
|
se_aes_ctr_crypt(keyslot_a, devpart->crypto_work_buffer, size, devpart->crypto_work_buffer, size, devpart->iv, 0x10);
|
|
|
|
return 0;
|
|
|
|
case DevicePartitionCryptoMode_Xts:
|
|
|
|
set_aes_keyslot(keyslot_a, devpart->keys[0], 0x10);
|
|
|
|
set_aes_keyslot(keyslot_b, devpart->keys[1], 0x10);
|
2018-05-17 13:15:55 +00:00
|
|
|
se_aes_128_xts_nintendo_encrypt(keyslot_a, keyslot_b, sector, devpart->crypto_work_buffer, devpart->crypto_work_buffer, size, devpart->sector_size);
|
2018-05-17 13:15:29 +00:00
|
|
|
return 0;
|
|
|
|
case DevicePartitionCryptoMode_None:
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
2018-05-13 17:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const device_partition_t g_mmc_devpart_template = {
|
|
|
|
.sector_size = 512,
|
|
|
|
.initializer = mmc_partition_initialize,
|
|
|
|
.finalizer = mmc_partition_finalize,
|
|
|
|
.reader = mmc_partition_read,
|
|
|
|
.writer = mmc_partition_write,
|
|
|
|
};
|
|
|
|
|
2018-07-19 20:07:53 +00:00
|
|
|
static int nxfs_mount_partition_gpt_callback(const efi_entry_t *entry, void *param, size_t entry_offset, FILE *disk) {
|
2018-05-13 17:53:55 +00:00
|
|
|
(void)entry_offset;
|
|
|
|
(void)disk;
|
|
|
|
device_partition_t *parent = (device_partition_t *)param;
|
|
|
|
device_partition_t devpart = *parent;
|
2018-07-26 17:45:18 +00:00
|
|
|
char name_buffer[128];
|
2018-05-13 17:53:55 +00:00
|
|
|
const uint16_t *utf16name = entry->name;
|
|
|
|
uint32_t name_len;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
const char *partition_name;
|
|
|
|
const char *mount_point;
|
|
|
|
bool is_fat;
|
|
|
|
bool is_encrypted;
|
2018-05-16 23:53:32 +00:00
|
|
|
bool register_immediately;
|
2018-05-13 17:53:55 +00:00
|
|
|
} known_partitions[] = {
|
2018-05-16 23:53:32 +00:00
|
|
|
{"PRODINFO", "prodinfo", false, true, false},
|
|
|
|
{"PRODINFOF", "prodinfof", true, true, false},
|
|
|
|
{"BCPKG2-1-Normal-Main", "bcpkg21", false, false, true},
|
|
|
|
{"BCPKG2-2-Normal-Sub", "bcpkg22", false, false, false},
|
|
|
|
{"BCPKG2-3-SafeMode-Main", "bcpkg23", false, false, false},
|
|
|
|
{"BCPKG2-4-SafeMode-Sub", "bcpkg24", false, false, false},
|
|
|
|
{"BCPKG2-5-Repair-Main", "bcpkg25", false, false, false},
|
|
|
|
{"BCPKG2-6-Repair-Sub", "bcpkg26", false, false, false},
|
|
|
|
{"SAFE", "safe", true, true, false},
|
|
|
|
{"SYSTEM", "system", true, true, false},
|
|
|
|
{"USER", "user", true, true, false},
|
2018-05-13 17:53:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Convert the partition name to ASCII, for comparison. */
|
|
|
|
for (name_len = 0; name_len < sizeof(entry->name) && *utf16name != 0; name_len++) {
|
|
|
|
name_buffer[name_len] = (char)*utf16name++;
|
|
|
|
}
|
|
|
|
name_buffer[name_len] = '\0';
|
|
|
|
|
|
|
|
/* Mount the partition, if we know about it. */
|
|
|
|
for (size_t i = 0; i < sizeof(known_partitions)/sizeof(known_partitions[0]); i++) {
|
|
|
|
if (strcmp(name_buffer, known_partitions[i].partition_name) == 0) {
|
|
|
|
devpart.start_sector += entry->first_lba;
|
2018-05-17 12:38:19 +00:00
|
|
|
devpart.num_sectors = (entry->last_lba + 1) - entry->first_lba;
|
2018-05-13 17:53:55 +00:00
|
|
|
if (parent->num_sectors < devpart.num_sectors) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (known_partitions[i].is_encrypted) {
|
2018-07-19 20:07:53 +00:00
|
|
|
devpart.read_cipher = nxfs_bis_crypto_decrypt;
|
|
|
|
devpart.write_cipher = nxfs_bis_crypto_encrypt;
|
2018-05-17 17:53:42 +00:00
|
|
|
devpart.crypto_mode = DevicePartitionCryptoMode_Xts;
|
2018-05-13 17:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (known_partitions[i].is_fat) {
|
2018-05-16 23:53:32 +00:00
|
|
|
rc = fsdev_mount_device(known_partitions[i].mount_point, &devpart, false);
|
2018-05-13 17:53:55 +00:00
|
|
|
if (rc == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-05-16 23:53:32 +00:00
|
|
|
if (known_partitions[i].register_immediately) {
|
|
|
|
rc = fsdev_register_device(known_partitions[i].mount_point);
|
|
|
|
if (rc == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2018-05-13 17:53:55 +00:00
|
|
|
} else {
|
2018-05-16 23:53:32 +00:00
|
|
|
rc = rawdev_mount_device(known_partitions[i].mount_point, &devpart, false);
|
2018-05-13 17:53:55 +00:00
|
|
|
if (rc == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-05-16 23:53:32 +00:00
|
|
|
if (known_partitions[i].register_immediately) {
|
|
|
|
rc = rawdev_register_device(known_partitions[i].mount_point);
|
|
|
|
if (rc == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2018-05-13 17:53:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-19 20:07:53 +00:00
|
|
|
int nxfs_mount_all(void) {
|
2018-05-13 17:53:55 +00:00
|
|
|
device_partition_t model;
|
|
|
|
int rc;
|
|
|
|
FILE *rawnand;
|
|
|
|
|
|
|
|
/* Initialize the SD card and its primary partition. */
|
|
|
|
model = g_mmc_devpart_template;
|
|
|
|
model.device_struct = &g_sd_mmcpart;
|
|
|
|
model.start_sector = 0;
|
|
|
|
model.num_sectors = 1u << 30; /* arbitrary numbers of sectors. TODO: find the size of the SD in sectors. */
|
2018-07-19 20:07:53 +00:00
|
|
|
|
2018-05-13 17:53:55 +00:00
|
|
|
rc = fsdev_mount_device("sdmc", &model, true);
|
2018-07-19 20:07:53 +00:00
|
|
|
|
2018-05-13 17:53:55 +00:00
|
|
|
if (rc == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-07-19 20:07:53 +00:00
|
|
|
|
2018-05-16 23:53:32 +00:00
|
|
|
rc = fsdev_register_device("sdmc");
|
2018-07-19 20:07:53 +00:00
|
|
|
|
2018-05-16 23:53:32 +00:00
|
|
|
if (rc == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-05-13 17:53:55 +00:00
|
|
|
|
|
|
|
/* Boot0. */
|
|
|
|
model = g_mmc_devpart_template;
|
2018-07-19 20:07:53 +00:00
|
|
|
model.device_struct = &g_emmc_boot0_mmcpart;
|
2018-05-13 17:53:55 +00:00
|
|
|
model.start_sector = 0;
|
|
|
|
model.num_sectors = 0x184000 / model.sector_size;
|
2018-07-19 20:07:53 +00:00
|
|
|
|
2018-05-13 17:53:55 +00:00
|
|
|
rc = rawdev_mount_device("boot0", &model, true);
|
2018-07-19 20:07:53 +00:00
|
|
|
|
2018-05-13 17:53:55 +00:00
|
|
|
if (rc == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-07-19 20:07:53 +00:00
|
|
|
|
2018-05-16 23:53:32 +00:00
|
|
|
rc = rawdev_register_device("boot0");
|
2018-07-19 20:07:53 +00:00
|
|
|
|
2018-05-16 23:53:32 +00:00
|
|
|
if (rc == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-07-19 20:07:53 +00:00
|
|
|
|
2018-05-13 17:53:55 +00:00
|
|
|
/* Boot1. */
|
|
|
|
model = g_mmc_devpart_template;
|
2018-07-19 20:07:53 +00:00
|
|
|
model.device_struct = &g_emmc_boot1_mmcpart;
|
2018-05-13 17:53:55 +00:00
|
|
|
model.start_sector = 0;
|
|
|
|
model.num_sectors = 0x80000 / model.sector_size;
|
2018-07-19 20:07:53 +00:00
|
|
|
|
2018-05-13 17:53:55 +00:00
|
|
|
rc = rawdev_mount_device("boot1", &model, false);
|
2018-07-19 20:07:53 +00:00
|
|
|
|
2018-05-13 17:53:55 +00:00
|
|
|
if (rc == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-07-19 20:07:53 +00:00
|
|
|
|
2018-05-16 23:53:32 +00:00
|
|
|
/* Don't register boot1 for now. */
|
2018-05-13 17:53:55 +00:00
|
|
|
|
|
|
|
/* Raw NAND (excluding boot partitions), and its partitions. */
|
|
|
|
model = g_mmc_devpart_template;
|
|
|
|
model = g_mmc_devpart_template;
|
2018-07-19 20:07:53 +00:00
|
|
|
model.device_struct = &g_emmc_user_mmcpart;
|
2018-05-13 17:53:55 +00:00
|
|
|
model.start_sector = 0;
|
|
|
|
model.num_sectors = (32ull << 30) / model.sector_size;
|
2018-07-19 20:07:53 +00:00
|
|
|
|
2018-05-13 17:53:55 +00:00
|
|
|
rc = rawdev_mount_device("rawnand", &model, false);
|
2018-07-19 20:07:53 +00:00
|
|
|
|
2018-05-13 17:53:55 +00:00
|
|
|
if (rc == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-07-19 20:07:53 +00:00
|
|
|
|
2018-05-16 23:53:32 +00:00
|
|
|
rc = rawdev_register_device("rawnand");
|
|
|
|
if (rc == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-07-19 20:07:53 +00:00
|
|
|
|
2018-05-13 17:53:55 +00:00
|
|
|
rawnand = fopen("rawnand:/", "rb");
|
2018-07-26 17:45:18 +00:00
|
|
|
|
|
|
|
if (rawnand == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-07-19 20:07:53 +00:00
|
|
|
rc = gpt_iterate_through_entries(rawnand, model.sector_size, nxfs_mount_partition_gpt_callback, &model);
|
2018-05-13 17:53:55 +00:00
|
|
|
fclose(rawnand);
|
2018-07-19 20:07:53 +00:00
|
|
|
|
2018-05-16 23:53:32 +00:00
|
|
|
if (rc == 0) {
|
2018-05-13 17:53:55 +00:00
|
|
|
rc = fsdev_set_default_device("sdmc");
|
|
|
|
}
|
2018-07-19 20:07:53 +00:00
|
|
|
|
2018-05-13 17:53:55 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2018-07-19 20:07:53 +00:00
|
|
|
int nxfs_unmount_all(void) {
|
|
|
|
return ((fsdev_unmount_all() || rawdev_unmount_all()) ? -1 : 0);
|
2018-05-13 17:53:55 +00:00
|
|
|
}
|