Add bsp-specific build configuration from build/

- Bring in some board-specific files from build/: sdcard, recovery,
bootloader (formerly u-boot.mk). Also, add includable stubs for pbuilder
rules that belong in the bsp repository, and for package lists to
install in the rootfs.

Change-Id: Ie59915219429a7c58023185460f838e0edc93247
diff --git a/bootloader.mk b/bootloader.mk
new file mode 100644
index 0000000..a1f1f27
--- /dev/null
+++ b/bootloader.mk
@@ -0,0 +1,30 @@
+# 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
+
+bootloader: $(PRODUCT_OUT)/u-boot.imx
+
+$(PRODUCT_OUT)/u-boot.imx: uboot-imx | out-dirs
+	$(LOG) u-boot extract
+	find $(PRODUCT_OUT)/packages -name 'uboot-imx*.deb' | xargs \
+	dpkg --fsys-tarfile | \
+	tar --strip-components 2 -C $(PRODUCT_OUT) -xf - ./boot/u-boot.imx
+	$(LOG) u-boot finished
+
+.PHONY:: bootloader
\ No newline at end of file
diff --git a/packages.mk b/packages.mk
new file mode 100644
index 0000000..ae66d6f
--- /dev/null
+++ b/packages.mk
@@ -0,0 +1,36 @@
+# 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.
+
+$(eval $(call make-pbuilder-bsp-package-target,imx-atf,imx-atf))
+$(eval $(call make-pbuilder-bsp-package-target,imx-firmware,imx-firmware))
+$(eval $(call make-pbuilder-bsp-package-target,imx-mkimage,tools/imx-mkimage))
+$(eval $(call make-pbuilder-bsp-package-target,uboot-imx,uboot-imx,imx-atf imx-firmware imx-mkimage))
+$(eval $(call make-pbuilder-bsp-package-target,wayland-protocols-imx,wayland-protocols-imx))
+$(eval $(call make-pbuilder-bsp-package-target,weston-imx,weston-imx,wayland-protocols-imx))
+$(eval $(call make-pbuilder-bsp-package-target,linux-imx,linux-imx))
+$(eval $(call make-pbuilder-bsp-package-target,imx-gpu-viv,imx-gpu-viv,linux-imx,,binary))
+$(eval $(call make-pbuilder-bsp-package-target,libdrm-imx,libdrm-imx))
+$(eval $(call make-pbuilder-bsp-package-target,imx-vpu-hantro,imx-vpu-hantro,linux-imx,,binary))
+$(eval $(call make-pbuilder-bsp-package-target,imx-vpuwrap,imx-vpuwrap,imx-vpu-hantro,,binary))
+$(eval $(call make-pbuilder-bsp-package-target,imx-gstreamer,imx-gstreamer))
+$(eval $(call make-pbuilder-bsp-package-target,imx-gst-plugins-base,imx-gst-plugins-base,imx-gstreamer))
+$(eval $(call make-pbuilder-bsp-package-target,imx-gst-plugins-good,imx-gst-plugins-good,imx-gst-plugins-base))
+$(eval $(call make-pbuilder-bsp-package-target,imx-gst-plugins-bad,imx-gst-plugins-bad,\
+	libdrm-imx imx-gst-plugins-base linux-imx))
+$(eval $(call make-pbuilder-bsp-package-target,imx-gst1.0-plugin,imx-gst1.0-plugin,\
+	imx-vpuwrap imx-gst-plugins-bad))
+$(eval $(call make-pbuilder-bsp-package-target,aiy-board-audio,packages/aiy-board-audio))
+$(eval $(call make-pbuilder-bsp-package-target,aiy-board-tools,packages/aiy-board-tools))
+$(eval $(call make-pbuilder-bsp-package-target,aiy-board-wlan,packages/aiy-board-wlan))
+$(eval $(call make-pbuilder-bsp-package-target,bluez-imx,bluez-imx))
\ No newline at end of file
diff --git a/recovery.mk b/recovery.mk
new file mode 100644
index 0000000..47d8027
--- /dev/null
+++ b/recovery.mk
@@ -0,0 +1,41 @@
+# 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: | out-dirs
+	fallocate -l 4M $(PRODUCT_OUT)/recovery.img
+
+$(PRODUCT_OUT)/recovery.img: recovery-allocate \
+                           $(ROOTDIR)/board/bootloader.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
new file mode 100644
index 0000000..92b681c
--- /dev/null
+++ b/rootfs.mk
@@ -0,0 +1,32 @@
+# 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.
+
+BSP_BASE_PACKAGES := \
+	aiy-board-audio \
+	aiy-board-tools \
+	aiy-board-wlan \
+	linux-image-4.9.51-aiy \
+	uboot-imx
+
+ifeq ($(IS_EXTERNAL),)
+BSP_BASE_PACKAGES += edgetpu-api
+endif
+
+BSP_GUI_PACKAGES := \
+	imx-gpu-viv \
+	imx-gst1.0-plugin \
+	imx-vpu-hantro \
+	imx-vpuwrap \
+	libdrm-vivante \
+	weston-imx
diff --git a/sdcard.mk b/sdcard.mk
new file mode 100644
index 0000000..e8f32a3
--- /dev/null
+++ b/sdcard.mk
@@ -0,0 +1,77 @@
+# 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
+
+# Image size variables
+BOOT_START := 8
+ROOTFS_START := $(shell echo $(BOOT_START)+$(BOOT_SIZE_MB) | bc)
+UBOOT_START := 66# Cribbed from Yocto.
+SDIMAGE_SIZE_MB := $(shell echo $(ROOTFS_START)+$(ROOTFS_SIZE_MB) | bc)
+
+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