From 61304dbec36dc445bbe7d2c19b4da0695861e0a8 Mon Sep 17 00:00:00 2001
From: Masahiro Yamada <yamada.masahiro@socionext.com>
Date: Mon, 30 Jan 2017 11:12:07 +0900
Subject: [PATCH] cmd: add a new command "config" to show .config contents

This feature is inspired by /proc/config.gz of Linux.  In Linux,
if CONFIG_IKCONFIG is enabled, the ".config" file contents are
embedded in the kernel image.  If CONFIG_IKCONFIG_PROC is also
enabled, the ".config" contents are exposed to /proc/config.gz.
Users can do "zcat /proc/config.gz" to check which config options
are enabled on the running kernel image.

The idea is almost the same here; if CONFIG_CMD_CONFIG is enabled,
the ".config" contents are compressed and saved in the U-Boot image,
then printed by the new command "config".

The usage is quite simple.  Enable CONFIG_CMD_CONFIG, then run
 > config
from the command line interface.  The ".config" contents will be
printed on the console.

This feature increases the U-Boot image size by about 4KB (this is
mostly due to the gzip-compressed .config file).  By default, it is
enabled only for Sandbox because we do not care about the memory
footprint on it.  Of course, this feature is architecture agnostic,
so you can enable it on any board if the image size increase is
acceptable for you.

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
---
 Kconfig            |  2 ++
 Makefile           | 12 +++++++-----
 cmd/.gitignore     |  3 +++
 cmd/Kconfig        | 12 ++++++++++++
 cmd/Makefile       | 22 ++++++++++++++++++++++
 cmd/config.c       | 44 ++++++++++++++++++++++++++++++++++++++++++++
 scripts/.gitignore |  1 +
 scripts/Kconfig    |  2 ++
 scripts/Makefile   |  4 ++++
 9 files changed, 97 insertions(+), 5 deletions(-)
 create mode 100644 cmd/.gitignore
 create mode 100644 cmd/config.c
 create mode 100644 scripts/Kconfig

diff --git a/Kconfig b/Kconfig
index 8f9ea97f3d..0dd21dfc17 100644
--- a/Kconfig
+++ b/Kconfig
@@ -325,3 +325,5 @@ source "fs/Kconfig"
 source "lib/Kconfig"
 
 source "test/Kconfig"
+
+source "scripts/Kconfig"
diff --git a/Makefile b/Makefile
index ed817f511d..8e417969e9 100644
--- a/Makefile
+++ b/Makefile
@@ -482,6 +482,13 @@ else
 # Build targets only - this includes vmlinux, arch specific targets, clean
 # targets and others. In general all targets except *config targets.
 
+# Additional helpers built in scripts/
+# Carefully list dependencies so we do not try to build scripts twice
+# in parallel
+PHONY += scripts
+scripts: scripts_basic include/config/auto.conf
+	$(Q)$(MAKE) $(build)=$(@)
+
 ifeq ($(dot-config),1)
 # Read in config
 -include include/config/auto.conf
@@ -1539,11 +1546,6 @@ tests:
 	$(Q)$(MAKE) $(build)=scripts build_docproc
 	$(Q)$(MAKE) $(build)=doc/DocBook $@
 
-# Dummies...
-PHONY += prepare scripts
-prepare: ;
-scripts: ;
-
 endif #ifeq ($(config-targets),1)
 endif #ifeq ($(mixed-targets),1)
 
diff --git a/cmd/.gitignore b/cmd/.gitignore
new file mode 100644
index 0000000000..7800586e8a
--- /dev/null
+++ b/cmd/.gitignore
@@ -0,0 +1,3 @@
+config_data.gz
+config_data_gz.h
+config_data_size.h
diff --git a/cmd/Kconfig b/cmd/Kconfig
index 4a0d489696..57e8e42f98 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -126,6 +126,18 @@ config CMD_BDI
 	help
 	  Print board info
 
+config CMD_CONFIG
+	bool "config"
+	select BUILD_BIN2C
+	default SANDBOX
+	help
+	  Print ".config" contents.
+
+	  If this option is enabled, the ".config" file contents are embedded
+	  in the U-Boot image and can be printed on the console by the "config"
+	  command.  This provides information of which options are enabled on
+	  the running U-Boot.
+
 config CMD_CONSOLE
 	bool "coninfo"
 	default y
diff --git a/cmd/Makefile b/cmd/Makefile
index 566fed9f7b..1a04e7e818 100644
--- a/cmd/Makefile
+++ b/cmd/Makefile
@@ -31,6 +31,7 @@ obj-$(CONFIG_CMD_BOOTI) += booti.o
 obj-$(CONFIG_CMD_CACHE) += cache.o
 obj-$(CONFIG_CMD_CBFS) += cbfs.o
 obj-$(CONFIG_CMD_CLK) += clk.o
+obj-$(CONFIG_CMD_CONFIG) += config.o
 obj-$(CONFIG_CMD_CONSOLE) += console.o
 obj-$(CONFIG_CMD_CPLBINFO) += cplbinfo.o
 obj-$(CONFIG_CMD_CPU) += cpu.o
@@ -165,3 +166,24 @@ obj-$(CONFIG_CMD_BLOB) += blob.o
 obj-y += nvedit.o
 
 obj-$(CONFIG_ARCH_MVEBU) += mvebu/
+
+filechk_data_gz = (echo "static const char data_gz[] ="; cat $< | scripts/bin2c; echo ";")
+
+filechk_data_size = \
+	(echo "static const size_t data_size = "; \
+	cat $< | wc -c; echo ";")
+
+# "config" command
+$(obj)/config.o: $(obj)/config_data_gz.h $(obj)/config_data_size.h
+
+targets += config_data.gz
+$(obj)/config_data.gz: $(KCONFIG_CONFIG) FORCE
+	$(call if_changed,gzip)
+
+targets += config_data_gz.h
+$(obj)/config_data_gz.h: $(obj)/config_data.gz FORCE
+	$(call filechk,data_gz)
+
+targets += config_data_size.h
+$(obj)/config_data_size.h: $(KCONFIG_CONFIG) FORCE
+	$(call filechk,data_size)
diff --git a/cmd/config.c b/cmd/config.c
new file mode 100644
index 0000000000..0c7f4e06cd
--- /dev/null
+++ b/cmd/config.c
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2017 Masahiro Yamada <yamada.masahiro@socionext.com>
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <command.h>
+#include <malloc.h>
+
+#include "config_data_gz.h"
+#include "config_data_size.h"
+
+static int do_config(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+	char *dst;
+	unsigned long len = data_size;
+	int ret = CMD_RET_SUCCESS;
+
+	dst = malloc(data_size + 1);
+	if (!dst)
+		return CMD_RET_FAILURE;
+
+	ret = gunzip(dst, data_size, (unsigned char *)data_gz, &len);
+	if (ret) {
+		printf("failed to uncompress .config data\n");
+		ret = CMD_RET_FAILURE;
+		goto free;
+	}
+
+	dst[data_size] = 0;
+	puts(dst);
+
+free:
+	free(dst);
+
+	return ret;
+}
+
+U_BOOT_CMD(
+	config, 1, 1, do_config,
+	"print .config",
+	""
+);
diff --git a/scripts/.gitignore b/scripts/.gitignore
index 82bc06ef98..17b903b0f5 100644
--- a/scripts/.gitignore
+++ b/scripts/.gitignore
@@ -1,4 +1,5 @@
 #
 # Generated files
 #
+bin2c
 docproc
diff --git a/scripts/Kconfig b/scripts/Kconfig
new file mode 100644
index 0000000000..2a2c18e96e
--- /dev/null
+++ b/scripts/Kconfig
@@ -0,0 +1,2 @@
+config BUILD_BIN2C
+	bool
diff --git a/scripts/Makefile b/scripts/Makefile
index 2f081f7104..3e10c16d59 100644
--- a/scripts/Makefile
+++ b/scripts/Makefile
@@ -7,6 +7,10 @@
 # SPDX-License-Identifier:	GPL-2.0
 #
 
+hostprogs-$(CONFIG_BUILD_BIN2C)		+= bin2c
+
+always		:= $(hostprogs-y)
+
 # The following hostprogs-y programs are only build on demand
 hostprogs-y += docproc
 
-- 
2.30.2