cros_bundle_firmware: Move firmware writing into write_firmware.py

This function should not be in cros_bundle_firmware - we move it into the
correct place so that it can be used with cros_write_firmware also.

BUG=chromium-os:17298
TEST=run cros_bundle_firmware -w and see that it still works

Change-Id: If1ab0c7ad1bc9f7bc424ba072c574a8ca8df1356
Reviewed-on: http://gerrit.chromium.org/gerrit/5591
Reviewed-by: Che-Liang Chiou <clchiou@chromium.org>
Tested-by: Simon Glass <sjg@chromium.org>
diff --git a/host/cros_bundle_firmware b/host/cros_bundle_firmware
index 52e12fa..cce398d 100755
--- a/host/cros_bundle_firmware
+++ b/host/cros_bundle_firmware
@@ -37,7 +37,7 @@
 
 from fdt import Fdt
 from pack_firmware import PackFirmware
-from write_firmware import WriteFirmware
+import write_firmware
 from bundle_firmware import Bundle
 import cros_output
 from tools import Tools
@@ -135,15 +135,12 @@
 
     # Write it to the board if required.
     if options.write:
-      write = WriteFirmware(tools, bundle.fdt, output)
       flasher = options.uboot_flasher
       if not flasher:
         flasher = bundle.uboot_fname
-      if write.FlashImage(flasher, bundle.bct_fname, out_fname):
-        output.Progress('Image uploaded - please wait for flashing to '
-            'complete')
-      else:
-        raise CmdError('Image upload failed - please check board connection')
+      write_firmware.DoWriteFirmware(output, tools, bundle.fdt, flasher,
+          bundle.bct_fname, out_fname)
+
   except (CmdError, ValueError) as err:
     # For verbosity 4 we want to display all possible information
     if options.verbosity >= 4:
diff --git a/host/lib/write_firmware.py b/host/lib/write_firmware.py
index 00cd17f..0478b26 100644
--- a/host/lib/write_firmware.py
+++ b/host/lib/write_firmware.py
@@ -205,3 +205,24 @@
         time.sleep(1)
 
     return False
+
+def DoWriteFirmware(output, tools, fdt, flasher, bct_fname, image_fname):
+  """A simple function to write firmware to the board.
+
+  This creates a WriteFirmware object and uses it to write the firmware image
+  to the board.
+
+  Args:
+    output: cros_output object to use.
+    tools: Tools object to use.
+    fdt: Fdt object to use as our device tree.
+    flasher: U-Boot binary to use as the flasher.
+    bct_fname: Bct file to use for the flasher.
+    image_fname: Filename of image to write.
+  """
+  write = WriteFirmware(tools, fdt, output)
+  if write.FlashImage(flasher, bct_fname, image_fname):
+    output.Progress('Image uploaded - please wait for flashing to '
+        'complete')
+  else:
+    raise CmdError('Image upload failed - please check board connection')