refactor: use event log utils in linux tpm tests

Simplifies event log parsing by replacing manual expect patterns with
capture_log, compare_log, and capture_and_compare_log utility functions.
Improves maintainability and consistency across TPM scripts.

Change-Id: I4e3ad47189f4ace1b447d28313902e6e357235bf
Signed-off-by: Harrison Mutai <harrison.mutai@arm.com>
diff --git a/expect/linux-tpm-384.exp b/expect/linux-tpm-384.exp
index 262cf4c..88410ed 100644
--- a/expect/linux-tpm-384.exp
+++ b/expect/linux-tpm-384.exp
@@ -13,32 +13,12 @@
 
 # File to store the event log from the ftpm service.
 set TFA_DIGEST [get_param tfa_digest "tfa_event_log"]
+set FTPM_DIGEST [get_param ftpm_digest "ftpm_event_log"]
 
 # regexp for non-zero PCR0
 set non_zero_pcr "(?!(\\s00){16})((\\s(\[0-9a-f\]){2}){16}\\s)"
 
-expect {
-        # Wait for the start of the event log dump.
-        "TCG_EfiSpecIDEvent:" {
-                set digest_log [open $TFA_DIGEST w]
-        }
-}
-
-expect {
-        # Parse the event log from the debug logs and store the digests
-        # so they can be matched later with what the fTPM reads.
-
-        -re "Digest(\\s|\\w)*:\\s(\\w{2}\\s){16}|\
-        : (\\w{2}\\s){16}|\
-        Event(\\s|\\w)*:\\s\\w+\\s" {
-                puts $digest_log $expect_out(0,string)
-                exp_continue
-        }
-
-        -exact "Booting BL31" {
-                close $digest_log
-        }
-}
+capture_log $TFA_DIGEST "Booting BL31"
 
 expect {
         "login" {
@@ -72,12 +52,4 @@
 
 expect_string "#" "finished reading PCRs"
 
-# Match the previously stored digest with the one generated by the
-# fTPM service. The pass criteria is that both digests must match,
-# meaning that TF-A successfully passed the event log to the TPM service.
-if {[catch {exec diff -s $TFA_DIGEST ftpm_event_log} result options] == 0} {
-        message "tests succeeded, digests matched"
-} else {
-        message "tests failed, digests did not match"
-        exit_uart -1
-}
+compare_log $TFA_DIGEST $FTPM_DIGEST
\ No newline at end of file
diff --git a/expect/linux-tpm.exp b/expect/linux-tpm.exp
index 8aff8ce..e5d255c 100644
--- a/expect/linux-tpm.exp
+++ b/expect/linux-tpm.exp
@@ -11,33 +11,13 @@
 
 # File to store the event log from the ftpm service.
 set TFA_DIGEST [get_param tfa_digest "tfa_event_log"]
+set FTPM_DIGEST [get_param ftpm_digest "ftpm_event_log"]
 
 # regexp for non-zero PCR0
 set non_zero_pcr "(?!(\\s00){16})((\\s(\[0-9a-f\]){2}){16}\\s)"
 set zero_pcr "(\\s00){16}\\s+(00\\s){16}"
 
-expect {
-        # Wait for the start of the event log dump.
-        "TCG_EfiSpecIDEvent:" {
-                set digest_log [open $TFA_DIGEST w]
-        }
-}
-
-expect {
-        # Parse the event log from the debug logs and store the digests
-        # so they can be matched later with what the fTPM reads.
-
-        -re "Digest(\\s|\\w)*:\\s(\\w{2}\\s){16}|\
-        : (\\w{2}\\s){16}|\
-        Event(\\s|\\w)*:\\s\\w+\\s" {
-                puts $digest_log $expect_out(0,string)
-                exp_continue
-        }
-
-        -exact "Booting BL31" {
-                close $digest_log
-        }
-}
+capture_log $TFA_DIGEST "Booting BL31"
 
 expect {
         "login" {
@@ -90,12 +70,4 @@
 
 expect_string "#" "finished reading PCRs"
 
-# Match the previously stored digest with the one generated by the
-# fTPM service. The pass criteria is that both digests must match,
-# meaning that TF-A successfully passed the event log to the TPM service.
-if {[catch {exec diff -s $TFA_DIGEST ftpm_event_log} result options] == 0} {
-        message "tests succeeded, digests matched"
-} else {
-        message "tests failed, digests did not match"
-        exit_uart -1
-}
+compare_log $TFA_DIGEST $FTPM_DIGEST
\ No newline at end of file
diff --git a/expect/tpm-logs.exp b/expect/tpm-logs.exp
index 7ed4479..e46beb6 100644
--- a/expect/tpm-logs.exp
+++ b/expect/tpm-logs.exp
@@ -11,23 +11,7 @@
 
 # File to store the event log from the ftpm service.
 set FTPM_DIGEST [get_param ftpm_digest "ftpm_event_log"]
-set event_log [open $FTPM_DIGEST w]
 
-expect -exact "Event log size"
+capture_log $FTPM_DIGEST "returned value"
 
-expect {
-        # Parse the event log from the debug logs and store the digests
-        # so they can be matched later with what TF-A stored on the event log.
-        -re "Digest(\\s|\\w)*:\\s(\\w{2}\\s){16}|\
-        : (\\w{2}\\s){16}|\
-        Event(\\s|\\w)*:\\s\\w+\\s" {
-                puts $event_log $expect_out(0,string)
-                exp_continue
-        }
-
-        -exact "returned value" {
-                close $event_log
-        }
-}
-
-source [file join [file dirname [info script]] uart-hold.inc]
+source [file join [file dirname [info script]] uart-hold.inc]
\ No newline at end of file
diff --git a/expect/utils.inc b/expect/utils.inc
index d9113d7..db3935b 100644
--- a/expect/utils.inc
+++ b/expect/utils.inc
@@ -125,10 +125,7 @@
     }
 }
 
-proc capture_and_compare_log {out end_re compare} {
-    set event_log [open $out w]
-    capture_log $out $end_re
-
+proc compare_log {out compare} {
     # Match the previously the given digests. The pass criteria is that both
     # digests must match.
     if {[catch {exec diff -s $out $compare} result options] == 0} {
@@ -138,3 +135,10 @@
             exit_uart -1
     }
 }
+
+proc capture_and_compare_log {out end_re compare} {
+    set event_log [open $out w]
+    capture_log $out $end_re
+
+    compare_log $out $compare
+}