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