adding & utilizing hex reader

pull/3/head
cTn 11 years ago
parent f15f749ed3
commit 3304bcf6f3

@ -0,0 +1,52 @@
// input = string
// result = if hex file is valid, result contains hex array of raw data (pure binary representation of firmware)
// if hex file wasn't valid (crc check failed on any of the lines), resul will be false
function read_hex_file(data) {
data = data.split("\n");
// check if there is an empty line in the end of hex file, if there is, remove it
if (data[data.length - 1] == "") {
data.pop();
}
var raw_hex = new Array();
var bytes_total = 0; // just for info / debug purposes
var hexfile_valid = true; // if any of the crc checks failed, this variable flips to false
for (var i = 0; i < data.length; i++) {
var byte_count = parseInt(data[i].substr(1, 2), 16) * 2; // each byte is represnted by two chars (* 2 to get the hex representation)
var address = data[i].substr(3, 4);
var record_type = parseInt(data[i].substr(7, 2), 16); // also converting from hex to decimal
var content = data[i].substr(9, byte_count);
var checksum = parseInt(data[i].substr(9 + byte_count, 2), 16); // also converting from hex to decimal (this is a 2's complement value)
if (byte_count > 0) {
var crc = (byte_count / 2) + parseInt(address.substr(0, 2), 16) + parseInt(address.substr(2, 2), 16) + record_type;
for (var needle = 0; needle < byte_count; needle += 2) {
var num = parseInt(content.substr(needle, 2), 16); // get one byte in hex and convert it to decimal
raw_hex.push(num);
crc += num;
bytes_total++;
}
// change crc to 2's complement (same as checksum)
crc = ~crc + 1;
crc &= 0xFF;
// verify
if (crc != checksum) {
hexfile_valid = false;
}
}
}
if (hexfile_valid) {
console.log('HEX file parsed: ' + bytes_total + ' bytes');
return raw_hex;
} else {
console.log('HEX file parsed, CRC check failed: ' + bytes_total + ' bytes');
return false;
}
}

@ -16,6 +16,7 @@
<script type="text/javascript" src="./js/msp.js"></script> <script type="text/javascript" src="./js/msp.js"></script>
<script type="text/javascript" src="./main.js"></script> <script type="text/javascript" src="./main.js"></script>
<script type="text/javascript" src="./js/backup_restore.js"></script> <script type="text/javascript" src="./js/backup_restore.js"></script>
<script type="text/javascript" src="./js/hex_reader.js"></script>
<!-- Various tabs are divided into separate files (for clarity) --> <!-- Various tabs are divided into separate files (for clarity) -->
<script type="text/javascript" src="./tabs/default.js"></script> <script type="text/javascript" src="./tabs/default.js"></script>

@ -2,7 +2,8 @@ function tab_initialize_firmware_flasher() {
ga_tracker.sendAppView('Firmware Flasher'); ga_tracker.sendAppView('Firmware Flasher');
GUI.active_tab = 'firmware_flasher'; GUI.active_tab = 'firmware_flasher';
var intel_hex = false; var intel_hex = false; // standard intel hex in string format
var raw_hex = false; // parsed raw hex in array format
$('#content').load("./tabs/firmware_flasher.html", function() { $('#content').load("./tabs/firmware_flasher.html", function() {
// UI Hooks // UI Hooks
@ -28,6 +29,7 @@ function tab_initialize_firmware_flasher() {
console.log('File loaded'); console.log('File loaded');
intel_hex = e.target.result; intel_hex = e.target.result;
raw_hex = read_hex_file(intel_hex);
}; };
reader.readAsText(file); reader.readAsText(file);

Loading…
Cancel
Save