program list of "partinfo"

#! /bin/bash

# print Partition Information of HDD MBR
# partinfo Ver. 0.002 (2008/08/08)
# Copyright (C) 2008 Adsaria

# This program is free software; you can redistribute it and/or modify it.
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY.

### Argument handling ##########################################################

ARGS_DEF_opt_char_val="p"
ARGS_DEF_opt_word_flg="help"
ARGS_DEF_opt_word_val="partition"
ARGS_DEF_arg_namelist="device"
ARGS_DEF_arg_num_must=0

. /usr/local/bin/shargs

if [ "${ARGS_error}" == "Y" ]; then echo "${ARGS_usage}" 1>&2 ; exit 1; fi
if [ "${ARGS_opt_help}" == "Y" ]; then echo "${ARGS_usage}" 1>&2 ; exit 1; fi

### Define shell vars ##########################################################

CMD_NAME=`basename "${ARGS_0}"`
HDD="${ARGS__device}"
PARTITION=

if [ -n "${ARGS_opt_p}" -o -n "${ARGS_opt_partition}" ]; then
	if [ -n "${ARGS_opt_p}" ]; then
		PARTITION="${ARGS_opt_p}"
		if [ -n "${ARGS_opt_partition}" ]; then
			if [ "$PARTITION" != "${ARGS_opt_partition}" ]; then
				echo "${CMD_NAME}: partition specification is corrupt." 1>&2
				exit 1
			fi
		fi
	else
		PARTITION="${ARGS_opt_partition}"
	fi
			
fi

TYPE_00='Empty'
TYPE_01='DOS 12-bit FAT'
TYPE_02='XENIX root'
TYPE_03='XENIX /usr'
TYPE_04='DOS 3.0+ 16-bit FAT <32M'
TYPE_05='DOS 3.3+ Extended Partition'
TYPE_06='DOS 3.31+ 16-bit FAT >=32M'
TYPE_07='HPFS/NTFS'
TYPE_08='AIX boot partition'
TYPE_09='AIX data partition'
TYPE_0a='OS/2 Boot Manager'
TYPE_0b='WIN95 OSR2 FAT32'
TYPE_0c='WIN95 OSR2 FAT32, LBA-mapped'
TYPE_0e='WIN95: DOS 16-bit FAT, LBA-mapped'
TYPE_0f='WIN95: Extended partition, LBA-mapped'
TYPE_10='OPUS'
TYPE_11='Hidden DOS 12-bit FAT'
TYPE_12='Configuration/diagnostics partition'
TYPE_14='Hidden DOS 16-bit FAT <32M'
TYPE_16='Hidden DOS 16-bit FAT >=32M'
TYPE_17='Hidden HPFS/NTFS'
TYPE_18='AST SmartSleep Partition'
TYPE_1b='Hidden WIN95 OSR2 FAT32'
TYPE_1c='Hidden WIN95 OSR2 FAT32, LBA-mapped'
TYPE_1e='Hidden WIN95 16-bit FAT, LBA-mapped'
TYPE_24='NEC DOS 3.x'
TYPE_27='Windows RE hidden partition'
TYPE_2a='AtheOS File System (AFS)'
TYPE_2b='SyllableSecure (SylStor)'
TYPE_32='NOS'
TYPE_35='JFS on OS/2 or eCS'
TYPE_38='THEOS ver 3.2 2gb partition'
TYPE_39='Plan 9 partition'
TYPE_39='THEOS ver 4 spanned / Plan 9 partition'
TYPE_3a='THEOS ver 4 4gb partition'
TYPE_3b='THEOS ver 4 extended partition'
TYPE_3c='PartitionMagic recovery partition'
TYPE_3d='Hidden NetWare'
TYPE_40='Venix 80286'
TYPE_41='PPC PReP (Power PC Reference Platform) Boot'
TYPE_42='SFS (Secure Filesystem)'
TYPE_43='Linux native (sharing disk with DRDOS)'
TYPE_44='GoBack partition'
TYPE_45='EUMEL/Elan'
TYPE_46='EUMEL/Elan'
TYPE_47='EUMEL/Elan'
TYPE_48='EUMEL/Elan'
TYPE_4a='Mark Aitchison'\''s ALFS/THIN lightweight filesystem for DOS'
TYPE_4c='Oberon partition'
TYPE_4d='QNX4.x'
TYPE_4e='QNX4.x 2nd part'
TYPE_4f='QNX4.x 3rd part'
TYPE_50='OnTrack Disk Manager (older versions) RO'
TYPE_51='OnTrack Disk Manager RW (DM6 Aux1)'
TYPE_52='CP/M'
TYPE_53='Disk Manager 6.0 Aux3'
TYPE_54='Disk Manager 6.0 Dynamic Drive Overlay (DDO)'
TYPE_55='EZ-Drive'
TYPE_56='Golden Bow VFeature Partitioned Volume.'
TYPE_57='DrivePro'
TYPE_5c='Priam EDisk'
TYPE_61='SpeedStor'
TYPE_63='Unix System V (SCO, ISC Unix, UnixWare, ...), Mach, GNU Hurd'
TYPE_64='Novell Netware 286, 2.xx'
TYPE_65='Novell Netware 386, 3.xx or 4.xx'
TYPE_66='Novell Netware SMS Partition'
TYPE_67='Novell'
TYPE_68='Novell'
TYPE_69='Novell Netware 5+, Novell Netware NSS Partition'
TYPE_70='DiskSecure Multi-Boot'
TYPE_72='V7/x86'
TYPE_74='Scramdisk partition'
TYPE_75='IBM PC/IX'
TYPE_77='M2FS/M2CS partition'
TYPE_77='VNDI Partition'
TYPE_78='XOSL FS'
TYPE_80='MINIX until 1.4a'
TYPE_81='MINIX since 1.4b, early Linux'
TYPE_82='Linux swap'
TYPE_83='Linux native partition'
TYPE_84='OS/2 hidden C: drive'
TYPE_85='Linux extended partition'
TYPE_86='FAT16 volume set'
TYPE_87='NTFS volume set'
TYPE_88='Linux plaintext partition table'
TYPE_8a='Linux Kernel Partition (used by AiR-BOOT)'
TYPE_8b='Legacy Fault Tolerant FAT32 volume'
TYPE_8c='Legacy Fault Tolerant FAT32 volume using BIOS extd INT 13h'
TYPE_8d='Free FDISK hidden Primary DOS FAT12 partitition'
TYPE_8e='Linux Logical Volume Manager partition'
TYPE_90='Free FDISK hidden Primary DOS FAT16 partitition'
TYPE_91='Free FDISK hidden DOS extended partitition'
TYPE_92='Free FDISK hidden Primary DOS large FAT16 partitition'
TYPE_93='Amoeba'
TYPE_94='Amoeba bad block table'
TYPE_95='MIT EXOPC native partitions'
TYPE_97='Free FDISK hidden Primary DOS FAT32 partitition'
TYPE_98='Free FDISK hidden Primary DOS FAT32 partitition (LBA)'
TYPE_98='Datalight ROM-DOS Super-Boot Partition'
TYPE_99='DCE376 logical drive'
TYPE_9a='Free FDISK hidden Primary DOS FAT16 partitition (LBA)'
TYPE_9b='Free FDISK hidden DOS extended partitition (LBA)'
TYPE_9f='BSD/OS'
TYPE_a0='Laptop hibernation partition'
TYPE_a1='HP Volume Expansion (SpeedStor variant)'
TYPE_a3='HP Volume Expansion (SpeedStor variant)'
TYPE_a4='HP Volume Expansion (SpeedStor variant)'
TYPE_a5='BSD/386, 386BSD, NetBSD, FreeBSD'
TYPE_a6='OpenBSD'
TYPE_a7='NeXTStep'
TYPE_a8='Mac OS-X'
TYPE_a9='NetBSD'
TYPE_aa='Olivetti Fat 12 1.44MB Service Partition'
TYPE_ab='Mac OS-X Boot partition'
TYPE_ae='ShagOS filesystem'
TYPE_af='MacOS X HFS'
TYPE_b0='BootStar Dummy'
TYPE_b1='HP Volume Expansion (SpeedStor variant)'
TYPE_b3='HP Volume Expansion (SpeedStor variant)'
TYPE_b4='HP Volume Expansion (SpeedStor variant)'
TYPE_b6='HP Volume Expansion (SpeedStor variant)'
TYPE_b7='BSDI BSD/386 filesystem'
TYPE_b8='BSDI BSD/386 swap partition'
TYPE_bb='Boot Wizard hidden'
TYPE_bc='Acronis backup partition'
TYPE_be='Solaris 8 boot partition'
TYPE_bf='New Solaris x86 partition'
TYPE_c0='DR-DOS/Novell DOS secured partition'
TYPE_c1='DRDOS/secured (FAT-12)'
TYPE_c2='Hidden Linux'
TYPE_c3='Hidden Linux swap'
TYPE_c4='DRDOS/secured (FAT-16, < 32M)'
TYPE_c5='DRDOS/secured (extended)'
TYPE_c6='DRDOS/secured (FAT-16, >= 32M)'
TYPE_c7='Syrinx boot'
TYPE_c8='Reserved for DR-DOS 8.0+'
TYPE_c9='Reserved for DR-DOS 8.0+'
TYPE_ca='Reserved for DR-DOS 8.0+'
TYPE_cb='DR-DOS 7.04+ secured FAT32 (CHS)/'
TYPE_cc='DR-DOS 7.04+ secured FAT32 (LBA)/'
TYPE_cd='CTOS Memdump'
TYPE_ce='DR-DOS 7.04+ FAT16X (LBA)/'
TYPE_cf='DR-DOS 7.04+ secured EXT DOS (LBA)/'
TYPE_d0='REAL/32 secure big partition'
TYPE_d0='Multiuser DOS secured partition'
TYPE_d1='Old Multiuser DOS secured FAT12'
TYPE_d4='Old Multiuser DOS secured FAT16 <32M'
TYPE_d5='Old Multiuser DOS secured extended partition'
TYPE_d6='Old Multiuser DOS secured FAT16 >=32M'
TYPE_d8='CP/M-86'
TYPE_da='Non-FS Data'
TYPE_db='Digital Research CP/M, Concurrent CP/M, Concurrent DOS'
TYPE_dd='Hidden CTOS Memdump'
TYPE_de='Dell PowerEdge Server utilities (FAT fs)'
TYPE_df='BootIt EMBRM'
TYPE_e0='Reserved by STMicroelectronics for a filesystem called ST AVFS.'
TYPE_e1='DOS access or SpeedStor 12-bit FAT extended partition'
TYPE_e3='DOS R/O or SpeedStor'
TYPE_e4='SpeedStor 16-bit FAT extended partition < 1024 cyl.'
TYPE_e5='Tandy MSDOS with logically sectored FAT'
TYPE_e6='Storage Dimensions SpeedStor'
TYPE_e8='LUKS'
TYPE_eb='BeOS BFS'
TYPE_ec='SkyOS SkyFS'
TYPE_ee='MBR is followed by an EFI header'
TYPE_ef='EFI file system'
TYPE_f0='Linux/PA-RISC boot loader'
TYPE_f1='Storage Dimensions SpeedStor'
TYPE_f2='DOS 3.3+ secondary partition'
TYPE_f4='SpeedStor large partition'
TYPE_f5='Prologue multi-volume partition'
TYPE_f6='Storage Dimensions SpeedStor'
TYPE_f9='pCache'
TYPE_fa='Bochs'
TYPE_fb='VMware File System partition'
TYPE_fc='VMware Swap partition'
TYPE_fd='Linux raid partition with autodetect using persistent superblock'
TYPE_fe='LANstep'
TYPE_ff='Xenix Bad Block Table'

################################################################################

if [ -n "${HDD}" ]; then
	if [ -b ${HDD} -o -f ${HDD} ]; then
		exec < "${HDD}"
	else
		echo "${CMD_NAME}: \"${HDD}\" does not exit, or is not block-device or file" 1>&2
		exit 1
	fi
fi

if [ -z "${PARTITION}" ]; then
	START=0
	END=3
else
	if [ ${PARTITION} -lt 1 -o ${PARTITION} -gt 4 ]; then
		echo "${CMD_NAME}: partition number should be 1..4." 1>&2
		exit 1
	fi
	START=`expr ${PARTITION} - 1`
	END=${START}
fi

PART=`dd bs=1 skip=446 count=66 2> /dev/null | 	\
	hexdump -v -e	"			\
		 1/1 \"%02x\" \" \"	\
		 3/1 \"%3u \" \" \"	\
		 1/1 \"%02x\" \" \"	\
		 3/1 \"%3u \" \" \"	\
		 2/4 \"%10u \" \" \" "	\
	`

if [ "${PART:208:6}" != "55 170" ]; then
	echo "${CMD_NAME}: partition table may be corrupted."
fi

for ((p=${START} ; p <= ${END} ; p++ )) do
	FLAG=${PART:`expr ${p} \* 52 + 0`:2}
	HD=${PART:`expr ${p} \* 52 + 3`:3}
	SC=${PART:`expr ${p} \* 52 + 7`:3}
	CY=${PART:`expr ${p} \* 52 + 11`:3}
	S_HD=`expr ${HD}`
	S_SC=`expr ${SC} % 64`
	S_CY=`expr ${SC} / 64 \* 256 + ${CY}`
	TYPE=${PART:`expr ${p} \* 52 + 15`:2}
	eval "TYPE_INFO=\${TYPE_${TYPE}}"
	HD=${PART:`expr ${p} \* 52 + 18`:3}
	SC=${PART:`expr ${p} \* 52 + 22`:3}
	CY=${PART:`expr ${p} \* 52 + 26`:3}
	E_HD=`expr ${HD}`
	E_SC=`expr ${SC} % 64`
	E_CY=`expr ${SC} / 64 \* 256 + ${CY}`
	S_LBA=${PART:`expr ${p} \* 52 + 30`:10}; S_LBA=`expr ${S_LBA}`
	N_LBA=${PART:`expr ${p} \* 52 + 41`:10}; N_LBA=`expr ${N_LBA}`

	printf "Partition %d\n" "`expr ${p} + 1`"
	printf "\tBoot Flag\t= [%s]\n" "${FLAG}"
	printf "\tStart Hd/Sc/Cy\t= [%02x/%02x/%03x] %d/%d/%d\n" "${S_HD}" "${S_SC}" "${S_CY}" "${S_HD}" "${S_SC}" "${S_CY}"
	printf "\tPartition Type\t= [%s] \"%s\"\n" "${TYPE}" "${TYPE_INFO}"
	printf "\tEnd Hd/Sc/Cy\t= [%02x/%02x/%03x] %d/%d/%d\n" "${E_HD}" "${E_SC}" "${E_CY}" "${E_HD}" "${E_SC}" "${E_CY}"
	printf "\tStart LBA\t= [%08x] %'d sector (%'d bytes offset)\n" "${S_LBA}" "${S_LBA}" `expr "${S_LBA}" \* 512`
	printf "\tNumber of LBA\t= [%08x] %'d sectors (%'d bytes)\n" "${N_LBA}" "${N_LBA}" `expr "${N_LBA}" \* 512`
done