blob: 38d87cc4011543760a462f53e9b183ac92325da4 [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"))
Brian Harringd5d5dbf2011-07-11 16:36:21 -070016. "${SCRIPT_ROOT}/common.sh" || { echo "Unable to load 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
21get_default_board
22
23DEFINE_string board "${DEFAULT_BOARD}" \
24 "The board to build an image for."
David James4dd4c542011-08-10 10:19:47 -070025DEFINE_string image "" "Path to the image to use"
Nick Sanders119677f2011-06-03 01:04:08 -070026
27# Parse command line.
28FLAGS "$@" || exit 1
29eval set -- "${FLAGS_ARGV}"
30
Vic Yang3450deb2012-05-18 11:57:06 +080031. "${SCRIPT_ROOT}/build_library/build_common.sh" || exit 1
32. "${BUILD_LIBRARY_DIR}/board_options.sh" || exit 1
33
Brian Harring7f175a52012-03-02 05:37:00 -080034switch_to_strict_mode
Nick Sanders119677f2011-06-03 01:04:08 -070035# build_packages artifact output.
36SYSROOT="${GCLIENT_ROOT}/chroot/build/${FLAGS_board}"
37# build_image artifact output.
38IMAGES_DIR="${CHROOT_TRUNK_DIR}/src/build/images"
Nick Sanders119677f2011-06-03 01:04:08 -070039
David James4dd4c542011-08-10 10:19:47 -070040if [ -n "${FLAGS_image}" ]; then
41 cd $(dirname "${FLAGS_image}")
42 INSTALL_SHIM=$(basename "${FLAGS_image}")
43else
44 cd ${IMAGES_DIR}/${FLAGS_board}/latest
45 # Canonical install shim name.
46 INSTALL_SHIM="factory_install_shim.bin"
47fi
Nick Sanders119677f2011-06-03 01:04:08 -070048
49if [ ! -f "${INSTALL_SHIM}" ]; then
50 echo "Cannot locate ${INSTALL_SHIM}, nothing to netbootify!"
51 exit 1
52fi
53
54# Generate staging dir for netboot files.
55sudo rm -rf netboot
56mkdir -p netboot
57
Nick Sanders119677f2011-06-03 01:04:08 -070058# Get netboot firmware.
Nick Sanders119677f2011-06-03 01:04:08 -070059# TODO(nsanders): Set default IP here when userspace
60# env modification is available.
Grant Grundler7c4328a2011-06-22 10:24:42 -070061# TODO(nsanders): ARM generic doesn't build chromeos-u-boot package.
62# When ARM generic goes away, delete the test.
Stefan Reinauerb3921532011-09-07 16:34:02 -070063if [ -r "${SYSROOT}/firmware/legacy_image.bin" ]; then
Grant Grundler7c4328a2011-06-22 10:24:42 -070064 echo "Copying netboot firmware legacy_image.bin"
Stefan Reinauerb3921532011-09-07 16:34:02 -070065 cp "${SYSROOT}/firmware/legacy_image.bin" "netboot"
Nick Sandersfa28c162011-07-19 01:48:57 -070066 cp "${GCLIENT_ROOT}/chroot/usr/bin/update_firmware_vars.py" "netboot"
Grant Grundler7c4328a2011-06-22 10:24:42 -070067else
Stefan Reinauerb3921532011-09-07 16:34:02 -070068 echo "Skipping legacy fw: ${SYSROOT}/firmware/legacy_image.bin not present?"
Grant Grundler7c4328a2011-06-22 10:24:42 -070069fi
Nick Sanders119677f2011-06-03 01:04:08 -070070
Nick Sanders119677f2011-06-03 01:04:08 -070071# Prepare to mount rootfs.
72umount_loop() {
73 sudo umount r || true
74 sudo umount s || true
75 false
76}
77
78echo "Unpack factory install shim partitions"
79./unpack_partitions.sh "${INSTALL_SHIM}"
80
81# Genrate clean mountpoints.
82sudo rm -rf r s
83mkdir -p r s
84
85# Clean ROified filesystem headers, and mount.
86trap "umount_loop" EXIT
87enable_rw_mount part_3
88sudo mount -o loop part_3 r
89sudo mount -o loop part_1 s
90echo "Mount install shim rootfs (partition 3)"
91
Vic Yang3450deb2012-05-18 11:57:06 +080092if [ "${ARCH}" = "arm" ]; then
93 export MKIMAGE_ARCH="arm"
94else
95 export MKIMAGE_ARCH="x86" # including amd64
96fi
97
Nick Sanders2027ca82011-08-09 00:48:29 -070098# Get netboot kernel.
Vic Yang3450deb2012-05-18 11:57:06 +080099if [ "${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"
105else
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 Yangc321a9a2012-05-22 15:24:27 +0800120 export USE='vfat blkdevram fbconsole'
Vic Yang3450deb2012-05-18 11:57:06 +0800121 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}"
135fi
Nick Sanders2027ca82011-08-09 00:48:29 -0700136
137echo "Add lsb-factory"
Nick Sanders119677f2011-06-03 01:04:08 -0700138# Copy factory config file.
139# TODO(nsanders): switch this to u-boot env var config.
140LSB_FACTORY_DIR="mnt/stateful_partition/dev_image/etc"
141sudo mkdir -p "r/${LSB_FACTORY_DIR}"
142sudo cp "s/dev_image/etc/lsb-factory" "r/${LSB_FACTORY_DIR}"
143
144# Clean up mounts.
145trap - EXIT
146sudo umount r s
147sudo rm -rf r s
148
149# Generate an initrd fo u-boot to load.
150gzip -9 -c part_3 > ext2_rootfs.gz
151echo "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 Yang3450deb2012-05-18 11:57:06 +0800155mkimage -A "${MKIMAGE_ARCH}" -O linux -T ramdisk -a 0x12008000 \
Nick Sanders119677f2011-06-03 01:04:08 -0700156 -n "Factory Install RootFS" -C gzip -d ext2_rootfs.gz \
157 netboot/initrd.uimg
158
159# Cleanup
160rm -rf ext2_rootfs.gz part_*