2019-03-07 23:14:43 +00:00
|
|
|
/*
|
|
|
|
* Atmosphère Fusée Secondary Storage parser.
|
|
|
|
*
|
2020-03-14 07:24:24 +00:00
|
|
|
* Copyright (c) 2019-2020 CTCaer
|
2019-03-07 23:14:43 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "fss.h"
|
|
|
|
#include "hos.h"
|
2020-06-14 14:45:45 +01:00
|
|
|
#include "../config.h"
|
|
|
|
#include <libs/fatfs/ff.h>
|
|
|
|
#include <mem/heap.h>
|
2019-06-30 01:24:58 +01:00
|
|
|
#include "../storage/emummc.h"
|
2020-06-14 14:45:45 +01:00
|
|
|
#include <storage/nx_sd.h>
|
2019-03-07 23:14:43 +00:00
|
|
|
|
2020-06-14 14:45:45 +01:00
|
|
|
#include <gfx_utils.h>
|
2019-03-07 23:14:43 +00:00
|
|
|
#define DPRINTF(...)
|
|
|
|
|
2019-06-30 01:24:58 +01:00
|
|
|
extern hekate_config h_cfg;
|
|
|
|
|
2019-12-11 22:13:32 +00:00
|
|
|
extern bool is_ipl_updated(void *buf, char *path, bool force);
|
|
|
|
|
2020-04-30 14:34:24 +01:00
|
|
|
// FSS0 Magic and Meta header offset.
|
2019-03-07 23:14:43 +00:00
|
|
|
#define FSS0_MAGIC 0x30535346
|
2020-04-30 14:34:24 +01:00
|
|
|
#define FSS0_META_OFFSET 0x4
|
|
|
|
|
|
|
|
// FSS0 Content Types.
|
2019-03-07 23:14:43 +00:00
|
|
|
#define CNT_TYPE_FSP 0
|
2020-04-30 14:34:24 +01:00
|
|
|
#define CNT_TYPE_EXO 1 // Exosphere (Secure Monitor).
|
|
|
|
#define CNT_TYPE_WBT 2 // Warmboot (SC7Exit fw).
|
|
|
|
#define CNT_TYPE_RBT 3 // Rebootstub (Warmboot based reboot fw).
|
|
|
|
#define CNT_TYPE_SP1 4 // Sept Primary (TSEC and Sept Secondary loader).
|
|
|
|
#define CNT_TYPE_SP2 5 // Sept Secondary (Acts as pkg11 and derives keys).
|
|
|
|
#define CNT_TYPE_KIP 6 // KIP1 (Used for replacement or addition).
|
2019-03-07 23:14:43 +00:00
|
|
|
#define CNT_TYPE_BMP 7
|
2019-06-30 01:24:58 +01:00
|
|
|
#define CNT_TYPE_EMC 8
|
2020-04-30 14:34:24 +01:00
|
|
|
#define CNT_TYPE_KLD 9 // Kernel Loader.
|
|
|
|
#define CNT_TYPE_KRN 10 // Kernel.
|
2019-03-07 23:14:43 +00:00
|
|
|
|
2020-04-30 14:34:24 +01:00
|
|
|
// FSS0 Content Flags.
|
2020-03-09 06:58:12 +00:00
|
|
|
#define CNT_FLAG0_EXPERIMENTAL (1 << 0)
|
|
|
|
|
2020-04-30 14:34:24 +01:00
|
|
|
// FSS0 Meta Header.
|
|
|
|
typedef struct _fss_meta_t
|
2019-03-07 23:14:43 +00:00
|
|
|
{
|
|
|
|
u32 magic;
|
|
|
|
u32 size;
|
|
|
|
u32 crt0_off;
|
|
|
|
u32 cnt_off;
|
|
|
|
u32 cnt_count;
|
|
|
|
u32 hos_ver;
|
|
|
|
u32 version;
|
|
|
|
u32 git_rev;
|
2020-04-30 14:34:24 +01:00
|
|
|
} fss_meta_t;
|
2019-03-07 23:14:43 +00:00
|
|
|
|
2020-04-30 14:34:24 +01:00
|
|
|
// FSS0 Content Header.
|
2019-03-07 23:14:43 +00:00
|
|
|
typedef struct _fss_content_t
|
|
|
|
{
|
|
|
|
u32 offset;
|
|
|
|
u32 size;
|
2020-03-09 06:58:12 +00:00
|
|
|
u8 type;
|
|
|
|
u8 flags0;
|
|
|
|
u8 flags1;
|
|
|
|
u8 flags2;
|
2019-03-07 23:14:43 +00:00
|
|
|
u32 rsvd1;
|
|
|
|
char name[0x10];
|
|
|
|
} fss_content_t;
|
|
|
|
|
2019-12-11 22:13:32 +00:00
|
|
|
static void _update_r2p(const char *path)
|
|
|
|
{
|
|
|
|
char *r2p_path = malloc(256);
|
|
|
|
u32 path_len = strlen(path);
|
|
|
|
strcpy(r2p_path, path);
|
|
|
|
|
|
|
|
while(path_len)
|
|
|
|
{
|
|
|
|
if ((r2p_path[path_len - 1] == '/') || (r2p_path[path_len - 1] == 0x5C))
|
|
|
|
{
|
|
|
|
r2p_path[path_len] = 0;
|
|
|
|
strcat(r2p_path, "reboot_payload.bin");
|
|
|
|
u8 *r2p_payload = sd_file_read(r2p_path, NULL);
|
|
|
|
|
|
|
|
is_ipl_updated(r2p_payload, r2p_path, h_cfg.updater2p ? true : false);
|
|
|
|
|
|
|
|
free(r2p_payload);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
path_len--;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(r2p_path);
|
|
|
|
}
|
|
|
|
|
2019-12-15 22:53:22 +00:00
|
|
|
int parse_fss(launch_ctxt_t *ctxt, const char *path, fss0_sept_t *sept_ctxt)
|
2019-03-07 23:14:43 +00:00
|
|
|
{
|
|
|
|
FIL fp;
|
2019-03-09 18:49:00 +00:00
|
|
|
|
|
|
|
bool stock = false;
|
2019-12-15 22:53:22 +00:00
|
|
|
int sept_used = 0;
|
2019-03-09 18:49:00 +00:00
|
|
|
|
2020-07-17 16:00:32 +01:00
|
|
|
// Skip if stock and Exosphere and warmboot are not needed.
|
2020-01-17 07:26:13 +00:00
|
|
|
if (!sept_ctxt)
|
2019-03-09 18:49:00 +00:00
|
|
|
{
|
2020-01-17 07:26:13 +00:00
|
|
|
LIST_FOREACH_ENTRY(ini_kv_t, kv, &ctxt->cfg->kvs, link)
|
|
|
|
{
|
|
|
|
if (!strcmp("stock", kv->key))
|
|
|
|
if (kv->val[0] == '1')
|
|
|
|
stock = true;
|
|
|
|
}
|
2019-03-09 18:49:00 +00:00
|
|
|
|
2020-07-17 22:54:35 +01:00
|
|
|
bool emummc_disabled = !emu_cfg.enabled || h_cfg.emummc_force_disable;
|
|
|
|
if (stock && ctxt->pkg1_id->kb <= KB_FIRMWARE_VERSION_620 && emummc_disabled)
|
2020-01-17 07:26:13 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2019-03-09 18:49:00 +00:00
|
|
|
|
2019-12-11 22:13:32 +00:00
|
|
|
if (f_open(&fp, path, FA_READ) != FR_OK)
|
2019-03-07 23:14:43 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
void *fss = malloc(f_size(&fp));
|
|
|
|
|
2020-04-30 14:34:24 +01:00
|
|
|
// Read first 1024 bytes of the fss file.
|
|
|
|
f_read(&fp, fss, 1024, NULL);
|
|
|
|
|
|
|
|
// Get FSS0 Meta header offset.
|
|
|
|
u32 fss_meta_addr = *(u32 *)(fss + FSS0_META_OFFSET);
|
|
|
|
fss_meta_t *fss_meta = (fss_meta_t *)(fss + fss_meta_addr);
|
2019-03-07 23:14:43 +00:00
|
|
|
|
2020-04-30 14:34:24 +01:00
|
|
|
// Check if valid FSS0 and parse it.
|
2019-03-07 23:14:43 +00:00
|
|
|
if (fss_meta->magic == FSS0_MAGIC)
|
|
|
|
{
|
2019-04-14 00:30:14 +01:00
|
|
|
gfx_printf("Found FSS0, Atmosphere %d.%d.%d-%08x\n"
|
2019-03-07 23:14:43 +00:00
|
|
|
"Max HOS supported: %d.%d.%d\n"
|
|
|
|
"Unpacking and loading components.. ",
|
|
|
|
fss_meta->version >> 24, (fss_meta->version >> 16) & 0xFF, (fss_meta->version >> 8) & 0xFF, fss_meta->git_rev,
|
|
|
|
fss_meta->hos_ver >> 24, (fss_meta->hos_ver >> 16) & 0xFF, (fss_meta->hos_ver >> 8) & 0xFF);
|
|
|
|
|
2020-01-17 07:26:13 +00:00
|
|
|
if (!sept_ctxt)
|
|
|
|
{
|
|
|
|
ctxt->atmosphere = true;
|
|
|
|
ctxt->fss0_hosver = fss_meta->hos_ver;
|
|
|
|
}
|
|
|
|
|
2020-04-30 14:34:24 +01:00
|
|
|
// Parse FSS0 contents.
|
2019-03-07 23:14:43 +00:00
|
|
|
fss_content_t *curr_fss_cnt = (fss_content_t *)(fss + fss_meta->cnt_off);
|
|
|
|
void *content;
|
2019-06-30 01:15:46 +01:00
|
|
|
for (u32 i = 0; i < fss_meta->cnt_count; i++)
|
2019-03-07 23:14:43 +00:00
|
|
|
{
|
|
|
|
content = (void *)(fss + curr_fss_cnt[i].offset);
|
2020-04-30 14:34:24 +01:00
|
|
|
|
|
|
|
// Check if offset is inside limits.
|
2019-03-07 23:14:43 +00:00
|
|
|
if ((curr_fss_cnt[i].offset + curr_fss_cnt[i].size) > fss_meta->size)
|
|
|
|
continue;
|
|
|
|
|
2020-04-30 14:34:24 +01:00
|
|
|
// If content is experimental and experimental flag is not enabled, skip it.
|
2020-10-17 21:16:16 +01:00
|
|
|
if ((curr_fss_cnt[i].flags0 & CNT_FLAG0_EXPERIMENTAL) && !ctxt->fss0_experimental)
|
2020-03-09 06:58:12 +00:00
|
|
|
continue;
|
|
|
|
|
2020-04-30 14:34:24 +01:00
|
|
|
// Parse content.
|
2019-12-15 22:53:22 +00:00
|
|
|
if (!sept_ctxt)
|
2019-03-07 23:14:43 +00:00
|
|
|
{
|
2020-04-30 14:34:24 +01:00
|
|
|
// Prepare content context.
|
2019-12-15 22:53:22 +00:00
|
|
|
switch (curr_fss_cnt[i].type)
|
|
|
|
{
|
|
|
|
case CNT_TYPE_KIP:
|
|
|
|
if (stock)
|
|
|
|
continue;
|
|
|
|
merge_kip_t *mkip1 = (merge_kip_t *)malloc(sizeof(merge_kip_t));
|
|
|
|
mkip1->kip1 = content;
|
|
|
|
list_append(&ctxt->kip1_list, &mkip1->link);
|
|
|
|
DPRINTF("Loaded %s.kip1 from FSS0 (size %08X)\n", curr_fss_cnt[i].name, curr_fss_cnt[i].size);
|
|
|
|
break;
|
2020-11-15 11:45:45 +00:00
|
|
|
|
|
|
|
case CNT_TYPE_KRN:
|
|
|
|
if (stock)
|
|
|
|
continue;
|
|
|
|
ctxt->kernel_size = curr_fss_cnt[i].size;
|
|
|
|
ctxt->kernel = content;
|
|
|
|
break;
|
|
|
|
|
2019-12-15 22:53:22 +00:00
|
|
|
case CNT_TYPE_EXO:
|
|
|
|
ctxt->secmon_size = curr_fss_cnt[i].size;
|
|
|
|
ctxt->secmon = content;
|
|
|
|
break;
|
2020-11-15 11:45:45 +00:00
|
|
|
|
2019-12-15 22:53:22 +00:00
|
|
|
case CNT_TYPE_WBT:
|
|
|
|
ctxt->warmboot_size = curr_fss_cnt[i].size;
|
|
|
|
ctxt->warmboot = content;
|
|
|
|
break;
|
2020-11-15 11:45:45 +00:00
|
|
|
|
2019-12-15 22:53:22 +00:00
|
|
|
default:
|
2019-03-09 18:49:00 +00:00
|
|
|
continue;
|
2019-12-15 22:53:22 +00:00
|
|
|
}
|
2020-04-30 14:34:24 +01:00
|
|
|
|
|
|
|
// Load content to launch context.
|
|
|
|
f_lseek(&fp, curr_fss_cnt[i].offset);
|
|
|
|
f_read(&fp, content, curr_fss_cnt[i].size, NULL);
|
2019-12-15 22:53:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-30 14:34:24 +01:00
|
|
|
// Load sept content directly to launch context.
|
2019-12-15 22:53:22 +00:00
|
|
|
switch (curr_fss_cnt[i].type)
|
|
|
|
{
|
|
|
|
case CNT_TYPE_SP1:
|
2020-01-17 07:27:53 +00:00
|
|
|
f_lseek(&fp, curr_fss_cnt[i].offset);
|
|
|
|
f_read(&fp, sept_ctxt->sept_primary, curr_fss_cnt[i].size, NULL);
|
2020-04-30 14:34:24 +01:00
|
|
|
break;
|
2019-12-15 22:53:22 +00:00
|
|
|
case CNT_TYPE_SP2:
|
|
|
|
if (!memcmp(curr_fss_cnt[i].name, (sept_ctxt->kb < KB_FIRMWARE_VERSION_810) ? "septsecondary00" : "septsecondary01", 15))
|
|
|
|
{
|
2020-01-17 07:27:53 +00:00
|
|
|
f_lseek(&fp, curr_fss_cnt[i].offset);
|
|
|
|
f_read(&fp, sept_ctxt->sept_secondary, curr_fss_cnt[i].size, NULL);
|
2019-12-15 22:53:22 +00:00
|
|
|
sept_used = 1;
|
2020-01-17 07:27:53 +00:00
|
|
|
goto out;
|
2019-12-15 22:53:22 +00:00
|
|
|
}
|
2020-04-30 14:34:24 +01:00
|
|
|
break;
|
2019-12-15 22:53:22 +00:00
|
|
|
default:
|
2020-04-30 14:34:24 +01:00
|
|
|
break;
|
2019-12-15 22:53:22 +00:00
|
|
|
}
|
2019-03-07 23:14:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-17 07:27:53 +00:00
|
|
|
out:
|
2019-04-14 00:30:14 +01:00
|
|
|
gfx_printf("Done!\n");
|
2019-03-07 23:14:43 +00:00
|
|
|
f_close(&fp);
|
|
|
|
|
2019-12-11 22:13:32 +00:00
|
|
|
_update_r2p(path);
|
|
|
|
|
2019-12-15 22:53:22 +00:00
|
|
|
return (!sept_ctxt ? 1 : sept_used);
|
2019-03-07 23:14:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f_close(&fp);
|
|
|
|
free(fss);
|
|
|
|
|
|
|
|
return 0;
|
2019-12-15 22:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int load_sept_from_ffs0(fss0_sept_t *sept_ctxt)
|
|
|
|
{
|
|
|
|
LIST_FOREACH_ENTRY(ini_kv_t, kv, &sept_ctxt->cfg_sec->kvs, link)
|
|
|
|
{
|
|
|
|
if (!strcmp("fss0", kv->key))
|
|
|
|
return parse_fss(NULL, kv->val, sept_ctxt);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|