Merge pull request #1612 from antonio-nino-diaz-arm/an/tools

tools: Make invocation of host compiler correct
diff --git a/Makefile b/Makefile
index d487eae..0d62dd7 100644
--- a/Makefile
+++ b/Makefile
@@ -288,7 +288,7 @@
 
 ifneq (${SPD},none)
 ifeq (${ARCH},aarch32)
-	$(error "Error: SPD is incompatible with AArch32.")
+        $(error "Error: SPD is incompatible with AArch32.")
 endif
 ifdef EL3_PAYLOAD_BASE
         $(warning "SPD and EL3_PAYLOAD_BASE are incompatible build options.")
@@ -396,12 +396,6 @@
 $(error USE_COHERENT_MEM cannot be enabled with HW_ASSISTED_COHERENCY)
 endif
 
-ifneq ($(MULTI_CONSOLE_API), 0)
-    ifeq (${ARCH},aarch32)
-        $(error "Error: MULTI_CONSOLE_API is not supported for AArch32")
-    endif
-endif
-
 #For now, BL2_IN_XIP_MEM is only supported when BL2_AT_EL3 is 1.
 ifeq ($(BL2_AT_EL3)-$(BL2_IN_XIP_MEM),0-1)
 $(error "BL2_IN_XIP_MEM is only supported when BL2_AT_EL3 is enabled")
diff --git a/bl31/aarch64/bl31_entrypoint.S b/bl31/aarch64/bl31_entrypoint.S
index 58e8afb..3a45e53 100644
--- a/bl31/aarch64/bl31_entrypoint.S
+++ b/bl31/aarch64/bl31_entrypoint.S
@@ -124,7 +124,7 @@
 	 * timestamp collection will need to do cache maintenance prior
 	 * to timestamp update.
 	 */
-	pmf_calc_timestamp_addr rt_instr_svc RT_INSTR_EXIT_HW_LOW_PWR
+	pmf_calc_timestamp_addr rt_instr_svc, RT_INSTR_EXIT_HW_LOW_PWR
 	mrs	x1, cntpct_el0
 	str	x1, [x0]
 #endif
@@ -180,7 +180,7 @@
 	bl	psci_warmboot_entrypoint
 
 #if ENABLE_RUNTIME_INSTRUMENTATION
-	pmf_calc_timestamp_addr rt_instr_svc RT_INSTR_EXIT_PSCI
+	pmf_calc_timestamp_addr rt_instr_svc, RT_INSTR_EXIT_PSCI
 	mov	x19, x0
 
 	/*
diff --git a/common/bl_common.c b/common/bl_common.c
index 60baa5b..d12a17c 100644
--- a/common/bl_common.c
+++ b/common/bl_common.c
@@ -48,8 +48,8 @@
 uintptr_t page_align(uintptr_t value, unsigned dir)
 {
 	/* Round up the limit to the next page boundary */
-	if (value & (PAGE_SIZE - 1)) {
-		value &= ~(PAGE_SIZE - 1);
+	if ((value & (PAGE_SIZE - 1U)) != 0U) {
+		value &= ~(PAGE_SIZE - 1U);
 		if (dir == UP)
 			value += PAGE_SIZE;
 	}
@@ -106,7 +106,7 @@
 	uintptr_t dev_handle;
 	uintptr_t image_handle;
 	uintptr_t image_spec;
-	size_t image_size = 0;
+	size_t image_size = 0U;
 	int io_result;
 
 	/* Obtain a reference to the image by querying the platform layer */
@@ -127,7 +127,7 @@
 
 	/* Find the size of the image */
 	io_result = io_size(image_handle, &image_size);
-	if ((io_result != 0) || (image_size == 0)) {
+	if ((io_result != 0) || (image_size == 0U)) {
 		WARN("Failed to determine the size of the image id=%u (%i)\n",
 			image_id, io_result);
 	}
@@ -182,12 +182,11 @@
 		return io_result;
 	}
 
-	INFO("Loading image id=%u at address %p\n", image_id,
-		(void *) image_base);
+	INFO("Loading image id=%u at address 0x%lx\n", image_id, image_base);
 
 	/* Find the size of the image */
 	io_result = io_size(image_handle, &image_size);
-	if ((io_result != 0) || (image_size == 0)) {
+	if ((io_result != 0) || (image_size == 0U)) {
 		WARN("Failed to determine the size of the image id=%u (%i)\n",
 			image_id, io_result);
 		goto exit;
@@ -200,7 +199,11 @@
 		goto exit;
 	}
 
-	image_data->image_size = image_size;
+	/*
+	 * image_data->image_max_size is a uint32_t so image_size will always
+	 * fit in image_data->image_size.
+	 */
+	image_data->image_size = (uint32_t)image_size;
 
 	/* We have enough space so load the image now */
 	/* TODO: Consider whether to try to recover/retry a partially successful read */
@@ -210,15 +213,15 @@
 		goto exit;
 	}
 
-	INFO("Image id=%u loaded: %p - %p\n", image_id, (void *) image_base,
-	     (void *) (image_base + image_size));
+	INFO("Image id=%u loaded: 0x%lx - 0x%lx\n", image_id, image_base,
+	     (uintptr_t)(image_base + image_size));
 
 exit:
-	io_close(image_handle);
+	(void)io_close(image_handle);
 	/* Ignore improbable/unrecoverable error in 'close' */
 
 	/* TODO: Consider maintaining open device connection from this bootloader stage */
-	io_dev_close(dev_handle);
+	(void)io_dev_close(dev_handle);
 	/* Ignore improbable/unrecoverable error in 'dev_close' */
 
 	return io_result;
@@ -274,7 +277,7 @@
 	 * the file has been successfully loaded and authenticated and flush
 	 * only for child images, not for the parents (certificates).
 	 */
-	if (!is_parent_image) {
+	if (is_parent_image == 0) {
 		flush_dcache_range(image_data->image_base,
 				   image_data->image_size);
 	}
@@ -296,7 +299,7 @@
 
 	do {
 		err = load_auth_image_internal(image_id, image_data, 0);
-	} while (err != 0 && plat_try_next_boot_source());
+	} while ((err != 0) && (plat_try_next_boot_source() != 0));
 
 	return err;
 }
@@ -306,7 +309,7 @@
  ******************************************************************************/
 void print_entry_point_info(const entry_point_info_t *ep_info)
 {
-	INFO("Entry point address = %p\n", (void *)ep_info->pc);
+	INFO("Entry point address = 0x%lx\n", ep_info->pc);
 	INFO("SPSR = 0x%x\n", ep_info->spsr);
 
 #define PRINT_IMAGE_ARG(n)					\
diff --git a/common/desc_image_load.c b/common/desc_image_load.c
index 28745d4..b07fba3 100644
--- a/common/desc_image_load.c
+++ b/common/desc_image_load.c
@@ -35,12 +35,12 @@
  ******************************************************************************/
 int get_bl_params_node_index(unsigned int image_id)
 {
-	int index;
+	unsigned int index;
 	assert(image_id != INVALID_IMAGE_ID);
 
-	for (index = 0; index < bl_mem_params_desc_num; index++) {
+	for (index = 0U; index < bl_mem_params_desc_num; index++) {
 		if (bl_mem_params_desc_ptr[index].image_id == image_id)
-			return index;
+			return (int)index;
 	}
 
 	return -1;
@@ -72,17 +72,17 @@
  ******************************************************************************/
 bl_load_info_t *get_bl_load_info_from_mem_params_desc(void)
 {
-	int index = 0;
+	unsigned int index = 0;
 
 	/* If there is no image to start with, return NULL */
-	if (!bl_mem_params_desc_num)
+	if (bl_mem_params_desc_num == 0U)
 		return NULL;
 
 	/* Assign initial data structures */
 	bl_load_info_node_t *bl_node_info =
 		&bl_mem_params_desc_ptr[index].load_node_mem;
 	bl_load_info.head = bl_node_info;
-	SET_PARAM_HEAD(&bl_load_info, PARAM_BL_LOAD_INFO, VERSION_2, 0);
+	SET_PARAM_HEAD(&bl_load_info, PARAM_BL_LOAD_INFO, VERSION_2, 0U);
 
 	/* Go through the image descriptor array and create the list */
 	for (; index < bl_mem_params_desc_num; index++) {
@@ -92,10 +92,10 @@
 		bl_node_info->image_info = &bl_mem_params_desc_ptr[index].image_info;
 
 		/* Link next image if present */
-		if ((index + 1) < bl_mem_params_desc_num) {
+		if ((index + 1U) < bl_mem_params_desc_num) {
 			/* Get the memory and link the next node */
 			bl_node_info->next_load_info =
-				&bl_mem_params_desc_ptr[index + 1].load_node_mem;
+				&bl_mem_params_desc_ptr[index + 1U].load_node_mem;
 			bl_node_info = bl_node_info->next_load_info;
 		}
 	}
@@ -112,19 +112,19 @@
  ******************************************************************************/
 bl_params_t *get_next_bl_params_from_mem_params_desc(void)
 {
-	int count;
-	unsigned int img_id = 0;
-	int link_index = 0;
+	unsigned int count;
+	unsigned int img_id = 0U;
+	unsigned int link_index = 0U;
 	bl_params_node_t *bl_current_exec_node = NULL;
 	bl_params_node_t *bl_last_exec_node = NULL;
 	bl_mem_params_node_t *desc_ptr;
 
 	/* If there is no image to start with, return NULL */
-	if (!bl_mem_params_desc_num)
+	if (bl_mem_params_desc_num == 0U)
 		return NULL;
 
 	/* Get the list HEAD */
-	for (count = 0; count < bl_mem_params_desc_num; count++) {
+	for (count = 0U; count < bl_mem_params_desc_num; count++) {
 
 		desc_ptr = &bl_mem_params_desc_ptr[count];
 
@@ -140,13 +140,13 @@
 	assert(next_bl_params.head != NULL);
 
 	/* Populate the HEAD information */
-	SET_PARAM_HEAD(&next_bl_params, PARAM_BL_PARAMS, VERSION_2, 0);
+	SET_PARAM_HEAD(&next_bl_params, PARAM_BL_PARAMS, VERSION_2, 0U);
 
 	/*
 	 * Go through the image descriptor array and create the list.
 	 * This bounded loop is to make sure that we are not looping forever.
 	 */
-	for (count = 0 ; count < bl_mem_params_desc_num; count++) {
+	for (count = 0U; count < bl_mem_params_desc_num; count++) {
 
 		desc_ptr = &bl_mem_params_desc_ptr[link_index];
 
@@ -161,7 +161,7 @@
 		bl_current_exec_node->image_info = &desc_ptr->image_info;
 		bl_current_exec_node->ep_info = &desc_ptr->ep_info;
 
-		if (bl_last_exec_node) {
+		if (bl_last_exec_node != NULL) {
 			/* Assert if loop detected */
 			assert(bl_last_exec_node->next_params_info == NULL);
 
@@ -179,7 +179,7 @@
 
 		/* Get the index for the next hand-off image */
 		link_index = get_bl_params_node_index(img_id);
-		assert((link_index > 0) &&
+		assert((link_index > 0U) &&
 			(link_index < bl_mem_params_desc_num));
 	}
 
@@ -243,17 +243,17 @@
 		 * overwriting the previous initialisations.
 		 */
 		if (params_node == bl2_to_next_bl_params->head) {
-			if (params_node->ep_info->args.arg1 == 0)
+			if (params_node->ep_info->args.arg1 == 0U)
 				params_node->ep_info->args.arg1 =
 								fw_config_base;
-			if (params_node->ep_info->args.arg2 == 0)
+			if (params_node->ep_info->args.arg2 == 0U)
 				params_node->ep_info->args.arg2 =
 								hw_config_base;
 		} else {
-			if (params_node->ep_info->args.arg0 == 0)
+			if (params_node->ep_info->args.arg0 == 0U)
 				params_node->ep_info->args.arg0 =
 								fw_config_base;
-			if (params_node->ep_info->args.arg1 == 0)
+			if (params_node->ep_info->args.arg1 == 0U)
 				params_node->ep_info->args.arg1 =
 								hw_config_base;
 		}
diff --git a/common/runtime_svc.c b/common/runtime_svc.c
index 03f7f7e..c30c0ec 100644
--- a/common/runtime_svc.c
+++ b/common/runtime_svc.c
@@ -35,16 +35,16 @@
 			     unsigned int flags)
 {
 	u_register_t x1, x2, x3, x4;
-	int index;
+	unsigned int index;
 	unsigned int idx;
 	const rt_svc_desc_t *rt_svc_descs;
 
-	assert(handle);
+	assert(handle != NULL);
 	idx = get_unique_oen_from_smc_fid(smc_fid);
 	assert(idx < MAX_RT_SVCS);
 
 	index = rt_svc_descs_indices[idx];
-	if (index < 0 || index >= (int)RT_SVC_DECS_NUM)
+	if (index >= RT_SVC_DECS_NUM)
 		SMC_RET1(handle, SMC_UNK);
 
 	rt_svc_descs = (rt_svc_desc_t *) RT_SVC_DESCS_START;
@@ -96,7 +96,7 @@
 void __init runtime_svc_init(void)
 {
 	int rc = 0;
-	unsigned int index, start_idx, end_idx;
+	uint8_t index, start_idx, end_idx;
 	rt_svc_desc_t *rt_svc_descs;
 
 	/* Assert the number of descriptors detected are less than maximum indices */
@@ -108,10 +108,10 @@
 		return;
 
 	/* Initialise internal variables to invalid state */
-	memset(rt_svc_descs_indices, -1, sizeof(rt_svc_descs_indices));
+	(void)memset(rt_svc_descs_indices, -1, sizeof(rt_svc_descs_indices));
 
 	rt_svc_descs = (rt_svc_desc_t *) RT_SVC_DESCS_START;
-	for (index = 0; index < RT_SVC_DECS_NUM; index++) {
+	for (index = 0U; index < RT_SVC_DECS_NUM; index++) {
 		rt_svc_desc_t *service = &rt_svc_descs[index];
 
 		/*
@@ -120,7 +120,7 @@
 		 * of this service.
 		 */
 		rc = validate_rt_svc_desc(service);
-		if (rc) {
+		if (rc != 0) {
 			ERROR("Invalid runtime service descriptor %p\n",
 				(void *) service);
 			panic();
@@ -133,9 +133,9 @@
 		 * an initialisation routine defined. Call the initialisation
 		 * routine for this runtime service, if it is defined.
 		 */
-		if (service->init) {
+		if (service->init != NULL) {
 			rc = service->init();
-			if (rc) {
+			if (rc != 0) {
 				ERROR("Error initializing runtime service %s\n",
 						service->name);
 				continue;
@@ -149,15 +149,15 @@
 		 * entity range.
 		 */
 #if SMCCC_MAJOR_VERSION == 1
-		start_idx = get_unique_oen(service->start_oen,
-					   service->call_type);
-		end_idx = get_unique_oen(service->end_oen,
-					 service->call_type);
+		start_idx = (uint8_t)get_unique_oen(service->start_oen,
+						    service->call_type);
+		end_idx = (uint8_t)get_unique_oen(service->end_oen,
+						  service->call_type);
 #elif SMCCC_MAJOR_VERSION == 2
-		start_idx = get_rt_desc_idx(service->start_oen,
-					    service->is_vendor);
-		end_idx = get_rt_desc_idx(service->end_oen,
-					  service->is_vendor);
+		start_idx = (uint8_t)get_rt_desc_idx(service->start_oen,
+						     service->is_vendor);
+		end_idx = (uint8_t)get_rt_desc_idx(service->end_oen,
+						   service->is_vendor);
 #endif
 		assert(start_idx <= end_idx);
 		assert(end_idx < MAX_RT_SVCS);
diff --git a/drivers/arm/pl011/aarch32/pl011_console.S b/drivers/arm/pl011/aarch32/pl011_console.S
index 3718fff..b7892e1 100644
--- a/drivers/arm/pl011/aarch32/pl011_console.S
+++ b/drivers/arm/pl011/aarch32/pl011_console.S
@@ -1,10 +1,12 @@
 /*
- * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved.
+ * Copyright (c) 2016-2018, ARM Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
 #include <arch.h>
 #include <asm_macros.S>
+#include <assert_macros.S>
+#include <console_macros.S>
 #include <pl011.h>
 
 /*
@@ -13,10 +15,18 @@
  */
 #include "../../../console/aarch32/console.S"
 
-	.globl	console_core_init
-	.globl	console_core_putc
-	.globl	console_core_getc
-	.globl	console_core_flush
+	/*
+	 * "core" functions are low-level implementations that don't require
+	 * writeable memory and are thus safe to call in BL1 crash context.
+	 */
+	.globl	console_pl011_core_init
+	.globl	console_pl011_core_putc
+	.globl	console_pl011_core_getc
+	.globl	console_pl011_core_flush
+
+	.globl	console_pl011_putc
+	.globl	console_pl011_getc
+	.globl	console_pl011_flush
 
 
 	/* -----------------------------------------------
@@ -33,7 +43,7 @@
 	 * Clobber list : r1, r2, r3
 	 * -----------------------------------------------
 	 */
-func console_core_init
+func console_pl011_core_init
 	/* Check the input base address */
 	cmp	r0, #0
 	beq	core_init_fail
@@ -73,7 +83,53 @@
 core_init_fail:
 	mov	r0, #0
 	bx	lr
-endfunc console_core_init
+endfunc console_pl011_core_init
+
+#if MULTI_CONSOLE_API
+	.globl console_pl011_register
+
+	/* -------------------------------------------------------
+	 * init console_pl011_register(console_pl011_t *console,
+	 *	uintptr_t base, uint32_t clk, uint32_t baud)
+	 * Function to initialize and register a new PL011
+	 * console. Storage passed in for the console struct
+	 * *must* be persistent (i.e. not from the stack).
+	 * In: r0 - UART register base address
+	 *     r1 - UART clock in Hz
+	 *     r2 - Baud rate
+	 *     r3 - pointer to empty console_pl011_t struct
+	 * Out: return 1 on success, 0 on error
+	 * Clobber list : r0, r1, r2
+	 * -------------------------------------------------------
+	 */
+func console_pl011_register
+	push	{r4, lr}
+	mov	r4, r3
+	cmp	r4, #0
+	beq	register_fail
+	str	r0, [r4, #CONSOLE_T_PL011_BASE]
+
+	bl console_pl011_core_init
+	cmp	r0, #0
+	beq	register_fail
+
+	mov	r0, r4
+	pop	{r4, lr}
+	finish_console_register pl011
+
+register_fail:
+	pop	{r4, pc}
+endfunc console_pl011_register
+#else
+	.globl console_core_init
+	.globl console_core_putc
+	.globl console_core_getc
+	.globl console_core_flush
+	.equ console_core_init, console_pl011_core_init
+	.equ console_core_putc, console_pl011_core_putc
+	.equ console_core_getc, console_pl011_core_getc
+	.equ console_core_flush, console_pl011_core_flush
+#endif
 
 	/* --------------------------------------------------------
 	 * int console_core_putc(int c, uintptr_t base_addr)
@@ -85,7 +141,7 @@
 	 * Clobber list : r2
 	 * --------------------------------------------------------
 	 */
-func console_core_putc
+func console_pl011_core_putc
 	/* Check the input parameter */
 	cmp	r1, #0
 	beq	putc_error
@@ -109,7 +165,26 @@
 putc_error:
 	mov	r0, #-1
 	bx	lr
-endfunc console_core_putc
+endfunc console_pl011_core_putc
+
+	/* --------------------------------------------------------
+	 * int console_pl011_putc(int c, console_pl011_t *console)
+	 * Function to output a character over the console. It
+	 * returns the character printed on success or -1 on error.
+	 * In: r0 - character to be printed
+	 *     r1 - pointer to console_t structure
+	 * Out : return -1 on error else return character.
+	 * Clobber list: r2
+	 * -------------------------------------------------------
+	 */
+func console_pl011_putc
+#if ENABLE_ASSERTIONS
+	cmp	r1, #0
+	ASM_ASSERT(ne)
+#endif /* ENABLE_ASSERTIONS */
+	ldr	r1, [r1, #CONSOLE_T_PL011_BASE]
+	b	console_pl011_core_putc
+endfunc console_pl011_putc
 
 	/* ---------------------------------------------
 	 * int console_core_getc(uintptr_t base_addr)
@@ -120,7 +195,7 @@
 	 * Clobber list : r0, r1
 	 * ---------------------------------------------
 	 */
-func console_core_getc
+func console_pl011_core_getc
 	cmp	r0, #0
 	beq	getc_error
 1:
@@ -134,7 +209,26 @@
 getc_error:
 	mov	r0, #-1
 	bx	lr
-endfunc console_core_getc
+endfunc console_pl011_core_getc
+
+	/* ------------------------------------------------
+	 * int console_pl011_getc(console_pl011_t *console)
+	 * Function to get a character from the console.
+	 * It returns the character grabbed on success
+	 * or -1 if no character is available.
+	 * In : r0 - pointer to console_t structure
+	 * Out: r0 - character if available, else -1
+	 * Clobber list: r0, r1
+	 * ------------------------------------------------
+	 */
+func console_pl011_getc
+#if ENABLE_ASSERTIONS
+	cmp	r0, #0
+	ASM_ASSERT(ne)
+#endif /* ENABLE_ASSERTIONS */
+	ldr	r0, [r0, #CONSOLE_T_PL011_BASE]
+	b	console_pl011_core_getc
+endfunc console_pl011_getc
 
 	/* ---------------------------------------------
 	 * int console_core_flush(uintptr_t base_addr)
@@ -145,7 +239,7 @@
 	 * Clobber list : r0, r1
 	 * ---------------------------------------------
 	 */
-func console_core_flush
+func console_pl011_core_flush
 	cmp	r0, #0
 	beq	flush_error
 
@@ -160,4 +254,22 @@
 flush_error:
 	mov	r0, #-1
 	bx	lr
-endfunc console_core_flush
+endfunc console_pl011_core_flush
+
+	/* ---------------------------------------------
+	 * int console_pl011_flush(console_pl011_t *console)
+	 * Function to force a write of all buffered
+	 * data that hasn't been output.
+	 * In : r0 - pointer to console_t structure
+	 * Out : return -1 on error else return 0.
+	 * Clobber list: r0, r1
+	 * ---------------------------------------------
+	 */
+func console_pl011_flush
+#if ENABLE_ASSERTIONS
+	cmp	r0, #0
+	ASM_ASSERT(ne)
+#endif /* ENABLE_ASSERTIONS */
+	ldr	r0, [r0, #CONSOLE_T_PL011_BASE]
+	b	console_pl011_core_flush
+endfunc console_pl011_flush
diff --git a/drivers/console/aarch32/console.S b/drivers/console/aarch32/console.S
index a3c6546..f909609 100644
--- a/drivers/console/aarch32/console.S
+++ b/drivers/console/aarch32/console.S
@@ -3,104 +3,9 @@
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
-#include <asm_macros.S>
 
-	.globl	console_init
-	.globl	console_uninit
-	.globl	console_putc
-	.globl	console_getc
-	.globl	console_flush
-
-	/*
-	 *  The console base is in the data section and not in .bss
-	 *  even though it is zero-init. In particular, this allows
-	 *  the console functions to start using this variable before
-	 *  the runtime memory is initialized for images which do not
-	 *  need to copy the .data section from ROM to RAM.
-	 */
-.section .data.console_base ; .align 2
-	console_base: .word 0x0
-
-	/* -----------------------------------------------
-	 * int console_init(uintptr_t base_addr,
-	 * unsigned int uart_clk, unsigned int baud_rate)
-	 * Function to initialize the console without a
-	 * C Runtime to print debug information. It saves
-	 * the console base to the data section.
-	 * In: r0 - console base address
-	 *     r1 - Uart clock in Hz
-	 *     r2 - Baud rate
-	 * out: return 1 on success else 0 on error
-	 * Clobber list : r1 - r3
-	 * -----------------------------------------------
-	 */
-func console_init
-	/* Check the input base address */
-	cmp	r0, #0
-	beq	init_fail
-	ldr	r3, =console_base
-	str	r0, [r3]
-	b	console_core_init
-init_fail:
-	bx	lr
-endfunc console_init
-
-	/* -----------------------------------------------
-	 * void console_uninit(void)
-	 * Function to finish the use of console driver.
-	 * It sets the console_base as NULL so that any
-	 * further invocation of `console_putc` or
-	 * `console_getc` APIs would return error.
-	 * -----------------------------------------------
-	 */
-func console_uninit
-	mov	r0, #0
-	ldr	r3, =console_base
-	str	r0, [r3]
-	bx	lr
-endfunc console_uninit
-
-	/* ---------------------------------------------
-	 * int console_putc(int c)
-	 * Function to output a character over the
-	 * console. It returns the character printed on
-	 * success or -1 on error.
-	 * In : r0 - character to be printed
-	 * Out : return -1 on error else return character.
-	 * Clobber list : r1, r2
-	 * ---------------------------------------------
-	 */
-func console_putc
-	ldr	r2, =console_base
-	ldr	r1, [r2]
-	b	console_core_putc
-endfunc console_putc
-
-	/* ---------------------------------------------
-	 * int console_getc(void)
-	 * Function to get a character from the console.
-	 * It returns the character grabbed on success
-	 * or -1 on error.
-	 * Clobber list : r0, r1
-	 * ---------------------------------------------
-	 */
-func console_getc
-	ldr	r1, =console_base
-	ldr	r0, [r1]
-	b	console_core_getc
-endfunc console_getc
-
-	/* ---------------------------------------------
-	 * int console_flush(void)
-	 * Function to force a write of all buffered
-	 * data that hasn't been output. It returns 0
-	 * upon successful completion, otherwise it
-	 * returns -1.
-	 * Clobber list : r0, r1
-	 * ---------------------------------------------
-	 */
-func console_flush
-	ldr	r1, =console_base
-	ldr	r0, [r1]
-	b	console_core_flush
-endfunc console_flush
+ #if MULTI_CONSOLE_API
+ #include "multi_console.S"
+ #else
+ #include "deprecated_console.S"
+ #endif
diff --git a/drivers/console/aarch32/deprecated_console.S b/drivers/console/aarch32/deprecated_console.S
new file mode 100644
index 0000000..f7e3c4f
--- /dev/null
+++ b/drivers/console/aarch32/deprecated_console.S
@@ -0,0 +1,112 @@
+/*
+ * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+#include <asm_macros.S>
+
+/*
+ * This is the common console core code for the deprecated single-console API.
+ * New platforms should set MULTI_CONSOLE_API=1 and not use this file.
+ */
+#warning "Using deprecated console implementation. Please migrate to MULTI_CONSOLE_API"
+
+	.globl	console_init
+	.globl	console_uninit
+	.globl	console_putc
+	.globl	console_getc
+	.globl	console_flush
+
+	/*
+	 *  The console base is in the data section and not in .bss
+	 *  even though it is zero-init. In particular, this allows
+	 *  the console functions to start using this variable before
+	 *  the runtime memory is initialized for images which do not
+	 *  need to copy the .data section from ROM to RAM.
+	 */
+.section .data.console_base ; .align 2
+	console_base: .word 0x0
+
+	/* -----------------------------------------------
+	 * int console_init(uintptr_t base_addr,
+	 * unsigned int uart_clk, unsigned int baud_rate)
+	 * Function to initialize the console without a
+	 * C Runtime to print debug information. It saves
+	 * the console base to the data section.
+	 * In: r0 - console base address
+	 *     r1 - Uart clock in Hz
+	 *     r2 - Baud rate
+	 * out: return 1 on success else 0 on error
+	 * Clobber list : r1 - r3
+	 * -----------------------------------------------
+	 */
+func console_init
+	/* Check the input base address */
+	cmp	r0, #0
+	beq	init_fail
+	ldr	r3, =console_base
+	str	r0, [r3]
+	b	console_core_init
+init_fail:
+	bx	lr
+endfunc console_init
+
+	/* -----------------------------------------------
+	 * void console_uninit(void)
+	 * Function to finish the use of console driver.
+	 * It sets the console_base as NULL so that any
+	 * further invocation of `console_putc` or
+	 * `console_getc` APIs would return error.
+	 * -----------------------------------------------
+	 */
+func console_uninit
+	mov	r0, #0
+	ldr	r3, =console_base
+	str	r0, [r3]
+	bx	lr
+endfunc console_uninit
+
+	/* ---------------------------------------------
+	 * int console_putc(int c)
+	 * Function to output a character over the
+	 * console. It returns the character printed on
+	 * success or -1 on error.
+	 * In : r0 - character to be printed
+	 * Out : return -1 on error else return character.
+	 * Clobber list : r1, r2
+	 * ---------------------------------------------
+	 */
+func console_putc
+	ldr	r2, =console_base
+	ldr	r1, [r2]
+	b	console_core_putc
+endfunc console_putc
+
+	/* ---------------------------------------------
+	 * int console_getc(void)
+	 * Function to get a character from the console.
+	 * It returns the character grabbed on success
+	 * or -1 on error.
+	 * Clobber list : r0, r1
+	 * ---------------------------------------------
+	 */
+func console_getc
+	ldr	r1, =console_base
+	ldr	r0, [r1]
+	b	console_core_getc
+endfunc console_getc
+
+	/* ---------------------------------------------
+	 * int console_flush(void)
+	 * Function to force a write of all buffered
+	 * data that hasn't been output. It returns 0
+	 * upon successful completion, otherwise it
+	 * returns -1.
+	 * Clobber list : r0, r1
+	 * ---------------------------------------------
+	 */
+func console_flush
+	ldr	r1, =console_base
+	ldr	r0, [r1]
+	b	console_core_flush
+endfunc console_flush
diff --git a/drivers/console/aarch32/multi_console.S b/drivers/console/aarch32/multi_console.S
new file mode 100644
index 0000000..e23b20e
--- /dev/null
+++ b/drivers/console/aarch32/multi_console.S
@@ -0,0 +1,318 @@
+/*
+ * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <asm_macros.S>
+#include <assert_macros.S>
+#include <console.h>
+
+	.globl	console_register
+	.globl	console_unregister
+	.globl	console_is_registered
+	.globl	console_set_scope
+	.globl	console_switch_state
+	.globl	console_putc
+	.globl	console_getc
+	.globl	console_flush
+
+	/*
+	 *  The console list pointer is in the data section and not in
+	 *  .bss even though it is zero-init. In particular, this allows
+	 *  the console functions to start using this variable before
+	 *  the runtime memory is initialized for images which do not
+	 *  need to copy the .data section from ROM to RAM.
+	 */
+.section .data.console_list ; .align 2
+	console_list: .word 0x0
+.section .data.console_state ; .align 0
+	console_state: .byte CONSOLE_FLAG_BOOT
+
+	/* -----------------------------------------------
+	 * int console_register(console_t *console)
+	 * Function to insert a new console structure into
+	 * the console list. Should usually be called by
+	 * console_<driver>_register implementations. The
+	 * data structure passed will be taken over by the
+	 * console framework and *MUST* be allocated in
+	 * persistent memory (e.g. the data section).
+	 * In : r0 - address of console_t structure
+	 * Out: r0 - Always 1 (for easier tail calling)
+	 * Clobber list: r0, r1
+	 * -----------------------------------------------
+	 */
+func console_register
+	push	{r6,  lr}
+#if ENABLE_ASSERTIONS
+	/* Assert that r0 isn't a NULL pointer */
+	cmp	r0, #0
+	ASM_ASSERT(ne)
+	/* Assert that the struct isn't in the stack */
+	ldr	r1, =__STACKS_START__
+	cmp	r0, r1
+	blo	not_on_stack
+	ldr	r1, =__STACKS_END__
+	cmp	r0, r1
+	ASM_ASSERT(hs)
+not_on_stack:
+	/* Assert that this struct isn't in the list */
+	mov	r1, r0 /* Preserve r0 and lr */
+	bl	console_is_registered
+	cmp	r0, #0
+	ASM_ASSERT(eq)
+	mov	r0, r1
+#endif /* ENABLE_ASSERTIONS */
+	ldr	r6, =console_list
+	ldr	r1, [r6]	/* R1 = first struct in list */
+	str	r0, [r6]	/* list head = new console */
+	str	r1, [r0, #CONSOLE_T_NEXT]	/* new console next ptr = R1 */
+	mov	r0, #1
+	pop	{r6, pc}
+endfunc console_register
+
+	/* -----------------------------------------------
+	 * int console_unregister(console_t *console)
+	 * Function to find a specific console in the list
+	 * of currently active consoles and remove it.
+	 * In: r0 - address of console_t struct to remove
+	 * Out: r0 - removed address, or NULL if not found
+	 * Clobber list: r0, r1
+	 * -----------------------------------------------
+	 */
+func console_unregister
+#if ENABLE_ASSERTIONS
+	/* Assert that r0 isn't a NULL pointer */
+	cmp	r0, #0
+	ASM_ASSERT(ne)
+#endif /* ENABLE_ASSERTIONS */
+	push	{r6}
+	ldr	r6, =console_list		/* R6 = ptr to first struct */
+	ldr	r1, [r6]			/* R1 = first struct */
+
+unregister_loop:
+	cmp	r1, #0
+	beq	unregister_not_found
+	cmp	r0, r1
+	beq	unregister_found
+	ldr	r6, [r6]			/* R6 = next ptr of struct */
+	ldr	r1, [r6]			/* R1 = next struct */
+	b	unregister_loop
+
+unregister_found:
+	ldr	r1, [r1]			/* R1 = next struct */
+	str	r1, [r6]			/* prev->next = cur->next */
+	pop	{r6}
+	bx	lr
+
+unregister_not_found:
+	mov	r0, #0				/* return NULL if not found */
+	pop	{r6}
+	bx	lr
+endfunc console_unregister
+
+	/* -----------------------------------------------
+	 * int console_is_registered(console_t *console)
+	 * Function to detect if a specific console is
+	 * registered or not.
+	 * In: r0 - address of console_t struct to remove
+	 * Out: r0 - 1 if it is registered, 0 if not.
+	 * Clobber list: r0
+	 * -----------------------------------------------
+	 */
+func console_is_registered
+#if ENABLE_ASSERTIONS
+	/* Assert that r0 isn't a NULL pointer */
+	cmp	r0, #0
+	ASM_ASSERT(ne)
+#endif /* ENABLE_ASSERTIONS */
+	push	{r6}
+	ldr	r6, =console_list
+	ldr	r6, [r6]	/* R6 = first console struct */
+check_registered_loop:
+	cmp	r6, #0			/* Check if end of list */
+	beq	console_not_registered
+	cmp	r0, r6		/* Check if the pointers are different */
+	beq	console_registered
+	ldr	r6, [r6, #CONSOLE_T_NEXT]	/* Get pointer to next struct */
+	b	check_registered_loop
+console_not_registered:
+	mov	r0, #0
+	pop	{r6}
+	bx	lr
+console_registered:
+	mov	r0, #1
+	pop	{r6}
+	bx	lr
+endfunc console_is_registered
+
+	/* -----------------------------------------------
+	 * void console_switch_state(unsigned int new_state)
+	 * Function to switch the current console state.
+	 * The console state determines which of the
+	 * registered consoles are actually used at a time.
+	 * In : r0 - global console state to move to
+	 * Clobber list: r0, r1
+	 * -----------------------------------------------
+	 */
+func console_switch_state
+	ldr	r1, =console_state
+	strb	r0, [r1]
+	bx	lr
+endfunc console_switch_state
+
+	/* -----------------------------------------------
+	 * void console_set_scope(console_t *console,
+	 *                       unsigned int scope)
+	 * Function to update the states that a given console
+	 * may be active in.
+	 * In : r0 - pointer to console_t struct
+	 *    : r1 - new active state mask
+	 * Clobber list: r0, r1, r2
+	 * -----------------------------------------------
+	 */
+func console_set_scope
+#if ENABLE_ASSERTIONS
+	ands	r2, r1, #~CONSOLE_FLAG_SCOPE_MASK
+	ASM_ASSERT(eq)
+#endif /* ENABLE_ASSERTIONS */
+	ldr	r2, [r0, #CONSOLE_T_FLAGS]
+	and	r2, r2, #~CONSOLE_FLAG_SCOPE_MASK
+	orr	r2, r2, r1
+	str	r2, [r0, #CONSOLE_T_FLAGS]
+	bx	lr
+endfunc console_set_scope
+
+	/* ---------------------------------------------
+	 * int console_putc(int c)
+	 * Function to output a character. Calls all
+	 * active console's putc() handlers in succession.
+	 * In : r0 - character to be printed
+	 * Out: r0 - printed character on success, or < 0
+	             if at least one console had an error
+	 * Clobber list : r0, r1, r2
+	 * ---------------------------------------------
+	 */
+func console_putc
+	push	{r4-r6, lr}
+	mov	r5, #ERROR_NO_VALID_CONSOLE	/* R5 = current return value */
+	mov	r4, r0				/* R4 = character to print */
+	ldr	r6, =console_list
+	ldr	r6, [r6]	/* R6 = first console struct */
+
+putc_loop:
+	cmp	r6, #0
+	beq	putc_done
+	ldr	r1, =console_state
+	ldrb	r1, [r1]
+	ldr	r2, [r6, #CONSOLE_T_FLAGS]
+	tst	r1, r2
+	beq	putc_continue
+	ldr	r2, [r6, #CONSOLE_T_PUTC]
+	cmp	r2, #0
+	beq	putc_continue
+	mov	r0, r4
+	mov	r1, r6
+	blx	r2
+	cmp	r5, #ERROR_NO_VALID_CONSOLE	/* update R5 if it's NOVALID */
+	cmpne	r0, #0				/* else update it if R0 < 0 */
+	movlt	r5, r0
+putc_continue:
+	ldr	r6, [r6]			/* R6 = next struct */
+	b	putc_loop
+
+putc_done:
+	mov	r0, r5
+	pop	{r4-r6, pc}
+endfunc console_putc
+
+	/* ---------------------------------------------
+	 * int console_getc(void)
+	 * Function to get a character from any console.
+	 * Keeps looping through all consoles' getc()
+	 * handlers until one of them returns a
+	 * character, then stops iterating and returns
+	 * that character to the caller. Will stop looping
+	 * if all active consoles report real errors
+	 * (other than just not having a char available).
+	 * Out : r0 - read character, or < 0 on error
+	 * Clobber list : r0, r1
+	 * ---------------------------------------------
+	 */
+func console_getc
+	push	{r5-r6, lr}
+getc_try_again:
+	mov	r5, #ERROR_NO_VALID_CONSOLE	/* R5 = current return value */
+	ldr	r6, =console_list
+	ldr	r6, [r6]			/* R6 = first console struct */
+	cmp	r6, #0
+	bne	getc_loop
+	mov	r0, r5				/* If no consoles registered */
+	pop	{r5-r6, pc}			/* return immediately. */
+
+getc_loop:
+	ldr	r0, =console_state
+	ldrb	r0, [r0]
+	ldr	r1, [r6, #CONSOLE_T_FLAGS]
+	tst	r0, r1
+	beq	getc_continue
+	ldr	r1, [r6, #CONSOLE_T_GETC]
+	cmp	r1, #0
+	beq	getc_continue
+	mov	r0, r6
+	blx	r1
+	cmp	r0, #0				/* if R0 >= 0: return */
+	bge	getc_found
+	cmp	r5, #ERROR_NO_PENDING_CHAR	/* may update R5 (NOCHAR has */
+	movne	r5, r0				/* precedence vs real errors) */
+getc_continue:
+	ldr	r6, [r6]			/* R6 = next struct */
+	cmp	r6, #0
+	bne	getc_loop
+	cmp	r5, #ERROR_NO_PENDING_CHAR	/* Keep scanning if at least */
+	beq	getc_try_again			/* one console returns NOCHAR */
+	mov	r0, r5
+
+getc_found:
+	pop	{r5-r6, pc}
+endfunc console_getc
+
+	/* ---------------------------------------------
+	 * int console_flush(void)
+	 * Function to force a write of all buffered
+	 * data that hasn't been output. Calls all
+	 * console's flush() handlers in succession.
+	 * Out: r0 - 0 on success, < 0 if at least one error
+	 * Clobber list : r0, r1, r2
+	 * ---------------------------------------------
+	 */
+func console_flush
+	push	{r5-r6, lr}
+	mov	r5, #ERROR_NO_VALID_CONSOLE	/* R5 = current return value */
+	ldr	r6, =console_list
+	ldr	r6, [r6]			/* R6 = first console struct */
+
+flush_loop:
+	cmp	r6, #0
+	beq	flush_done
+	ldr	r1, =console_state
+	ldrb	r1, [r1]
+	ldr	r2, [r6, #CONSOLE_T_FLAGS]
+	tst	r1, r2
+	beq	flush_continue
+	ldr	r1, [r6, #CONSOLE_T_FLUSH]
+	cmp	r1, #0
+	beq	flush_continue
+	mov	r0, r6
+	blx	r1
+	cmp	r5, #ERROR_NO_VALID_CONSOLE	/* update R5 if it's NOVALID */
+	cmpne	r0, #0				/* else update it if R0 < 0 */
+	movlt	r5, r0
+flush_continue:
+	ldr	r6, [r6]			/* R6 = next struct */
+	b	flush_loop
+
+flush_done:
+	mov	r0, r5
+	pop	{r5-r6, pc}
+endfunc console_flush
diff --git a/include/common/aarch32/console_macros.S b/include/common/aarch32/console_macros.S
new file mode 100644
index 0000000..480e3c2
--- /dev/null
+++ b/include/common/aarch32/console_macros.S
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+#ifndef __CONSOLE_MACROS_S__
+#define __CONSOLE_MACROS_S__
+
+#include <console.h>
+
+/*
+ * This macro encapsulates the common setup that has to be done at the end of
+ * a console driver's register function. It will register all of the driver's
+ * callbacks in the console_t structure and initialize the flags field (by
+ * default consoles are enabled for the "boot" and "crash" states, this can be
+ * changed after registration with the console_set_scope() function). It ends
+ * with a tail call that will include return to the caller.
+ * REQUIRES console_t pointer in x0 and a valid return address in x30.
+ */
+	.macro	finish_console_register _driver
+	/*
+	 * Add these weak definitions so we will automatically write a 0 if the
+	 * function doesn't exist. I'd rather use .ifdef but that only works if
+	 * the function was defined (not just declared .global) above this point
+	 * in the file, which we can't guarantee.
+	 */
+	.weak console_\_driver\()_putc
+	.weak console_\_driver\()_getc
+	.weak console_\_driver\()_flush
+
+	/* Don't use adrp on weak funcs! See GNU ld bugzilla issue 22589. */
+	ldr	r1, =console_\_driver\()_putc
+	str	r1, [r0, #CONSOLE_T_PUTC]
+	ldr	r1, =console_\_driver\()_getc
+	str	r1, [r0, #CONSOLE_T_GETC]
+	ldr	r1, =console_\_driver\()_flush
+	str	r1, [r0, #CONSOLE_T_FLUSH]
+	mov	r1, #(CONSOLE_FLAG_BOOT | CONSOLE_FLAG_CRASH)
+	str	r1, [r0, #CONSOLE_T_FLAGS]
+	b	console_register
+	.endm
+
+#endif /* __CONSOLE_MACROS_S__ */
diff --git a/include/common/bl_common.h b/include/common/bl_common.h
index bfe1d8c..af598d0 100644
--- a/include/common/bl_common.h
+++ b/include/common/bl_common.h
@@ -11,26 +11,26 @@
 #include <param_header.h>
 #include <utils_def.h>
 
-#define UP	1
-#define DOWN	0
+#define UP	U(1)
+#define DOWN	U(0)
 
 /*******************************************************************************
  * Constants to identify the location of a memory region in a given memory
  * layout.
 ******************************************************************************/
-#define TOP	0x1
-#define BOTTOM	!TOP
+#define TOP	U(0x1)
+#define BOTTOM	U(0x0)
 
 /*
  * The following are used for image state attributes.
  * Image can only be in one of the following state.
  */
-#define IMAGE_STATE_RESET			0
-#define IMAGE_STATE_COPIED			1
-#define IMAGE_STATE_COPYING			2
-#define IMAGE_STATE_AUTHENTICATED		3
-#define IMAGE_STATE_EXECUTED			4
-#define IMAGE_STATE_INTERRUPTED			5
+#define IMAGE_STATE_RESET		U(0)
+#define IMAGE_STATE_COPIED		U(1)
+#define IMAGE_STATE_COPYING		U(2)
+#define IMAGE_STATE_AUTHENTICATED	U(3)
+#define IMAGE_STATE_EXECUTED		U(4)
+#define IMAGE_STATE_INTERRUPTED		U(5)
 
 #define IMAGE_ATTRIB_SKIP_LOADING	U(0x02)
 #define IMAGE_ATTRIB_PLAT_SETUP		U(0x04)
@@ -40,22 +40,22 @@
 /*******************************************************************************
  * Constants to indicate type of exception to the common exception handler.
  ******************************************************************************/
-#define SYNC_EXCEPTION_SP_EL0		0x0
-#define IRQ_SP_EL0			0x1
-#define FIQ_SP_EL0			0x2
-#define SERROR_SP_EL0			0x3
-#define SYNC_EXCEPTION_SP_ELX		0x4
-#define IRQ_SP_ELX			0x5
-#define FIQ_SP_ELX			0x6
-#define SERROR_SP_ELX			0x7
-#define SYNC_EXCEPTION_AARCH64		0x8
-#define IRQ_AARCH64			0x9
-#define FIQ_AARCH64			0xa
-#define SERROR_AARCH64			0xb
-#define SYNC_EXCEPTION_AARCH32		0xc
-#define IRQ_AARCH32			0xd
-#define FIQ_AARCH32			0xe
-#define SERROR_AARCH32			0xf
+#define SYNC_EXCEPTION_SP_EL0		U(0x0)
+#define IRQ_SP_EL0			U(0x1)
+#define FIQ_SP_EL0			U(0x2)
+#define SERROR_SP_EL0			U(0x3)
+#define SYNC_EXCEPTION_SP_ELX		U(0x4)
+#define IRQ_SP_ELX			U(0x5)
+#define FIQ_SP_ELX			U(0x6)
+#define SERROR_SP_ELX			U(0x7)
+#define SYNC_EXCEPTION_AARCH64		U(0x8)
+#define IRQ_AARCH64			U(0x9)
+#define FIQ_AARCH64			U(0xa)
+#define SERROR_AARCH64			U(0xb)
+#define SYNC_EXCEPTION_AARCH32		U(0xc)
+#define IRQ_AARCH32			U(0xd)
+#define FIQ_AARCH32			U(0xe)
+#define SERROR_AARCH32			U(0xf)
 
 #ifndef __ASSEMBLY__
 #include <cassert.h>
diff --git a/include/common/fdt_wrappers.h b/include/common/fdt_wrappers.h
index 18a6d10..3eae944 100644
--- a/include/common/fdt_wrappers.h
+++ b/include/common/fdt_wrappers.h
@@ -10,7 +10,7 @@
 #define __FDT_WRAPPERS__
 
 /* Number of cells, given total length in bytes. Each cell is 4 bytes long */
-#define NCELLS(len) ((len) / 4)
+#define NCELLS(len) ((len) / 4U)
 
 int fdtw_read_cells(const void *dtb, int node, const char *prop,
 		unsigned int cells, void *value);
diff --git a/include/common/runtime_svc.h b/include/common/runtime_svc.h
index b8cf8de..e32c287 100644
--- a/include/common/runtime_svc.h
+++ b/include/common/runtime_svc.h
@@ -4,8 +4,8 @@
  * SPDX-License-Identifier: BSD-3-Clause
  */
 
-#ifndef __RUNTIME_SVC_H__
-#define __RUNTIME_SVC_H__
+#ifndef RUNTIME_SVC_H
+#define RUNTIME_SVC_H
 
 #include <bl_common.h>		/* to include exception types */
 #include <cassert.h>
@@ -21,15 +21,15 @@
  * descriptor
  */
 #ifdef AARCH32
-#define RT_SVC_SIZE_LOG2	4
-#define RT_SVC_DESC_INIT	8
-#define RT_SVC_DESC_HANDLE	12
+#define RT_SVC_SIZE_LOG2	U(4)
+#define RT_SVC_DESC_INIT	U(8)
+#define RT_SVC_DESC_HANDLE	U(12)
 #else
-#define RT_SVC_SIZE_LOG2	5
-#define RT_SVC_DESC_INIT	16
-#define RT_SVC_DESC_HANDLE	24
+#define RT_SVC_SIZE_LOG2	U(5)
+#define RT_SVC_DESC_INIT	U(16)
+#define RT_SVC_DESC_HANDLE	U(24)
 #endif /* AARCH32 */
-#define SIZEOF_RT_SVC_DESC	(1 << RT_SVC_SIZE_LOG2)
+#define SIZEOF_RT_SVC_DESC	(U(1) << RT_SVC_SIZE_LOG2)
 
 
 /*
@@ -43,9 +43,9 @@
  * handler and so the total number of runtime services is 32.
  */
 #if SMCCC_MAJOR_VERSION == 1
-#define MAX_RT_SVCS		128
+#define MAX_RT_SVCS		U(128)
 #elif SMCCC_MAJOR_VERSION == 2
-#define MAX_RT_SVCS		32
+#define MAX_RT_SVCS		U(32)
 #endif
 
 #ifndef __ASSEMBLY__
@@ -88,12 +88,12 @@
 #define DECLARE_RT_SVC(_name, _start, _end, _type, _setup, _smch)	\
 	static const rt_svc_desc_t __svc_desc_ ## _name			\
 		__section("rt_svc_descs") __used = {			\
-			.start_oen = _start,				\
-			.end_oen = _end,				\
-			.call_type = _type,				\
+			.start_oen = (_start),				\
+			.end_oen = (_end),				\
+			.call_type = (_type),				\
 			.name = #_name,					\
-			.init = _setup,					\
-			.handle = _smch					\
+			.init = (_setup),				\
+			.handle = (_smch)				\
 		}
 
 #elif SMCCC_MAJOR_VERSION == 2
@@ -101,12 +101,12 @@
 #define DECLARE_RT_SVC(_name, _start, _end, _type, _setup, _smch)	\
 	static const rt_svc_desc_t __svc_desc_ ## _name			\
 		__section("rt_svc_descs") __used = {			\
-			.start_oen = _start,				\
-			.end_oen = _end,				\
+			.start_oen = (_start),				\
+			.end_oen = (_end),				\
 			.is_vendor = 0,					\
 			.name = #_name,					\
-			.init = _setup,					\
-			.handle = _smch,				\
+			.init = (_setup),				\
+			.handle = (_smch),				\
 		};							\
 	CASSERT((_type) == SMC_TYPE_FAST, rt_svc_type_check_ ## _name)
 
@@ -146,35 +146,42 @@
 
 #if SMCCC_MAJOR_VERSION == 1
 /*
- * This macro combines the call type and the owning entity number corresponding
- * to a runtime service to generate a unique owning entity number. This unique
- * oen is used to access an entry in the 'rt_svc_descs_indices' array. The entry
- * contains the index of the service descriptor in the 'rt_svc_descs' array.
+ * This function combines the call type and the owning entity number
+ * corresponding to a runtime service to generate a unique owning entity number.
+ * This unique oen is used to access an entry in the 'rt_svc_descs_indices'
+ * array. The entry contains the index of the service descriptor in the
+ * 'rt_svc_descs' array.
  */
-#define get_unique_oen(oen, call_type)				\
-	(((uint32_t)(oen) & FUNCID_OEN_MASK) |			\
-	(((uint32_t)(call_type) & FUNCID_TYPE_MASK) << FUNCID_OEN_WIDTH))
+static inline uint32_t get_unique_oen(uint32_t oen, uint32_t call_type)
+{
+	return ((call_type & FUNCID_TYPE_MASK) << FUNCID_OEN_WIDTH) |
+		(oen & FUNCID_OEN_MASK);
+}
 
 /*
- * This macro generates the unique owning entity number from the SMC Function
+ * This function generates the unique owning entity number from the SMC Function
  * ID. This unique oen is used to access an entry in the 'rt_svc_descs_indices'
  * array to invoke the corresponding runtime service handler during SMC
  * handling.
  */
-#define get_unique_oen_from_smc_fid(fid)			\
-	get_unique_oen(GET_SMC_OEN(fid), GET_SMC_TYPE(fid))
+static inline uint32_t get_unique_oen_from_smc_fid(uint32_t fid)
+{
+	return get_unique_oen(GET_SMC_OEN(fid), GET_SMC_TYPE(fid));
+}
 
 #elif SMCCC_MAJOR_VERSION == 2
 
 /*
- * This macro combines the owning entity number corresponding to a runtime
+ * This function combines the owning entity number corresponding to a runtime
  * service with one extra bit for the vendor namespace to generate an index into
  * the 'rt_svc_descs_indices' array. The entry contains the index of the service
  * descriptor in the 'rt_svc_descs' array.
  */
-#define get_rt_desc_idx(oen, is_vendor)				\
-	(((uint32_t)(oen) & FUNCID_OEN_MASK) |			\
-	(((uint32_t)(is_vendor) & 1U) << FUNCID_OEN_WIDTH))
+static inline uint32_t get_rt_desc_idx(uint32_t oen, uint32_t is_vendor)
+{
+	return ((is_vendor & 1U) << FUNCID_OEN_WIDTH) |
+		(oen & FUNCID_OEN_MASK);
+}
 
 #endif
 
@@ -191,4 +198,4 @@
 extern uint8_t rt_svc_descs_indices[MAX_RT_SVCS];
 
 #endif /*__ASSEMBLY__*/
-#endif /* __RUNTIME_SVC_H__ */
+#endif /* RUNTIME_SVC_H */
diff --git a/include/lib/aarch32/smccc_helpers.h b/include/lib/aarch32/smccc_helpers.h
index d6e525f..5eeb204 100644
--- a/include/lib/aarch32/smccc_helpers.h
+++ b/include/lib/aarch32/smccc_helpers.h
@@ -10,19 +10,19 @@
 #include <smccc.h>
 
 /* These are offsets to registers in smc_ctx_t */
-#define SMC_CTX_GPREG_R0	0x0
-#define SMC_CTX_GPREG_R1	0x4
-#define SMC_CTX_GPREG_R2	0x8
-#define SMC_CTX_GPREG_R3	0xC
-#define SMC_CTX_GPREG_R4	0x10
-#define SMC_CTX_GPREG_R5	0x14
-#define SMC_CTX_SP_USR		0x34
-#define SMC_CTX_SPSR_MON	0x78
-#define SMC_CTX_SP_MON		0x7C
-#define SMC_CTX_LR_MON		0x80
-#define SMC_CTX_SCR		0x84
-#define SMC_CTX_PMCR		0x88
-#define SMC_CTX_SIZE		0x90
+#define SMC_CTX_GPREG_R0	U(0x0)
+#define SMC_CTX_GPREG_R1	U(0x4)
+#define SMC_CTX_GPREG_R2	U(0x8)
+#define SMC_CTX_GPREG_R3	U(0xC)
+#define SMC_CTX_GPREG_R4	U(0x10)
+#define SMC_CTX_GPREG_R5	U(0x14)
+#define SMC_CTX_SP_USR		U(0x34)
+#define SMC_CTX_SPSR_MON	U(0x78)
+#define SMC_CTX_SP_MON		U(0x7C)
+#define SMC_CTX_LR_MON		U(0x80)
+#define SMC_CTX_SCR		U(0x84)
+#define SMC_CTX_PMCR		U(0x88)
+#define SMC_CTX_SIZE		U(0x90)
 
 #ifndef __ASSEMBLY__
 #include <cassert.h>
@@ -105,7 +105,7 @@
 CASSERT(SMC_CTX_SPSR_MON == __builtin_offsetof(smc_ctx_t, spsr_mon), \
 	assert_smc_ctx_spsr_mon_offset_mismatch);
 
-CASSERT((sizeof(smc_ctx_t) & 0x7) == 0, assert_smc_ctx_not_aligned);
+CASSERT((sizeof(smc_ctx_t) & 0x7U) == 0U, assert_smc_ctx_not_aligned);
 CASSERT(SMC_CTX_SIZE == sizeof(smc_ctx_t), assert_smc_ctx_size_mismatch);
 
 /* Convenience macros to return from SMC handler */
diff --git a/include/lib/aarch64/smccc_helpers.h b/include/lib/aarch64/smccc_helpers.h
index 4d9217b..c76faf5 100644
--- a/include/lib/aarch64/smccc_helpers.h
+++ b/include/lib/aarch64/smccc_helpers.h
@@ -11,6 +11,7 @@
 
 #ifndef __ASSEMBLY__
 #include <context.h>
+#include <stdbool.h>
 
 /* Convenience macros to return from SMC handler */
 #define SMC_RET0(_h)	{					\
@@ -77,7 +78,7 @@
 		_x2 = read_ctx_reg(regs, CTX_GPREG_X2);		\
 		_x3 = read_ctx_reg(regs, CTX_GPREG_X3);		\
 		_x4 = read_ctx_reg(regs, CTX_GPREG_X4);		\
-	} while (0)
+	} while (false)
 
 #endif /*__ASSEMBLY__*/
 
diff --git a/include/lib/pmf/pmf_asm_macros.S b/include/lib/pmf/pmf_asm_macros.S
index 9ee8199..d58829e 100644
--- a/include/lib/pmf/pmf_asm_macros.S
+++ b/include/lib/pmf/pmf_asm_macros.S
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved.
+ * Copyright (c) 2016-2018, ARM Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
@@ -14,7 +14,7 @@
 	 * for the given service name and local timestamp id.
 	 * Clobbers: x0 - x9
 	 */
-	.macro pmf_calc_timestamp_addr _name _tid
+	.macro pmf_calc_timestamp_addr _name, _tid
 	mov	x9, x30
 	bl	plat_my_core_pos
 	mov	x30, x9
diff --git a/include/lib/runtime_instr.h b/include/lib/runtime_instr.h
index b2f1a69..ed564e2 100644
--- a/include/lib/runtime_instr.h
+++ b/include/lib/runtime_instr.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved.
+ * Copyright (c) 2016-2018, ARM Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
@@ -7,13 +7,15 @@
 #ifndef __RUNTIME_INSTR_H__
 #define __RUNTIME_INSTR_H__
 
-#define RT_INSTR_ENTER_PSCI		0
-#define RT_INSTR_EXIT_PSCI		1
-#define RT_INSTR_ENTER_HW_LOW_PWR	2
-#define RT_INSTR_EXIT_HW_LOW_PWR	3
-#define RT_INSTR_ENTER_CFLUSH		4
-#define RT_INSTR_EXIT_CFLUSH		5
-#define RT_INSTR_TOTAL_IDS		6
+#include <utils_def.h>
+
+#define RT_INSTR_ENTER_PSCI		U(0)
+#define RT_INSTR_EXIT_PSCI		U(1)
+#define RT_INSTR_ENTER_HW_LOW_PWR	U(2)
+#define RT_INSTR_EXIT_HW_LOW_PWR	U(3)
+#define RT_INSTR_ENTER_CFLUSH		U(4)
+#define RT_INSTR_EXIT_CFLUSH		U(5)
+#define RT_INSTR_TOTAL_IDS		U(6)
 
 #ifndef __ASSEMBLY__
 PMF_DECLARE_CAPTURE_TIMESTAMP(rt_instr_svc)
diff --git a/include/plat/arm/board/common/v2m_def.h b/include/plat/arm/board/common/v2m_def.h
index ce436d2..02c3494 100644
--- a/include/plat/arm/board/common/v2m_def.h
+++ b/include/plat/arm/board/common/v2m_def.h
@@ -1,36 +1,36 @@
 /*
- * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved.
+ * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
-#ifndef __V2M_DEF_H__
-#define __V2M_DEF_H__
+#ifndef V2M_DEF_H
+#define V2M_DEF_H
 
 #include <arm_xlat_tables.h>
 
 
 /* V2M motherboard system registers & offsets */
-#define V2M_SYSREGS_BASE		0x1c010000
-#define V2M_SYS_ID			0x0
-#define V2M_SYS_SWITCH			0x4
-#define V2M_SYS_LED			0x8
-#define V2M_SYS_NVFLAGS			0x38
-#define V2M_SYS_NVFLAGSSET		0x38
-#define V2M_SYS_NVFLAGSCLR		0x3c
-#define V2M_SYS_CFGDATA			0xa0
-#define V2M_SYS_CFGCTRL			0xa4
-#define V2M_SYS_CFGSTATUS		0xa8
+#define V2M_SYSREGS_BASE		UL(0x1c010000)
+#define V2M_SYS_ID			UL(0x0)
+#define V2M_SYS_SWITCH			UL(0x4)
+#define V2M_SYS_LED			UL(0x8)
+#define V2M_SYS_NVFLAGS			UL(0x38)
+#define V2M_SYS_NVFLAGSSET		UL(0x38)
+#define V2M_SYS_NVFLAGSCLR		UL(0x3c)
+#define V2M_SYS_CFGDATA			UL(0xa0)
+#define V2M_SYS_CFGCTRL			UL(0xa4)
+#define V2M_SYS_CFGSTATUS		UL(0xa8)
 
-#define V2M_CFGCTRL_START		(1 << 31)
-#define V2M_CFGCTRL_RW			(1 << 30)
+#define V2M_CFGCTRL_START		BIT_32(31)
+#define V2M_CFGCTRL_RW			BIT_32(30)
 #define V2M_CFGCTRL_FUNC_SHIFT		20
-#define V2M_CFGCTRL_FUNC(fn)		(fn << V2M_CFGCTRL_FUNC_SHIFT)
-#define V2M_FUNC_CLK_GEN		0x01
-#define V2M_FUNC_TEMP			0x04
-#define V2M_FUNC_DB_RESET		0x05
-#define V2M_FUNC_SCC_CFG		0x06
-#define V2M_FUNC_SHUTDOWN		0x08
-#define V2M_FUNC_REBOOT			0x09
+#define V2M_CFGCTRL_FUNC(fn)		((fn) << V2M_CFGCTRL_FUNC_SHIFT)
+#define V2M_FUNC_CLK_GEN		U(0x01)
+#define V2M_FUNC_TEMP			U(0x04)
+#define V2M_FUNC_DB_RESET		U(0x05)
+#define V2M_FUNC_SCC_CFG		U(0x06)
+#define V2M_FUNC_SHUTDOWN		U(0x08)
+#define V2M_FUNC_REBOOT			U(0x09)
 
 /* NVFLAGS in the V2M motherboard which is preserved after a watchdog reset */
  #define V2M_SYS_NVFLAGS_ADDR		(V2M_SYSREGS_BASE + V2M_SYS_NVFLAGS)
@@ -131,4 +131,4 @@
 						MT_DEVICE | MT_RW | MT_SECURE | MT_USER)
 
 
-#endif /* __V2M_DEF_H__ */
+#endif /* V2M_DEF_H */
diff --git a/include/plat/common/common_def.h b/include/plat/common/common_def.h
index 64807fd..5d76d6e 100644
--- a/include/plat/common/common_def.h
+++ b/include/plat/common/common_def.h
@@ -8,6 +8,7 @@
 
 #include <bl_common.h>
 #include <platform_def.h>
+#include <utils_def.h>
 #include <xlat_tables_defs.h>
 
 /******************************************************************************
@@ -73,12 +74,12 @@
 #define BL2_RO_DATA_END		round_up(BL2_ROM_END, PAGE_SIZE)
 #endif /* BL2_IN_XIP_MEM */
 #else
-#define BL_RO_DATA_BASE		0
-#define BL_RO_DATA_END		0
+#define BL_RO_DATA_BASE		UL(0)
+#define BL_RO_DATA_END		UL(0)
 #define BL1_CODE_END		round_up(BL1_ROM_END, PAGE_SIZE)
 #if BL2_IN_XIP_MEM
-#define BL2_RO_DATA_BASE	0
-#define BL2_RO_DATA_END		0
+#define BL2_RO_DATA_BASE	UL(0)
+#define BL2_RO_DATA_END		UL(0)
 #define BL2_CODE_END		round_up(BL2_ROM_END, PAGE_SIZE)
 #endif /* BL2_IN_XIP_MEM */
 #endif /* SEPARATE_CODE_AND_RODATA */
diff --git a/lib/xlat_tables/xlat_tables_common.c b/lib/xlat_tables/xlat_tables_common.c
index a9aaeee..2ee77c7 100644
--- a/lib/xlat_tables/xlat_tables_common.c
+++ b/lib/xlat_tables/xlat_tables_common.c
@@ -176,7 +176,7 @@
 {
 	const mmap_region_t *mm_cursor = mm;
 
-	while (mm_cursor->attr != 0U) {
+	while ((mm_cursor->size != 0U) || (mm_cursor->attr != 0U)) {
 		mmap_add_region(mm_cursor->base_pa, mm_cursor->base_va,
 				mm_cursor->size, mm_cursor->attr);
 		mm_cursor++;
diff --git a/lib/xlat_tables_v2/xlat_tables_core.c b/lib/xlat_tables_v2/xlat_tables_core.c
index 3b6c6bc..8ced76e 100644
--- a/lib/xlat_tables_v2/xlat_tables_core.c
+++ b/lib/xlat_tables_v2/xlat_tables_core.c
@@ -815,7 +815,7 @@
 {
 	const mmap_region_t *mm_cursor = mm;
 
-	while (mm_cursor->attr != 0U) {
+	while (mm_cursor->granularity != 0U) {
 		mmap_add_region_ctx(ctx, mm_cursor);
 		mm_cursor++;
 	}
diff --git a/make_helpers/build_macros.mk b/make_helpers/build_macros.mk
index 520725b..28ce2a4 100644
--- a/make_helpers/build_macros.mk
+++ b/make_helpers/build_macros.mk
@@ -451,17 +451,24 @@
 #   $(2) = input dts
 define MAKE_DTB
 
+# List of DTB file(s) to generate, based on DTS file basename list
 $(eval DOBJ := $(addprefix $(1)/,$(call SOURCES_TO_DTBS,$(2))))
+# List of the pre-compiled DTS file(s)
 $(eval DPRE := $(addprefix $(1)/,$(patsubst %.dts,%.pre.dts,$(notdir $(2)))))
-$(eval DEP := $(patsubst %.dtb,%.d,$(DOBJ)))
+# Dependencies of the pre-compiled DTS file(s) on its source and included files
+$(eval DTSDEP := $(patsubst %.dtb,%.o.d,$(DOBJ)))
+# Dependencies of the DT compilation on its pre-compiled DTS
+$(eval DTBDEP := $(patsubst %.dtb,%.d,$(DOBJ)))
 
 $(DOBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | fdt_dirs
 	@echo "  CPP     $$<"
-	$$(Q)$$(CPP) $$(CPPFLAGS) -x assembler-with-cpp -o $(DPRE) $$<
+	$(eval DTBS       := $(addprefix $(1)/,$(call SOURCES_TO_DTBS,$(2))))
+	$$(Q)$$(CPP) $$(CPPFLAGS) -x assembler-with-cpp -MT $(DTBS) -MMD -MF $(DTSDEP) -o $(DPRE) $$<
 	@echo "  DTC     $$<"
-	$$(Q)$$(DTC) $$(DTC_FLAGS) -i fdts -d $(DEP) -o $$@ $(DPRE)
+	$$(Q)$$(DTC) $$(DTC_FLAGS) -i fdts -d $(DTBDEP) -o $$@ $(DPRE)
 
--include $(DEP)
+-include $(DTBDEP)
+-include $(DTSDEP)
 
 endef
 
diff --git a/plat/arm/board/fvp/drivers/pwrc/fvp_pwrc.h b/plat/arm/board/fvp/drivers/pwrc/fvp_pwrc.h
index 9ad32d7..324f3e2 100644
--- a/plat/arm/board/fvp/drivers/pwrc/fvp_pwrc.h
+++ b/plat/arm/board/fvp/drivers/pwrc/fvp_pwrc.h
@@ -4,36 +4,36 @@
  * SPDX-License-Identifier: BSD-3-Clause
  */
 
-#ifndef __FVP_PWRC_H__
-#define __FVP_PWRC_H__
+#ifndef FVP_PWRC_H
+#define FVP_PWRC_H
 
 /* FVP Power controller register offset etc */
-#define PPOFFR_OFF		0x0
-#define PPONR_OFF		0x4
-#define PCOFFR_OFF		0x8
-#define PWKUPR_OFF		0xc
-#define PSYSR_OFF		0x10
+#define PPOFFR_OFF		U(0x0)
+#define PPONR_OFF		U(0x4)
+#define PCOFFR_OFF		U(0x8)
+#define PWKUPR_OFF		U(0xc)
+#define PSYSR_OFF		U(0x10)
 
-#define PWKUPR_WEN		(1ULL << 31)
+#define PWKUPR_WEN		BIT_32(31)
 
-#define PSYSR_AFF_L2		(1 << 31)
-#define PSYSR_AFF_L1		(1 << 30)
-#define PSYSR_AFF_L0		(1 << 29)
-#define PSYSR_WEN		(1 << 28)
-#define PSYSR_PC		(1 << 27)
-#define PSYSR_PP		(1 << 26)
+#define PSYSR_AFF_L2		BIT_32(31)
+#define PSYSR_AFF_L1		BIT_32(30)
+#define PSYSR_AFF_L0		BIT_32(29)
+#define PSYSR_WEN		BIT_32(28)
+#define PSYSR_PC		BIT_32(27)
+#define PSYSR_PP		BIT_32(26)
 
 #define PSYSR_WK_SHIFT		24
 #define PSYSR_WK_WIDTH		0x2
-#define PSYSR_WK_MASK		((1 << PSYSR_WK_WIDTH) - 1)
-#define PSYSR_WK(x)		(x >> PSYSR_WK_SHIFT) & PSYSR_WK_MASK
+#define PSYSR_WK_MASK		((1U << PSYSR_WK_WIDTH) - 1U)
+#define PSYSR_WK(x)		((x) >> PSYSR_WK_SHIFT) & PSYSR_WK_MASK
 
-#define WKUP_COLD		0x0
-#define WKUP_RESET		0x1
-#define WKUP_PPONR		0x2
-#define WKUP_GICREQ		0x3
+#define WKUP_COLD		U(0x0)
+#define WKUP_RESET		U(0x1)
+#define WKUP_PPONR		U(0x2)
+#define WKUP_GICREQ		U(0x3)
 
-#define PSYSR_INVALID		0xffffffff
+#define PSYSR_INVALID		U(0xffffffff)
 
 #ifndef __ASSEMBLY__
 
@@ -50,4 +50,4 @@
 
 #endif /*__ASSEMBLY__*/
 
-#endif /* __FVP_PWRC_H__ */
+#endif /* FVP_PWRC_H */
diff --git a/plat/arm/board/fvp/fvp_def.h b/plat/arm/board/fvp/fvp_def.h
index 4e20c31..eb6f77f 100644
--- a/plat/arm/board/fvp/fvp_def.h
+++ b/plat/arm/board/fvp/fvp_def.h
@@ -97,7 +97,7 @@
 #define ARCH_MODEL			0x1
 
 /* FVP Power controller base address*/
-#define PWRC_BASE			0x1c100000
+#define PWRC_BASE			UL(0x1c100000)
 
 /* FVP SP804 timer frequency is 35 MHz*/
 #define SP804_TIMER_CLKMULT		1
diff --git a/plat/arm/board/fvp/fvp_pm.c b/plat/arm/board/fvp/fvp_pm.c
index 065ecc1..7b85043 100644
--- a/plat/arm/board/fvp/fvp_pm.c
+++ b/plat/arm/board/fvp/fvp_pm.c
@@ -153,7 +153,7 @@
 	 */
 	do {
 		psysr = fvp_pwrc_read_psysr(mpidr);
-	} while (psysr & PSYSR_AFF_L0);
+	} while ((psysr & PSYSR_AFF_L0) != 0U);
 
 	fvp_pwrc_write_pponr(mpidr);
 	return rc;
@@ -312,7 +312,7 @@
 	 * The format of 'power_level' is implementation-defined, but 0 must
 	 * mean a CPU. We also allow 1 to denote the cluster
 	 */
-	if (power_level != ARM_PWR_LVL0 && power_level != ARM_PWR_LVL1)
+	if ((power_level != ARM_PWR_LVL0) && (power_level != ARM_PWR_LVL1))
 		return PSCI_E_INVALID_PARAMS;
 
 	/*
@@ -325,10 +325,10 @@
 		return PSCI_E_INVALID_PARAMS;
 
 	if (power_level == ARM_PWR_LVL0) {
-		ret = (psysr & PSYSR_AFF_L0) ? HW_ON : HW_OFF;
+		ret = ((psysr & PSYSR_AFF_L0) != 0U) ? HW_ON : HW_OFF;
 	} else {
 		/* power_level == ARM_PWR_LVL1 */
-		ret = (psysr & PSYSR_AFF_L1) ? HW_ON : HW_OFF;
+		ret = ((psysr & PSYSR_AFF_L1) != 0U) ? HW_ON : HW_OFF;
 	}
 
 	return ret;
diff --git a/plat/arm/common/aarch32/arm_helpers.S b/plat/arm/common/aarch32/arm_helpers.S
index c4cfa8a..badddd3 100644
--- a/plat/arm/common/aarch32/arm_helpers.S
+++ b/plat/arm/common/aarch32/arm_helpers.S
@@ -48,7 +48,7 @@
 	ldr	r0, =PLAT_ARM_CRASH_UART_BASE
 	ldr	r1, =PLAT_ARM_CRASH_UART_CLK_IN_HZ
 	ldr	r2, =ARM_CONSOLE_BAUDRATE
-	b	console_core_init
+	b	console_pl011_core_init
 endfunc plat_crash_console_init
 
 	/* ---------------------------------------------
@@ -60,7 +60,7 @@
 	 */
 func plat_crash_console_putc
 	ldr	r1, =PLAT_ARM_CRASH_UART_BASE
-	b	console_core_putc
+	b	console_pl011_core_putc
 endfunc plat_crash_console_putc
 
 	/* ---------------------------------------------
@@ -73,5 +73,5 @@
 	 */
 func plat_crash_console_flush
 	ldr	r0, =PLAT_ARM_CRASH_UART_BASE
-	b	console_core_flush
+	b	console_pl011_core_flush
 endfunc plat_crash_console_flush
diff --git a/plat/arm/common/arm_common.mk b/plat/arm/common/arm_common.mk
index 276f780..3fb1eff 100644
--- a/plat/arm/common/arm_common.mk
+++ b/plat/arm/common/arm_common.mk
@@ -123,9 +123,7 @@
 SEPARATE_CODE_AND_RODATA	:=	1
 
 # Use the multi console API, which is only available for AArch64 for now
-ifeq (${ARCH}, aarch64)
-  MULTI_CONSOLE_API		:=	1
-endif
+MULTI_CONSOLE_API		:=	1
 
 # Disable ARM Cryptocell by default
 ARM_CRYPTOCELL_INTEG		:=	0
diff --git a/plat/arm/common/arm_pm.c b/plat/arm/common/arm_pm.c
index 85efc7d..6394bfb 100644
--- a/plat/arm/common/arm_pm.c
+++ b/plat/arm/common/arm_pm.c
@@ -29,7 +29,7 @@
 	unsigned int pwr_lvl = psci_get_pstate_pwrlvl(power_state);
 	unsigned int i;
 
-	assert(req_state > 0U);
+	assert(req_state != NULL);
 
 	if (pwr_lvl > PLAT_MAX_PWR_LVL)
 		return PSCI_E_INVALID_PARAMS;
@@ -72,7 +72,7 @@
 	unsigned int state_id;
 	int i;
 
-	assert(req_state);
+	assert(req_state != NULL);
 
 	/*
 	 *  Currently we are using a linear search for finding the matching
@@ -128,7 +128,7 @@
 
 int arm_validate_psci_entrypoint(uintptr_t entrypoint)
 {
-	return arm_validate_ns_entrypoint(entrypoint) == 0 ? PSCI_E_SUCCESS :
+	return (arm_validate_ns_entrypoint(entrypoint) == 0) ? PSCI_E_SUCCESS :
 		PSCI_E_INVALID_ADDRESS;
 }
 
diff --git a/plat/arm/common/sp_min/arm_sp_min_setup.c b/plat/arm/common/sp_min/arm_sp_min_setup.c
index 10c1914..b8234c1 100644
--- a/plat/arm/common/sp_min/arm_sp_min_setup.c
+++ b/plat/arm/common/sp_min/arm_sp_min_setup.c
@@ -9,6 +9,7 @@
 #include <console.h>
 #include <debug.h>
 #include <mmio.h>
+#include <pl011.h>
 #include <plat_arm.h>
 #include <platform.h>
 #include <platform_def.h>
@@ -61,8 +62,7 @@
 			uintptr_t hw_config, void *plat_params_from_bl2)
 {
 	/* Initialize the console to provide early debug support */
-	console_init(PLAT_ARM_BOOT_UART_BASE, PLAT_ARM_BOOT_UART_CLK_IN_HZ,
-				ARM_CONSOLE_BAUDRATE);
+	arm_console_boot_init();
 
 #if RESET_TO_SP_MIN
 	/* There are no parameters from BL2 if SP_MIN is a reset vector */
@@ -152,8 +152,7 @@
 void arm_sp_min_plat_runtime_setup(void)
 {
 	/* Initialize the runtime console */
-	console_init(PLAT_ARM_SP_MIN_RUN_UART_BASE,
-		PLAT_ARM_SP_MIN_RUN_UART_CLK_IN_HZ, ARM_CONSOLE_BAUDRATE);
+	arm_console_runtime_init();
 }
 
 /*******************************************************************************
diff --git a/plat/arm/css/sgm/sgm_bl1_setup.c b/plat/arm/css/sgm/sgm_bl1_setup.c
index 51e3e53..dc3d71d 100644
--- a/plat/arm/css/sgm/sgm_bl1_setup.c
+++ b/plat/arm/css/sgm/sgm_bl1_setup.c
@@ -12,11 +12,13 @@
 
 void bl1_early_platform_setup(void)
 {
+
+	/* Initialize the console before anything else */
+	arm_bl1_early_platform_setup();
+
 	/* Initialize the platform configuration structure */
 	plat_config_init();
 
-	arm_bl1_early_platform_setup();
-
 #if !HW_ASSISTED_COHERENCY
 	/*
 	 * Initialize Interconnect for this cluster during cold boot.
diff --git a/plat/common/aarch32/plat_sp_min_common.c b/plat/common/aarch32/plat_sp_min_common.c
index a9a92c7..f1b1e9c 100644
--- a/plat/common/aarch32/plat_sp_min_common.c
+++ b/plat/common/aarch32/plat_sp_min_common.c
@@ -21,5 +21,9 @@
 	 * Finish the use of console driver in SP_MIN so that any runtime logs
 	 * from SP_MIN will be suppressed.
 	 */
+#if MULTI_CONSOLE_API
+	console_switch_state(CONSOLE_FLAG_RUNTIME);
+#else
 	console_uninit();
+#endif
 }
diff --git a/tools/doimage/doimage.c b/tools/doimage/doimage.c
index 6fc23d5..82fd375 100644
--- a/tools/doimage/doimage.c
+++ b/tools/doimage/doimage.c
@@ -216,7 +216,7 @@
 }
 
 /* globals */
-options_t opts = {
+static options_t opts = {
 	.bin_ext_file = "NA",
 	.sec_cfg_file = "NA",
 	.sec_opts = 0,
@@ -1578,9 +1578,9 @@
 
 int main(int argc, char *argv[])
 {
-	char in_file[MAX_FILENAME+1];
-	char out_file[MAX_FILENAME+1];
-	char ext_file[MAX_FILENAME+1];
+	char in_file[MAX_FILENAME+1] = { 0 };
+	char out_file[MAX_FILENAME+1] = { 0 };
+	char ext_file[MAX_FILENAME+1] = { 0 };
 	FILE *in_fd = NULL;
 	FILE *out_fd = NULL;
 	int parse = 0;
@@ -1590,6 +1590,7 @@
 	int image_size;
 	uint8_t *image_buf = NULL;
 	int read;
+	size_t len;
 	uint32_t nand_block_size_kb, mlc_nand;
 
 	/* Create temporary file for building extensions
@@ -1660,13 +1661,19 @@
 	if (optind >= argc)
 		usage_err("missing input file name");
 
-	strncpy(in_file, argv[optind], MAX_FILENAME);
+	len = strlen(argv[optind]);
+	if (len > MAX_FILENAME)
+		usage_err("file name too long");
+	memcpy(in_file, argv[optind], len);
 	optind++;
 
 	/* Output file must exist in non parse mode */
-	if (optind < argc)
-		strncpy(out_file, argv[optind], MAX_FILENAME);
-	else if (!parse)
+	if (optind < argc) {
+		len = strlen(argv[optind]);
+		if (len > MAX_FILENAME)
+			usage_err("file name too long");
+		memcpy(out_file, argv[optind], len);
+	} else if (!parse)
 		usage_err("missing output file name");
 
 	/* open the input file */