• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3# Clear environment variables by restarting script w/bare minimum passed through
4[ -z "$NOCLEAR" ] &&
5  exec env -i NOCLEAR=1 HOME="$HOME" PATH="$PATH" LINUX="$LINUX" \
6    CROSS_COMPILE="$CROSS_COMPILE" CROSS_SHORT="$CROSS_SHORT" "$0" "$@"
7
8# assign command line NAME=VALUE args to env vars
9while [ $# -ne 0 ]; do
10  X="${1/=*/}" Y="${1#*=}"
11  [ "${1/=/}" != "$1" ] && eval "export $X=\"\$Y\"" || echo "unknown $i"
12  shift
13done
14
15# If we're cross compiling, set appropriate environment variables.
16if [ -z "$CROSS_COMPILE" ]; then
17  echo "Building natively"
18  if ! cc --static -xc - -o /dev/null <<< "int main(void) {return 0;}"; then
19    echo "Warning: host compiler can't create static binaries." >&2
20    sleep 3
21  fi
22else
23  CROSS_PATH="$(dirname "$(which "${CROSS_COMPILE}cc")")"
24  CROSS_BASE="$(basename "$CROSS_COMPILE")"
25  [ -z "$CROSS_SHORT" ] && CROSS_SHORT="${CROSS_BASE/-*/}"
26  echo "Cross compiling to $CROSS_SHORT"
27  if [ -z "$CROSS_PATH" ]; then
28    echo "no ${CROSS_COMPILE}cc in path" >&2
29    exit 1
30  fi
31fi
32
33# set up directories (can override most of these paths on cmdline)
34TOP="$PWD/root"
35[ -z "$BUILD" ] && BUILD="$TOP/build"
36[ -z "$AIRLOCK" ] && AIRLOCK="$TOP/airlock"
37[ -z "$OUTPUT" ] && OUTPUT="$TOP/${CROSS_SHORT:-host}"
38[ -z "$ROOT" ] && ROOT="$OUTPUT/${CROSS_BASE}fs" && rm -rf "$ROOT"
39MYBUILD="$BUILD/${CROSS_BASE:-host-}tmp"
40rm -rf "$MYBUILD" && mkdir -p "$MYBUILD" || exit 1
41
42# Stabilize cross compiling by providing known $PATH contents
43if [ ! -z "$CROSS_COMPILE" ]; then
44  if [ ! -e "$AIRLOCK/toybox" ]; then
45    echo === Create airlock dir
46    PREFIX="$AIRLOCK" KCONFIG_CONFIG="$TOP"/.airlock CROSS_COMPILE= \
47      make clean defconfig toybox install_airlock &&
48    rm "$TOP"/.airlock || exit 1
49  fi
50  export PATH="$CROSS_PATH:$AIRLOCK"
51fi
52
53### Create files and directories
54mkdir -p "$ROOT"/{etc,tmp,proc,sys,dev,home,mnt,root,usr/{bin,sbin,lib},var} &&
55chmod a+rwxt "$ROOT"/tmp && ln -s usr/{bin,sbin,lib} "$ROOT" || exit 1
56
57# init script. Runs as pid 1 from initramfs to set up and hand off system.
58cat > "$ROOT"/init << 'EOF' &&
59#!/bin/sh
60
61export HOME=/home PATH=/bin:/sbin
62
63mountpoint -q proc || mount -t proc proc proc
64mountpoint -q sys || mount -t sysfs sys sys
65if ! mountpoint -q dev; then
66  mount -t devtmpfs dev dev || mdev -s
67  for i in ,fd /0,stdin /1,stdout /2,stderr
68  do ln -sf /proc/self/fd${i/,*/} dev/${i/*,/}; done
69  mkdir -p dev/{shm,pts}
70  mountpoint -q dev/pts || mount -t devpts dev/pts dev/pts
71  chmod +t /dev/shm
72fi
73
74if [ $$ -eq 1 ]; then
75  # Setup networking for QEMU (needs /proc)
76  ifconfig eth0 10.0.2.15
77  route add default gw 10.0.2.2
78  [ "$(date +%s)" -lt 1000 ] && rdate 10.0.2.2 # Ask QEMU what time it is
79  [ "$(date +%s)" -lt 10000000 ] && ntpd -nq -p north-america.pool.ntp.org
80
81  [ -z "$CONSOLE" ] && CONSOLE="$(</sys/class/tty/console/active)"
82  [ -z "$HANDOFF" ] && HANDOFF=/bin/sh && echo Type exit when done.
83  exec /sbin/oneit -c /dev/"${CONSOLE:-console}" $HANDOFF
84else
85  /bin/sh
86  umount /dev/pts /dev /sys /proc
87fi
88EOF
89chmod +x "$ROOT"/init &&
90
91# passwd and group with kernel special accounts (root and nobody) + guest user
92cat > "$ROOT"/etc/passwd << 'EOF' &&
93root::0:0:root:/root:/bin/sh
94guest:x:500:500:guest:/home/guest:/bin/sh
95nobody:x:65534:65534:nobody:/proc/self:/dev/null
96EOF
97echo -e 'root:x:0:\nguest:x:500:\nnobody:x:65534:' > "$ROOT"/etc/group &&
98# Google's public nameserver.
99echo "nameserver 8.8.8.8" > "$ROOT"/etc/resolv.conf || exit 1
100
101# Build toybox
102make clean
103make $([ -z .config ] && echo defconfig || echo silentoldconfig)
104LDFLAGS=--static PREFIX="$ROOT" make toybox install || exit 1
105
106write_miniconfig()
107{
108  # Generic options for all targets
109  KCGLOB=EARLY_PRINTK,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
110
111  echo "# make ARCH=$KARCH allnoconfig KCONFIG_ALLCONFIG=$TARGET.miniconf"
112  echo "# make ARCH=$KARCH -j \$(nproc)"
113  echo "# boot $VMLINUX"
114  echo
115  echo "# CONFIG_EMBEDDED is not set"
116
117  # Expand list of =y symbols
118  for i in $KCGLOB $KCONF; do
119    echo "# architecture ${X:-independent}"
120    sed -E '/^$/d;s/([^,]*)($|,)/CONFIG_\1=y\n/g' <<< "$i"
121    X=specific
122  done
123  echo "$KERNEL_CONFIG"
124}
125
126if [ -z "$LINUX" ] || [ ! -d "$LINUX/kernel" ]; then
127  echo 'No $LINUX directory, kernel build skipped.'
128else
129
130  # Which architecture are we building a kernel for?
131  [ -z "$TARGET" ] && TARGET="${CROSS_BASE/-*/}"
132  [ -z "$TARGET" ] && TARGET="$(uname -m)"
133
134  # Target-specific info in an (alphabetical order) if/else staircase
135  # Each target needs board config, serial console, RTC, ethernet, block device.
136
137  if [ "$TARGET" == armv5l ]; then
138
139    # This could use the same VIRT board as armv7, but let's demonstrate a
140    # different one requiring a separate device tree binary.
141    QEMU="arm -M versatilepb -net nic,model=rtl8139 -net user"
142    KARCH=arm KARGS=ttyAMA0 VMLINUX=arch/arm/boot/zImage
143    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
144    KERNEL_CONFIG="CONFIG_SCSI_SYM53C8XX_DMA_ADDRESSING_MODE=0"
145    DTB=arch/arm/boot/dts/versatile-pb.dtb
146  elif [ "$TARGET" == armv7l ] || [ "$TARGET" == aarch64 ]; then
147    if [ "$TARGET" == aarch64 ]; then
148      QEMU="aarch64 -M virt -cpu cortex-a57"
149      KARCH=arm64 VMLINUX=arch/arm64/boot/Image
150    else
151      QEMU="arm -M virt" KARCH=arm VMLINUX=arch/arm/boot/zImage
152    fi
153    KARGS=ttyAMA0
154    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
155  elif [ "$TARGET" == i486 ] || [ "$TARGET" == i686 ] ||
156       [ "$TARGET" == x86_64 ] || [ "$TARGET" == x32 ]; then
157    if [ "$TARGET" == i486 ]; then
158      QEMU="i386 -cpu 486 -global fw_cfg.dma_enabled=false" KCONF=M486
159    elif [ "$TARGET" == i686 ]; then
160      QEMU="i386 -cpu pentium3" KCONF=MPENTIUMII
161    else
162      QEMU=x86_64 KCONF=64BIT
163      [ "$TARGET" == x32 ] && KCONF=X86_X32
164    fi
165    KARCH=x86 KARGS=ttyS0 VMLINUX=arch/x86/boot/bzImage
166    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
167  elif [ "$TARGET" == mips ] || [ "$TARGET" == mipsel ]; then
168    QEMU="mips -M malta" KARCH=mips KARGS=ttyS0 VMLINUX=vmlinux
169    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
170    [ "$TARGET" == mipsel ] && KCONF=$KCONF,CPU_LITTLE_ENDIAN &&
171      QEMU="mipsel -M malta"
172  elif [ "$TARGET" == powerpc ]; then
173    KARCH=powerpc QEMU="ppc -M g3beige" KARGS=ttyS0 VMLINUX=vmlinux
174    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
175
176  elif [ "$TARGET" == powerpc64le ]; then
177    KARCH=powerpc QEMU="ppc64 -M pseries -vga none" KARGS=/dev/hvc0
178    VMLINUX=vmlinux
179    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
180
181  elif [ "$TARGET" = s390x ] ; then
182    QEMU="s390x" KARCH=s390 VMLINUX=arch/s390/boot/bzImage
183    KCONF=MARCH_Z900,PACK_STACK,NET_CORE,VIRTIO_NET,VIRTIO_BLK,SCLP_TTY,SCLP_CONSOLE,SCLP_VT220_TTY,SCLP_VT220_CONSOLE,S390_GUEST
184  elif [ "$TARGET" == sh4 ] ; then
185    QEMU="sh4 -M r2d -serial null -serial mon:stdio" KARCH=sh
186    KARGS="ttySC1 noiotrap" VMLINUX=arch/sh/boot/zImage
187    KERNEL_CONFIG="CONFIG_MEMORY_START=0x0c000000"
188    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
189#see also SPI SPI_SH_SCI MFD_SM501 RTC_CLASS RTC_DRV_R9701 RTC_DRV_SH RTC_HCTOSYS
190  else
191    echo "Unknown \$TARGET"
192    exit 1
193  fi
194
195  # Write the qemu launch script
196  echo "qemu-system-$QEMU" '"$@"' -nographic -no-reboot -m 256 \
197       "-kernel $(basename "$VMLINUX") -initrd ${CROSS_BASE}root.cpio.gz" \
198       "-append \"panic=1 HOST=$TARGET console=$KARGS \$KARGS\"" \
199       ${DTB:+-dtb "$(basename "$DTB")"} > "$OUTPUT/qemu-$TARGET.sh" &&
200  chmod +x "$OUTPUT/qemu-$TARGET.sh" &&
201
202  echo "Build linux for $KARCH"
203  pushd "$LINUX" && make distclean && popd &&
204  cp -sfR "$LINUX" "$MYBUILD/linux" && pushd "$MYBUILD/linux" || exit 1
205  write_miniconfig > "$OUTPUT/miniconfig-$TARGET" &&
206  make ARCH=$KARCH allnoconfig KCONFIG_ALLCONFIG="$OUTPUT/miniconfig-$TARGET" &&
207  make ARCH=$KARCH CROSS_COMPILE="$CROSS_COMPILE" -j $(nproc) || exit 1
208
209  # If we have a device tree binary, save it for QEMU.
210  if [ ! -z "$DTB" ]; then
211    cp "$DTB" "$OUTPUT/$(basename "$DTB")" || exit 1
212  fi
213
214  cp "$VMLINUX" "$OUTPUT/$(basename "$VMLINUX")" && cd .. && rm -rf linux &&
215    popd || exit 1
216fi
217
218rmdir "$MYBUILD" "$BUILD" 2>/dev/null
219
220# package root filesystem for initramfs.
221# we do it here so module install can add files (not implemented yet)
222echo === create "${CROSS_BASE}root.cpio.gz"
223
224(cd "$ROOT" && find . | cpio -o -H newc | gzip) > \
225  "$OUTPUT/${CROSS_BASE}root.cpio.gz"
226