blob: f73842acd351e3eaee5101b479bad22dab1a7829 [file] [log] [blame]
Soby Mathewafe7e2f2014-06-12 17:23:58 +01001/*
Soby Mathewf62d5462016-03-22 17:38:00 +00002 * Copyright (c) 2014-2016, ARM Limited and Contributors. All rights reserved.
Soby Mathewafe7e2f2014-06-12 17:23:58 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Soby Mathewafe7e2f2014-06-12 17:23:58 +01005 */
Soby Mathew0691d972016-05-05 12:34:41 +01006#include <arch.h>
7#include <arch_helpers.h>
8#include <assert.h>
Soby Mathewafe7e2f2014-06-12 17:23:58 +01009#include <debug.h>
Soby Mathew0691d972016-05-05 12:34:41 +010010#include <limits.h>
Soby Mathewafe7e2f2014-06-12 17:23:58 +010011#include <stdarg.h>
12#include <stdint.h>
13
14/***********************************************************
15 * The tf_printf implementation for all BL stages
16 ***********************************************************/
Soby Mathewf62d5462016-03-22 17:38:00 +000017
18#define get_num_va_args(args, lcount) \
19 (((lcount) > 1) ? va_arg(args, long long int) : \
20 ((lcount) ? va_arg(args, long int) : va_arg(args, int)))
21
22#define get_unum_va_args(args, lcount) \
23 (((lcount) > 1) ? va_arg(args, unsigned long long int) : \
24 ((lcount) ? va_arg(args, unsigned long int) : va_arg(args, unsigned int)))
25
Soby Mathewf583a062017-09-04 11:45:52 +010026void tf_string_print(const char *str)
Soby Mathewf62d5462016-03-22 17:38:00 +000027{
Soby Mathewf583a062017-09-04 11:45:52 +010028 assert(str);
29
Soby Mathewf62d5462016-03-22 17:38:00 +000030 while (*str)
31 putchar(*str++);
32}
33
34static void unsigned_num_print(unsigned long long int unum, unsigned int radix)
Soby Mathewafe7e2f2014-06-12 17:23:58 +010035{
36 /* Just need enough space to store 64 bit decimal integer */
37 unsigned char num_buf[20];
Sandrine Bailleuxa64a8542015-03-05 10:54:34 +000038 int i = 0, rem;
Soby Mathewafe7e2f2014-06-12 17:23:58 +010039
40 do {
41 rem = unum % radix;
42 if (rem < 0xa)
43 num_buf[i++] = '0' + rem;
44 else
45 num_buf[i++] = 'a' + (rem - 0xa);
46 } while (unum /= radix);
47
48 while (--i >= 0)
49 putchar(num_buf[i]);
50}
51
Soby Mathewafe7e2f2014-06-12 17:23:58 +010052/*******************************************************************
53 * Reduced format print for Trusted firmware.
Soby Mathewf62d5462016-03-22 17:38:00 +000054 * The following type specifiers are supported by this print
55 * %x - hexadecimal format
Soby Mathewafe7e2f2014-06-12 17:23:58 +010056 * %s - string format
Soby Mathewf62d5462016-03-22 17:38:00 +000057 * %d or %i - signed decimal format
58 * %u - unsigned decimal format
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +000059 * %p - pointer format
Soby Mathewf62d5462016-03-22 17:38:00 +000060 *
61 * The following length specifiers are supported by this print
62 * %l - long int (64-bit on AArch64)
63 * %ll - long long int (64-bit on AArch64)
64 * %z - size_t sized integer formats (64 bit on AArch64)
65 *
66 * The print exits on all other formats specifiers other than valid
67 * combinations of the above specifiers.
Soby Mathewafe7e2f2014-06-12 17:23:58 +010068 *******************************************************************/
Soby Mathewf583a062017-09-04 11:45:52 +010069void tf_vprintf(const char *fmt, va_list args)
Soby Mathewafe7e2f2014-06-12 17:23:58 +010070{
Soby Mathewf62d5462016-03-22 17:38:00 +000071 int l_count;
72 long long int num;
73 unsigned long long int unum;
Soby Mathewafe7e2f2014-06-12 17:23:58 +010074 char *str;
75
Soby Mathewafe7e2f2014-06-12 17:23:58 +010076 while (*fmt) {
Soby Mathewf62d5462016-03-22 17:38:00 +000077 l_count = 0;
Soby Mathewafe7e2f2014-06-12 17:23:58 +010078
79 if (*fmt == '%') {
80 fmt++;
81 /* Check the format specifier */
82loop:
83 switch (*fmt) {
84 case 'i': /* Fall through to next one */
85 case 'd':
Soby Mathewf62d5462016-03-22 17:38:00 +000086 num = get_num_va_args(args, l_count);
Soby Mathewafe7e2f2014-06-12 17:23:58 +010087 if (num < 0) {
88 putchar('-');
Soby Mathewf62d5462016-03-22 17:38:00 +000089 unum = (unsigned long long int)-num;
Soby Mathewafe7e2f2014-06-12 17:23:58 +010090 } else
Soby Mathewf62d5462016-03-22 17:38:00 +000091 unum = (unsigned long long int)num;
Soby Mathewafe7e2f2014-06-12 17:23:58 +010092
93 unsigned_num_print(unum, 10);
94 break;
95 case 's':
96 str = va_arg(args, char *);
Soby Mathewf583a062017-09-04 11:45:52 +010097 tf_string_print(str);
Soby Mathewafe7e2f2014-06-12 17:23:58 +010098 break;
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +000099 case 'p':
Soby Mathewf62d5462016-03-22 17:38:00 +0000100 unum = (uintptr_t)va_arg(args, void *);
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +0000101 if (unum)
Soby Mathewf583a062017-09-04 11:45:52 +0100102 tf_string_print("0x");
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +0000103
104 unsigned_num_print(unum, 16);
105 break;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100106 case 'x':
Soby Mathewf62d5462016-03-22 17:38:00 +0000107 unum = get_unum_va_args(args, l_count);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100108 unsigned_num_print(unum, 16);
109 break;
Scott Branden57bd75c2016-03-23 13:37:33 -0700110 case 'z':
111 if (sizeof(size_t) == 8)
Soby Mathewf62d5462016-03-22 17:38:00 +0000112 l_count = 2;
113
Scott Branden57bd75c2016-03-23 13:37:33 -0700114 fmt++;
115 goto loop;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100116 case 'l':
Soby Mathewf62d5462016-03-22 17:38:00 +0000117 l_count++;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100118 fmt++;
119 goto loop;
120 case 'u':
Soby Mathewf62d5462016-03-22 17:38:00 +0000121 unum = get_unum_va_args(args, l_count);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100122 unsigned_num_print(unum, 10);
123 break;
124 default:
125 /* Exit on any other format specifier */
Soby Mathewf583a062017-09-04 11:45:52 +0100126 return;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100127 }
128 fmt++;
129 continue;
130 }
131 putchar(*fmt++);
132 }
Soby Mathewf583a062017-09-04 11:45:52 +0100133}
134
135void tf_printf(const char *fmt, ...)
136{
137 va_list va;
138
139 va_start(va, fmt);
140 tf_vprintf(fmt, va);
141 va_end(va);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100142}