From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7DEA7C4321E for ; Sat, 26 Nov 2022 17:21:41 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 93F9B852D4; Sat, 26 Nov 2022 18:18:57 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=baylibre-com.20210112.gappssmtp.com header.i=@baylibre-com.20210112.gappssmtp.com header.b="5W8s+LZd"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id CA25184494; Sat, 26 Nov 2022 18:00:06 +0100 (CET) Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 0B5A785031 for ; Sat, 26 Nov 2022 17:59:59 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=souajih@baylibre.com Received: by mail-wm1-x32c.google.com with SMTP id j5-20020a05600c410500b003cfa9c0ea76so5509793wmi.3 for ; Sat, 26 Nov 2022 08:59:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=HPyEOcomPBUFExvZDsoLYhU3QAY32gmmGBbCQnqJF5Q=; b=5W8s+LZdU+67djJhq02mUbBsHmaNOGZlkYkBqSaIVVmJotucPWXpXkElWMvcR4ydqr L4xG1kBlvKctDOzQiRUJRYt7kgEP2NgcPlYs+ZnTMqrx9ytTJ2GfVn1PLdinLnLe6/JK nJz7/OClJRdAAdQy/8ko2YPJwsPs6+NXA6Vig8N+gyPyQX16TNynLriy+552oAbLultQ g8zqMTacmAheDQvqh5/xZ9vRuoiFk3rSTfL8qbiQj2XEvaQlsI1xwEf1H1hJjrfBoYC+ Fi8sdOkIJA6FX9kwqyea4vEKl05oMggcnhr0vyN4vY96PA99ORvK8/HF/fQA8BxJWmPA xkTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HPyEOcomPBUFExvZDsoLYhU3QAY32gmmGBbCQnqJF5Q=; b=uMB5iSm9YDDCPFer4W2ZURC0Qo68SWcMaL9P8jONO8TCGVu/SCy/Y+n+nufNBZxxw5 U1zK4xAnXVlmQe/sCYPwTYzOaFXjDOVPWPE041Eehn5ymNAFqFmoAjLggTc7zx3QTBqq VZXqJE/MfDS7gAJ46D25esT91sYH/ZRDav3/kuzmqYhAAmlT53VZG3rCjfawflfYEPeM 6imKnJ8qpevTWLtxc9aSISXlt5tjXXSEevhNkwHmHXfAs6KvBCpFuL+8Rmm6Y5DjX6bQ belBE+XQDeFYv4GsIWu9kVo2jZ+q0DpbvsAhOG8QluVx+ebbnP149JNYGFbbvZ/Hufci 9XFQ== X-Gm-Message-State: ANoB5pl2XvnJ1xMlPF/x4bmHHom22OPYoPBAQGHEcTifxmu+aOIYsyEI QmqcDMyUJeLtcdLrZKKTWp8DFfuc+nrJNQ== X-Google-Smtp-Source: AA0mqf7K/3JQ0EPwxnD9W1RBT3o4hXoJHOeVqndJcspCzEQ7FTyrIDB3EuxF8SrhS/pcYiO4BHGhtg== X-Received: by 2002:a05:600c:288:b0:3cf:758f:161f with SMTP id 8-20020a05600c028800b003cf758f161fmr30441267wmk.54.1669481998490; Sat, 26 Nov 2022 08:59:58 -0800 (PST) Received: from safae.baylibre.local ([2a04:cec0:11b5:4031:7588:eb2c:a5c2:aa6d]) by smtp.gmail.com with ESMTPSA id c15-20020adfe70f000000b00241e8d00b79sm7462408wrm.54.2022.11.26.08.59.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 26 Nov 2022 08:59:58 -0800 (PST) From: Safae Ouajih To: sjg@chromium.org Cc: u-boot@lists.denx.de, sean.anderson@seco.com, r.stratiienko@gmail.com, mkorpershoek@baylibre.com, glaroque@baylibre.com, khilman@baylibre.com Subject: [PATCH 16/17] lib: support libxbc Date: Sat, 26 Nov 2022 17:59:30 +0100 Message-Id: <20221126165931.197607-17-souajih@baylibre.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221126165931.197607-1-souajih@baylibre.com> References: <20221126165931.197607-1-souajih@baylibre.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Mailman-Approved-At: Sat, 26 Nov 2022 18:18:14 +0100 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean This adds support for libxbc used to support Bootconfig feature. Bootconfig documentation : [1] This was imported from [2], commit [3] [1] https://source.android.com/docs/core/architecture/bootloader/implementing-bootconfig [2] https://android.googlesource.com/platform/external/u-boot/ [3] `7af0a0506d4de6f5ea147d10fb0664a8af07d326` Signed-off-by: Safae Ouajih --- include/xbc.h | 1 + lib/Kconfig | 12 +++++ lib/Makefile | 1 + lib/libxbc/Makefile | 1 + lib/libxbc/libxbc.c | 112 ++++++++++++++++++++++++++++++++++++++++++++ lib/libxbc/libxbc.h | 54 +++++++++++++++++++++ 6 files changed, 181 insertions(+) create mode 100644 include/xbc.h create mode 100644 lib/libxbc/Makefile create mode 100644 lib/libxbc/libxbc.c create mode 100644 lib/libxbc/libxbc.h diff --git a/include/xbc.h b/include/xbc.h new file mode 100644 index 000000000000..725e65ff6ad8 --- /dev/null +++ b/include/xbc.h @@ -0,0 +1 @@ +#include <../lib/libxbc/libxbc.h> diff --git a/lib/Kconfig b/lib/Kconfig index 6abe1d0a863b..eca752b7db79 100644 --- a/lib/Kconfig +++ b/lib/Kconfig @@ -417,6 +417,18 @@ config LIBAVB endmenu +menu "Android Boot Configuration" +config LIBXBC + bool "Android Boot Configuration support" + depends on ANDROID_BOOT_IMAGE + default n + help + This enables support of Boot Configuration which can be used + to pass boot configuration parameters to user space. These + parameters will show up in /proc/bootconfig similar to the kernel + parameters that show up in /proc/cmdline +endmenu + menu "Hashing Support" config BLAKE2 diff --git a/lib/Makefile b/lib/Makefile index f2cfd1e42892..b0ad522ac116 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -87,6 +87,7 @@ obj-$(CONFIG_$(SPL_)LZ4) += lz4_wrapper.o obj-$(CONFIG_$(SPL_)LIB_RATIONAL) += rational.o obj-$(CONFIG_LIBAVB) += libavb/ +obj-$(CONFIG_LIBXBC) += libxbc/ obj-$(CONFIG_$(SPL_TPL_)OF_LIBFDT) += libfdt/ obj-$(CONFIG_$(SPL_TPL_)OF_REAL) += fdtdec_common.o fdtdec.o diff --git a/lib/libxbc/Makefile b/lib/libxbc/Makefile new file mode 100644 index 000000000000..7ac4cde05666 --- /dev/null +++ b/lib/libxbc/Makefile @@ -0,0 +1 @@ +obj-$(CONFIG_LIBXBC) += libxbc.o diff --git a/lib/libxbc/libxbc.c b/lib/libxbc/libxbc.c new file mode 100644 index 000000000000..129bffc7c628 --- /dev/null +++ b/lib/libxbc/libxbc.c @@ -0,0 +1,112 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "libxbc.h" +#define BOOTCONFIG_MAGIC "#BOOTCONFIG\n" +#define BOOTCONFIG_MAGIC_SIZE 12 +#define BOOTCONFIG_SIZE_SIZE 4 +#define BOOTCONFIG_CHECKSUM_SIZE 4 +#define BOOTCONFIG_TRAILER_SIZE BOOTCONFIG_MAGIC_SIZE + \ + BOOTCONFIG_SIZE_SIZE + \ + BOOTCONFIG_CHECKSUM_SIZE + +/* + * Simple checksum for a buffer. + * + * @param addr pointer to the start of the buffer. + * @param size size of the buffer in bytes. + * @return check sum result. + */ +static uint32_t checksum(const unsigned char* const buffer, uint32_t size) { + uint32_t sum = 0; + for (uint32_t i = 0; i < size; i++) { + sum += buffer[i]; + } + return sum; +} + +/* + * Check if the bootconfig trailer is present within the bootconfig section. + * + * @param bootconfig_end_addr address of the end of the bootconfig section. If + * the trailer is present, it will be directly preceding this address. + * @return true if the trailer is present, false if not. + */ +static bool isTrailerPresent(uint64_t bootconfig_end_addr) { + return !strncmp((char*)(bootconfig_end_addr - BOOTCONFIG_MAGIC_SIZE), + BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_SIZE); +} + +/* + * Add a string of boot config parameters to memory appended by the trailer. + */ +int32_t addBootConfigParameters(char* params, uint32_t params_size, + uint64_t bootconfig_start_addr, uint32_t bootconfig_size) { + if (!params || !bootconfig_start_addr) { + return -1; + } + if (params_size == 0) { + return 0; + } + int32_t applied_bytes = 0; + int32_t new_size = 0; + uint64_t end = bootconfig_start_addr + bootconfig_size; + if (isTrailerPresent(end)) { + end -= BOOTCONFIG_TRAILER_SIZE; + applied_bytes -= BOOTCONFIG_TRAILER_SIZE; + memcpy(&new_size, (void *)end, BOOTCONFIG_SIZE_SIZE); + } else { + new_size = bootconfig_size; + } + // params + memcpy((void*)end, params, params_size); + applied_bytes += params_size; + applied_bytes += addBootConfigTrailer(bootconfig_start_addr, + bootconfig_size + applied_bytes); + return applied_bytes; +} + +/* + * Add boot config trailer. + */ + +int32_t addBootConfigTrailer(uint64_t bootconfig_start_addr, + uint32_t bootconfig_size) { + if (!bootconfig_start_addr) { + return -1; + } + + if (bootconfig_size == 0) { + return 0; + } + + uint64_t end = bootconfig_start_addr + bootconfig_size; + if (isTrailerPresent(end)) { + return 0; + } + + // size + memcpy((void *)(end), &bootconfig_size, BOOTCONFIG_SIZE_SIZE); + + // checksum + uint32_t sum = checksum((unsigned char*)bootconfig_start_addr, bootconfig_size); + memcpy((void *)(end + BOOTCONFIG_SIZE_SIZE), &sum, BOOTCONFIG_CHECKSUM_SIZE); + + // magic + memcpy((void *)(end + BOOTCONFIG_SIZE_SIZE + BOOTCONFIG_CHECKSUM_SIZE), + BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_SIZE); + + return BOOTCONFIG_TRAILER_SIZE; +} diff --git a/lib/libxbc/libxbc.h b/lib/libxbc/libxbc.h new file mode 100644 index 000000000000..0b6ba4960905 --- /dev/null +++ b/lib/libxbc/libxbc.h @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef LIBXBC_H_ +#define LIBXBC_H_ + +#include +/* + * Add a string of boot config parameters to memory appended by the trailer. + * This memory needs to be immediately following the end of the ramdisks. + * The new boot config trailer will be written to the end of the entire + * parameter section(previous + new). The trailer contains a 4 byte size of the + * parameters, followed by a 4 byte checksum of the parameters, followed by a 12 + * byte magic string. + * + * @param params pointer to string of boot config parameters + * @param params_size size of params string in bytes + * @param bootconfig_start_addr address that the boot config section is starting + * at in memory. + * @param bootconfig_size size of the current bootconfig section in bytes. + * @return number of bytes added to the boot config section. -1 for error. + */ +int addBootConfigParameters(char *params, uint32_t params_size, + uint64_t bootconfig_start_addr, + uint32_t bootconfig_size); +/* + * Add the boot config trailer to the end of the boot config parameter section. + * This can be used after the vendor bootconfig section has been placed into + * memory if there are no additional parameters that need to be added. + * The new boot config trailer will be written to the end of the entire + * parameter section at (bootconfig_start_addr + bootconfig_size). + * The trailer contains a 4 byte size of the parameters, followed by a 4 byte + * checksum of the parameters, followed by a 12 byte magic string. + * + * @param bootconfig_start_addr address that the boot config section is starting + * at in memory. + * @param bootconfig_size size of the current bootconfig section in bytes. + * @return number of bytes added to the boot config section. -1 for error. + */ +int addBootConfigTrailer(uint64_t bootconfig_start_addr, + uint32_t bootconfig_size); +#endif /* LIBXBC_H_ */ -- 2.25.1