Exemplo n.º 1
0
            public override void OnAction(IMFDeployForm form, MFDevice device)
            {
                if (device.ConnectToTinyBooter())
                {
                    _WP.Commands.Monitor_FlashSectorMap.Reply reply = device.DbgEngine.GetFlashSectorMap();

                    if (reply != null)
                    {
                        foreach (_WP.Commands.Monitor_FlashSectorMap.FlashSectorData sector in reply.m_map)
                        {
                            if (0 != (sector.m_flags & _WP.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_CODE) ||
                                0 != (sector.m_flags & _WP.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_CONFIG))
                            {
                                device.DbgEngine.EraseMemory(sector.m_address, sector.m_size);
                            }
                        }
                    }
                }
            }
Exemplo n.º 2
0
        /// <summary>
        /// Erases the deployment sectors of the connected .Net Micro Framework device
        /// </summary>
        /// <param name="options">Identifies which areas are to be erased, if no options are given, all
        /// user sectors will be erased.
        /// </param>
        /// <returns>Returns false if the erase fails, true otherwise
        /// Possible exceptions: MFUserExitException, MFDeviceNoResponseException
        /// </returns>
        public bool Erase(params EraseOptions[] options)
        {
            bool ret    = false;
            bool fReset = false;

            if (m_eng == null)
            {
                throw new MFDeviceNoResponseException();
            }
            EraseOptions optionFlags = 0;

            if (options == null || options.Length == 0)
            {
                optionFlags = (EraseOptions.Deployment | EraseOptions.FileSystem | EraseOptions.UserStorage);
            }
            else
            {
                foreach (EraseOptions opt in options)
                {
                    optionFlags |= opt;
                }
            }

            if (!m_eng.TryToConnect(5, 100, true, _DBG.ConnectionSource.Unknown))
            {
                throw new MFDeviceNoResponseException();
            }

            if (!IsClrDebuggerEnabled())
            {
                fReset = (Ping() == PingConnectionType.TinyCLR);
                ConnectToTinyBooter();
            }

            _WP.Commands.Monitor_FlashSectorMap.Reply reply = m_eng.GetFlashSectorMap();

            if (reply == null)
            {
                throw new MFDeviceNoResponseException();
            }

            _WP.Commands.Monitor_Ping.Reply ping = m_eng.GetConnectionSource();

            ret = true;


            long total = 0;
            long value = 0;

            bool isConnectedToCLR = ((ping != null) && (ping.m_source == _WP.Commands.Monitor_Ping.c_Ping_Source_TinyCLR));


            if (isConnectedToCLR)
            {
                m_eng.PauseExecution();
            }

            List <_WP.Commands.Monitor_FlashSectorMap.FlashSectorData> eraseSectors = new List <_WP.Commands.Monitor_FlashSectorMap.FlashSectorData>();

            foreach (_WP.Commands.Monitor_FlashSectorMap.FlashSectorData fsd in reply.m_map)
            {
                if (EventCancel.WaitOne(0, false))
                {
                    throw new MFUserExitException();
                }

                switch (fsd.m_flags & _WP.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_MASK)
                {
                case _WP.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_DEPLOYMENT:
                    if (EraseOptions.Deployment == (optionFlags & EraseOptions.Deployment))
                    {
                        eraseSectors.Add(fsd);
                        total++;
                    }
                    break;

                case _WP.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_STORAGE_A:
                case _WP.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_STORAGE_B:
                    if (EraseOptions.UserStorage == (optionFlags & EraseOptions.UserStorage))
                    {
                        eraseSectors.Add(fsd);
                        total++;
                    }
                    break;

                case _WP.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_FS:
                    if (EraseOptions.FileSystem == (optionFlags & EraseOptions.FileSystem))
                    {
                        eraseSectors.Add(fsd);
                        total++;
                    }
                    break;
                }
            }


            foreach (_WP.Commands.Monitor_FlashSectorMap.FlashSectorData fsd in eraseSectors)
            {
                ret &= m_eng.EraseMemory(fsd.m_address, fsd.m_size);

                value++;

                if (OnProgress != null)
                {
                    OnProgress(value, total, string.Format(Properties.Resources.StatusEraseSector, fsd.m_address));
                }
            }

            // reset if we specifically entered tinybooter for the erase
            if (fReset)
            {
                m_eng.ExecuteMemory(0);
            }
            // reboot if we are talking to the clr
            if (isConnectedToCLR)
            {
                if (OnProgress != null)
                {
                    OnProgress(0, 0, Properties.Resources.StatusRebooting);
                }

                m_eng.RebootDevice(_DBG.Engine.RebootOption.RebootClrOnly);
                m_eng.ResumeExecution();
            }

            return(ret);
        }
Exemplo n.º 3
0
        private void PrepareForDeploy(ArrayList blocks)
        {
            const uint c_DeploySector    = _WP.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_DEPLOYMENT;
            const uint c_SectorUsageMask = _WP.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_MASK;

            bool fEraseDeployment = false;

            // if vsdebug is not enabled then we cannot write/erase
            if (!IsClrDebuggerEnabled())
            {
                if (OnProgress != null)
                {
                    OnProgress(0, 1, Properties.Resources.StatusConnectingToTinyBooter);
                }

                // only check for signature file if we are uploading firmware
                if (!ConnectToTinyBooter())
                {
                    throw new MFDeviceNoResponseException();
                }
            }

            _WP.Commands.Monitor_FlashSectorMap.Reply map = m_eng.GetFlashSectorMap();

            if (map == null)
            {
                throw new MFDeviceNoResponseException();
            }

            foreach (_DBG.SRecordFile.Block bl in blocks)
            {
                foreach (_WP.Commands.Monitor_FlashSectorMap.FlashSectorData sector in map.m_map)
                {
                    if (sector.m_address == bl.address)
                    {
                        // only support writing with CLR to the deployment sector and RESERVED sector (for digi)
                        if (c_DeploySector == (c_SectorUsageMask & sector.m_flags))
                        {
                            fEraseDeployment = true;
                        }
                        else
                        {
                            if (m_eng.ConnectionSource != _DBG.ConnectionSource.TinyBooter)
                            {
                                if (OnProgress != null)
                                {
                                    OnProgress(0, 1, Properties.Resources.StatusConnectingToTinyBooter);
                                }

                                // only check for signature file if we are uploading firmware
                                if (!ConnectToTinyBooter())
                                {
                                    throw new MFDeviceNoResponseException();
                                }
                            }
                        }
                        break;
                    }
                }
            }
            if (fEraseDeployment)
            {
                this.Erase(EraseOptions.Deployment);
            }
            else if (m_eng.ConnectionSource != _DBG.ConnectionSource.TinyBooter)
            {
                //if we are not writing to the deployment sector then assure that we are talking with TinyBooter
                ConnectToTinyBooter();
            }
            if (m_eng.ConnectionSource == _DBG.ConnectionSource.TinyCLR)
            {
                m_eng.PauseExecution();
            }
        }
Exemplo n.º 4
0
        private void EraseDialog_Load(object sender, EventArgs e)
        {
            _DBG.Engine engine = m_device.DbgEngine;

            _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.Reply reply = engine.GetFlashSectorMap();

            if (reply != null)
            {
                Dictionary <EraseOptions, _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.FlashSectorData> hashBlockType = new Dictionary <EraseOptions, _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.FlashSectorData>();
                Dictionary <EraseOptions, string> usageNameHash = new Dictionary <EraseOptions, string>();

                for (int i = 0; i < reply.m_map.Length; i++)
                {
                    _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.FlashSectorData fsd = reply.m_map[i];

                    string       usage = "";
                    EraseOptions eo    = (EraseOptions)(-1);

                    switch (fsd.m_flags & _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_MASK)
                    {
                    case _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_BOOTSTRAP:
                        //usage = "Bootstrap";
                        break;

                    case _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_CONFIG:
                    case _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_CODE:
                        if (m_device.DbgEngine.ConnectionSource == _DBG.ConnectionSource.TinyBooter)
                        {
                            usage = "Firmware";
                            eo    = EraseOptions.Firmware;
                        }
                        break;

                    case _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_DEPLOYMENT:
                        usage = "Deployment";
                        eo    = EraseOptions.Deployment;
                        break;

                    case _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_UPDATE:
                        usage = "Update Storage";
                        eo    = EraseOptions.UpdateStorage;
                        break;

                    case _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_FS:
                        usage = "File System";
                        eo    = EraseOptions.FileSystem;
                        break;

                    case _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_SIMPLE_B:
                    case _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_SIMPLE_A:
                        usage = "Simple Storage";
                        eo    = EraseOptions.SimpleStorage;
                        break;

                    case _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_STORAGE_A:
                    case _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_STORAGE_B:
                        usage = "User Storage";
                        eo    = EraseOptions.UserStorage;
                        break;
                    }

                    if (eo != (EraseOptions)(-1))
                    {
                        if (hashBlockType.ContainsKey(eo))
                        {
                            _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.FlashSectorData prev = hashBlockType[eo];
                            if (prev.m_address + prev.m_size == fsd.m_address)
                            {
                                prev.m_size      += fsd.m_size;
                                hashBlockType[eo] = prev;
                            }
                        }
                        else
                        {
                            hashBlockType[eo] = fsd;
                            usageNameHash[eo] = usage;
                        }
                    }
                }

                foreach (EraseOptions eo in hashBlockType.Keys)
                {
                    ListViewItem lvi = listViewEraseSectors.Items.Add(new ListViewItem(new string[] { usageNameHash[eo], string.Format("0x{0:X08}", hashBlockType[eo].m_address), string.Format("0x{0:X08}", hashBlockType[eo].m_size) }));
                    lvi.Tag = eo;
                    if (eo != EraseOptions.Firmware)
                    {
                        lvi.Checked = true;
                    }
                }
            }
        }
Exemplo n.º 5
0
        // Initialize the internal structures.
        private void InitializeConfigData()
        {
            uint hal_config_block_size  = 0;
            uint hal_config_static_size = 0;
            int  index = 0;

            unsafe
            {
                hal_config_block_size  = (uint)sizeof(HAL_CONFIG_BLOCK);
                hal_config_static_size = (uint)sizeof(HAL_CONFIGURATION_SECTOR) - hal_config_block_size;
            }

            m_cfg_sector.m_address = uint.MaxValue;
            m_cfg_sector.m_size    = 0;

            _DBG.Engine engine = m_device.DbgEngine;

            if (!engine.TryToConnect(10, 100, true, Microsoft.SPOT.Debugger.ConnectionSource.Unknown))
            {
                throw new MFDeviceNoResponseException();
            }

            if (m_device.DbgEngine.PortDefinition is _DBG.PortDefinition_Tcp)
            {
                m_device.UpgradeToSsl();
            }

            _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.Reply reply = engine.GetFlashSectorMap();

            // Find the config sector
            foreach (_DBG.WireProtocol.Commands.Monitor_FlashSectorMap.FlashSectorData fsd in reply.m_map)
            {
                const uint usage_mask = _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_MASK;
                const uint usage_cfg  = _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_CONFIG;

                if (usage_cfg == (fsd.m_flags & usage_mask))
                {
                    m_cfg_sector = fsd;
                    break;
                }
            }

            // read in the configuration data if the config sector was found
            if (m_cfg_sector.m_address != uint.MaxValue)
            {
                int hal_static_cfg_size = 0;
                unsafe
                {
                    hal_static_cfg_size = sizeof(HAL_CONFIGURATION_SECTOR);
                }

                // read in the static portion of the config sector
                engine.ReadMemory(m_cfg_sector.m_address, (uint)hal_static_cfg_size, out m_all_cfg_data);

                m_StaticConfig = (HAL_CONFIGURATION_SECTOR)UnmarshalData(m_all_cfg_data, typeof(HAL_CONFIGURATION_SECTOR));

                // uninitialized config sector, lets try to fix it
                if (m_StaticConfig.ConfigurationLength == 0xFFFFFFFF)
                {
                    m_StaticConfig.ConfigurationLength = (uint)hal_static_cfg_size;
                    m_StaticConfig.Version.Major       = 3;
                    m_StaticConfig.Version.Minor       = 0;
                    m_StaticConfig.Version.Extra       = 0;
                    m_StaticConfig.Version.TinyBooter  = 4;
                }

                if (m_StaticConfig.ConfigurationLength >= m_cfg_sector.m_size)
                {
                    throw new MFInvalidConfigurationSectorException();
                }

                if (m_StaticConfig.Version.TinyBooter == 4)
                {
                    m_fStaticCfgOK = true;
                }
                else
                {
                    m_fStaticCfgOK = (m_StaticConfig.ConfigurationLength != hal_config_static_size);
                }

                // determine if we have a new or old version of the config (security keys are only supported in the new version)
                m_fValidConfig = (m_StaticConfig.Version.TinyBooter == CONFIG_SECTOR_VERSION.c_CurrentTinyBooterVersion);

                if (m_fStaticCfgOK)
                {
                    m_firmwareKeyLocked = CheckKeyLocked(m_StaticConfig, true);
                    m_deployKeyLocked   = CheckKeyLocked(m_StaticConfig, false);
                }

                // move to the dynamic configuration section
                index = (int)m_StaticConfig.ConfigurationLength;

                m_lastCfgIndex = index;

                while (true)
                {
                    byte[] data;

                    // read the next configuration block
                    engine.ReadMemory((uint)(m_cfg_sector.m_address + index), hal_config_block_size, out data);

                    HAL_CONFIG_BLOCK cfg_header = (HAL_CONFIG_BLOCK)UnmarshalData(data, typeof(HAL_CONFIG_BLOCK));

                    // out of memory or last record
                    if (cfg_header.Size > m_cfg_sector.m_size)
                    {
                        // last record or bogus entry
                        m_lastCfgIndex = index;

                        // save the configuration data for later use
                        m_all_cfg_data = new byte[m_lastCfgIndex];

                        int    idx = 0;
                        byte[] tmp;

                        while (idx < index)
                        {
                            int size = 512;

                            if ((index - idx) < size)
                            {
                                size = index - idx;
                            }

                            engine.ReadMemory((uint)(m_cfg_sector.m_address + idx), (uint)size, out tmp);

                            Array.Copy(tmp, 0, m_all_cfg_data, idx, tmp.Length);

                            idx += size;
                        }

                        break; // no more configs
                    }

                    // move to the next configuration block
                    if (cfg_header.Size + hal_config_block_size + index > m_cfg_sector.m_size)
                    {
                        // end of config sector
                        break;
                    }

                    m_cfgHash[cfg_header.DriverNameString] = new ConfigIndexData(index, (int)(cfg_header.Size + hal_config_block_size));

                    index += (int)(cfg_header.Size + hal_config_block_size);

                    while (0 != (index % 4))
                    {
                        index++;
                    }
                }
            }
            m_init = true;
        }
Exemplo n.º 6
0
            public override void OnAction(IMFDeployForm form, MFDevice device)
            {
                if (form == null || device == null)
                {
                    return;
                }

                _DBG.Engine engine = device.DbgEngine;

                _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.Reply reply = engine.GetFlashSectorMap();

                if (reply != null)
                {
                    form.DumpToOutput(" Sector    Start       Size        Usage");
                    form.DumpToOutput("-----------------------------------------------");
                    for (int i = 0; i < reply.m_map.Length; i++)
                    {
                        _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.FlashSectorData fsd = reply.m_map[i];

                        string usage = "";
                        switch (fsd.m_flags & _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_MASK)
                        {
                        case _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_BOOTSTRAP:
                            usage = "Bootstrap";
                            break;

                        case _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_CODE:
                            usage = "Code";
                            break;

                        case _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_CONFIG:
                            usage = "Configuration";
                            break;

                        case _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_DEPLOYMENT:
                            usage = "Deployment";
                            break;

                        case _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_UPDATE:
                            usage = "Update Storage";
                            break;

                        case _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_FS:
                            usage = "File System";
                            break;

                        case _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_SIMPLE_A:
                            usage = "Simple Storage (A)";
                            break;

                        case _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_SIMPLE_B:
                            usage = "Simple Storage (B)";
                            break;

                        case _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_STORAGE_A:
                            usage = "EWR Storage (A)";
                            break;

                        case _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_STORAGE_B:
                            usage = "EWR Storage (B)";
                            break;
                        }

                        form.DumpToOutput(string.Format("{0,5}  {1,12}{2,12}   {3}", i, string.Format("0x{0:x08}", fsd.m_address), string.Format("0x{0:x08}", fsd.m_size), usage));
                    }
                }
            }
        private void buttonEraseDeployment_Click(object sender, EventArgs e)
        {
            bool   bWasStarted = EnsureDebuggerConnection();
            Cursor old         = Cursor.Current;

            Cursor = Cursors.WaitCursor;
            buttonEraseDeployment.Text = "Erasing...";
            buttonEraseDeployment.Update();
            NewText("Erasing Deployment Sector...\r\n");

            try
            {
                _DBG.WireProtocol.Commands.Monitor_Ping.Reply ping = m_eng.GetConnectionSource();
                if (ping == null)
                {
                    NewText("Unable to connect to device\r\n");
                    return;
                }

                bool fClrConnection = ping.m_source == _DBG.WireProtocol.Commands.Monitor_Ping.c_Ping_Source_TinyCLR;

                if (fClrConnection)
                {
                    m_eng.PauseExecution();
                }

                _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.Reply status = m_eng.GetFlashSectorMap() as _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.Reply;

                if (status == null)
                {
                    NewText("Erase Deployment may Not be supported on this device build\r\n");
                }
                else
                {
                    const uint c_deployFlag = _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_DEPLOYMENT;
                    const uint c_usageMask  = _DBG.WireProtocol.Commands.Monitor_FlashSectorMap.c_MEMORY_USAGE_MASK;

                    foreach (_DBG.WireProtocol.Commands.Monitor_FlashSectorMap.FlashSectorData sector in status.m_map)
                    {
                        if (c_deployFlag == (c_usageMask & sector.m_flags))
                        {
                            NewText(string.Format("Erasing sector at 0x{0:x08}\r\n", sector.m_address));
                            m_eng.EraseMemory(sector.m_address, sector.m_size);
                        }
                    }

                    if (fClrConnection)
                    {
                        m_eng.RebootDevice(_DBG.Engine.RebootOption.RebootClrOnly);
                    }
                    NewText("Erase Deployment Successfull");
                }
            }
            catch (Exception ex)
            {
                NewText("Exception: " + ex.Message + "\r\n");
            }
            finally
            {
                buttonEraseDeployment.Text = "Erase Deployment";

                Cursor = old;
                if (!bWasStarted)
                {
                    m_eng.Stop();
                    m_eng = null;
                }
            }
        }