1#!/usr/bin/env python3 2# coding=utf-8 3 4import os 5from scripts.scons_utils import scons_usr_bool_option 6from scripts.scons_utils import scons_get_cfg_val 7from scripts.scons_utils import scons_usr_int_option 8from scripts.scons_utils import cleanup 9from scripts import scons_env_cfg 10 11Import('env') 12Import('env_cfg') 13Import('module') 14 15if scons_usr_bool_option('CONFIG_TARGET_CHIP_HI3861') == 'y': 16 CONFIG_TARGET_BIN_NAME="Hi3861" 17else: 18 CONFIG_TARGET_BIN_NAME="Hi3861L" 19 20obj_path = env_cfg.obj_path 21bin_path = env_cfg.bin_path 22lib_path = env_cfg.lib_path 23cache_path = env_cfg.cache_path 24 25sign_tool = os.path.join("tools", "sign_tool", "sign_tool") 26link_path = os.path.join(env_cfg.root, "build", "link", "loaderboot_sha256.lds") 27encrypt_key_path = os.path.join('tools', 'sign_tool', 'aes_key.txt') 28if scons_usr_bool_option('CONFIG_TARGET_SIG_SHA256') == 'y': 29 link_path = os.path.join(env_cfg.root, "build", "link", "loaderboot_sha256.lds") 30if scons_usr_bool_option('CONFIG_TARGET_SIG_RSA_V15') == 'y': 31 link_path = os.path.join(env_cfg.root, "build", "link", "loaderboot_rsa.lds") 32 root_key_path = os.path.join('tools', 'sign_tool', 'root_rsa.pem') 33 sub_key_path = os.path.join('tools', 'sign_tool', 'sub_rsa.pem') 34 sign_type = 0 35if scons_usr_bool_option('CONFIG_TARGET_SIG_RSA_PSS') == 'y': 36 link_path = os.path.join(env_cfg.root, "build", "link", "loaderboot_rsa.lds") 37 root_key_path = os.path.join('tools', 'sign_tool', 'root_rsa.pem') 38 sub_key_path = os.path.join('tools', 'sign_tool', 'sub_rsa.pem') 39 sign_type = 1 40if scons_usr_bool_option('CONFIG_TARGET_SIG_ECC') == 'y': 41 link_path = os.path.join(env_cfg.root, "build", "link", "loaderboot_ecc.lds") 42 root_key_path = os.path.join('tools', 'sign_tool', 'root_ecc.pem') 43 sub_key_path = os.path.join('tools', 'sign_tool', 'sub_ecc.pem') 44 sign_type = 2 45 46#boot_ver 47boot_ver = scons_usr_int_option('CONFIG_TARGET_BOOT_VER') 48if (boot_ver < 0 or boot_ver > 16): 49 raise scons_utils.SconsBuildError("%s============== boot_ver invalied, should be 0-16 =============%s"%(scons_utils.colors['red'], scons_utils.colors['end'])) 50 51module_path = env_cfg.get_module_dir(module) 52module_libs = env_cfg.get_module_libs(module) 53 54env['LIBS'] = list(map(lambda x:'-l%s'%x, env_cfg.get_boot_libs())) 55env['LIBPATH'] = env_cfg.get_bootlib_path() 56env.Append(LIBPATH=os.path.join('-Lbuild', 'libs', 'boot_libs')) 57env = env.Clone() 58ld_flags = ["-nostdlib", "-nostartfiles", "-static", "--gc-sections"] 59boot_cflags = ["-mabi=ilp32", "-march=rv32imc", "-freorder-blocks-algorithm=simple", "-fno-schedule-insns", 60 "-nostdinc", "-fno-aggressive-loop-optimizations", "-fno-builtin", "-fstack-protector-strong", 61 "-fno-exceptions", "-fno-short-enums", "-mtune=size", "-msmall-data-limit=0", "-Wall", "-Werror", "-Os", 62 "-std=c99", "-falign-functions=2", "-fdata-sections", "-ffunction-sections", "-fno-common"] 63boot_asflags = ["-mabi=ilp32", "-march=rv32imc", "-x", "assembler-with-cpp", "-Os", "-Wall", "-Werror", 64 "-nostdinc", "-fno-common"] 65defines = ["-DARCH_RISCV", "-DLOS_COMPILE_LDM", "-DHI_BOARD_ASIC"] 66incs = [os.path.join('#', 'boot', 'loaderboot', 'include'), os.path.join('#', 'boot', 'loaderboot', 'fixed', 'include'), os.path.join('#', 'boot', 'loaderboot', 'secure'), os.path.join('#', 'boot', 'commonboot')] 67 68if scons_usr_bool_option('CONFIG_COMPRESSION_OTA_SUPPORT') == 'y': 69 defines.append("-DCONFIG_COMPRESSION_OTA_SUPPORT") 70if scons_usr_bool_option('CONFIG_DUAL_PARTITION_OTA_SUPPORT') == 'y': 71 defines.append("-DCONFIG_DUAL_PARTITION_OTA_SUPPORT") 72if scons_usr_bool_option('CONFIG_TARGET_SIG_ECC') == 'y': 73 defines.append("-DCONFIG_TARGET_SIG_ECC") 74if scons_usr_bool_option('CONFIG_TARGET_SIG_RSA_V15') == 'y': 75 defines.append("-DCONFIG_TARGET_SIG_RSA_V15") 76if scons_usr_bool_option('CONFIG_TARGET_SIG_RSA_PSS') == 'y': 77 defines.append("-DCONFIG_TARGET_SIG_RSA_PSS") 78if scons_usr_bool_option('CONFIG_FLASH_ENCRYPT_SUPPORT') == 'y': 79 defines.append("-DCONFIG_FLASH_ENCRYPT_SUPPORT") 80 81if scons_get_cfg_val('CONFIG_CHIP_PKT_48K') == 'y': 82 defines.append("-DCONFIG_CHIP_PKT_48K") 83else: 84 defines.append("-DCONFIG_CHIP_PKT_32K") 85 86env.Replace(CPPDEFINES=defines) 87env.Replace(CCFLAGS=boot_cflags) 88env.Replace(ASFLAGS=boot_asflags) 89env.Replace(CPPPATH=incs) 90env.Replace(LINKFLAGS=ld_flags) 91 92src_path = module_libs['loaderboot'] 93 94objs = [] 95for src in src_path: 96 path = src if '..' not in src else os.path.relpath(os.path.realpath(os.path.abspath(src)), os.path.join(env_cfg.root)) 97 objs += env.SConscript(os.path.join(src, 'SConscript'), {'env':env,}, variant_dir = os.path.join('#', obj_path, "loaderboot", path), duplicate=0) 98 99boot_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)) 100boot_bin = env.Command(target=os.path.join("#", cache_path, "hi_loader_boot.bin"), source=boot_elf, 101 action="$OBJCOPY -Obinary -R .rom.text -R .rom.code.text -R .u_boot_cmd -R .rom.data -R .rom.code.data -R .rom.bss \ 102 -R .rom.code.bss -S $SOURCE $TARGET") 103if scons_usr_bool_option('CONFIG_TARGET_SIG_SHA256') == 'y': 104 loader_signed_bin = env.Command(target=os.path.join("#", bin_path, "%s_loader_signed.bin"%(CONFIG_TARGET_BIN_NAME)), source=boot_bin, 105 action="%s -i $SOURCE -o $TARGET -n"%(sign_tool)) 106else: 107 if scons_usr_bool_option('CONFIG_BOOT_ENCRYPT') == 'y': 108 loader_signed_bin = env.Command(target=os.path.join("#", bin_path, "%s_loader_signed.bin"%(CONFIG_TARGET_BIN_NAME)), source=boot_bin, 109 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)) 110 else: 111 loader_signed_bin = env.Command(target=os.path.join("#", bin_path, "%s_loader_signed.bin"%(CONFIG_TARGET_BIN_NAME)), source=boot_bin, 112 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)) 113 114env.AddPostAction(boot_bin, cleanup) 115 116Return('loader_signed_bin') 117 118