about summary refs log tree commit diff stats
path: root/example/jitter/msp430.py
blob: 1ecb4cef0949c32a50073eb5c5f342d117848095 (plain) (blame)
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
#! /usr/bin/env python2
#-*- coding:utf-8 -*-
from __future__ import print_function
from argparse import ArgumentParser
from miasm2.analysis import debugging
from miasm2.jitter.csts import *
from miasm2.analysis.machine import Machine

parser = ArgumentParser(
    description="""Sandbox raw binary with msp430 engine
(ex: jit_msp430.py example/msp430_sc.bin 0)""")
parser.add_argument("-t", "--trace",
                    help="Log instructions/registers values",
                    action="store_true")
parser.add_argument("-n", "--log-newbloc",
                    help="Log basic blocks processed by the Jitter",
                    action="store_true")
parser.add_argument("-j", "--jitter",
                    help="Jitter engine (default is 'gcc')",
                    default="gcc")
parser.add_argument("-d", "--debugging",
                    help="Attach a CLI debugguer to the sandboxed program",
                    action="store_true")
parser.add_argument("binary",
                    help="binary to run")
parser.add_argument("addr",
                    help="start exec on addr")

machine = Machine("msp430")

def jit_msp430_binary(args):
    filepath, entryp = args.binary, int(args.addr, 0)
    myjit = machine.jitter(jit_type = args.jitter)
    myjit.init_stack()

    # Log level (if available with jitter engine)
    myjit.set_trace_log(
        trace_instr=args.trace,
        trace_regs=args.trace,
        trace_new_blocks=args.log_newbloc
    )

    myjit.vm.add_memory_page(
        0,
        PAGE_READ | PAGE_WRITE,
        open(filepath, "rb").read()
    )
    myjit.add_breakpoint(0x1337, lambda _: exit(0))


    # for stack
    myjit.vm.add_memory_page(0xF000, PAGE_READ | PAGE_WRITE, b"\x00"*0x1000)

    myjit.cpu.SP = 0xF800

    myjit.push_uint16_t(0x1337)
    myjit.init_run(entryp)



    # Handle debugging
    if args.debugging is True:
        dbg = debugging.Debugguer(myjit)
        cmd = debugging.DebugCmd(dbg)
        cmd.cmdloop()

    else:
        print(myjit.continue_run())

if __name__ == '__main__':
    args = parser.parse_args()
    jit_msp430_binary(args)