gen_mvebu_sdcard_img.sh 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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> <sectors_partitionN> <img_partitionN>]?"
  21. }
  22. # always require first 3 arguments
  23. # then in pairs up to 8 more for a total of up to 4 partitions
  24. if [ $# -lt 2 ] || [ $# -gt 15 ] || [ $((($# - 2) % 3)) -ne 0 ]; then
  25. usage
  26. exit 1
  27. fi
  28. set -e
  29. # parameters
  30. OUTFILE="$1"; shift
  31. BOOTLOADER="$1"; shift
  32. # generate image file
  33. printf "Creating $OUTFILE from /dev/zero: "
  34. dd if=/dev/zero of="$OUTFILE" bs=512 count=1 >/dev/null
  35. printf "Done\n"
  36. while [ "$#" -ge 3 ]; do
  37. ptgen_args="$ptgen_args -p $(($2 / 2 + 256)) -S 0x$1"
  38. parts="$parts$3 "
  39. shift; shift; shift
  40. done
  41. head=16
  42. sect=63
  43. # create real partition table using fdisk
  44. printf "Creating partition table: "
  45. set `ptgen -o "$OUTFILE" -h $head -s $sect -l 1024 $ptgen_args`
  46. printf "Done\n"
  47. # install bootloader
  48. printf "Writing bootloader: "
  49. dd of="$OUTFILE" if="$BOOTLOADER" bs=512 seek=1 conv=notrunc 2>/dev/null
  50. printf "Done\n"
  51. i=1
  52. while [ "$#" -ge 2 ]; do
  53. img="${parts%% *}"
  54. parts="${parts#* }"
  55. printf "Writing %s to partition %i: " "$img" $i
  56. (
  57. cat "$img"
  58. # add padding to avoid leaving behind old overlay fs data
  59. dd if=/dev/zero bs=128k count=1 2>/dev/null
  60. ) | dd of="$OUTFILE" bs=512 seek=$(($1 / 512)) conv=notrunc 2>/dev/null
  61. printf "Done\n"
  62. let i=i+1
  63. shift; shift
  64. done