Fix validate_target_files for target files modified by

add_img_to_target_files.py

See bug description:
"When resigning images, the validate_target_files.py can be used to
verify the images using avbtool. The script will use the vbmeta.img to
achieve this, and all relevant images need to be in the IMAGES folder.
However, due to changes on add_img_to_target_files.py and specifically
the commit 5277d1015, some images (e.g. acpio.img and tos.img) are no
longer copied from RADIO to the IMAGES folder. This causes an error on
validate_target_files.py indicating that it cannot find image such as
IMAGES/acpio.img."
This CL fixes this by symlink images under RADIO directory to IMAGES
directory before invoking avbtool.

Bug: 159299583
Test: python3 -m unittest test_validate_target_files
Change-Id: I5769ee2ab5230d2a3a7cef10706dcc5788e654f8
Merged-In: I5769ee2ab5230d2a3a7cef10706dcc5788e654f8
diff --git a/tools/releasetools/validate_target_files.py b/tools/releasetools/validate_target_files.py
index ac469eb..c260ace 100755
--- a/tools/releasetools/validate_target_files.py
+++ b/tools/releasetools/validate_target_files.py
@@ -236,6 +236,15 @@
 
   logging.info('Done checking %s', script_path)
 
+# Symlink files in `src` to `dst`, if the files do not
+# already exists in `dst` directory.
+def symlinkIfNotExists(src, dst):
+  if not os.path.isdir(src):
+    return
+  for filename in os.listdir(src):
+    if os.path.exists(os.path.join(dst, filename)):
+      continue
+    os.symlink(os.path.join(src, filename), os.path.join(dst, filename))
 
 def ValidateVerifiedBootImages(input_tmp, info_dict, options):
   """Validates the Verified Boot related images.
@@ -257,6 +266,12 @@
   Raises:
     AssertionError: On any verification failure.
   """
+  # See bug 159299583
+  # After commit 5277d1015, some images (e.g. acpio.img and tos.img) are no
+  # longer copied from RADIO to the IMAGES folder. But avbtool assumes that
+  # images are in IMAGES folder. So we symlink them.
+  symlinkIfNotExists(os.path.join(input_tmp, "RADIO"),
+                    os.path.join(input_tmp, "IMAGES"))
   # Verified boot 1.0 (images signed with boot_signer and verity_signer).
   if info_dict.get('boot_signer') == 'true':
     logging.info('Verifying Verified Boot images...')