• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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", "flashboot_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", "flashboot_sha256.lds")
30if scons_usr_bool_option('CONFIG_TARGET_SIG_RSA_V15') == 'y':
31    link_path = os.path.join(env_cfg.root, "build", "link", "flashboot_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", "flashboot_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", "flashboot_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', 'flashboot', 'fixed', 'include'), os.path.join('#', 'boot', 'flashboot', 'upg'),
67        os.path.join('#', 'boot', 'flashboot', 'include'), os.path.join('#', 'boot', 'flashboot', 'drivers', 'lsadc'), os.path.join('#', 'boot', 'flashboot', 'drivers', 'efuse'),
68        os.path.join('#', 'boot', 'flashboot', 'include', 'lzma'), os.path.join('#', 'boot', 'flashboot', 'drivers', 'gpio'), os.path.join('#', 'boot', 'flashboot', 'drivers', 'io'),
69        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')]
70
71if scons_usr_bool_option('CONFIG_COMPRESSION_OTA_SUPPORT') == 'y':
72    defines.append("-DCONFIG_COMPRESSION_OTA_SUPPORT")
73if scons_usr_bool_option('CONFIG_DUAL_PARTITION_OTA_SUPPORT') == 'y':
74    defines.append("-DCONFIG_DUAL_PARTITION_OTA_SUPPORT")
75if scons_usr_bool_option('CONFIG_TARGET_SIG_ECC') == 'y':
76    defines.append("-DCONFIG_TARGET_SIG_ECC")
77if scons_usr_bool_option('CONFIG_TARGET_SIG_RSA_V15') == 'y':
78    defines.append("-DCONFIG_TARGET_SIG_RSA_V15")
79if scons_usr_bool_option('CONFIG_TARGET_SIG_RSA_PSS') == 'y':
80    defines.append("-DCONFIG_TARGET_SIG_RSA_PSS")
81if scons_usr_bool_option('CONFIG_FLASH_ENCRYPT_SUPPORT') == 'y':
82    defines.append("-DCONFIG_FLASH_ENCRYPT_SUPPORT")
83if scons_usr_bool_option('CONFIG_FACTORY_TEST_SUPPORT') == 'y':
84    defines.append("-DCONFIG_FACTORY_TEST_SUPPORT")
85if scons_usr_bool_option('CONFIG_QUICK_SEND_MODE') == 'y':
86    defines.append("-DCONFIG_QUICK_SEND_MODE")
87
88if scons_get_cfg_val('CONFIG_CHIP_PKT_48K') == 'y':
89    defines.append("-DCONFIG_CHIP_PKT_48K")
90else:
91    defines.append("-DCONFIG_CHIP_PKT_32K")
92
93env.Replace(CPPDEFINES=defines)
94env.Replace(CCFLAGS=boot_cflags)
95env.Replace(ASFLAGS=boot_asflags)
96env.Replace(CPPPATH=incs)
97env.Replace(LINKFLAGS=ld_flags)
98
99src_path = module_libs['boot']
100
101objs = []
102for src in src_path:
103    path = src if '..' not in src else os.path.relpath(os.path.realpath(os.path.abspath(src)), os.path.join(env_cfg.root))
104    objs += env.SConscript(os.path.join(src, 'SConscript'), {'env':env,}, variant_dir = os.path.join('#', obj_path, "flashboot", path), duplicate=0)
105
106boot_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))
107boot_bin = env.Command(target=os.path.join("#", cache_path, "hi_flash_boot.bin"), source=boot_elf,
108    action="$OBJCOPY -Obinary -R .rom.text -R .rom.code.text -R .u_boot_cmd  -R .rom.data -R .rom.code.data -R .rom.bss \
109        -R .rom.code.bss -S $SOURCE $TARGET")
110boot_bin_B = os.path.join(bin_path, "%s_boot_signed_B.bin"%(CONFIG_TARGET_BIN_NAME))
111if scons_usr_bool_option('CONFIG_TARGET_SIG_SHA256') == 'y':
112    boot_signed_bin = env.Command(target=os.path.join("#", bin_path, "%s_boot_signed.bin"%(CONFIG_TARGET_BIN_NAME)), source=boot_bin,
113        action="%s -i $SOURCE -o $TARGET -n -t%s"%(sign_tool, boot_bin_B))
114else:
115    if  scons_usr_bool_option('CONFIG_BOOT_ENCRYPT') == 'y':
116        boot_signed_bin = env.Command(target=os.path.join("#", bin_path, "%s_boot_signed.bin"%(CONFIG_TARGET_BIN_NAME)), source=boot_bin,
117            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))
118    else:
119        boot_signed_bin = env.Command(target=os.path.join("#", bin_path, "%s_boot_signed.bin"%(CONFIG_TARGET_BIN_NAME)), source=boot_bin,
120            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))
121
122env.AddPostAction(boot_bin, cleanup)
123env.AddPostAction(boot_signed_bin, cleanup)
124
125Return('boot_signed_bin')
126
127