aboutsummaryrefslogtreecommitdiffstats
path: root/package/utils/osafeloader/src/osafeloader.c
blob: 9ffa5fe3cf6749079cc30f35c5a179473d9e2428 (plain)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
 * osafeloader
 *
 * Copyright (C) 2016 Rafał Miłecki <zajec5@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <byteswap.h>
#include <endian.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "md5.h"

#if !defined(__BYTE_ORDER)
#error "Unknown byte order"
#endif

#if __BYTE_ORDER == __BIG_ENDIAN
#define cpu_to_be32(x)	(x)
#define be32_to_cpu(x)	(x)
#define cpu_to_be16(x)	(x)
#define be16_to_cpu(x)	(x)
#elif __BYTE_ORDER == __LITTLE_ENDIAN
#define cpu_to_be32(x)	bswap_32(x)
#define be32_to_cpu(x)	bswap_32(x)
#define cpu_to_be16(x)	bswap_16(x)
#define be16_to_cpu(x)	bswap_16(x)
#else
#error "Unsupported endianness"
#endif

struct safeloader_header {
	uint32_t imagesize;
	uint8_t md5[16];
} __attribute__ ((packed));

char *safeloader_path;
char *partition_name;
char *out_path;

static inline size_t osafeloader_min(size_t x, size_t y) {
	return x < y ? x : y;
}

static const uint8_t md5_salt[16] = {
	0x7a, 0x2b, 0x15, 0xed,
	0x9b, 0x98, 0x59, 0x6d,
	0xe5, 0x04, 0xab, 0x44,
	0xac, 0x2a, 0x9f, 0x4e,
};

/**************************************************
 * Info
 **************************************************/

static int osafeloader_info(int argc, char **argv) {
	FILE *safeloader;
	struct safeloader_header hdr;
	MD5_CTX ctx;
	size_t bytes, imagesize;
	uint8_t buf[1024];
	uint8_t md5[16];
	char name[32];
	int base, size, i;
	int err = 0;

	if (argc < 3) {
		fprintf(stderr, "No SafeLoader file passed\n");
		err = -EINVAL;
		goto out;
	}
	safeloader_path = argv[2];

	safeloader = fopen(safeloader_path, "r");
	if (!safeloader) {
		fprintf(stderr, "Couldn't open %s\n", safeloader_path);
		err = -EACCES;
		goto out;
	}

	bytes = fread(&hdr, 1, sizeof(hdr), safeloader);
	if (bytes != sizeof(hdr)) {
		fprintf(stderr, "Couldn't read %s header\n", safeloader_path);
		err =  -EIO;
		goto err_close;
	}
	imagesize = be32_to_cpu(hdr.imagesize);

	MD5_Init(&ctx);
	MD5_Update(&ctx, md5_salt, sizeof(md5_salt));
	while ((bytes = fread(buf, 1, osafeloader_min(sizeof(buf), imagesize), safeloader)) > 0) {
		MD5_Update(&ctx, buf, bytes);
		imagesize -= bytes;
	}
	MD5_Final(md5, &ctx);

	if (memcmp(md5, hdr.md5, 16)) {
		fprintf(stderr, "Broken SafeLoader file with invalid MD5\n");
		err =  -EIO;
		goto err_close;
	}

	printf("%10s: %d\n", "Image size", be32_to_cpu(hdr.imagesize));
	printf("%10s: ", "MD5");
	for (i = 0; i < 16; i++)
		printf("%02x", md5[i]);
	printf("\n");

	/* Skip header & vendor info */
	fseek(safeloader, 0x1014, SEEK_SET);

	while (fscanf(safeloader, "fwup-ptn %s base 0x%x size 0x%x\t\r\n", name, &base, &size) == 3) {
		printf("%10s: %s (0x%x - 0x%x)\n", "Partition", name, base, base + size);
	}

err_close:
	fclose(safeloader);
out:
	return err;
}

/**************************************************
 * Extract
 **************************************************/

static void osafeloader_extract_parse_options(int argc, char **argv) {
	int c;

	while ((c = getopt(argc, argv, "p:o:")) != -1) {
		switch (c) {
		case 'p':
			partition_name = optarg;
			break;
		case 'o':
			out_path = optarg;
			break;
		}
	}
}

static int osafeloader_extract(int argc, char **argv) {
	FILE *safeloader;
	FILE *out;
	struct safeloader_header hdr;
	size_t bytes;
	char name[32];
	int base, size;
	int err = 0;

	if (argc < 3) {
		fprintf(stderr, "No SafeLoader file passed\n");
		err = -EINVAL;
		goto out;
	}
	safeloader_path = argv[2];

	optind = 3;
	osafeloader_extract_parse_options(argc, argv);
	if (!partition_name) {
		fprintf(stderr, "No partition name specified\n");
		err = -EINVAL;
		goto out;
	} else if (!out_path) {
		fprintf(stderr, "No output file specified\n");
		err = -EINVAL;
		goto out;
	}

	safeloader = fopen(safeloader_path, "r");
	if (!safeloader) {
		fprintf(stderr, "Couldn't open %s\n", safeloader_path);
		err = -EACCES;
		goto out;
	}

	out = fopen(out_path, "w");
	if (!out) {
		fprintf(stderr, "Couldn't open %s\n", out_path);
		err = -EACCES;
		goto err_close_safeloader;
	}

	bytes = fread(&hdr, 1, sizeof(hdr), safeloader);
	if (bytes != sizeof(hdr)) {
		fprintf(stderr, "Couldn't read %s header\n", safeloader_path);
		err =  -EIO;
		goto err_close_out;
	}

	/* Skip vendor info */
	fseek(safeloader, 0x1000, SEEK_CUR);

	err = -ENOENT;
	while (fscanf(safeloader, "fwup-ptn %s base 0x%x size 0x%x\t\r\n", name, &base, &size) == 3) {
		uint8_t buf[1024];

		if (strcmp(name, partition_name))
			continue;

		err = 0;

		fseek(safeloader, sizeof(hdr) + 0x1000 + base, SEEK_SET);

		while ((bytes = fread(buf, 1, osafeloader_min(sizeof(buf), size), safeloader)) > 0) {
			if (fwrite(buf, 1, bytes, out) != bytes) {
				fprintf(stderr, "Couldn't write %zu B to %s\n", bytes, out_path);
				err = -EIO;
				break;
			}
			size -= bytes;
		}

		if (size) {
			fprintf(stderr, "Couldn't extract whole partition %s from %s (%d B left)\n", partition_name, safeloader_path, size);
			err = -EIO;
		}

		break;
	}

err_close_out:
	fclose(out);
err_close_safeloader:
	fclose(safeloader);
out:
	return err;
}

/**************************************************
 * Start
 **************************************************/

static void usage() {
	printf("Usage:\n");
	printf("\n");
	printf("Info about SafeLoader:\n");
	printf("\tosafeloader info <file>\n");
	printf("\n");
	printf("Extract from SafeLoader:\n");
	printf("\tosafeloader extract <file> [options]\n");
	printf("\t-p name\t\t\t\tname of partition to extract\n");
	printf("\t-o file\t\t\t\toutput file\n");
}

int main(int argc, char **argv) {
	if (argc > 1) {
		if (!strcmp(argv[1], "info"))
			return osafeloader_info(argc, argv);
		else if (!strcmp(argv[1], "extract"))
			return osafeloader_extract(argc, argv);
	}

	usage();
	return 0;
}