touch_updater: Add report_tool_version to chromeos-touch-common.sh

Adds a new utility function "report_tool_version" to
"chromeos-touch-common.sh" to be used to add reports
entry about the updater that was used to initiate a touch firmware
update, or for config updaters.

BUG=b:397567795
TEST=CQ
TEST=Run \
     `cros build-packages --board=${BOARD} chromeos-base/touch_updater \
     && cros deploy ${DUT} chromeos-base/touch_updater \
     && ssh ${DUT} reboot` \
     then validated tool and version in firmware report file on DUT.

Change-Id: Ifcc76600807a94e0ac0a0437a8099df701b23d28
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/touch_updater/+/6375383
Reviewed-by: Henry Barnor <hbarnor@chromium.org>
Reviewed-by: Ray Xu <rayxu@google.com>
Commit-Queue: Henry Barnor <hbarnor@chromium.org>
Tested-by: Henry Barnor <hbarnor@chromium.org>
diff --git a/common/scripts/chromeos-touch-common.sh b/common/scripts/chromeos-touch-common.sh
index ed5a1fc..503c9df 100755
--- a/common/scripts/chromeos-touch-common.sh
+++ b/common/scripts/chromeos-touch-common.sh
@@ -472,3 +472,23 @@
   add_report_entry "${sysfs_path}" "\"flashed_config\": \"${flashed_config}\"" \
     "${extra_fields}"
 }
+
+# Adds a report entry stating the tool name being run and its version.
+# This could be executed at the beginning of a firmware updating script
+# or a config updating script.
+# Parameters:
+#   sysfs_path: a path to the device in sysfs (see add_report_entry).
+#   tool_name: the name of the tool being run (executable name).
+#   tool_version: the tool version being run.
+report_tool_version() {
+  local sysfs_path="$1"
+  local tool_name
+  local tool_version
+
+  tool_name="$(make_json_safe "$2")"
+  tool_version="$(make_json_safe "$3")"
+
+  local json_fields="\"tool\": \"${tool_name}\","
+  json_fields="${json_fields} \"version\": \"${tool_version}\""
+  add_report_entry "${sysfs_path}" "${json_fields}"
+}
diff --git a/scripts/chromeos-focal-hid-touch-firmware-update-legacy.sh b/scripts/chromeos-focal-hid-touch-firmware-update-legacy.sh
index 820e69c..b6954d8 100644
--- a/scripts/chromeos-focal-hid-touch-firmware-update-legacy.sh
+++ b/scripts/chromeos-focal-hid-touch-firmware-update-legacy.sh
@@ -110,6 +110,25 @@
   fi
 }
 
+get_tool_version() {
+  # Get updater tool version.
+  # Args:
+  #  $1: hid_pid of touch device.
+  # Outputs:
+  #  ${tool_version}: Updater tool version, e.g.: 2.4_2025-02-25
+  local tool_version
+  local pid="$1"
+
+  tool_version="$(
+    minijail0 -u fwupdate-hidraw -g fwupdate-hidraw \
+      -G -v -P /mnt/empty -b / -b /dev,,1 \
+      -t --uts -e -l -p -N -n \
+      -S /opt/google/touch/policies/focaltech.query.policy -- \
+      "${FOCALTECH_TOOL}" -v -P "${pid}"  2>&1
+  )"
+  echo -n "${tool_version}" | tr -d '\n\r '
+}
+
 main() {
 
   local active_product_id=
@@ -126,6 +145,9 @@
   active_product_id="${FLAGS_hid_pid}"
   log_msg "active_product_id: ${active_product_id}"
 
+  report_tool_version "${FLAGS_device_path}" "$(basename "${FOCALTECH_TOOL}")" \
+    "$(get_tool_version "${active_product_id}")"
+
 
   # Find the firmware that the updater is considering flashing
   # on the touch device.