b.liu | 4d51b68 | 2024-04-07 16:31:09 +0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <unistd.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <errno.h> |
| 5 | #include <string.h> |
| 6 | #include <sys/types.h> |
| 7 | #include <sys/stat.h> |
| 8 | #include <fcntl.h> |
b.liu | f678f99 | 2024-05-08 15:23:10 +0800 | [diff] [blame] | 9 | #include "mbtk_type.h" |
b.liu | 472cfaf | 2024-12-19 19:08:19 +0800 | [diff] [blame] | 10 | |
b.liu | 4d51b68 | 2024-04-07 16:31:09 +0800 | [diff] [blame] | 11 | #include "mbtk_device.h" |
b.liu | 4d51b68 | 2024-04-07 16:31:09 +0800 | [diff] [blame] | 12 | |
| 13 | /* |
| 14 | * revision_out start from 0x1000. |
| 15 | */ |
| 16 | #define REVISION_OUT_ADDR 0x1000 |
| 17 | #define DEV_INFO_FILE_NAME "dev_info.bin" |
| 18 | |
| 19 | static void help() |
| 20 | { |
| 21 | printf("ota_update -f [ota_bin] -v [new_revision_dir]\n"); |
| 22 | } |
| 23 | |
| 24 | /* |
| 25 | * |
| 26 | * ota_update -f [ota_bin] -v [new_revision_dir] |
| 27 | * |
| 28 | */ |
| 29 | int main(int argc, char *argv[]) |
| 30 | { |
| 31 | int ch; |
| 32 | char ota_bin[128] = {0}; |
| 33 | char dev_info_file[128] = {0}; |
| 34 | while((ch = getopt(argc, argv, "f:v:"))!= -1) |
| 35 | { |
| 36 | switch(ch) |
| 37 | { |
| 38 | case 'f': |
| 39 | if(strlen(optarg) > 0) |
| 40 | memcpy(ota_bin, optarg, strlen(optarg)); |
| 41 | break; |
| 42 | case 'v': |
| 43 | if(strlen(optarg) > 0) |
| 44 | memcpy(dev_info_file, optarg, strlen(optarg)); |
| 45 | break; |
| 46 | default: |
| 47 | help(); |
| 48 | return -1; |
| 49 | } |
| 50 | } |
| 51 | if(strlen(ota_bin) == 0 || strlen(dev_info_file) == 0) |
| 52 | { |
| 53 | help(); |
| 54 | return -1; |
| 55 | } |
| 56 | |
| 57 | printf("Ota Bin:%s, Revision Dir:%s\n", ota_bin, dev_info_file); |
| 58 | |
| 59 | sprintf(dev_info_file + strlen(dev_info_file), "/%s", DEV_INFO_FILE_NAME); |
| 60 | |
| 61 | if(access(ota_bin, F_OK)) |
| 62 | { |
| 63 | printf("%s not exist.", ota_bin); |
| 64 | return -1; |
| 65 | } |
| 66 | |
| 67 | if(access(dev_info_file, F_OK)) |
| 68 | { |
| 69 | printf("%s not exist.", dev_info_file); |
| 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | int fd = open(dev_info_file, O_RDONLY); |
| 74 | if(fd < 0) |
| 75 | { |
| 76 | printf("Open(%s) fail:%d\n", dev_info_file, errno); |
| 77 | return -1; |
| 78 | } |
| 79 | mbtk_device_info_header_t info_header; |
| 80 | memset(&info_header, 0, sizeof(mbtk_device_info_header_t)); |
| 81 | if(read(fd, &info_header, sizeof(mbtk_device_info_header_t)) != sizeof(mbtk_device_info_header_t)) { |
| 82 | printf("Read mbtk_device_info_header_t fail:%d\n", errno); |
| 83 | goto fail; |
| 84 | } |
| 85 | |
| 86 | if(lseek(fd, info_header.item_header[MBTK_DEVICE_INFO_ITEM_BASIC].addr, SEEK_SET) < 0) |
| 87 | { |
| 88 | printf("seek failed\n"); |
| 89 | goto fail; |
| 90 | } |
| 91 | |
| 92 | mbtk_device_info_basic_t info_basic; |
| 93 | memset(&info_basic, 0, sizeof(mbtk_device_info_basic_t)); |
| 94 | if(read(fd, &info_basic, sizeof(mbtk_device_info_basic_t)) != sizeof(mbtk_device_info_basic_t)) { |
| 95 | printf("Read mbtk_device_info_basic_t fail:%d\n", errno); |
| 96 | goto fail; |
| 97 | } |
| 98 | |
| 99 | printf("New Revision:%s\n", info_basic.revision_out); |
| 100 | |
| 101 | close(fd); |
| 102 | fd = open(ota_bin, O_WRONLY, 0644); |
| 103 | if(fd < 0) |
| 104 | { |
| 105 | printf("Open(%s) fail:%d\n", ota_bin, errno); |
| 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | if(lseek(fd, REVISION_OUT_ADDR, SEEK_SET) < 0) |
| 110 | { |
| 111 | printf("seek failed\n"); |
| 112 | goto fail; |
| 113 | } |
| 114 | |
| 115 | if(write(fd, info_basic.revision_out, strlen(info_basic.revision_out)) != strlen(info_basic.revision_out)) { |
| 116 | printf("Write revision failed\n"); |
| 117 | goto fail; |
| 118 | } |
| 119 | |
| 120 | printf("%s revision update to:%s\n", ota_bin, info_basic.revision_out); |
| 121 | close(fd); |
| 122 | return 0; |
| 123 | fail: |
| 124 | close(fd); |
| 125 | return -1; |
| 126 | } |
| 127 | |