DiagnoseMe: Enforce Servo MAC format and check uninitialized state

Enforce 88:54:1F OUI prefix validation for Servo V4.1 MAC address in
both backend and GUI. Disable Functional Testing card if MAC or
Serial is invalid, and fail console tests if device reports an
uninitialized MAC.

BUG=b:538099956
TEST=Verified unit tests and MAC validation logic

Change-Id: I580b2ff2b589c78165bff08aebb1bc034c167f1b
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/hwtools/diagnoseme/+/8158511
Reviewed-by: Michał Headcrab Barnaś <barnas@google.com>
Tested-by: Łukasz Hajec <hajec@google.com>
Reviewed-by: 488603086791@cloudbuild.gserviceaccount.com <488603086791@cloudbuild.gserviceaccount.com>
Commit-Queue: Łukasz Hajec <hajec@google.com>
Commit-Queue: Michał Headcrab Barnaś <barnas@google.com>
diff --git a/src/server/servo_manufacturing.py b/src/server/servo_manufacturing.py
index bf66447..88a7fc4 100644
--- a/src/server/servo_manufacturing.py
+++ b/src/server/servo_manufacturing.py
@@ -66,7 +66,9 @@
     # Regex to validate mac address input.
     # Note: the mac address regex lives here since it's universal. The regex for
     # serial numbers lives inside each manager, as they change per device.
-    MACADDR_RE = re.compile("^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$")
+    MACADDR_RE = re.compile(
+        r"^88[:-]54[:-]1[Ff][:-]([0-9A-Fa-f]{2}[:-]){2}([0-9A-Fa-f]{2})$"
+    )
 
     def __init__(self, servod_service=None):
         self._servod_service = servod_service
diff --git a/src/server/tests/unit/test_servo_manufacturing.py b/src/server/tests/unit/test_servo_manufacturing.py
index e01c25e..8f67b44 100644
--- a/src/server/tests/unit/test_servo_manufacturing.py
+++ b/src/server/tests/unit/test_servo_manufacturing.py
@@ -91,7 +91,7 @@
     def test_validate_servo_mac_address_valid(self):
         """Test case for validate_servo_mac_address with valid input."""
         request = servo_manufacturing_pb2.ValidateServoMacAddressRequest(
-            mac_address="00:11:22:33:44:55"
+            mac_address="88:54:1F:00:11:22"
         )
         response = self.servicer.validate_servo_mac_address(request, self.context)
         self.assertTrue(response.is_valid)
@@ -100,7 +100,7 @@
     def test_validate_servo_mac_address_invalid(self):
         """Test case for validate_servo_mac_address with invalid input."""
         request = servo_manufacturing_pb2.ValidateServoMacAddressRequest(
-            mac_address="invalid mac"
+            mac_address="00:11:22:33:44:55"
         )
         response = self.servicer.validate_servo_mac_address(request, self.context)
         self.assertFalse(response.is_valid)
diff --git a/src/server/tests/unit/test_v4p1_tester.py b/src/server/tests/unit/test_v4p1_tester.py
index dc06635..21584f4 100644
--- a/src/server/tests/unit/test_v4p1_tester.py
+++ b/src/server/tests/unit/test_v4p1_tester.py
@@ -19,7 +19,7 @@
     def setUp(self):
         self.tester = v4p1_tester.V4P1Tester(
             serial_number="SERVOV4P1-C-2210050007",
-            mac_address="00:11:22:33:44:55",
+            mac_address="88:54:1F:00:11:22",
             servod_address="localhost:6002",
         )
         self.loop = asyncio.new_event_loop()
@@ -58,7 +58,7 @@
         # Mock responses for serialno, macaddr get, pd role, and i2cxfer
         mock_console.issue_cmd.side_effect = [
             "SERVOV4P1-C-2210050007",  # serialno
-            "00 11 22 33 44 55",  # macaddr get
+            "88 54 1F 00 11 22",  # macaddr get
             "Role: SNK",  # pd 0 state
             "20: -- 0x21 -- -- -- -- -- -- -- -- -- -- -- -- --\n"
             "40: 0x40 0x41 0x42 -- -- -- -- -- 0x48 0x49 -- -- -- -- --\n"
diff --git a/src/server/v4p1_tester.py b/src/server/v4p1_tester.py
index 7e89788..ef0d50c 100644
--- a/src/server/v4p1_tester.py
+++ b/src/server/v4p1_tester.py
@@ -91,6 +91,18 @@
         try:
             output = console.issue_cmd(command)
             self._logger.info("Console output for '%s' received:\n%s", command, output)
+            if command == "macaddr":
+                if not self._mac_address or not regex:
+                    self._logger.error(
+                        "Console test '%s' failed: MAC address is empty or unsupplied",
+                        name,
+                    )
+                    return False
+                if "uninitialized" in output.lower() or "not set" in output.lower():
+                    self._logger.error(
+                        "Console test '%s' failed: MAC address is uninitialized", name
+                    )
+                    return False
             match = re.search(regex, output, re.IGNORECASE)
             if match:
                 self._logger.info(
@@ -731,10 +743,13 @@
         # )
 
         # Ethernet MAC Prefix Test
+        expected_prefix = (
+            self._mac_address[:8] if len(self._mac_address) >= 8 else "88:54:1f"
+        )
         all_results.append(
             (
-                "Ethernet MAC Prefix (88:54:1F)",
-                await self.test_mac_address_prefix("88:54:1F"),
+                f"Ethernet MAC Prefix ({expected_prefix.upper()})",
+                await self.test_mac_address_prefix(expected_prefix),
             )
         )
 
diff --git a/src/ui/src/app/servo-v4p1-manufacturing/servo-v4p1-manufacturing.html b/src/ui/src/app/servo-v4p1-manufacturing/servo-v4p1-manufacturing.html
index b0dda64..a561ee6 100644
--- a/src/ui/src/app/servo-v4p1-manufacturing/servo-v4p1-manufacturing.html
+++ b/src/ui/src/app/servo-v4p1-manufacturing/servo-v4p1-manufacturing.html
@@ -116,12 +116,12 @@
 
 </diagnoseme-rework-card>
 
-<diagnoseme-rework-card title="5. Functional Testing" [progress]="consoleTestingInProgress || functionalTestingInProgress" [disabled]="false">
+<diagnoseme-rework-card title="5. Functional Testing" [progress]="consoleTestingInProgress || functionalTestingInProgress" [disabled]="!canStartFunctionalTesting()">
 
   <diagnoseme-rework-card-column column="1">Start functional hardware verification:</diagnoseme-rework-card-column>
   <diagnoseme-rework-card-column column="2">
     <div style="display: flex; align-items: center; gap: 8px;">
-      <button #functionalTestStartButton mat-raised-button color="primary" (click)="runFunctionalTestingMerged()" [disabled]="consoleTestingInProgress || functionalTestingInProgress">Start</button>
+      <button #functionalTestStartButton mat-raised-button color="primary" (click)="runFunctionalTestingMerged()" [disabled]="!canStartFunctionalTesting() || consoleTestingInProgress || functionalTestingInProgress">Start</button>
       <test-pass-fail-button #consoleTestingStatus></test-pass-fail-button>
       <test-pass-fail-button #functionalTestingStatus></test-pass-fail-button>
     </div>
diff --git a/src/ui/src/app/servo-v4p1-manufacturing/servo-v4p1-manufacturing.ts b/src/ui/src/app/servo-v4p1-manufacturing/servo-v4p1-manufacturing.ts
index 0570fd0..c7cc9cb 100644
--- a/src/ui/src/app/servo-v4p1-manufacturing/servo-v4p1-manufacturing.ts
+++ b/src/ui/src/app/servo-v4p1-manufacturing/servo-v4p1-manufacturing.ts
@@ -219,6 +219,10 @@
     return this.isSerialNumberValid && this.isMacAddressValid;
   }
 
+  canStartFunctionalTesting(): boolean {
+    return this.isSerialNumberValid && this.isMacAddressValid;
+  }
+
   startHostSideProgramming() {
     this.hostSideProgrammingInProgress = true;
     this.hostProgrammingResult.retryCount++;