/src/barebox/common/misc.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-only |
2 | | /* |
3 | | * (C) Copyright 2000-2003 |
4 | | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
5 | | */ |
6 | | |
7 | | #include <common.h> |
8 | | #include <errno.h> |
9 | | #include <malloc.h> |
10 | | #include <magicvar.h> |
11 | | #include <globalvar.h> |
12 | | #include <environment.h> |
13 | | #include <led.h> |
14 | | #include <linux/ctype.h> |
15 | | #include <of.h> |
16 | | #include <restart.h> |
17 | | #include <poweroff.h> |
18 | | #include <string.h> |
19 | | #include <machine_id.h> |
20 | | #include <linux/stringify.h> |
21 | | |
22 | | int errno; |
23 | | EXPORT_SYMBOL(errno); |
24 | | |
25 | | |
26 | | const char *strerror(int errnum) |
27 | 30.9k | { |
28 | 30.9k | static char errno_string[sizeof("error -2147483648")]; |
29 | | |
30 | 30.9k | switch(errnum) { |
31 | 0 | #ifdef CONFIG_ERRNO_MESSAGES |
32 | 0 | case 0 : return "No error"; |
33 | 0 | case EPERM : return "Operation not permitted"; |
34 | 0 | case ENOENT : return "No such file or directory"; |
35 | 0 | case EIO : return "I/O error"; |
36 | 26.1k | case ENXIO : return "No such device or address"; |
37 | 3 | case E2BIG : return "Arg list too long"; |
38 | 0 | case ENOEXEC : return "Exec format error"; |
39 | 0 | case EBADF : return "Bad file number"; |
40 | 23 | case ENOMEM : return "Out of memory"; |
41 | 1 | case EACCES : return "Permission denied"; |
42 | 0 | case EFAULT : return "Bad address"; |
43 | 0 | case EBUSY : return "Device or resource busy"; |
44 | 0 | case EEXIST : return "File exists"; |
45 | 0 | case ENODEV : return "No such device"; |
46 | 0 | case ENOTDIR : return "Not a directory"; |
47 | 0 | case EISDIR : return "Is a directory"; |
48 | 4.49k | case EINVAL : return "Invalid argument"; |
49 | 0 | case ENOSPC : return "No space left on device"; |
50 | 0 | case ESPIPE : return "Illegal seek"; |
51 | 0 | case EROFS : return "Read-only file system"; |
52 | 0 | case ENAMETOOLONG : return "File name too long"; |
53 | 0 | case ENOSYS : return "Function not implemented"; |
54 | 0 | case ENOTEMPTY : return "Directory not empty"; |
55 | 0 | case EHOSTUNREACH : return "No route to host"; |
56 | 0 | case EINTR : return "Interrupted system call"; |
57 | 0 | case ENETUNREACH : return "Network is unreachable"; |
58 | 0 | case ENETDOWN : return "Network is down"; |
59 | 0 | case ETIMEDOUT : return "Connection timed out"; |
60 | 0 | case EPROBE_DEFER : return "Requested probe deferral"; |
61 | 0 | case ELOOP : return "Too many symbolic links encountered"; |
62 | 157 | case ENODATA : return "No data available"; |
63 | 0 | case EOPNOTSUPP : return "Operation not supported"; |
64 | 0 | case ENOIOCTLCMD : return "Command not found"; |
65 | | #if 0 /* These are probably not needed */ |
66 | | case ENOTBLK : return "Block device required"; |
67 | | case EFBIG : return "File too large"; |
68 | | case EBADSLT : return "Invalid slot"; |
69 | | case ETIME : return "Timer expired"; |
70 | | case ENONET : return "Machine is not on the network"; |
71 | | case EADV : return "Advertise error"; |
72 | | case ECOMM : return "Communication error on send"; |
73 | | case EPROTO : return "Protocol error"; |
74 | | case EBADMSG : return "Not a data message"; |
75 | | case EOVERFLOW : return "Value too large for defined data type"; |
76 | | case EBADFD : return "File descriptor in bad state"; |
77 | | case EREMCHG : return "Remote address changed"; |
78 | | case EMSGSIZE : return "Message too long"; |
79 | | case EPROTOTYPE : return "Protocol wrong type for socket"; |
80 | | case ENOPROTOOPT : return "Protocol not available"; |
81 | | case EPROTONOSUPPORT : return "Protocol not supported"; |
82 | | case ESOCKTNOSUPPORT : return "Socket type not supported"; |
83 | | case EPFNOSUPPORT : return "Protocol family not supported"; |
84 | | case EAFNOSUPPORT : return "Address family not supported by protocol"; |
85 | | case EADDRINUSE : return "Address already in use"; |
86 | | case EADDRNOTAVAIL : return "Cannot assign requested address"; |
87 | | case ENETRESET : return "Network dropped connection because of reset"; |
88 | | case ECONNABORTED : return "Software caused connection abort"; |
89 | | case ECONNRESET : return "Connection reset by peer"; |
90 | | case ENOBUFS : return "No buffer space available"; |
91 | | case ECONNREFUSED : return "Connection refused"; |
92 | | case EHOSTDOWN : return "Host is down"; |
93 | | case EALREADY : return "Operation already in progress"; |
94 | | case EINPROGRESS : return "Operation now in progress"; |
95 | | case ESTALE : return "Stale NFS file handle"; |
96 | | case EISNAM : return "Is a named type file"; |
97 | | case EREMOTEIO : return "Remote I/O error"; |
98 | | #endif |
99 | 0 | #endif |
100 | 97 | default: |
101 | 97 | sprintf(errno_string, "error %d", errnum); |
102 | 97 | return errno_string; |
103 | 30.9k | }; |
104 | 0 | } |
105 | | EXPORT_SYMBOL(strerror); |
106 | | |
107 | | void perror(const char *s) |
108 | 0 | { |
109 | 0 | #ifdef CONFIG_ERRNO_MESSAGES |
110 | 0 | printf("%s: %m\n", s); |
111 | | #else |
112 | | printf("%s returned with %d\n", s, errno); |
113 | | #endif |
114 | 0 | } |
115 | | EXPORT_SYMBOL(perror); |
116 | | |
117 | | static char *model; |
118 | | |
119 | | /* |
120 | | * The model is the verbose name of a board. It can contain |
121 | | * whitespaces, uppercase/lowcer letters, digits, ',', '.' |
122 | | * '-', '_' |
123 | | */ |
124 | | void barebox_set_model(const char *__model) |
125 | 30 | { |
126 | 30 | globalvar_add_simple_string("model", &model); |
127 | | |
128 | 30 | free(model); |
129 | 30 | model = xstrdup(__model); |
130 | 30 | } |
131 | | EXPORT_SYMBOL(barebox_set_model); |
132 | | |
133 | | const char *barebox_get_model(void) |
134 | 30 | { |
135 | 30 | if (model) |
136 | 30 | return model; |
137 | 0 | else |
138 | 0 | return "none"; |
139 | 30 | } |
140 | | EXPORT_SYMBOL(barebox_get_model); |
141 | | |
142 | | BAREBOX_MAGICVAR(global.model, "Product name of this hardware"); |
143 | | |
144 | | static char *hostname; |
145 | | static char *serial_number; |
146 | | static uuid_t product_uuid; |
147 | | |
148 | | /* Note that HOST_NAME_MAX is 64 on Linux */ |
149 | 30 | #define BAREBOX_HOST_NAME_MAX 64 |
150 | | |
151 | | static bool barebox_valid_ldh_char(char c) |
152 | 210 | { |
153 | | /* "LDH" -> "Letters, digits, hyphens", as per RFC 5890, Section 2.3.1 */ |
154 | 210 | return isalnum(c) || c == '-'; |
155 | 210 | } |
156 | | |
157 | | bool barebox_hostname_is_valid(const char *s) |
158 | 30 | { |
159 | 30 | const char *p; |
160 | 30 | bool dot, hyphen; |
161 | | |
162 | | /* |
163 | | * Check if s looks like a valid hostname or FQDN. This does not do full |
164 | | * DNS validation, but only checks if the name is composed of allowed |
165 | | * characters and the length is not above the maximum allowed by Linux. |
166 | | * Doesn't accept empty hostnames, hostnames with leading dots, and |
167 | | * hostnames with multiple dots in a sequence. Doesn't allow hyphens at |
168 | | * the beginning or end of label. |
169 | | */ |
170 | 30 | if (isempty(s)) |
171 | 0 | return false; |
172 | | |
173 | 240 | for (p = s, dot = hyphen = true; *p; p++) { |
174 | 210 | if (*p == '.') { |
175 | 0 | if (dot || hyphen) |
176 | 0 | return false; |
177 | | |
178 | 0 | dot = true; |
179 | 0 | hyphen = false; |
180 | |
|
181 | 210 | } else if (*p == '-') { |
182 | 0 | if (dot) |
183 | 0 | return false; |
184 | | |
185 | 0 | dot = false; |
186 | 0 | hyphen = true; |
187 | |
|
188 | 210 | } else { |
189 | 210 | if (!barebox_valid_ldh_char(*p)) |
190 | 0 | return false; |
191 | | |
192 | 210 | dot = false; |
193 | 210 | hyphen = false; |
194 | 210 | } |
195 | 210 | } |
196 | | |
197 | 30 | if (dot || hyphen) |
198 | 0 | return false; |
199 | | |
200 | 30 | if (p - s > BAREBOX_HOST_NAME_MAX) |
201 | 0 | return false; |
202 | | |
203 | 30 | return true; |
204 | 30 | } |
205 | | |
206 | | /* |
207 | | * The hostname is supposed to be the shortname of a board. It should |
208 | | * contain only letters, numbers, hyphens. No whitespaces allowed. |
209 | | */ |
210 | | void barebox_set_hostname(const char *__hostname) |
211 | 30 | { |
212 | 30 | globalvar_add_simple_string("hostname", &hostname); |
213 | | |
214 | 30 | free(hostname); |
215 | | |
216 | 30 | if (!barebox_hostname_is_valid(__hostname)) |
217 | 30 | pr_warn("Hostname is not valid, please fix it\n"); |
218 | | |
219 | 30 | hostname = xstrdup(__hostname); |
220 | 30 | } |
221 | | |
222 | | const char *barebox_get_hostname(void) |
223 | 30 | { |
224 | 30 | return hostname; |
225 | 30 | } |
226 | | EXPORT_SYMBOL(barebox_get_hostname); |
227 | | |
228 | | void barebox_set_hostname_no_overwrite(const char *__hostname) |
229 | 30 | { |
230 | 30 | if (!barebox_get_hostname()) |
231 | 30 | barebox_set_hostname(__hostname); |
232 | 30 | } |
233 | | EXPORT_SYMBOL(barebox_set_hostname_no_overwrite); |
234 | | |
235 | | BAREBOX_MAGICVAR(global.hostname, |
236 | | "shortname of the board. Also used as hostname for DHCP requests"); |
237 | | |
238 | | void barebox_set_serial_number(const char *__serial_number) |
239 | 56 | { |
240 | 56 | globalvar_add_simple_string("serial_number", &serial_number); |
241 | | |
242 | 56 | free(serial_number); |
243 | 56 | serial_number = xstrdup(__serial_number); |
244 | 56 | } |
245 | | |
246 | | const char *barebox_get_serial_number(void) |
247 | 0 | { |
248 | 0 | if (!serial_number || *serial_number == '\0') |
249 | 0 | return NULL; |
250 | 0 | return serial_number; |
251 | 0 | } |
252 | | |
253 | | BAREBOX_MAGICVAR(global.serial_number, "Board serial number"); |
254 | | |
255 | | void barebox_set_product_uuid(const uuid_t *__product_uuid) |
256 | 0 | { |
257 | 0 | globalvar_add_simple_uuid("product.uuid", &product_uuid); |
258 | 0 | product_uuid = *__product_uuid; |
259 | 0 | } |
260 | | |
261 | | const uuid_t *barebox_get_product_uuid(void) |
262 | 0 | { |
263 | 0 | return &product_uuid; |
264 | 0 | } |
265 | | |
266 | | BAREBOX_MAGICVAR(global.product.uuid, "SMBIOS-reported product UUID"); |
267 | | |
268 | | static char *soc_uid_str; |
269 | | static char *soc_uid_bin_str; |
270 | | static void *soc_uid; |
271 | | static size_t soc_uid_len; |
272 | | |
273 | | /* |
274 | | * barebox_set_soc_uid - set a SoC Unique ID |
275 | | * |
276 | | * Set a SoC Unique ID. The ID is usually read from SoC internal eFuses. It |
277 | | * can vary in length on different SoCs and can have different canonical hex |
278 | | * representations. @uidstr can be NULL in which case uidbuf is interpreted |
279 | | * as a byte array. |
280 | | * |
281 | | * barebox uses the SoC UID to generate a machine_id and exports it to the |
282 | | * environment via global.soc_uid. |
283 | | * |
284 | | * In Linux the serial number is exported as /sys/devices/soc0/serial_number |
285 | | * which should generally have the same format as *uidstr. |
286 | | */ |
287 | | void barebox_set_soc_uid(const char *uidstr, const void *uidbuf, size_t len) |
288 | 0 | { |
289 | 0 | if (soc_uid_str) { |
290 | 0 | pr_warn("SoC UID already set. Ignoring\n"); |
291 | 0 | return; |
292 | 0 | } |
293 | | |
294 | 0 | soc_uid = xmemdup(uidbuf, len); |
295 | 0 | soc_uid_len = len; |
296 | |
|
297 | 0 | soc_uid_bin_str = xzalloc(len * 2 + 1); |
298 | 0 | bin2hex(soc_uid_bin_str, uidbuf, len); |
299 | |
|
300 | 0 | if (uidstr) { |
301 | 0 | soc_uid_str = xstrdup(uidstr); |
302 | 0 | } else { |
303 | 0 | soc_uid_str = soc_uid_bin_str; |
304 | 0 | } |
305 | |
|
306 | 0 | machine_id_set_hashable(uidbuf, len); |
307 | |
|
308 | 0 | globalvar_add_simple_string("soc_uid", &soc_uid_str); |
309 | 0 | globalvar_add_simple_string("soc_uid_hex", &soc_uid_bin_str); |
310 | 0 | } |
311 | | BAREBOX_MAGICVAR(global.soc_uid, "SoC Unique ID"); |
312 | | BAREBOX_MAGICVAR(global.soc_uid_hex, "Raw SoC Unique ID representation"); |
313 | | |
314 | | const char *barebox_get_soc_uid(void) |
315 | 0 | { |
316 | 0 | return soc_uid_str; |
317 | 0 | } |
318 | | |
319 | | int barebox_get_soc_uid_bin(const void **buf, size_t *len) |
320 | 1 | { |
321 | 1 | if (!soc_uid) |
322 | 1 | return -ENOENT; |
323 | | |
324 | 0 | *buf = soc_uid; |
325 | 0 | *len = soc_uid_len; |
326 | |
|
327 | 0 | return 0; |
328 | 1 | } |
329 | | |
330 | | #ifdef CONFIG_OFTREE |
331 | | static char *of_machine_compatible; |
332 | | |
333 | | void barebox_set_of_machine_compatible(const char *__compatible) |
334 | 0 | { |
335 | 0 | free(of_machine_compatible); |
336 | 0 | of_machine_compatible = xstrdup(__compatible); |
337 | 0 | } |
338 | | |
339 | | const char *barebox_get_of_machine_compatible(void) |
340 | 0 | { |
341 | 0 | if (!of_machine_compatible || *of_machine_compatible == '\0') |
342 | 0 | return NULL; |
343 | 0 | return of_machine_compatible; |
344 | 0 | } |
345 | | |
346 | | static int of_kernel_init(void) |
347 | 30 | { |
348 | 30 | globalvar_add_simple_string("of.kernel.add_machine_compatible", |
349 | 30 | &of_machine_compatible); |
350 | | |
351 | 30 | return 0; |
352 | 30 | } |
353 | | device_initcall(of_kernel_init); |
354 | | |
355 | | BAREBOX_MAGICVAR(global.of.kernel.add_machine_compatible, "Extra machine/board compatible to prepend to kernel DT compatible"); |
356 | | #endif |
357 | | |
358 | | static void __noreturn do_panic(bool stacktrace, const char *fmt, va_list ap) |
359 | 0 | { |
360 | 0 | if (*fmt) |
361 | 0 | eprintf("PANIC: "); |
362 | 0 | veprintf(fmt, ap); |
363 | 0 | eputchar('\n'); |
364 | |
|
365 | 0 | if (stacktrace) |
366 | 0 | dump_stack(); |
367 | |
|
368 | 0 | led_trigger(LED_TRIGGER_PANIC, TRIGGER_ENABLE); |
369 | |
|
370 | 0 | if (IS_ENABLED(CONFIG_PANIC_HANG)) |
371 | 0 | hang(); |
372 | | |
373 | 0 | mdelay_non_interruptible(100); /* allow messages to go out */ |
374 | |
|
375 | 0 | if (IS_ENABLED(CONFIG_PANIC_POWEROFF)) |
376 | 0 | poweroff_machine(0); |
377 | 0 | else if (IS_ENABLED(CONFIG_PANIC_TRAP)) |
378 | 0 | __builtin_trap(); |
379 | 0 | else |
380 | 0 | restart_machine(0); |
381 | 0 | } |
382 | | |
383 | | void __noreturn panic(const char *fmt, ...) |
384 | 0 | { |
385 | 0 | va_list args; |
386 | |
|
387 | 0 | va_start(args, fmt); |
388 | 0 | do_panic(true, fmt, args); |
389 | 0 | va_end(args); |
390 | 0 | } |
391 | | EXPORT_SYMBOL(panic); |
392 | | |
393 | | void __noreturn panic_no_stacktrace(const char *fmt, ...) |
394 | 0 | { |
395 | 0 | va_list args; |
396 | |
|
397 | 0 | va_start(args, fmt); |
398 | 0 | do_panic(false, fmt, args); |
399 | 0 | va_end(args); |
400 | 0 | } |
401 | | EXPORT_SYMBOL(panic_no_stacktrace); |