Remove header patching (#86)

pull/34/head
Paulus Schoutsen 3 years ago committed by GitHub
parent 48bba2fde1
commit 6cc74dd684
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -174,6 +174,7 @@ function formatMacAddr(macAddr) {
async function clickConnect() { async function clickConnect() {
if (espStub) { if (espStub) {
await espStub.disconnect(); await espStub.disconnect();
await espStub.port.close();
toggleUIConnected(false); toggleUIConnected(false);
espStub = undefined; espStub = undefined;
return; return;

@ -31,20 +31,6 @@ export const ESP32_FLASH_SIZES = {
"64MB": 0x1a, "64MB": 0x1a,
}; };
export const FLASH_MODES = {
qio: 0,
qout: 1,
dio: 2,
dout: 3,
};
export const FLASH_FREQUENCIES = {
"40m": 0,
"26m": 1,
"20m": 2,
"80m": 0xf,
};
export const DETECTED_FLASH_SIZES = { export const DETECTED_FLASH_SIZES = {
0x12: "256KB", 0x12: "256KB",
0x13: "512KB", 0x13: "512KB",
@ -57,28 +43,12 @@ export const DETECTED_FLASH_SIZES = {
0x1a: "64MB", 0x1a: "64MB",
}; };
export const getFlashSizes = (chipFamily: ChipFamily) => {
switch (chipFamily) {
case CHIP_FAMILY_ESP32:
return ESP32_FLASH_SIZES;
case CHIP_FAMILY_ESP32S2:
return ESP32_FLASH_SIZES;
case CHIP_FAMILY_ESP8266:
return FLASH_SIZES;
case CHIP_FAMILY_ESP32C3:
return ESP32_FLASH_SIZES;
default:
return FLASH_SIZES;
}
};
export const FLASH_WRITE_SIZE = 0x400; export const FLASH_WRITE_SIZE = 0x400;
export const STUB_FLASH_WRITE_SIZE = 0x4000; export const STUB_FLASH_WRITE_SIZE = 0x4000;
export const FLASH_SECTOR_SIZE = 0x1000; // Flash sector size, minimum unit of erase. export const FLASH_SECTOR_SIZE = 0x1000; // Flash sector size, minimum unit of erase.
export const ESP_ROM_BAUD = 115200; export const ESP_ROM_BAUD = 115200;
export const ESP32_BOOTLOADER_FLASH_OFFSET = 0x1000; export const ESP32_BOOTLOADER_FLASH_OFFSET = 0x1000;
export const BOOTLOADER_FLASH_OFFSET = 0x0; export const BOOTLOADER_FLASH_OFFSET = 0x0;
export const ESP_IMAGE_MAGIC = 0xe9;
export const ESP32_SPI_REG_BASE = 0x3ff42000; export const ESP32_SPI_REG_BASE = 0x3ff42000;
export const ESP32_SPI_USR_OFFS = 0x1c; export const ESP32_SPI_USR_OFFS = 0x1c;

@ -37,10 +37,6 @@ import {
ESP_FLASH_DEFL_END, ESP_FLASH_DEFL_END,
ESP32_BOOTLOADER_FLASH_OFFSET, ESP32_BOOTLOADER_FLASH_OFFSET,
BOOTLOADER_FLASH_OFFSET, BOOTLOADER_FLASH_OFFSET,
ESP_IMAGE_MAGIC,
getFlashSizes,
FLASH_FREQUENCIES,
FLASH_MODES,
getSpiFlashAddresses, getSpiFlashAddresses,
SpiFlashAddresses, SpiFlashAddresses,
getUartDateRegAddress, getUartDateRegAddress,
@ -546,7 +542,20 @@ export class ESPLoader extends EventTarget {
offset = 0, offset = 0,
compress = false compress = false
) { ) {
this.updateImageFlashParams(offset, binaryData); if (binaryData.byteLength >= 8) {
// unpack the (potential) image header
var header = Array.from(new Uint8Array(binaryData, 0, 4));
let headerMagic = header[0];
let headerFlashMode = header[2];
let heatherFlashSizeFreq = header[3];
this.logger.debug(
`Image header, Magic=${toHex(headerMagic)}, FlashMode=${toHex(
headerFlashMode
)}, FlashSizeFreq=${toHex(heatherFlashSizeFreq)}`
);
}
let uncompressedFilesize = binaryData.byteLength; let uncompressedFilesize = binaryData.byteLength;
let compressedFilesize = 0; let compressedFilesize = 0;
@ -766,83 +775,6 @@ export class ESPLoader extends EventTarget {
return BOOTLOADER_FLASH_OFFSET; return BOOTLOADER_FLASH_OFFSET;
} }
updateImageFlashParams(offset: number, image: ArrayBuffer) {
// Modify the flash mode & size bytes if this looks like an executable bootloader image
if (image.byteLength < 8) {
return image; //# not long enough to be a bootloader image
}
// unpack the (potential) image header
var header = Array.from(new Uint8Array(image, 0, 4));
let headerMagic = header[0];
let headerFlashMode = header[2];
let heatherFlashSizeFreq = header[3];
this.logger.debug(
`Image header, Magic=${toHex(headerMagic)}, FlashMode=${toHex(
headerFlashMode
)}, FlashSizeFreq=${toHex(heatherFlashSizeFreq)}`
);
if (offset != this.getBootloaderOffset()) {
return image; // not flashing bootloader offset, so don't modify this image
}
// easy check if this is an image: does it start with a magic byte?
if (headerMagic != ESP_IMAGE_MAGIC) {
this.logger.log(
"Warning: Image file at %s doesn't look like an image file, so not changing any flash settings.",
toHex(offset, 4)
);
return image;
}
// make sure this really is an image, and not just data that
// starts with esp.ESP_IMAGE_MAGIC (mostly a problem for encrypted
// images that happen to start with a magic byte
// TODO Implement this test from esptool.py
/*
try:
test_image = esp.BOOTLOADER_IMAGE(io.BytesIO(image))
test_image.verify()
except Exception:
print("Warning: Image file at 0x%x is not a valid %s image, so not changing any flash settings." % (address, esp.CHIP_NAME))
return image
*/
this.logger.log("Image being flashed is a bootloader");
// For now we always select dio, a common value supported by many flash chips and ESP boards
let flashMode = FLASH_MODES["dio"];
// For now we always select 40m, a common value supported by many flash chips and ESP boards
let flashFreq = FLASH_FREQUENCIES["40m"];
let flashSize = getFlashSizes(this.getChipFamily())[
this.flashSize ? this.flashSize : "4MB"
]; // If size was autodetected we use it otherwise we default to 4MB
let flashParams = pack("BB", flashMode, flashSize + flashFreq);
let imageFlashParams = new Uint8Array(image, 2, 2);
if (
flashParams[0] != imageFlashParams[0] ||
flashParams[1] != imageFlashParams[1]
) {
imageFlashParams[0] = flashParams[0];
imageFlashParams[1] = flashParams[1];
this.logger.log(
`Patching Flash parameters header bytes to ${toHex(
flashParams[0],
2
)} ${toHex(flashParams[1], 2)}`
);
} else {
this.logger.log("Flash parameters header did not need patching.");
}
return image;
}
async flashId() { async flashId() {
let SPIFLASH_RDID = 0x9f; let SPIFLASH_RDID = 0x9f;
let result = await this.runSpiFlashCommand(SPIFLASH_RDID, [], 24); let result = await this.runSpiFlashCommand(SPIFLASH_RDID, [], 24);

Loading…
Cancel
Save