1 /** @file
2   Value transformations between stdio and the UEFI environment.
3 
4   Copyright (c) 2010 - 2011, 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  <Efi/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
Oflags2EFI(int oflags)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(flags & EFI_FILE_MODE_WRITE) {  // Asking for write only?
48     // EFI says the only two RW modes are read-only and read+write.
49     flags = EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE;
50   }
51   if(oflags & (O_CREAT | O_TRUNC)) {            // Now add the Create flag.
52     // Also added if O_TRUNC set since we will need to create a new file.
53     // We just set the flags here since the only valid EFI mode with create
54     // is Read+Write+Create.
55     flags = EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE;
56   }
57   return flags;
58 }
59 
60 /*  Transform the permissions flags into their equivalent UEFI File Attribute bits.
61     This transformation is most frequently used when translating attributes for use
62     by the UEFI EFI_FILE_PROTOCOL.SetInfo() function.
63 
64     The UEFI File attributes are:
65       // ******************************************************
66       // File Attributes
67       // ******************************************************
68       #define EFI_FILE_READ_ONLY         0x0000000000000001
69       #define EFI_FILE_HIDDEN            0x0000000000000002
70       #define EFI_FILE_SYSTEM            0x0000000000000004
71       #define EFI_FILE_RESERVED          0x0000000000000008
72       #define EFI_FILE_DIRECTORY         0x0000000000000010
73       #define EFI_FILE_ARCHIVE           0x0000000000000020
74       #define EFI_FILE_VALID_ATTR        0x0000000000000037
75 
76     The input permission flags consist of the following flags:
77       O_RDONLY    -- open for reading only
78       O_WRONLY    -- open for writing only
79       O_RDWR      -- open for reading and writing
80       O_ACCMODE   -- mask for above modes
81       O_NONBLOCK  -- no delay
82       O_APPEND    -- set append mode
83       O_CREAT     -- create if nonexistent
84       O_TRUNC     -- truncate to zero length
85       O_EXCL      -- error if already exists
86       O_HIDDEN    -- Hidden file attribute
87       O_SYSTEM    -- System file attribute
88       O_ARCHIVE   -- Archive file attribute
89 */
90 UINT64
Omode2EFI(int mode)91 Omode2EFI( int mode)
92 {
93   UINT64  flags = 0;
94 
95   /* File is Read-Only. */
96   if((mode & O_ACCMODE) == 0) {
97     flags = EFI_FILE_READ_ONLY;
98   }
99   /* Set the Hidden attribute. */
100   if((mode & O_HIDDEN) != 0) {
101     flags |= EFI_FILE_HIDDEN;
102   }
103   /* Set the System attribute. */
104   if((mode & O_SYSTEM) != 0) {
105     flags |= EFI_FILE_SYSTEM;
106     }
107   /* Set the Archive attribute. */
108   if((mode & O_ARCHIVE) != 0) {
109     flags |= EFI_FILE_ARCHIVE;
110   }
111   return flags;
112 }
113 
114 /* Converts the first several EFI status values into the appropriate errno value.
115 */
116 int
EFI2errno(RETURN_STATUS Status)117 EFI2errno( RETURN_STATUS Status)
118 {
119   int             retval;
120 
121   switch(Status) {
122     case RETURN_SUCCESS:
123       retval = 0;
124       break;
125     case RETURN_INVALID_PARAMETER:
126       retval = EINVAL;
127       break;
128     case RETURN_UNSUPPORTED:
129       retval = ENODEV;
130       break;
131     case RETURN_BAD_BUFFER_SIZE:
132     case RETURN_BUFFER_TOO_SMALL:
133       retval = EBUFSIZE;
134       break;
135     case RETURN_NOT_READY:
136       retval = EBUSY;
137       break;
138     case RETURN_WRITE_PROTECTED:
139       retval = EROFS;
140       break;
141     case RETURN_OUT_OF_RESOURCES:   // May be overridden by specific functions
142       retval = ENOMEM;
143       break;
144     case RETURN_VOLUME_FULL:
145       retval = ENOSPC;
146       break;
147     case RETURN_NOT_FOUND:
148     case RETURN_NO_MAPPING:
149       retval = ENOENT;
150       break;
151     case RETURN_TIMEOUT:
152       retval = ETIMEDOUT;
153       break;
154     case RETURN_NOT_STARTED:
155       retval = EAGAIN;
156       break;
157     case RETURN_ALREADY_STARTED:
158       retval = EALREADY;
159       break;
160     case RETURN_ABORTED:
161       retval = EINTR;
162       break;
163     case RETURN_ICMP_ERROR:
164     case RETURN_TFTP_ERROR:
165     case RETURN_PROTOCOL_ERROR:
166       retval = EPROTO;
167       break;
168     case RETURN_INCOMPATIBLE_VERSION:
169       retval = EPERM;
170       break;
171     case RETURN_ACCESS_DENIED:
172     case RETURN_SECURITY_VIOLATION:
173       retval = EACCES;
174       break;
175 /*  case RETURN_LOAD_ERROR:
176     case RETURN_DEVICE_ERROR:
177     case RETURN_VOLUME_CORRUPTED:
178     case RETURN_NO_MEDIA:
179     case RETURN_MEDIA_CHANGED:
180     case RETURN_NO_RESPONSE:
181     case RETURN_CRC_ERROR:
182     case RETURN_END_OF_MEDIA:
183     case RETURN_END_OF_FILE:
184     case RETURN_INVALID_LANGUAGE:
185 */
186     default:
187       retval = EIO;
188       break;
189   }
190   return retval;
191 }
192