Preserved Partition Change

This commit fixes the problem of corrupted or changed preserved
partitions. If one of the partitions becomes corrupted, or it
the size of the partition needs to be changes, these changes will
allow for the copying of the old partition, forcing the new one
to take its place.

Tested:
When a PRESERVED partition is altered (corrupted or contents
deleted) ensured that the contents was not copied into the
prsv directory and the working partition appeared upon
reboot.

Signed-off-by: Zami Seck <zimzam17@gmail.com>
Change-Id: I473bdef12dd14913c385ccd43693ad7176567313
diff --git a/mmc/obmc-flash-bios b/mmc/obmc-flash-bios
index 7b1f79b..88ad6d1 100644
--- a/mmc/obmc-flash-bios
+++ b/mmc/obmc-flash-bios
@@ -66,7 +66,26 @@
         if [ -L "${running_dir}/${prsv}" ]; then
           # Preserve the symlink target file
           prsv="$(readlink "${running_dir}/${prsv}")"
-          cp -p ${running_dir}/"${prsv}" ${prsv_dir}
+          runsize="$(stat -c '%s' "${running_dir}/${prsv}")"
+          rosize="$(stat -c '%s' "${ro_dir}/${prsv}")"
+          if [ "$runsize" != "$rosize" ]; then
+              # Partition size may have changed or became corrupted
+              # So it will not be copied to the preserved directory
+              # Log PEL to indicate such
+              busctl call xyz.openbmc_project.Logging \
+              /xyz/openbmc_project/logging \
+              xyz.openbmc_project.Logging.Create Create "ssa{ss}" \
+              xyz.openbmc_project.Software.Version.Error.HostFile \
+              xyz.openbmc_project.Logging.Entry.Level.Error 3 "FILE_NAME" \
+              "${prsv}" "CURRENT_FILE_SIZE" "${runsize}" "EXPECTED_FILE_SIZE" \
+              "${rosize}"
+              # Initiate dump
+              busctl call xyz.openbmc_project.Dump.Manager \
+              /xyz/openbmc_project/dump/bmc xyz.openbmc_project.Dump.Create \
+              CreateDump "a{sv}" 0
+          else
+              cp -p ${running_dir}/"${prsv}" ${prsv_dir}
+          fi
         fi
       done
     fi