crosutils: add HWID folder when building factory test image

To always have HWID bundles in build output, this CL copies the HWID bundle
files from board temporary space into build output folder.

The HWID folder in make_netboot is also removed.

BUG=chrome-os-partner:5796
TEST=./build_image --factory
     # see "hwid" folder in build output

Change-Id: I8c2cd32c257b117261fba3654b52929c71310c82
Reviewed-on: http://gerrit.chromium.org/gerrit/7331
Reviewed-by: Hung-Te Lin <hungte@chromium.org>
Tested-by: Hung-Te Lin <hungte@chromium.org>
diff --git a/build_library/test_image_util.sh b/build_library/test_image_util.sh
index 9542f5a..35a99a1 100755
--- a/build_library/test_image_util.sh
+++ b/build_library/test_image_util.sh
@@ -63,6 +63,22 @@
   emerge_to_image --root="${root_dev_dir}" chromeos-test
 }
 
+prepare_hwid_for_factory() {
+  local hwid_dest="$1/hwid"
+  local hwid_src="${BOARD_ROOT}/usr/share/chromeos-hwid"
+
+  # Force refreshing source folder in build root folder
+  sudo rm -rf "${hwid_src}" "${hwid_dest}"
+  emerge_to_image chromeos-hwid
+  if [ -d "${hwid_src}" ]; then
+    # TODO(hungte) After being archived by chromite, the HWID files will be in
+    # factory_test/hwid; we should move it to top level folder.
+    cp -r "${hwid_src}" "${hwid_dest}"
+  else
+    echo "Skipping HWID: No HWID bundles found."
+  fi
+}
+
 install_autotest_for_factory() {
   local autotest_src="${BOARD_ROOT}/usr/local/autotest"
   local stateful_root="${ROOT_FS_DIR}/usr/local"
@@ -121,6 +137,7 @@
   if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ]; then
     emerge_to_image --root="${ROOT_FS_DIR}" factorytest-init
 
+    prepare_hwid_for_factory "${image_dir}"
     install_autotest_for_factory
 
     local mod_factory_script
diff --git a/make_netboot.sh b/make_netboot.sh
index b457bcd..2a0cbf6 100755
--- a/make_netboot.sh
+++ b/make_netboot.sh
@@ -85,23 +85,6 @@
     echo "Skipping legacy fw: ${SYSROOT}/u-boot/legacy_image.bin not present?"
 fi
 
-# Get HWID bundle if available.
-# TODO(hungte) Move this to chromite because HWID bundles should be archived for
-# every factory archive instead of builds with netboot.
-hwid_dir="usr/share/chromeos-hwid"
-sudo rm -rf hwid
-mkdir -p hwid
-if hwid_files=$(ls "${SYSROOT}/${hwid_dir}/" | grep ^hwid_bundle); then
-    echo "Copying HWID bundles: $hwid_files"
-    for file in $hwid_files; do
-        cp "${SYSROOT}/${hwid_dir}/${file}" "hwid/"
-    done
-else
-    echo "Skipping HWID: ${SYSROOT}/${hwid_dir}/" \
-         "does not contain HWID bundle"
-fi
-
-
 # Prepare to mount rootfs.
 umount_loop() {
   sudo umount r || true