[golan] Do not assume all devices are identical
[ipxe.git] / src / hci / strerror.c
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
33 for_each_table_entry ( errortab, ERRORTAB ) {
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 */
87 errortab = find_closest_error ( errno );
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 = {
109 __einfo_errortab ( EINFO_ENOERR ),
110 __einfo_errortab ( EINFO_EACCES ),
111 __einfo_errortab ( EINFO_ECANCELED ),
112 __einfo_errortab ( EINFO_ECONNRESET ),
113 __einfo_errortab ( EINFO_EINVAL ),
114 __einfo_errortab ( EINFO_EIO ),
115 __einfo_errortab ( EINFO_ENETUNREACH ),
116 __einfo_errortab ( EINFO_ENODEV ),
117 __einfo_errortab ( EINFO_ENOENT ),
118 __einfo_errortab ( EINFO_ENOEXEC ),
119 __einfo_errortab ( EINFO_ENOMEM ),
120 __einfo_errortab ( EINFO_ENOSPC ),
121 __einfo_errortab ( EINFO_ENOTCONN ),
122 __einfo_errortab ( EINFO_ENOTSUP ),
123 __einfo_errortab ( EINFO_EPERM ),
124 __einfo_errortab ( EINFO_ERANGE ),
125 __einfo_errortab ( EINFO_ETIMEDOUT ),
126 };