b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * (C) Copyright 2000-2002 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
| 8 | #include <config.h> |
| 9 | #include <common.h> |
| 10 | #include <version.h> |
| 11 | #include <linux/ctype.h> |
| 12 | #include <asm/io.h> |
| 13 | |
| 14 | int display_options (void) |
| 15 | { |
| 16 | #if defined(BUILD_TAG) |
| 17 | printf ("\n\n%s, Build: %s\n\n", version_string, BUILD_TAG); |
| 18 | #else |
| 19 | printf ("\n\n%s\n\n", version_string); |
| 20 | #endif |
| 21 | return 0; |
| 22 | } |
| 23 | |
| 24 | /* |
| 25 | * print sizes as "xxx KiB", "xxx.y KiB", "xxx MiB", "xxx.y MiB", |
| 26 | * xxx GiB, xxx.y GiB, etc as needed; allow for optional trailing string |
| 27 | * (like "\n") |
| 28 | */ |
| 29 | void print_size(unsigned long long size, const char *s) |
| 30 | { |
| 31 | unsigned long m = 0, n; |
| 32 | unsigned long long f; |
| 33 | static const char names[] = {'E', 'P', 'T', 'G', 'M', 'K'}; |
| 34 | unsigned long d = 10 * ARRAY_SIZE(names); |
| 35 | char c = 0; |
| 36 | unsigned int i; |
| 37 | |
| 38 | for (i = 0; i < ARRAY_SIZE(names); i++, d -= 10) { |
| 39 | if (size >> d) { |
| 40 | c = names[i]; |
| 41 | break; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | if (!c) { |
| 46 | printf("%llu Bytes%s", size, s); |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | n = size >> d; |
| 51 | f = size & ((1ULL << d) - 1); |
| 52 | |
| 53 | /* If there's a remainder, deal with it */ |
| 54 | if (f) { |
| 55 | m = (10ULL * f + (1ULL << (d - 1))) >> d; |
| 56 | |
| 57 | if (m >= 10) { |
| 58 | m -= 10; |
| 59 | n += 1; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | printf ("%lu", n); |
| 64 | if (m) { |
| 65 | printf (".%ld", m); |
| 66 | } |
| 67 | printf (" %ciB%s", c, s); |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * Print data buffer in hex and ascii form to the terminal. |
| 72 | * |
| 73 | * data reads are buffered so that each memory address is only read once. |
| 74 | * Useful when displaying the contents of volatile registers. |
| 75 | * |
| 76 | * parameters: |
| 77 | * addr: Starting address to display at start of line |
| 78 | * data: pointer to data buffer |
| 79 | * width: data value width. May be 1, 2, or 4. |
| 80 | * count: number of values to display |
| 81 | * linelen: Number of values to print per line; specify 0 for default length |
| 82 | */ |
| 83 | #define MAX_LINE_LENGTH_BYTES (64) |
| 84 | #define DEFAULT_LINE_LENGTH_BYTES (16) |
| 85 | int print_buffer(ulong addr, const void *data, uint width, uint count, |
| 86 | uint linelen) |
| 87 | { |
| 88 | /* linebuf as a union causes proper alignment */ |
| 89 | union linebuf { |
| 90 | uint32_t ui[MAX_LINE_LENGTH_BYTES/sizeof(uint32_t) + 1]; |
| 91 | uint16_t us[MAX_LINE_LENGTH_BYTES/sizeof(uint16_t) + 1]; |
| 92 | uint8_t uc[MAX_LINE_LENGTH_BYTES/sizeof(uint8_t) + 1]; |
| 93 | } lb; |
| 94 | int i; |
| 95 | |
| 96 | if (linelen*width > MAX_LINE_LENGTH_BYTES) |
| 97 | linelen = MAX_LINE_LENGTH_BYTES / width; |
| 98 | if (linelen < 1) |
| 99 | linelen = DEFAULT_LINE_LENGTH_BYTES / width; |
| 100 | |
| 101 | while (count) { |
| 102 | uint thislinelen = linelen; |
| 103 | printf("%08lx:", addr); |
| 104 | |
| 105 | /* check for overflow condition */ |
| 106 | if (count < thislinelen) |
| 107 | thislinelen = count; |
| 108 | |
| 109 | /* Copy from memory into linebuf and print hex values */ |
| 110 | for (i = 0; i < thislinelen; i++) { |
| 111 | uint32_t x; |
| 112 | if (width == 4) |
| 113 | x = lb.ui[i] = *(volatile uint32_t *)data; |
| 114 | else if (width == 2) |
| 115 | x = lb.us[i] = *(volatile uint16_t *)data; |
| 116 | else |
| 117 | x = lb.uc[i] = *(volatile uint8_t *)data; |
| 118 | printf(" %0*x", width * 2, x); |
| 119 | data += width; |
| 120 | } |
| 121 | |
| 122 | while (thislinelen < linelen) { |
| 123 | /* fill line with whitespace for nice ASCII print */ |
| 124 | for (i=0; i<width*2+1; i++) |
| 125 | puts(" "); |
| 126 | linelen--; |
| 127 | } |
| 128 | |
| 129 | /* Print data in ASCII characters */ |
| 130 | for (i = 0; i < thislinelen * width; i++) { |
| 131 | if (!isprint(lb.uc[i]) || lb.uc[i] >= 0x80) |
| 132 | lb.uc[i] = '.'; |
| 133 | } |
| 134 | lb.uc[i] = '\0'; |
| 135 | printf(" %s\n", lb.uc); |
| 136 | |
| 137 | /* update references */ |
| 138 | addr += thislinelen * width; |
| 139 | count -= thislinelen; |
| 140 | |
| 141 | if (ctrlc()) |
| 142 | return -1; |
| 143 | } |
| 144 | |
| 145 | return 0; |
| 146 | } |