summaryrefslogtreecommitdiff
path: root/mkgpt
blob: 92119590f6311146472ac17da976f7fe5a64548b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/sh
set -e

usage() {
    cat <<EOF
Usage: `basename $0` [OPTIONS]

Create partition file to be used to flash GPT on DragonBoard

 -d         enable debug traces
 -a <dir>   output dir to create gpt_main.bin, gpt_backup.bin and gpt_both.bin
 -o <file>  output file (will be destroyed if exists)
 -i <file>  GPT backup file
 -x         enable shell debug mode
 -h         display help
EOF
    exit 1;
}

DEBUG=0
while getopts "a:o:i:s:dx" o; do
    case "${o}" in
    x|d)
        set -x
        DEBUG=1
        ;;
    a)
        OUT_DIR=${OPTARG}
        ;;
    o)
        OUT=${OPTARG}
        ;;
    i)
        IN=${OPTARG}
        ;;
    *)
        usage
        ;;
    esac
done
shift $((OPTIND-1))

if [ -z "$OUT_DIR" ] && [ -z "$OUT" ] || [ -z "$IN" ] ; then
    echo "Please specify an output and input file"
    echo ""
    usage
    exit 1
fi

# The GPT backup is a binary file consisting of the protective MBR,
# the main GPT header, the backup GPT header, and one copy of the
# partition table, in that order.
MBR=`mktemp`
PRIMARY=`mktemp`
SECONDARY=`mktemp`
DATA=`mktemp`

echo "=== Extract MBR, Primary and Secondary headers, and partition table"
dd if=$IN of=$MBR bs=512 count=1 > /dev/null 2>&1
dd if=$IN of=$PRIMARY bs=512 count=1 skip=1 > /dev/null 2>&1
dd if=$IN of=$SECONDARY bs=512 count=1 skip=2 > /dev/null 2>&1
dd if=$IN of=$DATA bs=512 count=32 skip=3 > /dev/null 2>&1
echo "=== Create file(s)"

if [ -n "$OUT_DIR" ] ; then
    cat $MBR $PRIMARY $DATA > $OUT_DIR/gpt_main.bin
    cat $DATA $SECONDARY > $OUT_DIR/gpt_backup.bin
    cat $OUT_DIR/gpt_main.bin $OUT_DIR/gpt_backup.bin > $OUT_DIR/gpt_both.bin
else
    cat $MBR $PRIMARY $DATA $DATA $SECONDARY > $OUT
fi

if [ "$DEBUG" = "1" ] ;then
    if [ -n "$OUT" ]; then
        ls -l $MBR $PRIMARY $DATA $DATA $SECONDARY $OUT
    else
        ls -l $MBR $PRIMARY $DATA $DATA $SECONDARY $OUT_DIR/gpt_{main,backup,both}.bin
    fi
fi

rm -f $MBR $PRIMARY $DATA $SECONDARY
echo "=== Done"