blob: faccfdff4966559933e84319c409f6f530a119b1 [file] [log] [blame]
Soby Mathewafe7e2f2014-06-12 17:23:58 +01001/*
Maksims Svecovsbed55212023-05-09 12:03:31 +01002 * Copyright (c) 2014-2023, 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>
Jorge Troncosoa7538dc2022-09-27 17:35:54 -070010#include <stddef.h>
Soby Mathewafe7e2f2014-06-12 17:23:58 +010011#include <stdint.h>
Jorge Troncosoa7538dc2022-09-27 17:35:54 -070012#include <stdio.h>
Soby Mathewafe7e2f2014-06-12 17:23:58 +010013
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
Claus Pedersen785e66c2022-09-12 22:42:58 +000046 /* num_buf is only large enough for radix >= 10 */
47 if (radix < 10) {
48 assert(0);
49 return 0;
50 }
51
Soby Mathewafe7e2f2014-06-12 17:23:58 +010052 do {
53 rem = unum % radix;
54 if (rem < 0xa)
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010055 num_buf[i] = '0' + rem;
Soby Mathewafe7e2f2014-06-12 17:23:58 +010056 else
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010057 num_buf[i] = 'a' + (rem - 0xa);
58 i++;
59 unum /= radix;
60 } while (unum > 0U);
Soby Mathewafe7e2f2014-06-12 17:23:58 +010061
Antonio Nino Diaz18c73532017-12-15 10:36:20 +000062 if (padn > 0) {
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010063 while (i < padn) {
64 (void)putchar(padc);
Antonio Nino Diazf4407082018-08-15 16:52:32 +010065 count++;
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010066 padn--;
Antonio Nino Diaz18c73532017-12-15 10:36:20 +000067 }
68 }
69
Antonio Nino Diazf4407082018-08-15 16:52:32 +010070 while (--i >= 0) {
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010071 (void)putchar(num_buf[i]);
Antonio Nino Diazf4407082018-08-15 16:52:32 +010072 count++;
73 }
74
75 return count;
Soby Mathewafe7e2f2014-06-12 17:23:58 +010076}
77
Soby Mathewafe7e2f2014-06-12 17:23:58 +010078/*******************************************************************
79 * Reduced format print for Trusted firmware.
Soby Mathewf62d5462016-03-22 17:38:00 +000080 * The following type specifiers are supported by this print
81 * %x - hexadecimal format
Soby Mathewafe7e2f2014-06-12 17:23:58 +010082 * %s - string format
Soby Mathewf62d5462016-03-22 17:38:00 +000083 * %d or %i - signed decimal format
Maksims Svecovsbed55212023-05-09 12:03:31 +010084 * %c - character format
Soby Mathewf62d5462016-03-22 17:38:00 +000085 * %u - unsigned decimal format
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +000086 * %p - pointer format
Soby Mathewf62d5462016-03-22 17:38:00 +000087 *
88 * The following length specifiers are supported by this print
89 * %l - long int (64-bit on AArch64)
90 * %ll - long long int (64-bit on AArch64)
91 * %z - size_t sized integer formats (64 bit on AArch64)
92 *
Antonio Nino Diaz18c73532017-12-15 10:36:20 +000093 * The following padding specifiers are supported by this print
94 * %0NN - Left-pad the number with 0s (NN is a decimal number)
95 *
Soby Mathewf62d5462016-03-22 17:38:00 +000096 * The print exits on all other formats specifiers other than valid
97 * combinations of the above specifiers.
Soby Mathewafe7e2f2014-06-12 17:23:58 +010098 *******************************************************************/
Antonio Nino Diazc0c8eb62018-08-15 17:02:28 +010099int vprintf(const char *fmt, va_list args)
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100100{
Soby Mathewf62d5462016-03-22 17:38:00 +0000101 int l_count;
102 long long int num;
103 unsigned long long int unum;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100104 char *str;
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100105 char padc = '\0'; /* Padding character */
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000106 int padn; /* Number of characters to pad */
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100107 int count = 0; /* Number of printed characters */
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100108
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100109 while (*fmt != '\0') {
Soby Mathewf62d5462016-03-22 17:38:00 +0000110 l_count = 0;
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000111 padn = 0;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100112
113 if (*fmt == '%') {
114 fmt++;
115 /* Check the format specifier */
116loop:
117 switch (*fmt) {
Heyi Guoeb250ed2020-10-27 08:36:40 +0800118 case '%':
119 (void)putchar('%');
120 break;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100121 case 'i': /* Fall through to next one */
122 case 'd':
Soby Mathewf62d5462016-03-22 17:38:00 +0000123 num = get_num_va_args(args, l_count);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100124 if (num < 0) {
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100125 (void)putchar('-');
Soby Mathewf62d5462016-03-22 17:38:00 +0000126 unum = (unsigned long long int)-num;
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000127 padn--;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100128 } else
Soby Mathewf62d5462016-03-22 17:38:00 +0000129 unum = (unsigned long long int)num;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100130
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100131 count += unsigned_num_print(unum, 10,
132 padc, padn);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100133 break;
Maksims Svecovsbed55212023-05-09 12:03:31 +0100134 case 'c':
135 (void)putchar(va_arg(args, int));
136 count++;
137 break;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100138 case 's':
139 str = va_arg(args, char *);
Antonio Nino Diazc0c8eb62018-08-15 17:02:28 +0100140 count += string_print(str);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100141 break;
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +0000142 case 'p':
Soby Mathewf62d5462016-03-22 17:38:00 +0000143 unum = (uintptr_t)va_arg(args, void *);
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100144 if (unum > 0U) {
Antonio Nino Diazc0c8eb62018-08-15 17:02:28 +0100145 count += string_print("0x");
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000146 padn -= 2;
147 }
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +0000148
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100149 count += unsigned_num_print(unum, 16,
150 padc, padn);
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +0000151 break;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100152 case 'x':
Soby Mathewf62d5462016-03-22 17:38:00 +0000153 unum = get_unum_va_args(args, l_count);
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100154 count += unsigned_num_print(unum, 16,
155 padc, padn);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100156 break;
Scott Branden57bd75c2016-03-23 13:37:33 -0700157 case 'z':
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100158 if (sizeof(size_t) == 8U)
Soby Mathewf62d5462016-03-22 17:38:00 +0000159 l_count = 2;
160
Scott Branden57bd75c2016-03-23 13:37:33 -0700161 fmt++;
162 goto loop;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100163 case 'l':
Soby Mathewf62d5462016-03-22 17:38:00 +0000164 l_count++;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100165 fmt++;
166 goto loop;
167 case 'u':
Soby Mathewf62d5462016-03-22 17:38:00 +0000168 unum = get_unum_va_args(args, l_count);
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100169 count += unsigned_num_print(unum, 10,
170 padc, padn);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100171 break;
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000172 case '0':
173 padc = '0';
174 padn = 0;
175 fmt++;
176
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100177 for (;;) {
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000178 char ch = *fmt;
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100179 if ((ch < '0') || (ch > '9')) {
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000180 goto loop;
181 }
182 padn = (padn * 10) + (ch - '0');
183 fmt++;
184 }
Daniel Boulby8942a1b2018-06-22 14:16:03 +0100185 assert(0); /* Unreachable */
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100186 default:
187 /* Exit on any other format specifier */
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100188 return -1;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100189 }
190 fmt++;
191 continue;
192 }
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100193 (void)putchar(*fmt);
194 fmt++;
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100195 count++;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100196 }
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100197
198 return count;
Soby Mathewf583a062017-09-04 11:45:52 +0100199}
200
Antonio Nino Diazc0c8eb62018-08-15 17:02:28 +0100201int printf(const char *fmt, ...)
Soby Mathewf583a062017-09-04 11:45:52 +0100202{
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100203 int count;
Soby Mathewf583a062017-09-04 11:45:52 +0100204 va_list va;
205
206 va_start(va, fmt);
Antonio Nino Diazc0c8eb62018-08-15 17:02:28 +0100207 count = vprintf(fmt, va);
Soby Mathewf583a062017-09-04 11:45:52 +0100208 va_end(va);
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100209
210 return count;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100211}