
/*
 *               Copyright (C) 2006, 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.
 * 
 *  "$Id: sdmon.c,v 1.3 2006/06/30 18:38:58 jprincen Exp $";
 *
 */


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

#include "sdi.h"

#include <sd_api.h>	/* includes sdio_api shared */	

/* Alle eer aan die Redder, ons lig in die Hemel */

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

static SDDev dev;
static SDDevHandle fd = &dev;

u32 data[1024*1024] __attribute__((aligned(4096)));   

static int writeFile(char *args, char *d);
static int readFile(char *args, char *d);
static int sdCardSize(char *args, char *d);

static Command commands[] = {
    {"help",    help,
        "help                       - Print this message", commands},
    {"read",  readFile,
        "read offset size filename  - Read from sd card data @offset into a file", 0},
    {"write", writeFile,
        "write offset filename      - Write file to sd card @offset", 0},
    {"size", sdCardSize,
        "size                       - Show sd card size", 0},
    {0},
};

/* 
 * Card Specific Data Register, size etc.
 */
int sdCardSize(char *args, char *d)
{
    s32 rc;
    u32 totalSize, totalSectors, sectorSize;

    rc = ISD_GetCardSize (fd, &totalSize, &totalSectors, &sectorSize);
    if (rc != IOS_ERROR_OK) {
    	printf("ISD_GetCardSize ERROR\n");
    } else {
    	printf("ISD_GetCardSize OK\n");
        printf("     Sector Size   = %d\n",sectorSize);
        printf("     Total Sectors = %d\n",totalSectors);
    	printf("     Card Capacity = %d\n",totalSize); 
    }   
    return rc;
}
        
/* test for multiblock async, callback to done at filesys integration */                    
int
sdReadFile(SDDevHandle fd, char *name, u32 offset, u32 length)
{
    FILE *fp = NULL;
    int count;
    IOSError rc = IOS_ERROR_OK;
    
    printf("ISD_ReadMultiBlock(offset=0x%x to data=0x%x blocks=%d)\n",
        offset,(unsigned int)data,length>>9);

    memset(data, 0, length);
    rc = ISD_ReadMultiBlockAsync (fd, offset, data, length, NULL, NULL);
    if (rc != IOS_ERROR_OK) {
        printf("SD Read Error:0x%x\n", rc);
        goto out;
    }  

    fp = fopen(name, "wb");
    if (fp == NULL) {
        printf ("%s: Can't open %s\n", __FUNCTION__, name);
        rc = -1;
        goto out;
    }

    count = fwrite(data, length, 1, fp);
    if (count != 1) {
        printf ("%s: bad write on file\n", __FUNCTION__);
        rc = -1;
        goto out;
    }

out:
    if (fp) fclose(fp);
    return rc;
}  

int
sdWriteFile(SDDevHandle fd, char* name, u32 offset)
{
    FILE *fp = NULL;
    u32 rc =0;
    u32 length;
    u32 count;
   
    fp = fopen(name, "rb");
    if (fp == NULL) {
        printf ("%s: Can't open %s\n", __FUNCTION__, name);
        rc = -1;
        goto out;
    } 

    length = flen(fp);
    printf("file size = %d\n",length);
    count = fread(data, length, 1, fp);
    if (count != 1) {
        printf ("%s: bad read on file1\n",
                __FUNCTION__);
        rc = -1;
        goto out;
    }
  
        
    /*
     * For Sd round the length UP to 512
     */
    length = (length + 511) & ~511;
    printf("ISD_WriteMultiBlock(0x%X -> blk:%d, %d)\n", data, offset, length);
    rc = ISD_WriteMultiBlockAsync (fd, offset, (u32)data, length, NULL, NULL); 
    if (rc != IOS_ERROR_OK) {
        printf("Write error:0x%x\n",rc);
        goto out;
    } 
    
out:
    if (fp) fclose(fp);
    return rc;

}

static IOSError
readFile(char *args, char *d)
{
    IOSError rc = 0;
    u32 addr;
    u32 length;
    char *ptr;

    if (getNextToken(&ptr, &args) == 0) {
        rc = IOS_ERROR_INVALID;
        goto out;
    }
    addr = asciiToInt(ptr);

    if (getNextToken(&ptr, &args) == 0) {
        rc = IOS_ERROR_INVALID;
        goto out;
    }

    length = (asciiToInt(ptr) + 511) & (~511);
    if (getNextToken(&ptr, &args) == 0) {
        rc = IOS_ERROR_INVALID;
        goto out;
    }
    printf("Filename %s\n", ptr);

    sdReadFile(fd, ptr, addr, length);

out:
    return rc;

}

static IOSError
writeFile(char *args, char *d)
{
    IOSError rc = IOS_ERROR_OK;
    u32 addr;
    char *ptr;

    /* Get address on card to write to */
    if (getNextToken(&ptr, &args) == 0) {
        rc = IOS_ERROR_INVALID;
        goto out;
    }
    addr = asciiToInt(ptr);

    if (getNextToken(&ptr, &args) == 0) {
        rc = IOS_ERROR_INVALID;
        goto out;
    }

    printf("Filename %s\n", ptr);
    sdWriteFile(fd, ptr, addr); 

out:

    if (rc != IOS_ERROR_OK) {
        printf("write file failed, rc=%d\n", rc);
    }

    return (rc);
}

#define LINE_SIZE 256

void
main(void)
{
    int rc;
    char line[LINE_SIZE];
    fd->SDDevSlot = 0;
    fd->SDDevFd = -1;

    printf("sdmon [");
    printf("built: " BUILD_TIME "]\n\n");

    rc = ISD_InitCard();
    rc = ISD_MountCard (fd->SDDevSlot,&fd); /* open */
    rc = ISD_ResetDevice(fd);  

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