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.

257 lines
4.8 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. freebytes() {
  15. read -r availeb < "/sys/devices/virtual/ubi/${ubidev}/avail_eraseblocks"
  16. echo $((availeb * ebsize))
  17. }
  18. totalbytes() {
  19. read -r totaleb < "/sys/devices/virtual/ubi/${ubidev}/total_eraseblocks"
  20. echo $((totaleb * ebsize))
  21. }
  22. getdev() {
  23. local voldir volname
  24. for voldir in "/sys/devices/virtual/ubi/${ubidev}/${ubidev}_"*; do
  25. read -r volname < "${voldir}/name"
  26. case "$volname" in
  27. uvol-[rw][owpd]-$1)
  28. basename "$voldir"
  29. break
  30. ;;
  31. *)
  32. continue
  33. ;;
  34. esac
  35. done
  36. }
  37. vol_is_mode() {
  38. local voldev="$1"
  39. local volname
  40. read -r volname < "/sys/devices/virtual/ubi/${ubidev}/${voldev}/name"
  41. case "$volname" in
  42. uvol-$2-*)
  43. return 0
  44. ;;
  45. esac
  46. return 1
  47. }
  48. getstatus() {
  49. local voldev
  50. voldev="$(getdev "$@")"
  51. [ "$voldev" ] || return 2
  52. vol_is_mode "$voldev" wo && return 22
  53. vol_is_mode "$voldev" wp && return 16
  54. vol_is_mode "$voldev" wd && return 1
  55. vol_is_mode "$voldev" ro && [ ! -e "/dev/ubiblock${voldev:3}" ] && return 1
  56. return 0
  57. }
  58. getsize() {
  59. local voldev
  60. voldev="$(getdev "$@")"
  61. [ "$voldev" ] || return 2
  62. cat "/sys/devices/virtual/ubi/${ubidev}/${voldev}/data_bytes"
  63. }
  64. getuserdev() {
  65. local voldev
  66. voldev="$(getdev "$@")"
  67. [ "$voldev" ] || return 2
  68. if vol_is_mode "$voldev" ro ; then
  69. echo "/dev/ubiblock${voldev:3}"
  70. elif vol_is_mode "$voldev" rw ; then
  71. echo "/dev/$voldev"
  72. fi
  73. }
  74. mkubifs() {
  75. local tmp_mp
  76. tmp_mp="$(mktemp -d)"
  77. mount -t ubifs "$1" "$tmp_mp"
  78. umount "$tmp_mp"
  79. rmdir "$tmp_mp"
  80. }
  81. createvol() {
  82. local mode ret voldev
  83. voldev=$(getdev "$@")
  84. [ "$voldev" ] && return 17
  85. case "$3" in
  86. ro|wo)
  87. mode=wo
  88. ;;
  89. rw)
  90. mode=wp
  91. ;;
  92. *)
  93. return 22
  94. ;;
  95. esac
  96. ubimkvol "/dev/$ubidev" -N "uvol-$mode-$1" -s "$2"
  97. ret=$?
  98. [ $ret -eq 0 ] || return $ret
  99. voldev="$(getdev "$@")"
  100. ubiupdatevol -t "/dev/$voldev"
  101. [ "$mode" = "wp" ] || return 0
  102. mkubifs "/dev/$voldev"
  103. ubirename "/dev/$ubidev" "uvol-wp-$1" "uvol-wd-$1"
  104. }
  105. removevol() {
  106. local voldev
  107. voldev=$(getdev "$@")
  108. [ "$voldev" ] || return 2
  109. local volnum="${voldev#${ubidev}_}"
  110. ubirmvol "/dev/$ubidev" -n "$volnum" || return $?
  111. }
  112. activatevol() {
  113. local voldev
  114. voldev="$(getdev "$@")"
  115. [ "$voldev" ] || return 2
  116. vol_is_mode "$voldev" rw && return 0
  117. vol_is_mode "$voldev" ro && return 0
  118. vol_is_mode "$voldev" wo && return 22
  119. vol_is_mode "$voldev" wp && return 16
  120. if vol_is_mode "$voldev" rd; then
  121. ubirename "/dev/$ubidev" "uvol-rd-$1" "uvol-ro-$1"
  122. ubiblock --create "/dev/$voldev"
  123. return 0
  124. elif vol_is_mode "$voldev" wd; then
  125. ubirename "/dev/$ubidev" "uvol-wd-$1" "uvol-rw-$1"
  126. return 0
  127. fi
  128. }
  129. disactivatevol() {
  130. local voldev
  131. voldev="$(getdev "$@")"
  132. [ "$voldev" ] || return 2
  133. vol_is_mode "$voldev" rd && return 0
  134. vol_is_mode "$voldev" wd && return 0
  135. vol_is_mode "$voldev" wo && return 22
  136. vol_is_mode "$voldev" wp && return 16
  137. if vol_is_mode "$voldev" ro; then
  138. [ -e "/dev/ubiblock${voldev:3}" ] || return 0
  139. ubiblock --remove "/dev/$voldev" || return $?
  140. ubirename "/dev/$ubidev" "uvol-ro-$1" "uvol-rd-$1" || return $?
  141. return 0
  142. elif vol_is_mode "$voldev" rw; then
  143. ubirename "/dev/$ubidev" "uvol-rw-$1" "uvol-wd-$1" || return $?
  144. return 0
  145. fi
  146. }
  147. updatevol() {
  148. local voldev
  149. voldev="$(getdev "$@")"
  150. [ "$voldev" ] || return 2
  151. [ "$2" ] || return 22
  152. vol_is_mode "$voldev" wo || return 22
  153. ubiupdatevol -s "$2" "/dev/$voldev" -
  154. ubirename "/dev/$ubidev" "uvol-wo-$1" "uvol-rd-$1"
  155. }
  156. listvols() {
  157. local volname volmode volsize
  158. for voldir in "/sys/devices/virtual/ubi/${ubidev}/${ubidev}_"*; do
  159. read -r volname < "$voldir/name"
  160. case "$volname" in
  161. uvol-[rw][wod]*)
  162. read -r volsize < "$voldir/data_bytes"
  163. ;;
  164. *)
  165. continue
  166. ;;
  167. esac
  168. volmode="${volname:5:2}"
  169. volname="${volname:8}"
  170. echo "$volname $volmode $volsize"
  171. done
  172. }
  173. bootvols() {
  174. local volname volmode volsize voldev fstype
  175. for voldir in "/sys/devices/virtual/ubi/${ubidev}/${ubidev}_"*; do
  176. read -r volname < "$voldir/name"
  177. voldev="$(basename "$voldir")"
  178. fstype=
  179. case "$volname" in
  180. uvol-ro-*)
  181. voldev="/dev/ubiblock${voldev:3}"
  182. ubiblock --create "/dev/$voldev"
  183. ;;
  184. *)
  185. continue
  186. ;;
  187. esac
  188. volmode="${volname:5:2}"
  189. volname="${volname:8}"
  190. done
  191. }
  192. case "$cmd" in
  193. align)
  194. echo "$ebsize"
  195. ;;
  196. free)
  197. freebytes
  198. ;;
  199. total)
  200. totalbytes
  201. ;;
  202. boot)
  203. bootvols
  204. ;;
  205. list)
  206. listvols "$@"
  207. ;;
  208. create)
  209. createvol "$@"
  210. ;;
  211. remove)
  212. removevol "$@"
  213. ;;
  214. device)
  215. getuserdev "$@"
  216. ;;
  217. size)
  218. getsize "$@"
  219. ;;
  220. up)
  221. activatevol "$@"
  222. ;;
  223. down)
  224. disactivatevol "$@"
  225. ;;
  226. status)
  227. getstatus "$@"
  228. ;;
  229. write)
  230. updatevol "$@"
  231. ;;
  232. *)
  233. echo "unknown command"
  234. return 1
  235. ;;
  236. esac