Linux的 __setup解析 -- 命令行处理

Linux的 __setup解析 – 命令行处理

  • 名称: __setup
  • 作用: 命令行处理,处理启动命令行(来自dts或者在编译内核的时候写到CONFIG_CMDLINE中)的相关参数
  • 示例
/**
 *  video_setup - process command line options
 *  @options: string of options                                                                                                                                                                        
 *
 *  Process command line options for frame buffer subsystem.
 *
 *  NOTE: This function is a __setup and __init function.
 *            It only stores the options.  Drivers have to call
 *            fb_get_options() as necessary.
 *
 *  Returns zero.
 *
 */
static int __init video_setup(char *options)
{
    int i, global = 0;

    if (!options || !*options)
        global = 1;

    if (!global && !strncmp(options, "ofonly", 6)) {
        ofonly = 1;
        global = 1;
    }

    if (!global && !strchr(options, ':')) {
        fb_mode_option = options;
        global = 1;
    }

    if (!global) {
        for (i = 0; i < FB_MAX; i++) {
            if (video_options[i] == NULL) {
                video_options[i] = options;
                break;
            }

        }
    }

    return 1;
}
__setup("video=", video_setup);

根据u-boot传参:

BOARD_KERNEL_CMDLINE := console=ttymxc0,115200 init=/init video=mxcfb0:dev=hdmi,1280x720M@60,if=RGB24,bpp=32 video=mxcfb1:off video=mxcfb2:off vmalloc=400M androidb
oot.console=ttymxc0 androidboot.hardware=freescale

所以video=mxcfb0 mxcfb1 mxcfb2 会作为参数options传入函数video_setup()

拓展:命令行的来源


arch/arm/Kconfig

config CMDLINE
    string "Default kernel command string"
    default ""
    help
      On some architectures (EBSA110 and CATS), there is currently no way
      for the boot loader to pass arguments to the kernel. For these
      architectures, you should supply some command-line options at build
      time by entering them here. As a minimum, you should specify the
      memory size and the root device (e.g., mem=64M root=/dev/nfs).

choice
    prompt "Kernel command line type" if CMDLINE != ""
    default CMDLINE_FROM_BOOTLOADER

config CMDLINE_FROM_BOOTLOADER
    bool "Use bootloader kernel arguments if available"
    help
      Uses the command-line options passed by the boot loader. If
      the boot loader doesn't provide any, the default kernel command
      string provided in CMDLINE will be used.

config CMDLINE_EXTEND
    bool "Extend bootloader kernel arguments"
    help
      The command-line arguments provided by the boot loader will be
      appended to the default kernel command string.

config CMDLINE_FORCE
    bool "Always use the default kernel command string"
    help
      Always use the default kernel command string, even if the boot
      loader passes other arguments to the kernel.
      This is useful if you cannot or don't want to change the
      command-line options your boot loader passes to the kernel.
endchoice

Kconfig choice


  • choice条目将多个类似的配置选项组合在一起,供用户单选或多选,这不同于menu条目

  • prompt “Kernel command line type” if CMDLINE != ""给出提示信息“ARM system type”,光标选中后回车进入就可以看到多个config条目定义的配置选项

  • choice条目中定义的变量只有bool和tristate

Boot options ->
    Default kernel command string      # 设置默认的启动命令

Boot options ->
    Kernel command line type
        -> () Use bootloader kernel arguments if available  # 从bootloader中获取启动参数(如上面的例子)
           () Extend bootloader kernel arguments
           () Always use the default kernel command string  # 使用上面设置的默认启动行

你可能感兴趣的:(Linux)