summaryrefslogtreecommitdiff
path: root/extra/usb_updater/servo_updater.py
blob: 0f7d640c39a2a37dfd50281ad438ee3da4b82725 (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
#!/usr/bin/python
# Copyright 2016 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from __future__ import print_function

import argparse
import errno
import os
import subprocess
import time

import json
import fw_update
import ecusb.tiny_servo_common as c


def flash(brdfile, serialno, binfile):
  p = fw_update.Supdate()
  p.load_board(brdfile)
  p.connect_usb(serialname=serialno)
  p.load_file(binfile)

  # Start transfer and erase.
  p.start()
  # Upload the bin file
  print("Uploading %s" % binfile)
  p.write_file()

  # Finalize
  print("Done. Finalizing.")
  p.stop()


def select(vidpid, serialno, region, debuglog=False):
  if region not in ["rw", "ro"]:
    raise Exception("Region must be ro or rw")

  # Make sure device is up
  c.wait_for_usb(vidpid, serialname=serialno)

  # make a console
  pty = c.setup_tinyservod(vidpid, 0, serialname=serialno, debuglog=debuglog)

  cmd = "sysjump %s\nreboot" % region
  pty._issue_cmd(cmd)
  time.sleep(1)
  pty.close()


def main():
  parser = argparse.ArgumentParser(description="Image a servo micro device")
  parser.add_argument('-s', '--serialno', type=str,
      help="serial number to program", default=None)
  parser.add_argument('-b', '--board', type=str,
      help="Board configuration json file", default="servo_v4.json")
  parser.add_argument('-f', '--file', type=str,
      help="Complete ec.bin file", default="servo_v4.bin")
  parser.add_argument('-v', '--verbose', action="store_true",
      help="Chatty output")

  args = parser.parse_args()

  brdfile = args.board
  binfile = args.file
  serialno = args.serialno
  debuglog = (args.verbose is True)

  with open(brdfile) as data_file:
      data = json.load(data_file)

  vidpid = "%04x:%04x" % (int(data['vid'], 0), int(data['pid'], 0))

  select(vidpid, serialno, "ro", debuglog=debuglog)

  flash(brdfile, serialno, binfile)

  select(vidpid, serialno, "rw", debuglog=debuglog)

  flash(brdfile, serialno, binfile)

if __name__ == "__main__":
  main()