| 1 | #include <stdio.h> |
| 2 | #include <sys/types.h> |
| 3 | #include <sys/stat.h> |
| 4 | #include <unistd.h> |
| 5 | #include <string.h> |
| 6 | #include <stdlib.h> |
| 7 | |
| 8 | int main(int argc, char **argv){ |
| 9 | unsigned char *buffer = malloc(64 * 1024); |
| 10 | struct stat s; |
| 11 | unsigned int size_vmlinux = 0, real_size_vmlinux = 0; |
| 12 | const unsigned char *magic_str = "ACME_PART_MAGIC"; |
| 13 | unsigned int loop; |
| 14 | unsigned char *magic; |
| 15 | |
| 16 | if(argc != 3){ |
| 17 | printf("%s in out\n", argv[0]); |
| 18 | return 1; |
| 19 | } |
| 20 | |
| 21 | printf("Generating image\n"); |
| 22 | |
| 23 | FILE *vmlinux = fopen(argv[1], "r"); |
| 24 | FILE *vmlinux_out = fopen(argv[2], "w"); |
| 25 | if((!vmlinux) || (!vmlinux_out)){ |
| 26 | printf("Error opening a file\n"); |
| 27 | return 1; |
| 28 | } |
| 29 | |
| 30 | stat(argv[1], &s); |
| 31 | size_vmlinux = s.st_size; |
| 32 | real_size_vmlinux = (size_vmlinux & 0xffff0000) + 0x10000; |
| 33 | |
| 34 | printf("vmlinux = 0x%.08X / 0x%.08X\n", size_vmlinux, real_size_vmlinux); |
| 35 | |
| 36 | unsigned int t = fread(buffer, 1, 64 * 1024, vmlinux); |
| 37 | for(loop = 0; loop < (64 * 1024) - sizeof(magic_str); loop++){ |
| 38 | if(buffer[loop] == magic_str[0]){ |
| 39 | if((magic = strstr(&buffer[loop], magic_str))){ |
| 40 | printf("Magic at 0x%.08X %p %p\n", magic - buffer, magic, buffer); |
| 41 | printf("Found Magic %X%X%X%X\n", |
| 42 | buffer[loop + strlen(magic_str)], |
| 43 | buffer[loop + strlen(magic_str) + 2], |
| 44 | buffer[loop + strlen(magic_str) + 1], |
| 45 | buffer[loop + strlen(magic_str) + 3]); |
| 46 | |
| 47 | buffer[loop + strlen(magic_str)] = real_size_vmlinux >> 24; |
| 48 | buffer[loop + strlen(magic_str) + 2] = (real_size_vmlinux >> 16) & 0xff; |
| 49 | buffer[loop + strlen(magic_str) + 1] = (real_size_vmlinux >> 8) & 0xff; |
| 50 | buffer[loop + strlen(magic_str) + 3] = (real_size_vmlinux) & 0xff; |
| 51 | |
| 52 | printf("Replaced with %.02X%.02X%.02X%.02X\n", |
| 53 | buffer[loop + strlen(magic_str)], |
| 54 | buffer[loop + strlen(magic_str) + 2], |
| 55 | buffer[loop + strlen(magic_str) + 1], |
| 56 | buffer[loop + strlen(magic_str) + 3]); |
| 57 | |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | fwrite(buffer, 1, 64 * 1024, vmlinux_out); |
| 63 | real_size_vmlinux -= 64 * 1024; |
| 64 | do { |
| 65 | real_size_vmlinux -= 64 * 1024; |
| 66 | memset(buffer, 0, 64 * 1024); |
| 67 | fread(buffer, 1, 64 * 1024, vmlinux); |
| 68 | fwrite(buffer, 1, 64 * 1024, vmlinux_out); |
| 69 | } while (real_size_vmlinux); |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |