diff options
Diffstat (limited to 'host/lib/utils/ihex.cpp')
-rw-r--r-- | host/lib/utils/ihex.cpp | 100 |
1 files changed, 53 insertions, 47 deletions
diff --git a/host/lib/utils/ihex.cpp b/host/lib/utils/ihex.cpp index 24d8cbdb6..bea03c2ac 100644 --- a/host/lib/utils/ihex.cpp +++ b/host/lib/utils/ihex.cpp @@ -22,7 +22,7 @@ using namespace uhd; */ static bool checksum(const std::string& record) { - size_t len = record.length(); + size_t len = record.length(); unsigned char sum = 0; unsigned int val; @@ -32,9 +32,9 @@ static bool checksum(const std::string& record) } if (sum == 0) - return true; + return true; else - return false; + return false; } @@ -48,13 +48,12 @@ static bool checksum(const std::string& record) * \param data output data * \return true if record is sucessfully read, false on error */ -static bool parse_record( - const std::string& record, - uint16_t &len, - uint16_t &addr, - uint16_t &type, - unsigned char* data -) { +static bool parse_record(const std::string& record, + uint16_t& len, + uint16_t& addr, + uint16_t& type, + unsigned char* data) +{ unsigned int i; unsigned int val; @@ -65,20 +64,19 @@ static bool parse_record( std::istringstream(record.substr(3, 4)) >> std::hex >> addr; std::istringstream(record.substr(7, 2)) >> std::hex >> type; - if (len > (2 * (record.length() - 9))) // sanity check to prevent buffer overrun + if (len > (2 * (record.length() - 9))) // sanity check to prevent buffer overrun return false; for (i = 0; i < len; i++) { std::istringstream(record.substr(9 + 2 * i, 2)) >> std::hex >> val; - data[i] = (unsigned char) val; + data[i] = (unsigned char)val; } return true; } -ihex_reader::ihex_reader(const std::string &ihex_filename) - : _ihex_filename(ihex_filename) +ihex_reader::ihex_reader(const std::string& ihex_filename) : _ihex_filename(ihex_filename) { // nop } @@ -86,12 +84,12 @@ ihex_reader::ihex_reader(const std::string &ihex_filename) void ihex_reader::read(ihex_reader::record_handle_type record_handler) { - const char *filename = _ihex_filename.c_str(); + const char* filename = _ihex_filename.c_str(); /* Fields used in every record. */ - uint16_t len = 0; - uint16_t type = 0; - uint16_t lower_address_bits = 0x0000; + uint16_t len = 0; + uint16_t type = 0; + uint16_t lower_address_bits = 0x0000; static const int MAX_RECORD_LENGTH = 255; unsigned char data[MAX_RECORD_LENGTH]; @@ -103,7 +101,7 @@ void ihex_reader::read(ihex_reader::record_handle_type record_handler) std::ifstream file; file.open(filename, std::ifstream::in); - if(!file.good()) { + if (!file.good()) { throw uhd::io_error("ihex_reader::read(): cannot open firmware input file"); } @@ -126,14 +124,16 @@ void ihex_reader::read(ihex_reader::record_handle_type record_handler) ret = record_handler(lower_address_bits, upper_address_bits, data, len); if (ret < 0) { - throw uhd::io_error("ihex_reader::read(): record hander returned failure code"); + throw uhd::io_error( + "ihex_reader::read(): record hander returned failure code"); } } /* Type 0x01: EOF. */ else if (type == 0x01) { - if (lower_address_bits != 0x0000 || len != 0 ) { - throw uhd::io_error("ihex_reader::read(): For EOF record, address must be 0, length must be 0."); + if (lower_address_bits != 0x0000 || len != 0) { + throw uhd::io_error("ihex_reader::read(): For EOF record, address must " + "be 0, length must be 0."); } /* Successful termination! */ @@ -143,35 +143,39 @@ void ihex_reader::read(ihex_reader::record_handle_type record_handler) /* Type 0x04: Extended Linear Address Record. */ else if (type == 0x04) { - if (lower_address_bits != 0x0000 || len != 2 ) { - throw uhd::io_error("ihex_reader::read(): For ELA record, address must be 0, length must be 2."); + if (lower_address_bits != 0x0000 || len != 2) { + throw uhd::io_error("ihex_reader::read(): For ELA record, address must " + "be 0, length must be 2."); } - upper_address_bits = ((uint16_t)((data[0] & 0x00FF) << 8))\ - + ((uint16_t)(data[1] & 0x00FF)); + upper_address_bits = + ((uint16_t)((data[0] & 0x00FF) << 8)) + ((uint16_t)(data[1] & 0x00FF)); } /* Type 0x05: Start Linear Address Record. */ else if (type == 0x05) { - if (lower_address_bits != 0x0000 || len != 4 ) { - throw uhd::io_error("ihex_reader::read(): For SLA record, address must be 0, length must be 4."); + if (lower_address_bits != 0x0000 || len != 4) { + throw uhd::io_error("ihex_reader::read(): For SLA record, address must " + "be 0, length must be 4."); } /* The firmware load is complete. We now need to tell the CPU * to jump to an execution address start point, now contained within * the data field. Parse these address bits out, and then push the * instruction. */ - upper_address_bits = ((uint16_t)((data[0] & 0x00FF) << 8))\ - + ((uint16_t)(data[1] & 0x00FF)); - lower_address_bits = ((uint16_t)((data[2] & 0x00FF) << 8))\ - + ((uint16_t)(data[3] & 0x00FF)); + upper_address_bits = + ((uint16_t)((data[0] & 0x00FF) << 8)) + ((uint16_t)(data[1] & 0x00FF)); + lower_address_bits = + ((uint16_t)((data[2] & 0x00FF) << 8)) + ((uint16_t)(data[3] & 0x00FF)); record_handler(lower_address_bits, upper_address_bits, 0, 0); } /* If we receive an unknown record type, error out. */ else { - throw uhd::io_error(str(boost::format("ihex_reader::read(): unsupported record type: %X.") % type)); + throw uhd::io_error( + str(boost::format("ihex_reader::read(): unsupported record type: %X.") + % type)); } } @@ -181,33 +185,33 @@ void ihex_reader::read(ihex_reader::record_handle_type record_handler) // We need a functor for the cast, a lambda would be perfect... int _file_writer_callback( - std::shared_ptr<std::ofstream> output_file, - unsigned char *buff, - uint16_t len -) { - output_file->write((const char *) buff, len); + std::shared_ptr<std::ofstream> output_file, unsigned char* buff, uint16_t len) +{ + output_file->write((const char*)buff, len); return 0; } -void ihex_reader::to_bin_file(const std::string &bin_filename) +void ihex_reader::to_bin_file(const std::string& bin_filename) { std::shared_ptr<std::ofstream> output_file(std::make_shared<std::ofstream>()); output_file->open(bin_filename.c_str(), std::ios::out | std::ios::binary); if (not output_file->is_open()) { - throw uhd::io_error(str(boost::format("Could not open file for writing: %s") % bin_filename)); + throw uhd::io_error( + str(boost::format("Could not open file for writing: %s") % bin_filename)); } - this->read(std::bind(&_file_writer_callback, output_file, std::placeholders::_3, std::placeholders::_4)); + this->read(std::bind(&_file_writer_callback, + output_file, + std::placeholders::_3, + std::placeholders::_4)); output_file->close(); } // We need a functor for the cast, a lambda would be perfect... int _vector_writer_callback( - std::vector<uint8_t>& vector, - unsigned char *buff, - uint16_t len -) { + std::vector<uint8_t>& vector, unsigned char* buff, uint16_t len) +{ for (size_t i = 0; i < len; i++) { vector.push_back(buff[i]); } @@ -220,8 +224,10 @@ std::vector<uint8_t> ihex_reader::to_vector(const size_t size_estimate) std::vector<uint8_t> buf; buf.reserve(size_estimate == 0 ? DEFAULT_SIZE_ESTIMATE : size_estimate); - this->read(std::bind(&_vector_writer_callback, std::ref(buf), std::placeholders::_3, std::placeholders::_4)); + this->read(std::bind(&_vector_writer_callback, + std::ref(buf), + std::placeholders::_3, + std::placeholders::_4)); return buf; } - |