[PATCH 16/17] lib: support libxbc
Safae Ouajih
souajih at baylibre.com
Sat Nov 26 17:59:30 CET 2022
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 <souajih at baylibre.com>
---
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 <common.h>
+/*
+ * 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
More information about the U-Boot
mailing list