install.sh.in 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0+
  3. # Copyright (C) 2020 Denis 'GNUtoo' Carikli
  4. uboot_file='@uboot_file@'
  5. uboot_offset='@uboot_offset@'
  6. usage()
  7. {
  8. echo "Usage: $0 [INSTALL_DEVICE]"
  9. }
  10. install_image()
  11. {
  12. install_device="$1"
  13. offset="$2"
  14. image="$3"
  15. # We need root permissions for now:
  16. # - blkid is used to bypass caching and it needs root access
  17. if [ "$(id -u)" != 0 ] ; then
  18. echo "Error: This script needs to be run as root"
  19. exit 1
  20. fi
  21. # We only support MBR for now
  22. # It will also catch errors like /dev/sdb1 and mmcbk0p1 as they
  23. # don't have the PTTYPE tag
  24. PTTYPE="$(blkid --probe --match-tag PTTYPE ${install_device} | \
  25. awk '{print $2}' | sed 's#^PTTYPE="##' | sed 's#"$##')"
  26. if [ -z "${PTTYPE}" ] ; then
  27. echo "Error: Could not find an (MBR) formating inside " \
  28. "${install_device}"
  29. echo " Possible causes:"
  30. echo " - Maybe {instal_device} is a partition"
  31. echo " - or Maybe it's completelyor unformated"
  32. exit 1
  33. elif [ "${PTTYPE}" != "dos" ] ; then
  34. echo "Error: ${install_device} is not in MBR format"
  35. echo " Currently, only the MBR format is supported"
  36. exit 1
  37. fi
  38. # Check if offset > MBR partition table part end
  39. if [ ${offset} -lt 512 ] ; then
  40. echo "Error: The offset (${offset}) is < 512"
  41. echo " offsets < 512 aren't supported (yet)"
  42. exit 1
  43. fi
  44. # Get the image size
  45. image_size="$(du --bytes --apparent-size ${image} | awk '{print $1}')"
  46. # With MBR, most partitioning tools leave makes the first partition
  47. # start at 1MiB. So we need to check if the bootloader doesn't end
  48. # up overwriting the first partition.
  49. if [ $(expr ${image_size} + ${offset}) -gt $(expr 1024 \* 1024) ] ; then
  50. echo "Error: ${image} is too big:"
  51. echo " offset: ${offset}"
  52. echo " size: ${image_size}"
  53. echo " By default, "\
  54. "partitioning tools makes the first partition start at 1MiB"
  55. echo " Instaling ${images} "\
  56. "would overwrite that first partition (if it's present)."
  57. echo " Please contact the Parabola developers " \
  58. "so they could fix that issue"
  59. exit 1
  60. fi
  61. # Copies with with a block size of 1 can be quite slow in practice
  62. if [ "$(expr ${offset} % 512)" = 0 ] ; then
  63. dd conv=notrunc "if=${image}" "of=${install_device}" \
  64. bs=512 "seek=$(expr ${offset} / 512)"
  65. else
  66. echo "Warning: slow copy"
  67. dd conv=notrunc "if=${image}" "of=${install_device}" \
  68. bs=1 "seek=${offset}"
  69. fi
  70. sync "${install_device}"
  71. }
  72. if [ $# -ne 1 ] ; then
  73. usage
  74. else
  75. install_image "$1" "${uboot_offset}" "${uboot_file}"
  76. fi