-
Notifications
You must be signed in to change notification settings - Fork 53
/
toflash_espressif.c
117 lines (90 loc) · 2.99 KB
/
toflash_espressif.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//
// qemu_toflash
//
// python /home/olas/esp/esp-idf/components/esptool_py/esptool/esptool.py --chip esp32 --port /dev/ttyUSB0 --baud 115200 --before default_reset --after hard_reset write_flash -z --flash_mode dio --flash_freq 40m --flash_size detect
// 0x1000 /home/olas/esp/esp-idf/examples/system/ota/build/bootloader/bootloader.bin
// 0x10000 /home/olas/esp/esp-idf/examples/system/ota/build/ota.bin
// 0x8000 /home/olas/esp/esp-idf/examples/system/ota/build/partitions_two_ota.bin
//
void merge_flash(char *binfile,char *flashfile,int flash_pos,int patch_hash)
{
FILE *fbin;
FILE *fflash;
unsigned char *tmp_data;
int j=0;
int file_size=0;
int flash_size=0;
fbin = fopen(binfile, "rb");
if (fbin == NULL) {
printf(" Can't open '%s' for reading.\n", binfile);
return;
}
if (fseek(fbin, 0 , SEEK_END) != 0) {
printf(" Can't seek end of '%s'.\n", binfile);
/* Handle Error */
}
file_size = ftell(fbin);
if (fseek(fbin, 0 , SEEK_SET) != 0) {
/* Handle Error */
}
fflash = fopen(flashfile, "rb+");
if (fflash == NULL) {
printf(" Can't open '%s' for writing.\n", flashfile);
return;
}
if (fseek(fflash, 0 , SEEK_END) != 0) {
printf(" Can't seek end of '%s'.\n", flashfile);
/* Handle Error */
}
flash_size = ftell(fflash);
rewind(fflash);
fseek(fflash,flash_pos,SEEK_SET);
tmp_data=malloc((1+file_size)*sizeof(char));
if (file_size<=0) {
printf("Not able to get file size %s",binfile);
}
int len_read=fread(tmp_data,sizeof(char),file_size,fbin);
if (patch_hash==1) {
for (j=0;j<33;j++)
{
tmp_data[file_size-j]=0;
}
}
int len_write=fwrite(tmp_data,sizeof(char),file_size,fflash);
if (len_read!=len_write) {
printf("Not able to merge %s, %d bytes read,%d to write,%d file_size\n",binfile,len_read,len_write,file_size);
//while()
}
fclose(fbin);
if (fseek(fflash, 0x3E8000*4 , SEEK_SET) != 0) {
}
fclose(fflash);
free(tmp_data);
}
int main(int argc,char *argv[])
{
if (argc>1) {
if (strcmp(argv[1],"-bl")==0) {
printf("Overwrite bootloader only \n");
merge_flash("build/bootloader/bootloader.bin","esp32flash.bin",0x1000,0);
system("cp esp32flash.bin ~/qemu_espressif");
exit(0);
}
}
// Overwrites esp32flash.bin file
system("dd if=/dev/zero bs=1M count=4 | tr \"\\000\" \"\\377\" > esp32flash.bin");
// Add bootloader
merge_flash("build/bootloader/bootloader.bin","esp32flash.bin",0x1000,0);
// Add partitions,
merge_flash("build/partition_table/partition-table.bin","esp32flash.bin",0x8000,0);
// Add application
if (argc>1) {
merge_flash(argv[1],"esp32flash.bin",0x10000,0);
} else {
merge_flash("build/app-template.bin","esp32flash.bin",0x10000,0);
}
system("cp esp32flash.bin ~/qemu_esp32");
}