• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# coding=utf-8
3
4'''
5* Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
6* Licensed under the Apache License, Version 2.0 (the "License");
7* you may not use this file except in compliance with the License.
8* You may obtain a copy of the License at
9*
10*     http://www.apache.org/licenses/LICENSE-2.0
11*
12* Unless required by applicable law or agreed to in writing, software
13* distributed under the License is distributed on an "AS IS" BASIS,
14* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15* See the License for the specific language governing permissions and
16* limitations under the License.
17*
18* Description: SCons build system entry.
19'''
20
21EnsureSConsVersion(3, 0, 1)
22EnsurePythonVersion(3, 7)
23
24import os
25import sys
26import datetime
27import atexit
28from distutils import dir_util
29sys.path.insert(0,os.path.join(os.getcwd(), 'build'))
30from scripts import common_env
31from scripts import scons_utils
32from scripts import scons_app
33from scripts import scons_env_cfg
34from scripts import pkt_builder
35from scripts.packet_create import packet_bin
36from tools.nvtool.build_nv import make_nv_bin
37import shutil
38
39#Init compile parameters
40env_cfg = scons_env_cfg.SconsEnvCfg()
41
42#Accept APP parameter
43app_name = ARGUMENTS.get('app', 'demo')
44factory_mode = ARGUMENTS.get('factory_mode', 'no')
45app_builder = scons_app.AppTarget(app_name, factory_mode)
46env_cfg.set_app_builder(app_builder)
47
48#Compile message output control
49if common_env.log_output_flag == False:
50    current = datetime.datetime.now()
51    if not os.path.exists(os.path.dirname(env_cfg.log_path)) or not os.path.exists(env_cfg.log_path):
52        os.makedirs(env_cfg.log_path)
53    log_file = os.path.join(env_cfg.log_path,'scons_trace.log')
54    old_stdout = sys.stdout
55    file = open(log_file, 'w+')
56    file.write("Building at %s %s\n" % (current.strftime('%Y/%m/%d'), current.strftime('%H:%M:%S')))
57    sys.stdout = file
58
59#Init environment
60env = Environment(ENV={'PATH':os.environ['PATH']},
61                  TARGET_PREFIX=env_cfg.target_name,)
62
63env_cfg.set_tools(env)
64env_cfg.set_environs(env)
65
66print('----------------------------top-----------------')
67#libraries to be built
68libs = [env.SConscript(os.path.join(env_cfg.get_module_dir(module), 'SConscript'), {'env':env, 'env_cfg':env_cfg, 'module':module},
69    variant_dir=os.path.join(env_cfg.lib_path, env_cfg.get_module_dir(module)), duplicate=0) for module in env_cfg.get_build_modules()]
70#Get settings
71env['LIBPATH'] = env_cfg.get_lib_path()
72
73if scons_utils.scons_usr_bool_option('CONFIG_TARGET_CHIP_HI3861') == 'y':
74    if scons_utils.scons_usr_bool_option('CONFIG_DIAG_SUPPORT') == 'y':
75        if factory_mode == 'yes':
76            env.Append(LIBPATH=os.path.join('-Lbuild', 'libs', 'hi3861', 'debug', 'factory'))
77        elif scons_utils.scons_usr_bool_option('CONFIG_QUICK_SEND_MODE') == 'y':
78            env.Append(LIBPATH=os.path.join('-Lbuild', 'libs', 'hi3861', 'debug', 'no_mesh_quick_start'))
79        elif scons_utils.scons_usr_bool_option('CONFIG_MESH_SUPPORT') == 'y':
80            env.Append(LIBPATH=os.path.join('-Lbuild', 'libs', 'hi3861', 'debug', 'mesh'))
81        else:
82            env.Append(LIBPATH=os.path.join('-Lbuild', 'libs', 'hi3861', 'debug', 'no_mesh'))
83    else:
84        if factory_mode == 'yes':
85            env.Append(LIBPATH=os.path.join('-Lbuild', 'libs', 'hi3861', 'release', 'factory'))
86        elif scons_utils.scons_usr_bool_option('CONFIG_QUICK_SEND_MODE') == 'y':
87            env.Append(LIBPATH=os.path.join('-Lbuild', 'libs', 'hi3861', 'release', 'no_mesh_quick_start'))
88        elif scons_utils.scons_usr_bool_option('CONFIG_MESH_SUPPORT') == 'y':
89            env.Append(LIBPATH=os.path.join('-Lbuild', 'libs', 'hi3861', 'release', 'mesh'))
90        else:
91            env.Append(LIBPATH=os.path.join('-Lbuild', 'libs', 'hi3861', 'release', 'no_mesh'))
92else:
93    if scons_utils.scons_usr_bool_option('CONFIG_DIAG_SUPPORT') == 'y':
94        if factory_mode == 'yes':
95            env.Append(LIBPATH=os.path.join('-Lbuild', 'libs', 'hi3861l', 'debug', 'factory'))
96        elif scons_utils.scons_get_cfg_val('CONFIG_CHIP_PKT_48K') == 'y':
97            env.Append(LIBPATH=os.path.join('-Lbuild', 'libs', 'hi3861l', 'debug', 'no_mesh_pkt_48k'))
98        elif scons_utils.scons_usr_bool_option('CONFIG_MESH_SUPPORT') == 'y':
99            env.Append(LIBPATH=os.path.join('-Lbuild', 'libs', 'hi3861l', 'debug', 'mesh'))
100        else:
101            env.Append(LIBPATH=os.path.join('-Lbuild', 'libs', 'hi3861l', 'debug', 'no_mesh'))
102    else:
103        if factory_mode == 'yes':
104            env.Append(LIBPATH=os.path.join('-Lbuild', 'libs', 'hi3861l', 'release', 'factory'))
105        elif scons_utils.scons_get_cfg_val('CONFIG_CHIP_PKT_48K') == 'y':
106            env.Append(LIBPATH=os.path.join('-Lbuild', 'libs', 'hi3861l', 'release', 'no_mesh_pkt_48k'))
107        elif scons_utils.scons_usr_bool_option('CONFIG_MESH_SUPPORT') == 'y':
108            env.Append(LIBPATH=os.path.join('-Lbuild', 'libs', 'hi3861l', 'release', 'mesh'))
109        else:
110            env.Append(LIBPATH=os.path.join('-Lbuild', 'libs', 'hi3861l', 'release', 'no_mesh'))
111
112env.Append(LIBPATH=app_builder.get_app_lib_path())
113## wifiiot_app is the only one app for ohos
114env['LIBS'] = list(map(lambda x:'-l%s'%x, env_cfg.get_libs()))
115env.Append(LIBS = app_builder.get_app_libs())
116env.Append(LIBS = '-lwifi_flash')
117env.Append(LIBS = '-lwifi')
118env.Append(LIBS = '-llitekernel_flash')
119env.Append(LIBS = '-lsystem')
120if scons_utils.scons_usr_bool_option('CONFIG_MESH_SUPPORT') == 'y':
121    if factory_mode != 'yes':
122        env.Append(LIBS = '-lmeshautolink')
123env['LIBS'] = sorted(env["LIBS"])
124if app_name == "wifiiot_app":
125    env['LIBS'] += ['--whole-archive'] + list(map(lambda x:'-l%s'%x, env_cfg.get_ohos_libs())) + ['--no-whole-archive']
126env['LIBPATH'] = sorted(env["LIBPATH"])
127env['LINKFILE'] = env_cfg.link_file
128env['MAPFILE'] = env_cfg.map_file
129print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~libpath:',env['LIBPATH'])
130print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~libs:',env['LIBS'])
131
132#######################################################  LINK by Command  #######################################################
133
134# config by menuconfig
135signature = {
136    'RSA':{
137        0:0x0054d3c0, # factory startup addr:0x1E3000(HILINK_ADDR - 600K) = 0x14D000
138        'A':0x0040d3c0,
139        'B':0x004f13c0,
140    },
141    'ECC':{
142        0:0x0054d3c0, # factory startup addr:0x1E3000(HILINK_ADDR - 600K) = 0x14D000
143        'A':0x0040d3c0,
144        'B':0x004f13c0,
145    }
146}
147
148link_env = env.Clone()
149
150"""Build link script
151"""
152linker_builder = Builder(
153    action='$CC $LINK_SCRIPTS_FLAG -E $SOURCE -o $TARGET -P',
154    src_suffix='.ld.S'
155)
156
157"""Build elf file
158"""
159elf_builder = Builder(
160    action='$LINK $LINKFLAGS $LIBPATH -T$LINKFILE -Map=$MAPFILE -o $TARGET --start-group $LIBS --end-group',#--verbose
161    suffix='.out'
162)
163
164"""Build binary from .out file
165"""
166binary_builder = Builder(
167    action='$OBJCOPY -O binary $SOURCE $TARGET',
168    suffix='.bin',
169    src_suffix='.out'
170)
171
172"""Build asm file from binary
173"""
174asm_builder = Builder(
175    action='$OBJDUMP -d $SOURCE >$TARGET',
176    suffix='.asm',
177    src_suffix='.out'
178)
179
180"""Base image builder
181"""
182def baseimg_builder(target, source, env):
183    base_bin_target = str(target[0])
184    scons_utils.scons_bin_dd(str(source[0]), base_bin_target, seek=0, count=278)
185    env_cfg.base_bin_check(base_bin_target)
186    no_base_bin_target = str(target[1])
187    scons_utils.scons_bin_dd(str(source[0]), no_base_bin_target, skip=278)
188
189"""NV image builder
190"""
191def nvimg_builder(target, source, env):
192    if os.path.exists(env_cfg.nv_path) is False:
193        os.makedirs(env_cfg.nv_path)
194    make_nv_bin(env_cfg.nv_path, env_cfg.target_name, env_cfg.nv_cfg_name) #genrate nv first
195
196"""Combine NV image and kernel
197"""
198def nvkernel_builder(target, source, env):
199    factory_nv = str(source[0])
200    normal_nv = str(source[1])
201    no_base_bin_target = str(source[2])
202    nv_kernel_bin = str(target[0])
203    scons_utils.scons_bin_dd(factory_nv, nv_kernel_bin, seek=0, bs=4096, count=1)
204    scons_utils.scons_bin_dd(normal_nv, nv_kernel_bin, seek=2, bs=4096, count=1)
205    scons_utils.scons_bin_dd(no_base_bin_target, nv_kernel_bin, seek=4, bs=4096)
206
207factory_nv = os.path.join(env_cfg.nv_path, env_cfg.nv_factory_name)
208normal_nv = os.path.join(env_cfg.nv_path, env_cfg.nv_normal_name)
209#Build flashboot
210flash_boot_bin = env.SConscript(os.path.join('boot', 'flashboot', 'SConscript'), {'env':env, 'env_cfg':env_cfg, 'module':'boot'}, duplicate=0)
211#Build loaderboot
212loader_boot_bin = env.SConscript(os.path.join('boot', 'loaderboot', 'SConscript'), {'env':env, 'env_cfg':env_cfg, 'module':'loaderboot'}, duplicate=0)
213#ota object
214ota_flag = 1 if scons_utils.scons_usr_bool_option('CONFIG_COMPRESSION_OTA_SUPPORT') != 'y' else 0
215ota_build_object = pkt_builder.ImageBuild(env_cfg.target_name, ota_mode=ota_flag)
216def init_ota_build_object(ota_build_object):
217    ota_build_object.set_pkt_path(env_cfg.bin_path)
218    ota_build_object.set_src_path(boot_bin_path=str(flash_boot_bin[0]), normal_nv_path=normal_nv, factory_nv_path=factory_nv)
219    ota_build_object.set_chip_product_name(env.get("CHIP_TYPE"))
220
221def get_ota_object():
222    return ota_build_object
223
224def get_secure_boot():
225    if (scons_utils.scons_usr_bool_option('CONFIG_TARGET_SIG_SHA256') == 'y'):
226        return False
227    else:
228        return True;
229
230def get_hilink_enable():
231    if (scons_utils.scons_usr_bool_option('CONFIG_HILINK') == 'y'):
232        return True
233    else:
234        return False;
235
236def get_sign_dict():
237    sig = ''
238    if scons_utils.scons_usr_bool_option('CONFIG_TARGET_SIG_RSA_V15') == 'y':
239        sig = 'RSA'
240        get_ota_object().set_sign_alg(0x0)
241    elif scons_utils.scons_usr_bool_option('CONFIG_TARGET_SIG_RSA_PSS') == 'y':
242        sig = 'RSA'
243        get_ota_object().set_sign_alg(0x1)
244    elif scons_utils.scons_usr_bool_option('CONFIG_TARGET_SIG_ECC') == 'y':
245        sig = 'ECC'
246        get_ota_object().set_sign_alg(0x10)
247    elif scons_utils.scons_usr_bool_option('CONFIG_TARGET_SIG_SHA256') == 'y':
248        sig = 'ECC'
249        get_ota_object().set_sign_alg(0x3F)
250    if sig not in signature:
251        raise scons_utils.SconsBuildError("%s============== <%s> SIGNATURE SETTING NULL =============%s"%(scons_utils.colors['red'], sig, scons_utils.colors['end']))
252    return signature[sig]
253
254init_ota_build_object(ota_build_object)
255sign_dict = get_sign_dict()    #signature mode configuration
256
257def ota_builder(target, source, env):
258    """Build upg binary
259    """
260    get_ota_object().set_encrypt_flag(0x42)
261    if scons_utils.scons_usr_bool_option('CONFIG_FLASH_ENCRYPT_SUPPORT') == 'y':
262        get_ota_object().set_file_attr_encrypt(0x2)
263    else:
264        get_ota_object().set_file_attr_encrypt(0x1)
265    if scons_utils.scons_usr_bool_option('CONFIG_COMPRESSION_OTA_SUPPORT') == 'y':
266        get_ota_object().set_kernel_file_attr_ota(0x4)
267        get_ota_object().set_kernel_max_size(0) #(912+972)KB
268    else:
269        get_ota_object().set_kernel_file_attr_ota(env['SIGN'])
270        get_ota_object().set_kernel_max_size(env['SIGN']) #912 or 972KB
271        if scons_utils.scons_usr_bool_option('CONFIG_FLASH_ENCRYPT_SUPPORT') == 'y':
272            get_ota_object().set_encrypt_flag(0xFF)
273    get_ota_object().set_src_path(kernel_bin_path=str(source[1]))
274    get_ota_object().BuildUpgBin(str(target[0]))
275
276    return 0
277
278def factory_builder(target, source, env):
279    """Build factory binary
280    """
281    get_ota_object().set_encrypt_flag(0x42)
282    if scons_utils.scons_usr_bool_option('CONFIG_FLASH_ENCRYPT_SUPPORT') == 'y':
283        get_ota_object().set_file_attr_encrypt(0x2)
284    else:
285        get_ota_object().set_file_attr_encrypt(0x1)
286    get_ota_object().set_kernel_file_attr_ota('A') # same as kernel A.
287    get_ota_object().set_kernel_max_size(0x4) #0x4 means __factory_bin_max_size:600K
288    get_ota_object().set_src_path(kernel_bin_path=str(source[1]))
289    get_ota_object().BuildUpgBurnBin(str(target[0]))
290
291    factory_bin_path = os.path.join('build', 'libs', 'factory_bin')
292    if not os.path.exists(factory_bin_path):
293        os.makedirs(factory_bin_path)
294    shutil.rmtree(factory_bin_path)
295    shutil.copytree(env_cfg.bin_path, factory_bin_path, False) #copy factory bin output to build/libs temply.
296
297    return 0
298
299def burn_bin_builder(target, source, env):
300    """Build binary
301    """
302    get_ota_object().set_build_temp_path(build_temp_path = env_cfg.cache_path)
303    burn_bin = get_ota_object().BuildHiburnBin(str(target[0]), str(source[0]))
304    loader_bin = str(source[1])
305    efuse_bin = str(source[2]) if len(source) == 3 else None
306    boot_b = os.path.join('output', 'bin', '%s_boot_signed_B.bin'%(env.get('CHIP_TYPE')))
307    boot_b_size = os.path.getsize(boot_b)
308    factory_bin_path = os.path.join('build', 'libs', 'factory_bin')
309    factory_bin = os.path.join(factory_bin_path, '%s_factory.bin'%env_cfg.target_name)
310    burn_for_erase_bin = os.path.join('build', 'basebin', 'burn_for_erase_4k.bin')
311    #证书安全存储示例
312    tee_cert1_file = os.path.join('build', 'basebin', 'tee_cert1.bin');
313    tee_cert2_file = os.path.join('build', 'basebin', 'tee_cert2.bin');
314    tee_key_file = os.path.join('build', 'basebin', 'tee_key.bin');
315    tee_cert_key_bin_max = 12*1024; #必须为4KB证书倍,需匹配分区表确定烧写地址和大小
316    tee_total_file_cnt = 3; #3个文件:2个证书,1个key。
317    burn_tee_cert = False
318    if ((os.path.exists(tee_cert1_file)) and (os.path.exists(tee_cert2_file)) and (os.path.exists(tee_key_file))):
319        burn_tee_cert = True
320
321    version_bin = bytearray(8)
322    boot_ver = get_ota_object().get_flashboot_file_ver()
323    kernel_ver = get_ota_object().get_kernel_file_ver()
324    boot_ver_bytes = boot_ver.to_bytes(4, byteorder = 'little', signed = True)
325    kernel_ver_bytes = kernel_ver.to_bytes(4, byteorder = 'little', signed = True)
326    version_bin[0:4] = boot_ver_bytes
327    version_bin[4:8] = kernel_ver_bytes
328    version_file = os.path.join("output", "bin", '%s_vercfg.bin'%env_cfg.target_name)
329    with open(version_file, 'wb+') as fp:
330        fp.write(version_bin)
331
332    burn_bin_ease_size = 0x200000;
333    #根据分区表适配烧写地址和大小
334    if (get_hilink_enable() == True):
335        burn_bin_ease_size = 0x200000 - 0x8000 - 0x1000 - 0x2000
336    if (burn_tee_cert == True):
337        burn_bin_ease_size = 0x200000 - 0x8000 - 0x1000 - 0x2000 - 0x5000
338
339    if os.path.exists(factory_bin):
340        cmd_list = ['%s|0|0|0'%loader_bin, '%s|0|0|3'%efuse_bin, '%s|0|%d|1'%(burn_bin, burn_bin_ease_size), '%s|%d|%d|6'%(factory_bin, 0x14D000, 0x96000)] if efuse_bin!=None else ['%s|0|0|0'%loader_bin, '%s|0|%d|1'%(burn_bin, burn_bin_ease_size), '%s|%d|%d|6'%(factory_bin, 0x14D000, 0x96000),]
341        shutil.copytree(factory_bin_path, os.path.join(env_cfg.bin_path, 'factory_bin'))
342    else:
343        cmd_list = ['%s|0|0|0'%loader_bin, '%s|0|0|3'%efuse_bin, '%s|0|%d|1'%(burn_bin, burn_bin_ease_size)] if efuse_bin!=None else ['%s|0|0|0'%loader_bin, '%s|0|%d|1'%(burn_bin, burn_bin_ease_size)]
344
345    if ((get_hilink_enable() == True) or (burn_tee_cert == True)):
346        cmd_list.append('%s|%d|%d|1'%(burn_for_erase_bin, 0x200000 - 0x8000 - 0x1000, 0x1000))
347
348    #cmd_list.append('%s|%d|%d|1'%(boot_b, 0x200000 - boot_b_size, boot_b_size));
349
350    if (burn_tee_cert == True):
351        cert_key_bin = bytearray(tee_cert_key_bin_max)
352        tee_cert1_size = os.path.getsize(tee_cert1_file)
353        tee_cert2_size = os.path.getsize(tee_cert2_file)
354        tee_key_size = os.path.getsize(tee_key_file)
355        total_cert_key_size = tee_cert1_size + tee_cert2_size + tee_key_size
356        if (total_cert_key_size > tee_cert_key_bin_max - 4 - 4 - 4*tee_total_file_cnt):
357            print ("%s============== cert total len bigger than %d=============%s"%(scons_utils.colors['red'], total_cert_key_size,scons_utils.colors['end']))
358            return -1
359        else:
360            with open(tee_cert1_file, 'rb') as fp:
361                tee_cert1_bin = fp.read()
362            with open(tee_cert2_file, 'rb') as fp:
363                tee_cert2_bin = fp.read()
364            with open(tee_key_file, 'rb') as fp:
365                tee_key_bin = fp.read()
366
367            #填充头部结构
368            start_flag = 0xDEADBEEF
369            start_flag_bytes = start_flag.to_bytes(4, byteorder = 'little', signed = False)
370            cert_key_bin[0:4] = start_flag_bytes #填充魔术字
371            tee_total_file_cnt_bytes = tee_total_file_cnt.to_bytes(4, byteorder = 'little', signed = True)
372            cert_key_bin[4:8] = tee_total_file_cnt_bytes #填充总的文件数
373            #填充各文件的大小
374            cert_key_bin[8:12] = tee_cert1_size.to_bytes(4, byteorder = 'little', signed = True)
375            cert_key_bin[12:16] = tee_cert2_size.to_bytes(4, byteorder = 'little', signed = True)
376            cert_key_bin[16:20] = tee_key_size.to_bytes(4, byteorder = 'little', signed = True)
377            #填充各文件
378            cert_key_bin[20:20 + tee_cert1_size] = tee_cert1_bin
379            cert_key_bin[20 + tee_cert1_size:20 + tee_cert1_size + tee_cert2_size] = tee_cert2_bin
380            cert_key_bin[20 + tee_cert1_size + tee_cert2_size:20 + tee_cert1_size + tee_cert2_size + tee_key_size] = tee_key_bin
381            #写文件
382            cert_bin_file = os.path.join("output", "bin", '%s_tee_cert_key.bin'%env_cfg.target_name)
383            with open(cert_bin_file, 'wb+') as fp:
384                fp.write(cert_key_bin)
385            cmd_list.append('%s|%d|%d|1'%(cert_bin_file, 0x200000 - 0x8000 - 0x1000 - 0x2000 - 0x5000, tee_cert_key_bin_max))
386
387    if (get_secure_boot() == True): #only need write ver file in secure boot mode.
388        cmd_list.append('%s|0|0|7'%version_file)
389
390    packet_bin(str(target[1]), cmd_list)
391    if os.path.exists(factory_bin_path):
392        shutil.rmtree(factory_bin_path)
393    return 0
394
395def compress_ota_builder(target, source, env):
396    """Build compressed upgrade file
397    """
398    get_ota_object().set_encrypt_flag(0x42)
399    if scons_utils.scons_usr_bool_option('CONFIG_FLASH_ENCRYPT_SUPPORT') == 'y':
400        get_ota_object().set_file_attr_encrypt(0x2)
401        get_ota_object().set_encrypt_flag(0xFF)
402    else:
403        get_ota_object().set_file_attr_encrypt(0x1)
404    get_ota_object().set_kernel_file_attr_ota(0x4)
405    get_ota_object().set_build_temp_path(build_temp_path = env_cfg.cache_path)
406    compress_ota = get_ota_object().BuildCompressUpgBin(str(target[0]), str(source[0]))
407    return 0
408
409def boot_ota_builder(target, source, env):
410    """Build boot ota
411    """
412    if scons_utils.scons_usr_bool_option('CONFIG_FLASH_ENCRYPT_SUPPORT') == 'y':
413        get_ota_object().set_file_attr_encrypt(0x2)
414    else:
415        get_ota_object().set_file_attr_encrypt(0x1)
416    if scons_utils.scons_usr_bool_option('CONFIG_COMPRESSION_OTA_SUPPORT') == 'y':
417        get_ota_object().set_flashboot_file_attr_ota(0x4)
418    else:
419        get_ota_object().set_flashboot_file_attr_ota(0x3)
420    get_ota_object().set_encrypt_flag(0x42)
421    boot_ota = get_ota_object().BuildUpgBoot(str(target[0]))
422    return 0
423
424#Builders
425link_env.Append(BUILDERS={'LinkFile':linker_builder,
426    'Elf':elf_builder,
427    'Binary':binary_builder,
428    'Asm':asm_builder,
429    'BaseImg':Builder(action=baseimg_builder),
430    'NVKernel':Builder(action=nvkernel_builder),
431    'BootOta':Builder(action=boot_ota_builder),
432    'OtaImg':Builder(action=ota_builder),
433    'FactoryImg':Builder(action=factory_builder),
434    'BurnImg':Builder(action=burn_bin_builder),
435    'CompressOtaImg':Builder(action = compress_ota_builder),
436})
437
438def build_all(build_env, link_sys, flash_boot_bin, loader_boot_bin):
439    """Processing build
440    """
441
442    #kernel_ver
443    kernel_ver = scons_utils.scons_usr_int_option('CONFIG_TARGET_KERNEL_VER')
444    if (kernel_ver < 0 or kernel_ver > 48):
445        raise scons_utils.SconsBuildError("%s============== kernel_ver invalied, should be 0-48 =============%s"%(scons_utils.colors['red'], scons_utils.colors['end']))
446    else:
447        get_ota_object().set_kernel_file_ver(kernel_ver)
448
449    #boot_ver
450    boot_ver = scons_utils.scons_usr_int_option('CONFIG_TARGET_BOOT_VER')
451    if (boot_ver < 0 or boot_ver > 16):
452        raise scons_utils.SconsBuildError("%s============== boot_ver invalied, should be 0-16 =============%s"%(scons_utils.colors['red'], scons_utils.colors['end']))
453    else:
454        get_ota_object().set_flashboot_file_ver(boot_ver)
455
456    #images container, insert boot ota at first
457    imgs = [build_env.BootOta(target=os.path.join(env_cfg.bin_path, '%s_flash_boot_ota.bin'%env_cfg.target_name), source=flash_boot_bin)]
458    for sig_key in sign_dict:
459        if sig_key == 0 and factory_mode != 'yes':
460            continue
461        sign_build = build_env.Clone()
462        sign_build['SIGN'] = sig_key
463        name_suffix = '_%s'%sig_key
464        if sig_key == 0:
465            name_suffix = '_factory'
466            ota_file = os.path.join(env_cfg.bin_path, '%s%s.bin'%(env_cfg.target_name, name_suffix))
467        elif scons_utils.scons_usr_bool_option('CONFIG_COMPRESSION_OTA_SUPPORT') == 'y':
468            name_suffix = ''
469            ota_file = os.path.join(env_cfg.cache_path, '%s_ota_%s.bin'%(env_cfg.target_name, 'temp')) #tmp file
470        else:
471            ota_file = os.path.join(env_cfg.bin_path, '%s_ota%s.bin'%(env_cfg.target_name, name_suffix))
472
473        sign_build['LINKFILE'] = os.path.join(env_cfg.link_path, 'link%s.lds'%name_suffix)
474        sign_build['MAPFILE'] = '%s%s.map'%(sign_build['MAPFILE'][:-len('.map')], name_suffix)
475        sign_build.Append(LINK_SCRIPTS_FLAG = '-DFLASH_FIRM_START=%s'%sign_dict[sig_key])
476
477        link_risc = sign_build.LinkFile(source=os.path.join('build', 'link', 'link'),
478                                        target='$LINKFILE')
479        sign_build.Depends(link_risc, link_sys)
480        #start
481        target_out = sign_build.Elf(source = libs, target = os.path.join(env_cfg.bin_path, '%s%s.out'%(env_cfg.target_name, name_suffix)))
482        sign_build.Depends(target_out, [link_risc, libs])
483        target_out_bin = sign_build.Binary(source = target_out, target = os.path.join(env_cfg.cache_path, '%s%s'%(env_cfg.target_name, name_suffix)))
484        target_asm = sign_build.Asm(source = target_out, target=os.path.join(env_cfg.bin_path, '%s%s.asm'%(env_cfg.target_name, name_suffix)))
485
486        base_bin_target = os.path.join(env_cfg.cache_path, '%s_base%s.bin'%(env_cfg.target_name, name_suffix))
487        kernel_bin_target = os.path.join(env_cfg.cache_path, '%s_kernel%s.bin'%(env_cfg.target_name, name_suffix))
488
489        #Build kernel and ota
490        kernel = sign_build.BaseImg(source=target_out_bin, target=[base_bin_target, kernel_bin_target])
491        if factory_mode == 'yes':
492            ota_bin = sign_build.FactoryImg(source=kernel, target=ota_file)
493        else:
494            ota_bin = sign_build.OtaImg(source=kernel, target=ota_file)
495        imgs.append(ota_bin)
496        sign_build.AddPostAction(kernel, scons_utils.cleanup) #hook clean
497        if sig_key == 'A': #need signature
498            loader_bin = os.path.join('#', env_cfg.bin_path, '%s_loader_signed.bin'%(env.get('CHIP_TYPE')))
499            efuse_bin = os.path.join('build', 'basebin', 'efuse_cfg.bin')
500            burn_bin = os.path.join(env_cfg.bin_path, '%s_burn.bin'%env_cfg.target_name)
501            allinone_bin = os.path.join(env_cfg.bin_path, '%s_allinone.bin'%env_cfg.target_name)
502            burn_bins = sign_build.BurnImg(source=[ota_bin, loader_bin, efuse_bin] if os.path.exists(efuse_bin) else [ota_bin, loader_bin],
503                                           target=[burn_bin, allinone_bin])
504            imgs.append(burn_bins)
505        #mark binaries to be built everytime
506        if sig_key in ['A', 'B']:
507            sign_build.AlwaysBuild([link_risc, target_out, target_out_bin, target_asm, kernel, ota_bin, burn_bins])
508        if scons_utils.scons_usr_bool_option('CONFIG_COMPRESSION_OTA_SUPPORT') == 'y' and sig_key == 'A':
509            compress_ota_bin = os.path.join(env_cfg.bin_path, '%s_ota.bin'%env_cfg.target_name)
510            compress_ota_file = sign_build.CompressOtaImg(source = ota_bin, target = compress_ota_bin)
511            break
512        if sig_key == 0:
513            break
514    return imgs
515
516link_env.AddMethod(build_all, 'BUILD')
517#prepare link script
518link_sys = link_env.LinkFile(source=os.path.join('build', 'link', 'system_config'),
519                             target=os.path.join(env_cfg.link_path, 'system_config.ld'))
520link_env.Depends(link_sys, [flash_boot_bin, loader_boot_bin])
521link_env.AddPostAction(link_sys, nvimg_builder) #prepare nv image
522
523# START LINK
524target_img = link_env.BUILD(link_sys, flash_boot_bin, loader_boot_bin)
525link_env.AlwaysBuild([target_img, link_sys])
526Clean(target_img, env_cfg.clean_list)
527#######################################################  LINK  #######################################################
528
529#######################################################  BUILD CHECK  #######################################################
530
531def build_status():
532    bf = GetBuildFailures()
533    status = 0
534    failures_message = ''
535    if bf:
536        status = -1
537        failures_message = "\n".join(["Failed building %s" % scons_utils.bf_to_str(x) for x in bf if x is not None])
538    return (status, failures_message)
539
540def display_build_status():
541    if env.GetOption("clean"):
542        return
543    status, failures_message = build_status()
544    if status == 0:
545        scons_utils.show_burn_tips()
546    else:
547        scons_utils.show_alert("BUILD FAILED!!!!")
548        scons_utils.show_alert(failures_message)
549
550atexit.register(display_build_status)
551
552if common_env.log_output_flag == False:
553    file.close() #close log file handler
554    sys.stdout = old_stdout
555
556