|
| 1 | +import struct |
1 | 2 | import portio |
| 3 | +import warnings |
2 | 4 | from .constants.COMMON import * |
3 | 5 | from .constants.LPC import * |
4 | 6 | from .constants.MEMMAP import * |
| 7 | +from .exceptions import ECError |
5 | 8 |
|
6 | 9 |
|
7 | | -def ec_init(address: Int32 = EC_LPC_ADDR_MEMMAP): |
8 | | - if portio.ioperm(address, EC_MEMMAP_SIZE, True): |
9 | | - print("Permission denied. Try running as root.") |
10 | | - exit(1) |
| 10 | +def ec_init(address: Int32 = EC_LPC_ADDR_MEMMAP) -> None: |
| 11 | + """ |
| 12 | + Initialise the EC. Checks for the EC, and configures the library to speak the same version. |
| 13 | + @param address: Address of the EC memory map. |
| 14 | + """ |
| 15 | + # Request I/O permissions |
| 16 | + if portio.ioperm(address, EC_MEMMAP_SIZE, True) or \ |
| 17 | + portio.ioperm(EC_LPC_ADDR_HOST_DATA, EC_MEMMAP_SIZE, True) or \ |
| 18 | + portio.ioperm(EC_LPC_ADDR_HOST_CMD, EC_MEMMAP_SIZE, True) or \ |
| 19 | + portio.ioperm(EC_LPC_ADDR_HOST_PACKET, EC_LPC_HOST_PACKET_SIZE, True): |
| 20 | + raise PermissionError("Permission denied. Try running as root.") |
| 21 | + |
| 22 | + status = 0xFF |
| 23 | + |
| 24 | + # Read status bits, at least one should be 0 |
| 25 | + status &= portio.inb(EC_LPC_ADDR_HOST_CMD) |
| 26 | + status &= portio.inb(EC_LPC_ADDR_HOST_DATA) |
| 27 | + |
| 28 | + if status == 0xFF: |
| 29 | + raise OSError("No EC detected.") |
| 30 | + |
| 31 | + # Check for 'EC' in memory map |
| 32 | + if portio.inb(address + EC_MEMMAP_ID) != ord("E") and portio.inb( |
| 33 | + address + EC_MEMMAP_ID + 1 |
| 34 | + ) != ord("C"): |
| 35 | + raise OSError("Invalid EC signature.") |
| 36 | + |
| 37 | + ec_get_cmd_version(address) |
| 38 | + |
| 39 | + |
| 40 | +def wait_for_ec(status_addr: Int32 = EC_LPC_ADDR_HOST_CMD) -> None: |
| 41 | + """ |
| 42 | + Wait for the EC to be ready after sending a command. |
| 43 | + @param status_addr: The status register to read. |
| 44 | + """ |
| 45 | + while portio.inb(status_addr) & EC_LPC_STATUS_BUSY_MASK: |
| 46 | + pass |
| 47 | + |
| 48 | + |
| 49 | +def ec_command_v2(): |
| 50 | + raise NotImplementedError |
| 51 | + |
| 52 | + |
| 53 | +def ec_command_v3(version: UInt8, command: UInt32, outsize: UInt16, insize: UInt32, data: bytes = None, |
| 54 | + warn: bool = True) -> bytes: |
| 55 | + """ |
| 56 | + Send a command to the EC and return the response. Uses the v3 command protocol over LPC. |
| 57 | + @param version: Command version number (often 0). |
| 58 | + @param command: Command to send (EC_CMD_...). |
| 59 | + @param outsize: Outgoing length in bytes. |
| 60 | + @param insize: Max number of bytes to accept from the EC. |
| 61 | + @param data: Outgoing data to EC. |
| 62 | + @param warn: Whether to warn if the response size is not as expected. Default is True. |
| 63 | + @return: Response from the EC. |
| 64 | + """ |
| 65 | + csum = 0 |
| 66 | + request = bytearray(struct.pack("BBHBxH", EC_HOST_REQUEST_VERSION, csum, command, version, outsize)) |
| 67 | + |
| 68 | + # Fail if output size is too big |
| 69 | + if outsize + len(request) > EC_LPC_HOST_PACKET_SIZE: |
| 70 | + raise ValueError("Output size too big!") |
| 71 | + |
| 72 | + # Copy data and start checksum |
| 73 | + for i in range(outsize): |
| 74 | + portio.outb(data[i], EC_LPC_ADDR_HOST_PACKET + len(request) + i) |
| 75 | + csum += data[i] |
| 76 | + |
| 77 | + # Finish checksum |
| 78 | + for i in range(len(request)): |
| 79 | + csum += request[i] |
| 80 | + |
| 81 | + request[1] = (-csum) & 0xff |
| 82 | + |
| 83 | + # Copy header |
| 84 | + for i in range(len(request)): |
| 85 | + portio.outb(request[i], EC_LPC_ADDR_HOST_PACKET + i) |
| 86 | + |
| 87 | + # Start the command |
| 88 | + portio.outb(EC_COMMAND_PROTOCOL_3, EC_LPC_ADDR_HOST_CMD) |
| 89 | + |
| 90 | + wait_for_ec() |
| 91 | + |
| 92 | + # Check result |
| 93 | + i = portio.inb(EC_LPC_ADDR_HOST_DATA) |
| 94 | + if i: |
| 95 | + raise ECError(i) |
| 96 | + |
| 97 | + # Read back response and start checksum |
| 98 | + csum = 0 |
| 99 | + data_out = bytearray(1 + 1 + 2 + 2 + 2) |
| 100 | + for i in range(len(data_out)): |
| 101 | + data_out[i] = portio.inb(EC_LPC_ADDR_HOST_PACKET + i) |
| 102 | + csum += data_out[i] |
| 103 | + |
| 104 | + response = struct.unpack("BBHHH", data_out) |
| 105 | + |
| 106 | + if response[0] != EC_HOST_RESPONSE_VERSION: |
| 107 | + raise IOError("Invalid response version!") |
| 108 | + |
| 109 | + if response[4]: |
| 110 | + # Reserved should be 0 |
| 111 | + raise IOError("Invalid response!") |
| 112 | + |
| 113 | + if response[3] != insize and warn: |
| 114 | + warnings.warn(f"Expected {insize} bytes, got {response[3]} back from EC", RuntimeWarning) |
| 115 | + |
| 116 | + # Read back data |
| 117 | + data = bytearray() |
| 118 | + for i in range(response[3]): |
| 119 | + data.append(portio.inb(EC_LPC_ADDR_HOST_PACKET + len(data_out) + i)) |
| 120 | + csum += data[i] |
| 121 | + |
| 122 | + if csum & 0xff: |
| 123 | + raise IOError("Checksum error!") |
| 124 | + |
| 125 | + return bytes(data) |
| 126 | + |
| 127 | + |
| 128 | +def ec_command(*args): |
| 129 | + """ |
| 130 | + Stub function, will get overwritten in ec_get_cmd_version. |
| 131 | + """ |
| 132 | + raise NotImplementedError |
| 133 | + |
| 134 | + |
| 135 | +def ec_get_cmd_version(address: Int32 = EC_LPC_ADDR_MEMMAP) -> int: |
| 136 | + global ec_command |
| 137 | + |
| 138 | + version = portio.inb(address + EC_MEMMAP_HOST_CMD_FLAGS) |
| 139 | + |
| 140 | + if version & EC_HOST_CMD_FLAG_VERSION_3: |
| 141 | + ec_command = ec_command_v3 |
| 142 | + return 3 |
| 143 | + elif version & EC_HOST_CMD_FLAG_LPC_ARGS_SUPPORTED: |
| 144 | + ec_command = ec_command_v2 |
| 145 | + return 2 |
| 146 | + else: |
| 147 | + warnings.warn("EC doesn't support commands!", RuntimeWarning) |
| 148 | + return 0 |
11 | 149 |
|
12 | 150 |
|
13 | 151 | def ec_readmem(offset: Int32, num_bytes: Int32, address: Int32 = EC_LPC_ADDR_MEMMAP) -> bytes: |
14 | 152 | """ |
15 | 153 | Read memory from the EC. |
16 | 154 | @param offset: Offset to read from. |
17 | 155 | @param num_bytes: Number of bytes to read. |
| 156 | + @param address: Address of the EC memory map. |
18 | 157 | @return: Bytes read from the EC. |
19 | 158 | """ |
20 | 159 | data = bytearray() |
|
0 commit comments