Skip to content

Commit

Permalink
Create a protected binary in the binary module
Browse files Browse the repository at this point in the history
  • Loading branch information
keeramis committed Jun 18, 2024
1 parent 6a954ea commit 6d6b7a0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
11 changes: 11 additions & 0 deletions src/cli/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,16 @@ module.exports = ({ commandProcessor, root }) => {
}
});

commandProcessor.createCommand(binary, 'enable-device-protection', 'Create a protected bootloader binary', {
params: '<filename>',
handler: (args) => {
const BinaryCommand = require('../cmd/binary');
return new BinaryCommand().createProtectedBinary({ file: args.params.filename, verbose: true });
},
examples: {
'$0 $command bootloader.bin': 'Provide bootloader binary to protect'
}
});

return binary;
};
11 changes: 0 additions & 11 deletions src/cli/device-protection.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,6 @@ module.exports = ({ commandProcessor, root }) => {
}
});

commandProcessor.createCommand(deviceProtection, 'protect', 'Adds device-protection to your bootloader binary', {
params: '<file>',
handler: (args) => {
const DeviceProtectionCommands = require('../cmd/device-protection');
return new DeviceProtectionCommands().protectBinary({ file: args.params.file, verbose: true });
},
examples: {
'$0 $command myBootloader.bin': 'Adds device-protection to your bootloader binary'
}
});

return deviceProtection;
};

21 changes: 20 additions & 1 deletion src/cmd/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const fs = require('fs-extra');
const path = require('path');
const VError = require('verror');
const chalk = require('chalk');
const { HalModuleParser: Parser, unpackApplicationAndAssetBundle, isAssetValid } = require('binary-version-reader');
const { HalModuleParser: Parser, unpackApplicationAndAssetBundle, isAssetValid, createProtectedModule } = require('binary-version-reader');
const utilities = require('../lib/utilities');
const ensureError = utilities.ensureError;

Expand All @@ -46,6 +46,25 @@ class BinaryCommand {
await this._verifyBundle(parsedAppInfo, assets);
}

async createProtectedBinary({ file, verbose=true }) {
await this._checkFile(file);

const fileName = path.basename(file);
const resBinaryName = fileName.replace('.bin', '-protected.bin');
const resBinaryPath = path.join(path.dirname(file), resBinaryName);

const binary = await fs.readFile(file);
const protectedBinary = await createProtectedModule(binary);
await fs.writeFile(resBinaryPath, protectedBinary);

if (verbose) {
console.log(`Protected binary saved at ${resBinaryPath}`);
}

return resBinaryPath;
}


async _checkFile(file) {
try {
await fs.access(file);
Expand Down
6 changes: 4 additions & 2 deletions src/cmd/device-protection.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const { downloadDeviceOsVersionBinaries } = require('../lib/device-os-version-ut
const FlashCommand = require('./flash');
const { platformForId } = require('../lib/platform');
const chalk = require('chalk');
const BinaryCommand = require('./binary');

module.exports = class DeviceProtectionCommands extends CLICommandBase {
constructor({ ui } = {}) {
Expand Down Expand Up @@ -152,7 +153,7 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
* It then downloads the device OS version binaries and returns the path to the bootloader binary.
*
* @async
* @returns {Promise<string>} The file path to the downloaded bootloader binary.
* @returns {Promise<string>} The file path to the downloaded bootloader binary.protectBinary
* @throws {Error} Throws an error if any of the async operations fail.
*/
async _downloadBootloader() {
Expand Down Expand Up @@ -208,7 +209,8 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
if (!s.protected && !s.overridden && deviceProtectionActiveInProduct) {
if (!protectedBinary) {
const localBootloaderPath = await this._downloadBootloader();
protectedBinary = await this.protectBinary({ file: localBootloaderPath, verbose: false });
const binary = new BinaryCommand();
protectedBinary = await binary.createProtectedBinary({ file: localBootloaderPath, verbose: false });
}
await this._flashBootloader(protectedBinary, 'enable');
addToOutput.push(`${deviceStr} is now a protected device.${os.EOL}`);
Expand Down

0 comments on commit 6d6b7a0

Please sign in to comment.