Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,25 @@ On-Line Applications Research Corporation.
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

The QR Code generator in external/qrcodegen is copyright Project Nayuki and
is distributed under the MIT license. The source files contain the following
license notice:

Copyright (c) Project Nayuki.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
- The Software is provided "as is", without warranty of any kind, express or
implied, including but not limited to the warranties of merchantability,
fitness for a particular purpose and noninfringement. In no event shall the
authors or copyright holders be liable for any claim, damages or other
liability, whether in an action of contract, tort or otherwise, arising from,
out of or in connection with the Software or the use or other dealings in the
Software.
4 changes: 4 additions & 0 deletions external/qrcodegen/README.libogc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
QR Code generator library (C), version 1.8.0
Copyright (c) Project Nayuki; MIT License (see source file headers).
Source: https://github.com/nayuki/QR-Code-generator/tree/v1.8.0/c
The source and header are included unmodified.
1,022 changes: 1,022 additions & 0 deletions external/qrcodegen/qrcodegen.c

Large diffs are not rendered by default.

385 changes: 385 additions & 0 deletions external/qrcodegen/qrcodegen.h

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions gc/ogc/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,32 @@ u32 SYS_InitFont(sys_fontheader *font_data);
void SYS_GetFontTexture(s32 c,void **image,s32 *xpos,s32 *ypos,s32 *width);
void SYS_GetFontTexel(s32 c,void *image,s32 pos,s32 stride,s32 *width);
void SYS_ResetSystem(s32 reset,u32 reset_code,s32 force_menu);

/*! \brief Initial display for the default panic handler. */
typedef enum {
SYS_PANIC_VIEW_DETAILS, /*!< Show the text details first. */
SYS_PANIC_VIEW_QR, /*!< Show the QR code first. */
} sys_panic_view;

/*! \fn void SYS_SetPanicDefaultView(sys_panic_view view)
\brief Select the initial display in the default panic handler.

The text screen is shown first by default and is used if the QR view cannot
fit in the framebuffer. The application must have enabled video before the
panic for either view to be visible. The QR code contains the exception ID, registers,
thread address and stack return addresses. DSI exceptions also include DAR
and DSISR. Keep the matching unstripped ELF to resolve the addresses.

Press RESET briefly or Y on an initialized GameCube controller to change
screens. Hold RESET for about one second or press Z to reload. The display
alternates every 15 seconds until one of these controls is used. This gives
systems like Wii mini access to both screens. An existing reload timeout
still applies.

\param[in] view Initial panic view. The default is SYS_PANIC_VIEW_DETAILS.
\note This setting does not affect custom panic handlers.
*/
void SYS_SetPanicDefaultView(sys_panic_view view);
void SYS_RegisterResetFunc(sys_resetinfo *info);
void SYS_UnregisterResetFunc(sys_resetinfo *info);

Expand Down
5 changes: 5 additions & 0 deletions libogc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@ add_library(ogc STATIC
texconv.c
sys_report.c
default_panic.c
panic_qr.c
panic_qr_encoder.c
)

# The panic path cannot safely invoke assert's reporting and abort machinery.
set_source_files_properties(panic_qr_encoder.c PROPERTIES COMPILE_DEFINITIONS NDEBUG)

if(NINTENDO_GAMECUBE)

target_sources(ogc PRIVATE
Expand Down
223 changes: 210 additions & 13 deletions libogc/default_panic.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,34 @@ distribution.
#include <ogc/lwp_watchdog.h>
#include <ogc/machine/processor.h>
#include "console_internal.h"
#include "panic_qr.h"
#include "panic_controls.h"

#define CPU_STACK_TRACE_DEPTH 10
#define PANIC_QR_QUIET_ZONE 4
#define PANIC_QR_TOTAL_MODULES (PANIC_QR_SIZE + 2 * PANIC_QR_QUIET_ZONE)

typedef struct PPCStackFrame {
uptr next;
void* lr;
} PPCStackFrame;

typedef struct PanicFramebuffer {
unsigned width, height, stride;
} PanicFramebuffer;

static void *exception_xfb = (void*)0xC1700000; //we use a static address above ArenaHi.
static int reload_timer = -1;
static sys_panic_view panic_default_view = SYS_PANIC_VIEW_DETAILS;
static u32 panic_dar, panic_dsisr;

/* Keep the report and QR buffers off the exception stack. */
static char panic_report[536];
static u8 panic_qr[PANIC_QR_SIZE * PANIC_QR_SIZE];

extern void VIDEO_SetFramebuffer(void *);
extern void __VIClearFramebuffer(void*, u32, u32);
extern bool __VIGetPanicFramebufferSize(unsigned*, unsigned*, unsigned*);
extern void __reload(void);

static const char* const exception_name[] =
Expand Down Expand Up @@ -109,50 +124,100 @@ void __exception_setreload(int t)
reload_timer = t*50;
}

static void waitForReload(void)
void SYS_SetPanicDefaultView(sys_panic_view view)
{
panic_default_view = view;
}

static void showPanicText(unsigned exid, PPCContext* ctx);
static unsigned showPanicQR(unsigned exid, const PPCContext* ctx, bool auto_view);

static void printPanicTextControls(bool auto_view)
{
kprintf("\nRESET/Y: show QR code Hold RESET/Z: reload\n");
if (auto_view) {
kprintf("Auto: 15s until input\n");
}
}

static void printReloadCountdown(unsigned qr_countdown_row)
{
if (qr_countdown_row) {
kprintf("\x1b[%u;1H\x1b[0J", qr_countdown_row);
} else {
kprintf("\x1b[2K\r");
}
kprintf("Reloading in %d seconds", 1 + reload_timer / 50);
}

static void waitForReload(unsigned exid, PPCContext* ctx, unsigned qr_countdown_row)
{
PAD_Init();
PanicControls controls = {0};

kprintf("\nPress RESET, or Z on a GameCube Controller, to reload\n");
if (!qr_countdown_row) {
printPanicTextControls(true);
}

for (;;) {
udelay(20000);
PAD_ScanPads();

unsigned buttonsDown = PAD_ButtonsDown(0);
bool was_auto_view = !controls.manual_input;
PanicControlAction action = panicControlsPoll(&controls, SYS_ResetButtonDown(),
(buttonsDown & PAD_BUTTON_Y) != 0, (buttonsDown & PAD_TRIGGER_Z) != 0);
bool auto_view = !controls.manual_input;
if (was_auto_view && !auto_view) {
if (!qr_countdown_row) {
/* The hint is one row above the text countdown. */
kprintf("\x1b[A\x1b[2K\x1b[B");
} else if (reload_timer <= 0) {
kprintf("\x1b[%u;1H\x1b[0J", qr_countdown_row);
}
}
if (action.show_other_view) {
if (qr_countdown_row) {
showPanicText(exid, ctx);
printPanicTextControls(auto_view);
qr_countdown_row = 0;
} else {
qr_countdown_row = showPanicQR(exid, ctx, auto_view);
}
}

if (!reload_timer || SYS_ResetButtonDown() || (buttonsDown & PAD_TRIGGER_Z)) {
kprintf("\x1b[2K\rReload\n");
if (!reload_timer || action.reload) {
if (!qr_countdown_row) {
kprintf("\x1b[2K\rReload\n");
}
__reload();
}

if (buttonsDown & PAD_BUTTON_A) {
kprintf("\x1b[2K\rReset\n");
if (!qr_countdown_row) {
kprintf("\x1b[2K\rReset\n");
}
// Destroy bootstub signature
g_envBootstub->magic[0] = 0;
g_envBootstub->magic[1] = 0;
__reload();
}

if (reload_timer > 0) {
kprintf("\x1b[2K\rReloading in %d seconds", 1+reload_timer/50);
}

if (reload_timer > 0) {
printReloadCountdown(qr_countdown_row);
reload_timer--;
}
}
}

void __libogc_panic(unsigned exid, PPCContext* ctx)
static void showPanicText(unsigned exid, PPCContext* ctx)
{
const bool is_pal = VIDEO_GetCurrentTvMode() == VI_PAL;
const unsigned console_width = 640;
const unsigned console_height = is_pal ? 574 : 480;
const unsigned border_width = 48;
const unsigned border_height = is_pal ? 96 : 48;

GX_AbortFrame();
VIDEO_SetFramebuffer(exception_xfb);
__VIClearFramebuffer(exception_xfb, console_height*console_width*VI_DISPLAY_PIX_SZ, COLOR_MAROON);
__console_init(exception_xfb, border_width, border_height, console_width - 2*border_width, console_height - 2*border_height, 2*console_width);
Expand All @@ -168,7 +233,7 @@ void __libogc_panic(unsigned exid, PPCContext* ctx)
kprintf(" PC %08X LR %08X MSR %08X Thrd %08X\n", ctx->pc, ctx->lr, ctx->msr, (u32)KThreadGetSelf());
kprintf(" CR %08X CTR %08X XER %08X\n", ctx->cr, ctx->ctr, ctx->xer);
if (exid == PPC_EXCPT_DSI) {
kprintf(" DAR %08X DSISR %08X\n", mfspr(DAR), mfspr(DSISR));
kprintf(" DAR %08X DSISR %08X\n", panic_dar, panic_dsisr);
}

__libogc_print_stack(ctx->pc, ctx->gpr[1]);
Expand All @@ -181,6 +246,138 @@ void __libogc_panic(unsigned exid, PPCContext* ctx)
&pAdd[i], pAdd[i], pAdd[i+1], pAdd[i+2], pAdd[i+3]);
}
}
}

static char* reportText(char* out, const char* text)
{
while (*text) {
*out++ = *text++;
}
return out;
}

static char* reportHex(char* out, u32 value)
{
static const char hex[] = "0123456789ABCDEF";
for (int shift = 28; shift >= 0; shift -= 4) {
*out++ = hex[(value >> shift) & 15];
}
return out;
}

static char* reportField(char* out, const char* label, u32 value)
{
return reportHex(reportText(out, label), value);
}

static void formatPanicReport(unsigned exid, const PPCContext* ctx)
{
char* out = panic_report;
out = reportField(out, "LIBOGC 1 EX ", exid);
out = reportField(out, " PC ", ctx->pc);
out = reportField(out, " LR ", ctx->lr);
out = reportField(out, " MSR ", ctx->msr);
out = reportField(out, " CR ", ctx->cr);
out = reportField(out, " CTR ", ctx->ctr);
out = reportField(out, " XER ", ctx->xer);
out = reportField(out, " THREAD ", (u32)KThreadGetSelf());
if (exid == PPC_EXCPT_DSI) {
out = reportField(out, " DAR ", panic_dar);
out = reportField(out, " DSISR ", panic_dsisr);
}
out = reportText(out, " GPR");
for (unsigned i = 0; i < 32; i++) {
out = reportField(out, " ", ctx->gpr[i]);
}
out = reportText(out, " STACK");
PPCStackFrame* frame = check_stack_address(ctx->gpr[1]);
for (unsigned i = 1; frame && i < CPU_STACK_TRACE_DEPTH; i++) {
out = reportField(out, " ", (u32)frame->lr);
frame = check_stack_address(frame->next);
}
*out = 0;
}

static void drawPanicQR(const PanicFramebuffer* fb, unsigned top, unsigned scale)
{
const unsigned side = PANIC_QR_TOTAL_MODULES * scale;
const unsigned x0 = ((fb->width - side) / 2) & ~1u;
const unsigned y0 = top + (fb->height - top - side) / 2;
u32* xfb = (u32*)exception_xfb;
/* The XFB stores pairs of pixels, so modules use an even pixel size. */
for (unsigned y = 0; y < side; y++) {
int my = (int)(y / scale) - PANIC_QR_QUIET_ZONE;
u32* row = xfb + (y0 + y) * (fb->stride / sizeof(*xfb)) + x0 / 2;
for (unsigned x = 0; x < side; x += 2) {
int mx = (int)(x / scale) - PANIC_QR_QUIET_ZONE;
bool dark = mx >= 0 && my >= 0 && mx < PANIC_QR_SIZE && my < PANIC_QR_SIZE
&& panic_qr[my * PANIC_QR_SIZE + mx];
row[x / 2] = dark ? COLOR_BLACK : COLOR_WHITE;
}
}
}

waitForReload();
static unsigned panicTextRows(unsigned length, unsigned columns)
{
return (length + columns - 1) / columns;
}

/* Returns the countdown row, or zero when the QR view is unavailable. */
static unsigned showPanicQR(unsigned exid, const PPCContext* ctx, bool auto_view)
{
static const char details_hint[] = "RESET/Y: show details";
static const char reload_hint[] = "Hold RESET/Z: reload";
PanicFramebuffer fb;
if (!__VIGetPanicFramebufferSize(&fb.width, &fb.height, &fb.stride)) {
return 0;
}

const unsigned border_x = fb.width >= 480 ? 48 : 16;
const unsigned border_y = fb.height >= 400 ? 48 : 16;
const unsigned console_width = fb.width - 2 * border_x;
const unsigned columns = console_width / FONT_XSIZE;
const unsigned hint_rows = panicTextRows(sizeof(details_hint) - 1, columns)
+ panicTextRows(sizeof(reload_hint) - 1, columns);
const unsigned countdown_row = hint_rows + 1;
const unsigned status_rows = panicTextRows(sizeof("Reloading in 0000000000 seconds") - 1, columns);
const unsigned header_height = (hint_rows + status_rows) * FONT_YSIZE;
const unsigned qr_top = border_y + header_height + FONT_YSIZE / 2;
if (fb.width < PANIC_QR_TOTAL_MODULES * 2 || fb.height < qr_top + PANIC_QR_TOTAL_MODULES * 2) {
return 0;
}

formatPanicReport(exid, ctx);
if (!__panic_qr_encode(panic_report, panic_qr)) {
return 0;
}

VIDEO_SetFramebuffer(exception_xfb);
__VIClearFramebuffer(exception_xfb, (fb.height * fb.stride + 255) & ~255u, COLOR_MAROON);
__console_init(exception_xfb, border_x, border_y, console_width, header_height, fb.stride);
kprintf("%s\n%s\n", details_hint, reload_hint);
if (reload_timer > 0) {
printReloadCountdown(countdown_row);
} else if (auto_view) {
kprintf("Auto: 15s until input");
}
const unsigned available_height = fb.height - qr_top;
const unsigned scale = fb.width >= PANIC_QR_TOTAL_MODULES * 4
&& available_height >= PANIC_QR_TOTAL_MODULES * 4 ? 4 : 2;
drawPanicQR(&fb, qr_top, scale);
return countdown_row;
}

void __libogc_panic(unsigned exid, PPCContext* ctx)
{
if (exid == PPC_EXCPT_DSI) {
panic_dar = mfspr(DAR);
panic_dsisr = mfspr(DSISR);
}
GX_AbortFrame();
showPanicText(exid, ctx);
unsigned qr_countdown_row = 0;
if (panic_default_view == SYS_PANIC_VIEW_QR) {
qr_countdown_row = showPanicQR(exid, ctx, true);
}
waitForReload(exid, ctx, qr_countdown_row);
}
Loading