Diablo support.
[busybox-power] / debian / scripts / functions
1 #!/bin/sh
2 # This file contains functions that are used in multiple other scripts, i.e.
3 # shared functions. The purpose of centralising these, is to deduplicate code
4 # and increase maintainability
5 #
6 # By Dennis Groenen <tj.groenen@gmail.com>
7 # GPLv3 licensed
8 #
9
10 # Verbose-aware echo
11 ECHO_VERBOSE() {
12   if test $VERBOSE == 1; then 
13     echo -e "$1"; fi
14 }
15
16 # Detect the current environment
17 CHECK_ENV() {
18     if test -d /scratchbox; then
19       ENVIRONMENT="SDK"
20     else
21       if test -e /proc/component_version; then
22         PROD=$($EXECPWR cat /proc/component_version | $EXECPWR grep product | $EXECPWR cut -d" " -f 6)
23       else
24         PROD=$(/usr/bin/sysinfoclient --get /component/product | $EXECPWR awk '{ print $3 }')
25       fi
26
27       case $PROD in
28         RX-34|RX-44)
29           ENVIRONMENT="DIABLO"
30           ;;
31         RX-51)
32           ENVIRONMENT="FREMANTLE"
33           ;;
34         RM-680|RM-696)
35           ENVIRONMENT="HARMATTAN"
36           ;;
37         *)
38           echo "busybox-power: unsupported environment: $PROD"
39           exit 1
40           ;;
41       esac
42     fi
43 }
44
45 # Check whether the user is root
46 CHECK_ROOT() {
47     if test "`$EXECPWR id -u`" -ne 0; then
48       echo "error: you're not running me as root"
49       exit 1
50     fi
51 }
52
53 # Get the version string of the package providing /bin/busybox
54 GETBBVERSION() {
55     # XXX We assume the package "busybox" provides /bin/busybox
56     /usr/bin/dpkg -s busybox | $EXECPWR awk '/^Version:/ {print $2}'
57 }
58
59 # Get the enforcement status of aegis' source origin check. Returns "1" when
60 # the check is active, otherwise "0"
61 GETORIGINCHECK_STATUS() {
62     ENFORCE="/sys/kernel/security/validator/enforce"
63     ENFORCE_HEX=`$EXECPWR cat $ENFORCE`
64     SID_CHECK_BIT="2"
65
66     if test "$ENFORCE_HEX" == ""; then exit 1; fi
67     RETVAL="1"
68     if test `echo $(($ENFORCE_HEX & $SID_CHECK_BIT))` -eq 0; then
69       RETVAL="0"
70     fi
71     echo $RETVAL
72 }
73
74 # Set the enforcement status of aegis' source origin check. The check will be
75 # enabled when passed "1"; passing "0" will disable it.
76 # Works in both normal and open mode via aegisctl, and in patched open mode via
77 # via writing to sysfs entries directly
78 SETORIGINCHECK_STATUS() {
79     ENABLE=$1
80
81     ENFORCE="/sys/kernel/security/validator/enforce"
82     ENFORCE_HEX=`$EXECPWR cat $ENFORCE`
83     SID_CHECK_BIT="2"
84
85     if test $ENABLE -gt 0; then
86       if test `GETORIGINCHECK_STATUS` -eq 1; then return; fi # Already on
87       ENFORCE_NEW_DEC=`echo $(($ENFORCE_HEX | $SID_CHECK_BIT))`
88       ENFORCE_NEW_HEX=`printf "0x%02x" $ENFORCE_NEW_DEC`
89       echo $ENFORCE_NEW_HEX > $ENFORCE 2> /dev/null
90       if test $? -gt 0; then
91         # Do not exit 1 on failure to re-enable the origincheck; not fatal for
92         # (un)installation of busybox-power
93         /usr/sbin/aegisctl +s > /dev/null
94       fi
95     else
96       if test `GETORIGINCHECK_STATUS` -eq 0; then return; fi # Already off
97       ENFORCE_NEW_DEC=`echo $(($ENFORCE_HEX ^ $SID_CHECK_BIT))`
98       ENFORCE_NEW_HEX=`printf "0x%02x" $ENFORCE_NEW_DEC`
99       echo $ENFORCE_NEW_HEX > $ENFORCE 2> /dev/null
100       if test $? -gt 0; then
101         /usr/sbin/aegisctl @s > /dev/null || exit 1
102       fi
103     fi
104
105     ECHO_VERBOSE "new origincheck: $ENABLE ($ENFORCE_NEW_HEX)"
106 }
107