summaryrefslogtreecommitdiffstats
path: root/src/mkrkboot.c
blob: aff8fed71c3c9a2dfed658e46cec6a45597fbc54 (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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* SPDX-License-Identifier: GPL-2.0-only */
/*
 * mkrkboot - build the Android boot image booted by Rockchip's U-Boot
 * together with the Rockchip resource image carrying the device tree
 */
#include <errno.h>
#include <getopt.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "sha1.h"

#define BLOCK_SIZE		512
#define SHA1_LEN		20
#define MAX_ENTRIES		16

#define BOOT_MAGIC		"ANDROID!"
#define BOOT_NAME_LEN		16
#define BOOT_CMDLINE_LEN	512
#define BOOT_ID_LEN		32
#define BOOT_EXTRA_LEN		1024
#define BOOT_OFF_KERNEL_SIZE	8
#define BOOT_OFF_KERNEL_ADDR	12
#define BOOT_OFF_SECOND_SIZE	24
#define BOOT_OFF_SECOND_ADDR	28
#define BOOT_OFF_TAGS_ADDR	32
#define BOOT_OFF_PAGE_SIZE	36
#define BOOT_OFF_NAME		48
#define BOOT_OFF_CMDLINE	(BOOT_OFF_NAME + BOOT_NAME_LEN)
#define BOOT_OFF_ID		(BOOT_OFF_CMDLINE + BOOT_CMDLINE_LEN)
#define BOOT_HDR_SIZE		(BOOT_OFF_ID + BOOT_ID_LEN + BOOT_EXTRA_LEN)

#define RES_MAGIC		"RSCE"
#define RES_ENTRY_MAGIC		"ENTR"
#define RES_PATH_LEN		220
#define RES_HASH_LEN		32
#define RES_OFF_HDR_BLOCKS	8
#define RES_OFF_TABLE_BLOCK	9
#define RES_OFF_ENTRY_BLOCKS	10
#define RES_OFF_COUNT		12
#define RES_OFF_PATH		4
#define RES_OFF_HASH		(RES_OFF_PATH + RES_PATH_LEN)
#define RES_OFF_HASH_SIZE	(RES_OFF_HASH + RES_HASH_LEN)
#define RES_OFF_OFFSET		(RES_OFF_HASH_SIZE + 4)
#define RES_OFF_SIZE		(RES_OFF_OFFSET + 4)

struct blob {
	uint8_t		*data;
	size_t		len;
};

struct res_entry {
	char		*path;
	struct blob	blob;
};

static void put_le32(uint8_t *buf, uint32_t val)
{
	buf[0] = val & 0xff;
	buf[1] = (val >> 8) & 0xff;
	buf[2] = (val >> 16) & 0xff;
	buf[3] = val >> 24;
}

static size_t round_up(size_t val, size_t align)
{
	return (val + align - 1) / align * align;
}

static int blob_read(const char *path, struct blob *blob)
{
	FILE *fp;
	long len;

	fp = fopen(path, "rb");
	if (!fp) {
		fprintf(stderr, "%s: %s\n", path, strerror(errno));
		return -1;
	}
	if (fseek(fp, 0, SEEK_END) || (len = ftell(fp)) < 0 ||
	    fseek(fp, 0, SEEK_SET)) {
		fprintf(stderr, "%s: cannot determine size\n", path);
		fclose(fp);
		return -1;
	}
	blob->len = len;
	blob->data = malloc(len ? len : 1);
	if (!blob->data || fread(blob->data, 1, len, fp) != (size_t)len) {
		fprintf(stderr, "%s: read failed\n", path);
		fclose(fp);
		return -1;
	}
	fclose(fp);
	return 0;
}

static int blob_write(const char *path, const struct blob *blob)
{
	FILE *fp;

	fp = fopen(path, "wb");
	if (!fp) {
		fprintf(stderr, "%s: %s\n", path, strerror(errno));
		return -1;
	}
	if (fwrite(blob->data, 1, blob->len, fp) != blob->len ||
	    fclose(fp)) {
		fprintf(stderr, "%s: write failed\n", path);
		return -1;
	}
	return 0;
}

static int res_build(struct res_entry *entries, int count,
		     struct blob *out)
{
	size_t table = BLOCK_SIZE + (size_t)count * BLOCK_SIZE;
	size_t offset;
	size_t total;
	uint8_t *ent;
	int i;

	total = table;
	for (i = 0; i < count; i++)
		total += round_up(entries[i].blob.len, BLOCK_SIZE);

	out->data = calloc(1, total);
	if (!out->data)
		return -1;
	out->len = total;

	memcpy(out->data, RES_MAGIC, 4);
	out->data[RES_OFF_HDR_BLOCKS] = 1;
	out->data[RES_OFF_TABLE_BLOCK] = 1;
	out->data[RES_OFF_ENTRY_BLOCKS] = 1;
	put_le32(out->data + RES_OFF_COUNT, count);

	offset = count + 1;
	for (i = 0; i < count; i++) {
		ent = out->data + BLOCK_SIZE + (size_t)i * BLOCK_SIZE;
		memcpy(ent, RES_ENTRY_MAGIC, 4);
		strncpy((char *)ent + RES_OFF_PATH, entries[i].path,
			RES_PATH_LEN - 1);
		sha1_csum(entries[i].blob.data, entries[i].blob.len,
			  ent + RES_OFF_HASH);
		put_le32(ent + RES_OFF_HASH_SIZE, SHA1_LEN);
		put_le32(ent + RES_OFF_OFFSET, offset);
		put_le32(ent + RES_OFF_SIZE, entries[i].blob.len);
		memcpy(out->data + offset * BLOCK_SIZE, entries[i].blob.data,
		       entries[i].blob.len);
		offset += round_up(entries[i].blob.len, BLOCK_SIZE) /
			  BLOCK_SIZE;
	}
	return 0;
}

static int boot_build(const struct blob *kernel, const struct blob *second,
		      uint32_t kernel_addr, uint32_t second_addr,
		      uint32_t tags_addr, const char *name,
		      const char *cmdline, size_t page, struct blob *out)
{
	size_t kernel_pad = round_up(kernel->len, page);
	size_t second_pad = round_up(second->len, page);
	size_t hdr_pad = round_up(BOOT_HDR_SIZE, page);
	uint8_t len_le[4];
	sha1_context sha;
	uint8_t *hdr;

	out->len = hdr_pad + kernel_pad + second_pad;
	out->data = calloc(1, out->len);
	if (!out->data)
		return -1;

	hdr = out->data;
	memcpy(hdr, BOOT_MAGIC, 8);
	put_le32(hdr + BOOT_OFF_KERNEL_SIZE, kernel->len);
	put_le32(hdr + BOOT_OFF_KERNEL_ADDR, kernel_addr);
	put_le32(hdr + BOOT_OFF_SECOND_SIZE, second->len);
	put_le32(hdr + BOOT_OFF_SECOND_ADDR, second_addr);
	put_le32(hdr + BOOT_OFF_TAGS_ADDR, tags_addr);
	put_le32(hdr + BOOT_OFF_PAGE_SIZE, page);
	strncpy((char *)hdr + BOOT_OFF_NAME, name, BOOT_NAME_LEN - 1);
	strncpy((char *)hdr + BOOT_OFF_CMDLINE, cmdline,
		BOOT_CMDLINE_LEN - 1);

	/* kernel, empty ramdisk and second stage, each followed by its size */
	sha1_starts(&sha);
	sha1_update(&sha, kernel->data, kernel->len);
	put_le32(len_le, kernel->len);
	sha1_update(&sha, len_le, 4);
	put_le32(len_le, 0);
	sha1_update(&sha, len_le, 4);
	sha1_update(&sha, second->data, second->len);
	put_le32(len_le, second->len);
	sha1_update(&sha, len_le, 4);
	sha1_finish(&sha, hdr + BOOT_OFF_ID);

	memcpy(out->data + hdr_pad, kernel->data, kernel->len);
	memcpy(out->data + hdr_pad + kernel_pad, second->data, second->len);
	return 0;
}

static void usage(const char *progname)
{
	fprintf(stderr,
		"Usage: %s -k <kernel> -d <dtb> -o <output> [options]\n"
		"\n"
		"Options:\n"
		"  -k <file>      kernel image\n"
		"  -d <file>      device tree, stored as rk-kernel.dtb\n"
		"  -e <name=file> additional resource image entry\n"
		"  -o <file>      output boot image\n"
		"  -a <addr>      kernel load address\n"
		"  -f <addr>      resource image load address\n"
		"  -t <addr>      kernel tags address\n"
		"  -c <string>    kernel command line\n"
		"  -n <string>    image name\n"
		"  -p <size>      page size (default 2048)\n"
		"  -h             show this help\n",
		progname);
}

int main(int argc, char *argv[])
{
	uint32_t kernel_addr = 0, second_addr = 0, tags_addr = 0;
	struct res_entry entries[MAX_ENTRIES];
	const char *cmdline = "", *name = "";
	struct blob resource, kernel, boot;
	const char *kernel_path = NULL;
	const char *out_path = NULL;
	const char *dtb_path = NULL;
	size_t page = 2048;
	int count = 1;
	char *sep;
	int opt;
	int i;

	while ((opt = getopt(argc, argv, "k:d:e:o:a:f:t:c:n:p:h")) != -1) {
		switch (opt) {
		case 'k':
			kernel_path = optarg;
			break;
		case 'd':
			dtb_path = optarg;
			break;
		case 'e':
			sep = strchr(optarg, '=');
			if (!sep || count >= MAX_ENTRIES) {
				usage(argv[0]);
				return EXIT_FAILURE;
			}
			*sep = '\0';
			entries[count].path = optarg;
			if (blob_read(sep + 1, &entries[count].blob))
				return EXIT_FAILURE;
			count++;
			break;
		case 'o':
			out_path = optarg;
			break;
		case 'a':
			kernel_addr = strtoul(optarg, NULL, 0);
			break;
		case 'f':
			second_addr = strtoul(optarg, NULL, 0);
			break;
		case 't':
			tags_addr = strtoul(optarg, NULL, 0);
			break;
		case 'c':
			cmdline = optarg;
			break;
		case 'n':
			name = optarg;
			break;
		case 'p':
			page = strtoul(optarg, NULL, 0);
			break;
		default:
			usage(argv[0]);
			return opt == 'h' ? EXIT_SUCCESS : EXIT_FAILURE;
		}
	}

	if (!kernel_path || !dtb_path || !out_path || !page) {
		usage(argv[0]);
		return EXIT_FAILURE;
	}

	entries[0].path = "rk-kernel.dtb";
	if (blob_read(dtb_path, &entries[0].blob) ||
	    blob_read(kernel_path, &kernel))
		return EXIT_FAILURE;

	for (i = 0; i < count; i++) {
		if (strlen(entries[i].path) >= RES_PATH_LEN) {
			fprintf(stderr, "%s: path too long\n", entries[i].path);
			return EXIT_FAILURE;
		}
	}

	if (res_build(entries, count, &resource) ||
	    boot_build(&kernel, &resource, kernel_addr, second_addr,
		       tags_addr, name, cmdline, page, &boot)) {
		fprintf(stderr, "out of memory\n");
		return EXIT_FAILURE;
	}

	return blob_write(out_path, &boot) ? EXIT_FAILURE : EXIT_SUCCESS;
}