aboutsummaryrefslogtreecommitdiffstats
path: root/package/boot/uboot-oxnas/files/tools/mkox820crc.c
blob: d100191f2dc96906abad0104f5d67dbf797c6b2a (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
/* J J Larworthy 27 September 2006 */

/* file to read the boot sector of a dis and the loaded image and report
 * if the boot rom would accept the data as intact and suitable for use
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/errno.h>

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

extern uint32_t crc32(uint32_t, const unsigned char *, unsigned int);

#define NUMBER_VECTORS   12
struct {
	unsigned int start_vector[NUMBER_VECTORS];
	char code[4];
	unsigned int header_length;
	unsigned int reserved[3];
	unsigned int length;
	unsigned int img_CRC;
	unsigned int CRC;
} img_header;

void print_usage(void)
{
	printf("update_header file.bin\n");
}

void print_header(void)
{
	int i;

	printf("vectors in header\n");
	for (i = 0; i < NUMBER_VECTORS; i++) {
		printf("%d:0x%08x\n", i, img_header.start_vector[i]);
	}
	printf("length:%8x\nimg_CRC:0x%08x\nHeader CRC:0x%08x\n",
		img_header.length, img_header.img_CRC, img_header.CRC);
}

int main(int argc, char **argv)
{
	int in_file;
	int status;
	int unsigned crc;
	int file_length;
	int len;

	struct stat file_stat;

	void *executable;

	in_file = open(argv[1], O_RDWR);

	if (in_file < 0) {
		printf("failed to open file:%s\n", argv[optind]);
		return -ENOENT;
	}

	status = fstat(in_file, &file_stat);

	/* read header and obtain size of image */
	status = read(in_file, &img_header, sizeof(img_header));

	file_length = file_stat.st_size - sizeof(img_header);

	if (img_header.length != file_length) {
		printf("size in header:%d, size of file: %d\n",
			img_header.length, file_length);
	}
	img_header.length = file_length;

	/* read working image and CRC */
	executable = malloc(file_length);

	status = read(in_file, executable, file_length);

	if (status != file_length) {
		printf("Failed to load image\n");
		return -ENOENT;
	}

	/* verify image CRC */
	crc = crc32(0, (const unsigned char *) executable, img_header.length);

	if (crc != img_header.img_CRC) {
		printf("New Image CRC:0x%08x, hdr:0x%08x\n", crc,
			img_header.img_CRC);
		img_header.img_CRC = crc;
	}
	memcpy(img_header.code, "BOOT", 4);
	img_header.header_length = sizeof(img_header);

	/* check header CRC */
	crc = crc32(0, (const unsigned char *) &img_header,
			sizeof(img_header) - sizeof(unsigned int));
	if (crc != img_header.CRC) {
		printf("New header CRC - crc:0x%08x hdr:0x%08x\n", crc,
			img_header.CRC);
		img_header.CRC = crc;
	}

	/* re-write the file */
	status = lseek(in_file, 0, SEEK_SET);
	if (status != 0) {
		printf("failed to rewind\n");
		return 1;
	}
	len = write(in_file, &img_header, sizeof(img_header));
	assert(len == sizeof(img_header));
	len = write(in_file, executable, file_length);
	assert(len == file_length);
	close(in_file);

	return 0;
}