-
Notifications
You must be signed in to change notification settings - Fork 2
/
pack_pos.c
108 lines (88 loc) · 2.86 KB
/
pack_pos.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
103
104
105
106
107
108
/*
* pack.c
*
* Copyright 2012 Emilio López <[email protected]>
*
* 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 <string.h>
#ifdef __APPLE__
#include <libkern/OSByteOrder.h>
#define htobe16(x) OSSwapHostToBigInt16(x)
#define htole16(x) OSSwapHostToLittleInt16(x)
#define be16toh(x) OSSwapBigToHostInt16(x)
#define le16toh(x) OSSwapLittleToHostInt16(x)
#define htobe32(x) OSSwapHostToBigInt32(x)
#define htole32(x) OSSwapHostToLittleInt32(x)
#define be32toh(x) OSSwapBigToHostInt32(x)
#define le32toh(x) OSSwapLittleToHostInt32(x)
#define htobe64(x) OSSwapHostToBigInt64(x)
#define htole64(x) OSSwapHostToLittleInt64(x)
#define be64toh(x) OSSwapBigToHostInt64(x)
#define le64toh(x) OSSwapLittleToHostInt64(x)
#include <sys/types.h>
#else
#include <endian.h>
#endif /* __APPLE__ */
#include "bootheader.h"
#define ERROR(...) do { fprintf(stderr, __VA_ARGS__); return 1; } while(0)
uint8_t calculate_checksum(struct bootheader * hdr) {
uint8_t sum = hdr->xor;
uint8_t *data = (uint8_t *) hdr;
int i;
for (i = 0; i < HEADER_SIZE; i++) {
sum ^= data[i];
}
return sum;
}
int main(int argc, char *argv[])
{
char *origin;
char *output;
FILE *forigin;
FILE *foutput;
char buf[BUFSIZ];
size_t size;
struct bootheader *file;
if (argc != 3)
ERROR("Usage: %s <valid image> <output>\n", argv[0]);
origin = argv[1];
output = argv[2];
forigin = fopen(origin, "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");
file->id[28] = 0x32;
file->xor = calculate_checksum(file);
if (fwrite(file, sizeof(struct bootheader), 1, foutput) != 1)
ERROR("ERROR writing image\n");
while ((size = fread(buf, 1, BUFSIZ, forigin))) {
fwrite(buf, 1, size, foutput);
}
return 0;
}