Skip to content

Commit

Permalink
added screen init, added splash screens, added visual debug output, a…
Browse files Browse the repository at this point in the history
…dded

the posibility to set a delay for checking the pressed key
  • Loading branch information
Hartie95 committed Feb 29, 2016
1 parent e1c7e8c commit 775dd89
Show file tree
Hide file tree
Showing 12 changed files with 524 additions and 26 deletions.
4 changes: 2 additions & 2 deletions bootloader/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ INCLUDES := include source/fatfs source/inih source/fatfs/sdmmc
#---------------------------------------------------------------------------------
# Setup some defines
#---------------------------------------------------------------------------------

VERSION := $(shell git describe --abbrev=4 --always --dirty --tags || echo "UNKNOWN")
#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
Expand All @@ -34,7 +34,7 @@ ARCH := -mthumb -mthumb-interwork
CFLAGS := -g -Wall -O2\
-march=armv5te -mtune=arm946e-s -fomit-frame-pointer\
-ffast-math -std=c99\
$(ARCH)
$(ARCH) -DVERSION_STRING="\"$(VERSION)\""

CFLAGS += $(INCLUDE) -DARM9

Expand Down
5 changes: 4 additions & 1 deletion bootloader/include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ typedef struct {
char path[64];
unsigned long long delay;
unsigned long payload;
unsigned long offset;
unsigned long offset;
int splash;
char splash_image[64];
unsigned int screenEnabled;
} configuration;

#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
Expand Down
25 changes: 25 additions & 0 deletions bootloader/include/draw.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include "common.h"

#define BYTES_PER_PIXEL 3
#define SCREEN_WIDTH 240
#define SCREEN_HEIGHT 400

#define SCREEN_SIZE (BYTES_PER_PIXEL * SCREEN_WIDTH * SCREEN_HEIGHT)

#define RGB(r,g,b) (r<<24|b<<16|g<<8|r)

#define TOP_SCREEN0 (u8*)(0x18300000)
#define BOT_SCREEN0 (u8*)(0x18346500)
extern int current_y;

void ClearScreen(unsigned char *screen, int color);
void clearScreens();
void DrawCharacter(unsigned char *screen, int character, int x, int y, int color, int bgcolor);
void DrawHex(unsigned char *screen, unsigned int hex, int x, int y, int color, int bgcolor);
u32 DrawString(unsigned char *screen, const char *str, int x, int y, int color, int bgcolor);
void DrawStringF(int x, int y, const char *format, ...);
void DrawHexWithName(unsigned char *screen, const char *str, unsigned int hex, int x, int y, int color, int bgcolor);

void drawDebug(const char *format, ...);
4 changes: 2 additions & 2 deletions bootloader/include/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

int initLog();
void closeLogFile();
void debug(const char *msg);
void panic(const char *msg);
void debug(const char *format, ...);
void panic(const char *format, ...);
void shutdown();
7 changes: 7 additions & 0 deletions bootloader/include/screen.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "common.h"

typedef void (*arm11Function)(void);
void screenInit();
void screenDeinit();
void __attribute__((naked)) disable_lcds();
void __attribute__ ((naked)) enable_lcd();
13 changes: 13 additions & 0 deletions bootloader/include/splash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include "common.h"

#define ASCII_ART_TEMPLATE (" _ __\n" \
"|_) _ _ _|_/ _|_ __\n" \
"|_)(_)(_) |_\\__ |_ |\n" \
"%s\n" \
"\n" \
"is loading...")

int splash_ascii(void);
int splash_image(char *splash_path);
10 changes: 8 additions & 2 deletions bootloader/source/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,13 @@ int handler(void *user, const char *section, const char *name, const char *value
} else if (MATCH(pconfig->section, "payload")) {
pconfig->payload = myAtoi(value);
} else if (MATCH(pconfig->section, "offset")) {
pconfig->offset = numberToInt(value);
}
pconfig->offset = numberToInt(value);
} else if (MATCH(pconfig->section, "splash")) {
pconfig->splash = myAtoi(value);
} else if (MATCH(pconfig->section, "splash_image")) {
strcpy (pconfig->splash_image,value);
} else if (MATCH(pconfig->section, "screenEnabled")) {
pconfig->screenEnabled = myAtoi(value);
}
return 1;
}
95 changes: 95 additions & 0 deletions bootloader/source/draw.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>

#include "font.h"
#include "draw.h"

int current_y = 0;

void ClearScreen(unsigned char *screen, int color)
{
int i;
unsigned char *screenPos = screen;
for (i = 0; i < (SCREEN_HEIGHT * SCREEN_WIDTH); i++) {
*(screenPos++) = color >> 16; // B
*(screenPos++) = color >> 8; // G
*(screenPos++) = color & 0xFF; // R
}
}

void clearScreens()
{
ClearScreen(TOP_SCREEN0,0);
ClearScreen(BOT_SCREEN0,0);
}

void DrawCharacter(unsigned char *screen, int character, int x, int y, int color, int bgcolor)
{
int yy, xx;
for (yy = 0; yy < 8; yy++) {
int xDisplacement = (x * BYTES_PER_PIXEL * SCREEN_WIDTH);
int yDisplacement = ((SCREEN_WIDTH - (y + yy) - 1) * BYTES_PER_PIXEL);
unsigned char *screenPos = screen + xDisplacement + yDisplacement;

unsigned char charPos = font[character * 8 + yy];
for (xx = 7; xx >= 0; xx--) {
if ((charPos >> xx) & 1) {
*(screenPos + 0) = color >> 16; // B
*(screenPos + 1) = color >> 8; // G
*(screenPos + 2) = color & 0xFF; // R
} else {
*(screenPos + 0) = bgcolor >> 16; // B
*(screenPos + 1) = bgcolor >> 8; // G
*(screenPos + 2) = bgcolor & 0xFF; // R
}
screenPos += BYTES_PER_PIXEL * SCREEN_WIDTH;
}
}
}

u32 DrawString(unsigned char *screen, const char *str, int x, int y, int color, int bgcolor)
{
int i,position=0;
for (i = 0; i < strlen(str); i++)
{
if(str[i]=='\n')
{
y+=10;
position=0;
}
else
{
DrawCharacter(screen, str[i], x + position, y, color, bgcolor);
position+=8;
}
}
return y;
}

void DrawStringF(int x, int y, const char *format, ...)
{
char str[256];
va_list va;

va_start(va, format);
vsnprintf(str, 256, format, va);
va_end(va);

DrawString(TOP_SCREEN0, str, x, y, RGB(255, 255, 255), RGB(0, 0, 0));
}

void drawDebug(const char *format, ...)
{
char str[256];
va_list va;

va_start(va, format);
vsnprintf(str, 256, format, va);
va_end(va);

current_y = DrawString(BOT_SCREEN0, str, 10, current_y, RGB(255, 255, 255), RGB(0, 0, 0));

current_y += 10;
}
38 changes: 30 additions & 8 deletions bootloader/source/log.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#include "log.h"
#include "draw.h"

#include "fatfs/ff.h"
#include "i2c.h"
#include "hid.h"
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>

#define LOGNAME "/arm9loaderhax/arm9bootloader.log"
#define LOGNAME_BACKUP "/arm9bootloader.log"
Expand Down Expand Up @@ -33,31 +39,47 @@ void closeLogFile()
}
}

void debug(const char *msg)
void debug(const char *format, ...)
{
char str[256];
va_list va;

va_start(va, format);
vsnprintf(str, 256, format, va);
va_end(va);

if(logFileOpened)
{
f_puts (msg, &logFile);
f_puts (str, &logFile);
f_putc ('\n', &logFile);
f_sync (&logFile);
}
drawDebug(str);
}

void panic(const char *msg)
void panic(const char *format, ...)
{
char str[256];
va_list va;

va_start(va, format);
vsnprintf(str, 256, format, va);
va_end(va);

if(logFileOpened)
{
debug("ERROR:");
debug(msg);
debug(str);
}

drawDebug(str);
drawDebug("Press any key to shutdown");
InputWait();
shutdown();
}

void shutdown() {
//debug("shutdown");
//closeLogFile();
debug("shutdown");
closeLogFile();
i2cWriteRegister(I2C_DEV_MCU, 0x20, (u8)(1<<0));
//exit(0);
while(1);
}
46 changes: 35 additions & 11 deletions bootloader/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@
#include "hid.h"
#include "log.h"
#include "i2c.h"
#include "screen.h"
#include "splash.h"


#define DEFAULT_PATH {0}
#define DEFAULT_DELAY 2000 /* ms */
#define DEFAULT_PATH {0}
#define DEFAULT_DELAY 100 /* ms */
#define DEFAULT_PAYLOAD -1 /* <0 - auto, 0 - disable, >0 - enabled */
#define DEFAULT_OFFSET 0x12000
#define DEFAULT_OFFSET 0x12000
#define DEFAULT_SECTION "GLOBAL"
#define INI_FILE "/arm9loaderhax/boot_config.ini"
#define DEFAULT_SCREEN 1
#define DEFAULT_SPLASH 3 /* 0 - disabled, 1 - splash screen, 2 - entry info, 3 - both */
#define DEFAULT_SPLASH_IMAGE {0}
#define INI_FILE "/arm9loaderhax/boot_config.ini"
#define INI_FILE_BOOTCTR "/boot_config.ini"


#define PAYLOAD_ADDRESS 0x23F00000
#define PAYLOAD_SIZE 0x00100000

#define SCREEN_SIZE 400 * 240 * 3 / 4 //yes I know this is more than the size of the bootom screen

bool file_exists(const char* path) {
FIL fd;
Expand Down Expand Up @@ -57,9 +62,18 @@ int main() {
.delay = DEFAULT_DELAY,
.payload = DEFAULT_PAYLOAD,
.offset = DEFAULT_OFFSET,
.splash = DEFAULT_SPLASH,
.splash_image = DEFAULT_SPLASH_IMAGE,
.screenEnabled = DEFAULT_SCREEN,
};
FATFS fs;
FIL payload;
if(*((u8*)0x101401C0) == 0x0)
{
screenInit();
debug("Enabeling screen");
}
clearScreens();

if(f_mount(&fs, "0:", 1) == FR_OK)
{
Expand All @@ -69,7 +83,10 @@ int main() {
iniparse(INI_FILE, handler, &app);

debug("Checking input");
for(volatile u64 i=0;i<0xEFF*app.delay;i++);
u32 key = GetInput();
app.delay=0;

// using X-macros to generate each switch-case rules
// https://en.wikibooks.org/wiki/C_Programming/Preprocessor#X-Macros
#define KEY(k) \
Expand All @@ -79,8 +96,7 @@ int main() {
#include "keys.def"
app.section = "DEFAULT";

debug("Key checked - selected section:");
debug(app.section);
debug("Key checked-selected section: %s",app.section);

int config_err = iniparse(INI_FILE, handler, &app);

Expand All @@ -96,7 +112,7 @@ int main() {
if (!app.path)
panic("Section [DEFAULT] not found or \"path\" not set.");
} else if (!file_exists(app.path)) {
debug("[ERROR] Target payload not found:");
debug("[ERROR] Target payload not found:\n%s",app.path);
panic(app.path);
}
break;
Expand All @@ -105,14 +121,14 @@ int main() {
break;
case -2:
// should not happen, however better be safe than sorry
panic("Config file is too big.");//, INI_FILE);
panic("Config file is too big.");
break;
default:
panic("Error found in config file");//,
//INI_FILE, config_err);
break;
}

debug("Checking payload");
if(app.payload==0)
{
Expand All @@ -123,6 +139,10 @@ int main() {
debug("[ERROR] Target payload not found:");
panic(app.path);
}

debug("Loading Splash image");
splash_image(app.splash_image);
for(volatile u64 i=0;i<0xEFF*app.delay;i++);

debug("Loading Payload:");
debug(app.path);
Expand All @@ -145,6 +165,10 @@ int main() {
closeLogFile();
f_mount(&fs, "0:", 1);
debug("Jumping to the payload");
if(app.screenEnabled==0)
{
screenDeinit();
}
((void (*)())PAYLOAD_ADDRESS)();
}
}
Expand Down
Loading

0 comments on commit 775dd89

Please sign in to comment.