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

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

nanshan 2024-10-09 13:02 17 浏览 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. 安排好启动顺序。

相关推荐

使用nginx配置域名及禁止直接通过IP访问网站

前段时间刚搭建好这个网站,一直没有关注一个问题,那就是IP地址也可以访问我的网站,今天就专门研究了一下nginx配置问题,争取把这个问题研究透彻。1.nginx配置域名及禁止直接通过IP访问先来看n...

如何在 Linux 中使用 PID 号查找进程名称?

在Linux的复杂世界中,进程是系统运行的核心,每个进程都由一个唯一的「进程ID」(PID)标识。无论是系统管理员在排查失控进程,还是开发者在调试应用程序,知道如何将PID映射到对应的进程名称都是一项...

Linux服务器硬件信息查询与日常运维命令总结

1.服务器硬件信息查询1.1CPU信息查询命令功能描述示例lscpu显示CPU架构、核心数、线程数等lscpucat/proc/cpuinfo详细CPU信息(型号、缓存、频率)cat/proc/c...

Ubuntu 操作系统常用命令详解(ubuntu常用的50个命令)

UbuntuLinux是一款流行的开源操作系统,广泛应用于服务器、开发、学习等场景。命令行是Ubuntu的灵魂,也是高效、稳定管理系统的利器。本文按照各大常用领域,详细总结Ubuntu必学...

从 0 到 1:打造基于 Linux 的私有 API 网关平台

在当今微服务架构盛行的时代,API网关作为服务入口和安全屏障,其重要性日益凸显。你是否想过,不依赖商业方案,完全基于开源组件,在Linux上构建一个属于自己的私有API网关平台?今天就带你...

Nginx搭建简单直播服务器(nginx 直播服务器搭建)

前言使用Nginx+Nginx-rtmp-module在Ubuntu中搭建简单的rtmp推流直播服务器。服务器环境Ubuntu16.04相关概念RTMP:RTMP协议是RealTi...

Linux连不上网?远程卡?这篇网络管理指南你不能错过!

大家好!今天咱们聊个所有Linux用户都躲不开的“老大难”——网络管理。我猜你肯定遇到过这些崩溃时刻:新装的Linux系统连不上Wi-Fi,急得直拍桌子;远程服务器SSH连不上,提示“Connecti...

7天从0到上线!手把手教你用Python Flask打造爆款Web服务

一、为什么全网开发者都在疯学Flask?在当今Web开发的战场,Flask就像一把“瑞士军刀”——轻量级架构让新手3天速成,灵活扩展能力又能支撑百万级用户项目!对比Django的“重型装甲”,Flas...

nginx配置文件详解(nginx反向代理配置详解)

Nginx是一个强大的免费开源的HTTP服务器和反向代理服务器。在Web开发项目中,nginx常用作为静态文件服务器处理静态文件,并负责将动态请求转发至应用服务器(如Django,Flask,et...

30 分钟搞定 Docker 安装与 Nginx 部署,轻松搭建高效 Web 服务

在云计算时代,利用容器技术快速部署应用已成为开发者必备技能。本文将手把手教你在阿里云轻量应用服务器上,通过Docker高效部署Nginx并发布静态网站,全程可视化操作,新手也能轻松上手!一、准...

Nginx 配置实战:从摸鱼到部署,手把手教你搞定生产级配置

各位摸鱼搭子们!今天咱不聊代码里的NullPointerException,改聊点「摸鱼必备生存技能」——Nginx配置!先灵魂拷问一下:写了一堆接口却不会部署?服务器被恶意请求打崩过?静态资源加载...

如何使用 Daphne + Nginx + supervisor部署 Django

前言:从Django3.0开始支持ASGI应用程序运行,使Django完全具有异步功能。Django目前已经更新到5.0,对异步支持也越来越好。但是,异步功能将仅对在ASGI下运行的应用程序可用...

Docker命令最全详解(39个最常用命令)

Docker是云原生的核心,也是大厂的必备技能,下面我就全面来详解Docker核心命令@mikechen本文作者:陈睿|mikechen文章来源:mikechen.cc一、Docker基本命令doc...

ubuntu中如何查看是否已经安装了nginx

在Ubuntu系统中,可以通过以下几种方法检查是否已安装Nginx:方法1:使用dpkg命令(适用于Debian/Ubuntu)bashdpkg-l|grepnginx输出...

OVN 概念与实践(德育概念的泛化在理论和实践中有什么弊端?)

今天我们来讲解OVN的概念和基础实践,要理解本篇博客的内容,需要前置学习:Linux网络设备-Bridge&VethPairLinux网络设备-Bridge详解OVS+Fa...

取消回复欢迎 发表评论: