@@ -9,7 +9,6 @@ package builder
99
1010import (
1111 "bytes"
12- "crypto/sha256"
1312 "debug/elf"
1413 "encoding/binary"
1514 "fmt"
@@ -100,12 +99,24 @@ func makeESPFirmwareImage(infile, outfile, format string) error {
10099 chip_id := map [string ]uint16 {
101100 "esp32" : 0x0000 ,
102101 "esp32c3" : 0x0005 ,
102+ "esp32c6" : 0x000D ,
103103 "esp32s3" : 0x0009 ,
104104 }[chip ]
105105
106+ // SPI flash speed/size byte (byte 3 of header):
107+ // Upper nibble = flash size, lower nibble = flash frequency.
108+ // The espflasher auto-detects and patches the flash size (upper nibble),
109+ // but the frequency (lower nibble) must be correct per chip.
110+ spiSpeedSize := map [string ]uint8 {
111+ "esp32" : 0x1f , // 80MHz=0x0F, 2MB=0x10
112+ "esp32c3" : 0x1f , // 80MHz=0x0F, 2MB=0x10
113+ "esp32c6" : 0x10 , // 80MHz=0x00, 2MB=0x10 (C6 uses different freq encoding)
114+ "esp32s3" : 0x1f , // 80MHz=0x0F, 2MB=0x10
115+ }[chip ]
116+
106117 // Image header.
107118 switch chip {
108- case "esp32" , "esp32c3" , "esp32s3" :
119+ case "esp32" , "esp32c3" , "esp32c6" , " esp32s3" :
109120 // Header format:
110121 // https://github.com/espressif/esp-idf/blob/v4.3/components/bootloader_support/include/esp_app_format.h#L71
111122 // Note: not adding a SHA256 hash as the binary is modified by
@@ -126,12 +137,12 @@ func makeESPFirmwareImage(infile, outfile, format string) error {
126137 }{
127138 magic : 0xE9 ,
128139 segment_count : byte (len (segments )),
129- spi_mode : 2 , // ESP_IMAGE_SPI_MODE_DIO
130- spi_speed_size : 0x1f , // ESP_IMAGE_SPI_SPEED_80M, ESP_IMAGE_FLASH_SIZE_2MB
140+ spi_mode : 2 , // ESP_IMAGE_SPI_MODE_DIO
141+ spi_speed_size : spiSpeedSize ,
131142 entry_addr : uint32 (inf .Entry ),
132143 wp_pin : 0xEE , // disable WP pin
133144 chip_id : chip_id ,
134- hash_appended : true , // add a SHA256 hash
145+ hash_appended : false , // disabled: espflasher patches header, invalidating the hash
135146 })
136147 case "esp8266" :
137148 // Header format:
@@ -173,11 +184,9 @@ func makeESPFirmwareImage(infile, outfile, format string) error {
173184 outf .Write (make ([]byte , 15 - outf .Len ()% 16 ))
174185 outf .WriteByte (checksum )
175186
176- if chip != "esp8266" {
177- // SHA256 hash (to protect against image corruption, not for security).
178- hash := sha256 .Sum256 (outf .Bytes ())
179- outf .Write (hash [:])
180- }
187+ // Note: SHA256 hash intentionally omitted. espflasher patches the header
188+ // (SPI mode/speed/size), which invalidates the hash. The ROM would report
189+ // "SHA-256 comparison failed" and boot anyway, so it's just noise.
181190
182191 // QEMU (or more precisely, qemu-system-xtensa from Espressif) expects the
183192 // image to be a certain size.
0 commit comments