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", "loaderboot_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", "loaderboot_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", "loaderboot_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", "loaderboot_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", "loaderboot_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', 'loaderboot', 'include'), os.path.join('#', 'boot', 'loaderboot', 'fixed', 'include'), os.path.join('#', 'boot', 'loaderboot', 'secure'), os.path.join('#', 'boot', 'commonboot')]
671bd4fe43Sopenharmony_ci
681bd4fe43Sopenharmony_ciif scons_usr_bool_option('CONFIG_COMPRESSION_OTA_SUPPORT') == 'y':
691bd4fe43Sopenharmony_ci    defines.append("-DCONFIG_COMPRESSION_OTA_SUPPORT")
701bd4fe43Sopenharmony_ciif scons_usr_bool_option('CONFIG_DUAL_PARTITION_OTA_SUPPORT') == 'y':
711bd4fe43Sopenharmony_ci    defines.append("-DCONFIG_DUAL_PARTITION_OTA_SUPPORT")
721bd4fe43Sopenharmony_ciif scons_usr_bool_option('CONFIG_TARGET_SIG_ECC') == 'y':
731bd4fe43Sopenharmony_ci    defines.append("-DCONFIG_TARGET_SIG_ECC")
741bd4fe43Sopenharmony_ciif scons_usr_bool_option('CONFIG_TARGET_SIG_RSA_V15') == 'y':
751bd4fe43Sopenharmony_ci    defines.append("-DCONFIG_TARGET_SIG_RSA_V15")
761bd4fe43Sopenharmony_ciif scons_usr_bool_option('CONFIG_TARGET_SIG_RSA_PSS') == 'y':
771bd4fe43Sopenharmony_ci    defines.append("-DCONFIG_TARGET_SIG_RSA_PSS")
781bd4fe43Sopenharmony_ciif scons_usr_bool_option('CONFIG_FLASH_ENCRYPT_SUPPORT') == 'y':
791bd4fe43Sopenharmony_ci    defines.append("-DCONFIG_FLASH_ENCRYPT_SUPPORT")
801bd4fe43Sopenharmony_ci
811bd4fe43Sopenharmony_ciif scons_get_cfg_val('CONFIG_CHIP_PKT_48K') == 'y':
821bd4fe43Sopenharmony_ci    defines.append("-DCONFIG_CHIP_PKT_48K")
831bd4fe43Sopenharmony_cielse:
841bd4fe43Sopenharmony_ci    defines.append("-DCONFIG_CHIP_PKT_32K")
851bd4fe43Sopenharmony_ci
861bd4fe43Sopenharmony_cienv.Replace(CPPDEFINES=defines)
871bd4fe43Sopenharmony_cienv.Replace(CCFLAGS=boot_cflags)
881bd4fe43Sopenharmony_cienv.Replace(ASFLAGS=boot_asflags)
891bd4fe43Sopenharmony_cienv.Replace(CPPPATH=incs)
901bd4fe43Sopenharmony_cienv.Replace(LINKFLAGS=ld_flags)
911bd4fe43Sopenharmony_ci
921bd4fe43Sopenharmony_cisrc_path = module_libs['loaderboot']
931bd4fe43Sopenharmony_ci
941bd4fe43Sopenharmony_ciobjs = []
951bd4fe43Sopenharmony_cifor src in src_path:
961bd4fe43Sopenharmony_ci    path = src if '..' not in src else os.path.relpath(os.path.realpath(os.path.abspath(src)), os.path.join(env_cfg.root))
971bd4fe43Sopenharmony_ci    objs += env.SConscript(os.path.join(src, 'SConscript'), {'env':env,}, variant_dir = os.path.join('#', obj_path, "loaderboot", path), duplicate=0)
981bd4fe43Sopenharmony_ci
991bd4fe43Sopenharmony_ciboot_elf = env.Command(target=os.path.join("#", cache_path, "loader_boot.elf"), source=sorted(objs), action="$LINK $LINKFLAGS -T%s $LIBPATH $SOURCES -o $TARGET --start-group $LIBS --end-group"%(link_path))
1001bd4fe43Sopenharmony_ciboot_bin = env.Command(target=os.path.join("#", cache_path, "hi_loader_boot.bin"), source=boot_elf,
1011bd4fe43Sopenharmony_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 \
1021bd4fe43Sopenharmony_ci        -R .rom.code.bss -S $SOURCE $TARGET")
1031bd4fe43Sopenharmony_ciif scons_usr_bool_option('CONFIG_TARGET_SIG_SHA256') == 'y':
1041bd4fe43Sopenharmony_ci    loader_signed_bin = env.Command(target=os.path.join("#", bin_path, "%s_loader_signed.bin"%(CONFIG_TARGET_BIN_NAME)), source=boot_bin,
1051bd4fe43Sopenharmony_ci        action="%s -i $SOURCE -o $TARGET -n"%(sign_tool))
1061bd4fe43Sopenharmony_cielse:
1071bd4fe43Sopenharmony_ci    if  scons_usr_bool_option('CONFIG_BOOT_ENCRYPT') == 'y':
1081bd4fe43Sopenharmony_ci        loader_signed_bin = env.Command(target=os.path.join("#", bin_path, "%s_loader_signed.bin"%(CONFIG_TARGET_BIN_NAME)), source=boot_bin,
1091bd4fe43Sopenharmony_ci            action="%s -i $SOURCE -o $TARGET -r%s -s%s -v %d -a %d -e%s"%(sign_tool, root_key_path, sub_key_path, boot_ver, sign_type, encrypt_key_path))
1101bd4fe43Sopenharmony_ci    else:
1111bd4fe43Sopenharmony_ci        loader_signed_bin = env.Command(target=os.path.join("#", bin_path, "%s_loader_signed.bin"%(CONFIG_TARGET_BIN_NAME)), source=boot_bin,
1121bd4fe43Sopenharmony_ci            action="%s -i $SOURCE -o $TARGET -r%s -s%s -v %d -a %d"%(sign_tool, root_key_path, sub_key_path, boot_ver, sign_type))
1131bd4fe43Sopenharmony_ci
1141bd4fe43Sopenharmony_cienv.AddPostAction(boot_bin, cleanup)
1151bd4fe43Sopenharmony_ci
1161bd4fe43Sopenharmony_ciReturn('loader_signed_bin')
1171bd4fe43Sopenharmony_ci
118