• 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", #"-fldm-stm-optimize",
60 "-freorder-blocks-algorithm=simple", "-fno-schedule-insns",
61    "-nostdinc", "-fno-aggressive-loop-optimizations", "-fno-builtin", "-Wpointer-arith", "-Wstrict-prototypes", "-Wno-write-strings",
62    "-fno-exceptions", "-fno-short-enums", "-Wundef", "-Wa,-enable-c-lbu-sb", "-mtune=size", "-msmall-data-limit=0", "-Wall", "-O2",
63    "-Werror", "-std=c99", "-falign-functions=2", "-fdata-sections", "-ffunction-sections", "-fno-common"]
64boot_asflags = ["-mabi=ilp32", "-march=rv32imc", "-x", "assembler-with-cpp", "-Wa,-enable-c-lbu-sb", "-O2", "-W", "-Wall", "-Werror",
65    "-nostdinc", "-fno-common"]
66defines = ["-DARCH_RISCV", "-DLOS_COMPILE_LDM", "-DHI_BOARD_ASIC"]
67incs = [os.path.join('#', 'boot', 'flashboot', 'fixed', 'include'), os.path.join('#', 'boot', 'flashboot', 'upg'),
68        os.path.join('#', 'boot', 'flashboot', 'include'), os.path.join('#', 'boot', 'flashboot', 'drivers', 'lsadc'), os.path.join('#', 'boot', 'flashboot', 'drivers', 'efuse'),
69        os.path.join('#', 'boot', 'flashboot', 'include', 'lzma'), os.path.join('#', 'boot', 'flashboot', 'drivers', 'gpio'), os.path.join('#', 'boot', 'flashboot', 'drivers', 'io'),
70        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')]
71
72if scons_usr_bool_option('CONFIG_COMPRESSION_OTA_SUPPORT') == 'y':
73    defines.append("-DCONFIG_COMPRESSION_OTA_SUPPORT")
74if scons_usr_bool_option('CONFIG_DUAL_PARTITION_OTA_SUPPORT') == 'y':
75    defines.append("-DCONFIG_DUAL_PARTITION_OTA_SUPPORT")
76if scons_usr_bool_option('CONFIG_TARGET_SIG_ECC') == 'y':
77    defines.append("-DCONFIG_TARGET_SIG_ECC")
78if scons_usr_bool_option('CONFIG_TARGET_SIG_RSA_V15') == 'y':
79    defines.append("-DCONFIG_TARGET_SIG_RSA_V15")
80if scons_usr_bool_option('CONFIG_TARGET_SIG_RSA_PSS') == 'y':
81    defines.append("-DCONFIG_TARGET_SIG_RSA_PSS")
82if scons_usr_bool_option('CONFIG_FLASH_ENCRYPT_SUPPORT') == 'y':
83    defines.append("-DCONFIG_FLASH_ENCRYPT_SUPPORT")
84if scons_usr_bool_option('CONFIG_FACTORY_TEST_SUPPORT') == 'y':
85    defines.append("-DCONFIG_FACTORY_TEST_SUPPORT")
86if scons_usr_bool_option('CONFIG_QUICK_SEND_MODE') == 'y':
87    defines.append("-DCONFIG_QUICK_SEND_MODE")
88
89if scons_get_cfg_val('CONFIG_CHIP_PKT_48K') == 'y':
90    defines.append("-DCONFIG_CHIP_PKT_48K")
91else:
92    defines.append("-DCONFIG_CHIP_PKT_32K")
93
94env.Replace(CPPDEFINES=defines)
95env.Replace(CCFLAGS=boot_cflags)
96env.Replace(ASFLAGS=boot_asflags)
97env.Replace(CPPPATH=incs)
98env.Replace(LINKFLAGS=ld_flags)
99#
100env['ASCOM'] = '$CC $ASPPFLAGS $CPPFLAGS $_CPPDEFFLAGS $_CPPINCFLAGS -c -o $TARGET $SOURCES'
101env['CCCOM'] = '$CC -o $TARGET -c $CFLAGS $CCFLAGS $_CCCOMCOM $SOURCES'
102src_path = module_libs['boot']
103
104objs = []
105for src in src_path:
106    path = src if '..' not in src else os.path.relpath(os.path.realpath(os.path.abspath(src)), os.path.join(env_cfg.root))
107    objs += env.SConscript(os.path.join(src, 'SConscript'), {'env':env,}, variant_dir = os.path.join('#', obj_path, "flashboot", path), duplicate=0)
108
109boot_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))
110boot_bin = env.Command(target=os.path.join("#", cache_path, "hi_flash_boot.bin"), source=boot_elf,
111    action="$OBJCOPY -Obinary -R .rom.text -R .rom.code.text -R .u_boot_cmd  -R .rom.data -R .rom.code.data -R .rom.bss \
112        -R .rom.code.bss -S $SOURCE $TARGET")
113boot_bin_B = os.path.join(bin_path, "%s_boot_signed_B.bin"%(CONFIG_TARGET_BIN_NAME))
114if scons_usr_bool_option('CONFIG_TARGET_SIG_SHA256') == 'y':
115    boot_signed_bin = env.Command(target=os.path.join("#", bin_path, "%s_boot_signed.bin"%(CONFIG_TARGET_BIN_NAME)), source=boot_bin,
116        action="%s -i $SOURCE -o $TARGET -n -t%s"%(sign_tool, boot_bin_B))
117else:
118    if  scons_usr_bool_option('CONFIG_BOOT_ENCRYPT') == 'y':
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 -e%s -t%s"%(sign_tool, root_key_path, sub_key_path, boot_ver, sign_type, encrypt_key_path, boot_bin_B))
121    else:
122        boot_signed_bin = env.Command(target=os.path.join("#", bin_path, "%s_boot_signed.bin"%(CONFIG_TARGET_BIN_NAME)), source=boot_bin,
123            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))
124
125env.AddPostAction(boot_bin, cleanup)
126env.AddPostAction(boot_signed_bin, cleanup)
127
128Return('boot_signed_bin')
129
130