fp@1707: /***************************************************************************** fp@1707: * fp@1708: * $Id$ fp@1707: * fp@1707: ****************************************************************************/ fp@1707: fp@1707: #include // basename() fp@1707: fp@1707: #include fp@1707: #include fp@1707: #include fp@1707: using namespace std; fp@1707: fp@1707: #include "CommandFoeWrite.h" fp@1707: #include "byteorder.h" fp@1708: #include "foe.h" fp@1707: fp@1707: /*****************************************************************************/ fp@1707: fp@1707: CommandFoeWrite::CommandFoeWrite(): fp@1707: FoeCommand("foe_write", "Store a file on a slave via FoE.") fp@1707: { fp@1707: } fp@1707: fp@1707: /*****************************************************************************/ fp@1707: fp@1707: string CommandFoeWrite::helpString() const fp@1707: { fp@1707: stringstream str; fp@1707: fp@1707: str << getName() << " [OPTIONS] " << endl fp@1707: << endl fp@1707: << getBriefDescription() << endl fp@1707: << endl fp@1707: << "This command requires a single slave to be selected." << endl fp@1707: << endl fp@1707: << "Arguments:" << endl fp@1707: << " FILENAME can either be a path to a file, or '-'. In" << endl fp@1707: << " the latter case, data are read from stdin and" << endl fp@1707: << " the --output-file option has to be specified." << endl fp@1707: << endl fp@1707: << "Command-specific options:" << endl fp@1707: << " --output-file -o Target filename on the slave." << endl fp@1707: << " If the FILENAME argument is" << endl fp@1707: << " '-', this is mandatory." << endl fp@1707: << " Otherwise, the basename() of" << endl fp@1707: << " FILENAME is used by default." << endl fp@1707: << " --alias -a " << endl fp@1707: << " --position -p Slave selection. See the help" << endl fp@1707: << " of the 'slaves' command." << endl fp@1707: << endl fp@1707: << numericInfo(); fp@1707: fp@1707: return str.str(); fp@1707: } fp@1707: fp@1707: /****************************************************************************/ fp@1707: fp@1707: void CommandFoeWrite::execute(MasterDevice &m, const StringVector &args) fp@1707: { fp@1707: stringstream err; fp@1707: ec_ioctl_slave_foe_t data; fp@1707: ifstream file; fp@1707: SlaveList slaves; fp@1707: string storeFileName; fp@1707: fp@1707: if (args.size() != 1) { fp@1707: err << "'" << getName() << "' takes exactly one argument!"; fp@1707: throwInvalidUsageException(err); fp@1707: } fp@1707: fp@1707: if (args[0] == "-") { fp@1707: loadFoeData(&data, cin); fp@1707: if (getOutputFile().empty()) { fp@1707: err << "Please specify a filename for the slave side" fp@1707: << " with --output-file!"; fp@1707: throwCommandException(err); fp@1707: } else { fp@1707: storeFileName = getOutputFile(); fp@1707: } fp@1707: } else { fp@1707: file.open(args[0].c_str(), ifstream::in | ifstream::binary); fp@1707: if (file.fail()) { fp@1707: err << "Failed to open '" << args[0] << "'!"; fp@1707: throwCommandException(err); fp@1707: } fp@1707: loadFoeData(&data, file); fp@1707: file.close(); fp@1707: if (getOutputFile().empty()) { fp@1707: char *cpy = strdup(args[0].c_str()); // basename can modify fp@1707: // the string contents fp@1707: storeFileName = basename(cpy); fp@1707: free(cpy); fp@1707: } else { fp@1707: storeFileName = getOutputFile(); fp@1707: } fp@1707: } fp@1707: fp@1707: try { fp@1707: m.open(MasterDevice::ReadWrite); fp@1707: } catch (MasterDeviceException &e) { fp@1707: if (data.buffer_size) fp@1707: delete [] data.buffer; fp@1707: throw e; fp@1707: } fp@1707: fp@1707: slaves = selectedSlaves(m); fp@1707: if (slaves.size() != 1) { fp@1707: if (data.buffer_size) fp@1707: delete [] data.buffer; fp@1707: throwSingleSlaveRequired(slaves.size()); fp@1707: } fp@1707: data.slave_position = slaves.front().position; fp@1707: fp@1707: // write data via foe to the slave fp@1707: data.offset = 0; fp@1707: strncpy(data.file_name, storeFileName.c_str(), sizeof(data.file_name)); fp@1707: fp@1707: try { fp@1707: m.writeFoe(&data); fp@1707: } catch (MasterDeviceException &e) { fp@1707: if (data.buffer_size) fp@1707: delete [] data.buffer; fp@1708: if (data.result) { fp@1708: if (data.result == FOE_OPCODE_ERROR) { fp@1708: err << "FoE write aborted with error code 0x" fp@1708: << setw(8) << setfill('0') << hex << data.error_code fp@1708: << ": " << errorText(data.error_code); fp@1708: } else { fp@1708: err << "Failed to write via FoE: " fp@1708: << resultText(data.result); fp@1708: } fp@1707: throwCommandException(err); fp@1707: } else { fp@1707: throw e; fp@1707: } fp@1707: } fp@1707: fp@1707: if (getVerbosity() == Verbose) { fp@1707: cerr << "FoE writing finished." << endl; fp@1707: } fp@1707: fp@1707: if (data.buffer_size) fp@1707: delete [] data.buffer; fp@1707: } fp@1707: fp@1707: /*****************************************************************************/ fp@1707: fp@1707: void CommandFoeWrite::loadFoeData( fp@1707: ec_ioctl_slave_foe_t *data, fp@1707: const istream &in fp@1707: ) fp@1707: { fp@1707: stringstream err; fp@1707: ostringstream tmp; fp@1707: fp@1707: tmp << in.rdbuf(); fp@1707: string const &contents = tmp.str(); fp@1707: fp@1707: if (getVerbosity() == Verbose) { fp@1707: cerr << "Read " << contents.size() << " bytes of FoE data." << endl; fp@1707: } fp@1707: fp@1707: data->buffer_size = contents.size(); fp@1707: fp@1707: if (data->buffer_size) { fp@1707: // allocate buffer and read file into buffer fp@1707: data->buffer = new uint8_t[data->buffer_size]; fp@1707: contents.copy((char *) data->buffer, contents.size()); fp@1707: } fp@1707: } fp@1707: fp@1707: /*****************************************************************************/