[contrib] Allow Network Protocol header to display in rom-o-matic
[gpxe.git] / src / hci / strerror.c
blob3a99811e002670d3385c175673197d967c901e7a
1 #include <errno.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include <gpxe/errortab.h>
6 /** @file
8 * Error descriptions.
10 * The error numbers used by Etherboot are a superset of those defined
11 * by the PXE specification version 2.1. See errno.h for a listing of
12 * the error values.
14 * To save space in ROM images, error string tables are optional. Use
15 * the ERRORMSG_XXX options in config.h to select which error string
16 * tables you want to include. If an error string table is omitted,
17 * strerror() will simply return the text "Error 0x<errno>".
21 FILE_LICENCE ( GPL2_OR_LATER );
23 /**
24 * Find error description
26 * @v errno Error number
27 * @ret errortab Error description, or NULL
29 static struct errortab * find_error ( int errno ) {
30 struct errortab *errortab;
32 for_each_table_entry ( errortab, ERRORTAB ) {
33 if ( errortab->errno == errno )
34 return errortab;
37 return NULL;
40 /**
41 * Find closest error description
43 * @v errno Error number
44 * @ret errortab Error description, or NULL
48 static struct errortab * find_closest_error ( int errno ) {
49 struct errortab *errortab;
51 /* First, look for an exact match */
52 if ( ( errortab = find_error ( errno ) ) != NULL )
53 return errortab;
55 /* Second, try masking off the gPXE-specific bit and seeing if
56 * we have an entry for the generic POSIX error message.
58 if ( ( errortab = find_error ( errno & 0x7f0000ff ) ) != NULL )
59 return errortab;
61 return NULL;
64 /**
65 * Retrieve string representation of error number.
67 * @v errno/rc Error number or return status code
68 * @ret strerror Pointer to error text
70 * If the error is not found in the linked-in error tables, generates
71 * a generic "Error 0x<errno>" message.
73 * The pointer returned by strerror() is valid only until the next
74 * call to strerror().
77 const char * strerror ( int errno ) {
78 static char errbuf[64];
79 struct errortab *errortab;
81 /* Allow for strerror(rc) as well as strerror(errno) */
82 if ( errno < 0 )
83 errno = -errno;
85 /* Find the error description, if one exists */
86 errortab = find_closest_error ( errno );
88 /* Construct the error message */
89 if ( errortab ) {
90 snprintf ( errbuf, sizeof ( errbuf ), "%s (%#08x)",
91 errortab->text, errno );
92 } else {
93 snprintf ( errbuf, sizeof ( errbuf ), "Error %#08x", errno );
96 return errbuf;
99 /* Do not include ERRFILE portion in the numbers in the error table */
100 #undef ERRFILE
101 #define ERRFILE 0
103 /** The most common errors */
104 struct errortab common_errors[] __errortab = {
105 __einfo_errortab ( EINFO_ENOERR ),
106 __einfo_errortab ( EINFO_EACCES ),
107 __einfo_errortab ( EINFO_ECANCELED ),
108 __einfo_errortab ( EINFO_ECONNRESET ),
109 __einfo_errortab ( EINFO_EINVAL ),
110 __einfo_errortab ( EINFO_EIO ),
111 __einfo_errortab ( EINFO_ENETUNREACH ),
112 __einfo_errortab ( EINFO_ENODEV ),
113 __einfo_errortab ( EINFO_ENOENT ),
114 __einfo_errortab ( EINFO_ENOEXEC ),
115 __einfo_errortab ( EINFO_ENOMEM ),
116 __einfo_errortab ( EINFO_ENOSPC ),
117 __einfo_errortab ( EINFO_ENOTCONN ),
118 __einfo_errortab ( EINFO_ENOTSUP ),
119 __einfo_errortab ( EINFO_EPERM ),
120 __einfo_errortab ( EINFO_ERANGE ),
121 __einfo_errortab ( EINFO_ETIMEDOUT ),