Skip to content

Commit 6d8a8bf

Browse files
committed
Add command to set binary image header layout
1 parent 24d8d7f commit 6d8a8bf

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

argparse/argparse_binimagecmd.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,17 @@ int argparse_binimagecmd(int num_args, char **arg_ptr)
161161
return 0;
162162
}
163163
return 2;
164+
165+
case 'l':
166+
if (num_args < 1)
167+
{
168+
return 0;
169+
}
170+
if (binimage_set_header_layout(arg_ptr[0]) == 0)
171+
{
172+
return 0;
173+
}
174+
return 2;
164175

165176
default:
166177
return 0;

binimage/esptool_binimage.c

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,18 @@
2929
#include "infohelper.h"
3030
#include "esptool_binimage.h"
3131

32+
typedef enum { HL_ESP8266, HL_ESP32BOOT, HL_ESP32 } binimage_header_layout_t;
33+
3234
static bin_image b_image = {
3335
.magic = 0xe9,
3436
.num_segments = 0,
3537
.flash_mode = FLASH_MODE_QIO,
3638
.flash_size_freq = FLASH_SIZE_512K | FLASH_FREQ_40
3739
};
3840

39-
unsigned int total_size = 0;
41+
static unsigned int total_size = 0;
42+
static binimage_header_layout_t header_layout = HL_ESP8266;
43+
4044

4145
int binimage_add_segment(uint32_t address, uint32_t size, unsigned char *data)
4246
{
@@ -139,6 +143,22 @@ int binimage_write(uint32_t padsize, bool close)
139143
}
140144

141145
total_size = 8;
146+
147+
if (header_layout == HL_ESP32BOOT)
148+
{
149+
LOGDEBUG("adding extra header for ESP32 boot image");
150+
uint8_t extra_header[16];
151+
// TODO: replace this dummy 16 byte array with the actual flash_extN_config_hdr structures
152+
memset(extra_header, 0, sizeof(extra_header));
153+
if(fwrite(extra_header, 1, sizeof(extra_header), b_image.image_file) != sizeof(extra_header))
154+
{
155+
LOGERR("cant write extra header to binimage file, aborting");
156+
fclose(b_image.image_file);
157+
b_image.image_file = 0;
158+
return 0;
159+
}
160+
total_size += sizeof(extra_header);
161+
}
142162

143163
for(cnt = 0; cnt < b_image.num_segments; cnt++)
144164
{
@@ -377,3 +397,21 @@ const char* binimage_flash_freq_to_str(unsigned char freq)
377397
}
378398
}
379399

400+
int binimage_set_header_layout(const char* layout)
401+
{
402+
LOGDEBUG("setting header layout to %s", layout);
403+
if (strcasecmp(layout, "esp32boot") == 0) {
404+
header_layout = HL_ESP32BOOT;
405+
return 1;
406+
}
407+
else if (strcasecmp(layout, "esp8266") == 0) {
408+
header_layout = HL_ESP8266;
409+
return 1;
410+
}
411+
else if (strcasecmp(layout, "esp32")) {
412+
header_layout = HL_ESP32;
413+
}
414+
LOGERR("invalid image header layout: %s", layout);
415+
return 0;
416+
}
417+

binimage/esptool_binimage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,6 @@ int binimage_add_segment(uint32_t address, uint32_t size, unsigned char *data);
136136
int binimage_set_flash_mode(const char* mode);
137137
int binimage_set_flash_size(const char* size);
138138
int binimage_set_flash_freq(const char* freq);
139-
139+
int binimage_set_header_layout(const char* layout);
140140

141141
#endif

0 commit comments

Comments
 (0)