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.

160 lines
2.8 KiB

  1. #!/bin/sh
  2. test -e /sys/class/ubi/version || return 0
  3. read ubiver < /sys/class/ubi/version
  4. [ "$ubiver" = "1" ] || return 1
  5. test -e /sys/devices/virtual/ubi || return 0
  6. ubidev=$(ls -1 /sys/devices/virtual/ubi | head -n 1)
  7. read ebsize < "/sys/devices/virtual/ubi/${ubidev}/eraseblock_size"
  8. freebytes() {
  9. read availeb < "/sys/devices/virtual/ubi/${ubidev}/avail_eraseblocks"
  10. echo $((availeb * ebsize))
  11. }
  12. totalbytes() {
  13. read totaleb < "/sys/devices/virtual/ubi/${ubidev}/total_eraseblocks"
  14. echo $((totaleb * ebsize))
  15. }
  16. getdev() {
  17. local voldir volname devname
  18. for voldir in /sys/devices/virtual/ubi/${ubidev}/${ubidev}_*; do
  19. read volname < "${voldir}/name"
  20. [ "$volname" = "uvol-ro-$1" ] || [ "$volname" = "uvol-rw-$1" ] || continue
  21. basename "$voldir"
  22. done
  23. }
  24. needs_ubiblock() {
  25. local voldev="$1"
  26. local volname
  27. read volname < "/sys/devices/virtual/ubi/${ubidev}/${voldev}/name"
  28. case "$volname" in
  29. uvol-ro-*)
  30. return 0
  31. ;;
  32. esac
  33. return 1
  34. }
  35. getstatus() {
  36. local voldev=$(getdev "$@")
  37. [ "$voldev" ] || return 2
  38. needs_ubiblock $voldev && [ ! -e "/dev/ubiblock${voldev:3}" ] && return 1
  39. return 0
  40. }
  41. getsize() {
  42. local voldev
  43. voldev=$(getdev "$@")
  44. [ "$voldev" ] || return 2
  45. cat /sys/devices/virtual/ubi/${ubidev}/${voldev}/data_bytes
  46. }
  47. getuserdev() {
  48. local voldev=$(getdev "$@")
  49. [ "$voldev" ] || return 2
  50. if needs_ubiblock $voldev ; then
  51. echo "/dev/ubiblock${voldev:3}"
  52. else
  53. echo "/dev/$voldev"
  54. fi
  55. }
  56. createvol() {
  57. local mode
  58. local existdev=$(getdev "$1")
  59. [ "$existdev" ] && return 17
  60. case "$3" in
  61. ro)
  62. mode=ro
  63. ;;
  64. rw)
  65. mode=rw
  66. ;;
  67. *)
  68. return 22
  69. ;;
  70. esac
  71. ubimkvol /dev/$ubidev -N "uvol-$mode-$1" -s "$2"
  72. }
  73. removevol() {
  74. local voldev=$(getdev "$@")
  75. [ "$voldev" ] || return 2
  76. local volnum=${voldev#${ubidev}_}
  77. ubirmvol /dev/$ubidev -n $volnum
  78. }
  79. activatevol() {
  80. local voldev=$(getdev "$@")
  81. [ "$voldev" ] || return 2
  82. needs_ubiblock $voldev || return 0
  83. [ -e "/dev/ubiblock${voldev:3}" ] && return 0
  84. ubiblock --create /dev/$voldev
  85. }
  86. disactivatevol() {
  87. local voldev=$(getdev "$@")
  88. [ "$voldev" ] || return 2
  89. needs_ubiblock $voldev || return 0
  90. [ -e "/dev/ubiblock${voldev:3}" ] || return 0
  91. ubiblock --remove /dev/$voldev
  92. }
  93. updatevol() {
  94. local voldev=$(getdev "$@")
  95. [ "$voldev" ] || return 2
  96. [ "$2" ] || return 22
  97. needs_ubiblock $voldev || return 22
  98. ubiupdatevol -s $2 /dev/$voldev -
  99. }
  100. getstatus() {
  101. local voldev=$(getdev "$@")
  102. [ "$voldev" ] || return 2
  103. needs_ubiblock $voldev && [ ! -e "/dev/ubiblock${voldev:3}" ] && return 1
  104. return 0
  105. }
  106. cmd="$1"
  107. shift
  108. case "$cmd" in
  109. free)
  110. freebytes
  111. ;;
  112. total)
  113. totalbytes
  114. ;;
  115. create)
  116. createvol "$@"
  117. ;;
  118. remove)
  119. removevol "$@"
  120. ;;
  121. device)
  122. getuserdev "$@"
  123. ;;
  124. size)
  125. getsize "$@"
  126. ;;
  127. up)
  128. activatevol "$@"
  129. ;;
  130. down)
  131. disactivatevol "$@"
  132. ;;
  133. status)
  134. getstatus "$@"
  135. ;;
  136. write)
  137. updatevol "$@"
  138. ;;
  139. *)
  140. echo "unknown command"
  141. return 1
  142. ;;
  143. esac