Skip to content

Commit 927bc73

Browse files
authored
Merge pull request #1008 from cla7aye15I4nd/gd32
Add riscv MCU support and update the argument setting
2 parents 748b6c6 + 88fb939 commit 927bc73

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+9498
-7545
lines changed

examples/mcu/gd32vf103_blink.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import sys
2+
sys.path.append("../..")
3+
4+
from qiling.core import Qiling
5+
from qiling.const import QL_VERBOSE
6+
from qiling.extensions.mcu.gd32vf1 import gd32vf103
7+
8+
ql = Qiling(['../rootfs/mcu/gd32vf103/blink.hex'], archtype="riscv64",
9+
env=gd32vf103, verbose=QL_VERBOSE.DEBUG)
10+
11+
ql.hw.create('rcu')
12+
ql.hw.create('gpioa').watch()
13+
ql.hw.create('gpioc').watch()
14+
15+
delay_cycles_begin = 0x800015c
16+
delay_cycles_end = 0x800018c
17+
18+
def skip_delay(ql):
19+
ql.reg.pc = delay_cycles_end
20+
21+
ql.hook_address(skip_delay, delay_cycles_begin)
22+
ql.hw.gpioc.hook_set(13, lambda : print('Set PC13'))
23+
24+
ql.run(count=20000)

examples/mcu/stm32f407_hack_lock.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
from qiling.core import Qiling
1414
from qiling.const import QL_VERBOSE
15+
from qiling.extensions.mcu.stm32f4 import stm32f407
16+
1517

1618
def dicts():
1719
a = 0x79df7
@@ -25,7 +27,7 @@ def dicts():
2527
# Cracking the passwd of lock
2628
def crack(passwd):
2729
ql = Qiling(["../../examples/rootfs/mcu/stm32f407/backdoorlock.hex"],
28-
archtype="cortex_m", profile="stm32f407", verbose=QL_VERBOSE.OFF)
30+
archtype="cortex_m", env=stm32f407, verbose=QL_VERBOSE.OFF)
2931

3032
ql.hw.create('spi2')
3133
ql.hw.create('gpioe')

examples/mcu/stm32f411_dma_logger.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
from qiling.core import Qiling
55
from qiling.const import QL_VERBOSE
6-
6+
from qiling.extensions.mcu.stm32f4 import stm32f411
77

88
def stm32f411_dma():
99
ql = Qiling(["../rootfs/mcu/stm32f411/dma-clock.hex"],
10-
archtype="cortex_m", profile="stm32f411", verbose=QL_VERBOSE.DEBUG)
10+
archtype="cortex_m", env=stm32f411, verbose=QL_VERBOSE.DEBUG)
1111

1212
ql.hw.create('usart2').watch()
1313
ql.hw.create('dma1').watch()

examples/mcu/stm32f411_freertos.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
from qiling.core import Qiling
55
from qiling.const import QL_VERBOSE
6+
from qiling.extensions.mcu.stm32f4 import stm32f411
67

78

89
def stm32f411_freertos():
910
ql = Qiling(["../rootfs/mcu/stm32f411/os-demo.hex"],
10-
archtype="cortex_m", profile="stm32f411", verbose=QL_VERBOSE.DEBUG)
11+
archtype="cortex_m", env=stm32f411, verbose=QL_VERBOSE.DEBUG)
1112

1213
ql.hw.create('usart2').watch()
1314
ql.hw.create('gpioa').watch()

examples/mcu/stm32f411_gpio_hook.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
from qiling.core import Qiling
55
from qiling.const import QL_VERBOSE
6-
6+
from qiling.extensions.mcu.stm32f4 import stm32f411
77

88
def test_mcu_gpio_stm32f411():
99
ql = Qiling(["../../examples/rootfs/mcu/stm32f411/hello_gpioA.hex"],
10-
archtype="cortex_m", profile="stm32f411", verbose=QL_VERBOSE.DEBUG)
10+
archtype="cortex_m", env=stm32f411, verbose=QL_VERBOSE.DEBUG)
1111

1212
ql.hw.create('usart2').watch()
1313
ql.hw.create('rcc').watch()

examples/mcu/stm32f411_i2c_lcd.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
from qiling.core import Qiling
66
from qiling.const import QL_VERBOSE
77
from qiling.hw.external_device.lcd.lcd1602 import PyGameLCD1602
8-
8+
from qiling.extensions.mcu.stm32f4 import stm32f411
99

1010
def create(path, lcd):
11-
ql = Qiling([path], archtype="cortex_m", profile="stm32f411", verbose=QL_VERBOSE.DEBUG)
11+
ql = Qiling([path], archtype="cortex_m", env=stm32f411, verbose=QL_VERBOSE.DEBUG)
1212

1313
ql.hw.create('i2c1')
1414
ql.hw.create('rcc')

examples/mcu/stm32f411_interact_usart.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212

1313
from qiling.core import Qiling
1414
from qiling.const import QL_VERBOSE
15+
from qiling.extensions.mcu.stm32f4 import stm32f411
16+
1517

1618
ql = Qiling(["../../examples/rootfs/mcu/stm32f411/md5_server.hex"],
17-
archtype="cortex_m", profile="stm32f411", verbose=QL_VERBOSE.OFF)
19+
archtype="cortex_m", env=stm32f411, verbose=QL_VERBOSE.OFF)
1820

1921
ql.hw.create('usart2')
2022
ql.hw.create('rcc')

qiling/arch/cortex_m.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def is_handler_mode(self):
106106
def using_psp(self):
107107
return not self.is_handler_mode() and (self.ql.reg.read('control') & CONTROL.SPSEL) > 0
108108

109-
def reset_register(self):
109+
def init_context(self):
110110
self.ql.reg.write('lr', 0xffffffff)
111111
self.ql.reg.write('msp', self.ql.mem.read_ptr(0x0))
112112
self.ql.reg.write('pc' , self.ql.mem.read_ptr(0x4))

qiling/arch/riscv.py

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ def get_init_uc(self) -> Uc:
3434

3535
def create_disassembler(self) -> Cs:
3636
try:
37-
from capstone import CS_ARCH_RISCV, CS_MODE_RISCV32
38-
return Cs(CS_ARCH_RISCV, CS_MODE_RISCV32)
37+
from capstone import CS_ARCH_RISCV, CS_MODE_RISCV32, CS_MODE_RISCVC
38+
return Cs(CS_ARCH_RISCV, CS_MODE_RISCV32 + CS_MODE_RISCVC)
3939
except ImportError:
4040
raise QlErrorNotImplemented("Capstone does not yet support riscv, upgrade to capstone 5.0")
4141

@@ -45,8 +45,37 @@ def create_assembler(self) -> Ks:
4545
def enable_float(self):
4646
self.ql.reg.mstatus = self.ql.reg.mstatus | MSTATUS.FS_DIRTY
4747

48-
def reset_register(self):
49-
self.enable_float()
48+
def init_context(self):
49+
self.ql.reg.pc = 0x08000000
5050

5151
def soft_interrupt_handler(self, ql, intno):
52-
raise QlErrorNotImplemented(f'Unhandled interrupt number ({intno})')
52+
if intno == 2:
53+
try:
54+
address, size = ql.reg.pc - 4, 4
55+
tmp = ql.mem.read(address, size)
56+
qd = ql.arch.create_disassembler()
57+
58+
insn = '\n> '.join(f'{insn.mnemonic} {insn.op_str}' for insn in qd.disasm(tmp, address))
59+
except QlErrorNotImplemented:
60+
insn = ''
61+
62+
ql.log.warning(f'[{hex(address)}] Illegal instruction ({insn})')
63+
else:
64+
raise QlErrorNotImplemented(f'Unhandled interrupt number ({intno})')
65+
66+
def step(self):
67+
self.ql.emu_start(self.get_pc(), 0, count=1)
68+
self.ql.hw.step()
69+
70+
def stop(self):
71+
self.runable = False
72+
73+
def run(self, count=-1, end=None):
74+
self.runable = True
75+
76+
while self.runable and count != 0:
77+
if self.get_pc() == end:
78+
break
79+
80+
self.step()
81+
count -= 1
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Cross Platform and Multi Architecture Advanced Binary Emulation Framework
4+
#
5+
6+
from .gd32vf103 import gd32vf103

0 commit comments

Comments
 (0)