├── README.md ├── arm └── bootimg-id └── bootimg-id.c /README.md: -------------------------------------------------------------------------------- 1 | Reads/writes the ID of a boot image 2 | 3 | License: whatever 4 | 5 | Also see: http://androidxref.com/6.0.1_r10/xref/system/core/mkbootimg/bootimg.h 6 | 7 | Example usage on an HTC10: 8 | 9 | ``` 10 | bootimg-id /dev/block/platform/soc/*/by-name/recovery $(getprop.ro.expect.recovery_id) 11 | ``` 12 | 13 | -------------------------------------------------------------------------------- /arm/bootimg-id: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chainfire/bootimg-id/3dc81921454d9d65faec2d3ddb168b5be6e18f9e/arm/bootimg-id -------------------------------------------------------------------------------- /bootimg-id.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define BLOCK_SIZE 4096 5 | #define ID_INDEX 576 6 | #define ID_LENGTH 32 7 | 8 | int read_block(char* blockdev, unsigned char* buffer) { 9 | int fd = open(blockdev, O_RDONLY); 10 | if (fd <= 0) { 11 | fprintf(stderr, "File open failure\n"); 12 | return 1; 13 | } 14 | 15 | int ret = 0; 16 | 17 | int r = read(fd, buffer, BLOCK_SIZE); 18 | if (r != BLOCK_SIZE) { 19 | fprintf(stderr, "Block read failure\n"); 20 | ret = 1; 21 | goto cleanup; 22 | } 23 | 24 | if (strncmp((char*)buffer, "ANDROID!", 8)) { 25 | fprintf(stderr, "Header magic mismatch"); 26 | ret = 1; 27 | goto cleanup; 28 | } 29 | 30 | cleanup: 31 | close(fd); 32 | return ret; 33 | } 34 | 35 | int write_block(char* blockdev, unsigned char* buffer) { 36 | int fd = open(blockdev, O_RDWR); 37 | if (fd <= 0) { 38 | fprintf(stderr, "File open failure\n"); 39 | return 1; 40 | } 41 | 42 | int ret = 0; 43 | 44 | int w = write(fd, buffer, BLOCK_SIZE); 45 | if (w != BLOCK_SIZE) { 46 | fprintf(stderr, "Block write failure\n"); 47 | ret = 1; 48 | goto cleanup; 49 | } 50 | 51 | cleanup: 52 | close(fd); 53 | return ret; 54 | } 55 | 56 | void print_id(unsigned char* buffer, int index) { 57 | printf("0x"); 58 | int i; 59 | for (i = 0; i < ID_LENGTH; i++) { 60 | printf("%02x", buffer[index + i]); 61 | } 62 | printf("\n"); 63 | } 64 | 65 | int main(int argc, char *argv[]) { 66 | fprintf(stderr, "bootimg-id by Chainfire\n\n"); 67 | if ((argc == 2) || (argc == 3)) { 68 | unsigned char buffer[BLOCK_SIZE]; 69 | if (read_block(argv[1], buffer)) return 1; 70 | print_id(buffer, ID_INDEX); 71 | 72 | if (argc == 2) return 0; 73 | 74 | char* parse = argv[2]; 75 | if (!strncmp(parse, "0x", 2)) parse = &parse[2]; 76 | if (strlen(parse) != ID_LENGTH * 2) { 77 | fprintf(stderr, "id length mismatch, expected %d, got %d (excluding 0x)\n", ID_LENGTH, strlen(parse)); 78 | } 79 | 80 | unsigned char id[ID_LENGTH]; 81 | char hex[3]; 82 | int i; 83 | for (i = 0; i < ID_LENGTH; i++) { 84 | memcpy(hex, &parse[i * 2], 2); 85 | id[i] = strtol(hex, NULL, 16); 86 | } 87 | print_id(id, 0); 88 | 89 | memcpy(&buffer[ID_INDEX], id, ID_LENGTH); 90 | 91 | if (write_block(argv[1], buffer)) return 1; 92 | 93 | return 0; 94 | } else { 95 | fprintf(stderr, "bootimg-id blockdev # get\n"); 96 | fprintf(stderr, "bootimg-id blockdev id # set\n"); 97 | return 0; 98 | } 99 | } --------------------------------------------------------------------------------