Structure to hold command parameters
Example #1
0
        /// <summary>
        /// Main entry point of application
        /// </summary>
        /// <param name="args">Array of command-line arguments</param>
        /// <returns>Return code: 0 for correct exit, -1 for unexpected exit</returns>
        static Int32 Main(String[] args)
        {
            // Begin main code
            Console.Clear();
            Console.WriteLine("-----------------------------------------------------");
            Console.WriteLine("   TI DVFlasher Host Program for DM644x              ");
            Console.WriteLine("   (C) 2007, Texas Instruments, Inc.                 ");
            Console.WriteLine("-----------------------------------------------------");
            Console.Write("\n\n");

            // Parse command line
            cmdParams = ParseCmdLine(args);
            if (!cmdParams.Valid)
            {
                DispHelp();
                return -1;
            }
            else
            {
                Console.Write(cmdString + "\n\n\n");
            }

            try
            {
                Console.WriteLine("Attempting to connect to device " + cmdParams.SerialPortName + "...");
                MySP = new SerialPort(cmdParams.SerialPortName, 115200, Parity.None, 8, StopBits.One);
                MySP.Encoding = Encoding.ASCII;
                MySP.Open();
            }
            catch(Exception e)
            {
                if (e is UnauthorizedAccessException)
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine("This application failed to open the COM port.");
                    Console.WriteLine("Most likely it is in use by some other application.");
                    return -1;
                }
                Console.WriteLine(e.Message);
                return -1;
            }

            Console.WriteLine("Press any key to end this program at any time.\n");

            // Setup the thread that will actually do all the work of interfacing to
            // the DM644x boot ROM.  Start that thread.
            workerThread = new Thread(new ThreadStart(Program.WorkerThreadStart));
            workerThread.Start();

            // Wait for a key to terminate the program
            while ((workerThread.IsAlive) && (!Console.KeyAvailable))
            {
                Thread.Sleep(1000);
            }

            // If a key is pressed then abort the worker thread and close the serial port
            try
            {
                if (Console.KeyAvailable)
                {
                    Console.ReadKey();
                    Console.WriteLine("Aborting program...");
                    workerThread.Abort();
                }
                else if (workerThread.IsAlive)
                {
                    Console.WriteLine("Aborting program...");
                    workerThread.Abort();
                }

                while ((workerThread.ThreadState & ThreadState.Stopped) != ThreadState.Stopped){}
            }
            catch (Exception e)
            {
                Console.WriteLine("Abort thread error...");
                Console.WriteLine(e.GetType());
                Console.WriteLine(e.Message);
            }

            if (workerThreadSucceeded)
            {
                Console.WriteLine("\nOperation completed successfully.");
                return 0;
            }
            else
            {
                Console.WriteLine("\n\nInterfacing to the DVEVM via UART failed." +
                    "\nPlease reset or power-cycle the board and try again...");
                return -1;
            }
        }
Example #2
0
        /// <summary>
        /// Parse the command line into the appropriate internal command structure
        /// </summary>
        /// <param name="args">The array of strings passed to the command line.</param>
        public static ProgramCmdParams ParseCmdLine(String[] args)
        {
            ProgramCmdParams myCmdParams =  new ProgramCmdParams();
            Boolean[] argsHandled = new Boolean[args.Length];
            Int32 numFiles = -1;
            UInt32 numUnhandledArgs,numHandledArgs=0;
            String s;

            if (args.Length == 0)
            {
                myCmdParams.Valid = false;
                return myCmdParams;
            }

            // Initialize array of handled argument booleans to false
            for (int i = 0; i < argsHandled.Length; i++ )
                argsHandled[i] = false;

            // Set Defualts for application
            myCmdParams.UBLFlashType = FlashType.NONE;

            myCmdParams.CMDMagicFlag = MagicFlags.MAGIC_NUMBER_INVALID;
            myCmdParams.Valid = true;
            myCmdParams.Verbose = false;
            myCmdParams.SerialPortName = null;
            myCmdParams.useEmbeddedUBL = true;

            myCmdParams.UARTUBLExecAddr = 0x0100;
            myCmdParams.UARTUBLUsed = true;

            myCmdParams.NORUBLExecAddr = 0x0100;
            myCmdParams.NANDUBLExecAddr = 0x0100;

            myCmdParams.APPMagicFlag = MagicFlags.UBL_MAGIC_SAFE;
            myCmdParams.APPFileName = null;
            myCmdParams.APPLoadAddr = 0xFFFFFFFF;
            myCmdParams.APPEntryPoint = 0xFFFFFFFF;

            myCmdParams.FLASHUBLMagicFlag = MagicFlags.UBL_MAGIC_SAFE;
            myCmdParams.FLASHUBLFileName = null;
            myCmdParams.FLASHUBLLoadAddr = 0xFFFFFFFF;
            myCmdParams.FLASHUBLExecAddr = 0x0100;

            // For loop for all dash options
            for(int i = 0; i<args.Length; i++)
            {
                s = args[i];
                if (s.StartsWith("-"))
                {
                    switch (s.Substring(1).ToLower())
                    {
                        case "fnorbin":
                            if (myCmdParams.CMDMagicFlag == MagicFlags.MAGIC_NUMBER_INVALID)
                                myCmdParams.CMDMagicFlag = MagicFlags.UBL_MAGIC_NOR_BIN_BURN;
                            else
                                myCmdParams.Valid = false;
                            numFiles = 2;
                            myCmdParams.UBLFlashType = FlashType.NOR;
                            cmdString = "Flashing NOR with ";
                            break;
                        case "fnorsrec":
                            if (myCmdParams.CMDMagicFlag == MagicFlags.MAGIC_NUMBER_INVALID)
                                myCmdParams.CMDMagicFlag = MagicFlags.UBL_MAGIC_NOR_SREC_BURN;
                            else
                                myCmdParams.Valid = false;
                            numFiles = 2;
                            myCmdParams.UBLFlashType = FlashType.NOR;
                            cmdString = "Flashing NOR with ";
                            break;
                        case "fnandbin":
                            if (myCmdParams.CMDMagicFlag == MagicFlags.MAGIC_NUMBER_INVALID)
                                myCmdParams.CMDMagicFlag = MagicFlags.UBL_MAGIC_NAND_BIN_BURN;
                            else
                                myCmdParams.Valid = false;
                            numFiles = 2;
                            myCmdParams.UBLFlashType = FlashType.NAND;
                            cmdString = "Flashing NAND with ";
                            break;
                        case "fnandsrec":
                            if (myCmdParams.CMDMagicFlag == MagicFlags.MAGIC_NUMBER_INVALID)
                                myCmdParams.CMDMagicFlag = MagicFlags.UBL_MAGIC_NAND_SREC_BURN;
                            else
                                myCmdParams.Valid = false;
                            numFiles = 2;
                            myCmdParams.UBLFlashType = FlashType.NAND;
                            cmdString = "Flashing NAND with ";
                            break;
                        case "enor":
                            if (myCmdParams.CMDMagicFlag == MagicFlags.MAGIC_NUMBER_INVALID)
                                myCmdParams.CMDMagicFlag = MagicFlags.UBL_MAGIC_NOR_GLOBAL_ERASE;
                            else
                                myCmdParams.Valid = false;
                            numFiles = 0;
                            myCmdParams.UBLFlashType = FlashType.NOR;
                            cmdString = "Globally erasing NOR flash.";
                            break;
                        case "enand":
                            if (myCmdParams.CMDMagicFlag == MagicFlags.MAGIC_NUMBER_INVALID)
                                myCmdParams.CMDMagicFlag = MagicFlags.UBL_MAGIC_NAND_GLOBAL_ERASE;
                            else
                                myCmdParams.Valid = false;
                            numFiles = 0;
                            myCmdParams.UBLFlashType = FlashType.NAND;
                            cmdString = "Globally erasing NAND flash.";
                            break;
                        case "r":
                            if (myCmdParams.CMDMagicFlag == MagicFlags.MAGIC_NUMBER_INVALID)
                                myCmdParams.CMDMagicFlag = MagicFlags.UBL_MAGIC_NOR_RESTORE;
                            else
                                myCmdParams.Valid = false;
                            numFiles = 1;
                            myCmdParams.UBLFlashType = FlashType.NOR;
                            cmdString = "Restoring NOR flash with ";
                            break;
                        case "b":
                            if (myCmdParams.CMDMagicFlag == MagicFlags.MAGIC_NUMBER_INVALID)
                                myCmdParams.CMDMagicFlag = MagicFlags.UBL_MAGIC_SAFE;
                            else
                                myCmdParams.Valid = false;
                            numFiles = 1;
                            myCmdParams.UBLFlashType = FlashType.NOR;
                            cmdString = "Sending and running application found in ";
                            break;
                        case "s":
                            if (args[i + 1].Contains("0x"))
                                myCmdParams.APPEntryPoint = UInt32.Parse(args[i + 1].Replace("0x", ""), NumberStyles.AllowHexSpecifier);
                            else if (args[i + 1].Contains("0X"))
                                myCmdParams.APPEntryPoint = UInt32.Parse(args[i + 1].Replace("0X", ""), NumberStyles.AllowHexSpecifier);
                            else
                                myCmdParams.APPEntryPoint = UInt32.Parse(args[i + 1], NumberStyles.AllowHexSpecifier);
                            Console.WriteLine("Using {0:x8} as the execution address.", myCmdParams.APPEntryPoint);
                            argsHandled[i+1] = true;
                            numHandledArgs++;
                            break;
                        case "p":
                            myCmdParams.SerialPortName = args[i + 1];
                            argsHandled[i + 1] = true;
                            numHandledArgs++;
                            break;
                        case "norbl":
                            myCmdParams.UARTUBLUsed = false;
                            break;
                        case "usemyubl":
                            myCmdParams.useEmbeddedUBL = false;
                            break;
                        case "v":
                            myCmdParams.Verbose = true;
                            break;
                        default:
                            myCmdParams.Valid = false;
                            break;
                    }
                    argsHandled[i] = true;
                    numHandledArgs++;
                    //Console.WriteLine("Argument: {0}, Handled: {1}, Valid: {2}", args[i], argsHandled[i], myCmdParams.Valid);
                    if (!myCmdParams.Valid)
                        return myCmdParams;
                }

            } // end of for loop for handling dash params

            // Check if we are using the embedded UBLs, if so adjust required file numbers
            if ((myCmdParams.useEmbeddedUBL) && (numFiles == 2))
            {
                numFiles = 1;
            }

            if (myCmdParams.UBLFlashType == FlashType.NOR)
            {

                myCmdParams.UARTUBLExecAddr = myCmdParams.NORUBLExecAddr;
            }
            else if (myCmdParams.UBLFlashType == FlashType.NAND)
            {
                myCmdParams.UARTUBLExecAddr = myCmdParams.NANDUBLExecAddr;
            }

            if (myCmdParams.useEmbeddedUBL)
            {
                myCmdParams.FLASHUBLExecAddr = myCmdParams.UARTUBLExecAddr;
            }

            // Verify that the number of unhandled arguments is equal to numFiles
            // If not, then there is a problem.
            numUnhandledArgs = (UInt32) (args.Length - numHandledArgs);
            if (numUnhandledArgs != numFiles)
            {
                myCmdParams.Valid = false;
                return myCmdParams;
            }

            // This for loop handles all othe params (namely filenames)
            for (int i = 0; i < args.Length; i++)
            {
                if (!argsHandled[i])
                {
                    if (myCmdParams.useEmbeddedUBL)
                    {
                        if (numFiles == 0)
                            myCmdParams.Valid = false;
                        else if (myCmdParams.APPFileName == null)
                        {
                            myCmdParams.APPFileName = args[i];
                            cmdString += myCmdParams.APPFileName + ".";
                        }
                        else
                            myCmdParams.Valid = false;
                    }
                    else
                    {
                        switch (numFiles)
                        {
                            case 1:
                                if (myCmdParams.APPFileName == null)
                                {
                                    myCmdParams.APPFileName = args[i];
                                    cmdString += myCmdParams.APPFileName + ".";
                                }
                                else
                                    myCmdParams.Valid = false;
                                break;
                            case 2:
                                if (myCmdParams.FLASHUBLFileName == null)
                                {
                                    myCmdParams.FLASHUBLFileName = args[i];
                                    cmdString += myCmdParams.FLASHUBLFileName + " and ";
                                }
                                else if (myCmdParams.APPFileName == null)
                                {
                                    myCmdParams.APPFileName = args[i];
                                    cmdString += myCmdParams.APPFileName + ".";
                                }
                                else
                                    myCmdParams.Valid = false;
                                break;
                            default:
                                myCmdParams.Valid = false;
                                break;
                        }
                    }
                    argsHandled[i] = true;
                    if (!myCmdParams.Valid)
                        return myCmdParams;
                }
            } // end of for loop for handling dash params

            // Set default binary execution address on target DVEVM
            if (myCmdParams.APPLoadAddr == 0xFFFFFFFF)
                myCmdParams.APPLoadAddr = 0x81080000;

            if (myCmdParams.APPEntryPoint == 0xFFFFFFFF)
                myCmdParams.APPEntryPoint = 0x81080000;

            if (myCmdParams.FLASHUBLLoadAddr == 0xFFFFFFFF)
                myCmdParams.FLASHUBLLoadAddr = 0x81070000;

            //Setup default serial port name
            if (myCmdParams.SerialPortName == null)
            {
                int p = (int)Environment.OSVersion.Platform;
                if ((p == 4) || (p == 128)) //Check for unix
                {
                    Console.WriteLine("Platform is Unix/Linux.");
                    myCmdParams.SerialPortName = "/dev/ttyS0";
                }
                else
                {
                    Console.WriteLine("Platform is Windows.");
                    myCmdParams.SerialPortName = "COM1";
                }
            }

            return myCmdParams;
        }