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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include <errno.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <gpxe/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. static struct errortab errortab_start[0]
  20. __table_start ( struct errortab, errortab );
  21. static struct errortab errortab_end[0]
  22. __table_end ( struct errortab, errortab );
  23. /**
  24. * Find error description
  25. *
  26. * @v errno Error number
  27. * @v mask Mask of bits that we care about
  28. * @ret errortab Error description, or NULL
  29. */
  30. static struct errortab * find_error ( int errno, int mask ) {
  31. struct errortab *errortab;
  32. for ( errortab = errortab_start ; errortab < errortab_end ;
  33. errortab++ ) {
  34. if ( ( ( errortab->errno ^ errno ) & mask ) == 0 )
  35. return errortab;
  36. }
  37. return NULL;
  38. }
  39. /**
  40. * Find closest error description
  41. *
  42. * @v errno Error number
  43. * @ret errortab Error description, or NULL
  44. *
  45. *
  46. */
  47. static struct errortab * find_closest_error ( int errno ) {
  48. struct errortab *errortab;
  49. /* First, look for an exact match */
  50. if ( ( errortab = find_error ( errno, 0x7fffffff ) ) != NULL )
  51. return errortab;
  52. /* Second, try masking off the gPXE-specific bit and seeing if
  53. * we have an entry for the generic POSIX error message.
  54. */
  55. if ( ( errortab = find_error ( errno, 0x0000ffff ) ) != NULL )
  56. return errortab;
  57. /* Lastly, try masking off the POSIX bits and seeing if we
  58. * have a match just based on the PXENV component. This
  59. * allows us to report errors from underlying PXE stacks.
  60. */
  61. if ( ( errortab = find_error ( ( errno & 0x000000ff ),
  62. 0xffff00ff ) ) != NULL )
  63. return errortab;
  64. return NULL;
  65. }
  66. /**
  67. * Retrieve string representation of error number.
  68. *
  69. * @v errno/rc Error number or return status code
  70. * @ret strerror Pointer to error text
  71. *
  72. * If the error is not found in the linked-in error tables, generates
  73. * a generic "Error 0x<errno>" message.
  74. *
  75. * The pointer returned by strerror() is valid only until the next
  76. * call to strerror().
  77. *
  78. */
  79. const char * strerror ( int errno ) {
  80. static char errbuf[64];
  81. struct errortab *errortab;
  82. /* Allow for strerror(rc) as well as strerror(errno) */
  83. if ( errno < 0 )
  84. errno = -errno;
  85. /* Find the error description, if one exists */
  86. errortab = find_closest_error ( errno );
  87. /* Construct the error message */
  88. if ( errortab ) {
  89. snprintf ( errbuf, sizeof ( errbuf ), "%s (%#08x)",
  90. errortab->text, errno );
  91. } else {
  92. snprintf ( errbuf, sizeof ( errbuf ), "Error %#08x", errno );
  93. }
  94. return errbuf;
  95. }
  96. /** The most common errors */
  97. struct errortab common_errors[] __errortab = {
  98. { 0, "No error" },
  99. { ENOMEM, "Out of memory" },
  100. { EINVAL, "Invalid argument" },
  101. { ENOSPC, "No space left on device" },
  102. { EIO, "Input/output error" },
  103. { EACCES, "Permission denied" },
  104. { ENOENT, "File not found" },
  105. { ENETUNREACH, "Network unreachable" },
  106. { ETIMEDOUT, "Connection timed out" },
  107. { EPIPE, "Broken pipe" },
  108. };