#/* SPDX-License-Identifier: LGPL-2.1-only OR BSD-3-Clause */
#/******************************************************************************
#*
#* This file is provided under a dual license. When you use or
#* distribute this software, you may choose to be licensed under
#* version 2.1 of the GNU Lesser General Public License
#* ("LGPLv2.1 License") or BSD License.
#*
#* LGPLv2.1 License
#*
#* Copyright(C) 2019 MediaTek Inc.
#*
#* This program is free software; you can redistribute it and/or modify
#* it under the terms of version 2.1 of the GNU Lesser General Public
#* License as published by the Free Software Foundation.
#*
#* This program is distributed in the hope that it will be useful, but
#* WITHOUT ANY WARRANTY; without even the implied warranty of
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#* See https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html for more
#* details.
#*
#* BSD LICENSE
#*
#* Copyright(C) 2019 MediaTek Inc.
#* All rights reserved.
#*
#* Redistribution and use in source and binary forms, with or without
#* modification, are permitted provided that the following conditions
#* are met:
#*
#* * Redistributions of source code must retain the above copyright
#* notice, this list of conditions and the following disclaimer.
#* * Redistributions in binary form must reproduce the above copyright
#* notice, this list of conditions and the following disclaimer in
#* the documentation and/or other materials provided with the
#* distribution.
#* * Neither the name of the copyright holder nor the names of its
#* contributors may be used to endorse or promote products derived
#* from this software without specific prior written permission.
#*
#* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
#* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
#* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#*
#*****************************************************************************/
#

# System config file
include ../config.mak

export MODE ?= debug
HDR_TOOL = $(TOOL_ROOT)/binary/open/mt5391_pack/mt5391_pack
LZHS_TOOL = $(TOOL_ROOT)/binary/open/mt5391_pack/lzhsenc
PACKIMG = $(TOOL_ROOT)/binary/open/mt5391_pack/packimg
SCRAMBLE = $(TOOL_ROOT)/binary/open/image-tools/scramble
AES_KEY2 := $(THIS_ROOT)/data/aes_key_set2

ifndef MTK_LOADER_MAX_SIZE
ifeq ($(BOOT_NO_LZHS), true)
MTK_LOADER_MAX_SIZE := 0x100000
else
MTK_LOADER_MAX_SIZE := 0x80000
endif
endif

clean distclean:
	rm -f  $(UBOOT_PACK_ROOT)/uboot.* $(UBOOT_PACK_ROOT)/*.map $(UBOOT_PACK_ROOT)/*preloader*

5365_package:
	@chmod +x $(PACKIMG)
	@chmod +x $(LZHS_TOOL)
	@chmod +x $(HDR_TOOL)
	@rm -f $(UBOOT_PACK_ROOT)/u-boot_pack.bin
	@rm -f $(UBOOT_PACK_ROOT)/u-boot_pack.lzhs
	@cp $(UBOOT_OBJ_ROOT)/u-boot.bin $(UBOOT_PACK_ROOT)/
	@rm -rf $(UBOOT_PACK_ROOT)/.dummy $(UBOOT_PACK_ROOT)/.dummy.hdr
	@touch $(UBOOT_PACK_ROOT)/.dummy
	cd $(UBOOT_PACK_ROOT); $(HDR_TOOL) $(MODEL)_$(MODE)_mtkloader_$(BOOT).bin .dummy .dummy.hdr 2 "$(COMPANY)" "$(MW_MODEL)" "$(VERSION)" $(MTK_LOADER_MAX_SIZE) 2
	@rm -rf $(UBOOT_PACK_ROOT)/.dummy $(UBOOT_PACK_ROOT)/.dummy.bin
	@cp $(UBOOT_PACK_ROOT)/$(MODEL)_$(MODE)_mtkloader_$(BOOT).bin.hdr.bin $(UBOOT_PACK_ROOT)/mtkloader.bin
ifeq ($(BOOT_NO_LZHS), true)
	cd $(UBOOT_PACK_ROOT) ; $(LZHS_TOOL) u-boot.bin u-boot_pack.lzhs 1
else
	cd $(UBOOT_PACK_ROOT) ; $(LZHS_TOOL) u-boot.bin u-boot_pack.lzhs
endif
ifeq "$(SECURE_BOOT_SCRAMBLE)" "true"
	echo "scramble uboot...$(SCRAMBLE) u-boot_pack.lzhs u-boot_pack.lzhs.aes $(AES_KEY2)";
	cd $(UBOOT_PACK_ROOT) ; \
	$(SCRAMBLE) u-boot_pack.lzhs u-boot_pack.lzhs.aes $(AES_KEY2) ; \
	mv u-boot_pack.lzhs u-boot_pack.lzhs.org ; \
	mv u-boot_pack.lzhs.aes u-boot_pack.lzhs
endif

ifeq ($(BOOT_NO_LZHS), true)
	/bin/sed 's/MODEL/$(MODEL)/g' uboot_no_lzhs.sc > $(UBOOT_PACK_ROOT)/temp1.sc;
else
	/bin/sed 's/MODEL/$(MODEL)/g' uboot.sc > $(UBOOT_PACK_ROOT)/temp1.sc;
endif
	/bin/sed 's/BOOT/$(BOOT)/g' $(UBOOT_PACK_ROOT)/temp1.sc | /bin/sed  's/MTK_LOADER_MAX_SIZE/$(MTK_LOADER_MAX_SIZE)/g' - > $(UBOOT_PACK_ROOT)/temp2.sc;
	cd $(UBOOT_PACK_ROOT) ; $(PACKIMG) -f temp2.sc;
	@mv -f $(UBOOT_PACK_ROOT)/$(MODEL)_$(BOOT)boot.bin  $(UBOOT_PACK_ROOT)/..
	cp -f $(UBOOT_OBJ_ROOT)/board/$(TARGET_IC)/env.o $(THIS_ROOT)/data/uenv.bin

cmmrm: cmmrm.c
	gcc -m32 -g -MD -o $(UBOOT_PACK_ROOT)/cmmrm cmmrm.c

make_cmmrm: make_cmmrm.c
	gcc -m32 -g -MD -o $(UBOOT_PACK_ROOT)/make_cmmrm make_cmmrm.c

-include $(wildcard $(UBOOT_PACK_ROOT)/*.d)

