mirror of
https://github.com/Ai-Thinker-Open/Ai-Thinker-Open_RTL8710BX_ALIOS_SDK.git
synced 2026-07-14 22:15:38 +00:00
rel_1.6.0 init
This commit is contained in:
commit
27b3e2883d
19359 changed files with 8093121 additions and 0 deletions
5
Living_SDK/security/irot/km/km.mk
Normal file
5
Living_SDK/security/irot/km/km.mk
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
NAME := libkm
|
||||
|
||||
$(NAME)_COMPONENTS := irot.km.platform alicrypto
|
||||
|
||||
$(NAME)_PREBUILT_LIBRARY := lib/$(HOST_ARCH)/libkm.a
|
||||
BIN
Living_SDK/security/irot/km/lib/ARM968E-S/libkm.a
Normal file
BIN
Living_SDK/security/irot/km/lib/ARM968E-S/libkm.a
Normal file
Binary file not shown.
BIN
Living_SDK/security/irot/km/lib/Cortex-M4/libkm.a
Normal file
BIN
Living_SDK/security/irot/km/lib/Cortex-M4/libkm.a
Normal file
Binary file not shown.
BIN
Living_SDK/security/irot/km/lib/linux/libkm.a
Normal file
BIN
Living_SDK/security/irot/km/lib/linux/libkm.a
Normal file
Binary file not shown.
181
Living_SDK/security/irot/km/platform/amebaz/plat_gen.c
Executable file
181
Living_SDK/security/irot/km/platform/amebaz/plat_gen.c
Executable file
|
|
@ -0,0 +1,181 @@
|
|||
/**
|
||||
* Copyright (C) 2017 The YunOS Project. All rights reserved.
|
||||
*/
|
||||
|
||||
#include "hal/wifi.h"
|
||||
#include "hal/soc/flash.h"
|
||||
#include "plat_gen.h"
|
||||
#include "plat_comm.h"
|
||||
|
||||
#define DEV_ID_LEN 6
|
||||
#define MIN_SECTOR_SIZE 4096
|
||||
#define MIN_SECTOR_SHIFT 12
|
||||
|
||||
#define ROUNDUP(a, b) (((a) + ((b)-1)) & ~((b)-1))
|
||||
#define ROUNDDOWN(a, b) ((a) & ~((b)-1))
|
||||
|
||||
int get_dev_id(uint8_t *dev_id, uint32_t *id_len)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (*id_len < DEV_ID_LEN) {
|
||||
PL_ERR("short buffer id len is %d\n", (unsigned int)*id_len);
|
||||
*id_len = DEV_ID_LEN;
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = hal_wifi_get_mac_addr(NULL, dev_id);
|
||||
if (ret) {
|
||||
PL_ERR("get mac addr failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
*id_len = DEV_ID_LEN;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//for amebaz_dev not support return 0 directly
|
||||
int open_rsvd_part(int flag)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int write_rsvd_part(int fd, uint32_t offset, void *data, uint32_t data_len)
|
||||
{
|
||||
int ret = 0;
|
||||
uint32_t erase_size = 0;
|
||||
uint32_t pre_len = 0;
|
||||
uint8_t *pre_buf = NULL;
|
||||
uint32_t suf_len = 0;
|
||||
uint8_t *suf_buf = NULL;
|
||||
uint32_t first_off = 0;
|
||||
uint32_t last_off = 0;
|
||||
uint32_t fin_off = offset + data_len;
|
||||
uint32_t tmp_off = 0;
|
||||
|
||||
if (data_len != 0 && data == NULL) {
|
||||
PL_ERR("bad param \n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (data_len == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
first_off = ROUNDDOWN(offset, MIN_SECTOR_SIZE);
|
||||
pre_len = offset - first_off;
|
||||
if (pre_len) {
|
||||
pre_buf = (uint8_t *)pl_malloc(pre_len);
|
||||
if (pre_buf == NULL) {
|
||||
PL_ERR("malloc pre buf failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
//store the data of first block that will be erased
|
||||
tmp_off = first_off;
|
||||
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
|
||||
pre_buf, pre_len);
|
||||
if (ret || (tmp_off - first_off != pre_len)) {
|
||||
PL_ERR("hal flash pre read failed\n");
|
||||
ret = -1;
|
||||
goto clean;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
last_off = ROUNDUP(fin_off, MIN_SECTOR_SIZE);
|
||||
suf_len = last_off - fin_off;
|
||||
if (suf_len) {
|
||||
suf_buf = (uint8_t *)pl_malloc(suf_len);
|
||||
if (suf_buf == NULL) {
|
||||
PL_ERR("malloc suf buf failed\n");
|
||||
ret = -1;
|
||||
goto clean;
|
||||
}
|
||||
|
||||
//read the data of last block that will be erased
|
||||
tmp_off = fin_off;
|
||||
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
|
||||
suf_buf, suf_len);
|
||||
if (ret || (tmp_off - fin_off != suf_len)) {
|
||||
PL_ERR("hal flash pre read failed\n");
|
||||
goto clean;
|
||||
}
|
||||
}
|
||||
|
||||
erase_size = last_off - first_off;
|
||||
ret = hal_flash_erase(HAL_PARTITION_PARAMETER_4, first_off, erase_size);
|
||||
if (ret) {
|
||||
PL_ERR("flash erase failed ret %d\n", ret);
|
||||
goto clean;
|
||||
}
|
||||
PL_INF("pre_len is %d, suf len is %d, erase len is %d, first_off is %d\n",
|
||||
(unsigned int)pre_len, (unsigned int)suf_len, (unsigned int)erase_size, (unsigned int)first_off);
|
||||
|
||||
/* write first sector data that be erased*/
|
||||
tmp_off = first_off;
|
||||
if (pre_len) {
|
||||
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, pre_buf, pre_len);
|
||||
if (ret || (tmp_off - first_off) != pre_len) {
|
||||
PL_ERR("write pre buf failed %d, len %d\n", ret, (unsigned int)tmp_off);
|
||||
goto clean;
|
||||
}
|
||||
}
|
||||
|
||||
/* write the data need to write */
|
||||
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, data, data_len);
|
||||
if (ret) {
|
||||
PL_ERR("write pre buf failed %d\n", ret);
|
||||
goto clean;
|
||||
}
|
||||
|
||||
/* write last sector data that be erased */
|
||||
if (suf_len) {
|
||||
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, suf_buf, suf_len);
|
||||
if (ret) {
|
||||
PL_ERR("write pre buf failed %d\n", ret);
|
||||
goto clean;
|
||||
}
|
||||
}
|
||||
|
||||
clean:
|
||||
if (suf_buf) {
|
||||
pl_free(suf_buf);
|
||||
suf_buf = NULL;
|
||||
}
|
||||
|
||||
if (pre_buf) {
|
||||
pl_free(pre_buf);
|
||||
pre_buf = NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int read_rsvd_part(int fd, uint32_t offset, void *buffer, uint32_t read_len)
|
||||
{
|
||||
uint32_t off_set = offset;
|
||||
uint32_t len = 0;
|
||||
int ret = 0;
|
||||
|
||||
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &off_set, buffer, read_len);
|
||||
if (ret) {
|
||||
PL_ERR("hal flash read failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
len = off_set - offset;
|
||||
if (len != read_len) {
|
||||
PL_ERR("read failed read_len is %d", (unsigned int)len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int close_rsvd_part(int fd)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
181
Living_SDK/security/irot/km/platform/hf-lpb130/plat_gen.c
Normal file
181
Living_SDK/security/irot/km/platform/hf-lpb130/plat_gen.c
Normal file
|
|
@ -0,0 +1,181 @@
|
|||
/**
|
||||
* Copyright (C) 2017 The YunOS Project. All rights reserved.
|
||||
*/
|
||||
|
||||
#include "hal/wifi.h"
|
||||
#include "hal/soc/flash.h"
|
||||
#include "plat_gen.h"
|
||||
#include "plat_comm.h"
|
||||
|
||||
#define DEV_ID_LEN 6
|
||||
#define MIN_SECTOR_SIZE 4096
|
||||
#define MIN_SECTOR_SHIFT 12
|
||||
|
||||
#define ROUNDUP(a, b) (((a) + ((b)-1)) & ~((b)-1))
|
||||
#define ROUNDDOWN(a, b) ((a) & ~((b)-1))
|
||||
|
||||
int get_dev_id(uint8_t *dev_id, uint32_t *id_len)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (*id_len < DEV_ID_LEN) {
|
||||
PL_ERR("short buffer id len is %d\n", (unsigned int)*id_len);
|
||||
*id_len = DEV_ID_LEN;
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = hal_wifi_get_mac_addr(NULL, dev_id);
|
||||
if (ret) {
|
||||
PL_ERR("get mac addr failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
*id_len = DEV_ID_LEN;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//for rda5981x not support return 0 directly
|
||||
int open_rsvd_part(int flag)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int write_rsvd_part(int fd, uint32_t offset, void *data, uint32_t data_len)
|
||||
{
|
||||
int ret = 0;
|
||||
uint32_t erase_size = 0;
|
||||
uint32_t pre_len = 0;
|
||||
uint8_t *pre_buf = NULL;
|
||||
uint32_t suf_len = 0;
|
||||
uint8_t *suf_buf = NULL;
|
||||
uint32_t first_off = 0;
|
||||
uint32_t last_off = 0;
|
||||
uint32_t fin_off = offset + data_len;
|
||||
uint32_t tmp_off = 0;
|
||||
|
||||
if (data_len != 0 && data == NULL) {
|
||||
PL_ERR("bad param \n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (data_len == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
first_off = ROUNDDOWN(offset, MIN_SECTOR_SIZE);
|
||||
pre_len = offset - first_off;
|
||||
if (pre_len) {
|
||||
pre_buf = (uint8_t *)pl_malloc(pre_len);
|
||||
if (pre_buf == NULL) {
|
||||
PL_ERR("malloc pre buf failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
//store the data of first block that will be erased
|
||||
tmp_off = first_off;
|
||||
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
|
||||
pre_buf, pre_len);
|
||||
if (ret || (tmp_off - first_off != pre_len)) {
|
||||
PL_ERR("hal flash pre read failed\n");
|
||||
ret = -1;
|
||||
goto clean;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
last_off = ROUNDUP(fin_off, MIN_SECTOR_SIZE);
|
||||
suf_len = last_off - fin_off;
|
||||
if (suf_len) {
|
||||
suf_buf = (uint8_t *)pl_malloc(suf_len);
|
||||
if (suf_buf == NULL) {
|
||||
PL_ERR("malloc suf buf failed\n");
|
||||
ret = -1;
|
||||
goto clean;
|
||||
}
|
||||
|
||||
//read the data of last block that will be erased
|
||||
tmp_off = fin_off;
|
||||
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
|
||||
suf_buf, suf_len);
|
||||
if (ret || (tmp_off - fin_off != suf_len)) {
|
||||
PL_ERR("hal flash pre read failed\n");
|
||||
goto clean;
|
||||
}
|
||||
}
|
||||
|
||||
erase_size = last_off - first_off;
|
||||
ret = hal_flash_erase(HAL_PARTITION_PARAMETER_4, first_off, erase_size);
|
||||
if (ret) {
|
||||
PL_ERR("flash erase failed ret %d\n", ret);
|
||||
goto clean;
|
||||
}
|
||||
PL_INF("pre_len is %d, suf len is %d, erase len is %d, first_off is %d\n",
|
||||
(unsigned int)pre_len, (unsigned int)suf_len, (unsigned int)erase_size, (unsigned int)first_off);
|
||||
|
||||
/* write first sector data that be erased*/
|
||||
tmp_off = first_off;
|
||||
if (pre_len) {
|
||||
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, pre_buf, pre_len);
|
||||
if (ret || (tmp_off - first_off) != pre_len) {
|
||||
PL_ERR("write pre buf failed %d, len %d\n", ret, (unsigned int)tmp_off);
|
||||
goto clean;
|
||||
}
|
||||
}
|
||||
|
||||
/* write the data need to write */
|
||||
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, data, data_len);
|
||||
if (ret) {
|
||||
PL_ERR("write pre buf failed %d\n", ret);
|
||||
goto clean;
|
||||
}
|
||||
|
||||
/* write last sector data that be erased */
|
||||
if (suf_len) {
|
||||
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, suf_buf, suf_len);
|
||||
if (ret) {
|
||||
PL_ERR("write pre buf failed %d\n", ret);
|
||||
goto clean;
|
||||
}
|
||||
}
|
||||
|
||||
clean:
|
||||
if (suf_buf) {
|
||||
pl_free(suf_buf);
|
||||
suf_buf = NULL;
|
||||
}
|
||||
|
||||
if (pre_buf) {
|
||||
pl_free(pre_buf);
|
||||
pre_buf = NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int read_rsvd_part(int fd, uint32_t offset, void *buffer, uint32_t read_len)
|
||||
{
|
||||
uint32_t off_set = offset;
|
||||
uint32_t len = 0;
|
||||
int ret = 0;
|
||||
|
||||
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &off_set, buffer, read_len);
|
||||
if (ret) {
|
||||
PL_ERR("hal flash read failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
len = off_set - offset;
|
||||
if (len != read_len) {
|
||||
PL_ERR("read failed read_len is %d", (unsigned int)len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int close_rsvd_part(int fd)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
181
Living_SDK/security/irot/km/platform/hf-lpb135/plat_gen.c
Normal file
181
Living_SDK/security/irot/km/platform/hf-lpb135/plat_gen.c
Normal file
|
|
@ -0,0 +1,181 @@
|
|||
/**
|
||||
* Copyright (C) 2017 The YunOS Project. All rights reserved.
|
||||
*/
|
||||
|
||||
#include "hal/wifi.h"
|
||||
#include "hal/soc/flash.h"
|
||||
#include "plat_gen.h"
|
||||
#include "plat_comm.h"
|
||||
|
||||
#define DEV_ID_LEN 6
|
||||
#define MIN_SECTOR_SIZE 4096
|
||||
#define MIN_SECTOR_SHIFT 12
|
||||
|
||||
#define ROUNDUP(a, b) (((a) + ((b)-1)) & ~((b)-1))
|
||||
#define ROUNDDOWN(a, b) ((a) & ~((b)-1))
|
||||
|
||||
int get_dev_id(uint8_t *dev_id, uint32_t *id_len)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (*id_len < DEV_ID_LEN) {
|
||||
PL_ERR("short buffer id len is %d\n", (unsigned int)*id_len);
|
||||
*id_len = DEV_ID_LEN;
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = hal_wifi_get_mac_addr(NULL, dev_id);
|
||||
if (ret) {
|
||||
PL_ERR("get mac addr failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
*id_len = DEV_ID_LEN;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//for rda5981x not support return 0 directly
|
||||
int open_rsvd_part(int flag)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int write_rsvd_part(int fd, uint32_t offset, void *data, uint32_t data_len)
|
||||
{
|
||||
int ret = 0;
|
||||
uint32_t erase_size = 0;
|
||||
uint32_t pre_len = 0;
|
||||
uint8_t *pre_buf = NULL;
|
||||
uint32_t suf_len = 0;
|
||||
uint8_t *suf_buf = NULL;
|
||||
uint32_t first_off = 0;
|
||||
uint32_t last_off = 0;
|
||||
uint32_t fin_off = offset + data_len;
|
||||
uint32_t tmp_off = 0;
|
||||
|
||||
if (data_len != 0 && data == NULL) {
|
||||
PL_ERR("bad param \n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (data_len == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
first_off = ROUNDDOWN(offset, MIN_SECTOR_SIZE);
|
||||
pre_len = offset - first_off;
|
||||
if (pre_len) {
|
||||
pre_buf = (uint8_t *)pl_malloc(pre_len);
|
||||
if (pre_buf == NULL) {
|
||||
PL_ERR("malloc pre buf failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
//store the data of first block that will be erased
|
||||
tmp_off = first_off;
|
||||
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
|
||||
pre_buf, pre_len);
|
||||
if (ret || (tmp_off - first_off != pre_len)) {
|
||||
PL_ERR("hal flash pre read failed\n");
|
||||
ret = -1;
|
||||
goto clean;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
last_off = ROUNDUP(fin_off, MIN_SECTOR_SIZE);
|
||||
suf_len = last_off - fin_off;
|
||||
if (suf_len) {
|
||||
suf_buf = (uint8_t *)pl_malloc(suf_len);
|
||||
if (suf_buf == NULL) {
|
||||
PL_ERR("malloc suf buf failed\n");
|
||||
ret = -1;
|
||||
goto clean;
|
||||
}
|
||||
|
||||
//read the data of last block that will be erased
|
||||
tmp_off = fin_off;
|
||||
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
|
||||
suf_buf, suf_len);
|
||||
if (ret || (tmp_off - fin_off != suf_len)) {
|
||||
PL_ERR("hal flash pre read failed\n");
|
||||
goto clean;
|
||||
}
|
||||
}
|
||||
|
||||
erase_size = last_off - first_off;
|
||||
ret = hal_flash_erase(HAL_PARTITION_PARAMETER_4, first_off, erase_size);
|
||||
if (ret) {
|
||||
PL_ERR("flash erase failed ret %d\n", ret);
|
||||
goto clean;
|
||||
}
|
||||
PL_INF("pre_len is %d, suf len is %d, erase len is %d, first_off is %d\n",
|
||||
(unsigned int)pre_len, (unsigned int)suf_len, (unsigned int)erase_size, (unsigned int)first_off);
|
||||
|
||||
/* write first sector data that be erased*/
|
||||
tmp_off = first_off;
|
||||
if (pre_len) {
|
||||
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, pre_buf, pre_len);
|
||||
if (ret || (tmp_off - first_off) != pre_len) {
|
||||
PL_ERR("write pre buf failed %d, len %d\n", ret, (unsigned int)tmp_off);
|
||||
goto clean;
|
||||
}
|
||||
}
|
||||
|
||||
/* write the data need to write */
|
||||
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, data, data_len);
|
||||
if (ret) {
|
||||
PL_ERR("write pre buf failed %d\n", ret);
|
||||
goto clean;
|
||||
}
|
||||
|
||||
/* write last sector data that be erased */
|
||||
if (suf_len) {
|
||||
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, suf_buf, suf_len);
|
||||
if (ret) {
|
||||
PL_ERR("write pre buf failed %d\n", ret);
|
||||
goto clean;
|
||||
}
|
||||
}
|
||||
|
||||
clean:
|
||||
if (suf_buf) {
|
||||
pl_free(suf_buf);
|
||||
suf_buf = NULL;
|
||||
}
|
||||
|
||||
if (pre_buf) {
|
||||
pl_free(pre_buf);
|
||||
pre_buf = NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int read_rsvd_part(int fd, uint32_t offset, void *buffer, uint32_t read_len)
|
||||
{
|
||||
uint32_t off_set = offset;
|
||||
uint32_t len = 0;
|
||||
int ret = 0;
|
||||
|
||||
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &off_set, buffer, read_len);
|
||||
if (ret) {
|
||||
PL_ERR("hal flash read failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
len = off_set - offset;
|
||||
if (len != read_len) {
|
||||
PL_ERR("read failed read_len is %d", (unsigned int)len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int close_rsvd_part(int fd)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
181
Living_SDK/security/irot/km/platform/hf-lpt130/plat_gen.c
Normal file
181
Living_SDK/security/irot/km/platform/hf-lpt130/plat_gen.c
Normal file
|
|
@ -0,0 +1,181 @@
|
|||
/**
|
||||
* Copyright (C) 2017 The YunOS Project. All rights reserved.
|
||||
*/
|
||||
|
||||
#include "hal/wifi.h"
|
||||
#include "hal/soc/flash.h"
|
||||
#include "plat_gen.h"
|
||||
#include "plat_comm.h"
|
||||
|
||||
#define DEV_ID_LEN 6
|
||||
#define MIN_SECTOR_SIZE 4096
|
||||
#define MIN_SECTOR_SHIFT 12
|
||||
|
||||
#define ROUNDUP(a, b) (((a) + ((b)-1)) & ~((b)-1))
|
||||
#define ROUNDDOWN(a, b) ((a) & ~((b)-1))
|
||||
|
||||
int get_dev_id(uint8_t *dev_id, uint32_t *id_len)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (*id_len < DEV_ID_LEN) {
|
||||
PL_ERR("short buffer id len is %d\n", (unsigned int)*id_len);
|
||||
*id_len = DEV_ID_LEN;
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = hal_wifi_get_mac_addr(NULL, dev_id);
|
||||
if (ret) {
|
||||
PL_ERR("get mac addr failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
*id_len = DEV_ID_LEN;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//for rda5981x not support return 0 directly
|
||||
int open_rsvd_part(int flag)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int write_rsvd_part(int fd, uint32_t offset, void *data, uint32_t data_len)
|
||||
{
|
||||
int ret = 0;
|
||||
uint32_t erase_size = 0;
|
||||
uint32_t pre_len = 0;
|
||||
uint8_t *pre_buf = NULL;
|
||||
uint32_t suf_len = 0;
|
||||
uint8_t *suf_buf = NULL;
|
||||
uint32_t first_off = 0;
|
||||
uint32_t last_off = 0;
|
||||
uint32_t fin_off = offset + data_len;
|
||||
uint32_t tmp_off = 0;
|
||||
|
||||
if (data_len != 0 && data == NULL) {
|
||||
PL_ERR("bad param \n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (data_len == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
first_off = ROUNDDOWN(offset, MIN_SECTOR_SIZE);
|
||||
pre_len = offset - first_off;
|
||||
if (pre_len) {
|
||||
pre_buf = (uint8_t *)pl_malloc(pre_len);
|
||||
if (pre_buf == NULL) {
|
||||
PL_ERR("malloc pre buf failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
//store the data of first block that will be erased
|
||||
tmp_off = first_off;
|
||||
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
|
||||
pre_buf, pre_len);
|
||||
if (ret || (tmp_off - first_off != pre_len)) {
|
||||
PL_ERR("hal flash pre read failed\n");
|
||||
ret = -1;
|
||||
goto clean;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
last_off = ROUNDUP(fin_off, MIN_SECTOR_SIZE);
|
||||
suf_len = last_off - fin_off;
|
||||
if (suf_len) {
|
||||
suf_buf = (uint8_t *)pl_malloc(suf_len);
|
||||
if (suf_buf == NULL) {
|
||||
PL_ERR("malloc suf buf failed\n");
|
||||
ret = -1;
|
||||
goto clean;
|
||||
}
|
||||
|
||||
//read the data of last block that will be erased
|
||||
tmp_off = fin_off;
|
||||
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
|
||||
suf_buf, suf_len);
|
||||
if (ret || (tmp_off - fin_off != suf_len)) {
|
||||
PL_ERR("hal flash pre read failed\n");
|
||||
goto clean;
|
||||
}
|
||||
}
|
||||
|
||||
erase_size = last_off - first_off;
|
||||
ret = hal_flash_erase(HAL_PARTITION_PARAMETER_4, first_off, erase_size);
|
||||
if (ret) {
|
||||
PL_ERR("flash erase failed ret %d\n", ret);
|
||||
goto clean;
|
||||
}
|
||||
PL_INF("pre_len is %d, suf len is %d, erase len is %d, first_off is %d\n",
|
||||
(unsigned int)pre_len, (unsigned int)suf_len, (unsigned int)erase_size, (unsigned int)first_off);
|
||||
|
||||
/* write first sector data that be erased*/
|
||||
tmp_off = first_off;
|
||||
if (pre_len) {
|
||||
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, pre_buf, pre_len);
|
||||
if (ret || (tmp_off - first_off) != pre_len) {
|
||||
PL_ERR("write pre buf failed %d, len %d\n", ret, (unsigned int)tmp_off);
|
||||
goto clean;
|
||||
}
|
||||
}
|
||||
|
||||
/* write the data need to write */
|
||||
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, data, data_len);
|
||||
if (ret) {
|
||||
PL_ERR("write pre buf failed %d\n", ret);
|
||||
goto clean;
|
||||
}
|
||||
|
||||
/* write last sector data that be erased */
|
||||
if (suf_len) {
|
||||
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, suf_buf, suf_len);
|
||||
if (ret) {
|
||||
PL_ERR("write pre buf failed %d\n", ret);
|
||||
goto clean;
|
||||
}
|
||||
}
|
||||
|
||||
clean:
|
||||
if (suf_buf) {
|
||||
pl_free(suf_buf);
|
||||
suf_buf = NULL;
|
||||
}
|
||||
|
||||
if (pre_buf) {
|
||||
pl_free(pre_buf);
|
||||
pre_buf = NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int read_rsvd_part(int fd, uint32_t offset, void *buffer, uint32_t read_len)
|
||||
{
|
||||
uint32_t off_set = offset;
|
||||
uint32_t len = 0;
|
||||
int ret = 0;
|
||||
|
||||
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &off_set, buffer, read_len);
|
||||
if (ret) {
|
||||
PL_ERR("hal flash read failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
len = off_set - offset;
|
||||
if (len != read_len) {
|
||||
PL_ERR("read failed read_len is %d", (unsigned int)len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int close_rsvd_part(int fd)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
181
Living_SDK/security/irot/km/platform/hf-lpt230/plat_gen.c
Normal file
181
Living_SDK/security/irot/km/platform/hf-lpt230/plat_gen.c
Normal file
|
|
@ -0,0 +1,181 @@
|
|||
/**
|
||||
* Copyright (C) 2017 The YunOS Project. All rights reserved.
|
||||
*/
|
||||
|
||||
#include "hal/wifi.h"
|
||||
#include "hal/soc/flash.h"
|
||||
#include "plat_gen.h"
|
||||
#include "plat_comm.h"
|
||||
|
||||
#define DEV_ID_LEN 6
|
||||
#define MIN_SECTOR_SIZE 4096
|
||||
#define MIN_SECTOR_SHIFT 12
|
||||
|
||||
#define ROUNDUP(a, b) (((a) + ((b)-1)) & ~((b)-1))
|
||||
#define ROUNDDOWN(a, b) ((a) & ~((b)-1))
|
||||
|
||||
int get_dev_id(uint8_t *dev_id, uint32_t *id_len)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (*id_len < DEV_ID_LEN) {
|
||||
PL_ERR("short buffer id len is %d\n", (unsigned int)*id_len);
|
||||
*id_len = DEV_ID_LEN;
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = hal_wifi_get_mac_addr(NULL, dev_id);
|
||||
if (ret) {
|
||||
PL_ERR("get mac addr failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
*id_len = DEV_ID_LEN;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//for rda5981x not support return 0 directly
|
||||
int open_rsvd_part(int flag)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int write_rsvd_part(int fd, uint32_t offset, void *data, uint32_t data_len)
|
||||
{
|
||||
int ret = 0;
|
||||
uint32_t erase_size = 0;
|
||||
uint32_t pre_len = 0;
|
||||
uint8_t *pre_buf = NULL;
|
||||
uint32_t suf_len = 0;
|
||||
uint8_t *suf_buf = NULL;
|
||||
uint32_t first_off = 0;
|
||||
uint32_t last_off = 0;
|
||||
uint32_t fin_off = offset + data_len;
|
||||
uint32_t tmp_off = 0;
|
||||
|
||||
if (data_len != 0 && data == NULL) {
|
||||
PL_ERR("bad param \n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (data_len == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
first_off = ROUNDDOWN(offset, MIN_SECTOR_SIZE);
|
||||
pre_len = offset - first_off;
|
||||
if (pre_len) {
|
||||
pre_buf = (uint8_t *)pl_malloc(pre_len);
|
||||
if (pre_buf == NULL) {
|
||||
PL_ERR("malloc pre buf failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
//store the data of first block that will be erased
|
||||
tmp_off = first_off;
|
||||
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
|
||||
pre_buf, pre_len);
|
||||
if (ret || (tmp_off - first_off != pre_len)) {
|
||||
PL_ERR("hal flash pre read failed\n");
|
||||
ret = -1;
|
||||
goto clean;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
last_off = ROUNDUP(fin_off, MIN_SECTOR_SIZE);
|
||||
suf_len = last_off - fin_off;
|
||||
if (suf_len) {
|
||||
suf_buf = (uint8_t *)pl_malloc(suf_len);
|
||||
if (suf_buf == NULL) {
|
||||
PL_ERR("malloc suf buf failed\n");
|
||||
ret = -1;
|
||||
goto clean;
|
||||
}
|
||||
|
||||
//read the data of last block that will be erased
|
||||
tmp_off = fin_off;
|
||||
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
|
||||
suf_buf, suf_len);
|
||||
if (ret || (tmp_off - fin_off != suf_len)) {
|
||||
PL_ERR("hal flash pre read failed\n");
|
||||
goto clean;
|
||||
}
|
||||
}
|
||||
|
||||
erase_size = last_off - first_off;
|
||||
ret = hal_flash_erase(HAL_PARTITION_PARAMETER_4, first_off, erase_size);
|
||||
if (ret) {
|
||||
PL_ERR("flash erase failed ret %d\n", ret);
|
||||
goto clean;
|
||||
}
|
||||
PL_INF("pre_len is %d, suf len is %d, erase len is %d, first_off is %d\n",
|
||||
(unsigned int)pre_len, (unsigned int)suf_len, (unsigned int)erase_size, (unsigned int)first_off);
|
||||
|
||||
/* write first sector data that be erased*/
|
||||
tmp_off = first_off;
|
||||
if (pre_len) {
|
||||
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, pre_buf, pre_len);
|
||||
if (ret || (tmp_off - first_off) != pre_len) {
|
||||
PL_ERR("write pre buf failed %d, len %d\n", ret, (unsigned int)tmp_off);
|
||||
goto clean;
|
||||
}
|
||||
}
|
||||
|
||||
/* write the data need to write */
|
||||
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, data, data_len);
|
||||
if (ret) {
|
||||
PL_ERR("write pre buf failed %d\n", ret);
|
||||
goto clean;
|
||||
}
|
||||
|
||||
/* write last sector data that be erased */
|
||||
if (suf_len) {
|
||||
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, suf_buf, suf_len);
|
||||
if (ret) {
|
||||
PL_ERR("write pre buf failed %d\n", ret);
|
||||
goto clean;
|
||||
}
|
||||
}
|
||||
|
||||
clean:
|
||||
if (suf_buf) {
|
||||
pl_free(suf_buf);
|
||||
suf_buf = NULL;
|
||||
}
|
||||
|
||||
if (pre_buf) {
|
||||
pl_free(pre_buf);
|
||||
pre_buf = NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int read_rsvd_part(int fd, uint32_t offset, void *buffer, uint32_t read_len)
|
||||
{
|
||||
uint32_t off_set = offset;
|
||||
uint32_t len = 0;
|
||||
int ret = 0;
|
||||
|
||||
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &off_set, buffer, read_len);
|
||||
if (ret) {
|
||||
PL_ERR("hal flash read failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
len = off_set - offset;
|
||||
if (len != read_len) {
|
||||
PL_ERR("read failed read_len is %d", (unsigned int)len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int close_rsvd_part(int fd)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
46
Living_SDK/security/irot/km/platform/include/plat_comm.h
Normal file
46
Living_SDK/security/irot/km/platform/include/plat_comm.h
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
/**
|
||||
* Copyright (C) 2017 The YunOS Project. All rights reserved.
|
||||
*/
|
||||
|
||||
#ifndef _PLAT_DBG_H_
|
||||
#define _PLAT_DBG_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define _DEPRESS_UNUSED_WARNING(_x) do { (_x) = (_x); } while (0)
|
||||
|
||||
#define PLAT_TAG "PL_LOG"
|
||||
|
||||
#ifndef PLATFORM_ANDROID
|
||||
#define PL_ERR(_f, _a ...) printf("PL ERR %s %d: "_f, __FUNCTION__, __LINE__, ##_a)
|
||||
#else
|
||||
#include <android/log.h>
|
||||
#define LOG_ERR(...) __android_log_print( \
|
||||
ANDROID_LOG_ERROR, \
|
||||
PLAT_TAG, \
|
||||
__VA_ARGS__)
|
||||
#define LOG_INF(...) __android_log_print( \
|
||||
ANDROID_LOG_INFO, \
|
||||
PLAT_TAG, \
|
||||
__VA_ARGS__)
|
||||
#define PL_ERR(_f, _a ...) LOG_ERR("ERR %s %d: "_f, __FUNCTION__, __LINE__, ##_a)
|
||||
#endif /* PLATFORM_ANDROID */
|
||||
|
||||
#ifdef CONFIG_PL_DBG
|
||||
void dump_data(char *name, uint8_t *data, uint32_t len);
|
||||
#ifndef PLATFORM_ANDROID
|
||||
#define PL_INF(_f, _a ...) printf("PL ERR %s %d: "_f, __FUNCTION__, __LINE__, ##_a)
|
||||
#else
|
||||
#define PL_INF(_f, _a ...) LOG_INF("%s %d: "_f, __FUNCTION__, __LINE__, ##_a)
|
||||
#endif /* PLATFORM_ANDROID */
|
||||
#else
|
||||
#define PL_INF(_f, _a ...)
|
||||
#endif
|
||||
|
||||
#define pl_malloc malloc
|
||||
#define pl_free free
|
||||
|
||||
#endif /* _PLAT_DBG_H_ */
|
||||
|
||||
77
Living_SDK/security/irot/km/platform/include/plat_gen.h
Normal file
77
Living_SDK/security/irot/km/platform/include/plat_gen.h
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
/**
|
||||
* Copyright (C) 2017 The YunOS Project. All rights reserved.
|
||||
*/
|
||||
|
||||
#ifndef _PLAT_GEN_H_
|
||||
#define _PLAT_GEN_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef enum {
|
||||
RO_READ = 1,
|
||||
RO_WRITE = 2
|
||||
} rsvd_part_perm_t;
|
||||
|
||||
/*
|
||||
* get device unique id
|
||||
*
|
||||
* param: out: dev_id: device uinque id
|
||||
* in_out: id_len: device uinque id length
|
||||
*
|
||||
* return: 0: success
|
||||
* -1: fail
|
||||
*
|
||||
*/
|
||||
int get_dev_id(uint8_t *dev_id, uint32_t *id_len);
|
||||
|
||||
/*
|
||||
* open the reserved partition that user has read and write permission
|
||||
*
|
||||
* parametr: in: flag: RO_READ: user only has read permission
|
||||
* RO_WRITE: user only has write permission
|
||||
* RO_READ | RO_WRITE: user has read and write permission
|
||||
*
|
||||
* return: the new file descriptor: success
|
||||
* -1: fail
|
||||
*/
|
||||
int open_rsvd_part(int flag);
|
||||
|
||||
/*
|
||||
* write reserved partition
|
||||
*
|
||||
* parametr: in: fd: file handle, can be ignored if no file system
|
||||
* offset: the offset of the reserved partition
|
||||
* data: the data need to write
|
||||
* data_len: the length of the data need to write
|
||||
*
|
||||
* return: 0: success
|
||||
* -1: fail
|
||||
*/
|
||||
int write_rsvd_part(int fd, uint32_t offset, void *data, uint32_t data_len);
|
||||
|
||||
/*
|
||||
* read reserved partition
|
||||
*
|
||||
* parametr: in: fd: file handle, can be ignored if no file system
|
||||
* offset: the offset of the reserved partition
|
||||
* read_len: the length of the data need to read
|
||||
* out: buffer: the data read from the reserved part
|
||||
|
||||
* return: 0: success
|
||||
-1: fail
|
||||
*/
|
||||
int read_rsvd_part(int fd, uint32_t offset, void *buffer, uint32_t read_len);
|
||||
|
||||
/*
|
||||
* close the file descriptor of reserved partition, if no file system support, return 0 directly
|
||||
*
|
||||
* parametr: in: fd: file descriptor of the reserved part
|
||||
|
||||
* return: 0: success
|
||||
-1: fail
|
||||
*/
|
||||
|
||||
int close_rsvd_part(int fd);
|
||||
|
||||
#endif /* _PLAT_GEN_H_ */
|
||||
|
||||
282
Living_SDK/security/irot/km/platform/linux/plat_gen.c
Normal file
282
Living_SDK/security/irot/km/platform/linux/plat_gen.c
Normal file
|
|
@ -0,0 +1,282 @@
|
|||
/**
|
||||
* Copyright (C) 2017 The YunOS Project. All rights reserved.
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include "plat_gen.h"
|
||||
#include "plat_comm.h"
|
||||
|
||||
#define DEV_ID_LEN 36
|
||||
#define FLASH_BLOCK_LEN 2048
|
||||
|
||||
#if CONFIG_SUDO_SUPPORT
|
||||
#define PROV_DATA_PATH "/usr/.security/.dev_key"
|
||||
#define DIR_NAME "/usr/.security"
|
||||
#else
|
||||
|
||||
#ifdef PLATFORM_ANDROID
|
||||
#define PROV_DATA_PATH "/sdcard/Android/data/.security/.dev_key"
|
||||
#define DIR_NAME "/sdcard/Android/data/.security"
|
||||
#else
|
||||
#define PROV_DATA_PATH "./.security/.dev_key"
|
||||
#define DIR_NAME "./.security"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define FILE_NAME PROV_DATA_PATH
|
||||
|
||||
#define pl_memcpy memcpy
|
||||
#define pl_memset memset
|
||||
#define pl_malloc malloc
|
||||
#define pl_free free
|
||||
|
||||
static int _init_rsvd_part()
|
||||
{
|
||||
//if no rserved partition just create km file
|
||||
DIR *dir;
|
||||
size_t file_len = 0;
|
||||
uint8_t *flash_block = NULL;
|
||||
int fd = 0;
|
||||
int fret = 0;
|
||||
int ret = 0;
|
||||
|
||||
dir = opendir((char *)DIR_NAME);
|
||||
if (NULL == dir) {
|
||||
fret = mkdir(DIR_NAME, S_IRWXU |
|
||||
S_IRGRP | S_IROTH | S_IXGRP | S_IXOTH);
|
||||
if (fret < 0) {
|
||||
PL_ERR("mkdir failed errno is %d\n", errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifndef PLATFORM_ANDROID
|
||||
if (chmod(DIR_NAME, S_IRWXU |
|
||||
S_IRGRP | S_IROTH | S_IXGRP | S_IXOTH)) {
|
||||
PL_ERR("chmod failed errno is %d\n", errno);
|
||||
closedir(dir);
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
} else {
|
||||
closedir(dir);
|
||||
}
|
||||
|
||||
//file has already exist
|
||||
if (access(FILE_NAME, F_OK) == 0) {
|
||||
PL_INF("file has already exist\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
//create FLASH_BLOCK_LEN file
|
||||
fd = open(FILE_NAME, O_CREAT|O_RDWR, S_IRWXU | S_IRGRP | S_IROTH);
|
||||
if (fd == -1) {
|
||||
PL_ERR("open file failed errno %d\n", errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifndef PLATFORM_ANDROID
|
||||
if (fchmod(fd, S_IRWXU | S_IRGRP | S_IROTH)) {
|
||||
PL_ERR("file chmod failed errno is %d\n", errno);
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
flash_block = (uint8_t *)pl_malloc(FLASH_BLOCK_LEN);
|
||||
if (!flash_block) {
|
||||
PL_ERR("malloc failed\n");
|
||||
ret = -1;
|
||||
goto clean;
|
||||
}
|
||||
|
||||
pl_memset(flash_block, 0, FLASH_BLOCK_LEN);
|
||||
|
||||
//fix file length first FLASH_BLOCK_LEN for km
|
||||
file_len = write(fd, flash_block, FLASH_BLOCK_LEN);
|
||||
if (file_len != FLASH_BLOCK_LEN) {
|
||||
PL_ERR("seek failed errno %d\n", errno);
|
||||
ret = -1;
|
||||
goto clean;
|
||||
}
|
||||
|
||||
//fix file length last FLASH_BLICK_LEN for prov
|
||||
file_len = write(fd, flash_block, FLASH_BLOCK_LEN);
|
||||
if (file_len != FLASH_BLOCK_LEN) {
|
||||
PL_ERR("seek failed errno %d\n", errno);
|
||||
ret = -1;
|
||||
goto clean;
|
||||
}
|
||||
|
||||
clean:
|
||||
close(fd);
|
||||
if (flash_block) {
|
||||
pl_free(flash_block);
|
||||
flash_block = NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if 0
|
||||
#define cpuid(in,a,b,c,d)
|
||||
asm("cpuid": "=a" (a), "=b" (b), "=c" (c), "=d" (d) : "a" (in));
|
||||
|
||||
int get_dev_id(uint8_t *dev_id, uint32_t *id_len)
|
||||
{
|
||||
int i = 0;
|
||||
unsigned long eax, ebx, ecx, edx;
|
||||
unsigned long maxi, unused;
|
||||
|
||||
if (*id_len < DEV_ID_LEN) {
|
||||
PL_ERR("short buffer id len is %d\n", *id_len);
|
||||
*id_len = DEV_ID_LEN;
|
||||
return -1;
|
||||
}
|
||||
|
||||
cpuid (0, maxi, unused, unused, unused);
|
||||
maxi &= 0xffff;
|
||||
|
||||
if (maxi < 3) {
|
||||
PL_ERR("not support get cpuid\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
cpuid (1, eax, ebx, ecx, edx);
|
||||
|
||||
dev_id[0] = (eax & 0xff000000) >> 24;
|
||||
dev_id[1] = (eax & 0x00ff0000) >> 16;
|
||||
dev_id[2] = (eax & 0x0000ff00) >> 8;
|
||||
dev_id[3] = eax & 0x000000ff;
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
int get_dev_id(uint8_t *dev_id, uint32_t *id_len)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (*id_len < DEV_ID_LEN) {
|
||||
PL_ERR("short buffer id len is %d\n", *id_len);
|
||||
*id_len = DEV_ID_LEN;
|
||||
return -1;
|
||||
}
|
||||
|
||||
#if 0
|
||||
size_t read_len = 0;
|
||||
int fd = -1;
|
||||
fd = open("/sys/class/dmi/id/product_uuid", O_RDONLY);
|
||||
if (fd == -1) {
|
||||
PL_ERR("open file failed errno %d\n", errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
lseek(fd, 0, SEEK_SET);
|
||||
read_len = read(fd, dev_id, DEV_ID_LEN);
|
||||
if (read_len != DEV_ID_LEN) {
|
||||
PL_ERR("read failed errno %d\n", errno);
|
||||
ret = -1;
|
||||
goto clean;
|
||||
}
|
||||
|
||||
*id_len = DEV_ID_LEN;
|
||||
|
||||
clean:
|
||||
close(fd);
|
||||
#else
|
||||
char *product_uuid = "12345678-1234-5678-ABCD-CBA987654321";
|
||||
pl_memcpy(dev_id, product_uuid, DEV_ID_LEN);
|
||||
*id_len = DEV_ID_LEN;
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int open_rsvd_part(int flag)
|
||||
{
|
||||
int fd = -1;
|
||||
int ret = 0;
|
||||
|
||||
//check if file has already exist
|
||||
if (access(FILE_NAME, F_OK)) {
|
||||
ret = _init_rsvd_part();
|
||||
if (ret) {
|
||||
PL_ERR("init rsvd part failed\n");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (RO_READ == flag) {
|
||||
fd = open(PROV_DATA_PATH, O_RDONLY);;
|
||||
} else if (RO_WRITE == flag) {
|
||||
fd = open(PROV_DATA_PATH, O_WRONLY);
|
||||
} else if ((RO_READ | RO_WRITE) == flag) {
|
||||
fd = open(PROV_DATA_PATH, O_RDWR, S_IRUSR|S_IWUSR);
|
||||
} else {
|
||||
PL_ERR("not support flag\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (fd == -1) {
|
||||
PL_ERR("open file failed errno %d\n", errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
int write_rsvd_part(int fd, uint32_t offset, void *data, uint32_t data_len)
|
||||
{
|
||||
uint32_t write_len = 0;
|
||||
|
||||
if (fd < 0 || (data_len != 0 && data == NULL)) {
|
||||
PL_ERR("bad param \n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (data_len == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
lseek(fd, offset, SEEK_SET);
|
||||
write_len = write(fd, data, data_len);
|
||||
if (write_len != data_len) {
|
||||
PL_ERR("write failed errno is %d\n", errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int read_rsvd_part(int fd, uint32_t offset, void *buffer, uint32_t read_len)
|
||||
{
|
||||
uint32_t real_read_len = 0;
|
||||
|
||||
if (fd < 0) {
|
||||
PL_ERR("bad params fd\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
lseek(fd, offset, SEEK_SET);
|
||||
|
||||
real_read_len = read(fd, buffer, read_len);
|
||||
if (real_read_len != read_len) {
|
||||
PL_ERR("read failed real read len is %d\n", real_read_len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int close_rsvd_part(int fd)
|
||||
{
|
||||
return close(fd);
|
||||
}
|
||||
|
||||
181
Living_SDK/security/irot/km/platform/mk3060/plat_gen.c
Executable file
181
Living_SDK/security/irot/km/platform/mk3060/plat_gen.c
Executable file
|
|
@ -0,0 +1,181 @@
|
|||
/**
|
||||
* Copyright (C) 2017 The YunOS Project. All rights reserved.
|
||||
*/
|
||||
|
||||
#include "hal/wifi.h"
|
||||
#include "hal/soc/flash.h"
|
||||
#include "plat_gen.h"
|
||||
#include "plat_comm.h"
|
||||
|
||||
#define DEV_ID_LEN 6
|
||||
#define MIN_SECTOR_SIZE 4096
|
||||
#define MIN_SECTOR_SHIFT 12
|
||||
|
||||
#define ROUNDUP(a, b) (((a) + ((b)-1)) & ~((b)-1))
|
||||
#define ROUNDDOWN(a, b) ((a) & ~((b)-1))
|
||||
|
||||
int get_dev_id(uint8_t *dev_id, uint32_t *id_len)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (*id_len < DEV_ID_LEN) {
|
||||
PL_ERR("short buffer id len is %d\n", (unsigned int)*id_len);
|
||||
*id_len = DEV_ID_LEN;
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = hal_wifi_get_mac_addr(NULL, dev_id);
|
||||
if (ret) {
|
||||
PL_ERR("get mac addr failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
*id_len = DEV_ID_LEN;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//for mk3060 not support return 0 directly
|
||||
int open_rsvd_part(int flag)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int write_rsvd_part(int fd, uint32_t offset, void *data, uint32_t data_len)
|
||||
{
|
||||
int ret = 0;
|
||||
uint32_t erase_size = 0;
|
||||
uint32_t pre_len = 0;
|
||||
uint8_t *pre_buf = NULL;
|
||||
uint32_t suf_len = 0;
|
||||
uint8_t *suf_buf = NULL;
|
||||
uint32_t first_off = 0;
|
||||
uint32_t last_off = 0;
|
||||
uint32_t fin_off = offset + data_len;
|
||||
uint32_t tmp_off = 0;
|
||||
|
||||
if (data_len != 0 && data == NULL) {
|
||||
PL_ERR("bad param \n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (data_len == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
first_off = ROUNDDOWN(offset, MIN_SECTOR_SIZE);
|
||||
pre_len = offset - first_off;
|
||||
if (pre_len) {
|
||||
pre_buf = (uint8_t *)pl_malloc(pre_len);
|
||||
if (pre_buf == NULL) {
|
||||
PL_ERR("malloc pre buf failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
//store the data of first block that will be erased
|
||||
tmp_off = first_off;
|
||||
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
|
||||
pre_buf, pre_len);
|
||||
if (ret || (tmp_off - first_off != pre_len)) {
|
||||
PL_ERR("hal flash pre read failed\n");
|
||||
ret = -1;
|
||||
goto clean;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
last_off = ROUNDUP(fin_off, MIN_SECTOR_SIZE);
|
||||
suf_len = last_off - fin_off;
|
||||
if (suf_len) {
|
||||
suf_buf = (uint8_t *)pl_malloc(suf_len);
|
||||
if (suf_buf == NULL) {
|
||||
PL_ERR("malloc suf buf failed\n");
|
||||
ret = -1;
|
||||
goto clean;
|
||||
}
|
||||
|
||||
//read the data of last block that will be erased
|
||||
tmp_off = fin_off;
|
||||
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
|
||||
suf_buf, suf_len);
|
||||
if (ret || (tmp_off - fin_off != suf_len)) {
|
||||
PL_ERR("hal flash pre read failed\n");
|
||||
goto clean;
|
||||
}
|
||||
}
|
||||
|
||||
erase_size = last_off - first_off;
|
||||
ret = hal_flash_erase(HAL_PARTITION_PARAMETER_4, first_off, erase_size);
|
||||
if (ret) {
|
||||
PL_ERR("flash erase failed ret %d\n", ret);
|
||||
goto clean;
|
||||
}
|
||||
PL_INF("pre_len is %d, suf len is %d, erase len is %d, first_off is %d\n",
|
||||
(unsigned int)pre_len, (unsigned int)suf_len, (unsigned int)erase_size, (unsigned int)first_off);
|
||||
|
||||
/* write first sector data that be erased*/
|
||||
tmp_off = first_off;
|
||||
if (pre_len) {
|
||||
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, pre_buf, pre_len);
|
||||
if (ret || (tmp_off - first_off) != pre_len) {
|
||||
PL_ERR("write pre buf failed %d, len %d\n", ret, (unsigned int)tmp_off);
|
||||
goto clean;
|
||||
}
|
||||
}
|
||||
|
||||
/* write the data need to write */
|
||||
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, data, data_len);
|
||||
if (ret) {
|
||||
PL_ERR("write pre buf failed %d\n", ret);
|
||||
goto clean;
|
||||
}
|
||||
|
||||
/* write last sector data that be erased */
|
||||
if (suf_len) {
|
||||
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, suf_buf, suf_len);
|
||||
if (ret) {
|
||||
PL_ERR("write pre buf failed %d\n", ret);
|
||||
goto clean;
|
||||
}
|
||||
}
|
||||
|
||||
clean:
|
||||
if (suf_buf) {
|
||||
pl_free(suf_buf);
|
||||
suf_buf = NULL;
|
||||
}
|
||||
|
||||
if (pre_buf) {
|
||||
pl_free(pre_buf);
|
||||
pre_buf = NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int read_rsvd_part(int fd, uint32_t offset, void *buffer, uint32_t read_len)
|
||||
{
|
||||
uint32_t off_set = offset;
|
||||
uint32_t len = 0;
|
||||
int ret = 0;
|
||||
|
||||
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &off_set, buffer, read_len);
|
||||
if (ret) {
|
||||
PL_ERR("hal flash read failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
len = off_set - offset;
|
||||
if (len != read_len) {
|
||||
PL_ERR("read failed read_len is %d", (unsigned int)len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int close_rsvd_part(int fd)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
181
Living_SDK/security/irot/km/platform/mk3080/plat_gen.c
Executable file
181
Living_SDK/security/irot/km/platform/mk3080/plat_gen.c
Executable file
|
|
@ -0,0 +1,181 @@
|
|||
/**
|
||||
* Copyright (C) 2017 The YunOS Project. All rights reserved.
|
||||
*/
|
||||
|
||||
#include "hal/wifi.h"
|
||||
#include "hal/soc/flash.h"
|
||||
#include "plat_gen.h"
|
||||
#include "plat_comm.h"
|
||||
|
||||
#define DEV_ID_LEN 6
|
||||
#define MIN_SECTOR_SIZE 4096
|
||||
#define MIN_SECTOR_SHIFT 12
|
||||
|
||||
#define ROUNDUP(a, b) (((a) + ((b)-1)) & ~((b)-1))
|
||||
#define ROUNDDOWN(a, b) ((a) & ~((b)-1))
|
||||
|
||||
int get_dev_id(uint8_t *dev_id, uint32_t *id_len)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (*id_len < DEV_ID_LEN) {
|
||||
PL_ERR("short buffer id len is %d\n", (unsigned int)*id_len);
|
||||
*id_len = DEV_ID_LEN;
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = hal_wifi_get_mac_addr(NULL, dev_id);
|
||||
if (ret) {
|
||||
PL_ERR("get mac addr failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
*id_len = DEV_ID_LEN;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//for mk3080 not support return 0 directly
|
||||
int open_rsvd_part(int flag)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int write_rsvd_part(int fd, uint32_t offset, void *data, uint32_t data_len)
|
||||
{
|
||||
int ret = 0;
|
||||
uint32_t erase_size = 0;
|
||||
uint32_t pre_len = 0;
|
||||
uint8_t *pre_buf = NULL;
|
||||
uint32_t suf_len = 0;
|
||||
uint8_t *suf_buf = NULL;
|
||||
uint32_t first_off = 0;
|
||||
uint32_t last_off = 0;
|
||||
uint32_t fin_off = offset + data_len;
|
||||
uint32_t tmp_off = 0;
|
||||
|
||||
if (data_len != 0 && data == NULL) {
|
||||
PL_ERR("bad param \n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (data_len == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
first_off = ROUNDDOWN(offset, MIN_SECTOR_SIZE);
|
||||
pre_len = offset - first_off;
|
||||
if (pre_len) {
|
||||
pre_buf = (uint8_t *)pl_malloc(pre_len);
|
||||
if (pre_buf == NULL) {
|
||||
PL_ERR("malloc pre buf failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
//store the data of first block that will be erased
|
||||
tmp_off = first_off;
|
||||
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
|
||||
pre_buf, pre_len);
|
||||
if (ret || (tmp_off - first_off != pre_len)) {
|
||||
PL_ERR("hal flash pre read failed\n");
|
||||
ret = -1;
|
||||
goto clean;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
last_off = ROUNDUP(fin_off, MIN_SECTOR_SIZE);
|
||||
suf_len = last_off - fin_off;
|
||||
if (suf_len) {
|
||||
suf_buf = (uint8_t *)pl_malloc(suf_len);
|
||||
if (suf_buf == NULL) {
|
||||
PL_ERR("malloc suf buf failed\n");
|
||||
ret = -1;
|
||||
goto clean;
|
||||
}
|
||||
|
||||
//read the data of last block that will be erased
|
||||
tmp_off = fin_off;
|
||||
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
|
||||
suf_buf, suf_len);
|
||||
if (ret || (tmp_off - fin_off != suf_len)) {
|
||||
PL_ERR("hal flash pre read failed\n");
|
||||
goto clean;
|
||||
}
|
||||
}
|
||||
|
||||
erase_size = last_off - first_off;
|
||||
ret = hal_flash_erase(HAL_PARTITION_PARAMETER_4, first_off, erase_size);
|
||||
if (ret) {
|
||||
PL_ERR("flash erase failed ret %d\n", ret);
|
||||
goto clean;
|
||||
}
|
||||
PL_INF("pre_len is %d, suf len is %d, erase len is %d, first_off is %d\n",
|
||||
(unsigned int)pre_len, (unsigned int)suf_len, (unsigned int)erase_size, (unsigned int)first_off);
|
||||
|
||||
/* write first sector data that be erased*/
|
||||
tmp_off = first_off;
|
||||
if (pre_len) {
|
||||
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, pre_buf, pre_len);
|
||||
if (ret || (tmp_off - first_off) != pre_len) {
|
||||
PL_ERR("write pre buf failed %d, len %d\n", ret, (unsigned int)tmp_off);
|
||||
goto clean;
|
||||
}
|
||||
}
|
||||
|
||||
/* write the data need to write */
|
||||
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, data, data_len);
|
||||
if (ret) {
|
||||
PL_ERR("write pre buf failed %d\n", ret);
|
||||
goto clean;
|
||||
}
|
||||
|
||||
/* write last sector data that be erased */
|
||||
if (suf_len) {
|
||||
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, suf_buf, suf_len);
|
||||
if (ret) {
|
||||
PL_ERR("write pre buf failed %d\n", ret);
|
||||
goto clean;
|
||||
}
|
||||
}
|
||||
|
||||
clean:
|
||||
if (suf_buf) {
|
||||
pl_free(suf_buf);
|
||||
suf_buf = NULL;
|
||||
}
|
||||
|
||||
if (pre_buf) {
|
||||
pl_free(pre_buf);
|
||||
pre_buf = NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int read_rsvd_part(int fd, uint32_t offset, void *buffer, uint32_t read_len)
|
||||
{
|
||||
uint32_t off_set = offset;
|
||||
uint32_t len = 0;
|
||||
int ret = 0;
|
||||
|
||||
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &off_set, buffer, read_len);
|
||||
if (ret) {
|
||||
PL_ERR("hal flash read failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
len = off_set - offset;
|
||||
if (len != read_len) {
|
||||
PL_ERR("read failed read_len is %d", (unsigned int)len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int close_rsvd_part(int fd)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
181
Living_SDK/security/irot/km/platform/mk5080/plat_gen.c
Executable file
181
Living_SDK/security/irot/km/platform/mk5080/plat_gen.c
Executable file
|
|
@ -0,0 +1,181 @@
|
|||
/**
|
||||
* Copyright (C) 2017 The YunOS Project. All rights reserved.
|
||||
*/
|
||||
|
||||
#include "hal/wifi.h"
|
||||
#include "hal/soc/flash.h"
|
||||
#include "plat_gen.h"
|
||||
#include "plat_comm.h"
|
||||
|
||||
#define DEV_ID_LEN 6
|
||||
#define MIN_SECTOR_SIZE 4096
|
||||
#define MIN_SECTOR_SHIFT 12
|
||||
|
||||
#define ROUNDUP(a, b) (((a) + ((b)-1)) & ~((b)-1))
|
||||
#define ROUNDDOWN(a, b) ((a) & ~((b)-1))
|
||||
|
||||
int get_dev_id(uint8_t *dev_id, uint32_t *id_len)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (*id_len < DEV_ID_LEN) {
|
||||
PL_ERR("short buffer id len is %d\n", (unsigned int)*id_len);
|
||||
*id_len = DEV_ID_LEN;
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = hal_wifi_get_mac_addr(NULL, dev_id);
|
||||
if (ret) {
|
||||
PL_ERR("get mac addr failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
*id_len = DEV_ID_LEN;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//for mk3080 not support return 0 directly
|
||||
int open_rsvd_part(int flag)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int write_rsvd_part(int fd, uint32_t offset, void *data, uint32_t data_len)
|
||||
{
|
||||
int ret = 0;
|
||||
uint32_t erase_size = 0;
|
||||
uint32_t pre_len = 0;
|
||||
uint8_t *pre_buf = NULL;
|
||||
uint32_t suf_len = 0;
|
||||
uint8_t *suf_buf = NULL;
|
||||
uint32_t first_off = 0;
|
||||
uint32_t last_off = 0;
|
||||
uint32_t fin_off = offset + data_len;
|
||||
uint32_t tmp_off = 0;
|
||||
|
||||
if (data_len != 0 && data == NULL) {
|
||||
PL_ERR("bad param \n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (data_len == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
first_off = ROUNDDOWN(offset, MIN_SECTOR_SIZE);
|
||||
pre_len = offset - first_off;
|
||||
if (pre_len) {
|
||||
pre_buf = (uint8_t *)pl_malloc(pre_len);
|
||||
if (pre_buf == NULL) {
|
||||
PL_ERR("malloc pre buf failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
//store the data of first block that will be erased
|
||||
tmp_off = first_off;
|
||||
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
|
||||
pre_buf, pre_len);
|
||||
if (ret || (tmp_off - first_off != pre_len)) {
|
||||
PL_ERR("hal flash pre read failed\n");
|
||||
ret = -1;
|
||||
goto clean;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
last_off = ROUNDUP(fin_off, MIN_SECTOR_SIZE);
|
||||
suf_len = last_off - fin_off;
|
||||
if (suf_len) {
|
||||
suf_buf = (uint8_t *)pl_malloc(suf_len);
|
||||
if (suf_buf == NULL) {
|
||||
PL_ERR("malloc suf buf failed\n");
|
||||
ret = -1;
|
||||
goto clean;
|
||||
}
|
||||
|
||||
//read the data of last block that will be erased
|
||||
tmp_off = fin_off;
|
||||
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
|
||||
suf_buf, suf_len);
|
||||
if (ret || (tmp_off - fin_off != suf_len)) {
|
||||
PL_ERR("hal flash pre read failed\n");
|
||||
goto clean;
|
||||
}
|
||||
}
|
||||
|
||||
erase_size = last_off - first_off;
|
||||
ret = hal_flash_erase(HAL_PARTITION_PARAMETER_4, first_off, erase_size);
|
||||
if (ret) {
|
||||
PL_ERR("flash erase failed ret %d\n", ret);
|
||||
goto clean;
|
||||
}
|
||||
PL_INF("pre_len is %d, suf len is %d, erase len is %d, first_off is %d\n",
|
||||
(unsigned int)pre_len, (unsigned int)suf_len, (unsigned int)erase_size, (unsigned int)first_off);
|
||||
|
||||
/* write first sector data that be erased*/
|
||||
tmp_off = first_off;
|
||||
if (pre_len) {
|
||||
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, pre_buf, pre_len);
|
||||
if (ret || (tmp_off - first_off) != pre_len) {
|
||||
PL_ERR("write pre buf failed %d, len %d\n", ret, (unsigned int)tmp_off);
|
||||
goto clean;
|
||||
}
|
||||
}
|
||||
|
||||
/* write the data need to write */
|
||||
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, data, data_len);
|
||||
if (ret) {
|
||||
PL_ERR("write pre buf failed %d\n", ret);
|
||||
goto clean;
|
||||
}
|
||||
|
||||
/* write last sector data that be erased */
|
||||
if (suf_len) {
|
||||
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, suf_buf, suf_len);
|
||||
if (ret) {
|
||||
PL_ERR("write pre buf failed %d\n", ret);
|
||||
goto clean;
|
||||
}
|
||||
}
|
||||
|
||||
clean:
|
||||
if (suf_buf) {
|
||||
pl_free(suf_buf);
|
||||
suf_buf = NULL;
|
||||
}
|
||||
|
||||
if (pre_buf) {
|
||||
pl_free(pre_buf);
|
||||
pre_buf = NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int read_rsvd_part(int fd, uint32_t offset, void *buffer, uint32_t read_len)
|
||||
{
|
||||
uint32_t off_set = offset;
|
||||
uint32_t len = 0;
|
||||
int ret = 0;
|
||||
|
||||
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &off_set, buffer, read_len);
|
||||
if (ret) {
|
||||
PL_ERR("hal flash read failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
len = off_set - offset;
|
||||
if (len != read_len) {
|
||||
PL_ERR("read failed read_len is %d", (unsigned int)len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int close_rsvd_part(int fd)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
13
Living_SDK/security/irot/km/platform/platform.mk
Normal file
13
Living_SDK/security/irot/km/platform/platform.mk
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
NAME := libplat_gen
|
||||
|
||||
GLOBAL_INCLUDES += include
|
||||
|
||||
ifeq ($(HOST_ARCH), linux)
|
||||
HOST_NAME := $(HOST_ARCH)
|
||||
else
|
||||
HOST_NAME := $(shell echo $(CONFIG_SYSINFO_DEVICE_NAME)|tr A-Z a-z)
|
||||
endif
|
||||
|
||||
$(NAME)_SOURCES := $(HOST_NAME)/plat_gen.c
|
||||
|
||||
$(NAME)_COMPONENTS := alicrypto
|
||||
181
Living_SDK/security/irot/km/platform/uno-91h/plat_gen.c
Normal file
181
Living_SDK/security/irot/km/platform/uno-91h/plat_gen.c
Normal file
|
|
@ -0,0 +1,181 @@
|
|||
/**
|
||||
* Copyright (C) 2017 The YunOS Project. All rights reserved.
|
||||
*/
|
||||
|
||||
#include "hal/wifi.h"
|
||||
#include "hal/soc/flash.h"
|
||||
#include "plat_gen.h"
|
||||
#include "plat_comm.h"
|
||||
|
||||
#define DEV_ID_LEN 6
|
||||
#define MIN_SECTOR_SIZE 4096
|
||||
#define MIN_SECTOR_SHIFT 12
|
||||
|
||||
#define ROUNDUP(a, b) (((a) + ((b)-1)) & ~((b)-1))
|
||||
#define ROUNDDOWN(a, b) ((a) & ~((b)-1))
|
||||
|
||||
int get_dev_id(uint8_t *dev_id, uint32_t *id_len)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (*id_len < DEV_ID_LEN) {
|
||||
PL_ERR("short buffer id len is %d\n", (unsigned int)*id_len);
|
||||
*id_len = DEV_ID_LEN;
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = hal_wifi_get_mac_addr(NULL, dev_id);
|
||||
if (ret) {
|
||||
PL_ERR("get mac addr failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
*id_len = DEV_ID_LEN;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//for rda5981x not support return 0 directly
|
||||
int open_rsvd_part(int flag)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int write_rsvd_part(int fd, uint32_t offset, void *data, uint32_t data_len)
|
||||
{
|
||||
int ret = 0;
|
||||
uint32_t erase_size = 0;
|
||||
uint32_t pre_len = 0;
|
||||
uint8_t *pre_buf = NULL;
|
||||
uint32_t suf_len = 0;
|
||||
uint8_t *suf_buf = NULL;
|
||||
uint32_t first_off = 0;
|
||||
uint32_t last_off = 0;
|
||||
uint32_t fin_off = offset + data_len;
|
||||
uint32_t tmp_off = 0;
|
||||
|
||||
if (data_len != 0 && data == NULL) {
|
||||
PL_ERR("bad param \n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (data_len == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
first_off = ROUNDDOWN(offset, MIN_SECTOR_SIZE);
|
||||
pre_len = offset - first_off;
|
||||
if (pre_len) {
|
||||
pre_buf = (uint8_t *)pl_malloc(pre_len);
|
||||
if (pre_buf == NULL) {
|
||||
PL_ERR("malloc pre buf failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
//store the data of first block that will be erased
|
||||
tmp_off = first_off;
|
||||
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
|
||||
pre_buf, pre_len);
|
||||
if (ret || (tmp_off - first_off != pre_len)) {
|
||||
PL_ERR("hal flash pre read failed\n");
|
||||
ret = -1;
|
||||
goto clean;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
last_off = ROUNDUP(fin_off, MIN_SECTOR_SIZE);
|
||||
suf_len = last_off - fin_off;
|
||||
if (suf_len) {
|
||||
suf_buf = (uint8_t *)pl_malloc(suf_len);
|
||||
if (suf_buf == NULL) {
|
||||
PL_ERR("malloc suf buf failed\n");
|
||||
ret = -1;
|
||||
goto clean;
|
||||
}
|
||||
|
||||
//read the data of last block that will be erased
|
||||
tmp_off = fin_off;
|
||||
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
|
||||
suf_buf, suf_len);
|
||||
if (ret || (tmp_off - fin_off != suf_len)) {
|
||||
PL_ERR("hal flash pre read failed\n");
|
||||
goto clean;
|
||||
}
|
||||
}
|
||||
|
||||
erase_size = last_off - first_off;
|
||||
ret = hal_flash_erase(HAL_PARTITION_PARAMETER_4, first_off, erase_size);
|
||||
if (ret) {
|
||||
PL_ERR("flash erase failed ret %d\n", ret);
|
||||
goto clean;
|
||||
}
|
||||
PL_INF("pre_len is %d, suf len is %d, erase len is %d, first_off is %d\n",
|
||||
(unsigned int)pre_len, (unsigned int)suf_len, (unsigned int)erase_size, (unsigned int)first_off);
|
||||
|
||||
/* write first sector data that be erased*/
|
||||
tmp_off = first_off;
|
||||
if (pre_len) {
|
||||
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, pre_buf, pre_len);
|
||||
if (ret || (tmp_off - first_off) != pre_len) {
|
||||
PL_ERR("write pre buf failed %d, len %d\n", ret, (unsigned int)tmp_off);
|
||||
goto clean;
|
||||
}
|
||||
}
|
||||
|
||||
/* write the data need to write */
|
||||
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, data, data_len);
|
||||
if (ret) {
|
||||
PL_ERR("write pre buf failed %d\n", ret);
|
||||
goto clean;
|
||||
}
|
||||
|
||||
/* write last sector data that be erased */
|
||||
if (suf_len) {
|
||||
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, suf_buf, suf_len);
|
||||
if (ret) {
|
||||
PL_ERR("write pre buf failed %d\n", ret);
|
||||
goto clean;
|
||||
}
|
||||
}
|
||||
|
||||
clean:
|
||||
if (suf_buf) {
|
||||
pl_free(suf_buf);
|
||||
suf_buf = NULL;
|
||||
}
|
||||
|
||||
if (pre_buf) {
|
||||
pl_free(pre_buf);
|
||||
pre_buf = NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int read_rsvd_part(int fd, uint32_t offset, void *buffer, uint32_t read_len)
|
||||
{
|
||||
uint32_t off_set = offset;
|
||||
uint32_t len = 0;
|
||||
int ret = 0;
|
||||
|
||||
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &off_set, buffer, read_len);
|
||||
if (ret) {
|
||||
PL_ERR("hal flash read failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
len = off_set - offset;
|
||||
if (len != read_len) {
|
||||
PL_ERR("read failed read_len is %d", (unsigned int)len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int close_rsvd_part(int fd)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
5
Living_SDK/security/irot/km/ucube.py
Normal file
5
Living_SDK/security/irot/km/ucube.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
src = Glob('*.c')
|
||||
component = aos_component('libkm', src)
|
||||
component.add_comp_deps('security/irot/km/platform', 'security/alicrypto')
|
||||
component.add_prebuilt_libs('lib/' + component.get_arch() + '/libkm.a')
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue