How to automatically check your harddrive / filesystem for badblocks / physical damage and also extract the hidden HPA partition

How to automatically check your harddrive / filesystem for badblocks / physical damage and also extract the hidden HPA partition

Some easy script for checking and repairing badblocks and more with hdparm

#!/bin/bash

echo "*********************************************************"
echo "* Teh badblock checker / HPA extractor script!! v0.2a   *"
echo "* _____________________________________________________ *"
echo "*********************************************************"

HDD=$1

# https://wiki.debianforum.de/Hdparm

# GET blocksize:
# touch file ; du -h file
#
# BLOCKSIZE = fsutil fsinfo ntfsinfo c:
# wmic partition get blocksize, startingoffset, name, index

# START-OFFSET / BLOCKSIZE / 8


# e2fsck -fccky /dev/sdXX # non-destructive read/write test (recommended)
# The -k is important, because it saves the previous bad block table, and adds any new bad blocks to that table.
# Without -k, you loose all of the prior bad block information.

#   -f    Force checking even if the file system seems clean.

#   -c    This option causes e2fsck to use badblocks(8) program to do
#         a read-only scan of the device in order to find any bad blocks.
#         If any bad blocks are found, they are added to the bad block
#         inode to prevent them from being allocated to a file or direc-
#         tory.  If this option is specified twice, then the bad block scan
#         will be done using a non-destructive read-write test.

#   -k    When combined with the -c option, any existing bad blocks in the
#         bad blocks list are preserved, and any new bad blocks found by
#         running badblocks(8) will be added to the existing bad blocks
#         list.

#   -y    Assume an answer of `yes' to all questions; allows e2fsck to be
#         used non-interactively. This option may not be specified at the
#         same time as the -n or -p options.

sleep 1

if [ "$2" = "--bad-only" ]; then
        echo "Checking badblock true style... experimental"
        badblocks -v -b 4096 $HDD > sectors-out.txt

        e2fsck -l sectors-out.txt $HDD
        echo "Set hdparm"

        for SECTOR in sectors-out.txt; do
                echo "hdparm --yes-i-know-what-i-am-doing --write-sector $SECTOR $HDD"
                #hdparm --yes-i-know-what-i-am-doing --write-sector $SECTOR $HDD
        done

elif [ "$2" = "--fscky-mode" ]; then
        sleep 2
        echo -n "Checking $HDD - OKAY?" ; read yn

        e2fsck -fccky $HDD
elif [ "$2" = "--info" ]; then
        sleep 2
        echo -n "Checking $HDD INFO - OKAY?" ; read yn
        hdparm -I $HDD
        echo "--------"
        echo "--------"
        fdisk -l $HDD
elif [ "$2" = "--hpa-xtract" ]; then
        sleep 2
        echo -n "Checking $HDD HPA-Hidden sector - OKAY?" ; read yn
        hdparm -N $HDD

        FS=$(hdparm -N /dev/sda | grep -o "[0-9]*" | head -n1)
        LS=$(hdparm -N /dev/sda | grep -o "[0-9]*" | tail -n1)

        echo "--------"
        echo "--------"
        fdisk -l $HDD

        #echo "If disabled:"
        #hdparm --yes-i-know-what-i-am-doing -N$FS $HDD

        echo "Disable and del HPA at sek $LS :"
        hdparm -Np$LS $HDD
        echo "Now reboot pls to take affect.."
        echo

        echo "Dump with dd sektor: $FS"
        dd if=$HDD of=hpa_dump.bin bs=512 skip=$FS

        echo "Hexedit and extract maybe something bios like fw or anything?"
        hexdump -Cn40 hpa_dump.bin

        # some extracting..
        #lha -x hpa.bin

else
        echo
        echo "Usage $0 [DEVICE] [--bad-only / --fscky-mode / --info / --hpa-xtract ]"
        exit 1;
fi

exit 0