fix: fix cppcheck misra errors

Fix remaining misra errors and now the error
count is 0 for CPPCheck version 2.13.4.

Change-Id: I903b198b137672b4c26457bbafcab05185dce608
Signed-off-by: Shruti Gupta <shruti.gupta@arm.com>
diff --git a/drivers/pl011/src/pl011.c b/drivers/pl011/src/pl011.c
index 31c71af..e7137bd 100644
--- a/drivers/pl011/src/pl011.c
+++ b/drivers/pl011/src/pl011.c
@@ -55,7 +55,7 @@
 
 /* Serial output - called from console driver */
 /* coverity[misra_c_2012_rule_8_7_violation:SUPPRESS] */
-static int pl011_putc(int c, struct console *csl)
+static int pl011_putc(int c, const struct console *csl)
 {
 	assert(csl != NULL);
 
diff --git a/lib/allocator/include/memory_alloc.h b/lib/allocator/include/memory_alloc.h
index 839338e..2695313 100644
--- a/lib/allocator/include/memory_alloc.h
+++ b/lib/allocator/include/memory_alloc.h
@@ -70,7 +70,7 @@
 	size_t			len;
 	memory_header_t		*first;
 	memory_header_t		*first_free;
-	int			verify;
+	unsigned int		verify;
 };
 
 struct memory_header_s {
diff --git a/lib/allocator/src/memory_alloc.c b/lib/allocator/src/memory_alloc.c
index 9a0a9f1..e5be4ef 100644
--- a/lib/allocator/src/memory_alloc.c
+++ b/lib/allocator/src/memory_alloc.c
@@ -141,7 +141,7 @@
 {
 	struct memory_header_s *new;
 	struct memory_header_s *cur = heap->first_free;
-	unsigned char *p;
+	uintptr_t p;
 	void *ret;
 	size_t original_len;
 	size_t len;
@@ -159,9 +159,9 @@
 		return NULL;
 	}
 
-	if ((len % MBEDTLS_MEMORY_ALIGN_MULTIPLE) != 0) {
-		len -= len % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
-		len += MBEDTLS_MEMORY_ALIGN_MULTIPLE;
+	if ((len % U(MBEDTLS_MEMORY_ALIGN_MULTIPLE)) != 0U) {
+		len -= len % U(MBEDTLS_MEMORY_ALIGN_MULTIPLE);
+		len += U(MBEDTLS_MEMORY_ALIGN_MULTIPLE);
 	}
 
 	/* Find block that fits */
@@ -182,7 +182,7 @@
 
 	/* Found location, split block if > memory_header + 4 room left */
 	if ((cur->size - len) <
-	    (sizeof(struct memory_header_s) + MBEDTLS_MEMORY_ALIGN_MULTIPLE)) {
+	   (sizeof(struct memory_header_s) + U(MBEDTLS_MEMORY_ALIGN_MULTIPLE))) {
 		cur->alloc = 1UL;
 
 		/* Remove from free_list */
@@ -199,17 +199,18 @@
 		cur->prev_free = NULL;
 		cur->next_free = NULL;
 
-		if ((heap->verify & MBEDTLS_MEMORY_VERIFY_ALLOC) != 0) {
+		/* cppcheck-suppress misra-c2012-10.1 */
+		if ((heap->verify & U(MBEDTLS_MEMORY_VERIFY_ALLOC)) != 0U) {
 			assert(verify_chain(heap) == 0);
 		}
 
-		ret = (unsigned char *) cur + sizeof(struct memory_header_s);
+		ret = (void *)((uintptr_t)cur + sizeof(struct memory_header_s));
 		(void)memset(ret, 0, original_len);
 
 		return ret;
 	}
 
-	p = ((unsigned char *) cur) + sizeof(struct memory_header_s) + len;
+	p = ((uintptr_t)cur) + sizeof(struct memory_header_s) + len;
 	new = (struct memory_header_s *) p;
 
 	new->size = cur->size - len - sizeof(struct memory_header_s);
@@ -242,11 +243,12 @@
 	cur->prev_free = NULL;
 	cur->next_free = NULL;
 
-	if ((heap->verify & MBEDTLS_MEMORY_VERIFY_ALLOC) != 0) {
+	/* cppcheck-suppress misra-c2012-10.1 */
+	if ((heap->verify & U(MBEDTLS_MEMORY_VERIFY_ALLOC)) != 0U) {
 		assert(verify_chain(heap) == 0);
 	}
 
-	ret = (unsigned char *) cur + sizeof(struct memory_header_s);
+	ret = (void *)((uintptr_t)cur + sizeof(struct memory_header_s));
 	(void)memset(ret, 0, original_len);
 
 	return ret;
@@ -265,13 +267,13 @@
 {
 	struct memory_header_s *hdr;
 	struct memory_header_s *old = NULL;
-	unsigned char *p = (unsigned char *) ptr;
+	uintptr_t p = (uintptr_t)ptr;
 
 	if ((ptr == NULL) || (heap->buf == NULL) || (heap->first == NULL)) {
 		return;
 	}
 
-	if ((p < heap->buf) || (p >= (heap->buf + heap->len))) {
+	if ((p < (uintptr_t)heap->buf) || (p >= ((uintptr_t)heap->buf + heap->len))) {
 		assert(0);
 	}
 
@@ -280,7 +282,7 @@
 
 	assert(verify_header(hdr) == 0);
 
-	if (hdr->alloc != 1) {
+	if (hdr->alloc != 1U) {
 		assert(0);
 	}
 
@@ -349,7 +351,8 @@
 		heap->first_free = hdr;
 	}
 
-	if ((heap->verify & MBEDTLS_MEMORY_VERIFY_FREE) != 0) {
+	/* cppcheck-suppress misra-c2012-10.1 */
+	if ((heap->verify & U(MBEDTLS_MEMORY_VERIFY_FREE)) != 0U) {
 		assert(verify_chain(heap));
 	}
 }
@@ -413,6 +416,8 @@
 
 void mbedtls_memory_buffer_alloc_init(unsigned char *buf, size_t len)
 {
+	uintptr_t p = (uintptr_t)buf;
+
 	/* The heap structure is obtained from the REC
 	 * while the buffer is passed in the init function.
 	 * This way the interface can remain the same.
@@ -424,14 +429,15 @@
 	(void)memset(heap, 0, sizeof(struct buffer_alloc_ctx));
 
 	if (len < sizeof(struct memory_header_s) +
-	    MBEDTLS_MEMORY_ALIGN_MULTIPLE) {
+	    U(MBEDTLS_MEMORY_ALIGN_MULTIPLE)) {
 		return;
-	} else if (((size_t)buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE) != 0U) {
+	} else if (((size_t)buf % U(MBEDTLS_MEMORY_ALIGN_MULTIPLE)) != 0U) {
 		/* Adjust len first since buf is used in the computation */
-		len -= MBEDTLS_MEMORY_ALIGN_MULTIPLE
-			- ((size_t)buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE);
-		buf += MBEDTLS_MEMORY_ALIGN_MULTIPLE
-			- ((size_t)buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE);
+		len -= U(MBEDTLS_MEMORY_ALIGN_MULTIPLE)
+			- ((size_t)buf % U(MBEDTLS_MEMORY_ALIGN_MULTIPLE));
+		p += U(MBEDTLS_MEMORY_ALIGN_MULTIPLE)
+			- ((size_t)buf % U(MBEDTLS_MEMORY_ALIGN_MULTIPLE));
+		buf = (unsigned char *)p;
 	}
 
 	(void)memset(buf, 0, len);
diff --git a/lib/console/include/console.h b/lib/console/include/console.h
index 92fb35c..435be9f 100644
--- a/lib/console/include/console.h
+++ b/lib/console/include/console.h
@@ -20,8 +20,8 @@
 struct console {
 	struct console *next;
 	uint64_t flags;
-	int (*const putc)(int character, struct console *console);
-	void (*const flush)(struct console *console);
+	int (*const putc)(int character, const struct console *console);
+	void (*const flush)(const struct console *console);
 	uintptr_t base;
 
 	/* Additional private driver data may follow here. */
diff --git a/lib/gic/src/gic.c b/lib/gic/src/gic.c
index 42c8e6f..5403778 100644
--- a/lib/gic/src/gic.c
+++ b/lib/gic/src/gic.c
@@ -99,7 +99,7 @@
 	 */
 	gic_virt_feature.max_vintid =
 				(EXTRACT(ICH_VTR_EL2_ID_BITS, vtr) == 0UL) ?
-				((1UL << 16U) - 1UL) : ((1UL << 24U) - 1UL);
+				((UL(1) << 16U) - 1UL) : ((UL(1) << 24U) - 1UL);
 
 	/* Number of virtual priority bits implemented */
 	nr_pri_bits = EXTRACT(ICH_VTR_EL2_PRI_BITS, vtr) + 1UL;
diff --git a/lib/realm/include/rec.h b/lib/realm/include/rec.h
index d65b96f..477daa8 100644
--- a/lib/realm/include/rec.h
+++ b/lib/realm/include/rec.h
@@ -22,7 +22,7 @@
 #include <utils_def.h>
 
 #ifndef CBMC
-#define RMM_REC_SAVED_GEN_REG_COUNT	31
+#define RMM_REC_SAVED_GEN_REG_COUNT	U(31)
 #define STRUCT_TYPE	                struct
 #else /* CBMC */
 #define RMM_REC_SAVED_GEN_REG_COUNT	SMC_RESULT_REGS
diff --git a/lib/s2tt/include/s2tt.h b/lib/s2tt/include/s2tt.h
index 27d47e7..cc7d0fd 100644
--- a/lib/s2tt/include/s2tt.h
+++ b/lib/s2tt/include/s2tt.h
@@ -38,11 +38,11 @@
 	 */
 };
 
-#define S2TT_MIN_IPA_BITS		32U
-#define S2TT_MAX_IPA_BITS		48U
+#define S2TT_MIN_IPA_BITS		U(32)
+#define S2TT_MAX_IPA_BITS		U(48)
 
-#define S2TT_MAX_IPA_BITS_LPA2		52U
-#define S2TT_MAX_IPA_SIZE_LPA2		(1UL << S2TT_MAX_IPA_BITS_LPA2)
+#define S2TT_MAX_IPA_BITS_LPA2		U(52)
+#define S2TT_MAX_IPA_SIZE_LPA2		(UL(1) << S2TT_MAX_IPA_BITS_LPA2)
 
 #define S2TT_MIN_STARTING_LEVEL		(0)
 #define S2TT_MIN_STARTING_LEVEL_LPA2	(-1)
@@ -53,7 +53,7 @@
  * S2TTE_STRIDE: The number of bits resolved in a single level of translation
  * walk (except for the starting level which may resolve more or fewer bits).
  */
-#define S2TTE_STRIDE		(GRANULE_SHIFT - 3U)
+#define S2TTE_STRIDE		(U(GRANULE_SHIFT) - 3U)
 #define S2TTES_PER_S2TT		(1UL << S2TTE_STRIDE)
 
 /*
diff --git a/lib/s2tt/src/s2tt.c b/lib/s2tt/src/s2tt.c
index 3ba0e13..b603640 100644
--- a/lib/s2tt/src/s2tt.c
+++ b/lib/s2tt/src/s2tt.c
@@ -598,11 +598,13 @@
 unsigned long s2tte_create_table(const struct s2tt_context *s2_ctx,
 				 unsigned long pa, long level)
 {
-	__unused int min_starting_level;
+	__unused long min_starting_level;
 
 	(void)level;
 
 	assert(s2_ctx != NULL);
+
+	/* cppcheck-suppress misra-c2012-10.6 */
 	min_starting_level = (s2_ctx->enable_lpa2 == true) ?
 			S2TT_MIN_STARTING_LEVEL_LPA2 : S2TT_MIN_STARTING_LEVEL;
 
@@ -1033,6 +1035,7 @@
 
 	assert(s2_ctx != NULL);
 
+	/* cppcheck-suppress misra-c2012-10.6 */
 	min_starting_level = (s2_ctx->enable_lpa2 == true) ?
 		S2TT_MIN_STARTING_LEVEL_LPA2 : S2TT_MIN_STARTING_LEVEL;
 	assert(level >= min_starting_level);
@@ -1055,6 +1058,7 @@
 {
 	assert(s2_ctx != NULL);
 
+	/* cppcheck-suppress misra-c2012-10.6 */
 	__unused long min_starting_level = (s2_ctx->enable_lpa2 == true) ?
 		S2TT_MIN_STARTING_LEVEL_LPA2 : S2TT_MIN_STARTING_LEVEL;
 	unsigned long levels = (unsigned long)(S2TT_PAGE_LEVEL - level);
@@ -1273,6 +1277,8 @@
 	unsigned long i, index = wi->index;
 	long level = wi->last_level;
 	unsigned long map_size;
+
+	/* cppcheck-suppress misra-c2012-10.6 */
 	__unused long min_starting_level = (s2_ctx->enable_lpa2 == true) ?
 			S2TT_MIN_STARTING_LEVEL_LPA2 : S2TT_MIN_STARTING_LEVEL;
 
diff --git a/lib/xlat/src/xlat_contexts.c b/lib/xlat/src/xlat_contexts.c
index 504e448..72eb985 100644
--- a/lib/xlat/src/xlat_contexts.c
+++ b/lib/xlat/src/xlat_contexts.c
@@ -258,6 +258,8 @@
 	}
 
 	cfg->max_va_size = va_size;
+
+	/* cppcheck-suppress misra-c2012-10.6 */
 	cfg->base_level = (GET_XLAT_TABLE_LEVEL_BASE(va_size));
 	cfg->region = region;
 	cfg->initialized = true;
diff --git a/lib/xlat/src/xlat_tables_core.c b/lib/xlat/src/xlat_tables_core.c
index bb48500..1e01847 100644
--- a/lib/xlat/src/xlat_tables_core.c
+++ b/lib/xlat/src/xlat_tables_core.c
@@ -309,7 +309,7 @@
 				TABLE_DESC | (uintptr_t)(void *)subtable;
 
 			/* Recurse to write into subtable */
-			/* FIXME: This violates misra-c2012-17.2 */
+			/* cppcheck-suppress misra-c2012-17.2 */
 			end_va = xlat_tables_map_region(ctx, mm, table_idx_va,
 					       subtable, XLAT_TABLE_ENTRIES,
 					       level + 1);
@@ -322,7 +322,7 @@
 
 			subtable = (uint64_t *)(void *)xlat_get_oa_from_tte(desc);
 			/* Recurse to write into subtable */
-			/* FIXME: This violates misra-c2012-17.2 */
+			/* cppcheck-suppress misra-c2012-17.2 */
 			end_va = xlat_tables_map_region(ctx, mm, table_idx_va,
 					       subtable, XLAT_TABLE_ENTRIES,
 					       level + 1);
diff --git a/plat/host/common/src/host_console.c b/plat/host/common/src/host_console.c
index 348d028..f9390d7 100644
--- a/plat/host/common/src/host_console.c
+++ b/plat/host/common/src/host_console.c
@@ -10,7 +10,7 @@
 #include <stdio.h>
 
 /* Serial output - called from console driver */
-static int host_csl_putc(int c, struct console *csl)
+static int host_csl_putc(int c, const struct console *csl)
 {
 	assert(csl != NULL);
 	return putchar(c);
diff --git a/runtime/core/handler.c b/runtime/core/handler.c
index 4f60568..203af89 100644
--- a/runtime/core/handler.c
+++ b/runtime/core/handler.c
@@ -229,6 +229,7 @@
 	}
 }
 
+/* cppcheck-suppress misra-c2012-8.4 */
 /* coverity[misra_c_2012_rule_8_4_violation:SUPPRESS] */
 /* coverity[misra_c_2012_rule_8_7_violation:SUPPRESS] */
 void handle_ns_smc(unsigned int function_id,
@@ -256,6 +257,7 @@
 
 	if (IS_SMC64_RMI_FID(function_id)) {
 		handler_id = RMI_HANDLER_ID(function_id);
+		/* cppcheck-suppress misra-c2012-17.3 */
 		if (handler_id < ARRAY_LEN(smc_handlers)) {
 			handler = &smc_handlers[handler_id];
 		}
@@ -446,7 +448,9 @@
  * continue from. Other register values are preserved.
  * If no match is found, it aborts the RMM.
  */
+/* cppcheck-suppress misra-c2012-8.4 */
 /* coverity[misra_c_2012_rule_8_4_violation:SUPPRESS] */
+/* coverity[misra_c_2012_rule_8_7_violation:SUPPRESS] */
 unsigned long handle_rmm_trap(dump_regs_t *regs)
 {
 	unsigned long esr = read_esr_el2();
diff --git a/runtime/core/sysregs.c b/runtime/core/sysregs.c
index 2821245..dfa615d 100644
--- a/runtime/core/sysregs.c
+++ b/runtime/core/sysregs.c
@@ -349,6 +349,7 @@
 	/* Check for 32-bit instruction trapped */
 	assert(ESR_IL(esr) != 0UL);
 
+	/* cppcheck-suppress misra-c2012-14.2 */
 	for (unsigned int i = 0U; i < ARRAY_LEN(sysreg_handlers); i++) {
 		const struct sysreg_handler *handler = &sysreg_handlers[i];
 
diff --git a/runtime/rmi/realm.c b/runtime/rmi/realm.c
index 3b6cc94..5019b20 100644
--- a/runtime/rmi/realm.c
+++ b/runtime/rmi/realm.c
@@ -113,6 +113,8 @@
 
 	max_ipa_bits = (lpa2 == true) ?
 				S2TT_MAX_IPA_BITS_LPA2 : S2TT_MAX_IPA_BITS;
+
+	/* cppcheck-suppress misra-c2012-10.6 */
 	min_starting_level = (lpa2 == true) ?
 				S2TT_MIN_STARTING_LEVEL_LPA2 : S2TT_MIN_STARTING_LEVEL;
 
@@ -193,7 +195,7 @@
 	 * the starting level.
 	 */
 	unsigned long sl_entry_map_size =
-			1UL << ((levels * S2TTE_STRIDE) + GRANULE_SHIFT);
+			(UL(1)) << U(U(levels * S2TTE_STRIDE) + U(GRANULE_SHIFT));
 
 	num_root_rtts = rd->s2_ctx.num_root_rtts;
 	for (unsigned int rtt = 0U; rtt < num_root_rtts; rtt++) {
diff --git a/runtime/rmi/run.c b/runtime/rmi/run.c
index eed04ef..2ba56a8 100644
--- a/runtime/rmi/run.c
+++ b/runtime/rmi/run.c
@@ -52,7 +52,7 @@
 
 		if (esr_sign_extend(esr)) {
 			unsigned int bit_count = access_len(esr) * 8U;
-			unsigned long mask = 1UL << (bit_count - 1U);
+			unsigned long mask = (UL(1)) << U(bit_count - 1U);
 
 			val = (val ^ mask) - mask;
 			if (!esr_sixty_four(esr)) {
diff --git a/runtime/rsi/logger.c b/runtime/rsi/logger.c
index f17f230..e0aeee1 100644
--- a/runtime/rsi/logger.c
+++ b/runtime/rsi/logger.c
@@ -131,6 +131,7 @@
 	return snprintf(buf, len, " > %lx", res);
 }
 
+/* cppcheck-suppress misra-c2012-8.4 */
 void rsi_log_on_exit(unsigned int function_id, unsigned long args[],
 		     unsigned long regs[])
 {
diff --git a/tools/cppcheck/CPPCheck.cmake b/tools/cppcheck/CPPCheck.cmake
index fc90ab8..688433c 100644
--- a/tools/cppcheck/CPPCheck.cmake
+++ b/tools/cppcheck/CPPCheck.cmake
@@ -31,7 +31,6 @@
 
 list(APPEND cppcheck-flags "--output-file=${CPPCHECK_OUTPUT}")
 list(APPEND cppcheck-flags "--cppcheck-build-dir=${CPPCHECK_BUILD_DIR}")
-list(APPEND cppcheck-flags "--checkers-report=${BUILD_DIR}/tools/cppcheck/checkers.log")
 
 #
 # Exclude files or directories we don't want to receive warnings about.
diff --git a/tools/cppcheck/suppressions.txt b/tools/cppcheck/suppressions.txt
index 3253dae..67ec17d 100644
--- a/tools/cppcheck/suppressions.txt
+++ b/tools/cppcheck/suppressions.txt
@@ -27,5 +27,8 @@
 constParameterPointer
 constParameter
 
+// Ignore cppcheck checkersReport error
+checkersReport
+
 // Ignore errors in ext folder
 *:*/ext/*