blob: 4480e94db7610659ff044a843541a7d570282f9e [file] [log] [blame]
Soby Mathewafe7e2f2014-06-12 17:23:58 +01001/*
Antonio Nino Diazc0c8eb62018-08-15 17:02:28 +01002 * Copyright (c) 2014-2018, 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 <assert.h>
Soby Mathewafe7e2f2014-06-12 17:23:58 +01007#include <debug.h>
8#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 Diaz2e74f9b2018-08-23 15:11:46 +010012#define get_num_va_args(_args, _lcount) \
13 (((_lcount) > 1) ? va_arg(_args, long long int) : \
14 (((_lcount) == 1) ? va_arg(_args, long int) : \
15 va_arg(_args, int)))
Soby Mathewf62d5462016-03-22 17:38:00 +000016
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010017#define get_unum_va_args(_args, _lcount) \
18 (((_lcount) > 1) ? va_arg(_args, unsigned long long int) : \
19 (((_lcount) == 1) ? va_arg(_args, unsigned long int) : \
20 va_arg(_args, unsigned int)))
Soby Mathewf62d5462016-03-22 17:38:00 +000021
Antonio Nino Diazc0c8eb62018-08-15 17:02:28 +010022static int string_print(const char *str)
Soby Mathewf62d5462016-03-22 17:38:00 +000023{
Antonio Nino Diazf4407082018-08-15 16:52:32 +010024 int count = 0;
25
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010026 assert(str != NULL);
Soby Mathewf583a062017-09-04 11:45:52 +010027
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010028 for ( ; *str != '\0'; str++) {
29 (void)putchar(*str);
Antonio Nino Diazf4407082018-08-15 16:52:32 +010030 count++;
31 }
32
33 return count;
Soby Mathewf62d5462016-03-22 17:38:00 +000034}
35
Antonio Nino Diazf4407082018-08-15 16:52:32 +010036static int unsigned_num_print(unsigned long long int unum, unsigned int radix,
37 char padc, int padn)
Soby Mathewafe7e2f2014-06-12 17:23:58 +010038{
39 /* Just need enough space to store 64 bit decimal integer */
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010040 char num_buf[20];
41 int i = 0, count = 0;
42 unsigned int rem;
Soby Mathewafe7e2f2014-06-12 17:23:58 +010043
44 do {
45 rem = unum % radix;
46 if (rem < 0xa)
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010047 num_buf[i] = '0' + rem;
Soby Mathewafe7e2f2014-06-12 17:23:58 +010048 else
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010049 num_buf[i] = 'a' + (rem - 0xa);
50 i++;
51 unum /= radix;
52 } while (unum > 0U);
Soby Mathewafe7e2f2014-06-12 17:23:58 +010053
Antonio Nino Diaz18c73532017-12-15 10:36:20 +000054 if (padn > 0) {
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010055 while (i < padn) {
56 (void)putchar(padc);
Antonio Nino Diazf4407082018-08-15 16:52:32 +010057 count++;
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010058 padn--;
Antonio Nino Diaz18c73532017-12-15 10:36:20 +000059 }
60 }
61
Antonio Nino Diazf4407082018-08-15 16:52:32 +010062 while (--i >= 0) {
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010063 (void)putchar(num_buf[i]);
Antonio Nino Diazf4407082018-08-15 16:52:32 +010064 count++;
65 }
66
67 return count;
Soby Mathewafe7e2f2014-06-12 17:23:58 +010068}
69
Soby Mathewafe7e2f2014-06-12 17:23:58 +010070/*******************************************************************
71 * Reduced format print for Trusted firmware.
Soby Mathewf62d5462016-03-22 17:38:00 +000072 * The following type specifiers are supported by this print
73 * %x - hexadecimal format
Soby Mathewafe7e2f2014-06-12 17:23:58 +010074 * %s - string format
Soby Mathewf62d5462016-03-22 17:38:00 +000075 * %d or %i - signed decimal format
76 * %u - unsigned decimal format
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +000077 * %p - pointer format
Soby Mathewf62d5462016-03-22 17:38:00 +000078 *
79 * The following length specifiers are supported by this print
80 * %l - long int (64-bit on AArch64)
81 * %ll - long long int (64-bit on AArch64)
82 * %z - size_t sized integer formats (64 bit on AArch64)
83 *
Antonio Nino Diaz18c73532017-12-15 10:36:20 +000084 * The following padding specifiers are supported by this print
85 * %0NN - Left-pad the number with 0s (NN is a decimal number)
86 *
Soby Mathewf62d5462016-03-22 17:38:00 +000087 * The print exits on all other formats specifiers other than valid
88 * combinations of the above specifiers.
Soby Mathewafe7e2f2014-06-12 17:23:58 +010089 *******************************************************************/
Antonio Nino Diazc0c8eb62018-08-15 17:02:28 +010090int vprintf(const char *fmt, va_list args)
Soby Mathewafe7e2f2014-06-12 17:23:58 +010091{
Soby Mathewf62d5462016-03-22 17:38:00 +000092 int l_count;
93 long long int num;
94 unsigned long long int unum;
Soby Mathewafe7e2f2014-06-12 17:23:58 +010095 char *str;
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +010096 char padc = '\0'; /* Padding character */
Antonio Nino Diaz18c73532017-12-15 10:36:20 +000097 int padn; /* Number of characters to pad */
Antonio Nino Diazf4407082018-08-15 16:52:32 +010098 int count = 0; /* Number of printed characters */
Soby Mathewafe7e2f2014-06-12 17:23:58 +010099
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100100 while (*fmt != '\0') {
Soby Mathewf62d5462016-03-22 17:38:00 +0000101 l_count = 0;
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000102 padn = 0;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100103
104 if (*fmt == '%') {
105 fmt++;
106 /* Check the format specifier */
107loop:
108 switch (*fmt) {
109 case 'i': /* Fall through to next one */
110 case 'd':
Soby Mathewf62d5462016-03-22 17:38:00 +0000111 num = get_num_va_args(args, l_count);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100112 if (num < 0) {
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100113 (void)putchar('-');
Soby Mathewf62d5462016-03-22 17:38:00 +0000114 unum = (unsigned long long int)-num;
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000115 padn--;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100116 } else
Soby Mathewf62d5462016-03-22 17:38:00 +0000117 unum = (unsigned long long int)num;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100118
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100119 count += unsigned_num_print(unum, 10,
120 padc, padn);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100121 break;
122 case 's':
123 str = va_arg(args, char *);
Antonio Nino Diazc0c8eb62018-08-15 17:02:28 +0100124 count += string_print(str);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100125 break;
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +0000126 case 'p':
Soby Mathewf62d5462016-03-22 17:38:00 +0000127 unum = (uintptr_t)va_arg(args, void *);
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100128 if (unum > 0U) {
Antonio Nino Diazc0c8eb62018-08-15 17:02:28 +0100129 count += string_print("0x");
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000130 padn -= 2;
131 }
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +0000132
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100133 count += unsigned_num_print(unum, 16,
134 padc, padn);
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +0000135 break;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100136 case 'x':
Soby Mathewf62d5462016-03-22 17:38:00 +0000137 unum = get_unum_va_args(args, l_count);
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100138 count += unsigned_num_print(unum, 16,
139 padc, padn);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100140 break;
Scott Branden57bd75c2016-03-23 13:37:33 -0700141 case 'z':
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100142 if (sizeof(size_t) == 8U)
Soby Mathewf62d5462016-03-22 17:38:00 +0000143 l_count = 2;
144
Scott Branden57bd75c2016-03-23 13:37:33 -0700145 fmt++;
146 goto loop;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100147 case 'l':
Soby Mathewf62d5462016-03-22 17:38:00 +0000148 l_count++;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100149 fmt++;
150 goto loop;
151 case 'u':
Soby Mathewf62d5462016-03-22 17:38:00 +0000152 unum = get_unum_va_args(args, l_count);
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100153 count += unsigned_num_print(unum, 10,
154 padc, padn);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100155 break;
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000156 case '0':
157 padc = '0';
158 padn = 0;
159 fmt++;
160
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100161 for (;;) {
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000162 char ch = *fmt;
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100163 if ((ch < '0') || (ch > '9')) {
Antonio Nino Diaz18c73532017-12-15 10:36:20 +0000164 goto loop;
165 }
166 padn = (padn * 10) + (ch - '0');
167 fmt++;
168 }
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100169 default:
170 /* Exit on any other format specifier */
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100171 return -1;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100172 }
173 fmt++;
174 continue;
175 }
Antonio Nino Diaz2e74f9b2018-08-23 15:11:46 +0100176 (void)putchar(*fmt);
177 fmt++;
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100178 count++;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100179 }
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100180
181 return count;
Soby Mathewf583a062017-09-04 11:45:52 +0100182}
183
Antonio Nino Diazc0c8eb62018-08-15 17:02:28 +0100184int printf(const char *fmt, ...)
Soby Mathewf583a062017-09-04 11:45:52 +0100185{
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100186 int count;
Soby Mathewf583a062017-09-04 11:45:52 +0100187 va_list va;
188
189 va_start(va, fmt);
Antonio Nino Diazc0c8eb62018-08-15 17:02:28 +0100190 count = vprintf(fmt, va);
Soby Mathewf583a062017-09-04 11:45:52 +0100191 va_end(va);
Antonio Nino Diazf4407082018-08-15 16:52:32 +0100192
193 return count;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100194}