ZYNQ U-Boot 程式碼分析 + Vivado SDK 載入除錯

語言: CN / TW / HK

復位後第一時間在復位向量.

進入後就會跳轉到復位.

進入後並沒開啟LPAE技術,LPAE和x86的36-bit地址異曲同工,可以支援到最大1TB記憶體,這裡沒需要,所以直接過去了,接下來讀取CPSR並儲存到R0,然後讓R0和0x1F進行AND運算,提取出M[4:0]設定處理器工作模式,然後現在R1儲存的是CPSR中的M[4:0],然後判斷是否為Hyp模式(11010),如果不是,就清除並設定R0暫存器儲存的M[4:0]為SVC模式(10011),然後R0再和0xC0或運算,使得FIQ,IRQ位為1,關閉中斷,最後把R0寫回CPSR.

接下來繼續執行,由於我們Z7實際上用的是FSBL載入,所以現在實際上DDR這些外存已經可以用了,空間不會太緊張,所以沒開SPL,那麼就要執行下面的程式碼.這裡目的是讀取CP15(協處理)的C1,並嘗試清除CR_V位,根據ARM定義( 點這裡看 ),他是向量表控制位,設定成0目的是為了重定向向量表,我們記得U-Boot一開始便是中斷向量表,因此也通過R0把中斷向量表送入CP15的C12,即VBAR暫存器.

接下來還要呼叫3個函式,其中cpu_init_cp15是Cache,MMU設定,

而cpu_init_crit實際上是lowlevel_init,而這裡實際就是啟動VFP.

然後進入彙編主函式,這裡有載入CONFIG_SYS_INIT_SP_ADDR到R0,CONFIG_SYS_INIT_SP_ADDR即從片上RAM頂部減去全域性變數和初始化記憶體的位置,比如這次編譯結果中地址在ffff1f30,可以從右上角暫存器看到.為了讓地址對齊,還進入board_init_f_alloc_reserve以便幫助16位元組對齊,所以最後結果ffff1660,將計算到的數值寫入R9,給後面C語言用,UBoot的全域性變數都在gd結構體裡,所以R9便是gd的地址.當然如果是AARCH64應該會儲存在X18暫存器,這個都在arch/arm/include/asm/global_data.h有定義.

剛才說gd是一個超大的全域性變數,他結構有多大呢,也在arch/arm/include/asm/global_data.h定義了.

typedef struct global_data {
	bd_t *bd;
	unsigned long flags;
	unsigned int baudrate;
	unsigned long cpu_clk;		/* CPU clock in Hz!		*/
	unsigned long bus_clk;
	/* We cannot bracket this with CONFIG_PCI due to mpc5xxx */
	unsigned long pci_clk;
	unsigned long mem_clk;
#if defined(CONFIG_LCD) || defined(CONFIG_VIDEO)
	unsigned long fb_base;		/* Base address of framebuffer mem */
#endif
#if defined(CONFIG_POST)
	unsigned long post_log_word;	/* Record POST activities */
	unsigned long post_log_res;	/* success of POST test */
	unsigned long post_init_f_time;	/* When post_init_f started */
#endif
#ifdef CONFIG_BOARD_TYPES
	unsigned long board_type;
#endif
	unsigned long have_console;	/* serial_init() was called */
#if CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
	unsigned long precon_buf_idx;	/* Pre-Console buffer index */
#endif
	unsigned long env_addr;		/* Address  of Environment struct */
	unsigned long env_valid;	/* Environment valid? enum env_valid */

	unsigned long ram_top;		/* Top address of RAM used by U-Boot */
	unsigned long relocaddr;	/* Start address of U-Boot in RAM */
	phys_size_t ram_size;		/* RAM size */
	unsigned long mon_len;		/* monitor len */
	unsigned long irq_sp;		/* irq stack pointer */
	unsigned long start_addr_sp;	/* start_addr_stackpointer */
	unsigned long reloc_off;
	struct global_data *new_gd;	/* relocated global data */

#ifdef CONFIG_DM
	struct udevice	*dm_root;	/* Root instance for Driver Model */
	struct udevice	*dm_root_f;	/* Pre-relocation root instance */
	struct list_head uclass_root;	/* Head of core tree */
#endif
#ifdef CONFIG_TIMER
	struct udevice	*timer;		/* Timer instance for Driver Model */
#endif

	const void *fdt_blob;		/* Our device tree, NULL if none */
	void *new_fdt;			/* Relocated FDT */
	unsigned long fdt_size;		/* Space reserved for relocated FDT */
#ifdef CONFIG_OF_LIVE
	struct device_node *of_root;
#endif
	struct jt_funcs *jt;		/* jump table */
	char env_buf[32];		/* buffer for env_get() before reloc. */
#ifdef CONFIG_TRACE
	void		*trace_buff;	/* The trace buffer */
#endif
#if defined(CONFIG_SYS_I2C)
	int		cur_i2c_bus;	/* current used i2c bus */
#endif
#ifdef CONFIG_SYS_I2C_MXC
	void *srdata[10];
#endif
	unsigned int timebase_h;
	unsigned int timebase_l;
#if CONFIG_VAL(SYS_MALLOC_F_LEN)
	unsigned long malloc_base;	/* base address of early malloc() */
	unsigned long malloc_limit;	/* limit address */
	unsigned long malloc_ptr;	/* current address */
#endif
#ifdef CONFIG_PCI
	struct pci_controller *hose;	/* PCI hose for early use */
	phys_addr_t pci_ram_top;	/* top of region accessible to PCI */
#endif
#ifdef CONFIG_PCI_BOOTDELAY
	int pcidelay_done;
#endif
	struct udevice *cur_serial_dev;	/* current serial device */
	struct arch_global_data arch;	/* architecture-specific data */
#ifdef CONFIG_CONSOLE_RECORD
	struct membuff console_out;	/* console output */
	struct membuff console_in;	/* console input */
#endif
#ifdef CONFIG_DM_VIDEO
	ulong video_top;		/* Top of video frame buffer area */
	ulong video_bottom;		/* Bottom of video frame buffer area */
#endif
#ifdef CONFIG_BOOTSTAGE
	struct bootstage_data *bootstage;	/* Bootstage information */
	struct bootstage_data *new_bootstage;	/* Relocated bootstage info */
#endif
#ifdef CONFIG_LOG
	int log_drop_count;		/* Number of dropped log messages */
	int default_log_level;		/* For devices with no filters */
	struct list_head log_head;	/* List of struct log_device */
#endif
} gd_t;
#endif

接下來就進入board_init_f_init_reserve,主要是初始化C環境程式碼,比如清零gd所在位置,不至於gd一進來就有資料,這很影響的,剛才已經得到的ffff1660,再加上gd的大小是ffff1728,然後做16位元組對齊,就是ffff1730,

從變數可以觀察到gd的malloc_base,early malloc也會從這裡開始,真的一點記憶體也沒浪費的呢.

接著清空R0之後,開始board_init_f,這裡會進行一系列初始化.

從名字能看出來,是執行init_sequence_f列出的內容,這裡就對裡面每一個函式分析就可以知道,大致就初始化一些非常底層的內容,初始化gd,拷貝gd到DDR等等.畢竟還沒完全部署好C環境.

其中把SP(gd->start_addr_sp)設定到R0,從暫存器能看到地址已經是3eb17ec0,這是DDR地址了,再R0做8位元組對齊給SP,由於gd位置變化,所以R9也要更新,所以先把gd->bd賦值給R9,然後再減去GD大小,那麼當前的R9就是新的gd位置,當然地址也在DDR中.然後設定LR為here,這樣執行其他函式後就返回到後面的here標籤位置,然後R0配置為gd->reloc_off,之後lr += r0就相當於重定位後,here也挪到DDR後面,接著r0就要儲存著重定向的地址,然後開始重定向程式碼.

當然除了搬程式碼,還要搬向量表.

當然,拷貝後會發現除錯”失效”了,是因為除錯檔案的地址和這裡的參考地址不同.暫時不管,之後的c_runtime_cpu_setup在arch/arm/cpu/armv7/start.S中,他的作用是如果ICACHE使能則失效他.

接下來清除BSS段並呼叫board_init_r,其實就是init_sequence_r函式列表.

由於現在地址重定向了,不太容易繼續分析,那麼現在可以看看之前的比如init_sequence_f,由於條件編譯的存在,有一些已經自動變灰的不用分析.