viragedump.c 8.61 KB
#include <PR/bcp.h>
#include <PR/R4300.h>
#include <PR/bbsim.h>
#include <PR/bbskapi.h>
#include <PR/bbvirage.h>
#include <PR/os.h>
#include <PR/os_bbfs.h>
#include <PR/os_bbatb.h>
#include <PR/os_bbexec.h>
#include <sha1.h>

#include "libfb.h"
#include "viragedump.h"


/*
 * buffer for FS must be dcache aligned. use macro below.
 */
#define ALIGN_DCACHE __attribute__((aligned(DCACHE_LINESIZE)))

/*
 * debug print support
 */

#undef PRINTF

#ifdef DEBUG
#define	PRINTF	osSyncPrintf
#else
#define	PRINTF(format, args...)
#endif



/*
 * Thread and stack structures
 */

char   bootStack[STACKSIZE] __attribute__ ((aligned (8)));

static OSThread idleThread;
static char     idleThreadStack[STACKSIZE] __attribute__ ((aligned (8)));

static OSThread mainThread;
static char     mainThreadStack[STACKSIZE] __attribute__ ((aligned (8)));

/* 
 * FS specific definitions
 */

static void	idleproc(char *);
static void	mainproc(char *);



#define DMA_QUEUE_SIZE	200

static OSMesg           PiMessages[DMA_QUEUE_SIZE];
static OSMesgQueue      PiMessageQ;

static OSMesg           SiMessages[DMA_QUEUE_SIZE];
static OSMesgQueue      SiMessageQ, contMessageQ;


/*
 * simple graphics
 */
static OSMesgQueue      retraceMessageQ;
static OSMesg           dummyMessage, retraceMessageBuf;

#if SCREEN_LOW
#define WD	320
#define HT	240
#else
#define WD	640
#define HT	480
#endif
static u16 cfb[WD*HT] __attribute__((aligned(64)));


void  __osBbVideoPllInit(u32);
void boot(u32 miSecModeRegVal)
{
    /* Init the video PLL */
    __osBbVideoPllInit(OS_TV_NTSC);

    osInitialize();

    osCreateThread(&idleThread, 1, (void(*)(void *))idleproc, (void *)0,
                   idleThreadStack+STACKSIZE, 8);
    osStartThread(&idleThread);
}


static void
idleproc(char *argv)		/* priority 8 */
{
    osCreateViManager(OS_PRIORITY_VIMGR);
#if	SCREEN_LOW
    osViSetMode(&osViModeTable[OS_VI_NTSC_LPN1]);
#else
    osViSetMode(&osViModeTable[OS_VI_NTSC_HPF1]);
#endif

    /*
     * Start PI Mgr for access to cartridge - start before the debugger
     */
    osCreatePiManager((OSPri) OS_PRIORITY_PIMGR, &PiMessageQ, PiMessages,
            DMA_QUEUE_SIZE);

    osCreateMesgQueue(&SiMessageQ, SiMessages, DMA_QUEUE_SIZE);
    osSetEventMesg(OS_EVENT_SI, &SiMessageQ, (OSMesg)DMA_QUEUE_SIZE);

    /*
     * The main thread's priority must be the same or lower than the original
     * idle's thread priority. This allows the idle thread to change its
     * priority to 0 before the main thread starts execution.
     */
    osCreateThread(&mainThread, 3, (void(*)(void *))mainproc, argv,
           mainThreadStack+STACKSIZE/8, (OSPri)7);
    osStartThread(&mainThread);

    osSetThreadPri(0, OS_PRIORITY_IDLE);

    for(;;) {
#if 0
	osBbSetErrorLed(1);
	delay();
	osBbSetErrorLed(0);
	delay();
#endif
    }
}

struct virage {
    BbVirage2 v2;
    BbVirage01 v1;
    BbVirage01 v0;
} virage;

static void
dump_v2(BbVirage2* v2) {
    char buf[256];
osSyncPrintf("dump 2\n");
    fbClear();
    sprintf(buf, "BBID = %08x",(unsigned int) v2->bbId);
    fbPrintStr(fbWhite, 3, 5, buf);


    sprintf(buf, "skhash = %08x %08x %08x %08x %08x", (unsigned int) v2->skHash[0], 
	    (unsigned int) v2->skHash[1], (unsigned int) v2->skHash[2],
	    (unsigned int) v2->skHash[3], (unsigned int) v2->skHash[4]);
    fbPrintStr(fbWhite, 3, 7, buf);

    sprintf(buf, "pvtkey = %08x %08x %08x %08x %08x", 
	    (unsigned int)v2->privateKey[0], 
	    (unsigned int)v2->privateKey[1], 
	    (unsigned int)v2->privateKey[2],
	    (unsigned int)v2->privateKey[3],
	    (unsigned int)v2->privateKey[4]);
    fbPrintStr(fbWhite, 3, 9, buf);

    sprintf(buf, "pvtkey = %08x %08x %08x ", 
	    (unsigned int)v2->privateKey[5], 
	    (unsigned int)v2->privateKey[6], 
	    (unsigned int)v2->privateKey[7]);
    fbPrintStr(fbWhite, 3, 11, buf);

    sprintf(buf, "reenc = %08x %08x %08x %08x", 
	    (unsigned int)v2->recryptListKey[0], 
	    (unsigned int)v2->recryptListKey[1],
	    (unsigned int)v2->recryptListKey[2],
	    (unsigned int)v2->recryptListKey[3]);
    fbPrintStr(fbWhite, 3, 13, buf);

    
    sprintf(buf, "publickey = %08x %08x %08x %08x %08x", 
	    (unsigned int)v2->publicKey[0], 
	    (unsigned int)v2->publicKey[1], 
	    (unsigned int)v2->publicKey[2],
	    (unsigned int)v2->publicKey[3],
	    (unsigned int)v2->publicKey[4]);
    fbPrintStr(fbWhite, 3, 15, buf);

    sprintf(buf, "publickey = %08x %08x %08x ", 
	    (unsigned int)v2->publicKey[5], 
	    (unsigned int)v2->publicKey[6], 
	    (unsigned int)v2->publicKey[7]);
    fbPrintStr(fbWhite, 3, 16, buf);
   
    sprintf(buf, "publickey = %08x %08x %08x %08x %08x", 
	    (unsigned int)v2->publicKey[8], 
	    (unsigned int)v2->publicKey[9], 
	    (unsigned int)v2->publicKey[10],
	    (unsigned int)v2->publicKey[11],
	    (unsigned int)v2->publicKey[12]);
    fbPrintStr(fbWhite, 3, 17, buf);

    sprintf(buf, "publickey = %08x %08x %08x ", 
	    (unsigned int)v2->publicKey[13], 
	    (unsigned int)v2->publicKey[14], 
	    (unsigned int)v2->publicKey[15]);
    fbPrintStr(fbWhite, 3, 18, buf);


    sprintf(buf, "bootapp key = %08x %08x %08x %08x", 
	    (unsigned int)v2->bootAppKey[0], 
	    (unsigned int)v2->bootAppKey[1],
	    (unsigned int)v2->bootAppKey[2],
	    (unsigned int)v2->bootAppKey[3]);
    fbPrintStr(fbWhite, 3, 20, buf);


    sprintf(buf, "checksum = %08x",(unsigned int) v2->csumAdjust);
    fbPrintStr(fbWhite, 3, 22, buf);


    sprintf(buf, "jtag enable = %08x",(unsigned int) v2->jtagEnable);
    fbPrintStr(fbWhite, 3, 24, buf);
    osWritebackDCacheAll();
}

static void
dump_v01(BbVirage01* v01) {
    int i;
osSyncPrintf("dump 01\n");
    fbClear();
    fbPrintf(fbWhite, 6, 5, "tsCrlVersion %02x\n", v01->tsCrlVersion);
    fbPrintf(fbWhite, 6, 6, "caCrlVersion %02x\n", v01->caCrlVersion);
    fbPrintf(fbWhite, 6, 7, "cpCrlVersion %02x\n", v01->cpCrlVersion);
    fbPrintf(fbWhite, 6, 8, "contentRlVersion %02x\n", v01->contentRlVersion);
    fbPrintf(fbWhite, 6, 9, "ticketRlVersion %04x\n", v01->ticketRlVersion);
    fbPrintf(fbWhite, 6, 10, "tidWindow %04x\n", v01->tidWindow);
    for(i = 0; i < BB_MAX_CC/2; i++)
	fbPrintf(fbWhite, 6, 11+i, "%04x  %04x\n", v01->cc[i], v01->cc[BB_MAX_CC/2+i]);
    fbPrintf(fbWhite, 6, 24, "seq %04x\n", v01->seq);
    fbPrintf(fbWhite, 6, 25, "sum %04x\n", v01->sum);
    osWritebackDCacheAll();
}

OSContStatus statusData[MAXCONTROLLERS];
OSContPad controllerData[MAXCONTROLLERS];
static u16 lastbutton[MAXCONTROLLERS];

static int no_controller = 1;
static void
initController(void)
{
    int i;
    u8 pattern;

    osCreateMesgQueue(&contMessageQ, &dummyMessage, 1);
    osSetEventMesg(OS_EVENT_SI, &contMessageQ, (OSMesg) 0);

    osContInit(&contMessageQ, &pattern, &statusData[0]);

    for (i = 0; i < MAXCONTROLLERS; i++) {
        if ((pattern & (1 << i)) &&
            !(statusData[i].errno & CONT_NO_RESPONSE_ERROR)) {
            no_controller = 0;
        }
    }
}

static void
processButton(u16 button, int lastbutton)
{
    if (button & D_JPAD && !(lastbutton & D_JPAD)) {
    }

    if (button & U_JPAD && !(lastbutton & U_JPAD)) {
    }

    if (button & R_JPAD && !(lastbutton & R_JPAD)) {
    }

    if (button & L_JPAD && !(lastbutton & L_JPAD)) {
    }

    if (button & B_BUTTON && !(lastbutton & B_BUTTON)) {
    }

    if (button & A_BUTTON && !(lastbutton & A_BUTTON)) {
	dump_v2(&virage.v2);
    }

    if (button & START_BUTTON && !(lastbutton & START_BUTTON)) {
	skExit();
    }

    if ((button & Z_TRIG) && !(lastbutton & Z_TRIG)) {
    }

    if ((button & L_TRIG) && !(lastbutton & L_TRIG)) {
	dump_v01(&virage.v0);
	return;
    }

    if ((button & R_TRIG) && !(lastbutton & R_TRIG)) {
	dump_v01(&virage.v1);
	return;
    }
}

static void
readControllers(void)
{
    int i;
    OSContPad *pad;

    osRecvMesg(&contMessageQ, &dummyMessage, OS_MESG_BLOCK); 
    osContGetReadData(controllerData);

    for (i = 0; i < MAXCONTROLLERS; ++i) {
        pad = &controllerData[i];
        if (pad->button) {
            processButton(pad->button, lastbutton[i]);
        }
        lastbutton[i] = pad->button;
    }
}

void mainproc(char *argv)
{
    
#if SCREEN_LOW
    fbInit(FB_LOW_RES);
#else
    fbInit(FB_HIGH_RES);
#endif
    /* setup text output to screen */
    osCreateMesgQueue(&retraceMessageQ, &retraceMessageBuf, 1);
    osViSetEvent(&retraceMessageQ, dummyMessage, 1);
    osViBlack(1);
    osViSwapBuffer(cfb);
    fbSetBg(fbBlack);
    fbClear();
    osViBlack(0);
    osWritebackDCacheAll();
    osViSwapBuffer(cfb);
    initController();

    fbPrintStr(fbWhite, 3, 3, "Dump Virage App");
    osWritebackDCacheAll();

#if 1
    skDumpVirage((u8 *) &virage);
#endif
    dump_v2(&virage.v2);
    for(;;) {
	osRecvMesg(&retraceMessageQ, NULL, OS_MESG_BLOCK);
	osContStartReadData(&contMessageQ);
	readControllers();
    }
    
}