fix for false PFR ProvisioningStatus in redfish

start the PFR manager service when PFR supported on the platform and
PFR-CPLD is available and PFR provisioned, Otherwilse, stop the PFR
manager service.

Tested:
1. When PFR Supported, PFR-CPLD is available and PFR provisioned.
Command: busctl tree xyz.openbmc_project.PFR.Manager
Response:
└─/xyz
  └─/xyz/openbmc_project
    ├─/xyz/openbmc_project/pfr
    └─/xyz/openbmc_project/software
      ├─/xyz/openbmc_project/software/bios_active
      ├─/xyz/openbmc_project/software/bios_recovery
      ├─/xyz/openbmc_project/software/bmc_active
      ├─/xyz/openbmc_project/software/bmc_recovery
      ├─/xyz/openbmc_project/software/cpld_active
      └─/xyz/openbmc_project/software/cpld_recovery

2. When PFR not supported or PFR-CPLD is not available and PFR not
   provisioned.
Command: busctl list
Response:
xyz.openbmc_project.ObjectMapper                   271 mapperx
xyz.openbmc_project.PCIe                           312 peci-pcie
xyz.openbmc_project.PMEM                           373 pmem_dbus_daemo
xyz.openbmc_project.PSURedundancy                  286 psuredundancy
xyz.openbmc_project.PSUSensor                      388 psusensor
xyz.openbmc_project.RestrictionMode.Manager        401 prov-mode-mgr

Command: busctl tree xyz.openbmc_project.PFR.Manager
Response:
Failed to introspect object / of service xyz.openbmc_project.PFR.Manager
: The name is not activatable
No objects discovered.

3. Tested on non-PFR platforms, no delay in BMC boot and
xyz.openbmc_project.PFR.Manager won't start.

Signed-off-by: Chalapathi <chalapathix.venkataramashetty@intel.com>
Change-Id: I08fd29f52dfe078b5b67f07a2ebb53efd38dea0c
diff --git a/CMakeLists.txt b/CMakeLists.txt
index a6c976e..a794eaa 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -10,3 +10,6 @@
 
 set(SERVICE_FILES ${PROJECT_SOURCE_DIR}/xyz.openbmc_project.PFR.Manager.service)
 install(FILES ${SERVICE_FILES} DESTINATION /lib/systemd/system/)
+
+set(SCRIPT_FILES ${PROJECT_SOURCE_DIR}/checkPfrPlatform.sh)
+install(PROGRAMS ${SCRIPT_FILES} DESTINATION /usr/bin/)
diff --git a/checkPfrPlatform.sh b/checkPfrPlatform.sh
new file mode 100644
index 0000000..5912159
--- /dev/null
+++ b/checkPfrPlatform.sh
@@ -0,0 +1,43 @@
+#!/bin/sh
+
+PFR_ID_REG=0x00
+PFR_PROV_REG=0x0a
+
+pfr_active_mode() {
+    busctl introspect xyz.openbmc_project.EntityManager $board/PFR >&/dev/null || return 1
+    address=$(busctl get-property xyz.openbmc_project.EntityManager $board/PFR xyz.openbmc_project.Configuration.PFR Address | cut -b 3-) || return 1
+    bus=$(busctl get-property xyz.openbmc_project.EntityManager $board/PFR xyz.openbmc_project.Configuration.PFR Bus | cut -b 3-) || return 1
+    #check for 0xde in register file 0
+    local id=$(i2cget -y $bus $address $PFR_ID_REG 2>/dev/null) || return 1
+    [ "$id" == "0xde" ] || return 1
+    local prov=$(i2cget -y $bus $address $PFR_PROV_REG 2>/dev/null) || return 1
+    prov=$((prov & 0x20))
+    [ "$prov" == "32" ] && return 0
+    return 1
+}
+
+#Read board name
+counter=0
+board=$(busctl call xyz.openbmc_project.ObjectMapper /xyz/openbmc_project/object_mapper xyz.openbmc_project.ObjectMapper  GetSubTreePaths sias "/xyz/openbmc_project/inventory" 0 2 "xyz.openbmc_project.Inventory.Item.Board" "xyz.openbmc_project.Inventory.Item.Chassis" | cut -b 7- | sed 's/.$//')
+
+while [ -z $board ]
+do
+   board=$(busctl call xyz.openbmc_project.ObjectMapper /xyz/openbmc_project/object_mapper xyz.openbmc_project.ObjectMapper  GetSubTreePaths sias "/xyz/openbmc_project/inventory" 0 2 "xyz.openbmc_project.Inventory.Item.Board" "xyz.openbmc_project.Inventory.Item.Chassis" | cut -b 7- | sed 's/.$//')
+   if [ $counter -eq 5 ]
+   then
+       echo "Unable to get board name"
+       exit
+   fi
+   counter=`expr $counter + 1`
+   sleep 10
+done
+
+if pfr_active_mode
+then
+    echo "Starting PFR manager service"
+    /usr/bin/intel-pfr-manager
+    exit
+else
+    echo "PFR not supported on this platforms and so not starting PFR manager service."
+    exit
+fi
diff --git a/xyz.openbmc_project.PFR.Manager.service b/xyz.openbmc_project.PFR.Manager.service
index 100fc09..f63acb6 100644
--- a/xyz.openbmc_project.PFR.Manager.service
+++ b/xyz.openbmc_project.PFR.Manager.service
@@ -1,14 +1,13 @@
 [Unit]
 Description=Daemon manages PFR dbus properties and Event logs
+After=xyz.openbmc_project.EntityManager.service
+After=xyz.openbmc_project.ObjectMapper.service
 
 [Service]
-Restart=always
-ExecStart=/usr/bin/pfr-manager
+Restart=on-failure
+ExecStart=/usr/bin/checkPfrPlatform.sh
 RestartSec=5
 StartLimitInterval=0
-SyslogIdentifier=pfr-manager
-Type=dbus
-BusName=xyz.openbmc_project.PFR.Manager
 
 [Install]
 WantedBy=multi-user.target