summaryrefslogtreecommitdiff
path: root/package/fwupdate/src/fwvalidate
blob: f388da7849a0d072f7c02ea2ebf2626b8df82cd6 (plain)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/sh
# This file is part of the OpenADK project.
# Validate update.

GRUB=$(which grub-reboot)
if [ "${GRUB}" = "/usr/sbin/grub-reboot" ]; then
  REVERSE=0
else
  REVERSE=1
fi

DISK=@@DISK@@

if [ $REVERSE -eq 1 ]; then
  PART0="/dev/${DISK}p1"
  PART1="/dev/${DISK}p2"
else
  PART0="/dev/${DISK}2"
  PART1="/dev/${DISK}3"
fi

APPLIANCE_NAME=OpenADK

BOOT0_NAME="OpenADK1"
BOOT1_NAME="OpenADK2"

CURRENT_SYS="$(rdev /|awk '{ print $1 }' )"
TIMEOUT=45
STAT_FILE="/tmp/update_status"

SSH_KEY_FOLDER=/etc/dropbear/
SSH_KEYS=("dropbear_dss_host_key" "dropbear_ecdsa_host_key" "dropbear_rsa_host_key")

DEBUG=1

if [ "x$1" == "xtest" ];then
  TIMEOUT=1
fi

get_interface(){
  ip route list | grep '^default' | cut -d\  -f 5
}
get_nw_mask(){
  # This function will get the NW Mask in the form /x e.g. /16
  local BIT=$(ip a s $(get_interface)| grep inet\ | cut -d/ -f2| cut -d\  -f1)
  echo $BIT
}

getip() {
  DEFDEVICE=$(ip route list | grep ^default | cut -d\  -f5)
  IPADDR=$(ip a s $(ip route list | grep ^default | cut -d\  -f5) | grep inet\ | grep 'inet' | cut -d\  -f 6 | cut -d/ -f1)
  echo $IPADDR
}

chk_initial_save(){
  if [ $(cfgfs status | wc -l) -gt 0 ];then
    echo "please save configuration"
  fi
}

updatebootflag(){

  case "$CURRENT_SYS" in
    "$PART1")
      sfdisk -A /dev/$DISK 1
      ;;
    "$PART0")
      sfdisk -A /dev/$DISK 2
      ;;
    *)
      echo "Current partition $CURRENT_SYS not recognized"
      exit 1
      ;;
  esac

}

updategrub(){
  
  mount -o remount,rw /boot

  case "$CURRENT_SYS" in
    "$PART1")
      grub-set-default OpenADK2
      ;;
    "$PART0")
      grub-set-default OpenADK1
      ;;
    *)
      echo "Current partition $CURRENT_SYS not recognized"
      exit 1
      ;;
  esac

  sync
  mount -o remount,ro /boot

}

base_check() {
  NET_PROGS="$(netstat -tulpn 2>/dev/null)"
  TESTS=0
  TESTSUM=0

  #test start: check if dropbear is running
  T_NAME=dropbear
  if [[ $NET_PROGS = *"/dropbear"* ]];then
    logger -t update "check $T_NAME  OK"
    TESTS=$(( $TESTS + 1 ))
  else
    logger -t update "check $T_NAME  FAILURE"
  fi
  ((TESTSUM = TESTSUM +1))
  #test end
}

if [ -f /installation_date.txt ];then
  echo "Update was applied at:" > $STAT_FILE
  echo "$(head -n1 /installation_date.txt)" >> $STAT_FILE
else
  rm -f $STAT_FILE
fi

# Do some checks before setting the new partiton as default boot partition.
if ( [ -f /firmware_check ] || [ "x$1" = "xtest" ] );then 
  logger -t update "check now!"
  base_check
  i=0
  while [ $TESTS -lt $TESTSUM ];do
      base_check
      [ $DEBUG -gt 0 ] && echo "$i Only $TESTS from $TESTSUM are passed wait until $TIMEOUT"
      sleep 1
      i=$(( $i + 1 ))
      if [ $i -ge $TIMEOUT ];then
	  break
      fi
  done
else
  logger -t update "$APPLIANCE_NAME validate nothing to do..."
  if [ -f $STAT_FILE ];then
    echo "Last update was successful" >> $STAT_FILE
  else
    echo "Firmware check was successful" >> $STAT_FILE
  fi

  n=0
  chk_initial_save
  exit 0
fi

if [ $TESTS -eq $TESTSUM ]; then
    logger -t update "All Tests passed."
    if [ "x$1" = "x" ]; then
      logger -t update "Set default boot partition for bootloader."
      mount -o remount,rw /
      rm /firmware_check
      mount -o remount,ro /
      echo "System check was successful" >> $STAT_FILE
      if [ $REVERSE -eq 1 ]; then
        echo "Nothing todo. All fine." 
        logger -t update "Nothing todo. All fine."
      else
	updategrub
      fi
    fi
else
    if [ $REVERSE -eq 1 ]; then
      updatebootflag
    fi
    logger -t update "Not all tests passed. The default system remains on the current partition."
    logger -t update "Please try to reboot the system and repeat the update."
    echo "ERROR last system update failed, please reboot and try again." >> $STAT_FILE
    exit 1
fi