Skip to content

Commit fecef65

Browse files
author
Me No Dev
committed
Merge remote-tracking branch 'igrr/master'
2 parents 35ab566 + 6d8a8bf commit fecef65

File tree

7 files changed

+74
-7
lines changed

7 files changed

+74
-7
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ Argument | Description
2626
```-bz <512K|256K|1M|2M|4M|8M|16M|32M>``` | Set the flash chip size. Default is 512K.
2727
```-bf <40|26|20|80>``` | Set the flash chip frequency, in MHz. Default is 40M.
2828
```-bs <section>``` | Read the specified section from the ELF file and append it to the firmware image. Sections will appear in the firmware image in the exact same order as the -bs commands are executed.
29-
```-bp <size>``` | Pad last written section of firmware image to the given size, in bytes.
29+
```-bp <size>``` | Finalize the firmware image, padding it with `0xaa` value until it is at least `<size>` bytes long. Unlike `-bc`, this doesn't close the file. This option can be used to combine bootloader with the rest of the application
30+
```-br <size>``` | Pad all the following sections to multiples of `<size>`. Default is 4 bytes. This option can be used to place sections on specific boundaries, e.g. 4k or 64k.
3031
```-bc``` | Close the firmware image and save the result as file to disk.
3132
```-v``` | Increase verbosity level of the tool. Add more v's to increase it even more, e.g. -vv, -vvv.
3233
```-q``` | Disable most of the output.

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ matrix:
1111

1212
build_script:
1313
- SET PATH=C:\MinGW\bin;C:\MinGW\msys\1.0\bin;%PATH%
14-
- make all
14+
- make all CC=gcc
1515
- for /f %%i in ('git describe --always') do set PRODUCT_VERSION=%%i
1616
- SET PRODUCT_NAME=esptool-%PRODUCT_VERSION%
1717
- SET PLATFORM_NAME=win32

argparse/argparse.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,13 @@ they are given.\n\
7878
same order as the -bs commands are executed.\n\
7979
\n\
8080
-bp <size>\n\
81-
Pad last written section of firmware image to the given size, in bytes.\n\
81+
Finalize the firmware image, padding it with '0xaa' value until it is at least\n\
82+
<size> bytes long. Unlike -bc, this doesn't close the file.\n\
83+
This option can be used to combine bootloader with the rest of the application\n\
84+
\n\
85+
-br <size>\n\
86+
Pad all the following sections to multiples of <size>. Default is 4 bytes.\n\
87+
This option can be used to place sections on specific boundaries, e.g. 4k or 64k.\n\
8288
\n\
8389
-bc\n\
8490
Close the firmware image and save the result as file to disk.\n\

argparse/argparse_binimagecmd.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include "esptool_elf_object.h"
3131
#include "esptool_binimage.h"
3232

33+
static unsigned section_pad_size = 4;
34+
3335
static int argparse_binimagecmd_add_segment(const char *sname, uint32_t padsize)
3436
{
3537
uint32_t snum;
@@ -93,7 +95,7 @@ int argparse_binimagecmd(int num_args, char **arg_ptr)
9395
{
9496
return 0;
9597
}
96-
if(argparse_binimagecmd_add_segment(arg_ptr[0], 4))
98+
if(argparse_binimagecmd_add_segment(arg_ptr[0], section_pad_size))
9799
{
98100
bimage_set_entry(get_elf_entry());
99101
return 2;
@@ -119,6 +121,14 @@ int argparse_binimagecmd(int num_args, char **arg_ptr)
119121
}
120122
break;
121123

124+
case 'r':
125+
if(num_args < 1)
126+
{
127+
return 0;
128+
}
129+
section_pad_size = (unsigned) atoi(arg_ptr[0]);
130+
return 2;
131+
122132
case 'm':
123133
if (num_args < 1)
124134
{
@@ -151,6 +161,17 @@ int argparse_binimagecmd(int num_args, char **arg_ptr)
151161
return 0;
152162
}
153163
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;
154175

155176
default:
156177
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

main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ int main(int argc, char **argv)
5959
num_args_parsed = parse_arg(num_args, arg_ptr);
6060
if(num_args_parsed == 0)
6161
{
62+
LOGERR("Invalid argument or value after %s (argument #%d)", arg_ptr[0], arg_ptr - argv + 1);
6263
goto EXITERROR;
6364
}
6465

@@ -77,5 +78,5 @@ int main(int argc, char **argv)
7778
EXITERROR:
7879
close_elf_object();
7980
binimage_write_close(16);
80-
return -1;
81+
return 2;
8182
}

0 commit comments

Comments
 (0)