/*
 *               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 common utility functions.
 */
#include <iostypes.h>
#include <ioslibc.h>
#include <elf.h>

u32
bindump(char *name, u32 addr, u32 len)
{
    FILE *fp = 0;
    u32 i = -1;
    u32 len1;
    u8 buf[128];
 
    if ( (NULL == name) || (0 == (fp = fopen(name, "wb")))) {
        printf("fopen: unable to open file '%s' in write mode\n", name);
        goto out;
    }
    len1 = len - (len %128);
    for (i = 0; i < len1; i += 128) {
        memcpy(buf, (u8*)(addr + i), 128);
        fwrite(buf, 128, 1, fp);
        printf("wrote block %d\n", i/128);
    }
    if ( (len1 = len%128) > 0 ) {
        memcpy(buf, (u8 *)(addr + len - len1), len1);
        printf("writing %d bytes\n", len1);
        fwrite(buf, len1, 1, fp);
    }
out:
    if (fp) fclose(fp);
    return i;
}
