/*
 *               Copyright (C) 2005, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  and  are protected by Federal copyright law. They may not be disclosed
 *  to  third  parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 */
#include <iostypes.h>
#include <iop.h>
#include <ios.h>
#include <ioslibc.h>
#include <apputils.h>
#include <elf.h>
#include <sys.h>
#include <hw.h>
#include <ahb_fdl_defs.h>


/* Read instructions from bin file, if specified */
void
__initBootPPC(enum sysMode_e mode, char *fname)
{
    FILE *fp = 0;
    u32 bootcodeSize = 0; 
    u32 bootcode[16] = { 0x60000000, /* nop */
                         0x60000000,
                         0x60000000,
                         0x60000000,
                         0x60000000,
                         0x60000000,
                         0x60000000,
                         0x60000000,
                         0x60000000,
                         0x60000000,
                         0x60000000,
                         0x60000000,
                         0x60000000,
                         0x60000000,
                         0x60000000,
                         0x60000000
                      };
   
    if ((NULL == fname) || (0 == (fp = fopen(fname, "rb")))) {
        if ( fname != NULL && fname[0] != '\0' )
            printf("fopen: Unable to open file: %s\n", fname);
        printf("Will load default instructions\n");
    }
    else {
        fread(bootcode, sizeof(bootcode), 1, fp);
        bootcodeSize = 16; /* 16 instructions */
    }
    /* XXX horti: Enable any permissions PPC needs */
//    enablePPCAccessCtrls();
  
    /* bootcodeSize == 0 for default bootcode */
    setupEXIRegBoot(mode, bootcode, bootcodeSize); 
}

int 
initBootPPC(char *args, char *d)
{
    char *name = NULL;
    printf("Initializing enhanced mode...\n");
    initSysMode(enhancedmode);
    if (getNextToken(&name, &args)) 
        printf("Loading EXI boot registers from file: %s\n", name);
    __initBootPPC(enhancedmode, name);
    return 0;
}

int
m_bootPPC(char *argc, char *d)
{
    bootPPC();
    return 0;
}

int
m_refreshNapaLoop(char *argc, char *d)
{
    refreshNapa();
    return 0;
}

int
m_loadBootPPC(char*args, char*d)
{
    if ( -1 != m_binload(args,d)) {
        bootPPC();
        refreshNapa();
    }
    return 0;
}
