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

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