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.

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