Save CRX cache from release stateful partition.

BUG=chrome-os-partner:25069
TEST=Run make_factory_package.sh, check that resultant \
  stateful partition contains correct stateful_files.tar.xz, \
  run gooftool untar_stateful_files and make sure files are correctly untarred

Change-Id: I9c5b305762f3dc6c8547e7d8ead17a1248680478
Reviewed-on: https://chromium-review.googlesource.com/182793
Commit-Queue: Jon Salz <jsalz@chromium.org>
Tested-by: Jon Salz <jsalz@chromium.org>
Reviewed-by: Jon Salz <jsalz@chromium.org>
(cherry picked from commit d4794ab3e2935be2919bfea22471ab3cd9ea066f)
Reviewed-on: https://chromium-review.googlesource.com/183403
Reviewed-by: Bowgo Tsai <bowgotsai@chromium.org>
Reviewed-by: Hung-Te Lin <hungte@chromium.org>
diff --git a/py/gooftool/gooftool.py b/py/gooftool/gooftool.py
index 5b308dd..3886970 100755
--- a/py/gooftool/gooftool.py
+++ b/py/gooftool/gooftool.py
@@ -41,7 +41,8 @@
 from cros.factory.hwdb import hwid_tool
 from cros.factory.hwid import common
 from cros.factory.hwid import hwid_utils
-from cros.factory.test.factory import FACTORY_LOG_PATH
+from cros.factory.test import shopfloor
+from cros.factory.test.factory import FACTORY_LOG_PATH, DEVICE_STATEFUL_PATH
 from cros.factory.utils.process_utils import Spawn
 from cros.factory.privacy import FilterDict
 
@@ -639,6 +640,19 @@
   VerifyRootFs(options)
   VerifyTPM(options)
 
+@Command('untar_stateful_files')
+def UntarStatefulFiles(dummy_options):
+  """Untars stateful files from stateful_files.tar.xz on stateful partition.
+
+  If that file does not exist (which should only be R30 and earlier),
+  this is a no-op.
+  """
+  tar_file = os.path.join(DEVICE_STATEFUL_PATH, 'stateful_files.tar.xz')
+  if os.path.exists(tar_file):
+    Spawn(['tar', 'xf', tar_file], cwd=DEVICE_STATEFUL_PATH,
+          log=True, check_call=True)
+  else:
+    logging.warning('No stateful files at %s', tar_file)
 
 @Command('log_system_details')
 def LogSystemDetails(options):  # pylint: disable=W0613
@@ -763,6 +777,8 @@
 
   This routine does the following:
   - Verifies system state (see verify command)
+  - Untars stateful_files.tar.xz, if it exists, in the stateful partition, to
+    initialize files such as the CRX cache
   - Modifies firmware bitmaps to match locale
   - Clears all factory-friendly flags from the GBB
   - Removes factory-specific entries from RW_VPD (factory.*)
@@ -772,6 +788,7 @@
     next boot.
   """
   Verify(options)
+  UntarStatefulFiles(options)
   SetFirmwareBitmapLocale(options)
   ClearGBBFlags(options)
   ClearFactoryVPDEntries(options)