Revert "Revert "gbmc-bridge: Add support for RWFS purging""

This reverts commit 38f9c458026480446a5c0b6c95d759ab24a43515.

Reason for revert: will submit with https://gbmc-internal-review.git.corp.google.com/c/meta-vendor-private/+/110569

Change-Id: I8f4c6dba64bfe4f37fb63a6677f3557a0ade8dca
diff --git a/recipes-google/networking/gbmc-bridge.bbappend b/recipes-google/networking/gbmc-bridge.bbappend
new file mode 100644
index 0000000..7cf4f47
--- /dev/null
+++ b/recipes-google/networking/gbmc-bridge.bbappend
@@ -0,0 +1,9 @@
+FILESEXTRAPATHS:prepend:gbmc := "${THISDIR}/${PN}:"
+SRC_URI:append:gbmc = " \
+  file://49-purge-rwfs.sh \
+  file://51-clear-purge-rwfs.sh \
+  "
+do_install:append:gbmc() {
+  install -m0644 ${WORKDIR}/49-purge-rwfs.sh ${D}${datadir}/gbmc-br-dhcp/
+  install -m0644 ${WORKDIR}/51-clear-purge-rwfs.sh ${D}${datadir}/gbmc-br-dhcp/
+}
diff --git a/recipes-google/networking/gbmc-bridge/49-purge-rwfs.sh b/recipes-google/networking/gbmc-bridge/49-purge-rwfs.sh
new file mode 100644
index 0000000..247f9ed
--- /dev/null
+++ b/recipes-google/networking/gbmc-bridge/49-purge-rwfs.sh
@@ -0,0 +1,34 @@
+#!/bin/bash
+# Copyright 2022 Google LLC
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+[ -z "${gbmc_purge_rwfs-}" ] || return
+
+gbmc_purge_rwfs_hook() {
+  # Only execute this hook if we aren't already purged
+  if [ -e '/run/initramfs/rwfs-purged' ]; then
+    echo 'RWFS already purged' >&2
+    return 0
+  fi
+
+  echo 'RWFS Purge is required' >&2
+  touch /var/google/do-rwfs-purge
+
+  echo 'Deferring powercycle' >&2
+  gbmc_psu_hardreset_needed=1
+}
+
+GBMC_BR_DHCP_HOOKS+=(gbmc_purge_rwfs_hook)
+
+gbmc_purge_rwfs=1
diff --git a/recipes-google/networking/gbmc-bridge/51-clear-purge-rwfs.sh b/recipes-google/networking/gbmc-bridge/51-clear-purge-rwfs.sh
new file mode 100644
index 0000000..198af89
--- /dev/null
+++ b/recipes-google/networking/gbmc-bridge/51-clear-purge-rwfs.sh
@@ -0,0 +1,24 @@
+#!/bin/bash
+# Copyright 2022 Google LLC
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+[ -z "${gbmc_clear_purge_rwfs-}" ] || return
+
+gbmc_clear_purge_rwfs_hook() {
+  rm -f /run/initramfs/rwfs-purged
+}
+
+GBMC_BR_DHCP_HOOKS+=(gbmc_clear_purge_rwfs_hook)
+
+gbmc_clear_purge_rwfs=1
diff --git a/recipes-phosphor/initrdscripts/obmc-phosphor-initfs.bbappend b/recipes-phosphor/initrdscripts/obmc-phosphor-initfs.bbappend
new file mode 100644
index 0000000..a2d368e
--- /dev/null
+++ b/recipes-phosphor/initrdscripts/obmc-phosphor-initfs.bbappend
@@ -0,0 +1,2 @@
+FILESEXTRAPATHS:prepend:gbmc := "${THISDIR}/${PN}:"
+SRC_URI:append:gbmc = " file://0001-obmc-phosphor-initfs-Add-support-for-purging-RWFS.patch"
diff --git a/recipes-phosphor/initrdscripts/obmc-phosphor-initfs/0001-obmc-phosphor-initfs-Add-support-for-purging-RWFS.patch b/recipes-phosphor/initrdscripts/obmc-phosphor-initfs/0001-obmc-phosphor-initfs-Add-support-for-purging-RWFS.patch
new file mode 100644
index 0000000..91a04fa
--- /dev/null
+++ b/recipes-phosphor/initrdscripts/obmc-phosphor-initfs/0001-obmc-phosphor-initfs-Add-support-for-purging-RWFS.patch
@@ -0,0 +1,31 @@
+From fba0afe53006051d2bfa33501e3e73bd7ad8e592 Mon Sep 17 00:00:00 2001
+From: "William A. Kennington III" <wak@google.com>
+Date: Thu, 16 Jun 2022 14:27:57 -0700
+Subject: [PATCH] obmc-phosphor-initfs: Add support for purging RWFS
+
+This adds a flag to the RWFS that makes it possible to purge on the next
+bootup.
+
+Signed-off-by: William A. Kennington III <wak@google.com>
+---
+ obmc-init.sh | 5 +++++
+ 1 file changed, 5 insertions(+)
+
+diff --git a/obmc-init.sh b/obmc-init.sh
+index e61ede9..43eb8e7 100644
+--- a/obmc-init.sh
++++ b/obmc-init.sh
+@@ -413,5 +413,10 @@ HERE
+ 
+ rm -rf $work
++if [ -e $upper/var/google/do-rwfs-purge ]; then
++	echo 'Purging RWFS'
++	rm -rf $upper
++	touch run/initramfs/rwfs-purged
++fi
+ mkdir -p $upper $work
+ 
+ mount -t overlay -o lowerdir=$rodir,upperdir=$upper,workdir=$work cow /root
+-- 
+2.37.0.rc0.104.g0611611a94-goog
+