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 
22 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
23 
24 /**
25  * Find error description
26  *
27  * @v errno Error number
28  * @ret errortab Error description, or NULL
29  */
30 static struct errortab * find_error ( int errno ) {
31  struct errortab *errortab;
32 
34  if ( errortab->errno == errno )
35  return errortab;
36  }
37 
38  return NULL;
39 }
40 
41 /**
42  * Find closest error description
43  *
44  * @v errno Error number
45  * @ret errortab Error description, or NULL
46  *
47  *
48  */
49 static struct errortab * find_closest_error ( int errno ) {
50  struct errortab *errortab;
51 
52  /* First, look for an exact match */
53  if ( ( errortab = find_error ( errno ) ) != NULL )
54  return errortab;
55 
56  /* Second, try masking off the iPXE-specific bit and seeing if
57  * we have an entry for the generic POSIX error message.
58  */
59  if ( ( errortab = find_error ( errno & 0x7f0000ff ) ) != NULL )
60  return errortab;
61 
62  return NULL;
63 }
64 
65 /**
66  * Retrieve string representation of error number.
67  *
68  * @v errno/rc Error number or return status code
69  * @ret strerror Pointer to error text
70  *
71  * If the error is not found in the linked-in error tables, generates
72  * a generic "Error 0x<errno>" message.
73  *
74  * The pointer returned by strerror() is valid only until the next
75  * call to strerror().
76  *
77  */
78 char * strerror ( int errno ) {
79  static char errbuf[64];
80  struct errortab *errortab;
81 
82  /* Allow for strerror(rc) as well as strerror(errno) */
83  if ( errno < 0 )
84  errno = -errno;
85 
86  /* Find the error description, if one exists */
88 
89  /* Construct the error message */
90  if ( errortab ) {
91  snprintf ( errbuf, sizeof ( errbuf ),
92  "%s (" PRODUCT_ERROR_URI ")",
93  errortab->text, errno );
94  } else {
95  snprintf ( errbuf, sizeof ( errbuf ),
96  "Error %#08x (" PRODUCT_ERROR_URI ")",
97  errno, errno );
98  }
99 
100  return errbuf;
101 }
102 
103 /* Do not include ERRFILE portion in the numbers in the error table */
104 #undef ERRFILE
105 #define ERRFILE 0
106 
107 /** The most common errors */
108 struct errortab common_errors[] __errortab = {
126 };
struct errortab common_errors [] __errortab
The most common errors.
Definition: strerror.c:108
#define EINFO_ENOTSUP
Definition: errno.h:590
#define EINFO_ENODEV
Definition: errno.h:510
Error message tables.
Error codes.
#define EINFO_EPERM
Definition: errno.h:615
int errno
Definition: errortab.h:15
#define __einfo_errortab(einfo)
Definition: errortab.h:23
Branding configuration.
#define EINFO_ETIMEDOUT
Definition: errno.h:670
#define EINFO_EINVAL
Definition: errno.h:429
#define EINFO_ENETUNREACH
Definition: errno.h:489
#define EINFO_ENOSPC
Definition: errno.h:550
int errno
Global "last error" number.
Definition: errno.c:20
#define EINFO_EIO
Definition: errno.h:434
const char * text
Definition: errortab.h:16
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
#define PRODUCT_ERROR_URI
Definition: branding.h:83
#define EINFO_ECANCELED
Definition: errno.h:344
#define EINFO_ENOEXEC
Definition: errno.h:520
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:385
#define EINFO_ENOENT
Definition: errno.h:515
#define EINFO_EACCES
Definition: errno.h:299
static struct errortab * find_error(int errno)
Find error description.
Definition: strerror.c:30
#define EINFO_ENOERR
Definition: errno.h:289
#define ERRORTAB
Definition: errortab.h:19
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:382
static struct errortab * find_closest_error(int errno)
Find closest error description.
Definition: strerror.c:49
#define EINFO_ERANGE
Definition: errno.h:640
#define EINFO_ENOTCONN
Definition: errno.h:570
#define EINFO_ECONNRESET
Definition: errno.h:364
#define EINFO_ENOMEM
Definition: errno.h:535
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.