b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 1 | /* |
| 2 | * mbtk_device_info.c |
| 3 | * |
| 4 | * MBTK device partition information utils. |
| 5 | * |
| 6 | */ |
| 7 | /****************************************************************************** |
| 8 | |
| 9 | EDIT HISTORY FOR FILE |
| 10 | |
| 11 | WHEN WHO WHAT,WHERE,WHY |
| 12 | -------- -------- ------------------------------------------------------- |
| 13 | 2024/2/27 LiuBin Initial version |
| 14 | |
| 15 | ******************************************************************************/ |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <unistd.h> |
| 19 | #include <errno.h> |
b.liu | bb5e768 | 2024-02-28 20:13:04 +0800 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <fcntl.h> |
b.liu | f678f99 | 2024-05-08 15:23:10 +0800 | [diff] [blame] | 23 | #include <sys/ioctl.h> |
| 24 | #include <mtd/mtd-user.h> |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 25 | |
b.liu | f678f99 | 2024-05-08 15:23:10 +0800 | [diff] [blame] | 26 | #include "mbtk_type.h" |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 27 | #include "mbtk_device.h" |
| 28 | #include "mbtk_log.h" |
| 29 | #include "mbtk_str.h" |
| 30 | #include "mbtk_mtd.h" |
| 31 | |
| 32 | /* |
b.liu | bb5e768 | 2024-02-28 20:13:04 +0800 | [diff] [blame] | 33 | * MBTK_DEVICE_INFO_ITEM_BASIC - mbtk_device_info_basic_t |
| 34 | * MBTK_DEVICE_INFO_ITEM_FOTA - mbtk_device_info_fota_t |
| 35 | * MBTK_DEVICE_INFO_ITEM_MODEM - mbtk_device_info_modem_t |
| 36 | * MBTK_DEVICE_INFO_ITEM_LOG - mbtk_device_info_log_t |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 37 | */ |
| 38 | int mbtk_dev_info_read(mbtk_device_info_item_enum item_type, void *item_ptr, int item_size) |
| 39 | { |
| 40 | int fd, len, i; |
| 41 | mbtk_device_info_header_t info_header; |
| 42 | memset(&info_header, 0, sizeof(mbtk_device_info_header_t)); |
| 43 | |
| 44 | switch(item_type) { |
| 45 | case MBTK_DEVICE_INFO_ITEM_BASIC: |
| 46 | { |
| 47 | if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_basic_t)) { |
| 48 | LOGE("ARG error:item-%d, item_size-%d", item_type, item_size); |
| 49 | return -1; |
| 50 | } |
| 51 | break; |
| 52 | } |
| 53 | case MBTK_DEVICE_INFO_ITEM_FOTA: |
| 54 | { |
| 55 | if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_fota_t)) { |
| 56 | LOGE("ARG error:item-%d, item_size-%d", item_type, item_size); |
| 57 | return -1; |
| 58 | } |
| 59 | break; |
| 60 | } |
| 61 | case MBTK_DEVICE_INFO_ITEM_MODEM: |
| 62 | { |
| 63 | if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_modem_t)) { |
| 64 | LOGE("ARG error:item-%d, item_size-%d", item_type, item_size); |
| 65 | return -1; |
| 66 | } |
| 67 | break; |
| 68 | } |
| 69 | case MBTK_DEVICE_INFO_ITEM_LOG: |
| 70 | { |
| 71 | if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_log_t)) { |
| 72 | LOGE("ARG error:item-%d, item_size-%d", item_type, item_size); |
| 73 | return -1; |
| 74 | } |
| 75 | break; |
| 76 | } |
| 77 | default: |
| 78 | { |
| 79 | LOGE("Item type[%d] error.", item_type); |
| 80 | return -1; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | mbtk_partition_info_t *partition_info = mbtk_partition_get(); |
| 85 | if(partition_info == NULL) { |
| 86 | LOGE("mbtk_partition_get() fail."); |
| 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | i = 0; |
| 91 | char dev[32] = {0}; |
| 92 | while(i < MBTK_PARTITION_NUM_MAX) { |
| 93 | if(partition_info[i].used && strcmp(partition_info[i].name, MBTK_DEVICE_INFO_PARTITION_NAME) == 0) { |
| 94 | snprintf(dev, 32, "/dev/%s", partition_info[i].dev); |
b.liu | bb5e768 | 2024-02-28 20:13:04 +0800 | [diff] [blame] | 95 | LOGD("%s -> %s", partition_info[i].name, dev); |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 96 | break; |
| 97 | } |
| 98 | i++; |
| 99 | } |
| 100 | if(strlen(dev) == 0) { |
| 101 | LOGE("DEV is null."); |
| 102 | return -1; |
| 103 | } |
| 104 | |
| 105 | fd = open(dev, O_RDONLY); |
| 106 | if (fd < 0) { |
| 107 | LOGE("Fatal error: can't open device_info %s\n", dev); |
| 108 | return -1; |
| 109 | } |
| 110 | |
| 111 | len = read(fd, &info_header, sizeof(mbtk_device_info_header_t)); |
| 112 | if (len != sizeof(mbtk_device_info_header_t)) { |
| 113 | LOGE("Fatal error: read %d bytes(expect %d)\n", len, sizeof(mbtk_device_info_header_t)); |
| 114 | close(fd); |
| 115 | goto fail; |
| 116 | } |
| 117 | |
| 118 | if(info_header.tag != MBTK_DEVICE_INFO_PARTITION_TAG) { |
| 119 | LOGE("TAG error : %08x", info_header.tag); |
| 120 | goto fail; |
| 121 | } |
| 122 | |
| 123 | if(info_header.version != MBTK_DEVICE_INFO_CURR_VERSION) { |
| 124 | LOGE("Version error : %d", info_header.version); |
| 125 | goto fail; |
| 126 | } |
| 127 | |
| 128 | LOGD("Item count:%d", info_header.item_count); |
| 129 | |
| 130 | if(info_header.item_header[item_type].addr == 0) { |
| 131 | LOGE("No found item : %d", item_type); |
| 132 | goto fail; |
| 133 | } |
| 134 | |
| 135 | lseek(fd, info_header.item_header[item_type].addr, SEEK_SET); |
| 136 | if (read(fd, item_ptr, item_size) != item_size) { |
| 137 | LOGE("Read fail:%d", errno); |
| 138 | goto fail; |
| 139 | } |
| 140 | |
| 141 | close(fd); |
| 142 | return 0; |
| 143 | |
| 144 | fail: |
| 145 | close(fd); |
| 146 | return -1; |
| 147 | } |
| 148 | |
b.liu | f678f99 | 2024-05-08 15:23:10 +0800 | [diff] [blame] | 149 | int mbtk_dev_info_write(mbtk_device_info_item_enum item_type, void *item_ptr, int item_size) |
| 150 | { |
| 151 | if(item_ptr == NULL) { |
| 152 | LOGE("ARG error."); |
| 153 | return -1; |
| 154 | } |
| 155 | |
| 156 | switch(item_type) { |
| 157 | case MBTK_DEVICE_INFO_ITEM_BASIC: |
| 158 | { |
| 159 | if(item_size != sizeof(mbtk_device_info_basic_t)) { |
| 160 | LOGE("item_size != sizeof(mbtk_device_info_basic_t)\n\r"); |
| 161 | return -1; |
| 162 | } |
| 163 | break; |
| 164 | } |
| 165 | case MBTK_DEVICE_INFO_ITEM_FOTA: |
| 166 | { |
| 167 | if(item_size != sizeof(mbtk_device_info_fota_t)) { |
| 168 | LOGE("item_size != sizeof(mbtk_device_info_fota_t)\n\r"); |
| 169 | return -1; |
| 170 | } |
| 171 | break; |
| 172 | } |
| 173 | case MBTK_DEVICE_INFO_ITEM_MODEM: |
| 174 | { |
| 175 | if(item_size != sizeof(mbtk_device_info_modem_t)) { |
| 176 | LOGE("item_size != sizeof(mbtk_device_info_modem_t)\n\r"); |
| 177 | return -1; |
| 178 | } |
| 179 | break; |
| 180 | } |
| 181 | case MBTK_DEVICE_INFO_ITEM_LOG: |
| 182 | { |
| 183 | if(item_size != sizeof(mbtk_device_info_log_t)) { |
| 184 | LOGE("item_size != sizeof(mbtk_device_info_log_t)\n\r"); |
| 185 | return -1; |
| 186 | } |
| 187 | break; |
| 188 | } |
| 189 | default: |
| 190 | { |
| 191 | LOGE("Item type[%d] error.\n\r", item_type); |
| 192 | return -1; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | mbtk_partition_info_t info; |
| 197 | memset(&info, 0x0, sizeof(mbtk_partition_info_t)); |
| 198 | if(mbtk_partition_get_by_name("device_info", &info)) { |
| 199 | LOGE("mbtk_partition_get_by_name() fail."); |
| 200 | return -1; |
| 201 | } |
| 202 | |
| 203 | LOGD("device_info name : %s, dev : %s, addr : %08x, size : %08x, erase_size : %08x", info.name, |
| 204 | info.dev, info.partition_start, info.partition_size, info.erase_size); |
| 205 | |
| 206 | if(info.erase_size <= 0 || info.partition_size <= 0) { |
| 207 | LOGE("partition info error."); |
| 208 | return -1; |
| 209 | } |
| 210 | |
| 211 | int fd = open(info.dev, O_RDWR); |
| 212 | if (fd < 0) { |
| 213 | LOGE("Fatal error: can't open device_info %s\n", info.dev); |
| 214 | return -1; |
| 215 | } |
| 216 | |
| 217 | char *mtd_buff = (char*)malloc(info.erase_size); |
| 218 | if(mtd_buff == NULL) { |
| 219 | LOGE("malloc() failed\n"); |
| 220 | return -1; |
| 221 | } |
| 222 | memset(mtd_buff, 0xFF, info.erase_size); |
| 223 | int len = read(fd, mtd_buff, info.erase_size); |
| 224 | if (len != info.erase_size) { |
| 225 | LOGE("Fatal error: read %d[%d] bytes(expect %d)\n", len, errno, info.erase_size); |
| 226 | goto fail; |
| 227 | } |
| 228 | |
| 229 | struct erase_info_user mtdEraseInfo; |
| 230 | if (lseek(fd, 0, SEEK_SET) < 0) |
| 231 | { |
| 232 | LOGE("seek failed\n"); |
| 233 | return -1; |
| 234 | } |
| 235 | |
| 236 | mtdEraseInfo.length = info.partition_size; |
| 237 | mtdEraseInfo.start = 0; |
| 238 | ioctl(fd, MEMUNLOCK, &mtdEraseInfo); |
| 239 | ioctl(fd, MEMERASE, &mtdEraseInfo); |
| 240 | |
| 241 | mbtk_device_info_header_t *info_header = (mbtk_device_info_header_t*)mtd_buff; |
| 242 | memcpy(mtd_buff + info_header->item_header[item_type].addr, item_ptr, item_size); |
| 243 | |
| 244 | lseek(fd, 0, SEEK_SET); |
| 245 | if (write(fd, mtd_buff, info.erase_size) != info.erase_size) { |
| 246 | LOGE("Write fail:%d", errno); |
| 247 | goto fail; |
| 248 | } |
| 249 | |
| 250 | if(mtd_buff) { |
| 251 | free(mtd_buff); |
| 252 | } |
| 253 | |
| 254 | close(fd); |
| 255 | return 0; |
| 256 | |
| 257 | fail: |
| 258 | close(fd); |
| 259 | return -1; |
| 260 | } |
| 261 | |
| 262 | |