예제 #1
0
        // TinyBooter uses 2 security keys one to control the deployment sector and the other for everything else (firmwareKey)
        // This method determines if the key is set for the given sector type.
        private bool CheckKeyLocked(HAL_CONFIGURATION_SECTOR cfg, bool firmwareKey)
        {
            bool locked = false;

            unsafe
            {
                byte *key = (firmwareKey ? cfg.PublicKeyFirmware.SectorKey : cfg.PublicKeyDeployment.SectorKey);

                // check to see if the key is un-initialized (all 0xff bytes - uninitialized FLASH memory)
                for (int i = 0; i < TINYBOOTER_KEY_CONFIG.c_RSAKeyLength; i++)
                {
                    if (key[i] != 0xFF)
                    {
                        locked = true;
                        break;
                    }
                }
            }

            return(locked);
        }
예제 #2
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)
            {
                // read in the static portion of the config sector
                engine.ReadMemory( m_cfg_sector.m_address, ( uint )hal_config_static_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_config_static_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;
        }
예제 #3
0
        // TinyBooter uses 2 security keys one to control the deployment sector and the other for everything else (firmwareKey)
        // This method determines if the key is set for the given sector type.  
        private bool CheckKeyLocked(HAL_CONFIGURATION_SECTOR cfg, bool firmwareKey)
        {
            bool locked = false;

            unsafe
            {
                byte* key = (firmwareKey ? cfg.PublicKeyFirmware.SectorKey : cfg.PublicKeyDeployment.SectorKey);

                // check to see if the key is un-initialized (all 0xff bytes - uninitialized FLASH memory)
                for (int i = 0; i < TINYBOOTER_KEY_CONFIG.c_RSAKeyLength; i++)
                {
                    if (key[i] != 0xFF)
                    {
                        locked = true;
                        break;
                    }
                }
            }

            return locked;
        }
예제 #4
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;
        }