Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp375568pxb; Thu, 25 Feb 2021 05:06:04 -0800 (PST) X-Google-Smtp-Source: ABdhPJw5G8UU0gJCaEBi2TRDVuckrWqpZpb4wsc6sLoEl/Zsr0YMzwKut8t6ZiQni3W6rjENIfIQ X-Received: by 2002:a05:6402:50c6:: with SMTP id h6mr2746242edb.117.1614258364025; Thu, 25 Feb 2021 05:06:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614258364; cv=none; d=google.com; s=arc-20160816; b=oY5G6N9Hg8+4XGn0pEkxDeoIpCo61xV6cut47nNoyDiOBFpTaWsX25eQHTiJoytBqy HddfYt1pY21jmyLWwv0jTbmvR2PfcRmglExb4a/Fz9qT2o+Day1T9zva6Ow5f0N1VLCS AIAYwQpUapIWloLTvUz4ypmj9oIFE7dRzl9qRZR9AYtG3/OTT2tqegbIVujA7cDONO9G 29BEZl+215EY7IDvbqP4vvgrpFUBnUy8k8f2yjDjA6CVPj7ukyH0C97hOPd3Se91WBWS DXv36A9gxTJioLrvJFTxGgnKeyBZQVmqoWUTnoIKTlwmgnnKx1iNfXgZ5bmUUWmzg1Su g+Zg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=nX8sUV+D5BxprJX/TMeH9HQZB8JiQA9Su7IWuD4eEew=; b=v/wcZCV3HOkuGrHjijwrtPO5owjjW3s1hjSYdR1i2o8O+BKLmgMCjwl5bQYo6y2I0T cLlNHAOd23zKTiz1mhcGM3aAfRHXVcgmNFNElj5hljPySBd28bxMFS6wmUECqUy2TWem 6jQxdNjwx7JAcicByts4ARYbsVYj6zCdlKG05TuPoopT6HENiK9srU+FAl7Rs+qk17zr 1TMd7VhFYmqixLVfTZPbmbrWg181fte9h9FGkCG/ut374J0fJqIwf7hqW+UvDXRC7iMo 1OOYZpUj1HHxMT8Y/hyVhZLfIhCsLDuAq9oHFtyrrYwFnc/zUBmJAZmlY+1TppbKfk4Y gAFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="Q1/8xkLk"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n20si3459078ejj.318.2021.02.25.05.05.40; Thu, 25 Feb 2021 05:06:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="Q1/8xkLk"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232290AbhBYNAx (ORCPT + 99 others); Thu, 25 Feb 2021 08:00:53 -0500 Received: from mail.kernel.org ([198.145.29.99]:53866 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229800AbhBYNAV (ORCPT ); Thu, 25 Feb 2021 08:00:21 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 9868364F1F; Thu, 25 Feb 2021 12:59:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1614257979; bh=iC4xPAAEf5Q25zrpO9QDpQdb8NdCLLgIwAsn5EyY2y8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Q1/8xkLkE0hSUhWick6mrwsZPeS4qyH3hqUYvsD4+gKdKfmRkNpu+3ZT8vDztt8q2 QPER5uBGs9Vj8iCXDOO+kPIkXeWTnrURYYH7CyjhJqvnP7atg04ZsmPZIVe/IlEx83 BCPsxK41NpvtI5zv3XVnSWDpnc3A6Uz14t2/f/QET26v7D5FA7hA679LPLs2BJAPbg Bury2yiPBIAVQsjP5nIIsC5JFBQlZkwIrEIMmVhjCEDbmarsEbIjTCJgRBu8+M0Pn3 QR3xhVbPUFODfYfqf65HE1Mh/VbnzbOQQaYB+L8mZVYikWhE7yRmozXkZrMhnB9CPO F+vrmJ0JpCQzQ== From: Will Deacon To: linux-kernel@vger.kernel.org Cc: Will Deacon , Max Uvarov , Rob Herring , Ard Biesheuvel , Marc Zyngier , Doug Anderson , Tyler Hicks , Frank Rowand , Arnd Bergmann , Palmer Dabbelt , Greg Kroah-Hartman , Catalin Marinas , kernel-team@android.com, linux-arm-kernel@lists.infradead.org, devicetree@vger.kernel.org Subject: [PATCH 2/2] of/fdt: Append bootloader arguments when CMDLINE_EXTEND=y Date: Thu, 25 Feb 2021 12:59:21 +0000 Message-Id: <20210225125921.13147-3-will@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210225125921.13147-1-will@kernel.org> References: <20210225125921.13147-1-will@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The Kconfig help text for CMDLINE_EXTEND is sadly duplicated across all architectures that implement it (arm, arm64, powerpc, riscv and sh), but they all seem to agree that the bootloader arguments will be appended to the CONFIG_CMDLINE. For example, on arm64: | The command-line arguments provided by the boot loader will be | appended to the default kernel command string. This also matches the behaviour of the EFI stub, which parses the bootloader arguments first if CMDLINE_EXTEND is set, as well as the out-of-tree CMDLINE_EXTEND implementation in Android. However, the behaviour in the upstream fdt code appears to be the other way around: CONFIG_CMDLINE is appended to the bootloader arguments. Fix the code to follow the documentation by moving the cmdline processing out into a new function, early_init_dt_retrieve_cmdline(), and copying CONFIG_CMDLINE to the beginning of the cmdline buffer rather than concatenating it onto the end. Cc: Max Uvarov Cc: Rob Herring Cc: Ard Biesheuvel Cc: Marc Zyngier Cc: Doug Anderson Cc: Tyler Hicks Cc: Frank Rowand Fixes: 34b82026a507 ("fdt: fix extend of cmd line") Signed-off-by: Will Deacon --- drivers/of/fdt.c | 64 +++++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c index dcc1dd96911a..83b9d065e58d 100644 --- a/drivers/of/fdt.c +++ b/drivers/of/fdt.c @@ -1033,11 +1033,48 @@ int __init early_init_dt_scan_memory(unsigned long node, const char *uname, return 0; } +static int __init cmdline_from_bootargs(unsigned long node, void *dst, int sz) +{ + int l; + const char *p = of_get_flat_dt_prop(node, "bootargs", &l); + + if (!p || l <= 0) + return -EINVAL; + + return strlcpy(dst, p, min(l, sz)); +} + +/* dst is a zero-initialised buffer of COMMAND_LINE_SIZE bytes */ +static void __init early_init_dt_retrieve_cmdline(unsigned long node, char *dst) +{ + if (IS_ENABLED(CONFIG_CMDLINE_EXTEND)) { + /* Copy CONFIG_CMDLINE to the start of destination buffer */ + size_t idx = strlcpy(dst, CONFIG_CMDLINE, COMMAND_LINE_SIZE); + + /* Check that we have enough space to concatenate */ + if (idx + 1 >= COMMAND_LINE_SIZE) + return; + + /* Append the bootloader arguments */ + dst[idx++] = ' '; + cmdline_from_bootargs(node, &dst[idx], COMMAND_LINE_SIZE - idx); + } else if (IS_ENABLED(CONFIG_CMDLINE_FORCE)) { + /* Just use CONFIG_CMDLINE */ + strlcpy(dst, CONFIG_CMDLINE, COMMAND_LINE_SIZE); + } else if (IS_ENABLED(CONFIG_CMDLINE_FROM_BOOTLOADER)) { + /* Use CONFIG_CMDLINE if no arguments from bootloader. */ + if (cmdline_from_bootargs(node, dst, COMMAND_LINE_SIZE) <= 0) + strlcpy(dst, CONFIG_CMDLINE, COMMAND_LINE_SIZE); + } else { + /* Just use bootloader arguments */ + cmdline_from_bootargs(node, dst, COMMAND_LINE_SIZE); + } +} + int __init early_init_dt_scan_chosen(unsigned long node, const char *uname, int depth, void *data) { int l; - const char *p; const void *rng_seed; pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname); @@ -1047,30 +1084,7 @@ int __init early_init_dt_scan_chosen(unsigned long node, const char *uname, return 0; early_init_dt_check_for_initrd(node); - - /* Retrieve command line */ - p = of_get_flat_dt_prop(node, "bootargs", &l); - if (p != NULL && l > 0) - strlcpy(data, p, min(l, COMMAND_LINE_SIZE)); - - /* - * CONFIG_CMDLINE is meant to be a default in case nothing else - * managed to set the command line, unless CONFIG_CMDLINE_FORCE - * is set in which case we override whatever was found earlier. - */ -#ifdef CONFIG_CMDLINE -#if defined(CONFIG_CMDLINE_EXTEND) - strlcat(data, " ", COMMAND_LINE_SIZE); - strlcat(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE); -#elif defined(CONFIG_CMDLINE_FORCE) - strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE); -#else - /* No arguments from boot loader, use kernel's cmdl*/ - if (!((char *)data)[0]) - strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE); -#endif -#endif /* CONFIG_CMDLINE */ - + early_init_dt_retrieve_cmdline(node, data); pr_debug("Command line is: %s\n", (char *)data); rng_seed = of_get_flat_dt_prop(node, "rng-seed", &l); -- 2.30.1.766.gb4fecdf3b7-goog