fp@1126: /***************************************************************************** fp@1126: * fp@1126: * $Id$ fp@1126: * fp@1126: ****************************************************************************/ fp@1126: fp@1126: #include fp@1126: #include fp@1126: #include fp@1126: using namespace std; fp@1126: fp@1126: #include "globals.h" fp@1126: #include "sii_crc.h" fp@1126: fp@1126: /****************************************************************************/ fp@1126: fp@1130: // FIXME fp@1126: const char *help_sii_write = fp@1126: "[OPTIONS]\n" fp@1126: "\n" fp@1126: "\n" fp@1126: "Command-specific options:\n"; fp@1126: fp@1126: /****************************************************************************/ fp@1126: fp@1126: void command_sii_write(void) fp@1126: { fp@1126: stringstream err; fp@1126: ec_ioctl_slave_sii_t data; fp@1126: ifstream file; fp@1126: unsigned int byte_size; fp@1126: const uint16_t *categoryHeader; fp@1126: uint16_t categoryType, categorySize; fp@1126: uint8_t crc; fp@1126: fp@1126: if (slavePosition < 0) { fp@1126: err << "'sii_write' requires a slave! Please specify --slave."; fp@1126: throw MasterDeviceException(err.str()); fp@1126: } fp@1126: data.slave_position = slavePosition; fp@1126: fp@1126: if (commandArgs.size() != 1) { fp@1126: err << "'ssi_write' takes exactly one argument!"; fp@1126: throw MasterDeviceException(err.str()); fp@1126: } fp@1126: fp@1126: file.open(commandArgs[0].c_str(), ifstream::in | ifstream::binary); fp@1126: if (file.fail()) { fp@1126: err << "Failed to open '" << commandArgs[0] << "'!"; fp@1126: throw MasterDeviceException(err.str()); fp@1126: } fp@1126: fp@1126: // get length of file fp@1126: file.seekg(0, ios::end); fp@1126: byte_size = file.tellg(); fp@1126: file.seekg(0, ios::beg); fp@1126: fp@1126: if (!byte_size || byte_size % 2) { fp@1126: stringstream err; fp@1126: err << "Invalid file size! Must be non-zero and even."; fp@1126: throw MasterDeviceException(err.str()); fp@1126: } fp@1126: fp@1126: data.nwords = byte_size / 2; fp@1126: if (data.nwords < 0x0041 && !force) { fp@1126: err << "SII data too short (" << data.nwords << " words)! Mimimum is" fp@1126: " 40 fixed words + 1 delimiter. Use --force to write anyway."; fp@1126: throw MasterDeviceException(err.str()); fp@1126: } fp@1126: fp@1126: // allocate buffer and read file into buffer fp@1126: data.words = new uint16_t[data.nwords]; fp@1126: file.read((char *) data.words, byte_size); fp@1126: file.close(); fp@1126: fp@1126: if (!force) { fp@1126: // calculate checksum over words 0 to 6 fp@1126: crc = calcSiiCrc((const uint8_t *) data.words, 14); fp@1126: if (crc != ((const uint8_t *) data.words)[14]) { fp@1126: err << "CRC incorrect. Must be 0x" fp@1126: << hex << setfill('0') << setw(2) << (unsigned int) crc fp@1126: << ". Use --force to write anyway."; fp@1126: throw MasterDeviceException(err.str()); fp@1126: } fp@1126: fp@1126: // cycle through categories to detect corruption fp@1126: categoryHeader = data.words + 0x0040U; fp@1126: categoryType = le16tocpu(*categoryHeader); fp@1126: while (categoryType != 0xffff) { fp@1126: if (categoryHeader + 1 > data.words + data.nwords) { fp@1126: err << "SII data seem to be corrupted! " fp@1126: << "Use --force to write anyway."; fp@1126: throw MasterDeviceException(err.str()); fp@1126: } fp@1126: categorySize = le16tocpu(*(categoryHeader + 1)); fp@1126: if (categoryHeader + 2 + categorySize + 1 fp@1126: > data.words + data.nwords) { fp@1126: err << "SII data seem to be corrupted! " fp@1126: "Use --force to write anyway."; fp@1126: throw MasterDeviceException(err.str()); fp@1126: } fp@1126: categoryHeader += 2 + categorySize; fp@1126: categoryType = le16tocpu(*categoryHeader); fp@1126: } fp@1126: } fp@1126: fp@1126: // send data to master fp@1126: masterDev.open(MasterDevice::ReadWrite); fp@1126: data.offset = 0; fp@1126: masterDev.writeSii(&data); fp@1126: } fp@1126: fp@1126: /*****************************************************************************/