[ql-tipc] Add U-boot example

Implements system dependencies, RPMB storage callbacks, and Makefile for
U-boot.

Bug: 33050583
Test: Built with u-boot and manually tested on imx6ul
Change-Id: Icf42219f4f22fd94b52580a9c15c0e418faac124
diff --git a/ql-tipc/README.md b/ql-tipc/README.md
index 509918f..76e3781 100644
--- a/ql-tipc/README.md
+++ b/ql-tipc/README.md
@@ -15,6 +15,7 @@
 
 ### Misc
 
+- examples/ - Implementations of bootloader-specific code.
 - arch/$ARCH/ - Architecture dependent implementation of Trusty device
    (see trusty_dev.h). Implements SMCs on ARM for example.
 
diff --git a/ql-tipc/examples/u-boot/Makefile b/ql-tipc/examples/u-boot/Makefile
new file mode 100644
index 0000000..51eb7f8
--- /dev/null
+++ b/ql-tipc/examples/u-boot/Makefile
@@ -0,0 +1,45 @@
+#
+# Copyright (C) 2016 The Android Open Source Project
+#
+# Permission is hereby granted, free of charge, to any person
+# obtaining a copy of this software and associated documentation
+# files (the "Software"), to deal in the Software without
+# restriction, including without limitation the rights to use, copy,
+# modify, merge, publish, distribute, sublicense, and/or sell copies
+# of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+#
+
+# Sample Makefile for U-boot
+
+#ccflags-y += -DTIPC_ENABLE_DEBUG
+
+TRUSTY_DIR = lib/trusty
+ccflags-y += -I$(TRUSTY_DIR)/ql-tipc/include
+ccflags-y += -I$(TRUSTY_DIR)/interface/include
+
+QL_TIPC = ../..
+obj-y += \
+    $(QL_TIPC)/avb.o \
+    $(QL_TIPC)/ipc.o \
+    $(QL_TIPC)/ipc_dev.o \
+    $(QL_TIPC)/libtipc.o \
+    $(QL_TIPC)/rpmb_proxy.o \
+    sysdeps_uboot.o \
+    storage_ops_uboot.o
+
+obj-$(CONFIG_ARM) += \
+    $(QL_TIPC)/arch/arm/trusty_mem.o \
+    $(QL_TIPC)/arch/arm/trusty_dev.o
diff --git a/ql-tipc/examples/u-boot/storage_ops_uboot.c b/ql-tipc/examples/u-boot/storage_ops_uboot.c
new file mode 100644
index 0000000..10cfcc4
--- /dev/null
+++ b/ql-tipc/examples/u-boot/storage_ops_uboot.c
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <trusty/rpmb.h>
+#include <trusty/trusty_dev.h>
+#include <trusty/util.h>
+
+#include <common.h>
+#include <mmc.h>
+
+void *rpmb_storage_get_ctx(void)
+{
+    int ret;
+    struct mmc *mmc = find_mmc_device(0);
+
+    /* Switch to RPMB partition */
+    if (mmc->part_num != MMC_PART_RPMB) {
+        ret = mmc_switch_part(0, MMC_PART_RPMB);
+        if (ret) {
+            trusty_error("failed to switch to RPMB partition\n");
+            return NULL;
+        }
+        mmc->part_num = MMC_PART_RPMB;
+    }
+    return (void *)mmc;
+}
+
+int rpmb_storage_send(void *rpmb_dev, const void *rel_write_data,
+                      size_t rel_write_size, const void *write_data,
+                      size_t write_size, void *read_buf, size_t read_size)
+{
+    ALLOC_CACHE_ALIGN_BUFFER(uint8_t, rpmb_rel_write_data, rel_write_size);
+    ALLOC_CACHE_ALIGN_BUFFER(uint8_t, rpmb_write_data, write_size);
+    ALLOC_CACHE_ALIGN_BUFFER(uint8_t, rpmb_read_data, read_size);
+    int ret;
+
+    if (rel_write_size) {
+        if (rel_write_size % MMC_BLOCK_SIZE) {
+            trusty_error(
+                "rel_write_size is not a multiple of MMC_BLOCK_SIZE: %d\n",
+                 rel_write_size);
+            return TRUSTY_ERR_INVALID_ARGS;
+        }
+        memcpy(rpmb_rel_write_data, rel_write_data, rel_write_size);
+        ret = mmc_rpmb_request(rpmb_dev,
+                               (const struct s_rpmb *)rpmb_rel_write_data,
+                               rel_write_size / MMC_BLOCK_SIZE, true);
+        if (ret) {
+            trusty_error("failed to execute rpmb reliable write\n");
+            return ret;
+        }
+    }
+    if (write_size) {
+        if (write_size % MMC_BLOCK_SIZE) {
+            trusty_error("write_size is not a multiple of MMC_BLOCK_SIZE: %d\n",
+                         write_size);
+            return TRUSTY_ERR_INVALID_ARGS;
+        }
+        memcpy(rpmb_write_data, write_data, write_size);
+        ret = mmc_rpmb_request(rpmb_dev, (const struct s_rpmb *)rpmb_write_data,
+                               write_size / MMC_BLOCK_SIZE, false);
+        if (ret) {
+            trusty_error("failed to execute rpmb write\n");
+            return ret;
+        }
+    }
+    if (read_size) {
+        if (read_size % MMC_BLOCK_SIZE) {
+            trusty_error("read_size is not a multiple of MMC_BLOCK_SIZE: %d\n",
+                         read_size);
+            return TRUSTY_ERR_INVALID_ARGS;
+        }
+        ret = mmc_rpmb_response(rpmb_dev, (struct s_rpmb *)rpmb_read_data,
+                                read_size / MMC_BLOCK_SIZE, 0);
+        memcpy((void *)read_buf, rpmb_read_data, read_size);
+        if (ret) {
+            trusty_error("failed to execute rpmb read\n");
+            return ret;
+        }
+    }
+    return TRUSTY_ERR_NONE;
+}
diff --git a/ql-tipc/examples/u-boot/sysdeps_uboot.c b/ql-tipc/examples/u-boot/sysdeps_uboot.c
new file mode 100644
index 0000000..376f415
--- /dev/null
+++ b/ql-tipc/examples/u-boot/sysdeps_uboot.c
@@ -0,0 +1,124 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <trusty/sysdeps.h>
+
+#include <common.h>
+#include <linux/string.h>
+#include <malloc.h>
+
+extern int trusty_encode_page_info(struct ns_mem_page_info *page_info,
+                                   void *vaddr);
+
+void trusty_lock(struct trusty_dev *dev)
+{
+}
+void trusty_unlock(struct trusty_dev *dev)
+{
+}
+
+void trusty_local_irq_disable(unsigned long *state)
+{
+    disable_interrupts();
+}
+
+void trusty_local_irq_restore(unsigned long *state)
+{
+    enable_interrupts();
+}
+
+void trusty_idle(struct trusty_dev *dev)
+{
+    wfi();
+}
+
+void trusty_abort(void)
+{
+    do_reset(NULL, 0, 0, NULL);
+    __builtin_unreachable();
+}
+
+void trusty_printv(const char *message, ...)
+{
+    va_list ap;
+
+    va_start(ap, message);
+    vprintf(message, ap);
+    va_end(ap);
+}
+
+void *trusty_memcpy(void *dest, void *src, size_t n)
+{
+    return memcpy(dest, src, n);
+}
+
+void *trusty_memset(void *dest, const int c, size_t n)
+{
+    return memset(dest, c, n);
+}
+
+char *trusty_strcpy(char *dest, const char *src)
+{
+    return strcpy(dest, src);
+}
+
+size_t trusty_strlen(const char *str)
+{
+    return strlen(str);
+}
+
+void *trusty_calloc(size_t n, size_t size)
+{
+    return calloc(n, size);
+}
+
+void trusty_free(void *addr)
+{
+    if (addr)
+        free(addr);
+}
+
+void *trusty_membuf_alloc(struct ns_mem_page_info *page_info, size_t size)
+{
+    void *va = NULL;
+    int res;
+
+    va = memalign(4096, size);
+    if (!va)
+        return NULL;
+
+    /* get memory attibutes */
+    res = trusty_encode_page_info(page_info, va);
+    if (res) {
+        trusty_membuf_free(va);
+        return NULL;
+    }
+    return va;
+}
+
+void trusty_membuf_free(void *va)
+{
+    if (va)
+        free(va);
+}