thsq-device-flasher
Advanced tools
Comparing version 1.0.1 to 1.0.2
{ | ||
"name": "thsq-device-flasher", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"author": "Thingsquare <support@thingsquare.com>", | ||
@@ -5,0 +5,0 @@ "description": "Thingsquare device flash utility", |
@@ -15,3 +15,2 @@ #!/usr/bin/env node | ||
var getopts = require("getopts"); | ||
var crypto = require('crypto'); | ||
var WebSocketClient = require('websocket').client; | ||
@@ -33,2 +32,3 @@ | ||
var picture; | ||
var devicenameprefix; /* prefix + index */ | ||
@@ -337,5 +337,5 @@ var xidarr = []; | ||
console.info('options, required: -u <usertoken>'); | ||
console.info('options, optional: -f <frontend> -b <backend> -s <autoshare users, comma separated> -p <platform> -P <picture>'); | ||
console.info('options, optional: -f <frontend> -b <backend> -s <autoshare users, comma separated> -p <platform> -P <picture> -n <device name prefix>'); | ||
console.info('example: node thsq-device-flasher.js -f b72af87d-42d7-4f09-bf8c-b9f721a3e6ef -b developer.thingsquare.com -u 1d2ab1d2ab -s example@example.com,example2@example.com app.bin'); | ||
console.info('path utility dependencies includes: md5sim, srfprog, dd'); | ||
console.info('path utility dependencies includes: srfprog'); | ||
process.exit(1); | ||
@@ -358,2 +358,3 @@ } | ||
picture = options.P; | ||
devicenameprefix = options.n; | ||
@@ -383,4 +384,26 @@ binfile = options._[0]; | ||
} | ||
console.log('OK: binfile ' + binfile + ', md5: ' + binfilemd5); | ||
function getFilesizeInBytes(filename) { | ||
const stats = fs.statSync(filename); | ||
const fileSizeInBytes = stats.size; | ||
return fileSizeInBytes; | ||
} | ||
var COMMAND_ERASE = 'srfprog -t lsidx(0) -e forced --reset normal'; | ||
var COMMAND_READMAC = 'srfprog -t lsidx(0) -r macpri'; | ||
var COMMAND_PROGRAM = 'srfprog -t lsidx(0) --program -a 0x000000 --verify --file %FILENAME% --reset normal'; | ||
var COMMAND_SCAN = 'srfprog -t lsidx(0) -ls auto'; | ||
var COMMAND_TEST = 'srfprog -ls'; | ||
var NVSTORAGE_OFFSET; | ||
var NVSTORAGE_SIGNATURE = 'ed55ceac'; /* uint32_t 'acce55ed' */ | ||
if(getFilesizeInBytes(binfile) === 128*1024) { | ||
NVSTORAGE_OFFSET = 0x1f000; /* 0x1f000 Nvstorage: 124-128 */ | ||
} else if(getFilesizeInBytes(binfile) === 512*1024) { | ||
NVSTORAGE_OFFSET = 0x27f000 - 0x200000; | ||
COMMAND_PROGRAM = 'srfprog -t lsidx(0) --program -a 0x200000 --file %FILENAME%'; | ||
} else { | ||
errorusage('Unsupported binfile size: ' + binfile + ': ' + getFilesizeInBytes(binfile)); | ||
} | ||
console.log('OK: binfile: ' + binfile, 'md5: ' + binfilemd5, 'size: ' + getFilesizeInBytes(binfile)); | ||
var srfprogfound = srfprogtest(); | ||
@@ -481,5 +504,2 @@ if (!srfprogfound) { | ||
/* Clone file */ | ||
systemSync('cp ' + binfile + ' ' + filename); | ||
var hexnumber = function (n) { | ||
@@ -496,7 +516,4 @@ return ("0" + Number(n).toString(16)).slice(-2); | ||
/* Patch file */ | ||
var OFFSET_NVSTORAGE = 126976; /* 0x1f000 Nvstorage: 124-128 */ | ||
var signature = 'ed55ceac'; /* uint32_t 'acce55ed' */ | ||
var hex = NVSTORAGE_SIGNATURE; | ||
var hex = signature; | ||
/* xid */ | ||
@@ -534,7 +551,11 @@ key = 'xid'; | ||
hex += '00'; /* End of nvstorage */ | ||
var nvstoragedata = new Buffer(hex, 'hex'); | ||
var data = new Buffer(hex, 'hex'); | ||
fs.writeFileSync(filename + '.nvstorage', data); | ||
var cmd = 'cat ' + filename + '.nvstorage | dd of=' + filename + ' bs=1 seek=' + OFFSET_NVSTORAGE + ' count=' + data.length + ' conv=notrunc status=none'; | ||
systemSync(cmd); | ||
/* write patched firmware file to disk */ | ||
var filedata = fs.readFileSync(binfile); | ||
for(var i = 0; i < nvstoragedata.length; i++) { | ||
filedata[NVSTORAGE_OFFSET + i] = nvstoragedata[i]; | ||
} | ||
fs.writeFileSync(filename, filedata); | ||
return true; | ||
@@ -608,4 +629,7 @@ } catch (err) { | ||
try { | ||
var results = systemSync('md5sum -b ' + filename); | ||
return results.trim().split(' ')[0]; | ||
var filedata = fs.readFileSync(filename); | ||
var valuemd5 = crypto.createHash('md5').update(filedata).digest(); | ||
if (valuemd5) { | ||
return valuemd5.toString('hex'); | ||
} | ||
} catch (err) { | ||
@@ -618,3 +642,3 @@ warn('Error reading MD5 of ' + filename + '\n'); | ||
try { | ||
var results = systemSync('srfprog -t lsidx(0) -ls auto'); | ||
var results = systemSync(COMMAND_SCAN); | ||
var arr = results.split('Chip: '); | ||
@@ -637,3 +661,3 @@ if (arr[1]) { | ||
try { | ||
systemSync('srfprog -ls'); | ||
systemSync(COMMAND_TEST); | ||
return true; | ||
@@ -647,3 +671,3 @@ } catch (err) { | ||
try { | ||
var results = systemSync('srfprog -t lsidx(0) -r macpri'); | ||
var results = systemSync(COMMAND_READMAC); | ||
var arr = results.split('\n'); | ||
@@ -679,3 +703,3 @@ var next = false; | ||
try { | ||
systemSync('srfprog -t lsidx(0) -e forced'); | ||
systemSync(COMMAND_ERASE); | ||
progress('Erased device'); | ||
@@ -702,3 +726,5 @@ return true; | ||
progress('Programming device...'); | ||
systemSync('srfprog -t lsidx(0) --program -a 0x000000 --verify --file ' + filename + ''); | ||
var cmd = COMMAND_PROGRAM; | ||
cmd = cmd.replace(/%FILENAME%/g, filename); | ||
systemSync(cmd); | ||
progress('Programmed device...'); | ||
@@ -711,10 +737,12 @@ return true; | ||
/*---------------------------------------------------------------------------*/ | ||
function flashconnecteddevice(device) { | ||
process.stdout.write('\t\tErasing... '); | ||
var ok = erase(); | ||
if (ok) { | ||
process.stdout.write('\r\t\tErasing... done\n'); | ||
} else { | ||
process.stdout.write('\r\t\tErasing... error\n'); | ||
return; | ||
function flashconnecteddevice(device, skiperase) { | ||
if (skiperase !== true) { | ||
process.stdout.write('\t\tErasing... '); | ||
var ok = erase(); | ||
if (ok) { | ||
process.stdout.write('\r\t\tErasing... done\n'); | ||
} else { | ||
process.stdout.write('\r\t\tErasing... error\n'); | ||
return; | ||
} | ||
} | ||
@@ -767,2 +795,23 @@ | ||
/*---------------------------------------------------------------------------*/ | ||
function suggestdevicename(thsqdevices) { | ||
/* Print next suggest devicename */ | ||
var existingnames = {}; /* index devicename */ | ||
var suffix = 0; | ||
if (!devicenameprefix) { | ||
return; | ||
} | ||
Object.keys(thsqdevices).map(function (unique) { | ||
var device = thsqdevices[unique]; | ||
existingnames[devicename(device)] = true; | ||
}); | ||
suffix = 0; | ||
while (existingnames[devicenameprefix + suffix]) { | ||
suffix++; | ||
} | ||
return devicenameprefix + suffix; | ||
} | ||
/*---------------------------------------------------------------------------*/ | ||
function countstr(arr, str, footer) { | ||
@@ -827,2 +876,7 @@ var count; | ||
if (devicenameprefix) { | ||
console.log("Next successfully flashed device will be named:", suggestdevicename(thsqdevices)); | ||
console.log(""); | ||
} | ||
console.log("Menu options:"); | ||
@@ -864,2 +918,11 @@ console.log(" 1-" + xidarr.length + " : generate and flash devices in given network"); | ||
if (ok) { | ||
if (devicenameprefix) { | ||
/* Give device a devicename */ | ||
var name = suggestdevicename(existing); | ||
if (name) { | ||
thsq.setVariable(auth, 's', 'name', name); | ||
console.log('Device name of new device:', name); | ||
} | ||
} | ||
claim(auth, function() { | ||
@@ -881,2 +944,11 @@ /* New device created, flashed, and claimed */ | ||
if (ok) { | ||
if (devicenameprefix) { | ||
/* Give device a devicename */ | ||
var name = suggestdevicename(existing); | ||
if (name) { | ||
thsq.setVariable(auth, 's', 'name', name); | ||
console.log('Device name of new device:', name); | ||
} | ||
} | ||
claim(auth, function() { | ||
@@ -897,2 +969,11 @@ /* New device created, flashed, and claimed */ | ||
if (ok) { | ||
if (devicenameprefix) { | ||
/* Give device a devicename */ | ||
var name = suggestdevicename(existing); | ||
if (name) { | ||
thsq.setVariable(auth, 's', 'name', name); | ||
console.log('Device name of new device:', name); | ||
} | ||
} | ||
claim(auth, function() { | ||
@@ -1037,3 +1118,3 @@ /* New device created, flashed, and claimed */ | ||
} else { | ||
var ok = flashconnecteddevice(matchingdevice); | ||
var ok = flashconnecteddevice(matchingdevice, true); | ||
if (ok) { | ||
@@ -1040,0 +1121,0 @@ console.log('Device ' + devicename(matchingdevice) + ' was reflashed: device id ' + matchingdevice.id); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43647
1047