1#!/bin/sh
2
3mmc_init() {
4  base_dir="/media/hostfw"
5  ro_dir="${base_dir}/running-ro"
6  running_dir="${base_dir}/running"
7  prsv_dir="${base_dir}/prsv"
8
9  if [ ! -d "${ro_dir}" ]; then
10    mkdir -p "${ro_dir}"
11  fi
12  if [ ! -d "${running_dir}" ]; then
13    mkdir -p ${running_dir}
14  fi
15  if [ ! -d "${prsv_dir}" ]; then
16    mkdir -p "${prsv_dir}"
17  fi
18
19  # Mount the image that corresponds to the boot label as read-only to be used
20  # to populate the running directory.
21  boot_label="$(fw_printenv -n bootside)"
22  mount ${base_dir}/hostfw-${boot_label} ${ro_dir} -o ro
23
24  # Determine if the running dir contains the running version
25  running_label=""
26  running_label_file="${running_dir}/partlabel"
27  if [ -f "${running_label_file}" ]; then
28    running_label=$(cat ${running_label_file})
29  fi
30  if [ "${running_label}" != "${boot_label}" ]; then
31    # Copy off the preserved partitions
32    # A line in the pnor.toc looks like this:
33    # partition05=SECBOOT,0x00381000,0x003a5000,00,ECC,PRESERVED
34    rm -f ${prsv_dir}/*
35    if [ -f ${running_dir}/pnor.toc ]; then
36      prsvs=$(grep PRESERVED ${running_dir}/pnor.toc)
37      for prsv in ${prsvs}; do
38        prsv=${prsv##partition*=}
39        prsv=$(echo ${prsv} | cut -d "," -f 1)
40        cp -p ${running_dir}/${prsv} ${prsv_dir}
41      done
42    fi
43
44    # Copy contents of running image to running dir
45    rm -f ${running_dir}/*
46    cp -p ${ro_dir}/* ${running_dir}/
47
48    # Restore the preserved partitions. Ignore error, there may be none.
49    cp -p ${prsv_dir}/* ${running_dir}/ 2>/dev/null || true
50    rm -f ${prsv_dir}/*
51
52    # Save the label
53    echo "${boot_label}" > "${running_label_file}"
54
55    # Remove patches
56    rm -f ${base_dir}/patch/*
57  fi
58
59  # Mount alternate dir
60  if [ "${boot_label}" = "a" ]; then
61    alternate_label="b"
62  else
63    alternate_label="a"
64  fi
65  alternate_dir="${base_dir}/alternate"
66  if [ ! -d "${alternate_dir}" ]; then
67    mkdir -p ${alternate_dir}
68  fi
69  mount ${base_dir}/hostfw-${alternate_label} ${alternate_dir} -o ro
70}
71
72mmc_patch() {
73  # Patching is disabled if field mode is set
74  if [[ "$(fw_printenv fieldmode 2>/dev/null)" == "fieldmode=true" ]]; then
75    return 0
76  fi
77
78  target="/media/hostfw/patch"
79  if [ ! -d "${target}" ]; then
80    mkdir -p "${target}"
81  fi
82
83  patchdir="/usr/local/share"
84  if [ ! -d "${patchdir}" ]; then
85    mkdir -p "${patchdir}"
86  fi
87
88  ln -s "${target}" "${patchdir}/hostfw"
89  ln -s "${target}" "${patchdir}/pnor"
90}
91
92case "$1" in
93  mmc-init)
94    mmc_init
95    ;;
96  mmc-patch)
97    mmc_patch
98    ;;
99  *)
100    echo "Invalid argument"
101    exit 1
102    ;;
103esac
104