[Feature]add MT2731_MP2_MR2_SVN388 baseline version
Change-Id: Ief04314834b31e27effab435d3ca8ba33b499059
diff --git a/src/bsp/lk/lib/fit/include/fit.h b/src/bsp/lk/lib/fit/include/fit.h
new file mode 100644
index 0000000..39b307a
--- /dev/null
+++ b/src/bsp/lk/lib/fit/include/fit.h
@@ -0,0 +1,110 @@
+/*
+ * Copyright (c) 2016 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.
+ */
+#pragma once
+
+#include <stdbool.h>
+#include <sys/types.h>
+
+/**
+ * fit_get_image() - load fit image from a partition
+ *
+ * the function will use bio to access a partition from a storage and
+ * check fdt header. If pass, allocate memory buffer to read the fit image
+ * to load_buf
+ *
+ * @label: partition name
+ * @load_buf: pointer to buffer pointer, the address of allocated memory
+ * buffer with fit image loaded was passing back to the caller
+ * via this argument.
+ *
+ * returns:
+ * 0, on success
+ * otherwise, on failure
+ *
+ */
+int fit_get_image(const char *label, void **load_buf);
+
+/**
+ * fit_image_get_data() - get data property of a subimage node
+ * @fit: fit image start address
+ * @noffset: the offset to the subimage node
+ * @data: return data pointer
+ * @size: return data size
+ *
+ * returns:
+ * 0, on success
+ * otherwise, on failure
+ */
+int fit_image_get_data(const void *fit, int noffset,
+ const void **data, uint32_t *size);
+/**
+ * fit_conf_verify_sig() - verify fit configuration signature
+ *
+ * @conf: configuration name
+ * @fit: fit image start address
+ *
+ * returns:
+ * 0, on success
+ * otherwise, on failure
+ */
+int fit_conf_verify_sig(const char *conf, void *fit);
+
+/**
+ * fit_load_image() - load fit image to proper address
+ *
+ * This checks FIT configuration to find sub-image nodes image
+ * and load the image to right address
+ *
+ * @conf: configuration name
+ * @img_pro: image property name
+ * @fit: fit image start address
+ * @load: returned load address
+ * @load_size: returned loaded raw image size
+ * @entry: returned entry address
+ * @need_verified: whether to check image integrity
+ *
+ * returns:
+ * 0, on success
+ * otherwise, on failure
+ *
+ */
+int fit_load_image(const char *conf, const char *img_pro, void *fit,
+ addr_t *load, size_t *load_size, paddr_t *entry,
+ bool need_verified);
+
+/**
+ * fit_load_loadable_image() - load "loadable" images to "load" address
+ *
+ * This function finds "sub_node_name" loadable image nodes, do integrity check
+ * per hash_check_enabled(), and load images to "load" address.
+ *
+ * @fit: fit image start address
+ * @sub_node_name: loadable image subnode name
+ * @load: returned loadable image load address
+ *
+ * return:
+ * 0: success
+ * otherwise: failure error code
+ *
+ */
+int fit_load_loadable_image(void *fit, const char *sub_node_name, addr_t *load);
diff --git a/src/bsp/lk/lib/fit/include/image.h b/src/bsp/lk/lib/fit/include/image.h
new file mode 100644
index 0000000..ff5d14c
--- /dev/null
+++ b/src/bsp/lk/lib/fit/include/image.h
@@ -0,0 +1,92 @@
+#ifndef _IMAGE_H_
+#define _IMAGE_H_
+
+#include <libfdt.h>
+#include <sys/types.h>
+
+#define SPEW_D 0
+#define FIT_MAX_HASH_LEN 32
+
+#ifndef CHUNKSZ_SHA1
+#define CHUNKSZ_SHA1 (64 * 1024)
+#endif
+
+#define IMAGE_ENABLE_TIMESTAMP 0
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+
+#define FIT_IMAGES_PATH "/images"
+#define FIT_CONFIGS_PATH "/configurations"
+
+/* hash/signature node */
+#define FDT_HASH_NODE "hash"
+#define FDT_ALGO_NODE "algo"
+#define FDT_VAL_NODE "value"
+#define FDT_SIG_NODE "signature"
+#define FDT_HASHED_NODE "hashed-nodes"
+#define FDT_HASHED_STR "hashed-strings"
+
+/*blob node */
+#define BLOB_REQ_NODE "required"
+#define BLOB_NBITS_NODE "rsa,num-bits"
+#define BLOB_N0INV_NODE "rsa,n0-inverse"
+#define BLOB_RSQU_NODE "rsa,r-squared"
+#define BLOB_MOD_NODE "rsa,modulus"
+#define BLOB_EXP_NODE "rsa,exponent"
+
+/* image node */
+#define FDT_DATA_NODE "data"
+
+struct fdt_region {
+ int offset;
+ int size;
+};
+
+struct image_region {
+ const void *data;
+ int size;
+};
+
+struct sig_info {
+ void *fit_image;
+ const void *pubkey;
+ struct sig_algo *algo;
+ int req_offset;
+};
+
+struct verify_data {
+ const void *fit_image;
+ int noffset;
+};
+
+struct hash_algo {
+ const char *hash;
+ const int hash_len;
+ const int pad_len;
+ int (*hash_cal)(const struct image_region region[],int region_count, uint8_t *checksum, int hash_len);
+ const uint8_t *hash_padding;
+};
+
+struct sig_algo {
+ const char *rsa;
+ struct hash_algo *hash_info;
+ int (*sig_verify)(struct sig_info *info,
+ const struct fdt_region region[],
+ int region_count, uint8_t *sig, uint sig_len);
+
+};
+
+static inline const char *fit_get_name(const void *fit_hdr,
+ int noffset, int *len)
+{
+ return fdt_get_name(fit_hdr, noffset, len);
+}
+
+struct sig_algo *image_get_sig_algo(const char *name);
+
+int fit_image_integrity_verify(const void *fit, int image_noffset);
+
+int rsa_check_enabled(void);
+int hash_check_enabled(void);
+int fit_verify_sign(const void *fit, int conf_noffset);
+
+#endif
diff --git a/src/bsp/lk/lib/fit/include/rsa.h b/src/bsp/lk/lib/fit/include/rsa.h
new file mode 100644
index 0000000..4247b53
--- /dev/null
+++ b/src/bsp/lk/lib/fit/include/rsa.h
@@ -0,0 +1,33 @@
+#ifndef _RSA_H
+#define _RSA_H
+
+#include <errno.h>
+#include "image.h"
+
+#define RSA2048_BYTES (2048 / 8)
+#define RSA4096_BYTES (4096 / 8)
+
+/* This is the minimum/maximum key size we support, in bits */
+#define RSA_MIN_KEY_BITS 2048
+#define RSA_MAX_KEY_BITS 4096
+
+/* This is the maximum signature length that we support, in bits */
+#define RSA_MAX_SIG_BITS 4096
+
+struct key_prop {
+
+ const void *rr;
+ const void *modulus;
+ const void *public_exponent;
+ uint64_t n0inv;
+ int num_bits;
+ uint32_t exp_len;
+};
+
+int rsa_verify(struct sig_info *info,
+ const struct fdt_region region[], int region_count,
+ uint8_t *sig, uint sig_len);
+void get_pubkey_info(struct key_prop *pubkey, const void *blob,int node,
+ int *len);
+
+#endif