[Feature]add MT2731_MP2_MR2_SVN388 baseline version
Change-Id: Ief04314834b31e27effab435d3ca8ba33b499059
diff --git a/src/bsp/lk/lib/fastboot/dl_commands.c b/src/bsp/lk/lib/fastboot/dl_commands.c
new file mode 100644
index 0000000..4a9d10f
--- /dev/null
+++ b/src/bsp/lk/lib/fastboot/dl_commands.c
@@ -0,0 +1,605 @@
+/*
+ * 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.
+ */
+
+#include <app.h>
+#include <arch/ops.h>
+#include <ctype.h>
+#include <debug.h>
+#include <dev/timer/arm_generic.h>
+#include <dev/udc.h>
+#include <errno.h>
+#include <fit.h>
+#include <kernel/event.h>
+#include <kernel/thread.h>
+#include <lib/bio.h>
+#include <lib/dl_commands.h>
+#include <lib/fastboot.h>
+#include <lib/mempool.h>
+#include <lib/partition.h>
+#include <lib/sparse_format.h>
+#include <libfdt.h>
+#include <platform.h>
+#include <platform/mmc_rpmb.h>
+#include <platform/mt_reg_base.h>
+#include <platform/mtk_wdt.h>
+#include <stdlib.h>
+#include <string.h>
+#include <target.h>
+#include <trace.h>
+
+#define LOCAL_TRACE 0
+
+#define MODULE_NAME "FASTBOOT_DOWNLOAD"
+
+extern void *download_base;
+extern unsigned download_max;
+extern unsigned download_size;
+extern unsigned fastboot_state;
+
+/*LXO: !Download related command*/
+
+#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
+#define INVALID_PTN -1
+
+lk_bigtime_t start_time_ms;
+#define TIME_STAMP current_time_hires()
+#define TIME_START {start_time_ms = current_time_hires();}
+#define TIME_ELAPSE (current_time_hires() - start_time_ms)
+
+extern int usb_write(void *buf, unsigned len);
+extern int usb_read(void *buf, unsigned len);
+
+static int create_download_membdev(const char *arg, unsigned sz);
+
+extern struct fastboot_var *varlist;
+void cmd_getvar(const char *arg, void *data, unsigned sz)
+{
+ struct fastboot_var *var;
+ char response[MAX_RSP_SIZE];
+
+ if (!strcmp(arg, "all")) {
+ for (var = varlist; var; var = var->next) {
+ snprintf(response, MAX_RSP_SIZE,"\t%s: %s", var->name, var->value);
+ fastboot_info(response);
+ }
+ fastboot_okay("Done!!");
+ return;
+ }
+ for (var = varlist; var; var = var->next) {
+ if (!strcmp(var->name, arg)) {
+ fastboot_okay(var->value);
+ return;
+ }
+ }
+ fastboot_okay("");
+}
+
+void cmd_reboot(const char *arg, void *data, unsigned sz)
+{
+ dprintf(ALWAYS, "rebooting the device\n");
+ fastboot_okay("");
+ set_clr_fastboot_mode(false);
+ mtk_arch_reset(1); /* bypass pwr key when reboot */
+}
+
+void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
+{
+ dprintf(ALWAYS, "rebooting the device to bootloader\n");
+ fastboot_okay("");
+ set_clr_fastboot_mode(true);
+ mtk_arch_reset(1); /* bypass pwr key when reboot */
+}
+
+__WEAK void set_clr_fastbootd_mode(bool flag)
+{
+ dprintf(ALWAYS, "not support rebooting the device to fastbootd\n");
+}
+
+void cmd_reboot_fastbootd(const char *arg, void *data, unsigned sz)
+{
+ dprintf(ALWAYS, "rebooting the device to fastbootd\n");
+ fastboot_okay("");
+ set_clr_fastboot_mode(false);
+ set_clr_recovery_mode(false);
+ set_clr_fastbootd_mode(true);
+ mtk_arch_reset(1); /* bypass pwr key when reboot */
+}
+
+void cmd_reboot_recovery(const char *arg, void *data, unsigned sz)
+{
+ dprintf(ALWAYS, "rebooting the device to recovery\n");
+ fastboot_okay("");
+ set_clr_recovery_mode(true);
+ mtk_arch_reset(1); /* bypass pwr key when reboot */
+}
+
+static void fastboot_fail_wrapper(const char *msg)
+{
+ fastboot_fail(msg);
+}
+
+static void fastboot_ok_wrapper(const char *msg, unsigned data_size)
+{
+ fastboot_okay("");
+}
+
+void cmd_download(const char *arg, void *data, unsigned sz)
+{
+ char *response;
+ char *endptr;
+ unsigned len = strtoul(arg, &endptr, 16);
+ int r;
+
+ download_size = 0;
+ if (len > download_max) {
+ fastboot_fail_wrapper("data is too large");
+ return;
+ }
+ response = (char *)memalign(CACHE_LINE, MAX_RSP_SIZE);
+ if (!response) {
+ fastboot_fail_wrapper("buffer allocation fail");
+ return;
+ }
+
+ snprintf(response, MAX_RSP_SIZE, "DATA%08x", len);
+ r = usb_write(response, strlen(response));
+ free(response);
+ if (r < 0) {
+ return;
+ }
+
+
+ TIME_START;
+ r = usb_read(download_base, len);
+ if ((r < 0) || ((unsigned) r != len)) {
+ fastboot_fail_wrapper("Read USB error");
+ fastboot_state = STATE_ERROR;
+ return;
+ }
+ download_size = len;
+
+ fastboot_ok_wrapper("USB Transmission OK", len);
+}
+
+void cmd_flash_img(const char *arg, void *data, unsigned sz)
+{
+ bdev_t *bdev;
+ int ret = 0;
+ off_t round_size;
+
+ if (!strncmp(arg, "download:", strlen("download:"))) {
+ if (create_download_membdev(arg, sz)) {
+ fastboot_fail_wrapper("create memdev fail.");
+ return;
+ }
+ }
+
+ bdev = bio_open_by_label(arg) ? : bio_open(arg);
+ if (!bdev) {
+ fastboot_fail_wrapper("Partition is not exist.");
+ return;
+ }
+
+ round_size = (off_t)ROUND_TO_PAGE(sz, bdev->block_size - 1);
+ LTRACEF("src size is %lld, dst size is %lld\n",
+ round_size, bdev->total_size);
+ if (bdev->total_size && (round_size > bdev->total_size)) {
+ fastboot_fail_wrapper("Image size is too large.");
+ goto closebdev;
+ }
+
+ ret = partition_update(bdev->name, 0x0, data, sz);
+ if (ret < 0) {
+ fastboot_fail_wrapper("Flash write failure.");
+ goto closebdev;
+ }
+
+ ret = partition_publish(bdev->name, 0x0);
+ if (ret > 0)
+ bio_dump_devices();
+
+ fastboot_okay("");
+
+closebdev:
+ bio_close(bdev);
+
+ return;
+}
+
+void cmd_flash_sparse_img(const char *arg, void *data, unsigned sz)
+{
+ bdev_t *bdev;
+ size_t ret = 0;
+ unsigned int chunk;
+ unsigned int chunk_data_sz;
+ uint32_t *fill_buf = NULL;
+ uint32_t fill_val, pre_fill_val, erase_val;
+ uint32_t chunk_blk_cnt = 0;
+ uint32_t i;
+ sparse_header_t *sparse_header;
+ chunk_header_t *chunk_header;
+ uint32_t total_blocks = 0;
+ unsigned long long size = 0;
+
+ bdev = bio_open_by_label(arg);
+ if (!bdev) {
+ fastboot_fail_wrapper("Partition is not exist.");
+ return;
+ }
+
+ size = bdev->total_size;
+ if (!size) {
+ fastboot_fail_wrapper("Size is uncorrect.");
+ goto closebdev;
+ }
+
+ /* Read and skip over sparse image header */
+ sparse_header = (sparse_header_t *) data;
+ dprintf(ALWAYS, "Image size span 0x%llx, partition size 0x%llx\n", (unsigned long long)sparse_header->total_blks*sparse_header->blk_sz, size);
+ if ((unsigned long long)sparse_header->total_blks*sparse_header->blk_sz > size) {
+ fastboot_fail("sparse image size span overflow.");
+ goto closebdev;
+ }
+
+ data += sparse_header->file_hdr_sz;
+ if (sparse_header->file_hdr_sz > sizeof(sparse_header_t)) {
+ /* Skip the remaining bytes in a header that is longer than
+ * we expected.
+ */
+ data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
+ }
+
+ /* sparse_header->blk_sz is used as denominator later, ensure it's not 0 */
+ if (sparse_header->blk_sz == 0) {
+ fastboot_fail("wrong blk_sz in sparse header.");
+ goto closebdev;
+ }
+
+ LTRACEF("=== Sparse Image Header ===\n");
+ LTRACEF("magic: 0x%x\n", sparse_header->magic);
+ LTRACEF("major_version: 0x%x\n", sparse_header->major_version);
+ LTRACEF("minor_version: 0x%x\n", sparse_header->minor_version);
+ LTRACEF("file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
+ LTRACEF("chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
+ LTRACEF("blk_sz: %d\n", sparse_header->blk_sz);
+ LTRACEF("total_blks: %d\n", sparse_header->total_blks);
+ LTRACEF("total_chunks: %d\n", sparse_header->total_chunks);
+
+ fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
+ if (!fill_buf) {
+ fastboot_fail_wrapper("Malloc failed for: CHUNK_TYPE_FILL");
+ goto closebdev;
+ }
+ erase_val = (int)bdev->erase_byte|bdev->erase_byte<<8|bdev->erase_byte<<16|bdev->erase_byte<<24;
+ pre_fill_val = erase_val;
+ LTRACEF("Init previous fill value to 0x%08x\n", pre_fill_val);
+
+ /* Start processing chunks */
+ for (chunk=0; chunk<sparse_header->total_chunks; chunk++) {
+ /* Read and skip over chunk header */
+ chunk_header = (chunk_header_t *)data;
+ data += sizeof(chunk_header_t);
+
+ LTRACEF("=== Chunk Header ===\n");
+ LTRACEF("chunk_type: 0x%x\n", chunk_header->chunk_type);
+ LTRACEF("chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
+ LTRACEF("total_size: 0x%x\n", chunk_header->total_sz);
+
+ if (sparse_header->chunk_hdr_sz > sizeof(chunk_header_t)) {
+ /* Skip the remaining bytes in a header that is longer than
+ * we expected.
+ */
+ data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
+ }
+
+ chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
+ switch (chunk_header->chunk_type) {
+ case CHUNK_TYPE_RAW:
+ if (chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
+ chunk_data_sz)) {
+ fastboot_fail("Bogus chunk size for chunk type Raw");
+ goto error;
+ }
+
+ LTRACEF("Raw: start block addr: 0x%x\n", total_blocks);
+
+ LTRACEF("addr 0x%llx, partsz 0x%x\n", ((unsigned long long)total_blocks*sparse_header->blk_sz) , chunk_data_sz);
+ ret = (size_t)bio_write(bdev, data, ((unsigned long long)total_blocks*sparse_header->blk_sz), chunk_data_sz);
+ if (ret != chunk_data_sz) {
+ fastboot_fail_wrapper("flash write failure");
+ goto error;
+ }
+
+ total_blocks += chunk_header->chunk_sz;
+ data += chunk_data_sz;
+ break;
+
+ case CHUNK_TYPE_DONT_CARE:
+ LTRACEF("!!Blank: start: 0x%x offset: 0x%x\n",
+ total_blocks, chunk_header->chunk_sz);
+ total_blocks += chunk_header->chunk_sz;
+ break;
+
+ case CHUNK_TYPE_FILL:
+ LTRACEF("CHUNK_TYPE_FILL=0x%x size=%d chunk_data_sz=%d\n",
+ *(uint32_t *)data,
+ ROUNDUP(sparse_header->blk_sz, CACHE_LINE),
+ chunk_data_sz);
+ if (chunk_header->total_sz != (sparse_header->chunk_hdr_sz + sizeof(uint32_t))) {
+ fastboot_fail_wrapper("Bogus chunk size for chunk type FILL");
+ goto error;
+ }
+
+ fill_val = *(uint32_t *)data;
+ data = (char *) data + sizeof(uint32_t);
+ chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
+
+ if (fill_val != pre_fill_val) {
+ pre_fill_val = fill_val;
+ for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
+ fill_buf[i] = fill_val;
+ }
+
+ for (i = 0; i < chunk_blk_cnt; i++) {
+ if (fill_val == erase_val) {
+ /* To assume partition already erased */
+ /* skip fill value as same as erase_byte */
+ LTRACEF("skip CHUNK_TYPE_FILL with value '%x'\n", fill_val);
+ } else {
+ ret = (size_t)bio_write(bdev, fill_buf, ((uint64_t)total_blocks*sparse_header->blk_sz), sparse_header->blk_sz);
+ if (ret != sparse_header->blk_sz) {
+ fastboot_fail_wrapper("CHUNK_TYPE_FILL flash write failure");
+ goto error;
+ }
+ }
+ total_blocks++;
+ }
+ break;
+
+ case CHUNK_TYPE_CRC:
+ if (chunk_header->total_sz != sparse_header->chunk_hdr_sz) {
+ fastboot_fail_wrapper("Bogus chunk size for chunk type Dont Care");
+ goto error;
+ }
+ total_blocks += chunk_header->chunk_sz;
+ data += chunk_data_sz;
+ break;
+
+ default:
+ fastboot_fail_wrapper("Unknown chunk type");
+ goto error;
+ }
+ }
+
+ dprintf(ALWAYS, "Wrote %d blocks, expected to write %d blocks\n",
+ total_blocks, sparse_header->total_blks);
+
+ if (total_blocks != sparse_header->total_blks) {
+ fastboot_fail_wrapper("sparse image write failure");
+ } else {
+ fastboot_okay("");
+ }
+
+error:
+ free(fill_buf);
+closebdev:
+ bio_close(bdev);
+
+ return;
+}
+
+void cmd_flash(const char *arg, void *data, unsigned sz)
+{
+ sparse_header_t *sparse_header;
+
+ dprintf(ALWAYS, "cmd_flash: %s, %d\n", arg, (u32)sz);
+
+ if (sz == 0) {
+ fastboot_okay("");
+ return;
+ }
+
+ TIME_START;
+
+// security check, only unlocked device can flash partitions
+#if !(defined FORCE_DISABLE_FLASH_CHECK)
+ int ret=-1;
+ unsigned char blk[256]={0};
+
+ ret=mmc_rpmb_block_read(INDEX_RPMB_UNLOCK,&blk[0]);
+ if(ret != 0){
+ dprintf(CRITICAL, "mmc_rpmb_block_read fail %d.\n", ret);
+ return;
+ }
+
+ if(blk[0] != AVB_DEVICE_UNLOCK){
+ dprintf(ALWAYS, "Device not unlocked, cmd_flash refused \n");
+ return;
+ }
+#endif
+
+ sparse_header = (sparse_header_t *) data;
+ if (sparse_header->magic != SPARSE_HEADER_MAGIC)
+ cmd_flash_img(arg, data, sz);
+ else {
+ cmd_flash_sparse_img(arg, data, sz);
+ }
+
+ return;
+}
+
+void cmd_erase(const char *arg, void *data, unsigned sz)
+{
+#define MAX_ERASE_SIZE (INT_MAX & ~(bdev->block_size - 1))
+ bdev_t *bdev;
+ ssize_t ret = 0, erase_len = 0;
+ char response[MAX_RSP_SIZE];
+
+ dprintf(ALWAYS, "cmd_erase\n");
+ bdev = bio_open_by_label(arg);
+ if (!bdev) {
+ bdev = bio_open(arg);
+ if (!bdev) {
+ fastboot_fail_wrapper("Partition is not exist.");
+ return;
+ }
+ }
+
+ if (!bdev->total_size) {
+ fastboot_fail_wrapper("Size is uncorrect.");
+ goto closebdev;
+ }
+
+ LTRACEF("%s %s %lld %d\n",
+ bdev->name, bdev->label, bdev->total_size, bdev->block_count);
+ if (bdev->total_size > (off_t)MAX_ERASE_SIZE) {
+ off_t offset = 0LL, size = bdev->total_size;
+ size_t len;
+ do {
+ len = (size_t)MIN(size, (off_t)MAX_ERASE_SIZE);
+ ret = bio_erase(bdev, offset, len);
+ if (ret < 0) {
+ fastboot_fail_wrapper("Erase failure.");
+ goto closebdev;
+ }
+ erase_len += ret;
+ size -= len;
+ offset += len;
+ } while (size);
+ } else {
+ ret = bio_erase(bdev, 0, bdev->total_size);
+ if (ret < 0) {
+ fastboot_fail_wrapper("Erase failure.");
+ goto closebdev;
+ }
+ erase_len = ret;
+ }
+
+ ret = partition_unpublish(bdev->name);
+ if (ret > 0)
+ bio_dump_devices();
+
+ snprintf(response, MAX_RSP_SIZE, "request sz: 0x%llx, real erase len: 0x%lx",
+ bdev->total_size, erase_len);
+ fastboot_info(response);
+
+ fastboot_okay("");
+
+closebdev:
+ bio_close(bdev);
+
+ return;
+}
+
+//to support AVB2.0 cmd: fastboot flashing [lock | unlock]
+#ifdef AVB_ENABLE_DEVICE_STATE_CHANGE
+int cmd_flash_lock_state(const char *arg)
+{
+ unsigned char blk[256]={0};
+ int ret=-1;
+
+ LTRACEF("start to flash device state [%s] \n",arg);
+
+ if (!strcmp(arg, " lock")) {
+ blk[0]=0;
+ LTRACEF("start to flash lock state \n");
+ ret=mmc_rpmb_block_write(INDEX_RPMB_UNLOCK,&blk[0]);
+ LTRACEF("end to flash lock state %d \n",ret);
+ }
+ else if (!strcmp(arg, " unlock")){
+ blk[0]=AVB_DEVICE_UNLOCK;
+ ret=mmc_rpmb_block_write(INDEX_RPMB_UNLOCK,&blk[0]);
+ LTRACEF("end to flash unlock state %d \n",ret);
+ }
+ else{
+ dprintf(ALWAYS, "cmd %s not supported \n",arg);
+ }
+
+ dprintf(ALWAYS, "cmd %s finish %d \n",arg,ret);
+
+ return ret;
+}
+
+void cmd_flashing(const char *arg, void *data, unsigned sz)
+{
+ char response[MAX_RSP_SIZE];
+ int ret=-1;
+
+ if(arg == NULL){
+ dprintf(ALWAYS, "invalid parameter \n");
+ return ;
+ }
+
+ dprintf(ALWAYS, "cmd_flashing: %s, \n", arg);
+
+ ret=cmd_flash_lock_state(arg);
+
+ snprintf(response, MAX_RSP_SIZE, "flashing state %s", (ret==0)?"sucessful":"fail");
+ fastboot_info(response);
+
+ fastboot_okay("");
+
+ return;
+}
+#endif
+
+extern unsigned fastboot_state;
+void cmd_continue_boot(const char *arg, void *data, unsigned sz)
+{
+ fastboot_okay("");
+ /* set state to leave fastboot command loop and handler */
+ fastboot_state = STATE_RETURN;
+ mtk_wdt_init();
+}
+
+static int create_download_membdev(const char *arg, unsigned sz)
+{
+ size_t membdev_size;
+ void *membdev_buf;
+ int rc;
+
+ /* destroy the membdev if previously created */
+ membdev_buf = destroy_membdev(arg);
+ if (membdev_buf) {
+ mempool_free(membdev_buf);
+ membdev_buf = NULL;
+ }
+
+ /* membdev size should be aligned to 512 bytes */
+ membdev_size = ROUNDUP(sz, 512);
+ membdev_buf = mempool_alloc(membdev_size, MEMPOOL_ANY);
+ if (membdev_buf == NULL) {
+ fastboot_fail_wrapper("size is too large");
+ return -1;
+ }
+
+ rc = create_membdev(arg, membdev_buf, membdev_size);
+ if (rc) {
+ fastboot_fail_wrapper("fail to create membdev");
+ mempool_free(membdev_buf);
+ return -1;
+ }
+ fastboot_okay("");
+
+ return 0;
+}