xref: /openbmc/u-boot/tools/gpimage-common.c (revision ef48f6dd)
1 /*
2  * (C) Copyright 2014
3  * Texas Instruments Incorporated
4  * Refactored common functions in to gpimage-common.c.
5  *
6  * (C) Copyright 2010
7  * Linaro LTD, www.linaro.org
8  * Author: John Rigby <john.rigby@linaro.org>
9  * Based on TI's signGP.c
10  *
11  * (C) Copyright 2009
12  * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
13  *
14  * (C) Copyright 2008
15  * Marvell Semiconductor <www.marvell.com>
16  * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
17  *
18  * SPDX-License-Identifier:	GPL-2.0+
19  */
20 
21 #include "imagetool.h"
22 #include <compiler.h>
23 #include <image.h>
24 #include "gpheader.h"
25 
26 /* Helper to convert size and load_addr to big endian */
27 void to_be32(uint32_t *gph_size, uint32_t *gph_load_addr)
28 {
29 	*gph_size = cpu_to_be32(*gph_size);
30 	*gph_load_addr = cpu_to_be32(*gph_load_addr);
31 }
32 
33 int gph_verify_header(struct gp_header *gph, int be)
34 {
35 	uint32_t gph_size = gph->size;
36 	uint32_t gph_load_addr = gph->load_addr;
37 
38 	if (be)
39 		to_be32(&gph_size, &gph_load_addr);
40 
41 	if (!gph_size || !gph_load_addr)
42 		return -1;
43 
44 	return 0;
45 }
46 
47 void gph_print_header(const struct gp_header *gph, int be)
48 {
49 	uint32_t gph_size = gph->size, gph_load_addr = gph->load_addr;
50 
51 	if (be)
52 		to_be32(&gph_size, &gph_load_addr);
53 
54 	if (!gph_size) {
55 		fprintf(stderr, "Error: invalid image size %x\n", gph_size);
56 		exit(EXIT_FAILURE);
57 	}
58 
59 	if (!gph_load_addr) {
60 		fprintf(stderr, "Error: invalid image load address %x\n",
61 			gph_load_addr);
62 		exit(EXIT_FAILURE);
63 	}
64 	printf("GP Header: Size %x LoadAddr %x\n", gph_size, gph_load_addr);
65 }
66 
67 void gph_set_header(struct gp_header *gph, uint32_t size, uint32_t load_addr,
68 	int be)
69 {
70 	gph->size = size;
71 	gph->load_addr = load_addr;
72 	if (be)
73 		to_be32(&gph->size, &gph->load_addr);
74 }
75 
76 int gpimage_check_params(struct image_tool_params *params)
77 {
78 	return	(params->dflag && (params->fflag || params->lflag)) ||
79 		(params->fflag && (params->dflag || params->lflag)) ||
80 		(params->lflag && (params->dflag || params->fflag));
81 }
82