gpxe import
[grub-extras.git] / gpxe / src / hci / strerror.c
blob216b1e578bcde1699c4ef318baad012efe55fe88
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 * @v mask Mask of bits that we care about
28 * @ret errortab Error description, or NULL
30 static struct errortab * find_error ( int errno, int mask ) {
31 struct errortab *errortab;
33 for_each_table_entry ( errortab, ERRORTAB ) {
34 if ( ( ( errortab->errno ^ errno ) & mask ) == 0 )
35 return errortab;
38 return NULL;
41 /**
42 * Find closest error description
44 * @v errno Error number
45 * @ret errortab Error description, or NULL
49 static struct errortab * find_closest_error ( int errno ) {
50 struct errortab *errortab;
52 /* First, look for an exact match */
53 if ( ( errortab = find_error ( errno, 0x7fffffff ) ) != NULL )
54 return errortab;
56 /* Second, try masking off the gPXE-specific bit and seeing if
57 * we have an entry for the generic POSIX error message.
59 if ( ( errortab = find_error ( errno, 0x4f0000ff ) ) != NULL )
60 return errortab;
62 return NULL;
65 /**
66 * Retrieve string representation of error number.
68 * @v errno/rc Error number or return status code
69 * @ret strerror Pointer to error text
71 * If the error is not found in the linked-in error tables, generates
72 * a generic "Error 0x<errno>" message.
74 * The pointer returned by strerror() is valid only until the next
75 * call to strerror().
78 const char * strerror ( int errno ) {
79 static char errbuf[64];
80 struct errortab *errortab;
82 /* Allow for strerror(rc) as well as strerror(errno) */
83 if ( errno < 0 )
84 errno = -errno;
86 /* Find the error description, if one exists */
87 errortab = find_closest_error ( errno );
89 /* Construct the error message */
90 if ( errortab ) {
91 snprintf ( errbuf, sizeof ( errbuf ), "%s (%#08x)",
92 errortab->text, errno );
93 } else {
94 snprintf ( errbuf, sizeof ( errbuf ), "Error %#08x", errno );
97 return errbuf;
100 /* Do not include ERRFILE portion in the numbers in the error table */
101 #undef ERRFILE
102 #define ERRFILE 0
104 /** The most common errors */
105 struct errortab common_errors[] __errortab = {
106 { 0, "No error" },
107 { EACCES, "Permission denied" },
108 { ECANCELED, "Operation cancelled" },
109 { ECONNRESET, "Connection reset" },
110 { EINVAL, "Invalid argument" },
111 { EIO, "Input/output error" },
112 { ENETUNREACH, "Network unreachable" },
113 { ENODEV, "No such device" },
114 { ENOENT, "File not found" },
115 { ENOEXEC, "Not an executable image" },
116 { ENOMEM, "Out of memory" },
117 { ENOSPC, "No space left on device" },
118 { ENOTCONN, "Not connected" },
119 { ENOTSUP, "Not supported" },
120 { EPERM, "Operation not permitted" },
121 { ERANGE, "Out of range" },
122 { ETIMEDOUT, "Connection timed out" },