You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

strerror.c 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include <errno.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <ipxe/errortab.h>
  5. /** @file
  6. *
  7. * Error descriptions.
  8. *
  9. * The error numbers used by Etherboot are a superset of those defined
  10. * by the PXE specification version 2.1. See errno.h for a listing of
  11. * the error values.
  12. *
  13. * To save space in ROM images, error string tables are optional. Use
  14. * the ERRORMSG_XXX options in config.h to select which error string
  15. * tables you want to include. If an error string table is omitted,
  16. * strerror() will simply return the text "Error 0x<errno>".
  17. *
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. /**
  21. * Find error description
  22. *
  23. * @v errno Error number
  24. * @ret errortab Error description, or NULL
  25. */
  26. static struct errortab * find_error ( int errno ) {
  27. struct errortab *errortab;
  28. for_each_table_entry ( errortab, ERRORTAB ) {
  29. if ( errortab->errno == errno )
  30. return errortab;
  31. }
  32. return NULL;
  33. }
  34. /**
  35. * Find closest error description
  36. *
  37. * @v errno Error number
  38. * @ret errortab Error description, or NULL
  39. *
  40. *
  41. */
  42. static struct errortab * find_closest_error ( int errno ) {
  43. struct errortab *errortab;
  44. /* First, look for an exact match */
  45. if ( ( errortab = find_error ( errno ) ) != NULL )
  46. return errortab;
  47. /* Second, try masking off the iPXE-specific bit and seeing if
  48. * we have an entry for the generic POSIX error message.
  49. */
  50. if ( ( errortab = find_error ( errno & 0x7f0000ff ) ) != NULL )
  51. return errortab;
  52. return NULL;
  53. }
  54. /**
  55. * Retrieve string representation of error number.
  56. *
  57. * @v errno/rc Error number or return status code
  58. * @ret strerror Pointer to error text
  59. *
  60. * If the error is not found in the linked-in error tables, generates
  61. * a generic "Error 0x<errno>" message.
  62. *
  63. * The pointer returned by strerror() is valid only until the next
  64. * call to strerror().
  65. *
  66. */
  67. const char * strerror ( int errno ) {
  68. static char errbuf[64];
  69. struct errortab *errortab;
  70. /* Allow for strerror(rc) as well as strerror(errno) */
  71. if ( errno < 0 )
  72. errno = -errno;
  73. /* Find the error description, if one exists */
  74. errortab = find_closest_error ( errno );
  75. /* Construct the error message */
  76. if ( errortab ) {
  77. snprintf ( errbuf, sizeof ( errbuf ),
  78. "%s (http://ipxe.org/%08x)",
  79. errortab->text, errno );
  80. } else {
  81. snprintf ( errbuf, sizeof ( errbuf ),
  82. "Error %#08x (http://ipxe.org/%08x)",
  83. errno, errno );
  84. }
  85. return errbuf;
  86. }
  87. /* Do not include ERRFILE portion in the numbers in the error table */
  88. #undef ERRFILE
  89. #define ERRFILE 0
  90. /** The most common errors */
  91. struct errortab common_errors[] __errortab = {
  92. __einfo_errortab ( EINFO_ENOERR ),
  93. __einfo_errortab ( EINFO_EACCES ),
  94. __einfo_errortab ( EINFO_ECANCELED ),
  95. __einfo_errortab ( EINFO_ECONNRESET ),
  96. __einfo_errortab ( EINFO_EINVAL ),
  97. __einfo_errortab ( EINFO_EIO ),
  98. __einfo_errortab ( EINFO_ENETUNREACH ),
  99. __einfo_errortab ( EINFO_ENODEV ),
  100. __einfo_errortab ( EINFO_ENOENT ),
  101. __einfo_errortab ( EINFO_ENOEXEC ),
  102. __einfo_errortab ( EINFO_ENOMEM ),
  103. __einfo_errortab ( EINFO_ENOSPC ),
  104. __einfo_errortab ( EINFO_ENOTCONN ),
  105. __einfo_errortab ( EINFO_ENOTSUP ),
  106. __einfo_errortab ( EINFO_EPERM ),
  107. __einfo_errortab ( EINFO_ERANGE ),
  108. __einfo_errortab ( EINFO_ETIMEDOUT ),
  109. };