grpc-blobs: add bios-settings-util helper

Add this helper script for bios-settings msvfud package so it doesn't
need to know where the bios settings file is stored.

Tested:
https://paste.googleplex.com/5940478543134720

Fusion-Link: N/A, the script is not used in existing presubmit platforms
Google-Bug-Id: 419102393
Change-Id: I3c9962dfa2cd714b792001cff7749348d4f2497a
Signed-off-by: Jinliang Wang <jinliangw@google.com>
diff --git a/recipes-phosphor/grpc-blobs/files/bios-settings-util b/recipes-phosphor/grpc-blobs/files/bios-settings-util
new file mode 100644
index 0000000..0e1ed7b
--- /dev/null
+++ b/recipes-phosphor/grpc-blobs/files/bios-settings-util
@@ -0,0 +1,96 @@
+#!/bin/bash
+
+readonly CONFIGURED_SETTINGS_FILE="/var/google/bios/configured-settings.txt"
+readonly REPORTED_SETTINGS_FILE="/var/google/bios/reported-settings.txt"
+
+# 10KB limit
+readonly FILE_SIZE_LIMIT=10240
+
+echo_err() {
+    echo 1>&2 "ERROR: $*"
+}
+
+show_usage() {
+    cat <<HERE
+Usage:
+    $0 getConfiguredSettings                 Read configured bios settings file from persist storage and return the file content through stdout
+    $0 storeConfiguredSettings settingsFile  Store the bios settings file into persist storage
+    $0 getReportedSettings                   Read reported bios settings file from persist storage and return the file content through stdout
+HERE
+}
+
+get_configured_settings() {
+    cat "$CONFIGURED_SETTINGS_FILE"
+    exit $?
+}
+
+get_reported_settings() {
+    cat "$REPORTED_SETTINGS_FILE"
+    exit $?
+}
+
+store_configured_settings() {
+    local input_file="$1"
+
+    if [[ ! -f "$input_file" ]]; then
+        echo_err "Input bios settings file $input_file doesn't exist."
+        exit 1
+    fi
+
+    local file_size
+    file_size="$(stat -c%s "$input_file")"
+    if ((file_size > FILE_SIZE_LIMIT)); then
+        echo_err "file size $file_size exceeds $FILE_SIZE_LIMIT."
+        exit 1
+    fi
+
+    # We need /proc/1/root prefix to persist write in msvfud post install environment
+    local dest_file="/proc/1/root$CONFIGURED_SETTINGS_FILE"
+
+    local parent_dir
+    parent_dir="$(dirname $dest_file)"
+    mkdir -p "$parent_dir" || {
+        echo_err "Failed to mkdir $parent_dir"
+        exit 1
+    }
+
+    if cmp -s "$input_file" "$dest_file"; then
+        # Skip write if $input_file is the same as $dest_file
+        exit 0
+    fi
+
+    cp "$input_file" "${dest_file}.tmp" || {
+        echo_err "Failed to cp $input_file to ${dest_file}.tmp"
+        exit 1
+    }
+
+    # the replacement is atomic
+    mv "${dest_file}.tmp" "$dest_file" || {
+        echo_err "Failed to mv $input_file to ${dest_file}.tmp"
+        exit 1
+    }
+
+    exit 0
+}
+
+if [[ $# -eq 0 ]]; then
+    show_usage
+    exit 0
+fi
+
+if [[ $# -eq 1 ]]; then
+    if [[ "$1" == "getConfiguredSettings" ]]; then
+        get_configured_settings
+    elif [[ "$1" == "getReportedSettings" ]]; then
+        get_reported_settings
+    fi
+fi
+
+if [[ $# -eq 2 ]]; then
+    if [[ "$1" == "storeConfiguredSettings" ]]; then
+        store_configured_settings "$2"
+    fi
+fi
+
+show_usage
+exit 1
diff --git a/recipes-phosphor/grpc-blobs/grpc-blobs_git.bb b/recipes-phosphor/grpc-blobs/grpc-blobs_git.bb
index 07b428f..8bf52b2 100644
--- a/recipes-phosphor/grpc-blobs/grpc-blobs_git.bb
+++ b/recipes-phosphor/grpc-blobs/grpc-blobs_git.bb
@@ -20,3 +20,17 @@
 SYSTEMD_SERVICE:${PN} += "grpc-blobs.service"
 
 inherit meson pkgconfig systemd
+
+FILESEXTRAPATHS:prepend := "${THISDIR}/files:"
+
+SRC_URI:append = " \
+  file://bios-settings-util \
+"
+
+RDEPENDS:${PN} += " \
+    bash \
+"
+do_install:append() {
+    install -d ${D}${bindir}
+    install -m 0755 ${WORKDIR}/bios-settings-util ${D}${bindir}/
+}