david/ipxe
david
/
ipxe
Archived
1
0
Fork 0
This repository has been archived on 2020-12-06. You can view files and clone it, but cannot push or open issues or pull requests.
ipxe/src/hci/strerror.c

125 lines
3.2 KiB
C
Raw Normal View History

2006-12-20 04:49:36 +01:00
#include <errno.h>
#include <string.h>
2007-01-19 02:13:12 +01:00
#include <stdio.h>
2006-12-20 04:49:36 +01:00
#include <gpxe/errortab.h>
/** @file
*
* Error descriptions.
*
* The error numbers used by Etherboot are a superset of those defined
* by the PXE specification version 2.1. See errno.h for a listing of
* the error values.
*
* To save space in ROM images, error string tables are optional. Use
* the ERRORMSG_XXX options in config.h to select which error string
* tables you want to include. If an error string table is omitted,
* strerror() will simply return the text "Error 0x<errno>".
*
*/
static struct errortab errortab_start[0]
__table_start ( struct errortab, errortab );
static struct errortab errortab_end[0]
__table_end ( struct errortab, errortab );
2006-12-20 04:49:36 +01:00
/**
2007-01-19 14:52:50 +01:00
* Find error description
*
* @v errno Error number
* @v mask Mask of bits that we care about
* @ret errortab Error description, or NULL
*/
static struct errortab * find_error ( int errno, int mask ) {
struct errortab *errortab;
for ( errortab = errortab_start ; errortab < errortab_end ;
errortab++ ) {
if ( ( ( errortab->errno ^ errno ) & mask ) == 0 )
return errortab;
}
return NULL;
}
/**
* Find closest error description
2006-12-20 04:49:36 +01:00
*
* @v errno Error number
2007-01-19 14:52:50 +01:00
* @ret errortab Error description, or NULL
*
*
*/
static struct errortab * find_closest_error ( int errno ) {
struct errortab *errortab;
/* First, look for an exact match */
if ( ( errortab = find_error ( errno, 0x7fffffff ) ) != NULL )
return errortab;
/* Second, try masking off the gPXE-specific bit and seeing if
* we have an entry for the generic POSIX error message.
*/
if ( ( errortab = find_error ( errno, 0x0000ffff ) ) != NULL )
return errortab;
/* Lastly, try masking off the POSIX bits and seeing if we
* have a match just based on the PXENV component. This
* allows us to report errors from underlying PXE stacks.
*/
if ( ( errortab = find_error ( ( errno & 0x000000ff ),
0xffff00ff ) ) != NULL )
return errortab;
return NULL;
}
/**
* Retrieve string representation of error number.
*
* @v errno/rc Error number or return status code
2006-12-20 04:49:36 +01:00
* @ret strerror Pointer to error text
*
* If the error is not found in the linked-in error tables, generates
* a generic "Error 0x<errno>" message.
*
* The pointer returned by strerror() is valid only until the next
* call to strerror().
*
*/
const char * strerror ( int errno ) {
2007-01-19 14:52:50 +01:00
static char errbuf[64];
2006-12-20 04:49:36 +01:00
struct errortab *errortab;
/* Allow for strerror(rc) as well as strerror(errno) */
if ( errno < 0 )
errno = -errno;
2007-01-19 14:52:50 +01:00
/* Find the error description, if one exists */
errortab = find_closest_error ( errno );
/* Construct the error message */
if ( errortab ) {
snprintf ( errbuf, sizeof ( errbuf ), "%s (%#08x)",
errortab->text, errno );
} else {
snprintf ( errbuf, sizeof ( errbuf ), "Error %#08x", errno );
2006-12-20 04:49:36 +01:00
}
2007-01-19 14:52:50 +01:00
return errbuf;
2006-12-20 04:49:36 +01:00
}
/** The most common errors */
struct errortab common_errors[] __errortab = {
{ 0, "No error" },
{ ENOMEM, "Out of memory" },
{ EINVAL, "Invalid argument" },
{ ENOSPC, "No space left on device" },
{ EIO, "Input/output error" },
{ EACCES, "Permission denied" },
{ ENOENT, "File not found" },
{ ENETUNREACH, "Network unreachable" },
2007-01-15 03:46:56 +01:00
{ ETIMEDOUT, "Connection timed out" },
2007-05-26 17:05:31 +02:00
{ EPIPE, "Broken pipe" },
};