|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Cross Platform and Multi Architecture Advanced Binary Emulation Framework |
| 4 | +# |
| 5 | + |
| 6 | + |
| 7 | +import sys, unittest |
| 8 | +sys.path.append("..") |
| 9 | + |
| 10 | +from qiling.core import Qiling |
| 11 | +from qiling.const import QL_VERBOSE |
| 12 | + |
| 13 | + |
| 14 | +class MCUTest(unittest.TestCase): |
| 15 | + def test_mcu_led_stm32f411(self): |
| 16 | + ql = Qiling(["../examples/rootfs/mcu/stm32f411/rand_blink.hex"], |
| 17 | + archtype="cortex_m", profile="stm32f411", verbose=QL_VERBOSE.DISASM) |
| 18 | + |
| 19 | + # Set verbose=QL_VERBOSE.DEFAULT to find warning |
| 20 | + ql.run(count=1000) |
| 21 | + |
| 22 | + del ql |
| 23 | + |
| 24 | + def test_mcu_usart_output_stm32f411(self): |
| 25 | + ql = Qiling(["../examples/rootfs/mcu/stm32f411/hello_usart.hex"], |
| 26 | + archtype="cortex_m", profile="stm32f411", verbose=QL_VERBOSE.DEFAULT) |
| 27 | + |
| 28 | + ql.hw.create('usart2') |
| 29 | + ql.hw.create('rcc') |
| 30 | + |
| 31 | + ql.run(count=2000) |
| 32 | + buf = ql.hw.usart2.recv() |
| 33 | + print('[1] Received from usart: ', buf) |
| 34 | + self.assertEqual(buf, b'Hello USART\n') |
| 35 | + |
| 36 | + del ql |
| 37 | + |
| 38 | + def test_mcu_usart_input_stm32f411(self): |
| 39 | + ql = Qiling(["../examples/rootfs/mcu/stm32f411/md5_server.hex"], |
| 40 | + archtype="cortex_m", profile="stm32f411", verbose=QL_VERBOSE.OFF) |
| 41 | + |
| 42 | + ql.hw.create('usart2') |
| 43 | + ql.hw.create('rcc') |
| 44 | + |
| 45 | + ql.run(count=1000) |
| 46 | + |
| 47 | + ql.hw.usart2.send(b'Hello\n') |
| 48 | + ql.run(count=30000) |
| 49 | + ql.hw.usart2.send(b'USART\n') |
| 50 | + ql.run(count=30000) |
| 51 | + ql.hw.usart2.send(b'Input\n') |
| 52 | + ql.run(count=30000) |
| 53 | + |
| 54 | + buf = ql.hw.usart2.recv() |
| 55 | + self.assertEqual(buf, b'8b1a9953c4611296a827abf8c47804d7\n2daeb613094400290a24fe5086c68f06\n324118a6721dd6b8a9b9f4e327df2bf5\n') |
| 56 | + |
| 57 | + del ql |
| 58 | + |
| 59 | + def test_mcu_patch_stm32f411(self): |
| 60 | + ql = Qiling(["../examples/rootfs/mcu/stm32f411/patch_test.hex"], |
| 61 | + archtype="cortex_m", profile="stm32f411", verbose=QL_VERBOSE.DEFAULT) |
| 62 | + |
| 63 | + ql.hw.create('usart2') |
| 64 | + ql.hw.create('rcc') |
| 65 | + ql.hw.create('gpioa') |
| 66 | + |
| 67 | + ql.patch(0x80005CA, b'\x00\xBF') |
| 68 | + ql.run(count=4000) |
| 69 | + |
| 70 | + del ql |
| 71 | + |
| 72 | + def test_mcu_freertos_stm32f411(self): |
| 73 | + ql = Qiling(["../examples/rootfs/mcu/stm32f411/os-demo.elf"], |
| 74 | + archtype="cortex_m", profile="stm32f411", verbose=QL_VERBOSE.DEBUG) |
| 75 | + |
| 76 | + ql.hw.create('usart2') |
| 77 | + ql.hw.create('rcc') |
| 78 | + ql.hw.create('gpioa') |
| 79 | + |
| 80 | + count = 0 |
| 81 | + def counter(): |
| 82 | + nonlocal count |
| 83 | + count += 1 |
| 84 | + |
| 85 | + ql.hw.gpioa.hook_set(5, counter) |
| 86 | + |
| 87 | + ql.run(count=200000) |
| 88 | + |
| 89 | + self.assertTrue(count >= 5) |
| 90 | + self.assertTrue(ql.hw.usart2.recv().startswith(b'Free RTOS\n' * 5)) |
| 91 | + |
| 92 | + del ql |
| 93 | + |
| 94 | + def test_mcu_dma_stm32f411(self): |
| 95 | + ql = Qiling(["../examples/rootfs/mcu/stm32f411/dma-clock.elf"], |
| 96 | + archtype="cortex_m", profile="stm32f411", verbose=QL_VERBOSE.DEBUG) |
| 97 | + |
| 98 | + ql.hw.create('usart2') |
| 99 | + ql.hw.create('dma1') |
| 100 | + ql.hw.create('rcc') |
| 101 | + |
| 102 | + ql.run(count=200000) |
| 103 | + buf = ql.hw.usart2.recv() |
| 104 | + |
| 105 | + ## check timestamp |
| 106 | + tick = [int(x) for x in buf.split()] |
| 107 | + for i in range(1, len(tick)): |
| 108 | + assert(4 <= tick[i] - tick[i - 1] <= 6) |
| 109 | + |
| 110 | + del ql |
| 111 | + |
| 112 | + def test_mcu_i2c_stm32f411(self): |
| 113 | + ql = Qiling(["../examples/rootfs/mcu/stm32f411/i2c-lcd.bin", 0x8000000], |
| 114 | + archtype="cortex_m", profile="stm32f411", verbose=QL_VERBOSE.DEBUG) |
| 115 | + |
| 116 | + ql.hw.create('i2c1') |
| 117 | + ql.hw.create('rcc') |
| 118 | + ql.hw.create('gpioa') |
| 119 | + ql.hw.create('gpiob') |
| 120 | + |
| 121 | + flag = False |
| 122 | + def indicator(): |
| 123 | + nonlocal flag |
| 124 | + flag = True |
| 125 | + |
| 126 | + ql.hw.gpioa.hook_set(5, indicator) |
| 127 | + |
| 128 | + class LCD: |
| 129 | + address = 0x3f << 1 |
| 130 | + |
| 131 | + def send(self, data): |
| 132 | + pass |
| 133 | + |
| 134 | + ql.hw.i2c1.connect(LCD()) |
| 135 | + ql.run(count=550000) |
| 136 | + |
| 137 | + self.assertTrue(flag) |
| 138 | + |
| 139 | + del ql |
| 140 | + |
| 141 | + def test_mcu_spi_stm32f411(self): |
| 142 | + ql = Qiling(["../examples/rootfs/mcu/stm32f411/spi-test.bin", 0x8000000], |
| 143 | + archtype="cortex_m", profile="stm32f411", verbose=QL_VERBOSE.DEBUG) |
| 144 | + |
| 145 | + ql.hw.create('spi1') |
| 146 | + ql.hw.create('rcc') |
| 147 | + ql.hw.create('usart2') |
| 148 | + ql.hw.create('gpioa') |
| 149 | + |
| 150 | + ql.run(count=30000) |
| 151 | + self.assertTrue(ql.hw.usart2.recv() == b'----------------SPI TEST----------------\najcmfoiblenhakdmgpjclfoibkengajd\nmfpicleohbkdngajcmfoiblenhakdmgp\njclfoibkengajdmfpicleohbkdngajcm\nfoiblenhakdmgpjclfoibkengajdmfpi\ncleohbkdngajcmfoiblenhakdmgpjclf\noibkenhajdmfpicleohbkdngajcmfpib\nlenhakdmgpjclfoibkenhajdmfpicleo\nhbkdngajcmfpiblenhakdmgpjclfoibk\n----------------TEST END----------------\n') |
| 152 | + |
| 153 | + del ql |
| 154 | + |
| 155 | + def test_mcu_led_rust_stm32f411(self): |
| 156 | + ql = Qiling(["../examples/rootfs/mcu/stm32f411/led-rust.hex"], |
| 157 | + archtype="cortex_m", profile="stm32f411", verbose=QL_VERBOSE.DEBUG) |
| 158 | + |
| 159 | + count = 0 |
| 160 | + def counter(): |
| 161 | + nonlocal count |
| 162 | + count += 1 |
| 163 | + |
| 164 | + ql.hw.create('gpioa').hook_set(5, counter) |
| 165 | + ql.hw.create('rcc') |
| 166 | + |
| 167 | + ql.run(count=1000) |
| 168 | + self.assertTrue(count >= 5) |
| 169 | + |
| 170 | + del ql |
| 171 | + |
| 172 | +if __name__ == "__main__": |
| 173 | + unittest.main() |
| 174 | + |
0 commit comments