mksd.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/bin/sh
  2. # This scripts makes a minimal bootable SD card image for the Chromebook.
  3. # The resulting file is called bootsd.img. It should be written directly
  4. # to the card:
  5. #
  6. # SD=/dev/mmcblk1 # check your device name!
  7. # dd if=output/images/bootsd.img of=$SD
  8. #
  9. # The partitions are created just large enough to hold the kernel and
  10. # the rootfs image. Most of the card will be empty, and the secondary
  11. # GPT will not be in its proper location.
  12. # cgpt does not create protective MBR, and the kernel refuses to read
  13. # GPT unless there's some kind of MBR in sector 0. So we need parted
  14. # to write that single sector before doing anything with the GPT.
  15. cgpt=$HOST_DIR/bin/cgpt
  16. parted=$HOST_DIR/sbin/parted
  17. kernel=$BINARIES_DIR/uImage.kpart
  18. rootfs=$BINARIES_DIR/rootfs.ext2
  19. run() { echo "$@"; "$@"; }
  20. die() { echo "$@" >&2; exit 1; }
  21. test -f $kernel || die "No kernel image found"
  22. test -f $rootfs || die "No rootfs image found"
  23. test -x $cgpt || die "cgpt not found (host-vboot-utils have not been built?)"
  24. # True file sizes in bytes
  25. kernelsize=`stat -t $kernel | cut -d\ -f2`
  26. rootfssize=`stat -t $rootfs | cut -d\ -f2`
  27. # The card is partitioned in sectors of 8KB.
  28. # 4 sectors are reserved for MBR+GPT. Their actual size turns out
  29. # to be 33 512-blocks which is just over 2 sectors, but we align
  30. # it to a nice round number.
  31. sec=8192
  32. kernelsec=$(((kernelsize+8191)>>13))
  33. rootfssec=$(((rootfssize+8191)>>13))
  34. headersec=4
  35. # There is also a copy of MBR+GPT at the end of the image.
  36. # It's going to be useless but both tools assume it's there.
  37. imagesec=$((2*headersec+kernelsec+rootfssec))
  38. bootsd="$BINARIES_DIR/bootsd.img"
  39. run dd bs=$sec count=$imagesec if=/dev/zero of=$bootsd
  40. # cgpt needs offsets and sizes in 512-blocks.
  41. block=512
  42. kernelstart=$((headersec<<4))
  43. kernelblocks=$((kernelsec<<4))
  44. rootfsblocks=$((rootfssec<<4))
  45. rootfsstart=$((kernelstart+kernelblocks))
  46. # This command initializes both GPT and MBR
  47. run $parted -s $bootsd mklabel gpt
  48. # The kernel partition must be marked as bootable, that's why -S -T -P
  49. run $cgpt add -i 1 -b $kernelstart -s $kernelblocks \
  50. -t kernel -l kernel \
  51. -S 1 -T 1 -P 10 $bootsd
  52. # It does not really matter where the rootfs partition is located as long
  53. # as the kernel can find it.
  54. # However, if anything is changed here, kernel.args must be updated as well.
  55. run $cgpt add -i 2 -b $rootfsstart -s $rootfsblocks \
  56. -t data -l rootfs $bootsd
  57. run dd bs=$block if=$kernel of=$bootsd seek=$kernelstart
  58. run dd bs=$block if=$rootfs of=$bootsd seek=$rootfsstart