Skip to content

Commit

Permalink
add progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
patois committed Apr 15, 2015
1 parent d775bfd commit 8104438
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 54 deletions.
10 changes: 9 additions & 1 deletion README
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
3DS DEVELOPMENT TOOLS

To be used with brahma (https://github.com/patois/Brahma/releases)
This tool runs in privileged mode on the ARM9 processor of the Nintendo (N)3DS.

Currently, it offers the ability to dump several memory regions to SD card
for analysis.

Use with Brahma (https://github.com/patois/Brahma/releases)
During startup, hold 'Y' button on N3DS consoles to decrypt ARM9 binary
(on O3DS consoles, the ARM9 binary does not have an additional encryption
layer).
4 changes: 3 additions & 1 deletion include/dumper.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@
#define ARM9_BOOTROM 0xFFFF0000
#define ARM9_BOOTROM_SIZE 0x00010000

u32 dump_mem (const char *filename, void *addr, u32 size);
typedef int progress_printer_t(int oldstep, u32 val, u32 maval);

u32 dump_mem (const char *filename, void *addr, u32 size, progress_printer_t *f);
3 changes: 3 additions & 0 deletions include/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "dumper.h"
#include "textmenu.h"

#define BRAHMA_TOOL_NAME "3DS DEVELOPMENT TOOLS"

void clear_top (void);
int ask_dump (char *what);
void newline (int count);
Expand All @@ -14,5 +16,6 @@ int menu_cb_dump_firm (int idx, void *notused);
int menu_cb_decrypt_loader(int idx, void *notused);
int print_menu (int idx, struct menu_t *menu);
int print_main_menu (int idx, struct menu_t *menu);
int print_progress(int oldstep, u32 val, u32 maxval);

extern struct menu_t main_menu;
7 changes: 6 additions & 1 deletion source/dumper.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,28 @@

// TODO: add callback argument for progress bar
// TODO: implement progress bar :)
u32 dump_mem (const char *filename, void *addr, u32 size) {
u32 dump_mem (const char *filename, void *addr, u32 size, progress_printer_t *f) {
u32 written = 0;
u32 total = 0;
u32 result = 0;
u32 num = 0;
const u32 sz_chunk = 0x10000;
int step = -1;

if (FileCreate(filename, true)) {
while (total < size) {
num = size - total < sz_chunk ? size - total : sz_chunk;
written = FileWrite((u8 *)addr + total,
num,
total);

if (written != num) {
break;
}
total += written;
if (f) {
step = f(step, total/100, size/100);
}
}
FileClose();
result = (size == total);
Expand Down
56 changes: 41 additions & 15 deletions source/interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ struct menu_t main_menu = {
};

void newline (int count) {
int i;
for(i=0; i<count; i++)
Debug("");
int i;
for(i=0; i<count; i++)
Debug("");
}

void clear_top (void) {
Expand All @@ -33,14 +33,36 @@ void clear_top (void) {
current_y = 0;
}

int print_progress(int oldstep, u32 val, u32 maxval) {
char buf[256];
char *p = (char *)&buf;
int i;
int curstep;
const int maxstep = 10;

curstep = (val * maxstep) / maxval;

if (curstep > oldstep) {
strcpy(p++, "[");
for (i = 0; i < maxstep; i++) {
snprintf(p++, sizeof(buf) - 1 - i, "%c", i < curstep ? '#' : ' ');
}
strncat(p, "]", sizeof(buf) - strlen(buf) - 1);
clear_top();
newline(1);
Debug("Dumping %s %3d%%", buf, (val * 100 + maxval/2) / maxval);
}
return curstep > oldstep ? curstep : oldstep;
}

int ask_dump (char *what) {
u32 key;

clear_top();
newline(2);
Debug("Dump %s?", what);
newline(1);
Debug("---------------------------");
Debug("---------------------------");
newline(1);
Debug("START : Confirm");
Debug("B : Abort");
Expand All @@ -49,7 +71,6 @@ int ask_dump (char *what) {
if (key & BUTTON_START) {
clear_top();
newline(1);
Debug("Do NOT turn off your console!");
break;
} else if (key & BUTTON_B) {
clear_top();
Expand All @@ -65,9 +86,10 @@ int menu_cb_dump_bootrom (int idx, void *notused) {
u32 result = 0;

if (ask_dump("ARM9 RAM")) {
result = dump_mem(PATH_BOOTROM, (void *)ARM9_BOOTROM, ARM9_BOOTROM_SIZE);
result = dump_mem(PATH_BOOTROM, (void *)ARM9_BOOTROM, ARM9_BOOTROM_SIZE, &print_progress);
newline(2);
Debug("Done: %s!", result ? "success":"failure");
result = 1;
}
return result;
}
Expand All @@ -76,9 +98,10 @@ int menu_cb_dump_arm9internal (int idx, void *notused) {
u32 result = 0;

if (ask_dump("ARM9 RAM")) {
result = dump_mem(PATH_ARM9INTERNAL, (void *)ARM9_INTERNAL, ARM9_INTERNAL_SIZE);
result = dump_mem(PATH_ARM9INTERNAL, (void *)ARM9_INTERNAL, ARM9_INTERNAL_SIZE, &print_progress);
newline(2);
Debug("Done: %s!", result ? "success":"failure");
result = 1;
}
return result;
}
Expand All @@ -87,9 +110,10 @@ int menu_cb_dump_fcram (int idx, void *notused) {
u32 result = 0;

if (ask_dump("FCRAM")) {
result = dump_mem(PATH_FCRAM, (void *)ARM9_FCRAM, ARM9_FCRAM_SIZE);
result = dump_mem(PATH_FCRAM, (void *)ARM9_FCRAM, ARM9_FCRAM_SIZE, &print_progress);
newline(2);
Debug("Done: %s!", result ? "success":"failure");
result = 1;
}
return result;
}
Expand All @@ -98,9 +122,10 @@ int menu_cb_dump_axiwram (int idx, void *notused) {
u32 result = 0;

if (ask_dump("AXI WRAM")) {
result = dump_mem(PATH_AXIWRAM, (void *)ARM9_AXIWRAM, ARM9_AXIWRAM_SIZE);
result = dump_mem(PATH_AXIWRAM, (void *)ARM9_AXIWRAM, ARM9_AXIWRAM_SIZE, &print_progress);
newline(2);
Debug("Done: %s!", result ? "success":"failure");
result = 1;
}
return result;
}
Expand All @@ -115,10 +140,11 @@ int menu_cb_dump_firm (int idx, void *notused) {
into account sections that are not 'properly' aligned */
if (is_valid_firm()) {
if (ask_dump("FIRM")) {
newline(1);
Debug("FIRM header:");
Debug("%08X-%08X (%08X)", firm, (u32)firm + sizeof(firm_header_t), sizeof(firm_header_t));
newline(1);
dump_mem(PATH_FIRM_HEADER, (void *)firm, sizeof(firm_header_t));
dump_mem(PATH_FIRM_HEADER, (void *)firm, sizeof(firm_header_t), 0);
for (int i=0; i<FIRM_MAX_SECTION_COUNT; i++) {

snprintf((char *)&filename, sizeof(filename) - 1, PATH_FMT_FIRM_SECTION, i);
Expand All @@ -128,15 +154,15 @@ int menu_cb_dump_firm (int idx, void *notused) {
Debug("Section %d/%d:", i + 1, FIRM_MAX_SECTION_COUNT);
if (addr && size) {
Debug("%08X-%08X (%08X)", addr, addr + size, size);
if (!dump_mem((char *)&filename, addr, size)) {
if (!dump_mem((char *)&filename, addr, size, 0)) {
Debug("[!] ERROR");
}
} else {
Debug("Empty section: skipping");
}
newline(1);
}
//Debug("Done:!");
result = 1;
}
} else {
Debug("Invalid FIRM header. Please turn off console.");
Expand All @@ -150,8 +176,8 @@ int menu_cb_decrypt_loader (int idx, void *param) {
u32 result = 0;
loader_info_t *li = param;

clear_top();
newline(2);
clear_top();
newline(2);

if (GetUnitPlatform() == PLATFORM_N3DS) {
if (li) {
Expand Down Expand Up @@ -208,7 +234,7 @@ int print_main_menu (int idx, struct menu_t *menu) {
int newidx = 0;

newline(1);
Debug("3DS DEVELOPMENT TOOLS");
Debug("%s", BRAHMA_TOOL_NAME);
newline(2);
Debug("===========================");
newidx = print_menu(idx, menu);
Expand Down
82 changes: 54 additions & 28 deletions source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,81 @@
#include "fs.h"
#include "textmenu.h"
#include "interface.h"
#include "arm9loader.h"
#include "platform.h"
//#include "arm9loader.h"

//extern u32 entry_point_arm9;
extern u32 is_arm9_decrypted;

void check_trigger (void) {
extern u32 arm9_decrypted;
extern u32 key_trigger;
u8 is_n3ds = (GetUnitPlatform() == PLATFORM_N3DS);

if (key_trigger) {
clear_top();
newline(1);
Debug("%s", BRAHMA_TOOL_NAME);
newline(4);

if (is_n3ds) {
if (arm9_decrypted) {
Debug("N3DS ARM9 binary successfully decrypted!");
newline(1);
Debug("If you require a \"clean\" dump,");
Debug("do NOT hold 'Y' during startup.");
} else {
Debug("Decryption of the N3DS binary failed!");
newline(1);
Debug("Your platform has not yet been added");
Debug("support for ARM9 binary decryption.");
}
} else { // old 3ds
newline(1);
Debug("Isn't it fun to be able to run code");
Debug("on this neat little handheld?");
}
wait_any_key();
}
}

int main () {
InitFS();

u32 pad_state;
int menuidx = 0;

if (is_arm9_decrypted) {
clear_top();
newline(1);
Debug("N3DS ARM9 binary successfully decrypted!");
newline(2);
Debug("Hold 'Y' during startup if you do not");
Debug("want the ARM9 binary to be decrypted");
wait_any_key();
}
//loader_info_t li;

//result = init_loader_info(&li);
//li.address = entry_point_arm9;

while (true) {
check_trigger();

while (true) {
clear_top();

menuidx = print_main_menu(menuidx, &main_menu);
pad_state = InputWait();
pad_state = InputWait();

if (pad_state & BUTTON_START) {
break;
}
else if (pad_state & BUTTON_A) {
break;
}
else if (pad_state & BUTTON_A) {
//menu_execute_function(menuidx, &main_menu, &li);
menu_execute_function(menuidx, &main_menu, 0);
wait_any_key();
}
else if (pad_state & BUTTON_UP) {
menuidx--;
}
else if (pad_state & BUTTON_DOWN) {
menuidx++;
}
}
if (menu_execute_function(menuidx, &main_menu, 0)) {
wait_any_key();
}
}
else if (pad_state & BUTTON_UP) {
menuidx--;
}
else if (pad_state & BUTTON_DOWN) {
menuidx++;
}
}

DeinitFS();

// return control to firmware
return 0;
return 0;
}
23 changes: 15 additions & 8 deletions source/start.s
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
.section ".init"

.global _start
.global is_arm9_decrypted
.global arm9_decrypted
.global key_trigger
.global entry_point_arm9

.extern main
Expand All @@ -16,23 +17,29 @@ _start:
@ required, don't move :)
entry_point_arm9: .long 0

is_arm9_decrypted: .long 0
arm9_decrypted: .long 0
key_trigger: .long 0
HID_PAD: .long 0x10146000

_init:
stmfd sp!, {r0-r12, lr}

bl fix_firm_hdr

bl is_n3ds
cmp r0, #0
beq j_main

@ attempt decryption only if
@ 'Y' is pressed during startup
ldr r0, HID_PAD
ldr r0, [r0]
ands r0, #0x800 @ BUTTON_Y
beq j_main
bne j_main

mov r0, #1
str r0, key_trigger

bl is_n3ds
cmp r0, #0
beq j_main

bl is_kernel_supported
cmp r0, #1
beq unpack_arm9_ldr
Expand All @@ -55,7 +62,7 @@ cont:
stmfd sp!, {r0-r12, lr}
bl restore_n3ds_ldr
mov r1, #1
str r1, is_arm9_decrypted
str r1, arm9_decrypted
ldmfd sp!, {r0-r12, lr}

_main:
Expand Down

0 comments on commit 8104438

Please sign in to comment.