1#!/bin/bash
2
3# Clear environment variables by restarting script w/bare minimum passed through
4[ -z "$NOCLEAR" ] && exec env -i NOCLEAR=1 HOME="$HOME" PATH="$PATH" \
5    "LINUX=$LINUX" "CROSS=$CROSS" CROSS_COMPILE="$CROSS_COMPILE" "$0" "$@"
6
7# assign command line NAME=VALUE args to env vars, keeping rest as packages
8while [ $# -ne 0 ]; do
9  [ "${1/=/}" != "$1" ] && eval "export ${1/=*/}=\"\${1#*=}\"" ||
10    { [ "$1" != '--' ] && PKG="${PKG:-plumbing} $1"; }
11  shift
12done
13
14die() { echo "$@" >&2; exit 1; }
15announce() { echo -e "\033]2;$CROSS $*\007\n=== $*"; }
16
17# Create target-independent working directories (cmdline can change locations)
18TOP="$PWD/root"
19mkdir -p ${BUILD:=$TOP/build} ${AIRLOCK:=$TOP/airlock} ${LOG:=$TOP/log} ||exit 1
20
21# set CROSS_COMPILE from $CROSS using ccc. Handle "all" w/log, list, and err chk
22if [ ! -z "$CROSS" ]; then
23  [ ! -d "${CCC:=$PWD/ccc}" ] && die "No ccc symlink to compiler directory."
24  CROSS_COMPILE="$(echo "$CCC/$CROSS"-*cross/bin/"$CROSS"*-cc | sed 's/cc$//')"
25  if [ "${CROSS::3}" == all ]; then
26    for i in $(ls "$CCC" | sed -n 's/-.*//p' | sort -u | xargs); do
27      { rm -f "$LOG/$i-log".{failed,success}
28        "$0" "$@" CROSS=$i ; [ $? -eq 0 ] && mv "$LOG/$i".{txt,success}
29      } |& tee "$LOG/$i.txt"
30      [ ! -e "$LOG/$i.success" ] &&
31        { mv "$LOG/$i".{txt,failed};[ "$CROSS" != allnonstop ] && exit 1; }
32    done
33    exit
34  elif [ ! -e "${CROSS_COMPILE}cc" ]; then
35    ls "$CCC" | sed -n 's/-.*//p' | sort -u | xargs
36    exit
37  fi
38fi
39
40${CROSS_COMPILE}cc --static -xc - -o /dev/null <<< "int main(void){return 0;}"||
41  die "${CROSS_COMPILE}cc can't create static binaries"
42
43# Parse and sanity check $CROSS_COMPILE (if any)
44if [ ! -z "$CROSS_COMPILE" ]; then
45  CROSS_PATH="$(dirname "$(which "${CROSS_COMPILE}cc")")"
46  [ -z "$CROSS_PATH" ] && die "no ${CROSS_COMPILE}cc in path"
47  : ${CROSS_BASE:=$(basename "$CROSS_COMPILE")} ${CROSS:=${CROSS_BASE/-*/}}
48fi
49echo "Building for ${CROSS:=host}"
50
51# Create target-specific work/output directories
52: ${OUTPUT:=$TOP/$CROSS} ${PKGDIR:=$PWD/scripts/root}
53MYBUILD="$BUILD/${CROSS_BASE:-host-}tmp"
54rm -rf "$MYBUILD" && mkdir -p "$MYBUILD" || exit 1
55[ -z "$ROOT" ] && ROOT="$OUTPUT/fs" && rm -rf "$ROOT" # only blank if NOT set
56
57# When cross compiling build everything under a host toybox with known behavior
58if [ ! -z "$CROSS_COMPILE" ]; then
59  if [ ! -e "$AIRLOCK/toybox" ]; then
60    announce "airlock"
61    PREFIX="$AIRLOCK" KCONFIG_CONFIG="$TOP"/.airlock CROSS_COMPILE= \
62      make clean defconfig toybox install_airlock && rm "$TOP"/.airlock ||exit 1
63  fi
64  export PATH="$CROSS_PATH:$AIRLOCK"
65fi
66
67# Create new root filesystem's directory layout
68mkdir -p "$ROOT"/{etc,tmp,proc,sys,dev,home,mnt,root,usr/{bin,sbin,lib},var} &&
69chmod a+rwxt "$ROOT"/tmp && ln -s usr/{bin,sbin,lib} "$ROOT" || exit 1
70
71# Write init script. Runs as pid 1 from initramfs to set up and hand off system.
72cat > "$ROOT"/init << 'EOF' &&
73#!/bin/sh
74
75export HOME=/home PATH=/bin:/sbin
76
77if ! mountpoint -q dev; then
78  mount -t devtmpfs dev dev || mdev -s
79  [ $$ -eq 1 ] && exec >/dev/console 2>&1
80  for i in ,fd /0,stdin /1,stdout /2,stderr
81  do ln -sf /proc/self/fd${i/,*/} dev/${i/*,/}; done
82  mkdir -p dev/{shm,pts}
83  mountpoint -q dev/pts || mount -t devpts dev/pts dev/pts
84  chmod +t /dev/shm
85fi
86mountpoint -q proc || mount -t proc proc proc
87mountpoint -q sys || mount -t sysfs sys sys
88
89if [ $$ -eq 1 ]; then # Setup networking for QEMU (needs /proc)
90  ifconfig lo 127.0.0.1
91  ifconfig eth0 10.0.2.15
92  route add default gw 10.0.2.2
93  [ "$(date +%s)" -lt 1000 ] && timeout 2 sntp -sq 10.0.2.2 # Ask host
94  [ "$(date +%s)" -lt 10000000 ] && sntp -sq time.google.com
95
96  # Run package scripts (if any)
97  [ -e /etc/rc ] && for i in $(echo /etc/rc/* | sort); do . $i; done
98
99  [ -z "$CONSOLE" ] && CONSOLE="$(</sys/class/tty/console/active)"
100  [ -z "$HANDOFF" ] && HANDOFF=/bin/sh && echo Type exit when done.
101  echo 3 > /proc/sys/kernel/printk
102  exec oneit -c /dev/"${CONSOLE:-console}" $HANDOFF
103else # for chroot
104  /bin/sh
105  umount /dev/pts /dev /sys /proc
106fi
107EOF
108chmod +x "$ROOT"/init &&
109
110# Google's nameserver, passwd+group with special (root/nobody) accounts + guest
111echo "nameserver 8.8.8.8" > "$ROOT"/etc/resolv.conf &&
112cat > "$ROOT"/etc/passwd << 'EOF' &&
113root:x:0:0:root:/root:/bin/sh
114guest:x:500:500:guest:/home/guest:/bin/sh
115nobody:x:65534:65534:nobody:/proc/self:/dev/null
116EOF
117echo -e 'root:x:0:\nguest:x:500:\nnobody:x:65534:' > "$ROOT"/etc/group || exit 1
118
119# Build static toybox with existing .config if there is one, else defconfig+sh
120announce toybox
121[ -e .config ] && [ -z "$PENDING" ] && CONF=silentoldconfig || unset CONF
122for i in $PENDING sh route; do XX="$XX"$'\n'CONFIG_${i^^?}=y; done
123make clean ${CONF:-defconfig KCONFIG_ALLCONFIG=<(echo "$XX")} &&
124LDFLAGS=--static PREFIX="$ROOT" make toybox install || exit 1
125
126# Build any packages listed on command line
127for i in $PKG; do
128  announce "$i"; PATH="$PKGDIR:$PATH" source $i; [ $? -ne 0 ] && die $i
129done
130
131if [ -z "$LINUX" ] || [ ! -d "$LINUX/kernel" ]; then
132  echo 'No $LINUX directory, kernel build skipped.'
133else
134  # Which architecture are we building a kernel for?
135  LINUX="$(realpath "$LINUX")"
136  [ -z "$TARGET" ] && TARGET="${CROSS_BASE/-*/}"
137  [ -z "$TARGET" ] && TARGET="$(uname -m)"
138
139  # Target-specific info in an (alphabetical order) if/else staircase
140  # Each target needs board config, serial console, RTC, ethernet, block device.
141
142  if [ "$TARGET" == armv5l ]; then
143    # This could use the same VIRT board as armv7, but let's demonstrate a
144    # different one requiring a separate device tree binary.
145    QEMU="arm -M versatilepb -net nic,model=rtl8139 -net user"
146    KARCH=arm KARGS=ttyAMA0 VMLINUX=arch/arm/boot/zImage
147    KCONF=CPU_ARM926T,MMU,VFP,ARM_THUMB,AEABI,ARCH_VERSATILE,ATAGS,DEPRECATED_PARAM_STRUCT,ARM_ATAG_DTB_COMPAT,ARM_ATAG_DTB_COMPAT_CMDLINE_EXTEND,SERIAL_AMBA_PL011,SERIAL_AMBA_PL011_CONSOLE,RTC_CLASS,RTC_DRV_PL031,RTC_HCTOSYS,PCI,PCI_VERSATILE,BLK_DEV_SD,SCSI,SCSI_LOWLEVEL,SCSI_SYM53C8XX_2,SCSI_SYM53C8XX_MMIO,NET_VENDOR_REALTEK,8139CP
148    KERNEL_CONFIG="CONFIG_SCSI_SYM53C8XX_DMA_ADDRESSING_MODE=0"
149    DTB=arch/arm/boot/dts/versatile-pb.dtb
150  elif [ "$TARGET" == armv7l ] || [ "$TARGET" == aarch64 ]; then
151    if [ "$TARGET" == aarch64 ]; then
152      QEMU="aarch64 -M virt -cpu cortex-a57"
153      KARCH=arm64 VMLINUX=arch/arm64/boot/Image
154    else
155      QEMU="arm -M virt" KARCH=arm VMLINUX=arch/arm/boot/zImage
156    fi
157    KARGS=ttyAMA0
158    KCONF=MMU,ARCH_MULTI_V7,ARCH_VIRT,SOC_DRA7XX,ARCH_OMAP2PLUS_TYPICAL,ARCH_ALPINE,ARM_THUMB,VDSO,CPU_IDLE,ARM_CPUIDLE,KERNEL_MODE_NEON,SERIAL_AMBA_PL011,SERIAL_AMBA_PL011_CONSOLE,RTC_CLASS,RTC_HCTOSYS,RTC_DRV_PL031,NET_CORE,VIRTIO_MENU,VIRTIO_NET,PCI,PCI_HOST_GENERIC,VIRTIO_BLK,VIRTIO_PCI,VIRTIO_MMIO,ATA,ATA_SFF,ATA_BMDMA,ATA_PIIX,PATA_PLATFORM,PATA_OF_PLATFORM,ATA_GENERIC
159  elif [ "$TARGET" == i486 ] || [ "$TARGET" == i686 ] ||
160       [ "$TARGET" == x86_64 ] || [ "$TARGET" == x32 ]; then
161    if [ "$TARGET" == i486 ]; then
162      QEMU="i386 -cpu 486 -global fw_cfg.dma_enabled=false" KCONF=M486
163    elif [ "$TARGET" == i686 ]; then
164      QEMU="i386 -cpu pentium3" KCONF=MPENTIUMII
165    else
166      QEMU=x86_64 KCONF=64BIT
167      [ "$TARGET" == x32 ] && KCONF=X86_X32
168    fi
169    KARCH=x86 KARGS=ttyS0 VMLINUX=arch/x86/boot/bzImage
170    KCONF=$KCONF,UNWINDER_FRAME_POINTER,PCI,BLK_DEV_SD,ATA,ATA_SFF,ATA_BMDMA,ATA_PIIX,NET_VENDOR_INTEL,E1000,SERIAL_8250,SERIAL_8250_CONSOLE,RTC_CLASS
171  elif [ "$TARGET" == m68k ]; then
172    QEMU="m68k -M q800" KARCH=m68k KARGS=ttyS0 VMLINUX=vmlinux
173    KCONF=MMU,M68040,M68KFPU_EMU,MAC,SCSI_MAC_ESP,MACINTOSH_DRIVERS,ADB,ADB_MACII,NET_CORE,MACSONIC,SERIAL_PMACZILOG,SERIAL_PMACZILOG_TTYS,SERIAL_PMACZILOG_CONSOLE
174  elif [ "$TARGET" == mips ] || [ "$TARGET" == mipsel ]; then
175    QEMU="mips -M malta" KARCH=mips KARGS=ttyS0 VMLINUX=vmlinux
176    KCONF=MIPS_MALTA,CPU_MIPS32_R2,SERIAL_8250,SERIAL_8250_CONSOLE,PCI,BLK_DEV_SD,ATA,ATA_SFF,ATA_BMDMA,ATA_PIIX,NET_VENDOR_AMD,PCNET32,POWER_RESET,POWER_RESET_SYSCON
177    [ "$TARGET" == mipsel ] && KCONF=$KCONF,CPU_LITTLE_ENDIAN &&
178      QEMU="mipsel -M malta"
179  elif [ "$TARGET" == powerpc ]; then
180    KARCH=powerpc QEMU="ppc -M g3beige" KARGS=ttyS0 VMLINUX=vmlinux
181    KCONF=ALTIVEC,PPC_PMAC,PPC_OF_BOOT_TRAMPOLINE,IDE,IDE_GD,IDE_GD_ATA,BLK_DEV_IDE_PMAC,BLK_DEV_IDE_PMAC_ATA100FIRST,MACINTOSH_DRIVERS,ADB,ADB_CUDA,NET_VENDOR_NATSEMI,NET_VENDOR_8390,NE2K_PCI,SERIO,SERIAL_PMACZILOG,SERIAL_PMACZILOG_TTYS,SERIAL_PMACZILOG_CONSOLE,BOOTX_TEXT
182  elif [ "$TARGET" == powerpc64le ]; then
183    KARCH=powerpc QEMU="ppc64 -M pseries -vga none" KARGS=/dev/hvc0
184    VMLINUX=vmlinux
185    KCONF=PPC64,PPC_PSERIES,CPU_LITTLE_ENDIAN,PPC_OF_BOOT_TRAMPOLINE,BLK_DEV_SD,SCSI_LOWLEVEL,SCSI_IBMVSCSI,ATA,NET_VENDOR_IBM,IBMVETH,HVC_CONSOLE,PPC_TRANSACTIONAL_MEM,PPC_DISABLE_WERROR,SECTION_MISMATCH_WARN_ONLY
186  elif [ "$TARGET" = s390x ]; then
187    QEMU="s390x" KARCH=s390 VMLINUX=arch/s390/boot/bzImage
188    KCONF=MARCH_Z900,PACK_STACK,NET_CORE,VIRTIO_NET,VIRTIO_BLK,SCLP_TTY,SCLP_CONSOLE,SCLP_VT220_TTY,SCLP_VT220_CONSOLE,S390_GUEST
189  elif [ "$TARGET" == sh2eb ]; then
190    KARCH=sh VMLINUX=vmlinux KERNEL_CONFIG='CONFIG_MEMORY_START=0x10000000
191CONFIG_CMDLINE="console=ttyUL0 earlycon"' BUILTIN=1
192    KCONF=CPU_SUBTYPE_J2,CPU_BIG_ENDIAN,SH_JCORE_SOC,SMP,BINFMT_ELF_FDPIC,JCORE_EMAC,SERIAL_UARTLITE,SERIAL_UARTLITE_CONSOLE,HZ_100,CMDLINE_OVERWRITE,SPI,SPI_JCORE,MMC,PWRSEQ_SIMPLE,MMC_BLOCK,MMC_SPI
193  elif [ "$TARGET" == sh4 ]; then
194    QEMU="sh4 -M r2d -serial null -serial mon:stdio" KARCH=sh
195    KARGS="ttySC1 noiotrap" VMLINUX=arch/sh/boot/zImage
196    KERNEL_CONFIG="CONFIG_MEMORY_START=0x0c000000"
197    KCONF=CPU_SUBTYPE_SH7751R,MMU,VSYSCALL,SH_FPU,SH_RTS7751R2D,RTS7751R2D_PLUS,SERIAL_SH_SCI,SERIAL_SH_SCI_CONSOLE,PCI,NET_VENDOR_REALTEK,8139CP,PCI,BLK_DEV_SD,ATA,ATA_SFF,ATA_BMDMA,PATA_PLATFORM,BINFMT_ELF_FDPIC,BINFMT_FLAT
198#see also SPI SPI_SH_SCI MFD_SM501 RTC_CLASS RTC_DRV_R9701 RTC_DRV_SH RTC_HCTOSYS
199  else die "Unknown \$TARGET $TARGET"
200  fi
201
202  # Write the qemu launch script
203  if [ ! -z "$QEMU" ]; then
204    [ -z "$BUILTIN" ] && INITRD="-initrd ${CROSS_BASE}root.cpio.gz"
205    echo qemu-system-"$QEMU" '"$@"' $QEMU_MORE -nographic -no-reboot -m 256 \
206         -kernel $(basename $VMLINUX) $INITRD \
207         "-append \"panic=1 HOST=$TARGET console=$KARGS \$KARGS\"" \
208         ${DTB:+-dtb "$(basename "$DTB")"} ";echo -e '\e[?7h'" \
209         > "$OUTPUT/qemu-$TARGET.sh" &&
210    chmod +x "$OUTPUT/qemu-$TARGET.sh" || exit 1
211  fi
212
213  announce "linux-$KARCH"
214  pushd "$LINUX" && make distclean && popd &&
215  cp -sfR "$LINUX" "$MYBUILD/linux" && pushd "$MYBUILD/linux" &&
216
217  # Write miniconfig
218  { echo "# make ARCH=$KARCH allnoconfig KCONFIG_ALLCONFIG=$TARGET.miniconf"
219    echo -e "# make ARCH=$KARCH -j \$(nproc)\n# boot $VMLINUX\n\n"
220    echo "# CONFIG_EMBEDDED is not set"
221
222    # Expand list of =y symbols, first generic then architecture-specific
223    for i in BINFMT_ELF,BINFMT_SCRIPT,NO_HZ,HIGH_RES_TIMERS,BLK_DEV,BLK_DEV_INITRD,RD_GZIP,BLK_DEV_LOOP,EXT4_FS,EXT4_USE_FOR_EXT2,VFAT_FS,FAT_DEFAULT_UTF8,MISC_FILESYSTEMS,SQUASHFS,SQUASHFS_XATTR,SQUASHFS_ZLIB,DEVTMPFS,DEVTMPFS_MOUNT,TMPFS,TMPFS_POSIX_ACL,NET,PACKET,UNIX,INET,IPV6,NETDEVICES,NET_CORE,NETCONSOLE,ETHERNET,COMPAT_32BIT_TIME,EARLY_PRINTK,IKCONFIG,IKCONFIG_PROC $KCONF ; do
224      echo "# architecture ${X:-independent}"
225      sed -E '/^$/d;s/([^,]*)($|,)/CONFIG_\1=y\n/g' <<< "$i"
226      X=specific
227    done
228    [ ! -z "$BUILTIN" ] && echo -e CONFIG_INITRAMFS_SOURCE="\"$OUTPUT/fs\""
229    echo "$KERNEL_CONFIG"
230  } > "$OUTPUT/miniconfig-$TARGET" &&
231  make ARCH=$KARCH allnoconfig KCONFIG_ALLCONFIG="$OUTPUT/miniconfig-$TARGET" &&
232
233  # Second config pass to remove stupid kernel defaults
234  # See http://lkml.iu.edu/hypermail/linux/kernel/1912.3/03493.html
235  sed -e 's/# CONFIG_EXPERT .*/CONFIG_EXPERT=y/' -e "$(sed -E -e '/^$/d' \
236    -e 's@([^,]*)($|,)@/^CONFIG_\1=y/d;$a# CONFIG_\1 is not set/\n@g' \
237       <<< VT,SCHED_DEBUG,DEBUG_MISC,X86_DEBUG_FPU)" -i .config &&
238  yes "" | make ARCH=$KARCH oldconfig > /dev/null &&
239
240  # Build kernel. Copy config, device tree binary, and kernel binary to output
241  make ARCH=$KARCH CROSS_COMPILE="$CROSS_COMPILE" -j $(nproc) &&
242  cp .config "$OUTPUT/linux-fullconfig" || exit 1
243  [ ! -z "$DTB" ] && { cp "$DTB" "$OUTPUT" || exit 1 ;}
244  cp "$VMLINUX" "$OUTPUT" && cd .. && rm -rf linux && popd || exit 1
245fi
246
247# clean up and package root filesystem for initramfs.
248[ -z "$BUILTIN" ] && announce "${CROSS_BASE}root.cpio.gz" &&
249  (cd "$ROOT" && find . | cpio -o -H newc ${CROSS_COMPILE:+--no-preserve-owner} | gzip) \
250    > "$OUTPUT/$CROSS_BASE"root.cpio.gz
251rmdir "$MYBUILD" "$BUILD" 2>/dev/null # remove if empty
252