You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. #!/bin/bash
  2. #
  3. # Provides functions for firmware data related actions
  4. #
  5. # Copyright (C) 2009, Niall Walsh <niallwalsh@users.berlios.de>
  6. # Derived from prior aptosid-scripts/fw-detect which is
  7. # Copyright 2007, Kel Modderman <kel@otaku42.de>
  8. #
  9. # This program is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU General Public License
  11. # as published by the Free Software Foundation; version 2 of the
  12. # License.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. . /usr/share/siduction-scripts/fw-data.sh
  20. function driver_firmware() {
  21. local dnum=0
  22. local test_driver
  23. local driver="${1}"
  24. for test_driver in ${FW_CARDS_FW_DEV_STR[@]} ; do
  25. case "${driver}" in
  26. ${test_driver})
  27. echo ${FW_CARDS_FW_DEV_NUM[${dnum}]}
  28. return
  29. ;;
  30. esac
  31. dnum=$((${dnum}+1))
  32. done
  33. }
  34. function firmware_needed() {
  35. local required=$(driver_firmware ${1})
  36. [ -n "${required}" ] && \
  37. echo "${FW_CARDS_FW[${required}]}"
  38. }
  39. function show_all_cards() {
  40. echo ${FW_CARDS[@]}
  41. }
  42. function show_all_detail() {
  43. i=0
  44. for card in ${FW_CARDS[@]} ; do
  45. echo "${FW_CARDS[$i]}"
  46. [ -n "${FW_CARDS_FW[$i]}" ] && echo " ${FW_CARDS_FW[$i]}"
  47. [ -n "${FW_CARDS_FW_URL[$i]}" ] && echo " url ${FW_CARDS_FW_URL[$i]}"
  48. [ -n "${FW_CARDS_FW_FILE[$i]}" ] && echo " file ${FW_CARDS_FW_FILE[$i]}"
  49. [ -n "${FW_CARDS_FW_OK[$i]}" ] && echo " test ${FW_CARDS_FW_OK[$i]}"
  50. local patterns=0
  51. local pnum=0
  52. local num
  53. for num in ${FW_CARDS_FW_DEV_NUM[@]} ; do
  54. if [ "${num}" = "$i" ] ; then
  55. if [ $patterns -eq 0 ]; then
  56. patterns=1
  57. echo -n " drivers "
  58. else
  59. echo -n " "
  60. fi
  61. echo -n "${FW_CARDS_FW_DEV_STR[$pnum]}"
  62. fi
  63. pnum=$((pnum+1))
  64. done
  65. if [ $patterns -gt 0 ] ; then
  66. echo ""
  67. fi
  68. i=$((i+1))
  69. done
  70. }
  71. function show_ini() {
  72. i=0
  73. for card in ${FW_CARDS[@]} ; do
  74. echo "[${FW_CARDS[$i]}]"
  75. [ -n "${FW_CARDS_FW[$i]}" ] && echo "fw=\"${FW_CARDS_FW[$i]}\""
  76. [ -n "${FW_CARDS_FW_URL[$i]}" ] && echo "url=\"${FW_CARDS_FW_URL[$i]}\""
  77. [ -n "${FW_CARDS_FW_FILE[$i]}" ] && echo "file=\"${FW_CARDS_FW_FILE[$i]}\""
  78. [ -n "${FW_CARDS_FW_OK[$i]}" ] && echo "ok=\"${FW_CARDS_FW_OK[$i]}\""
  79. local patterns=0
  80. local pnum=0
  81. local num
  82. for num in ${FW_CARDS_FW_DEV_NUM[@]} ; do
  83. if [ "${num}" = "$i" ] ; then
  84. if [ $patterns -eq 0 ]; then
  85. patterns=1
  86. echo -n 'drivers="'
  87. else
  88. echo -n ' '
  89. fi
  90. echo -n "${FW_CARDS_FW_DEV_STR[$pnum]}"
  91. fi
  92. pnum=$((pnum+1))
  93. done
  94. if [ $patterns -gt 0 ] ; then
  95. echo '"'
  96. fi
  97. echo ""
  98. i=$((i+1))
  99. done
  100. }
  101. function display_message() {
  102. # we need gettext (is loaded in ssft.sh or cloned...)
  103. if [ -f /usr/bin/gettext.sh ]; then
  104. . /usr/bin/gettext.sh || exit 1
  105. else
  106. exit 1
  107. fi
  108. export TEXTDOMAIN="fw-detect"
  109. export TEXTDOMAINDIR=/usr/share/locale
  110. # determine system status
  111. SSFTSH="$(which ssft.sh)" || SSFTSH="/usr/bin/ssft.sh"
  112. # initialize ssft
  113. . "$SSFTSH"
  114. # --- If you want another frontend, do "export SSFT_FRONTEND=$(ssft_choose_frontend)"
  115. # before starting fw-detect
  116. case "$1" in
  117. # ------------------------------------------------------------------
  118. apt-get)
  119. local firmware="$(driver_firmware ${driver})"
  120. ssft_display_message \
  121. "$(gettext "Detected driver that requires firmware to operate")" \
  122. "$(gettext "\
  123. #
  124. # Follow these instructions to obtain the correct firmware
  125. # and activate the ${driver} driver:
  126. apt-get update
  127. ${FW_CARDS_FW[${firmware}]}
  128. modprobe -r ${driver}
  129. modprobe ${driver}
  130. You must first configure apt to install this non-free software,
  131. refer to the offline manual that came with this distribution.
  132. ")"
  133. ;;
  134. # ------------------------------------------------------------------
  135. http)
  136. local firmware="$(driver_firmware ${driver})"
  137. if [[ ${fw_url} ]]; then
  138. FW_URL=$(gettext "\
  139. #
  140. # You can obtain the firmware for ${driver} directly from:
  141. # ${FW_CARDS_FW_URL[${firmware}]}
  142. #
  143. # Download the archive and uncompress it.
  144. # Copy the contents to:
  145. # /lib/firmware/")
  146. fi
  147. ssft_display_message \
  148. "$(gettext "Detected driver that requires firmware to operate")" \
  149. "$(gettext "\
  150. #
  151. # There is no readily available firmware package for ${driver}
  152. # Visit the following page for information on how to obtain and
  153. # install the required firmware:
  154. # ${FW_CARDS_FW[${firmware}]}
  155. ${FW_URL}
  156. #
  157. # Once the firmware files have been installed, the ${driver}
  158. # must be reloaded to activate the hardware.
  159. # Use the following commands:
  160. #
  161. modprobe -r ${driver}
  162. modprobe ${driver}")"
  163. ;;
  164. # ------------------------------------------------------------------
  165. dvb)
  166. local firmware="$(driver_firmware ${driver})"
  167. if ! ((DVB)); then
  168. dvb_message="$(gettext "\
  169. # The get_dvb_firmware script is required to install DVB related firmware.
  170. # <http://www.kernel.org/doc/Documentation/dvb/get_dvb_firmware>
  171. # Download it, copy it to /usr/local/bin and make it executable.
  172. #")"
  173. fi
  174. ssft_display_message \
  175. "$(gettext "Detected driver that requires firmware to operate")" \
  176. "$(gettext "\
  177. $dvb_message
  178. # Follow these instructions to obtain the correct firmware
  179. # and activate the ${driver} driver:
  180. #")
  181. cd /tmp
  182. ${FW_CARDS_FW[${firmware}]}
  183. mv ${FW_CARDS_FW_FILE[${firmware}]} /lib/firmware"
  184. ;;
  185. esac
  186. }
  187. function install_driver() {
  188. # root check
  189. if ((UID)); then
  190. echo "Error: You must be root to run this script!"
  191. exit 1
  192. fi
  193. # driver
  194. driver=$@
  195. echo $driver
  196. fw=""
  197. fw_url=""
  198. fw_file=""
  199. for name in ${FW_CARDS_FW_DEV_STR[@]}; do
  200. case "${driver}" in
  201. ${name})
  202. num=${FW_CARDS_FW_DEV_NUM[$num]}
  203. fw=${FW_CARDS_FW[$num]}
  204. fw_url=${FW_CARDS_FW_URL[$num]}
  205. fw_file=${FW_CARDS_FW_FILE[$num]}
  206. ;;
  207. esac
  208. [ -n "${fw}${fw_url}${fw_file}" ] && break
  209. done
  210. case "${fw}" in
  211. apt-get*)
  212. apt-get update
  213. ${fw}
  214. modprobe -r ${driver}
  215. modprobe ${driver}
  216. ;;
  217. http*)
  218. display_message http
  219. ;;
  220. get_dvb_firmware*)
  221. if ((DVB)); then
  222. TDIR="$(mktemp -d)"
  223. pushd "$TDIR"
  224. ${fw}
  225. mv "${fw_file}" /lib/firmware
  226. popd
  227. rm -fr "$TDIR"
  228. else
  229. display_message dvb
  230. fi
  231. ;;
  232. esac
  233. exit
  234. }
  235. function say() {
  236. [[ ${verbosity} ]] && echo ${@}
  237. }
  238. function kmodule_ok() {
  239. local kmod="$(/sbin/modinfo -n ${1} 2>/dev/null)"
  240. if [[ -f ${kmod} ]]; then
  241. say "Kernel module: ${kmod}"
  242. return 0
  243. fi
  244. return 1
  245. }
  246. function firmware_ok() {
  247. while [ -n "$1" ]; do
  248. local fwok="$1"
  249. local fw_dir="${fwok%% *}"
  250. # if the first part doesn't include a /, we operate in the default dir
  251. if [ -z "${fw_dir}" ] || ! echo "${fw_dir}" | grep '/' &> /dev/null; then
  252. fw_dir="/lib/firmware"
  253. else
  254. if [ "${fwok%% *}" = "${fwok##* }" ]; then
  255. fwok=""
  256. else
  257. fwok="${fwok#* }"
  258. fi
  259. fi
  260. if [ -d "${fw_dir}" ]; then
  261. say "Firmware dir found: ${fw_dir}"
  262. # check now if each named file exists in this dir
  263. while [ -n "${fwok}" ]; do
  264. local fw="${fwok%% *}"
  265. if [ -e "${fw_dir}/${fw}" ]; then
  266. say "Firmware found: ${fw_dir}/${fw}"
  267. else
  268. say "Firmware NOT found: ${fw_dir}/${fw}"
  269. return 1
  270. fi
  271. # was this the last filename to check?
  272. if [ "${fwok%% *}" = "${fwok##* }" ]; then
  273. fwok=""
  274. else
  275. fwok="${fwok#* }"
  276. fi
  277. done
  278. else
  279. say "Firmware dir NOT found: ${fw_dir}"
  280. return 1
  281. fi
  282. shift
  283. done
  284. say "Firmware is okay"
  285. return 0
  286. }
  287. function sys_detect() {
  288. #
  289. # check modules have required firmware
  290. #
  291. say "Looking for common drivers that need firmware..."
  292. for driver in /sys/module/*; do
  293. [ -d "${driver}" ] || continue
  294. driver=${driver#/sys/module/}
  295. unset fw fw_url
  296. pnum=0
  297. firmware=""
  298. for pat in ${FW_CARDS_FW_DEV_STR[@]} ; do
  299. case "${driver}" in
  300. $pat)
  301. say "Driver: ${driver}"
  302. firmware="$(driver_firmware ${driver})"
  303. ;;
  304. esac
  305. [ -n "${firmware}" ] && break
  306. done
  307. if [ -n "$firmware" ]; then
  308. firmware_ok "${FW_CARDS_FW_OK[$firmware]}" && continue
  309. if [ "$short" = "TRUE" ]; then
  310. if [ -n "${FW_CARDS_FW[$firmware]}" ]; then
  311. if [ "$install_possible" = "TRUE" ]; then
  312. case "${FW_CARDS_FW[$firmware]}" in
  313. apt-get*)
  314. echo $driver
  315. ;;
  316. get_dvb_firmware*)
  317. ((DVB)) && echo $driver
  318. ;;
  319. esac
  320. else
  321. echo $driver
  322. fi
  323. fi
  324. else
  325. case "${FW_CARDS_FW[$firmware]}" in
  326. apt-get*)
  327. display_message apt-get
  328. ;;
  329. http*)
  330. display_message http
  331. ;;
  332. get_dvb_firmware*)
  333. display_message dvb
  334. ;;
  335. esac
  336. fi
  337. pnum=$((pnum+1))
  338. fi
  339. done
  340. }
  341. function driver_check() {
  342. for f in ${@} ; do
  343. r=$(firmware_needed ${f})
  344. [ -n "${r}" ] && echo "${f} requires firmware: ${r}"
  345. done
  346. }