install-uboot4extlinux.sh.in 2.6 KB

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