Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 1 | /* |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 2 | * Copyright (c) 2014-2016, ARM Limited and Contributors. All rights reserved. |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 7 | * Redistributions of source code must retain the above copyright notice, this |
| 8 | * list of conditions and the following disclaimer. |
| 9 | * |
| 10 | * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * Neither the name of ARM nor the names of its contributors may be used |
| 15 | * to endorse or promote products derived from this software without specific |
| 16 | * prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | * POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
Soby Mathew | 0691d97 | 2016-05-05 12:34:41 +0100 | [diff] [blame] | 30 | #include <arch.h> |
| 31 | #include <arch_helpers.h> |
| 32 | #include <assert.h> |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 33 | #include <debug.h> |
Soby Mathew | 0691d97 | 2016-05-05 12:34:41 +0100 | [diff] [blame] | 34 | #include <limits.h> |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 35 | #include <stdarg.h> |
| 36 | #include <stdint.h> |
| 37 | |
| 38 | /*********************************************************** |
| 39 | * The tf_printf implementation for all BL stages |
| 40 | ***********************************************************/ |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 41 | |
| 42 | #define get_num_va_args(args, lcount) \ |
| 43 | (((lcount) > 1) ? va_arg(args, long long int) : \ |
| 44 | ((lcount) ? va_arg(args, long int) : va_arg(args, int))) |
| 45 | |
| 46 | #define get_unum_va_args(args, lcount) \ |
| 47 | (((lcount) > 1) ? va_arg(args, unsigned long long int) : \ |
| 48 | ((lcount) ? va_arg(args, unsigned long int) : va_arg(args, unsigned int))) |
| 49 | |
| 50 | static void string_print(const char *str) |
| 51 | { |
| 52 | while (*str) |
| 53 | putchar(*str++); |
| 54 | } |
| 55 | |
Soby Mathew | 0691d97 | 2016-05-05 12:34:41 +0100 | [diff] [blame] | 56 | #ifdef AARCH32 |
| 57 | #define unsigned_num_print(unum, radix) \ |
| 58 | do { \ |
| 59 | if ((radix) == 16) \ |
| 60 | unsigned_hex_print(unum); \ |
| 61 | else if ((radix) == 10) \ |
| 62 | unsigned_dec_print(unum); \ |
| 63 | else \ |
| 64 | string_print("tf_printf : Unsupported radix");\ |
| 65 | } while (0); |
| 66 | |
| 67 | /* |
| 68 | * Utility function to print an unsigned number in decimal format for AArch32. |
| 69 | * The function doesn't support printing decimal integers higher than 32 bits |
| 70 | * to avoid having to implement 64-bit integer compiler library functions. |
| 71 | */ |
| 72 | static void unsigned_dec_print(unsigned long long int unum) |
| 73 | { |
| 74 | unsigned int local_num; |
| 75 | /* Just need enough space to store 32 bit decimal integer */ |
| 76 | unsigned char num_buf[10]; |
| 77 | int i = 0, rem; |
| 78 | |
| 79 | if (unum > UINT_MAX) { |
| 80 | string_print("tf_printf : decimal numbers higher than 32 bits" |
| 81 | " not supported\n"); |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | local_num = (unsigned int)unum; |
| 86 | |
| 87 | do { |
| 88 | rem = local_num % 10; |
| 89 | num_buf[i++] = '0' + rem; |
| 90 | } while (local_num /= 10); |
| 91 | |
| 92 | while (--i >= 0) |
| 93 | putchar(num_buf[i]); |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Utility function to print an unsigned number in hexadecimal format for |
| 98 | * AArch32. The function doesn't use 64-bit integer arithmetic to avoid |
| 99 | * having to implement 64-bit compiler library functions. It splits the |
| 100 | * 64 bit number into two 32 bit numbers and converts them into equivalent |
| 101 | * ASCII characters. |
| 102 | */ |
| 103 | static void unsigned_hex_print(unsigned long long int unum) |
| 104 | { |
| 105 | /* Just need enough space to store 16 characters */ |
| 106 | unsigned char num_buf[16]; |
| 107 | int i = 0, rem; |
| 108 | uint32_t num_local = 0, num_msb = 0; |
| 109 | |
| 110 | /* Get the LSB of 64 bit unum */ |
| 111 | num_local = (uint32_t)unum; |
| 112 | /* Get the MSB of 64 bit unum. This works only on Little Endian */ |
| 113 | assert((read_sctlr() & SCTLR_EE_BIT) == 0); |
| 114 | num_msb = *(((uint32_t *) &unum) + 1); |
| 115 | |
| 116 | do { |
| 117 | do { |
| 118 | rem = (num_local & 0xf); |
| 119 | if (rem < 0xa) |
| 120 | num_buf[i++] = '0' + rem; |
| 121 | else |
| 122 | num_buf[i++] = 'a' + (rem - 0xa); |
| 123 | } while (num_local >>= 4); |
| 124 | |
| 125 | num_local = num_msb; |
| 126 | num_msb = 0; |
| 127 | } while (num_local); |
| 128 | |
| 129 | while (--i >= 0) |
| 130 | putchar(num_buf[i]); |
| 131 | } |
| 132 | |
| 133 | #else |
| 134 | |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 135 | static void unsigned_num_print(unsigned long long int unum, unsigned int radix) |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 136 | { |
| 137 | /* Just need enough space to store 64 bit decimal integer */ |
| 138 | unsigned char num_buf[20]; |
Sandrine Bailleux | a64a854 | 2015-03-05 10:54:34 +0000 | [diff] [blame] | 139 | int i = 0, rem; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 140 | |
| 141 | do { |
| 142 | rem = unum % radix; |
| 143 | if (rem < 0xa) |
| 144 | num_buf[i++] = '0' + rem; |
| 145 | else |
| 146 | num_buf[i++] = 'a' + (rem - 0xa); |
| 147 | } while (unum /= radix); |
| 148 | |
| 149 | while (--i >= 0) |
| 150 | putchar(num_buf[i]); |
| 151 | } |
Soby Mathew | 0691d97 | 2016-05-05 12:34:41 +0100 | [diff] [blame] | 152 | #endif /* AARCH32 */ |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 153 | |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 154 | /******************************************************************* |
| 155 | * Reduced format print for Trusted firmware. |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 156 | * The following type specifiers are supported by this print |
| 157 | * %x - hexadecimal format |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 158 | * %s - string format |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 159 | * %d or %i - signed decimal format |
| 160 | * %u - unsigned decimal format |
Antonio Nino Diaz | b3a0a7b | 2016-02-02 12:03:38 +0000 | [diff] [blame] | 161 | * %p - pointer format |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 162 | * |
| 163 | * The following length specifiers are supported by this print |
| 164 | * %l - long int (64-bit on AArch64) |
| 165 | * %ll - long long int (64-bit on AArch64) |
| 166 | * %z - size_t sized integer formats (64 bit on AArch64) |
| 167 | * |
| 168 | * The print exits on all other formats specifiers other than valid |
| 169 | * combinations of the above specifiers. |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 170 | *******************************************************************/ |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 171 | void tf_printf(const char *fmt, ...) |
| 172 | { |
| 173 | va_list args; |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 174 | int l_count; |
| 175 | long long int num; |
| 176 | unsigned long long int unum; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 177 | char *str; |
| 178 | |
| 179 | va_start(args, fmt); |
| 180 | while (*fmt) { |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 181 | l_count = 0; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 182 | |
| 183 | if (*fmt == '%') { |
| 184 | fmt++; |
| 185 | /* Check the format specifier */ |
| 186 | loop: |
| 187 | switch (*fmt) { |
| 188 | case 'i': /* Fall through to next one */ |
| 189 | case 'd': |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 190 | num = get_num_va_args(args, l_count); |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 191 | if (num < 0) { |
| 192 | putchar('-'); |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 193 | unum = (unsigned long long int)-num; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 194 | } else |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 195 | unum = (unsigned long long int)num; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 196 | |
| 197 | unsigned_num_print(unum, 10); |
| 198 | break; |
| 199 | case 's': |
| 200 | str = va_arg(args, char *); |
| 201 | string_print(str); |
| 202 | break; |
Antonio Nino Diaz | b3a0a7b | 2016-02-02 12:03:38 +0000 | [diff] [blame] | 203 | case 'p': |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 204 | unum = (uintptr_t)va_arg(args, void *); |
Antonio Nino Diaz | b3a0a7b | 2016-02-02 12:03:38 +0000 | [diff] [blame] | 205 | if (unum) |
| 206 | string_print("0x"); |
| 207 | |
| 208 | unsigned_num_print(unum, 16); |
| 209 | break; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 210 | case 'x': |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 211 | unum = get_unum_va_args(args, l_count); |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 212 | unsigned_num_print(unum, 16); |
| 213 | break; |
Scott Branden | 57bd75c | 2016-03-23 13:37:33 -0700 | [diff] [blame] | 214 | case 'z': |
| 215 | if (sizeof(size_t) == 8) |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 216 | l_count = 2; |
| 217 | |
Scott Branden | 57bd75c | 2016-03-23 13:37:33 -0700 | [diff] [blame] | 218 | fmt++; |
| 219 | goto loop; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 220 | case 'l': |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 221 | l_count++; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 222 | fmt++; |
| 223 | goto loop; |
| 224 | case 'u': |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 225 | unum = get_unum_va_args(args, l_count); |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 226 | unsigned_num_print(unum, 10); |
| 227 | break; |
| 228 | default: |
| 229 | /* Exit on any other format specifier */ |
| 230 | goto exit; |
| 231 | } |
| 232 | fmt++; |
| 233 | continue; |
| 234 | } |
| 235 | putchar(*fmt++); |
| 236 | } |
| 237 | exit: |
| 238 | va_end(args); |
| 239 | } |