Is it possible to modify the final binary image before uploading to esp32?

tanmanh0707
Posts: 15
Joined: Fri Jun 23, 2023 3:26 am

Is it possible to modify the final binary image before uploading to esp32?

Postby tanmanh0707 » Tue Aug 29, 2023 3:00 am

Hello,

I would like to modify the final binary image before uploading it to esp32.
The scenario is that I want to manipulate a special string in the final binary image, then upload it using esptool command line (not from the Arduino IDE)
The idea is, there is a template string in the code such as

Code: Select all

unsigned char template_string[] = "This-is-template-string";
Then Compile the sketch, when finishes, there is a *.bin file created. After that, I open that *.bin file in a Hex Editor Tool, and modify that string to another string with the same length such as "This-is-modified-string", then upload it using esptool.exe command line

My issue is that after uploading, it keeps rebooting with below logs:

Code: Select all

18:09:03.903 -> ESP-ROM:esp32s3-20210327
18:09:03.903 -> Build:Mar 27 2021
18:09:03.903 -> rst:0x3 (RTC_SW_SYS_RST),boot:0x8 (SPI_FAST_FLASH_BOOT)
18:09:03.903 -> Saved PC:0x403cd9f6
18:09:03.903 -> SPIWP:0xee
18:09:03.903 -> mode:DIO, clock div:1
18:09:03.903 -> load:0x3fce3808,len:0x44c
18:09:03.903 -> load:0x403c9700,len:0xbe4
18:09:03.903 -> load:0x403cc700,len:0x2a38
18:09:03.903 -> entry 0x403c98d4
I think it is Checksum issue, is there a way to do that?

Thank you,
Manh

esp_man
Posts: 14
Joined: Tue Mar 21, 2023 12:04 pm

Re: Is it possible to modify the final binary image before uploading to esp32?

Postby esp_man » Tue Aug 29, 2023 10:09 am

This should be possible if You also edit CRC properly.
First of all, You should locate where this CRC is.
For example, complie two programs.
One with:

Code: Select all

unsigned char template_string[] = "This-is-template-string";
The second one with:

Code: Select all

unsigned char template_string[] = "This-is-modified-string";
And next, compare two outputted bin files in hex editor. It will be different in this string, and also somewhere else.

lbernstone
Posts: 829
Joined: Mon Jul 22, 2019 3:20 pm

Re: Is it possible to modify the final binary image before uploading to esp32?

Postby lbernstone » Tue Aug 29, 2023 4:31 pm

It might be possible to do this if you are replacing with a string the same length, but I don't think you will save a whole lot of time.
On a decent machine (8GB RAM), you should be able to cache the whole of the input files. So, if you compile from the command line, you can feed in a define (in a for loop/batch) and compile over and over pretty quickly.

tanmanh0707
Posts: 15
Joined: Fri Jun 23, 2023 3:26 am

Re: Is it possible to modify the final binary image before uploading to esp32?

Postby tanmanh0707 » Thu Aug 31, 2023 10:30 am

esp_man wrote:
Tue Aug 29, 2023 10:09 am
This should be possible if You also edit CRC properly.
First of all, You should locate where this CRC is.
For example, complie two programs.
One with:

Code: Select all

unsigned char template_string[] = "This-is-template-string";
The second one with:

Code: Select all

unsigned char template_string[] = "This-is-modified-string";
And next, compare two outputted bin files in hex editor. It will be different in this string, and also somewhere else.
Thank you for your suggestion. I did and there are 2 differences
- 32 bytes from 0xB0 to 0xCF
- The last 32 bytes

So basically I can determine the differences but I don't know how to change them properly such as which CRC should be used and which data should be used to calculate CRC

tanmanh0707
Posts: 15
Joined: Fri Jun 23, 2023 3:26 am

Re: Is it possible to modify the final binary image before uploading to esp32?

Postby tanmanh0707 » Thu Aug 31, 2023 10:34 am

lbernstone wrote:
Tue Aug 29, 2023 4:31 pm
It might be possible to do this if you are replacing with a string the same length, but I don't think you will save a whole lot of time.
On a decent machine (8GB RAM), you should be able to cache the whole of the input files. So, if you compile from the command line, you can feed in a define (in a for loop/batch) and compile over and over pretty quickly.
Actually, this is not a case for time saving. My case is used on user's demand. Whenever there is a user request, it modifies the binrary to user data and return the final binary to user to upload. I am aware that there is a alternative option is to save data to SPIFFS instead of directly to source code. I just want to try if it is possible.

esp_man
Posts: 14
Joined: Tue Mar 21, 2023 12:04 pm

Re: Is it possible to modify the final binary image before uploading to esp32?

Postby esp_man » Sat Sep 02, 2023 7:36 am

tanmanh0707 wrote:
Thu Aug 31, 2023 10:30 am
Thank you for your suggestion. I did and there are 2 differences
- 32 bytes from 0xB0 to 0xCF
- The last 32 bytes
So You know now where the CRC may be.
There are online pages where you can check what CRC is used (you upload a file there).

Who is online

Users browsing this forum: No registered users and 56 guests