|
Lines 30-36
show_help() {
Link Here
|
| 30 |
|
30 |
|
| 31 |
print_version() { |
31 |
print_version() { |
| 32 |
cat <<-EOF |
32 |
cat <<-EOF |
| 33 |
$PROG version 2.0 |
33 |
$PROG version 2.1 |
| 34 |
Written by Alexey Gladkov. |
34 |
Written by Alexey Gladkov. |
| 35 |
|
35 |
|
| 36 |
Copyright (C) 2010-2012 Alexey Gladkov <gladkov.alexey@gmail.com> |
36 |
Copyright (C) 2010-2012 Alexey Gladkov <gladkov.alexey@gmail.com> |
|
Lines 40-45
print_version() {
Link Here
|
| 40 |
exit |
40 |
exit |
| 41 |
} |
41 |
} |
| 42 |
|
42 |
|
|
|
43 |
read_gpt() { |
| 44 |
local offset=$(($1 + 512)) len=${2:-1} |
| 45 |
local x=$(dd if="$dev" bs=1 skip=$offset count=$len 2>/dev/null | od -A n -t u$len) |
| 46 |
echo "${x// /}" |
| 47 |
} |
| 48 |
|
| 49 |
|
| 43 |
GETOPT_ALLOW_UNKNOWN=1 |
50 |
GETOPT_ALLOW_UNKNOWN=1 |
| 44 |
TEMP=`getopt -n $PROG -o b:,d,r:,v,V,h -l boot:,dry-run,root:,verbose,version,help -- "$@"` || |
51 |
TEMP=`getopt -n $PROG -o b:,d,r:,v,V,h -l boot:,dry-run,root:,verbose,version,help -- "$@"` || |
| 45 |
show_help |
52 |
show_help |
|
Lines 86-111
sysfs_path="$(readlink -ev "/sys/dev/block/$boot_majmin" 2>/dev/null)" ||
Link Here
|
| 86 |
read partition < "$sysfs_path/partition" |
93 |
read partition < "$sysfs_path/partition" |
| 87 |
|
94 |
|
| 88 |
dev="$(sed -n -e 's,^DEVNAME=,/dev/,p' "${sysfs_path%/*}/uevent")" |
95 |
dev="$(sed -n -e 's,^DEVNAME=,/dev/,p' "${sysfs_path%/*}/uevent")" |
| 89 |
partition_table="$(LANG=C parted -ms "$dev" print | grep ^/ |cut -d: -f6)" |
96 |
|
|
|
97 |
magic="$(dd if="$dev" bs=2 skip=255 count=1 2>/dev/null)" |
| 98 |
[ "$magic" = $'\x55\xAA' ] || |
| 99 |
fatal "Unknown partition table" |
| 100 |
|
| 101 |
magic="$(dd if="$dev" bs=8 skip=64 count=1 2>/dev/null)" |
| 102 |
if [ "$magic" != "EFI PART" ]; then |
| 103 |
partition_table='msdos' |
| 104 |
magic=4 |
| 105 |
else |
| 106 |
partition_table='gpt' |
| 107 |
magic=$(read_gpt 80 4) |
| 108 |
fi |
| 90 |
|
109 |
|
| 91 |
verbose "Partition table: $partition_table" |
110 |
verbose "Partition table: $partition_table" |
| 92 |
|
111 |
|
| 93 |
case "$partition_table" in |
112 |
[ $partition -ge 1 -a $partition -le $magic ] || |
| 94 |
gpt) |
113 |
fatal "invalid partition number" |
| 95 |
mbr_bin='gptmbr.bin' |
114 |
|
| 96 |
flag='legacy_boot' |
115 |
if [ "$partition_table" = "msdos" ]; then |
| 97 |
;; |
116 |
magic=$(($((partition - 1)) * 16 + 446)) |
| 98 |
msdos) |
117 |
magic="$(dd if="$dev" bs=1 skip=$magic count=1 2>/dev/null)" |
| 99 |
mbr_bin='mbr.bin' |
118 |
[ "$magic" = $'\x80' ] || |
| 100 |
flag='boot' |
119 |
fatal "partiton not bootable" |
| 101 |
;; |
120 |
mbr_bin='mbr.bin' |
| 102 |
*) fatal "Unknown partition table: $partition_table" ;; |
121 |
else |
| 103 |
esac |
122 |
entry_size=$(read_gpt 84 4) |
| 104 |
|
123 |
magic=$(($((partition - 1)) * $entry_size + 552)) |
| 105 |
bootflags="$(LANG=C parted -ms "$dev" print | grep "^$partition:" |cut -d: -f7)" |
124 |
magic="$(read_gpt $magic)" |
| 106 |
|
125 |
magic=$((magic & 2)) |
| 107 |
[ -z "${bootflags##*$flag*}" ] || |
126 |
[ "$magic" = "2" ] || |
| 108 |
fatal "partiton not bootable" |
127 |
fatal "partiton not bootable" |
|
|
128 |
mbr_bin='gptmbr.bin' |
| 129 |
fi |
| 109 |
|
130 |
|
| 110 |
DEVNAME= |
131 |
DEVNAME= |
| 111 |
dir="$(readlink -m "$ROOTDIR/")" |
132 |
dir="$(readlink -m "$ROOTDIR/")" |