
Signed-off-by: Michael Zaidman michael.zaidman@gmail.com --- drivers/serial/serial.c | 31 +++++++++++++++++++++---------- 1 files changed, 21 insertions(+), 10 deletions(-)
diff --git a/drivers/serial/serial.c b/drivers/serial/serial.c index dd5f332..8d78983 100644 --- a/drivers/serial/serial.c +++ b/drivers/serial/serial.c @@ -45,24 +45,27 @@ DECLARE_GLOBAL_DATA_PTR; #else #error "No console index specified." #endif /* CONFIG_SERIAL_MULTI */ -#elif (CONFIG_CONS_INDEX < 1) || (CONFIG_CONS_INDEX > 4) +#elif CONFIG_CONS_INDEX < 1 #error "Invalid console index value." #endif
-#if CONFIG_CONS_INDEX == 1 && !defined(CONFIG_SYS_NS16550_COM1) -#error "Console port 1 defined but not configured." -#elif CONFIG_CONS_INDEX == 2 && !defined(CONFIG_SYS_NS16550_COM2) -#error "Console port 2 defined but not configured." -#elif CONFIG_CONS_INDEX == 3 && !defined(CONFIG_SYS_NS16550_COM3) -#error "Console port 3 defined but not configured." -#elif CONFIG_CONS_INDEX == 4 && !defined(CONFIG_SYS_NS16550_COM4) -#error "Console port 4 defined but not configured." +#define CONSOLE_PORT_SANITY_CHECK(port) \ + CONFIG_CONS_INDEX == port && !defined(CONFIG_SYS_NS16550_COM##port) + +#if CONSOLE_PORT_SANITY_CHECK(1) +#error Console port 1 defined but not configured. +#elif CONSOLE_PORT_SANITY_CHECK(2) +#error Console port 2 defined but not configured. +#elif CONSOLE_PORT_SANITY_CHECK(3) +#error Console port 3 defined but not configured. +#elif CONSOLE_PORT_SANITY_CHECK(4) +#error Console port 4 defined but not configured. #endif
/* Note: The port number specified in the functions is 1 based. * the array is 0 based. */ -static NS16550_t serial_ports[4] = { +static NS16550_t serial_ports[] = { #ifdef CONFIG_SYS_NS16550_COM1 (NS16550_t)CONFIG_SYS_NS16550_COM1, #else @@ -85,6 +88,14 @@ static NS16550_t serial_ports[4] = { #endif };
+#define MAX_SER_PORTS ((sizeof(serial_ports)/sizeof(NS16550_t))) + +static inline void sanity_check(void) +{ + /* This will fail to compile if CONFIG_CONS_INDEX > MAX_SER_DEV */ + BUILD_BUG_ON(CONFIG_CONS_INDEX > MAX_SER_PORTS); +} + #define PORT serial_ports[port-1] #if defined(CONFIG_CONS_INDEX) #define CONSOLE (serial_ports[CONFIG_CONS_INDEX-1])