]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Uefi/SysCalls.c
2 EFI versions of NetBSD system calls.
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.
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.
15 #include <Library/UefiLib.h>
16 #include <Library/UefiBootServicesTableLib.h>
17 #include <Library/BaseLib.h>
18 #include <Library/MemoryAllocationLib.h>
19 #include <Library/ShellLib.h>
21 #include <LibConfig.h>
22 #include <sys/EfiCdefs.h>
31 #include <sys/fcntl.h>
33 #include <sys/syslimits.h>
34 #include <Efi/SysEfi.h>
36 #include <Device/Device.h>
38 #include <extern.h> // Library/include/extern.h: Private to implementation
39 #include <sys/EfiSysCall.h>
41 /* EFI versions of BSD system calls used in stdio */
43 /* Validate that fd refers to a valid file descriptor.
44 IsOpen is interpreted as follows:
45 - Positive fd must be OPEN
46 - Zero fd must be CLOSED
47 - Negative fd may be OPEN or CLOSED
49 @retval TRUE fd is VALID
50 @retval FALSE fd is INVALID
53 ValidateFD( int fd
, int IsOpen
)
55 struct __filedes
*filp
;
56 BOOLEAN retval
= FALSE
;
58 if((fd
>= 0) && (fd
< OPEN_MAX
)) {
59 filp
= &gMD
->fdarray
[fd
];
62 retval
= (BOOLEAN
)((filp
->f_iflags
!= 0) && // TRUE if OPEN
63 FILE_IS_USABLE(filp
)); // and Usable (not Larval or Closing)
64 if(IsOpen
== VALID_CLOSED
) {
65 retval
= (BOOLEAN
)!retval
; // We want TRUE if CLOSED
72 /* Find and reserve a free File Descriptor.
74 Returns the first free File Descriptor greater than or equal to the,
75 already validated, fd specified by Minfd.
77 @return Returns -1 if there are no free FDs. Otherwise returns the
81 FindFreeFD( int MinFd
)
83 struct __filedes
*Mfd
;
89 // Get an available fd
90 for(i
=MinFd
; i
< OPEN_MAX
; ++i
) {
91 if(Mfd
[i
].f_iflags
== 0) {
92 Mfd
[i
].f_iflags
= FIF_LARVAL
; // Temporarily mark this fd as reserved
100 /* Mark that an open file is to be deleted when closed. */
102 DeleteOnClose(int fd
)
106 if(ValidateFD( fd
, VALID_OPEN
)) {
107 gMD
->fdarray
[fd
].f_iflags
|= FIF_DELCLOSE
;
116 /** The isatty() function tests whether fildes, an open file descriptor,
117 is associated with a terminal device.
119 @retval 1 fildes is associated with a terminal.
120 @retval 0 fildes is not associated with a terminal. errno is set to
121 EBADF if fildes is not a valid open FD.
127 struct __filedes
*Fp
;
129 if(ValidateFD( fd
, VALID_OPEN
)) {
130 Fp
= &gMD
->fdarray
[fd
];
131 retval
= Fp
->f_iflags
& _S_ITTY
;
143 const struct fileops
*FileOps
;
147 if(ValidateFD( fd
, VALID_OPEN
)) {
148 FileOps
= gMD
->fdarray
[fd
].f_ops
;
149 DevData
= gMD
->fdarray
[fd
].devdata
;
150 for(i
=0; i
< OPEN_MAX
; ++i
) {
151 if(i
== fd
) continue;
152 if(ValidateFD( i
, VALID_OPEN
)) { // TRUE if fd is valid and OPEN
153 if((gMD
->fdarray
[i
].f_ops
== FileOps
)
154 &&(gMD
->fdarray
[i
].devdata
== DevData
)) {
165 _closeX (int fd
, int NewState
)
167 struct __filedes
*Fp
;
170 // Verify my pointers and get my FD.
171 if(ValidateFD( fd
, VALID_OPEN
)) {
172 Fp
= &gMD
->fdarray
[fd
];
173 // Check if there are other users of this FileHandle
174 if(Fp
->RefCount
== 1) { // There should be no other users
176 // Only do the close if no one else is using the FileHandle
177 if(Fp
->f_iflags
& FIF_DELCLOSE
) {
178 /* Handle files marked "Delete on Close". */
179 if(Fp
->f_ops
->fo_delete
!= NULL
) {
180 retval
= Fp
->f_ops
->fo_delete(Fp
);
184 retval
= Fp
->f_ops
->fo_close( Fp
);
187 Fp
->f_iflags
= NewState
; // Close this FD or reserve it
188 Fp
->RefCount
= 0; // No one using this FD
191 --Fp
->RefCount
; /* One less user of this FD */
202 /** The close() function shall deallocate the file descriptor indicated by fd.
203 To deallocate means to make the file descriptor available for return by
204 subsequent calls to open() or other functions that allocate file
205 descriptors. All outstanding record locks owned by the process on the file
206 associated with the file descriptor shall be removed (that is, unlocked).
208 @return Upon successful completion, 0 shall be returned; otherwise,
209 -1 shall be returned and errno set to indicate the error.
214 //Print(L"Closing fd %d\n", fd);
215 return _closeX(fd
, 0);
221 unlink (const char *path
)
223 struct __filedes
*Fp
;
227 EFIerrno
= RETURN_SUCCESS
;
229 fd
= open(path
, O_WRONLY
, 0);
231 Fp
= &gMD
->fdarray
[fd
];
233 if(Fp
->f_ops
->fo_delete
!= NULL
) {
234 retval
= Fp
->f_ops
->fo_delete(Fp
);
236 Fp
->f_iflags
= 0; // Close this FD
237 Fp
->RefCount
= 0; // No one using this FD
242 /** The fcntl() function shall perform the operations described below on open
243 files. The fildes argument is a file descriptor.
245 The available values for cmd are defined in <fcntl.h> and are as follows:
246 - F_DUPFD - Return a new file descriptor which shall be the lowest
247 numbered available (that is, not already open) file
248 descriptor greater than or equal to the third argument, arg,
249 taken as an integer of type int. The new file descriptor
250 shall refer to the same open file description as the original
251 file descriptor, and shall share any locks. The FD_CLOEXEC
252 flag associated with the new file descriptor shall be cleared
253 to keep the file open across calls to one of the exec functions.
254 - F_GETFD - Get the file descriptor flags defined in <fcntl.h> that are
255 associated with the file descriptor fildes. File descriptor
256 flags are associated with a single file descriptor and do not
257 affect other file descriptors that refer to the same file.
258 - F_SETFD - Set the file descriptor flags defined in <fcntl.h>, that are
259 associated with fildes, to the third argument, arg, taken
260 as type int. If the FD_CLOEXEC flag in the third argument
261 is 0, the file shall remain open across the exec
262 functions; otherwise, the file shall be closed upon
263 successful execution of one of the exec functions.
264 - F_GETFL - Get the file status flags and file access modes, defined in
265 <fcntl.h>, for the file description associated with fildes.
266 The file access modes can be extracted from the return
267 value using the mask O_ACCMODE, which is defined in
268 <fcntl.h>. File status flags and file access modes are
269 associated with the file description and do not affect
270 other file descriptors that refer to the same file with
271 different open file descriptions.
272 - F_SETFL - Set the file status flags, defined in <fcntl.h>, for the file
273 description associated with fildes from the corresponding
274 bits in the third argument, arg, taken as type int. Bits
275 corresponding to the file access mode and the file creation
276 flags, as defined in <fcntl.h>, that are set in arg shall
277 be ignored. If any bits in arg other than those mentioned
278 here are changed by the application, the result is unspecified.
279 - F_GETOWN - If fildes refers to a socket, get the process or process group
280 ID specified to receive SIGURG signals when out-of-band
281 data is available. Positive values indicate a process ID;
282 negative values, other than -1, indicate a process group
283 ID. If fildes does not refer to a socket, the results are
285 - F_SETOWN - If fildes refers to a socket, set the process or process
286 group ID specified to receive SIGURG signals when
287 out-of-band data is available, using the value of the third
288 argument, arg, taken as type int. Positive values indicate
289 a process ID; negative values, other than -1, indicate a
290 process group ID. If fildes does not refer to a socket, the
291 results are unspecified.
293 The fcntl() function shall fail if:
295 [EBADF] The fildes argument is not a valid open file descriptor.
296 [EINVAL] The cmd argument is invalid, or the cmd argument is F_DUPFD
297 and arg is negative or greater than or equal to {OPEN_MAX}.
298 [EMFILE] The argument cmd is F_DUPFD and {OPEN_MAX} file descriptors
299 are currently open in the calling process, or no file
300 descriptors greater than or equal to arg are available.
301 [EOVERFLOW] One of the values to be returned cannot be represented correctly.
303 @return Upon successful completion, the value returned shall depend on
305 - F_DUPFD - A new file descriptor.
306 - F_GETFD - Value of flags defined in <fcntl.h>. The return value
307 shall not be negative.
308 - F_SETFD - Value other than -1.
309 - F_GETFL - Value of file status flags and access modes. The return
310 value is not negative.
311 - F_SETFL - Value other than -1.
312 - F_GETOWN - Value of the socket owner process or process group;
314 - F_SETOWN - Value other than -1.
315 Otherwise, -1 shall be returned and errno set to indicate the error.
319 fcntl (int fildes
, int cmd
, ...)
322 struct __filedes
*MyFd
;
326 //Print(L"%a( %d, %d, ...)\n", __func__, fildes, cmd);
329 if(ValidateFD( fildes
, VALID_OPEN
)) {
330 MyFd
= &gMD
->fdarray
[fildes
];
334 temp
= va_arg(p3
, int);
335 if(ValidateFD( temp
, VALID_DONT_CARE
)) {
336 temp
= FindFreeFD( temp
);
341 /* temp is now a valid fd reserved for further use
342 so copy fd into temp.
344 (void)memcpy(&gMD
->fdarray
[temp
], MyFd
, sizeof(struct __filedes
));
353 retval
= MyFd
->Oflags
; // Get original value
354 temp
= va_arg(p3
, int);
355 temp
&= O_SETMASK
; // Only certain bits can be set
356 temp
|= retval
& O_SETMASK
;
357 MyFd
->Oflags
= temp
; // Set new value
361 retval
= MyFd
->f_iflags
;
364 // retval = MyFd->SocProc;
365 // MyFd->SocProc = va_arg(p3, int);
368 //retval = MyFd->Oflags;
369 retval
= MyFd
->f_iflags
;
372 //retval = MyFd->f_iflags;
373 retval
= MyFd
->Oflags
;
376 // retval = MyFd->SocProc;
391 /** The dup() function provides an alternative interface to the
392 service provided by fcntl() using the F_DUPFD command. The call:
394 shall be equivalent to:
395 - fid = fcntl(fildes, F_DUPFD, 0);
397 @return Upon successful completion a non-negative integer, namely the
398 file descriptor, shall be returned; otherwise, -1 shall be
399 returned and errno set to indicate the error.
404 return fcntl(fildes
, F_DUPFD
, 0);
407 /** The dup2() function provides an alternative interface to the
408 service provided by fcntl() using the F_DUPFD command. The call:
409 - fid = dup2(fildes, fildes2);
410 shall be equivalent to:
412 - fid = fcntl(fildes, F_DUPFD, fildes2);
413 except for the following:
414 - If fildes2 is less than 0 or greater than or equal to {OPEN_MAX},
415 dup2() shall return -1 with errno set to [EBADF].
416 - If fildes is a valid file descriptor and is equal to fildes2, dup2()
417 shall return fildes2 without closing it.
418 - If fildes is not a valid file descriptor, dup2() shall return -1 and
419 shall not close fildes2.
420 - The value returned shall be equal to the value of fildes2 upon
421 successful completion, or -1 upon failure.
423 @return Upon successful completion a non-negative integer, namely
424 fildes2, shall be returned; otherwise, -1 shall be
425 returned and errno set to EBADF indicate the error.
428 dup2 (int fildes
, int fildes2
)
432 if(ValidateFD( fildes
, VALID_OPEN
)) {
434 if( fildes
!= fildes2
) {
435 if(ValidateFD( fildes2
, VALID_DONT_CARE
)) {
436 gMD
->fdarray
[fildes2
].f_iflags
= FIF_LARVAL
; // Mark the file closed, but reserved
437 (void)memcpy(&gMD
->fdarray
[fildes2
], // Duplicate fildes into fildes2
438 &gMD
->fdarray
[fildes
], sizeof(struct __filedes
));
439 gMD
->fdarray
[fildes2
].MyFD
= (UINT16
)fildes2
;
453 /** Reposition a file's read/write offset.
455 The lseek() function repositions the offset of the file descriptor fildes
456 to the argument offset according to the directive how. The argument
457 fildes must be an open file descriptor. lseek() repositions the file
458 pointer fildes as follows:
460 If how is SEEK_SET, the offset is set to offset bytes.
462 If how is SEEK_CUR, the offset is set to its current location
465 If how is SEEK_END, the offset is set to the size of the file
468 The lseek() function allows the file offset to be set beyond the end of
469 the existing end-of-file of the file. If data is later written at this
470 point, subsequent reads of the data in the gap return bytes of zeros
471 (until data is actually written into the gap).
473 Some devices are incapable of seeking. The value of the pointer associ-
474 ated with such a device is undefined.
476 @return Upon successful completion, lseek() returns the resulting offset
477 location as measured in bytes from the beginning of the file.
478 Otherwise, a value of -1 is returned and errno is set to
482 lseek (int fd
, __off_t offset
, int how
)
485 // RETURN_STATUS Status = RETURN_SUCCESS;
486 struct __filedes
*filp
;
488 EFIerrno
= RETURN_SUCCESS
; // In case of error without an EFI call
490 if( how
== SEEK_SET
|| how
== SEEK_CUR
|| how
== SEEK_END
) {
491 if(ValidateFD( fd
, VALID_OPEN
)) {
492 filp
= &gMD
->fdarray
[fd
];
493 // Both of our parameters have been verified as valid
494 CurPos
= filp
->f_ops
->fo_lseek( filp
, offset
, how
);
496 filp
->f_offset
= CurPos
;
500 errno
= EBADF
; // Bad File Descriptor
504 errno
= EINVAL
; // Invalid how argument
509 /** The directory path is created with the access permissions specified by
512 The directory is closed after it is created.
514 @retval 0 The directory was created successfully.
515 @retval -1 An error occurred and error codes are stored in errno and EFIerrno.
518 mkdir (const char *path
, __mode_t perms
)
523 RETURN_STATUS Status
;
527 Status
= ParsePath(path
, &NewPath
, &Node
, &Instance
, NULL
);
528 if(Status
== RETURN_SUCCESS
) {
529 GenI
= Node
->InstanceList
;
535 //GenI += (Instance * Node->InstanceSize);
536 retval
= ((GenericInstance
*)GenI
)->Abstraction
.fo_mkdir( path
, perms
);
548 The EFI ShellOpenFileByName() function is used to perform the low-level
549 file open operation. The primary task of open() is to translate from the
550 flags used in the <stdio.h> environment to those used by the EFI function.
552 The only valid flag combinations for ShellOpenFileByName() are:
557 The mode value is saved in the FD to indicate permissions for further operations.
559 O_RDONLY -- flags = EFI_FILE_MODE_READ -- this is always done
560 O_WRONLY -- flags |= EFI_FILE_MODE_WRITE
561 O_RDWR -- flags |= EFI_FILE_MODE_WRITE -- READ is already set
563 O_NONBLOCK -- ignored
564 O_APPEND -- Seek to EOF before every write
565 O_CREAT -- flags |= EFI_FILE_MODE_CREATE
566 O_TRUNC -- delete first then create new
567 O_EXCL -- if O_CREAT is also set, open will fail if the file already exists.
579 struct __filedes
*filp
;
581 RETURN_STATUS Status
;
586 Status
= ParsePath(path
, &NewPath
, &Node
, &Instance
, &MPath
);
587 if(Status
== RETURN_SUCCESS
) {
589 (Node
->InstanceList
== NULL
)) {
593 // Could add a test to see if the file name begins with a period.
594 // If it does, then add the HIDDEN flag to Attributes.
596 // Get an available fd
597 fd
= FindFreeFD( VALID_CLOSED
);
600 // All available FDs are in use
604 filp
= &gMD
->fdarray
[fd
];
605 // Save the flags and mode in the File Descriptor
606 filp
->Oflags
= oflags
;
609 doresult
= Node
->OpenFunc(Node
, filp
, Instance
, NewPath
, MPath
);
611 filp
->f_iflags
= 0; // Release this FD
612 fd
= -1; // Indicate an error
615 // Re-use OpenMode in order to build our final f_iflags value
616 OpenMode
= ( mode
& S_ACC_READ
) ? S_ACC_READ
: 0;
617 OpenMode
|= ( mode
& S_ACC_WRITE
) ? S_ACC_WRITE
: 0;
619 filp
->f_iflags
|= (UINT32
)OpenMode
;
621 FILE_SET_MATURE(filp
);
625 if(NewPath
!= NULL
) {
630 free(MPath
); // We don't need this any more.
632 // return the fd of our now open file
638 Poll a list of file descriptors.
640 The ::poll routine waits for up to timeout milliseconds for an event
641 to occur on one or more of the file descriptors listed. The event
642 types of interested are specified for each file descriptor in the events
643 field. The actual event detected is returned in the revents field of
645 <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/poll.html">POSIX</a>
646 documentation is available online.
648 @param [in] pfd Address of an array of pollfd structures.
650 @param [in] nfds Number of elements in the array of pollfd structures.
652 @param [in] timeout Length of time in milliseconds to wait for the event
654 @returns The number of file descriptors with detected events. Zero
655 indicates that the call timed out and -1 indicates an error.
665 struct __filedes
* pDescriptor
;
666 struct pollfd
* pEnd
;
667 struct pollfd
* pPollFD
;
674 // Create the timer for the timeout
677 Status
= EFI_SUCCESS
;
678 if ( INFTIM
!= timeout
) {
679 Status
= gBS
->CreateEvent ( EVT_TIMER
,
684 if ( !EFI_ERROR ( Status
)) {
686 // Start the timeout timer
688 TimerTicks
= timeout
;
689 TimerTicks
*= 1000 * 10;
690 Status
= gBS
->SetTimer ( Timer
,
699 if ( !EFI_ERROR ( Status
)) {
701 // Poll until an event is detected or the timer fires
707 // Poll the list of file descriptors
710 pEnd
= &pPollFD
[ nfds
];
711 while ( pEnd
> pPollFD
) {
713 // Validate the file descriptor
715 if ( !ValidateFD ( pPollFD
->fd
, VALID_OPEN
)) {
721 // Poll the device or file
723 pDescriptor
= &gMD
->fdarray
[ pPollFD
->fd
];
724 pPollFD
->revents
= pDescriptor
->f_ops
->fo_poll ( pDescriptor
,
728 // Determine if this file descriptor detected an event
730 if ( 0 != pPollFD
->revents
) {
732 // Select this descriptor
738 // Set the next file descriptor
746 if ( NULL
!= Timer
) {
747 Status
= gBS
->CheckEvent ( Timer
);
748 if ( EFI_SUCCESS
== Status
) {
754 else if ( EFI_NOT_READY
== Status
) {
755 Status
= EFI_SUCCESS
;
758 } while (( 0 == SelectedFDs
)
759 && ( EFI_SUCCESS
== Status
));
764 if ( NULL
!= Timer
) {
765 gBS
->SetTimer ( Timer
,
778 if ( NULL
!= Timer
) {
779 gBS
->CloseEvent ( Timer
);
783 // Return the number of selected file system descriptors
789 /** The rename() function changes the name of a file.
790 The old argument points to the pathname of the file to be renamed. The new
791 argument points to the new pathname of the file.
793 If the old argument points to the pathname of a file that is not a
794 directory, the new argument shall not point to the pathname of a
795 directory. If the file named by the new argument exists, it shall be
796 removed and old renamed to new. Write access permission is required for
797 both the directory containing old and the directory containing new.
799 If the old argument points to the pathname of a directory, the new
800 argument shall not point to the pathname of a file that is not a
801 directory. If the directory named by the new argument exists, it shall be
802 removed and old renamed to new.
804 The new pathname shall not contain a path prefix that names old. Write
805 access permission is required for the directory containing old and the
806 directory containing new. If the old argument points to the pathname of a
807 directory, write access permission may be required for the directory named
808 by old, and, if it exists, the directory named by new.
810 If the rename() function fails for any reason other than [EIO], any file
811 named by new shall be unaffected.
813 @return Upon successful completion, rename() shall return 0; otherwise,
814 -1 shall be returned, errno shall be set to indicate the error,
815 and neither the file named by old nor the file named by new
816 shall be changed or created.
825 DeviceNode
*FromNode
;
828 RETURN_STATUS Status
;
831 Status
= ParsePath(from
, &FromPath
, &FromNode
, &Instance
, NULL
);
832 if(Status
== RETURN_SUCCESS
) {
833 GenI
= FromNode
->InstanceList
;
839 //GenI += (Instance * FromNode->InstanceSize);
840 retval
= ((GenericInstance
*)GenI
)->Abstraction
.fo_rename( from
, to
);
854 struct __filedes
*filp
;
858 fd
= open(path
, O_RDWR
, 0);
860 filp
= &gMD
->fdarray
[fd
];
862 retval
= filp
->f_ops
->fo_rmdir(filp
);
867 /** The fstat() function obtains information about an open file associated
868 with the file descriptor fildes, and shall write it to the area pointed to
871 The buf argument is a pointer to a stat structure, as defined
872 in <sys/stat.h>, into which information is placed concerning the file.
874 The structure members st_mode, st_ino, st_dev, st_uid, st_gid, st_atime,
875 st_ctime, and st_mtime shall have meaningful values. The value of the
876 member st_nlink shall be set to the number of links to the file.
878 The fstat() function shall update any time-related fields before writing
879 into the stat structure.
881 The fstat() function is implemented using the ShellGetFileInfo()
884 The stat structure members which don't have direct analogs to EFI file
885 information are filled in as follows:
886 - st_mode Populated with information from fildes
887 - st_ino Set to zero. (inode)
888 - st_dev Set to zero.
889 - st_uid Set to zero.
890 - st_gid Set to zero.
891 - st_nlink Set to one.
893 @param[in] fd File descriptor as returned from open().
894 @param[out] statbuf Buffer in which the file status is put.
896 @retval 0 Successful Completion.
897 @retval -1 An error has occurred and errno has been set to
901 fstat (int fd
, struct stat
*statbuf
)
904 struct __filedes
*filp
;
906 if(ValidateFD( fd
, VALID_OPEN
)) {
907 filp
= &gMD
->fdarray
[fd
];
908 retval
= filp
->f_ops
->fo_stat(filp
, statbuf
, NULL
);
916 /** Obtains information about the file pointed to by path.
918 Opens the file pointed to by path, calls _EFI_FileInfo with the file's handle,
919 then closes the file.
921 @retval 0 Successful Completion.
922 @retval -1 An error has occurred and errno has been set to
926 stat (const char *path
, void *statbuf
)
930 struct __filedes
*filp
;
932 fd
= open(path
, O_RDONLY
, 0);
934 filp
= &gMD
->fdarray
[fd
];
935 retval
= filp
->f_ops
->fo_stat( filp
, statbuf
, NULL
);
941 /** Same as stat since EFI doesn't have symbolic links. **/
943 lstat (const char *path
, struct stat
*statbuf
)
945 return stat(path
, statbuf
);
948 /** Control a device.
953 unsigned long request
,
958 struct __filedes
*filp
;
961 va_start(argp
, request
);
963 if(ValidateFD( fd
, VALID_OPEN
)) {
964 filp
= &gMD
->fdarray
[fd
];
965 retval
= filp
->f_ops
->fo_ioctl(filp
, request
, argp
);
975 /** Read from a file.
977 The read() function shall attempt to read nbyte bytes from the file
978 associated with the open file descriptor, fildes, into the buffer pointed
981 Before any action described below is taken, and if nbyte is zero, the
982 read() function may detect and return errors as described below. In the
983 absence of errors, or if error detection is not performed, the read()
984 function shall return zero and have no other results.
986 On files that support seeking (for example, a regular file), the read()
987 shall start at a position in the file given by the file offset associated
988 with fildes. The file offset shall be incremented by the number of bytes
991 Files that do not support seeking - for example, terminals - always read
992 from the current position. The value of a file offset associated with
993 such a file is undefined.
995 No data transfer shall occur past the current end-of-file. If the
996 starting position is at or after the end-of-file, 0 shall be returned.
998 The read() function reads data previously written to a file. If any
999 portion of a regular file prior to the end-of-file has not been written,
1000 read() shall return bytes with value 0. For example, lseek() allows the
1001 file offset to be set beyond the end of existing data in the file. If data
1002 is later written at this point, subsequent reads in the gap between the
1003 previous end of data and the newly written data shall return bytes with
1004 value 0 until data is written into the gap.
1006 Upon successful completion, where nbyte is greater than 0, read() shall
1007 mark for update the st_atime field of the file, and shall return the
1008 number of bytes read. This number shall never be greater than nbyte. The
1009 value returned may be less than nbyte if the number of bytes left in the
1010 file is less than nbyte, if the read() request was interrupted by a
1011 signal, or if the file is a pipe or FIFO or special file and has fewer
1012 than nbyte bytes immediately available for reading. For example, a read()
1013 from a file associated with a terminal may return one typed line of data.
1015 If fildes does not refer to a directory, the function reads the requested
1016 number of bytes from the file at the file's current position and returns
1017 them in buf. If the read goes beyond the end of the file, the read
1018 length is truncated to the end of the file. The file's current position is
1019 increased by the number of bytes returned.
1021 If fildes refers to a directory, the function reads the directory entry at
1022 the file's current position and returns the entry in buf. If buf
1023 is not large enough to hold the current directory entry, then
1024 errno is set to EBUFSIZE, EFIerrno is set to EFI_BUFFER_TOO_SMALL, and the
1025 current file position is not updated. The size of the buffer needed to read
1026 the entry will be returned as a negative number. On success, the current
1027 position is updated to the next directory entry. If there are no more
1028 directory entries, the read returns a zero-length buffer.
1029 EFI_FILE_INFO is the structure returned as the directory entry.
1031 @return Upon successful completion, read() returns a non-negative integer
1032 indicating the number of bytes actually read. Otherwise, the
1033 functions return a negative value and sets errno to indicate the
1034 error. If errno is EBUFSIZE, the absolute value of the
1035 return value indicates the size of the buffer needed to read
1036 the directory entry.
1039 read (int fildes
, void *buf
, size_t nbyte
)
1041 struct __filedes
*filp
;
1044 BufSize
= (ssize_t
)nbyte
;
1045 if(ValidateFD( fildes
, VALID_OPEN
)) {
1046 filp
= &gMD
->fdarray
[fildes
];
1048 BufSize
= filp
->f_ops
->fo_read(filp
, &filp
->f_offset
, nbyte
, buf
);
1057 /** Write data to a file.
1059 This function writes the specified number of bytes to the file at the current
1060 file position. The current file position is advanced the actual number of bytes
1061 written, which is returned in BufferSize. Partial writes only occur when there
1062 has been a data error during the write attempt (such as "volume space full").
1063 The file is automatically grown to hold the data if required. Direct writes to
1064 opened directories are not supported.
1066 If fildes refers to a terminal device, isatty() returns TRUE, a partial write
1067 will occur if a NULL or EOF character is encountered before n characters have
1068 been written. Characters inserted due to line-end translations will not be
1069 counted. Unconvertable characters are translated into the UEFI character
1070 BLOCKELEMENT_LIGHT_SHADE.
1072 Since the UEFI console device works on wide characters, the buffer is assumed
1073 to contain a single-byte character stream which is then translated to wide
1074 characters using the btowc() functions. The resulting wide character stream
1075 is what is actually sent to the UEFI console.
1077 QUESTION: Should writes to stdout or stderr always succeed?
1080 write (int fd
, const void *buf
, size_t nbyte
)
1082 struct __filedes
*filp
;
1084 // EFI_FILE_HANDLE FileHandle;
1085 // RETURN_STATUS Status = RETURN_SUCCESS;
1087 BufSize
= (ssize_t
)nbyte
;
1089 if(ValidateFD( fd
, VALID_OPEN
)) {
1090 filp
= &gMD
->fdarray
[fd
];
1092 BufSize
= filp
->f_ops
->fo_write(filp
, &filp
->f_offset
, nbyte
, buf
);
1101 /** Gets the current working directory.
1103 The getcwd() function shall place an absolute pathname of the current
1104 working directory in the array pointed to by buf, and return buf. The
1105 pathname copied to the array shall contain no components that are
1106 symbolic links. The size argument is the size in bytes of the character
1107 array pointed to by the buf argument.
1109 @param[in,out] buf The buffer to fill.
1110 @param[in] size The number of bytes in buffer.
1112 @retval NULL The function failed.
1113 @retval NULL Buf was NULL.
1114 @retval NULL Size was 0.
1115 @return buf The function completed successfully. See errno for info.
1118 *getcwd (char *buf
, size_t size
)
1122 if (size
== 0 || buf
== NULL
) {
1127 Cwd
= ShellGetCurrentDir(NULL
);
1132 if (size
< ((StrLen (Cwd
) + 1) * sizeof (CHAR8
))) {
1137 return (UnicodeStrToAsciiStr(Cwd
, buf
));
1140 /** Change the current working directory.
1142 The chdir() function shall cause the directory named by the pathname
1143 pointed to by the path argument to become the current working directory;
1144 that is, the starting point for path searches for pathnames not beginning
1147 @param[in] path The new path to set.
1149 @todo Add non-shell CWD changing.
1152 chdir (const char *path
)
1156 CHAR16
*UnicodePath
;
1158 Cwd
= ShellGetCurrentDir(NULL
);
1160 /* We have shell support */
1161 UnicodePath
= AllocatePool(((AsciiStrLen (path
) + 1) * sizeof (CHAR16
)));
1162 if (UnicodePath
== NULL
) {
1166 AsciiStrToUnicodeStr(path
, UnicodePath
);
1167 Status
= gEfiShellProtocol
->SetCurDir(NULL
, UnicodePath
);
1168 FreePool(UnicodePath
);
1169 if (EFI_ERROR(Status
)) {
1177 /* Add here for non-shell */
1182 pid_t
tcgetpgrp (int x
)
1184 return ((pid_t
)(UINTN
)(gImageHandle
));
1189 return ((pid_t
)(UINTN
)(gImageHandle
));