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 May 9, 2024
1 parent fa41831 commit 6ee4778
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
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
43 changes: 40 additions & 3 deletions examples/usbboot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,47 @@ 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 @@ -66,7 +101,9 @@ async function main() {
});
progressBar.terminate();
computeModule.removeListener('progress', onProgress);

console.log('Waiting for compute module to reattach as a block device');

const dest = await new Promise(
(resolve: (drive: sourceDestination.BlockDevice) => void, reject) => {
function onAttach(drive: scanner.adapters.AdapterSourceDestination) {
Expand All @@ -84,11 +121,11 @@ async function main() {
);
deviceScanner.stop();

if (argv.length >= 3) {
if (flags.source) {
console.log(`Writing image ${argv[2]}`);
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);

Check failure on line 34 in lib/scanner/adapters/usbboot.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test npm (18.x)

Expected 0 arguments, but got 1.

Check failure on line 34 in lib/scanner/adapters/usbboot.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test npm (20.x)

Expected 0 arguments, but got 1.

Check failure on line 34 in lib/scanner/adapters/usbboot.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test npm (21.x)

Expected 0 arguments, but got 1.
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

0 comments on commit 6ee4778

Please sign in to comment.