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  fi
56
57  # Mount alternate dir
58  if [ "${boot_label}" = "a" ]; then
59    alternate_label="b"
60  else
61    alternate_label="a"
62  fi
63  alternate_dir="${base_dir}/alternate"
64  if [ ! -d "${alternate_dir}" ]; then
65    mkdir -p ${alternate_dir}
66  fi
67  mount ${base_dir}/hostfw-${alternate_label} ${alternate_dir} -o ro
68}
69
70mmc_patch() {
71  # Patching is disabled if field mode is set
72  if [[ "$(fw_printenv fieldmode 2>/dev/null)" == "fieldmode=true" ]]; then
73    return 0
74  fi
75
76  boot_label="$(fw_printenv -n bootside)"
77  if [ "${boot_label}" = "a" ]; then
78    alternate_label="b"
79  else
80    alternate_label="a"
81  fi
82
83  # Create patch directories
84  patch_dir="/media/hostfw/patch-"
85  running_patch_dir="${patch_dir}${boot_label}"
86  if [ ! -d "${running_patch_dir}" ]; then
87    mkdir -p "${running_patch_dir}"
88  fi
89  alternate_patch_dir="${patch_dir}${alternate_label}"
90  if [ ! -d "${alternate_patch_dir}" ]; then
91    mkdir -p "${alternate_patch_dir}"
92  fi
93
94  # Create patch symlinks
95  symlink_base="/usr/local/share"
96  if [ ! -d "${symlink_base}" ]; then
97    mkdir -p "${symlink_base}"
98  fi
99  hostfw_symlink_base="${symlink_base}/hostfw"
100  if [ ! -d "${hostfw_symlink_base}" ]; then
101    mkdir -p "${hostfw_symlink_base}"
102  fi
103
104  ln -s "${running_patch_dir}" "${symlink_base}/pnor"
105  ln -s "${running_patch_dir}" "${hostfw_symlink_base}/running"
106  ln -s "${alternate_patch_dir}" "${hostfw_symlink_base}/alternate"
107}
108
109case "$1" in
110  mmc-init)
111    mmc_init
112    ;;
113  mmc-patch)
114    mmc_patch
115    ;;
116  *)
117    echo "Invalid argument"
118    exit 1
119    ;;
120esac
121