diff --git a/dist/hex2bin.js b/dist/hex2bin.js index 211fcea..cf5e858 100644 --- a/dist/hex2bin.js +++ b/dist/hex2bin.js @@ -54,8 +54,7 @@ * NOTE: the binary we generate may be bigger than this size because of padding. * Really this is just a sanity check for now. If the binary is much bigger than the hex then we probably have some kind of an error. */ - function helperGetHexSize(hex) { - var hexLines = hex.split("\n"); + function helperGetHexSize(hexLines) { var size = 0; hexLines.forEach(function(line) { if (line.substr(7, 2) === RECORD_TYPE.DATA) { @@ -70,8 +69,7 @@ * The first record of type extended linear address will store the start base address of the binary. * Then the first data record's address offset will complete our start address. */ - function helperGetBinaryStartAddress(hex) { - var hexLines = hex.split("\n"); + function helperGetBinaryStartAddress(hexLines) { var record; do { @@ -94,8 +92,7 @@ * The last record of type data will store the address offset and length of the data stored at that address. * Then the last extended linear address record's base address will complete our end address. */ - function helperGetBinaryEndAddress(hex) { - var hexLines = hex.split("\n"); + function helperGetBinaryEndAddress(hexLines, maxAddress) { var record; do { @@ -112,6 +109,10 @@ var lastBaseAddress = parseInt(record.substr(9, 4), 16) << 16; var endAddress = lastBaseAddress + lastDataRecordAddressOffset + lastDataRecordLength; + + if (endAddress > maxAddress) { + return helperGetBinaryEndAddress(hexLines, maxAddress); + } log('end address of binary: ' + endAddress); return endAddress; } @@ -123,21 +124,21 @@ * This is because we are not to send the Master Boot Recrod (under minAddress) when updating the SoftDevice. * And we are not to send UICR data (above maxAddress) when updating the bootloader or application. */ - return function(hex, minAddress, maxAddress) { - var hexLines = hex.split("\n"); + return function(hex, maxAddress, minAddress) { + maxAddress = maxAddress || 0xFFFFFFFF; + minAddress = minAddress || 0x0; - var hexSizeBytes = helperGetHexSize(hex); - var startAddress = helperGetBinaryStartAddress(hex); - var endAddress = helperGetBinaryEndAddress(hex); - - minAddress = minAddress || startAddress; - maxAddress = maxAddress || endAddress; + var hexSizeBytes = helperGetHexSize(hex.split("\n")); + var startAddress = helperGetBinaryStartAddress(hex.split("\n"), minAddress); + var endAddress = helperGetBinaryEndAddress(hex.split("\n"), maxAddress); if (startAddress < minAddress) { startAddress = minAddress; + log('trimmed start address of binary: ' + startAddress); } if (endAddress > maxAddress) { endAddress = maxAddress; + log('trimmed start address of binary: ' + startAddress); } var binarySizeBytes = endAddress - startAddress; @@ -148,6 +149,7 @@ var baseAddress; + var hexLines = hex.split("\n"); hexLines.forEach(function(line) { switch (line.substr(7, 2)) { @@ -158,7 +160,7 @@ var data = line.substr(9, length * 2); for (var i = 0; i < length * 2; i += 2) { var index = (baseAddress + addressOffset) - startAddress + (i / 2); - if (index > 0 || index <= binarySizeBytes) { // This cuts off any data below minAddress and above maxAddress. + if (index >= 0 && index < binarySizeBytes) { // This cuts off any data below minAddress and above maxAddress. view[index] = parseInt(data.substr(i, 2), 16); } } @@ -188,4 +190,4 @@ }); return buffer; }; -})); \ No newline at end of file +}));