百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

Linux嵌入式启动以及优化(linux嵌入式gui)

nanshan 2024-10-09 13:02 12 浏览 0 评论

Original address: http://blog.21ic.com/user1/5593/archives/2010/67071.html

以前写了一篇Linux PC启动过程的日记, 最近项目中, 想优化一下启动过程, 减少启动时间.

因此研究了我们项目的启动全过程.

1. 第一步: BootLoader -- U boot

1.1 在cpu/arm926ejs/start.s中

  1. b reset ; //jump to reset
  1. set cpsr ;svc mode ,disable I,F interrupt
  1. 调用lowlevel_init (在board\xxxx\lowlevel_init.S中), 将调用
  • __platform_cmu_init (设置cpu时钟,启动那些模块等)
  • __platform_mpmc_init (mpmc初始化,配置SDRAM时序)
  • __platform_static_memory_init
  • __platform_static_uart_init
  • __platform_mpmc_clear
  1. 用LDMIA,STMIA命令 copy uboot 到内存中
  1. ldr pc ,_start_armboot

执行start_armboot

1.2 start_armboot 在 lib-arm 中

  1. 根据init_sequence 执行初始化序列, 包括:
  • cpu_init
  • board_init
  • 中断初始化
  • initialize environment
  • initialze baudrate settings
  • serial communications setup
  • 打印uboot 版本
  • display_dram_config (打印DRAM大小)

而在board_init中

  • 将打印公司名称, 前后还加了delay
  • timer 初始化
  • dw_init --- I2C 设置
  • 验证时钟来源 (来自wifi还是DECT)
  • LCD初始化
  • 键盘初始化
  • Flash 初始化 (空函数)
  • 网卡初始化 (其中有个udelay(1000) 1ms的delay )
  1. NOR FLASH 初始化

display_flash_config (打印Flash大小)

  1. nand 初始化 (将scan整个nand chip,建立 bbt table)
  1. env_relocate 环境变量重新定位到内存中
  1. 得到IP 地址和网卡 MAC地址
  1. devices_init
  1. 中断enable

然后: start_armboot --> main_loop

1.3 main_loop在 common/main.c中

getenv("bootdelay")
    --> 循环 readline
        run_command

2. 第二步: Kernel

2.1 Kernel自解压

arch\arm\boot\compressed\head.S中调用decompress_kernel(misc.c),完了打印出"done,booting the kernel", 然后根据arch_id = 多少, 打印出 arch_id

2.2

在arch\arm\kernel\head.S中

  • check cpu 以及 machine ID
  • build the initial 页表
  • _switch_data (arm\kernel\head_common.s中) 将process id存入process_id变量中
  • start_kernel

2.3 start_kernel

  1. 打印Linux version information
  1. call setup_arch,(它将打印cpu特定的信息,

machine look_machine_type -> arm\tools\mach_types look_processor_type --> .proc.info.init. -->arm\mm\proc_arm926.S

在/arm\mach_xx\xx.c中,有MACHINE_START(….)

  1. 打印commnad_line
  1. 初始化

vfs_caches_init

虚拟文件系统VFS初始化,主要初始化dentry等,它将调用 mnt_init. 而mnt_init将调用init_rootfs,注册rootfs文件系统,init_mount_tree()创建rootfs文件系统,会把rootfs挂载到/目录.

  1. rest_init

启动init kernel thread

在init 线程中:

  1. populate_rootfs()

函数负责加载initramfs.

我们的系统没有定义CONFIG_BLK_DEV_INITRD,因此populate_rootfs什么也没做

  1. do_basic_setup

-->driver_init()->platform_bus_init()->…初始化platform bus(虚拟总线)

这样以后设备向内核注册的时候platform_device_register()->platform_device_add()->…内核把设备挂在虚拟的platform bus下,

驱动注册的时候 platform_driver_register()->driver_register()->bus_add_driver()->driver_attach()->bus_for_each_dev() 对每个挂在虚拟的platform bus的设备作 __driver_attach()->driver_probe_device()->drv->bus->match()==platform_match()->比较strncmp(pdev->name, drv->name, BUS_ID_SIZE),如果相符就调用platform_drv_probe()->driver->probe(),如果probe成 功则绑定该设备到该驱动.

好象声卡怎么先注册驱动,再注册设备呢?反了?

-->do_initcalls

而do_initcalls将调用__initcall_start到__initcall_end中的所有函数

__initcall_start和__initcall_end定义在arch/arm/kernel/vmlinux.lds.S中

它是这样定义的:

__initcall_start = .; *(.initcall1.init) *(.initcall2.init) *(.initcall3.init) *(.initcall4.init) *(.initcall5.init) *(.initcall6.init) *(.initcall7.init) __initcall_end = .;

而在include/linux/init.h中

#define core_initcall(fn) __define_initcall("1",fn) #define postcore_initcall(fn) __define_initcall("2",fn) #define arch_initcall(fn) __define_initcall("3",fn) #define subsys_initcall(fn) __define_initcall("4",fn) #define fs_initcall(fn) __define_initcall("5",fn) #define device_initcall(fn) __define_initcall("6",fn) #define late_initcall(fn) __define_initcall("7",fn)

其中

#define __define_initcall(level,fn) \ static initcall_t __initcall_##fn __attribute_used__ \ __attribute__((__section__(".initcall" level ".init"))) = fn

这说明core_initcall宏的作用是将函数指针(注意不是函数体本身)将放在.initcall1.init section中, 而device_initcall宏将函数指针将放在.initcall6.init section中.

函数本身用_init标识,在include/linux/init.h中

#define __init __attribute__ ((__section__ (".init.text")))

这些_init函数将放在.init.text这个区段内.函数的摆放顺序是和链接的顺序有关的,是不确定的。

因此函数的调用顺序是:

core_initcall postcore_initcall 如amba_init arch_init 如 subsys_initcall fs_initcall device_initcall ---> module_init late_initcall

先调用core_initcall区段中的函数,最后调用late_initcall中的函数,而对于上述7个区段中每个区段中的函数指针,由于其摆放顺序和链接的顺序有关的,是不确定的,因此其调用顺序也是不确定的.

  1. rootfs 加载

prepare_namespace 挂载真正的根文件系统,

在do_mounts.c中:

c static int __init root_dev_setup(char *line) { strlcpy(saved_root_name, line, sizeof(saved_root_name)); return 1; } __setup("root=", root_dev_setup);

也就是说: 在bootargs中root=/dev/nfs rw 或者 root=/dev/mtdblock4等将传入saved_root_name.

c void __init prepare_namespace(void) { int is_floppy; mount_devfs(); if (root_delay) { printk(KERN_INFO "Waiting %dsec before mounting root device...\n", root_delay); ssleep(root_delay); } md_run_setup(); if (saved_root_name[0]) { root_device_name = saved_root_name; //保存在root_device_name中 ROOT_DEV = name_to_dev_t(root_device_name); //在root_dev.h中定义了Root_NFS,Root_RAM0等结点号 if (strncmp(root_device_name, "/dev/", 5) == 0) root_device_name += 5; } is_floppy = MAJOR(ROOT_DEV) == FLOPPY_MAJOR; if (initrd_load()) goto out; if (is_floppy && rd_doload && rd_load_disk(0)) ROOT_DEV = Root_RAM0; mount_root(); //加载rootfs out: umount_devfs("/dev"); sys_mount(".", "/", NULL, MS_MOVE, NULL); sys_chroot("."); security_sb_post_mountroot(); mount_devfs_fs (); }

  1. yaffs2_read_super被调用来建立文件系统, 它scan所有的block
  1. free_initmem

释放init内存

  1. 打开/dev/console

失败则会打印:

printk(KERN_WARNING "Warning: unable to open an initial console.\n");

  1. 判断是否有execute_command,这个参数是在uboot参数的bootargs中init=xxx ,如果定义了的话则执行 run_init_process(execute_command).

可以通过这种方法实现自己的init process,

或者可以init=/linuxrc, 这样执行linuxrc

  1. 如果没有execute_command, init kernel线程缺省的也是最后的步骤是:

run_init_process("/sbin/init"); run_init_process("/etc/init"); run_init_process("/bin/init"); run_init_process("/bin/sh");

如果/sbin/init没有, 则执行/etc/init.

/etc/init没有则执行/bin/init, 如果这四者都没有, 则Linux打印

panic("No init found. Try passing init= option to kernel.");

3. 第三步: Init Process

run_init_process也就是调用execve, 这样就启动了init process

上面的/sbin/init,/etc/init,/bin/init,/bin/sh这四者都指向busybox, 但对于/bin/sh则只是打开shell, 然后等待用户命令.

而对于/sbin/init ,将分析/etc/inittab.

在/etc/inittab中

  1. id:5:initdefault: 缺省的runlevel x
  1. si::sysinit:/etc/init.d/rcS

执行 rcS脚本

  1. l5:5:wait:/etc/init.d/rc 5
  1. S:2345:respawn:/sbin/getty 38400 ttyDW0

getty提示用户输入username, 然后调用login, login的参数为username, 登录后启动了shell

如果修改为 /bin/sh 则直接启动shell, 此时你可以输入命令 比如ls

在/etc/init.d/rcS中

  1. mount proc 文件系统
  1. /etc/default/rcS (设置一些参数)
  1. exec /etc/init.d/rc S

执行 /etc/init.d/rc S --> 这样将执行/etc/rcS.d中以S开头的脚本

S00psplash.sh psplash S02banner.sh make node /dev/tty S03sysfs.sh mount sysfs S03udev 启动udev S06alignment.sh 为什么为3? S10checkroot.sh 读取fatab ,mount 这些文件系统 S20modutils.sh 加载module S35mountall.sh 不做什么事情 S37populate-volatile.sh S38devpts.sh mount devpts File System S39hostname.sh set hostname to /etc/hostname S40networking ifup -a to up the lo interface S45mountnfs.sh read /etc/fstab to whether NFS exists and then mount the NFS S55bootmisc.sh 调用/etc/init.d/hwclock.sh去设置时间,日期等 S60ldconfig.sh ldconfig建立库的路径

l5:5:wait:/etc/init.d/rc 5将执行 /etc/rc5.d/ 依次为:

S00qpe 启动qpe S02dbus-1 D_BUS dameon S10dropbear SSH service S20cron 自动执行指定任务的程序 cron , in etc/crontab , ntpd will run to get the NTP time S20ntpd Not used , should delete S20syslog run /sbin/klogd S39wifiinit.sh wifi init and calibration S70regaccess mknod regaccess.ko S99rmnologin.sh do nothing since DELAYLOGIN = no in /etc/default/rcS

整个系统启动后 ,将有 25 个进程 :其中12个内核的进程 ,13个用户进程

    1 root       1488 S   init [5]   
    2 root            SWN [ksoftirqd/0]
    3 root            SW< [events/0]
    4 root            SW< [khelper]
    5 root            SW< [kthread]
   12 root            SW< [kblockd/0]
   13 root            SW< [kseriod]
   41 root            SW  [pdflush]
   42 root            SW  [pdflush]
   43 root            SW  [kswapd0]
   44 root            SW< [aio/0]
  152 root            SW  [mtdblockd]
  208 root       1700 S < /sbin/udevd -d 
  343 root      36104 S   qpe 
  357 messagebus   2080 S   /usr/bin/dbus-daemon --system 
  361 root       2072 S   /usr/sbin/dropbear -r /etc/dropbear/dropbear_rsa_host
  364 root       1656 S   /usr/sbin/cron 
  369 root       2712 S   /sbin/klogd -n 
  394 root       2884 S   -sh 
  400 root      20396 S   /opt/Qtopia/bin/MainMenu -noshow 
  401 root      19196 S   /opt/Qtopia/bin/Settings -noshow 
  402 root      20504 S   /opt/Qtopia/bin/Organizer -noshow 
  403 root      20068 S   /opt/Qtopia/bin/Photo -noshow 
  404 root      34488 S N /opt/Qtopia/bin/quicklauncher 
  411 root      34488 S N /opt/Qtopia/bin/quicklauncher 

4. 优化:

uboot:

  1. setenv bootcmd1 "nand read.jffs2 0x62000000 kernel 0x180000 ; bootm 62000000"

这样 load内核的时候 从以前0x300000的3M->1.5M 省1S

2.setenv bootdelay 1 从2变为0 加上CONFIG_ZERO_BOOTDELAY_CHECK

  1. quiet=1

bootargs=root=/dev/mtdblock4 rootfstype=yaffs2 console=ttyDW0 mem=64M mtdparts=dwnand:3m(kernel),3m(splash),64m(rootfs),-(userdata);dwflash.0:384k(u-boot),128k(u-boot_env) quiet

加上quiet 省不到1S

  1. 启动的时候不扫描整个芯片的坏块, 因为uboot只会用到kernel和splash区,只需要检验这两个区的坏块。

可以省不到 0.2s ,没什么明显的改进

  1. 将环境变量verify设置为n, 这样load kernel后, 不会去计算校验kernel image的checksum
  1. 开始打印公司 这些可以去掉 ,在这里还有delay ,以及其他的一些不必要的打印 ,一起去掉
  1. 修改memcpy函数 在./lib_generic/string.c下:
/* Nonzero if either X or Y is not aligned on a "long" boundary.  */
#define UNALIGNED(X, Y) \
  (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))

/* How many bytes are copied each iteration of the 4X unrolled loop.  */
#define BIGBLOCKSIZE    (sizeof (long) << 2)

/* How many bytes are copied each iteration of the word copy loop.  */
#define LITTLEBLOCKSIZE (sizeof (long))

/* Threshhold for punting to the byte copier.  */
#define TOO_SMALL(LEN)  ((LEN) < BIGBLOCKSIZE)
void * memcpy(void * dst0,const void *src0,size_t len0)
{
  char *dst = dst0;
  const char *src = src0;
  long *aligned_dst;
  const  long *aligned_src;
  int   len =  len0;
  /* If the size is small, or either SRC or DST is unaligned,
     then punt into the byte copy loop.  This should be rare.  */
  if (!TOO_SMALL(len) && !UNALIGNED (src, dst))
    {
      aligned_dst = (long*)dst;
      aligned_src = (long*)src;

      /* Copy 4X long words at a time if possible.  */
      while (len >= BIGBLOCKSIZE)
        {
          *aligned_dst++ = *aligned_src++;
          *aligned_dst++ = *aligned_src++;
          *aligned_dst++ = *aligned_src++;
          *aligned_dst++ = *aligned_src++;
          len -= BIGBLOCKSIZE;
        }

      /* Copy one long word at a time if possible.  */
      while (len >= LITTLEBLOCKSIZE)
        {
          *aligned_dst++ = *aligned_src++;
          len -= LITTLEBLOCKSIZE;
        }

       /* Pick up any residual with a byte copier.  */
      dst = (char*)aligned_dst;
      src = (char*)aligned_src;
    }

  while (len--)
    *dst++ = *src++;

  return dst0;
}

(在linux 中,arm的memcpy有优化的版本 , 在/arch/arm/lib/memcpy.S中)

下面2个建议,没试过:

  1. 在环境变量区的末尾, 存有CRC,启动的时候会校验CRC ,去掉可以省一些时间
  1. 把一些驱动的初始化在正常启动的时候不执行,当用户按了键,进入uboot命令模式的时候执行
  1. 修改SDRAM控制器时序

Kernel:

启动时间 有两种方法 :

  1. 在u-boot的 bootargs 中加上参数 time
  1. 在内核的 kernel hacking 中选择 PRINTK_TIME

方法2的好处是可以得到内核在解析command_line前所有信息的时间, 而之前会有:打印linux 版本信息,CPU D cache , I cache 等等 。。。

启动完后用 :

dmesg -s 131072 > ktime

然后用:

/usr/src/linux-x.xx.xx/s/show_delta ktime > dtime

这样得到启动内核时间的报告

  1. 修改Nand驱动 提高读速度
  1. 从 JFFS2 换成 yaffs
  1. kernel变为非压缩的image, 但这样的话内核变大了, 从NAND中搬运内核的时间将变长, 所以需要测试是否使得时间变短

建议:

  1. 把delay的calibration去掉

上面改动后基本上8s从开机到 Freeing init memory

Application:

  1. udev 启动很花时间
  1. 安排好启动顺序。

相关推荐

Linux下C++访问web—使用libcurl库调用http接口发送解析json数据

一、背景这两天由于一些原因研究了研究如何在客户端C++代码中调用web服务端接口,需要访问url,并传入json数据,拿到返回值,并解析。 现在的情形是远程服务端的接口参数和返回类型都是json的字符...

干货 | 这 3 个超经典的Linux实战项目,让你分分钟入门Linux系统

编译安装nginx搭建小游戏网站编译安装流程下载nginx代码wget-P/server/tools/http:nginx.org/download/nginx1.22.0.tar.gz解压并进...

权限管理-树莓派linux⑦

前言当你在看这篇README,我感到非常荣幸。作为支持开源、分享的理念的我,给大家带来一些学习上的乐趣。由于本人并非专业的教育领域人士,很多时候天马行空,随心所欲的表达方式,可能让部分人感到不适。请根...

每天Linux学习:linux文件属性

ls-lih先通过这个命令来观察(-l列表显示目录内容详细,-i第一列显示inode,-h将文件大小显示为我们常见的kb,mb等单位)从截图中我们能看出文件属性由这9列信息组成:第1列:inod...

Linux ln、unlink命令用法

ln命令可以用来创建软链接或硬链接。1.创建软链接:ln-s源文件目标文件例如:ln-s/usr/lib/libc.so/usr/local/lib/libc.so.6这样就创建了一...

Linux 系统启动完整流程

一、启动系统流程简介如上图,简述系统启动的大概流程:1:硬件引导UEFi或BIOS初始化,运行POST开机自检2:grub2引导阶段系统固件会从MBR中读取启动加载器,然后将控制权交给启动加载器GRU...

最火的 CI/CD 平台 Jenkins 详细搭建教程(for Linux)

在正式学习Jenkins之前我们需要对两个名词有一定了解,其一是DevOps,另外一个就是CI/CD。何为DevOps?来自wiki百科介绍DevOps是一系列软件开发实践,强调开发人员(Dev)和测...

hadoop集群搭建详细方法

第一步:搭建配置新的虚拟机格式化之前先把tmp目录下所有与Hadoop有关的信息全部删除rm-rf/tmp/hadoop-centos*开启之后jps只有Java的进程:sudovi/et...

Linux 常用命令集合

系统信息arch显示机器的处理器架构(1)uname-m显示机器的处理器架构(2)uname-r显示正在使用的内核版本dmidecode-q显示硬件系统部件-(SMBIOS/DM...

inode文件索引,你了解嘛?你的Linux基础真的扎实嘛?

一、inode是什么?深入了解inode,就要从文件存储说起来!文件储存在硬盘上,硬盘的最小存储单位叫做"扇区"(Sector)。每个扇区储存512字节。读取硬盘的时候,不会一个个扇区地读取,这样效率...

linux实例之创建service服务

前面我们讲过可以通过service命令来启动,重启,停止指定的服务程序。service服务可以在系统启动时,自动运行该服务,我们可以利用这一特点,创建service文件,并且让系统重启时,自动执行命令...

linux之软连接和硬连接的区别

硬连接硬链接是通过索引节点进行的链接。在Linux中,多个文件指向同一个索引节点是允许的,像这样的链接就是硬链接。硬链接只能在同一文件系统中的文件之间进行链接,不能对目录进行创建。如果删除硬链接对应的...

Linux inode 详解

简介索引节点(IndexNode)是Linux/类unix系统文件系统上的一种数据结构,用于存储有关文件或目录的元数据。它包含文件的所有信息,除了文件名和数据。inode在文件系统如何存储和检...

Bash 脚本实例:获取符号链接的目标位置

我们都熟悉Linux中的符号链接,通常称为符号链接或软链接,符号链接是指向任何文件系统中的另一个文件或目录的特定文件。本文将介绍Linux中符号链接的基础知识,并创建一个简单的bash脚本...

windows快捷方式,符号链接,软链接和硬链接

当一个软件大量的向C盘写入数据,而我们又无法修改软件保存数据的位置时,可以使用windows系统的“符号链接”(SymbolicLink)功能,将保存数据的位置修改到其它分区中。符号链接类似于我们熟...

取消回复欢迎 发表评论: