[isa] Fix a small typo in isa.c.
[ipxe.git] / src / hci / strerror.c
1 #include <errno.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include <gpxe/errortab.h>
5
6 /** @file
7 *
8 * Error descriptions.
9 *
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.
13 *
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>".
18 *
19 */
20
21 static struct errortab errortab_start[0]
22 __table_start ( struct errortab, errortab );
23 static struct errortab errortab_end[0]
24 __table_end ( struct errortab, errortab );
25
26 /**
27 * Find error description
28 *
29 * @v errno Error number
30 * @v mask Mask of bits that we care about
31 * @ret errortab Error description, or NULL
32 */
33 static struct errortab * find_error ( int errno, int mask ) {
34 struct errortab *errortab;
35
36 for ( errortab = errortab_start ; errortab < errortab_end ;
37 errortab++ ) {
38 if ( ( ( errortab->errno ^ errno ) & mask ) == 0 )
39 return errortab;
40 }
41
42 return NULL;
43 }
44
45 /**
46 * Find closest error description
47 *
48 * @v errno Error number
49 * @ret errortab Error description, or NULL
50 *
51 *
52 */
53 static struct errortab * find_closest_error ( int errno ) {
54 struct errortab *errortab;
55
56 /* First, look for an exact match */
57 if ( ( errortab = find_error ( errno, 0x7fffffff ) ) != NULL )
58 return errortab;
59
60 /* Second, try masking off the gPXE-specific bit and seeing if
61 * we have an entry for the generic POSIX error message.
62 */
63 if ( ( errortab = find_error ( errno, 0x4f0000ff ) ) != NULL )
64 return errortab;
65
66 return NULL;
67 }
68
69 /**
70 * Retrieve string representation of error number.
71 *
72 * @v errno/rc Error number or return status code
73 * @ret strerror Pointer to error text
74 *
75 * If the error is not found in the linked-in error tables, generates
76 * a generic "Error 0x<errno>" message.
77 *
78 * The pointer returned by strerror() is valid only until the next
79 * call to strerror().
80 *
81 */
82 const char * strerror ( int errno ) {
83 static char errbuf[64];
84 struct errortab *errortab;
85
86 /* Allow for strerror(rc) as well as strerror(errno) */
87 if ( errno < 0 )
88 errno = -errno;
89
90 /* Find the error description, if one exists */
91 errortab = find_closest_error ( errno );
92
93 /* Construct the error message */
94 if ( errortab ) {
95 snprintf ( errbuf, sizeof ( errbuf ), "%s (%#08x)",
96 errortab->text, errno );
97 } else {
98 snprintf ( errbuf, sizeof ( errbuf ), "Error %#08x", 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 */
109 struct errortab common_errors[] __errortab = {
110 { 0, "No error" },
111 { EACCES, "Permission denied" },
112 { ECANCELED, "Operation cancelled" },
113 { ECONNRESET, "Connection reset" },
114 { EINVAL, "Invalid argument" },
115 { EIO, "Input/output error" },
116 { ENETUNREACH, "Network unreachable" },
117 { ENODEV, "No such device" },
118 { ENOENT, "File not found" },
119 { ENOEXEC, "Not an executable image" },
120 { ENOMEM, "Out of memory" },
121 { ENOSPC, "No space left on device" },
122 { ENOTSUP, "Not supported" },
123 { EPERM, "Operation not permitted" },
124 { ETIMEDOUT, "Connection timed out" },
125 };