@@ -105,12 +105,12 @@ EOF
105
105
# None
106
106
function lba_to_chs {
107
107
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
114
114
}
115
115
116
116
@@ -122,10 +122,10 @@ function lba_to_chs {
122
122
function ntohl {
123
123
if sed --version & > /dev/null; then
124
124
# 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}'
126
126
else
127
127
# 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}'
129
129
fi
130
130
}
131
131
@@ -141,7 +141,7 @@ function ntohl {
141
141
function entire_disk_partition_entry {
142
142
TOTAL_SIZE=$1
143
143
BLOCK_SIZE=$2
144
- MAX_LBA=$(( $ TOTAL_SIZE/ $ BLOCK_SIZE))
144
+ MAX_LBA=$(( TOTAL_SIZE/ BLOCK_SIZE))
145
145
146
146
# status / physical drive (bit 7 set: active / bootable, old MBRs only accept 80h), 00h: inactive, 01h–7Fh: invalid)
147
147
echo -n " 00"
@@ -150,13 +150,13 @@ function entire_disk_partition_entry {
150
150
# Partition type = FAT32 with CHS addressing
151
151
echo -n " 0b"
152
152
# 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
154
154
# From https://en.wikipedia.org/wiki/Master_boot_record#Partition_table_entries
155
155
# When a CHS address is too large to fit into these fields, the tuple (1023, 254, 63) is typically used today
156
156
echo -n " feffff"
157
157
else
158
158
# '-1' yields last usable sector
159
- lba_to_chs $(( $ MAX_LBA- 1 ))
159
+ lba_to_chs $(( MAX_LBA- 1 ))
160
160
fi
161
161
162
162
# LBA of first absolute sector in the partition.
@@ -355,8 +355,8 @@ LABEL=$2
355
355
356
356
# verify this is a device, not just a file
357
357
# `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)
360
360
361
361
# provide assuring exit message
362
362
trap exit_with_no_changes EXIT
395
395
# ##############################################################################
396
396
397
397
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"
401
401
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 "
404
404
else
405
405
echo " [-] Internal error 1" >&2
406
406
exit 1
@@ -417,7 +417,7 @@ if [[ -z $FORCE ]]; then
417
417
echo " For maximal compatibility, the recommendation is to format the entire device."
418
418
echo " If you continue, the resultant UDF partition will not be recognized on OS X."
419
419
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:]' )
421
421
if [[ $YES != " yes" ]]; then
422
422
exit 1
423
423
fi
@@ -426,7 +426,7 @@ if [[ -z $FORCE ]]; then
426
426
# give the user a chance to realize his/her mistake
427
427
echo " The above-listed device (and partitions, if any) will be completely erased."
428
428
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:]' )
430
430
if [[ $YES != " yes" ]]; then
431
431
exit 1
432
432
fi
438
438
# ##############################################################################
439
439
440
440
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 " )
443
443
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' )
445
445
else
446
446
echo " [-] Cannot detect total size" >&2
447
447
exit 1
@@ -460,10 +460,10 @@ echo "[+] Validating detected total size..."
460
460
461
461
if [[ -z $ARG_BLOCK_SIZE ]]; then
462
462
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 " )
465
465
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}' )
467
467
else
468
468
echo " [-] Cannot detect physical block size" >&2
469
469
exit 1
@@ -485,12 +485,12 @@ echo "[+] Validating detected block size..."
485
485
# ##############################################################################
486
486
487
487
echo " [+] Unmounting device..."
488
- if [[ $TOOL_UNMOUNT = $TOOL_UMOUNT ]]; then
488
+ if [[ $TOOL_UNMOUNT = " $TOOL_UMOUNT " ]]; then
489
489
# `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
492
492
# `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
494
494
else
495
495
echo " [-] Internal error 2" >&2
496
496
exit 1
@@ -510,11 +510,11 @@ case $WIPE_METHOD in
510
510
;;
511
511
zero)
512
512
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
514
514
;;
515
515
scrub)
516
516
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 "
518
518
;;
519
519
* )
520
520
echo " [-] Internal error 3" >&2
@@ -529,30 +529,30 @@ esac
529
529
530
530
echo " [+] Zeroing out first chunk of device..."
531
531
# 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
533
533
534
534
535
535
# ##############################################################################
536
536
# format device
537
537
# ##############################################################################
538
538
539
539
echo " [+] Formatting /dev/$DEVICE ..."
540
- if [[ $TOOL_UDF = $TOOL_MKUDFFS ]]; then
540
+ if [[ $TOOL_UDF = " $TOOL_MKUDFFS " ]]; then
541
541
# --utf8 - encode file names in UTF8 (since pali/udftools@52afdce, this must be specified as the first argument)
542
542
# --blocksize - the size of blocks in bytes. should be the same as the drive's physical block size.
543
543
# --udfrev - the udf revision to use. 2.01 is the latest revision available that supports writing in Linux.
544
544
# --lvid - logical volume identifier
545
545
# --vid - volume identifier
546
546
# --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
549
549
# -b - the size of blocks in bytes. should be the same as the drive's physical block size.
550
550
# -m - "blk" type covers both hard drives and USB drives
551
551
# -t - "overwrite" access type
552
552
# -r - the udf revision to use. 2.01 is the latest revision available that supports writing in Linux.
553
553
# -v - volume identifier
554
554
# --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)
556
556
else
557
557
echo " [-] Internal error 4" >&2
558
558
exit 1
@@ -570,9 +570,9 @@ case $PARTITION_TYPE in
570
570
mbr)
571
571
echo " [+] Writing fake MBR..."
572
572
# 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
574
574
# 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
576
576
;;
577
577
* )
578
578
echo " [-] Internal error 5" >&2
587
587
588
588
# following call to blkid sometimes exits with failure, even though the device is formatted properly.
589
589
# `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
591
591
echo " [+] Successfully formatted $SUMMARY "
592
592
593
593
# TODO find a way to auto-mount (`sudo mount -a` doesn't work). in the meantime...
0 commit comments