cros_bundle_firmware: Support writing images to Dediprog EM100

The Dediprog EM100 is a useful device for emulating SPI flash. We can
write to it over USB using the em100 tool.

BUG=chromium-os:31624
TEST=manual
$ cros_bundle_firmware -b link -w em100 -u u-boot.bin
See that the board boots the new image

Change-Id: Icc56f2ca7f1744a495d481ee6e36c1734205b25f
Reviewed-on: https://gerrit.chromium.org/gerrit/24768
Reviewed-by: Randall Spangler <rspangler@chromium.org>
Commit-Ready: Simon Glass <sjg@chromium.org>
Tested-by: Simon Glass <sjg@chromium.org>
diff --git a/host/lib/write_firmware.py b/host/lib/write_firmware.py
index 1d802d9..7b8f831 100644
--- a/host/lib/write_firmware.py
+++ b/host/lib/write_firmware.py
@@ -574,6 +574,31 @@
       for disk in disks:
         self._out.UserOutput('  %s' % disk[4])
 
+  def _Em100FlashImage(self, image_fname):
+    """Send an image to an attached EM100 device.
+
+    This is a Dediprog EM100 SPI flash emulation device. We set up servo2
+    to do the SPI emulation, then write the image, then boot the board.
+    All going well, this is enough to get U-Boot running.
+
+    Args:
+      image_fname: Filename of image to send
+    """
+    args = ['spi2_vref:off', 'spi2_buf_en:off', 'spi2_buf_on_flex_en:off']
+    args.append('spi_hold:on')
+    self._tools.Run('dut-control', args)
+
+    # TODO(sjg@chromium.org): This is for link. We could make this
+    # configurable from the fdt.
+    args = ['-c', 'W25Q64CV', '-d', self._tools.Filename(image_fname), '-r']
+    self._out.Progress('Writing image to em100')
+    self._tools.Run('em100', args, sudo=True)
+
+    self._out.Progress('Resetting board')
+    args = ['cold_reset:on', 'sleep:.2', 'cold_reset:off', 'sleep:.5']
+    args.extend(['pwr_button:press', 'sleep:.2', 'pwr_button:release'])
+    self._tools.Run('dut-control', args)
+
 
 def DoWriteFirmware(output, tools, fdt, flasher, file_list, image_fname,
                     bundle, update=True, verify=False, dest=None,
@@ -623,6 +648,10 @@
           'complete')
     else:
       raise CmdError('Image upload failed - please check board connection')
+  elif dest == 'em100':
+    # crosbug.com/31625
+    tools.CheckTool('em100')
+    write._Em100FlashImage(image_fname)
   elif dest.startswith('sd'):
     write.SendToSdCard(dest[2:], flash_dest, flasher, image_fname)
   else: