Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/sc 123873/check hash #693

Merged
merged 13 commits into from
Nov 30, 2023
65 changes: 42 additions & 23 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
"sinon-chai": "^3.3.0"
},
"optionalDependencies": {
"particle-usb": "^2.4.1",
"particle-usb": "^2.5.0",
"serialport": "^9.2.8"
},
"engines": {
Expand Down
22 changes: 12 additions & 10 deletions src/cmd/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ class BinaryCommand {
async inspectBinary(file) {
await this._checkFile(file);
const extractedFiles = await this._extractFiles(file);
const parsedBinaryInfo = await this._parseApplicationBinary(extractedFiles.application);
await this._verifyBundle(parsedBinaryInfo, extractedFiles.assets);
const parsedAppInfo = await this._parseApplicationBinary(extractedFiles.application);
const assets = extractedFiles.assets;
await this._verifyBundle(parsedAppInfo, assets);
}

async _checkFile(file) {
Expand Down Expand Up @@ -85,21 +86,22 @@ class BinaryCommand {
return fileInfo;
}

async _verifyBundle(binaryFileInfo, assets) {
if (binaryFileInfo.assets && assets.length){
async _verifyBundle(appInfo, assets) {
const appAssets = appInfo.assets;
if (appAssets && assets.length > 0) {
console.log('It depends on assets:');
for (const assetInfo of binaryFileInfo.assets) {
const asset = assets.find((asset) => asset.name === assetInfo.name);
for (const appAsset of appAssets) {
const asset = assets.find((bundleAsset) => bundleAsset.name === appAsset.name);
if (asset) {
const valid = isAssetValid(asset.data, assetInfo);
const valid = isAssetValid(asset.data, appAsset);

if (valid) {
console.log(' ' + chalk.bold(assetInfo.name) + ' (hash ' + assetInfo.hash + ')');
console.log(' ' + chalk.bold(appAsset.name) + ' (hash ' + appAsset.hash + ')');
} else {
console.log(chalk.red(' ' + assetInfo.name + ' failed' + ' (hash should be ' + assetInfo.hash + ')'));
console.log(chalk.red(' ' + appAsset.name + ' failed' + ' (hash should be ' + appAsset.hash + ')'));
}
} else {
console.log(chalk.red(' ' + assetInfo.name + ' failed' + ' (hash should be ' + assetInfo.hash + ' but is not in the bundle)'));
console.log(chalk.red(' ' + appAsset.name + ' failed' + ' (hash should be ' + appAsset.hash + ' but is not in the bundle)'));
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No functional change here. Felt like the readability of this code could be improved.

}
}
Expand Down
2 changes: 2 additions & 0 deletions src/cmd/cloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ module.exports = class CloudCommand extends CLICommandBase {
}

await this._doFlash({ product, deviceId: device, fileMapping });

this.ui.stdout.write(`Flash success!${os.EOL}`);
} catch (error) {
const message = `Failed to flash ${device}`;
throw createAPIErrorResult({ error, message });
Expand Down
90 changes: 16 additions & 74 deletions src/cmd/flash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const fs = require('fs-extra');
const os = require('os');
const ParticleApi = require('./api');
const { ModuleInfo } = require('binary-version-reader');
const { errors: { usageError } } = require('../app/command-processor');
Expand Down Expand Up @@ -58,8 +57,6 @@ module.exports = class FlashCommand extends CLICommandBase {
} else {
await this.flashCloud({ device, files, target });
}

this.ui.write('Flash success!');
}

async flashOverUsb({ binary, factory }) {
Expand Down Expand Up @@ -125,41 +122,49 @@ module.exports = class FlashCommand extends CLICommandBase {
const { api, auth } = this._particleApi();
const device = await usbUtils.getOneUsbDevice({ idOrName: deviceIdOrName, api, auth, ui: this.ui });

const platformName = platformForId(device.platformId).name;
const platformId = device.platformId;
const platformName = platformForId(platformId).name;
const currentDeviceOsVersion = device.firmwareVersion;

this.ui.write(`Flashing ${platformName} ${deviceIdOrName || device.id}`);

validateDFUSupport({ device, ui: this.ui });

let { skipDeviceOSFlash, files: filesToFlash } = await this._prepareFilesToFlash({
knownApp,
parsedFiles,
platformId: device.platformId,
platformId,
platformName,
target
});

filesToFlash = await this._processBundle({ filesToFlash });

const fileModules = await parseModulesToFlash({ files: filesToFlash });
await this._validateModulesForPlatform({ modules: fileModules, platformId: device.platformId, platformName });

await this._validateModulesForPlatform({ modules: fileModules, platformId, platformName });

const deviceOsBinaries = await this._getDeviceOsBinaries({
currentDeviceOsVersion: device.firmwareVersion,
currentDeviceOsVersion,
skipDeviceOSFlash,
target,
modules: fileModules,
platformId: device.platformId,
platformId,
applicationOnly
});
const deviceOsModules = await parseModulesToFlash({ files: deviceOsBinaries });
let modulesToFlash = [...fileModules, ...deviceOsModules];
modulesToFlash = filterModulesToFlash({ modules: modulesToFlash, platformId: device.platformId });
modulesToFlash = filterModulesToFlash({ modules: modulesToFlash, platformId });

const flashSteps = await createFlashSteps({
modules: modulesToFlash,
isInDfuMode: device.isInDfuMode,
platformId
});

const flashSteps = await createFlashSteps({ modules: modulesToFlash, isInDfuMode: device.isInDfuMode , platformId: device.platformId });
await flashFiles({ device, flashSteps, ui: this.ui });
}


async _analyzeFiles(files) {
const apps = knownAppNames();

Expand Down Expand Up @@ -388,67 +393,4 @@ module.exports = class FlashCommand extends CLICommandBase {
}
return { module: null, applicationDeviceOsVersion: null };
}

_createFlashProgress({ flashSteps }) {
const NORMAL_MULTIPLIER = 10; // flashing in normal mode is slower so count each byte more
const { isInteractive } = this.ui;
let progressBar;
if (isInteractive) {
progressBar = this.ui.createProgressBar();
// double the size to account for the erase and programming steps
const total = flashSteps.reduce((total, step) => total + step.data.length * 2 * (step.flashMode === 'normal' ? NORMAL_MULTIPLIER : 1), 0);
progressBar.start(total, 0, { description: 'Preparing to flash' });
}

let flashMultiplier = 1;
let eraseSize = 0;
let step = null;
let description;
return (payload) => {
switch (payload.event) {
case 'flash-file':
description = `Flashing ${payload.filename}`;
if (isInteractive) {
progressBar.update({ description });
} else {
this.ui.stdout.write(`${description}${os.EOL}`);
}
step = flashSteps.find(step => step.name === payload.filename);
flashMultiplier = step.flashMode === 'normal' ? NORMAL_MULTIPLIER : 1;
eraseSize = 0;
break;
case 'switch-mode':
description = `Switching device to ${payload.mode} mode`;
if (isInteractive) {
progressBar.update({ description });
} else {
this.ui.stdout.write(`${description}${os.EOL}`);
}
break;
case 'erased':
if (isInteractive) {
// In DFU, entire sectors are erased so the count of bytes can be higher than the actual size
// of the file. Ignore the extra bytes to avoid issues with the progress bar
if (step && eraseSize + payload.bytes > step.data.length) {
progressBar.increment((step.data.length - eraseSize) * flashMultiplier);
eraseSize = step.data.length;
} else {
progressBar.increment(payload.bytes * flashMultiplier);
eraseSize += payload.bytes;
}
}
break;
case 'downloaded':
if (isInteractive) {
progressBar.increment(payload.bytes * flashMultiplier);
}
break;
case 'finish':
if (isInteractive) {
progressBar.stop();
}
break;
}
};
}
};
2 changes: 1 addition & 1 deletion src/cmd/flash.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { expect, sinon } = require('../../test/setup');
const fs = require('fs-extra'); // Use fs-extra instead of fs
const fs = require('fs-extra');
const nock = require('nock');
const temp = require('temp').track();
const path = require('path');
Expand Down
Loading