@@ -15,6 +15,7 @@ import (
15
15
"fmt"
16
16
"io/ioutil"
17
17
"sort"
18
+ "strings"
18
19
)
19
20
20
21
type espImageSegment struct {
@@ -78,15 +79,31 @@ func makeESPFirmareImage(infile, outfile, format string) error {
78
79
// An added benefit is that we don't need to check for errors all the time.
79
80
outf := & bytes.Buffer {}
80
81
82
+ // Separate esp32 and esp32-img. The -img suffix indicates we should make an
83
+ // image, not just a binary to be flashed at 0x1000 for example.
84
+ chip := format
85
+ makeImage := false
86
+ if strings .HasSuffix (format , "-img" ) {
87
+ makeImage = true
88
+ chip = format [:len (format )- len ("-img" )]
89
+ }
90
+
91
+ if makeImage {
92
+ // The bootloader starts at 0x1000, or 4096.
93
+ // TinyGo doesn't use a separate bootloader and runs the entire
94
+ // application in the bootloader location.
95
+ outf .Write (make ([]byte , 4096 ))
96
+ }
97
+
81
98
// Chip IDs. Source:
82
99
// https://github.com/espressif/esp-idf/blob/v4.3/components/bootloader_support/include/esp_app_format.h#L22
83
100
chip_id := map [string ]uint16 {
84
101
"esp32" : 0x0000 ,
85
102
"esp32c3" : 0x0005 ,
86
- }[format ]
103
+ }[chip ]
87
104
88
105
// Image header.
89
- switch format {
106
+ switch chip {
90
107
case "esp32" , "esp32c3" :
91
108
// Header format:
92
109
// https://github.com/espressif/esp-idf/blob/v4.3/components/bootloader_support/include/esp_app_format.h#L71
@@ -155,12 +172,22 @@ func makeESPFirmareImage(infile, outfile, format string) error {
155
172
outf .Write (make ([]byte , 15 - outf .Len ()% 16 ))
156
173
outf .WriteByte (checksum )
157
174
158
- if format != "esp8266" {
175
+ if chip != "esp8266" {
159
176
// SHA256 hash (to protect against image corruption, not for security).
160
177
hash := sha256 .Sum256 (outf .Bytes ())
161
178
outf .Write (hash [:])
162
179
}
163
180
181
+ // QEMU (or more precisely, qemu-system-xtensa from Espressif) expects the
182
+ // image to be a certain size.
183
+ if makeImage {
184
+ // Use a default image size of 4MB.
185
+ grow := 4096 * 1024 - outf .Len ()
186
+ if grow > 0 {
187
+ outf .Write (make ([]byte , grow ))
188
+ }
189
+ }
190
+
164
191
// Write the image to the output file.
165
192
return ioutil .WriteFile (outfile , outf .Bytes (), 0666 )
166
193
}
0 commit comments