blob: 1ac111b2adc52288d52cdc10f825ea55b5eb2b9b [file] [log] [blame]
Sylvain Munaut26bc4652020-09-14 10:19:49 +02001/*
2 * utils.c
3 *
4 * Copyright (C) 2019-2020 Sylvain Munaut <tnt@246tNt.com>
5 * SPDX-License-Identifier: LGPL-3.0-or-later
6 */
7
Sylvain Munaut7cfe0172022-01-09 16:52:46 +01008#include <stdarg.h>
Sylvain Munaut26bc4652020-09-14 10:19:49 +02009#include <stdint.h>
10#include <stdbool.h>
11
Sylvain Munaut7cfe0172022-01-09 16:52:46 +010012#include "console.h"
13#include "led.h"
14#include "mini-printf.h"
15#include "misc.h"
16
Sylvain Munaut26bc4652020-09-14 10:19:49 +020017char *
18hexstr(void *d, int n, bool space)
19{
20 static const char * const hex = "0123456789abcdef";
21 static char buf[96];
22 uint8_t *p = d;
23 char *s = buf;
24 char c;
25
26 while (n--) {
27 c = *p++;
28 *s++ = hex[c >> 4];
29 *s++ = hex[c & 0xf];
30 if (space)
31 *s++ = ' ';
32 }
33
34 s[space?-1:0] = '\0';
35
36 return buf;
37}
Sylvain Munaut7cfe0172022-01-09 16:52:46 +010038
39
40void
41_panic(const char *file, int lineno, const char *fmt, ...)
42{
43 char buf[256];
44 va_list va;
45 int l;
46
47 /* Fast hard red blinking led */
48 led_state(true);
49 led_color(255, 0, 8);
50 led_breathe(false, 0, 0);
51 led_blink(true, 75, 75);
52
53 /* Prepare buffer */
54 l = mini_snprintf(buf, 255, "PANIC @ %s:%d = ", file, lineno);
55
56 va_start(va, fmt);
57 l += mini_vsnprintf(buf+l, 255-l, fmt, va);
58 va_end(va);
59
60 buf[l] = '\n';
61 buf[l+1] = '\0';
62
63 /* Print once */
64 puts(buf);
65
66 /* Loop waiting for commands */
67 while (1) {
68 int cmd = getchar_nowait();
69
70 switch (cmd) {
71 case 'b':
72 /* Reboot */
73 reboot(2);
74 break;
75 case ' ':
76 /* Print error again */
77 puts(buf);
78 break;
79 }
80 }
81}