Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.87 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # state:
  4. # 1 => hardware detection ok
  5. # 2 => usb ok upgrade
  6. # 3 => usb ok downgrade
  7. # 4 => usb ok eingehangen
  8.  
  9. # fwstate
  10. # 2=>4.8-
  11. # 3=>4.9+
  12.  
  13. hs=0
  14. state=0
  15. fwstate=0
  16.  
  17. check_fw_sources() {
  18. echo "check firmware sources..."
  19. result=0
  20. if [ -f /tmp/upload/firmware/hs.hashes ]; then
  21. echo "- firmware found"
  22. cd /tmp/upload/firmware/
  23. busybox sha256sum -cs hs.hashes
  24. if [ $? -eq 0 ]; then
  25. echo "- firmware: ok"
  26. result=1
  27. if [ -f /tmp/upload/frproject/stamm.dat ]; then
  28. echo "- project found"
  29. cd /tmp/upload/frproject/
  30. result=2
  31. for fn in "stamm.dat" "stammpw.dat" "putemerg.dat" "putemergpw.dat"
  32. do
  33. if [ -f ${fn} ]; then
  34. busybox sha256sum -cs ${fn}.hash
  35. if [ $? -eq 0 ]; then
  36. echo "- project file ${fn}: OK"
  37. else
  38. echo "- project file ${fn}: FAIlED"
  39. result=1
  40. break
  41. fi
  42. fi
  43. done
  44. fi
  45. fi
  46. fi
  47. return ${result}
  48. }
  49.  
  50. error_signal() {
  51. for i in 1 2 3 4 5 6
  52. do
  53. echo -ne "\a"
  54. sleep 0.3
  55. done
  56. }
  57.  
  58. fin_signal() {
  59. for i in 1 2 3 4 5 6
  60. do
  61. echo -ne "\a"
  62. sleep 0.3
  63. done
  64. }
  65.  
  66. echo "check device"
  67. fname="/sys/devices/virtual/dmi/id/product_serial"
  68. if [ -f ${fname} ]; then
  69. prodid=`cat ${fname}|cut -c1-8|tr "a-z" "A-Z"`
  70. [ "${prodid}" = "000AB302" ] && hs=1
  71. [ "${prodid}" = "000AB304" ] && hs=2
  72. [ "${prodid}" = "000AB3E2" ] && hs=3
  73. fi
  74. if [ ${hs} -eq 0 ]; then
  75. echo "hs/fs not found"
  76. error_signal
  77. exit
  78. fi
  79.  
  80. # check device
  81. echo "check version"
  82. if [ -b /dev/sda4 ]; then
  83. echo "detecting version 4.8 or older, check files..."
  84. [ -d /sda2 ] || mkdir /sda2
  85. mount -t vfat /dev/sda2 /sda2
  86. if [ -f /sda2/DOLINUX2.BAT ]; then
  87. echo "version 4.8 or older found."
  88. fwstate=2
  89. state=1
  90. fi
  91. umount /sda2
  92. rmdir /sda2
  93. elif [ -b /dev/sda1 -a ! -b /dev/sda4 ]; then
  94. echo "detecting version 4.9 or newer, check files..."
  95. state=1
  96. [ -d /sda1 ] || mkdir /sda1
  97. mount -t ext4 -o noatime /dev/sda1 /sda1
  98. if [ -d /sda1/hsdata/project/ -a -d /sda1/boot/ ]; then
  99. # hs version 4.9+
  100. echo "version 4.9 or newer found."
  101. fwstate=3
  102. fi
  103. umount /sda1
  104. rmdir /sda1
  105. else
  106. echo "unkown device"
  107. ls -l /dev/sd*
  108. echo "---"
  109. fi
  110.  
  111. if [ ${state} -eq 1 ]; then
  112. # mount usb device
  113. echo "try to mount usb device..."
  114. [ -d /usb ] || mkdir -p /usb
  115. sleep 2
  116. for i in $(seq 1 60)
  117. do
  118. if [ -b /dev/sdb1 ]; then
  119. mount -t vfat /dev/sdb1 /usb 2>&1
  120. if [ $? -eq 0 ]; then
  121. echo "usb device ok"
  122. state=4
  123. if [ -f /usb/upload.hs ]; then
  124. echo "usb device for reset/update/project detected"
  125. state=2
  126. fi
  127. if [ -f /usb/downgrade/p1 -a -f /usb/downgrade/p2 -a -f /usb/downgrade/p3 ]; then
  128. echo "usb device for downgrade detected"
  129. state=3
  130. fi
  131. break
  132. fi
  133. else
  134. echo "device /dev/sdb1 does not exist"
  135. fi
  136. if [ ${state} -ne 2 ]; then
  137. sleep 1
  138. fi
  139. done
  140. fi
  141.  
  142. if [ ${state} -eq 2 -a -f /usb/upload.hs ]; then
  143. mkdir -p /tmp/upload
  144. rm -rf /tmp/upload/*
  145. unzip -q -o /usb/upload.hs -d /tmp/upload
  146. if [ -f /tmp/upload/factoryreset.bef ]; then
  147. # factory reset 4.9+
  148. echo "factory reset detected..."
  149. check_fw_sources
  150. ok=$?
  151. if [ ${ok} -eq 2 ]; then
  152. echo "- reset storage..."
  153. dd if=/dev/zero of=/dev/sda bs=1M
  154. echo "- create partitions..."
  155. echo -e "n\np\n1\n\n\nw\n" | fdisk /dev/sda >/dev/tty5
  156. sync
  157. echo -e "t\n83\nw\n" | fdisk /dev/sda >/dev/tty5
  158. sync
  159. echo "- format..."
  160. /sbin/mkfs.ext4 /dev/sda1 >/dev/tty5
  161. sync
  162. echo "- set boot flag..."
  163. echo -e "a\n1\nw\n" | fdisk /dev/sda >/dev/tty5
  164. sync
  165. echo "- write mbr..."
  166. install-mbr -v -p 1 -i n /dev/sda
  167. sync
  168. echo "- copy files"
  169. mkdir /sda1
  170. mount -t ext4 -o noatime /dev/sda1 /sda1
  171. extlinux -i /sda1
  172. mkdir -p -m 750 /sda1/boot0/
  173. mkdir -p -m 750 /sda1/boot1/
  174. ln -s boot0 /sda1/boot
  175. mkdir -p /sda1/hsdata/project
  176. mkdir -p -m 750 /sda1/hsdata/remanent
  177. chown 1000.1000 /sda1/hsdata/remanent
  178. mkdir -p -m 755 /sda1/hsdata/cert
  179. mkdir -p -m 755 /sda1/hsdata/ntp
  180. chown 101.101 /sda1/hsdata/ntp
  181. cp -R /tmp/upload/firmware/* /sda1/boot/
  182. cp -R /tmp/upload/frproject/* /sda1/hsdata/project/
  183. for fn in "stammpw.dat" "putemergpw.dat"
  184. do
  185. if [ -f /sda1/hsdata/project/${fn} ]; then
  186. echo "chmod for ${fn}"
  187. chmod 400 /sda1/hsdata/project/${fn}q
  188. fi
  189. done
  190. cd /sda1/boot/
  191. sha256sum -c hs.hashes
  192. cd /sda1/hsdata/project/
  193. for fn in "stamm.dat" "stammpw.dat" "putemerg.dat" "putemergpw.dat"
  194. do
  195. sha256sum -c ${fn}.hash
  196. done
  197. cd /
  198. umount /sda1
  199. rmdir /sda1
  200. sleep 1
  201. echo "check partitions..."
  202. /sbin/fsck.ext4 -p /dev/sda1
  203. else
  204. echo "missing files and/or corrupt files found (${ok})"
  205. fi
  206. elif [ -f /tmp/upload/update.bef -a ${fwstate} -eq 3 ]; then
  207. # fw update 4.9+
  208. echo "update detected..."
  209. check_fw_sources
  210. ok=$?
  211. if [ ${ok} -eq 1 ]; then
  212. echo "copy firmware files..."
  213. [ -d /sda1 ] || mkdir /sda1
  214. mount -t ext4 -o noatime /dev/sda1 /sda1
  215. rm -rf /sda1/boot/*
  216. cp -R /tmp/upload/firmware/* /sda1/boot/
  217. umount /sda1
  218. rmdir /sda1
  219. else
  220. echo "missing files and/or corrupt files found (${ok})"
  221. fi
  222. fi
  223. if [ -f /tmp/upload/project.bef -a ${fwstate} -eq 3 ]; then
  224. echo "project file found"
  225. [ -d /sda1 ] || mkdir /sda1
  226. mount -t ext4 -o noatime /dev/sda1 /sda1
  227. diff -q -w -i -B -b /tmp/upload/project/version.dat /sda1/boot/project.version
  228. if [ $? -eq 0 ]; then
  229. echo "- version correct"
  230. if [ -f /tmp/upload/project/stamm.dat ]; then
  231. echo "- project file found"
  232. rm -rf /sda1/hsdata/project/*
  233. cp /tmp/upload/project/stamm.dat /sda1/hsdata/project/
  234. cp /tmp/upload/project/stamm.dat.hash /sda1/hsdata/project/
  235. if [ -f /tmp/upload/project/stammpw.dat ]; then
  236. echo "- project (pw) found"
  237. cp /tmp/upload/project/stammpw.dat /sda1/hsdata/project/
  238. cp /tmp/upload/project/stammpw.dat.hash /sda1/hsdata/project/
  239. chmod 400 /sda1/hsdata/project/stammpw.dat
  240. fi
  241. if [ -f /tmp/upload/project/data.bef ]; then
  242. echo "- project data found"
  243. cp -R /tmp/upload/project/dat /sda1/hsdata/project/
  244. fi
  245. if [ -f /tmp/upload/project/putemerg.dat ]; then
  246. echo "- emergency project found"
  247. cp /tmp/upload/project/putemerg.dat /sda1/hsdata/project/
  248. cp /tmp/upload/project/putemerg.dat.hash /sda1/hsdata/project/
  249. if [ -f /tmp/upload/project/putemergpw.dat ]; then
  250. echo "- emergency project (pw) found"
  251. cp /tmp/upload/project/putemergpw.dat /sda1/hsdata/project/
  252. cp /tmp/upload/project/putemergpw.dat.hash /sda1/hsdata/project/
  253. chmod 400 /sda1/hsdata/project/putemergpw.dat
  254. fi
  255. fi
  256. fi
  257. if [ -f /tmp/upload/project/delrem.bef ]; then
  258. echo "- remove storage"
  259. rm -rf /sda1/hsdata/remanent/*
  260. fi
  261. if [ -f /tmp/upload/project/delcert.bef ]; then
  262. echo "- remove certificates"
  263. rm -rf /sda1/hsdata/cert/*
  264. fi
  265. echo "copy finished"
  266. else
  267. echo "- versions not identical"
  268. fi
  269. umount /sda1
  270. rmdir /sda1
  271. fi
  272. elif [ ${state} -eq 3 -a -d /usb/downgrade ]; then
  273. ok=0
  274. if [ -f /usb/downgrade/hashes ]; then
  275. echo "check files"
  276. cd /usb/downgrade/
  277. busybox sha256sum -c hashes
  278. if [ $? -eq 0 ]; then
  279. echo "- hashes: ok"
  280. ok=1
  281. fi
  282. cd /
  283. else
  284. ok=1
  285. fi
  286. if [ ${ok} -eq 1 ]; then
  287. echo "- reset storage"
  288. dd if=/dev/zero of=/dev/sda bs=1M
  289.  
  290. size=`fdisk -l /dev/sda|grep "Disk /dev/sda:"|awk -F' ' '{print $5}'`
  291. echo "- disk size: ${size}"
  292. if [ ${size} -ge 4000000000 ]; then
  293. echo "- partitioning (4GB)..."
  294. echo -e "n\np\n1\n\n2\nn\np\n2\n\n+200M\nn\np\n3\n\n+1840M\nn\np\n\n\n\nw\n" | fdisk /dev/sda
  295. else
  296. echo "- partitioning (2GB)..."
  297. echo -e "n\np\n1\n\n2\nn\np\n2\n\n+100M\nn\np\n3\n\n+920M\nn\np\n\n\n\nw\n" | fdisk /dev/sda
  298. fi
  299.  
  300. sleep 2
  301. echo -e "t\n1\n6\nt\n2\n6\nw\n" | fdisk /dev/sda
  302. sleep 5
  303.  
  304. echo "- write partition 1"
  305. dd if=/usb/downgrade/p1 of=/dev/sda1
  306.  
  307. echo "- write partition 2"
  308. mkdosfs -F 16 /dev/sda2
  309. sync
  310. echo -e "a\n1\nw\n" | fdisk /dev/sda
  311. sleep 5
  312.  
  313. mkdir -p /sda2
  314. mount -t vfat /dev/sda2 /sda2
  315. unzip /usb/downgrade/p2 -d /sda2/
  316. umount /sda2
  317.  
  318. echo "- write partition 3"
  319. dd if=/usb/downgrade/p3 of=/dev/sda3
  320.  
  321. echo "- write mbr"
  322. install-mbr -v -p 1 -i n /dev/sda
  323.  
  324. echo "check partitions"
  325. fsck.fat -a -w /dev/sda1
  326. fsck.fat -a -w /dev/sda2
  327. fi
  328. fi
  329.  
  330. if [ ${state} -ge 2 ]; then
  331. echo "unmount usb device..."
  332. umount /usb
  333. rmdir /usb
  334. fi
  335.  
  336. # finished, wait for reboot
  337. echo "wait for reboot, remove usb or shutdown..."
  338. while :
  339. do
  340. fin_signal
  341. sleep 10
  342. if [ ! -b /dev/sdb1 ]; then
  343. echo "usb device removed, reboot..."
  344. echo -ne "\a"
  345. sleep 0.3
  346. break
  347. else
  348. sleep 20
  349. fi
  350. done
  351. reboot
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement