@@ -22,15 +22,15 @@ type espImageSegment struct {
2222 data []byte
2323}
2424
25- // makeESP32Firmare converts an input ELF file to an image file for the ESP32
26- // chip. This is a special purpose image format just for the ESP32 chip, and is
27- // parsed by the on-chip mask ROM bootloader.
25+ // makeESPFirmare converts an input ELF file to an image file for an ESP32 or
26+ // ESP8266 chip. This is a special purpose image format just for the ESP chip
27+ // family, and is parsed by the on-chip mask ROM bootloader.
2828//
2929// The following documentation has been used:
3030// https://github.com/espressif/esptool/wiki/Firmware-Image-Format
3131// https://github.com/espressif/esp-idf/blob/8fbb63c2a701c22ccf4ce249f43aded73e134a34/components/bootloader_support/include/esp_image_format.h#L58
3232// https://github.com/espressif/esptool/blob/master/esptool.py
33- func makeESP32FirmareImage (infile , outfile string ) error {
33+ func makeESPFirmareImage (infile , outfile , format string ) error {
3434 inf , err := elf .Open (infile )
3535 if err != nil {
3636 return err
@@ -79,26 +79,49 @@ func makeESP32FirmareImage(infile, outfile string) error {
7979 outf := & bytes.Buffer {}
8080
8181 // Image header.
82- // Details: https://github.com/espressif/esp-idf/blob/8fbb63c2/components/bootloader_support/include/esp_image_format.h#L58
83- binary .Write (outf , binary .LittleEndian , struct {
84- magic uint8
85- segment_count uint8
86- spi_mode uint8
87- spi_speed_size uint8
88- entry_addr uint32
89- wp_pin uint8
90- spi_pin_drv [3 ]uint8
91- reserved [11 ]uint8
92- hash_appended bool
93- }{
94- magic : 0xE9 ,
95- segment_count : byte (len (segments )),
96- spi_mode : 0 , // irrelevant, replaced by esptool when flashing
97- spi_speed_size : 0 , // spi_speed, spi_size: replaced by esptool when flashing
98- entry_addr : uint32 (inf .Entry ),
99- wp_pin : 0xEE , // disable WP pin
100- hash_appended : true , // add a SHA256 hash
101- })
82+ switch format {
83+ case "esp32" :
84+ // Header format:
85+ // https://github.com/espressif/esp-idf/blob/8fbb63c2/components/bootloader_support/include/esp_image_format.h#L58
86+ binary .Write (outf , binary .LittleEndian , struct {
87+ magic uint8
88+ segment_count uint8
89+ spi_mode uint8
90+ spi_speed_size uint8
91+ entry_addr uint32
92+ wp_pin uint8
93+ spi_pin_drv [3 ]uint8
94+ reserved [11 ]uint8
95+ hash_appended bool
96+ }{
97+ magic : 0xE9 ,
98+ segment_count : byte (len (segments )),
99+ spi_mode : 0 , // irrelevant, replaced by esptool when flashing
100+ spi_speed_size : 0 , // spi_speed, spi_size: replaced by esptool when flashing
101+ entry_addr : uint32 (inf .Entry ),
102+ wp_pin : 0xEE , // disable WP pin
103+ hash_appended : true , // add a SHA256 hash
104+ })
105+ case "esp8266" :
106+ // Header format:
107+ // https://github.com/espressif/esptool/wiki/Firmware-Image-Format
108+ // Basically a truncated version of the ESP32 header.
109+ binary .Write (outf , binary .LittleEndian , struct {
110+ magic uint8
111+ segment_count uint8
112+ spi_mode uint8
113+ spi_speed_size uint8
114+ entry_addr uint32
115+ }{
116+ magic : 0xE9 ,
117+ segment_count : byte (len (segments )),
118+ spi_mode : 0 , // irrelevant, replaced by esptool when flashing
119+ spi_speed_size : 0x20 , // spi_speed, spi_size: replaced by esptool when flashing
120+ entry_addr : uint32 (inf .Entry ),
121+ })
122+ default :
123+ return fmt .Errorf ("builder: unknown binary format %#v, expected esp32 or esp8266" , format )
124+ }
102125
103126 // Write all segments to the image.
104127 // https://github.com/espressif/esptool/wiki/Firmware-Image-Format#segment
@@ -119,9 +142,11 @@ func makeESP32FirmareImage(infile, outfile string) error {
119142 outf .Write (make ([]byte , 15 - outf .Len ()% 16 ))
120143 outf .WriteByte (checksum )
121144
122- // SHA256 hash (to protect against image corruption, not for security).
123- hash := sha256 .Sum256 (outf .Bytes ())
124- outf .Write (hash [:])
145+ if format == "esp32" {
146+ // SHA256 hash (to protect against image corruption, not for security).
147+ hash := sha256 .Sum256 (outf .Bytes ())
148+ outf .Write (hash [:])
149+ }
125150
126151 // Write the image to the output file.
127152 return ioutil .WriteFile (outfile , outf .Bytes (), 0666 )
0 commit comments