2005-11-19 05:17:20 +02:00
|
|
|
#!/bin/sh
|
2006-06-27 03:36:13 +03:00
|
|
|
# Copyright (C) 2006 OpenWrt.org
|
2006-07-29 22:59:45 +03:00
|
|
|
# Copyright (C) 2006 Fokus Fraunhofer <carsten.tittel@fokus.fraunhofer.de>
|
2006-06-27 03:36:13 +03:00
|
|
|
|
2005-02-06 02:52:16 +02:00
|
|
|
alias debug=${DEBUG:-:}
|
|
|
|
|
2006-06-20 00:45:07 +03:00
|
|
|
# newline
|
2006-11-23 20:22:06 +02:00
|
|
|
N="
|
2006-06-20 00:45:07 +03:00
|
|
|
"
|
|
|
|
|
2006-06-25 18:32:30 +03:00
|
|
|
_C=0
|
2007-02-26 22:04:04 +02:00
|
|
|
NO_EXPORT=1
|
2006-06-25 18:32:30 +03:00
|
|
|
|
2005-11-19 05:17:20 +02:00
|
|
|
hotplug_dev() {
|
2007-03-04 22:31:53 +02:00
|
|
|
env -i ACTION=$1 INTERFACE=$2 /sbin/hotplug-call net
|
2005-10-22 23:47:19 +03:00
|
|
|
}
|
2006-05-10 21:17:12 +03:00
|
|
|
|
2006-06-20 00:45:07 +03:00
|
|
|
append() {
|
|
|
|
local var="$1"
|
|
|
|
local value="$2"
|
|
|
|
local sep="${3:- }"
|
2006-11-21 04:13:08 +02:00
|
|
|
|
2007-02-26 22:04:04 +02:00
|
|
|
eval "export ${NO_EXPORT:+-n} -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
|
2006-06-20 00:45:07 +03:00
|
|
|
}
|
|
|
|
|
2006-06-25 18:32:30 +03:00
|
|
|
reset_cb() {
|
2006-11-21 04:13:08 +02:00
|
|
|
config_cb() { return 0; }
|
|
|
|
option_cb() { return 0; }
|
2006-05-10 21:17:12 +03:00
|
|
|
}
|
2006-06-25 18:32:30 +03:00
|
|
|
reset_cb
|
2006-05-10 21:17:12 +03:00
|
|
|
|
|
|
|
config () {
|
2006-11-21 04:13:08 +02:00
|
|
|
local cfgtype="$1"
|
|
|
|
local name="$2"
|
2006-12-20 06:15:13 +02:00
|
|
|
|
2007-02-26 22:04:04 +02:00
|
|
|
export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=$(($CONFIG_NUM_SECTIONS + 1))
|
2006-12-20 06:15:13 +02:00
|
|
|
name="${name:-cfg$CONFIG_NUM_SECTIONS}"
|
|
|
|
append CONFIG_SECTIONS "$name"
|
2007-04-11 22:34:20 +03:00
|
|
|
[ -n "$NO_CALLBACK" ] || config_cb "$cfgtype" "$name"
|
2007-02-26 22:04:04 +02:00
|
|
|
export ${NO_EXPORT:+-n} CONFIG_SECTION="$name"
|
|
|
|
export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_TYPE=$cfgtype"
|
2006-05-10 21:17:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
option () {
|
2006-07-29 22:59:45 +03:00
|
|
|
local varname="$1"; shift
|
2006-11-22 15:32:24 +02:00
|
|
|
local value="$*"
|
2006-11-21 04:13:08 +02:00
|
|
|
|
2007-02-26 22:04:04 +02:00
|
|
|
export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_${varname}=$value"
|
2007-04-11 22:34:20 +03:00
|
|
|
[ -n "$NO_CALLBACK" ] || option_cb "$varname" "$*"
|
2006-05-10 21:17:12 +03:00
|
|
|
}
|
|
|
|
|
2006-10-09 08:59:26 +03:00
|
|
|
config_rename() {
|
|
|
|
local OLD="$1"
|
|
|
|
local NEW="$2"
|
2006-11-21 04:13:08 +02:00
|
|
|
local oldvar
|
2006-10-09 08:59:26 +03:00
|
|
|
local newvar
|
|
|
|
|
2007-06-15 18:59:56 +03:00
|
|
|
[ -n "$OLD" -a -n "$NEW" ] || return
|
2006-11-21 04:13:08 +02:00
|
|
|
for oldvar in `set | grep ^CONFIG_${OLD}_ | \
|
2006-10-09 08:59:26 +03:00
|
|
|
sed -e 's/\(.*\)=.*$/\1/'` ; do
|
2006-11-21 04:13:08 +02:00
|
|
|
newvar="CONFIG_${NEW}_${oldvar##CONFIG_${OLD}_}"
|
2007-02-26 22:04:04 +02:00
|
|
|
eval "export ${NO_EXPORT:+-n} \"$newvar=\${$oldvar}\""
|
2006-11-21 04:13:08 +02:00
|
|
|
unset "$oldvar"
|
2006-10-09 08:59:26 +03:00
|
|
|
done
|
2007-02-26 22:04:04 +02:00
|
|
|
export ${NO_EXPORT:+-n} CONFIG_SECTIONS="$(echo " $CONFIG_SECTIONS " | sed -e "s, $OLD , $NEW ,")"
|
2006-11-21 04:13:08 +02:00
|
|
|
|
2007-02-26 22:04:04 +02:00
|
|
|
[ "$CONFIG_SECTION" = "$OLD" ] && export ${NO_EXPORT:+-n} CONFIG_SECTION="$NEW"
|
2006-10-09 08:59:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
config_unset() {
|
|
|
|
config_set "$1" "$2" ""
|
|
|
|
}
|
|
|
|
|
2006-05-10 21:17:12 +03:00
|
|
|
config_clear() {
|
2006-11-21 04:13:08 +02:00
|
|
|
local SECTION="$1"
|
|
|
|
local oldvar
|
|
|
|
|
2007-02-26 22:04:04 +02:00
|
|
|
export ${NO_EXPORT:+-n} CONFIG_SECTIONS="$(echo " $CONFIG_SECTIONS " | sed -e "s, $OLD , ,")"
|
|
|
|
export ${NO_EXPORT:+-n} CONFIG_SECTIONS="${SECTION:+$CONFIG_SECTIONS}"
|
2006-12-20 06:15:13 +02:00
|
|
|
|
2007-01-30 00:07:17 +02:00
|
|
|
for oldvar in `set | grep ^CONFIG_${SECTION:+${SECTION}_} | \
|
2006-05-10 21:17:12 +03:00
|
|
|
sed -e 's/\(.*\)=.*$/\1/'` ; do
|
2006-11-21 04:13:08 +02:00
|
|
|
unset $oldvar
|
2006-05-10 21:17:12 +03:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
config_load() {
|
2007-07-11 23:39:28 +03:00
|
|
|
local cfg
|
|
|
|
local uci
|
|
|
|
local PACKAGE="$1"
|
|
|
|
|
|
|
|
case "$PACKAGE" in
|
|
|
|
/*) cfg="$PACKAGE"
|
|
|
|
uci=""
|
|
|
|
;;
|
|
|
|
*) cfg="$UCI_ROOT/etc/config/$PACKAGE"
|
|
|
|
uci="/tmp/.uci/${PACKAGE}"
|
|
|
|
;;
|
2007-06-15 18:12:57 +03:00
|
|
|
esac
|
2007-07-11 23:39:28 +03:00
|
|
|
|
|
|
|
[ -e "$cfg" ] || cfg=""
|
|
|
|
[ -e "$uci" ] || uci=""
|
|
|
|
|
|
|
|
# no config
|
|
|
|
[ -z "$cfg" -a -z "$uci" ] && return 1
|
|
|
|
|
2006-06-25 18:32:30 +03:00
|
|
|
_C=0
|
2007-02-26 22:04:04 +02:00
|
|
|
export ${NO_EXPORT:+-n} CONFIG_SECTIONS=
|
|
|
|
export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=0
|
|
|
|
export ${NO_EXPORT:+-n} CONFIG_SECTION=
|
2007-07-11 23:39:28 +03:00
|
|
|
|
|
|
|
${cfg:+. "$cfg"}
|
|
|
|
${uci:+. "$uci"}
|
2006-11-21 04:13:08 +02:00
|
|
|
|
2006-05-10 21:17:12 +03:00
|
|
|
${CONFIG_SECTION:+config_cb}
|
|
|
|
}
|
|
|
|
|
|
|
|
config_get() {
|
|
|
|
case "$3" in
|
2006-10-09 08:59:26 +03:00
|
|
|
"") eval "echo \"\${CONFIG_${1}_${2}}\"";;
|
2007-02-26 22:04:04 +02:00
|
|
|
*) eval "export ${NO_EXPORT:+-n} -- \"$1=\${CONFIG_${2}_${3}}\"";;
|
2006-05-10 21:17:12 +03:00
|
|
|
esac
|
2007-02-28 01:09:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# config_get_bool <variable> <section> <option> [<default>]
|
|
|
|
config_get_bool() {
|
|
|
|
local _tmp
|
|
|
|
config_get "_tmp" "$2" "$3"
|
|
|
|
case "$_tmp" in
|
|
|
|
1|on|enabled) export ${NO_EXPORT:+-n} "$1=1";;
|
|
|
|
0|off|disabled) export ${NO_EXPORT:+-n} "$1=0";;
|
|
|
|
*) eval "$1=${4:-0}";;
|
|
|
|
esac
|
2006-05-10 21:17:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
config_set() {
|
2006-11-22 15:32:24 +02:00
|
|
|
local section="$1"
|
|
|
|
local option="$2"
|
|
|
|
local value="$3"
|
2007-03-24 15:43:04 +02:00
|
|
|
local old_section="$CONFIG_SECTION"
|
|
|
|
|
|
|
|
CONFIG_SECTION="$section"
|
|
|
|
option "$option" "$value"
|
|
|
|
CONFIG_SECTION="$old_section"
|
2006-05-10 21:17:12 +03:00
|
|
|
}
|
2006-05-30 22:38:38 +03:00
|
|
|
|
2006-12-20 06:15:13 +02:00
|
|
|
config_foreach() {
|
|
|
|
local function="$1"
|
2007-07-22 19:55:31 +03:00
|
|
|
[ "$#" -ge 1 ] && shift
|
|
|
|
local type="$1"
|
|
|
|
[ "$#" -ge 1 ] && shift
|
2007-03-02 17:48:29 +02:00
|
|
|
local section cfgtype
|
2006-12-20 06:15:13 +02:00
|
|
|
|
|
|
|
[ -z "$CONFIG_SECTIONS" ] && return 0
|
|
|
|
for section in ${CONFIG_SECTIONS}; do
|
2007-03-02 17:48:29 +02:00
|
|
|
config_get cfgtype "$section" TYPE
|
2007-06-15 18:59:56 +03:00
|
|
|
[ -n "$type" -a "x$cfgtype" != "x$type" ] && continue
|
2007-07-22 19:55:31 +03:00
|
|
|
eval "$function \"\$section\" \"\$@\""
|
2006-12-20 06:15:13 +02:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2006-05-30 22:38:38 +03:00
|
|
|
load_modules() {
|
2007-05-31 18:58:43 +03:00
|
|
|
[ -d /etc/modules.d ] && {
|
|
|
|
cd /etc/modules.d
|
|
|
|
sed 's/^[^#]/insmod &/' $* | ash 2>&- || :
|
|
|
|
}
|
2006-05-30 22:38:38 +03:00
|
|
|
}
|
2006-07-29 22:59:45 +03:00
|
|
|
|
|
|
|
include() {
|
2006-11-21 04:13:08 +02:00
|
|
|
local file
|
|
|
|
|
2006-09-24 16:32:18 +03:00
|
|
|
for file in $(ls $1/*.sh 2>/dev/null); do
|
2006-07-29 22:59:45 +03:00
|
|
|
. $file
|
|
|
|
done
|
|
|
|
}
|
2006-10-26 04:35:50 +03:00
|
|
|
|
|
|
|
find_mtd_part() {
|
|
|
|
local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
|
2007-03-02 21:01:49 +02:00
|
|
|
local PREFIX=/dev/mtdblock
|
2006-11-21 04:13:08 +02:00
|
|
|
|
2006-10-26 04:35:50 +03:00
|
|
|
PART="${PART##mtd}"
|
2007-03-02 21:01:49 +02:00
|
|
|
[ -d /dev/mtdblock ] && PREFIX=/dev/mtdblock/
|
|
|
|
echo "${PART:+$PREFIX$PART}"
|
2006-10-26 04:35:50 +03:00
|
|
|
}
|
|
|
|
|
2006-11-21 04:13:08 +02:00
|
|
|
strtok() { # <string> { <variable> [<separator>] ... }
|
|
|
|
local tmp
|
|
|
|
local val="$1"
|
2006-11-20 21:30:50 +02:00
|
|
|
local count=0
|
|
|
|
|
|
|
|
shift
|
|
|
|
|
|
|
|
while [ $# -gt 1 ]; do
|
2006-11-21 04:13:08 +02:00
|
|
|
tmp="${val%%$2*}"
|
2006-11-20 21:30:50 +02:00
|
|
|
|
2006-11-21 04:13:08 +02:00
|
|
|
[ "$tmp" = "$val" ] && break
|
2006-11-20 21:30:50 +02:00
|
|
|
|
2006-11-21 04:13:08 +02:00
|
|
|
val="${val#$tmp$2}"
|
2006-11-20 21:30:50 +02:00
|
|
|
|
2007-02-26 22:04:04 +02:00
|
|
|
export ${NO_EXPORT:+-n} "$1=$tmp"; count=$((count+1))
|
2006-11-20 21:30:50 +02:00
|
|
|
shift 2
|
|
|
|
done
|
|
|
|
|
2007-06-15 18:59:56 +03:00
|
|
|
if [ $# -gt 0 -a -n "$val" ]; then
|
2007-02-26 22:04:04 +02:00
|
|
|
export ${NO_EXPORT:+-n} "$1=$val"; count=$((count+1))
|
2006-11-20 21:30:50 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
return $count
|
|
|
|
}
|
2007-04-17 02:29:44 +03:00
|
|
|
|
|
|
|
|
|
|
|
jffs2_mark_erase() {
|
|
|
|
local part="$(find_mtd_part "$1")"
|
|
|
|
[ -z "$part" ] && {
|
|
|
|
echo Partition not found.
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
echo -e "\xde\xad\xc0\xde" | mtd -qq write - "$1"
|
|
|
|
}
|