/*
 *               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.
 *
 *  Description:
 *      This file implements an utility to configure the SC
 *
 */


#include <ioslibc.h>
#include <apputils.h>

#ifdef NET_CARD
#include <sc/iostypes.h>
#include <sc/sc.h>
#endif

#ifdef NEXT_GEN
#include <rvl/iostypes.h>
#include <rvl/iop.h>
#include <iosc.h>
#endif

#include "fscmd.h"


#define STACK_SIZE                  (2*1024)
const u8        _initStack[STACK_SIZE];
const u32       _initStackSize = sizeof(_initStack);
const u32       _initPriority = 10;


/* Maximum command line size including NULL character */
#define LINE_SIZE               128

static s32
quit(char *args, char *d)
{
    printf("Quitting...\n");

    /* Free up file system resources */
    isfscmdQuit();

    exit(0);
    return (0);
}


/* Command blocks */
static Command commands[] = {
    {"cap", cap,
        "cap                    - Display flash capabilities", 0},
    {"id", id,
        "id                     - Display flash id", 0},
    {"statfs", statfs,
        "statfs                 - Get file system statistics", 0},
    {"erase", erase,
        "erase start end        - Erase the flash blocks specified", 0},
    {"cd", cd,
        "cd dirname             - Change to a directory", 0},
    {"ls", ls,
        "ls                     - List the names of files and directories", 0},
    {"cp", cp,
        "cp src_file dst_file   - Copy a file", 0},
    {"mv", mv,
        "mv src_file dst_file   - Move a file", 0},
    {"get", get,
        "get src_file dst_file  - Get a file from the flash", 0},
    {"put", put,
        "put src_file dst_file  - Put a file onto the flash", 0},
    {"dump", dump,
        "dump fname             - Dump first 1KB of the file", 0},
    {"mkdir", mkdir,
        "mkdir dirname          - Create a new directory", 0},
    {"rm", rm,
        "rm name                - Remove a file or directory", 0},
    {"stat", stat,
        "stat name              - Get file stats", 0},
    {"attr", attr,
        "attr name              - Get file/dir attributes", 0},
    {"format", format,
        "format                 - Re-format the flash", 0},
    {"writeboot", writeboot,
        "writeboot b1 b2        - Write boot1 and boot2", 0},
    {"writeboot1", writeboot1,
        "writeboot1 boot1.aes   - Write boot1", 0},
    {"writeboot2", writeboot2,
        "writeboot2 boot2.wad   - Write boot2", 0},
    {"showbad", showBad,
        "showbad                - Show a list of flash blocks marked bad", 0},
    {"markbad", markBad,
        "markbad block          - Mark the flash block as bad", 0},
    {"showbadall", showBadAll,
        "showbadall             - Show all the bad blocks in the FAT", 0},
    {"checkpage", checkPage,
        "checkpage start num    - Check the ECC of a page", 0},
    {"blocktest", blockTest,
        "blocktest block cycles - Repeatedly write to a block", 0},
    {"copytest", copyTest,
        "copytest               - Repeatedly do copy-back", 0},
    {"ecctest", eccTest,
        "ecctest page           - Simulate single-bit ECC error", 0},
    {"source",source,
        "source file            - Execute commands from file", commands},
    {"help", help,
        "help                   - Print this message", commands},
    {"quit", quit,
        "quit                   - Quit SC Monitor", 0},
    {0}
};


/*
 *  Function: main
 *
 *  Description:
 *      Entry point for CPU 0
 *
 *  Inputs:
 *      None
 *
 *  Ouputs:
 *      None
 *
 *  Return values:
 *      0 - Successful completion
 *      < 0 - Error values for regression tests
 *
 */
s32
main(void)
{
    s32         rc = 0;
    char        line[LINE_SIZE];

    printf("File System Monitor\n");

#ifdef NEXT_GEN
    u32 devId = 0;
    rc = IOSC_GetData(IOSC_DEV_ID_HANDLE, &devId);
    if (rc != IOS_ERROR_OK) {
        printf("* Unable to get device ID, rc=%d\n", rc);
    }
    printf("built: " BUILD_TIME ", device ID: %08x\n\n", devId);
#else
    printf("built: " BUILD_TIME "\n\n");
#endif

    rc = isfscmdInit();
    if (rc < 0) {
        goto main_out;
    }

    while (1) {
        printf("[FSMon] Enter command: ");
        gets(line);
        if (parseCommand(line, commands) < 0) {
            help("", (void *)commands);
        }
    }

main_out:

    return (rc);
}
