icwmp/scripts/functions/common
2020-12-13 11:20:47 +01:00

169 lines
4.3 KiB
Bash

#!/bin/sh
# Copyright (C) 2011-2012 Luka Perkov <freecwmp@lukaperkov.net>
# Copyright (C) 2013-2019 iopsys Software Solutions AB
# Author Ahmed Zribi <ahmed.zribi@pivasoftware.com>
# Author Mohamed Kallel <mohamed.kallel@pivasoftware.com>
FIRMWARE_UPGRADE_IMAGE="/tmp/firmware.bin"
icwmp_fault_output()
{
local MSG=""
local fault_code="$2"
if ([ "$action" = "du_install" ] || [ "$action" = "du_update" ]);then
local package_name="$3"
local package_version="$4"
local package_uuid="$5"
local package_env="$6"
fi
case "$action" in
du_install|du_update)
json_init
json_add_string "fault_code" "$fault_code"
if [ "$#" = "6" ];then
json_add_string "package_name" "$package_name"
json_add_string "package_version" "$package_version"
json_add_string "package_uuid" "$package_uuid"
json_add_string "package_env" "$package_env"
fi
json_close_object
MSG=`json_dump`
;;
*download|*upload)
json_init
json_add_string "fault_code" "$fault_code"
json_close_object
MSG=`json_dump`
;;
esac
echo "$MSG"
}
icwmp_check_image()
{
code="$(ubus -t 10 call rpc-sys upgrade_test | jsonfilter -e @.code)"
[ "$code" == "0" ] && return 0 || return 1
}
icwmp_check_flash_size()
{
local size=0
if [ -f /proc/mtd ];then
for line in `cat /proc/mtd`
do
if [ "`echo $line|grep "^([^[:space:]]+)[[:space:]]+([^[:space:]]+)[[:space:]]+([^[:space:]]+)[[:space:]]+\"([^[:space:]]+)\""`" != "" ]
then
b=`cat $line|cut -f1 -d " "`
n=`cat $line|cut -f3 -d " "`
if [ "$n" = "\"linux\"" -o "$n" = "\"firmware\"" ];then
size=`echo $(($s))`
break;
fi
fi
done
elif [ -f /proc/partitions ]
then
for line in `cat /proc/partitions`
do
if [ "`echo $line|grep "[[:space:]]*([[:digit:]]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([^[:space:]]+)[[:space:]]+([^[:space:]]+)"`" != "" ]
then
b=`cat $line|cut -f2 -d " "`
n=`cat $line|cut -f3 -d " "`
if [ checkline "([^[:space:]]+)" $n ];then
size=`let $b*1024`;
break;
fi
fi
done
fi
echo "$size"
}
icwmp_apply_firmware()
{
local fault_code="9000"
sync
uci set cwmp.cpe.exec_download=1
uci commit cwmp
ubus call rpc-sys upgrade_start '{"keep":true}'
sleep 60
[ 0 -gt 1 ] # If the updade did not upgrade within 60 seconds, assume it has failed
if [ "$?" != "0" ]; then
let fault_code=$fault_code+$FAULT_CPE_DOWNLOAD_FAIL_FILE_CORRUPTED
icwmp_fault_output "" "$fault_code"
uci set cwmp.cpe.exec_download=0
uci commit cwmp
else
icwmp_fault_output "" "$FAULT_CPE_NO_FAULT"
fi
}
icwmp_apply_web_content()
{
local fault_code="9000"
/bin/opkg install /tmp/web_content.ipk
if [ "$?" != "0" ];then
rm /tmp/web_content.ipk 2> /dev/null
let fault_code=$fault_code+$FAULT_CPE_DOWNLOAD_FAIL_FILE_CORRUPTED
icwmp_fault_output "" "$fault_code"
else
icwmp_fault_output "" "$FAULT_CPE_NO_FAULT"
fi
}
icwmp_apply_vendor_configuration()
{
local fault_code="9000"
local current_file="/tmp/vendor_configuration_file.cfg"
if [ $# -eq 0 ]; then
/sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} -q import < /tmp/vendor_configuration_file.cfg
else
current_file="/etc/vendor_configuration_file_$1.cfg"
/sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} -q import < /etc/vendor_configuration_file_$1.cfg
rm "$current_file" 2> /dev/null
fi
if [ "$?" = "0" ];then
/sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} -q commit
if [ "$?" != "0" ];then
let fault_code=$fault_code+$FAULT_CPE_INTERNAL_ERROR
icwmp_fault_output "" "$fault_code"
else
icwmp_fault_output "" "$FAULT_CPE_NO_FAULT"
sync
reboot
fi
else
if [ -f "$current_file" ]; then
rm "$current_file" 2> /dev/null;
fi
let fault_code=$fault_code+$FAULT_CPE_DOWNLOAD_FAIL_FILE_CORRUPTED
icwmp_fault_output "" "$fault_code"
fi
}
icwmp_apply_ca_ssl_certificate_key()
{
local fault_code="9000"
local current_file="/tmp/owsd-repeater-control-cert.pem"
# if [ $# -gt 0 ]; then
# current_file="/tmp/$1"
# fi
cert=`uci get owsd.ubusproxy.peer_cert`
# i=1
# test=true
# path=""
# while [[ $test ]]
# do
# i=$((i+1))
# command="echo $cert | awk -F\"/\" '{print \$$i}'"
# dir=`eval $command`
# if [[ -z $dir ]]; then break; fi
# path=$path"/"$dir
# if [ ! -d $path ]; then mkdir $path; fi
# done
cp $current_file $cert
}