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