beagle_x15: Copy kernel modules to vendor partition

In order to make some subsystems functional (e.g. touchscreen, sound),
corresponding kernel modules (.ko) should be dynamically loaded. It can
be done automatically by ueventd daemon, thanks to this patch in
system/core:

    ueventd: Add dynamic kernel module loading

*This* patch implements kernel modules copying into /vendor/lib/modules:
  - if KERNELDIR environment variable is defined, then *.ko files will
    be copied from locally built kernel in KERNELDIR
  - otherwise *.ko files will be copied from prebuilt kernel dir

Right now we don't have *.ko files in prebuilt kernel dir, so some
functionality is missing (touchscreen, sound). It should be out
next step to add all required *.ko there.

Corresponding modules.dep and modules.alias files are generated
automatically by Android build system, in build/make project, thanks to
next commits:

    - Support kernel modules in vendor, recovery images
    - package modules.alias onto device

Now that SGX kernel module can be built inside of Linux kernel source
tree, so it will be copied along with other *.ko files. This way we can
avoid disagreement between kernel version and SGX module version, when
using locally built kernel from KERNELDIR. Also we want to warn the user
if SGX module is not available, as graphics won't work in this case.

While at it, provide the correct mechanism for copying dtb/dtbo files
from locally built kernel when KERNELDIR is defined.

A few points about implementation:

  1. In boot_fit/Android.mk file, before *.dtbo copying the check for
     *.dtbo existence is done:

         ifneq ($(wildcard $(DTBO_DIR)/*.dtbo),)
             cp $(DTBO_DIR)/*.dtbo $(PRIVATE_INTERMEDIATES)/
         endif

     This is needed because when using linux-mainline in KERNELDIR, next
     error will occur (because linux-mainline doesn't have *.dtbo files):

         FAILED: out/target/product/beagle_x15/boot_fit.img
         cp: bad '.../linux-mainline/arch/arm/boot/dts/ti/*.dtbo':
             No such file or directory

     For the same reason we don't provide $(wildcard $(DTBO_DIR)/*.dtbo)
     to $(BOOTIMG_FIT) target's prerequisites, like it's done for
     DTB_DIR (linux-mainline doesn't have dtbo's, os it's an optional
     dependency).

  2. In BoardConfig.mk file, the BOARD_VENDOR_KERNEL_MODULES is assigned
     via intermediate BOARD_ALL_MODULES variable, because if we assign
     it directly to $(shell ...) command, next build error happens:

         [100% 359/359] writing build rules ...
         FAILED:
         build/make/core/Makefile:2807: error: kati doesn't support
           passing results of $(shell) to other make constructs:
           find .../linux -type f -iname '*.ko'

All that said, we want (and we can, with this patch) to support AOSP
build with next kernel options:
  1. Prebuilt kernel from device/ti/beagle_x15-kernel/*
  2. Locally built TI Android kernel as KERNELDIR
  3. Locally built linux-mainline kernel as KERNELDIR

As (1) is convenient for end user, options (2) and (3) is convenient for
development.

Change-Id: I3aee2da99ede252919e7f42f74f9ba25ec7d364c
Signed-off-by: Sam Protsenko <semen.protsenko@linaro.org>
diff --git a/BoardConfig.mk b/BoardConfig.mk
index f493fa2..47c8a51 100644
--- a/BoardConfig.mk
+++ b/BoardConfig.mk
@@ -69,6 +69,13 @@
 TARGET_UBOOT_MAKE_TARGET := u-boot-img.bin
 TARGET_UBOOT_COPY_TARGETS := u-boot.img MLO
 
-# Graphics
-BOARD_VENDOR_KERNEL_MODULES += \
-	device/ti/beagle_x15-kernel/$(TARGET_KERNEL_USE)/pvrsrvkm.ko
+# Copy kernel modules (including pvrsrvkm.ko) into /vendor/lib/modules
+BOARD_ALL_MODULES := $(shell find $(LOCAL_KERNEL_HOME) -type f -iname '*.ko')
+BOARD_VENDOR_KERNEL_MODULES += $(BOARD_ALL_MODULES)
+
+# Check if SGX kernel module is present in chosen kernel directory
+SGX_KO := $(shell find $(LOCAL_KERNEL_HOME) -type f -name 'pvrsrvkm.ko')
+ifeq ($(SGX_KO),)
+  $(warning SGX module (pvrsrvkm.ko) not found, graphics won't work)
+  $(warning SGX module search path is: $(LOCAL_KERNEL_HOME))
+endif
diff --git a/boot_fit/Android.mk b/boot_fit/Android.mk
index fed6a39..fed87fa 100644
--- a/boot_fit/Android.mk
+++ b/boot_fit/Android.mk
@@ -17,12 +17,15 @@
 $(BOOTIMG_FIT): PRIVATE_INSTALLED_KERNEL_TARGET := $(BOOTIMG_FIT_INSTALLED_KERNEL_TARGET)
 $(BOOTIMG_FIT): PRIVATE_INSTALLED_RAMDISK_TARGET := $(BOOTIMG_FIT_INSTALLED_RAMDISK_TARGET)
 $(BOOTIMG_FIT): $(BOOTIMG_FIT_INSTALLED_KERNEL_TARGET) $(BOOTIMG_FIT_INSTALLED_RAMDISK_TARGET)
-$(BOOTIMG_FIT): $(BOARD_DIR)/$(ITS) $(MKIMAGE) $(DTC) $(wildcard $(LOCAL_KERNEL)/$(TARGET_KERNEL_USE)/*.dtb*)
+$(BOOTIMG_FIT): $(BOARD_DIR)/$(ITS) $(MKIMAGE) $(DTC) $(wildcard $(DTB_DIR)/*.dtb)
 	mkdir -p $(PRIVATE_INTERMEDIATES)
 	cp $(PRIVATE_BOARD_DIR)/$(PRIVATE_ITS) $(PRIVATE_INTERMEDIATES)/
 	cp $(PRIVATE_INSTALLED_RAMDISK_TARGET) $(PRIVATE_INTERMEDIATES)/
 	cp $(PRIVATE_INSTALLED_KERNEL_TARGET) $(PRIVATE_INTERMEDIATES)/zImage
-	cp $(LOCAL_KERNEL)/$(TARGET_KERNEL_USE)/*.dtb* $(PRIVATE_INTERMEDIATES)/
+	cp $(DTB_DIR)/*.dtb $(PRIVATE_INTERMEDIATES)/
+ifneq ($(wildcard $(DTBO_DIR)/*.dtbo),)
+	cp $(DTBO_DIR)/*.dtbo $(PRIVATE_INTERMEDIATES)/
+endif
 	PATH=$(HOST_OUT_EXECUTABLES):$$PATH $(PRIVATE_MKIMAGE) -D "$(PRIVATE_DTC_FLAGS_MKIMAGE)" -f $(PRIVATE_INTERMEDIATES)/$(PRIVATE_ITS) $@
 
 include $(CLEAR_VARS)
diff --git a/device.mk b/device.mk
index b766b4e..576694c 100644
--- a/device.mk
+++ b/device.mk
@@ -23,10 +23,24 @@
 # Set custom settings
 DEVICE_PACKAGE_OVERLAYS := device/ti/beagle_x15/overlay
 
-LOCAL_KERNEL := device/ti/beagle_x15-kernel
+PREBUILT_DIR := device/ti/beagle_x15-kernel
 TARGET_KERNEL_USE ?= 4.14
-TARGET_PREBUILT_KERNEL := $(LOCAL_KERNEL)/$(TARGET_KERNEL_USE)/zImage
-PRODUCT_COPY_FILES += $(TARGET_PREBUILT_KERNEL):kernel
+
+# Helper variables for working with kernel files
+ifneq ($(KERNELDIR),)
+  LOCAL_KERNEL_HOME := $(KERNELDIR)
+  LOCAL_KERNEL := $(KERNELDIR)/arch/arm/boot/zImage
+  DTB_DIR := $(KERNELDIR)/arch/arm/boot/dts
+  DTBO_DIR := $(KERNELDIR)/arch/arm/boot/dts/ti
+else
+  LOCAL_KERNEL_HOME := $(PREBUILT_DIR)/$(TARGET_KERNEL_USE)
+  LOCAL_KERNEL := $(LOCAL_KERNEL_HOME)/zImage
+  DTB_DIR := $(LOCAL_KERNEL_HOME)
+  DTBO_DIR := $(DTB_DIR)
+endif
+
+TARGET_PREBUILT_KERNEL := $(LOCAL_KERNEL)
+PRODUCT_COPY_FILES += $(LOCAL_KERNEL):kernel
 
 # Graphics
 PRODUCT_PACKAGES += \