]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Uefi/Devices/Utility/DevGenisis.c
2bdb33ac53bdb5e5b8d394b65d9894810e46e248
[mirror_edk2.git] / StdLib / LibC / Uefi / Devices / Utility / DevGenisis.c
1 /** @file
2 Device Abstraction: device creation utility functions.
3
4 Copyright (c) 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 #include <Uefi.h>
14 #include <Library/BaseLib.h>
15 #include <Library/MemoryAllocationLib.h>
16
17 #include <LibConfig.h>
18
19 #include <errno.h>
20 #include <sys/poll.h>
21 #include <kfile.h>
22 #include <Device/Device.h>
23 #include <MainData.h>
24
25 LIST_ENTRY daDeviceList = INITIALIZE_LIST_HEAD_VARIABLE(daDeviceList);
26 DeviceNode *daDefaultDevice = NULL; ///< Device to use if nothing else found
27 DeviceNode *daRootDevice = NULL; ///< Device containing the root file system
28 DeviceNode *daCurrentDevice = NULL; ///< Device currently being accessed
29
30 /* Commonly used fileops
31 fnullop_* Does nothing and returns success.
32 fbadop_* Does nothing and returns EPERM
33 */
34 int fnullop_fcntl (struct __filedes *filp, UINT32 Cmd, void *p3, void *p4)
35 { return 0; }
36
37 short fnullop_poll (struct __filedes *filp, short Events)
38 {
39 return ((POLLIN | POLLRDNORM | POLLOUT) & Events);
40 }
41
42 int fnullop_flush (struct __filedes *filp)
43 { return 0; }
44
45 int fbadop_stat (struct __filedes *filp, struct stat *StatBuf, void *Buf)
46 { return -EPERM; }
47
48 int fbadop_ioctl (struct __filedes *filp, ULONGN Cmd, void *argp)
49 { return -EPERM; }
50
51 int fbadop_delete (struct __filedes *filp)
52 { return -EPERM; }
53
54 int fbadop_mkdir (const char *path, __mode_t perms)
55 { return -EPERM; }
56
57 int fbadop_rename (const char *from, const char *to)
58 { return -EPERM; }
59
60 int fbadop_rmdir (struct __filedes *filp)
61 { return -EPERM; }
62
63 /** Add a new device to the device list.
64 If both DevName and DevProto are NULL, register this as the Default device.
65
66 @param DevName Name of the device to add.
67 @param DevProto Pointer to the GUID identifying the protocol associated with this device.
68 If DevProto is NULL, startup code will not try to find instances
69 of this device.
70 @param OpenFunc Pointer to the device's Open function.
71 @param InstanceList Optional pointer to the device's initialized instance list.
72 If InstanceList is NULL, the application startup code will
73 scan for instances of the protocol identified by DevProto and
74 populate the InstanceList in the order those protocols are found.
75 @param NumInstance Number of instances in InstanceList.
76 @param Modes Bit-mapped flags indicating operations (R, W, RW, ...) permitted to this device.
77
78 **/
79 DeviceNode *
80 EFIAPI
81 __DevRegister(
82 IN const CHAR16 *DevName,
83 IN GUID *DevProto,
84 IN FO_OPEN OpenFunc,
85 IN void *InstanceList,
86 IN int NumInstance,
87 IN UINT32 InstanceSize,
88 IN UINT32 Modes
89 )
90 {
91 DeviceNode *Node;
92 GenericInstance *GIp;
93 char *GenPtr;
94 int i;
95
96 /* Validate parameters */
97 if(((DevName == NULL) && (DevProto != NULL)) ||
98 (OpenFunc == NULL)) {
99 EFIerrno = RETURN_INVALID_PARAMETER;
100 return NULL;
101 }
102 Node = (DeviceNode *)AllocateZeroPool(sizeof(DeviceNode));
103 if(Node == NULL) {
104 EFIerrno = RETURN_OUT_OF_RESOURCES;
105 return NULL;
106 }
107
108 Node->DevName = DevName;
109 Node->DevProto = DevProto;
110 Node->InstanceList = InstanceList;
111 Node->OpenFunc = OpenFunc;
112 Node->InstanceSize = InstanceSize;
113 Node->NumInstances = NumInstance;
114 Node->OpModes = Modes;
115
116 /* Update the Parent member of each element of the InstanceList */
117 if(InstanceList != NULL) {
118 GenPtr = InstanceList;
119
120 for(i = 0; i < NumInstance; ++i) { // Iterate through each element of InstanceList
121 GIp = (GenericInstance *)GenPtr;
122 GIp->Parent = Node; // Initializing the Parent member & InstanceNum
123 //GIp->InstanceNum = i;
124 GenPtr += InstanceSize;
125 }
126 }
127 if(DevName == NULL) {
128 if(daDefaultDevice != NULL) {
129 EFIerrno = RETURN_INVALID_PARAMETER;
130 return NULL;
131 }
132 daDefaultDevice = Node;
133 }
134 else {
135 (void) InsertTailList(&daDeviceList, &Node->DevList);
136 }
137 EFIerrno = RETURN_SUCCESS;
138 return Node;
139 }