]>
Commit | Line | Data |
---|---|---|
2aa62f2b | 1 | /** @file\r |
2 | Value transformations between stdio and the UEFI environment.\r | |
3 | \r | |
53e1e5c6 | 4 | Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>\r |
2aa62f2b | 5 | This program and the accompanying materials are licensed and made available under\r |
6 | the terms and conditions of the BSD License that accompanies this distribution.\r | |
7 | The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php.\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
13 | **/\r | |
14 | #include <Uefi.h>\r | |
15 | \r | |
16 | #include <LibConfig.h>\r | |
17 | #include <sys/EfiCdefs.h>\r | |
18 | \r | |
19 | #include <errno.h>\r | |
20 | #include <fcntl.h>\r | |
53e1e5c6 | 21 | #include <Efi/SysEfi.h>\r |
2aa62f2b | 22 | \r |
23 | /** Translate the Open flags into a Uefi Open Modes value.\r | |
24 | \r | |
25 | The Open Flags are:\r | |
26 | O_RDONLY, O_WRONLY, O_RDWR // Pick only one\r | |
27 | \r | |
28 | O_NONBLOCK, O_APPEND, O_CREAT, O_TRUNC, O_EXCL // ORed with one of the previous\r | |
29 | \r | |
30 | The UEFI Open modes are:\r | |
31 | // ******************************************************\r | |
32 | // Open Modes\r | |
33 | // ******************************************************\r | |
34 | #define EFI_FILE_MODE_READ 0x0000000000000001\r | |
35 | #define EFI_FILE_MODE_WRITE 0x0000000000000002\r | |
36 | #define EFI_FILE_MODE_CREATE 0x8000000000000000\r | |
37 | \r | |
38 | \r | |
39 | */\r | |
40 | UINT64\r | |
41 | Oflags2EFI( int oflags )\r | |
42 | {\r | |
43 | UINT64 flags;\r | |
44 | \r | |
45 | // Build the Open Modes\r | |
46 | flags = (UINT64)((oflags & O_ACCMODE) + 1); // Handle the Read/Write flags\r | |
53e1e5c6 | 47 | if(flags & EFI_FILE_MODE_WRITE) { // Asking for write only?\r |
48 | // EFI says the only two RW modes are read-only and read+write.\r | |
49 | flags = EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE;\r | |
50 | }\r | |
2aa62f2b | 51 | if(oflags & (O_CREAT | O_TRUNC)) { // Now add the Create flag.\r |
52 | // Also added if O_TRUNC set since we will need to create a new file.\r | |
53 | // We just set the flags here since the only valid EFI mode with create\r | |
54 | // is Read+Write+Create.\r | |
55 | flags = EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE;\r | |
56 | }\r | |
57 | return flags;\r | |
58 | }\r | |
59 | \r | |
0c1992fb | 60 | /* Transform the permissions flags into their equivalent UEFI File Attribute bits.\r |
61 | This transformation is most frequently used when translating attributes for use\r | |
62 | by the UEFI EFI_FILE_PROTOCOL.SetInfo() function.\r | |
2aa62f2b | 63 | \r |
64 | The UEFI File attributes are:\r | |
65 | // ******************************************************\r | |
66 | // File Attributes\r | |
67 | // ******************************************************\r | |
68 | #define EFI_FILE_READ_ONLY 0x0000000000000001\r | |
69 | #define EFI_FILE_HIDDEN 0x0000000000000002\r | |
70 | #define EFI_FILE_SYSTEM 0x0000000000000004\r | |
71 | #define EFI_FILE_RESERVED 0x0000000000000008\r | |
72 | #define EFI_FILE_DIRECTORY 0x0000000000000010\r | |
73 | #define EFI_FILE_ARCHIVE 0x0000000000000020\r | |
74 | #define EFI_FILE_VALID_ATTR 0x0000000000000037\r | |
75 | \r | |
0c1992fb | 76 | The input permission flags consist of the following flags:\r |
77 | O_RDONLY -- open for reading only\r | |
78 | O_WRONLY -- open for writing only\r | |
79 | O_RDWR -- open for reading and writing\r | |
80 | O_ACCMODE -- mask for above modes\r | |
81 | O_NONBLOCK -- no delay\r | |
82 | O_APPEND -- set append mode\r | |
83 | O_CREAT -- create if nonexistent\r | |
84 | O_TRUNC -- truncate to zero length\r | |
85 | O_EXCL -- error if already exists\r | |
86 | O_HIDDEN -- Hidden file attribute\r | |
87 | O_SYSTEM -- System file attribute\r | |
88 | O_ARCHIVE -- Archive file attribute\r | |
2aa62f2b | 89 | */\r |
90 | UINT64\r | |
91 | Omode2EFI( int mode)\r | |
92 | {\r | |
93 | UINT64 flags = 0;\r | |
94 | \r | |
0c1992fb | 95 | /* File is Read-Only. */\r |
96 | if((mode & O_ACCMODE) == 0) {\r | |
97 | flags = EFI_FILE_READ_ONLY;\r | |
98 | }\r | |
99 | /* Set the Hidden attribute. */\r | |
100 | if((mode & O_HIDDEN) != 0) {\r | |
101 | flags |= EFI_FILE_HIDDEN;\r | |
102 | }\r | |
103 | /* Set the System attribute. */\r | |
104 | if((mode & O_SYSTEM) != 0) {\r | |
105 | flags |= EFI_FILE_SYSTEM;\r | |
2aa62f2b | 106 | }\r |
0c1992fb | 107 | /* Set the Archive attribute. */\r |
108 | if((mode & O_ARCHIVE) != 0) {\r | |
109 | flags |= EFI_FILE_ARCHIVE;\r | |
2aa62f2b | 110 | }\r |
111 | return flags;\r | |
112 | }\r | |
113 | \r | |
114 | /* Converts the first several EFI status values into the appropriate errno value.\r | |
115 | */\r | |
116 | int\r | |
117 | EFI2errno( RETURN_STATUS Status)\r | |
118 | {\r | |
119 | int retval;\r | |
120 | \r | |
121 | switch(Status) {\r | |
122 | case RETURN_SUCCESS:\r | |
123 | retval = 0;\r | |
124 | break;\r | |
125 | case RETURN_INVALID_PARAMETER:\r | |
126 | retval = EINVAL;\r | |
127 | break;\r | |
128 | case RETURN_UNSUPPORTED:\r | |
129 | retval = ENODEV;\r | |
130 | break;\r | |
131 | case RETURN_BAD_BUFFER_SIZE:\r | |
132 | case RETURN_BUFFER_TOO_SMALL:\r | |
133 | retval = EBUFSIZE;\r | |
134 | break;\r | |
135 | case RETURN_NOT_READY:\r | |
136 | retval = EBUSY;\r | |
137 | break;\r | |
138 | case RETURN_WRITE_PROTECTED:\r | |
139 | retval = EROFS;\r | |
140 | break;\r | |
141 | case RETURN_OUT_OF_RESOURCES: // May be overridden by specific functions\r | |
142 | retval = ENOMEM;\r | |
143 | break;\r | |
144 | case RETURN_VOLUME_FULL:\r | |
145 | retval = ENOSPC;\r | |
146 | break;\r | |
147 | case RETURN_NOT_FOUND:\r | |
148 | case RETURN_NO_MAPPING:\r | |
149 | retval = ENOENT;\r | |
150 | break;\r | |
151 | case RETURN_TIMEOUT:\r | |
152 | retval = ETIMEDOUT;\r | |
153 | break;\r | |
154 | case RETURN_NOT_STARTED:\r | |
155 | retval = EAGAIN;\r | |
156 | break;\r | |
157 | case RETURN_ALREADY_STARTED:\r | |
158 | retval = EALREADY;\r | |
159 | break;\r | |
160 | case RETURN_ABORTED:\r | |
161 | retval = EINTR;\r | |
162 | break;\r | |
163 | case RETURN_ICMP_ERROR:\r | |
164 | case RETURN_TFTP_ERROR:\r | |
165 | case RETURN_PROTOCOL_ERROR:\r | |
166 | retval = EPROTO;\r | |
167 | break;\r | |
168 | case RETURN_INCOMPATIBLE_VERSION:\r | |
169 | retval = EPERM;\r | |
170 | break;\r | |
171 | case RETURN_ACCESS_DENIED:\r | |
172 | case RETURN_SECURITY_VIOLATION:\r | |
173 | retval = EACCES;\r | |
174 | break;\r | |
175 | /* case RETURN_LOAD_ERROR:\r | |
176 | case RETURN_DEVICE_ERROR:\r | |
177 | case RETURN_VOLUME_CORRUPTED:\r | |
178 | case RETURN_NO_MEDIA:\r | |
179 | case RETURN_MEDIA_CHANGED:\r | |
180 | case RETURN_NO_RESPONSE:\r | |
181 | case RETURN_CRC_ERROR:\r | |
182 | case RETURN_END_OF_MEDIA:\r | |
183 | case RETURN_END_OF_FILE:\r | |
184 | case RETURN_INVALID_LANGUAGE:\r | |
185 | */\r | |
186 | default:\r | |
187 | retval = EIO;\r | |
188 | break;\r | |
189 | }\r | |
190 | return retval;\r | |
191 | }\r |