2 # corrupt a GPT table; ensure parted takes notice
4 # Copyright (C) 2009-2010 Free Software Foundation, Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 if test "$VERBOSE" = yes; then
29 case $# in 2) ;; *) echo "usage: peek FILE 0_BASED_OFFSET" >&2; exit 1;; esac
30 case $2 in *[^0-9]*) echo "peek: invalid offset: $2"; exit 1 ;; esac
31 dd if="$1" bs=1 skip="$2" count=1
36 case $# in 3) ;; *) echo "usage: poke FILE 0_BASED_OFFSET BYTE" >&2; exit 1;;
38 case $2 in *[^0-9]*) echo "poke: invalid offset: $2"; exit 1 ;; esac
39 case $3 in ?) ;; *) echo "poke: invalid byte: '$3'"; exit 1 ;; esac
40 printf %s "$3" | dd of="$1" bs=1 seek="$2" count=1 conv=notrunc
49 dd if=/dev/null of=$dev bs=$ss seek=$n_sectors || fail=1
52 parted -s $dev mklabel gpt > empty 2>&1 || fail=1
53 compare /dev/null empty || fail=1
55 # print the empty table
56 parted -m -s $dev unit s print > t 2>&1 || fail=1
57 sed "s,.*/$dev:,$dev:," t > out || fail=1
59 # check for expected output
60 printf "BYT;\n$dev:${n_sectors}s:file:$sector_size_:$sector_size_:gpt:;\n" \
62 compare exp out || fail=1
65 parted -s $dev mkpart sw linux-swap 2048s 4095s > empty 2>&1 || fail=1
66 compare /dev/null empty || fail=1
68 # We're going to change the name of the first partition,
69 # thus invalidating the PartitionEntryArrayCRC32 checksum.
71 # byte 56 of the partition entry is the first byte of its 72-byte name field
72 pte_offset=$(expr $ss \* 2 + 56)
74 # get the first byte of the name
75 pte_byte=$(peek $dev $pte_offset) || fail=1
77 test x"$pte_byte" = xA && new_byte=B || new_byte=A
79 # Replace with a different byte
80 poke $dev $pte_offset "$new_byte" || fail=1
82 # printing the table must succeed, but with a scary diagnostic.
83 parted -s $dev print > err 2>&1 || fail=1
84 grep Error: err > k && mv k err || fail=1
86 # check for expected diagnostic
87 echo 'Error: The primary GPT table is corrupt, but the backup appears OK,' \
88 'so that will be used.' > exp || fail=1
89 compare exp err || fail=1
91 # ----------------------------------------------------------
92 # Now, restore things, and corrupt the MyLBA in the alternate GUID table.
95 # Restore original byte
96 poke $dev $pte_offset "$orig_byte" || fail=1
99 parted -s $dev print > out 2> err || fail=1
100 compare /dev/null err || fail=1
102 # The MyLBA member of the alternate table is in the last sector,
103 # $n_sectors, 8-byte field starting at offset 24.
104 alt_my_lba_offset=$(expr $n_sectors \* $ss - $ss + 24)
105 # get the first byte of MyLBA
106 byte=$(peek $dev $alt_my_lba_offset) || fail=1
109 test x"$byte" = xA && new_byte=B || new_byte=A
111 # Replace good byte with the bad one.
112 poke $dev $alt_my_lba_offset "$new_byte" || fail=1
114 # attempting to set partition name must print a diagnostic
115 parted -m -s $dev name 1 foo > err 2>&1 || fail=1
117 # check for expected diagnostic
118 echo 'Error: The backup GPT table is corrupt, but the primary appears OK,' \
119 'so that will be used.' > exp || fail=1
120 compare exp err || fail=1
122 # corruption is fixed; printing the table now elicits no diagnostic
123 parted -m -s $dev u s print > out 2>&1 || fail=1
125 # check for expected output
126 printf "BYT;\nfile\n1:2048s:4095s:2048s::foo:;\n" > exp || fail=1
127 sed "s/.*gpt:;/file/" out > k && mv k out || fail=1
128 compare exp out || fail=1