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