[sanboot] Prevent leaking a stack reference for "keep-san" AoE
[gpxe.git] / src / hci / strerror.c
blob94547dd4b19018defe6cb8f1d4eb394c830b96dd
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 { 0, "No error" },
106 { EACCES, "Permission denied" },
107 { ECANCELED, "Operation cancelled" },
108 { ECONNRESET, "Connection reset" },
109 { EINVAL, "Invalid argument" },
110 { EIO, "Input/output error" },
111 { ENETUNREACH, "Network unreachable" },
112 { ENODEV, "No such device" },
113 { ENOENT, "File not found" },
114 { ENOEXEC, "Not an executable image" },
115 { ENOMEM, "Out of memory" },
116 { ENOSPC, "No space left on device" },
117 { ENOTCONN, "Not connected" },
118 { ENOTSUP, "Not supported" },
119 { EPERM, "Operation not permitted" },
120 { ERANGE, "Out of range" },
121 { ETIMEDOUT, "Connection timed out" },