sptool.c 7.69 KB
Newer Older
1
/*
2
 * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
3
4
5
6
7
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <stdarg.h>
8
#include <stdbool.h>
9
10
11
12
13
14
15
16
17
18
19
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "sptool.h"

#define PAGE_SIZE		4096

/*
20
 * Entry describing Secure Partition package.
21
 */
22
struct sp_pkg_info {
23
	/* Location of the files in the host's RAM. */
24
	void *img_data, *pm_data;
25
26

	/* Size of the files. */
27
	uint32_t img_size, pm_size;
28
29

	/* Location of the binary files inside the package output file */
30
	uint32_t img_offset, pm_offset;
31
32
};

33
34
35
36
37
38
39
/*
 * List of input provided by user
 */
struct arg_list {
	char *usr_input;
	struct arg_list *next;
};
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

/* Align an address to a power-of-two boundary. */
static unsigned int align_to(unsigned int address, unsigned int boundary)
{
	unsigned int mask = boundary - 1U;

	if ((address & mask) != 0U)
		return (address + boundary) & ~mask;
	else
		return address;
}

/* Allocate a memory area of 'size' bytes and zero it. */
static void *xzalloc(size_t size, const char *msg)
{
	void *d;

	d = malloc(size);
	if (d == NULL) {
		fprintf(stderr, "error: malloc: %s\n", msg);
		exit(1);
	}

	memset(d, 0, size);

	return d;
}

/*
 * Write 'size' bytes from 'buf' into the specified file stream.
 * Exit the program on error.
 */
static void xfwrite(void *buf, size_t size, FILE *fp)
{
	if (fwrite(buf, 1, size, fp) != size) {
		fprintf(stderr, "error: Failed to write to output file.\n");
		exit(1);
	}
}

/*
 * Set the file position indicator for the specified file stream.
 * Exit the program on error.
 */
static void xfseek(FILE *fp, long offset, int whence)
{
	if (fseek(fp, offset, whence) != 0) {
		fprintf(stderr, "error: Failed to set file to offset 0x%lx (%d).\n",
		       offset, whence);
		perror(NULL);
		exit(1);
	}
}

94
95
96
97
/*
 * Free SP package structure
 */
static void cleanup(struct sp_pkg_info *sp)
98
99
{

100
101
102
103
	if (sp != NULL) {
		if (sp->img_data != NULL) {
			free(sp->img_data);
		}
104

105
106
107
		if (sp->pm_data != NULL) {
			free(sp->pm_data);
		}
108
109
110
111

		free(sp);

	}
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
/*
 * Free argument list structure
 */
static void freelist(struct arg_list *head)
{
	struct arg_list *tmp;

	while (head != NULL) {
		tmp = head;
		head = head->next;
		free(tmp);
	}
}

/*
 * Append user inputs in argument list structure
 */
static void append_user_input(struct arg_list **head, char *args)
{
	struct arg_list *tmp = *head;

	if (tmp == NULL) {
		tmp = xzalloc(sizeof(struct arg_list),
				"Failed to allocate arg_list struct");
		tmp->usr_input = args;
		*head = tmp;
	} else {
		while (tmp->next != NULL) {
			tmp = tmp->next;
		}
		tmp->next = xzalloc(sizeof(struct arg_list),
				"Failed to allocate arg_list struct");
		tmp = tmp->next;
		tmp->usr_input = args;
	}
149
150
151
152
153
154
155
}

/*
 * Allocate a buffer big enough to store the content of the specified file and
 * load the file into it. Fill 'size' with the file size. Exit the program on
 * error.
 */
156
static void load_file(const char *path, void **ptr, uint32_t *size)
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
{
	FILE *f = fopen(path, "rb");
	if (f == NULL) {
		fprintf(stderr, "error: %s couldn't be opened.\n", path);
		exit(1);
	}

	xfseek(f, 0, SEEK_END);
	*size = ftell(f);
	if (*size == 0) {
		fprintf(stderr, "error: Size of %s is 0\n", path);
		exit(1);
	}

	rewind(f);

	*ptr = malloc(*size);
	if (*ptr == NULL) {
		fprintf(stderr, "error: Not enough memory to load %s\n", path);
		exit(1);
	}

	if (fread(*ptr, *size, 1, f) != 1) {
		fprintf(stderr, "error: Couldn't read %s\n", path);
		exit(1);
	}

	fclose(f);
}

187
188
189
190
191
/*
 * Parse the string containing input payloads and fill in the
 * SP Package data structure.
 */
static void load_sp_pm(char *path, struct sp_pkg_info **sp_out)
192
{
193
194
	struct sp_pkg_info *sp_pkg;

195
196
197
198
199
	char *split_mark = strstr(path, ":");

	*split_mark = '\0';

	char *sp_path = path;
200
	char *pm_path = split_mark + 1;
201

202
203
	sp_pkg = xzalloc(sizeof(struct sp_pkg_info),
		"Failed to allocate sp_pkg_info struct");
204

205
206
	load_file(pm_path, &sp_pkg->pm_data, &sp_pkg->pm_size);
	printf("\nLoaded SP Manifest file %s (%u bytes)\n", pm_path, sp_pkg->pm_size);
207

208
209
	load_file(sp_path, &sp_pkg->img_data, &sp_pkg->img_size);
	printf("Loaded SP Image file %s (%u bytes)\n", sp_path, sp_pkg->img_size);
210

211
	*sp_out = sp_pkg;
212
213
}

214
215
216
217
/*
 * Write SP package data structure into output file.
 */
static void output_write(const char *path, struct sp_pkg_info *sp, bool header)
218
{
219
220
	struct sp_pkg_header sp_header_info;
	unsigned int file_ptr = 0;
221
222
223
224
225
226
227

	FILE *f = fopen(path, "wb");
	if (f == NULL) {
		fprintf(stderr, "error: Failed to open %s\n", path);
		exit(1);
	}

228
229
230
	/* Reserve Header size */
	if (header) {
		file_ptr = sizeof(struct sp_pkg_header);
231
232
	}

233
234
235
236
	/* Save partition manifest */
	xfseek(f, file_ptr, SEEK_SET);
	printf("Writing SP Manifest at offset 0x%x (%u bytes)\n",
	       file_ptr, sp->pm_size);
237

238
239
	sp->pm_offset = file_ptr;
	xfwrite(sp->pm_data, sp->pm_size, f);
240

241
242
243
244
245
	/* Save partition image aligned to Page size */
	file_ptr = align_to((sp->pm_offset + sp->pm_size), PAGE_SIZE);
	xfseek(f, file_ptr, SEEK_SET);
	printf("Writing SP Image at offset 0x%x (%u bytes)\n",
	       file_ptr, sp->img_size);
246

247
248
	sp->img_offset = file_ptr;
	xfwrite(sp->img_data, sp->img_size, f);
249

250
251
252
253
254
255
256
257
	/* Finally, write header, if needed */
	if (header) {
		sp_header_info.magic = SECURE_PARTITION_MAGIC;
		sp_header_info.version = 0x1;
		sp_header_info.img_offset = sp->img_offset;
		sp_header_info.img_size = sp->img_size;
		sp_header_info.pm_offset = sp->pm_offset;
		sp_header_info.pm_size = sp->pm_size;
258

259
		xfseek(f, 0, SEEK_SET);
260

261
		printf("Writing package header\n");
262

263
		xfwrite(&sp_header_info, sizeof(struct sp_pkg_header), f);
264
265
266
	}

	/* All information has been written now */
267
	printf("\nsptool: Built Secure Partition blob %s\n", path);
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282

	fclose(f);
}

static void usage(void)
{
	printf("usage: sptool ");
#ifdef VERSION
	printf(VERSION);
#else
	/* If built from sptool directory, VERSION is not set. */
	printf("version unknown");
#endif
	printf(" [<args>]\n\n");

283
284
285
286
287
	printf("This tool takes as input set of image binary files and the\n"
	       "partition manifest blobs as input and generates set of\n"
	       "output package files\n"
	       "Usage example: sptool -i sp1.bin:sp1.dtb -o sp1.pkg\n"
	       "                      -i sp2.bin:sp2.dtb -o sp2.pkg ...\n\n");
288
289
	printf("Commands supported:\n");
	printf("  -o <path>            Set output file path.\n");
290
291
	printf("  -i <sp_path:pm_path> Add Secure Partition image and\n"
	       "                       Manifest blob (specified in two paths\n"
292
	       "                       separated by a colon).\n");
293
	printf("  -n                   Generate package without header\n");
294
295
296
297
298
299
	printf("  -h                   Show this message.\n");
	exit(1);
}

int main(int argc, char *argv[])
{
300
301
302
303
304
305
306
307
	struct sp_pkg_info *sp_pkg = NULL;
	struct arg_list *in_head = NULL;
	struct arg_list *out_head = NULL;
	struct arg_list *in_list = NULL;
	struct arg_list *out_list = NULL;
	unsigned int match_counter = 0;
	bool need_header = true;

308
309
	int ch;

310
311
312
313
314
315
316
	if (argc <= 1) {
		fprintf(stderr, "error: File paths must be provided.\n\n");
		usage();
		return 1;
	}

	while ((ch = getopt(argc, argv, "hni:o:")) != -1) {
317
318
		switch (ch) {
		case 'i':
319
320
			append_user_input(&in_head, optarg);
			match_counter++;
321
322
			break;
		case 'o':
323
324
325
326
327
			append_user_input(&out_head, optarg);
			match_counter--;
			break;
		case 'n':
			need_header = false;
328
329
330
331
332
333
334
			break;
		case 'h':
		default:
			usage();
		}
	}

335
336
337
338
	if (match_counter) {
		fprintf(stderr, "error: Input/Output count mismatch.\n\n");
		freelist(in_head);
		freelist(out_head);
339
340
341
342
		usage();
		return 1;
	}

343
344
345
346
347
348
349
350
351
352
353
	in_list = in_head;
	out_list = out_head;
	while (in_list != NULL) {
		load_sp_pm(in_list->usr_input, &sp_pkg);
		output_write(out_list->usr_input, sp_pkg, need_header);
		in_list = in_list->next;
		out_list = out_list->next;
	}

	argc -= optind;
	argv += optind;
354

355
356
357
	cleanup(sp_pkg);
	freelist(in_head);
	freelist(out_head);
358
359
360

	return 0;
}