/
counter_to_tffs
executable file
·100 lines (100 loc) · 3.54 KB
/
counter_to_tffs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#! /bin/sh
# SPDX-License-Identifier: GPL-2.0-or-later
##################################################################################
#
# prepare a (partial) TFFS image from a counter dump file (from EVA FTP session)
#
##################################################################################
#
# TFFS entries are built of:
#
# offset length meaning
# 0 2 node ID - 256-511 reserved for environment values, extended with
# bb0-bb32 (0x200-0x21f) beyond this limit, < 256 used for zlib
# compressed config settings (accessible via TFFS driver) and some
# counters exist with IDs in the range of 0x400-0x407
# 2 2 length of data following this header (in native endianess of the
# target device), including the terminating NUL for the string
# 4 n data for this entry
#
# Each entry is aligned on the next 4-byte boundary, if the length isn't integral.
#
##################################################################################
#
# The counter file (usually read from EVA with "RETR count") is expected on
# stdin and the binary output data is written to stdout.
#
##################################################################################
#
# some constants
#
##################################################################################
[ ${#TMP} -eq 0 ] && TMP=/tmp
tmpdir=$TMP/tmp_$(date +%s)_$$
cntfile=$tmpdir/counter
##################################################################################
#
# helper functions
#
##################################################################################
. ${YF_SCRIPT_DIR:-.}/yf_helpers
##################################################################################
#
# cleanup at exit
#
##################################################################################
trap "rm -r $tmpdir 2>/dev/null" EXIT HUP
##################################################################################
#
# create temporary directory and store stdin (it contains our counter file)
#
##################################################################################
mkdir -p $tmpdir
sed -e "s|^\([^ ]*\)[ ]*\([^\r]*\)\r\?$|name=\"\1\" value=\"\2\"|" >$cntfile
##################################################################################
#
# process environment file
#
##################################################################################
out=""
counters="run_years run_hours run_days run_mounths reboot_major reboot_minor"
# yes, the wrong spelling of "months" is spread across all (or at least most)
# models, as far as I could discover
ids="0405 0402 0403 0404 0400 0401"
lens="4 4 4 4 8 4"
for counter in $counters; do
eval $counter=-1
done
while read line; do
eval $line
if ! yf_is_decimal $value; then
# some devices (e.g. 6490) return strange values ...
value=0
fi
index=$(yf_index_of "$name" "$counters")
if [ $? -ne 0 ]; then
echo "unknown name '$name' found in counter file" 1>&2
else
eval $name=$(( -1 << $value ))
fi
done <$cntfile
index=0
for counter in $counters; do
index=$(( index + 1 ))
id=$(yf_word_of $index "$ids")
len=$(yf_word_of $index "$lens")
eval value=\$$counter
val="$(printf "%0x" $value)"
val="$(yf_from_right "$val" $(( len * 2 )))"
val="$(yf_reverse_hex "$val")"
entry="$id$(yf_dec2hex $len 2)$val"
out="$out$entry"
done
echo "$out" | yf_hex2bin
##################################################################################
#
# regular end of script reached
#
##################################################################################
rm -r $tmpdir
exit 0