base-files: mtd_get_mac_ascii bugfix
[openwrt/svn-archive/archive.git] / package / base-files / files / lib / functions.sh
1 #!/bin/sh
2 # Copyright (C) 2006-2013 OpenWrt.org
3 # Copyright (C) 2006 Fokus Fraunhofer <carsten.tittel@fokus.fraunhofer.de>
4 # Copyright (C) 2010 Vertical Communications
5
6
7 debug () {
8 ${DEBUG:-:} "$@"
9 }
10
11 # newline
12 N="
13 "
14
15 _C=0
16 NO_EXPORT=1
17 LOAD_STATE=1
18 LIST_SEP=" "
19
20 hotplug_dev() {
21 env -i ACTION=$1 INTERFACE=$2 /sbin/hotplug-call net
22 }
23
24 append() {
25 local var="$1"
26 local value="$2"
27 local sep="${3:- }"
28
29 eval "export ${NO_EXPORT:+-n} -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
30 }
31
32 list_contains() {
33 local var="$1"
34 local str="$2"
35 local val
36
37 eval "val=\" \${$var} \""
38 [ "${val%% $str *}" != "$val" ]
39 }
40
41 list_remove() {
42 local var="$1"
43 local remove="$2"
44 local val
45
46 eval "val=\" \${$var} \""
47 val1="${val%% $remove *}"
48 [ "$val1" = "$val" ] && return
49 val2="${val##* $remove }"
50 [ "$val2" = "$val" ] && return
51 val="${val1## } ${val2%% }"
52 val="${val%% }"
53 eval "export ${NO_EXPORT:+-n} -- \"$var=\$val\""
54 }
55
56 config_load() {
57 [ -n "$IPKG_INSTROOT" ] && return 0
58 uci_load "$@"
59 }
60
61 reset_cb() {
62 config_cb() { return 0; }
63 option_cb() { return 0; }
64 list_cb() { return 0; }
65 }
66 reset_cb
67
68 package() {
69 return 0
70 }
71
72 config () {
73 local cfgtype="$1"
74 local name="$2"
75
76 export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=$(($CONFIG_NUM_SECTIONS + 1))
77 name="${name:-cfg$CONFIG_NUM_SECTIONS}"
78 append CONFIG_SECTIONS "$name"
79 [ -n "$NO_CALLBACK" ] || config_cb "$cfgtype" "$name"
80 export ${NO_EXPORT:+-n} CONFIG_SECTION="$name"
81 export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_TYPE=$cfgtype"
82 }
83
84 option () {
85 local varname="$1"; shift
86 local value="$*"
87
88 export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_${varname}=$value"
89 [ -n "$NO_CALLBACK" ] || option_cb "$varname" "$*"
90 }
91
92 list() {
93 local varname="$1"; shift
94 local value="$*"
95 local len
96
97 config_get len "$CONFIG_SECTION" "${varname}_LENGTH" 0
98 [ $len = 0 ] && append CONFIG_LIST_STATE "${CONFIG_SECTION}_${varname}"
99 len=$(($len + 1))
100 config_set "$CONFIG_SECTION" "${varname}_ITEM$len" "$value"
101 config_set "$CONFIG_SECTION" "${varname}_LENGTH" "$len"
102 append "CONFIG_${CONFIG_SECTION}_${varname}" "$value" "$LIST_SEP"
103 list_cb "$varname" "$*"
104 }
105
106 config_rename() {
107 local OLD="$1"
108 local NEW="$2"
109 local oldvar
110 local newvar
111
112 [ -n "$OLD" -a -n "$NEW" ] || return
113 for oldvar in `set | grep ^CONFIG_${OLD}_ | \
114 sed -e 's/\(.*\)=.*$/\1/'` ; do
115 newvar="CONFIG_${NEW}_${oldvar##CONFIG_${OLD}_}"
116 eval "export ${NO_EXPORT:+-n} \"$newvar=\${$oldvar}\""
117 unset "$oldvar"
118 done
119 export ${NO_EXPORT:+-n} CONFIG_SECTIONS="$(echo " $CONFIG_SECTIONS " | sed -e "s, $OLD , $NEW ,")"
120
121 [ "$CONFIG_SECTION" = "$OLD" ] && export ${NO_EXPORT:+-n} CONFIG_SECTION="$NEW"
122 }
123
124 config_unset() {
125 config_set "$1" "$2" ""
126 }
127
128 config_clear() {
129 local SECTION="$1"
130 local oldvar
131
132 list_remove CONFIG_SECTIONS "$SECTION"
133 export ${NO_EXPORT:+-n} CONFIG_SECTIONS="${SECTION:+$CONFIG_SECTIONS}"
134
135 for oldvar in `set | grep ^CONFIG_${SECTION:+${SECTION}_} | \
136 sed -e 's/\(.*\)=.*$/\1/'` ; do
137 unset $oldvar
138 done
139 }
140
141 # config_get <variable> <section> <option> [<default>]
142 # config_get <section> <option>
143 config_get() {
144 case "$3" in
145 "") eval echo "\${CONFIG_${1}_${2}:-\${4}}";;
146 *) eval export ${NO_EXPORT:+-n} -- "${1}=\${CONFIG_${2}_${3}:-\${4}}";;
147 esac
148 }
149
150 # config_get_bool <variable> <section> <option> [<default>]
151 config_get_bool() {
152 local _tmp
153 config_get _tmp "$2" "$3" "$4"
154 case "$_tmp" in
155 1|on|true|enabled) _tmp=1;;
156 0|off|false|disabled) _tmp=0;;
157 *) _tmp="$4";;
158 esac
159 export ${NO_EXPORT:+-n} "$1=$_tmp"
160 }
161
162 config_set() {
163 local section="$1"
164 local option="$2"
165 local value="$3"
166 local old_section="$CONFIG_SECTION"
167
168 CONFIG_SECTION="$section"
169 option "$option" "$value"
170 CONFIG_SECTION="$old_section"
171 }
172
173 config_foreach() {
174 local ___function="$1"
175 [ "$#" -ge 1 ] && shift
176 local ___type="$1"
177 [ "$#" -ge 1 ] && shift
178 local section cfgtype
179
180 [ -z "$CONFIG_SECTIONS" ] && return 0
181 for section in ${CONFIG_SECTIONS}; do
182 config_get cfgtype "$section" TYPE
183 [ -n "$___type" -a "x$cfgtype" != "x$___type" ] && continue
184 eval "$___function \"\$section\" \"\$@\""
185 done
186 }
187
188 config_list_foreach() {
189 [ "$#" -ge 3 ] || return 0
190 local section="$1"; shift
191 local option="$1"; shift
192 local function="$1"; shift
193 local val
194 local len
195 local c=1
196
197 config_get len "${section}" "${option}_LENGTH"
198 [ -z "$len" ] && return 0
199 while [ $c -le "$len" ]; do
200 config_get val "${section}" "${option}_ITEM$c"
201 eval "$function \"\$val\" \"\$@\""
202 c="$(($c + 1))"
203 done
204 }
205
206 insert_modules() {
207 [ -d /etc/modules.d ] && {
208 cd /etc/modules.d
209 sed 's/^[^#]/insmod &/' $* | ash 2>&- || :
210 }
211 }
212
213 include() {
214 local file
215
216 for file in $(ls $1/*.sh 2>/dev/null); do
217 . $file
218 done
219 }
220
221 find_mtd_index() {
222 local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
223 local INDEX="${PART##mtd}"
224
225 echo ${INDEX}
226 }
227
228 find_mtd_part() {
229 local INDEX=$(find_mtd_index "$1")
230 local PREFIX=/dev/mtdblock
231
232 [ -d /dev/mtdblock ] && PREFIX=/dev/mtdblock/
233 echo "${INDEX:+$PREFIX$INDEX}"
234 }
235
236 find_mtd_chardev() {
237 local INDEX=$(find_mtd_index "$1")
238 local PREFIX=/dev/mtd
239
240 [ -d /dev/mtd ] && PREFIX=/dev/mtd/
241 echo "${INDEX:+$PREFIX$INDEX}"
242 }
243
244 mtd_get_mac_ascii()
245 {
246 local mtdname="$1"
247 local key="$2"
248 local part
249 local mac_dirty
250
251 part=$(find_mtd_part "$mtdname")
252 if [ -z "$part" ]; then
253 echo "mtd_get_mac_ascii: partition $mtdname not found!" >&2
254 return
255 fi
256
257 mac_dirty=$(strings "$part" | sed -n 's/^'"$key"'=//p')
258
259 # "canonicalize" mac
260 [ -n "$mac_dirty" ] && echo ${mac_dirty} | tr [A-F] [a-f]
261 }
262
263 mtd_get_mac_binary() {
264 local mtdname="$1"
265 local offset="$2"
266 local part
267
268 part=$(find_mtd_part "$mtdname")
269 if [ -z "$part" ]; then
270 echo "mtd_get_mac_binary: partition $mtdname not found!" >&2
271 return
272 fi
273
274 dd bs=1 skip=$offset count=6 if=$part 2>/dev/null | hexdump -v -n 6 -e '5/1 "%02x:" 1/1 "%02x"'
275 }
276
277 mtd_get_part_size() {
278 local part_name=$1
279 local first dev size erasesize name
280 while read dev size erasesize name; do
281 name=${name#'"'}; name=${name%'"'}
282 if [ "$name" = "$part_name" ]; then
283 echo $((0x$size))
284 break
285 fi
286 done < /proc/mtd
287 }
288
289 macaddr_add() {
290 local mac=$1
291 local val=$2
292 local oui=${mac%:*:*:*}
293 local nic=${mac#*:*:*:}
294
295 nic=$(printf "%06x" $((0x${nic//:/} + $val & 0xffffff)) | sed 's/^\(.\{2\}\)\(.\{2\}\)\(.\{2\}\)/\1:\2:\3/')
296 echo $oui:$nic
297 }
298
299 macaddr_setbit_la()
300 {
301 local mac=$1
302
303 printf "%02x:%s" $((0x${mac%%:*} | 0x02)) ${mac#*:}
304 }
305
306 macaddr_2bin()
307 {
308 local mac=$1
309
310 echo -ne \\x${mac//:/\\x}
311 }
312
313 strtok() { # <string> { <variable> [<separator>] ... }
314 local tmp
315 local val="$1"
316 local count=0
317
318 shift
319
320 while [ $# -gt 1 ]; do
321 tmp="${val%%$2*}"
322
323 [ "$tmp" = "$val" ] && break
324
325 val="${val#$tmp$2}"
326
327 export ${NO_EXPORT:+-n} "$1=$tmp"; count=$((count+1))
328 shift 2
329 done
330
331 if [ $# -gt 0 -a -n "$val" ]; then
332 export ${NO_EXPORT:+-n} "$1=$val"; count=$((count+1))
333 fi
334
335 return $count
336 }
337
338
339 jffs2_mark_erase() {
340 local part="$(find_mtd_part "$1")"
341 [ -z "$part" ] && {
342 echo Partition not found.
343 return 1
344 }
345 echo -e "\xde\xad\xc0\xde" | mtd -qq write - "$1"
346 }
347
348 uci_apply_defaults() {
349 cd /etc/uci-defaults || return 0
350 files="$(ls)"
351 [ -z "$files" ] && return 0
352 mkdir -p /tmp/.uci
353 for file in $files; do
354 ( . "./$(basename $file)" ) && rm -f "$file"
355 done
356 uci commit
357 }
358
359 group_add() {
360 local name="$1"
361 local gid="$2"
362 local rc
363 [ -f "${IPKG_INSTROOT}/etc/group" ] || return 1
364 [ -n "$IPKG_INSTROOT" ] || lock /var/lock/group
365 echo "${name}:x:${gid}:" >> ${IPKG_INSTROOT}/etc/group
366 rc=$?
367 [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/group
368 return $rc
369 }
370
371 group_exists() {
372 grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/group
373 }
374
375 user_add() {
376 local name="${1}"
377 local uid="${2}"
378 local gid="${3:-$2}"
379 local desc="${4:-$1}"
380 local home="${5:-/var/run/$1}"
381 local shell="${6:-/bin/false}"
382 local rc
383 [ -f "${IPKG_INSTROOT}/etc/passwd" ] || return 1
384 [ -n "$IPKG_INSTROOT" ] || lock /var/lock/passwd
385 echo "${name}:x:${uid}:${gid}:${desc}:${home}:${shell}" >> ${IPKG_INSTROOT}/etc/passwd
386 echo "${name}:x:0:0:99999:7:::" >> ${IPKG_INSTROOT}/etc/shadow
387 rc=$?
388 [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/passwd
389 return $rc
390 }
391
392 user_exists() {
393 grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/passwd
394 }
395
396
397 pi_include() {
398 if [ -f "/tmp/overlay/$1" ]; then
399 . "/tmp/overlay/$1"
400 elif [ -f "$1" ]; then
401 . "$1"
402 elif [ -d "/tmp/overlay/$1" ]; then
403 if [ -n "$(ls /tmp/overlay/$1/*.sh 2>/dev/null)" ]; then
404 for src_script in /tmp/overlay/$1/*.sh; do
405 . "$src_script"
406 done
407 fi
408 elif [ -d "$1" ]; then
409 if [ -n "$(ls $1/*.sh 2>/dev/null)" ]; then
410 for src_script in $1/*.sh; do
411 . "$src_script"
412 done
413 fi
414 else
415 echo "WARNING: $1 not found"
416 return 1
417 fi
418 return 0
419 }
420
421 boot_hook_splice_start() {
422 export -n PI_HOOK_SPLICE=1
423 }
424
425 boot_hook_splice_finish() {
426 local hook
427 for hook in $PI_STACK_LIST; do
428 local v; eval "v=\${${hook}_splice:+\$${hook}_splice }$hook"
429 export -n "${hook}=${v% }"
430 export -n "${hook}_splice="
431 done
432 export -n PI_HOOK_SPLICE=
433 }
434
435 boot_hook_init() {
436 local hook="${1}_hook"
437 export -n "PI_STACK_LIST=${PI_STACK_LIST:+$PI_STACK_LIST }$hook"
438 export -n "$hook="
439 }
440
441 boot_hook_add() {
442 local hook="${1}_hook${PI_HOOK_SPLICE:+_splice}"
443 local func="${2}"
444
445 [ -n "$func" ] && {
446 local v; eval "v=\$$hook"
447 export -n "$hook=${v:+$v }$func"
448 }
449 }
450
451 boot_hook_shift() {
452 local hook="${1}_hook"
453 local rvar="${2}"
454
455 local v; eval "v=\$$hook"
456 [ -n "$v" ] && {
457 local first="${v%% *}"
458
459 [ "$v" != "${v#* }" ] && \
460 export -n "$hook=${v#* }" || \
461 export -n "$hook="
462
463 export -n "$rvar=$first"
464 return 0
465 }
466
467 return 1
468 }
469
470 boot_run_hook() {
471 local hook="$1"
472 local func
473
474 while boot_hook_shift "$hook" func; do
475 local ran; eval "ran=\$PI_RAN_$func"
476 [ -n "$ran" ] || {
477 export -n "PI_RAN_$func=1"
478 $func "$1" "$2"
479 }
480 done
481 }
482
483 jffs2_ready() {
484 mtdpart="$(find_mtd_part rootfs_data)"
485 [ -z "$mtdpart" ] && return 1
486 magic=$(hexdump $mtdpart -n 4 -e '4/1 "%02x"')
487 [ "$magic" != "deadc0de" ]
488 }
489
490 dupe() { # <new_root> <old_root>
491 cd $1
492 echo -n "creating directories... "
493 {
494 cd $2
495 find . -xdev -type d
496 echo "./dev ./overlay ./mnt ./proc ./tmp"
497 # xdev skips mounted directories
498 cd $1
499 } | xargs mkdir -p
500 echo "done"
501
502 echo -n "setting up symlinks... "
503 for file in $(cd $2; find . -xdev -type f;); do
504 case "$file" in
505 ./rom/note) ;; #nothing
506 ./etc/config*|\
507 ./usr/lib/opkg/info/*) cp -af $2/$file $file;;
508 *) ln -sf /rom/${file#./*} $file;;
509 esac
510 done
511 for file in $(cd $2; find . -xdev -type l;); do
512 cp -af $2/${file#./*} $file
513 done
514 echo "done"
515 }
516
517 pivot() { # <new_root> <old_root>
518 mount -o noatime,move /proc $1/proc && \
519 pivot_root $1 $1$2 && {
520 mount -o noatime,move $2/dev /dev
521 mount -o noatime,move $2/tmp /tmp
522 mount -o noatime,move $2/sys /sys 2>&-
523 mount -o noatime,move $2/overlay /overlay 2>&-
524 return 0
525 }
526 }
527
528 fopivot() { # <rw_root> <ro_root> <dupe?>
529 root=$1
530 {
531 if grep -q overlay /proc/filesystems; then
532 mount -o noatime,lowerdir=/,upperdir=$1 -t overlayfs "overlayfs:$1" /mnt && root=/mnt
533 elif grep -q mini_fo /proc/filesystems; then
534 mount -t mini_fo -o noatime,base=/,sto=$1 "mini_fo:$1" /mnt 2>&- && root=/mnt
535 else
536 mount --bind -o noatime / /mnt
537 mount --bind -o noatime,union "$1" /mnt && root=/mnt
538 fi
539 } || {
540 [ "$3" = "1" ] && {
541 mount | grep "on $1 type" 2>&- 1>&- || mount -o noatime,bind $1 $1
542 dupe $1 $rom
543 }
544 }
545 pivot $root $2
546 }
547
548 ramoverlay() {
549 mkdir -p /tmp/root
550 mount -t tmpfs -o noatime,mode=0755 root /tmp/root
551 fopivot /tmp/root /rom 1
552 }
553
554 [ -z "$IPKG_INSTROOT" -a -f /lib/config/uci.sh ] && . /lib/config/uci.sh