|
9 | 9 | #![no_std]
|
10 | 10 | #![no_main]
|
11 | 11 |
|
12 |
| -use core::{mem::size_of, usize}; |
| 12 | +use core::{cmp, usize}; |
13 | 13 | use drv_lpc55_syscon_api::Syscon;
|
14 | 14 | use drv_rng_api::RngError;
|
15 | 15 | use idol_runtime::{ClientError, NotificationHandler, RequestError};
|
@@ -89,24 +89,19 @@ impl idl::InOrderRngImpl for Lpc55RngServer {
|
89 | 89 | dest: idol_runtime::Leased<idol_runtime::W, [u8]>,
|
90 | 90 | ) -> Result<usize, RequestError<RngError>> {
|
91 | 91 | let mut cnt = 0;
|
92 |
| - const STEP: usize = size_of::<u32>(); |
93 |
| - let mut buf = [0u8; STEP]; |
94 |
| - // fill in multiples of STEP / RNG register size |
95 |
| - for _ in 0..(dest.len() / STEP) { |
96 |
| - self.0.try_fill_bytes(&mut buf).map_err(RngError::from)?; |
97 |
| - dest.write_range(cnt..cnt + STEP, &buf) |
| 92 | + let mut buf = [0u8; 32]; |
| 93 | + while cnt < dest.len() { |
| 94 | + let len = cmp::min(buf.len(), dest.len() - cnt); |
| 95 | + |
| 96 | + self.0 |
| 97 | + .try_fill_bytes(&mut buf[..len]) |
| 98 | + .map_err(RngError::from)?; |
| 99 | + dest.write_range(cnt..cnt + len, &buf[..len]) |
98 | 100 | .map_err(|_| RequestError::Fail(ClientError::WentAway))?;
|
99 |
| - cnt += STEP; |
100 |
| - } |
101 |
| - // fill in remaining |
102 |
| - let remain = dest.len() - cnt; |
103 |
| - assert!(remain < STEP); |
104 |
| - if remain > 0 { |
105 |
| - self.0.try_fill_bytes(&mut buf).map_err(RngError::from)?; |
106 |
| - dest.write_range(dest.len() - remain..dest.len(), &buf) |
107 |
| - .map_err(|_| RequestError::Fail(ClientError::WentAway))?; |
108 |
| - cnt += remain; |
| 101 | + |
| 102 | + cnt += len; |
109 | 103 | }
|
| 104 | + |
110 | 105 | Ok(cnt)
|
111 | 106 | }
|
112 | 107 | }
|
|
0 commit comments