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.

357 lines
7.0 KiB

  1. #!/bin/sh
  2. cmd="$1"
  3. shift
  4. if [ "$cmd" = "name" ]; then
  5. echo "UBI"
  6. return 0
  7. fi
  8. test -e /sys/class/ubi/version || return 0
  9. read -r ubiver < /sys/class/ubi/version
  10. [ "$ubiver" = "1" ] || return 1
  11. test -e /sys/devices/virtual/ubi || return 0
  12. ubidev=$(ls -1 /sys/devices/virtual/ubi | head -n 1)
  13. read -r ebsize < "/sys/devices/virtual/ubi/${ubidev}/eraseblock_size"
  14. . /lib/functions/uvol.sh
  15. freebytes() {
  16. read -r availeb < "/sys/devices/virtual/ubi/${ubidev}/avail_eraseblocks"
  17. echo $((availeb * ebsize))
  18. }
  19. totalbytes() {
  20. read -r totaleb < "/sys/devices/virtual/ubi/${ubidev}/total_eraseblocks"
  21. echo $((totaleb * ebsize))
  22. }
  23. getdev() {
  24. local voldir volname
  25. for voldir in "/sys/devices/virtual/ubi/${ubidev}/${ubidev}_"*; do
  26. read -r volname < "${voldir}/name"
  27. case "$volname" in
  28. uvol-[rw][owpd]-$1)
  29. basename "$voldir"
  30. break
  31. ;;
  32. *)
  33. continue
  34. ;;
  35. esac
  36. done
  37. }
  38. vol_is_mode() {
  39. local voldev="$1"
  40. local volname
  41. read -r volname < "/sys/devices/virtual/ubi/${ubidev}/${voldev}/name"
  42. case "$volname" in
  43. uvol-$2-*)
  44. return 0
  45. ;;
  46. esac
  47. return 1
  48. }
  49. getstatus() {
  50. local voldev
  51. voldev="$(getdev "$@")"
  52. [ "$voldev" ] || return 2
  53. vol_is_mode "$voldev" wo && return 22
  54. vol_is_mode "$voldev" wp && return 16
  55. vol_is_mode "$voldev" wd && return 1
  56. vol_is_mode "$voldev" ro && [ ! -e "/dev/ubiblock${voldev:3}" ] && return 1
  57. return 0
  58. }
  59. getsize() {
  60. local voldev
  61. voldev="$(getdev "$@")"
  62. [ "$voldev" ] || return 2
  63. cat "/sys/devices/virtual/ubi/${ubidev}/${voldev}/data_bytes"
  64. }
  65. getuserdev() {
  66. local voldev
  67. voldev="$(getdev "$@")"
  68. [ "$voldev" ] || return 2
  69. if vol_is_mode "$voldev" ro ; then
  70. echo "/dev/ubiblock${voldev:3}"
  71. elif vol_is_mode "$voldev" rw ; then
  72. echo "/dev/$voldev"
  73. fi
  74. }
  75. mkubifs() {
  76. local tmp_mp
  77. tmp_mp="$(mktemp -d)"
  78. mount -t ubifs "$1" "$tmp_mp" || return $?
  79. umount "$tmp_mp" || return $?
  80. rmdir "$tmp_mp" || return $?
  81. return 0
  82. }
  83. createvol() {
  84. local mode ret voldev
  85. voldev=$(getdev "$@")
  86. [ "$voldev" ] && return 17
  87. case "$3" in
  88. ro|wo)
  89. mode=wo
  90. ;;
  91. rw)
  92. mode=wp
  93. ;;
  94. *)
  95. return 22
  96. ;;
  97. esac
  98. ubimkvol "/dev/$ubidev" -N "uvol-$mode-$1" -s "$2" || return $?
  99. ret=$?
  100. [ $ret -eq 0 ] || return $ret
  101. voldev="$(getdev "$@")"
  102. ubiupdatevol -t "/dev/$voldev" || return $?
  103. [ "$mode" = "wp" ] || return 0
  104. mkubifs "/dev/$voldev" || return $?
  105. uvol_uci_add "$1" "/dev/$voldev" "rw"
  106. ubirename "/dev/$ubidev" "uvol-wp-$1" "uvol-wd-$1" || return $?
  107. }
  108. removevol() {
  109. local voldev volnum
  110. voldev=$(getdev "$@")
  111. [ "$voldev" ] || return 2
  112. vol_is_mode "$voldev" rw && return 16
  113. vol_is_mode "$voldev" ro && return 16
  114. volnum="${voldev#${ubidev}_}"
  115. ubirmvol "/dev/$ubidev" -n "$volnum" || return $?
  116. uvol_uci_remove "$1"
  117. uvol_uci_commit "$1"
  118. }
  119. block_hotplug() {
  120. export ACTION="$1"
  121. export DEVNAME="$2"
  122. /sbin/block hotplug
  123. }
  124. activatevol() {
  125. local voldev
  126. voldev="$(getdev "$@")"
  127. [ "$voldev" ] || return 2
  128. vol_is_mode "$voldev" rw && return 0
  129. vol_is_mode "$voldev" ro && return 0
  130. vol_is_mode "$voldev" wo && return 22
  131. vol_is_mode "$voldev" wp && return 16
  132. uvol_uci_commit "$1"
  133. if vol_is_mode "$voldev" rd; then
  134. ubirename "/dev/$ubidev" "uvol-rd-$1" "uvol-ro-$1" || return $?
  135. ubiblock --create "/dev/$voldev" || return $?
  136. return 0
  137. elif vol_is_mode "$voldev" wd; then
  138. ubirename "/dev/$ubidev" "uvol-wd-$1" "uvol-rw-$1" || return $?
  139. block_hotplug add "$voldev"
  140. return 0
  141. fi
  142. }
  143. disactivatevol() {
  144. local voldev
  145. voldev="$(getdev "$@")"
  146. [ "$voldev" ] || return 2
  147. vol_is_mode "$voldev" rd && return 0
  148. vol_is_mode "$voldev" wd && return 0
  149. vol_is_mode "$voldev" wo && return 22
  150. vol_is_mode "$voldev" wp && return 16
  151. if vol_is_mode "$voldev" ro; then
  152. /sbin/block umount "ubiblock${voldev:3}"
  153. ubiblock --remove "/dev/$voldev"
  154. ubirename "/dev/$ubidev" "uvol-ro-$1" "uvol-rd-$1" || return $?
  155. return 0
  156. elif vol_is_mode "$voldev" rw; then
  157. /sbin/block umount "$voldev"
  158. ubirename "/dev/$ubidev" "uvol-rw-$1" "uvol-wd-$1" || return $?
  159. block_hotplug remove "$voldev"
  160. return 0
  161. fi
  162. }
  163. updatevol() {
  164. local voldev
  165. voldev="$(getdev "$@")"
  166. [ "$voldev" ] || return 2
  167. [ "$2" ] || return 22
  168. vol_is_mode "$voldev" wo || return 22
  169. ubiupdatevol -s "$2" "/dev/$voldev" -
  170. ubiblock --create "/dev/$voldev"
  171. uvol_uci_add "$1" "/dev/ubiblock${voldev:3}" "ro"
  172. ubiblock --remove "/dev/$voldev"
  173. ubirename "/dev/$ubidev" "uvol-wo-$1" "uvol-rd-$1"
  174. }
  175. listvols() {
  176. local volname volmode volsize json_output json_notfirst
  177. if [ "$1" = "-j" ]; then
  178. json_output=1
  179. shift
  180. echo "["
  181. fi
  182. for voldir in "/sys/devices/virtual/ubi/${ubidev}/${ubidev}_"*; do
  183. read -r volname < "$voldir/name"
  184. case "$volname" in
  185. uvol-[rw][wod]*)
  186. read -r volsize < "$voldir/data_bytes"
  187. ;;
  188. *)
  189. continue
  190. ;;
  191. esac
  192. volmode="${volname:5:2}"
  193. volname="${volname:8}"
  194. if [ "$json_output" = "1" ]; then
  195. [ "$json_notfirst" = "1" ] && echo ","
  196. echo -e "\t{"
  197. echo -e "\t\t\"name\": \"$volname\","
  198. echo -e "\t\t\"mode\": \"$volmode\","
  199. echo -e "\t\t\"size\": $volsize"
  200. echo -n -e "\t}"
  201. json_notfirst=1
  202. else
  203. echo "$volname $volmode $volsize"
  204. fi
  205. done
  206. if [ "$json_output" = "1" ]; then
  207. [ "$json_notfirst" = "1" ] && echo
  208. echo "]"
  209. fi
  210. }
  211. bootvols() {
  212. local volname volmode volsize voldev fstype
  213. for voldir in "/sys/devices/virtual/ubi/${ubidev}/${ubidev}_"*; do
  214. read -r volname < "$voldir/name"
  215. voldev="$(basename "$voldir")"
  216. fstype=
  217. case "$volname" in
  218. uvol-ro-*)
  219. ubiblock --create "/dev/$voldev" || return $?
  220. ;;
  221. *)
  222. continue
  223. ;;
  224. esac
  225. volmode="${volname:5:2}"
  226. volname="${volname:8}"
  227. done
  228. }
  229. detect() {
  230. local volname voldev volmode voldev fstype tmpdev=""
  231. for voldir in "/sys/devices/virtual/ubi/${ubidev}/${ubidev}_"*; do
  232. read -r volname < "$voldir/name"
  233. voldev="$(basename "$voldir")"
  234. fstype=
  235. case "$volname" in
  236. uvol-r[od]-*)
  237. if ! [ -e "/dev/ubiblock${voldev:3}" ]; then
  238. ubiblock --create "/dev/$voldev" || return $?
  239. fi
  240. case "$volname" in
  241. uvol-rd-*)
  242. tmpdev="$tmpdev $voldev"
  243. ;;
  244. esac
  245. ;;
  246. *)
  247. continue
  248. ;;
  249. esac
  250. volmode="${volname:5:2}"
  251. volname="${volname:8}"
  252. done
  253. uvol_uci_init
  254. for voldir in "/sys/devices/virtual/ubi/${ubidev}/${ubidev}_"*; do
  255. read -r volname < "$voldir/name"
  256. voldev="$(basename "$voldir")"
  257. case "$volname" in
  258. uvol-[rw][wod]*)
  259. true
  260. ;;
  261. *)
  262. continue
  263. ;;
  264. esac
  265. volmode="${volname:5:2}"
  266. volname="${volname:8}"
  267. case "$volmode" in
  268. "ro" | "rd")
  269. uvol_uci_add "$volname" "/dev/ubiblock${voldev:3}" "ro"
  270. ;;
  271. "rw" | "wd")
  272. uvol_uci_add "$volname" "/dev/${voldev}" "rw"
  273. ;;
  274. esac
  275. done
  276. uvol_uci_commit
  277. for voldev in $tmpdev ; do
  278. ubiblock --remove "/dev/$voldev" || return $?
  279. done
  280. }
  281. case "$cmd" in
  282. align)
  283. echo "$ebsize"
  284. ;;
  285. free)
  286. freebytes
  287. ;;
  288. total)
  289. totalbytes
  290. ;;
  291. detect)
  292. detect
  293. ;;
  294. boot)
  295. bootvols
  296. ;;
  297. list)
  298. listvols "$@"
  299. ;;
  300. create)
  301. createvol "$@"
  302. ;;
  303. remove)
  304. removevol "$@"
  305. ;;
  306. device)
  307. getuserdev "$@"
  308. ;;
  309. size)
  310. getsize "$@"
  311. ;;
  312. up)
  313. activatevol "$@"
  314. ;;
  315. down)
  316. disactivatevol "$@"
  317. ;;
  318. status)
  319. getstatus "$@"
  320. ;;
  321. write)
  322. updatevol "$@"
  323. ;;
  324. *)
  325. echo "unknown command"
  326. return 1
  327. ;;
  328. esac