本文以三星 s5pv210(ARM 平台)為例,梳理 kernel 早期 console 機制 earlycon 的運作方式。
printk 的日誌最終是靠 console 輸出,而正常 console 是在 device probe 階段才註冊的。也就是說,kernel 剛啟動、驅動還沒就位的那段時間,串口根本還不能用,log 自然也沒地方去。為了解決這個空窗期,Linux 提供了 early console 機制,讓訊息在設備註冊之前就能被送出,這樣的 console 也被稱為 boot console,簡稱 bcon。它在 kernel 啟動很早的階段就完成註冊,做法是直接呼叫輸出設備(通常是串口)最樸素的 write 方法把資料寫出去,而這個 write 由平台自行實作。
前提條件是:作為輸出的串口必須已在 bootloader 階段完成初始化。early console 有兩代實現:早期的 early_printk 與後來的 earlycon,本文聚焦於 earlycon。
earlycon 是比 early_printk 更新的機制。early_printk 需要平台實作 early_console->write 的最終環節 addruart,各平台各自用函數定義的方式提供,相容性偏差;而且 UART 暫存器位址由平台自行定義,與正常 console 的定義完全脫節,可攜性也不理想。
earlycon 則用 __earlycon_table 統一維護所有 earlycon_id,再透過 dts 中正常 console 的 compatible 屬性反查要用的 earlycon_id;UART 暫存器基底位址同樣取自 dts 中正常 console 的設定。相容性與可攜性都明顯更好。early_printk 的細節可參考《[console] early_printk 實現流程》。
(1)開啟必要宏:
CONFIG_SERIAL_EARLYCON CONFIG_OF_EARLY_FLATTREE
平台還要開啟對應 earlycon_id 的實作宏,以 tiny210(s5pv210)為例:
CONFIG_SERIAL_SAMSUNG_CONSOLE
(2)在 cmdline 中加入 earlycon。tiny210 透過 bootargs 傳 cmdline,所以寫在 bootargs 裡。
(3)在 dts 的 chosen 節點加上 linux,stdout-path 或 stdout-path,用來指定標準輸出的 dts 節點路徑。
chosen {
bootargs = "console=ttySAC0,115200n8 root=/dev/ram rw rootwait ignore_loglevel earlycon";
linux,stdout-path = &uart0;
};
(4)正常呼叫 printk 即可。
每個 earlycon 對應一個 earlycon_id,全部掛在 __earlycon_table 這個 linker section 裡:
OF_EARLYCON_DECLARE(s5pv210, "samsung,s5pv210-uart", s5pv210_early_console_setup);
展開後(include/linux/serial_core.h):
#define OF_EARLYCON_DECLARE(_name, compat, fn) \
static const struct earlycon_id __UNIQUE_ID(__earlycon_##_name) \
__used __section(__earlycon_table) \
= { .name = __stringify(_name), \
.compatible = compat, \
.setup = fn }
也就是:
static const struct earlycon_id __UNIQUE_ID(__earlycon_s5pv210) \
__used __section(__earlycon_table) \
= { .name = s5pv210, \
.compatible = "samsung,s5pv210-uart", \
.setup = s5pv210_early_console_setup}
earlycon_id 的結構如下:
struct earlycon_id {
char name[16];
char compatible[128];
int (*setup)(struct earlycon_device *, const char *options);
} __aligned(32);
其中 name 是 console 名稱;compatible 用於匹配 UART 對應的 dts node;setup 負責設定 write 函數。
struct earlycon_device {
struct console *con;
struct uart_port port;
char options[16]; /* e.g., 115200n8 */
unsigned int baud;
};
con 是要註冊進 console 子系統的 console;port 是對應串口的 uart_port,需在解析過程中填好;options 保存參數;baud 是波特率。
drivers/of/fdt.c:
early_param("earlycon", setup_of_earlycon);
static int __init setup_of_earlycon(char *buf)
{
if (buf)
return 0;
return early_init_dt_scan_chosen_serial();
}
收到 earlycon 參數後轉交 early_init_dt_scan_chosen_serial 去搜尋 dts 節點。
#ifdef CONFIG_SERIAL_EARLYCON
static int __init early_init_dt_scan_chosen_serial(void)
{
offset = fdt_path_offset(fdt, "/chosen");
if (offset < 0)
offset = fdt_path_offset(fdt, "/chosen@0");
if (offset < 0)
return -ENOENT;
p = fdt_getprop(fdt, offset, "stdout-path", &l);
if (!p)
p = fdt_getprop(fdt, offset, "linux,stdout-path", &l);
if (!p || !l)
return -ENOENT;
q = strchrnul(p, ':');
if (*q != '\0')
options = q + 1;
l = q - p;
offset = fdt_path_offset_namelen(fdt, p, l);
if (offset < 0) {
pr_warn("earlycon: stdout-path %.*s not found\n", l, p);
return 0;
}
for (match = __earlycon_table; match < __earlycon_table_end; match++) {
if (!match->compatible[0])
continue;
if (fdt_node_check_compatible(fdt, offset, match->compatible))
continue;
of_setup_earlycon(match, offset, options);
return 0;
}
return -ENODEV;
}
先從 chosen 節點取出 stdout-path 或 linux,stdout-path,例如 tiny210 的 linux,stdout-path = &uart0;;再用該節點的 compatible 去比對 earlycon_id。tiny210 標準輸出串口的節點是:
uart0: serial@e2900000 {
compatible = "samsung,s5pv210-uart";
...
與 s5pv210 的 earlycon_id compatible 完全一致,於是命中並進入 of_setup_earlycon。
drivers/tty/serial/earlycon.c:
#ifdef CONFIG_OF_EARLY_FLATTREE
int __init of_setup_earlycon(const struct earlycon_id *match,
unsigned long node, const char *options)
{
struct uart_port *port = &early_console_dev.port;
port->iotype = UPIO_MEM;
port->mapbase = addr;
port->uartclk = BASE_BAUD * 16;
port->membase = earlycon_map(port->mapbase, SZ_4K);
earlycon_init(&early_console_dev, match->name);
err = match->setup(&early_console_dev, options);
register_console(early_console_dev.con);
流程是:依 dts 節點內容初始化 port → earlycon_init 補齊其餘參數 → 由 match->setup 設定 write → 最後 register_console 註冊進 console 子系統。此後 printk 就能透過 early_console_dev.con->write 輸出日誌。
static struct console early_con = {
.name = "uart", /* fixed up at earlycon registration */
.flags = CON_PRINTBUFFER | CON_BOOT,
.index = 0,
};
static struct earlycon_device early_console_dev = {
.con = &early_con,
};
CON_PRINTBUFFER 表示註冊時要把 printk 緩衝區裡既有的 log 一併補印;CON_BOOT 表示這是 boot console,等真正非 boot 的 console 註冊時必須先把它卸下。
setup 的唯一目標,就是替 early_console_dev.con 掛上 write 函數。
static int __init s5pv210_early_console_setup(struct earlycon_device *device, const char *opt)
{
device->port.private_data = &s5pv210_early_console_data;
return samsung_early_console_setup(device, opt);
}
static int __init samsung_early_console_setup(struct earlycon_device *device, const char *opt)
{
if (!device->port.membase)
return -ENODEV;
device->con->write = samsung_early_write;
return 0;
}
至此 earlycon 初始化完成。
register_console(early_console_dev.con) 之後,console_drivers 鏈表裡就有了這個 con。標準 printk 呼叫路徑為:
printk -> vprintk -> console_unlock -> call_console_drivers
call_console_drivers 中:
for_each_console(con) {
con->write(con, text, len);
}
#define for_each_console(con) \
for (con = console_drivers; con != NULL; con = con->next)
early_console_dev.con 作為其中一員,write 自然會被呼叫,最終落到平台實作裡。
arch/arm/boot/dts/s5pv210.dtsi:
uart0: serial@e2900000 {
compatible = "samsung,s5pv210-uart";
reg = <0xe2900000 0x400>;
};
對 earlycon 而言只在意這兩個屬性,其餘初始化都已在 uboot 完成。compatible 用於比對 earlycon_id;reg 提供 UART 暫存器的基底位址與長度(與通用 UART 驅動共用),write 函數中的暫存器就是在此基底上做偏移。
drivers/tty/serial/samsung.c,需開啟:
#ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
重點是定義 earlycon_id,而定義 earlycon_id 的重點是 setup,setup 的重點則是 UART 的 write。
OF_EARLYCON_DECLARE(s5pv210, "samsung,s5pv210-uart", s5pv210_early_console_setup);
字串 "samsung,s5pv210-uart" 必須與 dtsi 中 console 節點一致。
static void samsung_early_write(struct console *con, const char *s, unsigned n)
{
struct earlycon_device *dev = con->data;
uart_console_write(&dev->port, s, n, samsung_early_putc);
}
static void samsung_early_putc(struct uart_port *port, int c)
{
if (readl(port->membase + S3C2410_UFCON) & S3C2410_UFCON_FIFOMODE)
samsung_early_busyuart_fifo(port);
else
samsung_early_busyuart(port);
writeb(c, port->membase + S3C2410_UTXH);
}
#define S3C2410_UTXH (0x20)
uart_console_write 會逐字元回呼 samsung_early_putc 把資料推進暫存器。成功後可看到:
earlycon: s5pv210 at MMIO 0xe2900000 (options '') bootconsole [s5pv210] enabled