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