gen_mvebu_sdcard_img.sh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env bash
  2. #
  3. # Copyright (C) 2016 Josua Mayer
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. #
  19. usage() {
  20. echo "$0 <outfile> [<bootloader> <type_partitionN> <size_partitionN> <img_partitionN>]?"
  21. }
  22. # always require first 2 or 3 arguments
  23. # then in pairs up to 8 more for a total of up to 4 partitions
  24. if [ $# -lt 1 ] || [ $# -gt 14 ] || [ $((($# - 1) % 3)) -ne 0 ]; then
  25. if [ $# -lt 2 ] || [ $# -gt 15 ] || [ $((($# - 2) % 3)) -ne 0 ]; then
  26. usage
  27. exit 1
  28. else
  29. BOOTLOADER="$2"
  30. fi
  31. fi
  32. set -e
  33. # parameters
  34. OUTFILE="$1"; shift
  35. if [ -n "$BOOTLOADER" ]; then
  36. shift
  37. fi
  38. # generate image file
  39. printf "Creating $OUTFILE from /dev/zero: "
  40. dd if=/dev/zero of="$OUTFILE" bs=512 count=1 >/dev/null
  41. printf "Done\n"
  42. while [ "$#" -ge 3 ]; do
  43. ptgen_args="$ptgen_args -t $1 -p $(($2 * 1024 + 256))"
  44. parts="$parts$3 "
  45. shift; shift; shift
  46. done
  47. head=16
  48. sect=63
  49. # create real partition table using fdisk
  50. printf "Creating partition table: "
  51. set `ptgen -o "$OUTFILE" -h $head -s $sect -l 1024 -S 0x$SIGNATURE $ptgen_args`
  52. printf "Done\n"
  53. # install bootloader
  54. if [ -n "$BOOTLOADER" ]; then
  55. printf "Writing bootloader: "
  56. dd of="$OUTFILE" if="$BOOTLOADER" bs=512 seek=1 conv=notrunc 2>/dev/null
  57. printf "Done\n"
  58. fi
  59. i=1
  60. while [ "$#" -ge 2 ]; do
  61. img="${parts%% *}"
  62. parts="${parts#* }"
  63. printf "Writing %s to partition %i: " "$img" $i
  64. (
  65. cat "$img"
  66. # add padding to avoid leaving behind old overlay fs data
  67. dd if=/dev/zero bs=128k count=1 2>/dev/null
  68. ) | dd of="$OUTFILE" bs=512 seek=$(($1 / 512)) conv=notrunc 2>/dev/null
  69. printf "Done\n"
  70. let i=i+1
  71. shift; shift
  72. done