Guest User

Untitled

a guest
Nov 25th, 2012
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.32 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdint.h>
  4. #include <stdlib.h>
  5. #include <sys/stat.h>
  6. #include <endian.h>
  7.  
  8. #include "bootheader.h"
  9.  
  10. #define ERROR(...) do { fprintf(stderr, __VA_ARGS__); return 1; } while(0)
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14.     char *origin;
  15.     char *bzImage;
  16.     char *ramdisk;
  17.     char *output;
  18.     FILE *forigin;
  19.     FILE *foutput;
  20.     FILE *fbzImage;
  21.     FILE *framdisk;
  22.     struct stat st;
  23.     uint32_t tmp;
  24.     char buf[BUFSIZ];
  25.     size_t size;
  26.     struct bootheader *file;
  27.  
  28.     if (argc != 5) {
  29.         ERROR("Usage: %s <valid image> <bzImage> <ramdisk> <output>\n", argv[0]);
  30.     }
  31.  
  32.     origin = argv[1];
  33.     bzImage = argv[2];
  34.     ramdisk = argv[3];
  35.     output = argv[4];
  36.  
  37.     forigin = fopen(origin, "rb");
  38.     fbzImage = fopen(bzImage, "rb");
  39.     framdisk = fopen(ramdisk, "rb");
  40.     foutput = fopen(output, "wb");
  41.     if (!forigin || !foutput)
  42.         ERROR("ERROR: failed to open origin or output image\n");
  43.  
  44.     /* Allocate memory and copy bootstub to it */
  45.     file = malloc(sizeof(struct bootheader));
  46.     if (file == NULL)
  47.         ERROR("ERROR allocating memory\n");
  48.  
  49.     if (fread(file, sizeof(struct bootheader), 1, forigin) != 1)
  50.         ERROR("ERROR reading bootstub\n");
  51.  
  52.     /* Figure out the bzImage size and set it */
  53.     if (stat(bzImage, &st) == 0) {
  54.         tmp = st.st_size;
  55.         file->bzImageSize = htole32(tmp);
  56.     } else
  57.         ERROR("ERROR reading bzImage size\n");
  58.  
  59.     /* Figure out the ramdisk size and set it */
  60.     if (stat(ramdisk, &st) == 0) {
  61.         tmp = st.st_size;
  62.         file->initrdSize = htole32(tmp);
  63.     } else
  64.         ERROR("ERROR reading ramdisk\n");
  65.  
  66.     uint32_t usefulSize = sizeof(struct bootheader) + file->bzImageSize + file->initrdSize;
  67.     uint32_t placeHolderSize = 0;
  68.     if (usefulSize % 512) {
  69.         placeHolderSize = ((usefulSize / 512) + 1) * 512 - usefulSize;
  70.     }
  71.  
  72.     file->bootSector.sectors = ((usefulSize + placeHolderSize) / 512) - 1;
  73.  
  74.     /* Write the patched bootstub to the new image */
  75.     if (fwrite(file, sizeof(struct bootheader), 1, foutput) != 1)
  76.         ERROR("ERROR writing image\n");
  77.  
  78.     /* Then copy the new bzImage */
  79.     while ((size = fread(buf, 1, BUFSIZ, fbzImage))) {
  80.         fwrite(buf, 1, size, foutput);
  81.     }
  82.  
  83.     /* And finally copy the ramdisk */
  84.     while ((size = fread(buf, 1, BUFSIZ, framdisk))) {
  85.         fwrite(buf, 1, size, foutput);
  86.     }
  87.  
  88.     char placeHolder[] = {"\xFF"};
  89.     for (uint32_t i = 0; i < placeHolderSize; i++) {
  90.         fwrite(placeHolder, 1, 1, foutput);
  91.     }
  92.  
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment