summaryrefslogtreecommitdiff
path: root/snd-alpx/tools/audio_card_update_firmware.sh
blob: 9dfa0c7b8ffbd2e7bb67ccd0da5d694442445db8 (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
#!/bin/bash


# This script is used to update the firmware of the card FOR Audio card only (Alpxxy) with x and y are numbers.
# Param 1 : mtd partition number of the "fw-user-updatable", see /proc/mtd file and select
# the one of the to be updated card.
#Param 2 : firmware complete filepath

#NOTE : use BASH read, SH read : follow https://stackoverflow.com/questions/226703/how-do-i-prompt-for-yes-no-cancel-input-in-a-linux-shell-script


user_manual(){
 echo "Param 1 : mtd partition number of the \"fw-user-updatable\", see /proc/mtd file and select"
 echo "Param 2 : firmware complete filepath"
 echo "Check user can write to the mtd partition's file"
 return 0
}


## MAIN ##
# warn about supported card
#Warn about the card supported model : Alp Audio card.
read -r -n 1 -p "WARNING: This is only for AUDIO based Alp cards (ie Alp222, Alp882 and Alp442) update ? [y/N]:" user_resp

echo    # (optional) move to a new line
if [[ ! "$user_resp" =~ ^[Yy]$ ]]
then
	# handle exits from shell or function but don't exit interactive shell
    [[ "$0" = "$BASH_SOURCE" ]] && echo "CANCELED by User" && exit 1 || return 1 
fi


#Check parameters
[ "$#" -ne "2" ] && user_manual && exit 1

fw_partition_path="$1"
source_fw_file="$2"



# /dev/mtd is a character device
[ ! -c "$fw_partition_path" ] && echo "$fw_partition_path Not a C Device" && exit 2
#Write access ?
[ ! -w "$fw_partition_path" ] && echo "Not writeable" && exit 2

# firmware file condition
[ !  -e "$source_fw_file" ] && user_manual && exit 2
[  ! -f "$source_fw_file" ] && user_manual && exit 2
[  ! -s "$source_fw_file" ] && user_manual && exit 2
[  ! -r "$source_fw_file" ] && user_manual && exit 2

#Check available space !! TODO
# It  Requires firmware size + 4kB for the extracted HEADER.

#read : BASH extensions used !
read  -r -n 1 -p Writing\ "$source_fw_file"\ to\ "$fw_partition_path ? [y/N]: " user_resp

echo    # (optional) move to a new line
if [[ ! "$user_resp" =~ ^[Yy]$ ]]
then
	# handle exits from shell or function but don't exit interactive shell
    [[ "$0" = "$BASH_SOURCE" ]] && echo "CANCELED by User" && exit 1 || return 1 
fi

dd if="$source_fw_file" of=fw_header.bin bs=4096 count=1 status=none
if [ "$?" -ne "0" ] ; then
	echo "Error when preparing the firmware, check disk size please."
	exit 4;
fi

dd if="$source_fw_file" of=fw_body.bin bs=4096 skip=1 seek=1 status=none
if [ "$?" -ne "0" ] ; then
	echo "Error when preparing the firmware, check disk size please."
	exit 4;
fi


echo "Updating first phase ..."
cat fw_body.bin > "$fw_partition_path"
if [ "$?" -ne "0" ] ; then
	echo "!! Update failed DON'T poweroff, correct to retry."
	rm -f fw_body.bin fw_header.bin
	exit 3;
fi

echo "Updating second phase ..."
cat fw_header.bin > "$fw_partition_path"

if [ "$?" -ne "0" ] ; then
	echo "!! Update failed DON'T poweroff, correct to retry."
	rm -f fw_body.bin fw_header.bin
	exit 3;
fi

echo "Update SUCCEEDED"

rm -f fw_body.bin fw_header.bin

exit 0