-
Notifications
You must be signed in to change notification settings - Fork 0
/
chs2lba.sh
executable file
·50 lines (39 loc) · 1.05 KB
/
chs2lba.sh
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
#!/bin/bash
#
# Cylinder-Head-Sector to Logical Block Addressing converter.
#
# This script converts a set of co-ordinates from CHS to LBA for use with
# recovery and forensic scripts that might need this.
#
declare -i CYLINDER
declare -i HEAD
declare -i SECTOR
exit_with_error(){
echo "chs2lba.sh: ERROR: ${@:2}"
exit ${1}
}
help_and_exit(){
cat 1>&2 << EOF
chs2lba.sh - convert CHS corindates into LBA sector count.
USAGE:
$ chs2lbah.sh <cylinder> <head> <sector>
All three must be intergers
EOF
exit 2
}
main() {
# Check for input errors
[ -z ${1} ] && help_and_exit
[[ *help* = ${1} ]] && help_and_exit
[ -z ${2} -o -z ${3} ] && exit_with_error 1 "cylinder, head and sector must be specified --help"
CYLINDER=${1}
HEAD=${2}
SECTOR=${3}
# Do the math: https://stackoverflow.com/questions/32642016/chs-to-lba-mapping-disk-storage
local -i bytes_cylinder=$((${CYLINDER} * 256))
local -i bytes_head=$((${HEAD} * 63 ))
local -i lba_sector=$((${bytes_cylinder} + ${bytes_head} + ${SECTOR} ))
#output
echo ${lba_sector}
}
main $@