自定義組合按鍵進入Uboot

uboot源碼中提供的進入uboot命令行的方式爲輸入任意鍵。這樣有一個隱患,當調試串口有干擾輸入信號時,系統會將干擾信號識別爲任意鍵而進入uboot,從而無法正常引導操作系統,這樣對產品將是災難性的影響。
解決或者規避該問題的方法之一是將任意鍵改爲組合鍵減小異常機率,當然歸根結底還是要從硬件的角度避免干擾信號的產生。

修改方法:

不同版本需要修改的文件不一樣,可能是common/autoboot.c或者common/main.c。
或者在uboot源碼根目錄搜索關鍵字’Hit any key’尋找該文件。

$find .|xargs grep -ri ‘Hit any key’
./common/autoboot.c: printf(“Hit any key to stop autoboot: %2d “, bootdelay);

代碼片段:

static int __abortboot(int bootdelay)
{
    int abort = 0;
    unsigned long ts;

#ifdef CONFIG_MENUPROMPT
    printf(CONFIG_MENUPROMPT);
#else
//    printf("Hit any key to stop autoboot: %2d ", bootdelay);
    printf("Press 'ctrl+q/Q' to stop autoboot: %2d ", bootdelay);
#endif
    /*
     * Check if key already pressed
     */
    if (tstc()) {   /* we got a key press   */
        (void) getc();  /* consume input        */
        puts("\b\b\b 0");
        abort = 1;      /* don't auto boot      */
    }

    while ((bootdelay > 0) && (!abort)) {
        --bootdelay;
        /* delay 1000 ms */
        ts = get_timer(0);
        do {
            if (tstc()) {   /* we got a key press   */
                if(17 == getc()){                   
                    abort  = 1;     /* don't auto boot      */
                    bootdelay = 0;  /* no more delay        */

# ifdef CONFIG_MENUKEY
                    menukey = getc();
# else
                    (void) getc();  /* consume input        */
# endif
                    break;
                    }
                }
                udelay(10000);
        } while (!abort && get_timer(ts) < 1000);

        printf("\b\b\b%2d ", bootdelay);
    }
    putc('\n');
    return abort;
}

將按下任意鍵的提示信息改爲ctrl+q/Q。

當檢測到tstc()函數返回真時,即識別到按鍵,此時執行getc()函數獲取按鍵值。
uboot getc()、tstc()函數API說明如下:

這裏寫圖片描述

判斷getc()的返回值爲17時,退出循環,進入uboot命令行。
ctrl+q/Q組合鍵的值爲十六進制11,也是就十進制17。

這裏寫圖片描述

類似的,可以將ctrl+q/Q改爲其他組合鍵。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章