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.

358 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. [ "${volname:0:1}" = "." ] && continue
  195. if [ "$json_output" = "1" ]; then
  196. [ "$json_notfirst" = "1" ] && echo ","
  197. echo -e "\t{"
  198. echo -e "\t\t\"name\": \"$volname\","
  199. echo -e "\t\t\"mode\": \"$volmode\","
  200. echo -e "\t\t\"size\": $volsize"
  201. echo -n -e "\t}"
  202. json_notfirst=1
  203. else
  204. echo "$volname $volmode $volsize"
  205. fi
  206. done
  207. if [ "$json_output" = "1" ]; then
  208. [ "$json_notfirst" = "1" ] && echo
  209. echo "]"
  210. fi
  211. }
  212. bootvols() {
  213. local volname volmode volsize voldev fstype
  214. for voldir in "/sys/devices/virtual/ubi/${ubidev}/${ubidev}_"*; do
  215. read -r volname < "$voldir/name"
  216. voldev="$(basename "$voldir")"
  217. fstype=
  218. case "$volname" in
  219. uvol-ro-*)
  220. ubiblock --create "/dev/$voldev" || return $?
  221. ;;
  222. *)
  223. continue
  224. ;;
  225. esac
  226. volmode="${volname:5:2}"
  227. volname="${volname:8}"
  228. done
  229. }
  230. detect() {
  231. local volname voldev volmode voldev fstype tmpdev=""
  232. for voldir in "/sys/devices/virtual/ubi/${ubidev}/${ubidev}_"*; do
  233. read -r volname < "$voldir/name"
  234. voldev="$(basename "$voldir")"
  235. fstype=
  236. case "$volname" in
  237. uvol-r[od]-*)
  238. if ! [ -e "/dev/ubiblock${voldev:3}" ]; then
  239. ubiblock --create "/dev/$voldev" || return $?
  240. fi
  241. case "$volname" in
  242. uvol-rd-*)
  243. tmpdev="$tmpdev $voldev"
  244. ;;
  245. esac
  246. ;;
  247. *)
  248. continue
  249. ;;
  250. esac
  251. volmode="${volname:5:2}"
  252. volname="${volname:8}"
  253. done
  254. uvol_uci_init
  255. for voldir in "/sys/devices/virtual/ubi/${ubidev}/${ubidev}_"*; do
  256. read -r volname < "$voldir/name"
  257. voldev="$(basename "$voldir")"
  258. case "$volname" in
  259. uvol-[rw][wod]*)
  260. true
  261. ;;
  262. *)
  263. continue
  264. ;;
  265. esac
  266. volmode="${volname:5:2}"
  267. volname="${volname:8}"
  268. case "$volmode" in
  269. "ro" | "rd")
  270. uvol_uci_add "$volname" "/dev/ubiblock${voldev:3}" "ro"
  271. ;;
  272. "rw" | "wd")
  273. uvol_uci_add "$volname" "/dev/${voldev}" "rw"
  274. ;;
  275. esac
  276. done
  277. uvol_uci_commit
  278. for voldev in $tmpdev ; do
  279. ubiblock --remove "/dev/$voldev" || return $?
  280. done
  281. }
  282. case "$cmd" in
  283. align)
  284. echo "$ebsize"
  285. ;;
  286. free)
  287. freebytes
  288. ;;
  289. total)
  290. totalbytes
  291. ;;
  292. detect)
  293. detect
  294. ;;
  295. boot)
  296. bootvols
  297. ;;
  298. list)
  299. listvols "$@"
  300. ;;
  301. create)
  302. createvol "$@"
  303. ;;
  304. remove)
  305. removevol "$@"
  306. ;;
  307. device)
  308. getuserdev "$@"
  309. ;;
  310. size)
  311. getsize "$@"
  312. ;;
  313. up)
  314. activatevol "$@"
  315. ;;
  316. down)
  317. disactivatevol "$@"
  318. ;;
  319. status)
  320. getstatus "$@"
  321. ;;
  322. write)
  323. updatevol "$@"
  324. ;;
  325. *)
  326. echo "unknown command"
  327. return 1
  328. ;;
  329. esac