/*
 *               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 <stdio.h>
#include <stdlib.h>
#include <fs.h>


/* FAT structure */
typedef struct {
    u8          magic[4];
    u32         seq;
    u32         gVersion;
    u8          arrays[0];
} ISFSFat;

/* Block entry */
typedef u16     ISFSBlockEntry;

/* Inode entry */
typedef struct {
    u8      name[ISFS_INODE_NAMELEN];
    u8      typeAcc;
    u8      attr;
    union attr1 {
        ISFSBlockEntry  block;
        u16             child;
    } __attribute__((packed)) addr;
    u16     nextSibling;
    u32     size;
    IOSUid  owner;
    IOSGid  group;
    u32     version;
} __attribute__((packed)) ISFSInodeEntry;


/* Maximum FAT size supported */
#define MAX_FAT_SIZE        (256*1024)

/* Number of FS block entries for the specified flash size */
#define ISFS_BLOCK_ENTRIES(fl)      ((fl) / ISFS_BLOCK_SIZE)

/* Number of inode entries for the specified FAT and flash size */
#define ISFS_INODE_ENTRIES(fat, fl) (((fat) -                               \
                                        (sizeof (ISFSBlockEntry) *          \
                                                ISFS_BLOCK_ENTRIES(fl)) -   \
                                        sizeof (ISFSFat)) /                 \
                                    sizeof (ISFSInodeEntry))

#define ISFS_FAT_BLOCK_ARRAY(fatp)  ((ISFSBlockEntry *)&(fatp)->arrays[0])

/* Inode array */
#define ISFS_FAT_INODE_ARRAY(fatp, fl)                                      \
        ((ISFSInodeEntry *)&(fatp)->arrays[sizeof (ISFSBlockEntry) *        \
                                        ISFS_BLOCK_ENTRIES(fl)])

/* Top 4 bits of the type field is used for access or file attribute */
#define ISFS_INODE_TYPE_MASK    0x3
#define ISFS_INODE_TYPE_INVALID 0       /* Un-initialized */
#define ISFS_INODE_TYPE_FILE    1       /* Inode is a file */
#define ISFS_INODE_TYPE_DIR     2       /* Inode is a directory */

/* Macros for differentiating inode types */
#define ISFS_INODE_TYPE_ISFILE(typeAcc)                                       \
    ((typeAcc & ISFS_INODE_TYPE_MASK) == ISFS_INODE_TYPE_FILE)
#define ISFS_INODE_TYPE_ISDIR(typeAcc)                                        \
    ((typeAcc & ISFS_INODE_TYPE_MASK) == ISFS_INODE_TYPE_DIR)
#define ISFS_INODE_TYPE_ISINVALID(typeAcc)                                    \
    ((typeAcc & ISFS_INODE_TYPE_MASK) == ISFS_INODE_TYPE_INVALID)

/* Max inode name length  */
#define ISFS_INODE_NAMELEN     12

/* Block entry state */
#define ISFS_BLOCK_AVAIL_CLEAN 0xFFFF  /* Available clean FS block */
#define ISFS_BLOCK_AVAIL_DIRTY 0xFFFE  /* Available dirty FS block */
#define ISFS_BLOCK_BAD         0xFFFD  /* Flash block is bad */
#define ISFS_BLOCK_RESERVED    0xFFFC  /* SA, FAT, or replacement reserve */
#define ISFS_BLOCK_LAST        0xFFFB  /* Last FS block in file */
#define ISFS_BLOCK_VALID(bl)   (bl <= ISFS_BLOCK_LAST)  /* Data blocks */



/* Buffer used to read in the FAT */
static u8 buf[MAX_FAT_SIZE];


int
main(int argc, char *argv[])
{
    int rc = 0;
    FILE *fp;
    unsigned int fatSz, flashSz, count, numInodes;
    ISFSFat *fatp = (ISFSFat *) buf;

    if (argc < 4) {
        printf("Usage: %s <file> <fatSz> <flashSz>\n", argv[0]);
        goto main_out;
    }

    /* Open the input file */
    fp = fopen(argv[1], "r");
    if (fp == NULL) {
        printf("Failed to open input file\n");
        rc = -1;
        goto main_out;
    }

    /* Get the sizes */
    fatSz = atoi(argv[2]);
    if (fatSz > MAX_FAT_SIZE) {
        printf("FAT size too large, please increase the buffer size\n");
        rc = -1;
        goto main_out;
    }
    flashSz = atoi(argv[3]);
    numInodes = ISFS_INODE_ENTRIES(fatSz, flashSz);

    /* Read in the FAT */
    rc = fread(fatp, 1, fatSz, fp);
    if (rc != fatSz) {
        printf("Failed to read in the entire FAT\n");
        rc = -2;
        goto main_out;
    }
 
    /* Print the FAT header */
    printf("Magic: %c%c%c%c\n", fatp->magic[0], fatp->magic[1],
        fatp->magic[2], fatp->magic[3]);
    printf("Seq: %d\n", fatp->seq);
    printf("Version: %d\n", fatp->gVersion);

    /* Print out a list of files and directories */
    for (count = 0; count < numInodes; count++) {
        ISFSInodeEntry *inode_p =
                            &(ISFS_FAT_INODE_ARRAY(fatp, flashSz)[count]);
        u32 charCount;

        if (ISFS_INODE_TYPE_ISINVALID(inode_p->typeAcc)) {
            continue;
        }

        printf("Inode entry: %08d    Name:", count);
        for (charCount = 0; charCount < ISFS_INODE_NAMELEN; charCount++) {
            printf("%c", inode_p->name[charCount]);
        }
        if (ISFS_INODE_TYPE_ISFILE(inode_p->typeAcc)) {
            printf("    (file)\n");
        } else {
            printf("    (dir)\n");
        }
        printf("Addr:  %08d    Sibling: %08d    Size:    %08d\n",
            inode_p->addr.block, inode_p->nextSibling, inode_p->size);
        printf("Owner: %08d    Group:   %08d    Version: %08d\n",
            inode_p->owner, inode_p->group, inode_p->version);

        /* Print file block list */
        if (ISFS_INODE_TYPE_ISFILE(inode_p->typeAcc)) {
            u32     block = inode_p->addr.block;

            printf("File block list: %08d ", block);
            while (block !=  ISFS_BLOCK_LAST) {
                if (!ISFS_BLOCK_VALID(block)) {
                    printf("WARNING: Found corrupted file %d\n", block);
                    break;
                }

                block = ISFS_FAT_BLOCK_ARRAY(fatp)[block];
                printf("%08d ", block);
            }
            printf("\n");
        }
    }
    printf("\n");

    /* Print out a list of clean, dirty, bad, and reserved blocks */
    printf("The following are bad blocks:\n");
    for (count = 0; count < ISFS_BLOCK_ENTRIES(flashSz); count++) {
        if (ISFS_FAT_BLOCK_ARRAY(fatp)[count] == ISFS_BLOCK_BAD) {
            printf("%08d\n", count);
        }
    }
    printf("\n");
    printf("The following are reserved blocks:\n");
    for (count = 0; count < ISFS_BLOCK_ENTRIES(flashSz); count++) {
        if (ISFS_FAT_BLOCK_ARRAY(fatp)[count] == ISFS_BLOCK_RESERVED) {
            printf("%08d\n", count);
        }
    }
    printf("\n");
    printf("The following are dirty blocks:\n");
    for (count = 0; count < ISFS_BLOCK_ENTRIES(flashSz); count++) {
        if (ISFS_FAT_BLOCK_ARRAY(fatp)[count] == ISFS_BLOCK_AVAIL_DIRTY) {
            printf("%08d\n", count);
        }
    }
    printf("\n");
    printf("The following are clean blocks:\n");
    for (count = 0; count < ISFS_BLOCK_ENTRIES(flashSz); count++) {
        if (ISFS_FAT_BLOCK_ARRAY(fatp)[count] == ISFS_BLOCK_AVAIL_CLEAN) {
            printf("%08d\n", count);
        }
    }
    printf("\n");

main_out:

    return (rc);
}
