Move bsp-specific configuration out of build/

- Moves lots of board specific stuff out of this repo. That includes
stuff like bootloader building, recovery images, sdcard (if a platform
supports it), and board-specific pbuilder targets.
- Also notably, this allows pbuilder output to end up in different
directories, to help mirror our apt repository configuration: after this
change, packages will either end up in packages/core or packages/bsp.

Change-Id: I11d137f8ba8d71ef3e14afa325d79be06d8ded0e
diff --git a/Makefile b/Makefile
index fe5721e..9f40f37 100644
--- a/Makefile
+++ b/Makefile
@@ -25,11 +25,12 @@
 	+make -f $(ROOTDIR)/build/Makefile all
 
 out-dirs:
-	@mkdir -p $(PRODUCT_OUT)/packages
+	@mkdir -p $(PRODUCT_OUT)/packages/core
+	@mkdir -p $(PRODUCT_OUT)/packages/bsp
 	@mkdir -p $(PRODUCT_OUT)/obj
 	@mkdir -p $(ROOTDIR)/cache
 
-all: $(PRODUCT_OUT)/rootfs.img $(PRODUCT_OUT)/u-boot.imx partition-table
+all: rootfs bootloader partition-table
 
 help: targets
 targets::
@@ -41,13 +42,13 @@
 include $(ROOTDIR)/build/partition-table.mk
 include $(ROOTDIR)/build/prereqs.mk
 include $(ROOTDIR)/build/rootfs.mk
-include $(ROOTDIR)/build/sdcard.mk
-include $(ROOTDIR)/build/u-boot.mk
-include $(ROOTDIR)/build/recovery.mk
-
 include $(ROOTDIR)/build/docker.mk
 include $(ROOTDIR)/build/packages.mk
 
+include $(ROOTDIR)/board/bootloader.mk
+-include $(ROOTDIR)/board/sdcard.mk
+-include $(ROOTDIR)/board/recovery.mk
+
 clean::
 	rm -rf $(ROOTDIR)/out
 
diff --git a/packages.mk b/packages.mk
index c5eee86..db36dfc 100644
--- a/packages.mk
+++ b/packages.mk
@@ -56,6 +56,7 @@
 # $3: space separated list of package dependencies (may be empty)
 # $4: space separated list of external dependencies (may be empty)
 # $5: dpkg-buildpackage --build value (may be empty, defaults to full)
+# $6: repository which package belongs to (e.g. core or bsp)
 define make-pbuilder-package-target
 $1: $(PRODUCT_OUT)/.$1-pbuilder-$(USERSPACE_ARCH)
 PBUILDER_TARGETS += $(PRODUCT_OUT)/.$1-pbuilder-$(USERSPACE_ARCH)
@@ -87,7 +88,7 @@
 
 	$(LOG) $1 pbuilder pdebuild
 	cd $(PRODUCT_OUT)/obj/$1; pdebuild \
-		--buildresult $(PRODUCT_OUT)/packages -- \
+		--buildresult $(PRODUCT_OUT)/packages/$(if $6,$6,core) -- \
 		--debbuildopts "--build=$(if $5,$5,full)" \
 		--basetgz $(ROOTDIR)/cache/base.tgz \
 		--configfile $(ROOTDIR)/build/pbuilderrc \
@@ -109,43 +110,22 @@
 .PHONY:: $1
 endef
 
-$(eval $(call make-pbuilder-package-target,imx-atf,imx-atf))
-$(eval $(call make-pbuilder-package-target,imx-firmware,imx-firmware))
-$(eval $(call make-pbuilder-package-target,imx-mkimage,tools/imx-mkimage))
-$(eval $(call make-pbuilder-package-target,uboot-imx,uboot-imx,imx-atf imx-firmware imx-mkimage))
+# Convenience macro to target a package to the bsp repo
+define make-pbuilder-bsp-package-target
+$(call make-pbuilder-package-target,$1,$2,$3,$4,$5,bsp)
+endef
 
-$(eval $(call make-pbuilder-package-target,wayland-protocols-imx,wayland-protocols-imx))
-$(eval $(call make-pbuilder-package-target,weston-imx,weston-imx,wayland-protocols-imx))
-
-$(eval $(call make-pbuilder-package-target,linux-imx,linux-imx))
-
-$(eval $(call make-pbuilder-package-target,imx-gpu-viv,imx-gpu-viv,linux-imx,,binary))
-$(eval $(call make-pbuilder-package-target,libdrm-imx,libdrm-imx))
-$(eval $(call make-pbuilder-package-target,imx-vpu-hantro,imx-vpu-hantro,linux-imx,,binary))
-$(eval $(call make-pbuilder-package-target,imx-vpuwrap,imx-vpuwrap,imx-vpu-hantro,,binary))
-$(eval $(call make-pbuilder-package-target,imx-gstreamer,imx-gstreamer))
-$(eval $(call make-pbuilder-package-target,imx-gst-plugins-base,imx-gst-plugins-base,imx-gstreamer))
-$(eval $(call make-pbuilder-package-target,imx-gst-plugins-good,imx-gst-plugins-good,imx-gst-plugins-base))
-$(eval $(call make-pbuilder-package-target,imx-gst-plugins-bad,imx-gst-plugins-bad,\
-	libdrm-imx imx-gst-plugins-base linux-imx))
-$(eval $(call make-pbuilder-package-target,imx-gst1.0-plugin,imx-gst1.0-plugin,\
-	imx-vpuwrap imx-gst-plugins-bad))
-
-$(eval $(call make-pbuilder-package-target,aiy-board-audio,packages/aiy-board-audio))
 $(eval $(call make-pbuilder-package-target,aiy-board-gadget,packages/aiy-board-gadget))
 $(eval $(call make-pbuilder-package-target,aiy-board-keyring,packages/aiy-board-keyring))
-$(eval $(call make-pbuilder-package-target,aiy-board-tools,packages/aiy-board-tools))
 $(eval $(call make-pbuilder-package-target,aiy-board-tweaks,packages/aiy-board-tweaks))
-$(eval $(call make-pbuilder-package-target,aiy-board-wlan,packages/aiy-board-wlan))
-
-$(eval $(call make-pbuilder-package-target,bluez-imx,bluez-imx))
-
 $(eval $(call make-pbuilder-package-target,base-files,packages/base-files))
 
 ifeq ($(IS_EXTERNAL),)
 $(eval $(call make-pbuilder-package-target,edgetpu-api,packages/edgetpu-api,,,binary))
 endif
 
+include $(ROOTDIR)/board/packages.mk
+
 ALL_PACKAGE_TARGETS := $(PBUILDER_TARGETS)
 packages-tarball: $(ROOTDIR)/cache/packages.tgz
 $(info )
diff --git a/preamble.mk b/preamble.mk
index 4f3de09..3f853a4 100644
--- a/preamble.mk
+++ b/preamble.mk
@@ -156,10 +156,5 @@
 PACKAGES_REVISION ?= latest
 PACKAGES_FETCH_ROOT_DIRECTORY ?= /google/data/ro/teams/spacepark/enterprise/kokoro/prod/spacepark/enterprise/continuous
 
-# Image size variables
-BOOT_START := 8
 BOOT_SIZE_MB := 128
-ROOTFS_START := $(shell echo $(BOOT_START)+$(BOOT_SIZE_MB) | bc)
-ROOTFS_SIZE_MB := 4096
-UBOOT_START := 66# Cribbed from Yocto.
-SDIMAGE_SIZE_MB := $(shell echo $(ROOTFS_START)+$(ROOTFS_SIZE_MB) | bc)
+ROOTFS_SIZE_MB := 4096
\ No newline at end of file
diff --git a/recovery.mk b/recovery.mk
deleted file mode 100644
index d7b999b..0000000
--- a/recovery.mk
+++ /dev/null
@@ -1,41 +0,0 @@
-# Copyright 2018 Google LLC
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#     https://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-ifeq ($(ROOTDIR),)
-$(error $$ROOTDIR IS NOT DEFINED -- don\'t forget to source setup.sh)
-endif
-
-include $(ROOTDIR)/build/preamble.mk
-
-recovery: $(PRODUCT_OUT)/recovery.img
-recovery-xz: $(PRODUCT_OUT)/recovery.img.xz
-
-recovery-allocate: | $(PRODUCT_OUT)
-	fallocate -l 4M $(PRODUCT_OUT)/recovery.img
-
-$(PRODUCT_OUT)/recovery.img: recovery-allocate \
-                           $(ROOTDIR)/build/u-boot.mk \
-                           | $(PRODUCT_OUT)/u-boot.imx
-	dd if=$(PRODUCT_OUT)/u-boot.imx of=$(PRODUCT_OUT)/recovery.img conv=notrunc seek=66 bs=512
-
-$(PRODUCT_OUT)/recovery.img.xz: $(PRODUCT_OUT)/recovery.img
-	xz -k -T0 -0 $(PRODUCT_OUT)/recovery.img
-
-targets::
-	@echo "recovery     - generate a flashable recovery image"
-
-clean::
-	rm -f $(PRODUCT_OUT)/recovery.img
-
-.PHONY:: recovery recovery-xz
diff --git a/rootfs.mk b/rootfs.mk
index 96ff62b..7b5a9f0 100644
--- a/rootfs.mk
+++ b/rootfs.mk
@@ -26,12 +26,9 @@
 ROOTFS_REVISION ?= latest
 
 BASE_PACKAGES := \
-	aiy-board-audio \
 	aiy-board-gadget \
 	aiy-board-keyring \
-	aiy-board-tools \
 	aiy-board-tweaks \
-	aiy-board-wlan \
 	base-files \
 	bluetooth \
 	bluez \
@@ -39,10 +36,6 @@
 	linux-image-4.9.51-aiy \
 	uboot-imx
 
-ifeq ($(IS_EXTERNAL),)
-BASE_PACKAGES += edgetpu-api
-endif
-
 GUI_PACKAGES := \
 	gstreamer1.0-alsa \
 	gstreamer1.0-plugins-bad \
@@ -50,26 +43,21 @@
 	gstreamer1.0-plugins-base-apps \
 	gstreamer1.0-plugins-good \
 	gstreamer1.0-tools \
-	imx-gpu-viv \
-	imx-gst1.0-plugin \
-	imx-vpu-hantro \
-	imx-vpuwrap \
 	libdrm2 \
-	libdrm-vivante \
 	libgstreamer1.0-0 \
 	libgstreamer-plugins-bad1.0-0 \
-	libgstreamer-plugins-base1.0-0 \
-	wayland-protocols \
-	weston-imx
+	libgstreamer-plugins-base1.0-0
+
+include $(ROOTDIR)/board/rootfs.mk
 
 ifeq ($(HEADLESS_BUILD),)
     $(info )
     $(info *** GUI build selected -- set HEADLESS_BUILD=true if this is not what you intend.)
-	PRE_INSTALL_PACKAGES := $(BASE_PACKAGES) $(GUI_PACKAGES)
+	PRE_INSTALL_PACKAGES := $(BASE_PACKAGES) $(BSP_BASE_PACKAGES) $(GUI_PACKAGES) $(BSP_GUI_PACKAGES)
 else
     $(info )
     $(info *** Headless build selected -- unset HEADLESS_BUILD if this is not what you intend.)
-	PRE_INSTALL_PACKAGES := $(BASE_PACKAGES)
+	PRE_INSTALL_PACKAGES := $(BASE_PACKAGES) $(BSP_BASE_PACKAGES)
 endif
 
 rootfs: $(PRODUCT_OUT)/rootfs.img
diff --git a/sdcard.mk b/sdcard.mk
deleted file mode 100644
index eae07d6..0000000
--- a/sdcard.mk
+++ /dev/null
@@ -1,71 +0,0 @@
-# Copyright 2018 Google LLC
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#     https://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-ifeq ($(ROOTDIR),)
-$(error $$ROOTDIR IS NOT DEFINED -- don\'t forget to source setup.sh)
-endif
-
-include $(ROOTDIR)/build/preamble.mk
-
-sdcard: $(PRODUCT_OUT)/sdcard.img
-sdcard-xz: $(PRODUCT_OUT)/sdcard.img.xz
-
-sdcard-allocate: | $(PRODUCT_OUT)
-	fallocate -l $(SDIMAGE_SIZE_MB)M $(PRODUCT_OUT)/sdcard.img
-
-$(PRODUCT_OUT)/sdcard.img: sdcard-allocate \
-                           $(ROOTDIR)/build/rootfs.mk \
-                           $(ROOTDIR)/build/boot.mk \
-                           $(ROOTDIR)/build/u-boot.mk \
-                           $(ROOTDIR)/board/fstab.sdcard \
-                           | $(PRODUCT_OUT)/u-boot.imx \
-                           $(PRODUCT_OUT)/boot.img \
-                           $(PRODUCT_OUT)/obj/ROOTFS/rootfs.patched.img
-	parted -s $(PRODUCT_OUT)/sdcard.img mklabel msdos
-	parted -s $(PRODUCT_OUT)/sdcard.img unit MiB mkpart primary ext2 $(BOOT_START) $(ROOTFS_START)
-	parted -s $(PRODUCT_OUT)/sdcard.img unit MiB mkpart primary ext4 $(ROOTFS_START) 100%
-	dd if=$(PRODUCT_OUT)/u-boot.imx of=$(PRODUCT_OUT)/sdcard.img conv=notrunc seek=$(UBOOT_START) bs=512
-	dd if=$(PRODUCT_OUT)/boot.img of=$(PRODUCT_OUT)/sdcard.img conv=notrunc seek=$(BOOT_START) bs=1M
-	dd if=$(PRODUCT_OUT)/obj/ROOTFS/rootfs.patched.img \
-		of=$(PRODUCT_OUT)/sdcard.img conv=notrunc seek=$(ROOTFS_START) bs=1M
-
-
-	mkdir -p $(ROOTFS_DIR)
-	-sudo umount $(ROOTFS_DIR)/boot
-	-sudo umount $(ROOTFS_DIR)
-	$(eval LOOP=$(shell sudo losetup --show -f $(PRODUCT_OUT)/sdcard.img))
-	-sudo partx -d $(LOOP)
-	sudo partx -a $(LOOP)
-
-	sudo mount $(LOOP)p2 $(ROOTFS_DIR)
-	sudo mount $(LOOP)p1 $(ROOTFS_DIR)/boot
-
-	sudo cp $(ROOTDIR)/board/fstab.sdcard $(ROOTFS_DIR)/etc/fstab
-
-	sudo umount $(ROOTFS_DIR)/boot
-	sudo umount $(ROOTFS_DIR)
-	sudo partx -d $(LOOP)
-	sudo losetup -d $(LOOP)
-	rmdir $(ROOTFS_DIR)
-
-$(PRODUCT_OUT)/sdcard.img.xz: $(PRODUCT_OUT)/sdcard.img
-	xz -k -T0 -0 $(PRODUCT_OUT)/sdcard.img
-
-targets::
-	@echo "sdcard     - generate a flashable sdcard image"
-
-clean::
-	rm -f $(PRODUCT_OUT)/sdcard.img $(PRODUCT_OUT)/sdcard.img.xz
-
-.PHONY:: sdcard sdcard-xz
diff --git a/u-boot.mk b/u-boot.mk
deleted file mode 100644
index 2f0d789..0000000
--- a/u-boot.mk
+++ /dev/null
@@ -1,33 +0,0 @@
-# Copyright 2018 Google LLC
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#     https://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-ifeq ($(ROOTDIR),)
-$(error $$ROOTDIR IS NOT DEFINED -- don\'t forget to source setup.sh)
-endif
-
-include $(ROOTDIR)/build/preamble.mk
-
-u-boot: $(PRODUCT_OUT)/u-boot.imx
-
-$(PRODUCT_OUT)/u-boot.imx: uboot-imx | out-dirs
-	$(LOG) u-boot extract
-	dpkg --fsys-tarfile $(PRODUCT_OUT)/packages/uboot-imx*.deb | \
-	tar --strip-components 2 -C $(PRODUCT_OUT) -xf - ./boot/u-boot.imx
-	$(LOG) u-boot finished
-
-targets::
-	@echo "u-boot - builds the bootloader"
-
-.PHONY:: u-boot
-
diff --git a/update_packages.sh b/update_packages.sh
index d435649..667cd5c 100755
--- a/update_packages.sh
+++ b/update_packages.sh
@@ -16,5 +16,6 @@
 
 (
 	flock -e 200
-	( cd $PRODUCT_OUT/packages; apt-ftparchive packages . > Packages )
+	( cd $PRODUCT_OUT/packages; apt-ftparchive packages bsp > Packages )
+	( cd $PRODUCT_OUT/packages; apt-ftparchive packages core >> Packages )
 ) 200>$PRODUCT_OUT/obj/packages-lock