11bd4fe43Sopenharmony_ci#!/usr/bin/env python3
21bd4fe43Sopenharmony_ci# coding=utf-8
31bd4fe43Sopenharmony_ci
41bd4fe43Sopenharmony_ciimport os
51bd4fe43Sopenharmony_cifrom scripts.scons_utils import scons_usr_bool_option
61bd4fe43Sopenharmony_cifrom scripts.scons_utils import scons_get_cfg_val
71bd4fe43Sopenharmony_cifrom scripts.scons_utils import scons_usr_int_option
81bd4fe43Sopenharmony_cifrom scripts.scons_utils import cleanup
91bd4fe43Sopenharmony_cifrom scripts import scons_env_cfg
101bd4fe43Sopenharmony_ci
111bd4fe43Sopenharmony_ciImport('env')
121bd4fe43Sopenharmony_ciImport('env_cfg')
131bd4fe43Sopenharmony_ciImport('module')
141bd4fe43Sopenharmony_ci
151bd4fe43Sopenharmony_ciif scons_usr_bool_option('CONFIG_TARGET_CHIP_HI3861') == 'y':
161bd4fe43Sopenharmony_ci    CONFIG_TARGET_BIN_NAME="Hi3861"
171bd4fe43Sopenharmony_cielse:
181bd4fe43Sopenharmony_ci    CONFIG_TARGET_BIN_NAME="Hi3861L"
191bd4fe43Sopenharmony_ci
201bd4fe43Sopenharmony_ciobj_path = env_cfg.obj_path
211bd4fe43Sopenharmony_cibin_path = env_cfg.bin_path
221bd4fe43Sopenharmony_cilib_path = env_cfg.lib_path
231bd4fe43Sopenharmony_cicache_path = env_cfg.cache_path
241bd4fe43Sopenharmony_ci
251bd4fe43Sopenharmony_cisign_tool = os.path.join("tools", "sign_tool", "sign_tool")
261bd4fe43Sopenharmony_cilink_path = os.path.join(env_cfg.root, "build", "link", "flashboot_sha256.lds")
271bd4fe43Sopenharmony_ciencrypt_key_path = os.path.join('tools', 'sign_tool', 'aes_key.txt')
281bd4fe43Sopenharmony_ciif scons_usr_bool_option('CONFIG_TARGET_SIG_SHA256') == 'y':
291bd4fe43Sopenharmony_ci    link_path = os.path.join(env_cfg.root, "build", "link", "flashboot_sha256.lds")
301bd4fe43Sopenharmony_ciif scons_usr_bool_option('CONFIG_TARGET_SIG_RSA_V15') == 'y':
311bd4fe43Sopenharmony_ci    link_path = os.path.join(env_cfg.root, "build", "link", "flashboot_rsa.lds")
321bd4fe43Sopenharmony_ci    root_key_path = os.path.join('tools', 'sign_tool', 'root_rsa.pem')
331bd4fe43Sopenharmony_ci    sub_key_path = os.path.join('tools', 'sign_tool', 'sub_rsa.pem')
341bd4fe43Sopenharmony_ci    sign_type = 0
351bd4fe43Sopenharmony_ciif scons_usr_bool_option('CONFIG_TARGET_SIG_RSA_PSS') == 'y':
361bd4fe43Sopenharmony_ci    link_path = os.path.join(env_cfg.root, "build", "link", "flashboot_rsa.lds")
371bd4fe43Sopenharmony_ci    root_key_path = os.path.join('tools', 'sign_tool', 'root_rsa.pem')
381bd4fe43Sopenharmony_ci    sub_key_path = os.path.join('tools', 'sign_tool', 'sub_rsa.pem')
391bd4fe43Sopenharmony_ci    sign_type = 1
401bd4fe43Sopenharmony_ciif scons_usr_bool_option('CONFIG_TARGET_SIG_ECC') == 'y':
411bd4fe43Sopenharmony_ci    link_path = os.path.join(env_cfg.root, "build", "link", "flashboot_ecc.lds")
421bd4fe43Sopenharmony_ci    root_key_path = os.path.join('tools', 'sign_tool', 'root_ecc.pem')
431bd4fe43Sopenharmony_ci    sub_key_path = os.path.join('tools', 'sign_tool', 'sub_ecc.pem')
441bd4fe43Sopenharmony_ci    sign_type = 2
451bd4fe43Sopenharmony_ci
461bd4fe43Sopenharmony_ci#boot_ver
471bd4fe43Sopenharmony_ciboot_ver = scons_usr_int_option('CONFIG_TARGET_BOOT_VER')
481bd4fe43Sopenharmony_ciif (boot_ver < 0 or boot_ver > 16):
491bd4fe43Sopenharmony_ci        raise scons_utils.SconsBuildError("%s============== boot_ver invalied, should be 0-16 =============%s"%(scons_utils.colors['red'], scons_utils.colors['end']))
501bd4fe43Sopenharmony_ci
511bd4fe43Sopenharmony_cimodule_path = env_cfg.get_module_dir(module)
521bd4fe43Sopenharmony_cimodule_libs = env_cfg.get_module_libs(module)
531bd4fe43Sopenharmony_ci
541bd4fe43Sopenharmony_cienv['LIBS'] = list(map(lambda x:'-l%s'%x, env_cfg.get_boot_libs()))
551bd4fe43Sopenharmony_cienv['LIBPATH'] = env_cfg.get_bootlib_path()
561bd4fe43Sopenharmony_cienv.Append(LIBPATH=os.path.join('-Lbuild', 'libs', 'boot_libs'))
571bd4fe43Sopenharmony_cienv = env.Clone()
581bd4fe43Sopenharmony_cild_flags = ["-nostdlib", "-nostartfiles", "-static", "--gc-sections"]
591bd4fe43Sopenharmony_ciboot_cflags = ["-mabi=ilp32", "-march=rv32imc", "-freorder-blocks-algorithm=simple", "-fno-schedule-insns",
601bd4fe43Sopenharmony_ci    "-nostdinc", "-fno-aggressive-loop-optimizations", "-fno-builtin", "-fstack-protector-strong",
611bd4fe43Sopenharmony_ci    "-fno-exceptions", "-fno-short-enums", "-mtune=size", "-msmall-data-limit=0", "-Wall", "-Werror", "-Os",
621bd4fe43Sopenharmony_ci    "-std=c99", "-falign-functions=2", "-fdata-sections", "-ffunction-sections", "-fno-common"]
631bd4fe43Sopenharmony_ciboot_asflags = ["-mabi=ilp32", "-march=rv32imc", "-x", "assembler-with-cpp", "-Os", "-Wall", "-Werror",
641bd4fe43Sopenharmony_ci    "-nostdinc", "-fno-common"]
651bd4fe43Sopenharmony_cidefines = ["-DARCH_RISCV", "-DLOS_COMPILE_LDM", "-DHI_BOARD_ASIC"]
661bd4fe43Sopenharmony_ciincs = [os.path.join('#', 'boot', 'flashboot', 'fixed', 'include'), os.path.join('#', 'boot', 'flashboot', 'upg'),
671bd4fe43Sopenharmony_ci        os.path.join('#', 'boot', 'flashboot', 'include'), os.path.join('#', 'boot', 'flashboot', 'drivers', 'lsadc'), os.path.join('#', 'boot', 'flashboot', 'drivers', 'efuse'),
681bd4fe43Sopenharmony_ci        os.path.join('#', 'boot', 'flashboot', 'include', 'lzma'), os.path.join('#', 'boot', 'flashboot', 'drivers', 'gpio'), os.path.join('#', 'boot', 'flashboot', 'drivers', 'io'),
691bd4fe43Sopenharmony_ci        os.path.join('#', 'third_party', 'u-boot-v2019.07', 'u-boot-v2019.07', 'lib', 'lzma'), os.path.join('#', 'boot', 'flashboot', 'secure'), os.path.join('#', 'boot', 'commonboot')]
701bd4fe43Sopenharmony_ci
711bd4fe43Sopenharmony_ciif scons_usr_bool_option('CONFIG_COMPRESSION_OTA_SUPPORT') == 'y':
721bd4fe43Sopenharmony_ci    defines.append("-DCONFIG_COMPRESSION_OTA_SUPPORT")
731bd4fe43Sopenharmony_ciif scons_usr_bool_option('CONFIG_DUAL_PARTITION_OTA_SUPPORT') == 'y':
741bd4fe43Sopenharmony_ci    defines.append("-DCONFIG_DUAL_PARTITION_OTA_SUPPORT")
751bd4fe43Sopenharmony_ciif scons_usr_bool_option('CONFIG_TARGET_SIG_ECC') == 'y':
761bd4fe43Sopenharmony_ci    defines.append("-DCONFIG_TARGET_SIG_ECC")
771bd4fe43Sopenharmony_ciif scons_usr_bool_option('CONFIG_TARGET_SIG_RSA_V15') == 'y':
781bd4fe43Sopenharmony_ci    defines.append("-DCONFIG_TARGET_SIG_RSA_V15")
791bd4fe43Sopenharmony_ciif scons_usr_bool_option('CONFIG_TARGET_SIG_RSA_PSS') == 'y':
801bd4fe43Sopenharmony_ci    defines.append("-DCONFIG_TARGET_SIG_RSA_PSS")
811bd4fe43Sopenharmony_ciif scons_usr_bool_option('CONFIG_FLASH_ENCRYPT_SUPPORT') == 'y':
821bd4fe43Sopenharmony_ci    defines.append("-DCONFIG_FLASH_ENCRYPT_SUPPORT")
831bd4fe43Sopenharmony_ciif scons_usr_bool_option('CONFIG_FACTORY_TEST_SUPPORT') == 'y':
841bd4fe43Sopenharmony_ci    defines.append("-DCONFIG_FACTORY_TEST_SUPPORT")
851bd4fe43Sopenharmony_ciif scons_usr_bool_option('CONFIG_QUICK_SEND_MODE') == 'y':
861bd4fe43Sopenharmony_ci    defines.append("-DCONFIG_QUICK_SEND_MODE")
871bd4fe43Sopenharmony_ci
881bd4fe43Sopenharmony_ciif scons_get_cfg_val('CONFIG_CHIP_PKT_48K') == 'y':
891bd4fe43Sopenharmony_ci    defines.append("-DCONFIG_CHIP_PKT_48K")
901bd4fe43Sopenharmony_cielse:
911bd4fe43Sopenharmony_ci    defines.append("-DCONFIG_CHIP_PKT_32K")
921bd4fe43Sopenharmony_ci
931bd4fe43Sopenharmony_cienv.Replace(CPPDEFINES=defines)
941bd4fe43Sopenharmony_cienv.Replace(CCFLAGS=boot_cflags)
951bd4fe43Sopenharmony_cienv.Replace(ASFLAGS=boot_asflags)
961bd4fe43Sopenharmony_cienv.Replace(CPPPATH=incs)
971bd4fe43Sopenharmony_cienv.Replace(LINKFLAGS=ld_flags)
981bd4fe43Sopenharmony_ci
991bd4fe43Sopenharmony_cisrc_path = module_libs['boot']
1001bd4fe43Sopenharmony_ci
1011bd4fe43Sopenharmony_ciobjs = []
1021bd4fe43Sopenharmony_cifor src in src_path:
1031bd4fe43Sopenharmony_ci    path = src if '..' not in src else os.path.relpath(os.path.realpath(os.path.abspath(src)), os.path.join(env_cfg.root))
1041bd4fe43Sopenharmony_ci    objs += env.SConscript(os.path.join(src, 'SConscript'), {'env':env,}, variant_dir = os.path.join('#', obj_path, "flashboot", path), duplicate=0)
1051bd4fe43Sopenharmony_ci
1061bd4fe43Sopenharmony_ciboot_elf = env.Command(target=os.path.join("#", cache_path, "flash_boot.elf"), source=sorted(objs), action="$LINK $LINKFLAGS -T%s $LIBPATH $SOURCES -o $TARGET --start-group $LIBS --end-group"%(link_path))
1071bd4fe43Sopenharmony_ciboot_bin = env.Command(target=os.path.join("#", cache_path, "hi_flash_boot.bin"), source=boot_elf,
1081bd4fe43Sopenharmony_ci    action="$OBJCOPY -Obinary -R .rom.text -R .rom.code.text -R .u_boot_cmd  -R .rom.data -R .rom.code.data -R .rom.bss \
1091bd4fe43Sopenharmony_ci        -R .rom.code.bss -S $SOURCE $TARGET")
1101bd4fe43Sopenharmony_ciboot_bin_B = os.path.join(bin_path, "%s_boot_signed_B.bin"%(CONFIG_TARGET_BIN_NAME))
1111bd4fe43Sopenharmony_ciif scons_usr_bool_option('CONFIG_TARGET_SIG_SHA256') == 'y':
1121bd4fe43Sopenharmony_ci    boot_signed_bin = env.Command(target=os.path.join("#", bin_path, "%s_boot_signed.bin"%(CONFIG_TARGET_BIN_NAME)), source=boot_bin,
1131bd4fe43Sopenharmony_ci        action="%s -i $SOURCE -o $TARGET -n -t%s"%(sign_tool, boot_bin_B))
1141bd4fe43Sopenharmony_cielse:
1151bd4fe43Sopenharmony_ci    if  scons_usr_bool_option('CONFIG_BOOT_ENCRYPT') == 'y':
1161bd4fe43Sopenharmony_ci        boot_signed_bin = env.Command(target=os.path.join("#", bin_path, "%s_boot_signed.bin"%(CONFIG_TARGET_BIN_NAME)), source=boot_bin,
1171bd4fe43Sopenharmony_ci            action="%s -i $SOURCE -o $TARGET -r%s -s%s -v %d -a %d -e%s -t%s"%(sign_tool, root_key_path, sub_key_path, boot_ver, sign_type, encrypt_key_path, boot_bin_B))
1181bd4fe43Sopenharmony_ci    else:
1191bd4fe43Sopenharmony_ci        boot_signed_bin = env.Command(target=os.path.join("#", bin_path, "%s_boot_signed.bin"%(CONFIG_TARGET_BIN_NAME)), source=boot_bin,
1201bd4fe43Sopenharmony_ci            action="%s -i $SOURCE -o $TARGET -r%s -s%s -v %d -a %d -t%s"%(sign_tool, root_key_path, sub_key_path, boot_ver, sign_type, boot_bin_B))
1211bd4fe43Sopenharmony_ci
1221bd4fe43Sopenharmony_cienv.AddPostAction(boot_bin, cleanup)
1231bd4fe43Sopenharmony_cienv.AddPostAction(boot_signed_bin, cleanup)
1241bd4fe43Sopenharmony_ci
1251bd4fe43Sopenharmony_ciReturn('boot_signed_bin')
1261bd4fe43Sopenharmony_ci
127