微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 手机设计讨论 > MTK手机平台交流 > 怎么才能将LK的变量,在KERNEL中读取使用?

怎么才能将LK的变量,在KERNEL中读取使用?

时间:10-02 整理:3721RD 点击:
怎么才能将LK的变量,在KERNEL中读取使用?

写个简单版本的,未亲测
以lk传递lcm的名称为例说明:
lk的文件位置
       ./bootable/bootloader/lk/app/mt_boot/mt_boot.c
        custom_port_in_kernel(g_boot_mode, commanline);
        //FIXME, Waiting LCM Driver owner to fix it
            strlen += sprintf(commanline, "%s lcm=%1d-%s", commanline, DISP_IsLcmFound(), mt_disp_get_lcm_id());
            strlen += sprintf(commanline, "%s fps=%1d", commanline, mt_disp_get_lcd_time());

            boot_linux((void *)CFG_BOOTIMG_LOAD_ADDR, (unsigned *)CFG_BOOTARGS_ADDR,
                   (char *)commanline, board_machtype(),
                   (void *)CFG_RAMDISK_LOAD_ADDR, g_rimg_sz);
kernel的读取位置
        ./mediatek/kernel/drivers/video/mtkfb.c
        BOOL mtkfb_find_lcm_driver(void)
        {
                    BOOL ret = FALSE;
                    char *p, *q;
                    p = strstr(saved_command_line, "lcm=");

用 __setup
__setup这条宏在Linux Kernel中使用最多的地方就是定义处理Kernel的启动参数的函数及数据结构

  1. static int __init hw_configuration(char *str)
  2. {
  3.         char cfg[30];
  4.         char *arg1, *arg2;

  5.         memset(cfg, 0, 30);
  6.         strcpy(cfg, str);
  7.         arg1 = strchr(cfg, ',');
  8.         memcpy(hw_config, ++arg1, 8);
  9.         printk("hw_configuration: hw_config=%s\n", hw_config);

  10.         arg2 = strchr(arg1, ',');
  11.         memcpy(sw_config, ++arg2, 3);
  12.         printk("hw_configuration: sw_config=%s\n", sw_config);
  13.        
  14.         memcpy(board_cfg.idx, hw_config, 1);
  15.         memcpy(lcm_cfg.idx, hw_config+1, 1);
  16.         memcpy(tp_cfg.idx, hw_config+2, 1);  
  17.         memcpy(ps_cfg.idx, hw_config+3, 1);  
  18.         memcpy(acc_cfg.idx, hw_config+4, 1);   
  19.         memcpy(bt_cfg.idx, hw_config+5, 1);  
  20.         memcpy(mcp_cfg.idx, hw_config+6, 1);
  21.         memcpy(res2_cfg.idx, hw_config+7, 1);

  22.         if(strcmp(lcm_cfg.idx, "1") == 0)
  23.                 strcpy(lcm_cfg.name, "hx8357c_ysm");
  24.         else if(strcmp(lcm_cfg.idx, "2") == 0)
  25.                 strcpy(lcm_cfg.name, "hx8357d_ysm");
  26.         else if(strcmp(lcm_cfg.idx, "3") == 0)
  27.                 strcpy(lcm_cfg.name, "hx8357d_tm");
  28.         else if(strcmp(lcm_cfg.idx, "5") == 0)
  29.                 strcpy(lcm_cfg.name, "hx8357d_tm");
  30.         else if(strcmp(lcm_cfg.idx, "6") == 0)
  31.                 strcpy(lcm_cfg.name, "nt35510_xxd");
  32.         else if(strcmp(lcm_cfg.idx, "9") == 0)
  33.                 strcpy(lcm_cfg.name, "sh1282_ysm");
  34.         else if(strcmp(lcm_cfg.idx, "a") == 0)
  35.                 strcpy(lcm_cfg.name, "rm68171_blj");
  36.         else if(strcmp(lcm_cfg.idx, "b") == 0)
  37.                 strcpy(lcm_cfg.name, "8018b_xxd");
  38.         else
  39.                 strcpy(lcm_cfg.name, "hx8357c_ysm");

  40.         printk("lcm_cfg: idx=%s, name=%s\n", lcm_cfg.idx, lcm_cfg.name);


  41.         if(strcmp(tp_cfg.idx, "1") == 0)
  42.                 strcpy(tp_cfg.name, "gsl1680_huihai");
  43.         else if(strcmp(tp_cfg.idx, "2") == 0)
  44.                 strcpy(tp_cfg.name, "gsl1680_hefang");
  45.         else if(strcmp(tp_cfg.idx, "3") == 0)
  46.                 strcpy(tp_cfg.name, "gsl1688e_hefang");
  47.         else if(strcmp(tp_cfg.idx, "4") == 0)
  48.                 strcpy(tp_cfg.name, "gsl1688e_zhengxing");

  49. /*-------------------- for new r360 tp start --------------------*/       
  50.         else if(strcmp(tp_cfg.idx, "5") == 0)
  51.                 strcpy(tp_cfg.name, "gsl1688e_duocai_om");
  52.         else if(strcmp(tp_cfg.idx, "6") == 0)
  53.                 strcpy(tp_cfg.name, "gsl1688e_dongyu");
  54.         else if(strcmp(tp_cfg.idx, "7") == 0)
  55.                 strcpy(tp_cfg.name, "gsl1688e_green_gg");
  56. /*-------------------- for new r360 tp end --------------------*/       

  57. /*-------------------- for r40v1 tp start --------------------*/       
  58.         else if(strcmp(tp_cfg.idx, "a") == 0)
  59.                 strcpy(tp_cfg.name, "gsl1688e_hc_ogs");       
  60.         else if(strcmp(tp_cfg.idx, "b") == 0)
  61.                 strcpy(tp_cfg.name, "gsl1688e_green40v1_gg");       
  62. /*-------------------- for r40v1 tp end --------------------*/       


  63. /*-------------------- for r390 tp start --------------------*/       
  64.         else if(strcmp(tp_cfg.idx, "8") == 0)
  65.                 strcpy(tp_cfg.name, "gsl1688e_chenhe");
  66. /*-------------------- for r390 tp end --------------------*/       
  67.         else
  68.                 strcpy(tp_cfg.name, "gsl1688e_hefang");
  69.        
  70.         printk("tp_cfg: idx=%s, name=%s\n", tp_cfg.idx, tp_cfg.name);


  71.         if(strcmp(ps_cfg.idx, "1") == 0)
  72.                 strcpy(ps_cfg.name, "ltr502_2in1");
  73.         else if(strcmp(ps_cfg.idx, "2") == 0)
  74.                 strcpy(ps_cfg.name, "epl6881_3in1");
  75.         else if(strcmp(ps_cfg.idx, "3") == 0)
  76.                 strcpy(ps_cfg.name, "tpasps");
  77.         else
  78.                 strcpy(ps_cfg.name, "ltr502_2in1");

  79.         printk("ps_cfg: idx=%s, name=%s\n", ps_cfg.idx, ps_cfg.name);


  80.         if(strcmp(acc_cfg.idx, "1") == 0)
  81.                 strcpy(acc_cfg.name, "mma7660");
  82.         else if(strcmp(acc_cfg.idx, "2") == 0)
  83.                 strcpy(acc_cfg.name, "mc3230");
  84.         else if(strcmp(acc_cfg.idx, "3") == 0)
  85.                 strcpy(acc_cfg.name, "stk8312");
  86.         else
  87.                 strcpy(acc_cfg.name, "mc3230");

  88.         printk("acc_cfg: idx=%s, name=%s\n", acc_cfg.idx, acc_cfg.name);


  89.         if(strcmp(bt_cfg.idx, "1") == 0)
  90.                 strcpy(bt_cfg.name, "withbt");
  91.         else if(strcmp(bt_cfg.idx, "2") == 0)
  92.                 strcpy(bt_cfg.name, "nobt");
  93.         else
  94.                 strcpy(bt_cfg.name, "nobt");

  95.         printk("bt_cfg: idx=%s, name=%s\n", bt_cfg.idx, bt_cfg.name);

  96.         if(strcmp(board_cfg.idx, "1") == 0)
  97.                 strcpy(board_cfg.name, "R360");
  98.         else if(strcmp(board_cfg.idx, "2") == 0)
  99.                 strcpy(board_cfg.name, "R360C");
  100.         else if(strcmp(board_cfg.idx, "3") == 0)
  101.                 strcpy(board_cfg.name, "R40V1C");
  102.         else if(strcmp(board_cfg.idx, "4") == 0)
  103.                 strcpy(board_cfg.name, "R40V1");
  104.         else if(strcmp(board_cfg.idx, "5") == 0)
  105.                 strcpy(board_cfg.name, "R360SC");
  106.         else if(strcmp(board_cfg.idx, "6") == 0)
  107.                 strcpy(board_cfg.name, "R360S");
  108.         else if(strcmp(board_cfg.idx, "7") == 0)
  109.                 strcpy(board_cfg.name, "R395C");
  110.         else if(strcmp(board_cfg.idx, "8") == 0)
  111.                 strcpy(board_cfg.name, "R395");               
  112.         else
  113.                 strcpy(board_cfg.name, "R360");

  114.         printk("board_cfg: idx=%s, name=%s\n", board_cfg.idx, board_cfg.name);

  115.         return 1;
  116. }

  117. __setup("cfg=", hw_configuration);

复制代码


以上这段代码就是处理cmdline传过来的参数, cfg  hw_configuration就是处理函数。

__setup("cfg=", hw_configuration);
关键就是这句代码, 处理函数根据自己从cmdline传过来的参数自己写。
我从cmdline传过来的参数是这样的:
"console=ttyMSM0,115200,n8 androidboot.hardware=qcom loglevel=1 cfg=H80,49b30040,018";
H80,49b30040,018 这个参数就是我的 hw_configuration 需要处理的。

mark,之前一直看__setup看不懂,还请详细解释一下这个宏。还有cmdline是什么。

你这个没有LK的代码?
你在LK中是怎么用cmdline传过来的?
你这个只是kernel的代码吧?

在aboot.c里面有个函数 boot_linux_from_flash() (我这个是nandflash版本)
emmc应该是 boot_linux_from_mmc()
我贴出代码:


  1. int boot_linux_from_flash(void)
  2. {
  3.         struct boot_img_hdr *hdr = (void*) buf;
  4.         unsigned n;
  5.         struct ptentry *ptn;
  6.         struct ptable *ptable;
  7.         unsigned offset = 0;
  8.         const char *cmdline;

  9.         if (target_is_emmc_boot()) {
  10.                 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
  11.                 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
  12.                         dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
  13.                         return -1;
  14.                 }
  15.                 goto continue_boot;
  16.         }

  17.         ptable = flash_get_ptable();
  18.         if (ptable == NULL) {
  19.                 dprintf(CRITICAL, "ERROR: Partition table not found\n");
  20.                 return -1;
  21.         }

  22.         if(!boot_into_recovery)
  23.         {
  24.                 ptn = ptable_find(ptable, "boot");
  25.                 if (ptn == NULL) {
  26.                         dprintf(CRITICAL, "ERROR: No boot partition found\n");
  27.                         return -1;
  28.                 }
  29.         }
  30.         else
  31.         {
  32.                 ptn = ptable_find(ptable, "recovery");
  33.                 if (ptn == NULL) {
  34.                         dprintf(CRITICAL, "ERROR: No recovery partition found\n");
  35.                         return -1;
  36.                 }
  37.         }

  38.         if (flash_read(ptn, offset, buf, page_size)) {
  39.                 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
  40.                 return -1;
  41.         }
  42.         offset += page_size;

  43.         if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
  44.                 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
  45.                 return -1;
  46.         }

  47.         if (hdr->page_size != page_size) {
  48.                 dprintf(CRITICAL, "ERROR: Invalid boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
  49.                 return -1;
  50.         }

  51.         n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
  52.         if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
  53.                 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
  54.                 return -1;
  55.         }
  56.         offset += n;

  57.         n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
  58.         if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
  59.                 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
  60.                 return -1;
  61.         }
  62.         offset += n;

  63. continue_boot:
  64.         dprintf(INFO, "\nkernel  @ %x (%d bytes)\n", hdr->kernel_addr,
  65.                 hdr->kernel_size);
  66.         dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
  67.                 hdr->ramdisk_size);

  68.         //if(hdr->cmdline[0]) {
  69.         //        cmdline = (char*) hdr->cmdline;
  70.         //} else {
  71.         //        cmdline = DEFAULT_CMDLINE;
  72.         //}

  73.         cmdline = CUSTOMIZED_CMDLINE;
  74.         dprintf(INFO, "cmdline = '%s'\n", cmdline);

  75.         /* TODO: create/pass atags to kernel */
  76.        
  77. #ifdef WIDE_AREA
  78.         //        turn on backlight normal boot
  79.         gpio_set(GPIO_BACKLIGHT_EN, 1);
  80. #endif

  81.         dprintf(INFO, "\nBooting Linux\n");
  82.         boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
  83.                    (const char *)cmdline, board_machtype(),
  84.                    (void *)hdr->ramdisk_addr, hdr->ramdisk_size);

  85.         return 0;
  86. }

复制代码


代码 79-85行,我把原来的注掉了,改成了 cmdline = CUSTOMIZED_CMDLINE;
CUSTOMIZED_CMDLINE = "console=ttyMSM0,115200,n8 androidboot.hardware=qcom loglevel=1 cfg=H80,49b30040,018";

比较靠谱 再详细点就好了

还是楼上的头像霸气

哈哈。你的也不差啊

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top