aboutsummaryrefslogtreecommitdiff
path: root/tools/moot/mtldr
blob: e7eb58cdb49a5d5f22afe04b2b4141e52caa9f70 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#! /usr/bin/env python

"""
 Copyright 2016 Google Inc. All Rights Reserved.
 Author: gkalsi@google.com (Gurjant Kalsi)

 Permission is hereby granted, free of charge, to any person obtaining
 a copy of this software and associated documentation files
 (the "Software"), to deal in the Software without restriction,
 including without limitation the rights to use, copy, modify, merge,
 publish, distribute, sublicense, and/or sell copies of the Software,
 and to permit persons to whom the Software is furnished to do so,
 subject to the following conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import argparse
import binascii
import logging
import struct
import time

import usb.core
import usb.util


class Command:
    flash   = 0x01
    boot    = 0x02
    devinfo = 0x03


class DataPhaseType:
    none           = 0
    host_to_device = 1
    device_to_host = 2


class Retcode:
    # Normal operation
    no_error            = (0x00)
    xmit_ready          = (0x01)
    recv_ready          = (0x02)

    # Malformed reqeust
    bad_data_len        = (0xAAA0)
    bad_magic           = (0xAAA1)
    unknown_command     = (0xAAA2)

    # Device side system error.
    sys_image_too_big   = (0xFFF1)
    err_open_sys_flash  = (0xFFF2)
    err_erase_sys_flash = (0xFFF3)
    err_write_sys_flash = (0xFFF4)
    cant_find_buildsig  = (0xFFF5)
    usb_read_error      = (0xFFF6)


class CommandParam:
    def __init__(self, data_phase_type):
        self.data_phase_type = data_phase_type

VENDOR_ID = 0x18D1
PRODUCT_ID = 0xA010
CLASS_VENDOR_SPECIFIC = 0xFF
SUBCLASS_MTLDR_DEBUG = 0x01

# create logger
logger = logging.getLogger('mtldr')
logger.setLevel(logging.INFO)


class FindByDeviceClass(object):
    # Callable object that selects a USB device by a Sub/Device class pair

    def __init__(self, device, subdevice):
        self._device_class = device
        self._subdevice_class = subdevice

    def __call__(self, device):
        for cfg in device:
            intf = usb.util.find_descriptor(
                cfg, bInterfaceClass=self._device_class,
                bInterfaceSubClass=self._subdevice_class)
            if intf:
                return True
        return False


class CommandDispatcher:
    header_struct = struct.Struct("< i i i")
    header_struct_len = 12  # bytes

    response_struct = struct.Struct("< i i i")
    response_struct_len = 12  # bytes

    cmd_magic = 0x4d4f4f54
    resp_magic = 0x52455350

    data_phase_timeout = 10000  # ms

    CommandParams = {
        Command.flash: CommandParam(DataPhaseType.host_to_device),
        Command.boot: CommandParam(DataPhaseType.none),
        Command.devinfo: CommandParam(DataPhaseType.device_to_host),
    }

    def __init__(self, ep_in, ep_out):
        self._ep_in = ep_in
        self._ep_out = ep_out

    def __read_response(self, timeout=None):
        """
        reads a standard response from the USB device.
        """
        if timeout:
            resp = self._ep_in.read(CommandDispatcher.response_struct_len, timeout=timeout).tostring()
        else:
            resp = self._ep_in.read(CommandDispatcher.response_struct_len).tostring()

        logger.debug(
            ("Read %d bytes: " % CommandDispatcher.response_struct_len) +
            str(binascii.hexlify(resp))
        )

        resp = CommandDispatcher.response_struct.unpack(resp)
        if resp[0] != CommandDispatcher.resp_magic:
            raise("Device responded with an unexpected magic value.")

        logger.debug(
            ("Read Response - retcode = %d, nbytes = %d" % (resp[1], resp[2]))
        )

        return (resp[1], int(resp[2]))

    def __dispatch_no_data(self, command):
        """
        Dispatches a command that has no data phase.
        """
        logger.debug("Write %d bytes, command = %d" % (0, command))
        command = CommandDispatcher.header_struct.pack(CommandDispatcher.cmd_magic, command, 0)
        self._ep_out.write(command)

        retcode, datalen = self.__read_response()
        assert datalen == 0  # A command with no data can't have a datalen

        return (retcode, list())

    def __dispatch_device_to_host(self, command):
        """
        Dispatches a command that has a device to host data phase.
        """
        logger.debug("Write %d bytes, command = %d" % (0, command))
        command = CommandDispatcher.header_struct.pack(CommandDispatcher.cmd_magic, command, 0)
        self._ep_out.write(command)

        retcode, datalen = self.__read_response()
        if retcode != Retcode.xmit_ready:
            return (retcode, list())

        logger.debug("Read %d bytes, retcode = %d" % (int(datalen), retcode))
        resp = self._ep_in.read(int(datalen), timeout=CommandDispatcher.data_phase_timeout)

        retcode, datalen = self.__read_response()

        return (retcode, resp)

    def __dispatch_host_to_device(self, command, data):
        """
        Dispatches a command that has a host to device data phase.
        """
        logger.debug("Write %d bytes, command = %d" % (len(data), command))
        # Tell the device that we're about to send it data. Also mention how
        # much data we're about to send.
        command = CommandDispatcher.header_struct.pack(CommandDispatcher.cmd_magic, command, len(data))
        self._ep_out.write(command)

        # The device will signal back to us that it's ready to read data.
        retcode, datalen = self.__read_response(CommandDispatcher.data_phase_timeout)
        assert datalen == 0

        if retcode != Retcode.recv_ready:
            # The device experienced an error and is not ready to receive data.
            return (retcode, list())

        # Write the data back to the device.
        self._ep_out.write(data, timeout=CommandDispatcher.data_phase_timeout)

        # The device will reply to us to let us know whether or not it received
        # our data correctly.
        retcode, datalen = self.__read_response()
        return (retcode, list())

    def dispatch(self, command, data=None):
        """
        Dispatches a command to the connected USB device. 

        A command is composed of a command phase followed by an optional data
        phase. If the data parameter is specified, dispatch(...) will also 
        attempt to send data to the device.

        Returns a 2-Tuple as follows: (command result, optional data). If the 
        device returned data during the data phase, optional data will contain 
        that data, otherwise it will be None.
        """
        # Make sure the command actually exists.
        params = CommandDispatcher.CommandParams.get(command)
        if not params:
            raise("Command " + str(command) + " does not exist.")

        if params.data_phase_type == DataPhaseType.none:
            result = self.__dispatch_no_data(command)
        elif params.data_phase_type == DataPhaseType.host_to_device:
            result = self.__dispatch_host_to_device(command, data)
        elif params.data_phase_type == DataPhaseType.device_to_host:
            result = self.__dispatch_device_to_host(command)

        return result


def cmd_devinfo(dispatcher, args):
    retcode, data = dispatcher.dispatch(Command.devinfo)

    if retcode != Retcode.no_error:
        print ("Error %d while reading devinfo" % retcode)
    else:
        print data.tostring()


def cmd_flash(dispatcher, args):
    with open(args.bin, 'rb') as file:
        binary = file.read()

    retcode, data = dispatcher.dispatch(Command.flash, binary)
    if retcode != Retcode.no_error:
        print ("Error %d while flashing device" % retcode)



def cmd_boot(dispatcher, args):
    retcode, data = dispatcher.dispatch(Command.boot)
    if retcode != Retcode.no_error:
        print ("Error %d while booting device" % retcode)


def main():
    # Setup the Logger
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    formatter = logging.Formatter(
        '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    ch.setFormatter(formatter)
    logger.addHandler(ch)

    # Setup the argument parser
    parser = argparse.ArgumentParser(prog='PROG')
    subparsers = parser.add_subparsers(help='sub-command help')

    devinfo_parser = subparsers.add_parser('devinfo', help='a help')
    devinfo_parser.set_defaults(func=cmd_devinfo)

    flash_parser = subparsers.add_parser('flash', help='b help')
    flash_parser.add_argument('bin', help="Path to the LK Binary to flash")
    flash_parser.set_defaults(func=cmd_flash)

    boot_parser = subparsers.add_parser('boot', help='b help')
    boot_parser.set_defaults(func=cmd_boot)

    args = parser.parse_args()

    logger.info("Waiting for device (vid=0x%04x, pid=0x%04x, "
                "class=0x%02x, subclass=0x%02x)" %
                (VENDOR_ID, PRODUCT_ID, CLASS_VENDOR_SPECIFIC,
                 SUBCLASS_MTLDR_DEBUG))
    while True:
        dev = usb.core.find(
            idVendor=VENDOR_ID,
            idProduct=PRODUCT_ID,
            custom_match=FindByDeviceClass(CLASS_VENDOR_SPECIFIC,
                                           SUBCLASS_MTLDR_DEBUG))
        if dev:
            break
        time.sleep(0.5)

    logger.info("Found USB Device!")
    dev.set_configuration()

    cfg = dev.get_active_configuration()
    intf = usb.util.find_descriptor(
        cfg, bInterfaceClass=CLASS_VENDOR_SPECIFIC,
        bInterfaceSubClass=SUBCLASS_MTLDR_DEBUG)

    ep_out = usb.util.find_descriptor(
        intf,
        custom_match=lambda e:
        usb.util.endpoint_direction(e.bEndpointAddress) ==
        usb.util.ENDPOINT_OUT)
    ep_in = usb.util.find_descriptor(
        intf,
        custom_match=lambda e:
        usb.util.endpoint_direction(e.bEndpointAddress) ==
        usb.util.ENDPOINT_IN)

    dispatcher = CommandDispatcher(ep_in, ep_out)
    args.func(dispatcher, args)


if __name__ == "__main__":
    main()