#!/bin/sh # Tool for recovering VISTA & Windows7 MBR disk signature by guessing it # from the information in Boot/BCD # Originally written by Kiyoshi SUZUKI in Fukui, Japan # at 2010/9/8 readloop () { while [ a = a ] do read a if [ $a -ge 0 ] then if [ $? = 1 ] then echo "Please type number" continue elif [ $a -lt "$(echo $*|tr ' ' '\n'|wc -l)" ] then break else echo "Please type number smaller than $(echo $*|tr ' ' '\n'|wc -l)" continue fi echo "Please type number larger than 0 or 0" fi done } deviceslist=$(blkid|cut -d: -f1) okdeviceslist="" for a in $deviceslist do umount -l /mnt 2>/dev/null mount -t $(blkid $a|sed -e "s/.*TYPE=\"//"|sed -e "s/\".*//") $a /mnt 2>/dev/null || continue if [ "$(ls /mnt/Boot/BCD 2>/dev/null)" ] then okdeviceslist="${okdeviceslist} $a" fi umount -l /mnt 2>/dev/null done if [ "$(echo $okdeviceslist|grep \ )" ] then echo "Boot/BCD is found in the partitions the device file name of which is shown below:" echo i=0 for a in ${okdeviceslist} do echo $i":"$(blkid|grep "${a}:") echo i=$(expr $i + 1) done echo echo "You can look upper by pushing SHIFT and PageUp keys at the same time." echo "Please type the number of the device file name of the partition of Boot/BCD you want to deal with" readloop $okdeviceslist okdeviceslist=$(echo $okdeviceslist|cut -d" " -f$(expr $a + 1)) fi echo Boot/BCD in $okdeviceslist will be used to search disk signature echo disk signature candidates shown below are found in Boot/BCD in $okdeviceslist echo ====================================================================== mount -t $(blkid $okdeviceslist|sed -e "s/.*TYPE=\"//"|sed -e "s/\".*//") $okdeviceslist /mnt 2>/dev/null hd /mnt/Boot/BCD | grep " 00 00 00 00 00 00 00 00 01 00 00 00 .. .. .. .. "|sed -e "s/.* 01 00 00 00 //"|sed -e "s/ |.*$//" > /tmp/kiyoplog1 hd /mnt/Boot/BCD | grep " 01 00 00 00 .. .. .. .. 00 00 00 00 00 00 00 00 "|sed -e "s/.* 01 00 00 00 //"|sed -e "s/ .*$//" >> /tmp/kiyoplog1 sort /tmp/kiyoplog1|uniq -c|sed -e "s/^[^0-9]*//"|sed -e "s/ /times /" > /tmp/kiyoplog2 num=$(wc -l /tmp/kiyoplog2|cut -d" " -f1) i=0 while [ $i -lt $num ] do echo $i:$(sed -n -e "$(expr $i + 1)p" /tmp/kiyoplog2) i=$(expr $i + 1) done echo ====================================================================== echo "Please type the number of the disk signature suitable to be written in MBR" readloop $(cat /tmp/kiyoplog2|cut -d" " -f1) disksignature=$(sed -n -e $(expr $a + 1)p /tmp/kiyoplog2|sed -e "s/.*times //") echo you selected $a:$disksignature bcdline=$(hd /mnt/Boot/BCD|grep " 01 00 00 00 $disksignature "|sed -n -e 1p) num=$(echo "obase=10;ibase=16;$(echo $bcdline|tr a-f A-F|cut -d" " -f1) + 4"|bc) if [ -n "$(echo "$bcdline"|grep " 00 01 00 00 00 $disksignature")" ] then num=$(expr $num + 8) fi if [ "$disksignature" = "$(dd if=/mnt/Boot/BCD bs=1 count=4 skip=$num 2>/dev/null|hd|cut -d" " -f3-6|sed -n -e 1p)" ] then echo "Please type yes if disk signature: \"$disksignature\" may be written to address 0x1b8-0x1bb in MBR(/dev/sda)." read a if [ "$a" = "yes" ] then dd if=/dev/sda bs=512 count=1 of=sdambr_$(date --iso-8601=minutes|tr -d " ") 2>/dev/null echo Present MBR of /dev/sda is saved in the file: $PWD/sdambr_$(date --iso-8601=minutes|tr -d " ") dd if=/mnt/Boot/BCD bs=1 count=4 skip=$num seek=440 of=/dev/sda 2>/dev/null&& echo $disksignature is successfully written in MBR of /dev/sda || echo Error in writting disk signature to MBR else echo MBR is not changed. fi else echo "Something wrong happened. MBR has not been changed." fi exit 0