Skip to content

Commit a65d245

Browse files
committed
More updates
1 parent 3ff3cdb commit a65d245

File tree

5 files changed

+36
-17
lines changed

5 files changed

+36
-17
lines changed

GhidraESP8266_2/src/main/java/ghidraesp8266_2/ESP8266Constants.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@ public final class ESP8266Constants {
77
public final static String SECONDARY_KEY = "2";
88
public final static int IROM_MAP_START = 0x40200000;
99
public final static int IROM_MAP_END = 0x40300000;
10-
10+
public final static int SEGMENT_USER_CODE_BASE = 0x40100000;
11+
public final static int SEGMENT_USER_DATA_BASE = 0x3FFE8000;
12+
public final static int SEGMENT_DATA_END = 0x3FFFFFFF;
13+
public final static int SEGMENT_CODE_BASE = 0x40100000;
1114
}

GhidraESP8266_2/src/main/java/ghidraesp8266_2/ESP8266Header.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ public class ESP8266Header implements StructConverter {
1515
private byte segments;
1616
private byte flash_mode;
1717
private byte flash_size_free;
18-
private byte entrypoint;
18+
private long entrypoint;
1919

2020
public ESP8266Header(BinaryReader reader) throws IOException {
2121
magic = reader.readNextByte();
2222
segments = reader.readNextByte();
23+
flash_mode = reader.readNextByte();
24+
flash_size_free = reader.readNextByte();
25+
entrypoint = reader.readNextInt();
2326
if (ESP8266Constants.ESP_MAGIC_BASE != getMagic()) {
2427
throw new IOException("not an ESP8266 file.");
2528
}
@@ -29,10 +32,10 @@ public ESP8266Header(BinaryReader reader) throws IOException {
2932
public DataType toDataType() throws DuplicateNameException, IOException {
3033
Structure structure = new StructureDataType("header_item", 0);
3134
structure.add(BYTE, 1, "magic", null);
32-
structure.add(BYTE, 1, "segments", null);
35+
structure.add(BYTE, 1, "segments", "Number of segments");
3336
structure.add(BYTE, 1, "flash_mode", null);
3437
structure.add(BYTE, 1, "flash_size_free", null);
35-
structure.add(BYTE, 1, "entrypoint", null);
38+
structure.add(DWORD, 4, "entrypoint", "The entry function");
3639
return structure;
3740
}
3841

@@ -68,15 +71,11 @@ public void setFlashSizeFree(byte flash_size_free) {
6871
this.flash_size_free = flash_size_free;
6972
}
7073

71-
public byte getEntrypoint() {
74+
public long getEntrypoint() {
7275
return entrypoint;
7376
}
7477

75-
public void setEntrypoint(byte entrypoint) {
78+
public void setEntrypoint(long entrypoint) {
7679
this.entrypoint = entrypoint;
7780
}
78-
79-
80-
81-
8281
}

GhidraESP8266_2/src/main/java/ghidraesp8266_2/ESP8266Module.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,18 @@
99
public class ESP8266Module {
1010

1111
private ESP8266Header header;
12+
private ESP8266Header userheader;
1213
private List<ESP8266Section> sections = new ArrayList<ESP8266Section>();
1314

1415
public ESP8266Module(BinaryReader reader) throws IOException {
1516
header = new ESP8266Header(reader);
16-
while (reader.getPointerIndex() < reader.length()) {
17+
for(int i=0; i < header.getSegmentCount(); ++i) {
18+
sections.add(new ESP8266Section(reader));
19+
}
20+
// Parse user ROM
21+
reader.setPointerIndex(0x1000);
22+
userheader = new ESP8266Header(reader);
23+
for(int i=0; i < userheader.getSegmentCount(); ++i) {
1724
sections.add(new ESP8266Section(reader));
1825
}
1926
}

GhidraESP8266_2/src/main/java/ghidraesp8266_2/ESP8266Section.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ public byte[] getContent() {
4141
}
4242

4343
public String getName() {
44-
return String.format("Section %08x, size %u", offset, size);
44+
// Rules based on ranges
45+
if(offset == ESP8266Constants.SEGMENT_USER_CODE_BASE)
46+
return ".user_code";
47+
else if(offset == ESP8266Constants.SEGMENT_USER_DATA_BASE)
48+
return ".user_data";
49+
else if(offset <= ESP8266Constants.SEGMENT_DATA_END)
50+
return ".data";
51+
else if(offset > ESP8266Constants.SEGMENT_CODE_BASE)
52+
return ".code";
53+
else
54+
return ".unknown";
4555
}
4656
}

GhidraESP8266_2/src/main/java/ghidraesp8266_2/GhidraESP8266_2Loader.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,10 @@ public Data createData(Program program, Listing listing, Address address, DataTy
124124
return d;
125125
}
126126
catch (CodeUnitInsertionException e) {
127-
Msg.warn(this, "ELF data markup conflict at " + address);
127+
Msg.warn(this, "Data markup conflict at " + address);
128128
}
129129
catch (DataTypeConflictException e) {
130-
Msg.error(this, "ELF data type markup conflict:" + e.getMessage());
130+
Msg.error(this, "Data type markup conflict:" + e.getMessage());
131131
}
132132
return null;
133133
}
@@ -140,9 +140,6 @@ protected void load(ByteProvider provider, LoadSpec loadSpec, List<Option> optio
140140
monitor.setMessage( "ESP8266 Loader: Start loading" );
141141

142142
try {
143-
Address start = program.getAddressFactory().getDefaultAddressSpace().getAddress( 0x0 );
144-
long length = provider.length();
145-
146143
InputStream inputStream;
147144
inputStream = provider.getInputStream(0);
148145
mbu = new MemoryBlockUtil(program, handler);
@@ -160,6 +157,9 @@ protected void load(ByteProvider provider, LoadSpec loadSpec, List<Option> optio
160157
for (ESP8266Section section : module.getSections()) {
161158
monitor.setMessage("Loaded " + section.getName());
162159
}
160+
// Create entry point
161+
Address entryAddress = program.getAddressFactory().getDefaultAddressSpace().getAddress(module.getHeader().getEntrypoint(), true);
162+
program.getSymbolTable().addExternalEntryPoint(entryAddress);
163163
} catch (Exception e) {
164164
log.appendException( e );
165165
}

0 commit comments

Comments
 (0)