From a0a02f4c3abb9d6166fa01075ea30ee39bbf98b3 Mon Sep 17 00:00:00 2001 From: Gilad Arnold Date: Mon, 13 Feb 2012 09:23:00 -0800 Subject: [PATCH] Extended device autodetection to work with --to_product. This both fixes an incorrect behavior where --to_product would unsafely lead to using a target device not intended to by the user; and reuses the existing logic for device autodetection to work with provided product strings. As a result, it is easier to find devices, and the script's behavior is more consistent as far as device detection and selection. This preserves the pattern matching on product string (now properly highlighted in usage text and inline comments). BUG=None TEST=Ran script with various flag combinations (--to, --to_product), product strings, and devices connected. Change-Id: I80bfca086029555853a680e7c97251b11adc912e Reviewed-on: https://gerrit.chromium.org/gerrit/15757 Reviewed-by: Richard Barnette Commit-Ready: Gilad Arnold Tested-by: Gilad Arnold --- image_to_usb.sh | 55 +++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/image_to_usb.sh b/image_to_usb.sh index 397d86cdab..173fa14ff1 100755 --- a/image_to_usb.sh +++ b/image_to_usb.sh @@ -48,7 +48,7 @@ DEFINE_string from "" \ DEFINE_string to "/dev/sdX" \ "write to a specific disk or image file" DEFINE_string to_product "" \ - "write to a disk with product name matching a pattern" + "find target device with product name matching a string (accepts wildcards)" DEFINE_boolean yes ${FLAGS_FALSE} \ "answer yes to all prompts" \ y @@ -145,47 +145,42 @@ if [ ! -d "${FLAGS_from}" ] ; then die "Cannot find image directory ${FLAGS_from}" fi -if [ -n "${FLAGS_to_product}" ]; then - if [ "${FLAGS_to}" != "/dev/sdX" ]; then - die "Cannot specify both --to and --to_product" - fi - - match="" - for disk in $(list_usb_disks) $(list_mmc_disks); do - if [[ "$(get_disk_info $disk product)" = ${FLAGS_to_product} ]]; then - if [ -n "${match}" ]; then - die "Multiple devices match product '${FLAGS_to_product}', aborting" - fi - match="$disk" - fi - done - - if [ -z "${match}" ]; then - echo "Failed to find a devices matching product '${FLAGS_to_product}'" - # Leave FLAGS_to set to its default and fall through to the error report. - else - FLAGS_to="/dev/${match}" - fi -fi - # No target provided, attempt autodetection. if [ "${FLAGS_to}" == "/dev/sdX" ]; then - echo "No target device specified, autodetecting..." + if [ -z "${FLAGS_to_product}" ]; then + echo "No target device specified, autodetecting..." + else + echo "Looking for target devices matching '${FLAGS_to_product}'..." + fi # Obtain list of USB and MMC device names. disk_list=( $(list_usb_disks) $(list_mmc_disks) ) - if (( ! ${#disk_list[*]} )); then - die "No target device could be detected" - fi # Build list of descriptive strings for detected devices. unset disk_string_list for disk in "${disk_list[@]}"; do + # If --to_product was used, match against provided string. + # Note: we intentionally use [[ ... != ... ]] to allow pattern matching on + # the product string. + if [ -n "${FLAGS_to_product}" ] && + [[ "$(get_disk_info ${disk} product)" != ${FLAGS_to_product} ]]; then + continue + fi + disk_string=$(get_disk_string /dev/${disk}) - disk_string_list=( "${disk_string_list[@]}" \ + disk_string_list=( "${disk_string_list[@]}" "/dev/${disk}: ${disk_string}" ) done + # If no (matching) devices found, quit. + if (( ! ${#disk_string_list[*]} )); then + if [ -z "${FLAGS_to_product}" ]; then + die "No USB/MMC devices could be detected" + else + die "No matching USB/MMC devices could be detected" + fi + fi + # Prompt for selection, or autoselect if only one device was found. if (( ${#disk_string_list[*]} > 1 )); then PS3="Select a target device: " @@ -201,6 +196,8 @@ if [ "${FLAGS_to}" == "/dev/sdX" ]; then fi FLAGS_to="${disk_string%%:*}" +elif [ -n "${FLAGS_to_product}" ]; then + die "Cannot specify both --to and --to_product" fi # Guess ARCH if it's unset