Skip to content

Commit a925219

Browse files
oem-factory-reset: Improve prompt flow formatting flash drive
Combine prompt to disconnect other devices with prompt to connect the desired device. Show block device sizes in MB/GB when selecting device so it is easier to select. file_selector now supports --show-size to include block device sizes in menu. Rework file_selector so menu options can contain spaces (use bash array) and to simplify logic. Prompt to select flash drive and LUKS percentage in OEM reset before actually taking any actions, so aborting doesn't half-reset the system. Abort OEM reset if user aborts the flash drive selection instead of looping forever. (Canceling the confirmation still loops to retry but it is possible to exit by aborting the repeated menu.) Signed-off-by: Jonathon Hall <jonathon.hall@puri.sm>
1 parent e924a8a commit a925219

File tree

2 files changed

+143
-96
lines changed

2 files changed

+143
-96
lines changed

initrd/bin/oem-factory-reset

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -307,22 +307,12 @@ keytocard_subkeys_to_smartcard() {
307307
TRACE "oem-factory-reset:keytocard_subkeys_to_smartcard done"
308308
}
309309

310-
#Whiptail prompt to disconnect any external USB storage device
311-
prompt_disconnect_external_USB_storage_device() {
312-
TRACE "Under oem-factory-reset:disconnect_external_USB_storage_device"
313-
#Whiptail $BG_COLOR_WARNING warning about removing any external USB storage device currently connected
314-
whiptail $BG_COLOR_WARNING --title 'WARNING: Please disconnect any external USB storage device' \
315-
--msgbox "An external USB storage device will be WIPED next.\n\nPlease disconnect all external USB storage devices." 0 80 ||
316-
die "Error displaying warning about removing any external USB storage device currently connected"
317-
318-
}
319-
320310
#Whiptail prompt to insert to be wiped thumb drive
321311
prompt_insert_to_be_wiped_thumb_drive() {
322312
TRACE "Under oem-factory-reset:prompt_insert_to_be_wiped_thumb_drive"
323313
#Whiptail warning about having only desired to be wiped thumb drive inserted
324314
whiptail $BG_COLOR_WARNING --title 'WARNING: Please insert the thumb drive to be wiped' \
325-
--msgbox "The thumb drive will be WIPED next.\n\nPlease have connected only the thumb drive to be wiped." 0 80 ||
315+
--msgbox "The thumb drive will be WIPED next.\n\nPlease connect only the thumb drive to be wiped and disconnect others." 0 80 ||
326316
die "Error displaying warning about having only desired to be wiped thumb drive inserted"
327317
}
328318

@@ -412,10 +402,13 @@ export_public_key_to_thumbdrive_public_partition() {
412402
TRACE "oem-factory-reset:export_public_key_to_thumbdrive_public_partition done"
413403
}
414404

415-
#Wipe a thumb drive and export master key and subkeys to it
416-
wipe_thumb_drive_and_copy_gpg_key_material() {
405+
# Select thumb drive and LUKS container size for GPG key export
406+
# Sets variables containing selections:
407+
# - thumb_drive
408+
# - thumb_drive_luks_percent
409+
select_thumb_drive_for_key_material() {
417410
TRACE "Under oem-factory-reset:wipe_thumb_drive_and_copy_gpg_key_material"
418-
prompt_disconnect_external_USB_storage_device
411+
419412
#enable usb storage
420413
enable_usb
421414
enable_usb_storage
@@ -426,41 +419,32 @@ wipe_thumb_drive_and_copy_gpg_key_material() {
426419
prompt_insert_to_be_wiped_thumb_drive
427420
#list usb storage devices
428421
list_usb_storage disks >/tmp/usb_disk_list
429-
if [ $(cat /tmp/usb_disk_list | wc -l) -gt 0 ]; then
430-
file_selector "/tmp/usb_disk_list" "Select USB device to partition"
431-
if [ "$FILE" == "" ]; then
432-
#No USB storage device selected
433-
warn "No USB storage device selected!"
434-
else
435-
# Obtain size of thumb drive to be wiped with fdisk
436-
disk_size_bytes="$(blockdev --getsize64 "$FILE")"
437-
#Convert disk size to GB
438-
thumb_drive_size_mb=$((disk_size_bytes / 1024 / 1024))
439-
thumb_drive_size_gb=$((thumb_drive_size_mb / 1024 ))
440-
441-
#if thumb_drive_size_gb is 0, then disk size is less than 1GB
442-
thumb_drive_size_message=""
443-
if [ "$thumb_drive_size_gb" -eq 0 ]; then
444-
thumb_drive_size_message="$thumb_drive_size_mb MB"
445-
if [ "$thumb_drive_size_mb" -lt 128 ]; then
446-
warn "Thumb drive size is less than 128MB!"
447-
warn "LUKS container needs to be at least 8mb!"
448-
warn "If the next operation fails, try with a bigger thumb drive"
449-
fi
450-
else
451-
thumb_drive_size_message="$thumb_drive_size_gb GB"
452-
fi
453-
454-
# confirm with user size of thumb drive to be wiped
455-
whiptail --title "Confirm thumb drive to be wiped" --yesno "Are you sure you want to wipe the following thumb drive?\n\n$FILE\n\nSize: $thumb_drive_size_message" 0 0
456-
if [ $? -ne 0 ]; then
457-
warn "Thumb drive wipe aborted by user!"
458-
continue
459-
fi
460-
461-
#User chose and confirmed a thumb drive and its size to be wiped
462-
thumb_drive=$FILE
422+
# Abort if:
423+
# - no disks found (prevent file_selector's nonsense prompt)
424+
# - file_selector fails for any reason
425+
# - user aborts (file_selector succeeds but FILE is empty)
426+
if [ $(cat /tmp/usb_disk_list | wc -l) -gt 0 ] &&
427+
file_selector --show-size "/tmp/usb_disk_list" "Select USB device to partition" &&
428+
[ -n "$FILE" ]; then
429+
# Obtain size of thumb drive to be wiped with fdisk
430+
disk_size_bytes="$(blockdev --getsize64 "$FILE")"
431+
if [ "$disk_size_bytes" -lt "$((128*1024*1024))" ]; then
432+
warn "Thumb drive size is less than 128MB!"
433+
warn "LUKS container needs to be at least 8MB!"
434+
warn "If the next operation fails, try with a bigger thumb drive"
435+
fi
436+
437+
thumb_drive_size_message="$(display_size "$disk_size_bytes")"
438+
# confirm with user size of thumb drive to be wiped
439+
whiptail --title "Confirm thumb drive to be wiped" --yesno \
440+
"Are you sure you want to wipe the following thumb drive?\n\n$FILE\n\nSize: $thumb_drive_size_message" 0 0
441+
if [ $? -ne 0 ]; then
442+
warn "Thumb drive wipe aborted by user!"
443+
continue
463444
fi
445+
446+
#User chose and confirmed a thumb drive and its size to be wiped
447+
thumb_drive=$FILE
464448
else
465449
#No USB storage device detected
466450
warn "No USB storage device detected! Aborting OEM Factory Reset / Re-Ownership"
@@ -470,8 +454,21 @@ wipe_thumb_drive_and_copy_gpg_key_material() {
470454
done
471455

472456
select_luks_container_size_percent
457+
thumb_drive_luks_percent="$(cat /tmp/luks_container_size_percent)"
458+
}
459+
460+
#Wipe a thumb drive and export master key and subkeys to it
461+
# $1 - thumb drive block device
462+
# $2 - LUKS container percentage [1-99]
463+
wipe_thumb_drive_and_copy_gpg_key_material() {
464+
TRACE "Under oem-factory-reset:wipe_thumb_drive_and_copy_gpg_key_material"
465+
466+
local thumb_drive thumb_drive_luks_percent
467+
thumb_drive="$1"
468+
thumb_drive_luks_percent="$2"
469+
473470
#Wipe thumb drive with a LUKS container of size $(cat /tmp/luks_container_size_percent)
474-
prepare_thumb_drive --device "$thumb_drive" --percentage "$(cat /tmp/luks_container_size_percent)" --pass "${ADMIN_PIN}"
471+
prepare_thumb_drive --device "$thumb_drive" --percentage "$thumb_drive_luks_percent" --pass "${ADMIN_PIN}"
475472
#Export master key and subkeys to thumb drive first partition
476473
export_master_key_subkeys_and_revocation_key_to_private_LUKS_container --mode rw --device "$thumb_drive"1 --mountpoint /media --pass "${ADMIN_PIN}"
477474
#Export public key to thumb drive's public partition
@@ -1068,6 +1065,10 @@ if [ "$use_defaults" == "n" -o "$use_defaults" == "N" ]; then
10681065
}
10691066
done
10701067
fi
1068+
1069+
if [ "$GPG_GEN_KEY_IN_MEMORY" = "y" ]; then
1070+
select_thumb_drive_for_key_material
1071+
fi
10711072
fi
10721073

10731074
# If nothing is stored in custom variables, we set them to their defaults
@@ -1184,7 +1185,7 @@ if [ "$GPG_GEN_KEY_IN_MEMORY" = "y" ]; then
11841185
else
11851186
die "Unsupported GPG_ALGO: $GPG_ALGO"
11861187
fi
1187-
wipe_thumb_drive_and_copy_gpg_key_material
1188+
wipe_thumb_drive_and_copy_gpg_key_material "$thumb_drive" "$thumb_drive_luks_percent"
11881189
set_user_config "CONFIG_HAVE_GPG_KEY_BACKUP" "y"
11891190
if [ "$GPG_GEN_KEY_IN_MEMORY_COPY_TO_SMARTCARD" = "y" ]; then
11901191
keytocard_subkeys_to_smartcard

initrd/etc/gui_functions

Lines changed: 92 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -34,54 +34,100 @@ mount_usb()
3434
fi
3535
}
3636

37+
# Create display text for a size in bytes in either MB or GB, unit selected
38+
# automatically, rounded to nearest
39+
display_size() {
40+
local size_bytes unit_divisor unit_symbol
41+
size_bytes="$1"
42+
43+
# If it's less than 1 GB, display MB
44+
if [ "$((size_bytes))" -lt "$((1024*1024*1024))" ]; then
45+
unit_divisor=$((1024*1024))
46+
unit_symbol="MB"
47+
else
48+
unit_divisor=$((1024*1024*1024))
49+
unit_symbol="GB"
50+
fi
51+
52+
# Divide by the unit divisor and round to nearest
53+
echo "$(( (size_bytes + unit_divisor/2) / unit_divisor )) $unit_symbol"
54+
}
55+
56+
# Create display text for the size of a block device using MB or GB, rounded to
57+
# nearest
58+
display_block_device_size() {
59+
local block_dev disk_size_bytes
60+
block_dev="$1"
61+
62+
# Obtain size of thumb drive to be wiped with fdisk
63+
if ! disk_size_bytes="$(blockdev --getsize64 "$block_dev")"; then
64+
exit 1
65+
fi
66+
67+
display_size "$disk_size_bytes"
68+
}
69+
70+
# Display a menu to select a file from a list. Pass the name of a file
71+
# containing the list.
72+
# --show-size: Append sizes of files listed. Currently only supports block
73+
# devices.
74+
# $1: Name of file listing files that can be chosen (one per line)
75+
# $2: Optional prompt message
76+
# $3: Optional prompt title
77+
#
78+
# Success: Sets FILE with the selected file
79+
# User aborted: Exits successfully with FILE empty
80+
# No entries in list: Displays error and exits unsuccessfully
3781
file_selector()
3882
{
39-
TRACE "under gui_functions:file_selector"
40-
FILE=""
41-
FILE_LIST=$1
42-
MENU_MSG=${2:-"Choose the file"}
43-
MENU_TITLE=${3:-"Select your File"}
44-
45-
# create file menu options
46-
if [ `cat "$FILE_LIST" | wc -l` -gt 0 ]; then
47-
option=""
48-
while [ -z "$option" ]
49-
do
50-
MENU_OPTIONS=""
51-
n=0
52-
while read option
53-
do
54-
n=`expr $n + 1`
55-
option=$(echo $option | tr " " "_")
56-
MENU_OPTIONS="$MENU_OPTIONS $n ${option}"
57-
done < $FILE_LIST
58-
59-
MENU_OPTIONS="$MENU_OPTIONS a Abort"
60-
whiptail --title "${MENU_TITLE}" \
61-
--menu "${MENU_MSG} [1-$n, a to abort]:" 20 120 8 \
62-
-- $MENU_OPTIONS \
63-
2>/tmp/whiptail || die "Aborting"
64-
65-
option_index=$(cat /tmp/whiptail)
66-
67-
if [ "$option_index" = "a" ]; then
68-
option="a"
69-
return
70-
fi
71-
72-
option=`head -n $option_index $FILE_LIST | tail -1`
73-
if [ "$option" == "a" ]; then
74-
return
75-
fi
76-
done
77-
if [ -n "$option" ]; then
78-
FILE=$option
79-
fi
80-
else
81-
whiptail $BG_COLOR_ERROR --title 'ERROR: No Files Found' \
82-
--msgbox "No Files found matching the pattern. Aborting." 0 80
83-
exit 1
84-
fi
83+
TRACE "under gui_functions:file_selector"
84+
85+
local FILE_LIST MENU_MSG MENU_TITLE CHOICE_ARGS SHOW_SIZE OPTION_SIZE option_index
86+
87+
FILE=""
88+
89+
if [ "$1" = "--show-size" ]; then
90+
SHOW_SIZE=y
91+
shift
92+
fi
93+
94+
FILE_LIST=$1
95+
MENU_MSG=${2:-"Choose the file"}
96+
MENU_TITLE=${3:-"Select your File"}
97+
98+
CHOICE_ARGS=()
99+
n=0
100+
while read option; do
101+
n="$((++n))"
102+
103+
if [ "$SHOW_SIZE" = "y" ] && OPTION_SIZE="$(display_block_device_size "$option")"; then
104+
option="$option - $OPTION_SIZE"
105+
fi
106+
CHOICE_ARGS+=("$n" "$option")
107+
done < "$FILE_LIST"
108+
109+
if [ "${#CHOICE_ARGS[@]}" -eq 0 ]; then
110+
whiptail $BG_COLOR_ERROR --title 'ERROR: No Files Found' \
111+
--msgbox "No Files found matching the pattern. Aborting." 0 80
112+
exit 1
113+
fi
114+
115+
CHOICE_ARGS+=(a Abort)
116+
117+
# create file menu options
118+
option_index=""
119+
while [ -z "$option_index" ]; do
120+
whiptail --title "${MENU_TITLE}" \
121+
--menu "${MENU_MSG} [1-$n, a to abort]:" 20 120 8 \
122+
-- "${CHOICE_ARGS[@]}" \
123+
2>/tmp/whiptail || die "Aborting"
124+
125+
option_index=$(cat /tmp/whiptail)
126+
127+
if [ "$option_index" != "a" ]; then
128+
FILE="$(head -n "$option_index" "$FILE_LIST" | tail -1)"
129+
fi
130+
done
85131
}
86132

87133
show_system_info()

0 commit comments

Comments
 (0)