images: firmware: Accept extracted factory images

This commit is contained in:
Danny Lin 2021-12-18 03:06:20 -08:00
parent 204ecec1cd
commit 860c21c077

View file

@ -7,9 +7,8 @@ export const ANDROID_INFO = 'android-info.txt'
export type FirmwareImages = Map<string, ArrayBuffer> export type FirmwareImages = Map<string, ArrayBuffer>
export async function extractFactoryFirmware(zipPath: string) { async function extractFactoryZipFirmware(path: string, images: FirmwareImages) {
let reader = new NodeFileReader(zipPath) let reader = new NodeFileReader(path)
let images: FirmwareImages = new Map<string, ArrayBuffer>()
try { try {
let { entries } = await unzipit.unzip(reader) let { entries } = await unzipit.unzip(reader)
@ -22,13 +21,36 @@ export async function extractFactoryFirmware(zipPath: string) {
images.set('radio.img', await entry.arrayBuffer()) images.set('radio.img', await entry.arrayBuffer())
} }
} }
return images
} finally { } finally {
await reader.close() await reader.close()
} }
} }
async function extractFactoryDirFirmware(path: string, images: FirmwareImages) {
for (let file of await fs.readdir(path)) {
if (file.startsWith('bootloader-')) {
let buf = await fs.readFile(`${path}/${file}`)
images.set('bootloader.img', buf.buffer)
} else if (file.startsWith('radio-')) {
let buf = await fs.readFile(`${path}/${file}`)
images.set('radio.img', buf.buffer)
}
}
}
// Path can be a directory or zip
export async function extractFactoryFirmware(path: string) {
let images: FirmwareImages = new Map<string, ArrayBuffer>()
if ((await fs.stat(path)).isDirectory()) {
await extractFactoryDirFirmware(path, images)
} else {
await extractFactoryZipFirmware(path, images)
}
return images
}
export async function writeFirmwareImages(images: FirmwareImages, fwDir: string) { export async function writeFirmwareImages(images: FirmwareImages, fwDir: string) {
let paths = [] let paths = []
for (let [name, buffer] of images.entries()) { for (let [name, buffer] of images.entries()) {