轉載於:http://blog.csdn.net/jgdu1981/article/details/8643057
linux啟動時uboot傳遞進console=ttyS0,115200n8的參數
內核中用__setup()宏聲明參數處理的方法
關於__setup宏參考 early_param和__setup宏
- __setup("console=", console_setup);
console_setup函數處理
1.console_cmdline結構體
- struct console_cmdline
- {
- char name[8]; //驅動名
- int index; //次設備號
- char *options; //選項
- #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
- char *brl_options;
- #endif
- };
2.console_setup
- static int __init console_setup(char *str)
- {
- char buf[sizeof(console_cmdline[0].name) + 4]; //分配驅動名+index的緩沖區
- char *s, *options, *brl_options = NULL;
- int idx;
- #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
- if (!memcmp(str, "brl,", 4)) {
- brl_options = "";
- str += 4;
- } else if (!memcmp(str, "brl=", 4)) {
- brl_options = str + 4;
- str = strchr(brl_options, ',');
- if (!str) {
- printk(KERN_ERR "need port name after brl=\n");
- return 1;
- }
- *(str++) = 0;
- }
- #endif
- if (str[0] >= '0' && str[0] <= '9') { //第一個參數屬於[0,9]
- strcpy(buf, "ttyS"); //則將其驅動名設為ttyS
- strncpy(buf + 4, str, sizeof(buf) - 5);//將次設備號放其后面
- } else {
- strncpy(buf, str, sizeof(buf) - 1); //放設備號到其后面
- }
- buf[sizeof(buf) - 1] = 0;
- if ((options = strchr(str, ',')) != NULL) //獲取options
- *(options++) = 0;
- #ifdef __sparc__
- if (!strcmp(str, "ttya"))
- strcpy(buf, "ttyS0");
- if (!strcmp(str, "ttyb"))
- strcpy(buf, "ttyS1");
- #endif
- for (s = buf; *s; s++)
- if ((*s >= '0' && *s <= '9') || *s == ',')
- break;
- idx = simple_strtoul(s, NULL, 10); //獲取次設備號
- *s = 0;
- __add_preferred_console(buf, idx, options, brl_options);
- console_set_on_cmdline = 1;
- return 1;
- }
__add_preferred_console函數
- static int __add_preferred_console(char *name, int idx, char *options,char *brl_options)
- {
- struct console_cmdline *c;
- int i;
- for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++) //可以最多8個console
- if (strcmp(console_cmdline[i].name, name) == 0 && console_cmdline[i].index == idx) {
- //比較已注冊的console_cmdline數組中的項的名字及次設備號,若console_cmdline已經存在
- if (!brl_options)
- selected_console = i; //設置全局selected_console索引號
- return 0; //則返回
- }
- if (i == MAX_CMDLINECONSOLES) //判斷console_cmdline數組是否滿了
- return -E2BIG;
- if (!brl_options)
- selected_console = i; //設置全局selected_console索引號
- c = &console_cmdline[i]; //獲取全局console_cmdline數組的第i項地址
- strlcpy(c->name, name, sizeof(c->name)); //填充全局console_cmdline的驅動名
- c->options = options; //填充配置選項115200n8
- #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
- c->brl_options = brl_options;
- #endif
- c->index = idx; //填充索引號0
- return 0;
- }
整體的作用是根據uboot傳遞的參數設置全局console_cmdline數組 該數組及全局selected_console,在register_console中會使用到 二 console 設備驅動