2018-09-07 16:00:13 +01:00
|
|
|
/*
|
2019-04-08 03:00:49 +01:00
|
|
|
* Copyright (c) 2018-2019 Atmosphère-NX
|
2018-09-07 16:00:13 +01: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/>.
|
|
|
|
*/
|
|
|
|
|
2018-04-07 22:43:54 +01:00
|
|
|
#include "utils.h"
|
2018-05-21 01:24:41 +01:00
|
|
|
#include "exception_handlers.h"
|
2018-05-20 13:11:46 +01:00
|
|
|
#include "panic.h"
|
2018-04-07 22:43:54 +01:00
|
|
|
#include "hwinit.h"
|
2018-08-18 17:59:33 +01:00
|
|
|
#include "di.h"
|
2018-07-19 21:07:53 +01:00
|
|
|
#include "timers.h"
|
2018-05-13 22:49:50 +01:00
|
|
|
#include "fs_utils.h"
|
2018-04-08 05:51:24 +01:00
|
|
|
#include "stage2.h"
|
2018-05-07 22:32:45 +01:00
|
|
|
#include "chainloader.h"
|
2018-07-04 21:55:27 +01:00
|
|
|
#include "sdmmc/sdmmc.h"
|
2018-05-06 16:22:12 +01:00
|
|
|
#include "lib/fatfs/ff.h"
|
2018-09-26 01:18:50 +01:00
|
|
|
#include "lib/log.h"
|
2018-09-28 21:12:59 +01:00
|
|
|
#include "lib/vsprintf.h"
|
|
|
|
#include "lib/ini.h"
|
2018-04-08 05:51:24 +01:00
|
|
|
#include "display/video_fb.h"
|
2018-04-07 22:43:54 +01:00
|
|
|
|
2018-05-08 15:51:43 +01:00
|
|
|
extern void (*__program_exit_callback)(int rc);
|
|
|
|
|
|
|
|
static void *g_framebuffer;
|
2018-05-07 22:32:45 +01:00
|
|
|
static char g_bct0_buffer[BCTO_MAX_SIZE];
|
2018-04-07 22:43:54 +01:00
|
|
|
|
2018-09-28 21:12:59 +01:00
|
|
|
#define CONFIG_LOG_LEVEL_KEY "log_level"
|
|
|
|
|
2018-05-04 20:52:38 +01:00
|
|
|
#define DEFAULT_BCT0_FOR_DEBUG \
|
|
|
|
"BCT0\n"\
|
|
|
|
"[stage1]\n"\
|
2018-12-04 11:54:25 +00:00
|
|
|
"stage2_path = atmosphere/fusee-secondary.bin\n"\
|
2019-07-26 20:38:15 +01:00
|
|
|
"stage2_mtc_path = atmosphere/fusee-mtc.bin\n"\
|
2018-05-07 22:50:30 +01:00
|
|
|
"stage2_addr = 0xF0000000\n"\
|
|
|
|
"stage2_entrypoint = 0xF0000000\n"
|
2018-04-08 06:04:30 +01:00
|
|
|
|
2018-05-08 15:51:43 +01:00
|
|
|
static const char *load_config(void) {
|
2018-12-04 11:54:25 +00:00
|
|
|
if (!read_from_file(g_bct0_buffer, BCTO_MAX_SIZE, "atmosphere/BCT.ini")) {
|
2018-09-26 01:18:50 +01:00
|
|
|
print(SCREEN_LOG_LEVEL_DEBUG, "Failed to read BCT0 from SD!\n");
|
|
|
|
print(SCREEN_LOG_LEVEL_DEBUG, "Using default BCT0!\n");
|
2018-05-07 22:32:45 +01:00
|
|
|
memcpy(g_bct0_buffer, DEFAULT_BCT0_FOR_DEBUG, sizeof(DEFAULT_BCT0_FOR_DEBUG));
|
2018-04-07 22:43:54 +01:00
|
|
|
}
|
2018-05-04 20:52:38 +01:00
|
|
|
|
2018-05-07 22:32:45 +01:00
|
|
|
if (memcmp(g_bct0_buffer, "BCT0", 4) != 0) {
|
2018-05-20 15:18:48 +01:00
|
|
|
fatal_error("Unexpected magic in BCT.ini!\n");
|
2018-04-08 05:51:24 +01:00
|
|
|
}
|
2018-09-28 21:12:59 +01:00
|
|
|
|
2018-04-08 05:51:24 +01:00
|
|
|
/* Return pointer to first line of the ini. */
|
2018-05-07 22:32:45 +01:00
|
|
|
const char *bct0 = g_bct0_buffer;
|
2018-04-08 05:51:24 +01:00
|
|
|
while (*bct0 && *bct0 != '\n') {
|
|
|
|
bct0++;
|
|
|
|
}
|
2018-09-28 21:12:59 +01:00
|
|
|
|
2018-04-08 05:51:24 +01:00
|
|
|
if (!bct0) {
|
2018-05-20 15:18:48 +01:00
|
|
|
fatal_error("BCT.ini has no newline!\n");
|
2018-04-08 05:51:24 +01:00
|
|
|
}
|
2018-09-28 21:12:59 +01:00
|
|
|
|
2018-04-08 05:51:24 +01:00
|
|
|
return bct0;
|
2018-04-07 22:43:54 +01:00
|
|
|
}
|
|
|
|
|
2018-09-28 21:12:59 +01:00
|
|
|
static int config_ini_handler(void *user, const char *section, const char *name, const char *value) {
|
|
|
|
if (strcmp(section, "config") == 0) {
|
|
|
|
if (strcmp(name, CONFIG_LOG_LEVEL_KEY) == 0) {
|
|
|
|
ScreenLogLevel *config_log_level = (ScreenLogLevel *)user;
|
|
|
|
int log_level = 0;
|
|
|
|
sscanf(value, "%d", &log_level);
|
|
|
|
*config_log_level = (ScreenLogLevel)log_level;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-07-06 20:58:01 +01:00
|
|
|
static void setup_display(void) {
|
2018-05-08 15:51:43 +01:00
|
|
|
g_framebuffer = (void *)0xC0000000;
|
2019-07-06 20:58:01 +01:00
|
|
|
|
2018-05-08 12:36:05 +01:00
|
|
|
/* Zero-fill the framebuffer and register it as printk provider. */
|
2018-05-08 15:51:43 +01:00
|
|
|
video_init(g_framebuffer);
|
2018-05-08 12:36:05 +01:00
|
|
|
|
2018-04-08 05:51:24 +01:00
|
|
|
/* Initialize the display. */
|
|
|
|
display_init();
|
2018-05-04 20:52:38 +01:00
|
|
|
|
2018-05-08 12:36:05 +01:00
|
|
|
/* Set the framebuffer. */
|
2018-05-08 15:51:43 +01:00
|
|
|
display_init_framebuffer(g_framebuffer);
|
2018-05-04 20:52:38 +01:00
|
|
|
|
2018-04-08 06:04:30 +01:00
|
|
|
/* Turn on the backlight after initializing the lfb */
|
|
|
|
/* to avoid flickering. */
|
2018-08-18 17:59:33 +01:00
|
|
|
display_backlight(true);
|
2019-07-06 20:58:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cleanup_display(void) {
|
|
|
|
/* Turn off the backlight. */
|
|
|
|
display_backlight(false);
|
|
|
|
|
|
|
|
/* Terminate the display. */
|
|
|
|
display_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setup_env(void) {
|
|
|
|
/* Initialize hardware. */
|
|
|
|
nx_hwinit();
|
2018-05-21 01:24:41 +01:00
|
|
|
|
|
|
|
/* Set up the exception handlers. */
|
|
|
|
setup_exception_handlers();
|
2018-12-04 12:01:22 +00:00
|
|
|
|
2018-07-04 21:55:27 +01:00
|
|
|
/* Mount the SD card. */
|
|
|
|
mount_sd();
|
2018-05-08 15:51:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cleanup_env(void) {
|
2018-07-04 21:55:27 +01:00
|
|
|
/* Unmount the SD card. */
|
|
|
|
unmount_sd();
|
2018-05-08 15:51:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void exit_callback(int rc) {
|
|
|
|
(void)rc;
|
|
|
|
relocate_and_chainload();
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void) {
|
|
|
|
const char *bct0;
|
|
|
|
const char *stage2_path;
|
2018-05-21 02:42:05 +01:00
|
|
|
stage2_args_t *stage2_args;
|
2018-05-21 13:18:03 +01:00
|
|
|
uint32_t stage2_version = 0;
|
2019-07-06 20:58:01 +01:00
|
|
|
ScreenLogLevel log_level = SCREEN_LOG_LEVEL_NONE;
|
|
|
|
|
|
|
|
/* Initialize the boot environment. */
|
2018-05-08 15:51:43 +01:00
|
|
|
setup_env();
|
2019-04-12 23:28:46 +01:00
|
|
|
|
|
|
|
/* Check for panics. */
|
|
|
|
check_and_display_panic();
|
2018-07-04 21:55:27 +01:00
|
|
|
|
2018-04-08 05:51:24 +01:00
|
|
|
/* Load the BCT0 configuration ini off of the SD. */
|
|
|
|
bct0 = load_config();
|
2018-05-04 20:52:38 +01:00
|
|
|
|
2019-04-07 19:02:01 +01:00
|
|
|
/* Extract the logging level from the BCT.ini file. */
|
|
|
|
if (ini_parse_string(bct0, config_ini_handler, &log_level) < 0) {
|
2018-09-28 21:12:59 +01:00
|
|
|
fatal_error("Failed to parse BCT.ini!\n");
|
|
|
|
}
|
|
|
|
|
2019-07-06 20:58:01 +01:00
|
|
|
/* Override the global logging level. */
|
|
|
|
log_set_log_level(log_level);
|
|
|
|
|
|
|
|
if (log_level != SCREEN_LOG_LEVEL_NONE) {
|
|
|
|
/* Initialize the display for debugging. */
|
|
|
|
setup_display();
|
|
|
|
}
|
|
|
|
|
2018-09-28 21:12:59 +01:00
|
|
|
/* Say hello. */
|
2019-07-06 20:58:01 +01:00
|
|
|
print(SCREEN_LOG_LEVEL_DEBUG | SCREEN_LOG_LEVEL_NO_PREFIX, "Welcome to Atmosph\xe8re Fus\xe9" "e!\n");
|
2019-02-20 21:12:15 +00:00
|
|
|
print(SCREEN_LOG_LEVEL_DEBUG, "Using color linear framebuffer at 0x%p!\n", g_framebuffer);
|
2018-09-28 21:12:59 +01:00
|
|
|
|
2018-04-07 22:43:54 +01:00
|
|
|
/* Load the loader payload into DRAM. */
|
2018-05-07 22:32:45 +01:00
|
|
|
load_stage2(bct0);
|
|
|
|
|
|
|
|
/* Setup argument data. */
|
|
|
|
stage2_path = stage2_get_program_path();
|
|
|
|
strcpy(g_chainloader_arg_data, stage2_path);
|
2018-05-21 13:18:03 +01:00
|
|
|
stage2_args = (stage2_args_t *)(g_chainloader_arg_data + strlen(stage2_path) + 1); /* May be unaligned. */
|
|
|
|
memcpy(&stage2_args->version, &stage2_version, 4);
|
2018-12-04 11:54:25 +00:00
|
|
|
memcpy(&stage2_args->log_level, &log_level, sizeof(log_level));
|
2018-05-21 02:42:05 +01:00
|
|
|
strcpy(stage2_args->bct0, bct0);
|
|
|
|
g_chainloader_argc = 2;
|
2019-01-26 07:50:50 +00:00
|
|
|
|
2019-07-06 20:58:01 +01:00
|
|
|
/* Terminate the boot environment. */
|
2018-05-08 15:51:43 +01:00
|
|
|
cleanup_env();
|
2019-07-06 20:58:01 +01:00
|
|
|
|
|
|
|
if (log_level != SCREEN_LOG_LEVEL_NONE) {
|
|
|
|
/* Wait a while for debugging. */
|
|
|
|
mdelay(1000);
|
|
|
|
|
|
|
|
/* Terminate the display for debugging. */
|
|
|
|
cleanup_display();
|
|
|
|
}
|
2018-05-08 15:51:43 +01:00
|
|
|
|
|
|
|
/* Finally, after the cleanup routines (__libc_fini_array, etc.) are called, jump to Stage2. */
|
|
|
|
__program_exit_callback = exit_callback;
|
2018-04-07 22:43:54 +01:00
|
|
|
return 0;
|
2019-04-07 19:02:01 +01:00
|
|
|
}
|