]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - usr.sbin/bsdconfig/share/device.subr
MFC r258458:
[FreeBSD/stable/10.git] / usr.sbin / bsdconfig / share / device.subr
1 if [ ! "$_DEVICE_SUBR" ]; then _DEVICE_SUBR=1
2 #
3 # Copyright (c) 2012-2013 Devin Teske
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 # 1. Redistributions of source code must retain the above copyright
10 #    notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 #    notice, this list of conditions and the following disclaimer in the
13 #    documentation and/or other materials provided with the distribution.
14 #
15 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 # SUCH DAMAGE.
26 #
27 # $FreeBSD$
28 #
29 ############################################################ INCLUDES
30
31 BSDCFG_SHARE="/usr/share/bsdconfig"
32 . $BSDCFG_SHARE/common.subr || exit 1
33 f_dprintf "%s: loading includes..." device.subr
34 f_include $BSDCFG_SHARE/dialog.subr
35 f_include $BSDCFG_SHARE/strings.subr
36 f_include $BSDCFG_SHARE/struct.subr
37
38 BSDCFG_LIBE="/usr/libexec/bsdconfig"
39 f_include_lang $BSDCFG_LIBE/include/messages.subr
40
41 ############################################################ GLOBALS
42
43 DEVICES=
44 DEVICE_NAMES=
45
46 # A "device" from sysinstall's point of view
47 f_struct_define DEVICE \
48         name            \
49         desc            \
50         devname         \
51         type            \
52         capacity        \
53         enabled         \
54         init            \
55         get             \
56         shutdown        \
57         flags           \
58         private         \
59         volume  
60
61 # Network devices have their `private' property set to this
62 f_struct_define DEVICE_INFO \
63         use_rtsol use_dhcp ipaddr ipv6addr netmask extras
64
65 setvar DEVICE_TYPE_NONE         1
66 setvar DEVICE_TYPE_DISK         2
67 setvar DEVICE_TYPE_FLOPPY       3
68 setvar DEVICE_TYPE_FTP          4
69 setvar DEVICE_TYPE_NETWORK      5
70 setvar DEVICE_TYPE_CDROM        6
71 setvar DEVICE_TYPE_USB          7
72 setvar DEVICE_TYPE_DOS          8
73 setvar DEVICE_TYPE_UFS          9
74 setvar DEVICE_TYPE_NFS          10
75 setvar DEVICE_TYPE_ANY          11
76 setvar DEVICE_TYPE_HTTP_PROXY   12
77 setvar DEVICE_TYPE_HTTP         13
78
79 # Network devices have the following flags available
80 setvar IF_ETHERNET      1
81 setvar IF_WIRELESS      2
82 setvar IF_ACTIVE        4
83
84 #
85 # Default behavior is to call f_device_get_all() automatically when loaded.
86 #
87 : ${DEVICE_SELF_SCAN_ALL=1}
88
89 ############################################################ FUNCTIONS
90
91 # f_device_try $name [$i [$var_path]]
92 #
93 # Test a particular device. If $i is given, then $name is expected to contain a
94 # single "%d" where $i will be inserted using printf. If $var_path is given,
95 # it is used as a variable name to provide the caller the device pathname.
96 #
97 # Returns success if the device path exists and is a cdev.
98 #
99 f_device_try()
100 {
101         local name="$1" i="$2" var_path="$3" unit
102         if [ "$i" ]; then
103                 unit=$( printf "$name" "$i" )
104         else
105                 unit="$name"
106         fi
107         case "$unit" in
108         /dev/*) : good ;; # already qualified
109         *) unit="/dev/$unit" ;;
110         esac
111         [ "$var_path" ] && setvar "$var_path" "$unit"
112         f_dprintf "f_device_try: making sure %s is a device node" "$unit"
113         if [ -c "$unit" ]; then
114                 f_dprintf "f_device_try: %s is a cdev [good]" "$unit"
115                 return $SUCCESS
116         else
117                 f_dprintf "f_device_try: %s is not a cdev [skip]" "$unit"
118                 return $FAILURE
119         fi
120 }
121
122 # f_device_register $name $desc $devname $type $enabled $init_function \
123 #                   $get_function $shutdown_function $private $capacity
124 #
125 # Register a device. A `structure' (see struct.subr) is created with the name
126 # device_$name (so make sure $name contains only alpha-numeric characters or
127 # the underscore, `_'). The remaining arguments after $name correspond to the
128 # properties of the `DEVICE' structure-type (defined above).
129 #
130 # If not already registered, the device is then appended to the DEVICES
131 # environment variable, a space-separated list of all registered devices.
132 #
133 f_device_register()
134 {
135         local name="$1" desc="$2" devname="$3" type="$4" enabled="$5"
136         local init_func="$6" get_func="$7" shutdown_func="$8" private="$9"
137         local capacity="${10}"
138
139         f_struct_new DEVICE "device_$name" || return $FAILURE
140         device_$name set name     "$name"
141         device_$name set desc     "$desc"
142         device_$name set devname  "$devname"
143         device_$name set type     "$type"
144         device_$name set enabled  "$enabled"
145         device_$name set init     "$init_func"
146         device_$name set get      "$get_func"
147         device_$name set shutdown "$shutdown_func"
148         device_$name set private  "$private"
149         device_$name set capacity "$capacity"
150
151         # Scan our global register to see if it needs ammending
152         local dev found=
153         for dev in $DEVICES; do
154                 [ "$dev" = "$name" ] || continue
155                 found=1 && break
156         done
157         [ "$found" ] || DEVICES="$DEVICES $name"
158
159         return $SUCCESS
160 }
161
162 # f_device_reset
163 #
164 # Reset the registered device chain.
165 #
166 f_device_reset()
167 {
168         local dev
169         for dev in $DEVICES; do
170                 f_device_shutdown $dev
171
172                 #
173                 # XXX this potentially leaks $dev->private if it's being
174                 # used to point to something dynamic, but you're not supposed
175                 # to call this routine at such times that some open instance
176                 # has its private member pointing somewhere anyway. XXX
177                 #
178                 f_struct_free device_$dev
179         done
180         DEVICES=
181 }
182
183 # f_device_reset_network
184 #
185 # Reset the registered network device chain.
186 #
187 f_device_reset_network()
188 {
189         local dev type private pruned_list=
190         for dev in $DEVICES; do
191                 device_$dev get type type
192                 if [ "$type" != "$DEVICE_TYPE_NETWORK" ]; then
193                         pruned_list="$pruned_list $dev"
194                         continue
195                 fi
196
197                 #
198                 # Leave the device up (don't call shutdown routine)
199                 #
200
201                 # Network devices may have DEVICE_INFO private member
202                 device_$dev get private private
203                 [ "$private" ] && f_struct_free "$private"
204
205                 f_struct_free device_$dev
206         done
207         DEVICES="${pruned_list# }"
208 }
209
210 # f_device_get_all
211 #
212 # Get all device information for devices we have attached.
213 #
214 f_device_get_all()
215 {
216         local devname desc
217
218         f_dprintf "f_device_get_all: Probing devices..."
219         f_dialog_info "$msg_probing_devices_please_wait_this_can_take_a_while"
220
221         # First go for the network interfaces
222         f_device_get_all_network
223
224         # Next, try to find all the types of devices one might use
225         # as a media source for content
226         #
227
228         local dev desc type max n=0
229         for dev in $DEVICE_NAMES; do
230                 n=$(( $n + 1 ))
231                 # Get the desc, type, and max (with debugging disabled)
232                 # NOTE: Bypassing f_device_name_get() for efficiency
233                 # ASIDE: This would be equivalent to the following:
234                 #       debug= f_device_name_get $dev desc
235                 #       debug= f_device_name_get $dev type
236                 #       debug= f_device_name_get $dev max
237                 debug= f_getvar _device_desc$n desc
238                 debug= f_getvar _device_type$n type
239                 debug= f_getvar _device_max$n max
240
241                 local k=0
242                 while [ $k -lt ${max:-0} ]; do
243                         i=$k k=$(( $k + 1 ))
244                         devname=""
245                         case "$type" in
246                         $DEVICE_TYPE_CDROM)
247                                 f_device_try "$dev" "$i" devname || continue
248                                 f_device_register "${devname##*/}" "$desc" \
249                                         "$devname" $DEVICE_TYPE_CDROM 1 \
250                                         f_media_init_cdrom f_media_get_cdrom \
251                                         f_media_shutdown_cdrom "" \
252                                         "$( f_device_capacity "$devname" )"
253                                 f_dprintf "Found a CDROM device for %s" \
254                                           "$devname"
255                                 ;;
256                         $DEVICE_TYPE_FLOPPY)
257                                 f_device_try "$dev" "$i" devname || continue
258                                 f_device_register "${devname##*/}" "$desc" \
259                                         "$devname" $DEVICE_TYPE_FLOPPY 1 \
260                                         f_media_init_floppy \
261                                         f_media_get_floppy \
262                                         f_media_shutdown_floppy "" \
263                                         "$( f_device_capacity "$devname" )"
264                                 f_dprintf "Found a floppy device for %s" \
265                                           "$devname"
266                                 ;;
267                         $DEVICE_TYPE_USB)
268                                 f_device_try "$dev" "$i" devname || continue
269                                 f_device_register "${devname##*/}" "$desc" \
270                                         "$devname" $DEVICE_TYPE_USB 1 \
271                                         f_media_init_usb f_media_get_usb \
272                                         f_media_shutdown_usb "" \
273                                         "$( f_device_capacity "$devname" )"
274                                 f_dprintf "Found a USB disk for %s" "$devname"
275                                 ;;
276                         esac
277                 done
278         done
279
280         # Register ISO9660 providers as CDROM devices
281         for devname in /dev/iso9660/*; do
282                 f_device_try "$devname" || continue
283                 f_device_register "${devname##*/}" "ISO9660 file system" \
284                         "$devname" $DEVICE_TYPE_CDROM 1 \
285                         f_media_init_cdrom f_media_get_cdrom \
286                         f_media_shutdown_cdrom "" \
287                         "$( f_device_capacity "$devname" )"
288                 f_dprintf "Found a CDROM device for %s" "$devname"
289         done
290
291         # Scan for mdconfig(8)-created md(4) devices
292         local filename
293         for devname in /dev/md[0-9] /dev/md[0-9][0-9]; do
294                 f_device_try "$devname" || continue
295
296                 # See if the md(4) device is a vnode type backed by a file
297                 filename=$( sysctl kern.geom.conftxt |
298                         awk -v devname="${devname##*/}" \
299                         '
300                                 ( $2 == "MD" ) && \
301                                 ( $3 == devname ) && \
302                                 ( $(NF-2) == "vnode" ) && \
303                                 ( $(NF-1) == "file" ) \
304                                 {
305                                         print $NF
306                                 }
307                         ' )
308                 case "$filename" in
309                 *.iso) # Register the device as an ISO9660 provider
310                         f_device_register "${devname##*/}" \
311                                 "md(4) vnode file system" \
312                                 "$devname" $DEVICE_TYPE_CDROM 1 \
313                                 f_media_init_cdrom f_media_get_cdrom \
314                                 f_media_shutdown_cdrom "" \
315                                 "$( f_device_capacity "$devname" )"
316                         f_dprintf "Found a CDROM device for %s" "$devname"
317                         ;;
318                 esac
319         done
320
321         # Finally go get the disks and look for partitions to register
322         local diskname slices index type rest slice part
323         for diskname in $( sysctl -n kern.disks ); do
324
325                 case "$diskname" in
326                 cd*)
327                         # XXX
328                         #  Due to unknown reasons, kern.disks returns SCSI
329                         # CDROM as a valid disk. This will prevent bsdconfig
330                         # from presenting SCSI CDROMs as available disks in
331                         # various menus. Why GEOM treats SCSI CDROM as a disk
332                         # is beyond me and that should be investigated.
333                         # For temporary workaround, ignore SCSI CDROM device.
334                         #
335                         continue ;;
336                 esac
337
338                 # Try to create a list of partitions and their types,
339                 # consisting of "N,typeN ..." (e.g., "1,0xa5 2,0x06").
340                 if ! slices=$( fdisk -p "$diskname" 2> /dev/null |
341                         awk '( $1 == "p" ) { print $2","$3 }' )
342                 then
343                         f_dprintf "Unable to open disk %s" "$diskname"
344                         continue
345                 fi
346
347                 # Try and find its description
348                 f_device_desc "$diskname" $DEVICE_TYPE_DISK desc
349
350                 f_device_register "$diskname" "$desc" \
351                                   "/dev/$diskname" $DEVICE_TYPE_DISK 0 \
352                                   "" "" "" "" \
353                                   "$( f_device_capacity "$diskname" )"
354                 f_dprintf "Found a disk device named %s" "$diskname"
355
356                 # Look for existing partitions to register
357                 for slice in $slices; do
358                         index="${slice%%,*}" type="${slice#*,}"
359                         slice=${diskname}s$index
360                         case "$type" in
361                         0x01|0x04|0x06|0x0b|0x0c|0x0e|0xef)
362                                 # DOS partitions to add as "DOS media devices"
363                                 f_device_register "$slice" "" \
364                                         "/dev/$slice" $DEVICE_TYPE_DOS 1 \
365                                         f_media_init_dos f_media_get_dos \
366                                         f_media_shutdown_dos "" \
367                                         "$( f_device_capacity "/dev/$slice" )"
368                                 f_dprintf "Found a DOS partition %s" "$slice"
369                                 ;;
370                         0xa5) # FreeBSD partition
371                                 for part in $(
372                                         bsdlabel -r $slice 2> /dev/null |
373                                                 awk -v slice="$slice" '
374                                                 ( $1 ~ /[abdefgh]:/ ) {
375                                                         printf "%s%s\n",
376                                                                slice,
377                                                                substr($1,1,1)
378                                                 }'
379                                 ); do
380                                         f_quietly dumpfs -m /dev/$part ||
381                                                 continue
382                                         f_device_register \
383                                                 "$part" "" "/dev/$part" \
384                                                 $DEVICE_TYPE_UFS 1 \
385                                                 f_media_init_ufs \
386                                                 f_media_get_ufs \
387                                                 f_media_shutdown_ufs "" \
388                                                 "$( f_device_capacity \
389                                                         "$/dev/$part" )"
390                                         f_dprintf "Found a UFS partition %s" \
391                                                   "$part"
392                                 done # parts
393                                 ;;
394                         esac
395                 done # slices
396
397         done # disks
398 }
399
400 # f_device_get_all_network
401 #
402 # Get all network device information for attached network devices.
403 #
404 f_device_get_all_network()
405 {
406         local devname desc flags
407         for devname in $( ifconfig -l ); do
408                 # Eliminate network devices that don't make sense
409                 case "$devname" in
410                 lo*) continue ;;
411                 esac
412
413                 # Try and find its description
414                 f_device_desc "$devname" $DEVICE_TYPE_NETWORK desc
415
416                 f_dprintf "Found a network device named %s" "$devname"
417                 f_device_register $devname \
418                         "$desc" "$devname" $DEVICE_TYPE_NETWORK 1 \
419                         f_media_init_network "" f_media_shutdown_network "" -1
420
421                 # Set flags based on media and status
422                 flags=0
423                 eval "$( ifconfig $devname 2> /dev/null | awk -v var=flags '
424                 function _or(var, mask) {
425                         printf "%s=$(( $%s | $%s ))\n", var, var, mask
426                 }
427                 BEGIN { S = "[[:space:]]+" }
428                 {
429                         if (!match($0, "^" S "(media|status):" S)) next
430                         value = substr($0, RLENGTH + 1)
431                         if ($1 == "media:") {
432                                 if (value ~ /Ethernet/) _or(var, "IF_ETHERNET")
433                                 if (value ~ /802\.11/) _or(var, "IF_WIRELESS")
434                         } else if ($1 == "status:") {
435                                 if (value ~ /^active/) _or(var, "IF_ACTIVE")
436                         }
437                 }' )"
438                 device_$devname set flags $flags
439         done
440 }
441
442 # f_device_name_get $type $name type|desc|max [$var_to_set]
443 #
444 # Fetch the device type (type), description (desc), or maximum number of
445 # devices to scan for (max) associated with device $name and $type. If $type is
446 # either NULL, missing, or set to $DEVICE_TYPE_ANY then only $name is used.
447 # Returns success if a match was found, otherwise failure.
448 #
449 # If $var_to_set is missing or NULL, the device name is printed to standard out
450 # for capturing in a sub-shell (which is less-recommended because of
451 # performance degredation; for example, when called in a loop).
452 #
453 f_device_name_get()
454 {
455         local __type="$1" __name="$2" __prop="$3" __var_to_set="$4"
456         local __dev __devtype __n=0
457
458         # Return failure if no $name or $prop is an unknown property
459         [ "$__name" ] || return $FAILURE
460         case "$__prop" in type|desc|max) : good ;;
461         *) return $FAILURE; esac
462
463         #
464         # Attempt to create an alternate-form of $__name that contains the
465         # first contiguous string of numbers replaced with `%d' for comparison
466         # against stored pattern names (see MAIN).
467         #
468         local __left="${__name%%[0-9]*}" __right="${__name#*[0-9]}" __dname=
469         if [ "$__left" != "$__name" ]; then
470                 # Chop leading digits from right 'til we hit first non-digit
471                 while :; do
472                         case "$__right" in
473                         [0-9]*) __right="${__right#[0-9]}" ;;
474                              *) break
475                         esac
476                 done
477                 __dname="${__left}%d$__right"
478         fi
479
480         [ "$__type" = "$DEVICE_TYPE_ANY" ] && __type=
481         for __dev in $DEVICE_NAMES; do
482                 __n=$(( $__n + 1 ))
483                 [ "$__dev" = "$__name" -o "$__dev" = "$__dname" ] || continue
484                 f_getvar _device_type$__n __devtype
485                 [ "${__type:-$__devtype}" = "$__devtype" ] || continue
486                 f_getvar _device_$__prop$__n $__var_to_set
487                 return $?
488         done
489         return $FAILURE
490 }
491
492 # f_device_name_set $type $name $desc [$max]
493 #
494 # Store a description (desc) and [optionally] maximum number of devices to scan
495 # for (max) in-association with device $type and $name. Returns success unless
496 # $name is NULL or missing. Use the f_device_name_get() routine with the same
497 # $name and optionally $type to retrieve one of type, desc, or max properties.
498 #
499 f_device_name_set()
500 {
501         local type="$1" name="$2" desc="$3" max="$4"
502         local dev devtype n=0 found=
503         [ "$name" ] || return $FAILURE
504         for dev in $DEVICE_NAMES; do
505                 n=$(( $n + 1 ))
506                 [ "$dev" = "$name" ] || continue
507                 if f_getvar _device_type$n devtype; then
508                         # Allow multiple entries with same name but diff type
509                         [ "$devtype" = "$type" ] || continue
510                 fi
511                 found=1 && break
512         done
513         if [ ! "$found" ]; then
514                 DEVICE_NAMES="$DEVICE_NAMES $name"
515                 n=$(( $n + 1 ))
516         fi
517         setvar _device_type$n "$type"
518         setvar _device_desc$n "$desc"
519         [ "${4+set}" ] && setvar _device_max$n "$max"
520         return $SUCCESS
521 }
522
523 # f_device_desc $device_name $device_type [$var_to_set]
524 #
525 # Print a description for a device name (eg., `fxp0') given a specific device
526 # type/class.
527 #
528 # If $var_to_set is missing or NULL, the device description is printed to
529 # standard out for capturing in a sub-shell (which is less-recommended because
530 # of performance degredation; for example, when called in a loop).
531 #
532 f_device_desc()
533 {
534         local __name="$1" __type="$2" __var_to_set="$3"
535         local __devname __devunit __cp
536
537         # Check variables
538         [ "$__name" ] || return $SUCCESS
539         [ "$__type" = "$DEVICE_TYPE_ANY" ] && type=
540         [ "$__var_to_set" ] && { setvar "$__var_to_set" "" || return; }
541
542         #
543         # Return sysctl MIB dev.NAME.UNIT.%desc if it exists,
544         # otherwise fall through to below static list.
545         #
546         if f_have sysctl; then
547                 __devname="${__name%%[0-9]*}"
548                 __devunit="${__name#$__devname}"
549                 __devunit="${__devunit%%[!0-9]*}"
550                 if [ "$__var_to_set" ]; then
551                         if __cp=$(
552                                 sysctl -n "dev.$__devname.$__devunit.%desc" \
553                                 2> /dev/null
554                         ); then
555                                 setvar "$__var_to_set" "$__cp" &&
556                                         return $SUCCESS
557                         fi
558                 else
559                         sysctl -n "dev.$__devname.$__devunit.%desc" \
560                                 2> /dev/null && return $SUCCESS
561                 fi
562         fi
563
564         #
565         # For disks, attempt to return camcontrol(8) descriptions.
566         # Otherwise fall through to below static list.
567         #
568         f_have camcontrol &&
569         [ "${__type:-$DEVICE_TYPE_DISK}" = "$DEVICE_TYPE_DISK" ] &&
570         __cp=$( camcontrol devlist 2> /dev/null | awk -v disk="$__name" '
571                 $0~"(\\(|,)"disk"(,|\\))" {
572                         if (!match($0, "<[^>]+>")) next
573                         print substr($0, RSTART+1, RLENGTH-2)
574                         found = 1
575                         exit
576                 }
577                 END { exit ! found }
578         ' ) && setvar "$__var_to_set" "$__cp" && return $SUCCESS
579
580         #
581         # Attempt to create an alternate-form of $__name that contains the
582         # first contiguous string of numbers replaced with `%d' for comparison
583         # against stored pattern names (see MAIN).
584         #
585         local __left="${__name%%[0-9]*}" __right="${__name#*[0-9]}" __dname=
586         if [ "$__left" != "$__name" ]; then
587                 # Chop leading digits from right 'til we hit first non-digit
588                 while :; do
589                         case "$__right" in
590                         [0-9]*) __right="${__right#[0-9]}" ;;
591                              *) break
592                         esac
593                 done
594                 __dname="${__left}%d$__right"
595         fi
596
597         local __dev __devtype __n=0
598         for __dev in $DEVICE_NAMES; do
599                 __n=$(( $__n + 1 ))
600                 debug= f_getvar _device_type$__n __devtype
601                 [ "${__type:-$__devtype}" = "$__devtype" ] || continue
602                 if [ "$__devtype" = "$DEVICE_TYPE_NETWORK" ]; then
603                         __devname=$( f_substr "$__name" 0 ${#__dev} )
604                         [ "$__devname" = "$__dev" ] || continue
605                 else
606                         [ "$__dev" = "$__name" -o "$__dev" = "$__dname" ] ||
607                                 continue
608                 fi
609                 debug= f_getvar _device_desc$__n $__var_to_set
610                 return $?
611         done
612
613         #
614         # Sensible fall-backs for specific types
615         #
616         case "$__type" in
617         $DEVICE_TYPE_CDROM)   __cp="<unknown cdrom device type>" ;;
618         $DEVICE_TYPE_DISK)    __cp="<unknown disk device type>" ;;
619         $DEVICE_TYPE_FLOPPY)  __cp="<unknown floppy device type>" ;;
620         $DEVICE_TYPE_USB)     __cp="<unknown usb storage device type>" ;;
621         $DEVICE_TYPE_NETWORK) __cp="<unknown network interface type>" ;;
622         *)
623                 __cp="<unknown device type>"
624         esac
625
626         if [ "$__var_to_set" ]; then
627                 setvar "$__var_to_set" "$__cp"
628         else
629                 echo "$__cp"
630         fi
631
632         return $FAILURE
633 }
634
635 # f_device_is_ethernet $device
636 #
637 # Returns true if $device is a wired Ethernet network interface. Otherwise
638 # returns false. Example wired interfaces include: fxp0 em0 bge0 rl0 etc.
639 #
640 f_device_is_ethernet()
641 {
642         local dev="$1" type flags
643
644         # Make sure we have an actual device by that name
645         f_struct "device_$dev" || return $FAILURE
646
647         # Make sure that the device is a network device
648         device_$dev get type type
649         [ "$type" = "$DEVICE_TYPE_NETWORK" ] || return $FAILURE
650
651         # Make sure that the media flags indicate that it is Ethernet
652         device_$dev get flags flags
653         [ $(( ${flags:-0} & $IF_ETHERNET )) -eq $IF_ETHERNET ]
654 }
655
656 # f_device_is_wireless $device
657 #
658 # Returns true if $device is a Wireless network interface. Otherwise returns
659 # false. Examples of wireless interfaces include: iwn0
660 #
661 f_device_is_wireless()
662 {
663         local dev="$1" type flags
664
665         # Make sure we have an actual device by that name
666         f_struct "device_$dev" || return $FAILURE
667
668         # Make sure that the device is a network device
669         device_$dev get type type
670         [ "$type" = "$DEVICE_TYPE_NETWORK" ] || return $FAILURE
671
672         # Make sure that the media flags indicate that it is Ethernet
673         device_$dev get flags flags
674         [ $(( ${flags:-0} & $IF_WIRELESS )) -eq $IF_WIRELESS ]
675 }
676
677 # f_device_is_active $device
678 #
679 # Returns true if $device is active. Otherwise returns false. Currently this
680 # only works for network interfaces.
681 #
682 f_device_is_active()
683 {
684         local dev="$1" type flags=0
685
686         # Make sure we have an actual device by that name
687         f_struct "device_$dev" || return $FAILURE
688
689         device_$dev get type type
690         case "$type" in
691         $DEVICE_TYPE_NETWORK)
692                 # Make sure that the media flags indicate that it is active
693                 device_$dev get flags flags
694                 [ $(( ${flags:-0} & $IF_ACTIVE )) -eq $IF_ACTIVE ]
695                 ;;
696         *)
697                 return $FAILURE
698         esac
699 }
700
701 # f_device_rescan
702 #
703 # Rescan all devices, after closing previous set - convenience function.
704 #
705 f_device_rescan()
706 {
707         f_device_reset
708         f_device_get_all
709 }
710
711 # f_device_rescan_network
712 #
713 # Rescan all network devices, after closing previous set - for convenience.
714 #
715 f_device_rescan_network()
716 {
717         f_device_reset_network
718         f_device_get_all_network
719 }
720
721 # f_device_find $name [$type [$var_to_set]] 
722 #
723 # Find one or more registered devices by name, type, or both. Returns a space-
724 # separated list of devices matching the search criterion.
725 #
726 # If $var_to_set is missing or NULL, the device name(s) are printed to standard
727 # out for capturing in a sub-shell (which is less-recommended because of
728 # performance degredation; for example, when called in a loop).
729 #
730 f_device_find()
731 {
732         local __name="$1" __type="${2:-$DEVICE_TYPE_ANY}" __var_to_set="$3"
733         local __dev __devname __devtype __found=
734         for __dev in $DEVICES; do
735                 device_$__dev get name __devname
736                 device_$__dev get type __devtype
737                 if [ "$__name" = "$__devname" -o ! "$__name" ] &&
738                    [ "$__type" = "$DEVICE_TYPE_ANY" -o \
739                      "$__type" = "$__devtype" ]
740                 then
741                         __found="$__found $__dev"
742                 fi
743         done
744         if [ "$__var_to_set" ]; then
745                 setvar "$__var_to_set" "${__found# }"
746         else
747                 echo $__found
748         fi
749         [ "$__found" ] # Return status
750 }
751
752 # f_device_init $name
753 #
754 # Initialize a device by evaluating its `init' function.
755 #
756 f_device_init()
757 {
758         local name="$1" init_func
759         device_$name get init init_func || return $?
760         ${init_func:-:} $name
761 }
762
763 # f_device_get $name $file [$probe]
764 #
765 # Read $file by evaluating the device's `get' function. The file is commonly
766 # produced on standard output (but it truly depends on the function called).
767 #
768 f_device_get()
769 {
770         local name="$1" file="$2" probe="$3" get_func
771         device_$name get get get_func || return $?
772         ${get_func:-:} $name "$file" ${3+"$probe"}
773 }
774
775 # f_device_shutdown $name
776 #
777 # Shutdown a device by evaluating its `shutdown' function.
778 #
779 f_device_shutdown()
780 {
781         local name="$1" shutdown_func
782         device_$name get shutdown shutdown_func || return $?
783         ${shutdown_func:-:} $name
784 }
785
786 # f_device_menu $title $prompt $hline $device_type [$helpfile]
787 #
788 # Display a menu listing all the devices of a certain type in the system.
789 #
790 f_device_menu()
791 {
792         f_dialog_title "$1"
793         local title="$DIALOG_TITLE" btitle="$DIALOG_BACKTITLE"
794         f_dialog_title_restore
795
796         local prompt="$2" hline="$3" type="$4" helpfile="$5"
797
798         local dev devtype devs=
799         for dev in $DEVICES; do
800                 device_$dev get type devtype || continue
801                 [ "$devtype" = "$type" ] || continue
802                 devs="$devs $dev"
803         done
804         [ "$devs" ] || return $DIALOG_CANCEL
805
806         local desc menu_list=
807         for dev in $devs; do
808                 device_$dev get desc desc
809                 f_shell_escape "$desc" desc
810                 menu_list="$menu_list '$dev' '$desc'"
811         done
812
813         local height width rows
814         eval f_dialog_menu_size height width rows \
815                                 \"\$title\"  \
816                                 \"\$btitle\" \
817                                 \"\$prompt\" \
818                                 \"\$hline\"  \
819                                 $menu_list
820
821         local errexit=
822         case $- in *e*) errexit=1; esac
823         set +e
824
825         local mtag
826         while :; do
827                 mtag=$( eval $DIALOG \
828                         --title \"\$title\"             \
829                         --backtitle \"\$btitle\"        \
830                         --ok-label \"\$msg_ok\"         \
831                         --cancel-label \"\$msg_cancel\" \
832                         ${helpfile:+                    \
833                           --help-button                 \
834                           --help-label \"\$msg_help\"   \
835                           ${USE_XDIALOG:+--help \"\"}   \
836                         }                               \
837                         --menu \"\$prompt\"             \
838                         $height $width $rows            \
839                         $menu_list                      \
840                         2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
841                 )
842                 local retval=$?
843
844                 [ $retval -ne $DIALOG_HELP ] && break
845                         # Otherwise, the Help button was pressed
846                 f_show_help "$helpfile"
847                         # ...then loop back to menu
848         done
849         f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
850
851         [ "$errexit" ] && set -e
852
853         if [ $retval -eq $DIALOG_OK ]; then
854                 # Clean up the output of [X]dialog(1) and return it
855                 f_dialog_data_sanitize mtag
856                 echo "$mtag" >&2
857         fi
858
859         return $retval
860 }
861
862 # f_device_capacity $device [$var_to_set]
863 #
864 # Return the capacity of $device in bytes.
865 #
866 f_device_capacity()
867 {
868         local __dev="$1" __var_to_set="$2"
869         local __bytes
870
871         __bytes=$( diskinfo -v "$__dev" 2> /dev/null |
872                 awk '/# mediasize in bytes/{print $1}' ) || __bytes=-1
873
874         if [ "$__var_to_set" ]; then
875                 setvar "$__var_to_set" "$__bytes"
876         else
877                 echo "$__bytes"
878         fi
879 }
880
881 #
882 # Short-hand
883 #
884 f_cdrom()   {  f_device_name_set $DEVICE_TYPE_CDROM   "$1" "$2" "$3";  }
885 f_disk()    {  f_device_name_set $DEVICE_TYPE_DISK    "$1" "$2" "$3";  }
886 f_floppy()  {  f_device_name_set $DEVICE_TYPE_FLOPPY  "$1" "$2" "$3";  }
887 f_serial()  {  f_device_name_set $DEVICE_TYPE_NETWORK "$1" "$2" "$3";  }
888 f_usb()     {  f_device_name_set $DEVICE_TYPE_USB     "$1" "$2" "$3";  }
889 f_network() {  f_device_name_set $DEVICE_TYPE_NETWORK "$1" "$2";       }
890
891 ############################################################ MAIN
892
893 # CDROM, Disk, Floppy, Serial, and USB devices/names
894 f_cdrom  "cd%d"   "SCSI CDROM drive"                  4
895 f_cdrom  "mcd%d"  "Mitsumi (old model) CDROM drive"   4
896 f_cdrom  "scd%d"  "Sony CDROM drive - CDU31/33A type" 4
897 f_disk   "aacd%d" "Adaptec FSA RAID array"            4
898 f_disk   "ada%d"  "ATA/SATA disk device"              16
899 f_disk   "amrd%d" "AMI MegaRAID drive"                4
900 f_disk   "da%d"   "SCSI disk device"                  16
901 f_disk   "idad%d" "Compaq RAID array"                 4
902 f_disk   "ipsd%d" "IBM ServeRAID RAID array"          4
903 f_disk   "mfid%d" "LSI MegaRAID SAS array"            4
904 f_disk   "mlxd%d" "Mylex RAID disk"                   4
905 f_disk   "twed%d" "3ware ATA RAID array"              4
906 f_disk   "vtbd%d" "VirtIO Block Device"               16
907 f_floppy "fd%d"   "Floppy Drive unit A"               4
908 f_serial "cuau%d" "%s on device %s (COM%d)"           16
909 f_usb    "da%da"  "USB Mass Storage Device"           16
910
911 # Network interfaces/names
912 f_network "ae"    "Attansic/Atheros L2 Fast Ethernet"
913 f_network "age"   "Attansic/Atheros L1 Gigabit Ethernet"
914 f_network "alc"   "Atheros AR8131/AR8132 PCIe Ethernet"
915 f_network "ale"   "Atheros AR8121/AR8113/AR8114 PCIe Ethernet"
916 f_network "an"    "Aironet 4500/4800 802.11 wireless adapter"
917 f_network "ath"   "Atheros IEEE 802.11 wireless adapter"
918 f_network "aue"   "ADMtek USB Ethernet adapter"
919 f_network "axe"   "ASIX Electronics USB Ethernet adapter"
920 f_network "bce"   "Broadcom NetXtreme II Gigabit Ethernet card"
921 f_network "bfe"   "Broadcom BCM440x PCI Ethernet card"
922 f_network "bge"   "Broadcom BCM570x PCI Gigabit Ethernet card"
923 f_network "bm"    "Apple BMAC Built-in Ethernet"
924 f_network "bwn"   "Broadcom BCM43xx IEEE 802.11 wireless adapter"
925 f_network "cas"   "Sun Cassini/Cassini+ or NS DP83065 Saturn Ethernet"
926 f_network "cc3i"  "SDL HSSI sync serial PCI card"
927 f_network "cue"   "CATC USB Ethernet adapter"
928 f_network "cxgb"  "Chelsio T3 10Gb Ethernet card"
929 f_network "dc"    "DEC/Intel 21143 (and clones) PCI Fast Ethernet card"
930 f_network "de"    "DEC DE435 PCI NIC or other DC21040-AA based card"
931 f_network "disc"  "Software discard network interface"
932 f_network "ed"    "Novell NE1000/2000; 3C503; NE2000-compatible PCMCIA"
933 f_network "el"    "3Com 3C501 Ethernet card"
934 f_network "em"    "Intel(R) PRO/1000 Ethernet card"
935 f_network "en"    "Efficient Networks ATM PCI card"
936 f_network "ep"    "3Com 3C509 Ethernet card/3C589 PCMCIA"
937 f_network "et"    "Agere ET1310 based PCI Express Gigabit Ethernet card"
938 f_network "ex"    "Intel EtherExpress Pro/10 Ethernet card"
939 f_network "fe"    "Fujitsu MB86960A/MB86965A Ethernet card"
940 f_network "fpa"   "DEC DEFPA PCI FDDI card"
941 f_network "fwe"   "FireWire Ethernet emulation"
942 f_network "fwip"  "IP over FireWire"
943 f_network "fxp"   "Intel EtherExpress Pro/100B PCI Fast Ethernet card"
944 f_network "gem"   "Apple GMAC or Sun ERI/GEM Ethernet adapter"
945 f_network "hme"   "Sun HME (Happy Meal Ethernet) Ethernet adapter"
946 f_network "ie"    "AT&T StarLAN 10 and EN100; 3Com 3C507; NI5210"
947 f_network "igb"   "Intel(R) PRO/1000 PCI Express Gigabit Ethernet card"
948 f_network "ipw"   "Intel PRO/Wireless 2100 IEEE 802.11 adapter"
949 f_network "iwi"   "Intel PRO/Wireless 2200BG/2225BG/2915ABG adapter"
950 f_network "iwn"   "Intel Wireless WiFi Link 4965AGN IEEE 802.11n adapter"
951 f_network "ixgbe" "Intel(R) PRO/10Gb Ethernet card"
952 f_network "ixgb"  "Intel(R) PRO/10Gb Ethernet card"
953 f_network "ix"    "Intel Etherexpress Ethernet card"
954         # Maintain sequential order of above(3): ixgbe ixgb ix
955 f_network "jme"   "JMicron JMC250 Gigabit/JMC260 Fast Ethernet"
956 f_network "kue"   "Kawasaki LSI USB Ethernet adapter"
957 f_network "le"    "AMD Am7900 LANCE or Am79C9xx PCnet Ethernet adapter"
958 f_network "lge"   "Level 1 LXT1001 Gigabit Ethernet card"
959 f_network "lnc"   "Lance/PCnet (Isolan/Novell NE2100/NE32-VL) Ethernet"
960 f_network "lo"    "Loop-back (local) network interface"
961 f_network "lp"    "Parallel Port IP (PLIP) peer connection"
962 f_network "malo"  "Marvell Libertas 88W8335 802.11 wireless adapter"
963 f_network "msk"   "Marvell/SysKonnect Yukon II Gigabit Ethernet"
964 f_network "mxge"  "Myricom Myri10GE 10Gb Ethernet card"
965 f_network "nfe"   "NVIDIA nForce MCP Ethernet"
966 f_network "nge"   "NatSemi PCI Gigabit Ethernet card"
967 f_network "ng"    "Vimage netgraph(4) bridged Ethernet device"
968         # Maintain sequential order of above(2): nge ng
969 f_network "nve"   "NVIDIA nForce MCP Ethernet"
970 f_network "nxge"  "Neterion Xframe 10GbE Server/Storage adapter"
971 f_network "pcn"   "AMD Am79c79x PCI Ethernet card"
972 f_network "plip"  "Parallel Port IP (PLIP) peer connection"
973 f_network "ral"   "Ralink Technology IEEE 802.11 wireless adapter"
974 f_network "ray"   "Raytheon Raylink 802.11 wireless adapter"
975 f_network "re"    "RealTek 8139C+/8169/8169S/8110S PCI Ethernet adapter"
976 f_network "rl"    "RealTek 8129/8139 PCI Ethernet card"
977 f_network "rue"   "RealTek USB Ethernet card"
978 f_network "rum"   "Ralink Technology USB IEEE 802.11 wireless adapter"
979 f_network "sf"    "Adaptec AIC-6915 PCI Ethernet card"
980 f_network "sge"   "Silicon Integrated Systems SiS190/191 Ethernet"
981 f_network "sis"   "SiS 900/SiS 7016 PCI Ethernet card"
982 f_network "sk"    "SysKonnect PCI Gigabit Ethernet card"
983 f_network "snc"   "SONIC Ethernet card"
984 f_network "sn"    "SMC/Megahertz Ethernet card"
985         # Maintain sequential order of above(2): snc sn
986 f_network "sr"    "SDL T1/E1 sync serial PCI card"
987 f_network "ste"   "Sundance ST201 PCI Ethernet card"
988 f_network "stge"  "Sundance/Tamarack TC9021 Gigabit Ethernet"
989 f_network "ti"    "Alteon Networks PCI Gigabit Ethernet card"
990 f_network "tl"    "Texas Instruments ThunderLAN PCI Ethernet card"
991 f_network "txp"   "3Com 3cR990 Ethernet card"
992 f_network "tx"    "SMC 9432TX Ethernet card"
993         # Maintain sequential order of above(2): txp tx
994 f_network "uath"  "Atheros AR5005UG and AR5005UX USB wireless adapter"
995 f_network "upgt"  "Conexant/Intersil PrismGT USB wireless adapter"
996 f_network "ural"  "Ralink Technology RT2500USB 802.11 wireless adapter"
997 f_network "urtw"  "Realtek 8187L USB wireless adapter"
998 f_network "vge"   "VIA VT612x PCI Gigabit Ethernet card"
999 f_network "vlan"  "IEEE 802.1Q VLAN network interface"
1000 f_network "vr"    "VIA VT3043/VT86C100A Rhine PCI Ethernet card"
1001 f_network "vx"    "3COM 3c590 / 3c595 Ethernet card"
1002 f_network "wb"    "Winbond W89C840F PCI Ethernet card"
1003 f_network "wi"    "Lucent WaveLAN/IEEE 802.11 wireless adapter"
1004 f_network "wpi"   "Intel 3945ABG IEEE 802.11 wireless adapter"
1005 f_network "wx"    "Intel Gigabit Ethernet (82452) card"
1006 f_network "xe"    "Xircom/Intel EtherExpress Pro100/16 Ethernet card"
1007 f_network "xl"    "3COM 3c90x / 3c90xB PCI Ethernet card"
1008 f_network "zyd"   "ZyDAS ZD1211/ZD1211B USB 802.11 wireless adapter"
1009
1010 f_dprintf "%s: Initialized %u known device names/descriptions." device.subr \
1011           "$( set -- $DEVICE_NAMES; echo $# )"
1012
1013 #
1014 # Scan for the above devices unless requeted otherwise
1015 #
1016 f_dprintf "%s: DEVICE_SELF_SCAN_ALL=[%s]" device.subr "$DEVICE_SELF_SCAN_ALL"
1017 case "$DEVICE_SELF_SCAN_ALL" in
1018 ""|0|[Nn][Oo]|[Oo][Ff][Ff]|[Ff][Aa][Ll][Ss][Ee]) : do nothing ;;
1019 *) f_device_get_all
1020 esac
1021
1022 f_dprintf "%s: Successfully loaded." device.subr
1023
1024 fi # ! $_DEVICE_SUBR