create-boot-sd.sh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/bin/sh
  2. set -u
  3. set -e
  4. PROGNAME=$(basename $0)
  5. usage()
  6. {
  7. echo "Create an SD card that boots on an i.MX53/6 board."
  8. echo
  9. echo "Note: all data on the the card will be completely deleted!"
  10. echo "Use with care!"
  11. echo "Superuser permissions may be required to write to the device."
  12. echo
  13. echo "Usage: ${PROGNAME} <sd_block_device>"
  14. echo "Arguments:"
  15. echo " <sd_block_device> The device to be written to"
  16. echo
  17. echo "Example: ${PROGNAME} /dev/mmcblk0"
  18. echo
  19. }
  20. if [ $# -ne 1 ]; then
  21. usage
  22. exit 1
  23. fi
  24. if [ $(id -u) -ne 0 ]; then
  25. echo "${PROGNAME} must be run as root"
  26. exit 1
  27. fi
  28. DEV=${1}
  29. # The partition name prefix depends on the device name:
  30. # - /dev/sde -> /dev/sde1
  31. # - /dev/mmcblk0 -> /dev/mmcblk0p1
  32. if echo ${DEV}|grep -q mmcblk ; then
  33. PART="p"
  34. else
  35. PART=""
  36. fi
  37. PART1=${DEV}${PART}1
  38. PART2=${DEV}${PART}2
  39. # Unmount the partitions if mounted
  40. umount ${PART1} || true
  41. umount ${PART2} || true
  42. # First, clear the card
  43. dd if=/dev/zero of=${DEV} bs=1M count=20
  44. sync
  45. # Partition the card.
  46. # SD layout for i.MX6 boot:
  47. # - Bootloader at offset 1024
  48. # - FAT partition starting at 1MB offset, containing uImage and *.dtb
  49. # - ext2/3 partition formatted as ext2 or ext3, containing the root filesystem.
  50. sfdisk ${DEV} <<EOF
  51. 32,480,b
  52. 512,,L
  53. EOF
  54. sync
  55. # Copy the bootloader at offset 1024
  56. dd if=output/images/u-boot.imx of=${DEV} obs=512 seek=2
  57. # Prepare a temp dir for mounting partitions
  58. TMPDIR=$(mktemp -d)
  59. # FAT partition: kernel and DTBs
  60. mkfs.vfat ${PART1}
  61. mount ${PART1} ${TMPDIR}
  62. cp output/images/*Image ${TMPDIR}/
  63. cp output/images/*.dtb ${TMPDIR}/ || true
  64. sync
  65. umount ${TMPDIR}
  66. # ext2 partition: root filesystem
  67. mkfs.ext2 ${PART2}
  68. mount ${PART2} ${TMPDIR}
  69. tar -C ${TMPDIR}/ -xf output/images/rootfs.tar
  70. sync
  71. umount ${TMPDIR}
  72. # Cleanup
  73. rmdir ${TMPDIR}
  74. sync
  75. echo Done