xref: /linux/tools/crypto/ccp/dbc.py (revision 55ec81f7517fad09135f65552cea0a3ee84fff30)
1#!/usr/bin/python3
2# SPDX-License-Identifier: GPL-2.0
3
4import ctypes
5import os
6
7DBC_UID_SIZE = 16
8DBC_NONCE_SIZE = 16
9DBC_SIG_SIZE = 32
10
11PARAM_GET_FMAX_CAP = (0x3,)
12PARAM_SET_FMAX_CAP = (0x4,)
13PARAM_GET_PWR_CAP = (0x5,)
14PARAM_SET_PWR_CAP = (0x6,)
15PARAM_GET_GFX_MODE = (0x7,)
16PARAM_SET_GFX_MODE = (0x8,)
17PARAM_GET_CURR_TEMP = (0x9,)
18PARAM_GET_FMAX_MAX = (0xA,)
19PARAM_GET_FMAX_MIN = (0xB,)
20PARAM_GET_SOC_PWR_MAX = (0xC,)
21PARAM_GET_SOC_PWR_MIN = (0xD,)
22PARAM_GET_SOC_PWR_CUR = (0xE,)
23
24DEVICE_NODE = "/dev/dbc"
25
26lib = ctypes.CDLL("./dbc_library.so", mode=ctypes.RTLD_GLOBAL)
27
28
29def handle_error(code):
30    val = code * -1
31    raise OSError(val, os.strerror(val))
32
33
34def get_nonce(device, signature):
35    if not device:
36        raise ValueError("Device required")
37    buf = ctypes.create_string_buffer(DBC_NONCE_SIZE)
38    ret = lib.get_nonce(device.fileno(), ctypes.byref(buf), signature)
39    if ret:
40        handle_error(ret)
41    return buf.value
42
43
44def set_uid(device, new_uid, signature):
45    if not signature:
46        raise ValueError("Signature required")
47    if not new_uid:
48        raise ValueError("UID required")
49    ret = lib.set_uid(device.fileno(), new_uid, signature)
50    if ret:
51        handle_error(ret)
52    return True
53
54
55def process_param(device, message, signature, data=None):
56    if not signature:
57        raise ValueError("Signature required")
58    if type(message) != tuple:
59        raise ValueError("Expected message tuple")
60    arg = ctypes.c_int(data if data else 0)
61    ret = lib.process_param(device.fileno(), message[0], signature, ctypes.pointer(arg))
62    if ret:
63        handle_error(ret)
64    return arg, signature
65