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.

277 lines
5.4 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
  177. for voldir in "/sys/devices/virtual/ubi/${ubidev}/${ubidev}_"*; do
  178. read -r volname < "$voldir/name"
  179. case "$volname" in
  180. uvol-[rw][wod]*)
  181. read -r volsize < "$voldir/data_bytes"
  182. ;;
  183. *)
  184. continue
  185. ;;
  186. esac
  187. volmode="${volname:5:2}"
  188. volname="${volname:8}"
  189. echo "$volname $volmode $volsize"
  190. done
  191. }
  192. bootvols() {
  193. local volname volmode volsize voldev fstype
  194. for voldir in "/sys/devices/virtual/ubi/${ubidev}/${ubidev}_"*; do
  195. read -r volname < "$voldir/name"
  196. voldev="$(basename "$voldir")"
  197. fstype=
  198. case "$volname" in
  199. uvol-ro-*)
  200. ubiblock --create "/dev/$voldev" || return $?
  201. ;;
  202. *)
  203. continue
  204. ;;
  205. esac
  206. volmode="${volname:5:2}"
  207. volname="${volname:8}"
  208. done
  209. }
  210. case "$cmd" in
  211. align)
  212. echo "$ebsize"
  213. ;;
  214. free)
  215. freebytes
  216. ;;
  217. total)
  218. totalbytes
  219. ;;
  220. boot)
  221. bootvols
  222. ;;
  223. list)
  224. listvols "$@"
  225. ;;
  226. create)
  227. createvol "$@"
  228. ;;
  229. remove)
  230. removevol "$@"
  231. ;;
  232. device)
  233. getuserdev "$@"
  234. ;;
  235. size)
  236. getsize "$@"
  237. ;;
  238. up)
  239. activatevol "$@"
  240. ;;
  241. down)
  242. disactivatevol "$@"
  243. ;;
  244. status)
  245. getstatus "$@"
  246. ;;
  247. write)
  248. updatevol "$@"
  249. ;;
  250. *)
  251. echo "unknown command"
  252. return 1
  253. ;;
  254. esac