blob: 6931a7ea117fc68583d157ca5e93daf432901e9d [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,
Matt Schulte50851f02023-07-13 11:10:32 -070039 char padc, int padn, bool uppercase)
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;
Matt Schulte50851f02023-07-13 11:10:32 -070054 if (rem < 0xa) {
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010055 num_buf[i] = '0' + rem;
Matt Schulte50851f02023-07-13 11:10:32 -070056 } else if (uppercase) {
57 num_buf[i] = 'A' + (rem - 0xa);
58 } else {
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010059 num_buf[i] = 'a' + (rem - 0xa);
Matt Schulte50851f02023-07-13 11:10:32 -070060 }
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010061 i++;
62 unum /= radix;
63 } while (unum > 0U);
Soby Mathewafe7e2f2014-06-12 17:23:58 +010064
Antonio Nino Diaz18c73532017-12-15 10:36:20 +000065 if (padn > 0) {
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010066 while (i < padn) {
67 (void)putchar(padc);
Antonio Nino Diazf4407082018-08-15 16:52:32 +010068 count++;
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010069 padn--;
Antonio Nino Diaz18c73532017-12-15 10:36:20 +000070 }
71 }
72
Antonio Nino Diazf4407082018-08-15 16:52:32 +010073 while (--i >= 0) {
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010074 (void)putchar(num_buf[i]);
Antonio Nino Diazf4407082018-08-15 16:52:32 +010075 count++;
76 }
77
78 return count;
Soby Mathewafe7e2f2014-06-12 17:23:58 +010079}
80
Soby Mathewafe7e2f2014-06-12 17:23:58 +010081/*******************************************************************
82 * Reduced format print for Trusted firmware.
Soby Mathewf62d5462016-03-22 17:38:00 +000083 * The following type specifiers are supported by this print
84 * %x - hexadecimal format
Soby Mathewafe7e2f2014-06-12 17:23:58 +010085 * %s - string format
Soby Mathewf62d5462016-03-22 17:38:00 +000086 * %d or %i - signed decimal format
Maksims Svecovsbed55212023-05-09 12:03:31 +010087 * %c - character format
Soby Mathewf62d5462016-03-22 17:38:00 +000088 * %u - unsigned decimal format
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +000089 * %p - pointer format
Soby Mathewf62d5462016-03-22 17:38:00 +000090 *
91 * The following length specifiers are supported by this print
92 * %l - long int (64-bit on AArch64)
93 * %ll - long long int (64-bit on AArch64)
94 * %z - size_t sized integer formats (64 bit on AArch64)
95 *
Antonio Nino Diaz18c73532017-12-15 10:36:20 +000096 * The following padding specifiers are supported by this print
97 * %0NN - Left-pad the number with 0s (NN is a decimal number)
98 *
Soby Mathewf62d5462016-03-22 17:38:00 +000099 * The print exits on all other formats specifiers other than valid
100 * combinations of the above specifiers.
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100101 *******************************************************************/
Antonio Nino Diazc0c8eb62018-08-15 17:02:28 +0100102int vprintf(const char *fmt, va_list args)
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100103{
Soby Mathewf62d5462016-03-22 17:38:00 +0000104 int l_count;
105 long long int num;
106 unsigned long long int unum;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100107 char *str;
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100108 char padc = '\0'; /* Padding character */
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000109 int padn; /* Number of characters to pad */
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100110 int count = 0; /* Number of printed characters */
Matt Schulte50851f02023-07-13 11:10:32 -0700111 bool uppercase; /* Print characters in uppercase */
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100112
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100113 while (*fmt != '\0') {
Matt Schulte50851f02023-07-13 11:10:32 -0700114 uppercase = false;
Soby Mathewf62d5462016-03-22 17:38:00 +0000115 l_count = 0;
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000116 padn = 0;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100117
118 if (*fmt == '%') {
119 fmt++;
120 /* Check the format specifier */
121loop:
122 switch (*fmt) {
Heyi Guoeb250ed2020-10-27 08:36:40 +0800123 case '%':
124 (void)putchar('%');
125 break;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100126 case 'i': /* Fall through to next one */
127 case 'd':
Soby Mathewf62d5462016-03-22 17:38:00 +0000128 num = get_num_va_args(args, l_count);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100129 if (num < 0) {
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100130 (void)putchar('-');
Soby Mathewf62d5462016-03-22 17:38:00 +0000131 unum = (unsigned long long int)-num;
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000132 padn--;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100133 } else
Soby Mathewf62d5462016-03-22 17:38:00 +0000134 unum = (unsigned long long int)num;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100135
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100136 count += unsigned_num_print(unum, 10,
Matt Schulte50851f02023-07-13 11:10:32 -0700137 padc, padn, uppercase);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100138 break;
Maksims Svecovsbed55212023-05-09 12:03:31 +0100139 case 'c':
140 (void)putchar(va_arg(args, int));
141 count++;
142 break;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100143 case 's':
144 str = va_arg(args, char *);
Antonio Nino Diazc0c8eb62018-08-15 17:02:28 +0100145 count += string_print(str);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100146 break;
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +0000147 case 'p':
Soby Mathewf62d5462016-03-22 17:38:00 +0000148 unum = (uintptr_t)va_arg(args, void *);
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100149 if (unum > 0U) {
Antonio Nino Diazc0c8eb62018-08-15 17:02:28 +0100150 count += string_print("0x");
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000151 padn -= 2;
152 }
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +0000153
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100154 count += unsigned_num_print(unum, 16,
Matt Schulte50851f02023-07-13 11:10:32 -0700155 padc, padn, uppercase);
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +0000156 break;
Matt Schulte50851f02023-07-13 11:10:32 -0700157 case 'X':
158 uppercase = true;
159 // fall through
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100160 case 'x':
Soby Mathewf62d5462016-03-22 17:38:00 +0000161 unum = get_unum_va_args(args, l_count);
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100162 count += unsigned_num_print(unum, 16,
Matt Schulte50851f02023-07-13 11:10:32 -0700163 padc, padn, uppercase);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100164 break;
Scott Branden57bd75c2016-03-23 13:37:33 -0700165 case 'z':
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100166 if (sizeof(size_t) == 8U)
Soby Mathewf62d5462016-03-22 17:38:00 +0000167 l_count = 2;
168
Scott Branden57bd75c2016-03-23 13:37:33 -0700169 fmt++;
170 goto loop;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100171 case 'l':
Soby Mathewf62d5462016-03-22 17:38:00 +0000172 l_count++;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100173 fmt++;
174 goto loop;
175 case 'u':
Soby Mathewf62d5462016-03-22 17:38:00 +0000176 unum = get_unum_va_args(args, l_count);
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100177 count += unsigned_num_print(unum, 10,
Matt Schulte50851f02023-07-13 11:10:32 -0700178 padc, padn, uppercase);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100179 break;
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000180 case '0':
181 padc = '0';
182 padn = 0;
183 fmt++;
184
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100185 for (;;) {
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000186 char ch = *fmt;
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100187 if ((ch < '0') || (ch > '9')) {
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000188 goto loop;
189 }
190 padn = (padn * 10) + (ch - '0');
191 fmt++;
192 }
Daniel Boulby8942a1b2018-06-22 14:16:03 +0100193 assert(0); /* Unreachable */
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100194 default:
195 /* Exit on any other format specifier */
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100196 return -1;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100197 }
198 fmt++;
199 continue;
200 }
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100201 (void)putchar(*fmt);
202 fmt++;
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100203 count++;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100204 }
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100205
206 return count;
Soby Mathewf583a062017-09-04 11:45:52 +0100207}
208
Antonio Nino Diazc0c8eb62018-08-15 17:02:28 +0100209int printf(const char *fmt, ...)
Soby Mathewf583a062017-09-04 11:45:52 +0100210{
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100211 int count;
Soby Mathewf583a062017-09-04 11:45:52 +0100212 va_list va;
213
214 va_start(va, fmt);
Antonio Nino Diazc0c8eb62018-08-15 17:02:28 +0100215 count = vprintf(fmt, va);
Soby Mathewf583a062017-09-04 11:45:52 +0100216 va_end(va);
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100217
218 return count;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100219}