Guest User

Untitled

a guest
Nov 18th, 2012
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.07 KB | None | 0 0
  1. /*
  2.  * pack.c
  3.  *
  4.  * Copyright 2012 Emilio López <[email protected]>
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19.  * MA 02110-1301, USA.
  20.  *
  21.  *
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <stdint.h>
  26. #include <stdlib.h>
  27. #include <sys/stat.h>
  28. #include <endian.h>
  29.  
  30. #include "bootheader.h"
  31.  
  32. #define ERROR(...) do { fprintf(stderr, __VA_ARGS__); return 1; } while(0)
  33.  
  34. int main(int argc, char *argv[])
  35. {
  36.     char *origin;
  37.     char *bzImage;
  38.     char *ramdisk;
  39.     char *output;
  40.     FILE *forigin;
  41.     FILE *foutput;
  42.     FILE *fbzImage;
  43.     FILE *framdisk;
  44.     struct stat st;
  45.     uint32_t tmp;
  46.     char buf[BUFSIZ];
  47.     size_t size;
  48.     struct bootheader *file;
  49.  
  50.     if (argc != 5) {
  51.         ERROR("Usage: %s <valid image> <bzImage> <ramdisk> <output>\n", argv[0]);
  52.     }
  53.  
  54.     origin = argv[1];
  55.     bzImage = argv[2];
  56.     ramdisk = argv[3];
  57.     output = argv[4];
  58.  
  59.     forigin = fopen(origin, "rb");
  60.     fbzImage = fopen(bzImage, "rb");
  61.     framdisk = fopen(ramdisk, "rb");
  62.     foutput = fopen(output, "wb");
  63.     if (!forigin || !foutput)
  64.         ERROR("ERROR: failed to open origin or output image\n");
  65.  
  66.     /* Allocate memory and copy bootstub to it */
  67.     file = calloc(sizeof(struct bootheader), sizeof(char));
  68.     if (file == NULL)
  69.         ERROR("ERROR allocating memory\n");
  70.  
  71.     if (fread(file, sizeof(struct bootheader), 1, forigin) != 1)
  72.         ERROR("ERROR reading bootstub\n");
  73.  
  74.     /* Figure out the bzImage size and set it */
  75.     if (stat(bzImage, &st) == 0) {
  76.         tmp = st.st_size;
  77.         file->bzImageSize = htole32(tmp);
  78.     } else
  79.         ERROR("ERROR reading bzImage size\n");
  80.  
  81.     /* Figure out the ramdisk size and set it */
  82.     if (stat(ramdisk, &st) == 0) {
  83.         tmp = st.st_size;
  84.         file->initrdSize = htole32(tmp);
  85.     } else
  86.         ERROR("ERROR reading ramdisk\n");
  87.  
  88.     /* Write the patched bootstub to the new image */
  89.     if (fwrite(file, sizeof(struct bootheader), 1, foutput) != 1)
  90.         ERROR("ERROR writing image\n");
  91.  
  92.     /* Then copy the new bzImage */
  93.     while ((size = fread(buf, 1, BUFSIZ, fbzImage))) {
  94.         fwrite(buf, 1, size, foutput);
  95.     }
  96.  
  97.     /* And finally copy the ramdisk */
  98.     while ((size = fread(buf, 1, BUFSIZ, framdisk))) {
  99.         fwrite(buf, 1, size, foutput);
  100.     }
  101.  
  102.     uint32_t usefulSize = sizeof(struct bootheader) + file->bzImageSize + file->initrdSize;
  103.     uint32_t placeHolderSize = 0;
  104.     if (usefulSize % 512) {
  105.         placeHolderSize = ((usefulSize / 512) + 1) * 512 - usefulSize;
  106.     }
  107.     char placeHolder[] = {"\xFF"};
  108.     for (uint32_t i = 0; i < placeHolderSize; i++) {
  109.         fwrite(placeHolder, 1, 1, foutput);
  110.     }
  111.  
  112.     return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment