aboutsummaryrefslogtreecommitdiff
path: root/target/msm7630_surf/tools/mkheader.c
blob: 7d3ce221f182954532f1f8bc23b31910066882e8 (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
/* Copyright 2007, Google Inc. */

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

#include <sys/stat.h>

#ifndef MEMBASE
#define MEMBASE 0
#endif

int main(int argc, char *argv[])
{
	struct stat s;
	unsigned size, base;
	int unified_boot = 0;
	unsigned unified_boot_magic[20];
	unsigned non_unified_boot_magic[10];
	unsigned magic_len = 0;
	unsigned *magic;
	int fd;

	if(argc < 3) {
		fprintf(stderr,"usage: mkheader <bin> <hdr>\n");
		return -1;
	}

	if (argc == 4) {
		if(!strcmp("unified-boot",argv[3])) {
			unified_boot = 1;
		}
	}

	if(stat(argv[1], &s)) {
		perror("cannot stat binary");
		return -1;
	}

	if(unified_boot) {
		magic = unified_boot_magic;
		magic_len = sizeof(unified_boot_magic);
	} else {
		magic = non_unified_boot_magic;
		magic_len = sizeof(non_unified_boot_magic);
	}

	size = s.st_size;
	base = MEMBASE;

	magic[0] = 0x00000005; /* appsbl */
	magic[1] = 0x00000002; /* nand */
	magic[2] = 0x00000000;
	magic[3] = base;
	magic[4] = size;
	magic[5] = size;
	magic[6] = size + base;
	magic[7] = 0x00000000;
	magic[8] = size + base;
	magic[9] = 0x00000000;

	if (unified_boot == 1)
	{
		magic[10] = 0x33836685; /* cookie magic number */
		magic[11] = 0x00000001; /* cookie version */
		magic[12] = 0x00000002; /* file formats */
		magic[13] = 0x00000000;
		magic[14] = 0x00500000; /* 5M for boot.img */
		magic[15] = 0x00000000;
		magic[16] = 0x00000000;
		magic[17] = 0x00000000;
		magic[18] = 0x00000000;
		magic[19] = 0x00000000;
	}

	fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
	if(fd < 0) {
		perror("cannot open header for writing");
		return -1;
	}
	if(write(fd, magic, magic_len) != magic_len) {
		perror("cannot write header");
		close(fd);
		unlink(argv[2]);
		return -1;
	}
	close(fd);

	return 0;
}