在Kernel下讀取Uboot env

舉例: 我要在 /proc/mydevice 底下, cat model

  1. 去Uboot的code, 去找到define setargs 的地方, 加入自己要創建的proc id, 例如我要加入model, code 如下:
#define CONFIG_SETBOOTARGS "setenv bootargs ${bootargs} ${mtdparts};"\ "setenv bootargs ${bootargs} phy_mode=${phy_mode} coherent_pool=1M uuid=${uuid} model=${model};"

2. 進入機器的uboot, 輸入printenv, 會看到如下:

3. 去kernel的code, 找到有關static init __init XXXX的檔案, 比如我的是 init/main.c, 加入以下code:

char struuid[32+1]={0};
static int __init myuuid(char *str)
{
    struuid[ sizeof( struuid )-1 ] = 0x00;
    strncpy( struuid, str, sizeof( struuid )-1 );
    return 1;
}

char strmodel[32+1]={0};
static int __init mymodel(char *str)
{
    char         * pStr;
    strmodel[ sizeof( strmodel )-1 ] = 0x00;

    for ( pStr = str; *pStr == '_'; pStr++ );
    strncpy( strmodel, pStr, sizeof( strmodel ) -1 );

    return 1;
}

__setup("model=", mymodel);
__setup("uuid=", myuuid);

4. 去Kernel的code, 大約路徑在 /fs/proc底下, 新增一支檔案, 內容如下, 然後重新燒入, 就大功成啦.

/***********************************************************************
 *                                                                     *
 ***********************************************************************/
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/kernel.h>
#include <linux/mtd/mtd.h>
#include <linux/err.h>
/***********************************************************************
 *                                                                     *
 **********************************************************************/
struct proc_dir_entry * mydir = NULL;
extern char strmodel[];
/***********************************************************************
 *                                                                     *
 ***********************************************************************/
static int model_proc_show(struct seq_file *m, void *v)
{
  seq_printf(m, "%s\n", strmodel);
  return 0;
}
/***********************************************************************
 *                                                                     *
 ***********************************************************************/
static int model_proc_open(struct inode *inode, struct file *file)
{
  return single_open(file, model_proc_show, NULL);
}
/***********************************************************************
 *                                                                     *
 ***********************************************************************/
static const struct file_operations model_proc_fops = {
  .open   = model_proc_open,
  .read   = seq_read,
  .llseek   = seq_lseek,
  .release  = single_release,
};
/***********************************************************************
 *                                                                     *
 ***********************************************************************/
static int __init proc_model_init(void)
{
  if ( mydir == NULL )
  {
    mydir = proc_mkdir("mydevice", NULL);
  }

  proc_create("model", S_IRUGO, mydir, &model_proc_fops);
  return 0;
}

/***********************************************************************
 *                                                                     *
 ***********************************************************************/
module_init(proc_model_init);
/***********************************************************************
 *                                                                     *
 ***********************************************************************/

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *