summaryrefslogtreecommitdiffstats
path: root/tools/crypto/ccp/dbc_cli.py
blob: bf52233fd0380a7bde82281f41f4aff263363ff0 (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/python3
# SPDX-License-Identifier: GPL-2.0
import argparse
import binascii
import os
import errno
from dbc import *

ERRORS = {
    errno.EACCES: "Access is denied",
    errno.E2BIG: "Excess data provided",
    errno.EINVAL: "Bad parameters",
    errno.EAGAIN: "Bad state",
    errno.ENOENT: "Not implemented or message failure",
    errno.EBUSY: "Busy",
    errno.ENFILE: "Overflow",
    errno.EPERM: "Signature invalid",
}

messages = {
    "get-fmax-cap": PARAM_GET_FMAX_CAP,
    "set-fmax-cap": PARAM_SET_FMAX_CAP,
    "get-power-cap": PARAM_GET_PWR_CAP,
    "set-power-cap": PARAM_SET_PWR_CAP,
    "get-graphics-mode": PARAM_GET_GFX_MODE,
    "set-graphics-mode": PARAM_SET_GFX_MODE,
    "get-current-temp": PARAM_GET_CURR_TEMP,
    "get-fmax-max": PARAM_GET_FMAX_MAX,
    "get-fmax-min": PARAM_GET_FMAX_MIN,
    "get-soc-power-max": PARAM_GET_SOC_PWR_MAX,
    "get-soc-power-min": PARAM_GET_SOC_PWR_MIN,
    "get-soc-power-cur": PARAM_GET_SOC_PWR_CUR,
}


def _pretty_buffer(ba):
    return str(binascii.hexlify(ba, " "))


def parse_args():
    parser = argparse.ArgumentParser(
        description="Dynamic Boost control command line interface"
    )
    parser.add_argument(
        "command",
        choices=["get-nonce", "get-param", "set-param", "set-uid"],
        help="Command to send",
    )
    parser.add_argument("--device", default="/dev/dbc", help="Device to operate")
    parser.add_argument("--signature", help="File containing signature for command")
    parser.add_argument("--message", choices=messages.keys(), help="Message index")
    parser.add_argument("--data", help="Argument to pass to message")
    parser.add_argument("--uid", help="File containing UID to pass")
    return parser.parse_args()


def pretty_error(code):
    if code in ERRORS:
        print(ERRORS[code])
    else:
        print("failed with return code %d" % code)


if __name__ == "__main__":
    args = parse_args()
    data = 0
    sig = None
    uid = None
    if not os.path.exists(args.device):
        raise IOError("Missing device {device}".format(device=args.device))
    if args.signature:
        if not os.path.exists(args.signature):
            raise ValueError("Invalid signature file %s" % args.signature)
        with open(args.signature, "rb") as f:
            sig = f.read()
        if len(sig) != DBC_SIG_SIZE:
            raise ValueError(
                "Invalid signature length %d (expected %d)" % (len(sig), DBC_SIG_SIZE)
            )
    if args.uid:
        if not os.path.exists(args.uid):
            raise ValueError("Invalid uid file %s" % args.uid)
        with open(args.uid, "rb") as f:
            uid = f.read()
        if len(uid) != DBC_UID_SIZE:
            raise ValueError(
                "Invalid UID length %d (expected %d)" % (len(uid), DBC_UID_SIZE)
            )
    if args.data:
        try:
            data = int(args.data, 10)
        except ValueError:
            data = int(args.data, 16)

    with open(args.device) as d:
        if args.command == "get-nonce":
            try:
                nonce = get_nonce(d, sig)
                print("Nonce: %s" % _pretty_buffer(bytes(nonce)))
            except OSError as e:
                pretty_error(e.errno)
        elif args.command == "set-uid":
            try:
                result = set_uid(d, uid, sig)
                if result:
                    print("Set UID")
            except OSError as e:
                pretty_error(e.errno)
        elif args.command == "get-param":
            if not args.message or args.message.startswith("set"):
                raise ValueError("Invalid message %s" % args.message)
            try:
                param, signature = process_param(d, messages[args.message], sig)
                print(
                    "Parameter: {par}, response signature {sig}".format(
                        par=param,
                        sig=_pretty_buffer(bytes(signature)),
                    )
                )
            except OSError as e:
                pretty_error(e.errno)
        elif args.command == "set-param":
            if not args.message or args.message.startswith("get"):
                raise ValueError("Invalid message %s" % args.message)
            try:
                param, signature = process_param(d, messages[args.message], sig, data)
                print(
                    "Parameter: {par}, response signature {sig}".format(
                        par=param,
                        sig=_pretty_buffer(bytes(signature)),
                    )
                )
            except OSError as e:
                pretty_error(e.errno)