2
1

create-boot-sd.sh 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.MX28 EVK 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. PART3=${DEV}${PART}3
  40. # Unmount the partitions if mounted
  41. umount ${PART1} || true
  42. umount ${PART2} || true
  43. umount ${PART3} || true
  44. # First, clear the card
  45. dd if=/dev/zero of=${DEV} bs=1M count=20
  46. sync
  47. # Partition the card.
  48. # SD layout for i.MX28 boot:
  49. # - Special partition type 53 at sector 2048, containing an SD-SB-encapsulated u-boot
  50. # - FAT partition containing zImage
  51. # - ext2/3 partition formatted as ext2 or ext3, containing the root filesystem.
  52. sfdisk --force -u S ${DEV} <<EOF
  53. 2048,2000,53
  54. 4048,16000,b
  55. 20048,,L
  56. EOF
  57. sync
  58. # Copy the bootloader at offset 2048
  59. # (We need to skip the partition table in the .sd, too.)
  60. dd if=output/images/u-boot.sd of=${DEV}1 bs=1M
  61. # Prepare a temp dir for mounting partitions
  62. TMPDIR=$(mktemp -d)
  63. # FAT partition: kernel
  64. mkfs.vfat ${PART2}
  65. mount ${PART2} ${TMPDIR}
  66. cp output/images/*Image ${TMPDIR}/
  67. cp output/images/*.dtb ${TMPDIR}/ || true
  68. sync
  69. umount ${TMPDIR}
  70. # ext2 partition: root filesystem
  71. mkfs.ext2 ${PART3}
  72. mount ${PART3} ${TMPDIR}
  73. tar -C ${TMPDIR}/ -xf output/images/rootfs.tar
  74. sync
  75. umount ${TMPDIR}
  76. # Cleanup
  77. rmdir ${TMPDIR}
  78. sync
  79. echo Done