1  /*
2  * Copyright (C) 2017-2024 Alibaba Group Holding Limited
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
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 
19 /******************************************************************************
20  * @file     pre_main.c
21  * @brief    source file for the pre_main
22  * @version  V1.0
23  * @date     04. April 2024
24  ******************************************************************************/
25 #include "pre_main.h"
26 
27 /*
28  *  The ranges of copy from/to are specified by following symbols
29  *    __erodata: LMA of start of the section to copy from. Usually end of rodata
30  *    __data_start__: VMA of start of the section to copy to
31  *    __data_end__: VMA of end of the section to copy to
32  *
33  *  All addresses must be aligned to 4 bytes boundary.
34  */
section_data_copy(void)35 void section_data_copy(void)
36 {
37     extern unsigned long __erodata;
38     extern unsigned long __data_start__;
39     extern unsigned long __data_end__;
40 
41     if (((unsigned long)&__erodata != (unsigned long)&__data_start__)) {
42         unsigned long src_addr = (unsigned long)&__erodata;
43         memcpy((void *)(&__data_start__), \
44                (void *)src_addr, \
45                (unsigned long)(&__data_end__) - (unsigned long)(&__data_start__));
46     }
47 }
48 
section_ram_code_copy(void)49 void section_ram_code_copy(void)
50 {
51     extern unsigned long __erodata;
52     extern unsigned long __data_start__;
53     extern unsigned long __data_end__;
54     extern unsigned long __ram_code_start__;
55     extern unsigned long __ram_code_end__;
56 
57     if (((unsigned long)&__erodata != (unsigned long)&__data_start__)) {
58         unsigned long src_addr = (unsigned long)&__erodata;
59         src_addr += (unsigned long)(&__data_end__) - (unsigned long)(&__data_start__);
60         memcpy((void *)(&__ram_code_start__), \
61                (void *)src_addr, \
62                (unsigned long)(&__ram_code_end__) - (unsigned long)(&__ram_code_start__));
63     }
64 }
65 
66 /*
67  *  The BSS section is specified by following symbols
68  *    __bss_start__: start of the BSS section.
69  *    __bss_end__: end of the BSS section.
70  *
71  *  Both addresses must be aligned to 4 bytes boundary.
72  */
section_bss_clear(void)73 void section_bss_clear(void)
74 {
75     extern unsigned long __bss_start__;
76     extern unsigned long __bss_end__;
77 
78     memset((void *)(&__bss_start__), \
79            0, \
80            (unsigned long)(&__bss_end__) - (unsigned long)(&__bss_start__));
81 
82 }
83