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 | * |
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 | |
| 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))) |
| 21 | |
| 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))) |
| 25 | |
| 26 | static void string_print(const char *str) |
| 27 | { |
| 28 | while (*str) |
| 29 | putchar(*str++); |
| 30 | } |
| 31 | |
| 32 | 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] | 33 | { |
| 34 | /* Just need enough space to store 64 bit decimal integer */ |
| 35 | unsigned char num_buf[20]; |
Sandrine Bailleux | a64a854 | 2015-03-05 10:54:34 +0000 | [diff] [blame] | 36 | int i = 0, rem; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 37 | |
| 38 | do { |
| 39 | rem = unum % radix; |
| 40 | if (rem < 0xa) |
| 41 | num_buf[i++] = '0' + rem; |
| 42 | else |
| 43 | num_buf[i++] = 'a' + (rem - 0xa); |
| 44 | } while (unum /= radix); |
| 45 | |
| 46 | while (--i >= 0) |
| 47 | putchar(num_buf[i]); |
| 48 | } |
| 49 | |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 50 | /******************************************************************* |
| 51 | * Reduced format print for Trusted firmware. |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 52 | * The following type specifiers are supported by this print |
| 53 | * %x - hexadecimal format |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 54 | * %s - string format |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 55 | * %d or %i - signed decimal format |
| 56 | * %u - unsigned decimal format |
Antonio Nino Diaz | b3a0a7b | 2016-02-02 12:03:38 +0000 | [diff] [blame] | 57 | * %p - pointer format |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 58 | * |
| 59 | * The following length specifiers are supported by this print |
| 60 | * %l - long int (64-bit on AArch64) |
| 61 | * %ll - long long int (64-bit on AArch64) |
| 62 | * %z - size_t sized integer formats (64 bit on AArch64) |
| 63 | * |
| 64 | * The print exits on all other formats specifiers other than valid |
| 65 | * combinations of the above specifiers. |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 66 | *******************************************************************/ |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 67 | void tf_printf(const char *fmt, ...) |
| 68 | { |
| 69 | va_list args; |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 70 | int l_count; |
| 71 | long long int num; |
| 72 | unsigned long long int unum; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 73 | char *str; |
| 74 | |
| 75 | va_start(args, fmt); |
| 76 | while (*fmt) { |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 77 | l_count = 0; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 78 | |
| 79 | if (*fmt == '%') { |
| 80 | fmt++; |
| 81 | /* Check the format specifier */ |
| 82 | loop: |
| 83 | switch (*fmt) { |
| 84 | case 'i': /* Fall through to next one */ |
| 85 | case 'd': |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 86 | num = get_num_va_args(args, l_count); |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 87 | if (num < 0) { |
| 88 | putchar('-'); |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 89 | unum = (unsigned long long int)-num; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 90 | } else |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 91 | unum = (unsigned long long int)num; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 92 | |
| 93 | unsigned_num_print(unum, 10); |
| 94 | break; |
| 95 | case 's': |
| 96 | str = va_arg(args, char *); |
| 97 | string_print(str); |
| 98 | break; |
Antonio Nino Diaz | b3a0a7b | 2016-02-02 12:03:38 +0000 | [diff] [blame] | 99 | case 'p': |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 100 | unum = (uintptr_t)va_arg(args, void *); |
Antonio Nino Diaz | b3a0a7b | 2016-02-02 12:03:38 +0000 | [diff] [blame] | 101 | if (unum) |
| 102 | string_print("0x"); |
| 103 | |
| 104 | unsigned_num_print(unum, 16); |
| 105 | break; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 106 | case 'x': |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 107 | unum = get_unum_va_args(args, l_count); |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 108 | unsigned_num_print(unum, 16); |
| 109 | break; |
Scott Branden | 57bd75c | 2016-03-23 13:37:33 -0700 | [diff] [blame] | 110 | case 'z': |
| 111 | if (sizeof(size_t) == 8) |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 112 | l_count = 2; |
| 113 | |
Scott Branden | 57bd75c | 2016-03-23 13:37:33 -0700 | [diff] [blame] | 114 | fmt++; |
| 115 | goto loop; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 116 | case 'l': |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 117 | l_count++; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 118 | fmt++; |
| 119 | goto loop; |
| 120 | case 'u': |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 121 | unum = get_unum_va_args(args, l_count); |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 122 | unsigned_num_print(unum, 10); |
| 123 | break; |
| 124 | default: |
| 125 | /* Exit on any other format specifier */ |
| 126 | goto exit; |
| 127 | } |
| 128 | fmt++; |
| 129 | continue; |
| 130 | } |
| 131 | putchar(*fmt++); |
| 132 | } |
| 133 | exit: |
| 134 | va_end(args); |
| 135 | } |