11#!/usr/bin/env python3
2- #
2+ #
33# Cross Platform and Multi Architecture Advanced Binary Emulation Framework
4- # Built on top of Unicorn emulator (www.unicorn-engine.org)
4+ # Built on top of Unicorn emulator (www.unicorn-engine.org)
55
66
77import io
@@ -27,7 +27,7 @@ def __init__(self, path):
2727 if addr != begin + len (stream ):
2828 self .segments .append ((begin , stream ))
2929 begin , stream = addr , data
30-
30+
3131 else :
3232 stream += data
3333
@@ -36,13 +36,13 @@ def __init__(self, path):
3636 def parse_line (self , line ):
3737 if len (line ) < 9 :
3838 return
39-
39+
4040 desc = line [7 : 9 ]
41- size = int (line [1 : 3 ], 16 )
42-
41+ size = int (line [1 : 3 ], 16 )
42+
4343 addr = bytes .fromhex (line [3 : 7 ])
44- data = bytes .fromhex (line [9 : 9 + size * 2 ])
45-
44+ data = bytes .fromhex (line [9 : 9 + size * 2 ])
45+
4646 if desc == '00' : # Data
4747 offset = int .from_bytes (addr , byteorder = 'big' )
4848 self .mem .append ((self .base + offset , data ))
@@ -52,20 +52,20 @@ def parse_line(self, line):
5252
5353 elif desc == '04' : # Extended Linear Address
5454 self .base = int .from_bytes (data , byteorder = 'big' ) * 0x10000
55-
55+
5656
5757class QlLoaderMCU (QlLoader ):
5858 def __init__ (self , ql :Qiling ):
59- super ().__init__ (ql )
60-
59+ super ().__init__ (ql )
60+
6161 self .entry_point = 0
6262 self .load_address = 0
6363 self .filetype = self .guess_filetype ()
64-
64+
6565 if self .filetype == 'elf' :
6666 with open (self .ql .path , 'rb' ) as infile :
6767 self .elf = ELFFile (io .BytesIO (infile .read ()))
68-
68+
6969 elif self .filetype == 'bin' :
7070 self .map_address = self .argv [1 ]
7171
@@ -74,16 +74,16 @@ def __init__(self, ql:Qiling):
7474
7575 def guess_filetype (self ):
7676 if self .ql .path .endswith ('.elf' ):
77- return 'elf'
78-
77+ return 'elf'
78+
7979 if self .ql .path .endswith ('.bin' ):
8080 return 'bin'
8181
8282 if self .ql .path .endswith ('.hex' ):
8383 return 'hex'
8484
8585 return 'elf'
86-
86+
8787 def reset (self ):
8888 if self .filetype == 'elf' :
8989 for segment in self .elf .iter_segments (type = 'PT_LOAD' ):
@@ -99,7 +99,7 @@ def reset(self):
9999 for begin , data in self .ihex .segments :
100100 self .ql .mem .write (begin , data )
101101
102-
102+
103103 self .ql .arch .init_context ()
104104 self .entry_point = self .ql .arch .regs .read ('pc' )
105105
@@ -109,11 +109,12 @@ def load_profile(self):
109109 def load_env (self ):
110110 for name , args in self .env .items ():
111111 memtype = args ['type' ]
112+
112113 if memtype == 'memory' :
113114 size = args ['size' ]
114115 base = args ['base' ]
115116 self .ql .mem .map (base , size , info = f'[{ name } ]' )
116-
117+
117118 # elif memtype == 'remap':
118119 # size = args['size']
119120 # base = args['base']
@@ -134,8 +135,8 @@ def load_env(self):
134135 def run (self ):
135136 self .load_profile ()
136137 self .load_env ()
137-
138+
138139 ## Handle interrupt from instruction execution
139140 self .ql .hook_intr (self .ql .arch .unicorn_exception_handler )
140-
141+
141142 self .reset ()
0 commit comments