mmc.sh 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #
  2. # Copyright (C) 2016 lede-project.org
  3. #
  4. # this can be used as a generic mmc upgrade script
  5. # just add a device entry in platform.sh,
  6. # define "kernelname" and "rootfsname" and call mmc_do_upgrade
  7. # after the kernel and rootfs flash a loopdev (as overlay) is
  8. # setup on top of the rootfs partition
  9. # for the proper function a padded rootfs image is needed, basically
  10. # append "pad-to 64k" to the image definition
  11. # this is based on the ipq806x zyxel.sh mmc upgrade
  12. . /lib/functions.sh
  13. mmc_do_upgrade() {
  14. local tar_file="$1"
  15. local rootfs=
  16. local kernel=
  17. [ -z "$kernel" ] && kernel=$(find_mmc_part ${kernelname})
  18. [ -z "$rootfs" ] && rootfs=$(find_mmc_part ${rootfsname})
  19. [ -z "$kernel" ] && echo "Upgrade failed: kernel partition not found! Rebooting..." && reboot -f
  20. [ -z "$rootfs" ] && echo "Upgrade failed: rootfs partition not found! Rebooting..." && reboot -f
  21. mmc_do_flash $tar_file $kernel $rootfs
  22. return 0
  23. }
  24. mmc_do_flash() {
  25. local tar_file=$1
  26. local kernel=$2
  27. local rootfs=$3
  28. # keep sure its unbound
  29. losetup --detach-all || {
  30. echo Failed to detach all loop devices. Skip this try.
  31. reboot -f
  32. }
  33. # use the first found directory in the tar archive
  34. local board_dir=$(tar tf $tar_file | grep -m 1 '^sysupgrade-.*/$')
  35. board_dir=${board_dir%/}
  36. echo "flashing kernel to $kernel"
  37. tar xf $tar_file ${board_dir}/kernel -O >$kernel
  38. echo "flashing rootfs to ${rootfs}"
  39. tar xf $tar_file ${board_dir}/root -O >"${rootfs}"
  40. # a padded rootfs is needed for overlay fs creation
  41. local offset=$(tar xf $tar_file ${board_dir}/root -O | wc -c)
  42. [ $offset -lt 65536 ] && {
  43. echo Wrong size for rootfs: $offset
  44. sleep 10
  45. reboot -f
  46. }
  47. # Mount loop for rootfs_data
  48. local loopdev="$(losetup -f)"
  49. losetup -o $offset $loopdev $rootfs || {
  50. echo "Failed to mount looped rootfs_data."
  51. sleep 10
  52. reboot -f
  53. }
  54. echo "Format new rootfs_data at position ${offset}."
  55. mkfs.ext4 -F -L rootfs_data $loopdev
  56. mkdir /tmp/new_root
  57. mount -t ext4 $loopdev /tmp/new_root && {
  58. echo "Saving config to rootfs_data at position ${offset}."
  59. cp -v "$UPGRADE_BACKUP" "/tmp/new_root/$BACKUP_FILE"
  60. umount /tmp/new_root
  61. }
  62. # Cleanup
  63. losetup -d $loopdev >/dev/null 2>&1
  64. sync
  65. umount -a
  66. reboot -f
  67. }