Skip to content

Commit 21b9b91

Browse files
MathiasDuckeckxiaoxiang781216
authored andcommitted
arch/arm/samv7: allow all values for count in read
The parameter count of read had to be a multiple of 4, otherwise the result was truncated. Now all values are possible. Signed-off-by: Mathias Duckeck <mathias.duckeck@avat.de>
1 parent 4863564 commit 21b9b91

File tree

1 file changed

+20
-27
lines changed

1 file changed

+20
-27
lines changed

arch/arm/src/samv7/sam_trng.c

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ struct trng_dev_s
6666
{
6767
mutex_t lock; /* Enforces exclusive access to the TRNG */
6868
sem_t waitsem; /* Wait for buffer full */
69-
uint32_t *samples; /* Current buffer being filled */
70-
size_t maxsamples; /* Size of the current buffer (in 32-bit words) */
69+
uint32_t odata; /* last random value */
70+
uint8_t *samples; /* Current buffer being filled */
71+
size_t maxsamples; /* Size of the current buffer (in bytes) */
7172
volatile size_t nsamples; /* Number of samples currently buffered */
7273
volatile bool first; /* The first random number must be handled differently */
7374
};
@@ -145,22 +146,22 @@ static int sam_interrupt(int irq, void *context, void *arg)
145146
* number generator test).
146147
*/
147148

148-
if (g_trngdev.nsamples == 0)
149+
if (g_trngdev.first)
149150
{
150151
/* This is the first sample we have taken. Save it for subsequent
151152
* comparison.
152153
*/
153154

154-
g_trngdev.samples[0] = odata;
155-
g_trngdev.nsamples = 1;
155+
g_trngdev.odata = odata;
156+
g_trngdev.first = false;
156157
continue;
157158
}
158159

159160
/* This is not the first sample. Check if the new sample differs from
160161
* the preceding sample.
161162
*/
162163

163-
else if (odata == g_trngdev.samples[g_trngdev.nsamples - 1])
164+
if (odata == g_trngdev.odata)
164165
{
165166
/* Two samples with the same value. Discard this one and try
166167
* again.
@@ -169,33 +170,25 @@ static int sam_interrupt(int irq, void *context, void *arg)
169170
continue;
170171
}
171172

172-
/* This sample differs from the previous value. Have we discarded the
173-
* first sample yet?
174-
*/
173+
g_trngdev.odata = odata;
175174

176-
if (g_trngdev.first)
175+
/* Add the new random number to the buffer */
176+
177+
if (g_trngdev.nsamples + 4 <= g_trngdev.maxsamples)
177178
{
178-
/* No, discard it now by replacing it with the new sample */
179+
/* copy all 4 bytes */
179180

180-
g_trngdev.samples[0] = odata;
181-
g_trngdev.nsamples = 1;
182-
g_trngdev.first = false;
181+
*((uint32_t *)&g_trngdev.samples[g_trngdev.nsamples]) = odata;
182+
g_trngdev.nsamples += 4;
183183
}
184-
185-
/* Yes.. the first sample has been discarded */
186-
187184
else
188185
{
189-
/* Add the new random number to the buffer */
186+
/* copy the remaining bytes */
190187

191-
g_trngdev.samples[g_trngdev.nsamples] = odata;
192-
g_trngdev.nsamples++;
193-
}
188+
memcpy(&g_trngdev.samples[g_trngdev.nsamples], &odata,
189+
(g_trngdev.maxsamples - g_trngdev.nsamples));
190+
g_trngdev.nsamples = g_trngdev.maxsamples;
194191

195-
/* Have all of the requested samples been saved? */
196-
197-
if (g_trngdev.nsamples == g_trngdev.maxsamples)
198-
{
199192
/* Yes.. disable any further interrupts */
200193

201194
putreg32(TRNG_INT_DATRDY, SAM_TRNG_IDR);
@@ -249,7 +242,7 @@ static ssize_t sam_read(struct file *filep, char *buffer, size_t buflen)
249242
DEBUGASSERT(((uintptr_t)buffer & 3) == 0);
250243

251244
g_trngdev.samples = (uint32_t *)buffer;
252-
g_trngdev.maxsamples = buflen >> 2;
245+
g_trngdev.maxsamples = buflen;
253246
g_trngdev.nsamples = 0;
254247
g_trngdev.first = true;
255248

@@ -294,7 +287,7 @@ static ssize_t sam_read(struct file *filep, char *buffer, size_t buflen)
294287

295288
/* Success... calculate the number of bytes to return */
296289

297-
retval = g_trngdev.nsamples << 2;
290+
retval = g_trngdev.nsamples;
298291

299292
errout:
300293

0 commit comments

Comments
 (0)