diff --git a/js/script.js b/js/script.js index beb89918..efd7b666 100644 --- a/js/script.js +++ b/js/script.js @@ -174,6 +174,7 @@ function formatMacAddr(macAddr) { async function clickConnect() { if (espStub) { await espStub.disconnect(); + await espStub.port.close(); toggleUIConnected(false); espStub = undefined; return; diff --git a/src/const.ts b/src/const.ts index afaa4586..2773a6c6 100644 --- a/src/const.ts +++ b/src/const.ts @@ -31,20 +31,6 @@ export const ESP32_FLASH_SIZES = { "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 = { 0x12: "256KB", 0x13: "512KB", @@ -57,28 +43,12 @@ export const DETECTED_FLASH_SIZES = { 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 STUB_FLASH_WRITE_SIZE = 0x4000; export const FLASH_SECTOR_SIZE = 0x1000; // Flash sector size, minimum unit of erase. export const ESP_ROM_BAUD = 115200; export const ESP32_BOOTLOADER_FLASH_OFFSET = 0x1000; export const BOOTLOADER_FLASH_OFFSET = 0x0; -export const ESP_IMAGE_MAGIC = 0xe9; export const ESP32_SPI_REG_BASE = 0x3ff42000; export const ESP32_SPI_USR_OFFS = 0x1c; diff --git a/src/esp_loader.ts b/src/esp_loader.ts index 1c47a27f..6538757f 100644 --- a/src/esp_loader.ts +++ b/src/esp_loader.ts @@ -37,10 +37,6 @@ import { ESP_FLASH_DEFL_END, ESP32_BOOTLOADER_FLASH_OFFSET, BOOTLOADER_FLASH_OFFSET, - ESP_IMAGE_MAGIC, - getFlashSizes, - FLASH_FREQUENCIES, - FLASH_MODES, getSpiFlashAddresses, SpiFlashAddresses, getUartDateRegAddress, @@ -546,7 +542,20 @@ export class ESPLoader extends EventTarget { offset = 0, 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 compressedFilesize = 0; @@ -766,83 +775,6 @@ export class ESPLoader extends EventTarget { 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() { let SPIFLASH_RDID = 0x9f; let result = await this.runSpiFlashCommand(SPIFLASH_RDID, [], 24);