xref: /freebsd/stand/efi/libefi/errno.c (revision 3705d679a6344c957cae7a1b6372a8bfb8c44f0e)
1 /*-
2  * Copyright (c) 2006 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <efi.h>
28 #include <efilib.h>
29 
30 EFI_STATUS
31 errno_to_efi_status(int errno)
32 {
33         EFI_STATUS status;
34 
35         switch (errno) {
36         case EPERM:
37                 status = EFI_ACCESS_DENIED;
38                 break;
39 
40         case EOVERFLOW:
41                 status = EFI_BUFFER_TOO_SMALL;
42                 break;
43 
44         case EIO:
45                 status = EFI_DEVICE_ERROR;
46                 break;
47 
48         case EINVAL:
49                 status = EFI_INVALID_PARAMETER;
50                 break;
51 
52         case ESTALE:
53                 status = EFI_MEDIA_CHANGED;
54                 break;
55 
56         case ENXIO:
57                 status = EFI_NO_MEDIA;
58                 break;
59 
60         case ENOENT:
61                 status = EFI_NOT_FOUND;
62                 break;
63 
64         case ENOMEM:
65                 status = EFI_OUT_OF_RESOURCES;
66                 break;
67 
68         case ENOTSUP:
69         case ENODEV:
70                 status = EFI_UNSUPPORTED;
71                 break;
72 
73         case ENOSPC:
74                 status = EFI_VOLUME_FULL;
75                 break;
76 
77         case EACCES:
78                 status = EFI_WRITE_PROTECTED;
79                 break;
80 
81         case 0:
82                 status = EFI_SUCCESS;
83                 break;
84 
85         default:
86                 status = EFI_DEVICE_ERROR;
87                 break;
88         }
89 
90         return (status);
91 }
92 
93 int
94 efi_status_to_errno(EFI_STATUS status)
95 {
96 	int errno;
97 
98 	switch (status) {
99 	case EFI_ACCESS_DENIED:
100 		errno = EPERM;
101 		break;
102 
103 	case EFI_BUFFER_TOO_SMALL:
104 		errno = EOVERFLOW;
105 		break;
106 
107 	case EFI_DEVICE_ERROR:
108 	case EFI_VOLUME_CORRUPTED:
109 		errno = EIO;
110 		break;
111 
112 	case EFI_INVALID_PARAMETER:
113 		errno = EINVAL;
114 		break;
115 
116 	case EFI_MEDIA_CHANGED:
117 		errno = ESTALE;
118 		break;
119 
120 	case EFI_NO_MEDIA:
121 		errno = ENXIO;
122 		break;
123 
124 	case EFI_NOT_FOUND:
125 		errno = ENOENT;
126 		break;
127 
128 	case EFI_OUT_OF_RESOURCES:
129 		errno = ENOMEM;
130 		break;
131 
132 	case EFI_UNSUPPORTED:
133 		errno = ENODEV;
134 		break;
135 
136 	case EFI_VOLUME_FULL:
137 		errno = ENOSPC;
138 		break;
139 
140 	case EFI_WRITE_PROTECTED:
141 		errno = EACCES;
142 		break;
143 
144 	case 0:
145 		errno = 0;
146 		break;
147 
148 	default:
149 		errno = EDOOFUS;
150 		break;
151 	}
152 
153 	return (errno);
154 }
155