summaryrefslogtreecommitdiff
path: root/package/fwupdate/src/fwupdate
blob: 68796d7bd40d4b6b52caad9f41f58412ac2a5163 (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
#!/bin/sh
# This file is part of the OpenADK project.
# Do 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

# Name of the archive, which is the firmware. For this file is the checksum calculated and
# checked against the one from the tar archive.
FW_NAME="openadk.tar.xz"
CHECK_SUM_FILE="sha256.txt"
TMP_MOUNT="/mnt"

if [ -z $1 ]; then
  echo "Usage: $0 <full path to update filename>"
  exit 1
fi
if [ ! -f $1 ]; then
  echo "File $1 not found"
  exit 1
fi
FIRMWARE=$1

echo "Validate new firmware $FIRMWARE"
echo "Extract checksum file ..."
cmd="tar -xf $FIRMWARE $CHECK_SUM_FILE"
$cmd
if [ ! -f $CHECK_SUM_FILE ];then
  echo "No checksum file found! $CHECK_SUM_FILE"
  exit 3
fi

if [ $(wc -m $CHECK_SUM_FILE | cut -d\  -f1) -eq 65 ];then
  echo "Found a checksum in file $CHECK_SUM_FILE"
else
  echo "No checksum found in archive! $(wc -m $CHECK_SUM_FILE | cut -d\  -f1)"
  exit 4
fi

CHECK_SUM_UPDATE_FILE="$(cat $CHECK_SUM_FILE)"

echo "Calculate the checksum of the new firmware ..."
CHECK_SUM_NEW_SYSTEM=$(tar -xf $FIRMWARE $FW_NAME -O | sha256sum - |cut -d\  -f1)

echo "Compare the checksums ..."
if [ "X$CHECK_SUM_NEW_SYSTEM" = "X$CHECK_SUM_UPDATE_FILE" ]; then
    echo "Checksum verified (they match) ..."
else
    echo "Checksum does not match!"
    echo "${CHECK_SUM_UPDATE_FILE} "
    echo "${CHECK_SUM_NEW_SYSTEM}"
fi

echo "First unmount $TMP_MOUNT the spare partition just in case ..."
umount $TMP_MOUNT 2>/dev/null

# create the mkfs options depending on which is the active partition
CURRENT_SYS="$(rdev /|awk '{ print $1 }')"
case "$CURRENT_SYS" in
  "$PART0")
    MOUNTPART="$PART1"
    PARTNUM=2
    OS=OpenADK2
    ;;
  "$PART1")
    MOUNTPART="$PART0"
    PARTNUM=1
    OS=OpenADK1
    ;;
  *)
    echo "Current partition $CURRENT_SYS not recognized"
    exit 5
    ;;
esac
echo "Currently the system is running on $CURRENT_SYS"

# Create filesystem on inactive partition
echo "The partition $MOUNTPART is going to be prepared for the new system."
echo "Creating the filesystem ..."

mkfs.ext2 -j -q -F $MOUNTPART
if [ $? -ne 0 ];then
  echo "It was not possible to create the new filesystem on $MOUNTPART"
  exit 6
fi

echo "Mount the new partition $MOUNTPART ..."
mount -t ext4 $MOUNTPART $TMP_MOUNT
if [ $? -ne 0 ];then
  echo "It was not possible to mount the partition for the new system!"
  echo "Please reboot the system and try to update again."
  exit 7
fi

cd $TMP_MOUNT
echo "The new system is going to be extracted into the partition $MOUNTPART ..."
tar -xf $FIRMWARE $FW_NAME -O|tar -xJf -
if [ $? -ne 0 ]; then
  echo "The extraction of the new system failed!"
  echo "Please reboot the system and try to update again."
  cd /
  umount $MOUNTPART
  exit 8
fi

echo "Checking the new system on next boot"
touch $TMP_MOUNT/firmware_check
if [ $? -ne 0 ]; then
  echo "General ERROR"
  echo "Please reboot the system and try to update again."
  cd /
  umount $MOUNTPART
  exit 9
fi
# echo "DD.MM.YYYY hh:mm:hh\n$(date '+%d.%m.%Y %H:%M:%S')" > install_date.txt
date '+%d.%B.%Y %H:%M:%S' > $TMP_MOUNT/installation_date.txt
if [ $? -ne 0 ]; then
  echo "General ERROR"
  echo "Please reboot the system and try to update again."
  cd /
  umount $MOUNTPART
  exit 9
fi
echo "$CHECK_SUM_NEW_FW" >> $TMP_MOUNT/installation_date.txt
if [ $? -ne 0 ]; then
  echo "General ERROR"
  echo "Please reboot the system and try to update again."
  cd /
  umount $MOUNTPART
  exit 9
fi

cd /
umount $MOUNTPART
if [ $REVERSE -eq 1 ]; then
  echo "Switch bootable partition to new system"
  sfdisk -A /dev/$DISK $PARTNUM >/dev/null 2>&1
else
grep /boot /proc/mounts 2>/dev/null
  if [ $? -eq 0 ]; then
    mount -o remount,rw /boot
  else
    mount /dev/sda1 /boot
  fi
  grub-reboot $OS
  grep /boot /proc/mounts 2>/dev/null
  if [ $? -eq 0 ]; then
    mount -o remount,ro /boot
  else
    umount /boot
  fi
fi
sync
echo "Reboot now to the updated system $OS"