i32BootFileLength);

        //
        // Append the application to the boot loader image.
        //
        if(fread(&pui8FileBuffer[ui32Address], 1, ui32FileLength, hFile) !=
            ui32FileLength)
        {
            return(-1);
        }
    }
    else
    {
        //
        // Just read in the full application since there is not boot loader.
        //
        if(fread(pui8FileBuffer, 1, ui32TransferLength, hFile) != ui32TransferLength)
        {
            return(-1);
        }
    }

    //
    // Build up the download command and send it to the board.
    //
    g_pui8Buffer[0] = COMMAND_DOWNLOAD;
    g_pui8Buffer[1] = (uint8_t)(ui32TransferStart >> 24);
    g_pui8Buffer[2] = (uint8_t)(ui32TransferStart >> 16);
    g_pui8Buffer[3] = (uint8_t)(ui32TransferStart >> 8);
    g_pui8Buffer[4] = (uint8_t)ui32TransferStart;
    g_pui8Buffer[5] = (uint8_t)(ui32TransferLength>>24);
    g_pui8Buffer[6] = (uint8_t)(ui32TransferLength>>16);
    g_pui8Buffer[7] = (uint8_t)(ui32TransferLength>>8);
    g_pui8Buffer[8] = (uint8_t)ui32TransferLength;
    if(SendCommand(g_pui8Buffer, 9) < 0)
    {
        printf("Failed to Send Download Command\n");
        return(-1);
    }

    ui32Offset = 0;

    printf("Remaining Bytes: ");
    do
    {
        uint8_t ui8BytesSent;

        g_pui8Buffer[0] = COMMAND_SEND_DATA;

        printf("%08ld", ui32TransferLength);

        //
        // Send out 8 bytes at a time to throttle download rate and avoid
        // overruning the device since it is programming flash on the fly.
        //
        if(ui32TransferLength >= g_ui32DataSize)
        {
            memcpy(&g_pui8Buffer[1], &pui8FileBuffer[ui32Offset], g_ui32DataSize);

            ui32Offset += g_ui32DataSize;
            ui32TransferLength -= g_ui32DataSize;
            ui8BytesSent = g_ui32DataSize + 1;
        }
        else
        {
            memcpy(&g_pui8Buffer[1], &pui8FileBuffer[ui32Offset], ui32TransferLength);
            ui32Offset += ui32TransferLength;
            ui8BytesSent = ui32TransferLength + 1;
            ui32TransferLength = 0;
        }
        //
        // Send the Send Data command to the device.
        //
        if(SendCommand(g_pui8Buffer, ui8BytesSent) < 0)
        {
            printf("Failed to Send Packet data\n");
            break;
        }

        printf("\b\b\b\b\b\b\b\b");
    } while (ui32TransferLength);
    printf("00000000\n");

    if(pui8FileBuffer)
    {
        free(pui8FileBuffer);
    }
    return(0);
}

//*****************************************************************************
//
//! CheckArgs() ensures that the arguments passed in are correct.
//!
//! This routine makes sure that the combination of command line parameters
//! that were passed into the program are valid.  If only a Boot Loader binary
//! was specified then the parameters will be modified to be application update
//! update at address zero.  This step is done to simplfy logic later in the
//! update program.
//!
//! \return This function either returns a negative value indicating a failure
//!     or zero if the update was successful.
//
//*****************************************************************************
int32_t
CheckArgs(void)
{
    //
    // No file names specified.
    //
    if((g_pcFilename == 0) && (g_pcBootLoadName == 0))
    {
        printf("ERROR: no file names specified.\n");
        return(-1);
    }

    //
    // Both filenames are specified but the address is zero.
    //
    if((g_pcFilename != 0) && (g_pcBootLoadName != 0) &&
        (g_ui32DownloadAddress == 0))
    {
        printf(
        "ERROR: Download address cannot be zero and specify a boot loader \n"
        "binary as well as an application binary\n");
        return(-1);
    }

    //
    // If only a boot loader was specified then set the address to 0 and
    // specify only one file to download.
    //
    if((g_pcBootLoadName != 0) && (g_pcFilename == 0))
    {
        g_pcFilename = g_pcBootLoadName;
        g_pcBootLoadName = 0;
        g_ui32DownloadAddress = 0;
    }
    return(0);
}

//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************
