|
1 | 1 | /* |
2 | 2 | * Copyright (C) 2015-2017 Alibaba Group Holding Limited |
3 | 3 | */ |
4 | | -#include <fcntl.h> |
5 | | -#include <sys/types.h> |
6 | 4 | #include <unistd.h> |
7 | 5 | #include <stdio.h> |
8 | | -#include <sys/ioctl.h> |
9 | 6 | #include "k_api.h" |
10 | 7 | #include "kv_conf.h" |
11 | 8 | #include "kv_adapt.h" |
12 | | -#include "aos/hal/flash.h" |
13 | | -#include "vfsdev/flash_dev.h" |
14 | | -#if (CONFIG_U_FLASH_CORE > 0) |
15 | | -#include <aos/mtd.h> |
16 | | -#endif |
| 9 | +#include <aos/flashpart.h> |
| 10 | +#include <aos/hal/flash.h> |
17 | 11 |
|
18 | | -static int32_t g_kv_flash_fd = -1; |
| 12 | +static aos_flashpart_ref_t kv_flashpart_ref = AOS_DEV_REF_INIT_VAL; |
19 | 13 |
|
20 | | -static int32_t kv_flash_fd_open(void) |
| 14 | +static aos_status_t kv_flash_open(void) |
21 | 15 | { |
22 | | - int fd; |
23 | | - char dev_str[16] = {0}; |
24 | | - if(g_kv_flash_fd >= 0){ |
25 | | - return g_kv_flash_fd; |
26 | | - } |
27 | | - |
28 | | -#if (CONFIG_U_FLASH_CORE > 0) |
29 | | - snprintf(dev_str,16-1,"/dev/mtdblock%d",KV_PARTITION); |
30 | | -#else |
31 | | - snprintf(dev_str,16-1,"/dev/flash%d",KV_PARTITION); |
32 | | -#endif |
33 | | - |
34 | | - fd = open(dev_str, 0); |
35 | | - if(fd < 0){ |
36 | | - return fd; |
37 | | - } |
38 | | - g_kv_flash_fd = fd; |
39 | | - |
40 | | - return g_kv_flash_fd; |
| 16 | + return aos_flashpart_get(&kv_flashpart_ref, KV_PARTITION); |
41 | 17 | } |
42 | 18 |
|
43 | 19 | int32_t kv_flash_read(uint32_t offset, void *buf, uint32_t nbytes) |
44 | 20 | { |
45 | | - int32_t ret; |
46 | | - int fd; |
47 | | - if(kv_flash_fd_open() < 0){ |
48 | | - return -1; |
49 | | - } |
50 | | - fd = g_kv_flash_fd; |
51 | | - ret = lseek(fd, offset, SEEK_SET); |
52 | | - if(ret < 0){ |
53 | | - return ret; |
54 | | - } |
55 | | - ret = read(fd, buf, nbytes); |
56 | | - if(ret > 0){ |
57 | | - return 0; |
58 | | - }else{ |
| 21 | + aos_status_t r; |
| 22 | + |
| 23 | + if (!aos_dev_ref_is_valid(&kv_flashpart_ref) && kv_flash_open()) |
59 | 24 | return -1; |
60 | | - } |
| 25 | + |
| 26 | + r = aos_flashpart_read(&kv_flashpart_ref, offset, buf, nbytes); |
| 27 | + |
| 28 | + return (r >= 0 && r < AOS_FLASH_ECC_ERROR) ? 0 : -1; |
61 | 29 | } |
62 | 30 |
|
63 | 31 | int32_t kv_flash_write(uint32_t offset, void *buf, uint32_t nbytes) |
64 | 32 | { |
65 | | - int32_t ret; |
66 | | - int fd; |
67 | | - if(kv_flash_fd_open() < 0){ |
| 33 | + if (!aos_dev_ref_is_valid(&kv_flashpart_ref) && kv_flash_open()) |
68 | 34 | return -1; |
69 | | - } |
70 | | - fd = g_kv_flash_fd; |
71 | | - ret = lseek(fd, offset, SEEK_SET); |
72 | | - if(ret < 0){ |
73 | | - return ret; |
74 | | - } |
75 | | - ret = write(fd, buf, nbytes); |
76 | | - if(ret > 0){ |
77 | | - return 0; |
78 | | - }else{ |
79 | | - return -1; |
80 | | - } |
| 35 | + |
| 36 | + return aos_flashpart_write(&kv_flashpart_ref, offset, buf, nbytes) ? -1 : 0; |
81 | 37 | } |
82 | 38 |
|
83 | 39 | int32_t kv_flash_erase(uint32_t offset, uint32_t size) |
84 | 40 | { |
85 | | - int32_t ret; |
86 | | - int fd; |
87 | | -#if (CONFIG_U_FLASH_CORE > 0) |
88 | | - struct mtd_erase_info erase_arg; |
89 | | -#endif |
90 | | - |
91 | | - if(kv_flash_fd_open() < 0){ |
| 41 | + if (!aos_dev_ref_is_valid(&kv_flashpart_ref) && kv_flash_open()) |
92 | 42 | return -1; |
93 | | - } |
94 | | - fd = g_kv_flash_fd; |
95 | | - ret = lseek(fd, offset, SEEK_SET); |
96 | | - if(ret < 0){ |
97 | | - return ret; |
98 | | - } |
99 | | - |
100 | | -#if (CONFIG_U_FLASH_CORE > 0) |
101 | | - erase_arg.offset = offset; |
102 | | - erase_arg.length = size; |
103 | | - ret = ioctl(fd, IOC_MTD_ERASE, &erase_arg); |
104 | | -#else |
105 | | - ret = ioctl(fd, IOC_FLASH_ERASE_FLASH, size); |
106 | | -#endif |
107 | 43 |
|
108 | | - return ret; |
| 44 | + return aos_flashpart_erase(&kv_flashpart_ref, offset, size) ? -1 : 0; |
109 | 45 | } |
110 | 46 |
|
111 | 47 | void *kv_lock_create(void) |
|
0 commit comments