public byte[] FindConfig(string configName) { byte[] retVal = null; if (!isInitialized) { InitializeConfigData(); } // see if we have seen this configuration. if (cfgHash.ContainsKey(configName)) { ConfigIndexData cid = (ConfigIndexData)cfgHash[configName]; retVal = new byte[cid.Size]; Array.Copy(all_cfg_data, cid.Index, retVal, 0, cid.Size); } return(retVal); }
// Write the concatonated header and configuration data to the Flash config sector private void WriteConfig(string configName, byte[] data, bool staticSize, bool updateConfigSector) { if (!isInitialized) { InitializeConfigData(); } // updating the config if (cfgHash.ContainsKey(configName)) { ConfigIndexData cid = (ConfigIndexData)cfgHash[configName]; // If old and new data are different sizes if (cid.Size != data.Length) { // If data comes from a well defined structure, its size cannot vary //if (staticSize) throw new MFInvalidConfigurationDataException(); uint newNextIndex, oldNextIndex; byte[] temp; int diff = 0; // Figure out where any following configuration data will start newNextIndex = (uint)(cid.Index + data.Length); while (0 != (newNextIndex % 4)) { newNextIndex++; // Force a 4 byte boundary } // Figure out where any following configuration data previously started oldNextIndex = (uint)(cid.Index + cid.Size); while (0 != (oldNextIndex % 4)) { oldNextIndex++; // Force a 4 byte boundary } diff = (int)newNextIndex - (int)oldNextIndex; // Find the adjusted difference in size between old and new config data temp = new byte[lastCfgIndex + diff]; // Create a new byte array to contain all the configuration data Array.Copy(all_cfg_data, temp, cid.Index); // Copy all preceding data to new array Array.Copy(data, 0, temp, cid.Index, data.Length); // Copy new configuration to new array if (oldNextIndex < lastCfgIndex) // Copy all following data (if it exists) to new array { Array.Copy(all_cfg_data, oldNextIndex, temp, newNextIndex, (all_cfg_data.Length - oldNextIndex)); } // Update the local copy of the configuration list all_cfg_data = temp; lastCfgIndex += diff; } else { // Copy the new configuration data on top of the old Array.Copy(data, 0, all_cfg_data, cid.Index, data.Length); } } else // adding a new configuration to the end of the current list { uint newLastIndex; if (lastCfgIndex == -1) { throw new OutOfMemoryException(); } // Find the new size of the whole configuration list newLastIndex = (uint)(lastCfgIndex + data.Length); while (0 != (newLastIndex % 4)) { newLastIndex++; // Force a 4 byte boundary } byte[] temp = new byte[lastCfgIndex >= all_cfg_data.Length ? lastCfgIndex + data.Length : all_cfg_data.Length]; Array.Copy(all_cfg_data, 0, temp, 0, all_cfg_data.Length); Array.Copy(data, 0, temp, lastCfgIndex, data.Length); // Update the local copy of the configuration list all_cfg_data = temp; lastCfgIndex = (int)newLastIndex; } if (!updateConfigSector) { return; } // Rewrite entire configuration list to Flash var devices = DfuContext.Current.GetDevices(); DfuDevice device = devices[0]; byte[] clear = new byte[all_cfg_data.Length]; device.EraseSector(configAddress); device.Upload(all_cfg_data, configAddress); // Rebuild hash table cfgHash.Clear(); uint hal_config_block_size = 0; unsafe { hal_config_block_size = (uint)sizeof(HAL_CONFIG_BLOCK); } int index = (int)staticConfig.ConfigurationLength; byte[] headerData = new byte[hal_config_block_size]; HAL_CONFIG_BLOCK cfg_header; while (index < lastCfgIndex) { // Read in next configuration header Array.Copy(all_cfg_data, index, headerData, 0, hal_config_block_size); cfg_header = (HAL_CONFIG_BLOCK)UnmarshalData(headerData, typeof(HAL_CONFIG_BLOCK)); cfgHash[cfg_header.DriverNameString] = new ConfigIndexData(index, (int)(cfg_header.Size + hal_config_block_size)); // Index of next configuration header must lie on a 4 byte boundary index += (int)(cfg_header.Size + hal_config_block_size); while (0 != (index % 4)) { index++; // Force a 4 byte boundary } } }
// Write the concatonated header and configuration data to the Flash config sector private void WriteConfig(string configName, byte[] data, bool staticSize, bool updateConfigSector) { _DBG.Engine engine = m_device.DbgEngine; if (!m_init) { InitializeConfigData(); } // updating the config if (m_cfgHash.ContainsKey(configName)) { ConfigIndexData cid = (ConfigIndexData)m_cfgHash[configName]; // If old and new data are different sizes if (cid.Size != data.Length) { // If data comes from a well defined structure, its size cannot vary if (staticSize) { throw new MFInvalidConfigurationDataException(); } uint newNextIndex, oldNextIndex; byte[] temp; int diff = 0; // Figure out where any following configuration data will start newNextIndex = (uint)(cid.Index + data.Length); while (0 != (newNextIndex % 4)) { newNextIndex++; // Force a 4 byte boundary } // Figure out where any following configuration data previously started oldNextIndex = (uint)(cid.Index + cid.Size); while (0 != (oldNextIndex % 4)) { oldNextIndex++; // Force a 4 byte boundary } diff = (int)newNextIndex - (int)oldNextIndex; // Find the adjusted difference in size between old and new config data temp = new byte[m_lastCfgIndex + diff]; // Create a new byte array to contain all the configuration data Array.Copy(m_all_cfg_data, temp, cid.Index); // Copy all preceding data to new array Array.Copy(data, 0, temp, cid.Index, data.Length); // Copy new configuration to new array if (oldNextIndex < m_lastCfgIndex) // Copy all following data (if it exists) to new array { Array.Copy(m_all_cfg_data, oldNextIndex, temp, newNextIndex, (m_all_cfg_data.Length - oldNextIndex)); } // Update the local copy of the configuration list m_all_cfg_data = temp; m_lastCfgIndex += diff; } else { // Copy the new configuration data on top of the old Array.Copy(data, 0, m_all_cfg_data, cid.Index, data.Length); } } else // adding a new configuration to the end of the current list { uint newLastIndex; if (m_lastCfgIndex == -1) { throw new MFConfigurationSectorOutOfMemoryException(); } // Find the new size of the whole configuration list newLastIndex = (uint)(m_lastCfgIndex + data.Length); while (0 != (newLastIndex % 4)) { newLastIndex++; // Force a 4 byte boundary } byte[] temp = new byte[m_lastCfgIndex >= m_all_cfg_data.Length ? m_lastCfgIndex + data.Length : m_all_cfg_data.Length]; Array.Copy(m_all_cfg_data, 0, temp, 0, m_all_cfg_data.Length); Array.Copy(data, 0, temp, m_lastCfgIndex, data.Length); // Update the local copy of the configuration list m_all_cfg_data = temp; m_lastCfgIndex = (int)newLastIndex; } if (!updateConfigSector) { return; } // Rewrite entire configuration list to Flash if (!engine.EraseMemory(m_cfg_sector.m_address, (uint)m_all_cfg_data.Length)) { throw new MFConfigSectorEraseFailureException(); } if (!engine.WriteMemory(m_cfg_sector.m_address, m_all_cfg_data)) { throw new MFConfigSectorWriteFailureException(); } // Rebuild hash table m_cfgHash.Clear(); uint hal_config_block_size = 0; unsafe { hal_config_block_size = (uint)sizeof(HAL_CONFIG_BLOCK); } int index = (int)m_StaticConfig.ConfigurationLength; byte[] headerData = new byte[hal_config_block_size]; HAL_CONFIG_BLOCK cfg_header; while (index < m_lastCfgIndex) { // Read in next configuration header Array.Copy(m_all_cfg_data, index, headerData, 0, hal_config_block_size); cfg_header = (HAL_CONFIG_BLOCK)UnmarshalData(headerData, typeof(HAL_CONFIG_BLOCK)); m_cfgHash[cfg_header.DriverNameString] = new ConfigIndexData(index, (int)(cfg_header.Size + hal_config_block_size)); // Index of next configuration header must lie on a 4 byte boundary index += (int)(cfg_header.Size + hal_config_block_size); while (0 != (index % 4)) { index++; // Force a 4 byte boundary } } // we need to perform signature check regardless of key update, in order for the device to write from ram buffer to flash if (!engine.CheckSignature(new byte[TINYBOOTER_KEY_CONFIG.c_KeySignatureLength], 0)) { if (engine.ConnectionSource == Microsoft.SPOT.Debugger.ConnectionSource.TinyBooter) { throw new MFConfigSectorWriteFailureException(); } } if (engine.ConnectionSource == Microsoft.SPOT.Debugger.ConnectionSource.TinyBooter && m_fRestartClr) { engine.ExecuteMemory(c_EnumerateAndLaunchAddr); } }