Skip to content

Commit

Permalink
minor: allow passing custom assets to start SB protected CM4
Browse files Browse the repository at this point in the history
  • Loading branch information
aethernet committed Jun 7, 2024
1 parent fa41831 commit 71b05d1
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
extends: ["./node_modules/@balena/lint/config/.eslintrc.js"],
root: true,
ignorePatterns: ["node_modules/", "dist/", "examples/"],
ignorePatterns: ["node_modules/", "dist/", "tests/", "examples"],
rules: {
"@typescript-eslint/no-floating-promises": "off",
"no-bitwise": "off",
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
build
*~
node_modules
yalc.lock
.yalc
6 changes: 4 additions & 2 deletions examples/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ import { platform } from 'os';

import { scanner } from '../lib/';

async function main() {
const bootImageFolder = String(process.argv.slice(2));

async function main(usbBootExtraFolder?: string) {
const adapters: scanner.adapters.Adapter[] = [
new scanner.adapters.BlockDeviceAdapter({
includeSystemDrives: () => true,
}),
new scanner.adapters.UsbbootDeviceAdapter(),
new scanner.adapters.UsbbootDeviceAdapter(bootImageFolder),
];
if (platform() === 'win32') {
if (scanner.adapters.DriverlessDeviceAdapter !== undefined) {
Expand Down
56 changes: 51 additions & 5 deletions examples/usbboot.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2019 balena.io
* Copyright 2024 balena.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,19 +14,61 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { argv } from 'process';

/**
* Unlocking with secureboot
*
* If you need to unlock an cm4 with secureboot, you need to provide a signed boot-image.
* Pass the bootImageFolder flag with the path to the folder containing the signed boot-image
*/

import ProgressBar = require('progress');

import { scanner, sourceDestination } from '../lib/';

import { pipeSourceToDestinationsWithProgressBar } from './utils';

// Parse command line arguments
const args = process.argv.slice(2); // removes 'node' and the script name from the args
const flags: any = {};

args.forEach((arg: string, index: number) => {
// Check if the argument is a flag in the format --flag=value
if (arg.startsWith('--')) {
const key: string = arg.substring(2);
const value: string = args[index + 1];
flags[key] = value;
}
});

if (!flags.source) {
console.log("No source has been provided, won't try to flash anything");
}

if (flags.bootImageFolder !== '') {
console.log(`Using external folder ${flags['bootImageFolder']}`);
}

if (flags.help) {
console.log(
'Usage: ts-node usbboot.js --bootImageFolder <bootImageFolder> --source <image>',
);
console.log(
'Beware, `source` image will be flashed to all USBboot devices, so make sure you know what you are doing',
);
console.log(
'To unlock a secureboot CM4, set the bootImageFodler to the folder containing a signed boot-image and config.txt',
);
process.exit(0);
}

async function main() {
const bootImageFolder = flags.bootImageFolder;
const adapters: scanner.adapters.Adapter[] = [
new scanner.adapters.BlockDeviceAdapter({
includeSystemDrives: () => false,
}),
new scanner.adapters.UsbbootDeviceAdapter(),
new scanner.adapters.UsbbootDeviceAdapter(bootImageFolder),
];
const deviceScanner = new scanner.Scanner(adapters);
console.log('Waiting for one compute module');
Expand Down Expand Up @@ -74,6 +117,8 @@ async function main() {
drive instanceof sourceDestination.BlockDevice &&
drive.description === 'Compute Module'
) {
drive.oWrite = true;
drive.oDirect = true;
resolve(drive);
deviceScanner.removeListener('attach', onAttach);
}
Expand All @@ -84,11 +129,12 @@ async function main() {
);
deviceScanner.stop();

if (argv.length >= 3) {
console.log(`Writing image ${argv[2]}`);
if (flags.source) {
console.log(JSON.stringify(dest));
console.log(`Writing image ${flags.source} to ${dest.path}`);
const source: sourceDestination.SourceDestination =
new sourceDestination.File({
path: argv[2],
path: flags.source,
});
void pipeSourceToDestinationsWithProgressBar({
source,
Expand Down
4 changes: 2 additions & 2 deletions lib/scanner/adapters/usbboot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ export class UsbbootDeviceAdapter extends Adapter {
private drives: Map<UsbbootDevice, UsbbootDrive> = new Map();
private scanner?: UsbbootScannerType;

constructor() {
constructor(usbBootExtraFolder?: string | undefined) {
super();
const rpiUsbboot = getRaspberrypiUsbboot();
if (rpiUsbboot !== undefined) {
this.scanner = new rpiUsbboot.UsbbootScanner();
this.scanner = new rpiUsbboot.UsbbootScanner(usbBootExtraFolder);
this.scanner.on('attach', this.onAttach.bind(this));
this.scanner.on('detach', this.onDetach.bind(this));
this.scanner.on('ready', this.emit.bind(this, 'ready'));
Expand Down
27 changes: 15 additions & 12 deletions package-lock.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 @@ -64,7 +64,7 @@
"lzma-native": "^8.0.6",
"minimatch": "^9.0.3",
"mountutils": "^1.3.20",
"node-raspberrypi-usbboot": "1.0.7",
"node-raspberrypi-usbboot": "1.1.0",
"outdent": "^0.8.0",
"partitioninfo": "^6.0.2",
"rwmutex": "^1.0.0",
Expand Down

0 comments on commit 71b05d1

Please sign in to comment.