blob: e52cbed735559459d0a1143d131e406c67bae47d [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>
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
84 * %u - unsigned decimal format
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +000085 * %p - pointer format
Soby Mathewf62d5462016-03-22 17:38:00 +000086 *
87 * The following length specifiers are supported by this print
88 * %l - long int (64-bit on AArch64)
89 * %ll - long long int (64-bit on AArch64)
90 * %z - size_t sized integer formats (64 bit on AArch64)
91 *
Antonio Nino Diaz18c73532017-12-15 10:36:20 +000092 * The following padding specifiers are supported by this print
93 * %0NN - Left-pad the number with 0s (NN is a decimal number)
94 *
Soby Mathewf62d5462016-03-22 17:38:00 +000095 * The print exits on all other formats specifiers other than valid
96 * combinations of the above specifiers.
Soby Mathewafe7e2f2014-06-12 17:23:58 +010097 *******************************************************************/
Antonio Nino Diazc0c8eb62018-08-15 17:02:28 +010098int vprintf(const char *fmt, va_list args)
Soby Mathewafe7e2f2014-06-12 17:23:58 +010099{
Soby Mathewf62d5462016-03-22 17:38:00 +0000100 int l_count;
101 long long int num;
102 unsigned long long int unum;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100103 char *str;
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100104 char padc = '\0'; /* Padding character */
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000105 int padn; /* Number of characters to pad */
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100106 int count = 0; /* Number of printed characters */
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100107
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100108 while (*fmt != '\0') {
Soby Mathewf62d5462016-03-22 17:38:00 +0000109 l_count = 0;
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000110 padn = 0;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100111
112 if (*fmt == '%') {
113 fmt++;
114 /* Check the format specifier */
115loop:
116 switch (*fmt) {
Heyi Guoeb250ed2020-10-27 08:36:40 +0800117 case '%':
118 (void)putchar('%');
119 break;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100120 case 'i': /* Fall through to next one */
121 case 'd':
Soby Mathewf62d5462016-03-22 17:38:00 +0000122 num = get_num_va_args(args, l_count);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100123 if (num < 0) {
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100124 (void)putchar('-');
Soby Mathewf62d5462016-03-22 17:38:00 +0000125 unum = (unsigned long long int)-num;
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000126 padn--;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100127 } else
Soby Mathewf62d5462016-03-22 17:38:00 +0000128 unum = (unsigned long long int)num;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100129
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100130 count += unsigned_num_print(unum, 10,
131 padc, padn);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100132 break;
133 case 's':
134 str = va_arg(args, char *);
Antonio Nino Diazc0c8eb62018-08-15 17:02:28 +0100135 count += string_print(str);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100136 break;
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +0000137 case 'p':
Soby Mathewf62d5462016-03-22 17:38:00 +0000138 unum = (uintptr_t)va_arg(args, void *);
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100139 if (unum > 0U) {
Antonio Nino Diazc0c8eb62018-08-15 17:02:28 +0100140 count += string_print("0x");
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000141 padn -= 2;
142 }
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +0000143
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100144 count += unsigned_num_print(unum, 16,
145 padc, padn);
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +0000146 break;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100147 case 'x':
Soby Mathewf62d5462016-03-22 17:38:00 +0000148 unum = get_unum_va_args(args, l_count);
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100149 count += unsigned_num_print(unum, 16,
150 padc, padn);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100151 break;
Scott Branden57bd75c2016-03-23 13:37:33 -0700152 case 'z':
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100153 if (sizeof(size_t) == 8U)
Soby Mathewf62d5462016-03-22 17:38:00 +0000154 l_count = 2;
155
Scott Branden57bd75c2016-03-23 13:37:33 -0700156 fmt++;
157 goto loop;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100158 case 'l':
Soby Mathewf62d5462016-03-22 17:38:00 +0000159 l_count++;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100160 fmt++;
161 goto loop;
162 case 'u':
Soby Mathewf62d5462016-03-22 17:38:00 +0000163 unum = get_unum_va_args(args, l_count);
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100164 count += unsigned_num_print(unum, 10,
165 padc, padn);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100166 break;
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000167 case '0':
168 padc = '0';
169 padn = 0;
170 fmt++;
171
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100172 for (;;) {
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000173 char ch = *fmt;
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100174 if ((ch < '0') || (ch > '9')) {
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000175 goto loop;
176 }
177 padn = (padn * 10) + (ch - '0');
178 fmt++;
179 }
Daniel Boulby8942a1b2018-06-22 14:16:03 +0100180 assert(0); /* Unreachable */
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100181 default:
182 /* Exit on any other format specifier */
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100183 return -1;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100184 }
185 fmt++;
186 continue;
187 }
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100188 (void)putchar(*fmt);
189 fmt++;
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100190 count++;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100191 }
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100192
193 return count;
Soby Mathewf583a062017-09-04 11:45:52 +0100194}
195
Antonio Nino Diazc0c8eb62018-08-15 17:02:28 +0100196int printf(const char *fmt, ...)
Soby Mathewf583a062017-09-04 11:45:52 +0100197{
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100198 int count;
Soby Mathewf583a062017-09-04 11:45:52 +0100199 va_list va;
200
201 va_start(va, fmt);
Antonio Nino Diazc0c8eb62018-08-15 17:02:28 +0100202 count = vprintf(fmt, va);
Soby Mathewf583a062017-09-04 11:45:52 +0100203 va_end(va);
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100204
205 return count;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100206}