1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "los_random.h"
33 #include "fcntl.h"
34 #include "hisoc/random.h"
35 #include "linux/kernel.h"
36 #include "fs/driver.h"
37
38 static RandomOperations g_randomOp;
RandomOperationsInit(const RandomOperations * r)39 void RandomOperationsInit(const RandomOperations *r)
40 {
41 if (r != NULL) {
42 (void)memcpy_s(&g_randomOp, sizeof(RandomOperations), r, sizeof(RandomOperations));
43 } else {
44 PRINT_ERR("%s %d param is invalid\n", __FUNCTION__, __LINE__);
45 }
46 return;
47 }
RandomHwOpen(struct file * filep)48 static int RandomHwOpen(struct file *filep)
49 {
50 if (g_randomOp.init != NULL) {
51 g_randomOp.init();
52 return ENOERR;
53 }
54 return -1;
55 }
56
RandomHwClose(struct file * filep)57 static int RandomHwClose(struct file *filep)
58 {
59 if (g_randomOp.deinit != NULL) {
60 g_randomOp.deinit();
61 return ENOERR;
62 }
63 return -1;
64 }
65
RandomHwIoctl(struct file * filep,int cmd,unsigned long arg)66 static int RandomHwIoctl(struct file *filep, int cmd, unsigned long arg)
67 {
68 int ret = -1;
69
70 switch (cmd) {
71 default:
72 PRINT_ERR("!!!bad command!!!\n");
73 return -EINVAL;
74 }
75 return ret;
76 }
77
RandomHwRead(struct file * filep,char * buffer,size_t buflen)78 static ssize_t RandomHwRead(struct file *filep, char *buffer, size_t buflen)
79 {
80 int ret = -1;
81
82 if (g_randomOp.read != NULL) {
83 ret = g_randomOp.read(buffer, buflen);
84 if (ret == ENOERR) {
85 ret = buflen;
86 }
87 } else {
88 ret = -1;
89 }
90 return ret;
91 }
92
RandomMap(struct file * filep,LosVmMapRegion * region)93 static ssize_t RandomMap(struct file *filep, LosVmMapRegion *region)
94 {
95 PRINTK("%s %d, mmap is not support\n", __FUNCTION__, __LINE__);
96 return 0;
97 }
98
99 static const struct file_operations_vfs g_randomHwDevOps = {
100 RandomHwOpen, /* open */
101 RandomHwClose, /* close */
102 RandomHwRead, /* read */
103 NULL, /* write */
104 NULL, /* seek */
105 RandomHwIoctl, /* ioctl */
106 RandomMap, /* mmap */
107 #ifndef CONFIG_DISABLE_POLL
108 NULL, /* poll */
109 #endif
110 NULL, /* unlink */
111 };
112
DevUrandomRegister(void)113 int DevUrandomRegister(void)
114 {
115 if (g_randomOp.support != NULL) {
116 int ret = g_randomOp.support();
117 if (ret) {
118 return register_driver("/dev/urandom", &g_randomHwDevOps, 0666, 0); /* 0666: file mode */
119 }
120 }
121 return -EPERM;
122 }
123
124