b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | #include "print.h" |
| 2 | #include "serial.h" |
| 3 | #include "PlatformConfig.h" |
| 4 | extern int ModOfTwoNumbers(int Numerator, int Denominator); |
| 5 | extern int DivideTwoNumbers(int Numerator, int Denominator); |
| 6 | |
| 7 | static volatile unsigned char quiet = 0; |
| 8 | |
| 9 | #if CONFIG_SAVE_LOG_TO_MEM |
| 10 | static boot_log_t *pboot_log = (boot_log_t *)CONFIG_BOOT_LOG_ADDR; |
| 11 | |
| 12 | static void boot_log_write_mem(char *text, int size) |
| 13 | { |
| 14 | uint32_t bufsize; |
| 15 | static int boot_log_inited = -1; |
| 16 | |
| 17 | if (PlatformCheckEMMDStatus()) |
| 18 | return; |
| 19 | |
| 20 | bufsize = CONFIG_BOOT_LOG_SIZE - sizeof(pboot_log->buf_pos) - sizeof(pboot_log->magic); |
| 21 | if (boot_log_inited == -1) { |
| 22 | pboot_log->buf_pos = 0; |
| 23 | pboot_log->magic = BOOT_LOG_MAGIC; |
| 24 | boot_log_inited = 1; |
| 25 | } |
| 26 | |
| 27 | if (pboot_log->buf) { |
| 28 | if (pboot_log->buf_pos >= bufsize) |
| 29 | pboot_log->buf_pos = pboot_log->buf_pos % bufsize; |
| 30 | |
| 31 | if ((size >= bufsize)) |
| 32 | size = size % bufsize; |
| 33 | |
| 34 | if ((size + pboot_log->buf_pos <= bufsize)) |
| 35 | memcpy(&pboot_log->buf[pboot_log->buf_pos], text, size); |
| 36 | else { |
| 37 | unsigned int first = bufsize - pboot_log->buf_pos; |
| 38 | unsigned int second = size - first; |
| 39 | memcpy(&pboot_log->buf[pboot_log->buf_pos], text, first); |
| 40 | memcpy(&pboot_log->buf[pboot_log->buf_pos], text + first, second); |
| 41 | } |
| 42 | |
| 43 | pboot_log->buf_pos += size; /* Check overflow */ |
| 44 | if ((pboot_log->buf_pos >= bufsize)) |
| 45 | pboot_log->buf_pos -= size; |
| 46 | } |
| 47 | } |
| 48 | #endif |
| 49 | |
| 50 | static void normal_print_char(int c) |
| 51 | { |
| 52 | #if CONFIG_SAVE_LOG_TO_MEM |
| 53 | boot_log_write_mem((char *)&c, 1); |
| 54 | #endif |
| 55 | #if USE_SERIAL_DEBUG |
| 56 | (void) serial_write (c); |
| 57 | #endif |
| 58 | } |
| 59 | |
| 60 | static void quiet_print_char(int c) |
| 61 | { |
| 62 | #if CONFIG_SAVE_LOG_TO_MEM |
| 63 | boot_log_write_mem((char *)&c, 1); |
| 64 | #else |
| 65 | void(c); |
| 66 | #endif |
| 67 | } |
| 68 | |
| 69 | typedef void (*print_char)(int); |
| 70 | |
| 71 | print_char log_print_char = normal_print_char; |
| 72 | |
| 73 | int serial_get_quiet(void) |
| 74 | { |
| 75 | return quiet; |
| 76 | } |
| 77 | |
| 78 | void serial_set_quiet(int on) |
| 79 | { |
| 80 | quiet = on; |
| 81 | if (quiet) { |
| 82 | log_print_char = quiet_print_char; |
| 83 | } |
| 84 | else { |
| 85 | log_print_char = normal_print_char; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | static void printchar (char **str, int c) |
| 90 | { |
| 91 | if (str) { |
| 92 | **str = c; |
| 93 | ++(*str); |
| 94 | } |
| 95 | else { |
| 96 | (*log_print_char)(c); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | #define PAD_RIGHT 1 |
| 101 | #define PAD_ZERO 2 |
| 102 | |
| 103 | static int prints (char **out, const char *string, int width, int pad) |
| 104 | { |
| 105 | register int pc = 0, padchar = ' '; |
| 106 | if (width > 0) { |
| 107 | int len = 0; |
| 108 | const char *ptr; |
| 109 | for (ptr = string; *ptr; ++ptr) |
| 110 | |
| 111 | ++len; |
| 112 | if (len >= width) |
| 113 | width = 0; |
| 114 | else |
| 115 | width -= len; |
| 116 | if (pad & PAD_ZERO) |
| 117 | padchar = '0'; |
| 118 | } |
| 119 | if (!(pad & PAD_RIGHT)) { |
| 120 | for (; width > 0; --width) { |
| 121 | printchar (out, padchar); |
| 122 | ++pc; |
| 123 | } |
| 124 | } |
| 125 | for (; *string; ++string) { |
| 126 | printchar (out, *string); |
| 127 | ++pc; |
| 128 | } |
| 129 | for (; width > 0; --width) { |
| 130 | printchar (out, padchar); |
| 131 | ++pc; |
| 132 | } |
| 133 | return pc; |
| 134 | } |
| 135 | |
| 136 | #define PRINT_BUF_LEN 20 |
| 137 | static int printi (char **out, long long i, int b, int sg, int width, int pad, int letbase) |
| 138 | { |
| 139 | char print_buf[PRINT_BUF_LEN]; |
| 140 | char *s; |
| 141 | int t, neg = 0, pc = 0; |
| 142 | unsigned long long u = (unsigned long long) i; |
| 143 | if (i == 0) { |
| 144 | print_buf[0] = '0'; |
| 145 | print_buf[1] = '\0'; |
| 146 | return prints (out, print_buf, width, pad); |
| 147 | } |
| 148 | if (sg && b == 10 && i < 0) { |
| 149 | neg = 1; |
| 150 | u = (unsigned long long) -i; |
| 151 | } |
| 152 | s = print_buf + PRINT_BUF_LEN - 1; |
| 153 | *s = '\0'; |
| 154 | while (u) { |
| 155 | //t = u % b; //lint !e573 Warning 573: Signed-unsigned mix with divide |
| 156 | t = ModOfTwoNumbers(u, b); |
| 157 | if (t >= 10) |
| 158 | t += letbase - '0' - 10; |
| 159 | *--s = t + '0'; |
| 160 | //u /= b; //lint !e573 Warning 573: Signed-unsigned mix with divide |
| 161 | u = DivideTwoNumbers(u, b); |
| 162 | } |
| 163 | if (neg) { |
| 164 | if (width && (pad & PAD_ZERO)) { |
| 165 | printchar (out, '-'); |
| 166 | ++pc; |
| 167 | --width; |
| 168 | } |
| 169 | else { |
| 170 | *--s = '-'; |
| 171 | } |
| 172 | } |
| 173 | return pc + prints (out, s, width, pad); |
| 174 | } |
| 175 | |
| 176 | int print (char **out, int *varg, int align) |
| 177 | { |
| 178 | int post_decimal ; |
| 179 | int width, pad, l_num; |
| 180 | unsigned dec_width = 0 ; |
| 181 | int pc = 0; |
| 182 | int varg_bk = (int)(varg - align); |
| 183 | char *format = (char *) (*varg++); |
| 184 | char scr[2]; |
| 185 | for (; *format != 0; ++format) { |
| 186 | if (*format == '%') { |
| 187 | ++format; |
| 188 | width = pad = 0; |
| 189 | l_num = 0; |
| 190 | if (*format == '\0') |
| 191 | break; |
| 192 | if (*format == '%') |
| 193 | goto out; |
| 194 | if (*format == '-') { |
| 195 | ++format; |
| 196 | pad = PAD_RIGHT; |
| 197 | } |
| 198 | while (*format == '0') { |
| 199 | ++format; |
| 200 | pad |= PAD_ZERO; |
| 201 | } |
| 202 | post_decimal = 0 ; |
| 203 | if (*format == '.' || |
| 204 | (*format >= '0' && *format <= '9') || *format == '*') |
| 205 | { |
| 206 | |
| 207 | while (1) { |
| 208 | if (*format == '.') { |
| 209 | post_decimal = 1 ; |
| 210 | dec_width = 0 ; |
| 211 | format++ ; |
| 212 | } else if ((*format >= '0' && *format <= '9')) { |
| 213 | if (post_decimal) { |
| 214 | dec_width *= 10; |
| 215 | dec_width += *format - '0'; |
| 216 | } else { |
| 217 | width *= 10; |
| 218 | width += *format - '0'; |
| 219 | } |
| 220 | format++ ; |
| 221 | } else if(*format == '*'){ |
| 222 | if(post_decimal){ |
| 223 | dec_width = (int)(*varg++); |
| 224 | } |
| 225 | else |
| 226 | width = (int)(*varg++); |
| 227 | format++ ; |
| 228 | } else { |
| 229 | break; |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | while (*format == 'l' || *format == 'L') { |
| 234 | ++format; |
| 235 | ++l_num; |
| 236 | } |
| 237 | switch (*format) { |
| 238 | case 's': |
| 239 | { |
| 240 | char *s = *((char **) varg++); //lint !e740 |
| 241 | pc += prints (out, s ? s : "(null)", width, pad); |
| 242 | } |
| 243 | break; |
| 244 | case 'd': |
| 245 | case 'i': |
| 246 | if (l_num > 1) { |
| 247 | long long *llptr = (long long *)((((int)(varg) +7 -varg_bk) & (~7)) + varg_bk); |
| 248 | pc += printi (out, *llptr, 10, 1, width, pad, 'a'); |
| 249 | varg = (int *)(llptr + 1); |
| 250 | } else { |
| 251 | pc += printi (out, (long long)(*varg++), 10, 1, width, pad, 'a'); |
| 252 | } |
| 253 | break; |
| 254 | case 'x': |
| 255 | if (l_num > 1) { |
| 256 | long long *llptr = (long long *)((((int)(varg) +7 -varg_bk) & (~7)) + varg_bk); |
| 257 | pc += printi (out, *llptr, 16, 0, width, pad, 'a'); |
| 258 | varg = (int *)(llptr + 1); |
| 259 | } else { |
| 260 | pc += printi (out, (long long)((unsigned int)(*varg++)), 16, 0, width, pad, 'a'); |
| 261 | } |
| 262 | break; |
| 263 | case 'X': |
| 264 | if (l_num > 1) { |
| 265 | long long *llptr = (long long *)((((int)(varg) +7 -varg_bk) & (~7)) + varg_bk); |
| 266 | pc += printi (out, *llptr, 16, 0, width, pad, 'A'); |
| 267 | varg = (int *)(llptr + 1); |
| 268 | } else { |
| 269 | pc += printi (out, (long long)((unsigned int)(*varg++)), 16, 0, width, pad, 'A'); |
| 270 | } |
| 271 | break; |
| 272 | case 'u': |
| 273 | if (l_num > 1) { |
| 274 | long long *llptr = (long long *)((((int)(varg) +7 -varg_bk) & (~7)) + varg_bk); |
| 275 | pc += printi (out, *llptr, 10, 0, width, pad, 'a'); |
| 276 | varg = (int *)(llptr + 1); |
| 277 | } else { |
| 278 | pc += printi (out, (long long)((unsigned int)(*varg++)), 10, 0, width, pad, 'a'); |
| 279 | } |
| 280 | break; |
| 281 | case 'c': |
| 282 | /* char are converted to int then pushed on the stack */ |
| 283 | scr[0] = *varg++; |
| 284 | scr[1] = '\0'; |
| 285 | pc += prints (out, scr, width, pad); |
| 286 | break; |
| 287 | default: |
| 288 | printchar (out, '%'); |
| 289 | printchar (out, *format); |
| 290 | break; |
| 291 | } |
| 292 | } |
| 293 | else { |
| 294 | out: |
| 295 | printchar (out, *format); |
| 296 | ++pc; |
| 297 | } |
| 298 | } |
| 299 | if (out) |
| 300 | **out = '\0'; |
| 301 | return pc; |
| 302 | } |
| 303 | |
| 304 | int err_msg(const char *format, ...) |
| 305 | { |
| 306 | print_char saved_print_char; |
| 307 | saved_print_char = log_print_char; |
| 308 | log_print_char = normal_print_char; |
| 309 | int size; |
| 310 | int *varg = (int *) (char *) (&format); |
| 311 | size = print (0, varg, 0); |
| 312 | |
| 313 | log_print_char = saved_print_char; |
| 314 | return size; |
| 315 | } |
| 316 | |
| 317 | int obm_printf(const char *format, ...) |
| 318 | { |
| 319 | int size; |
| 320 | int *varg = (int *) (char *) (&format); |
| 321 | size = print (0, varg, 0); |
| 322 | |
| 323 | return size; |
| 324 | } |
| 325 | |
| 326 | int sprintf(char *str, const char *format, ...) |
| 327 | { |
| 328 | int size; |
| 329 | int *varg = (int *) (char *) (&format); |
| 330 | size = print (&str, varg, 0); |
| 331 | return size; |
| 332 | } |