/// <summary> /// The WriteConfig method is used to update or create a device configuration. If the name of the configuration exists /// on the device, then the configuration is updated. Otherwise, a new configuration is added. /// </summary> /// <param name="configName">Unique case-sensitive name of the configuration</param> /// <param name="data">Data to be written for the given name (not including the header)</param> public void WriteConfig(string configName, byte[] data) { uint hal_config_block_size = 0; HAL_CONFIG_BLOCK header = new HAL_CONFIG_BLOCK(); // Create a header for the configuration data unsafe { hal_config_block_size = (uint)sizeof(HAL_CONFIG_BLOCK); header.DriverNameString = configName; } header.HeaderCRC = 0; header.DataCRC = CRC.ComputeCRC(data, 0, data.Length, 0);; header.Size = (uint)data.Length; header.Signature = version_V2; // Calculate CRC information for header and data header.DataCRC = CRC.ComputeCRC(data, 0, (int)data.Length, 0); byte[] headerBytes = MarshalData(header); header.HeaderCRC = CRC.ComputeCRC(headerBytes, (2 * sizeof(UInt32)), (int)hal_config_block_size - (2 * sizeof(UInt32)), seed); headerBytes = MarshalData(header); // Concatonate the header and data byte[] allData = new byte[hal_config_block_size + data.Length]; Array.Copy(headerBytes, allData, hal_config_block_size); Array.Copy(data, 0, allData, hal_config_block_size, data.Length); WriteConfig(configName, allData, false, true); }
public void WriteConfig(string configName, IHAL_CONFIG_BASE config, bool updateConfigSector) { uint hal_config_block_size = 0; HAL_CONFIG_BLOCK header = config.ConfigHeader; unsafe { hal_config_block_size = (uint)sizeof(HAL_CONFIG_BLOCK); header.DriverNameString = configName; } // set up the configuration data header.HeaderCRC = 0; header.DataCRC = 0; header.Size = (uint)config.Size - hal_config_block_size; header.Signature = version_V2; config.ConfigHeader = header; // calculate the data crc byte[] data = MarshalData(config); header.DataCRC = CRC.ComputeCRC(data, (int)hal_config_block_size, (int)(header.Size /* - hal_config_block_size*/), 0); // this enables the data type to update itself with the crc (required because there is no class inheritence in structs and therefore no polymorphism) config.ConfigHeader = header; // calculate the header crc data = MarshalData(config); header.HeaderCRC = CRC.ComputeCRC(data, 2 * sizeof(UInt32), (int)hal_config_block_size - (2 * sizeof(UInt32)), seed); // this enables the data type to update itself with the crc (required because there is no class inheritence in structs and therefore no polymorphism) config.ConfigHeader = header; data = MarshalData(config); WriteConfig(configName, data, true, updateConfigSector); }
// Initialize the internal structures. public 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; } // read in the configuration data if the config sector was found if ((uint)configAddress != uint.MaxValue) { int hal_static_cfg_size = 0; unsafe { hal_static_cfg_size = sizeof(HAL_CONFIGURATION_SECTOR); } var devices = DfuContext.Current.GetDevices(); var device = devices[0]; all_cfg_data = new byte[hal_static_cfg_size]; device.Download(all_cfg_data, configAddress); staticConfig = (HAL_CONFIGURATION_SECTOR)UnmarshalData(all_cfg_data, typeof(HAL_CONFIGURATION_SECTOR)); // uninitialized config sector, lets try to fix it if (staticConfig.ConfigurationLength == 0xFFFFFFFF) { staticConfig.ConfigurationLength = (uint)hal_static_cfg_size; staticConfig.Version.Major = 3; staticConfig.Version.Minor = 0; staticConfig.Version.Extra = 0; staticConfig.Version.TinyBooter = 4; } // move to the dynamic configuration section index = (int)staticConfig.ConfigurationLength; lastCfgIndex = index; while (true) { byte[] data = new byte[hal_config_block_size]; // read the next configuration block device.Download(data, configAddress + index); HAL_CONFIG_BLOCK cfg_header = (HAL_CONFIG_BLOCK)UnmarshalData(data, typeof(HAL_CONFIG_BLOCK)); // out of memory or last record if (cfg_header.Size > configSize) { // last record or bogus entry lastCfgIndex = index; // save the configuration data for later use all_cfg_data = new byte[lastCfgIndex]; int idx = 0; byte[] tmp = null; while (idx < index) { int size = 512; if ((index - idx) < size) { size = index - idx; } tmp = new byte[size]; device.Download(tmp, configAddress + idx); Array.Copy(tmp, 0, 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 > configSize) { // end of config sector break; } 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++; } } } isInitialized = true; }