blob: d7a0fd15b038e164cdf6fe709ec632efe58d79db [file] [log] [blame]
Nick Sanders119677f2011-06-03 01:04:08 -07001#!/bin/bash
2
Chris Sosa2eb73c02011-09-01 18:48:38 -07003# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
Nick Sanders119677f2011-06-03 01:04:08 -07004# 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 Harringaa13ea42012-03-15 18:31:03 -070015SCRIPT_ROOT=$(dirname $(readlink -f "$0"))
David James359d3e12012-07-10 13:09:48 -070016. "${SCRIPT_ROOT}/common.sh" || exit 1
Brian Harringaa13ea42012-03-15 18:31:03 -070017
Nick Sanders119677f2011-06-03 01:04:08 -070018# Script must be run inside the chroot.
19restart_in_chroot_if_needed "$@"
20
Nick Sanders119677f2011-06-03 01:04:08 -070021DEFINE_string board "${DEFAULT_BOARD}" \
22 "The board to build an image for."
David James4dd4c542011-08-10 10:19:47 -070023DEFINE_string image "" "Path to the image to use"
Nick Sanders119677f2011-06-03 01:04:08 -070024
25# Parse command line.
26FLAGS "$@" || exit 1
27eval set -- "${FLAGS_ARGV}"
28
Vic Yang3450deb2012-05-18 11:57:06 +080029. "${SCRIPT_ROOT}/build_library/build_common.sh" || exit 1
30. "${BUILD_LIBRARY_DIR}/board_options.sh" || exit 1
31
Brian Harring7f175a52012-03-02 05:37:00 -080032switch_to_strict_mode
Nick Sanders119677f2011-06-03 01:04:08 -070033# build_packages artifact output.
34SYSROOT="${GCLIENT_ROOT}/chroot/build/${FLAGS_board}"
35# build_image artifact output.
36IMAGES_DIR="${CHROOT_TRUNK_DIR}/src/build/images"
Nick Sanders119677f2011-06-03 01:04:08 -070037
David James4dd4c542011-08-10 10:19:47 -070038if [ -n "${FLAGS_image}" ]; then
39 cd $(dirname "${FLAGS_image}")
40 INSTALL_SHIM=$(basename "${FLAGS_image}")
41else
42 cd ${IMAGES_DIR}/${FLAGS_board}/latest
43 # Canonical install shim name.
44 INSTALL_SHIM="factory_install_shim.bin"
45fi
Nick Sanders119677f2011-06-03 01:04:08 -070046
47if [ ! -f "${INSTALL_SHIM}" ]; then
48 echo "Cannot locate ${INSTALL_SHIM}, nothing to netbootify!"
49 exit 1
50fi
51
52# Generate staging dir for netboot files.
53sudo rm -rf netboot
54mkdir -p netboot
55
Nick Sanders119677f2011-06-03 01:04:08 -070056# Get netboot firmware.
Nick Sanders119677f2011-06-03 01:04:08 -070057# TODO(nsanders): Set default IP here when userspace
58# env modification is available.
Grant Grundler7c4328a2011-06-22 10:24:42 -070059# TODO(nsanders): ARM generic doesn't build chromeos-u-boot package.
60# When ARM generic goes away, delete the test.
Stefan Reinauerb3921532011-09-07 16:34:02 -070061if [ -r "${SYSROOT}/firmware/legacy_image.bin" ]; then
Grant Grundler7c4328a2011-06-22 10:24:42 -070062 echo "Copying netboot firmware legacy_image.bin"
Stefan Reinauerb3921532011-09-07 16:34:02 -070063 cp "${SYSROOT}/firmware/legacy_image.bin" "netboot"
Nick Sandersfa28c162011-07-19 01:48:57 -070064 cp "${GCLIENT_ROOT}/chroot/usr/bin/update_firmware_vars.py" "netboot"
Grant Grundler7c4328a2011-06-22 10:24:42 -070065else
Stefan Reinauerb3921532011-09-07 16:34:02 -070066 echo "Skipping legacy fw: ${SYSROOT}/firmware/legacy_image.bin not present?"
Grant Grundler7c4328a2011-06-22 10:24:42 -070067fi
Nick Sanders119677f2011-06-03 01:04:08 -070068
Nick Sanders119677f2011-06-03 01:04:08 -070069# Prepare to mount rootfs.
70umount_loop() {
Brian Harringece65e02012-08-30 13:42:21 -070071 safe_umount r || true
72 safe_umount s || true
Nick Sanders119677f2011-06-03 01:04:08 -070073 false
74}
75
76echo "Unpack factory install shim partitions"
77./unpack_partitions.sh "${INSTALL_SHIM}"
78
79# Genrate clean mountpoints.
80sudo rm -rf r s
81mkdir -p r s
82
83# Clean ROified filesystem headers, and mount.
84trap "umount_loop" EXIT
85enable_rw_mount part_3
86sudo mount -o loop part_3 r
87sudo mount -o loop part_1 s
88echo "Mount install shim rootfs (partition 3)"
89
Vic Yang3450deb2012-05-18 11:57:06 +080090if [ "${ARCH}" = "arm" ]; then
91 export MKIMAGE_ARCH="arm"
92else
93 export MKIMAGE_ARCH="x86" # including amd64
94fi
95
Nick Sanders2027ca82011-08-09 00:48:29 -070096# Get netboot kernel.
Vic Yang3450deb2012-05-18 11:57:06 +080097if [ "${ARCH}" = "arm" ]; then
98 # Currently we don't use initramfs for ARM. Someday we would probably want
99 # initramfs for USB factory installation.
100 # TODO: Converge build processes of ARM and x86.
101 echo "Generating netboot kernel vmlinux.uimg"
102 cp "r/boot/vmlinux.uimg" "netboot"
103else
104 echo "Building kernel"
105
106 # Create temporary emerge root
107 temp_build_path="$(mktemp -d bk_XXXXXXXX)"
108 if ! [ -d "${temp_build_path}" ]; then
109 echo "Failed to create temporary directory."
110 exit 1
111 fi
112
113 # Emerge network boot kernel
114 # We don't want to build whole install shim everytime we run this script,
115 # and thus we only build kernel here. If this script is run against install
116 # shim with different kernel version, this might not work. But as we don't
117 # upgrade kernel so often, this is probably fine.
Vic Yangc321a9a2012-05-22 15:24:27 +0800118 export USE='vfat blkdevram fbconsole'
Vic Yang3450deb2012-05-18 11:57:06 +0800119 export EMERGE_BOARD_CMD="emerge-${FLAGS_board}"
120 emerge_custom_kernel ${temp_build_path}
121
122 # Generate kernel uImage
123 echo "Generating netboot kernel vmlinux.uimg"
124
125 # U-boot put kernel image at 0x100000. We load it at 0x3000000 because
126 # 0x3000000 is safe enough not to overlap with image at 0x100000.
127 mkimage -A "${MKIMAGE_ARCH}" -O linux -T kernel -n "Linux kernel" -C none \
128 -d "${temp_build_path}"/boot/vmlinuz \
129 -a 0x03000000 -e 0x03000000 netboot/vmlinux.uimg
130
131 # Clean up temporary emerge root
132 sudo rm -rf "${temp_build_path}"
133fi
Nick Sanders2027ca82011-08-09 00:48:29 -0700134
135echo "Add lsb-factory"
Nick Sanders119677f2011-06-03 01:04:08 -0700136# Copy factory config file.
137# TODO(nsanders): switch this to u-boot env var config.
138LSB_FACTORY_DIR="mnt/stateful_partition/dev_image/etc"
139sudo mkdir -p "r/${LSB_FACTORY_DIR}"
140sudo cp "s/dev_image/etc/lsb-factory" "r/${LSB_FACTORY_DIR}"
141
142# Clean up mounts.
143trap - EXIT
Brian Harringece65e02012-08-30 13:42:21 -0700144safe_umount r s
Nick Sanders119677f2011-06-03 01:04:08 -0700145sudo rm -rf r s
146
147# Generate an initrd fo u-boot to load.
148gzip -9 -c part_3 > ext2_rootfs.gz
149echo "Generating netboot rootfs initrd.uimg"
150# U-boot's uimg wrapper specifies where we will load the blob into memory.
151# tftp boot's default root address is set to 0x12008000 in legacy_image.bin,
152# so we want to unpack it there.
Vic Yang3450deb2012-05-18 11:57:06 +0800153mkimage -A "${MKIMAGE_ARCH}" -O linux -T ramdisk -a 0x12008000 \
Nick Sanders119677f2011-06-03 01:04:08 -0700154 -n "Factory Install RootFS" -C gzip -d ext2_rootfs.gz \
155 netboot/initrd.uimg
156
157# Cleanup
158rm -rf ext2_rootfs.gz part_*