【openwrt】UBOOT传递参数到内核parameters

UBOOT系列文章

第二章 UBOOT参数传递



前言

Uboot向内核传递的方式有多种,这里介绍一种简单的参数传递方式,通过bootargs传递


一、为啥要uboot参数传递?

在某些时候,需要uboot通过bootargs参数传递给内核,然后通过传入的参数进行一些特殊的操作

二、操作方法

1.uboot更改

代码如下(示例):

static void boot_prep_linux(bootm_headers_t *images)
{
	/*此处省略*/
	board_prep_linux(images);
}

需自己更改添加一个board_prep_linux函数,进行参数传递
void board_prep_linux(bootm_headers_t *images)
{
	void *fdt = images->ft_addr;
	const char *orig_bootargs;
	int nodeoffset, len;
	char *new_cmdline;
	u32 newlen;
	int ret;

	if (!(CONFIG_IS_ENABLED(OF_LIBFDT) && images->ft_len)) {
		printf("Warning: no FDT present for current image!\n");
		return;
	}

	/* find or create "/chosen" node. */
	nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
	if (nodeoffset < 0)
		return;

	orig_bootargs = fdt_getprop(fdt, nodeoffset, "bootargs", &len);

	debug("%s: orig cmdline = \"%s\"\n", __func__, orig_bootargs);

	/* setup bootargs */
    if (/*添加逻辑判断什么时候可新增boot_param参数*/) {
    	bootargs_set("boot_param.AAAA", "1");
    	bootargs_set("boot_param.BBBB", "BBBB");

		ret = cmdline_merge(orig_bootargs, bootargs, bootargs_used, &new_cmdline);
		if (!ret) {
			panic("Error: failed to generate new kernel cmdline\n");
			return;
		}

		debug("%s: new cmdline = \"%s\"\n", __func__, new_cmdline);

		newlen = strlen(new_cmdline) + 1;
		fdt_shrink_to_minimum(fdt, newlen);

		ret = fdt_setprop(fdt, nodeoffset, "bootargs", new_cmdline, newlen);
		if (ret < 0) {
			panic("Error: failed to set new kernel cmdline\n");
			return;
		}
    }

}

2.内核添加boot_param.c函数

代码如下(示例):

diff -uNr linux-5.15.134/kernel/boot_param.c linux-5.15.134-new/kernel/boot_param.c
--- linux-5.15.134/kernel/boot_param.c	1970-01-01 08:00:00.000000000 +0800
+++ linux-5.15.134-new/kernel/boot_param.c	2024-07-27 12:21:45.792252500 +0800
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/*
+* Copyright (C) 2022. All rights reserved.
+ *
+ * Author: 
+ */
+#include <linux/bug.h>
+#include <linux/kernel.h>
+#include <linux/moduleparam.h>
+
+#define BOOT_PARAM_STR_MAX_LEN			256
+
+static uint AAAA;
+module_param(AAAA, uint, 0444);
+
+static char BBBB[BOOT_PARAM_STR_MAX_LEN];
+module_param_string(BBBB, BBBB, BOOT_PARAM_STR_MAX_LEN, 0444);
diff -uNr linux-5.15.134/kernel/Makefile linux-5.15.134-new/kernel/Makefile
--- linux-5.15.134/kernel/Makefile	2024-08-07 15:22:07.255300247 +0800
+++ linux-5.15.134-new/kernel/Makefile	2024-08-07 15:28:34.776916563 +0800
@@ -12,6 +12,8 @@
 	    notifier.o ksysfs.o cred.o reboot.o \
 	    async.o range.o smpboot.o ucount.o regset.o
 
+obj-y += boot_param.o
+


进入内核shell,通过查看目录下文件 ls /sys/module/boot_param/parameters
root@-:~# cat /sys/module/boot_param/parameters/AAAA
1
root@-:~# cat /sys/module/boot_param/parameters/BBBB
BBBB

[ 0.000000] Booting Linux on physical CPU 0x0000000000 [0x410fd034]
[ 0.000000] Linux version 5.4.246 (gcc version 8.4.0 (OpenWrt GCC 8.4.0 r0-9e341057)) #0 SMP Tue Aug 13 05:54:21 2024
[ 0.000000] Machine model: MediaTek MT7981 RFB
[ 0.000000] earlycon: uart8250 at MMIO32 0x0000000011002000 (options ‘’)
[ 0.000000] printk: bootconsole [uart8250] enabled
[ 0.000000] On node 0 totalpages: 64592
[ 0.000000] DMA32 zone: 1024 pages used for memmap
[ 0.000000] DMA32 zone: 0 pages reserved
[ 0.000000] DMA32 zone: 64592 pages, LIFO batch:15
[ 0.000000] psci: probing for conduit method from DT.
[ 0.000000] psci: PSCIv1.1 detected in firmware.
[ 0.000000] psci: Using standard PSCI v0.2 function IDs
[ 0.000000] psci: MIGRATE_INFO_TYPE not supported.
[ 0.000000] psci: SMC Calling Convention v1.0
[ 0.000000] percpu: Embedded 20 pages/cpu s43992 r8192 d29736 u81920
[ 0.000000] pcpu-alloc: s43992 r8192 d29736 u81920 alloc=20*4096
[ 0.000000] pcpu-alloc: [0] 0 [0] 1
[ 0.000000] Detected VIPT I-cache on CPU0
[ 0.000000] CPU features: detected: GIC system register CPU interface
[ 0.000000] CPU features: kernel page table isolation disabled by kernel configuration
[ 0.000000] Built 1 zonelists, mobility grouping on. Total pages: 63568
[ 0.000000] Kernel command line: console=ttyS0,115200n1 loglevel=8 earlycon=uart8250,mmio32,0x11002000 boot_param.AAAA=1 boot_param.BBBB=BBBB


总结

例如:以上就是uboot通过bootargs传递参数给内核的简单方法

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值