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