1: #!/bin/sh
2: #
3: # U-boot cfg nand map load/save management script
4: #
5: # $Id: rc.cfg,v 1.4 2021/03/11 13:59:50 misho Exp $
6:
7: CFG_NAND_LIST="/etc/cfg.map"
8: CFG_NAND_MAP="/dev/ada0g"
9: CFG_NAND_SIZE=524288
10:
11: ELWIX2="/dev/ufs/elwix2"
12: ELWIX="/dev/ufs/elwix"
13:
14: UPGRADE_GPIO_PINS="1 2 3"
15:
16: ELWIX_CFG_BACKUP_NAME="elwix_cfg_$(hostname).bin"
17:
18: CFDIR="/elwix/cf"
19: PKG2CF="${CFDIR}/pkg"
20:
21: die() { echo -n "EXIT: " >&2; echo "$@" >&2; exit 1; }
22:
23: cd ${2:-/etc}
24:
25: case "$1" in
26: load)
27: CFG_NAND_MAP=`gpart show -p | awk '(NR == 1) { disk = $4; } ($3 == disk"g") { printf("/dev/%s", $3); }'`
28: CFG_NAND_SIZE=`gpart show -p | awk '(NR == 1) { disk = $4; } ($3 == disk"g") { printf("%s", $2 * 512); }'`
29:
30: imgupd -gs ${CFG_NAND_SIZE} -f ${CFG_NAND_MAP} | \
31: gunzip 2>&- | cpio -iud 2>&- || die "ERROR:: config load"
32: ;;
33: save)
34: CFG_NAND_MAP=`gpart show -p | awk '(NR == 1) { disk = $4; } ($3 == disk"g") { printf("/dev/%s", $3); }'`
35: CFG_NAND_SIZE=`gpart show -p | awk '(NR == 1) { disk = $4; } ($3 == disk"g") { printf("%s", $2 * 512); }'`
36:
37: if [ ! -r ${CFG_NAND_LIST} ]; then
38: echo "# Auto generated manifest file" >${CFG_NAND_LIST}
39: find . >>${CFG_NAND_LIST}
40: if [ $? -ne 0 -o ! -r ${CFG_NAND_LIST} ]; then
41: die "ERROR:: Manifest cfg nand map - file '${CFG_NAND_LIST}' not found!"
42: fi
43: fi
44:
45: sed -nE -e 's/^ *([^ #]+) */\1/p' < ${CFG_NAND_LIST} | xargs find | \
46: cpio -o | gzip -9 | \
47: imgupd -is ${CFG_NAND_SIZE} -f ${CFG_NAND_MAP} || die "ERROR:: config save"
48: ;;
49: erase)
50: CFG_NAND_MAP=`gpart show -p | awk '(NR == 1) { disk = $4; } ($3 == disk"g") { printf("/dev/%s", $3); }'`
51: CFG_NAND_SIZE=`gpart show -p | awk '(NR == 1) { disk = $4; } ($3 == disk"g") { printf("%s", $2 * 512); }'`
52:
53: imgupd -is ${CFG_NAND_SIZE} -f ${CFG_NAND_MAP} /dev/zero || die "ERROR:: config erase"
54: echo "Config NAND was erased!"
55: ;;
56: restore)
57: CFG_NAND_MAP=`gpart show -p | awk '(NR == 1) { disk = $4; } ($3 == disk"g") { printf("/dev/%s", $3); }'`
58: CFG_NAND_SIZE=`gpart show -p | awk '(NR == 1) { disk = $4; } ($3 == disk"g") { printf("%s", $2 * 512); }'`
59:
60: ELWIX_REMOTE_HOST=`cat /etc/myelwix`
61: ping -c1 $(echo ${ELWIX_REMOTE_HOST} | cut -f3 -d '/' | cut -f2 -d '@') >/dev/null 2>&1
62: if [ $? -ne 0 ]; then
63: die "ERROR:: ELWIX restore from host ${ELWIX_REMOTE_HOST} failed!!!"
64: fi
65:
66: echo "ELWIX starting restore process from ${ELWIX_REMOTE_HOST}"
67: fetch -q -o- --user-agent=ELWIX_Restore --allow-sslv2 \
68: --no-verify-peer --no-verify-hostname \
69: ${ELWIX_REMOTE_HOST}/${ELWIX_CFG_BACKUP_NAME} | \
70: imgupd -R -is ${CFG_NAND_SIZE} -f ${CFG_NAND_MAP} || die "ERROR:: config restore"
71: echo "ELWIX restore process complete. Now rebooting system ..."
72: ;;
73: backup)
74: CFG_NAND_MAP=`gpart show -p | awk '(NR == 1) { disk = $4; } ($3 == disk"g") { printf("/dev/%s", $3); }'`
75: CFG_NAND_SIZE=`gpart show -p | awk '(NR == 1) { disk = $4; } ($3 == disk"g") { printf("%s", $2 * 512); }'`
76:
77: ELWIX_REMOTE_HOST="/tmp/${ELWIX_CFG_BACKUP_NAME}"
78:
79: echo "ELWIX starting backup process to ${ELWIX_REMOTE_HOST}"
80: imgupd -gs ${CFG_NAND_SIZE} -f ${CFG_NAND_MAP} ${ELWIX_REMOTE_HOST} || die "ERROR:: config backup"
81: echo "ELWIX backup process complete."
82: ;;
83: upgrade)
84: echo "ELWIX switch to upgrade mode. Starting TFTPd ..."
85: echo " You can upload now elwix_firmware.bin image to device with \"firmware\" name!!!"
86: echo " Example: put elwix_firmware.bin firmware"
87: for i in ${UPGRADE_GPIO_PINS}; do
88: gpioctl $i 1 >/dev/null 2>&1 || pceng "$i=0" >/dev/null 2>&1
89: done
90: TFTPd -wR
91: ;;
92: pkg_save)
93: echo "ELWIX syncing information about installed packages into storage"
94: if mount | grep "${CFDIR}" >/dev/null 2>&1; then
95: mount -uw ${CFDIR}
96: fi
97: cfexec -L "Locked by rc.cfg for pkg_save"
98: tar -czvf /elwix/altroot/pkg/db/pkgz.tar.gz /var/db/pkg/*
99: if [ $? -ne 0 ]; then
100: echo "ERROR:: package save"
101: else
102: echo "ELWIX sync process complete."
103: fi
104: if mount | grep "${CFDIR}" >/dev/null 2>&1; then
105: mount -ur ${CFDIR}
106: fi
107: cfexec -U
108: ;;
109: pkg_load)
110: echo "ELWIX loading information about installed packages from storage"
111: tar -xzvpf /elwix/altroot/pkg/db/pkgz.tar.gz -C / || die "ERROR:: package load"
112: echo "ELWIX load process complete."
113: ;;
114: pkg2cf)
115: echo "ELWIX setup new pkg location on CF at $PKGCF"
116: if ! mount | grep "${CFDIR}" >/dev/null 2>&1; then
117: echo "Skip pkg2cf setup, because CF partition isn't mounted ..."
118: exit 1
119: fi
120: if [ ! -L /elwix/local -a ! -L /elwix/altroot/pkg/db ]; then
121: mount -uw ${CFDIR}
122: mkdir -p ${PKG2CF}/db
123: mkdir -p ${PKG2CF}/local
124:
125: cfexec -L "Locked by rc.cfg for pkg2cf"
126: if [ ! -L /elwix/altroot/pkg/db ]; then
127: cp -r /elwix/altroot/pkg/db/* ${PKG2CF}/db >/dev/null 2>&1
128: rm -rf /elwix/altroot/pkg/db
129: ln -s ${PKG2CF}/db /elwix/altroot/pkg/db
130: fi
131: if [ ! -L /elwix/local ]; then
132: cp -r /elwix/local/* ${PKG2CF}/local >/dev/null 2>&1
133: rm -rf /elwix/local
134: ln -s ${PKG2CF}/local /elwix/local
135: fi
136: cfexec -U
137:
138: mount -ur ${CFDIR}
139: fi
140: echo "ELWIX pkg2cf setup complete."
141: ;;
142: boot2)
143: echo "ELWIX setup system boot from second image"
144: if [ -c $ELWIX2 ]; then
145: if mount | grep "${ELWIX2} " >/dev/null 2>&1; then
146: die "You already booted from backup image! Skip changing image ..."
147: fi
148:
149: IMGTYPE=$(cat ./platform | awk '{ printf("%s", $2); }')
150: case $IMGTYPE in
151: base)
152: if ! mount $ELWIX2 /mnt; then
153: die "Backup image seems like broken ..."
154: fi
155:
156: awk -v elwix="$ELWIX" -v elwix2="$ELWIX2" '
157: ($1 == elwix) { gsub("\/", "\/", elwix); sub(elwix, elwix2, $0); }
158: { print $0; }
159: ' /mnt/etc/fstab > /tmp/fstab.tmp
160: if [ $? -eq 0 ]; then
161: cp /tmp/fstab.tmp /mnt/etc/fstab
162: rm -f /tmp/fstab.tmp
163: fi
164:
165: umount /mnt
166: echo "ELWIX prepare backup image for next boot!"
167: ;;
168: *)
169: ;;
170: esac
171:
172: cfger -o /tmp/loader.conf.local.tmp -c "Edit by /etc/rc.cfg" \
173: -JSs currdev /boot/loader.conf.local "\"disk0f:\""
174: cfexec -L "Locked by rc.cfg for boot2"
175: cp /tmp/loader.conf.local.tmp /boot/loader.conf.local
176: cfexec -U
177: rm -f /tmp/loader.conf.local.tmp
178: else
179: die "ERROR:: ELWIX has no second usable image!"
180: fi
181: echo "ELWIX boot2 setup complete."
182: ;;
183: boot)
184: echo "ELWIX setup system boot from main image"
185: if mount | grep "${ELWIX2} " >/dev/null 2>&1; then
186: mount ${ELWIX} /mnt
187:
188: cfger -o /tmp/loader.conf.local.tmp -c "Edit by /etc/rc.cfg" \
189: -JSs currdev /mnt/boot/loader.conf.local "\"disk0a:\""
190: cp /tmp/loader.conf.local.tmp /mnt/boot/loader.conf.local
191: rm -f /tmp/loader.conf.local.tmp
192:
193: umount /mnt
194: else
195: die "You already booted from main image! Skip changing image ..."
196: fi
197: echo "ELWIX boot setup complete."
198: ;;
199: *)
200: echo "Missing parameter - available options are :"
201: echo " load | save | erase | backup | restore | upgrade | "
202: echo " pkg_load | pkg_save | pkg2cf | boot2 | boot"
203: ;;
204: esac
205:
206: cd -
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>