1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-11-06 04:01:44 +00:00

Add custom panic driver

This commit is contained in:
TuxSH 2018-05-20 14:11:46 +02:00
parent c820375a86
commit ec7e0d923d
14 changed files with 298 additions and 57 deletions

View file

@ -1,4 +1,5 @@
#include "utils.h" #include "utils.h"
#include "panic.h"
#include "hwinit.h" #include "hwinit.h"
#include "fuse.h" #include "fuse.h"
#include "se.h" #include "se.h"
@ -67,6 +68,9 @@ static void setup_env(void) {
/* TODO: What can be stripped out to make this minimal? */ /* TODO: What can be stripped out to make this minimal? */
nx_hwinit(); nx_hwinit();
/* Check for panics. */
check_and_display_panic();
/* Try to load the SBK into the security engine, if possible. */ /* Try to load the SBK into the security engine, if possible. */
/* TODO: Should this be done later? */ /* TODO: Should this be done later? */
load_sbk(); load_sbk();

View file

@ -0,0 +1,82 @@
#include "panic.h"
#include "pmc.h"
#include "fuse.h"
#include "utils.h"
#include "hwinit.h"
static uint32_t g_panic_code = 0;
void check_and_display_panic(void) {
/* We also handle our own panics. */
/* In the case of our own panics, we assume that the display has already been initialized. */
bool has_panic = APBDEV_PMC_RST_STATUS_0 != 0 || g_panic_code != 0;
uint32_t code = g_panic_code == 0 ? APBDEV_PMC_SCRATCH200_0 : g_panic_code;
has_panic = has_panic && !(APBDEV_PMC_RST_STATUS_0 != 1 && code == PANIC_CODE_SAFEMODE);
if (has_panic) {
uint32_t color;
/* Check for predefined codes: */
switch (code & MASK(20)) {
case 0x01: /* Package2 signature verification failed. */
case 0x02: /* Package2 meta verification failed. */
case 0x03: /* Package2 version check failed. */
case 0x04: /* Package2 payload verification failed. */
color = PANIC_COLOR_KERNEL;
break;
case 0x05: /* Unknown SMC. */
case 0x06: /* Unknown Abort. */
color = PANIC_COLOR_SECMON_GENERIC;
break;
case 0x07: /* Invalid CPU context. */
case 0x08: /* Invalid SE state. */
case 0x09: /* CPU is already awake (2.0.0+). */
color = PANIC_COLOR_SECMON_DEEPSLEEP;
break;
case 0x10: /* Unknown exception. */
color = PANIC_COLOR_SECMON_EXCEPTION;
break;
case 0x30: /* General bootloader error. */
case 0x31: /* Invalid DRAM ID. */
case 0x32: /* Invalid size. */
case 0x33: /* Invalid arguement. */
case 0x34: /* Bad GPT. */
case 0x35: /* Failed to boot SafeMode. */
case 0x36: /* Activity monitor fired (4.0.0+). */
color = PANIC_COLOR_BOOTLOADER_GENERIC;
break;
case 0x40: /* Kernel panic. */
color = PANIC_COLOR_KERNEL;
break;
default:
color = code >> 20;
color |= color << 4;
break;
}
if (g_panic_code == 0) {
display_init();
}
display_color_screen(color);
wait_for_button_and_pmc_reboot();
} else {
g_panic_code = 0;
APBDEV_PMC_SCRATCH200_0 = 0;
}
}
__attribute__ ((noreturn)) void panic(uint32_t code) {
/* Set panic code. */
if (g_panic_code == 0) {
g_panic_code = code;
APBDEV_PMC_SCRATCH200_0 = code;
}
fuse_disable_programming();
APBDEV_PMC_CRYPTO_OP_0 = 1; /* Disable all SE operations. */
check_and_display_panic();
while(true);
}

View file

@ -0,0 +1,18 @@
#ifndef FUSEE_PANIC_H
#define FUSEE_PANIC_H
#include <stdint.h>
#define PANIC_COLOR_KERNEL 0x0000FF
#define PANIC_COLOR_SECMON_EXCEPTION 0xFF7700
#define PANIC_COLOR_SECMON_GENERIC 0x00FFFF
#define PANIC_COLOR_SECMON_DEEPSLEEP 0xFF77FF /* 4.0+ color */
#define PANIC_COLOR_BOOTLOADER_GENERIC 0xAA00FF
#define PANIC_COLOR_BOOTLOADER_SAFEMODE 0xFFFFAA /* Removed */
#define PANIC_CODE_SAFEMODE 0x00000020
void check_and_display_panic(void);
__attribute__ ((noreturn)) void panic(uint32_t code);
#endif

View file

@ -20,6 +20,7 @@
#define APBDEV_PM_0 MAKE_REG32(PMC_BASE + 0x14) #define APBDEV_PM_0 MAKE_REG32(PMC_BASE + 0x14)
#define APBDEV_PMC_WAKE2_STATUS_0 MAKE_REG32(PMC_BASE + 0x168) #define APBDEV_PMC_WAKE2_STATUS_0 MAKE_REG32(PMC_BASE + 0x168)
#define APBDEV_PMC_RST_STATUS_0 MAKE_REG32(PMC_BASE + 0x1B4)
#define APBDEV_PMC_CNTRL2_0 MAKE_REG32(PMC_BASE + 0x440) #define APBDEV_PMC_CNTRL2_0 MAKE_REG32(PMC_BASE + 0x440)
#define APBDEV_PMC_SCRATCH43_0 MAKE_REG32(PMC_BASE + 0x22C) #define APBDEV_PMC_SCRATCH43_0 MAKE_REG32(PMC_BASE + 0x22C)
@ -28,6 +29,7 @@
/** /**
* Definitions of the Tegra PMC. * Definitions of the Tegra PMC.
* NOTE: Incomplete, do not use
*/ */
struct tegra_pmc { struct tegra_pmc {
uint32_t cntrl; uint32_t cntrl;
@ -306,11 +308,11 @@ enum tegra_pmc_masks {
/** /**
* Utility function that grabs the Tegra pinmux registers. * Utility function that grabs the Tegra PMC registers.
*/ */
static inline struct tegra_pmc *pmc_get_regs(void) static inline volatile struct tegra_pmc *pmc_get_regs(void)
{ {
return (struct tegra_pmc *)0x7000e400; return (volatile struct tegra_pmc *)0x7000E400;
} }

View file

@ -2440,8 +2440,8 @@ static void sdmmc_apply_card_type(struct mmc *mmc, enum sdmmc_card_type type)
// Switch-cart protocol cards // Switch-cart protocol cards
case MMC_CARD_CART: case MMC_CARD_CART:
printk("BUG: trying to use an impossible code path!\n"); printk("BUG: trying to use an impossible code path, hanging!\n");
panic(0); while(true);
} }
} }

View file

@ -3,41 +3,49 @@
#include "se.h" #include "se.h"
#include "fuse.h" #include "fuse.h"
#include "pmc.h" #include "pmc.h"
#include "panic_color.h"
#include "timers.h" #include "timers.h"
#include "hwinit/btn.h" #include "hwinit/btn.h"
__attribute__((noreturn)) void watchdog_reboot(void) {
__attribute__ ((noreturn)) void panic(uint32_t code) { volatile watchdog_timers_t *wdt = GET_WDT(4);
/* Set Panic Code for NX_BOOTLOADER. */ wdt->PATTERN = WDT_REBOOT_PATTERN;
if (APBDEV_PMC_SCRATCH200_0 == 0) { wdt->COMMAND = 2; /* Disable Counter. */
APBDEV_PMC_SCRATCH200_0 = code; GET_WDT_REBOOT_CFG_REG(4) = 0xC0000000;
wdt->CONFIG = 0x8019; /* Full System Reset after Fourth Counter expires, using TIMER(9). */
wdt->COMMAND = 1; /* Enable Counter. */
while (true) {
/* Wait for reboot. */
} }
}
/* TODO: Custom Panic Driver, which displays to screen without rebooting. */ __attribute__((noreturn)) void pmc_reboot(uint32_t scratch0) {
/* For now, just use NX BOOTLOADER's panic. */ APBDEV_PMC_SCRATCH0_0 = scratch0;
fuse_disable_programming();
APBDEV_PMC_CRYPTO_OP_0 = 1; /* Disable all SE operations. */
/* FIXME: clean up and use this instead of replacing things */
while(btn_read() != BTN_POWER);
/* Ensure we boot back into RCM, for development. */
APBDEV_PMC_SCRATCH0_0 = (1 << 1);
/* Reset the processor. */ /* Reset the processor. */
APBDEV_PMC_CONTROL = (1 << 4); APBDEV_PMC_CONTROL = BIT(4);
while(1);
while (true) {
/* Wait for reboot. */
}
}
__attribute__((noreturn)) void wait_for_button_and_pmc_reboot(void) {
uint32_t button;
while (true) {
button = btn_read();
if (button & BTN_POWER) {
/* Reboot into RCM. */
pmc_reboot(BIT(1) | 0);
} else if (button & (BTN_VOL_UP | BTN_VOL_DOWN)) {
/* Reboot normally. */
pmc_reboot(0);
}
}
} }
__attribute__ ((noreturn)) void generic_panic(void) { __attribute__ ((noreturn)) void generic_panic(void) {
panic(0xFF000006); while(true);//panic(0xFF000006);
}
__attribute__ ((noreturn)) void panic_predefined(uint32_t which) {
static const uint32_t codes[0x10] = {COLOR_0, COLOR_1, COLOR_2, COLOR_3, COLOR_4, COLOR_5, COLOR_6, COLOR_7, COLOR_8, COLOR_9, COLOR_A, COLOR_B, COLOR_C, COLOR_D, COLOR_E, COLOR_F};
panic(codes[which & 0xF]);
} }
__attribute__((noinline)) bool overlaps(uint64_t as, uint64_t ae, uint64_t bs, uint64_t be) __attribute__((noinline)) bool overlaps(uint64_t as, uint64_t ae, uint64_t bs, uint64_t be)

View file

@ -101,8 +101,11 @@ static inline bool check_32bit_address_range_in_program(uintptr_t addr, size_t s
overlaps_a(start, end, __start__, __end__); overlaps_a(start, end, __start__, __end__);
} }
void panic(uint32_t code); __attribute__((noreturn)) void watchdog_reboot(void);
__attribute__((noreturn)) void pmc_reboot(uint32_t scratch0);
__attribute__((noreturn)) void wait_for_button_and_pmc_reboot(void);
void generic_panic(void); void generic_panic(void);
void panic_predefined(uint32_t which);
#endif #endif

View file

@ -4,6 +4,7 @@
#include <errno.h> #include <errno.h>
#include <malloc.h> #include <malloc.h>
#include "utils.h" #include "utils.h"
#include "panic.h"
#include "loader.h" #include "loader.h"
#include "chainloader.h" #include "chainloader.h"
#include "stage2.h" #include "stage2.h"
@ -20,6 +21,9 @@ static stage2_args_t *g_stage2_args;
static bool g_do_nxboot; static bool g_do_nxboot;
static void setup_env(void) { static void setup_env(void) {
/* Check for panics. */
check_and_display_panic();
/* Set the console up. */ /* Set the console up. */
if (console_init() == -1) { if (console_init() == -1) {
generic_panic(); generic_panic();

View file

@ -0,0 +1,82 @@
#include "panic.h"
#include "pmc.h"
#include "fuse.h"
#include "utils.h"
#include "hwinit.h"
static uint32_t g_panic_code = 0;
void check_and_display_panic(void) {
/* We also handle our own panics. */
/* In the case of our own panics, we assume that the display has already been initialized. */
bool has_panic = APBDEV_PMC_RST_STATUS_0 != 0 || g_panic_code != 0;
uint32_t code = g_panic_code == 0 ? APBDEV_PMC_SCRATCH200_0 : g_panic_code;
has_panic = has_panic && !(APBDEV_PMC_RST_STATUS_0 != 1 && code == PANIC_CODE_SAFEMODE);
if (has_panic) {
uint32_t color;
/* Check for predefined codes: */
switch (code & MASK(20)) {
case 0x01: /* Package2 signature verification failed. */
case 0x02: /* Package2 meta verification failed. */
case 0x03: /* Package2 version check failed. */
case 0x04: /* Package2 payload verification failed. */
color = PANIC_COLOR_KERNEL;
break;
case 0x05: /* Unknown SMC. */
case 0x06: /* Unknown Abort. */
color = PANIC_COLOR_SECMON_GENERIC;
break;
case 0x07: /* Invalid CPU context. */
case 0x08: /* Invalid SE state. */
case 0x09: /* CPU is already awake (2.0.0+). */
color = PANIC_COLOR_SECMON_DEEPSLEEP;
break;
case 0x10: /* Unknown exception. */
color = PANIC_COLOR_SECMON_EXCEPTION;
break;
case 0x30: /* General bootloader error. */
case 0x31: /* Invalid DRAM ID. */
case 0x32: /* Invalid size. */
case 0x33: /* Invalid arguement. */
case 0x34: /* Bad GPT. */
case 0x35: /* Failed to boot SafeMode. */
case 0x36: /* Activity monitor fired (4.0.0+). */
color = PANIC_COLOR_BOOTLOADER_GENERIC;
break;
case 0x40: /* Kernel panic. */
color = PANIC_COLOR_KERNEL;
break;
default:
color = code >> 20;
color |= color << 4;
break;
}
if (g_panic_code == 0) {
display_init();
}
display_color_screen(color);
wait_for_button_and_pmc_reboot();
} else {
g_panic_code = 0;
APBDEV_PMC_SCRATCH200_0 = 0;
}
}
__attribute__ ((noreturn)) void panic(uint32_t code) {
/* Set panic code. */
if (g_panic_code == 0) {
g_panic_code = code;
APBDEV_PMC_SCRATCH200_0 = code;
}
fuse_disable_programming();
APBDEV_PMC_CRYPTO_OP_0 = 1; /* Disable all SE operations. */
check_and_display_panic();
while(true);
}

View file

@ -0,0 +1,18 @@
#ifndef FUSEE_PANIC_H
#define FUSEE_PANIC_H
#include <stdint.h>
#define PANIC_COLOR_KERNEL 0x0000FF
#define PANIC_COLOR_SECMON_EXCEPTION 0xFF7700
#define PANIC_COLOR_SECMON_GENERIC 0x00FFFF
#define PANIC_COLOR_SECMON_DEEPSLEEP 0xFF77FF /* 4.0+ color */
#define PANIC_COLOR_BOOTLOADER_GENERIC 0xAA00FF
#define PANIC_COLOR_BOOTLOADER_SAFEMODE 0xFFFFAA /* Removed */
#define PANIC_CODE_SAFEMODE 0x00000020
void check_and_display_panic(void);
__attribute__ ((noreturn)) void panic(uint32_t code);
#endif

View file

@ -20,6 +20,7 @@
#define APBDEV_PM_0 MAKE_REG32(PMC_BASE + 0x14) #define APBDEV_PM_0 MAKE_REG32(PMC_BASE + 0x14)
#define APBDEV_PMC_WAKE2_STATUS_0 MAKE_REG32(PMC_BASE + 0x168) #define APBDEV_PMC_WAKE2_STATUS_0 MAKE_REG32(PMC_BASE + 0x168)
#define APBDEV_PMC_RST_STATUS_0 MAKE_REG32(PMC_BASE + 0x1B4)
#define APBDEV_PMC_CNTRL2_0 MAKE_REG32(PMC_BASE + 0x440) #define APBDEV_PMC_CNTRL2_0 MAKE_REG32(PMC_BASE + 0x440)
#define APBDEV_PMC_SCRATCH43_0 MAKE_REG32(PMC_BASE + 0x22C) #define APBDEV_PMC_SCRATCH43_0 MAKE_REG32(PMC_BASE + 0x22C)
@ -28,6 +29,7 @@
/** /**
* Definitions of the Tegra PMC. * Definitions of the Tegra PMC.
* NOTE: Incomplete, do not use
*/ */
struct tegra_pmc { struct tegra_pmc {
uint32_t cntrl; uint32_t cntrl;
@ -306,11 +308,11 @@ enum tegra_pmc_masks {
/** /**
* Utility function that grabs the Tegra pinmux registers. * Utility function that grabs the Tegra PMC registers.
*/ */
static inline struct tegra_pmc *pmc_get_regs(void) static inline volatile struct tegra_pmc *pmc_get_regs(void)
{ {
return (struct tegra_pmc *)0x7000e400; return (volatile struct tegra_pmc *)0x7000E400;
} }

View file

@ -2440,8 +2440,8 @@ static void sdmmc_apply_card_type(struct mmc *mmc, enum sdmmc_card_type type)
// Switch-cart protocol cards // Switch-cart protocol cards
case MMC_CARD_CART: case MMC_CARD_CART:
printk("BUG: trying to use an impossible code path!\n"); printk("BUG: trying to use an impossible code path, hanging!\n");
panic(0); while(true);
} }
} }

View file

@ -3,35 +3,51 @@
#include "se.h" #include "se.h"
#include "fuse.h" #include "fuse.h"
#include "pmc.h" #include "pmc.h"
#include "panic_color.h"
#include "timers.h" #include "timers.h"
#include "hwinit/btn.h"
#include <stdio.h> #include <stdio.h>
#include <inttypes.h> #include <inttypes.h>
__attribute__ ((noreturn)) void panic(uint32_t code) { __attribute__((noreturn)) void watchdog_reboot(void) {
/* Set Panic Code for NX_BOOTLOADER. */ volatile watchdog_timers_t *wdt = GET_WDT(4);
if (APBDEV_PMC_SCRATCH200_0 == 0) { wdt->PATTERN = WDT_REBOOT_PATTERN;
APBDEV_PMC_SCRATCH200_0 = code; wdt->COMMAND = 2; /* Disable Counter. */
GET_WDT_REBOOT_CFG_REG(4) = 0xC0000000;
wdt->CONFIG = 0x8019; /* Full System Reset after Fourth Counter expires, using TIMER(9). */
wdt->COMMAND = 1; /* Enable Counter. */
while (true) {
/* Wait for reboot. */
} }
}
/* TODO: Custom Panic Driver, which displays to screen without rebooting. */ __attribute__((noreturn)) void pmc_reboot(uint32_t scratch0) {
/* For now, just use NX BOOTLOADER's panic. */ APBDEV_PMC_SCRATCH0_0 = scratch0;
fuse_disable_programming();
APBDEV_PMC_CRYPTO_OP_0 = 1; /* Disable all SE operations. */ /* Reset the processor. */
/* TODO: watchdog_reboot(); */ APBDEV_PMC_CONTROL = BIT(4);
while (1) { } while (true) {
/* Wait for reboot. */
}
}
__attribute__((noreturn)) void wait_for_button_and_pmc_reboot(void) {
uint32_t button;
while (true) {
button = btn_read();
if (button & BTN_POWER) {
/* Reboot into RCM. */
pmc_reboot(BIT(1) | 0);
} else if (button & (BTN_VOL_UP | BTN_VOL_DOWN)) {
/* Reboot normally. */
pmc_reboot(0);
}
}
} }
__attribute__ ((noreturn)) void generic_panic(void) { __attribute__ ((noreturn)) void generic_panic(void) {
panic(0xFF000006); while(true);//panic(0xFF000006);
} }
__attribute__ ((noreturn)) void panic_predefined(uint32_t which) {
static const uint32_t codes[0x10] = {COLOR_0, COLOR_1, COLOR_2, COLOR_3, COLOR_4, COLOR_5, COLOR_6, COLOR_7, COLOR_8, COLOR_9, COLOR_A, COLOR_B, COLOR_C, COLOR_D, COLOR_E, COLOR_F};
panic(codes[which & 0xF]);
}
__attribute__((noinline)) bool overlaps(uint64_t as, uint64_t ae, uint64_t bs, uint64_t be) __attribute__((noinline)) bool overlaps(uint64_t as, uint64_t ae, uint64_t bs, uint64_t be)
{ {
if(as <= bs && bs <= ae) if(as <= bs && bs <= ae)

View file

@ -104,8 +104,10 @@ static inline bool check_32bit_address_range_in_program(uintptr_t addr, size_t s
void hexdump(const void* data, size_t size, uintptr_t addrbase); void hexdump(const void* data, size_t size, uintptr_t addrbase);
__attribute__ ((noreturn)) void panic(uint32_t code); __attribute__((noreturn)) void watchdog_reboot(void);
__attribute__ ((noreturn)) void generic_panic(void); __attribute__((noreturn)) void pmc_reboot(uint32_t scratch0);
__attribute__ ((noreturn)) void panic_predefined(uint32_t which); __attribute__((noreturn)) void wait_for_button_and_pmc_reboot(void);
void generic_panic(void);
#endif #endif