1#!/bin/sh 2 3# This scripts makes a minimal bootable SD card image for the Chromebook. 4# The resulting file is called bootsd.img. It should be written directly 5# to the card: 6# 7# SD=/dev/mmcblk1 # check your device name! 8# dd if=output/images/bootsd.img of=$SD 9# 10# The partitions are created just large enough to hold the kernel and 11# the rootfs image. Most of the card will be empty, and the secondary 12# GPT will not be in its proper location. 13 14# cgpt does not create protective MBR, and the kernel refuses to read 15# GPT unless there's some kind of MBR in sector 0. So we need parted 16# to write that single sector before doing anything with the GPT. 17cgpt=$HOST_DIR/bin/cgpt 18parted=$HOST_DIR/sbin/parted 19kernel=$BINARIES_DIR/uImage.kpart 20rootfs=$BINARIES_DIR/rootfs.ext2 21 22run() { echo "$@"; "$@"; } 23die() { echo "$@" >&2; exit 1; } 24test -f $kernel || die "No kernel image found" 25test -f $rootfs || die "No rootfs image found" 26test -x $cgpt || die "cgpt not found (host-vboot-utils have not been built?)" 27 28# True file sizes in bytes 29kernelsize=`stat -t $kernel | cut -d\ -f2` 30rootfssize=`stat -t $rootfs | cut -d\ -f2` 31 32# The card is partitioned in sectors of 8KB. 33# 4 sectors are reserved for MBR+GPT. Their actual size turns out 34# to be 33 512-blocks which is just over 2 sectors, but we align 35# it to a nice round number. 36sec=8192 37kernelsec=$(((kernelsize+8191)>>13)) 38rootfssec=$(((rootfssize+8191)>>13)) 39headersec=4 40 41# There is also a copy of MBR+GPT at the end of the image. 42# It's going to be useless but both tools assume it's there. 43imagesec=$((2*headersec+kernelsec+rootfssec)) 44bootsd="$BINARIES_DIR/bootsd.img" 45run dd bs=$sec count=$imagesec if=/dev/zero of=$bootsd 46 47# cgpt needs offsets and sizes in 512-blocks. 48block=512 49kernelstart=$((headersec<<4)) 50kernelblocks=$((kernelsec<<4)) 51rootfsblocks=$((rootfssec<<4)) 52rootfsstart=$((kernelstart+kernelblocks)) 53 54# This command initializes both GPT and MBR 55run $parted -s $bootsd mklabel gpt 56 57# The kernel partition must be marked as bootable, that's why -S -T -P 58run $cgpt add -i 1 -b $kernelstart -s $kernelblocks \ 59 -t kernel -l kernel \ 60 -S 1 -T 1 -P 10 $bootsd 61 62# It does not really matter where the rootfs partition is located as long 63# as the kernel can find it. 64# However, if anything is changed here, kernel.args must be updated as well. 65run $cgpt add -i 2 -b $rootfsstart -s $rootfsblocks \ 66 -t data -l rootfs $bootsd 67 68run dd bs=$block if=$kernel of=$bootsd seek=$kernelstart 69run dd bs=$block if=$rootfs of=$bootsd seek=$rootfsstart 70