Skip to content

Commit

Permalink
ImageWorker: combine unpack and verify image (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
incognitojam authored Aug 22, 2023
1 parent 0551167 commit b2ae513
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 60 deletions.
5 changes: 0 additions & 5 deletions src/app/Flash.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ const steps = {
bgColor: 'bg-blue-500',
icon: cloudDownload,
},
[Step.VERIFYING]: {
status: 'Verifying...',
bgColor: 'bg-blue-500',
icon: cloudDownload,
},
[Step.FLASHING]: {
status: 'Flashing device...',
description: 'Do not unplug your device until the process is complete.',
Expand Down
31 changes: 6 additions & 25 deletions src/utils/fastboot.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export const Step = {
CONNECTING: 2,
DOWNLOADING: 3,
UNPACKING: 4,
VERIFYING: 5,
FLASHING: 6,
ERASING: 7,
DONE: 8,
Expand Down Expand Up @@ -286,33 +285,15 @@ export function useFastboot() {
unpackImages()
.then(() => {
console.debug('[fastboot] Unpacked all images')
setStep(Step.VERIFYING)
})
.catch((err) => {
console.error('[fastboot] Unpack error', err)
setError(Error.UNPACK_FAILED)
})
break
}

case Step.VERIFYING: {
setProgress(0)

async function verifyImages() {
for await (const [image, onProgress] of withProgress(manifest.current, setProgress)) {
setMessage(`Verifying ${image.name}`)
await imageWorker.current.verifyImage(image, Comlink.proxy(onProgress))
}
}

verifyImages()
.then(() => {
console.debug('[fastboot] Verified all images')
setStep(Step.FLASHING)
})
.catch((err) => {
console.error('[fastboot] Verification error', err)
setError(Error.CHECKSUM_MISMATCH)
console.error('[fastboot] Unpack error', err)
if (err.startsWith('Checksum mismatch')) {
setError(Error.CHECKSUM_MISMATCH)
} else {
setError(Error.UNPACK_FAILED)
}
})
break
}
Expand Down
36 changes: 6 additions & 30 deletions src/workers/image.worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,16 @@ const imageWorker = {
},

/**
* Unpack a downloaded image archive.
* Unpack and verify a downloaded image archive.
*
* Throws an error if the checksum does not match.
*
* @param {Image} image
* @param {progressCallback} [onProgress]
* @returns {Promise<void>}
*/
async unpackImage(image, onProgress = undefined) {
const { archiveFileName, fileName } = image
const { archiveFileName, checksum: expectedChecksum, fileName } = image

let archiveFile
try {
Expand All @@ -124,13 +126,15 @@ const imageWorker = {
throw `Error opening output file handle: ${e}`
}

const shaObj = new jsSHA('SHA-256', 'UINT8ARRAY')
let complete
try {
const reader = archiveFile.stream().getReader()
await new Promise(async (resolve, reject) => {
const inflator = new pako.Inflate()
inflator.onData = function (chunk) {
writable.write(chunk)
shaObj.update(chunk)
}
inflator.onEnd = function (status) {
if (status) {
Expand Down Expand Up @@ -160,34 +164,6 @@ const imageWorker = {
} catch (e) {
throw `Error closing file handle: ${e}`
}
},

/**
* Verify the checksum of an image.
*
* @param {Image} image
* @param {progressCallback} [onProgress]
* @throws {string} If the checksum does not match
* @returns {Promise<void>}
*/
async verifyImage(image, onProgress = undefined) {
const { checksum: expectedChecksum, fileName } = image

let file
try {
const fileHandle = await root.getFileHandle(fileName, { create: false })
file = await fileHandle.getFile()
} catch (e) {
throw `Error opening file: ${e}`
}

const shaObj = new jsSHA('SHA-256', 'UINT8ARRAY')
const reader = file.stream().getReader()
await readChunks(reader, file.size, {
onChunk: (chunk) => shaObj.update(chunk),
onProgress,
})
onProgress?.(1)

const checksum = shaObj.getHash('HEX')
if (checksum !== expectedChecksum) {
Expand Down

0 comments on commit b2ae513

Please sign in to comment.