1#!/bin/sh
2
3bios_reset_if_needed() {
4    resetval=$(fw_printenv -n rwreset 2>/dev/nell)
5    if [ "$resetval" = "0" ] || [ "$resetval" = "1" ]; then
6        varstore="/media/hostfw/reset-gpio-val"
7        if [ -f "$varstore" ]; then
8            if [ "$resetval" != "$(cat $varstore)" ]; then
9                if busctl call org.open_power.Software.Host.Updater /xyz/openbmc_project/software xyz.openbmc_project.Common.FactoryReset Reset; then
10                    echo "$resetval" > "$varstore"
11                fi
12            fi
13        else
14            echo "$resetval" > "$varstore"
15        fi
16    fi
17}
18
19mmc_init() {
20    base_dir="/media/hostfw"
21    ro_dir="${base_dir}/running-ro"
22    running_dir="${base_dir}/running"
23    prsv_dir="${base_dir}/prsv"
24    staging_dir="${base_dir}/staging"
25    nvram_dir="${base_dir}/nvram"
26
27    if [ ! -d "${ro_dir}" ]; then
28        mkdir -p "${ro_dir}"
29    fi
30    if [ ! -d "${running_dir}" ]; then
31        mkdir -p ${running_dir}
32    fi
33    if [ ! -d "${prsv_dir}" ]; then
34        mkdir -p "${prsv_dir}"
35    fi
36    if [ ! -d "${staging_dir}" ]; then
37        mkdir -p "${staging_dir}"
38    fi
39    if [ ! -d "${nvram_dir}" ]; then
40        mkdir -p "${nvram_dir}"
41    fi
42
43    # Mount the image that corresponds to the boot label as read-only to be used
44    # to populate the running directory.
45    boot_label="$(fw_printenv -n bootside)"
46    if ! grep -q "${ro_dir}" /proc/mounts; then
47        mount ${base_dir}/hostfw-"${boot_label}" ${ro_dir} -o ro
48    fi
49
50    # Determine if the running dir contains the running version
51    running_label=""
52    running_label_file="${running_dir}/partlabel"
53    if [ -f "${running_label_file}" ]; then
54        running_label=$(cat ${running_label_file})
55    fi
56    if [ "${running_label}" != "${boot_label}" ]; then
57        # Copy off the preserved partitions
58        # A line in the pnor.toc (81e00994.lid) looks like this:
59        # partition05=SECBOOT,0x00381000,0x003a5000,00,ECC,PRESERVED
60        rm -f ${prsv_dir}/*
61        if [ -f "${ro_dir}/81e00994.lid" ]; then
62            prsvs=$(grep PRESERVED "${ro_dir}/81e00994.lid")
63            for prsv in ${prsvs}; do
64                prsv=${prsv##partition*=}
65                prsv=$(echo "${prsv}" | cut -d "," -f 1)
66                if [ -L "${running_dir}/${prsv}" ]; then
67                    # Preserve the symlink target file
68                    prsv="$(readlink "${running_dir}/${prsv}")"
69                    if [ -f "${running_dir}/${prsv}" ] && [ -f "${ro_dir}/${prsv}" ]; then
70                        runsize="$(stat -c '%s' "${running_dir}/${prsv}")"
71                        rosize="$(stat -c '%s' "${ro_dir}/${prsv}")"
72                        if [ "$runsize" != "$rosize" ]; then
73                            # Partition size may have changed or became corrupted
74                            # So it will not be copied to the preserved directory
75                            # Log PEL to indicate such
76                            busctl call xyz.openbmc_project.Logging \
77                                /xyz/openbmc_project/logging \
78                                xyz.openbmc_project.Logging.Create Create "ssa{ss}" \
79                                xyz.openbmc_project.Software.Version.Error.HostFile \
80                                xyz.openbmc_project.Logging.Entry.Level.Error 3 "FILE_NAME" \
81                                "${prsv}" "CURRENT_FILE_SIZE" "${runsize}" "EXPECTED_FILE_SIZE" \
82                                "${rosize}"
83                            # Initiate dump
84                            busctl call xyz.openbmc_project.Dump.Manager \
85                                /xyz/openbmc_project/dump/bmc xyz.openbmc_project.Dump.Create \
86                                CreateDump "a{sv}" 0
87                        else
88                            cp -p ${running_dir}/"${prsv}" ${prsv_dir}
89                        fi
90                    fi
91                fi
92            done
93        fi
94
95        # Copy lid contents of running image to running dir
96        rm -f ${running_dir}/*
97        cp -p ${ro_dir}/*.lid ${running_dir}/
98
99        # Restore the preserved partitions. Ignore error, there may be none.
100        cp -p ${prsv_dir}/* ${running_dir}/ 2>/dev/null || true
101        rm -f "${prsv_dir:?}/"*
102
103        # Clean up the staging dir in case of a failed update
104        rm -rf "${staging_dir:?}/"*
105
106        # Save the label
107        echo "${boot_label}" > "${running_label_file}"
108
109    fi
110
111    # Mount alternate dir
112    if [ "${boot_label}" = "a" ]; then
113        alternate_label="b"
114    else
115        alternate_label="a"
116    fi
117    alternate_dir="${base_dir}/alternate"
118    if [ ! -d "${alternate_dir}" ]; then
119        mkdir -p ${alternate_dir}
120    fi
121    if ! grep -q "${alternate_dir}" /proc/mounts; then
122        mount ${base_dir}/hostfw-${alternate_label} ${alternate_dir} -o ro
123    fi
124}
125
126mmc_patch() {
127    # Patching is disabled if field mode is set
128    if [ "$(fw_printenv fieldmode 2>/dev/null)" = "fieldmode=true" ]; then
129        return 0
130    fi
131
132    boot_label="$(fw_printenv -n bootside)"
133    if [ "${boot_label}" = "a" ]; then
134        alternate_label="b"
135    else
136        alternate_label="a"
137    fi
138
139    # Create patch directories
140    patch_dir="/media/hostfw/patch-"
141    running_patch_dir="${patch_dir}${boot_label}"
142    if [ ! -d "${running_patch_dir}" ]; then
143        mkdir -p "${running_patch_dir}"
144    fi
145    alternate_patch_dir="${patch_dir}${alternate_label}"
146    if [ ! -d "${alternate_patch_dir}" ]; then
147        mkdir -p "${alternate_patch_dir}"
148    fi
149
150    # Create patch symlinks
151    symlink_base="/usr/local/share"
152    if [ ! -d "${symlink_base}" ]; then
153        mkdir -p "${symlink_base}"
154    fi
155    hostfw_symlink_base="${symlink_base}/hostfw"
156    if [ ! -d "${hostfw_symlink_base}" ]; then
157        mkdir -p "${hostfw_symlink_base}"
158    fi
159
160    if [ "$(readlink -f "${symlink_base}/pnor")" != "${running_patch_dir}" ]; then
161        ln -s "${running_patch_dir}" "${symlink_base}/pnor"
162    fi
163    if [ "$(readlink -f "${hostfw_symlink_base}/running")" != "${running_patch_dir}" ]; then
164        ln -s "${running_patch_dir}" "${hostfw_symlink_base}/running"
165    fi
166    if [ "$(readlink -f "${hostfw_symlink_base}/alternate")" != "${alternate_patch_dir}" ]; then
167        ln -s "${alternate_patch_dir}" "${hostfw_symlink_base}/alternate"
168    fi
169}
170
171case "$1" in
172    bios-reset-if-needed)
173        bios_reset_if_needed
174        ;;
175    mmc-init)
176        mmc_init
177        ;;
178    mmc-patch)
179        mmc_patch
180        ;;
181    *)
182        echo "Invalid argument: $1" >&2
183        exit 1
184        ;;
185esac
186