[Feature]add MT2731_MP2_MR2_SVN388 baseline version

Change-Id: Ief04314834b31e27effab435d3ca8ba33b499059
diff --git a/src/bsp/lk/lib/nftl/nftl_bdev.c b/src/bsp/lk/lib/nftl/nftl_bdev.c
new file mode 100644
index 0000000..c7de4ac
--- /dev/null
+++ b/src/bsp/lk/lib/nftl/nftl_bdev.c
@@ -0,0 +1,234 @@
+/*
+ * Copyright (c) 2018 MediaTek Inc.
+ *
+ * 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 <err.h>
+#include <errno.h>
+#include <lib/bio.h>
+#include <lib/nftl.h>
+#include <lib/partition.h>
+#include <malloc.h>
+#include <pow2.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+
+#define BBT_RESERVED_BLOCK  4
+#define SGPT_RESERVED_BLOCK 4
+
+static int nftl_bio_block_mapping(struct bdev *dev, struct nftl_info *info,
+                                  bnum_t *page)
+{
+    struct nftl_info *part;
+    u64 offset, start = 0;
+    u32 i, ppb = info->erase_size / info->write_size;
+    u32 block_count;
+    bool align;
+    bnum_t cur_page;
+    int ret = 0;
+
+#ifdef SUPPORT_GPT_FIXED_LBS
+    block_count = dev->block_count * (dev->block_size / info->write_size);
+#else
+    block_count = dev->block_count;
+#endif
+    offset = (u64)info->write_size * (*page);
+    part = nftl_search_by_address(offset, &start);
+    if (part == NULL) {
+        cur_page = *page;
+        ret = nftl_block_isbad(info, cur_page);
+        align = (block_count - 1 - cur_page) / ppb;
+        /* block not in partitions */
+        if (align && ret) {
+            i = 1;
+            do {
+                cur_page += ppb;
+                ret = nftl_block_isbad(info, cur_page);
+                i++;
+            } while (ret && i < SGPT_RESERVED_BLOCK);
+        }
+        if (ret == 0)
+            *page = cur_page;
+        else
+            ret = ERR_NOT_FOUND;
+    } else {
+        offset -= start;
+        *page = offset / info->write_size;
+        ret = nftl_block_mapping(part, page);
+        *page += start / info->write_size;
+    }
+
+    return ret;
+}
+
+static ssize_t nftl_bio_read_block(struct bdev *dev, void *buf, bnum_t block,
+                                   uint count)
+{
+    struct nftl_info *info = nftl_open(dev->name);
+    uint32_t page_per_block = info->erase_size / info->write_size;
+    uint32_t read_count;
+    ssize_t err = 0, bytes_read = 0;
+    bnum_t phy_block;
+
+#ifdef SUPPORT_GPT_FIXED_LBS
+    count *= (dev->block_size / info->write_size);
+    block *= (dev->block_size / info->write_size);
+#endif
+    while (count) {
+        read_count = page_per_block - (block % page_per_block);
+        read_count = MIN(read_count, count);
+        phy_block = block;
+        err = nftl_bio_block_mapping(dev, info, &phy_block);
+        if (err < 0)
+            break;
+        err = nftl_read(info, buf, phy_block * info->write_size,
+                        read_count * info->write_size);
+        if (err < 0)
+            break;
+        bytes_read += read_count * info->write_size;
+        count -= read_count;
+        block += read_count;
+        buf += read_count * info->write_size;
+    }
+
+    return (err < 0) ? err : bytes_read;
+}
+
+static ssize_t nftl_bio_write_block(struct bdev *dev, const void *buf,
+                                    bnum_t block, uint count)
+{
+    struct nftl_info *info = nftl_open(dev->name);
+    uint32_t page_per_block = info->erase_size / info->write_size;
+    uint32_t write_count;
+    ssize_t err = 0, bytes_write = 0;
+    bnum_t phy_block;
+
+#ifdef SUPPORT_GPT_FIXED_LBS
+    count *= (dev->block_size / info->write_size);
+    block *= (dev->block_size / info->write_size);
+#endif
+    while (count) {
+        write_count = page_per_block - (block % page_per_block);
+        write_count = MIN(write_count, count);
+        phy_block = block;
+        err = nftl_bio_block_mapping(dev, info, &phy_block);
+        if (err < 0)
+            break;
+        err = nftl_write(info, buf, phy_block * info->write_size,
+                         write_count * info->write_size);
+        if (err < 0)
+            break;
+        bytes_write += write_count * info->write_size;
+        count -= write_count;
+        block += write_count;
+        buf += write_count * info->write_size;
+    }
+
+    return (err < 0) ? err : bytes_write;
+}
+
+static ssize_t nftl_bio_erase(struct bdev *dev, off_t offset, size_t len)
+{
+    u32 blocks;
+    ssize_t erase_len = 0, ret = 0;
+    struct nftl_info *info = nftl_open(dev->name);
+    bnum_t erase_page;
+
+    if (offset % info->erase_size)
+        return ERR_INVALID_ARGS;
+    if (len % info->erase_size)
+        return ERR_INVALID_ARGS;
+    if (len == 0)
+        return 0;
+
+    blocks = len / info->erase_size;
+
+    while (blocks) {
+        erase_page = offset / info->write_size;
+        if (nftl_bio_block_mapping(dev, info, &erase_page) >= 0) {
+            ret = nftl_erase(info, erase_page * info->write_size, info->erase_size);
+            if (ret < 0)
+                break;
+            erase_len += info->erase_size;
+        }
+        offset += info->erase_size;
+        blocks--;
+    }
+
+    return (ret < 0) ? ret : erase_len;
+}
+
+static int nftl_bio_ioctl(struct bdev *dev, int request, void *argp)
+{
+    struct nftl_info *info = nftl_open(dev->name);
+
+    return info->ioctl(info, request, argp);
+}
+
+int nftl_mount_bdev(struct nftl_info *info)
+{
+    struct bdev *dev;
+    bio_erase_geometry_info_t *geometry;
+    u32 lba_count, lba_size;
+
+    dev = (struct bdev *)malloc(sizeof(struct bdev));
+    if (!dev) {
+        dprintf(CRITICAL, "%s: no enough memory\n", __func__);
+        return -ENOMEM;
+    }
+
+    memset(dev, 0, sizeof(struct bdev));
+
+    geometry = malloc(sizeof(bio_erase_geometry_info_t));
+    if (!geometry) {
+        dprintf(CRITICAL, "%s: no enough memory for geometry\n", __func__);
+        free(dev);
+        return -ENOMEM;
+    }
+
+#ifdef SUPPORT_GPT_FIXED_LBS
+    lba_size = 4096;
+#else
+    lba_size = info->write_size;
+#endif
+    lba_count = info->total_size / lba_size;
+    /* reserve 4blocks for bbt and 4blocks for SGPT */
+    lba_count -= (BBT_RESERVED_BLOCK * info->erase_size / lba_size);
+    lba_count -= (SGPT_RESERVED_BLOCK * info->erase_size / lba_size);
+
+    geometry->start = 0;
+    geometry->size = lba_count * lba_size;
+    geometry->erase_size = info->erase_size;
+    geometry->erase_shift = log2_uint(info->erase_size);
+
+    bio_initialize_bdev(dev, info->name, lba_size, lba_count,
+                        1, geometry, BIO_FLAGS_NONE);
+    dev->read_block = nftl_bio_read_block;
+    dev->write_block = nftl_bio_write_block;
+    dev->erase = nftl_bio_erase;
+    dev->erase_byte = 0xff;
+    dev->ioctl = nftl_bio_ioctl;
+    bio_register_device(dev);
+
+    partition_publish(info->name, 0);
+
+    return 0;
+}
diff --git a/src/bsp/lk/lib/nftl/nftl_core.c b/src/bsp/lk/lib/nftl/nftl_core.c
new file mode 100644
index 0000000..30769c4
--- /dev/null
+++ b/src/bsp/lk/lib/nftl/nftl_core.c
@@ -0,0 +1,92 @@
+/*
+ * Copyright (c) 2018 MediaTek Inc.
+ *
+ * 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 <debug.h>
+#include <err.h>
+#include <errno.h>
+#include <lib/nftl.h>
+#include <sys/types.h>
+
+int nftl_block_isbad(struct nftl_info *info, u32 page)
+{
+    if (page >= info->total_size / info->write_size)
+        return ERR_INVALID_ARGS;
+
+    return info->block_isbad(info, page);
+}
+
+int nftl_block_mapping(struct nftl_info *info, u32 *page)
+{
+    u32 page_per_block = info->erase_size / info->write_size;
+    u32 count = info->total_size / info->write_size;
+    u32 i, block;
+    int ret = 0;
+
+    if (*page >= info->total_size / info->write_size)
+        return ERR_INVALID_ARGS;
+
+    block = *page / page_per_block + 1;
+
+    for (i = 0; i < count; i += page_per_block) {
+        if (nftl_block_isbad(info, i))
+            continue;
+
+        if (--block == 0)
+            break;
+    }
+
+    if (block == 0)
+        *page = i + *page % page_per_block;
+    else
+        ret = ERR_OUT_OF_RANGE;
+
+    return ret;
+}
+
+ssize_t nftl_erase(struct nftl_info *info, off_t offset, ssize_t len)
+{
+    if (offset < 0 || len < 0 || (u64)offset >= info->total_size
+        || (u64)len > info->total_size - offset)
+        return ERR_INVALID_ARGS;
+
+    return info->erase(info, offset, len);
+}
+
+ssize_t nftl_read(struct nftl_info *info, void *buf, off_t offset, ssize_t len)
+{
+    if (offset < 0 || len < 0 || (u64)offset >= info->total_size
+        || (u64)len > info->total_size - offset)
+        return ERR_INVALID_ARGS;
+
+    return info->read(info, buf, offset, len);
+}
+
+ssize_t nftl_write(struct nftl_info *info, const void *buf, off_t offset,
+                   ssize_t len)
+{
+    if (offset < 0 || len < 0 || (u64)offset >= info->total_size
+        || (u64)len > info->total_size - offset)
+        return ERR_INVALID_ARGS;
+
+    return info->write(info, buf, offset, len);
+}
+
diff --git a/src/bsp/lk/lib/nftl/nftl_part.c b/src/bsp/lk/lib/nftl/nftl_part.c
new file mode 100644
index 0000000..22402e2
--- /dev/null
+++ b/src/bsp/lk/lib/nftl/nftl_part.c
@@ -0,0 +1,232 @@
+/*
+ * Copyright (c) 2018 MediaTek Inc.
+ *
+ * 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 <assert.h>
+#include <err.h>
+#include <errno.h>
+#include <kernel/mutex.h>
+#include <lib/nftl.h>
+#include <malloc.h>
+#include <string.h>
+#include <sys/types.h>
+
+struct nftl_part {
+    struct nftl_info info;
+    struct nftl_info *parent;
+
+    struct list_node node;
+    volatile int ref;
+
+    /* start erase block */
+    u64 offset;
+};
+
+static struct {
+    struct list_node list;
+    mutex_t lock;
+} nftlparts = {
+    .list = LIST_INITIAL_VALUE(nftlparts.list),
+    .lock = MUTEX_INITIAL_VALUE(nftlparts.lock),
+};
+
+static void nftl_part_register(struct nftl_part *part)
+{
+    DEBUG_ASSERT(part);
+
+    part->ref = 1;
+    mutex_acquire(&nftlparts.lock);
+    list_add_tail(&nftlparts.list, &part->node);
+    mutex_release(&nftlparts.lock);
+}
+
+static void nftl_part_unregister(struct nftl_part *part)
+{
+    DEBUG_ASSERT(part);
+
+    mutex_acquire(&nftlparts.lock);
+    list_delete(&part->node);
+    mutex_release(&nftlparts.lock);
+    part->ref--;
+}
+
+struct nftl_info *nftl_search_by_address(u64 address, u64 *start)
+{
+    struct nftl_part *entry;
+    struct nftl_info *info = NULL;
+
+    mutex_acquire(&nftlparts.lock);
+    list_for_every_entry(&nftlparts.list, entry, struct nftl_part, node) {
+        if (entry->parent != NULL && entry->offset <= address
+            && entry->info.total_size > address - entry->offset) {
+            info = &entry->info;
+            *start = entry->offset;
+            break;
+        }
+    }
+    mutex_release(&nftlparts.lock);
+
+    return info;
+}
+
+struct nftl_info *nftl_open(const char *name)
+{
+    struct nftl_part *entry;
+    struct nftl_info *info = NULL;
+
+    mutex_acquire(&nftlparts.lock);
+    list_for_every_entry(&nftlparts.list, entry, struct nftl_part, node) {
+        if (!strcmp(entry->info.name, name) || !strcmp(entry->info.label, name)) {
+            info = &entry->info;
+            entry->ref++;
+            break;
+        }
+    }
+    mutex_release(&nftlparts.lock);
+
+    return info;
+}
+
+void nftl_close(struct nftl_info *info)
+{
+    struct nftl_part *part = (struct nftl_part *)info;
+
+    part->ref--;
+}
+
+static int nftl_part_block_isbad(struct nftl_info *info, u32 page)
+{
+    struct nftl_part *part = (struct nftl_part *)info;
+    u32 start = part->offset / info->write_size;
+
+    return part->parent->block_isbad(part->parent, page + start);
+}
+
+static ssize_t nftl_part_erase(struct nftl_info *info, off_t offset,
+                               ssize_t len)
+{
+    struct nftl_part *part = (struct nftl_part *)info;
+    u64 start = part->offset;
+
+    return part->parent->erase(info, offset + start, len);
+}
+
+static ssize_t nftl_part_read(struct nftl_info *info, void *buf, off_t offset,
+                              ssize_t len)
+{
+    struct nftl_part *part = (struct nftl_part *)info;
+    u64 start = part->offset;
+
+    return part->parent->read(info, buf, offset + start, len);
+}
+
+static ssize_t nftl_part_write(struct nftl_info *info, const void *buf,
+                               off_t offset, ssize_t len)
+{
+    struct nftl_part *part = (struct nftl_part *)info;
+    u64 start = part->offset;
+
+    return part->parent->write(info, buf, offset + start, len);
+}
+
+int nftl_add_part(const char *main_part, const char *sub_part,
+                  const char *sub_label, u64 offset, u64 len)
+{
+    struct nftl_info *parent;
+    struct nftl_part *sub;
+
+    parent = nftl_open(main_part);
+    if (!parent)
+        return ERR_NOT_FOUND;
+
+    if (len > parent->total_size - offset) {
+        nftl_close(parent);
+        return ERR_INVALID_ARGS;
+    }
+
+    sub = malloc(sizeof(struct nftl_part));
+    if (!sub) {
+        nftl_close(parent);
+        return ERR_NO_MEMORY;
+    }
+
+    sub->parent = parent;
+    sub->offset = offset;
+    sub->info.name = strdup(sub_part);
+    if (sub_label)
+        sub->info.label = strdup(sub_label);
+    sub->info.total_size = len;
+    sub->info.erase_size = sub->parent->erase_size;
+    sub->info.write_size = sub->parent->write_size;
+    sub->info.block_isbad = nftl_part_block_isbad;
+    sub->info.erase = nftl_part_erase;
+    sub->info.read = nftl_part_read;
+    sub->info.write = nftl_part_write;
+    sub->info.ioctl = sub->parent->ioctl;
+    nftl_part_register(sub);
+
+    return 0;
+}
+
+struct nftl_info *nftl_add_master(const char *name)
+{
+    struct nftl_part *part;
+
+    part = malloc(sizeof(struct nftl_part));
+    if (!part)
+        return NULL;
+
+    part->parent = NULL;
+    part->offset = 0;
+    part->info.name = strdup(name);
+    part->info.label = strdup(name);
+
+    nftl_part_register(part);
+
+    return &part->info;
+}
+
+int nftl_delete_part(const char *name)
+{
+    struct nftl_info *info;
+    struct nftl_part *part;
+
+    info = nftl_open(name);
+    if (info) {
+        part = (struct nftl_part *)info;
+        nftl_part_unregister(part);
+    }
+
+    return 0;
+}
+
+void nftl_dump_parts(void)
+{
+    printf("nftl parts:\n");
+    struct nftl_part *entry;
+
+    mutex_acquire(&nftlparts.lock);
+    list_for_every_entry(&nftlparts.list, entry, struct nftl_part, node)
+        printf("\t%s, size %lld, offset %lld, ref %d, label %s\n",
+               entry->info.name, entry->info.total_size, entry->offset,
+               entry->ref, entry->info.label);
+    mutex_release(&nftlparts.lock);
+}
diff --git a/src/bsp/lk/lib/nftl/rules.mk b/src/bsp/lk/lib/nftl/rules.mk
new file mode 100644
index 0000000..763b5f4
--- /dev/null
+++ b/src/bsp/lk/lib/nftl/rules.mk
@@ -0,0 +1,10 @@
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+MODULE := $(LOCAL_DIR)
+
+MODULE_SRCS += \
+    $(LOCAL_DIR)/nftl_core.c \
+    $(LOCAL_DIR)/nftl_part.c \
+    $(LOCAL_DIR)/nftl_bdev.c \
+
+include make/module.mk