查看: 2375|回复: 0

[原创] uboot 源码深度分析注释,基于米尔 mys_y6ulx 开发板

[复制链接]
  • TA的每日心情
    开心
    2024-1-16 17:48
  • 签到天数: 592 天

    连续签到: 1 天

    [LV.9]以坛为家II

    发表于 2019-6-8 17:26:02 | 显示全部楼层 |阅读模式
    分享到:
    本贴所有源码同步到 github

    源码位于:https://github.com/robe-zhang/mys_y6ulx/tree/master/MYiR-iMX-uboot

    分析位于:https://github.com/robe-zhang/mys_y6ulx/blob/master/note_robe/note_uboot/note_uboot_code
    整个 uboot 项目源码分析注释:
    1. # ============================== 11111111
    2. (arch/arm/lib/vectors.S)
    3. _start:                                                             uboot 入口,中断向量表

    4. #ifdef CONFIG_SYS_DV_NOR_BOOT_CFG
    5.         .word        CONFIG_SYS_DV_NOR_BOOT_CFG
    6. #endif

    7.         b        reset
    8.         ldr        pc, _undefined_instruction
    9.         ldr        pc, _software_interrupt
    10.         ldr        pc, _prefetch_abort
    11.         ldr        pc, _data_abort
    12.         ldr        pc, _not_used
    13.         ldr        pc, _irq
    14.         ldr        pc, _fiq
    15.        
    16.         .globl        _undefined_instruction
    17.         .globl        _software_interrupt
    18.         .globl        _prefetch_abort
    19.         .globl        _data_abort
    20.         .globl        _not_used
    21.         .globl        _irq
    22.         .globl        _fiq

    23. _undefined_instruction:        .word undefined_instruction
    24. _software_interrupt:        .word software_interrupt
    25. _prefetch_abort:        .word prefetch_abort
    26. _data_abort:                .word data_abort
    27. _not_used:                .word not_used
    28. _irq:                        .word irq
    29. _fiq:                        .word fiq

    30.         .balignl 16,0xdeadbeef
    31.                                                                     irq stack frame 宏定义
    32.                                                                                                                                 中断处理函数,汇编处理,c 处理函数在 arch/arm/lib/interrupts.c
    33. # ==============================    22222222
    34. (arch/arm/cpu/armv7/start.o(uboot/u-boot.lds))
    35. reset:
    36.         /* Allow the board to save important registers */
    37.         b        save_boot_params
    38. save_boot_params_ret:
    39.         /*
    40.          * disable interrupts (FIQ and IRQ), also set the cpu to SVC32 mode,
    41.          * except if in HYP mode already
    42.          */
    43.         mrs        r0, cpsr
    44.         and        r1, r0, #0x1f                @ mask mode bits
    45.         teq        r1, #0x1a                @ test for HYP mode
    46.         bicne        r0, r0, #0x1f                @ clear all mode bits
    47.         orrne        r0, r0, #0x13                @ set SVC mode
    48.         orr        r0, r0, #0xc0                @ disable FIQ and IRQ
    49.         msr        cpsr,r0                                                 设置 SVC 模式,关中断
    50. #ifndef CONFIG_SKIP_LOWLEVEL_INIT
    51.         bl        cpu_init_cp15                                           协处理器 cp15 初始化,mmu,cache,TLBs 初始化,下面详细解析
    52.         bl        cpu_init_crit                                           系统复位初始化,pll 外设时钟初始化    ============   此部分有 C 代码,设置了初始栈指针
    53. #endif

    54.         bl        _main                                                   uboot 主体部分,下面详细解析

    55. # ==============================    33333333
    56. (arch\arm\cpu\armv7\start.S)                                                                                                                                  
    57. ENTRY(save_boot_params)
    58.         b        save_boot_params_ret                @ back to my caller                    用户自己可重新定义  save_boot_params 函数,实现功能
    59. ENDPROC(save_boot_params)
    60.         .weak        save_boot_params

    61. # ==============================    44444444
    62. (arch\arm\cpu\armv7\start.S)
    63. ENTRY(cpu_init_cp15)
    64.         /*
    65.          * Invalidate L1 I/D
    66.          */
    67.         mov        r0, #0                        @ set up for MCR                                    关闭 L1 i-cache,d-cache
    68.         mcr        p15, 0, r0, c8, c7, 0        @ invalidate TLBs
    69.         mcr        p15, 0, r0, c7, c5, 0        @ invalidate icache
    70.         mcr        p15, 0, r0, c7, c5, 6        @ invalidate BP array
    71.         mcr     p15, 0, r0, c7, c10, 4        @ DSB
    72.         mcr     p15, 0, r0, c7, c5, 4        @ ISB

    73.         /*
    74.          * disable MMU stuff and caches
    75.          */
    76.         mrc        p15, 0, r0, c1, c0, 0                                               关闭 mmu,caches
    77.         bic        r0, r0, #0x00002000        @ clear bits 13 (--V-)
    78.         bic        r0, r0, #0x00000007        @ clear bits 2:0 (-CAM)
    79.         orr        r0, r0, #0x00000002        @ set bit 1 (--A-) Align
    80.         orr        r0, r0, #0x00000800        @ set bit 11 (Z---) BTB
    81. #ifdef CONFIG_SYS_ICACHE_OFF
    82.         bic        r0, r0, #0x00001000        @ clear bit 12 (I) I-cache
    83. #else
    84.         orr        r0, r0, #0x00001000        @ set bit 12 (I) I-cache
    85. #endif
    86.         mcr        p15, 0, r0, c1, c0, 0

    87. #ifdef CONFIG_ARM_ERRATA_716044                                                   errata 处理
    88.         mrc        p15, 0, r0, c1, c0, 0        @ read system control register
    89.         orr        r0, r0, #1 << 11        @ set bit #11
    90.         mcr        p15, 0, r0, c1, c0, 0        @ write system control register
    91. #endif

    92. #if (defined(CONFIG_ARM_ERRATA_742230) || defined(CONFIG_ARM_ERRATA_794072))
    93.         mrc        p15, 0, r0, c15, c0, 1        @ read diagnostic register
    94.         orr        r0, r0, #1 << 4                @ set bit #4
    95.         mcr        p15, 0, r0, c15, c0, 1        @ write diagnostic register
    96. #endif

    97. #ifdef CONFIG_ARM_ERRATA_743622
    98.         mrc        p15, 0, r0, c15, c0, 1        @ read diagnostic register
    99.         orr        r0, r0, #1 << 6                @ set bit #6
    100.         mcr        p15, 0, r0, c15, c0, 1        @ write diagnostic register
    101. #endif

    102. #ifdef CONFIG_ARM_ERRATA_751472
    103.         mrc        p15, 0, r0, c15, c0, 1        @ read diagnostic register
    104.         orr        r0, r0, #1 << 11        @ set bit #11
    105.         mcr        p15, 0, r0, c15, c0, 1        @ write diagnostic register
    106. #endif
    107. #ifdef CONFIG_ARM_ERRATA_761320
    108.         mrc        p15, 0, r0, c15, c0, 1        @ read diagnostic register
    109.         orr        r0, r0, #1 << 21        @ set bit #21
    110.         mcr        p15, 0, r0, c15, c0, 1        @ write diagnostic register
    111. #endif
    112. #ifdef CONFIG_ARM_ERRATA_845369
    113.         mrc        p15, 0, r0, c15, c0, 1        @ read diagnostic register
    114.         orr        r0, r0, #1 << 22        @ set bit #22
    115.         mcr        p15, 0, r0, c15, c0, 1        @ write diagnostic register
    116. #endif

    117.         mov        r5, lr                        @ Store my Caller                                           cpu 版本相关的 errata 处理
    118.         mrc        p15, 0, r1, c0, c0, 0        @ r1 has Read Main ID Register (MIDR)
    119.         mov        r3, r1, lsr #20                @ get variant field
    120.         and        r3, r3, #0xf                @ r3 has CPU variant
    121.         and        r4, r1, #0xf                @ r4 has CPU revision
    122.         mov        r2, r3, lsl #4                @ shift variant field for combined value
    123.         orr        r2, r4, r2                @ r2 has combined CPU variant + revision

    124. #ifdef CONFIG_ARM_ERRATA_798870
    125.         cmp        r2, #0x30                @ Applies to lower than R3p0
    126.         bge        skip_errata_798870      @ skip if not affected rev
    127.         cmp        r2, #0x20                @ Applies to including and above R2p0
    128.         blt        skip_errata_798870      @ skip if not affected rev

    129.         mrc        p15, 1, r0, c15, c0, 0  @ read l2 aux ctrl reg
    130.         orr        r0, r0, #1 << 7         @ Enable hazard-detect timeout
    131.         push        {r1-r5}                        @ Save the cpu info registers
    132.         bl        v7_arch_cp15_set_l2aux_ctrl
    133.         isb                                @ Recommended ISB after l2actlr update
    134.         pop        {r1-r5}                        @ Restore the cpu info - fall through
    135. skip_errata_798870:
    136. #endif

    137. #ifdef CONFIG_ARM_ERRATA_801819
    138.         cmp        r2, #0x24                @ Applies to lt including R2p4
    139.         bgt        skip_errata_801819      @ skip if not affected rev
    140.         cmp        r2, #0x20                @ Applies to including and above R2p0
    141.         blt        skip_errata_801819      @ skip if not affected rev
    142.         mrc        p15, 0, r0, c0, c0, 6        @ pick up REVIDR reg
    143.         and        r0, r0, #1 << 3                @ check REVIDR[3]
    144.         cmp        r0, #1 << 3
    145.         beq        skip_errata_801819        @ skip erratum if REVIDR[3] is set

    146.         mrc        p15, 0, r0, c1, c0, 1        @ read auxilary control register
    147.         orr        r0, r0, #3 << 27        @ Disables streaming. All write-allocate
    148.                                         @ lines allocate in the L1 or L2 cache.
    149.         orr        r0, r0, #3 << 25        @ Disables streaming. All write-allocate
    150.                                         @ lines allocate in the L1 cache.
    151.         push        {r1-r5}                        @ Save the cpu info registers
    152.         bl        v7_arch_cp15_set_acr
    153.         pop        {r1-r5}                        @ Restore the cpu info - fall through
    154. skip_errata_801819:
    155. #endif

    156. #ifdef CONFIG_ARM_ERRATA_454179
    157.         cmp        r2, #0x21                @ Only on < r2p1
    158.         bge        skip_errata_454179

    159.         mrc        p15, 0, r0, c1, c0, 1        @ Read ACR
    160.         orr        r0, r0, #(0x3 << 6)        @ Set DBSM(BIT7) and IBE(BIT6) bits
    161.         push        {r1-r5}                        @ Save the cpu info registers
    162.         bl        v7_arch_cp15_set_acr
    163.         pop        {r1-r5}                        @ Restore the cpu info - fall through

    164. skip_errata_454179:
    165. #endif

    166. #ifdef CONFIG_ARM_ERRATA_430973
    167.         cmp        r2, #0x21                @ Only on < r2p1
    168.         bge        skip_errata_430973

    169.         mrc        p15, 0, r0, c1, c0, 1        @ Read ACR
    170.         orr        r0, r0, #(0x1 << 6)        @ Set IBE bit
    171.         push        {r1-r5}                        @ Save the cpu info registers
    172.         bl        v7_arch_cp15_set_acr
    173.         pop        {r1-r5}                        @ Restore the cpu info - fall through

    174. skip_errata_430973:
    175. #endif

    176. #ifdef CONFIG_ARM_ERRATA_621766
    177.         cmp        r2, #0x21                @ Only on < r2p1
    178.         bge        skip_errata_621766

    179.         mrc        p15, 0, r0, c1, c0, 1        @ Read ACR
    180.         orr        r0, r0, #(0x1 << 5)        @ Set L1NEON bit
    181.         push        {r1-r5}                        @ Save the cpu info registers
    182.         bl        v7_arch_cp15_set_acr
    183.         pop        {r1-r5}                        @ Restore the cpu info - fall through

    184. skip_errata_621766:
    185. #endif

    186.         mov        pc, r5                        @ back to my caller                                        返回
    187. ENDPROC(cpu_init_cp15)

    188. # ==============================    55555555
    189. (arch\arm\cpu\armv7\start.S)
    190. #ifndef CONFIG_SKIP_LOWLEVEL_INIT
    191. /*************************************************************************
    192. *
    193. * CPU_init_critical registers
    194. *
    195. * setup important registers
    196. * setup memory timing
    197. *
    198. *************************************************************************/
    199. ENTRY(cpu_init_crit)
    200.         /*
    201.          * Jump to board specific initialization...
    202.          * The Mask ROM will have already initialized
    203.          * basic memory. Go here to bump up clock rate and handle
    204.          * wake up conditions.
    205.          */
    206.         b        lowlevel_init                @ go setup pll,mux,memory
    207. ENDPROC(cpu_init_crit)
    208. #endif

    209. # ==============================      55555555.11111111
    210. (arch\arm\cpu\armv7\lowlevel_init.S)
    211. ENTRY(lowlevel_init)
    212.         /*
    213.          * Setup a temporary stack. Global data is not available yet.
    214.          */
    215.         ldr        sp, =CONFIG_SYS_INIT_SP_ADDR                                                   设置临时栈指针 CONFIG_SYS_INIT_SP_ADDR
    216.         bic        sp, sp, #7 /* 8-byte alignment for ABI compliance */
    217. #ifdef CONFIG_SPL_DM
    218.         mov        r9, #0
    219. #else
    220.         /*
    221.          * Set up global data for boards that still need it. This will be
    222.          * removed soon.
    223.          */
    224. #ifdef CONFIG_SPL_BUILD
    225.         ldr        r9, =gdata
    226. #else
    227.         sub        sp, sp, #GD_SIZE                                                                预留 gdata 内存 GD_SIZE
    228.         bic        sp, sp, #7
    229.         mov        r9, sp
    230. #endif
    231. #endif
    232.         /*
    233.          * Save the old lr(passed in ip) and the current lr to stack
    234.          */
    235.         push        {ip, lr}

    236.         /*
    237.          * Call the very early init function. This should do only the
    238.          * absolute bare minimum to get started. It should not:
    239.          *
    240.          * - set up DRAM
    241.          * - use global_data
    242.          * - clear BSS
    243.          * - try to start a console
    244.          *
    245.          * For boards with SPL this should be empty since SPL can do all of
    246.          * this init in the SPL board_init_f() function which is called
    247.          * immediately after this.
    248.          */
    249.         bl        s_init                                                                   系统复位初始化,pll 外设时钟初始化 / anatop(SRC/system reset control),ccm 等,代码位于 (arch\arm\cpu\armv7\mx6\soc.c)
    250.                                                                                      芯片寄存器定义位于 (arch\arm\include\asm\arch-mx6\imx-regs.h)
    251.         pop        {ip, pc}                                                                 弹栈,返回
    252. ENDPROC(lowlevel_init)

    253. # ==============================
    254. void s_init(void)
    255. {
    256.         struct anatop_regs *anatop = (struct anatop_regs *)ANATOP_BASE_ADDR;
    257.         struct mxc_ccm_reg *ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
    258.         u32 mask480;
    259.         u32 mask528;
    260.         u32 reg, periph1, periph2;

    261.         if (is_cpu_type(MXC_CPU_MX6SX) || is_cpu_type(MXC_CPU_MX6UL) ||
    262.             is_cpu_type(MXC_CPU_MX6ULL))
    263.                 return;

    264.         /* Due to hardware limitation, on MX6Q we need to gate/ungate all PFDs
    265.          * to make sure PFD is working right, otherwise, PFDs may
    266.          * not output clock after reset, MX6DL and MX6SL have added 396M pfd
    267.          * workaround in ROM code, as bus clock need it
    268.          */

    269.         mask480 = ANATOP_PFD_CLKGATE_MASK(0) |
    270.                 ANATOP_PFD_CLKGATE_MASK(1) |
    271.                 ANATOP_PFD_CLKGATE_MASK(2) |
    272.                 ANATOP_PFD_CLKGATE_MASK(3);
    273.         mask528 = ANATOP_PFD_CLKGATE_MASK(1) |
    274.                 ANATOP_PFD_CLKGATE_MASK(3);

    275.         reg = readl(&ccm->cbcmr);
    276.         periph2 = ((reg & MXC_CCM_CBCMR_PRE_PERIPH2_CLK_SEL_MASK)
    277.                 >> MXC_CCM_CBCMR_PRE_PERIPH2_CLK_SEL_OFFSET);
    278.         periph1 = ((reg & MXC_CCM_CBCMR_PRE_PERIPH_CLK_SEL_MASK)
    279.                 >> MXC_CCM_CBCMR_PRE_PERIPH_CLK_SEL_OFFSET);

    280.         /* Checking if PLL2 PFD0 or PLL2 PFD2 is using for periph clock */
    281.         if ((periph2 != 0x2) && (periph1 != 0x2))
    282.                 mask528 |= ANATOP_PFD_CLKGATE_MASK(0);

    283.         if ((periph2 != 0x1) && (periph1 != 0x1) &&
    284.                 (periph2 != 0x3) && (periph1 != 0x3))
    285.                 mask528 |= ANATOP_PFD_CLKGATE_MASK(2);

    286.         writel(mask480, &anatop->pfd_480_set);
    287.         writel(mask528, &anatop->pfd_528_set);
    288.         writel(mask480, &anatop->pfd_480_clr);
    289.         writel(mask528, &anatop->pfd_528_clr);
    290. }

    291. # ==============================    66666666
    292. (arch\arm\lib\crt0.S)
    293. ENTRY(_main)

    294. /*
    295. * Set up initial C runtime environment and call board_init_f(0).
    296. */

    297. #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_STACK)                       设置栈指针 1111
    298.         ldr        sp, =(CONFIG_SPL_STACK)
    299. #else
    300.         ldr        sp, =(CONFIG_SYS_INIT_SP_ADDR)
    301. #endif
    302. #if defined(CONFIG_CPU_V7M)        /* v7M forbids using SP as BIC destination */
    303.         mov        r3, sp
    304.         bic        r3, r3, #7
    305.         mov        sp, r3
    306. #else
    307.         bic        sp, sp, #7        /* 8-byte alignment for ABI compliance */
    308. #endif
    309.         mov        r0, sp
    310.         bl        board_init_f_alloc_reserve                                               board_init_f_alloc_reserve  2222  预留 堆空间 和 global_data 内存空间
    311.         mov        sp, r0
    312.         /* set up gd here, outside any C code */
    313.         mov        r9, r0
    314.         bl        board_init_f_init_reserve                                                board_init_f_init_reserve   3333  16字节对齐,从上下为 堆空间/gd->malloc_base,global_data 空间

    315.         mov        r0, #0
    316.         bl        board_init_f                                                             board_init_f                4444  最后调用 init_sequence_f

    317. #if ! defined(CONFIG_SPL_BUILD)

    318. /*
    319. * Set up intermediate environment (new sp and gd) and call
    320. * relocate_code(addr_moni). Trick here is that we'll return
    321. * 'here' but relocated.
    322. */

    323.         ldr        sp, [r9, #GD_START_ADDR_SP]        /* sp = gd->start_addr_sp */                 设置栈指针,对齐
    324. #if defined(CONFIG_CPU_V7M)        /* v7M forbids using SP as BIC destination */
    325.         mov        r3, sp
    326.         bic        r3, r3, #7
    327.         mov        sp, r3
    328. #else
    329.         bic        sp, sp, #7        /* 8-byte alignment for ABI compliance */                    
    330. #endif
    331.         ldr        r9, [r9, #GD_BD]                /* r9 = gd->bd */                               分配新 GD 内存,设置 r9 为新 GD 地址
    332.         sub        r9, r9, #GD_SIZE                /* new GD is below bd */

    333.         adr        lr, here
    334.         ldr        r0, [r9, #GD_RELOC_OFF]                /* r0 = gd->reloc_off */                    设置 relocate 后的 here 代码地址
    335.         add        lr, lr, r0
    336. #if defined(CONFIG_CPU_V7M)
    337.         orr        lr, #1                                /* As required by Thumb-only */
    338. #endif
    339.         ldr        r0, [r9, #GD_RELOCADDR]                /* r0 = gd->relocaddr */                     设置 gd->relocaddr 指针后
    340.         b        relocate_code                                                            relocate_code             5555          relocate __image_copy 段 __rel_dyn 段
    341. here:
    342. /*
    343. * now relocate vectors
    344. */

    345.         bl        relocate_vectors                                                         relocate_vectors          6666          relocate 中断向量,并设置 VBAR

    346. /* Set up final (full) environment */

    347.         bl        c_runtime_cpu_setup        /* we still call old routine here */                 c_runtime_cpu_setup       7777          关闭 I-cache 后返回
    348. #endif
    349. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FRAMEWORK)
    350. # ifdef CONFIG_SPL_BUILD
    351.         /* Use a DRAM stack for the rest of SPL, if requested */
    352.         bl        spl_relocate_stack_gd                                                    spl_relocate_stack_gd                   relocate 堆空间和 gd
    353.         cmp        r0, #0
    354.         movne        sp, r0
    355.         movne        r9, r0
    356. # endif
    357.         ldr        r0, =__bss_start        /* this is auto-relocated! */

    358. #ifdef CONFIG_USE_ARCH_MEMSET
    359.         ldr        r3, =__bss_end                /* this is auto-relocated! */
    360.         mov        r1, #0x00000000                /* prepare zero to clear BSS */                      清空 bss 空间

    361.         subs        r2, r3, r0                /* r2 = memset len */
    362.         bl        memset
    363. #else
    364.         ldr        r1, =__bss_end                /* this is auto-relocated! */
    365.         mov        r2, #0x00000000                /* prepare zero to clear BSS */

    366. clbss_l:cmp        r0, r1                        /* while not at end of BSS */
    367. #if defined(CONFIG_CPU_V7M)
    368.         itt        lo
    369. #endif
    370.         strlo        r2, [r0]                /* clear 32-bit BSS word */
    371.         addlo        r0, r0, #4                /* move to next */
    372.         blo        clbss_l
    373. #endif

    374. #if ! defined(CONFIG_SPL_BUILD)
    375.         bl coloured_LED_init                                                         coloured_LED_init                       板上初始化指示灯
    376.         bl red_led_on                                                                red_led_on
    377. #endif
    378.         /* call board_init_r(gd_t *id, ulong dest_addr) */
    379.         mov     r0, r9                  /* gd_t */
    380.         ldr        r1, [r9, #GD_RELOCADDR]        /* dest_addr */
    381.         /* call board_init_r */
    382. #if defined(CONFIG_SYS_THUMB_BUILD)
    383.         ldr        lr, =board_init_r        /* this is auto-relocated! */
    384.         bx        lr
    385. #else
    386.         ldr        pc, =board_init_r        /* this is auto-relocated! */                       board_init_r             8888            再次调用 board_init_r(new gd, gd->relocaddr)
    387. #endif
    388.         /* we should not return here. */
    389. #endif

    390. ENDPROC(_main)

    391. # ======================================================================================================================================================
    392. # ==============================     1111
    393. (arch\arm\include\asm\arch-mx6\imx-regs.h)
    394. (include\configs\mys_imx6ull.h)
    395. CONFIG_SYS_INIT_SP_ADDR="(CONFIG_SYS_INIT_RAM_ADDR + CONFIG_SYS_INIT_SP_OFFSET)"               IRAM 地址及大小,芯片级
    396. CONFIG_SYS_INIT_RAM_ADDR="IRAM_BASE_ADDR"
    397. CONFIG_SYS_INIT_SP_OFFSET="(CONFIG_SYS_INIT_RAM_SIZE - GENERATED_GBL_DATA_SIZE)"
    398. CONFIG_SYS_INIT_RAM_SIZE="IRAM_SIZE"

    399. # ==============================     2222
    400. (common\init\board_init.c)
    401. ulong board_init_f_alloc_reserve(ulong top)
    402. {
    403.         /* Reserve early malloc arena */
    404. #if defined(CONFIG_SYS_MALLOC_F)
    405.         top -= CONFIG_SYS_MALLOC_F_LEN;                                                            预留堆空间
    406. #endif
    407.         /* LAST : reserve GD (rounded up to a multiple of 16 bytes) */
    408.         top = rounddown(top-sizeof(struct global_data), 16);                                       预留 global_data 空间,并且对齐

    409.         return top;
    410. }

    411. # ==============================     3333
    412. void board_init_f_init_reserve(ulong base)
    413. {
    414.         struct global_data *gd_ptr;
    415. #ifndef _USE_MEMCPY
    416.         int *ptr;
    417. #endif

    418.         /*
    419.          * clear GD entirely and set it up.
    420.          * Use gd_ptr, as gd may not be properly set yet.
    421.          */

    422.         gd_ptr = (struct global_data *)base;
    423.         /* zero the area */
    424. #ifdef _USE_MEMCPY                                                                            清零 gdata 空间
    425.         memset(gd_ptr, '\0', sizeof(*gd));
    426. #else
    427.         for (ptr = (int *)gd_ptr; ptr < (int *)(gd_ptr + 1); )
    428.                 *ptr++ = 0;
    429. #endif
    430.         /* set GD unless architecture did it already */
    431. #if !defined(CONFIG_ARM)
    432.         arch_setup_gd(gd_ptr);
    433. #endif
    434.         /* next alloc will be higher by one GD plus 16-byte alignment */
    435.         base += roundup(sizeof(struct global_data), 16);                                         对齐,设置堆指针

    436.         /*
    437.          * record early malloc arena start.
    438.          * Use gd as it is now properly set for all architectures.
    439.          */

    440. #if defined(CONFIG_SYS_MALLOC_F)
    441.         /* go down one 'early malloc arena' */
    442.         gd->malloc_base = base;                                                                  
    443.         /* next alloc will be higher by one 'early malloc arena' size */
    444.         base += CONFIG_SYS_MALLOC_F_LEN;
    445. #endif
    446. }

    447. # ==============================     4444
    448. (uboot\common\board_f.c)
    449. void board_init_f(ulong boot_flags)
    450. {
    451. #ifdef CONFIG_SYS_GENERIC_GLOBAL_DATA
    452.         /*
    453.          * For some archtectures, global data is initialized and used before
    454.          * calling this function. The data should be preserved. For others,
    455.          * CONFIG_SYS_GENERIC_GLOBAL_DATA should be defined and use the stack
    456.          * here to host global data until relocation.
    457.          */
    458.         gd_t data;

    459.         gd = &data;

    460.         /*
    461.          * Clear global data before it is accessed at debug print
    462.          * in initcall_run_list. Otherwise the debug print probably
    463.          * get the wrong vaule of gd->have_console.
    464.          */
    465.         zero_global_data();                                                       清零 global_data 内存
    466. #endif

    467.         gd->flags = boot_flags;
    468.         gd->have_console = 0;                                                     还没有 console 可用  /  gd->have_console = 0;

    469.         if (initcall_run_list(init_sequence_f))                                   初始化 init_sequence_f
    470.                 hang();

    471. #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
    472.                 !defined(CONFIG_EFI_APP)
    473.         /* NOTREACHED - jump_to_copy() does not return */
    474.         hang();
    475. #endif
    476. }
    477. // -----------------------------------
    478. (uboot\common\board_f.c)
    479. static init_fnc_t init_sequence_f[] = {
    480. #ifdef CONFIG_SANDBOX
    481.         setup_ram_buf,
    482. #endif
    483.         setup_mon_len,                                                            设置 gd->mon_len 的值 / = (ulong)&__bss_end - (ulong)_start;
    484. #ifdef CONFIG_OF_CONTROL
    485.         fdtdec_setup,
    486. #endif
    487. #ifdef CONFIG_TRACE
    488.         trace_early_init,
    489. #endif
    490.         initf_malloc,                                                             设置堆指针和堆大小: gd->malloc_limit = CONFIG_SYS_MALLOC_F_LEN;    gd->malloc_ptr = 0;
    491.         initf_console_record,
    492. #if defined(CONFIG_MPC85xx) || defined(CONFIG_MPC86xx)
    493.         /* TODO: can this go into arch_cpu_init()? */
    494.         probecpu,
    495. #endif
    496. #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
    497.         x86_fsp_init,
    498. #endif
    499.         arch_cpu_init,                /* basic arch cpu dependent setup */                         
    500.                                                                                      init_aips();              // aips 寄存器 reset(恢复到开机默认值)
    501.                                                                                      clear_mmdc_ch_mask        // 清除 mmdc/DDR ch mask 位
    502.                                                                                      init_bandgap              // 初始化基准频率/bandgap
    503.                                                                                      imx_set_wdog_powerdown    // 关闭 wdog
    504.                                                                                      mxs_dma_init              // apbh dma 初始化
    505.                                                                                      init_src                  // 初始化 src,热重启       
    506.                                                                                                                                                          
    507.         initf_dm,                                                                dm 初始化
    508.         arch_cpu_init_dm,                                                        
    509.         mark_bootstage,                /* need timer, go after init dm */                   设置 bootstage  "board_init_f"
    510. #if defined(CONFIG_BOARD_EARLY_INIT_F)
    511.         board_early_init_f,                                                      初始化 uart pin 脚
    512. #endif
    513.         /* TODO: can any of this go into arch_cpu_init()? */
    514. #if defined(CONFIG_PPC) && !defined(CONFIG_8xx_CPUCLK_DEFAULT)
    515.         get_clocks,                /* get CPU and bus clocks (etc.) */
    516. #if defined(CONFIG_TQM8xxL) && !defined(CONFIG_TQM866M) \
    517.                 && !defined(CONFIG_TQM885D)
    518.         adjust_sdram_tbs_8xx,
    519. #endif
    520.         /* TODO: can we rename this to timer_init()? */
    521.         init_timebase,
    522. #endif
    523. #if defined(CONFIG_ARM) || defined(CONFIG_MIPS) || \
    524.                 defined(CONFIG_BLACKFIN) || defined(CONFIG_NDS32) || \
    525.                 defined(CONFIG_SPARC)
    526.         timer_init,                /* initialize timer */                                   初始化 GPT1,并启用,设置  gd->arch.tbl = __raw_readl(&cur_gpt->counter);  gd->arch.tbu = 0;
    527. #endif
    528. #ifdef CONFIG_SYS_ALLOC_DPRAM
    529. #if !defined(CONFIG_CPM2)
    530.         dpram_init,
    531. #endif
    532. #endif
    533. #if defined(CONFIG_BOARD_POSTCLK_INIT)
    534.         board_postclk_init,                                                      设置 ldo 电压
    535. #endif
    536. #if defined(CONFIG_SYS_FSL_CLK) || defined(CONFIG_M68K)
    537.         get_clocks,                                                              设置 SD 时钟频率  /  gd->arch.sdhc_clk
    538. #endif
    539.         env_init,                /* initialize environment */                             设置 env 地址和有效性 /  gd->env_addr        = (ulong)&default_environment[0];  gd->env_valid        = 1;
    540. #if defined(CONFIG_8xx_CPUCLK_DEFAULT)
    541.         /* get CPU and bus clocks according to the environment variable */
    542.         get_clocks_866,
    543.         /* adjust sdram refresh rate according to the new clock */
    544.         sdram_adjust_866,
    545.         init_timebase,
    546. #endif
    547.         init_baud_rate,                /* initialze baudrate settings */                    设置串口波特率,(从环境变量获取)  /  gd->baudrate
    548.         serial_init,                /* serial communications setup */                    调用驱动,串口初始化,并添加 flag  /  gd->flag |= GD_FLG_SERIAL_READY
    549.         console_init_f,                /* stage 1 init of console */                        console 已经可用,修改 flag,并输出信息  /  gd->have_console = 1; gd->flags |= GD_FLG_SILENT;  
    550. #ifdef CONFIG_SANDBOX
    551.         sandbox_early_getopt_check,
    552. #endif
    553. #ifdef CONFIG_OF_CONTROL
    554.         fdtdec_prepare_fdt,
    555. #endif
    556.         display_options,        /* say that we are here */                           输出 "U-Boot 2016.03 (Jun 05 2019 - 01:45:20 +0800)"
    557.         display_text_info,        /* show debugging info if required */                输出 debug 信息(需要对应 DEBUG 宏)
    558. #if defined(CONFIG_MPC8260)
    559.         prt_8260_rsr,
    560.         prt_8260_clks,
    561. #endif /* CONFIG_MPC8260 */
    562. #if defined(CONFIG_MPC83xx)
    563.         prt_83xx_rsr,
    564. #endif
    565. #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
    566.         checkcpu,
    567. #endif
    568.         print_cpuinfo,                /* display cpu info (and speed) */                   输出 cpu 信息
    569. #if defined(CONFIG_MPC5xxx)
    570.         prt_mpc5xxx_clks,
    571. #endif /* CONFIG_MPC5xxx */
    572. #if defined(CONFIG_DISPLAY_BOARDINFO)
    573.         show_board_info,                                                         输出 board 信息,调用 checkboard()
    574. #endif
    575.         INIT_FUNC_WATCHDOG_INIT                                                  初始化 watchdog
    576. #if defined(CONFIG_MISC_INIT_F)
    577.         misc_init_f,
    578. #endif
    579.         INIT_FUNC_WATCHDOG_RESET
    580. #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C)
    581.         init_func_i2c,                                                           初始化 iic 控制器
    582. #endif
    583. #if defined(CONFIG_HARD_SPI)
    584.         init_func_spi,
    585. #endif
    586.         announce_dram_init,                                                      输出 DRAM
    587.         /* TODO: unify all these dram functions? */
    588. #if defined(CONFIG_ARM) || defined(CONFIG_X86) || defined(CONFIG_NDS32) || \
    589.                 defined(CONFIG_MICROBLAZE) || defined(CONFIG_AVR32)
    590.         dram_init,                /* configure available RAM banks */                      初始化 DRAM (测试 DRAM 大小,获取 gd->ram_size)  (DCD 数据初始化 DRAM,什么原理,ROM 如何实现的) =============== get help form NXP
    591. #endif
    592. #if defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(CONFIG_M68K)
    593.         init_func_ram,
    594. #endif
    595. #ifdef CONFIG_POST
    596.         post_init_f,
    597. #endif
    598.         INIT_FUNC_WATCHDOG_RESET
    599. #if defined(CONFIG_SYS_DRAM_TEST)
    600.         testdram,
    601. #endif /* CONFIG_SYS_DRAM_TEST */
    602.         INIT_FUNC_WATCHDOG_RESET

    603. #ifdef CONFIG_POST
    604.         init_post,
    605. #endif
    606.         INIT_FUNC_WATCHDOG_RESET
    607.         /*
    608.          * Now that we have DRAM mapped and working, we can
    609.          * relocate the code and continue running from DRAM.
    610.          *
    611.          * Reserve memory at end of RAM for (top down in that order):
    612.          *  - area that won't get touched by U-Boot and Linux (optional)
    613.          *  - kernel log buffer
    614.          *  - protected RAM
    615.          *  - LCD framebuffer
    616.          *  - monitor code
    617.          *  - board info struct
    618.          */
    619.         setup_dest_addr,                                                         设置 gd->ram_size,gd->ram_top,gd->relocaddr
    620. #if defined(CONFIG_BLACKFIN)
    621.         /* Blackfin u-boot monitor should be on top of the ram */
    622.         reserve_uboot,
    623. #endif
    624. #if defined(CONFIG_SPARC)
    625.         reserve_prom,
    626. #endif
    627. #if defined(CONFIG_LOGBUFFER) && !defined(CONFIG_ALT_LB_ADDR)
    628.         reserve_logbuffer,                                                       保留 logbuffer 内存
    629. #endif
    630. #ifdef CONFIG_PRAM
    631.         reserve_pram,                                                            保留 pram 内存
    632. #endif
    633.         reserve_round_4k,
    634. #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF)) && \
    635.                 defined(CONFIG_ARM)
    636.         reserve_mmu,                                                             保留 tlb 内存(gd->arch.tlb_size, gd->arch.tlb_addr)
    637. #endif
    638. #ifdef CONFIG_DM_VIDEO
    639.         reserve_video,                                                           保留 video 内存 (gd->video_top ,gd->video_bottom)
    640. #else
    641. # ifdef CONFIG_LCD
    642.         reserve_lcd,                                                             保留 gd->fb_base 内存
    643. # endif
    644.         /* TODO: Why the dependency on CONFIG_8xx? */
    645. # if defined(CONFIG_VIDEO) && (!defined(CONFIG_PPC) || defined(CONFIG_8xx)) && \
    646.                 !defined(CONFIG_ARM) && !defined(CONFIG_X86) && \
    647.                 !defined(CONFIG_BLACKFIN) && !defined(CONFIG_M68K)
    648.         reserve_legacy_video,
    649. # endif
    650. #endif /* CONFIG_DM_VIDEO */
    651.         reserve_trace,                                                          保留 gd->trace_buff 内存
    652. #if !defined(CONFIG_BLACKFIN)
    653.         reserve_uboot,                                                          保留 uboot 内存
    654. #endif
    655. #ifndef CONFIG_SPL_BUILD
    656.         reserve_malloc,                                                         保留 堆 空间
    657.         reserve_board,                                                          保留 board 内存(gd->bd)
    658. #endif
    659.         setup_machine,                                                          设置 machine type (gd->bd->bi_arch_number)
    660.         reserve_global_data,                                                    保留 global_data 内存 (gd->new_gd)
    661.         reserve_fdt,                                                            保留 fdt 内存
    662.         reserve_arch,
    663.         reserve_stacks,                                                         保留 gd->irq_sp
    664.         setup_dram_config,                                                      设置 gd->bd->bi_dram[0].start,gd->bd->bi_dram[0].size  
    665.         show_dram_config,                                                       输出 dram bank 信息
    666. #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_MIPS)
    667.         setup_board_part1,
    668. #endif
    669. #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
    670.         INIT_FUNC_WATCHDOG_RESET
    671.         setup_board_part2,
    672. #endif
    673.         display_new_sp,                                                         输出 new_sp 信息
    674. #ifdef CONFIG_SYS_EXTBDINFO
    675.         setup_board_extra,
    676. #endif
    677.         INIT_FUNC_WATCHDOG_RESET                                                看门狗复位
    678.         reloc_fdt,                                                              relocate fdt
    679.         setup_reloc,                                                            设置新 gd->reloc_off,并且复制 gd 到 gd->new_gd
    680. #if defined(CONFIG_X86) || defined(CONFIG_ARC)
    681.         copy_uboot_to_ram,
    682.         clear_bss,
    683.         do_elf_reloc_fixups,
    684. #endif
    685. #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX)
    686.         jump_to_copy,
    687. #endif
    688.         NULL,
    689. };

    690. # ==============================     5555
    691. (arch\arm\lib\relocate.S)
    692. ENTRY(relocate_code)
    693.         ldr        r1, =__image_copy_start        /* r1 <- SRC &__image_copy_start */             relocate __image_copy 段
    694.         subs        r4, r0, r1                /* r4 <- relocation offset */
    695.         beq        relocate_done                /* skip relocation */
    696.         ldr        r2, =__image_copy_end        /* r2 <- SRC &__image_copy_end */

    697. copy_loop:
    698.         ldmia        r1!, {r10-r11}                /* copy from source address [r1]    */
    699.         stmia        r0!, {r10-r11}                /* copy to   target address [r0]    */
    700.         cmp        r1, r2                        /* until source end address [r2]    */
    701.         blo        copy_loop

    702.         /*
    703.          * fix .rel.dyn relocations
    704.          */
    705.         ldr        r2, =__rel_dyn_start        /* r2 <- SRC &__rel_dyn_start */                relocate __rel_dyn 段
    706.         ldr        r3, =__rel_dyn_end        /* r3 <- SRC &__rel_dyn_end */
    707. fixloop:
    708.         ldmia        r2!, {r0-r1}                /* (r0,r1) <- (SRC location,fixup) */
    709.         and        r1, r1, #0xff
    710.         cmp        r1, #23                        /* relative fixup? */
    711.         bne        fixnext

    712.         /* relative fix: increase location by offset */
    713.         add        r0, r0, r4
    714.         ldr        r1, [r0]
    715.         add        r1, r1, r4
    716.         str        r1, [r0]
    717. fixnext:
    718.         cmp        r2, r3
    719.         blo        fixloop

    720. relocate_done:

    721. #ifdef __XSCALE__
    722.         /*
    723.          * On xscale, icache must be invalidated and write buffers drained,
    724.          * even with cache disabled - 4.2.7 of xscale core developer's manual
    725.          */
    726.         mcr        p15, 0, r0, c7, c7, 0        /* invalidate icache */
    727.         mcr        p15, 0, r0, c7, c10, 4        /* drain write buffer */
    728. #endif

    729.         /* ARMv4- don't know bx lr but the assembler fails to see that */

    730. #ifdef __ARM_ARCH_4__
    731.         mov        pc, lr
    732. #else
    733.         bx        lr
    734. #endif

    735. ENDPROC(relocate_code)

    736. # ==============================     6666
    737. ENTRY(relocate_vectors)

    738. #ifdef CONFIG_CPU_V7M
    739.         /*
    740.          * On ARMv7-M we only have to write the new vector address
    741.          * to VTOR register.
    742.          */
    743.         ldr        r0, [r9, #GD_RELOCADDR]        /* r0 = gd->relocaddr */
    744.         ldr        r1, =V7M_SCB_BASE
    745.         str        r0, [r1, V7M_SCB_VTOR]
    746. #else
    747. #ifdef CONFIG_HAS_VBAR
    748.         /*
    749.          * If the ARM processor has the security extensions,
    750.          * use VBAR to relocate the exception vectors.
    751.          */
    752.         ldr        r0, [r9, #GD_RELOCADDR]        /* r0 = gd->relocaddr */                            relocate 中断向量,并设置 VBAR
    753.         mcr     p15, 0, r0, c12, c0, 0  /* Set VBAR */
    754. #else
    755.         /*
    756.          * Copy the relocated exception vectors to the
    757.          * correct address
    758.          * CP15 c1 V bit gives us the location of the vectors:
    759.          * 0x00000000 or 0xFFFF0000.
    760.          */
    761.         ldr        r0, [r9, #GD_RELOCADDR]        /* r0 = gd->relocaddr */
    762.         mrc        p15, 0, r2, c1, c0, 0        /* V bit (bit[13]) in CP15 c1 */
    763.         ands        r2, r2, #(1 << 13)
    764.         ldreq        r1, =0x00000000                /* If V=0 */
    765.         ldrne        r1, =0xFFFF0000                /* If V=1 */
    766.         ldmia        r0!, {r2-r8,r10}
    767.         stmia        r1!, {r2-r8,r10}
    768.         ldmia        r0!, {r2-r8,r10}
    769.         stmia        r1!, {r2-r8,r10}
    770. #endif
    771. #endif
    772.         bx        lr

    773. ENDPROC(relocate_vectors)

    774. # ==============================     7777
    775. ENTRY(c_runtime_cpu_setup)
    776. /*
    777. * If I-cache is enabled invalidate it
    778. */
    779. #ifndef CONFIG_SYS_ICACHE_OFF
    780.         mcr        p15, 0, r0, c7, c5, 0        @ invalidate icache
    781.         mcr     p15, 0, r0, c7, c10, 4        @ DSB
    782.         mcr     p15, 0, r0, c7, c5, 4        @ ISB
    783. #endif

    784.         bx        lr

    785. ENDPROC(c_runtime_cpu_setup)



    786. # ==============================      8888
    787. init_fnc_t init_sequence_r[] = {
    788.         initr_trace,
    789.         initr_reloc,                                                                完全 relocate 代码 / gd->flags |= GD_FLG_RELOC | GD_FLG_FULL_MALLOC_INIT;
    790.         /* TODO: could x86/PPC have this also perhaps? */
    791. #ifdef CONFIG_ARM
    792.         initr_caches,                                                               关闭 dcache,再打开 dcache,使能 OCRAM,RAM
    793.         /* Note: For Freescale LS2 SoCs, new MMU table is created in DDR.
    794.          *         A temporary mapping of IFC high region is since removed,
    795.          *         so environmental variables in NOR flash is not availble
    796.          *         until board_init() is called below to remap IFC to high
    797.          *         region.
    798.          */
    799. #endif
    800.         initr_reloc_global_data,                                                    monitor_flash_len = _end - __image_copy_start;
    801. #if defined(CONFIG_SYS_INIT_RAM_LOCK) && defined(CONFIG_E500)
    802.         initr_unlock_ram_in_cache,
    803. #endif
    804.         initr_barrier,
    805.         initr_malloc,                                                               malloc_start = gd->relocaddr - TOTAL_MALLOC_LEN;
    806.         initr_console_record,
    807. #ifdef CONFIG_SYS_NONCACHED_MEMORY
    808.         initr_noncached,                                                            预留 mmu section
    809. #endif
    810.         bootstage_relocate,                                                         relocate bootstage 分配空间,添加名字
    811. #ifdef CONFIG_DM
    812.         initr_dm,                                                                   保留旧 dm_root, 初始化新 dm_root, gd->dm_root_f = gd->dm_root;gd->dm_root = NULL;
    813. #endif
    814.         initr_bootstage,                                                            添加 bootstage records
    815. #if defined(CONFIG_ARM) || defined(CONFIG_NDS32)
    816.         board_init,        /* Setup chipselects */
    817. #endif
    818.         /*
    819.          * TODO: printing of the clock inforamtion of the board is now
    820.          * implemented as part of bdinfo command. Currently only support for
    821.          * davinci SOC's is added. Remove this check once all the board
    822.          * implement this.
    823.          */
    824. #ifdef CONFIG_CLOCKS
    825.         set_cpu_clk_info, /* Setup clock information */                     
    826. #endif
    827.         stdio_init_tables,                                                          初始化(stdio_dev) devs->list
    828.         initr_serial,                                                               注册串口驱动
    829.         initr_announce,                                                             打印信息 "Now running in RAM - U-Boot at: %08lx\n"
    830.         INIT_FUNC_WATCHDOG_RESET
    831. #ifdef CONFIG_NEEDS_MANUAL_RELOC
    832.         initr_manual_reloc_cmdtable,                                                reloc cmdtable,reloc 之前不做
    833. #endif
    834. #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
    835.         initr_trap,
    836. #endif
    837. #ifdef CONFIG_ADDR_MAP
    838.         initr_addr_map,
    839. #endif
    840. #if defined(CONFIG_BOARD_EARLY_INIT_R)
    841.         board_early_init_r,
    842. #endif
    843.         INIT_FUNC_WATCHDOG_RESET
    844. #ifdef CONFIG_LOGBUFFER
    845.         initr_logbuffer,                                                           sdram 最顶部设置 logbuffer
    846. #endif
    847. #ifdef CONFIG_POST
    848.         initr_post_backlog,                                                        post_backlog 测试
    849. #endif
    850.         INIT_FUNC_WATCHDOG_RESET
    851. #ifdef CONFIG_SYS_DELAYED_ICACHE
    852.         initr_icache_enable,
    853. #endif
    854. #if defined(CONFIG_PCI) && defined(CONFIG_SYS_EARLY_PCI_INIT)
    855.         /*
    856.          * Do early PCI configuration _before_ the flash gets initialised,
    857.          * because PCU ressources are crucial for flash access on some boards.
    858.          */
    859.         initr_pci,
    860. #endif
    861. #ifdef CONFIG_WINBOND_83C553
    862.         initr_w83c553f,
    863. #endif
    864. #ifdef CONFIG_ARCH_EARLY_INIT_R
    865.         arch_early_init_r,
    866. #endif
    867.         power_init_board,                                                              pmic 初始化
    868. #ifndef CONFIG_SYS_NO_FLASH
    869.         initr_flash,                                                                   初始化 flash ,设置 gd->bd 的 bi_flashstart, bi_flashsize, bi_flashoffset
    870. #endif
    871.         INIT_FUNC_WATCHDOG_RESET
    872. #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_X86) || \
    873.         defined(CONFIG_SPARC)
    874.         /* initialize higher level parts of CPU like time base and timers */
    875.         cpu_init_r,
    876. #endif
    877. #ifdef CONFIG_PPC
    878.         initr_spi,
    879. #endif
    880. #ifdef CONFIG_CMD_NAND
    881.         initr_nand,                                                                    初始化 nand
    882. #endif
    883. #ifdef CONFIG_CMD_ONENAND
    884.         initr_onenand,
    885. #endif
    886. #ifdef CONFIG_GENERIC_MMC
    887.         initr_mmc,                                                                     初始化 mmc
    888. #endif
    889. #ifdef CONFIG_HAS_DATAFLASH
    890.         initr_dataflash,
    891. #endif
    892.         initr_env,                                                                     初始化 env
    893. #ifdef CONFIG_SYS_BOOTPARAMS_LEN
    894.         initr_malloc_bootparams,                                                       分配 bootparams 内存,赋值给 gd->bd->bi_boot_params
    895. #endif
    896.         INIT_FUNC_WATCHDOG_RESET
    897.         initr_secondary_cpu,
    898. #if defined(CONFIG_ID_EEPROM) || defined(CONFIG_SYS_I2C_MAC_OFFSET)
    899.         mac_read_from_eeprom,                                                          读取 eeprom 中的 mac 地址
    900. #endif
    901.         INIT_FUNC_WATCHDOG_RESET
    902. #if defined(CONFIG_PCI) && !defined(CONFIG_SYS_EARLY_PCI_INIT)
    903.         /*
    904.          * Do pci configuration
    905.          */
    906.         initr_pci,
    907. #endif
    908.         stdio_add_devices,                                                             设备初始化,调用驱动/common
    909.                                                                                    keyboard 初始化, IIC 初始化,LCD 初始化,logbuff 驱动,串口驱动,usb tty 驱动,nc/output 驱动,jtag 驱动注册
    910.                                                                                                                                                                   
    911.         initr_jumptable,                                                               分配内存,赋值给 gd->jt
    912. #ifdef CONFIG_API
    913.         initr_api,                                                                     api 驱动初始化
    914. #endif
    915.         console_init_r,                /* fully init console as a device */                       初始化控制台 console
    916. #ifdef CONFIG_DISPLAY_BOARDINFO_LATE
    917.         show_board_info,
    918. #endif
    919. #ifdef CONFIG_ARCH_MISC_INIT
    920.         arch_misc_init,                /* miscellaneous arch-dependent init */
    921. #endif
    922. #ifdef CONFIG_MISC_INIT_R
    923.         misc_init_r,                /* miscellaneous platform-dependent init */
    924. #endif
    925.         INIT_FUNC_WATCHDOG_RESET
    926. #ifdef CONFIG_CMD_KGDB
    927.         initr_kgdb,                                                                     kgdb 初始化
    928. #endif
    929.         interrupt_init,                                                                  设置中断栈指针
    930. #if defined(CONFIG_ARM) || defined(CONFIG_AVR32)
    931.         initr_enable_interrupts,                                                          开启中断
    932. #endif
    933. #if defined(CONFIG_MICROBLAZE) || defined(CONFIG_AVR32) || defined(CONFIG_M68K)
    934.         timer_init,                /* initialize timer */
    935. #endif
    936. #if defined(CONFIG_STATUS_LED)
    937.         initr_status_led,                                                                 led 初始化
    938. #endif
    939.         /* PPC has a udelay(20) here dating from 2002. Why? */
    940. #ifdef CONFIG_CMD_NET
    941.         initr_ethaddr,                                                                    从环境变量获取 ethaddr 设置 gd->bd->bi_enetaddr
    942. #endif
    943. #ifdef CONFIG_BOARD_LATE_INIT
    944.         board_late_init,                                                                  设置环境变量 board_name, board_ver, 设置环境变量 mmcdev,mmcroot
    945. #endif
    946. #ifdef CONFIG_FSL_FASTBOOT
    947.         initr_fastboot_setup,
    948. #endif
    949. #if defined(CONFIG_CMD_AMBAPP)
    950.         ambapp_init_reloc,
    951. #if defined(CONFIG_SYS_AMBAPP_PRINT_ON_STARTUP)
    952.         initr_ambapp_print,
    953. #endif
    954. #endif
    955. #ifdef CONFIG_CMD_SCSI
    956.         INIT_FUNC_WATCHDOG_RESET
    957.         initr_scsi,
    958. #endif
    959. #ifdef CONFIG_CMD_DOC
    960.         INIT_FUNC_WATCHDOG_RESET
    961.         initr_doc,
    962. #endif
    963. #ifdef CONFIG_BITBANGMII
    964.         initr_bbmii,                                                                      bbmii 初始化
    965. #endif
    966. #ifdef CONFIG_CMD_NET
    967.         INIT_FUNC_WATCHDOG_RESET
    968.         initr_net,                                                                        eth 初始化
    969. #endif
    970. #ifdef CONFIG_POST
    971.         initr_post,
    972. #endif
    973. #if defined(CONFIG_CMD_PCMCIA) && !defined(CONFIG_CMD_IDE)
    974.         initr_pcmcia,
    975. #endif
    976. #if defined(CONFIG_CMD_IDE)
    977.         initr_ide,
    978. #endif
    979. #ifdef CONFIG_LAST_STAGE_INIT
    980.         INIT_FUNC_WATCHDOG_RESET
    981.         /*
    982.          * Some parts can be only initialized if all others (like
    983.          * Interrupts) are up and running (i.e. the PC-style ISA
    984.          * keyboard).
    985.          */
    986.         last_stage_init,
    987. #endif
    988. #ifdef CONFIG_CMD_BEDBUG
    989.         INIT_FUNC_WATCHDOG_RESET
    990.         initr_bedbug,
    991. #endif
    992. #if defined(CONFIG_PRAM) || defined(CONFIG_LOGBUFFER)
    993.         initr_mem,
    994. #endif
    995. #ifdef CONFIG_PS2KBD
    996.         initr_kbd,                                                                      kbd 初始化
    997. #endif
    998. #if defined(CONFIG_SPARC)
    999.         prom_init,
    1000. #endif
    1001. #ifdef CONFIG_FSL_FASTBOOT
    1002.         initr_check_fastboot,                                                      check_fastboot
    1003. #endif
    1004.         run_main_loop,                                                             主循环
    1005. };

    1006. # -------------------------------
    1007. static int run_main_loop(void)
    1008. {
    1009. #ifdef CONFIG_SANDBOX
    1010.         sandbox_main_loop_init();
    1011. #endif
    1012.         /* main_loop() can return to retry autoboot, if so just run it again */
    1013.         for (;;)
    1014.                 main_loop();                                                           // ========================================================== 重点分析
    1015.         return 0;
    1016. }

    1017. # -------------------------------
    1018. void main_loop(void)
    1019. {
    1020.         const char *s;

    1021.         bootstage_mark_name(BOOTSTAGE_ID_MAIN_LOOP, "main_loop");

    1022. #ifndef CONFIG_SYS_GENERIC_BOARD
    1023.         puts("Warning: Your board does not use generic board. Please read\n");
    1024.         puts("doc/README.generic-board and take action. Boards not\n");
    1025.         puts("upgraded by the late 2014 may break or be removed.\n");
    1026. #endif

    1027. #ifdef CONFIG_VERSION_VARIABLE
    1028.         setenv("ver", version_string);  /* set version variable */
    1029. #endif /* CONFIG_VERSION_VARIABLE */

    1030.         cli_init();

    1031.         run_preboot_environment_command();                                          运行 preboot 命令 ( preboot 命令来自于env ):
    1032.                                                                                     调用 run_command_list
    1033.                                                                                     cli_simple_run_command_list
    1034.                                                                                     cli_simple_run_command
    1035.                                                                                     cmd_process
    1036. #if defined(CONFIG_UPDATE_TFTP)
    1037.         update_tftp(0UL, NULL, NULL);
    1038. #endif /* CONFIG_UPDATE_TFTP */

    1039.         s = bootdelay_process();                                                    获取 bootdelay 延时时间(环境变量/配置定义),并且保存在 stored_bootdelay
    1040.                                                                                     获取 bootcmd 保存在 s 中
    1041.         if (cli_process_fdt(&s))                                                    尝试从 fdt 中获取 boootcmd 变量,和 secure_boot 变量
    1042.                 cli_secure_boot_cmd(s);                                                 如果有 secure_boot 变量,优先从 secure_boot 启动

    1043.         autoboot_command(s);                                                        运行 s/bootcmd 命令

    1044.         cli_loop();
    1045. }

    1046. // ----------------------
    1047. autoboot_command
    1048.     abortboot                                                                   打印"Hit any key to stop autoboot: %2d ",延时处理,
    1049.             abortboot_normal                                                            如果有按键,放弃自动启动,如果没有按键,延时。最后都返回 abort 状态
    1050.                                                                                             根据 abort 状态,要不要继续往下运行。放弃 autoboot 之后返回到 cli_loop。
    1051.     run_command_list                                                            执行 bootcmd 命令,bootcmd 是个命令列表
    1052.         cli_simple_run_command_list                                                        循环执行 bootcmd 命令列表中的每个命令
    1053.                     cli_simple_run_command                                                  执行单个命令
    1054.                 cmd_process(flag, argc, argv, &repeatable, NULL)                    
    1055.                                     cmd_call(cmdtp, flag, argc, argv);
    1056.                                             (cmdtp->cmd)(cmdtp, flag, argc, argv)
    1057.                                                
    1058. // ----------------------
    1059. cli_loop
    1060.     cli_simple_loop                                                             陷入死循环中,不断读取 console buffer 然后执行,同样是调用 cli_simple_run_command -> cmd_process
    1061.             cli_simple_run_command                                                  
    1062.                     cmd_process



    复制代码



    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-4-20 07:19 , Processed in 0.106872 second(s), 14 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.