{"id":41,"date":"2023-08-16T16:03:01","date_gmt":"2023-08-16T08:03:01","guid":{"rendered":"https:\/\/www.tinatsao.com\/wordpress\/?p=41"},"modified":"2023-08-16T16:29:38","modified_gmt":"2023-08-16T08:29:38","slug":"%e5%9c%a8kernel%e4%b8%8b%e8%ae%80%e5%8f%96uboot-env","status":"publish","type":"post","link":"https:\/\/www.tinatsao.com\/wordpress\/?p=41","title":{"rendered":"\u5728Kernel\u4e0b\u8b80\u53d6Uboot env"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u8209\u4f8b: \u6211\u8981\u5728 \/proc\/mydevice \u5e95\u4e0b, cat model<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u53bbUboot\u7684code, \u53bb\u627e\u5230define setargs \u7684\u5730\u65b9, \u52a0\u5165\u81ea\u5df1\u8981\u5275\u5efa\u7684proc id, \u4f8b\u5982\u6211\u8981\u52a0\u5165model, code \u5982\u4e0b:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">#define CONFIG_SETBOOTARGS \"setenv bootargs ${bootargs} ${mtdparts};\"\\ \"setenv bootargs ${bootargs} phy_mode=${phy_mode} coherent_pool=1M uuid=${uuid} model=${model};\"<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. \u9032\u5165\u6a5f\u5668\u7684uboot, \u8f38\u5165printenv, \u6703\u770b\u5230\u5982\u4e0b:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"839\" height=\"528\" src=\"https:\/\/www.tinatsao.com\/wordpress\/wp-content\/uploads\/2023\/08\/boot-2.png\" alt=\"\" class=\"wp-image-47\" srcset=\"https:\/\/www.tinatsao.com\/wordpress\/wp-content\/uploads\/2023\/08\/boot-2.png 839w, https:\/\/www.tinatsao.com\/wordpress\/wp-content\/uploads\/2023\/08\/boot-2-300x189.png 300w, https:\/\/www.tinatsao.com\/wordpress\/wp-content\/uploads\/2023\/08\/boot-2-768x483.png 768w, https:\/\/www.tinatsao.com\/wordpress\/wp-content\/uploads\/2023\/08\/boot-2-477x300.png 477w\" sizes=\"auto, (max-width: 839px) 100vw, 839px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">3. \u53bbkernel\u7684code, \u627e\u5230\u6709\u95dcstatic init __init XXXX\u7684\u6a94\u6848, \u6bd4\u5982\u6211\u7684\u662f init\/main.c, \u52a0\u5165\u4ee5\u4e0bcode:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">char struuid[32+1]={0};\nstatic int __init myuuid(char *str)\n{\n    struuid[ sizeof( struuid )-1 ] = 0x00;\n    strncpy( struuid, str, sizeof( struuid )-1 );\n    return 1;\n}\n\nchar strmodel[32+1]={0};\nstatic int __init mymodel(char *str)\n{\n    char         * pStr;\n    strmodel[ sizeof( strmodel )-1 ] = 0x00;\n\n    for ( pStr = str; *pStr == '_'; pStr++ );\n    strncpy( strmodel, pStr, sizeof( strmodel ) -1 );\n\n    return 1;\n}\n\n__setup(\"model=\", mymodel);\n__setup(\"uuid=\", myuuid);\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4. \u53bbKernel\u7684code, \u5927\u7d04\u8def\u5f91\u5728 \/fs\/proc\u5e95\u4e0b, \u65b0\u589e\u4e00\u652f\u6a94\u6848, \u5167\u5bb9\u5982\u4e0b, \u7136\u5f8c\u91cd\u65b0\u71d2\u5165, \u5c31\u5927\u529f\u6210\u5566.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/***********************************************************************\n *                                                                     *\n ***********************************************************************\/\n#include &lt;linux\/fs.h>\n#include &lt;linux\/init.h>\n#include &lt;linux\/proc_fs.h>\n#include &lt;linux\/seq_file.h>\n#include &lt;linux\/kernel.h>\n#include &lt;linux\/mtd\/mtd.h>\n#include &lt;linux\/err.h>\n\/***********************************************************************\n *                                                                     *\n **********************************************************************\/\nstruct proc_dir_entry * mydir = NULL;\nextern char strmodel[];\n\/***********************************************************************\n *                                                                     *\n ***********************************************************************\/\nstatic int model_proc_show(struct seq_file *m, void *v)\n{\n  seq_printf(m, \"%s\\n\", strmodel);\n  return 0;\n}\n\/***********************************************************************\n *                                                                     *\n ***********************************************************************\/\nstatic int model_proc_open(struct inode *inode, struct file *file)\n{\n  return single_open(file, model_proc_show, NULL);\n}\n\/***********************************************************************\n *                                                                     *\n ***********************************************************************\/\nstatic const struct file_operations model_proc_fops = {\n  .open   = model_proc_open,\n  .read   = seq_read,\n  .llseek   = seq_lseek,\n  .release  = single_release,\n};\n\/***********************************************************************\n *                                                                     *\n ***********************************************************************\/\nstatic int __init proc_model_init(void)\n{\n  if ( mydir == NULL )\n  {\n    mydir = proc_mkdir(\"mydevice\", NULL);\n  }\n\n  proc_create(\"model\", S_IRUGO, mydir, &amp;model_proc_fops);\n  return 0;\n}\n\n\/***********************************************************************\n *                                                                     *\n ***********************************************************************\/\nmodule_init(proc_model_init);\n\/***********************************************************************\n *                                                                     *\n ***********************************************************************\/\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8209\u4f8b: \u6211\u8981\u5728 \/proc\/mydevice \u5e95\u4e0b, cat model #de &hellip; <a href=\"https:\/\/www.tinatsao.com\/wordpress\/?p=41\">\u95b1\u8b80\u5168\u6587 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-41","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.tinatsao.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/41","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tinatsao.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tinatsao.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tinatsao.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tinatsao.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=41"}],"version-history":[{"count":6,"href":"https:\/\/www.tinatsao.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/41\/revisions"}],"predecessor-version":[{"id":51,"href":"https:\/\/www.tinatsao.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/41\/revisions\/51"}],"wp:attachment":[{"href":"https:\/\/www.tinatsao.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tinatsao.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tinatsao.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}