Skip to content

Commit

Permalink
add reset stub and passthrough
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor403 committed Jul 11, 2024
1 parent de2c893 commit eb14628
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 143 deletions.
2 changes: 1 addition & 1 deletion cubeboot/source/flippy_sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ int dvd_read_data(void* dst, unsigned int len, uint64_t offset, unsigned int fd)

if(((uint32_t)dst & 0x1F) || (len & 0x1F) || (offset & 0x3)) //Buffer, length, or offset is not aligned
{
GCN_ALIGNED(u8) aligned_buffer[FD_IPC_MAXRESP];
static GCN_ALIGNED(u8) aligned_buffer[FD_IPC_MAXRESP];

while (remaining > 0)
{
Expand Down
128 changes: 22 additions & 106 deletions cubeboot/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,7 @@

#include "flippy_sync.h"

typedef struct {
struct gcm_disk_header header;
BNR banner;
// u8 icon_rgb5[64*64*2];
char path[128];
} game_asset;

// static u32 prog_entrypoint, prog_dst, prog_src, prog_len;
#define STUB_ADDR 0x80001800

#define BS2_BASE_ADDR 0x81300000
static void (*bs2entry)(void) = (void(*)(void))BS2_BASE_ADDR;
Expand All @@ -75,7 +68,7 @@ void __SYS_PreInit() {
// memcpy(current_dol_buf, &_start, current_dol_len);
}

int main() {
int main(int argc, char **argv) {
u64 startts, endts;

startts = ticks_to_millisecs(gettime());
Expand Down Expand Up @@ -153,7 +146,18 @@ int main() {
#ifdef VIDEO_ENABLE
iprintf("XFB = %08x [max=%x]\n", (u32)xfb, VIDEO_GetFrameBufferSize(&TVPal576ProgScale));
#endif
iprintf("current_dol_len = %d\n", current_dol_len);

// setup passthrough arg
u32 force_passthrough = 0;
if (argc > 1 && strcmp(argv[1], "passthrough") == 0) {
force_passthrough = 1;
}

if (*(u32*)STUB_ADDR == 0x50415353) {
force_passthrough = 1;
}

iprintf("force_passthrough = %d\n", force_passthrough);

#if 0
// setup config device
Expand All @@ -169,100 +173,12 @@ int main() {
if (check_load_program()) {
can_load_dol = true;
}
#elif 0
// dvd setup
// DVDInit();

// fake settings
{
memset(&settings, 0, sizeof(settings));

// cube color
// settings.cube_color = 0x660089;
settings.cube_color = 0; // default

// cube logo
settings.cube_logo = NULL;

// default program
settings.default_program = NULL;

// fallback enable
settings.fallback_enabled = 0;

// progressive enable
// GXRModeObj *temp = VIDEO_GetPreferredMode(NULL);
// settings.progressive_enabled = (temp->viTVMode & 3) == VI_PROGRESSIVE ? 1 : 0;
// iprintf("Detected progressive: %d\n", settings.progressive_enabled);
// settings.progressive_enabled = 1;
settings.progressive_enabled = 0;

// preboot delay
settings.preboot_delay_ms = 0;

// postboot delay
settings.postboot_delay_ms = 0;
}
#else
// setup settings
iprintf("Loading settings\n");
load_settings();
#endif

#if 0
iprintf("Checkup, done=%08x\n", state->boot_code);
if (state->boot_code == 0xCAFEBEEF) {
iprintf("He's alive! The doc's alive! He's in the old west, but he's alive!!\n");

#ifdef VIDEO_ENABLE
VIDEO_WaitVSync();
#endif

// check for a held button
int held_max_index = -1;
u64 held_current_max = 0;
for (int i = 0; i < MAX_BUTTONS; i++) {
if (state->held_buttons[i].status == 0) continue;

u64 ts = state->held_buttons[i].timestamp;
u32 ms = ticks_to_millisecs(ts);

if (ms > held_current_max) {
held_max_index = i;
held_current_max = ms;
}

iprintf("HELD: %s\n", buttons_names[i]);
}

// only boot when held > 300ms
if (held_current_max < 300) {
held_max_index = -1;
}

if (held_max_index != -1) {
char *button_name = buttons_names[held_max_index];
iprintf("MAX HELD: %s\n", button_name);
char buf[64];

char *filename = settings.boot_buttons[held_max_index];
if (filename == NULL) {
filename = &buf[0];
strcpy(filename, button_name);
strcat(filename, ".dol");
}

if (*button_name == '_') {
filename = NULL;
}

boot_program(filename);
} else {
boot_program(NULL);
}
}
#endif

//// fun stuff

// load ipl
Expand Down Expand Up @@ -384,12 +300,12 @@ int main() {
prog_halt("Failed BIOS Patching relocation\n");
}

// local vars
u8 *image_data = NULL;
if (settings.cube_logo != NULL) {
image_data = load_logo_texture(settings.cube_logo);
iprintf("img can be found at %08x\n", (u32)image_data);
}
// // local vars
// u8 *image_data = NULL;
// if (settings.cube_logo != NULL && strlen(settings.cube_logo) > 0) {
// image_data = load_logo_texture(settings.cube_logo);
// iprintf("img can be found at %08x\n", (u32)image_data);
// }

// // load current program
// prog_entrypoint = (u32)&_start;
Expand All @@ -406,9 +322,9 @@ int main() {
// set_patch_value(symshdr, syment, symstringdata, "prog_len", prog_len);

// Copy settings into place
// set_patch_value(symshdr, syment, symstringdata, "start_game", can_load_dol);
set_patch_value(symshdr, syment, symstringdata, "start_passthrough_game", force_passthrough);
set_patch_value(symshdr, syment, symstringdata, "cube_color", settings.cube_color);
set_patch_value(symshdr, syment, symstringdata, "cube_text_tex", (u32)image_data);
// set_patch_value(symshdr, syment, symstringdata, "cube_text_tex", (u32)image_data);
set_patch_value(symshdr, syment, symstringdata, "force_progressive", settings.progressive_enabled);
set_patch_value(symshdr, syment, symstringdata, "force_swiss_boot", settings.force_swiss_default);

Expand Down
2 changes: 1 addition & 1 deletion cubeboot/source/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ int iprintf(const char *fmt, ...) {
va_start(args, fmt);
// length = vsprintf(line, fmt, args);
// length = sprintf(buf, "(%f): %s", (f32)diff_usec(first_print, gettime()) / 1000.0, line);
length = vsprintf(buf, fmt, args);
length = vsnprintf(buf, sizeof(buf), fmt, args);

write(2, buf, length);

Expand Down
6 changes: 3 additions & 3 deletions cubeboot/source/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ void load_settings() {
return;
}

((u8*)config_buf)[config_size - 1] = '\0';
((char*)config_buf)[config_size - 1] = '\0';

iprintf("DUMP:\n");
iprintf("%s\n", (char*)config_buf);
// iprintf("DUMP:\n");
// iprintf("%s\n\n", (char*)config_buf);

ini_t *conf = ini_load(config_buf, config_size);

Expand Down
24 changes: 14 additions & 10 deletions patches/source/boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ void load_stub() {
file_size += 31;
file_size &= 0xffffffe0;

dvd_read((void*)0x80001800, file_size, 0, file_status->fd);
dvd_read((void*)STUB_ADDR, file_size, 0, file_status->fd);
ICInvalidateRange((void*)STUB_ADDR, file_size);

dvd_custom_close(file_status->fd);
return;
}
Expand Down Expand Up @@ -85,24 +87,26 @@ void prepare_game_lowmem(char *boot_path) {
dvd_custom_close(file_status->fd);
}

void chainload_boot_game(char *boot_path) {
void chainload_boot_game(char *boot_path, bool passthrough) {
// read boot info into lowmem
struct dolphin_lowmem *lowmem = (struct dolphin_lowmem*)0x80000000;
lowmem->a_boot_magic = 0x0D15EA5E;
lowmem->a_version = 0x00000001;
lowmem->b_physical_memory_size = 0x01800000;

// open file
dvd_custom_open(boot_path, FILE_ENTRY_TYPE_FILE, 0);
file_status_t *file_status = dvd_custom_status();
if (file_status == NULL || file_status->result != 0) {
custom_OSReport("Failed to open file %s\n", boot_path);
return;
if (!passthrough) {
// open file
dvd_custom_open(boot_path, FILE_ENTRY_TYPE_FILE, 0);
file_status_t *file_status = dvd_custom_status();
if (file_status == NULL || file_status->result != 0) {
custom_OSReport("Failed to open file %s\n", boot_path);
return;
}

dvd_set_default_fd(file_status->fd);
}

dvd_set_default_fd(file_status->fd);
dvd_read(&lowmem->b_disk_info, 0x20, 0, 0);

void *entrypoint = load_apploader();

struct gcm_disk_header_info *bi2 = lowmem->a_bi2;
Expand Down
6 changes: 3 additions & 3 deletions patches/source/boot.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <gctypes.h>

#define STUB_ADDR 0x80001800

typedef struct {
u32 max_addr;
void *entrypoint;
Expand All @@ -9,9 +11,7 @@ void load_stub();
dol_info_t load_dol(char *path, bool flash);

void *load_apploader();
// uint8_t prepare_game(char *boot_path);
// void chainload_boot_game(uint8_t fd);
void prepare_game_lowmem(char *boot_path);
void chainload_boot_game(char *boot_path);
void chainload_boot_game(char *boot_path, bool passthrough);

void run(register void* entry_point);
3 changes: 2 additions & 1 deletion patches/source/filebrowser.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ void *file_enum_worker(void* param) {
#ifdef PRINT_READDIR_FILES
OSReport("FSTSize = 0x%08x\n", game_backing->fst_size);
#endif
// TODO: get_gcm_banner_fast
__DVDFSInit_threaded(status->fd, game_backing);
if (game_backing->dvd_bnr_offset == 0) {
OSReport("No opening.bnr found, skipping\n");
Expand Down Expand Up @@ -447,7 +448,7 @@ void *file_enum_worker(void* param) {
OSReport("Second file enum completed! took=%f (%d)\n", runtime, game_backing_count);
(void)runtime;

OSReport("[DONE] - KILL DOLPHIN NOW\n");
// OSReport("[DONE] - KILL DOLPHIN NOW\n");
return NULL;
}

Expand Down
20 changes: 19 additions & 1 deletion patches/source/flippy_sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ int dvd_custom_open_flash(char *path, uint8_t type, uint8_t flags) {
return 0;
}

void dvd_custom_bypass() {
void dvd_custom_bypass_enter() {
_di_regs[DI_SR] = (DI_SR_BRKINTMASK | DI_SR_TCINTMASK | DI_SR_DEINT | DI_SR_DEINTMASK);
_di_regs[DI_CVR] = 0; // clear cover int

Expand All @@ -420,6 +420,24 @@ void dvd_custom_bypass() {
return;
}

void dvd_custom_bypass_exit() {
_di_regs[DI_SR] = (DI_SR_BRKINTMASK | DI_SR_TCINTMASK | DI_SR_DEINT | DI_SR_DEINTMASK);
_di_regs[DI_CVR] = 0; // clear cover int

_di_regs[DI_CMDBUF0] = 0xDC000000;
_di_regs[DI_CMDBUF1] = FD_BYPASS_EXIT_MAGIC0;
_di_regs[DI_CMDBUF2] = FD_BYPASS_EXIT_MAGIC1;

_di_regs[DI_MAR] = 0;
_di_regs[DI_LENGTH] = 0;
_di_regs[DI_CR] = DI_CR_TSTART;

while (_di_regs[DI_CR] & DI_CR_TSTART)
; // transfer complete register

return;
}

int dvd_custom_presence(bool playing, const char *status, const char *sub_status)
{
GCN_ALIGNED(flippydrive_net_presence_t) presence = {};
Expand Down
3 changes: 2 additions & 1 deletion patches/source/flippy_sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ int dvd_custom_unlink(char *path);
int dvd_custom_unlink_flash(char *path);
int dvd_custom_open(char *path, uint8_t type, uint8_t flags);
int dvd_custom_open_flash(char *path, uint8_t type, uint8_t flags);
void dvd_custom_bypass();
void dvd_custom_bypass_enter();
void dvd_custom_bypass_exit();

int dvd_custom_presence(bool playing, const char *status, const char* sub_status);
43 changes: 43 additions & 0 deletions patches/source/gc_dvd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "time.h"

volatile unsigned long* dvd = (volatile unsigned long*)0xCC006000;

int dvd_cover_status() {
return (dvd[1] & 1); // 0: cover closed, 1: cover opened
}

int dvd_read_id()
{
dvd[0] = 0x2E;
dvd[1] = 0;
dvd[2] = 0xA8000040;
dvd[3] = 0;
dvd[4] = 0x20;
dvd[5] = 0;
dvd[6] = 0x20;
dvd[7] = 3; // enable reading!
while (dvd[7] & 1)
;
if (dvd[0] & 0x4)
return 1;
return 0;
}

unsigned int dvd_get_error(void)
{
dvd[2] = 0xE0000000;
dvd[8] = 0;
dvd[7] = 1; // IMM
while (dvd[7] & 1);
return dvd[8];
}


void dvd_reset()
{
dvd[1] = 2;
volatile unsigned long v = *(volatile unsigned long*)0xcc003024;
*(volatile unsigned long*)0xcc003024 = (v & ~4) | 1;
udelay(12);
*(volatile unsigned long*)0xcc003024 = v | 5;
}
3 changes: 3 additions & 0 deletions patches/source/gc_dvd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
void dvd_reset();
unsigned int dvd_get_error(void);
int dvd_read_id();
Loading

0 comments on commit eb14628

Please sign in to comment.