add detect chip with get security info - #197
Conversation
|
Download the artifacts for this pull request: |
8eb97db to
494c635
Compare
63630dd to
c2750cd
Compare
c2750cd to
aeb1f3d
Compare
|
PTAL @RushikeshPatange We need this to support ESP32 S31 |
Sure will review the changes |
RushikeshPatange
left a comment
There was a problem hiding this comment.
Note: The points below are based on a comparison of logs between esptool-js (current branch) and esptool.py (main), as reviewed by Claude. Please consider them accordingly, as per the scope of this MR.
A few points that might be worth considering:
-
main()may still encounter an issue with chips in Secure Download Mode (SDM). While the detection now handles SDM correctly,esploader.ts:1480subsequently callsgetChipDescription/readMacunconditionally, which useREAD_REG(unsupported in SDM), followed byrunStub. In Python,run_stubhandles this gracefully by warning and continuing without the stub. It might be worth considering skipping the register-read information and stub upload whenthis.secureDownloadModeistrue. -
H21/E22 flashing may still not work end-to-end.
getStubJsonByChipNamereturnsundefinedfor these chips, andrunStub(esploader.ts:1417) currently throws"Error loading Stub json"instead of falling back to ROM flashing. Python handles this by warning that the stub flasher is not yet supported on ESP32-H21/E22 and continuing without the stub. It might be helpful to have a similar graceful fallback here; otherwise, the newly added targets may still not be usable for these two chips. -
securityInfoCachedoes not appear to be invalidated on reconnect. A reconnect, including the retry insideidentifyChip, may therefore reuse the previously parsed information. This is harmless with the current flow, but it might be safer to either callgetSecurityInfo(cache=false)after reconnect or clear the cache inconnect(). -
Minor / pre-existing: The
usesUsbOtg-style checks appear to compare only the USB PID rather than the VID + PID (this is also mentioned in the S31 comment). Additionally, Python's verification in the non-detectingconnect()flow —"This chip is X, not Y — wrong chip argument?"— does not appear to be ported. Since esptool-js always autodetects the chip, I believe this is relatively low priority.
| @@ -0,0 +1,21 @@ | |||
| MIT License | |||
|
|
|||
| Copyright (c) 2025 Espressif Systems (Shanghai) CO LTD | |||
There was a problem hiding this comment.
Should we consider 2026 instead of 2025 ?
| same "printed page" as the copyright notice for easier | ||
| identification within third-party archives. | ||
|
|
||
| Copyright 2025 Espressif Systems (Shanghai) CO LTD |
There was a problem hiding this comment.
Should we consider 2026 instead of 2025 ?
| // throw new Error("Powering on flash in secure download mode"); | ||
| // } | ||
| if (loader.secureDownloadMode) { | ||
| throw new Error("Powering on flash in secure download mode"); |
There was a problem hiding this comment.
Not getting the semantics of "Does this operation allowed or not in SDM ?" from the error message
Should it be Powering on flash in secure download mode not allowed or something ?
| */ | ||
| export type FlashReadCallback = ((packet: Uint8Array, progress: number, totalSize: number) => void) | null; | ||
|
|
||
| export { SecurityInfo, SECURITY_INFO_FLAG_MAP, ParsedSecurityFlags } from "./types/securityInfo.js"; |
There was a problem hiding this comment.
This re-export seems to be redundant, should be removed from here and incorporated in the index barrel file
// esploader.ts — delete line 26 entirely
// index.ts
export { ESPLoader, FlashReadCallback } from "./esploader.js";
export { SecurityInfo, SECURITY_INFO_FLAG_MAP, ParsedSecurityFlags } from "./types/securityInfo.js";
// SecurityInfo and ParsedSecurityFlags are pure types, so export type { ... } would be more precise to have| * @param {Before} mode Reset mode used if a reconnect is required | ||
| * @param {number} attempts Connection attempts used if a reconnect is required | ||
| */ | ||
| private async identifyChip(mode: Before, attempts: number) { |
There was a problem hiding this comment.
Few thoughts on identifyChip method breakdown for better modularity, readability and reusability
-
identifyChip()is doing too many things. It currently handles chip-ID detection, magic-value fallback, the S2-in-SDM case, reconnect/retry, and updating loader state. It may be cleaner to split these into small helpers such as:romFromChipId(chipId)readSecureDownloadMode()identifyChipByMagic()applyDetectedChip(chip)
-
connect()andidentifyChip()are somewhat coupled.connect()callsidentifyChip(), which can callconnect()again for retry. This is currently safe because ofdetecting=false, but moving the retry logic to the caller would make the flow easier to understand. -
There is some duplicated SDM/security-info logic. A common
readSecureDownloadMode()helper could avoid duplication and also provide a single place to handle security-info cache invalidation after reconnect. -
The S2-SDM path creates another
ESP32S2ROMinstance. SinceCHIP_DEFS.esp32s2already exists in the target registry, it would be better to reuse that instance instead of creating a new one. This keeps all detection paths consistent. -
The biggest concern for me is using error messages for control flow. For example, checking
error.message === "unsupported command error"orstartsWith("Unexpected chip ID value"). A small change to the error message could break the detection flow. It would be more robust to introduce typed errors such asUnsupportedCommandErrorandUnexpectedChipIdErrorand check them usinginstanceof, similar to Python. -
The catch block currently treats any failure as "GET_SECURITY_INFO not supported". An actual timeout or connection failure could therefore incorrectly fall back to magic-value detection. Typed errors would make it possible to fall back only for the expected unsupported-command case and propagate unexpected errors.
-
Returning the detected ROM instead of modifying loader state inside
identifyChip()could make the flow clearer. Something likeidentifyChip(): Promise<ROM>would make it easier to see what the method actually produces and could also make the logic easier to reuse later.
Overall, I think typed errors (5) would be the most important change to consider in this MR because it affects correctness. The remaining points are mostly structural improvements and can reasonably be deferred if we want to keep this MR focused on parity.
Description
Fix #248
Add get security info detect chip to read IMAGE_CHIP_ID from devices before using magic number as fallback.
This should minimize maintenance of magic number for new chips.
This pull request introduces significant improvements to chip detection, security information handling, and support for new ESP32 chip variants. The changes enhance the robustness of chip identification, add support for secure download mode, and expand compatibility with additional ESP32 chips. There are also updates to stub loader file naming and the firmware image loader to accommodate new devices.
Chip detection, security, and communication improvements:
ESPLoaderto use theGET_SECURITY_INFOcommand for identifying chips, with a fallback to the magic register method for older or unsupported chips. This makes chip detection more reliable and future-proof.ESPLoaderandESP32C5ROM. [1] [2] [3] [4]SecurityInfo,SECURITY_INFO_FLAG_MAP,ParsedSecurityFlags) for use by downstream consumers. [1] [2]Expanded chip and firmware image support:
esp32s31,esp32e22,esp32h21,esp32h4) in the firmware image loader and implemented corresponding firmware image classes. [1] [2] [3] [4] [5]USES_MAGIC_VALUEproperty, clarifying detection capabilities. [1] [2]Stub loader and file naming updates:
stub_flasher_prefix and updating import logic accordingly.Firmware image header handling:
ESP32FirmwareImageby handling cases whereIMAGE_CHIP_IDmay be undefined, preventing potential errors when working with new or custom chip variants. [1] [2]Developer experience and documentation:
These changes collectively make the codebase more maintainable, extensible, and ready for future ESP32 chip variants.
Testing
Ideally you could try testing to connect a board which magic number is not defined in esptool-js magic2Chip function.
Another test is esp32 or esp32s2 will try to connect and run the get security info command, which will fail, and fallback to use the magic number to identify ROM class.
Checklist
Before submitting a Pull Request, please ensure the following: