uboot misc


uboot misc

global_data struct成员含义

mon_len
uboot image size,uboot image里包含.text,.rodata, .data, .u_boot_list, .bss等section,这些section size之和即是mon_len

static int setup_mon_len(void)
{
#if defined(__ARM__) || defined(__MICROBLAZE__)
    gd->mon_len = (ulong)&__bss_end - (ulong)_start;

ram_base/ram_top/ram_size
表示uboot可以访问的DRAM的地址范围,这个范围即是(ram_base, ram_top)。ram_size即是uboot可以访问DRAM范围的size,在dram_init()里设置

setup_dest_addr()

#ifdef CONFIG_SYS_SDRAM_BASE
    gd->ram_base = CONFIG_SYS_SDRAM_BASE;
#endif
    gd->ram_top = gd->ram_base + get_effective_memsize();
    gd->ram_top = board_get_usable_ram_top(gd->mon_len);

uboot下如何查看内存里的数据

使用md工具

md.b $address $count (从地址$address处显示$count个字节的数据,b=byte,8位)

md.w $address $count (从地址$address处显示$count个的数据,w=word,16位)

md.l $address $count (从地址$address处显示$count个双字的数据,32位,默认读取单位为32位)

uboot memory layout

     "hidden" RAM
     kernel log buffer (optional)
     "protected" RAM (optional)
     video and/or LCD framebuffer (optional)
     U-Boot
     heap (for malloc)
     board info structure
     global data structure
     stack
from: https://lists.denx.de/pipermail/u-boot/2013-December/169910.html

layout diagram

https://icode.best/i/08517234442699

uboot命令学习笔记

 https://blog.csdn.net/weixin_43471255/article/details/112299968

 

调用board_init_f_alloc_reserve,这个函数将返回一个指向global_data struct的pointer,这个值保存在x18,同时也将是sp的值,向上是global_data struct,向下是stack

66 ENTRY(_main)
83     bl    board_init_f_alloc_reserve
84     mov    sp, x0
85     /* set up gd here, outside any C code */
86     mov    x18, x0
87     bl    board_init_f_init_reserve
88 
89     mov    x0, #0
90     bl    board_init_f
...
92 #if !defined(CONFIG_SPL_BUILD)
93 /*
94  * Set up intermediate environment (new sp and gd) and call
95  * relocate_code(addr_moni). Trick here is that we'll return
96  * 'here' but relocated.
97  */
98     ldr    x0, [x18, #GD_START_ADDR_SP]    /* x0 <- gd->start_addr_sp */
99     bic    sp, x0, #0xf    /* 16-byte alignment for ABI compliance */
100     ldr    x18, [x18, #GD_NEW_GD]        /* x18 <- gd->new_gd */

111     ldr    x9, [x18, #GD_RELOC_OFF]    /* x9 <- gd->reloc_off */
112     add    lr, lr, x9    /* new return address after relocation */
113     ldr    x0, [x18, #GD_RELOCADDR]    /* x0 <- gd->relocaddr */
114     b    relocate_code

在board_init_f后,将gd->new_gd的值读出来保存到x18,这个是relocation的global_data struct的地址,所以还是x18保存了global_data struct的地址

gd指针使用DECLARE_GLOBAL_DATA_PTR来define,在需要使用它的c文件里,使用这个macro来define一个register变量,这个变量的值就是x18的值,而x18保存的就是global_data struct的地址。

如果uboot不是用clang来编译的,__clang__是没有define的,所以是else部分(红色字体部分,ARM64)

#ifdef __clang__
86  
87  #define DECLARE_GLOBAL_DATA_PTR
88  #define gd    get_gd()
89  
90  static inline gd_t *get_gd(void)
91  {
92      gd_t *gd_ptr;
93  
94  #ifdef CONFIG_ARM64
95      /*
96       * Make will already error that reserving x18 is not supported at the
97       * time of writing, clang: error: unknown argument: '-ffixed-x18'
98       */
99      __asm__ volatile("mov %0, x18\n" : "=r" (gd_ptr));
100  #else
101      __asm__ volatile("mov %0, r9\n" : "=r" (gd_ptr));
102  #endif
103  
104      return gd_ptr;
105  }
106  
107  #else
108  
109  #ifdef CONFIG_ARM64
110  #define DECLARE_GLOBAL_DATA_PTR        register volatile gd_t *gd asm ("x18")
111  #else
112  #define DECLARE_GLOBAL_DATA_PTR        register volatile gd_t *gd asm ("r9")
113  #endif
114  #endif