blob: 08416943d142970245934b618a2878288f3078de [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 Yang90e3b252013-01-09 18:34:35 +080097echo "Building kernel"
98
99# Create temporary emerge root
100temp_build_path="$(mktemp -d bk_XXXXXXXX)"
101if ! [ -d "${temp_build_path}" ]; then
102 echo "Failed to create temporary directory."
103 exit 1
104fi
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.
111export USE='vfat blkdevram fbconsole'
112export EMERGE_BOARD_CMD="emerge-${FLAGS_board}"
113emerge_custom_kernel ${temp_build_path}
114
115# Generate kernel uImage
116echo "Generating netboot kernel vmlinux.uimg"
117
Vic Yang3450deb2012-05-18 11:57:06 +0800118if [ "${ARCH}" = "arm" ]; then
Vic Yang90e3b252013-01-09 18:34:35 +0800119 cp "${temp_build_path}"/boot/vmlinux.uimg netboot/
Vic Yang3450deb2012-05-18 11:57:06 +0800120else
Vic Yang3450deb2012-05-18 11:57:06 +0800121 # 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 Yang3450deb2012-05-18 11:57:06 +0800126fi
Nick Sanders2027ca82011-08-09 00:48:29 -0700127
Vic Yang90e3b252013-01-09 18:34:35 +0800128# Clean up temporary emerge root
129sudo rm -rf "${temp_build_path}"
130
Nick Sanders2027ca82011-08-09 00:48:29 -0700131echo "Add lsb-factory"
Nick Sanders119677f2011-06-03 01:04:08 -0700132# Copy factory config file.
133# TODO(nsanders): switch this to u-boot env var config.
134LSB_FACTORY_DIR="mnt/stateful_partition/dev_image/etc"
135sudo mkdir -p "r/${LSB_FACTORY_DIR}"
136sudo cp "s/dev_image/etc/lsb-factory" "r/${LSB_FACTORY_DIR}"
137
138# Clean up mounts.
139trap - EXIT
Brian Harringece65e02012-08-30 13:42:21 -0700140safe_umount r s
Nick Sanders119677f2011-06-03 01:04:08 -0700141sudo rm -rf r s
142
143# Generate an initrd fo u-boot to load.
144gzip -9 -c part_3 > ext2_rootfs.gz
145echo "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 Yang3450deb2012-05-18 11:57:06 +0800149mkimage -A "${MKIMAGE_ARCH}" -O linux -T ramdisk -a 0x12008000 \
Nick Sanders119677f2011-06-03 01:04:08 -0700150 -n "Factory Install RootFS" -C gzip -d ext2_rootfs.gz \
151 netboot/initrd.uimg
152
153# Cleanup
154rm -rf ext2_rootfs.gz part_*