Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migration to python3 #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions umap2/apps/list_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def run(self):
ks = self.umap_classes
verbose = self.options.get('--verbose', False)
if verbose:
print '%-20s %s' % ('Device', 'Description')
print '-------------------- ----------------------------------------------------'
print('%-20s %s' % ('Device', 'Description'))
print('-------------------- ----------------------------------------------------')
for k in ks:
if verbose:
print('%-20s %s' % (k, self.umap_class_dict[k][1]))
Expand Down
64 changes: 32 additions & 32 deletions umap2/dev/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,23 @@ def setup_local_handlers(self):
}
self._settings = {
# (val, index): [cur, min, max, res, (idle)]
(0x0100, 0x0001): ['\x44\xac\x00', '\x44\xac\x00', '\x80\xbb\x00', '\x80\xbb\x00'],
(0x0100, 0x0001): [b'\x44\xac\x00', b'\x44\xac\x00', b'\x80\xbb\x00', b'\x80\xbb\x00'],
# (0x0100, 0x0002): ['\x44\xac\x00', '\x44\xac\x00', '\x80\xbb\x00', '\x80\xbb\x00'],
(0x0100, 0x0082): ['\x44\xac\x00', '\x44\xac\x00', '\x80\xbb\x00', '\x80\xbb\x00'],
(0x0100, 0x0900): ['\x00', '\x00', '\xff', '\x00'],
(0x0100, 0x0a00): ['\x01', '\x00', '\xff', '\x00'],
(0x0100, 0x0d00): ['\x01', '\x00', '\xff', '\x00'],
(0x0101, 0x0f00): ['\x01', '\x00', '\xff', '\x00'],
(0x0102, 0x0f00): ['\x01', '\x00', '\xff', '\x00'],
(0x0200, 0x0a00): ['\x00\x00', '\x00\x00', '\x55\x00', '\x30\x00', '\x00\x00'],
(0x0200, 0x0d00): ['\x80\x22', '\x00\x00', '\xd0\x00', '\x30\x00'],
(0x0201, 0x0900): ['\x80\x22', '\x20\x00', '\xa0\x00', '\x30\x00'],
(0x0201, 0x0f00): ['\x01', '\x00', '\xff', '\x00'],
(0x0202, 0x0900): ['\xcf\x00', '\x00\x00', '\xcf\x00', '\x30\x00'],
(0x0202, 0x0f00): ['\x01', '\x00', '\xff', '\x00'],
(0x0301, 0x0f00): ['\x01', '\x00', '\xff', '\x00'],
(0x0302, 0x0f00): ['\x00\x00', '\x00\x00', '\x00\x00', '\x00\x00'],
(0x0700, 0x0a00): ['\x01', '\x00', '\xff', '\x00'],
(0x0100, 0x0082): [b'\x44\xac\x00', b'\x44\xac\x00', b'\x80\xbb\x00', b'\x80\xbb\x00'],
(0x0100, 0x0900): [b'\x00', b'\x00', b'\xff', b'\x00'],
(0x0100, 0x0a00): [b'\x01', b'\x00', b'\xff', b'\x00'],
(0x0100, 0x0d00): [b'\x01', b'\x00', b'\xff', b'\x00'],
(0x0101, 0x0f00): [b'\x01', b'\x00', b'\xff', b'\x00'],
(0x0102, 0x0f00): [b'\x01', b'\x00', b'\xff', b'\x00'],
(0x0200, 0x0a00): [b'\x00\x00', b'\x00\x00', b'\x55\x00', b'\x30\x00', b'\x00\x00'],
(0x0200, 0x0d00): [b'\x80\x22', b'\x00\x00', b'\xd0\x00', b'\x30\x00'],
(0x0201, 0x0900): [b'\x80\x22', b'\x20\x00', b'\xa0\x00', b'\x30\x00'],
(0x0201, 0x0f00): [b'\x01', b'\x00', b'\xff', b'\x00'],
(0x0202, 0x0900): [b'\xcf\x00', b'\x00\x00', b'\xcf\x00', b'\x30\x00'],
(0x0202, 0x0f00): [b'\x01', b'\x00', b'\xff', b'\x00'],
(0x0301, 0x0f00): [b'\x01', b'\x00', b'\xff', b'\x00'],
(0x0302, 0x0f00): [b'\x00\x00', b'\x00\x00', b'\x00\x00', b'\x00\x00'],
(0x0700, 0x0a00): [b'\x01', b'\x00', b'\xff', b'\x00'],
}

self._cur = b'\x44\xac\x00'
Expand Down Expand Up @@ -205,29 +205,29 @@ def __init__(self, app, phy, vid=0x0d8c, pid=0x000c, rev=0x0001, *args, **kwargs
app=app, phy=phy, iface_num=0, iface_alt=0, iface_str_idx=0,
cs_ifaces=[
# Class specific AC interface: header (4.3.2)
USBCSInterface('ACHeader', app, phy, '\x01\x00\x01\x64\x00\x02\x01\x02'),
USBCSInterface('ACHeader', app, phy, b'\x01\x00\x01\x64\x00\x02\x01\x02'),
# Class specific AC interface: input terminal (Table 4.3.2.1)
USBCSInterface('ACInputTerminal0', app, phy, '\x02\x01\x01\x01\x00\x02\x03\x00\x00\x00'),
USBCSInterface('ACInputTerminal1', app, phy, '\x02\x02\x01\x02\x00\x01\x01\x00\x00\x00'),
USBCSInterface('ACInputTerminal0', app, phy, b'\x02\x01\x01\x01\x00\x02\x03\x00\x00\x00'),
USBCSInterface('ACInputTerminal1', app, phy, b'\x02\x02\x01\x02\x00\x01\x01\x00\x00\x00'),
# Class specific AC interface: output terminal (Table 4.3.2.2)
USBCSInterface('ACOutputTerminal0', app, phy, '\x03\x06\x01\x03\x00\x09\x00'),
USBCSInterface('ACOutputTerminal1', app, phy, '\x03\x07\x01\x01\x00\x08\x00'),
USBCSInterface('ACOutputTerminal0', app, phy, b'\x03\x06\x01\x03\x00\x09\x00'),
USBCSInterface('ACOutputTerminal1', app, phy, b'\x03\x07\x01\x01\x00\x08\x00'),
# Class specific AC interface: selector unit (Table 4.3.2.4)
USBCSInterface('ACSelectorUnit', app, phy, '\x05\x08\x01\x0a\x00'),
USBCSInterface('ACSelectorUnit', app, phy, b'\x05\x08\x01\x0a\x00'),
# Class specific AC interface: feature unit (Table 4.3.2.5)
USBCSInterface('ACFeatureUnit0', app, phy, '\x06\x09\x0f\x01\x01\x02\x02\x00'),
USBCSInterface('ACFeatureUnit1', app, phy, '\x06\x0a\x02\x01\x43\x00\x00'),
USBCSInterface('ACFeatureUnit2', app, phy, '\x06\x0d\x02\x01\x03\x00\x00'),
USBCSInterface('ACFeatureUnit0', app, phy, b'\x06\x09\x0f\x01\x01\x02\x02\x00'),
USBCSInterface('ACFeatureUnit1', app, phy, b'\x06\x0a\x02\x01\x43\x00\x00'),
USBCSInterface('ACFeatureUnit2', app, phy, b'\x06\x0d\x02\x01\x03\x00\x00'),
# Class specific AC interface: mixer unit (Table 4.3.2.3)
USBCSInterface('ACMixerUnit', app, phy, '\x04\x0f\x02\x01\x0d\x02\x03\x00\x00\x00\x00'),
USBCSInterface('ACMixerUnit', app, phy, b'\x04\x0f\x02\x01\x0d\x02\x03\x00\x00\x00\x00'),
],
usb_class=usb_class
),
USBAudioStreamingInterface(
app=app, phy=phy, iface_num=1, iface_alt=0, iface_str_idx=0,
cs_ifaces=[
USBCSInterface('ASGeneral', app, phy, '\x01\x01\x01\x01\x00'),
USBCSInterface('ASFormatType', app, phy, '\x02\x01\x02\x02\x10\x02\x44\xac\x00\x44\xac\x00'),
USBCSInterface('ASGeneral', app, phy, b'\x01\x01\x01\x01\x00'),
USBCSInterface('ASFormatType', app, phy, b'\x02\x01\x02\x02\x10\x02\x44\xac\x00\x44\xac\x00'),
],
endpoints=[
USBEndpoint(
Expand All @@ -240,7 +240,7 @@ def __init__(self, app, phy, vid=0x0d8c, pid=0x000c, rev=0x0001, *args, **kwargs
interval=1,
handler=audio_streaming.data_available,
cs_endpoints=[
USBCSEndpoint('ASEndpoint', app, phy, '\x01\x01\x01\x01\x00')
USBCSEndpoint('ASEndpoint', app, phy, b'\x01\x01\x01\x01\x00')
],
usb_class=usb_class,
)
Expand All @@ -250,8 +250,8 @@ def __init__(self, app, phy, vid=0x0d8c, pid=0x000c, rev=0x0001, *args, **kwargs
USBAudioStreamingInterface(
app=app, phy=phy, iface_num=2, iface_alt=0, iface_str_idx=0,
cs_ifaces=[
USBCSInterface('ASGeneral', app, phy, '\x01\x07\x01\x01\x00'),
USBCSInterface('ASFormatType', app, phy, '\x02\x01\x01\x02\x10\x02\x44\xac\x00\x44\xac\x00'),
USBCSInterface('ASGeneral', app, phy, b'\x01\x07\x01\x01\x00'),
USBCSInterface('ASFormatType', app, phy, b'\x02\x01\x01\x02\x10\x02\x44\xac\x00\x44\xac\x00'),
],
endpoints=[
USBEndpoint(
Expand All @@ -264,7 +264,7 @@ def __init__(self, app, phy, vid=0x0d8c, pid=0x000c, rev=0x0001, *args, **kwargs
interval=1,
handler=audio_streaming.buffer_available,
cs_endpoints=[
USBCSEndpoint('ASEndpoint', app, phy, '\x01\x01\x00\x00\x00')
USBCSEndpoint('ASEndpoint', app, phy, b'\x01\x01\x00\x00\x00')
],
usb_class=usb_class,
)
Expand Down
2 changes: 1 addition & 1 deletion umap2/dev/cdc.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def handle_getter(self, req):
key = (param_id, req.value, req.index)
if key in self.params:
return self.params[key]
return '\x00' * req.length
return b'\x00' * req.length

def handle_clear(self, req):
param_id = self.get_param_id_from_request(req.request)
Expand Down
2 changes: 1 addition & 1 deletion umap2/dev/cdc_acm.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(self, app, phy, vid=0x2548, pid=0x1001, rev=0x0010, cs_interfaces=N
cdc_cls = self.get_default_class(app, phy)
cs_interfaces = [
# Header Functional Descriptor
FD(app, phy, FD.Header, '\x01\x01'),
FD(app, phy, FD.Header, b'\x01\x01'),
# Call Management Functional Descriptor
FD(app, phy, FD.CM, struct.pack('BB', bmCapabilities, USBCDCDevice.bDataInterface)),
FD(app, phy, FD.ACM, struct.pack('B', bmCapabilities)),
Expand Down
2 changes: 1 addition & 1 deletion umap2/dev/cdc_dl.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(self, app, phy, vid=0x2548, pid=0x1001, rev=0x0010, cs_interfaces=N
cdc_cls = self.get_default_class(app, phy)
cs_interfaces = [
# Header Functional Descriptor
FD(app, phy, FD.Header, '\x01\x01'),
FD(app, phy, FD.Header, b'\x01\x01'),
# Call Management Functional Descriptor
FD(app, phy, FD.CM, struct.pack('BB', bmCapabilities, USBCDCDevice.bDataInterface)),
FD(app, phy, FD.DLM, struct.pack('B', bmCapabilities)),
Expand Down
4 changes: 2 additions & 2 deletions umap2/dev/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ def handle_get_descriptor(self, req):
num_bytes = self.num_ports // 7
if self.num_ports % 7 != 0:
num_bytes += 1
d += '\x00' * num_bytes
d += '\xff' * num_bytes
d += b'\x00' * num_bytes
d += b'\xff' * num_bytes
d = struct.pack('B', len(d) + 1) + d
return d

Expand Down
4 changes: 2 additions & 2 deletions umap2/dev/mass_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def put_sector_data(self, address, data):
block_end = (address + 1) * self.block_size # slices are NON-inclusive

pad_len = (self.block_size - (len(data) % self.block_size)) % self.block_size
data += '\x00' * pad_len
data += b'\x00' * pad_len
self.image[block_start:block_end] = data[:self.block_size]
self.image.flush()

Expand Down Expand Up @@ -391,7 +391,7 @@ def handle_scsi_mode_sense(self, mode_type, page, subpage, alloc_len, ctrl, with
if report is None:
# default behaviour, taken from previous implementation
# this should probably be changed ...
report = '\x07\x00\x00\x00\x00\x00\x00\x00'
report = b'\x07\x00\x00\x00\x00\x00\x00\x00'
if with_header:
self.debug('SCSI mode sense (%d) - adding header' % (mode_type))
report = self._report_header(mode_type, len(report)) + report
Expand Down
2 changes: 1 addition & 1 deletion umap2/fuzz/fuzz_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from kitty.model import GraphModel
from kitty.model import Template, Meta, String, UInt32

from templates import audio, cdc, enum, generic, hid, hub, mass_storage
from . templates import audio, cdc, enum, generic, hid, hub, mass_storage
from templates import smart_card

from controller import UmapController
Expand Down
11 changes: 5 additions & 6 deletions umap2/fuzz/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
'''

import traceback
import binascii
import inspect


class StageLogger(object):

def __init__(self, filename):
Expand All @@ -22,7 +20,8 @@ def stop(self):

def log_stage(self, stage):
if self.fd:
self.fd.write(stage + '\n')

self.fd.write(stage.encode() + b'\n')
self.fd.flush()


Expand All @@ -46,8 +45,8 @@ def mutable(stage, silent=False):
def wrap_f(func):
func_self = None
if inspect.ismethod(func):
func_self = func.im_self
func = func.im_func
func_self = func.__self__
func = func.__func__

def wrapper(*args, **kwargs):
if func_self is None:
Expand Down Expand Up @@ -79,7 +78,7 @@ def wrapper(*args, **kwargs):
self.logger.error(''.join(traceback.format_stack()))
raise e
if response is not None:
info('Response: %s' % binascii.hexlify(response))
info(f'Response: {response.hex()}')
return response
return wrapper
return wrap_f
6 changes: 3 additions & 3 deletions umap2/fuzz/templates/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from kitty.model import Template, Repeat, List, Container, ForEach, OneOf
from kitty.model import ElementCount, SizeInBytes
from kitty.model import ENC_INT_LE
from hid import GenerateHidReport
from generic import Descriptor, SizedPt, DynamicInt, SubDescriptor
from . hid import GenerateHidReport
from . generic import Descriptor, SizedPt, DynamicInt, SubDescriptor


class _AC_DescriptorSubTypes: # AC Interface Descriptor Subtype
Expand Down Expand Up @@ -138,7 +138,7 @@ class _AS_DescriptorSubTypes: # AS Interface Descriptor Subtype
audio_report_descriptor = Template(
name='audio_report_descriptor',
fields=GenerateHidReport(
'050C0901A1011500250109E909EA75019502810209E209008106050B092095018142050C09009503810226FF000900750895038102090095049102C0'.decode('hex')
bytes.fromhex('050C0901A1011500250109E909EA75019502810209E209008106050B092095018142050C09009503810226FF000900750895038102090095049102C0')
)
)

Expand Down
2 changes: 1 addition & 1 deletion umap2/fuzz/templates/cdc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from kitty.model import Template, Repeat, List, Container, ForEach, OneOf
from kitty.model import ElementCount
from kitty.model import MutableField
from generic import SubDescriptor
from . generic import SubDescriptor


cdc_control_interface_descriptor = Template(
Expand Down
2 changes: 1 addition & 1 deletion umap2/fuzz/templates/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from kitty.model import ElementCount, SizeInBytes
# encoders
from kitty.model import StrEncodeEncoder, ENC_INT_LE
from generic import Descriptor, SubDescriptor
from . generic import Descriptor, SubDescriptor


# Device descriptor
Expand Down
8 changes: 4 additions & 4 deletions umap2/fuzz/templates/hid.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from kitty.model import ENC_INT_LE
from kitty.core import KittyException
from random import Random
from generic import DynamicInt, Descriptor
from . generic import DynamicInt, Descriptor


opcodes = {
Expand Down Expand Up @@ -124,7 +124,7 @@ def GenerateHidReport(report_str, name=None):
index = 0
namer = NameGen()
while index < len(report_str):
opcode = ord(report_str[index])
opcode = report_str[index]
num_args = opcode & 3
if index + num_args >= len(report_str):
raise KittyException('Not enough bytes in hid report for last opcode')
Expand All @@ -134,7 +134,7 @@ def GenerateHidReport(report_str, name=None):
fields.append(UInt8(opcode, name=cur_name))
else:
args = report_str[index:index + num_args]
value = sum(ord(args[i]) << (i * 8) for i in range(len(args))) # little endian...
value = sum(args[i] << (i * 8) for i in range(len(args))) # little endian...
fields.append(Container(
name=cur_name,
fields=[
Expand Down Expand Up @@ -179,6 +179,6 @@ def GenerateHidReport(report_str, name=None):
hid_report_descriptor = Template(
name='hid_report_descriptor',
fields=GenerateHidReport(
'05010906A101050719E029E7150025017501950881029501750881011900296515002565750895018100C0'.decode('hex')
bytes.fromhex('05010906A101050719E029E7150025017501950881029501750881011900296515002565750895018100C0')
)
)
2 changes: 1 addition & 1 deletion umap2/phy/facedancer/max342x_phy.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def service_irqs(self):
self.clear_irq_bit(Regs.endpoint_irq, PINCTL.setup_data_avail)

b = self.read_bytes(Regs.setup_data_fifo, 8)
if (irq & PINCTL.out0_data_avail) and (ord(b[0]) & 0x80 == 0x00):
if (irq & PINCTL.out0_data_avail) and (b[0] & 0x80 == 0x00):
data_bytes_len = struct.unpack('<H', b[6:])[0]
b += self.read_bytes(Regs.ep0_fifo, data_bytes_len)
self.app.signal_setup_packet_received()
Expand Down