blob: 45e153ec7ded8c730f90553161070b6ff5816f70 [file] [log] [blame]
Soby Mathewafe7e2f2014-06-12 17:23:58 +01001/*
Heyi Guoeb250ed2020-10-27 08:36:40 +08002 * Copyright (c) 2014-2021, 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 */
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00006
Soby Mathew0691d972016-05-05 12:34:41 +01007#include <assert.h>
Soby Mathewafe7e2f2014-06-12 17:23:58 +01008#include <stdarg.h>
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +01009#include <stdbool.h>
Soby Mathewafe7e2f2014-06-12 17:23:58 +010010#include <stdint.h>
11
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012#include <common/debug.h>
13
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010014#define get_num_va_args(_args, _lcount) \
15 (((_lcount) > 1) ? va_arg(_args, long long int) : \
16 (((_lcount) == 1) ? va_arg(_args, long int) : \
17 va_arg(_args, int)))
Soby Mathewf62d5462016-03-22 17:38:00 +000018
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010019#define get_unum_va_args(_args, _lcount) \
20 (((_lcount) > 1) ? va_arg(_args, unsigned long long int) : \
21 (((_lcount) == 1) ? va_arg(_args, unsigned long int) : \
22 va_arg(_args, unsigned int)))
Soby Mathewf62d5462016-03-22 17:38:00 +000023
Antonio Nino Diazc0c8eb62018-08-15 17:02:28 +010024static int string_print(const char *str)
Soby Mathewf62d5462016-03-22 17:38:00 +000025{
Antonio Nino Diazf4407082018-08-15 16:52:32 +010026 int count = 0;
27
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010028 assert(str != NULL);
Soby Mathewf583a062017-09-04 11:45:52 +010029
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010030 for ( ; *str != '\0'; str++) {
31 (void)putchar(*str);
Antonio Nino Diazf4407082018-08-15 16:52:32 +010032 count++;
33 }
34
35 return count;
Soby Mathewf62d5462016-03-22 17:38:00 +000036}
37
Antonio Nino Diazf4407082018-08-15 16:52:32 +010038static int unsigned_num_print(unsigned long long int unum, unsigned int radix,
39 char padc, int padn)
Soby Mathewafe7e2f2014-06-12 17:23:58 +010040{
41 /* Just need enough space to store 64 bit decimal integer */
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010042 char num_buf[20];
43 int i = 0, count = 0;
44 unsigned int rem;
Soby Mathewafe7e2f2014-06-12 17:23:58 +010045
46 do {
47 rem = unum % radix;
48 if (rem < 0xa)
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010049 num_buf[i] = '0' + rem;
Soby Mathewafe7e2f2014-06-12 17:23:58 +010050 else
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010051 num_buf[i] = 'a' + (rem - 0xa);
52 i++;
53 unum /= radix;
54 } while (unum > 0U);
Soby Mathewafe7e2f2014-06-12 17:23:58 +010055
Antonio Nino Diaz18c73532017-12-15 10:36:20 +000056 if (padn > 0) {
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010057 while (i < padn) {
58 (void)putchar(padc);
Antonio Nino Diazf4407082018-08-15 16:52:32 +010059 count++;
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010060 padn--;
Antonio Nino Diaz18c73532017-12-15 10:36:20 +000061 }
62 }
63
Antonio Nino Diazf4407082018-08-15 16:52:32 +010064 while (--i >= 0) {
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010065 (void)putchar(num_buf[i]);
Antonio Nino Diazf4407082018-08-15 16:52:32 +010066 count++;
67 }
68
69 return count;
Soby Mathewafe7e2f2014-06-12 17:23:58 +010070}
71
Soby Mathewafe7e2f2014-06-12 17:23:58 +010072/*******************************************************************
73 * Reduced format print for Trusted firmware.
Soby Mathewf62d5462016-03-22 17:38:00 +000074 * The following type specifiers are supported by this print
75 * %x - hexadecimal format
Soby Mathewafe7e2f2014-06-12 17:23:58 +010076 * %s - string format
Soby Mathewf62d5462016-03-22 17:38:00 +000077 * %d or %i - signed decimal format
78 * %u - unsigned decimal format
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +000079 * %p - pointer format
Soby Mathewf62d5462016-03-22 17:38:00 +000080 *
81 * The following length specifiers are supported by this print
82 * %l - long int (64-bit on AArch64)
83 * %ll - long long int (64-bit on AArch64)
84 * %z - size_t sized integer formats (64 bit on AArch64)
85 *
Antonio Nino Diaz18c73532017-12-15 10:36:20 +000086 * The following padding specifiers are supported by this print
87 * %0NN - Left-pad the number with 0s (NN is a decimal number)
88 *
Soby Mathewf62d5462016-03-22 17:38:00 +000089 * The print exits on all other formats specifiers other than valid
90 * combinations of the above specifiers.
Soby Mathewafe7e2f2014-06-12 17:23:58 +010091 *******************************************************************/
Antonio Nino Diazc0c8eb62018-08-15 17:02:28 +010092int vprintf(const char *fmt, va_list args)
Soby Mathewafe7e2f2014-06-12 17:23:58 +010093{
Soby Mathewf62d5462016-03-22 17:38:00 +000094 int l_count;
95 long long int num;
96 unsigned long long int unum;
Soby Mathewafe7e2f2014-06-12 17:23:58 +010097 char *str;
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010098 char padc = '\0'; /* Padding character */
Antonio Nino Diaz18c73532017-12-15 10:36:20 +000099 int padn; /* Number of characters to pad */
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100100 int count = 0; /* Number of printed characters */
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100101
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100102 while (*fmt != '\0') {
Soby Mathewf62d5462016-03-22 17:38:00 +0000103 l_count = 0;
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000104 padn = 0;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100105
106 if (*fmt == '%') {
107 fmt++;
108 /* Check the format specifier */
109loop:
110 switch (*fmt) {
Heyi Guoeb250ed2020-10-27 08:36:40 +0800111 case '%':
112 (void)putchar('%');
113 break;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100114 case 'i': /* Fall through to next one */
115 case 'd':
Soby Mathewf62d5462016-03-22 17:38:00 +0000116 num = get_num_va_args(args, l_count);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100117 if (num < 0) {
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100118 (void)putchar('-');
Soby Mathewf62d5462016-03-22 17:38:00 +0000119 unum = (unsigned long long int)-num;
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000120 padn--;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100121 } else
Soby Mathewf62d5462016-03-22 17:38:00 +0000122 unum = (unsigned long long int)num;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100123
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100124 count += unsigned_num_print(unum, 10,
125 padc, padn);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100126 break;
127 case 's':
128 str = va_arg(args, char *);
Antonio Nino Diazc0c8eb62018-08-15 17:02:28 +0100129 count += string_print(str);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100130 break;
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +0000131 case 'p':
Soby Mathewf62d5462016-03-22 17:38:00 +0000132 unum = (uintptr_t)va_arg(args, void *);
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100133 if (unum > 0U) {
Antonio Nino Diazc0c8eb62018-08-15 17:02:28 +0100134 count += string_print("0x");
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000135 padn -= 2;
136 }
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +0000137
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100138 count += unsigned_num_print(unum, 16,
139 padc, padn);
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +0000140 break;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100141 case 'x':
Soby Mathewf62d5462016-03-22 17:38:00 +0000142 unum = get_unum_va_args(args, l_count);
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100143 count += unsigned_num_print(unum, 16,
144 padc, padn);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100145 break;
Scott Branden57bd75c2016-03-23 13:37:33 -0700146 case 'z':
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100147 if (sizeof(size_t) == 8U)
Soby Mathewf62d5462016-03-22 17:38:00 +0000148 l_count = 2;
149
Scott Branden57bd75c2016-03-23 13:37:33 -0700150 fmt++;
151 goto loop;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100152 case 'l':
Soby Mathewf62d5462016-03-22 17:38:00 +0000153 l_count++;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100154 fmt++;
155 goto loop;
156 case 'u':
Soby Mathewf62d5462016-03-22 17:38:00 +0000157 unum = get_unum_va_args(args, l_count);
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100158 count += unsigned_num_print(unum, 10,
159 padc, padn);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100160 break;
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000161 case '0':
162 padc = '0';
163 padn = 0;
164 fmt++;
165
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100166 for (;;) {
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000167 char ch = *fmt;
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100168 if ((ch < '0') || (ch > '9')) {
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000169 goto loop;
170 }
171 padn = (padn * 10) + (ch - '0');
172 fmt++;
173 }
Daniel Boulby8942a1b2018-06-22 14:16:03 +0100174 assert(0); /* Unreachable */
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100175 default:
176 /* Exit on any other format specifier */
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100177 return -1;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100178 }
179 fmt++;
180 continue;
181 }
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100182 (void)putchar(*fmt);
183 fmt++;
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100184 count++;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100185 }
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100186
187 return count;
Soby Mathewf583a062017-09-04 11:45:52 +0100188}
189
Antonio Nino Diazc0c8eb62018-08-15 17:02:28 +0100190int printf(const char *fmt, ...)
Soby Mathewf583a062017-09-04 11:45:52 +0100191{
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100192 int count;
Soby Mathewf583a062017-09-04 11:45:52 +0100193 va_list va;
194
195 va_start(va, fmt);
Antonio Nino Diazc0c8eb62018-08-15 17:02:28 +0100196 count = vprintf(fmt, va);
Soby Mathewf583a062017-09-04 11:45:52 +0100197 va_end(va);
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100198
199 return count;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100200}