Skip to content

Commit e33e962

Browse files
committed
Updates per shellcheck
1 parent 30ef3f3 commit e33e962

File tree

1 file changed

+40
-40
lines changed

1 file changed

+40
-40
lines changed

format-udf.sh

+40-40
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,12 @@ EOF
105105
# None
106106
function lba_to_chs {
107107
LBA=$1
108-
C=$((($LBA/($HPC*$SPT)) % (2**10)))
109-
C_HI=$((($C>>8) % (2**2)))
110-
C_LO=$(($C % (2**8)))
111-
H=$(((($LBA/$SPT) % $HPC) % (2**8)))
112-
S=$(((($LBA % $SPT) + 1) % (2**6)))
113-
printf "%02x%02x%02x" $H $((($C_HI<<6)|$S)) $C_LO
108+
C=$(((LBA/(HPC*SPT)) % (2**10)))
109+
C_HI=$(((C>>8) % (2**2)))
110+
C_LO=$((C % (2**8)))
111+
H=$((((LBA/SPT) % HPC) % (2**8)))
112+
S=$((((LBA % SPT) + 1) % (2**6)))
113+
printf "%02x%02x%02x" $H $(((C_HI<<6)|S)) $C_LO
114114
}
115115

116116

@@ -122,10 +122,10 @@ function lba_to_chs {
122122
function ntohl {
123123
if sed --version &> /dev/null; then
124124
# this box has GNU sed ('-r' for extended regex)
125-
printf "%08x" $1 | tail -c 8 | sed -r 's/(..)/\1 /g' | awk '{print $4 $3 $2 $1}'
125+
printf "%08x" "$1" | tail -c 8 | sed -r 's/(..)/\1 /g' | awk '{print $4 $3 $2 $1}'
126126
else
127127
# this machine must have BSD sed ('-E' for extended regex)
128-
printf "%08x" $1 | tail -c 8 | sed -E 's/(..)/\1 /g' | awk '{print $4 $3 $2 $1}'
128+
printf "%08x" "$1" | tail -c 8 | sed -E 's/(..)/\1 /g' | awk '{print $4 $3 $2 $1}'
129129
fi
130130
}
131131

@@ -141,7 +141,7 @@ function ntohl {
141141
function entire_disk_partition_entry {
142142
TOTAL_SIZE=$1
143143
BLOCK_SIZE=$2
144-
MAX_LBA=$(($TOTAL_SIZE/$BLOCK_SIZE))
144+
MAX_LBA=$((TOTAL_SIZE/BLOCK_SIZE))
145145

146146
# status / physical drive (bit 7 set: active / bootable, old MBRs only accept 80h), 00h: inactive, 01h–7Fh: invalid)
147147
echo -n "00"
@@ -150,13 +150,13 @@ function entire_disk_partition_entry {
150150
# Partition type = FAT32 with CHS addressing
151151
echo -n "0b"
152152
# CHS address of last absolute sector in partition. The format is described by 3 bytes.
153-
if [[ $MAX_LBA -ge $((1024*$HPC*$SPT-1)) ]]; then
153+
if [[ $MAX_LBA -ge $((1024*HPC*SPT-1)) ]]; then
154154
# From https://en.wikipedia.org/wiki/Master_boot_record#Partition_table_entries
155155
# When a CHS address is too large to fit into these fields, the tuple (1023, 254, 63) is typically used today
156156
echo -n "feffff"
157157
else
158158
# '-1' yields last usable sector
159-
lba_to_chs $(($MAX_LBA-1))
159+
lba_to_chs $((MAX_LBA-1))
160160
fi
161161

162162
# LBA of first absolute sector in the partition.
@@ -355,8 +355,8 @@ LABEL=$2
355355

356356
# verify this is a device, not just a file
357357
# `true` is so that a failure here doesn't cause entire script to exit prematurely
358-
mount /dev/$DEVICE 2>/dev/null || true
359-
[[ -b /dev/$DEVICE ]] || (echo "[-] /dev/$DEVICE either doesn't exist or is not block special" >&2; false)
358+
mount "/dev/$DEVICE" 2>/dev/null || true
359+
[[ -b "/dev/$DEVICE" ]] || (echo "[-] /dev/$DEVICE either doesn't exist or is not block special" >&2; false)
360360

361361
# provide assuring exit message
362362
trap exit_with_no_changes EXIT
@@ -395,12 +395,12 @@ fi
395395
###############################################################################
396396

397397
echo "[+] Gathering drive information..."
398-
if [[ $TOOL_DRIVE_LISTING = $TOOL_BLOCKDEV ]]; then
399-
sudo blkid -c /dev/null /dev/$DEVICE || true
400-
cat /sys/block/$PARENT_DEVICE/device/model
398+
if [[ $TOOL_DRIVE_LISTING = "$TOOL_BLOCKDEV" ]]; then
399+
sudo blkid -c /dev/null "/dev/$DEVICE" || true
400+
cat "/sys/block/$PARENT_DEVICE/device/model"
401401
sudo blockdev --report | egrep "(Device|$DEVICE)"
402-
elif [[ $TOOL_DRIVE_LISTING = $TOOL_DISKUTIL ]]; then
403-
diskutil list $DEVICE
402+
elif [[ $TOOL_DRIVE_LISTING = "$TOOL_DISKUTIL" ]]; then
403+
diskutil list "$DEVICE"
404404
else
405405
echo "[-] Internal error 1" >&2
406406
exit 1
@@ -417,7 +417,7 @@ if [[ -z $FORCE ]]; then
417417
echo "For maximal compatibility, the recommendation is to format the entire device."
418418
echo "If you continue, the resultant UDF partition will not be recognized on OS X."
419419
read -p "Type 'yes' if this is what you intend: " YES_CASE
420-
YES=$(echo $YES_CASE | tr '[:upper:]' '[:lower:]')
420+
YES=$(echo "$YES_CASE" | tr '[:upper:]' '[:lower:]')
421421
if [[ $YES != "yes" ]]; then
422422
exit 1
423423
fi
@@ -426,7 +426,7 @@ if [[ -z $FORCE ]]; then
426426
# give the user a chance to realize his/her mistake
427427
echo "The above-listed device (and partitions, if any) will be completely erased."
428428
read -p "Type 'yes' if this is what you intend: " YES_CASE
429-
YES=$(echo $YES_CASE | tr '[:upper:]' '[:lower:]')
429+
YES=$(echo "$YES_CASE" | tr '[:upper:]' '[:lower:]')
430430
if [[ $YES != "yes" ]]; then
431431
exit 1
432432
fi
@@ -438,10 +438,10 @@ fi
438438
###############################################################################
439439

440440
echo "[+] Detecting total size..."
441-
if [[ $TOOL_DRIVE_LISTING = $TOOL_BLOCKDEV ]]; then
442-
TOTAL_SIZE=$(sudo blockdev --getsize64 /dev/$DEVICE)
441+
if [[ $TOOL_DRIVE_LISTING = "$TOOL_BLOCKDEV" ]]; then
442+
TOTAL_SIZE=$(sudo blockdev --getsize64 "/dev/$DEVICE")
443443
elif [[ -x $TOOL_DISKUTIL ]]; then
444-
TOTAL_SIZE=$(diskutil info $DEVICE | egrep -i '(Total|Disk) Size' | awk -F ':' '{print $2}' | egrep -oi '\([0-9]+ B' | sed 's/[^0-9]//g')
444+
TOTAL_SIZE=$(diskutil info "$DEVICE" | egrep -i '(Total|Disk) Size' | awk -F ':' '{print $2}' | egrep -oi '\([0-9]+ B' | sed 's/[^0-9]//g')
445445
else
446446
echo "[-] Cannot detect total size" >&2
447447
exit 1
@@ -460,10 +460,10 @@ echo "[+] Validating detected total size..."
460460

461461
if [[ -z $ARG_BLOCK_SIZE ]]; then
462462
echo "[+] Detecting physical block size..."
463-
if [[ $TOOL_DRIVE_LISTING = $TOOL_BLOCKDEV ]]; then
464-
BLOCK_SIZE=$(sudo blockdev --getpbsz /dev/$DEVICE)
463+
if [[ $TOOL_DRIVE_LISTING = "$TOOL_BLOCKDEV" ]]; then
464+
BLOCK_SIZE=$(sudo blockdev --getpbsz "/dev/$DEVICE")
465465
elif [[ -x $TOOL_DISKUTIL ]]; then
466-
BLOCK_SIZE=$(diskutil info $DEVICE | grep -i 'Device Block Size' | awk -F ':' '{print $2}' | awk '{print $1}')
466+
BLOCK_SIZE=$(diskutil info "$DEVICE" | grep -i 'Device Block Size' | awk -F ':' '{print $2}' | awk '{print $1}')
467467
else
468468
echo "[-] Cannot detect physical block size" >&2
469469
exit 1
@@ -485,12 +485,12 @@ echo "[+] Validating detected block size..."
485485
###############################################################################
486486

487487
echo "[+] Unmounting device..."
488-
if [[ $TOOL_UNMOUNT = $TOOL_UMOUNT ]]; then
488+
if [[ $TOOL_UNMOUNT = "$TOOL_UMOUNT" ]]; then
489489
# `true` is so that a failure here doesn't cause entire script to exit prematurely
490-
sudo umount /dev/$DEVICE || true
491-
elif [[ $TOOL_UNMOUNT = $TOOL_DISKUTIL ]]; then
490+
sudo umount "/dev/$DEVICE" || true
491+
elif [[ $TOOL_UNMOUNT = "$TOOL_DISKUTIL" ]]; then
492492
# `true` is so that a failure here doesn't cause entire script to exit prematurely
493-
sudo diskutil unmountDisk /dev/$DEVICE || true
493+
sudo diskutil unmountDisk "/dev/$DEVICE" || true
494494
else
495495
echo "[-] Internal error 2" >&2
496496
exit 1
@@ -510,11 +510,11 @@ case $WIPE_METHOD in
510510
;;
511511
zero)
512512
echo "[+] Overwriting device with zeros. This will likely take a LONG time..."
513-
sudo dd if=/dev/zero of=/dev/$DEVICE bs=$BLOCK_SIZE || true
513+
sudo dd if=/dev/zero of="/dev/$DEVICE" bs="$BLOCK_SIZE" || true
514514
;;
515515
scrub)
516516
echo "[+] Scrubbing device with random patterns. This will likely take a LONG time..."
517-
sudo scrub -f /dev/$DEVICE
517+
sudo scrub -f "/dev/$DEVICE"
518518
;;
519519
*)
520520
echo "[-] Internal error 3" >&2
@@ -529,30 +529,30 @@ esac
529529

530530
echo "[+] Zeroing out first chunk of device..."
531531
# 4096 was arbitrarily chosen to be "big enough" to delete first chunk of device
532-
sudo dd if=/dev/zero of=/dev/$DEVICE bs=$BLOCK_SIZE count=4096
532+
sudo dd if=/dev/zero of="/dev/$DEVICE" bs="$BLOCK_SIZE" count=4096
533533

534534

535535
###############################################################################
536536
# format device
537537
###############################################################################
538538

539539
echo "[+] Formatting /dev/$DEVICE ..."
540-
if [[ $TOOL_UDF = $TOOL_MKUDFFS ]]; then
540+
if [[ $TOOL_UDF = "$TOOL_MKUDFFS" ]]; then
541541
# --utf8 - encode file names in UTF8 (since pali/udftools@52afdce, this must be specified as the first argument)
542542
# --blocksize - the size of blocks in bytes. should be the same as the drive's physical block size.
543543
# --udfrev - the udf revision to use. 2.01 is the latest revision available that supports writing in Linux.
544544
# --lvid - logical volume identifier
545545
# --vid - volume identifier
546546
# --media-type - "hd" type covers both hard drives and USB drives
547-
(sudo mkudffs --utf8 --blocksize=$BLOCK_SIZE --udfrev=0x0201 --lvid="$LABEL" --vid="$LABEL" --media-type=hd /dev/$DEVICE) || (echo "[-] Format failed!" >&2; false)
548-
elif [[ $TOOL_UDF = $TOOL_NEWFS_UDF ]]; then
547+
(sudo mkudffs --utf8 --blocksize="$BLOCK_SIZE" --udfrev=0x0201 --lvid="$LABEL" --vid="$LABEL" --media-type=hd "/dev/$DEVICE") || (echo "[-] Format failed!" >&2; false)
548+
elif [[ $TOOL_UDF = "$TOOL_NEWFS_UDF" ]]; then
549549
# -b - the size of blocks in bytes. should be the same as the drive's physical block size.
550550
# -m - "blk" type covers both hard drives and USB drives
551551
# -t - "overwrite" access type
552552
# -r - the udf revision to use. 2.01 is the latest revision available that supports writing in Linux.
553553
# -v - volume identifier
554554
# --enc - encode volume name in UTF8
555-
(sudo newfs_udf -b $BLOCK_SIZE -m blk -t ow -r 2.01 -v "$LABEL" --enc utf8 /dev/$DEVICE) || (echo "[-] Format failed!" >&2; false)
555+
(sudo newfs_udf -b "$BLOCK_SIZE" -m blk -t ow -r 2.01 -v "$LABEL" --enc utf8 "/dev/$DEVICE") || (echo "[-] Format failed!" >&2; false)
556556
else
557557
echo "[-] Internal error 4" >&2
558558
exit 1
@@ -570,9 +570,9 @@ case $PARTITION_TYPE in
570570
mbr)
571571
echo "[+] Writing fake MBR..."
572572
# first block has already been zero'd. start by writing the (only) partition entry at its correct offset.
573-
entire_disk_partition_entry $TOTAL_SIZE $BLOCK_SIZE | xxd -r -p | sudo dd of=/dev/$DEVICE bs=1 seek=446 count=16
573+
entire_disk_partition_entry "$TOTAL_SIZE" "$BLOCK_SIZE" | xxd -r -p | sudo dd of="/dev/$DEVICE" bs=1 seek=446 count=16
574574
# Boot signature at the end of the block
575-
echo -n 55aa | xxd -r -p | sudo dd of=/dev/$DEVICE bs=1 seek=510 count=2
575+
echo -n 55aa | xxd -r -p | sudo dd of="/dev/$DEVICE" bs=1 seek=510 count=2
576576
;;
577577
*)
578578
echo "[-] Internal error 5" >&2
@@ -587,7 +587,7 @@ esac
587587

588588
# following call to blkid sometimes exits with failure, even though the device is formatted properly.
589589
# `true` is so that a failure here doesn't cause entire script to exit prematurely
590-
SUMMARY=$([[ -x $(which blkid) ]] && sudo blkid -c /dev/null /dev/$DEVICE 2>/dev/null) || true
590+
SUMMARY=$([[ -x $(which blkid) ]] && sudo blkid -c /dev/null "/dev/$DEVICE" 2>/dev/null) || true
591591
echo "[+] Successfully formatted $SUMMARY"
592592

593593
# TODO find a way to auto-mount (`sudo mount -a` doesn't work). in the meantime...

0 commit comments

Comments
 (0)