Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 1 | /* |
Heyi Guo | eb250ed | 2020-10-27 08:36:40 +0800 | [diff] [blame] | 2 | * Copyright (c) 2014-2021, 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> |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 10 | #include <stdint.h> |
| 11 | |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 12 | #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 Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 16 | |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 17 | #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 Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 21 | |
Antonio Nino Diaz | c0c8eb6 | 2018-08-15 17:02:28 +0100 | [diff] [blame] | 22 | static int string_print(const char *str) |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 23 | { |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 24 | int count = 0; |
| 25 | |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 26 | assert(str != NULL); |
Soby Mathew | f583a06 | 2017-09-04 11:45:52 +0100 | [diff] [blame] | 27 | |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 28 | for ( ; *str != '\0'; str++) { |
| 29 | (void)putchar(*str); |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 30 | count++; |
| 31 | } |
| 32 | |
| 33 | return count; |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 36 | static int unsigned_num_print(unsigned long long int unum, unsigned int radix, |
| 37 | char padc, int padn) |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 38 | { |
| 39 | /* Just need enough space to store 64 bit decimal integer */ |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 40 | char num_buf[20]; |
| 41 | int i = 0, count = 0; |
| 42 | unsigned int rem; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 43 | |
Claus Pedersen | 785e66c | 2022-09-12 22:42:58 +0000 | [diff] [blame] | 44 | /* num_buf is only large enough for radix >= 10 */ |
| 45 | if (radix < 10) { |
| 46 | assert(0); |
| 47 | return 0; |
| 48 | } |
| 49 | |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 50 | do { |
| 51 | rem = unum % radix; |
| 52 | if (rem < 0xa) |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 53 | num_buf[i] = '0' + rem; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 54 | else |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 55 | num_buf[i] = 'a' + (rem - 0xa); |
| 56 | i++; |
| 57 | unum /= radix; |
| 58 | } while (unum > 0U); |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 59 | |
Antonio Nino Diaz | 18c7353 | 2017-12-15 10:36:20 +0000 | [diff] [blame] | 60 | if (padn > 0) { |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 61 | while (i < padn) { |
| 62 | (void)putchar(padc); |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 63 | count++; |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 64 | padn--; |
Antonio Nino Diaz | 18c7353 | 2017-12-15 10:36:20 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 68 | while (--i >= 0) { |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 69 | (void)putchar(num_buf[i]); |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 70 | count++; |
| 71 | } |
| 72 | |
| 73 | return count; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 74 | } |
| 75 | |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 76 | /******************************************************************* |
| 77 | * Reduced format print for Trusted firmware. |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 78 | * The following type specifiers are supported by this print |
| 79 | * %x - hexadecimal format |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 80 | * %s - string format |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 81 | * %d or %i - signed decimal format |
| 82 | * %u - unsigned decimal format |
Antonio Nino Diaz | b3a0a7b | 2016-02-02 12:03:38 +0000 | [diff] [blame] | 83 | * %p - pointer format |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 84 | * |
| 85 | * The following length specifiers are supported by this print |
| 86 | * %l - long int (64-bit on AArch64) |
| 87 | * %ll - long long int (64-bit on AArch64) |
| 88 | * %z - size_t sized integer formats (64 bit on AArch64) |
| 89 | * |
Antonio Nino Diaz | 18c7353 | 2017-12-15 10:36:20 +0000 | [diff] [blame] | 90 | * The following padding specifiers are supported by this print |
| 91 | * %0NN - Left-pad the number with 0s (NN is a decimal number) |
| 92 | * |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 93 | * The print exits on all other formats specifiers other than valid |
| 94 | * combinations of the above specifiers. |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 95 | *******************************************************************/ |
Antonio Nino Diaz | c0c8eb6 | 2018-08-15 17:02:28 +0100 | [diff] [blame] | 96 | int vprintf(const char *fmt, va_list args) |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 97 | { |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 98 | int l_count; |
| 99 | long long int num; |
| 100 | unsigned long long int unum; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 101 | char *str; |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 102 | char padc = '\0'; /* Padding character */ |
Antonio Nino Diaz | 18c7353 | 2017-12-15 10:36:20 +0000 | [diff] [blame] | 103 | int padn; /* Number of characters to pad */ |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 104 | int count = 0; /* Number of printed characters */ |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 105 | |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 106 | while (*fmt != '\0') { |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 107 | l_count = 0; |
Antonio Nino Diaz | 18c7353 | 2017-12-15 10:36:20 +0000 | [diff] [blame] | 108 | padn = 0; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 109 | |
| 110 | if (*fmt == '%') { |
| 111 | fmt++; |
| 112 | /* Check the format specifier */ |
| 113 | loop: |
| 114 | switch (*fmt) { |
Heyi Guo | eb250ed | 2020-10-27 08:36:40 +0800 | [diff] [blame] | 115 | case '%': |
| 116 | (void)putchar('%'); |
| 117 | break; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 118 | case 'i': /* Fall through to next one */ |
| 119 | case 'd': |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 120 | num = get_num_va_args(args, l_count); |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 121 | if (num < 0) { |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 122 | (void)putchar('-'); |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 123 | unum = (unsigned long long int)-num; |
Antonio Nino Diaz | 18c7353 | 2017-12-15 10:36:20 +0000 | [diff] [blame] | 124 | padn--; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 125 | } else |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 126 | unum = (unsigned long long int)num; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 127 | |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 128 | count += unsigned_num_print(unum, 10, |
| 129 | padc, padn); |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 130 | break; |
| 131 | case 's': |
| 132 | str = va_arg(args, char *); |
Antonio Nino Diaz | c0c8eb6 | 2018-08-15 17:02:28 +0100 | [diff] [blame] | 133 | count += string_print(str); |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 134 | break; |
Antonio Nino Diaz | b3a0a7b | 2016-02-02 12:03:38 +0000 | [diff] [blame] | 135 | case 'p': |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 136 | unum = (uintptr_t)va_arg(args, void *); |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 137 | if (unum > 0U) { |
Antonio Nino Diaz | c0c8eb6 | 2018-08-15 17:02:28 +0100 | [diff] [blame] | 138 | count += string_print("0x"); |
Antonio Nino Diaz | 18c7353 | 2017-12-15 10:36:20 +0000 | [diff] [blame] | 139 | padn -= 2; |
| 140 | } |
Antonio Nino Diaz | b3a0a7b | 2016-02-02 12:03:38 +0000 | [diff] [blame] | 141 | |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 142 | count += unsigned_num_print(unum, 16, |
| 143 | padc, padn); |
Antonio Nino Diaz | b3a0a7b | 2016-02-02 12:03:38 +0000 | [diff] [blame] | 144 | break; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 145 | case 'x': |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 146 | unum = get_unum_va_args(args, l_count); |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 147 | count += unsigned_num_print(unum, 16, |
| 148 | padc, padn); |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 149 | break; |
Scott Branden | 57bd75c | 2016-03-23 13:37:33 -0700 | [diff] [blame] | 150 | case 'z': |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 151 | if (sizeof(size_t) == 8U) |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 152 | l_count = 2; |
| 153 | |
Scott Branden | 57bd75c | 2016-03-23 13:37:33 -0700 | [diff] [blame] | 154 | fmt++; |
| 155 | goto loop; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 156 | case 'l': |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 157 | l_count++; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 158 | fmt++; |
| 159 | goto loop; |
| 160 | case 'u': |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 161 | unum = get_unum_va_args(args, l_count); |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 162 | count += unsigned_num_print(unum, 10, |
| 163 | padc, padn); |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 164 | break; |
Antonio Nino Diaz | 18c7353 | 2017-12-15 10:36:20 +0000 | [diff] [blame] | 165 | case '0': |
| 166 | padc = '0'; |
| 167 | padn = 0; |
| 168 | fmt++; |
| 169 | |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 170 | for (;;) { |
Antonio Nino Diaz | 18c7353 | 2017-12-15 10:36:20 +0000 | [diff] [blame] | 171 | char ch = *fmt; |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 172 | if ((ch < '0') || (ch > '9')) { |
Antonio Nino Diaz | 18c7353 | 2017-12-15 10:36:20 +0000 | [diff] [blame] | 173 | goto loop; |
| 174 | } |
| 175 | padn = (padn * 10) + (ch - '0'); |
| 176 | fmt++; |
| 177 | } |
Daniel Boulby | 8942a1b | 2018-06-22 14:16:03 +0100 | [diff] [blame] | 178 | assert(0); /* Unreachable */ |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 179 | default: |
| 180 | /* Exit on any other format specifier */ |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 181 | return -1; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 182 | } |
| 183 | fmt++; |
| 184 | continue; |
| 185 | } |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 186 | (void)putchar(*fmt); |
| 187 | fmt++; |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 188 | count++; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 189 | } |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 190 | |
| 191 | return count; |
Soby Mathew | f583a06 | 2017-09-04 11:45:52 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Antonio Nino Diaz | c0c8eb6 | 2018-08-15 17:02:28 +0100 | [diff] [blame] | 194 | int printf(const char *fmt, ...) |
Soby Mathew | f583a06 | 2017-09-04 11:45:52 +0100 | [diff] [blame] | 195 | { |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 196 | int count; |
Soby Mathew | f583a06 | 2017-09-04 11:45:52 +0100 | [diff] [blame] | 197 | va_list va; |
| 198 | |
| 199 | va_start(va, fmt); |
Antonio Nino Diaz | c0c8eb6 | 2018-08-15 17:02:28 +0100 | [diff] [blame] | 200 | count = vprintf(fmt, va); |
Soby Mathew | f583a06 | 2017-09-04 11:45:52 +0100 | [diff] [blame] | 201 | va_end(va); |
Antonio Nino Diaz | f440708 | 2018-08-15 16:52:32 +0100 | [diff] [blame] | 202 | |
| 203 | return count; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 204 | } |