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