Translate

Image of Beginning Google Maps API 3
Image of XSLT 2.0 and XPath 2.0 Programmer's Reference (Programmer to Programmer)
Image of Operating System Concepts
Image of Linux Kernel Development (3rd Edition)

Bootable DVD/CDROM

Have you ever wanted to determine whether a DVD or CDROM is a bootable CDROM or DVD from within a shell script?  If you use GNU/Linux you could parse the output from the isoinfo utility which is part of the CDRecord tools suite.

What if the isoinfo utility is not available on your operating system?  In this case you have to resort to rolling you own compiled langauge utility or shell script.  To do this you need to understand where to find the specific bytes on a CDROM/DVD which indicate whether it is a bootable CDROM/DVD or not.  The relevant specification covering bootable CDROMs (and by extension bootable DVDs) is the El Torito specification which is an extension to the ISO 9660 specification.

Our first shell script uses the dd utility to read in the required bytes which are then sliced and diced using the xxd and cut utilities..  Sector 17 (hexadecimal 11, the Boot Record Volume) contains two bytes at a specific offset which are an absolute pointer to the Booting Catalog which in turn contains a specific byte which indicates whether a CDROM/DVD is bootable or not.

#!/bin/bash
#
# use dd to read in the required bytes
#

DEVICE="/dev/sr0"
VERBOSE=1

# get 2 bytes at absolute offset 34887
bchex=`dd if=${DEVICE} skip=$(( 0x800 * 0x11 + 0x47))  bs=1 count=2 conv=swab 2>/dev/null | xxd | cut -d" " -f2`

# get one byte
bcdec=`printf "%d" 0x${bchex}`
bootable=`dd if=${DEVICE}  bs=1 skip=$(( 0x800 * bcdec + 0x20))  count=1  2>/dev/null | xxd | cut -d" " -f2`

if [[ $bootable = 88 ]]; then
   [ $VERBOSE ] && printf "Bootable CDROM\n"
   exit 0
else
   [ $VERBOSE ] && printf "Not a bootable CDROM\n"
   exit 1
fi

The previous shell script is classical in its approach to the problem in that it uses a number of small specialized utilities to achieve the desired result.

Another way of achieving the same result is to use the extended I/O features of ksh93 as shown in the following shell script.

#!/bin/ksh93
#
# Cannot open DVD/CDROM for read/write - hence cannot seek 
# forward on file descriptor. Workaround is to close and 
# reopen the file descriptor.
#

DEVICE="/dev/sr0"
VERBOSE=1

# read 2 bytes
redirect 3< $DEVICE || exit 3
3<#(( 0x800 * 0x11 + 0x47 ))
read -u3 -N 1 bchex1
read -u3 -N 1 bchex2
redirect 3<&-

# calculate absolute offset
integer bcdec=$(printf "%d" 0x$(printf "%x%x\n"  "'$bchex2" "'$bchex1"))

# read 1 byte
redirect 3< $DEVICE || exit 3
3<#(( 0x800 * bcdec + 0x20 ))
read -u3 -N 1 bootable
redirect 3<&-

if [[ $(printf "%x" "'$bootable") = 88 ]]; then
   [ $VERBOSE ] && printf "Bootable CDROM\n"
   exit 0
else
   [ $VERBOSE ] && printf "Not a bootable CDROM\n"
   exit 1
fi

The advantage of this approach is that all operations are done within the existing shell process without the overhead of fork/execs to invoke external utilities such as dd etc.

Note that both of the above shell scripts assume that the CDROM or DVD being checked is either non-bootable or contains a single boot-image configuration.  They can easily be extended to check for a multiple boot-image configuration.  That is left as an exercise for you the reader.

For the record, these two shell scripts were tested on Fedora 10 using version M of ksh93.
 

Comments are closed.