tiny4412u-boot分析u-boot引导内核流程

CrazyDiode CrazyDiode     2022-08-10     682

关键词:

在u-boot中,通过bootm命令启动内核。bootm命令的作用是将内核加载到指定的内存地址,然后通过R0、R1、R2寄存器传递启动参数之后启动内核。在启动内核之前需要对环境做一些初始化工作,主要有如下几个方面:

(1)、cpu 寄存器设置

    * R0 = 0

    * R1 = 板级 id

    * R2 = 启动参数在内存中的起始地址

(2)、cpu 模式

    * 禁止所有中断

    * 必须为SVC(超级用户)模式

(3)、缓存、MMU

    * 关闭 MMU

    * 指令缓存可以开启或者关闭

    * 数据缓存必须关闭并且不能包含任何脏数据

(4)、设备

    * DMA 设备应当停止工作

(5)、boot loader 需要跳转到内核镜像的第一条指令处

这些需求都由 boot loader 实现,在常用的 uboot 中完成一系列的初始化后最后通过 bootm 命令加载 linux 内核。bootm 将内核镜像从各种媒介中读出,存放在指定的位置;然后设置标记列表给内核传递参数;最后跳到内核的入口点去执行。

在分析u-boot源码之前,我们首先来分析一下u-boot中的命令格式。u-boot中每个命令都是通过 U_BOOT_CMD 宏来定义的,格式如下:

 U_BOOT_CMD(name,maxargs,repeatable,command,"usage","help")

各项参数的意义如下:

(1) -- name:命令的名字,注意,它不是一个字符串(不要用双引号括起来);

(2)-- maxargs:最大的参数个数;

(3)-- repeatable:命令是否可以重复,可重复是指运行一个命令后,下次敲回车即可再次运行;

(4)-- command:对应的函数指针,类型为(*cmd)(struct cmd_tbl_s *, int, int, char *[]);

(5) -- usage:简单的使用说明,这是个字符串;

(6)-- help:较详细的使用说明,这是个字符串。

下面就来具体分析一下bootm命令。bootm命令的源码路径为:u-boot源码路径/common/cmd_bootm.c

我们通过

U_BOOT_CMD(
    bootm,    CONFIG_SYS_MAXARGS,    1,    do_bootm, ...)

可以看出bootm命令的入口函数为d_bootm,下面我们就去看一下它的庐山真面目。

/*******************************************************************/
/* bootm - boot application image from image in memory */
/*******************************************************************/
int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
#ifdef CONFIG_ZIMAGE_BOOT
#define LINUX_ZIMAGE_MAGIC    0x016f2818
    image_header_t    *hdr;
    ulong        addr;
   //找到内核镜像的地址
    /* find out kernel image address */
    if (argc < 2) {
        addr = load_addr;
        debug ("*  kernel: default image load address = 0x%08lx
",
                load_addr);
    } else {
        addr = simple_strtoul(argv[1], NULL, 16);
    }
  //检查内核是否为zImage格式
    if (*(ulong *)(addr + 9*4) == LINUX_ZIMAGE_MAGIC) {
        u32 val;
        printf("Boot with zImage
");
    //将内核地址转换为物理地址
        //addr = virt_to_phys(addr);
        hdr = (image_header_t *)addr;
        hdr->ih_os = IH_OS_LINUX;
        hdr->ih_ep = ntohl(addr);
        //提取内核镜像的头信息
        memmove (&images.legacy_hdr_os_copy, hdr, sizeof(image_header_t));
    //保存头信息
        /* save pointer to image header */
        images.legacy_hdr_os = hdr;
        images.legacy_hdr_valid = 1;
        goto after_header_check;
    }
#endif
#ifdef CONFIG_NEEDS_MANUAL_RELOC
    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
  //加载内核
    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
");
            } else {
                puts ("ERROR: new format image overwritten - "
                    "must RESET the board to recover
");
                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);
#if defined(CONFIG_ZIMAGE_BOOT)
after_header_check:
    images.os.os = hdr->ih_os;
    images.ep = image_get_ep (&images.legacy_hdr_os_copy);
#endif
#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
",
            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 ("
## Control returned to monitor - resetting...
");
#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!
");
        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!
");
            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!
");
            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!
");
            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!
");
            show_boot_progress (-112);
            return 1;
        }
        break;
#endif
    default:
        puts ("ERROR: unknown image format type!
");
        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!
");
            return 1;
        }
#endif
    } else {
        puts ("Could not find kernel entry point!
");
        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
");
            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
");
            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!
");
        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!
");
            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!
");
            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!
");
            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!
");
            show_boot_progress (-112);
            return 1;
        }
        break;
#endif
    default:
        puts ("ERROR: unknown image format type!
");
        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!
");
            return 1;
        }
#endif
    } else {
        puts ("Could not find kernel entry point!
");
        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
");
            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
");
            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
");
        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
");
            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
", 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
");
    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) ...
",
           (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

static void setup_start_tag (bd_t *bd)
{
       //将tags的首地址也就是bi_boot_params传给kernel
        params = (struct tag *) bd->bi_boot_params;
        params->hdr.tag = ATAG_CORE;
        params->hdr.size = tag_size (tag_core);
        params->u.core.flags = 0;
        params->u.core.pagesize = 0;
        params->u.core.rootdev = 0;
        params = tag_next (params);
}
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来引导内核(代码与4412无关,是学6410时的笔记)

//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");
    
    }

 

tiny4412u-boot分析u-boot启动流程

从大方面来说,u-boot的启动分成两个阶段,第一个阶段主要的职责是准备初始化的环境,主要有以下几点①设置异常向量表②把CPU的工作模式设置为SVC32模式③关闭中断、MMU和cache④关闭看门狗⑤初始化内存、时钟、串口⑥设置... 查看详情

第二章tiny4412u-boot移植二启动分析

本文转载自:http://blog.csdn.net/eshing/article/details/37521481版权声明:本文为博主原创文章,未经博主允许不得转载。一、启动过程说明      讲解启动过程,首先的源头就是打开电源,这个相信没人人不知道。CPU... 查看详情

tiny4412-1312__uboot移植(代码片段)

...inaro提供的gcc-linaro-6.1.1-2016.08-x86_64_arm-linux-gnueabi要移植的u-boot版本:u-boot-2016-11Tiny4412开发板硬件版本为:  底板:  Tiny4412SDK1312B  核心板:Tiny4412-1306 1.获取U-BOOT源码从FTP站点下载: ftp://ftp.denx.de/pub/... 查看详情

tiny4412u-boot烧写及根文件系统制作(不进入终端问题)

http://m.blog.csdn.net/article/details?id=51400196(转) VMware12环境:ubuntu12.4开发板:tiny4412首先烧写bootloader,我用一个8G的内存卡,现在不说sd卡的制作过程了,网上可以参考。现在就把我给arm的emmc烧写过程开始说。用sd卡启动,开发... 查看详情

uboot移植前奏

  Tiny4412开发板硬件版本为:     底板: Tiny4412/Super4412SDK1506      核心板:Tiny4412-1412 1、下载u-boot源代码,建立u-boot代码仓库   参考:h 查看详情

tiny4412裸机程序说明

...动,只能尽可能找来各种相关资料进行参考分析。我想在U-Boot程序中加入LCD显示功能,但始终都没有正常点亮LCD,放弃的心都有了,后 查看详情

tiny4412学习杂记

1.Android挂载NFS使用busyboxmount来替代mount命令2.修改Uboot中fastboot最大buff 使用U-boot烧写Android5.0的时候出现remote:datatoolarge将 include/configs/tiny4412.h 改大一点3.tiny4412内核中trustzone配置开关tiny4412 trust 查看详情

linux内核lcd驱动分析与换屏方法(tiny4412)

​Linux内核中换屏技术​21.5.1u-boot中的参数bootargs实现换屏​在uboot中有一个bootargs环境变量,这个参数就是传递数据给内核的。​对tiny4412提供的内核,可以通过修改bootargs实现驱动不同的LCD屏。​21.5.2分析bootargs中的lcd参数再启... 查看详情

tiny4412标准版,编译u-boot并烧录到sd卡,从sd卡启动后只打印‘ok’两个字符

本人开发板是tiny4412(1G+4G)的标准版,由于开发板自带的光盘资料无法读取,所以就从友善官网下载到最新的资料;然而在做uboot烧录实验的时候,发现烧录完成从SD卡启动后只打印“OK”两个字符,无法正常... 查看详情

x-001friendlyarmtiny4412uboot移植前奏

...inaro提供的gcc-linaro-6.1.1-2016.08-x86_64_arm-linux-gnueabi要移植的u-boot版本:u-boot-2016-09参考u-boot版本:友善之臂提供的u-boot-2010-12Tiny4412开发板硬件版本 查看详情

tiny4412--uboot移植串口(代码片段)

...inaro提供的gcc-linaro-6.1.1-2016.08-x86_64_arm-linux-gnueabi要移植的u-boot版本:u-boot-2016-11Tiny4412开发板硬件版本为:  底板:  Tiny4412SDK1312B  核心板:Tiny4412-1306 1,原理图查看tiny4412SDK-1312B-Schematic.pdf   ... 查看详情

tiny4412--uboot移植时钟(代码片段)

...inaro提供的gcc-linaro-6.1.1-2016.08-x86_64_arm-linux-gnueabi要移植的u-boot版本:u-boot-2016-11Tiny4412开发板硬件版本为:  底板:  Tiny4412SDK1312B  核心板:Tiny4412-1306 1、时钟体系exynos4412芯片时钟体系的介绍在《Exynos4412... 查看详情

tiny4412标准版,编译u-boot并烧录到sd卡,从sd卡启动后只打印‘ok’两个字符

本人开发板是tiny4412(1G+4G)的标准版,由于开发板自带的光盘资料无法读取,所以就从友善官网下载到最新的资料;然而在做uboot烧录实验的时候,发现烧录完成从SD卡启动后只打印“OK”两个字符,无法正常... 查看详情

u-boot-2014-07代码流程分析

前言以前接触到的u-boot启动方式只有Flash和NandFlash这两种方式,最近接触是SD卡启动方式,SoC是S5P4418,启动方式也第一次接触到,根据S5P4418用户手册可以找到系统使用SD卡启动时,片内iROM内固化的代码会自动映射到0x00地址,也... 查看详情

u-boot启动流程详解-基于itop4412开发板

前言u-boot的作用:CPU上电后,需要设置很多状态,包括CPU状态、中断状态、MMU状态等,其次要做的就是对硬件资源经行板级初始化、代码重定向等,最后若不进入命令行模式,就会将linux内核从flash(NAND,NORFLASH,SD,MMC等)拷贝到D... 查看详情

u-boot分析与使用(代码片段)

文章目录一、u-boot介绍二、u-boot源码结构三、u-boot打补丁、编译、烧写四、uboot功能、结构,结合Makefile进行分析五、u-boot分析之源码阶段六、u-boot分析之命令实现七、uboot启动内核一、u-boot介绍u-boot即通用的BootLoader,是... 查看详情

10th.u-boot——代码结构分析

简介    U-Boot(UniversalBootLoader),通用BootLoader,一是可以引导多种操作系统,二是支持多种架构CPU。     教程中是在sourcefoge网站上下载的U-boot-1.1.6源码,进行分析与移植。 结构分析    U-boot-1.1.6有26个子目录... 查看详情

u-boot分析与使用(代码片段)

文章目录一、u-boot介绍二、u-boot源码结构三、u-boot打补丁、编译、烧写四、uboot功能、结构,结合Makefile进行分析五、u-boot分析之源码阶段六、u-boot分析之命令实现七、uboot启动内核一、u-boot介绍u-boot即通用的BootLoader,是... 查看详情