Skip to content

Commit

Permalink
firmware: validate checksum while flashing
Browse files Browse the repository at this point in the history
Since we are using the checksum message for throttling flash speed
anyway, we can validate the checksum during the flash process to
catch errors earlier.

Issue: pybricks/support#433
  • Loading branch information
dlech committed Aug 13, 2021
1 parent 59db5f4 commit 7ddeed5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/firmware/sagas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,7 @@ describe('flashFirmware', () => {

const dummyPayload = new ArrayBuffer(0);
let id = 2;

for (let count = 1, offset = 0; ; count++, offset += 14) {
action = await saga.take();
expect(action).toEqual(
Expand Down Expand Up @@ -1413,7 +1414,7 @@ describe('flashFirmware', () => {
expect(action).toEqual(checksumRequest(++id));

saga.put(didRequest(id));
saga.put(checksumResponse(0));
saga.put(checksumResponse(0x9b));
}
}

Expand Down
44 changes: 36 additions & 8 deletions src/firmware/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import {
compile,
} from '../mpy/actions';
import { RootState } from '../reducers';
import { defined, maybe } from '../utils';
import { defined, hex, maybe } from '../utils';
import { fmod, sumComplement32 } from '../utils/math';
import {
FailToFinishReasonType,
Expand Down Expand Up @@ -379,8 +379,16 @@ function* flashFirmware(action: FlashFirmwareFlashAction): Generator {
// 14 is "safe" size for all hubs
const maxDataSize = MaxProgramFlashSize.get(info.hubType) || 14;

let runningChecksum = 0xff;

for (let count = 1, offset = 0; ; count++) {
const payload = firmware.slice(offset, offset + maxDataSize);

runningChecksum = payload.reduce(
(prev, curr) => prev ^ curr,
runningChecksum,
);

const programAction = yield* put(
programRequest(
nextMessageId(),
Expand All @@ -405,13 +413,33 @@ function* flashFirmware(action: FlashFirmwareFlashAction): Generator {
// the hub is not known and could vary by device.
if (count % 10 === 0) {
const checksumAction = yield* put(checksumRequest(nextMessageId()));
yield* all({

const { response } = yield* all({
sent: waitForDidRequest(checksumAction.id),
checksum: waitForResponse<BootloaderChecksumResponseAction>(
response: waitForResponse<BootloaderChecksumResponseAction>(
BootloaderResponseActionType.Checksum,
5000,
),
});

if (response.checksum !== runningChecksum) {
// istanbul ignore next
if (process.env.NODE_ENV !== 'test') {
console.error(
`checksum: got ${hex(response.checksum, 2)} expected ${hex(
runningChecksum,
2,
)}`,
);
}
yield* put(
didFailToFinish(
FailToFinishReasonType.HubError,
HubError.ChecksumMismatch,
),
);
yield* disconnectAndCancel();
}
}
}

Expand All @@ -430,14 +458,14 @@ function* flashFirmware(action: FlashFirmwareFlashAction): Generator {
yield* disconnectAndCancel();
}

const checksum = firmware.reduce((prev, curr) => prev ^ curr, 0xff);
if (flash.checksum !== checksum) {
if (flash.checksum !== runningChecksum) {
// istanbul ignore next
if (process.env.NODE_ENV !== 'test') {
console.error(
'checksum:',
flash.checksum.toString(16).padStart(2, '0').padStart(4, '0x'),
checksum.toString(16).padStart(2, '0').padStart(4, '0x'),
`final checksum: got ${hex(flash.checksum, 2)} expected ${hex(
runningChecksum,
2,
)}`,
);
}
yield* put(
Expand Down

0 comments on commit 7ddeed5

Please sign in to comment.