config: add ringbuffer-size in config file options

Enable fine-tuning the memory usage by assigning ringbuffer size by
setting ringbuffer-size in the config file.

Change-Id: I2425e4297aadbb528598ac9bc80840774a117a03
Signed-off-by: Medicine Yeh <medicinehy@gmail.com>
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c05dd3d..0f38aeb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -20,6 +20,7 @@
 ### Added
 
 1. config: Added support for the `aspeed-uart-routing` configuration key
+2. config: Added support for the `ringbuffer-size` configuration key
 
 ### Removed
 
diff --git a/console-server.c b/console-server.c
index 1337be0..4443a26 100644
--- a/console-server.c
+++ b/console-server.c
@@ -42,8 +42,8 @@
 
 #define DEV_PTS_PATH "/dev/pts"
 
-/* size of the shared backlog ringbuffer */
-const size_t buffer_size = 128ul * 1024ul;
+/* default size of the shared backlog ringbuffer */
+const size_t default_buffer_size = 128ul * 1024ul;
 
 /* state shared with the signal handler */
 static bool sigint;
@@ -840,7 +840,13 @@
 	for (;;) {
 		uint8_t buf[4096];
 
-		BUILD_ASSERT(sizeof(buf) <= buffer_size);
+		if (console->rb->size < sizeof(buf)) {
+			fprintf(stderr,
+				"Ringbuffer size should be greater than %zuB\n",
+				sizeof(buf));
+			rc = -1;
+			break;
+		}
 
 		if (sigint) {
 			fprintf(stderr, "Received interrupt, exiting\n");
@@ -905,8 +911,10 @@
 
 int main(int argc, char **argv)
 {
+	size_t buffer_size = default_buffer_size;
 	const char *config_filename = NULL;
 	const char *config_tty_kname = NULL;
+	const char *buffer_size_str = NULL;
 	const char *console_id = NULL;
 	struct console *console;
 	struct config *config;
@@ -939,14 +947,22 @@
 		config_tty_kname = argv[optind];
 	}
 
+	config = config_init(config_filename);
+
 	console = malloc(sizeof(struct console));
 	memset(console, 0, sizeof(*console));
 	console->pollfds =
 		calloc(MAX_INTERNAL_POLLFD, sizeof(*console->pollfds));
+	buffer_size_str = config_get_value(config, "ringbuffer-size");
+	if (buffer_size_str) {
+		rc = config_parse_bytesize(buffer_size_str, &buffer_size);
+		if (rc) {
+			warn("Invalid ringbuffer-size. Default to %zukB",
+			     buffer_size >> 10);
+		}
+	}
 	console->rb = ringbuffer_init(buffer_size);
 
-	config = config_init(config_filename);
-
 	if (set_socket_info(console, config, console_id)) {
 		rc = -1;
 		goto out_config_fini;