Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 1 | /* |
Maksims Svecovs | bed5521 | 2023-05-09 12:03:31 +0100 | [diff] [blame] | 2 | * Copyright (c) 2014-2023, Arm Limited and Contributors. All rights reserved. |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 5 | */ |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 6 | |
Soby Mathew | 0691d97 | 2016-05-05 12:34:41 +0100 | [diff] [blame] | 7 | #include <assert.h> |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 8 | #include <stdarg.h> |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 9 | #include <stdbool.h> |
Jorge Troncoso | a7538dc | 2022-09-27 17:35:54 -0700 | [diff] [blame] | 10 | #include <stddef.h> |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 11 | #include <stdint.h> |
Jorge Troncoso | a7538dc | 2022-09-27 17:35:54 -0700 | [diff] [blame] | 12 | #include <stdio.h> |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 13 | |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 14 | #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 Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 18 | |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 19 | #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 Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 23 | |
Antonio Nino Diaz | c0c8eb6 | 2018-08-15 17:02:28 +0100 | [diff] [blame] | 24 | static int string_print(const char *str) |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 25 | { |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 26 | int count = 0; |
| 27 | |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 28 | assert(str != NULL); |
Soby Mathew | f583a06 | 2017-09-04 11:45:52 +0100 | [diff] [blame] | 29 | |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 30 | for ( ; *str != '\0'; str++) { |
| 31 | (void)putchar(*str); |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 32 | count++; |
| 33 | } |
| 34 | |
| 35 | return count; |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 38 | static int unsigned_num_print(unsigned long long int unum, unsigned int radix, |
Matt Schulte | 50851f0 | 2023-07-13 11:10:32 -0700 | [diff] [blame] | 39 | char padc, int padn, bool uppercase) |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 40 | { |
| 41 | /* Just need enough space to store 64 bit decimal integer */ |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 42 | char num_buf[20]; |
| 43 | int i = 0, count = 0; |
| 44 | unsigned int rem; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 45 | |
Claus Pedersen | 785e66c | 2022-09-12 22:42:58 +0000 | [diff] [blame] | 46 | /* num_buf is only large enough for radix >= 10 */ |
| 47 | if (radix < 10) { |
| 48 | assert(0); |
| 49 | return 0; |
| 50 | } |
| 51 | |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 52 | do { |
| 53 | rem = unum % radix; |
Matt Schulte | 50851f0 | 2023-07-13 11:10:32 -0700 | [diff] [blame] | 54 | if (rem < 0xa) { |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 55 | num_buf[i] = '0' + rem; |
Matt Schulte | 50851f0 | 2023-07-13 11:10:32 -0700 | [diff] [blame] | 56 | } else if (uppercase) { |
| 57 | num_buf[i] = 'A' + (rem - 0xa); |
| 58 | } else { |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 59 | num_buf[i] = 'a' + (rem - 0xa); |
Matt Schulte | 50851f0 | 2023-07-13 11:10:32 -0700 | [diff] [blame] | 60 | } |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 61 | i++; |
| 62 | unum /= radix; |
| 63 | } while (unum > 0U); |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 64 | |
Antonio Nino Diaz | 18c7353 | 2017-12-15 10:36:20 +0000 | [diff] [blame] | 65 | if (padn > 0) { |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 66 | while (i < padn) { |
| 67 | (void)putchar(padc); |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 68 | count++; |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 69 | padn--; |
Antonio Nino Diaz | 18c7353 | 2017-12-15 10:36:20 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 73 | while (--i >= 0) { |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 74 | (void)putchar(num_buf[i]); |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 75 | count++; |
| 76 | } |
| 77 | |
| 78 | return count; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 81 | /******************************************************************* |
| 82 | * Reduced format print for Trusted firmware. |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 83 | * The following type specifiers are supported by this print |
| 84 | * %x - hexadecimal format |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 85 | * %s - string format |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 86 | * %d or %i - signed decimal format |
Maksims Svecovs | bed5521 | 2023-05-09 12:03:31 +0100 | [diff] [blame] | 87 | * %c - character format |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 88 | * %u - unsigned decimal format |
Antonio Nino Diaz | b3a0a7b | 2016-02-02 12:03:38 +0000 | [diff] [blame] | 89 | * %p - pointer format |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 90 | * |
| 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 Diaz | 18c7353 | 2017-12-15 10:36:20 +0000 | [diff] [blame] | 96 | * The following padding specifiers are supported by this print |
| 97 | * %0NN - Left-pad the number with 0s (NN is a decimal number) |
Juan Pablo Conde | 746c0af | 2023-11-08 13:03:53 -0600 | [diff] [blame] | 98 | * %NN - Left-pad the number with spaces (NN is a decimal number) |
Antonio Nino Diaz | 18c7353 | 2017-12-15 10:36:20 +0000 | [diff] [blame] | 99 | * |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 100 | * The print exits on all other formats specifiers other than valid |
| 101 | * combinations of the above specifiers. |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 102 | *******************************************************************/ |
Antonio Nino Diaz | c0c8eb6 | 2018-08-15 17:02:28 +0100 | [diff] [blame] | 103 | int vprintf(const char *fmt, va_list args) |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 104 | { |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 105 | int l_count; |
| 106 | long long int num; |
| 107 | unsigned long long int unum; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 108 | char *str; |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 109 | char padc = '\0'; /* Padding character */ |
Antonio Nino Diaz | 18c7353 | 2017-12-15 10:36:20 +0000 | [diff] [blame] | 110 | int padn; /* Number of characters to pad */ |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 111 | int count = 0; /* Number of printed characters */ |
Matt Schulte | 50851f0 | 2023-07-13 11:10:32 -0700 | [diff] [blame] | 112 | bool uppercase; /* Print characters in uppercase */ |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 113 | |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 114 | while (*fmt != '\0') { |
Matt Schulte | 50851f0 | 2023-07-13 11:10:32 -0700 | [diff] [blame] | 115 | uppercase = false; |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 116 | l_count = 0; |
Antonio Nino Diaz | 18c7353 | 2017-12-15 10:36:20 +0000 | [diff] [blame] | 117 | padn = 0; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 118 | |
| 119 | if (*fmt == '%') { |
| 120 | fmt++; |
| 121 | /* Check the format specifier */ |
| 122 | loop: |
| 123 | switch (*fmt) { |
Heyi Guo | eb250ed | 2020-10-27 08:36:40 +0800 | [diff] [blame] | 124 | case '%': |
| 125 | (void)putchar('%'); |
| 126 | break; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 127 | case 'i': /* Fall through to next one */ |
| 128 | case 'd': |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 129 | num = get_num_va_args(args, l_count); |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 130 | if (num < 0) { |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 131 | (void)putchar('-'); |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 132 | unum = (unsigned long long int)-num; |
Antonio Nino Diaz | 18c7353 | 2017-12-15 10:36:20 +0000 | [diff] [blame] | 133 | padn--; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 134 | } else |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 135 | unum = (unsigned long long int)num; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 136 | |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 137 | count += unsigned_num_print(unum, 10, |
Matt Schulte | 50851f0 | 2023-07-13 11:10:32 -0700 | [diff] [blame] | 138 | padc, padn, uppercase); |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 139 | break; |
Maksims Svecovs | bed5521 | 2023-05-09 12:03:31 +0100 | [diff] [blame] | 140 | case 'c': |
| 141 | (void)putchar(va_arg(args, int)); |
| 142 | count++; |
| 143 | break; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 144 | case 's': |
| 145 | str = va_arg(args, char *); |
Antonio Nino Diaz | c0c8eb6 | 2018-08-15 17:02:28 +0100 | [diff] [blame] | 146 | count += string_print(str); |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 147 | break; |
Antonio Nino Diaz | b3a0a7b | 2016-02-02 12:03:38 +0000 | [diff] [blame] | 148 | case 'p': |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 149 | unum = (uintptr_t)va_arg(args, void *); |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 150 | if (unum > 0U) { |
Antonio Nino Diaz | c0c8eb6 | 2018-08-15 17:02:28 +0100 | [diff] [blame] | 151 | count += string_print("0x"); |
Antonio Nino Diaz | 18c7353 | 2017-12-15 10:36:20 +0000 | [diff] [blame] | 152 | padn -= 2; |
| 153 | } |
Antonio Nino Diaz | b3a0a7b | 2016-02-02 12:03:38 +0000 | [diff] [blame] | 154 | |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 155 | count += unsigned_num_print(unum, 16, |
Matt Schulte | 50851f0 | 2023-07-13 11:10:32 -0700 | [diff] [blame] | 156 | padc, padn, uppercase); |
Antonio Nino Diaz | b3a0a7b | 2016-02-02 12:03:38 +0000 | [diff] [blame] | 157 | break; |
Matt Schulte | 50851f0 | 2023-07-13 11:10:32 -0700 | [diff] [blame] | 158 | case 'X': |
| 159 | uppercase = true; |
| 160 | // fall through |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 161 | case 'x': |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 162 | unum = get_unum_va_args(args, l_count); |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 163 | count += unsigned_num_print(unum, 16, |
Matt Schulte | 50851f0 | 2023-07-13 11:10:32 -0700 | [diff] [blame] | 164 | padc, padn, uppercase); |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 165 | break; |
Scott Branden | 57bd75c | 2016-03-23 13:37:33 -0700 | [diff] [blame] | 166 | case 'z': |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 167 | if (sizeof(size_t) == 8U) |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 168 | l_count = 2; |
| 169 | |
Scott Branden | 57bd75c | 2016-03-23 13:37:33 -0700 | [diff] [blame] | 170 | fmt++; |
| 171 | goto loop; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 172 | case 'l': |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 173 | l_count++; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 174 | fmt++; |
| 175 | goto loop; |
| 176 | case 'u': |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 177 | unum = get_unum_va_args(args, l_count); |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 178 | count += unsigned_num_print(unum, 10, |
Matt Schulte | 50851f0 | 2023-07-13 11:10:32 -0700 | [diff] [blame] | 179 | padc, padn, uppercase); |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 180 | break; |
Antonio Nino Diaz | 18c7353 | 2017-12-15 10:36:20 +0000 | [diff] [blame] | 181 | case '0': |
| 182 | padc = '0'; |
| 183 | padn = 0; |
| 184 | fmt++; |
| 185 | |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 186 | for (;;) { |
Antonio Nino Diaz | 18c7353 | 2017-12-15 10:36:20 +0000 | [diff] [blame] | 187 | char ch = *fmt; |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 188 | if ((ch < '0') || (ch > '9')) { |
Antonio Nino Diaz | 18c7353 | 2017-12-15 10:36:20 +0000 | [diff] [blame] | 189 | goto loop; |
| 190 | } |
| 191 | padn = (padn * 10) + (ch - '0'); |
| 192 | fmt++; |
| 193 | } |
Daniel Boulby | 8942a1b | 2018-06-22 14:16:03 +0100 | [diff] [blame] | 194 | assert(0); /* Unreachable */ |
Juan Pablo Conde | 746c0af | 2023-11-08 13:03:53 -0600 | [diff] [blame] | 195 | case '1': |
| 196 | case '2': |
| 197 | case '3': |
| 198 | case '4': |
| 199 | case '5': |
| 200 | case '6': |
| 201 | case '7': |
| 202 | case '8': |
| 203 | case '9': |
| 204 | padc = ' '; |
| 205 | padn = 0; |
| 206 | |
| 207 | for (;;) { |
| 208 | char ch = *fmt; |
| 209 | if ((ch < '0') || (ch > '9')) { |
| 210 | goto loop; |
| 211 | } |
| 212 | padn = (padn * 10) + (ch - '0'); |
| 213 | fmt++; |
| 214 | } |
| 215 | assert(0); /* Unreachable */ |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 216 | default: |
| 217 | /* Exit on any other format specifier */ |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 218 | return -1; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 219 | } |
| 220 | fmt++; |
| 221 | continue; |
| 222 | } |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 223 | (void)putchar(*fmt); |
| 224 | fmt++; |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 225 | count++; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 226 | } |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 227 | |
| 228 | return count; |
Soby Mathew | f583a06 | 2017-09-04 11:45:52 +0100 | [diff] [blame] | 229 | } |
| 230 | |
Antonio Nino Diaz | c0c8eb6 | 2018-08-15 17:02:28 +0100 | [diff] [blame] | 231 | int printf(const char *fmt, ...) |
Soby Mathew | f583a06 | 2017-09-04 11:45:52 +0100 | [diff] [blame] | 232 | { |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 233 | int count; |
Soby Mathew | f583a06 | 2017-09-04 11:45:52 +0100 | [diff] [blame] | 234 | va_list va; |
| 235 | |
| 236 | va_start(va, fmt); |
Antonio Nino Diaz | c0c8eb6 | 2018-08-15 17:02:28 +0100 | [diff] [blame] | 237 | count = vprintf(fmt, va); |
Soby Mathew | f583a06 | 2017-09-04 11:45:52 +0100 | [diff] [blame] | 238 | va_end(va); |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 239 | |
| 240 | return count; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 241 | } |