]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Uefi/Xform.c
Standard Libraries for EDK II.
[mirror_edk2.git] / StdLib / LibC / Uefi / Xform.c
1 /** @file
2 Value transformations between stdio and the UEFI environment.
3
4 Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available under
6 the terms and conditions of the BSD License that accompanies this distribution.
7 The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14 #include <Uefi.h>
15
16 #include <LibConfig.h>
17 #include <sys/EfiCdefs.h>
18
19 #include <errno.h>
20 #include <fcntl.h>
21 #include "SysEfi.h"
22
23 /** Translate the Open flags into a Uefi Open Modes value.
24
25 The Open Flags are:
26 O_RDONLY, O_WRONLY, O_RDWR // Pick only one
27
28 O_NONBLOCK, O_APPEND, O_CREAT, O_TRUNC, O_EXCL // ORed with one of the previous
29
30 The UEFI Open modes are:
31 // ******************************************************
32 // Open Modes
33 // ******************************************************
34 #define EFI_FILE_MODE_READ 0x0000000000000001
35 #define EFI_FILE_MODE_WRITE 0x0000000000000002
36 #define EFI_FILE_MODE_CREATE 0x8000000000000000
37
38
39 */
40 UINT64
41 Oflags2EFI( int oflags )
42 {
43 UINT64 flags;
44
45 // Build the Open Modes
46 flags = (UINT64)((oflags & O_ACCMODE) + 1); // Handle the Read/Write flags
47 if(oflags & (O_CREAT | O_TRUNC)) { // Now add the Create flag.
48 // Also added if O_TRUNC set since we will need to create a new file.
49 // We just set the flags here since the only valid EFI mode with create
50 // is Read+Write+Create.
51 flags = EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE;
52 }
53 return flags;
54 }
55
56 /* Transform the permissions flags from the open() call into the
57 Attributes bits needed by UEFI.
58
59 The UEFI File attributes are:
60 // ******************************************************
61 // File Attributes
62 // ******************************************************
63 #define EFI_FILE_READ_ONLY 0x0000000000000001
64 #define EFI_FILE_HIDDEN 0x0000000000000002
65 #define EFI_FILE_SYSTEM 0x0000000000000004
66 #define EFI_FILE_RESERVED 0x0000000000000008
67 #define EFI_FILE_DIRECTORY 0x0000000000000010
68 #define EFI_FILE_ARCHIVE 0x0000000000000020
69 #define EFI_FILE_VALID_ATTR 0x0000000000000037
70
71 The input permission flags consist of two groups:
72 ( S_IRUSR | S_IRGRP | S_IROTH ) -- S_ACC_READ
73 ( S_IWUSR | S_IWGRP | S_IWOTH ) -- S_ACC_WRITE
74
75 The only thing we can set, at this point, is whether or not
76 this is a SYSTEM file. If the group and other bits are
77 zero and the user bits are non-zero then set SYSTEM. Otherwise
78 the attributes are zero.
79
80 The attributes can be set later using fcntl().
81 */
82 UINT64
83 Omode2EFI( int mode)
84 {
85 UINT64 flags = 0;
86
87 if((mode & (S_IRWXG | S_IRWXO)) == 0) {
88 if((mode & S_IRWXU) != 0) {
89 // Only user permissions so set system
90 flags = EFI_FILE_SYSTEM;
91 }
92 }
93 return flags;
94 }
95
96 /* Converts the first several EFI status values into the appropriate errno value.
97 */
98 int
99 EFI2errno( RETURN_STATUS Status)
100 {
101 int retval;
102
103 switch(Status) {
104 case RETURN_SUCCESS:
105 retval = 0;
106 break;
107 case RETURN_INVALID_PARAMETER:
108 retval = EINVAL;
109 break;
110 case RETURN_UNSUPPORTED:
111 retval = ENODEV;
112 break;
113 case RETURN_BAD_BUFFER_SIZE:
114 case RETURN_BUFFER_TOO_SMALL:
115 retval = EBUFSIZE;
116 break;
117 case RETURN_NOT_READY:
118 retval = EBUSY;
119 break;
120 case RETURN_WRITE_PROTECTED:
121 retval = EROFS;
122 break;
123 case RETURN_OUT_OF_RESOURCES: // May be overridden by specific functions
124 retval = ENOMEM;
125 break;
126 case RETURN_VOLUME_FULL:
127 retval = ENOSPC;
128 break;
129 case RETURN_NOT_FOUND:
130 case RETURN_NO_MAPPING:
131 retval = ENOENT;
132 break;
133 case RETURN_TIMEOUT:
134 retval = ETIMEDOUT;
135 break;
136 case RETURN_NOT_STARTED:
137 retval = EAGAIN;
138 break;
139 case RETURN_ALREADY_STARTED:
140 retval = EALREADY;
141 break;
142 case RETURN_ABORTED:
143 retval = EINTR;
144 break;
145 case RETURN_ICMP_ERROR:
146 case RETURN_TFTP_ERROR:
147 case RETURN_PROTOCOL_ERROR:
148 retval = EPROTO;
149 break;
150 case RETURN_INCOMPATIBLE_VERSION:
151 retval = EPERM;
152 break;
153 case RETURN_ACCESS_DENIED:
154 case RETURN_SECURITY_VIOLATION:
155 retval = EACCES;
156 break;
157 /* case RETURN_LOAD_ERROR:
158 case RETURN_DEVICE_ERROR:
159 case RETURN_VOLUME_CORRUPTED:
160 case RETURN_NO_MEDIA:
161 case RETURN_MEDIA_CHANGED:
162 case RETURN_NO_RESPONSE:
163 case RETURN_CRC_ERROR:
164 case RETURN_END_OF_MEDIA:
165 case RETURN_END_OF_FILE:
166 case RETURN_INVALID_LANGUAGE:
167 */
168 default:
169 retval = EIO;
170 break;
171 }
172 return retval;
173 }