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 | |
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 | |
| 34 | 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] | 35 | { |
| 36 | /* Just need enough space to store 64 bit decimal integer */ |
| 37 | unsigned char num_buf[20]; |
Sandrine Bailleux | a64a854 | 2015-03-05 10:54:34 +0000 | [diff] [blame] | 38 | int i = 0, rem; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 39 | |
| 40 | do { |
| 41 | rem = unum % radix; |
| 42 | if (rem < 0xa) |
| 43 | num_buf[i++] = '0' + rem; |
| 44 | else |
| 45 | num_buf[i++] = 'a' + (rem - 0xa); |
| 46 | } while (unum /= radix); |
| 47 | |
| 48 | while (--i >= 0) |
| 49 | putchar(num_buf[i]); |
| 50 | } |
| 51 | |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 52 | /******************************************************************* |
| 53 | * Reduced format print for Trusted firmware. |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 54 | * The following type specifiers are supported by this print |
| 55 | * %x - hexadecimal format |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 56 | * %s - string format |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 57 | * %d or %i - signed decimal format |
| 58 | * %u - unsigned decimal format |
Antonio Nino Diaz | b3a0a7b | 2016-02-02 12:03:38 +0000 | [diff] [blame] | 59 | * %p - pointer format |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 60 | * |
| 61 | * The following length specifiers are supported by this print |
| 62 | * %l - long int (64-bit on AArch64) |
| 63 | * %ll - long long int (64-bit on AArch64) |
| 64 | * %z - size_t sized integer formats (64 bit on AArch64) |
| 65 | * |
| 66 | * The print exits on all other formats specifiers other than valid |
| 67 | * combinations of the above specifiers. |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 68 | *******************************************************************/ |
Soby Mathew | f583a06 | 2017-09-04 11:45:52 +0100 | [diff] [blame] | 69 | void tf_vprintf(const char *fmt, va_list args) |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 70 | { |
Soby Mathew | f62d546 | 2016-03-22 17:38:00 +0000 | [diff] [blame] | 71 | int l_count; |
| 72 | long long int num; |
| 73 | unsigned long long int unum; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 74 | char *str; |
| 75 | |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 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 *); |
Soby Mathew | f583a06 | 2017-09-04 11:45:52 +0100 | [diff] [blame] | 97 | tf_string_print(str); |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 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) |
Soby Mathew | f583a06 | 2017-09-04 11:45:52 +0100 | [diff] [blame] | 102 | tf_string_print("0x"); |
Antonio Nino Diaz | b3a0a7b | 2016-02-02 12:03:38 +0000 | [diff] [blame] | 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 */ |
Soby Mathew | f583a06 | 2017-09-04 11:45:52 +0100 | [diff] [blame] | 126 | return; |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 127 | } |
| 128 | fmt++; |
| 129 | continue; |
| 130 | } |
| 131 | putchar(*fmt++); |
| 132 | } |
Soby Mathew | f583a06 | 2017-09-04 11:45:52 +0100 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void tf_printf(const char *fmt, ...) |
| 136 | { |
| 137 | va_list va; |
| 138 | |
| 139 | va_start(va, fmt); |
| 140 | tf_vprintf(fmt, va); |
| 141 | va_end(va); |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 142 | } |