mkfimage.c 2.1 KB

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