uboot分析之cconsole_init_r函式
阿新 • • 發佈:2019-02-03
1 /* Called after the relocation - use desired console functions */
2 int console_init_r (void)
3 {
4 device_t *inputdev = NULL, *outputdev = NULL;
5 int i, items = ListNumItems (devlist);
// 第5行, 取得裝置鏈中的裝置數
6 /* Scan devices looking for input and output devices */
7 for (i = 1;
8 (i <= items) && ((inputdev == NULL) || (outputdev == NULL));
9 i++
10 ) {
11 device_t *dev = ListGetPtrToItem (devlist, i);
12 if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
13 inputdev = dev;
14 }
15 if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
16 outputdev = dev;
17 }
18 }
// 7~18行,在裝置鏈中按註冊的順序查詢輸入輸出裝置,在設備註冊時 dev.flags表示此裝置的型別。
// 比如這裡drv_system_init,此裝置是第一個註冊的裝置,且其dev.flags為
// DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM 所以上面18行過後,輸入輸出裝置都指定為
// drv_system_init裡註冊的裝置了
19 /* Initializes output console first */
20 if (outputdev != NULL) {
21 console_setfile (stdout, outputdev);
22 console_setfile (stderr, outputdev);
23 }
24 /* Initializes input console */
25 if (inputdev != NULL) {
26 console_setfile (stdin, inputdev);
27 }
21~27行, console_setfile做如下幾件事:
1. 如果初始化該裝置時註冊了device_t.start,即啟動裝置的函式,則執行該函式,開啟該裝置
2. 將裝置的指標存入stdio_devices[file],這應該是標準輸入標準輸出、標準出錯。
#define stdin 0
#define stdout 1
#define stderr 2
22行將標準出錯定為輸出裝置,這樣有錯誤資訊就會通過輸出裝置打印出來了
28 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
29 /* Print information */
30 puts ("In: ");
31 if (stdio_devices[stdin] == NULL) {
32 puts ("No input devices available!\n");
33 } else {
34 printf ("%s\n", stdio_devices[stdin]->name);
35 }
36 puts ("Out: ");
37 if (stdio_devices[stdout] == NULL) {
38 puts ("No output devices available!\n");
39 } else {
40 printf ("%s\n", stdio_devices[stdout]->name);
41 }
42 puts ("Err: ");
43 if (stdio_devices[stderr] == NULL) {
44 puts ("No error devices available!\n");
45 } else {
46 printf ("%s\n", stdio_devices[stderr]->name);
47 }
30~47行,將資訊打印出來,這裡打印出出的資訊就為
In: serial
Out: serial
Err: serial
48 /* Setting environment variables */
49 for (i = 0; i < 3; i++) {
50 setenv (stdio_names[i], stdio_devices[i]->name);
51 }
49~51行 將資訊寫到環境變數中去
char *stdio_names[MAX_FILES] = { "stdin", "stdout", "stderr" };
這樣環境變數裡 stdin stdout stderr 都為serial
52 return (0);
53 }