Skip to content

Commit

Permalink
Merge pull request commaai#276 from commaai/python3
Browse files Browse the repository at this point in the history
Python3
  • Loading branch information
geohot committed Sep 27, 2019
2 parents 9893a84 + d326869 commit 11b7151
Show file tree
Hide file tree
Showing 65 changed files with 354 additions and 285 deletions.
19 changes: 18 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ RUN apt-get update && apt-get install -y \
gperf \
help2man \
iputils-ping \
libbz2-dev \
libexpat-dev \
libffi-dev \
libssl-dev \
libstdc++-arm-none-eabi-newlib \
libtool \
libtool-bin \
libusb-1.0-0 \
locales \
make \
ncurses-dev \
network-manager \
Expand All @@ -38,7 +42,20 @@ RUN apt-get update && apt-get install -y \
screen \
vim \
wget \
wireless-tools
wireless-tools \
zlib1g-dev

RUN sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && locale-gen
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8

RUN curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash

ENV PATH="/root/.pyenv/bin:/root/.pyenv/shims:${PATH}"
RUN pyenv install 3.7.3
RUN pyenv global 3.7.3
RUN pyenv rehash

RUN pip install --upgrade pip==18.0

Expand Down
4 changes: 2 additions & 2 deletions board/tools/enter_download_mode.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python
from __future__ import print_function
#!/usr/bin/env python3


import sys
import time
Expand Down
16 changes: 8 additions & 8 deletions crypto/getcertheader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
import sys
import struct
from Crypto.PublicKey import RSA
Expand Down Expand Up @@ -26,7 +26,7 @@ def to_c_uint32(x):
nums = []
for i in range(0x20):
nums.append(x%(2**32))
x /= (2**32)
x //= (2**32)
return "{"+'U,'.join(map(str, nums))+"U}"

for fn in sys.argv[1:]:
Expand All @@ -36,11 +36,11 @@ def to_c_uint32(x):

cname = fn.split("/")[-1].split(".")[0] + "_rsa_key"

print 'RSAPublicKey '+cname+' = {.len = 0x20,'
print ' .n0inv = %dU,' % n0inv
print ' .n = %s,' % to_c_uint32(rsa.n)
print ' .rr = %s,' % to_c_uint32(rr)
print ' .exponent = %d,' % rsa.e
print '};'
print('RSAPublicKey '+cname+' = {.len = 0x20,')
print(' .n0inv = %dU,' % n0inv)
print(' .n = %s,' % to_c_uint32(rsa.n))
print(' .rr = %s,' % to_c_uint32(rr))
print(' .exponent = %d,' % rsa.e)
print('};')


18 changes: 10 additions & 8 deletions crypto/sign.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#!/usr/bin/env python
#!/usr/bin/env python3
import os
import sys
import struct
import hashlib
from Crypto.PublicKey import RSA
import binascii

rsa = RSA.importKey(open(sys.argv[3]).read())

with open(sys.argv[1]) as f:
with open(sys.argv[1], "rb") as f:
dat = f.read()

print "signing", len(dat), "bytes"
print("signing", len(dat), "bytes")

with open(sys.argv[2], "wb") as f:
if os.getenv("SETLEN") is not None:
Expand All @@ -20,10 +21,11 @@
else:
x = dat
dd = hashlib.sha1(dat).digest()
print "hash:",dd.encode("hex")
dd = "\x00\x01" + "\xff"*0x69 + "\x00" + dd
rsa_out = pow(int(dd.encode("hex"), 16), rsa.d, rsa.n)
sig = (hex(rsa_out)[2:-1].rjust(0x100, '0')).decode("hex")
x += sig

print("hash:", str(binascii.hexlify(dd), "utf-8"))
dd = b"\x00\x01" + b"\xff"*0x69 + b"\x00" + dd
rsa_out = pow(int.from_bytes(dd, byteorder='big', signed=False), rsa.d, rsa.n)
sig = (hex(rsa_out)[2:].rjust(0x100, '0'))
x += binascii.unhexlify(sig)
f.write(x)

18 changes: 9 additions & 9 deletions examples/can_bit_transition.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

import binascii
import csv
Expand All @@ -13,13 +13,13 @@ def __init__(self, message_id):

def printBitDiff(self, other):
"""Prints bits that transition from always zero to always 1 and vice versa."""
for i in xrange(len(self.ones)):
for i in range(len(self.ones)):
zero_to_one = other.zeros[i] & self.ones[i]
if zero_to_one:
print 'id %s 0 -> 1 at byte %d bitmask %d' % (self.message_id, i, zero_to_one)
print('id %s 0 -> 1 at byte %d bitmask %d' % (self.message_id, i, zero_to_one))
one_to_zero = other.ones[i] & self.zeros[i]
if one_to_zero:
print 'id %s 1 -> 0 at byte %d bitmask %d' % (self.message_id, i, one_to_zero)
print('id %s 1 -> 0 at byte %d bitmask %d' % (self.message_id, i, one_to_zero))


class Info():
Expand Down Expand Up @@ -56,7 +56,7 @@ def load(self, filename, start, end):
new_message = True
message = self.messages[message_id]
bytes = bytearray.fromhex(data)
for i in xrange(len(bytes)):
for i in range(len(bytes)):
ones = int(bytes[i])
message.ones[i] = ones if new_message else message.ones[i] & ones
# Inverts the data and masks it to a byte to get the zeros as ones.
Expand All @@ -65,11 +65,11 @@ def load(self, filename, start, end):

def PrintUnique(log_file, low_range, high_range):
# find messages with bits that are always low
start, end = map(float, low_range.split('-'))
start, end = list(map(float, low_range.split('-')))
low = Info()
low.load(log_file, start, end)
# find messages with bits that are always high
start, end = map(float, high_range.split('-'))
start, end = list(map(float, high_range.split('-')))
high = Info()
high.load(log_file, start, end)
# print messages that go from low to high
Expand All @@ -78,10 +78,10 @@ def PrintUnique(log_file, low_range, high_range):
if message_id in low.messages:
high.messages[message_id].printBitDiff(low.messages[message_id])
found = True
if not found: print 'No messages that transition from always low to always high found!'
if not found: print('No messages that transition from always low to always high found!')

if __name__ == "__main__":
if len(sys.argv) < 4:
print 'Usage:\n%s log.csv <low-start>-<low-end> <high-start>-<high-end>' % sys.argv[0]
print('Usage:\n%s log.csv <low-start>-<low-end> <high-start>-<high-end>' % sys.argv[0])
sys.exit(0)
PrintUnique(sys.argv[1], sys.argv[2], sys.argv[3])
4 changes: 2 additions & 2 deletions examples/can_logger.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python
from __future__ import print_function
#!/usr/bin/env python3

import binascii
import csv
import sys
Expand Down
18 changes: 9 additions & 9 deletions examples/can_unique.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

# Given an interesting CSV file of CAN messages and a list of background CAN
# messages, print which bits in the interesting file have never appeared
Expand Down Expand Up @@ -28,15 +28,15 @@ def __init__(self, message_id):

def printBitDiff(self, other):
"""Prints bits that are set or cleared compared to other background."""
for i in xrange(len(self.ones)):
for i in range(len(self.ones)):
new_ones = ((~other.ones[i]) & 0xff) & self.ones[i]
if new_ones:
print 'id %s new one at byte %d bitmask %d' % (
self.message_id, i, new_ones)
print('id %s new one at byte %d bitmask %d' % (
self.message_id, i, new_ones))
new_zeros = ((~other.zeros[i]) & 0xff) & self.zeros[i]
if new_zeros:
print 'id %s new zero at byte %d bitmask %d' % (
self.message_id, i, new_zeros)
print('id %s new zero at byte %d bitmask %d' % (
self.message_id, i, new_zeros))


class Info():
Expand Down Expand Up @@ -67,7 +67,7 @@ def load(self, filename):
if data not in self.messages[message_id].data:
message.data[data] = True
bytes = bytearray.fromhex(data)
for i in xrange(len(bytes)):
for i in range(len(bytes)):
message.ones[i] = message.ones[i] | int(bytes[i])
# Inverts the data and masks it to a byte to get the zeros as ones.
message.zeros[i] = message.zeros[i] | ( (~int(bytes[i])) & 0xff)
Expand All @@ -80,14 +80,14 @@ def PrintUnique(interesting_file, background_files):
interesting.load(interesting_file)
for message_id in sorted(interesting.messages):
if message_id not in background.messages:
print 'New message_id: %s' % message_id
print('New message_id: %s' % message_id)
else:
interesting.messages[message_id].printBitDiff(
background.messages[message_id])


if __name__ == "__main__":
if len(sys.argv) < 3:
print 'Usage:\n%s interesting.csv background*.csv' % sys.argv[0]
print('Usage:\n%s interesting.csv background*.csv' % sys.argv[0])
sys.exit(0)
PrintUnique(sys.argv[1], sys.argv[2:])
4 changes: 2 additions & 2 deletions examples/get_panda_password.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
from panda import Panda

def get_panda_password():
Expand All @@ -17,4 +17,4 @@ def get_panda_password():
print("Password: " + wifi[1])

if __name__ == "__main__":
get_panda_password()
get_panda_password()
10 changes: 5 additions & 5 deletions examples/query_vin_and_stats.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
import time
import struct
from panda import Panda
Expand Down Expand Up @@ -36,23 +36,23 @@ def get_supported_pids():
isotp_send(panda, "\x09\x02", 0x7df)
ret = isotp_recv(panda, 0x7e8)
hexdump(ret)
print "VIN: %s" % ret[2:]
print("VIN: %s" % ret[2:])

# 03 = get DTCS
isotp_send(panda, "\x03", 0x7e0)
dtcs = isotp_recv(panda, 0x7e8)
print "DTCs:", dtcs[2:].encode("hex")
print("DTCs:", dtcs[2:].encode("hex"))

supported_pids = get_supported_pids()
print "Supported PIDs:",supported_pids
print("Supported PIDs:",supported_pids)

while 1:
speed = struct.unpack(">B", get_current_data_for_pid(13)[2:])[0] # kph
rpm = struct.unpack(">H", get_current_data_for_pid(12)[2:])[0]/4.0 # revs
throttle = struct.unpack(">B", get_current_data_for_pid(17)[2:])[0]/255.0 * 100 # percent
temp = struct.unpack(">B", get_current_data_for_pid(5)[2:])[0] - 40 # degrees C
load = struct.unpack(">B", get_current_data_for_pid(4)[2:])[0]/255.0 * 100 # percent
print "%d KPH, %d RPM, %.1f%% Throttle, %d deg C, %.1f%% load" % (speed, rpm, throttle, temp, load)
print("%d KPH, %d RPM, %.1f%% Throttle, %d deg C, %.1f%% load" % (speed, rpm, throttle, temp, load))
time.sleep(0.2)


Expand Down
2 changes: 1 addition & 1 deletion examples/tesla_tester.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
import sys
import binascii
from panda import Panda
Expand Down
22 changes: 11 additions & 11 deletions python/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# python library to interface with panda
from __future__ import print_function

import binascii
import struct
import hashlib
Expand All @@ -9,12 +9,12 @@
import time
import traceback
import subprocess
from dfu import PandaDFU
from esptool import ESPROM, CesantaFlasher
from flash_release import flash_release
from update import ensure_st_up_to_date
from serial import PandaSerial
from isotp import isotp_send, isotp_recv
from .dfu import PandaDFU
from .esptool import ESPROM, CesantaFlasher
from .flash_release import flash_release
from .update import ensure_st_up_to_date
from .serial import PandaSerial
from .isotp import isotp_send, isotp_recv

__version__ = '0.0.9'

Expand Down Expand Up @@ -232,7 +232,7 @@ def reconnect(self):
def flash_static(handle, code):
# confirm flasher is present
fr = handle.controlRead(Panda.REQUEST_IN, 0xb0, 0, 0, 0xc)
assert fr[4:8] == "\xde\xad\xd0\x0d"
assert fr[4:8] == b"\xde\xad\xd0\x0d"

# unlock flash
print("flash: unlocking")
Expand Down Expand Up @@ -274,7 +274,7 @@ def flash(self, fn=None, code=None, reconnect=True):
fn = os.path.join(BASEDIR, "board", fn)

if code is None:
with open(fn) as f:
with open(fn, "rb") as f:
code = f.read()

# get version
Expand Down Expand Up @@ -364,7 +364,7 @@ def enter_bootloader(self):
pass

def get_version(self):
return self._handle.controlRead(Panda.REQUEST_IN, 0xd6, 0, 0, 0x40)
return self._handle.controlRead(Panda.REQUEST_IN, 0xd6, 0, 0, 0x40).decode('utf8')

def get_type(self):
return self._handle.controlRead(Panda.REQUEST_IN, 0xc1, 0, 0, 0x40)
Expand Down Expand Up @@ -479,7 +479,7 @@ def can_recv(self):
break
except (usb1.USBErrorIO, usb1.USBErrorOverflow):
print("CAN: BAD RECV, RETRYING")
time.sleep(0.1)
time.sleep(0.1)
return parse_can_buffer(dat)

def can_clear(self, bus):
Expand Down
2 changes: 1 addition & 1 deletion python/dfu.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from __future__ import print_function

import os
import usb1
import struct
Expand Down
Loading

0 comments on commit 11b7151

Please sign in to comment.