fp@1142: /***************************************************************************** fp@1142: * fp@1142: * $Id$ fp@1142: * fp@1142: ****************************************************************************/ fp@1142: fp@1142: #include fp@1142: #include fp@1142: #include fp@1142: using namespace std; fp@1142: fp@1142: #include "CommandSiiWrite.h" fp@1142: #include "sii_crc.h" fp@1142: #include "byteorder.h" fp@1142: fp@1142: /*****************************************************************************/ fp@1142: fp@1142: CommandSiiWrite::CommandSiiWrite(): fp@1142: Command("sii_write", "Write SII contents to a slave.") fp@1142: { fp@1142: } fp@1142: fp@1142: /*****************************************************************************/ fp@1142: fp@1142: string CommandSiiWrite::helpString() const fp@1142: { fp@1142: stringstream str; fp@1142: fp@1142: str << getName() << " [OPTIONS] " << endl fp@1142: << endl fp@1142: << getBriefDescription() << endl fp@1142: << endl fp@1142: << "The file contents are checked for validity and integrity." << endl fp@1142: << "These checks can be overridden with the --force option." << endl fp@1142: << endl fp@1142: << "Arguments:" << endl fp@1142: << " FILENAME must be a path to a file that contains a" << endl fp@1142: << " positive number of words." << endl fp@1142: << endl fp@1142: << "Command-specific options:" << endl fp@1142: << " --slave -s Positive numerical ring position" << endl fp@1142: << " (mandatory)." << endl fp@1142: << " --force -f Override validity checks." << endl fp@1142: << endl fp@1142: << numericInfo(); fp@1142: fp@1142: return str.str(); fp@1142: } fp@1142: fp@1142: /****************************************************************************/ fp@1142: fp@1142: void CommandSiiWrite::execute(MasterDevice &m, const StringVector &args) fp@1142: { fp@1142: stringstream err; fp@1142: ec_ioctl_slave_sii_t data; fp@1142: ifstream file; fp@1142: unsigned int byte_size; fp@1142: const uint16_t *categoryHeader; fp@1142: uint16_t categoryType, categorySize; fp@1142: uint8_t crc; fp@1142: fp@1142: if (slavePosition < 0) { fp@1142: err << "'" << getName() << "' requires a slave! " fp@1142: << "Please specify --slave."; fp@1142: throwInvalidUsageException(err); fp@1142: } fp@1142: data.slave_position = slavePosition; fp@1142: fp@1142: if (args.size() != 1) { fp@1142: err << "'" << getName() << "' takes exactly one argument!"; fp@1142: throwInvalidUsageException(err); fp@1142: } fp@1142: fp@1142: file.open(args[0].c_str(), ifstream::in | ifstream::binary); fp@1142: if (file.fail()) { fp@1142: err << "Failed to open '" << args[0] << "'!"; fp@1142: throwCommandException(err); fp@1142: } fp@1142: fp@1142: // get length of file fp@1142: file.seekg(0, ios::end); fp@1142: byte_size = file.tellg(); fp@1142: file.seekg(0, ios::beg); fp@1142: fp@1142: if (!byte_size || byte_size % 2) { fp@1142: err << "Invalid file size! Must be non-zero and even."; fp@1142: throwCommandException(err); fp@1142: } fp@1142: fp@1142: data.nwords = byte_size / 2; fp@1142: if (data.nwords < 0x0041 && !force) { fp@1142: err << "SII data too short (" << data.nwords << " words)! Mimimum is" fp@1142: " 40 fixed words + 1 delimiter. Use --force to write anyway."; fp@1142: throwCommandException(err); fp@1142: } fp@1142: fp@1142: // allocate buffer and read file into buffer fp@1142: data.words = new uint16_t[data.nwords]; fp@1142: file.read((char *) data.words, byte_size); fp@1142: file.close(); fp@1142: fp@1142: if (!force) { fp@1142: // calculate checksum over words 0 to 6 fp@1142: crc = calcSiiCrc((const uint8_t *) data.words, 14); fp@1142: if (crc != ((const uint8_t *) data.words)[14]) { fp@1142: err << "CRC incorrect. Must be 0x" fp@1142: << hex << setfill('0') << setw(2) << (unsigned int) crc fp@1142: << ". Use --force to write anyway."; fp@1142: throwCommandException(err); fp@1142: } fp@1142: fp@1142: // cycle through categories to detect corruption fp@1142: categoryHeader = data.words + 0x0040U; fp@1142: categoryType = le16tocpu(*categoryHeader); fp@1142: while (categoryType != 0xffff) { fp@1142: if (categoryHeader + 1 > data.words + data.nwords) { fp@1142: err << "SII data seem to be corrupted! " fp@1142: << "Use --force to write anyway."; fp@1142: throwCommandException(err); fp@1142: } fp@1142: categorySize = le16tocpu(*(categoryHeader + 1)); fp@1142: if (categoryHeader + 2 + categorySize + 1 fp@1142: > data.words + data.nwords) { fp@1142: err << "SII data seem to be corrupted! " fp@1142: "Use --force to write anyway."; fp@1142: throwCommandException(err); fp@1142: } fp@1142: categoryHeader += 2 + categorySize; fp@1142: categoryType = le16tocpu(*categoryHeader); fp@1142: } fp@1142: } fp@1142: fp@1142: // send data to master fp@1142: m.open(MasterDevice::ReadWrite); fp@1142: data.offset = 0; fp@1142: m.writeSii(&data); fp@1142: } fp@1142: fp@1142: /*****************************************************************************/