Nick Sanders | 119677f | 2011-06-03 01:04:08 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
Chris Sosa | 2eb73c0 | 2011-09-01 18:48:38 -0700 | [diff] [blame] | 3 | # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
Nick Sanders | 119677f | 2011-06-03 01:04:08 -0700 | [diff] [blame] | 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | # make_netboot.sh --board=[board] |
| 8 | # |
| 9 | # This script validates that the current latest image is an install shim, |
| 10 | # and generates a netboot image from it. This pulls the u-boot kernel |
| 11 | # image bundle (uimg), the legacy firmware for netbooting, and the install |
| 12 | # shim kernel image, bundled as a uboot gz/uimg, and places them in a |
| 13 | # "netboot" subfolder. |
| 14 | |
Brian Harring | aa13ea4 | 2012-03-15 18:31:03 -0700 | [diff] [blame] | 15 | SCRIPT_ROOT=$(dirname $(readlink -f "$0")) |
Brian Harring | d5d5dbf | 2011-07-11 16:36:21 -0700 | [diff] [blame] | 16 | . "${SCRIPT_ROOT}/common.sh" || { echo "Unable to load common.sh"; exit 1; } |
Brian Harring | aa13ea4 | 2012-03-15 18:31:03 -0700 | [diff] [blame] | 17 | |
Nick Sanders | 119677f | 2011-06-03 01:04:08 -0700 | [diff] [blame] | 18 | # Script must be run inside the chroot. |
| 19 | restart_in_chroot_if_needed "$@" |
| 20 | |
| 21 | get_default_board |
| 22 | |
| 23 | DEFINE_string board "${DEFAULT_BOARD}" \ |
| 24 | "The board to build an image for." |
David James | 4dd4c54 | 2011-08-10 10:19:47 -0700 | [diff] [blame] | 25 | DEFINE_string image "" "Path to the image to use" |
Nick Sanders | 119677f | 2011-06-03 01:04:08 -0700 | [diff] [blame] | 26 | |
| 27 | # Parse command line. |
| 28 | FLAGS "$@" || exit 1 |
| 29 | eval set -- "${FLAGS_ARGV}" |
| 30 | |
Vic Yang | 3450deb | 2012-05-18 11:57:06 +0800 | [diff] [blame] | 31 | . "${SCRIPT_ROOT}/build_library/build_common.sh" || exit 1 |
| 32 | . "${BUILD_LIBRARY_DIR}/board_options.sh" || exit 1 |
| 33 | |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 34 | switch_to_strict_mode |
Nick Sanders | 119677f | 2011-06-03 01:04:08 -0700 | [diff] [blame] | 35 | # build_packages artifact output. |
| 36 | SYSROOT="${GCLIENT_ROOT}/chroot/build/${FLAGS_board}" |
| 37 | # build_image artifact output. |
| 38 | IMAGES_DIR="${CHROOT_TRUNK_DIR}/src/build/images" |
Nick Sanders | 119677f | 2011-06-03 01:04:08 -0700 | [diff] [blame] | 39 | |
David James | 4dd4c54 | 2011-08-10 10:19:47 -0700 | [diff] [blame] | 40 | if [ -n "${FLAGS_image}" ]; then |
| 41 | cd $(dirname "${FLAGS_image}") |
| 42 | INSTALL_SHIM=$(basename "${FLAGS_image}") |
| 43 | else |
| 44 | cd ${IMAGES_DIR}/${FLAGS_board}/latest |
| 45 | # Canonical install shim name. |
| 46 | INSTALL_SHIM="factory_install_shim.bin" |
| 47 | fi |
Nick Sanders | 119677f | 2011-06-03 01:04:08 -0700 | [diff] [blame] | 48 | |
| 49 | if [ ! -f "${INSTALL_SHIM}" ]; then |
| 50 | echo "Cannot locate ${INSTALL_SHIM}, nothing to netbootify!" |
| 51 | exit 1 |
| 52 | fi |
| 53 | |
| 54 | # Generate staging dir for netboot files. |
| 55 | sudo rm -rf netboot |
| 56 | mkdir -p netboot |
| 57 | |
Nick Sanders | 119677f | 2011-06-03 01:04:08 -0700 | [diff] [blame] | 58 | # Get netboot firmware. |
Nick Sanders | 119677f | 2011-06-03 01:04:08 -0700 | [diff] [blame] | 59 | # TODO(nsanders): Set default IP here when userspace |
| 60 | # env modification is available. |
Grant Grundler | 7c4328a | 2011-06-22 10:24:42 -0700 | [diff] [blame] | 61 | # TODO(nsanders): ARM generic doesn't build chromeos-u-boot package. |
| 62 | # When ARM generic goes away, delete the test. |
Stefan Reinauer | b392153 | 2011-09-07 16:34:02 -0700 | [diff] [blame] | 63 | if [ -r "${SYSROOT}/firmware/legacy_image.bin" ]; then |
Grant Grundler | 7c4328a | 2011-06-22 10:24:42 -0700 | [diff] [blame] | 64 | echo "Copying netboot firmware legacy_image.bin" |
Stefan Reinauer | b392153 | 2011-09-07 16:34:02 -0700 | [diff] [blame] | 65 | cp "${SYSROOT}/firmware/legacy_image.bin" "netboot" |
Nick Sanders | fa28c16 | 2011-07-19 01:48:57 -0700 | [diff] [blame] | 66 | cp "${GCLIENT_ROOT}/chroot/usr/bin/update_firmware_vars.py" "netboot" |
Grant Grundler | 7c4328a | 2011-06-22 10:24:42 -0700 | [diff] [blame] | 67 | else |
Stefan Reinauer | b392153 | 2011-09-07 16:34:02 -0700 | [diff] [blame] | 68 | echo "Skipping legacy fw: ${SYSROOT}/firmware/legacy_image.bin not present?" |
Grant Grundler | 7c4328a | 2011-06-22 10:24:42 -0700 | [diff] [blame] | 69 | fi |
Nick Sanders | 119677f | 2011-06-03 01:04:08 -0700 | [diff] [blame] | 70 | |
Nick Sanders | 119677f | 2011-06-03 01:04:08 -0700 | [diff] [blame] | 71 | # Prepare to mount rootfs. |
| 72 | umount_loop() { |
| 73 | sudo umount r || true |
| 74 | sudo umount s || true |
| 75 | false |
| 76 | } |
| 77 | |
| 78 | echo "Unpack factory install shim partitions" |
| 79 | ./unpack_partitions.sh "${INSTALL_SHIM}" |
| 80 | |
| 81 | # Genrate clean mountpoints. |
| 82 | sudo rm -rf r s |
| 83 | mkdir -p r s |
| 84 | |
| 85 | # Clean ROified filesystem headers, and mount. |
| 86 | trap "umount_loop" EXIT |
| 87 | enable_rw_mount part_3 |
| 88 | sudo mount -o loop part_3 r |
| 89 | sudo mount -o loop part_1 s |
| 90 | echo "Mount install shim rootfs (partition 3)" |
| 91 | |
Vic Yang | 3450deb | 2012-05-18 11:57:06 +0800 | [diff] [blame] | 92 | if [ "${ARCH}" = "arm" ]; then |
| 93 | export MKIMAGE_ARCH="arm" |
| 94 | else |
| 95 | export MKIMAGE_ARCH="x86" # including amd64 |
| 96 | fi |
| 97 | |
Nick Sanders | 2027ca8 | 2011-08-09 00:48:29 -0700 | [diff] [blame] | 98 | # Get netboot kernel. |
Vic Yang | 3450deb | 2012-05-18 11:57:06 +0800 | [diff] [blame] | 99 | if [ "${ARCH}" = "arm" ]; then |
| 100 | # Currently we don't use initramfs for ARM. Someday we would probably want |
| 101 | # initramfs for USB factory installation. |
| 102 | # TODO: Converge build processes of ARM and x86. |
| 103 | echo "Generating netboot kernel vmlinux.uimg" |
| 104 | cp "r/boot/vmlinux.uimg" "netboot" |
| 105 | else |
| 106 | echo "Building kernel" |
| 107 | |
| 108 | # Create temporary emerge root |
| 109 | temp_build_path="$(mktemp -d bk_XXXXXXXX)" |
| 110 | if ! [ -d "${temp_build_path}" ]; then |
| 111 | echo "Failed to create temporary directory." |
| 112 | exit 1 |
| 113 | fi |
| 114 | |
| 115 | # Emerge network boot kernel |
| 116 | # We don't want to build whole install shim everytime we run this script, |
| 117 | # and thus we only build kernel here. If this script is run against install |
| 118 | # shim with different kernel version, this might not work. But as we don't |
| 119 | # upgrade kernel so often, this is probably fine. |
Vic Yang | c321a9a | 2012-05-22 15:24:27 +0800 | [diff] [blame^] | 120 | export USE='vfat blkdevram fbconsole' |
Vic Yang | 3450deb | 2012-05-18 11:57:06 +0800 | [diff] [blame] | 121 | export EMERGE_BOARD_CMD="emerge-${FLAGS_board}" |
| 122 | emerge_custom_kernel ${temp_build_path} |
| 123 | |
| 124 | # Generate kernel uImage |
| 125 | echo "Generating netboot kernel vmlinux.uimg" |
| 126 | |
| 127 | # U-boot put kernel image at 0x100000. We load it at 0x3000000 because |
| 128 | # 0x3000000 is safe enough not to overlap with image at 0x100000. |
| 129 | mkimage -A "${MKIMAGE_ARCH}" -O linux -T kernel -n "Linux kernel" -C none \ |
| 130 | -d "${temp_build_path}"/boot/vmlinuz \ |
| 131 | -a 0x03000000 -e 0x03000000 netboot/vmlinux.uimg |
| 132 | |
| 133 | # Clean up temporary emerge root |
| 134 | sudo rm -rf "${temp_build_path}" |
| 135 | fi |
Nick Sanders | 2027ca8 | 2011-08-09 00:48:29 -0700 | [diff] [blame] | 136 | |
| 137 | echo "Add lsb-factory" |
Nick Sanders | 119677f | 2011-06-03 01:04:08 -0700 | [diff] [blame] | 138 | # Copy factory config file. |
| 139 | # TODO(nsanders): switch this to u-boot env var config. |
| 140 | LSB_FACTORY_DIR="mnt/stateful_partition/dev_image/etc" |
| 141 | sudo mkdir -p "r/${LSB_FACTORY_DIR}" |
| 142 | sudo cp "s/dev_image/etc/lsb-factory" "r/${LSB_FACTORY_DIR}" |
| 143 | |
| 144 | # Clean up mounts. |
| 145 | trap - EXIT |
| 146 | sudo umount r s |
| 147 | sudo rm -rf r s |
| 148 | |
| 149 | # Generate an initrd fo u-boot to load. |
| 150 | gzip -9 -c part_3 > ext2_rootfs.gz |
| 151 | echo "Generating netboot rootfs initrd.uimg" |
| 152 | # U-boot's uimg wrapper specifies where we will load the blob into memory. |
| 153 | # tftp boot's default root address is set to 0x12008000 in legacy_image.bin, |
| 154 | # so we want to unpack it there. |
Vic Yang | 3450deb | 2012-05-18 11:57:06 +0800 | [diff] [blame] | 155 | mkimage -A "${MKIMAGE_ARCH}" -O linux -T ramdisk -a 0x12008000 \ |
Nick Sanders | 119677f | 2011-06-03 01:04:08 -0700 | [diff] [blame] | 156 | -n "Factory Install RootFS" -C gzip -d ext2_rootfs.gz \ |
| 157 | netboot/initrd.uimg |
| 158 | |
| 159 | # Cleanup |
| 160 | rm -rf ext2_rootfs.gz part_* |