From 3d4b8d1750b70c77c2bffe14c5d272151c75c014 Mon Sep 17 00:00:00 2001 From: Dong Heng Date: Mon, 18 Nov 2019 17:56:46 +0800 Subject: [PATCH] feat(newlib): add libc platform header file --- .../newlib/newlib/port/include/sys/random.h | 30 +++++++++++++ .../newlib/newlib/port/include/sys/uio.h | 21 +++++++++ components/newlib/newlib/port/random.c | 43 +++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 components/newlib/newlib/port/include/sys/random.h create mode 100644 components/newlib/newlib/port/include/sys/uio.h create mode 100644 components/newlib/newlib/port/random.c diff --git a/components/newlib/newlib/port/include/sys/random.h b/components/newlib/newlib/port/include/sys/random.h new file mode 100644 index 00000000..6de53845 --- /dev/null +++ b/components/newlib/newlib/port/include/sys/random.h @@ -0,0 +1,30 @@ +// Copyright 2019-2020 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef __SYS_RANDOM__ +#define __SYS_RANDOM__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +ssize_t getrandom(void *buf, size_t buflen, unsigned int flags); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif //__SYS_RANDOM__ diff --git a/components/newlib/newlib/port/include/sys/uio.h b/components/newlib/newlib/port/include/sys/uio.h new file mode 100644 index 00000000..c7d0c0b3 --- /dev/null +++ b/components/newlib/newlib/port/include/sys/uio.h @@ -0,0 +1,21 @@ +// Copyright 2019-2020 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#ifndef _ESP_PLATFORM_SYS_UIO_H_ +#define _ESP_PLATFORM_SYS_UIO_H_ + +int writev(int s, const struct iovec *iov, int iovcnt); + +ssize_t readv(int fd, const struct iovec *iov, int iovcnt); + +#endif // _ESP_PLATFORM_SYS_UIO_H_ diff --git a/components/newlib/newlib/port/random.c b/components/newlib/newlib/port/random.c new file mode 100644 index 00000000..f6709774 --- /dev/null +++ b/components/newlib/newlib/port/random.c @@ -0,0 +1,43 @@ +// Copyright 2019-2020 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include +#include +#include +#include "esp_system.h" +#include "esp_log.h" + +static const char *TAG = "RANDOM"; + +ssize_t getrandom(void *buf, size_t buflen, unsigned int flags) +{ + // Flags are ignored because: + // - esp_random is non-blocking so it works for both blocking and non-blocking calls, + // - don't have opportunity so set som other source of entropy. + + ESP_LOGD(TAG, "getrandom(buf=0x%x, buflen=%d, flags=%u)", (int) buf, buflen, flags); + + if (buf == NULL) { + errno = EFAULT; + ESP_LOGD(TAG, "getrandom returns -1 (EFAULT)"); + return -1; + } + + esp_fill_random(buf, buflen); + + ESP_LOGD(TAG, "getrandom returns %d", buflen); + return buflen; +}