zyxel.sh 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #
  2. # Copyright (C) 2016 lede-project.org
  3. #
  4. zyxel_get_rootfs() {
  5. local rootfsdev
  6. if read cmdline < /proc/cmdline; then
  7. case "$cmdline" in
  8. *root=*)
  9. rootfsdev="${cmdline##*root=}"
  10. rootfsdev="${rootfsdev%% *}"
  11. ;;
  12. esac
  13. echo "${rootfsdev}"
  14. fi
  15. }
  16. zyxel_do_flash() {
  17. local tar_file=$1
  18. local board=$2
  19. local kernel=$3
  20. local rootfs=$4
  21. # keep sure its unbound
  22. losetup --detach-all || {
  23. echo Failed to detach all loop devices. Skip this try.
  24. reboot -f
  25. }
  26. echo "flashing kernel to /dev/${kernel}"
  27. tar xf $tar_file sysupgrade-$board/kernel -O >/dev/$kernel
  28. echo "flashing rootfs to ${rootfs}"
  29. tar xf $tar_file sysupgrade-$board/root -O >"${rootfs}"
  30. # a padded rootfs is needed for overlay fs creation
  31. local offset=$(tar xf $tar_file sysupgrade-$board/root -O | wc -c)
  32. [ $offset -lt 65536 ] && {
  33. echo Wrong size for rootfs: $offset
  34. sleep 10
  35. reboot -f
  36. }
  37. # Mount loop for rootfs_data
  38. losetup -o $offset /dev/loop0 "${rootfs}" || {
  39. echo "Failed to mount looped rootfs_data."
  40. sleep 10
  41. reboot -f
  42. }
  43. echo "Format new rootfs_data at position ${offset}."
  44. mkfs.ext4 -F -L rootfs_data /dev/loop0
  45. mkdir /tmp/new_root
  46. mount -t ext4 /dev/loop0 /tmp/new_root && {
  47. echo "Saving config to rootfs_data at position ${offset}."
  48. cp -v /tmp/sysupgrade.tgz /tmp/new_root/
  49. umount /tmp/new_root
  50. }
  51. # Cleanup
  52. losetup -d /dev/loop0 >/dev/null 2>&1
  53. sync
  54. umount -a
  55. reboot -f
  56. }
  57. zyxel_do_upgrade() {
  58. local tar_file="$1"
  59. local board=$(cat /tmp/sysinfo/board_name)
  60. local rootfs="$(zyxel_get_rootfs)"
  61. local kernel=
  62. [ -b "${rootfs}" ] || return 1
  63. case "$board" in
  64. nbg6817)
  65. kernel=mmcblk0p4
  66. ;;
  67. *)
  68. return 1
  69. esac
  70. zyxel_do_flash $tar_file $board $kernel $rootfs
  71. return 0
  72. }