[stage2] Remove printk, introduce UTF-8 console stdio

This commit is contained in:
TuxSH 2018-05-05 23:55:40 +02:00
parent 02217bde55
commit 8648cac77b
15 changed files with 246 additions and 149 deletions

View file

@ -0,0 +1,132 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <sys/iosupport.h>
#include "console.h"
#include "display/video_fb.h"
static ssize_t console_write(struct _reent *r, void *fd, const char *ptr, size_t len);
static const devoptab_t dotab_stdout = {
.name = "con",
.write_r = console_write,
};
/* https://github.com/switchbrew/libnx/blob/master/nx/source/runtime/util/utf/decode_utf8.c */
ssize_t decode_utf8(uint32_t *out, const uint8_t *in) {
uint8_t code1, code2, code3, code4;
code1 = *in++;
if(code1 < 0x80) {
/* 1-byte sequence */
*out = code1;
return 1;
} else if(code1 < 0xC2) {
return -1;
} else if(code1 < 0xE0) {
/* 2-byte sequence */
code2 = *in++;
if((code2 & 0xC0) != 0x80) {
return -1;
}
*out = (code1 << 6) + code2 - 0x3080;
return 2;
} else if(code1 < 0xF0) {
/* 3-byte sequence */
code2 = *in++;
if((code2 & 0xC0) != 0x80) {
return -1;
}
if(code1 == 0xE0 && code2 < 0xA0) {
return -1;
}
code3 = *in++;
if((code3 & 0xC0) != 0x80) {
return -1;
}
*out = (code1 << 12) + (code2 << 6) + code3 - 0xE2080;
return 3;
} else if(code1 < 0xF5) {
/* 4-byte sequence */
code2 = *in++;
if((code2 & 0xC0) != 0x80) {
return -1;
}
if(code1 == 0xF0 && code2 < 0x90) {
return -1;
}
if(code1 == 0xF4 && code2 >= 0x90) {
return -1;
}
code3 = *in++;
if((code3 & 0xC0) != 0x80) {
return -1;
}
code4 = *in++;
if((code4 & 0xC0) != 0x80) {
return -1;
}
*out = (code1 << 18) + (code2 << 12) + (code3 << 6) + code4 - 0x3C82080;
return 4;
}
return -1;
}
static ssize_t console_write(struct _reent *r, void *fd, const char *ptr, size_t len) {
size_t i = 0;
while (i < len) {
uint32_t chr;
ssize_t n = decode_utf8(&chr, (uint8_t *)(ptr + i));
if (n == -1) {
break;
} else {
i += (size_t)n;
video_putc(chr > 0xFF ? '?' : (char)chr);
}
}
return i;
}
static bool g_console_created = false;
static int console_create(void) {
if (g_console_created) {
errno = EEXIST;
return -1;
}
devoptab_list[STD_OUT] = &dotab_stdout;
devoptab_list[STD_ERR] = &dotab_stdout;
setvbuf(stdout, NULL , _IONBF, 0);
setvbuf(stderr, NULL , _IONBF, 0);
g_console_created = true;
return 0;
}
int console_init(void *fb) {
if (video_init(fb) == -1) {
errno = EIO;
return -1;
}
return console_create();
}
int console_resume(void *fb, int row, int col) {
video_resume(fb, row, col); if(false){// if (video_resume(fb, row, col) == -1) {
errno = EIO;
return -1;
}
return console_create();
}

View file

@ -0,0 +1,7 @@
#ifndef FUSEE_CONSOLE_H
#define FUSEE_CONSOLE_H
int console_init(void *fb);
int console_resume(void *fb, int row, int col);
#endif

View file

@ -52,6 +52,7 @@ int video_get_row(void);
int video_init(void *fb); int video_init(void *fb);
int video_resume(void *fb, int row, int col); int video_resume(void *fb, int row, int col);
void video_putc(char c);
void video_puts(const char *s); void video_puts(const char *s);
#endif /*_VIDEO_FB_H_ */ #endif /*_VIDEO_FB_H_ */

View file

@ -1,9 +1,9 @@
#include <stdio.h>
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
#include <errno.h> #include <errno.h>
#include "gpio.h" #include "gpio.h"
#include "lib/printk.h"
enum tegra_gpio_shifts { enum tegra_gpio_shifts {
GPIO_BANK_SHIFT = 5, GPIO_BANK_SHIFT = 5,

View file

@ -1,30 +0,0 @@
/**
* Kernel print functions.
*/
#include <stdio.h>
#include "printk.h"
#include "../display/video_fb.h"
/**
* Temporary stand-in main printk.
*
* TODO: This should print via UART, console framebuffer, and to a ring for
* consumption by Horizon
*/
void printk(char *fmt, ...)
{
va_list list;
va_start(list, fmt);
vprintk(fmt, list);
va_end(list);
}
void vprintk(char *fmt, va_list args)
{
char buf[512];
vsnprintf(buf, sizeof(buf), fmt, args);
video_puts(buf);
}

View file

@ -1,9 +0,0 @@
#ifndef __PRINTK_H__
#define __PRINTK_H__
#include <stdarg.h>
void printk(char *fmt, ...);
void vprintk(char *fmt, va_list args);
#endif

View file

@ -4,7 +4,6 @@
#include "loader.h" #include "loader.h"
#include "sd_utils.h" #include "sd_utils.h"
#include "stage2.h" #include "stage2.h"
#include "lib/printk.h"
#include "lib/ini.h" #include "lib/ini.h"
const char *g_bct0 = NULL; const char *g_bct0 = NULL;
@ -52,29 +51,29 @@ void load_list_entry(const char *key) {
load_file_t load_file_ctx = {0}; load_file_t load_file_ctx = {0};
load_file_ctx.key = key; load_file_ctx.key = key;
printk("Loading %s\n", key); printf("Loading %s\n", key);
if (ini_parse_string(get_loader_ctx()->bct0, loadlist_entry_ini_handler, &load_file_ctx) < 0) { if (ini_parse_string(get_loader_ctx()->bct0, loadlist_entry_ini_handler, &load_file_ctx) < 0) {
printk("Error: Failed to parse BCT.ini!\n"); printf("Error: Failed to parse BCT.ini!\n");
generic_panic(); generic_panic();
} }
if (load_file_ctx.load_address == 0 || load_file_ctx.path[0] == '\x00') { if (load_file_ctx.load_address == 0 || load_file_ctx.path[0] == '\x00') {
printk("Error: Failed to determine where to load %s!\n", key); printf("Error: Failed to determine where to load %s!\n", key);
generic_panic(); generic_panic();
} }
printk("Loading %s from %s to 0x%08x\n", key, load_file_ctx.path, load_file_ctx.load_address); printf("Loading %s from %s to 0x%08x\n", key, load_file_ctx.path, load_file_ctx.load_address);
if (!validate_load_address(load_file_ctx.load_address)) { if (!validate_load_address(load_file_ctx.load_address)) {
printk("Error: Load address 0x%08x is invalid!\n"); printf("Error: Load address 0x%08x is invalid!\n", load_file_ctx.load_address);
generic_panic(); generic_panic();
} }
/* Read file off of SD. */ /* Read file off of SD. */
load_file_ctx.load_size = read_sd_file((void *)load_file_ctx.load_address, LOADER_FILESIZE_MAX, load_file_ctx.path); load_file_ctx.load_size = read_sd_file((void *)load_file_ctx.load_address, LOADER_FILESIZE_MAX, load_file_ctx.path);
if (load_file_ctx.load_size == 0) { if (load_file_ctx.load_size == 0) {
printk("Error: Failed to read %s!\n", load_file_ctx.path); printf("Error: Failed to read %s!\n", load_file_ctx.path);
generic_panic(); generic_panic();
} }
@ -91,7 +90,7 @@ void load_list_entry(const char *key) {
} }
void parse_loadlist(const char *ll) { void parse_loadlist(const char *ll) {
printk("Parsing load list: %s\n", ll); printf("Parsing load list: %s\n", ll);
char load_list[0x200] = {0}; char load_list[0x200] = {0};
strncpy(load_list, ll, 0x200); strncpy(load_list, ll, 0x200);
@ -112,7 +111,7 @@ void parse_loadlist(const char *ll) {
/* Skip to the next delimiter. */ /* Skip to the next delimiter. */
for (; *p == ' ' || *p == '\t' || *p == '\x00'; p++) { } for (; *p == ' ' || *p == '\t' || *p == '\x00'; p++) { }
if (*p != '|') { if (*p != '|') {
printk("Error: Load list is malformed!\n"); printf("Error: Load list is malformed!\n");
generic_panic(); generic_panic();
} else { } else {
/* Skip to the next entry. */ /* Skip to the next entry. */
@ -158,7 +157,7 @@ void load_payload(const char *bct0) {
ctx->bct0 = bct0; ctx->bct0 = bct0;
if (ini_parse_string(ctx->bct0, loadlist_ini_handler, ctx) < 0) { if (ini_parse_string(ctx->bct0, loadlist_ini_handler, ctx) < 0) {
printk("Error: Failed to parse BCT.ini!\n"); printf("Error: Failed to parse BCT.ini!\n");
generic_panic(); generic_panic();
} }
} }

View file

@ -1,11 +1,11 @@
#include <stdio.h>
#include "utils.h" #include "utils.h"
#include "hwinit.h" #include "hwinit.h"
#include "loader.h" #include "loader.h"
#include "stage2.h" #include "stage2.h"
#include "nxboot.h" #include "nxboot.h"
#include "console.h"
#include "sd_utils.h" #include "sd_utils.h"
#include "lib/printk.h"
#include "display/video_fb.h"
#include "fs_dev.h" #include "fs_dev.h"
/* TODO: Add a #define for this size, somewhere. Also, primary can only actually load 0x7000. */ /* TODO: Add a #define for this size, somewhere. Also, primary can only actually load 0x7000. */
@ -40,16 +40,16 @@ int main(int argc, void **argv) {
/* TODO: What other hardware init should we do here? */ /* TODO: What other hardware init should we do here? */
/* Setup LFB. */ /* Setup console/stdout. */
video_resume(args.lfb, args.console_row, args.console_col); console_resume(args.lfb, args.console_row, args.console_col);
printk("Welcome to Atmosph\xe8re Fus\xe9" "e Stage 2!\n"); printf(u8"Welcome to Atmosphère Fusée Stage 2!\n");
printk("Stage 2 executing from: %s\n", (const char *)argv[STAGE2_ARGV_PROGRAM_PATH]); printf("Stage 2 executing from: %s\n", (const char *)argv[STAGE2_ARGV_PROGRAM_PATH]);
/* This will load all remaining binaries off of the SD. */ /* This will load all remaining binaries off of the SD. */
load_payload(g_bct0); load_payload(g_bct0);
printk("Loaded payloads!\n"); printf("Loaded payloads!\n");
/* Unmount everything (this causes all open files to be flushed and closed) */ /* Unmount everything (this causes all open files to be flushed and closed) */
fsdev_unmount_all(); fsdev_unmount_all();

View file

@ -1,3 +1,4 @@
#include <stdio.h>
#include "utils.h" #include "utils.h"
#include "nxboot.h" #include "nxboot.h"
#include "key_derivation.h" #include "key_derivation.h"
@ -5,7 +6,6 @@
#include "loader.h" #include "loader.h"
#include "splash_screen.h" #include "splash_screen.h"
#include "exocfg.h" #include "exocfg.h"
#include "lib/printk.h"
#include "display/video_fb.h" #include "display/video_fb.h"
#include "lib/ini.h" #include "lib/ini.h"
#include "hwinit/cluster.h" #include "hwinit/cluster.h"
@ -31,12 +31,12 @@ void nxboot_configure_exosphere(void) {
exo_cfg.target_firmware = EXOSPHERE_TARGET_FIRMWARE_MAX; exo_cfg.target_firmware = EXOSPHERE_TARGET_FIRMWARE_MAX;
if (ini_parse_string(get_loader_ctx()->bct0, exosphere_ini_handler, &exo_cfg) < 0) { if (ini_parse_string(get_loader_ctx()->bct0, exosphere_ini_handler, &exo_cfg) < 0) {
printk("Error: Failed to parse BCT.ini!\n"); printf("Error: Failed to parse BCT.ini!\n");
generic_panic(); generic_panic();
} }
if (exo_cfg.target_firmware < EXOSPHERE_TARGET_FIRMWARE_MIN || exo_cfg.target_firmware > EXOSPHERE_TARGET_FIRMWARE_MAX) { if (exo_cfg.target_firmware < EXOSPHERE_TARGET_FIRMWARE_MIN || exo_cfg.target_firmware > EXOSPHERE_TARGET_FIRMWARE_MAX) {
printk("Error: Invalid Exosphere target firmware!\n"); printf("Error: Invalid Exosphere target firmware!\n");
generic_panic(); generic_panic();
} }

View file

@ -1,10 +1,10 @@
#include <stdio.h>
#include "utils.h" #include "utils.h"
#include "masterkey.h" #include "masterkey.h"
#include "stratosphere.h" #include "stratosphere.h"
#include "package2.h" #include "package2.h"
#include "kip.h" #include "kip.h"
#include "se.h" #include "se.h"
#include "lib/printk.h"
/* Stage 2 executes from DRAM, so we have tons of space. */ /* Stage 2 executes from DRAM, so we have tons of space. */
/* This *greatly* simplifies logic. */ /* This *greatly* simplifies logic. */
@ -200,7 +200,7 @@ void package2_decrypt(void *package2_address) {
void package2_add_thermosphere_section(void) { void package2_add_thermosphere_section(void) {
if (g_patched_package2_header->metadata.section_sizes[PACKAGE2_SECTION_UNUSED] != 0) { if (g_patched_package2_header->metadata.section_sizes[PACKAGE2_SECTION_UNUSED] != 0) {
printk("Error: Package2 has no unused section for Thermosph\xe8re!\n"); printf(u8"Error: Package2 has no unused section for Thermosphère!\n");
generic_panic(); generic_panic();
} }
@ -233,7 +233,7 @@ void package2_fixup_header_and_section_hashes(void) {
size_t size = (size_t)g_patched_package2_header->metadata.section_sizes[section]; size_t size = (size_t)g_patched_package2_header->metadata.section_sizes[section];
if (sizeof(package2_header_t) + cur_section_offset + size > PACKAGE2_SIZE_MAX) { if (sizeof(package2_header_t) + cur_section_offset + size > PACKAGE2_SIZE_MAX) {
printk("Error: Patched Package2 is too big!\n"); printf("Error: Patched Package2 is too big!\n");
generic_panic(); generic_panic();
} }

View file

@ -2,8 +2,6 @@
#include "sd_utils.h" #include "sd_utils.h"
#include "hwinit.h" #include "hwinit.h"
#include "sdmmc.h" #include "sdmmc.h"
#include "lib/printk.h"
#include "lib/fatfs/ff.h"
/* This is used by diskio.h. */ /* This is used by diskio.h. */
struct mmc sd_mmc; struct mmc sd_mmc;
@ -23,7 +21,7 @@ int initialize_sd(void) {
} }
mc_enable_ahb_redirect(); mc_enable_ahb_redirect();
if (sdmmc_init(&sd_mmc, SWITCH_MICROSD) == 0) { if (sdmmc_init(&sd_mmc, SWITCH_MICROSD) == 0) {
printk("Initialized SD card!\n"); printf("Initialized SD card!\n");
initialized_sd = 1; initialized_sd = 1;
} }
return initialized_sd; return initialized_sd;

View file

@ -2,7 +2,8 @@
* Fusée SD/MMC driver for the Switch * Fusée SD/MMC driver for the Switch
* ~ktemkin * ~ktemkin
*/ */
#include <stdio.h>
#include <stdarg.h>
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
#include <errno.h> #include <errno.h>
@ -16,7 +17,6 @@
#include "supplies.h" #include "supplies.h"
#include "pmc.h" #include "pmc.h"
#include "pad_control.h" #include "pad_control.h"
#include "lib/printk.h"
#define TEGRA_SDMMC_BASE (0x700B0000) #define TEGRA_SDMMC_BASE (0x700B0000)
#define TEGRA_SDMMC_SIZE (0x200) #define TEGRA_SDMMC_SIZE (0x200)
@ -427,9 +427,9 @@ static void mmc_vprint(struct mmc *mmc, char *fmt, int required_loglevel, va_lis
if (sdmmc_loglevel < required_loglevel) if (sdmmc_loglevel < required_loglevel)
return; return;
printk("%s: ", mmc->name); printf("%s: ", mmc->name);
vprintk(fmt, list); vprintf(fmt, list);
printk("\n"); printf("\n");
} }
@ -732,7 +732,7 @@ static int sdmmc_set_up_clocking_parameters(struct mmc *mmc)
break; break;
default: default:
printk("ERROR: initialization not yet writen for SDMMC%d", mmc->controller); printf("ERROR: initialization not yet writen for SDMMC%d", mmc->controller);
return ENODEV; return ENODEV;
} }
@ -1480,7 +1480,7 @@ static uint32_t sdmmc_extract_csd_bits(uint32_t *csd, int start, int width)
// Sanity check our span. // Sanity check our span.
if ((start + width) > 128) { if ((start + width) > 128) {
printk("MMC ERROR: invalid CSD slice!\n"); printf("MMC ERROR: invalid CSD slice!\n");
return 0xFFFFFFFF; return 0xFFFFFFFF;
} }
@ -2205,7 +2205,7 @@ static void sdmmc_initialize_defaults(struct mmc *mmc)
break; break;
default: default:
printk("ERROR: initialization not yet writen for SDMMC%d", mmc->controller); printf("ERROR: initialization not yet writen for SDMMC%d", mmc->controller);
break; break;
} }
} }

View file

@ -1,15 +1,15 @@
#include <stdio.h>
#include "utils.h" #include "utils.h"
#include "timers.h" #include "timers.h"
#include "splash_screen.h" #include "splash_screen.h"
#include "sd_utils.h" #include "sd_utils.h"
#include "lib/printk.h"
#include "display/video_fb.h" #include "display/video_fb.h"
void display_splash_screen_bmp(const char *custom_splash_path) { void display_splash_screen_bmp(const char *custom_splash_path) {
uint8_t *splash_screen = g_default_splash_screen; uint8_t *splash_screen = g_default_splash_screen;
if (custom_splash_path != NULL && custom_splash_path[0] != '\x00') { if (custom_splash_path != NULL && custom_splash_path[0] != '\x00') {
if (!read_sd_file(splash_screen, sizeof(g_default_splash_screen), custom_splash_path)) { if (!read_sd_file(splash_screen, sizeof(g_default_splash_screen), custom_splash_path)) {
printk("Error: Failed to read custom splash screen from %s!\n", custom_splash_path); printf("Error: Failed to read custom splash screen from %s!\n", custom_splash_path);
generic_panic(); generic_panic();
} }
} }

View file

@ -3,7 +3,6 @@
#include "package2.h" #include "package2.h"
#include "stratosphere.h" #include "stratosphere.h"
#include "sd_utils.h" #include "sd_utils.h"
#include "lib/printk.h"
unsigned char g_stratosphere_ini1[PACKAGE2_SIZE_MAX]; unsigned char g_stratosphere_ini1[PACKAGE2_SIZE_MAX];
static bool g_initialized_stratosphere_ini1 = false; static bool g_initialized_stratosphere_ini1 = false;
@ -32,7 +31,7 @@ size_t stratosphere_merge_inis(void *dst, ini1_header_t **inis, unsigned int num
/* Validate all ini headers. */ /* Validate all ini headers. */
for (unsigned int i = 0; i < num_inis; i++) { for (unsigned int i = 0; i < num_inis; i++) {
if (inis[i] == NULL || inis[i]->magic != MAGIC_INI1 || inis[i]->num_processes > INI1_MAX_KIPS) { if (inis[i] == NULL || inis[i]->magic != MAGIC_INI1 || inis[i]->num_processes > INI1_MAX_KIPS) {
printk("Error: INI1s[%d] section appears to not contain an INI1!\n", i); printf("Error: INI1s[%d] section appears to not contain an INI1!\n", i);
generic_panic(); generic_panic();
} }
} }
@ -54,7 +53,7 @@ size_t stratosphere_merge_inis(void *dst, ini1_header_t **inis, unsigned int num
for (unsigned int p = 0; p < inis[i]->num_processes; p++) { for (unsigned int p = 0; p < inis[i]->num_processes; p++) {
kip1_header_t *current_kip = (kip1_header_t *)(inis[i]->kip_data + offset); kip1_header_t *current_kip = (kip1_header_t *)(inis[i]->kip_data + offset);
if (current_kip->magic != MAGIC_KIP1) { if (current_kip->magic != MAGIC_KIP1) {
printk("Error: INI1s[%d][%d] appears not to be a KIP1!\n", i, p); printf("Error: INI1s[%d][%d] appears not to be a KIP1!\n", i, p);
generic_panic(); generic_panic();
} }
@ -71,16 +70,16 @@ size_t stratosphere_merge_inis(void *dst, ini1_header_t **inis, unsigned int num
} }
/* TODO: What folder should these be read out of? */ /* TODO: What folder should these be read out of? */
snprintf(sd_path, sizeof(sd_path), "atmosph\xe8re/titles/%016llx/%016llx.kip", current_kip->title_id, current_kip->title_id); snprintf(sd_path, sizeof(sd_path), "atmosphere/titles/%016llx/%016llx.kip", current_kip->title_id, current_kip->title_id);
/* Try to load an override KIP from SD, if possible. */ /* Try to load an override KIP from SD, if possible. */
if (read_sd_file(current_dst_kip, remaining_size, sd_path)) { if (read_sd_file(current_dst_kip, remaining_size, sd_path)) {
kip1_header_t *sd_kip = (kip1_header_t *)(current_dst_kip); kip1_header_t *sd_kip = (kip1_header_t *)(current_dst_kip);
if (sd_kip->magic != MAGIC_KIP1) { if (sd_kip->magic != MAGIC_KIP1) {
printk("Error: %s is not a KIP1?\n", sd_path); printf("Error: %s is not a KIP1?\n", sd_path);
generic_panic(); generic_panic();
} else if (sd_kip->title_id != current_kip->title_id) { } else if (sd_kip->title_id != current_kip->title_id) {
printk("Error: %s has wrong Title ID!\n", sd_path); printf("Error: %s has wrong Title ID!\n", sd_path);
generic_panic(); generic_panic();
} }
uint64_t sd_kip_size = kip1_get_size_from_header(sd_kip); uint64_t sd_kip_size = kip1_get_size_from_header(sd_kip);
@ -89,7 +88,7 @@ size_t stratosphere_merge_inis(void *dst, ini1_header_t **inis, unsigned int num
} else { } else {
uint64_t current_kip_size = kip1_get_size_from_header(current_kip); uint64_t current_kip_size = kip1_get_size_from_header(current_kip);
if (current_kip_size > remaining_size) { if (current_kip_size > remaining_size) {
printk("Error: Not enough space for all the KIP1s!\n"); printf("Error: Not enough space for all the KIP1s!\n");
generic_panic(); generic_panic();
} }
memcpy(current_dst_kip, current_kip, current_kip_size); memcpy(current_dst_kip, current_kip, current_kip_size);

View file

@ -3,8 +3,8 @@
* ~ktemkin * ~ktemkin
*/ */
#include <stdio.h>
#include "supplies.h" #include "supplies.h"
#include "lib/printk.h"
// FIXME: replace hwinit with our own code // FIXME: replace hwinit with our own code
#include "hwinit/max7762x.h" #include "hwinit/max7762x.h"
@ -23,7 +23,7 @@ void supply_enable(enum switch_power_supply supply)
return; return;
default: default:
printk("ERROR: could not enable unknown supply %d!\n", supply); printf("ERROR: could not enable unknown supply %d!\n", supply);
return; return;
} }
} }