| #!/bin/bash |
| # Copyright 2017 The Fuchsia Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| ### make a zedboot USB key |
| |
| ## usage: fx mkzedboot [options] <usb device> |
| ## -f force writing to a non-usb target |
| |
| set -e |
| |
| source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"/lib/image_build_vars.sh |
| |
| force=false |
| if [[ "$1" == "-f" ]]; then |
| shift |
| force=true |
| fi |
| |
| is_usb() { |
| if ! ${force}; then |
| fx-command-run list-usb-disks | grep "$1" |
| fi |
| } |
| |
| USB_DEVICE="$1" |
| if [[ -z "${USB_DEVICE}" ]]; then |
| echo >&2 "device argument required" |
| echo "USB disks:" |
| fx-command-run list-usb-disks |
| exit 1 |
| fi |
| if ! is_usb "${USB_DEVICE}"; then |
| echo >&2 "${USB_DEVICE} does not look like a USB device, use -f to force, or pick from below" |
| echo "USB disks:" |
| fx-command-run list-usb-disks |
| exit 1 |
| fi |
| |
| echo >&2 "Changing ownership of ${USB_DEVICE} to ${USER}" |
| sudo chown "${USER}" "${USB_DEVICE}" |
| |
| # Linux doesn't automount things aggressively, so no work to do. |
| unmountcmd="" |
| # MacOS is really persistent about remounting disks after the raw block device |
| # is closed, so, keep doing this. In the future we will replace CGPT with our |
| # own tool that can do everything in one shot, like make-fuchsia-vol does. The |
| # hack here keeps an open reference to the disk device, which prevents |
| # re-initialization of the gpt driver. |
| if [[ "$(uname)" == "Darwin" ]]; then |
| echo |
| echo "### NOTE:" |
| echo "### NOTE: Click 'ignore' on any MacOS disk dialog boxes" |
| echo "### NOTE:" |
| echo |
| |
| unmountcmd="diskutil quiet unmountDisk ${USB_DEVICE}" |
| fi |
| |
| # Destroy any existing GPT/MBR on the device and re-create |
| echo "Create new GPT partition table... " |
| $unmountcmd |
| "${FUCHSIA_BUILD_DIR}/tools/cgpt" create "${USB_DEVICE}" |
| $unmountcmd |
| "${FUCHSIA_BUILD_DIR}/tools/cgpt" boot -p "${USB_DEVICE}" |
| $unmountcmd |
| echo "done" |
| |
| echo "Create new partitions... " |
| # ESP needs to be a FAT compatible size |
| esp_size=$(((63*1024*1024)/512)) |
| vboot_size=$(((64*1024*1024)/512)) |
| esp_offset=2048 |
| vboot_offset=$(($esp_size + $esp_offset)) |
| $unmountcmd |
| "${FUCHSIA_BUILD_DIR}/tools/cgpt" add -s "${esp_size}" -t efi -b "${esp_offset}" -l esp "${USB_DEVICE}" |
| $unmountcmd |
| "${FUCHSIA_BUILD_DIR}/tools/cgpt" add -s "${vboot_size}" -t kernel -b "${vboot_offset}" -l zedboot "${USB_DEVICE}" |
| $unmountcmd |
| "${FUCHSIA_BUILD_DIR}/tools/cgpt" add -i 2 -T 1 -S 1 -P 2 "${USB_DEVICE}" |
| echo "done" |
| |
| echo "Building zedboot partitions..." |
| fx-command-run build "images/zedboot-${ZIRCON_PROJECT}.vboot" "images/zedboot-${ZIRCON_PROJECT}.esp.blk" |
| echo "done" |
| |
| echo "Writing zedboot for EFI" |
| $unmountcmd |
| dd if="${FUCHSIA_BUILD_DIR}/images/zedboot-${ZIRCON_PROJECT}.esp.blk" of="${USB_DEVICE}" seek=${esp_offset} |
| echo "Writing zedboot for Cros" |
| $unmountcmd |
| dd if="${FUCHSIA_BUILD_DIR}/images/zedboot-${ZIRCON_PROJECT}.vboot" of="${USB_DEVICE}" seek=${vboot_offset} |
| echo "done" |
| |
| case "$(uname)" in |
| Linux) |
| eject "${USB_DEVICE}" |
| ;; |
| Darwin) |
| diskutil eject "${USB_DEVICE}" |
| ;; |
| esac |
| |