iPXE
strerror.c
Go to the documentation of this file.
1#include <errno.h>
2#include <string.h>
3#include <stdio.h>
4#include <ipxe/errortab.h>
5#include <config/branding.h>
6
7/** @file
8 *
9 * Error descriptions.
10 *
11 * The error numbers used by Etherboot are a superset of those defined
12 * by the PXE specification version 2.1. See errno.h for a listing of
13 * the error values.
14 *
15 * To save space in ROM images, error string tables are optional. Use
16 * the ERRORMSG_XXX options in config.h to select which error string
17 * tables you want to include. If an error string table is omitted,
18 * strerror() will simply return the text "Error 0x<errno>".
19 *
20 */
21
22FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
23FILE_SECBOOT ( PERMITTED );
24
25/**
26 * Find error description
27 *
28 * @v errno Error number
29 * @ret errortab Error description, or NULL
30 */
31static struct errortab * find_error ( int errno ) {
32 struct errortab *errortab;
33
35 if ( errortab->errno == errno )
36 return errortab;
37 }
38
39 return NULL;
40}
41
42/**
43 * Find closest error description
44 *
45 * @v errno Error number
46 * @ret errortab Error description, or NULL
47 *
48 *
49 */
50static struct errortab * find_closest_error ( int errno ) {
51 struct errortab *errortab;
52
53 /* First, look for an exact match */
54 if ( ( errortab = find_error ( errno ) ) != NULL )
55 return errortab;
56
57 /* Second, try masking off the iPXE-specific bit and seeing if
58 * we have an entry for the generic POSIX error message.
59 */
60 if ( ( errortab = find_error ( errno & 0x7f0000ff ) ) != NULL )
61 return errortab;
62
63 return NULL;
64}
65
66/**
67 * Retrieve string representation of error number.
68 *
69 * @v errno/rc Error number or return status code
70 * @ret strerror Pointer to error text
71 *
72 * If the error is not found in the linked-in error tables, generates
73 * a generic "Error 0x<errno>" message.
74 *
75 * The pointer returned by strerror() is valid only until the next
76 * call to strerror().
77 *
78 */
79char * strerror ( int errno ) {
80 static char errbuf[64];
81 struct errortab *errortab;
82
83 /* Allow for strerror(rc) as well as strerror(errno) */
84 if ( errno < 0 )
85 errno = -errno;
86
87 /* Find the error description, if one exists */
89
90 /* Construct the error message */
91 if ( errortab ) {
92 snprintf ( errbuf, sizeof ( errbuf ),
93 "%s (" PRODUCT_ERROR_URI ")",
95 } else {
96 snprintf ( errbuf, sizeof ( errbuf ),
97 "Error %#08x (" PRODUCT_ERROR_URI ")",
98 errno, errno );
99 }
100
101 return errbuf;
102}
103
104/* Do not include ERRFILE portion in the numbers in the error table */
105#undef ERRFILE
106#define ERRFILE 0
107
108/** The most common errors */
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
Branding configuration.
#define PRODUCT_ERROR_URI
Definition branding.h:84
int errno
Global "last error" number.
Definition errno.c:21
Error codes.
Error message tables.
#define __errortab
Definition errortab.h:22
#define ERRORTAB
Definition errortab.h:20
#define __einfo_errortab(einfo)
Definition errortab.h:24
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define EINFO_ENODEV
Definition errno.h:511
#define EINFO_EIO
Definition errno.h:435
#define EINFO_ENOSPC
Definition errno.h:551
#define EINFO_ENOERR
Definition errno.h:290
#define EINFO_ETIMEDOUT
Definition errno.h:671
#define EINFO_ENOEXEC
Definition errno.h:521
#define EINFO_ERANGE
Definition errno.h:641
#define EINFO_EINVAL
Definition errno.h:430
#define EINFO_ENETUNREACH
Definition errno.h:490
#define EINFO_ENOTSUP
Definition errno.h:591
#define EINFO_EPERM
Definition errno.h:616
#define EINFO_ENOENT
Definition errno.h:516
#define EINFO_ENOTCONN
Definition errno.h:571
#define EINFO_ECANCELED
Definition errno.h:345
#define EINFO_ECONNRESET
Definition errno.h:365
#define EINFO_ENOMEM
Definition errno.h:536
#define EINFO_EACCES
Definition errno.h:300
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
String functions.
static struct errortab * find_closest_error(int errno)
Find closest error description.
Definition strerror.c:50
static struct errortab * find_error(int errno)
Find error description.
Definition strerror.c:31
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
const char * text
Definition errortab.h:17
int errno
Definition errortab.h:16
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition tables.h:386
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition vsprintf.c:383