uboot命令之bootm詳解

開發板:DM3730 cortex-a8
虛擬機:ubuntu 14.04
編譯器:gcc-linaro-5.3-2016.02-x86_64_arm-linux-gnueabihf
開發板內核:linux 4.4.12

bootm 用於將內核鏡像加載到內存的指定地址處,如果有需要還要解壓鏡像,然後根據操作系統和體系結構的不同給內核傳遞不同的啓動參數,最後啓動內核。
一、arm 架構處理器對 linux 內核啓動之前環境的五點需求

1、cpu 寄存器設置

* R0 = 0

* R1 = 板級 id

* R2 = 啓動參數在內存中的起始地址

2、cpu 模式

* 禁止所有中斷

* 必須爲SVC(超級用戶)模式

3、緩存、MMU

* 關閉 MMU

* 指令緩存可以開啓或者關閉

* 數據緩存必須關閉並且不能包含任何髒數據

4、設備

* DMA 設備應當停止工作

5、boot loader 需要跳轉到內核鏡像的第一條指令處
這些需求都由 boot loader 實現,在常用的 uboot 中完成一系列的初始化後最後通過 bootm 命令加載 linux 內核。該命令用法介紹如下:

int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
    ulong       iflag;
    ulong       load_end = 0;
    int     ret;
    boot_os_fn  *boot_fn;
#ifndef CONFIG_RELOC_FIXUP_WORKS
    static int relocated = 0;
    /* 重定位啓動函數表 */
    /* relocate boot function table */
    if (!relocated) {
        int i;
        for (i = 0; i < ARRAY_SIZE(boot_os); i++)
            if (boot_os[i] != NULL)
                boot_os[i] += gd->reloc_off;
        relocated = 1;
    }
#endif
     //判斷是否有子命令
    /* determine if we have a sub command */
    if (argc > 1) {
        char *endp;

        simple_strtoul(argv[1], &endp, 16);
        /* endp pointing to NULL means that argv[1] was just a
         * valid number, pass it along to the normal bootm processing
         *
         * If endp is ':' or '#' assume a FIT identifier so pass
         * along for normal processing.
         *
         * Right now we assume the first arg should never be '-'
         */
        if ((*endp != 0) && (*endp != ':') && (*endp != '#'))
            return do_bootm_subcommand(cmdtp, flag, argc, argv);
    }
    /* 獲取內核相關信息 */
    if (bootm_start(cmdtp, flag, argc, argv))
        return 1;

    /*
     * We have reached the point of no return: we are going to
     * overwrite all exception vector code, so we cannot easily
     * recover from any failures any more...
     */
     /* 關閉中斷 */
    iflag = disable_interrupts();

#if defined(CONFIG_CMD_USB)
    /*
     * turn off USB to prevent the host controller from writing to the
     * SDRAM while Linux is booting. This could happen (at least for OHCI
     * controller), because the HCCA (Host Controller Communication Area)
     * lies within the SDRAM and the host controller writes continously to
     * this area (as busmaster!). The HccaFrameNumber is for example
     * updated every 1 ms within the HCCA structure in SDRAM! For more
     * details see the OpenHCI specification.
     */
     /* 關閉USB */
    usb_stop();
#endif

#ifdef CONFIG_AMIGAONEG3SE
    /*
     * We've possible left the caches enabled during
     * bios emulation, so turn them off again
     */
     /* 關閉指令cache和數據cache */
    icache_disable();
    dcache_disable();
#endif
    /* 加載內核 */
    ret = bootm_load_os(images.os, &load_end, 1);

    if (ret < 0) {
        if (ret == BOOTM_ERR_RESET)
            do_reset (cmdtp, flag, argc, argv);
        if (ret == BOOTM_ERR_OVERLAP) {
            if (images.legacy_hdr_valid) {
                if (image_get_type (&images.legacy_hdr_os_copy) == IH_TYPE_MULTI)
                    puts ("WARNING: legacy format multi component "
                        "image overwritten\n");
            } else {
                puts ("ERROR: new format image overwritten - "
                    "must RESET the board to recover\n");
                show_boot_progress (-113);
                do_reset (cmdtp, flag, argc, argv);
            }
        }
        if (ret == BOOTM_ERR_UNIMPLEMENTED) {
            if (iflag)
                enable_interrupts();
            show_boot_progress (-7);
            return 1;
        }
    }

    lmb_reserve(&images.lmb, images.os.load, (load_end - images.os.load));

    if (images.os.type == IH_TYPE_STANDALONE) {
        if (iflag)
            enable_interrupts();
        /* This may return when 'autostart' is 'no' */
        bootm_start_standalone(iflag, argc, argv);
        return 0;
    }

    show_boot_progress (8);

#ifdef CONFIG_SILENT_CONSOLE
    if (images.os.os == IH_OS_LINUX)
        fixup_silent_linux();
#endif
    //獲取內核啓動參數
    boot_fn = boot_os[images.os.os];

    if (boot_fn == NULL) {
        if (iflag)
            enable_interrupts();
        printf ("ERROR: booting os '%s' (%d) is not supported\n",
            genimg_get_os_name(images.os.os), images.os.os);
        show_boot_progress (-8);
        return 1;
    }
    //內核啓動前的準備
    arch_preboot_os();
    /* 啓動內核,不返回 */
    boot_fn(0, argc, argv, &images);

    show_boot_progress (-9);
#ifdef DEBUG
    puts ("\n## Control returned to monitor - resetting...\n");
#endif
    do_reset (cmdtp, flag, argc, argv);

    return 1;
}

該函數主要的工作流程是,通過bootm_start來獲取內核鏡像文件的信息,然後通過bootm_load_os函數來加載內核,最後通過boot_fn來啓動內核。

首先看一下bootm_start,該函數主要進行鏡像的有效性判定、校驗、計算入口地址等操作,大部分工作通過 boot_get_kernel -> image_get_kernel 完成。

static int bootm_start(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
    void        *os_hdr;
    int        ret;
    memset ((void *)&images, 0, sizeof (images));
    //讀取環境變量,從環境變量中檢查是否要對鏡像的數據(不是鏡像頭)進行校驗
    images.verify = getenv_yesno ("verify");
    //不做任何有意義的工作,除了定義# define lmb_reserve(lmb, base, size)  
    bootm_start_lmb();
    //獲取鏡像頭,加載地址,長度,返回指向內存中鏡像頭的指針
    /* get kernel image header, start address and length */
    os_hdr = boot_get_kernel (cmdtp, flag, argc, argv,
            &images, &images.os.image_start, &images.os.image_len);
    if (images.os.image_len == 0) {
        puts ("ERROR: can't get kernel image!\n");
        return 1;
    }
    //根據鏡像魔數獲取鏡像類型  
    /* get image parameters */
    switch (genimg_get_format (os_hdr)) {
    case IMAGE_FORMAT_LEGACY:
        images.os.type = image_get_type (os_hdr);//鏡像類型  
        images.os.comp = image_get_comp (os_hdr);//壓縮類型  
        images.os.os = image_get_os (os_hdr);//操作系統類型 
        images.os.end = image_get_image_end (os_hdr);//當前鏡像的尾地址
        images.os.load = image_get_load (os_hdr);//鏡像數據的載入地址 
        break;
#if defined(CONFIG_FIT)
    case IMAGE_FORMAT_FIT:
        if (fit_image_get_type (images.fit_hdr_os,
                    images.fit_noffset_os, &images.os.type)) {
            puts ("Can't get image type!\n");
            show_boot_progress (-109);
            return 1;
        }
        if (fit_image_get_comp (images.fit_hdr_os,
                    images.fit_noffset_os, &images.os.comp)) {
            puts ("Can't get image compression!\n");
            show_boot_progress (-110);
            return 1;
        }
        if (fit_image_get_os (images.fit_hdr_os,
                    images.fit_noffset_os, &images.os.os)) {
            puts ("Can't get image OS!\n");
            show_boot_progress (-111);
            return 1;
        }
        images.os.end = fit_get_end (images.fit_hdr_os);
        if (fit_image_get_load (images.fit_hdr_os, images.fit_noffset_os,
                    &images.os.load)) {
            puts ("Can't get image load address!\n");
            show_boot_progress (-112);
            return 1;
        }
        break;
#endif
    default:
        puts ("ERROR: unknown image format type!\n");
        return 1;
    }
     //獲取內核入口地址
    /* find kernel entry point */
    if (images.legacy_hdr_valid) {
        images.ep = image_get_ep (&images.legacy_hdr_os_copy);
#if defined(CONFIG_FIT)
    } else if (images.fit_uname_os) {
        ret = fit_image_get_entry (images.fit_hdr_os,
                images.fit_noffset_os, &images.ep);
        if (ret) {
            puts ("Can't get entry point property!\n");
            return 1;
        }
#endif
    } else {
        puts ("Could not find kernel entry point!\n");
        return 1;
    }
    if (((images.os.type == IH_TYPE_KERNEL) ||
         (images.os.type == IH_TYPE_MULTI)) &&
        (images.os.os == IH_OS_LINUX)) {
        //獲取虛擬磁盤
        /* find ramdisk */
        ret = boot_get_ramdisk (argc, argv, &images, IH_INITRD_ARCH,
                &images.rd_start, &images.rd_end);
        if (ret) {
            puts ("Ramdisk image is corrupt or invalid\n");
            return 1;
        }

#if defined(CONFIG_OF_LIBFDT)
         //獲取設備樹,設備樹是linux 3.XX版本特有的
        /* find flattened device tree */
        ret = boot_get_fdt (flag, argc, argv, &images,
                    &images.ft_addr, &images.ft_len);
        if (ret) {
            puts ("Could not find a valid device tree\n");
            return 1;
        }
        set_working_fdt_addr(images.ft_addr);
#endif
    }
    //將內核加載地址賦值給images.os.start
    images.os.start = (ulong)os_hdr;
    //更新鏡像狀態
    images.state = BOOTM_STATE_START;
    return 0;
}

接着看一下bootm_load_os函數,它的主要工作是解壓內核鏡像文件,並且將它移動到內核加載地址。

首先看一下兩個重要的結構體

//include/image.h 
typedef struct image_header {
        uint32_t        ih_magic;       /* Image Header Magic Number    */
        uint32_t        ih_hcrc;        /* Image Header CRC Checksum    */
        uint32_t        ih_time;        /* Image Creation Timestamp     */
        uint32_t        ih_size;        /* Image Data Size              */
        uint32_t        ih_load;        /* Data  Load  Address          */
        uint32_t        ih_ep;          /* Entry Point Address          */
        uint32_t        ih_dcrc;        /* Image Data CRC Checksum      */
        uint8_t         ih_os;          /* Operating System             */
        uint8_t         ih_arch;        /* CPU architecture             */
        uint8_t         ih_type;        /* Image Type                   */
        uint8_t         ih_comp;        /* Compression Type             */
        uint8_t         ih_name[IH_NMLEN];      /* Image Name           */
} image_header_t;
typedef struct image_info {
        ulong           start, end;             /* start/end of blob */
        ulong           image_start, image_len; /* start of image within blob, len of image */
        ulong           load;                   /* load addr for the image */
        uint8_t         comp, type, os;         /* compression, type of image, os type */
} image_info_t;
static int bootm_start(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
    void        *os_hdr;
    int        ret;
    memset ((void *)&images, 0, sizeof (images));
    //讀取環境變量,從環境變量中檢查是否要對鏡像的數據(不是鏡像頭)進行校驗
    images.verify = getenv_yesno ("verify");
    //不做任何有意義的工作,除了定義# define lmb_reserve(lmb, base, size)  
    bootm_start_lmb();
    //獲取鏡像頭,加載地址,長度,返回指向內存中鏡像頭的指針
    /* get kernel image header, start address and length */
    os_hdr = boot_get_kernel (cmdtp, flag, argc, argv,
            &images, &images.os.image_start, &images.os.image_len);
    if (images.os.image_len == 0) {
        puts ("ERROR: can't get kernel image!\n");
        return 1;
    }
    //根據鏡像魔數獲取鏡像類型  
    /* get image parameters */
    switch (genimg_get_format (os_hdr)) {
    case IMAGE_FORMAT_LEGACY:
        images.os.type = image_get_type (os_hdr);//鏡像類型  
        images.os.comp = image_get_comp (os_hdr);//壓縮類型  
        images.os.os = image_get_os (os_hdr);//操作系統類型 
        images.os.end = image_get_image_end (os_hdr);//當前鏡像的尾地址
        images.os.load = image_get_load (os_hdr);//鏡像數據的載入地址 
        break;
#if defined(CONFIG_FIT)
    case IMAGE_FORMAT_FIT:
        if (fit_image_get_type (images.fit_hdr_os,
                    images.fit_noffset_os, &images.os.type)) {
            puts ("Can't get image type!\n");
            show_boot_progress (-109);
            return 1;
        }
        if (fit_image_get_comp (images.fit_hdr_os,
                    images.fit_noffset_os, &images.os.comp)) {
            puts ("Can't get image compression!\n");
            show_boot_progress (-110);
            return 1;
        }
        if (fit_image_get_os (images.fit_hdr_os,
                    images.fit_noffset_os, &images.os.os)) {
            puts ("Can't get image OS!\n");
            show_boot_progress (-111);
            return 1;
        }
        images.os.end = fit_get_end (images.fit_hdr_os);
        if (fit_image_get_load (images.fit_hdr_os, images.fit_noffset_os,
                    &images.os.load)) {
            puts ("Can't get image load address!\n");
            show_boot_progress (-112);
            return 1;
        }
        break;
#endif
    default:
        puts ("ERROR: unknown image format type!\n");
        return 1;
    }
     //獲取內核入口地址
    /* find kernel entry point */
    if (images.legacy_hdr_valid) {
        images.ep = image_get_ep (&images.legacy_hdr_os_copy);
#if defined(CONFIG_FIT)
    } else if (images.fit_uname_os) {
        ret = fit_image_get_entry (images.fit_hdr_os,
                images.fit_noffset_os, &images.ep);
        if (ret) {
            puts ("Can't get entry point property!\n");
            return 1;
        }
#endif
    } else {
        puts ("Could not find kernel entry point!\n");
        return 1;
    }
    if (((images.os.type == IH_TYPE_KERNEL) ||
         (images.os.type == IH_TYPE_MULTI)) &&
        (images.os.os == IH_OS_LINUX)) {
        //獲取虛擬磁盤
        /* find ramdisk */
        ret = boot_get_ramdisk (argc, argv, &images, IH_INITRD_ARCH,
                &images.rd_start, &images.rd_end);
        if (ret) {
            puts ("Ramdisk image is corrupt or invalid\n");
            return 1;
        }

#if defined(CONFIG_OF_LIBFDT)
         //獲取設備樹,設備樹是linux 3.XX版本特有的
        /* find flattened device tree */
        ret = boot_get_fdt (flag, argc, argv, &images,
                    &images.ft_addr, &images.ft_len);
        if (ret) {
            puts ("Could not find a valid device tree\n");
            return 1;
        }
        set_working_fdt_addr(images.ft_addr);
#endif
    }
    //將內核加載地址賦值給images.os.start
    images.os.start = (ulong)os_hdr;
    //更新鏡像狀態
    images.state = BOOTM_STATE_START;
    return 0;
}
#define BOOTM_ERR_RESET        -1
#define BOOTM_ERR_OVERLAP    -2
#define BOOTM_ERR_UNIMPLEMENTED    -3
static int bootm_load_os(image_info_t os, ulong *load_end, int boot_progress)
{
    uint8_t comp = os.comp;//壓縮格式
    ulong load = os.load;//加載地址
    ulong blob_start = os.start;//系統起始地址
    ulong blob_end = os.end;//系統結束地址
    ulong image_start = os.image_start;//鏡像起始地址
    ulong image_len = os.image_len;//鏡像大小
    uint unc_len = CONFIG_SYS_BOOTM_LEN;//鏡像最大長度
#if defined(CONFIG_LZMA) || defined(CONFIG_LZO)
    int ret;
#endif /* defined(CONFIG_LZMA) || defined(CONFIG_LZO) */
    //獲取鏡像類型
    const char *type_name = genimg_get_type_name (os.type);
    switch (comp) {
    case IH_COMP_NONE://鏡像沒有壓縮過
        if (load == blob_start) {//判斷是否需要移動鏡像
            printf ("   XIP %s ... ", type_name);
        } else {
            printf ("   Loading %s ... ", type_name);
            memmove_wd ((void *)load, (void *)image_start,
                    image_len, CHUNKSZ);
        }
        *load_end = load + image_len;
        puts("OK\n");
        break;
#ifdef CONFIG_GZIP
    case IH_COMP_GZIP://鏡像使用gzip壓縮
        printf ("   Uncompressing %s ... ", type_name);
        //解壓鏡像文件
        if (gunzip ((void *)load, unc_len,
                    (uchar *)image_start, &image_len) != 0) {
            puts ("GUNZIP: uncompress, out-of-mem or overwrite error "
                "- must RESET board to recover\n");
            if (boot_progress)
                show_boot_progress (-6);
            return BOOTM_ERR_RESET;
        }
        *load_end = load + image_len;
        break;
#endif /* CONFIG_GZIP */
......
    return 0;
}

最後看一下boot_fn函數,boot_fn的定義爲

boot_os_fn *boot_fn;

可以看出它是一個boot_os_fn類型的函數指針。它的定義爲

//  common/cmd_bootm.c
typedef int boot_os_fn (int flag, int argc, char * const argv[],
                        bootm_headers_t *images); /* pointers to os/initrd/fdt */
#ifdef CONFIG_BOOTM_LINUX
extern boot_os_fn do_bootm_linux;
#endif
......

然後boot_fn在do_bootm函數中被賦值爲

boot_fn = boot_os[images.os.os];

boot_os是一個函數指針數組

//  common/cmd_bootm.c
static boot_os_fn *boot_os[] = {
#ifdef CONFIG_BOOTM_LINUX
    [IH_OS_LINUX] = do_bootm_linux,
#endif
#ifdef CONFIG_BOOTM_NETBSD
    [IH_OS_NETBSD] = do_bootm_netbsd,
#endif
#ifdef CONFIG_LYNXKDI
    [IH_OS_LYNXOS] = do_bootm_lynxkdi,
#endif
#ifdef CONFIG_BOOTM_RTEMS
    [IH_OS_RTEMS] = do_bootm_rtems,
#endif
#if defined(CONFIG_BOOTM_OSE)
    [IH_OS_OSE] = do_bootm_ose,
#endif
#if defined(CONFIG_CMD_ELF)
    [IH_OS_VXWORKS] = do_bootm_vxworks,
    [IH_OS_QNX] = do_bootm_qnxelf,
#endif
#ifdef CONFIG_INTEGRITY
    [IH_OS_INTEGRITY] = do_bootm_integrity,
#endif
};

可以看出 boot_fn 函數指針最後指向的函數是位於 arch/arm/lib/bootm.c的 do_bootm_linux,這是內核啓動前最後的一個函數,該函數主要完成啓動參數的初始化,並將板子設定爲滿足內核啓動的環境。

int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images)
{
    //從全局變量結構體中獲取串口參數
    bd_t    *bd = gd->bd;
    char    *s;
    //獲取機器碼
    int    machid = bd->bi_arch_number;
    //內核入口函數
    void    (*kernel_entry)(int zero, int arch, uint params);
    int    ret;
    //獲取啓動參數
#ifdef CONFIG_CMDLINE_TAG
    char *commandline = getenv ("bootargs");
#endif
    if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
        return 1;
    //從環境變量中獲取機器碼
    s = getenv ("machid");
    if (s) {
        machid = simple_strtoul (s, NULL, 16);
        printf ("Using machid 0x%x from environment\n", machid);
    }
    //獲取ramdisk
    ret = boot_get_ramdisk(argc, argv, images, IH_ARCH_ARM, 
            &(images->rd_start), &(images->rd_end));
    if(ret)
        printf("[err] boot_get_ramdisk\n");
    show_boot_progress (15);
#ifdef CONFIG_OF_LIBFDT
    if (images->ft_len)
        return bootm_linux_fdt(machid, images);
#endif
    kernel_entry = (void (*)(int, int, uint))images->ep;
    debug ("## Transferring control to Linux (at address %08lx) ...\n",
           (ulong) kernel_entry);
#if defined (CONFIG_SETUP_MEMORY_TAGS) || \
    defined (CONFIG_CMDLINE_TAG) || \
    defined (CONFIG_INITRD_TAG) || \
    defined (CONFIG_SERIAL_TAG) || \
    defined (CONFIG_REVISION_TAG)
    setup_start_tag (bd);
#ifdef CONFIG_SERIAL_TAG
    setup_serial_tag (params);
#endif
#ifdef CONFIG_REVISION_TAG
    setup_revision_tag (params);
#endif
#ifdef CONFIG_SETUP_MEMORY_TAGS
    setup_memory_tags (bd);
#endif
#ifdef CONFIG_CMDLINE_TAG
    setup_commandline_tag (bd, commandline);
#endif
#ifdef CONFIG_INITRD_TAG
    if (images->rd_start && images->rd_end)
        setup_initrd_tag (bd, images->rd_start, images->rd_end);
#endif
    setup_end_tag(bd);
#endif
    announce_and_cleanup();
#ifdef CONFIG_ENABLE_MMU
    theLastJump((void *)virt_to_phys(kernel_entry), machid, bd->bi_boot_params);
#else
    kernel_entry(0, machid, bd->bi_boot_params);
    /* does not return */
#endif
    return 1;
}

kernel_entry(0, machid, r2)

真正將控制權交給內核, 啓動內核;

滿足arm架構linux內核啓動時的寄存器設置條件:第一個參數爲0 ;第二個參數爲板子id需與內核中的id匹配,第三個參數爲啓動參數地址bi_boot_params 。

(1)首先取出環境變量bootargs,這就是要傳遞給內核的參數。

(2)調用setup_XXX_tag

kernel_entry(0, machid, r2) 

真正將控制權交給內核, 啓動內核;

滿足arm架構linux內核啓動時的寄存器設置條件:第一個參數爲0 ;第二個參數爲板子id需與內核中的id匹配,第三個參數爲啓動參數地址bi_boot_params 。

(1)首先取出環境變量bootargs,這就是要傳遞給內核的參數。

(2)調用setup_XXX_tag

params是一個用來存儲要傳給kernel的參數的靜態全局變量。

u-boot 是通過標記列表向內核傳遞參數,標記在源代碼中定義爲tag,是一個結構體,在 arch/arm/include/asm/setup.h 中定義。

struct tag {                                                                                                                                                              
        struct tag_header hdr;
        union {
                struct tag_core         core;
                struct tag_mem32        mem;
                struct tag_videotext    videotext;
                struct tag_ramdisk      ramdisk;
                struct tag_initrd       initrd;
                struct tag_serialnr     serialnr;
                struct tag_revision     revision;
                struct tag_videolfb     videolfb;
                struct tag_cmdline      cmdline;
                /*
                 * Acorn specific
                 */
                struct tag_acorn        acorn;
                /*
                 * DC21285 specific
                 */
                struct tag_memclk       memclk;
        } u;

tag包括hdr和各種類型的tag_*,hdr來標誌當前的tag是哪種類型的tag。setup_start_tag是初始化了第一個tag,是tag_core類型的tag。最後調用tag_next跳到第一個tag末尾,爲下一個tag做準備。

tag_next是一個宏定義,被定義在arch/arm/include/asm/setup.h中

#define tag_next(t)     ((struct tag *)((u32 *)(t) + (t)->hdr.size))


struct tag_header {
        u32 size;
        u32 tag;
};

最後調用setup_end_tag,將末尾的tag設置爲ATAG_NONE,標誌tag列表結束。

static void setup_end_tag (bd_t *bd)                                                                                                                                    
{
        params->hdr.tag = ATAG_NONE;
        params->hdr.size = 0;
}

u-boot將參數以tag數組的形式佈局在內存的某一個地址,每個tag代表一種類型的參數,首尾tag標誌開始和結束,首地址傳給kernel供其解析

通過上面的分析,我們可以嘗試自己寫一個bootm來引導內核

//atag.h
#define ATAG_CORE    0x54410001
#define ATAG_MEM    0x54410002
#define ATAG_CMDLINE    0x54410009
#define ATAG_NONE    0x00000000
struct tag_header {
    unsigned int size;
    unsigned int tag;
};
struct tag_core {
    unsigned int flags;        
    unsigned int pagesize;
    unsigned int rootdev;
};
struct tag_mem32 {
    unsigned int    size;
    unsigned int    start;    
};
struct tag_cmdline {
    char    cmdline[1];    
};
struct tag {
    struct tag_header hdr;
    union {
        struct tag_core        core;
        struct tag_mem32    mem;
        struct tag_cmdline    cmdline;
    } u;
};
#define tag_size(type)    ((sizeof(struct tag_header) + sizeof(struct type)) >> 2)
#define tag_next(t)    ((struct tag *)((unsigned int *)(t) + (t)->hdr.size))
//boot.c
#include "atag.h"
#include "string.h"
void (*theKernel)(int , int , unsigned int );
#define SDRAM_KERNEL_START 0x51000000
#define SDRAM_TAGS_START   0x50000100
#define SDRAM_ADDR_START   0x50000000
#define SDRAM_TOTAL_SIZE   0x16000000
struct tag *pCurTag;
const char *cmdline = "console=ttySAC0,115200 init=/init";
void setup_core_tag()
{
     pCurTag = (struct tag *)SDRAM_TAGS_START;

     pCurTag->hdr.tag = ATAG_CORE;
     pCurTag->hdr.size = tag_size(tag_core); 

     pCurTag->u.core.flags = 0;
     pCurTag->u.core.pagesize = 4096;
     pCurTag->u.core.rootdev = 0;

     pCurTag = tag_next(pCurTag);
}
void setup_mem_tag()
{
     pCurTag->hdr.tag = ATAG_MEM;
     pCurTag->hdr.size = tag_size(tag_mem32); 

     pCurTag->u.mem.start = SDRAM_ADDR_START;
     pCurTag->u.mem.size = SDRAM_TOTAL_SIZE;

     pCurTag = tag_next(pCurTag);
}
void setup_cmdline_tag()
{
     int linelen = strlen(cmdline);

     pCurTag->hdr.tag = ATAG_CMDLINE;
     pCurTag->hdr.size = (sizeof(struct tag_header)+linelen+1+4)>>2;

     strcpy(pCurTag->u.cmdline.cmdline,cmdline);

     pCurTag = tag_next(pCurTag);
}
void setup_end_tag()
{
    pCurTag->hdr.tag = ATAG_NONE;
    pCurTag->hdr.size = 0;
}
void boot_linux(){

    //1.獲取Linux啓動地址
    theKernel = (void (*)(int , int , unsigned int ))SDRAM_KERNEL_START;
    printf("huo qu linux qi dong di zhi");
    //2.設置啓動參數
    //2.1.設置核心啓動參數
    setup_core_tag();
    //2.2.設置內存參數
    setup_mem_tag();
    //2.3.設置命令行參數
    setup_cmdline_tag();
    //2.4.設置結束標誌
    setup_end_tag();

    //4.啓動Linux內核
    theKernel(0,1626,SDRAM_TAGS_START);
    printf("qi dong linux nei he");

    }

轉自http://www.cnblogs.com/CoderTian/p/6006400.html

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