 
     fw = Path.join(File.cwd!, "_images/#{target}/#{otp_app}.fw")
     unless File.exists?(fw) do
-      raise "Firmware for target #{target} not found at #{fw} run `mix firmware` to build"
+      Mix.raise "Firmware for target #{target} not found at #{fw} run `mix firmware` to build"
     end
 
-    args = ["-a", "-i", fw, "-t", "complete"] ++ argv
+    dev =
+      case opts[:device] do
+        nil -> prompt_dev()
+        dev -> dev
+      end
+    burn(fw, dev, opts, argv)
+  end
+
+  defp burn(fw, dev, opts, argv) do
+    task = opts[:task] || "complete"
+    args = ["-a", "-i", fw, "-t", task, "-d", dev] ++ argv
     {cmd, args} =
       case :os.type do
         {_, :darwin} ->

  use Mix.Task
  import Mix.Nerves.Utils

  def run(argv) do
    preflight

    Mix.shell.info "Nerves Firmware Burn"
    config = Mix.Project.config

      end
    shell(cmd, args)
  end

  defp get_devs do
    {result, 0} = System.cmd("fwup", ["--detect"])
    if result == "" do
      Mix.raise "Could not auto detect your SD card"
    end
    result
    |> String.strip
    |> String.split("\n")
    |> Enum.map(&String.split(&1, ","))
  end

  defp prompt_dev() do
    case get_devs() do
      [[dev, bytes]] ->
        choice =
          Mix.shell.yes?("Use #{bytes_to_gigabytes(bytes)} GiB memory card found at #{dev}?")
        if choice do
          dev
        else
          Mix.raise "Aborted"
        end
      devs ->
        choices =
          devs
          |> Enum.zip(0..length(devs))
          |> Enum.reduce([], fn({[dev, bytes], idx}, acc) ->
            ["#{idx}) #{bytes_to_gigabytes(bytes)} GiB found at #{dev}" | acc]
          end)
          |> Enum.reverse
        choice = Mix.shell.prompt("Discovered devices:\n#{Enum.join(choices, "\n")}\nWhich device do you want to burn to?")
        |> String.strip
        idx =
          case Integer.parse(choice) do
            {idx, _} -> idx
            _ -> Mix.raise "Invalid selection #{choice}"
          end
        case Enum.fetch(devs, idx) do
          {:ok, [dev, _]} -> dev
          _ -> Mix.raise "Invalid selection #{choice}"
        end
    end
  end

  defp bytes_to_gigabytes(bytes) when is_binary(bytes) do
    {bytes, _} = Integer.parse(bytes)
    bytes_to_gigabytes(bytes)
  end

  defp bytes_to_gigabytes(bytes) do
    gb = bytes / 1024 / 1024 / 1024
    Float.round(gb, 2)
  end
end
