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 "linux/kernel.h"
35 #include "fs/driver.h"
36
37
38 static unsigned long g_randomMax = 0x7FFFFFFF;
39
DoRand(unsigned long * value)40 static long DoRand(unsigned long *value)
41 {
42 long quotient, remainder, t;
43
44 quotient = *value / 127773L;
45 remainder = *value % 127773L;
46 t = 16807L * remainder - 2836L * quotient;
47 if (t <= 0) {
48 t += 0x7fffffff;
49 }
50 return ((*value = t) % (g_randomMax + 1));
51 }
52
53 static unsigned long g_seed = 1;
54
RanOpen(struct file * filep)55 int RanOpen(struct file *filep)
56 {
57 g_seed = (unsigned long)(LOS_CurrNanosec() & 0xffffffff);
58 return 0;
59 }
60
RanClose(struct file * filep)61 static int RanClose(struct file *filep)
62 {
63 return 0;
64 }
65
RanIoctl(struct file * filep,int cmd,unsigned long arg)66 int RanIoctl(struct file *filep, int cmd, unsigned long arg)
67 {
68 PRINT_ERR("random ioctl is not supported\n");
69 return -ENOTSUP;
70 }
71
RanRead(struct file * filep,char * buffer,size_t buflen)72 ssize_t RanRead(struct file *filep, char *buffer, size_t buflen)
73 {
74 ssize_t len = buflen;
75 char *buf = buffer;
76 unsigned int temp;
77 int ret;
78
79 if (len % sizeof(unsigned int)) {
80 PRINT_ERR("random size not aligned by 4 bytes\n");
81 return -EINVAL;
82 }
83 while (len > 0) {
84 temp = DoRand(&g_seed);
85 ret = LOS_CopyFromKernel((void *)buf, sizeof(unsigned int), (void *)&temp, sizeof(unsigned int));
86 if (ret) {
87 break;
88 }
89 len -= sizeof(unsigned int);
90 buf += sizeof(unsigned int);
91 }
92 return (buflen - len); /* return a successful len */
93 }
94
RanMap(struct file * filep,LosVmMapRegion * region)95 static ssize_t RanMap(struct file *filep, LosVmMapRegion *region)
96 {
97 PRINTK("%s %d, mmap is not support\n", __FUNCTION__, __LINE__);
98 return 0;
99 }
100
101 static const struct file_operations_vfs g_ranDevOps = {
102 RanOpen, /* open */
103 RanClose, /* close */
104 RanRead, /* read */
105 NULL, /* write */
106 NULL, /* seek */
107 RanIoctl, /* ioctl */
108 RanMap, /* mmap */
109 #ifndef CONFIG_DISABLE_POLL
110 NULL, /* poll */
111 #endif
112 NULL, /* unlink */
113 };
114
DevRandomRegister(void)115 int DevRandomRegister(void)
116 {
117 return register_driver("/dev/random", &g_ranDevOps, 0666, 0); /* 0666: file mode */
118 }
119
120