├── readme.md ├── src ├── misc.h ├── main.cpp └── misc.cpp └── LICENSE /readme.md: -------------------------------------------------------------------------------- 1 | # miscEdit 2 | 3 | 用于编辑词典笔的`Misc`分区内容的工具. 4 | 5 | ## 适配 6 | 7 | |型号|状态| 8 | |---|---| 9 | |ydp021|支持| 10 | |ydp022|支持| 11 | 12 | ## Misc结构 13 | 14 | |偏移|名称|内容|大小| 15 | |---|---|---|---| 16 | |0x800|A/B slot data|ab槽的元数据|32| 17 | |0x1800|Wipe command|清除`userdata`分区的命令|20| 18 | |0x4000|Bootloader message|`Recovery`和`Bootloader`之间的共享数据|2048| 19 | |0x100000|Update info|ab槽的版本信息和更新信息|512| 20 | 21 | ## Misc作用 22 | 23 | - bootloader启动a槽或b槽的依据 24 | - 有ab各槽的版本信息 25 | - 决定下次启动是否清除`userdata`分区 26 | - ... 27 | 28 | ## 使用方法 29 | 30 | ``` 31 | Usage: miscedit [option] 32 | Option: 33 | -f (miscFile) specify the file to edit. if not specify it will use "/dev/block/by-name/misc". 34 | -r read all info from misc. 35 | -w (name)=(value) write value to property not for solt. 36 | -ws (slot),(name)=(value) write value to property for slot. 37 | --no-crc32 no update crc32 value after modify misc. 38 | Example: 39 | miscedit -f ./misc.bin -ws 2,priority=14 40 | miscedit -w update.info=hello\ world! 41 | miscedit --no-crc32 -w crc32=112233ff 42 | miscedit -w update.info=test -ws 0,priority=15 -ws 0,tries_remaining=3 43 | ``` 44 | 45 | ## 构建 46 | 47 | 环境要求: `linux`, 架构不限(你想在笔上使用则是`aarch64`) 48 | 49 | ```bash 50 | sudo apt update 51 | sudo apt install gcc g++ -y 52 | chmod +x ./build.sh 53 | ./build.sh 54 | ``` -------------------------------------------------------------------------------- /src/misc.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | /* == Partitions == */ 4 | 5 | // A/B slot data 6 | #define MISC_OFFSET 0x800 7 | 8 | // Update info 9 | #define MISC_UPDATE_INFO_OFFSET 0x100000 10 | #define MISC_UPDATE_INFO_LENGTH 512 11 | 12 | // Bootloader message 13 | #define MISC_BOOTLOADER_MESSAGE_OFFSET 0x4000 14 | #define MISC_BOOTLOADER_MESSAGE_LENGTH 2048 15 | 16 | // Wipe command 17 | #define MISC_WIPE_CMDLINE_OFFSET 0x1800 18 | #define MISC_WIPE_CMDLINE_LENGTH 20 19 | 20 | 21 | /* == A/B slot struct definitions == */ 22 | 23 | // Magic for the A/B struct when serialized. 24 | #define AVB_AB_MAGIC "\0AB0" 25 | #define AVB_AB_MAGIC_LENGTH 4 26 | 27 | // Versioning for the on-disk A/B metadata - keep in sync with avbtool. 28 | #define AVB_AB_MAJOR_VERSION 1 29 | #define AVB_AB_MINOR_VERSION 0 30 | 31 | 32 | /* == Other == */ 33 | 34 | // Path of default misc file to use 35 | #define MISC_DEFAULT "/dev/block/by-name/misc" 36 | 37 | 38 | 39 | struct AvbABSlotData { 40 | /* Slot priority. Valid values range from 0 to AVB_AB_MAX_PRIORITY, 41 | * both inclusive with 1 being the lowest and AVB_AB_MAX_PRIORITY 42 | * being the highest. The special value 0 is used to indicate the 43 | * slot is unbootable. 44 | */ 45 | unsigned char priority; 46 | 47 | /* Number of times left attempting to boot this slot ranging from 0 48 | * to AVB_AB_MAX_TRIES_REMAINING. 49 | */ 50 | unsigned char tries_remaining; 51 | 52 | /* Non-zero if this slot has booted successfully, 0 otherwise. */ 53 | unsigned char successful_boot; 54 | 55 | /* Reserved for future use. */ 56 | unsigned char reserved[1]; 57 | }; 58 | 59 | /* Struct used for recording A/B metadata. 60 | * 61 | * When serialized, data is stored in network byte-order. 62 | */ 63 | struct AvbABData { 64 | /* Magic number used for identification - see AVB_AB_MAGIC. */ 65 | unsigned char magic[AVB_AB_MAGIC_LENGTH]; 66 | 67 | /* Version of on-disk struct - see AVB_AB_{MAJOR,MINOR}_VERSION. */ 68 | unsigned char version_major; //AVB_AB_MAJOR_VERSION 69 | unsigned char version_minor; //AVB_AB_MINOR_VERSION 70 | 71 | /* Padding to ensure |slots| field start eight bytes in. */ 72 | unsigned char reserved1[2]; 73 | 74 | /* Per-slot metadata. */ 75 | AvbABSlotData slots[2]; 76 | 77 | /* Reserved for future use. */ 78 | unsigned char last_boot; 79 | unsigned char reserved2[11]; 80 | 81 | /* CRC32 of all 28 bytes preceding this field. */ 82 | unsigned int crc32; 83 | }; 84 | 85 | 86 | /* Bootloader Message 87 | * 88 | * This structure describes the content of a block in flash 89 | * that is used for recovery and the bootloader to talk to 90 | * each other. 91 | * 92 | * The command field is updated by linux when it wants to 93 | * reboot into recovery or to update radio or bootloader firmware. 94 | * It is also updated by the bootloader when firmware update 95 | * is complete (to boot into recovery for any final cleanup) 96 | * 97 | * The status field is written by the bootloader after the 98 | * completion of an "update-radio" or "update-hboot" command. 99 | * 100 | * The recovery field is only written by linux and used 101 | * for the system to send a message to recovery or the 102 | * other way around. 103 | * 104 | * The systemFlag field is used for the system to send a message to recovery. 105 | */ 106 | struct bootloaderMessage_t { 107 | char command[32]; 108 | char status[32]; 109 | char recovery[768]; 110 | char needUpdate[4]; 111 | char systemFlag[252]; 112 | }; 113 | 114 | typedef struct updateInfo_t{ 115 | char magic[4]; 116 | char info[504]; 117 | unsigned int crc32; 118 | }updateInfo_t; 119 | 120 | 121 | typedef struct miscData_t{ 122 | AvbABData main; 123 | bootloaderMessage_t bootloaderMessage; 124 | updateInfo_t updateInfo; 125 | char wipeCmdline[MISC_WIPE_CMDLINE_LENGTH]; 126 | }miscData_t; 127 | 128 | 129 | int readMisc(struct miscData_t *data, const char *fileName); 130 | void displayMisc(struct miscData_t *data); 131 | int writeMisc(struct miscData_t *data, const char *fileName,bool updateCrc=true); 132 | -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "misc.h" 5 | 6 | const int version[3] = {1, 0, 1}; 7 | 8 | const char *helpMsg = "\ 9 | Usage: miscedit [option]\ 10 | Option:\ 11 | -f (miscFile) specify the file to edit. if not specify it will use \"/dev/block/by-name/misc\".\ 12 | -r read all info from misc.\ 13 | -w (name)=(value) write value to property not for solt.\ 14 | -ws (slot),(name)=(value) write value to property for slot.\ 15 | --no-crc32 no update crc32 value after modify misc.\ 16 | Example:\ 17 | miscedit -f ./misc.bin -ws 2,priority=14\ 18 | miscedit -w update.info=hello\\ world!\ 19 | miscedit --no-crc32 -w crc32=112233ff\ 20 | miscedit -w update.info=test -ws 0,priority=15 -ws 0,tries_remaining=3\ 21 | "; 22 | 23 | void printHelpMsg(bool exitAfterPrint = true) 24 | { 25 | printf("miscedit\nVersion %d.%d.%d", version[0], version[1], version[2]); 26 | printf(helpMsg); 27 | if (exitAfterPrint) 28 | exit(0); 29 | } 30 | 31 | bool handleHelp(int n, char *arg[]) 32 | { 33 | if (n == 1) 34 | return true; 35 | for (int i = 1; i < n; i++) 36 | { 37 | if (!strcmp(arg[i], "--help") || !strcmp(arg[i], "-h") || !strcmp(arg[i], "-?")) 38 | return true; 39 | } 40 | return false; 41 | } 42 | 43 | bool handleFile(const char **str, int n, char *arg[]) 44 | { 45 | for (int i = 1; i < n; i++) 46 | { 47 | if (!strcmp(arg[i], "-f") && i < n - 1) 48 | { 49 | *str = arg[i + 1]; 50 | return true; 51 | } 52 | } 53 | *str = MISC_DEFAULT; 54 | return false; 55 | } 56 | 57 | bool handleRead(miscData_t *data, int n, char *arg[]) 58 | { 59 | for (int i = 1; i < n; i++) 60 | { 61 | if (!strcmp(arg[i], "-r")) 62 | { 63 | displayMisc(data); 64 | return true; 65 | } 66 | } 67 | return false; 68 | } 69 | 70 | bool handleNoCrc(int n, char *arg[]) 71 | { 72 | for (int i = 1; i < n; i++) 73 | { 74 | if (!strcmp(arg[i], "--no-crc32")) 75 | return true; 76 | } 77 | return false; 78 | } 79 | 80 | void cutStr(char *str, char **left, char **right, const char *spliter) 81 | { 82 | int len = 0; 83 | for (int i = 0; i < strlen(str); i++) 84 | { 85 | len++; 86 | // printf("%s %s\n",((char*)(str+i)),spliter); 87 | if (*((char *)(str + i)) == *((char *)spliter)) 88 | break; 89 | } 90 | char *out = (char *)malloc(len); 91 | if (out == 0) 92 | throw "malloc failed."; 93 | memset(out, 0, len); 94 | memcpy(out, str, len - 1); 95 | *left = out; 96 | *right = str + len; 97 | } 98 | 99 | char hexstr2str(char *input) 100 | { 101 | char out = 0; 102 | for (int i = 0; i < 2; i++) 103 | { 104 | if (*(input + i) == 0) 105 | break; 106 | out *= 16; 107 | if (*(input + i) >= '0' && *(input + i) <= '9') 108 | out += (*(input + i) - '0'); 109 | if (*(input + i) >= 'A' && *(input + i) <= 'F') 110 | out += (*(input + i) - 'A' + 10); 111 | if (*(input + i) >= 'a' && *(input + i) <= 'f') 112 | out += (*(input + i) - 'a' + 10); 113 | } 114 | return out; 115 | } 116 | 117 | char octstr2str(char *input) 118 | { 119 | char out = 0; 120 | long len = strlen(input); 121 | for (int i = 0; i < len; i++) 122 | { 123 | if (*(input + i) == 0) 124 | break; 125 | out *= 10; 126 | if (*(input + i) >= '0' && *(input + i) <= '9') 127 | out += (*(input + i) - '0'); 128 | } 129 | return out; 130 | } 131 | 132 | void doWrite(miscData_t *data, void *map[], int mapLen, char *name, char *value) 133 | { 134 | for (int i = 0; i < mapLen; i += 4) 135 | { 136 | if (!strcmp(name, ((char *)map[i]))) 137 | { 138 | memset(map[i + 1], 0, (unsigned long)map[i + 2]); 139 | switch ((long)map[i + 3]) 140 | { 141 | case 0: 142 | { 143 | memcpy(map[i + 1], value, (strlen(value) < (unsigned long)map[i + 2] ? strlen(value) : (unsigned long)map[i + 2])); 144 | break; 145 | } 146 | case 1: 147 | { 148 | long len = strlen(value); 149 | for (int ii = 0; ii < len; ii += 2) 150 | { 151 | if (ii / 2 == (long)map[i + 2]) 152 | break; 153 | *(char *)((long)map[i + 1] + ii / 2) = hexstr2str((char *)(value + ii)); 154 | } 155 | break; 156 | } 157 | case 2: 158 | { 159 | *(char *)((long)map[i + 1]) = octstr2str((char *)(value)); 160 | break; 161 | } 162 | } 163 | return; 164 | } 165 | } 166 | printf("Not match any property name: %s.\n", name); 167 | } 168 | 169 | void doWriteSlot(miscData_t *data, void *map[], int mapLen, char *name, char *value, int slot) 170 | { 171 | for (int i = 0; i < mapLen; i += 3) 172 | { 173 | if (!strcmp(((char *)map[i]), name)) 174 | { 175 | void *ptr = (void *)((long)(&(data->main.slots)) + sizeof(AvbABSlotData) * slot + (long)map[i + 1]); 176 | memset(ptr, 0, (unsigned long)map[i + 2]); 177 | *(char *)((long)ptr) = octstr2str((char *)(value)); 178 | return; 179 | } 180 | } 181 | printf("Not match any property name: slot[%d]->%s.\n", slot, name); 182 | } 183 | 184 | void handleWrite(miscData_t *data, int n, char *arg[]) 185 | { 186 | void *wMap[] = { 187 | (void *)"magic", &(data->main.magic), (void *)4, (void *)1, 188 | (void *)"version_major", &(data->main.version_major), (void *)1, (void *)2, 189 | (void *)"version_min", &(data->main.version_minor), (void *)1, (void *)2, 190 | (void *)"crc32", &(data->main.crc32), (void *)4, (void *)1, 191 | (void *)"last_boot", &(data->main.last_boot), (void *)1, (void *)2, 192 | (void *)"bootloader.command", &(data->bootloaderMessage.command), (void *)32, (void *)0, 193 | (void *)"bootloader.status", &(data->bootloaderMessage.status), (void *)32, (void *)0, 194 | (void *)"bootloader.recovery", &(data->bootloaderMessage.recovery), (void *)768, (void *)0, 195 | (void *)"bootloader.needUpdate", &(data->bootloaderMessage.needUpdate), (void *)4, (void *)0, 196 | (void *)"bootloader.systemFlag", &(data->bootloaderMessage.systemFlag), (void *)252, (void *)0, 197 | (void *)"cmdline", (data->wipeCmdline), (void *)MISC_WIPE_CMDLINE_LENGTH, (void *)0, 198 | (void *)"update.info", (data->updateInfo.info), (void *)504, (void *)0, 199 | (void *)"update.crc32", &(data->updateInfo.crc32), (void *)4, (void *)1 200 | }; 201 | void *wsMap[] = { 202 | (void *)"priority", (void *)((long)(&(data->main.slots[0].priority)) - (long)(&(data->main.slots))), (void *)1, 203 | (void *)"tries_remaining", (void *)((long)(&(data->main.slots[0].tries_remaining)) - (long)(&(data->main.slots))), (void *)1, 204 | (void *)"successful_boot", (void *)((long)(&(data->main.slots[0].successful_boot)) - (long)(&(data->main.slots))), (void *)1 205 | }; 206 | for (int i = 1; i < n; i++) 207 | { 208 | if (!strcmp(arg[i], "-w") && i < n - 1) 209 | { 210 | char *name = 0; 211 | char *value = 0; 212 | cutStr(arg[i + 1], &name, &value, "="); 213 | doWrite(data, wMap, sizeof(wMap) / sizeof(void *), name, value); 214 | free(name); 215 | i++; 216 | continue; 217 | } 218 | if (!strcmp(arg[i], "-ws") && i < n - 1) 219 | { 220 | char *name = 0; 221 | char *tmp = 0; 222 | char *value = 0; 223 | int slot = 0; 224 | cutStr(arg[i + 1], &tmp, &value, ","); 225 | if (*tmp != *"1" && *tmp != *"0") 226 | { 227 | printf("No such a slot: slot[%s].\n", tmp); 228 | continue; 229 | } 230 | slot = (*tmp == *"1"); 231 | free(tmp); 232 | tmp = 0; 233 | cutStr(value, &name, &value, "="); 234 | doWriteSlot(data, wsMap, sizeof(wsMap) / sizeof(void *), name, value, slot); 235 | free(name); 236 | i++; 237 | continue; 238 | } 239 | } 240 | } 241 | 242 | int main(int argc, char *argv[]) 243 | { 244 | 245 | if (handleHelp(argc, argv)) 246 | { 247 | printHelpMsg(true); 248 | } 249 | 250 | const char *fileName = 0; 251 | bool noCrc = false; 252 | 253 | handleFile(&fileName, argc, argv); 254 | if (handleFile(&fileName, argc, argv) && argc == 3) 255 | printHelpMsg(true); 256 | noCrc = handleNoCrc(argc, argv); 257 | 258 | miscData_t miscData; 259 | if (readMisc(&miscData, fileName) != 0) 260 | { 261 | printf("readMisc failed.\n"); 262 | return 1; 263 | } 264 | 265 | if (handleRead(&miscData, argc, argv)) 266 | return 0; 267 | 268 | handleWrite(&miscData, argc, argv); 269 | 270 | if (writeMisc(&miscData, fileName, !noCrc) != 0) 271 | { 272 | printf("writeMisc failed.\n"); 273 | return 1; 274 | } 275 | 276 | return 0; 277 | } -------------------------------------------------------------------------------- /src/misc.cpp: -------------------------------------------------------------------------------- 1 | #include "misc.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | static unsigned int iavb_crc32_tab[] = { 15 | 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 16 | 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 17 | 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, 18 | 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 19 | 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 20 | 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 21 | 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, 22 | 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 23 | 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 24 | 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 25 | 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, 26 | 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, 27 | 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 28 | 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 29 | 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, 30 | 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, 31 | 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 32 | 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 33 | 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 34 | 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, 35 | 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 36 | 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 37 | 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, 38 | 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 39 | 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 40 | 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 41 | 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, 42 | 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 43 | 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 44 | 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 45 | 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, 46 | 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, 47 | 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 48 | 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 49 | 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, 50 | 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, 51 | 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 52 | 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 53 | 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 54 | 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, 55 | 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 56 | 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 57 | 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d}; 58 | 59 | /* Converts a 32-bit unsigned integer from host to big-endian byte order. */ 60 | static unsigned int avb_htobe32(unsigned int in) 61 | { 62 | union 63 | { 64 | unsigned int word; 65 | unsigned char bytes[4]; 66 | } ret; 67 | ret.bytes[0] = (in >> 24) & 0xff; 68 | ret.bytes[1] = (in >> 16) & 0xff; 69 | ret.bytes[2] = (in >> 8) & 0xff; 70 | ret.bytes[3] = in & 0xff; 71 | return ret.word; 72 | } 73 | 74 | /* 75 | * A function that calculates the CRC-32 based on the table above is 76 | * given below for documentation purposes. An equivalent implementation 77 | * of this function that's actually used in the kernel can be found 78 | * in sys/libkern.h, where it can be inlined. 79 | */ 80 | static unsigned int iavb_crc32(unsigned int crc_in, const unsigned char *buf, int size) 81 | { 82 | const unsigned char *p = buf; 83 | unsigned int crc; 84 | 85 | crc = crc_in ^ ~0U; 86 | while (size--) 87 | crc = iavb_crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8); 88 | return crc ^ ~0U; 89 | } 90 | 91 | static unsigned int avb_crc32(const unsigned char *buf, size_t size) 92 | { 93 | return iavb_crc32(0, buf, size); 94 | } 95 | 96 | static void AvbABData_update_crc(struct AvbABData *data) 97 | { 98 | data->crc32 = avb_htobe32(avb_crc32((const unsigned char *)data, sizeof(struct AvbABData) - sizeof(unsigned int))); 99 | } 100 | 101 | static void updateUpdateInfoCrc(struct updateInfo_t *data) 102 | { 103 | data->crc32 = avb_htobe32(avb_crc32((const unsigned char *)data, sizeof(struct updateInfo_t) - sizeof(unsigned int))); 104 | } 105 | 106 | static void makeCrcDisplayTxt(char *str, unsigned int a, unsigned int b) 107 | { 108 | if (a == b) 109 | { 110 | sprintf(str, "%x (match)", avb_htobe32(a)); 111 | return; 112 | } 113 | sprintf(str, "%x (not match, %x)", avb_htobe32(a), avb_htobe32(b)); 114 | } 115 | 116 | void displayMisc(struct miscData_t *data) 117 | { 118 | char crcTxt[64]; 119 | char updateInfoCrcTxt[64]; 120 | memset(crcTxt, 0, 64); 121 | memset(updateInfoCrcTxt, 0, 64); 122 | makeCrcDisplayTxt(crcTxt, data->main.crc32, avb_htobe32(avb_crc32((const unsigned char *)&(data->main), sizeof(struct AvbABData) - sizeof(unsigned int)))); 123 | makeCrcDisplayTxt(updateInfoCrcTxt, data->updateInfo.crc32, avb_htobe32(avb_crc32((const unsigned char *)&(data->updateInfo), sizeof(struct updateInfo_t) - sizeof(unsigned int)))); 124 | 125 | printf("===============Misc=============\n"); 126 | printf(" magic = %x.\n", avb_htobe32(*((unsigned int *)(data->main.magic)))); 127 | printf(" version_major = %d.\n", data->main.version_major); 128 | printf(" version_min = %d.\n", data->main.version_minor); 129 | printf(" crc32 = %s.\n", crcTxt); 130 | printf("===============Boot=============\n"); 131 | printf(" last_boot = %d.\n", data->main.last_boot); 132 | printf(" bootloader.command = %s.\n", data->bootloaderMessage.command); 133 | printf(" bootloader.status = %s.\n", data->bootloaderMessage.status); 134 | printf(" bootloader.recovery = %s.\n", data->bootloaderMessage.recovery); 135 | printf(" bootloader.needUpdate = %s.\n", data->bootloaderMessage.needUpdate); 136 | printf(" bootloader.systemFlag = %s.\n", data->bootloaderMessage.systemFlag); 137 | printf("=============System A===========\n"); 138 | printf(" slot[0]->priority = %d.\n", data->main.slots[0].priority); 139 | printf(" slot[0]->tries_remaining = %d.\n", data->main.slots[0].tries_remaining); 140 | printf(" slot[0]->successful_boot = %d.\n", data->main.slots[0].successful_boot); 141 | printf("=============System B===========\n"); 142 | printf(" slot[1]->priority = %d.\n", data->main.slots[1].priority); 143 | printf(" slot[1]->tries_remaining = %d.\n", data->main.slots[1].tries_remaining); 144 | printf(" slot[1]->successful_boot = %d.\n", data->main.slots[1].successful_boot); 145 | printf("==============Update============\n"); 146 | printf(" update.info = %s.\n", data->updateInfo.info); 147 | printf(" update.crc32 = %s.\n", updateInfoCrcTxt); 148 | printf("===============Wipe=============\n"); 149 | printf(" cmdline = %s.\n", data->wipeCmdline); 150 | printf("================================\n"); 151 | } 152 | 153 | static int readMiscData(void *info, const char *fileName, long offset, long length) 154 | { 155 | memset(info, 0, length); 156 | 157 | int fd = open(fileName, O_RDONLY); 158 | if (fd < 0) 159 | { 160 | printf("open %s failed. %s\n", fileName, strerror(errno)); 161 | return -1; 162 | } 163 | 164 | if (lseek(fd, offset, SEEK_SET) == -1) 165 | { 166 | printf("lseek %s error. %s\n", fileName, strerror(errno)); 167 | close(fd); 168 | return -1; 169 | } 170 | 171 | if (read(fd, info, length) != length) 172 | { 173 | printf("read failed, %s\n", strerror(errno)); 174 | close(fd); 175 | return -1; 176 | } 177 | 178 | close(fd); 179 | return 0; 180 | } 181 | 182 | static int writeMiscData(void *info, const char *fileName, long offset, long length) 183 | { 184 | int fd = open(fileName, O_WRONLY); 185 | if (fd < 0) 186 | { 187 | printf("open %s failed.\n", fileName); 188 | return -1; 189 | } 190 | 191 | if (lseek(fd, offset, SEEK_SET) == -1) 192 | { 193 | printf("lseek %s error. %s\n", fileName, strerror(errno)); 194 | close(fd); 195 | return -1; 196 | } 197 | 198 | if (write(fd, info, length) != length) 199 | { 200 | printf("write failed, %s\n", strerror(errno)); 201 | close(fd); 202 | return -1; 203 | } 204 | sync(); 205 | close(fd); 206 | 207 | unsigned char *data_check = (unsigned char *)malloc(length); 208 | if (data_check == 0) 209 | { 210 | printf("write error: malloc while checking data\n"); 211 | return -1; 212 | } 213 | if (readMiscData(data_check, fileName, offset, length) == -1) 214 | { 215 | free(data_check); 216 | return -1; 217 | } 218 | if (memcmp(data_check, info, length) != 0) 219 | { 220 | printf("write error: memcmp \n"); 221 | free(data_check); 222 | return -1; 223 | } 224 | free(data_check); 225 | return 0; 226 | } 227 | 228 | int readMisc(struct miscData_t *data, const char *fileName) 229 | { 230 | memset(data, 0, sizeof(struct miscData_t)); 231 | if (readMiscData(&(data->main), fileName, MISC_OFFSET, sizeof(AvbABData)) != 0) 232 | { 233 | printf("readMiscMain failed.\n"); 234 | return -1; 235 | } 236 | if (readMiscData(&(data->updateInfo), fileName, MISC_UPDATE_INFO_OFFSET, sizeof(updateInfo_t)) != 0) 237 | { 238 | printf("readMiscUpdateInfo failed.\n"); 239 | return -1; 240 | } 241 | if (readMiscData(&(data->bootloaderMessage), fileName, MISC_BOOTLOADER_MESSAGE_OFFSET, sizeof(bootloaderMessage_t)) != 0) 242 | { 243 | printf("readMiscBootloaderMessage failed.\n"); 244 | return -1; 245 | } 246 | if (readMiscData(&(data->wipeCmdline), fileName, MISC_WIPE_CMDLINE_OFFSET, MISC_WIPE_CMDLINE_LENGTH) != 0) 247 | { 248 | printf("readMiscWipeCmdline failed.\n"); 249 | return -1; 250 | } 251 | return 0; 252 | } 253 | 254 | int writeMisc(struct miscData_t *data, const char *fileName, bool updateCrc) 255 | { 256 | if (updateCrc) 257 | { 258 | AvbABData_update_crc(&(data->main)); 259 | updateUpdateInfoCrc(&(data->updateInfo)); 260 | } 261 | if (writeMiscData(&(data->main), fileName, MISC_OFFSET, sizeof(AvbABData)) != 0) 262 | { 263 | printf("writeMiscMain failed.\n"); 264 | return -1; 265 | } 266 | if (writeMiscData(&(data->updateInfo), fileName, MISC_UPDATE_INFO_OFFSET, sizeof(updateInfo_t)) != 0) 267 | { 268 | printf("writeMiscUpdateInfo failed.\n"); 269 | return -1; 270 | } 271 | if (writeMiscData(&(data->bootloaderMessage), fileName, MISC_BOOTLOADER_MESSAGE_OFFSET, sizeof(bootloaderMessage_t)) != 0) 272 | { 273 | printf("writeMiscBootloaderMessage failed.\n"); 274 | return -1; 275 | } 276 | if (writeMiscData(&(data->wipeCmdline), fileName, MISC_WIPE_CMDLINE_OFFSET, MISC_WIPE_CMDLINE_LENGTH) != 0) 277 | { 278 | printf("writeMiscWipeCmdline failed.\n"); 279 | return -1; 280 | } 281 | return 0; 282 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------