Linux启动时,内核需要进行解压缩。在ARM平台上,它使用Zlib库进行解压缩。解压缩过程中,需要将调试信息输出到控制台。在ARM平台上,控制台一般是通过串行UART实现的。
要在UART上输出字符,可以使用UART驱动程序提供的函数进行操作。以下是一段示例代码,演示如何将字符输出到UART:
#include
#include
void putc(char ch) {
unsigned int status;
do {
status = readl(UARTn_BASE + UART_LSR);
} while (!(status & UART_LSR_THRE));
writel(ch & 0xff, UARTn_BASE + UART_TX);
}
要使用上述代码,在初始化阶段从串口设备树上获取UART设备的基地址,然后调用putc
函数输出字符即可。以下是一些示例代码:
#include
#include
#define UART0_BASE (void *)0x01C28000
void putc(char ch) {
unsigned int status;
do {
status = readl(UART0_BASE + UART_LSR);
} while (!(status & UART_LSR_THRE));
writel(ch & 0xff, UART0_BASE + UART_TX);
}
void early_print(char *s) {
while(*s)
putc(*s++);
}
void asmlinkage __init decompress_kernel(void) {
char *s = "Hello, world!\n";
early_print(s);
}
在上述代码中,首先定义了UART0的基地址。然后实现了putc
函数,它会一直等待UART处于空闲状态并输出单个字符。最后,在解压缩内核时,调用了early_print
函数,该函数使用putc
函数输出字符串。