Skip to content

Commit

Permalink
kernel: add find and replace bootargs hack
Browse files Browse the repository at this point in the history
Add find and replace bootargs to device tree cmdline hack. Now we can
find and replace bootargs by setting bootargs-find-1, bootargs-replace-1
and bootargs-find-2, bootargs-replace-2 under the chosen node in the
device tree.

This hack was implemented to support Linksys MX4300, a dual firmware
device. This device's default kernel command line isn't compatiailbe
with OpenWrt's UBI, so we leverage find and replace to dynamically
patch the command line while keeping the partition to boot set by u-boot.

Signed-off-by: Qiyuan Zhang <[email protected]>
  • Loading branch information
asd333111 authored and Qiyuan Zhang committed Aug 3, 2024
1 parent 1fdb0ec commit 4328963
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
71 changes: 68 additions & 3 deletions target/linux/generic/hack-6.6/920-device_tree_cmdline.patch
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,78 @@ Subject: [PATCH] of/ftd: add device tree cmdline

--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -1185,6 +1185,9 @@ int __init early_init_dt_scan_chosen(cha
p = of_get_flat_dt_prop(node, "bootargs", &l);
@@ -1157,6 +1157,16 @@ int __init early_init_dt_scan_chosen(cha
const void *rng_seed;
const void *fdt = initial_boot_params;

+ int i, cmd_len, f_len, r_len;
+ char *s_ptr, *l_end, *r_end, *cur_ptr, *end_ptr;
+ int offset, step;
+
+ const int bootargs_replace_num = 2;
+ const char *bootargs_replace_nodes[2][2] =
+ { {"bootargs-find-1", "bootargs-replace-1"},
+ {"bootargs-find-2", "bootargs-replace-2"} };
+
+
node = fdt_path_offset(fdt, "/chosen");
if (node < 0)
node = fdt_path_offset(fdt, "/chosen@0");
@@ -1186,6 +1196,57 @@ int __init early_init_dt_scan_chosen(cha
if (p != NULL && l > 0)
strscpy(cmdline, p, min(l, COMMAND_LINE_SIZE));

+ for(i = 0; i < bootargs_replace_num; i++) {
+ p = of_get_flat_dt_prop(node, bootargs_replace_nodes[i][0], &f_len);
+ if (p != NULL && f_len > 0 && (s_ptr = strstr(cmdline, p))) {
+ p = of_get_flat_dt_prop(node, bootargs_replace_nodes[i][1], &r_len);
+ if (p != NULL && f_len > 0) {
+ p = of_get_flat_dt_prop(node, bootargs_replace_nodes[i][1], &r_len);
+
+ if (p != NULL && r_len > 0) {
+ pr_info("Input kernel commad line: %s\n", cmdline);
+
+ cmd_len = strlen(cmdline);
+
+ if (cmd_len - f_len + r_len < COMMAND_LINE_SIZE) {
+
+ pr_info("Replace kernel command line with %s\n", bootargs_replace_nodes[i][1]);
+
+ offset = r_len - f_len;
+
+ if (offset != 0) {
+ l_end = s_ptr + f_len - 1;
+ r_end = cmdline + cmd_len;
+
+ if (offset > 0) {
+ step = -1;
+ cur_ptr = r_end;
+ end_ptr = l_end + step;
+ } else {
+ step = 1;
+ cur_ptr = l_end;
+ end_ptr = r_end + step;
+ }
+
+ for (; cur_ptr != end_ptr; cur_ptr += step) {
+ *(cur_ptr + offset) = *cur_ptr;
+ }
+ }
+
+ strncpy(s_ptr, p, r_len - 1);
+
+ pr_info("Kernel command line after replacement: %s\n", cmdline);
+ } else {
+ pr_err("Replace kernel command line with %s failed\n", bootargs_replace_nodes[i][1]);
+ }
+ }
+ }
+ }
+ }
+ p = of_get_flat_dt_prop(node, "bootargs-append", &l);
+ if (p != NULL && l > 0)
+ strlcat(cmdline, p, min_t(int, strlen(cmdline) + (int)l, COMMAND_LINE_SIZE));
+
handle_cmdline:
/*
* CONFIG_CMDLINE is meant to be a default in case nothing else
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
/ {
model = "Linksys MX4300";
compatible = "linksys,mx4300", "qcom,ipq8074";

chosen {
bootargs-append = "";
// Replace the bootargs from u-boot with find and replace
bootargs-find-1 = "init=/sbin/init rootfstype=squashfs ubi.mtd=22,2048 ubi.block=0,0 root=/dev/ubiblock0_0 rootwait ro";
bootargs-find-2 = "init=/sbin/init rootfstype=squashfs ubi.mtd=24,2048 ubi.block=0,0 root=/dev/ubiblock0_0 rootwait ro";
bootargs-replace-1 = "rootfstype=squashfs ubi.mtd=22,4096 ubi.block=0,0 root=/dev/ubiblock0_0 rootwait ro";
bootargs-replace-2 = "rootfstype=squashfs ubi.mtd=24,4096 ubi.block=0,0 root=/dev/ubiblock0_0 rootwait ro";
};
};

&qpic_nand {
Expand Down

0 comments on commit 4328963

Please sign in to comment.