aboutsummaryrefslogtreecommitdiffstats
path: root/tools/modem-image-tool.c
blob: 4d30a7fddc4f00c4a7fa95559eae758dfad1f5bc (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
/*
 * This file is part of libsamsung-ipc.
 *
 * Copyright (C) 2020 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
 *
 * libsamsung-ipc is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * libsamsung-ipc is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with libsamsung-ipc.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <sys/mman.h>
#include <sys/stat.h>

#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../samsung-ipc/devices/i9300/i9300.h"

/* Taken from the herolte patch which is GPLv2 or later and has the following:
 * Copyright (C) 2020 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
 */
struct toc_part_info {
	char name[12];
	uint32_t offset;
	uint32_t loadaddr;
	uint32_t size;
	uint32_t crc;
	uint32_t entryid;
} __attribute__((__packed__));

/* TODO: fill in loadaddr, crc, entryid */
static struct toc_part_info gt_i9300[] = {
	{"PSIRAM", I9300_PSI_OFFSET, 0, I9300_PSI_SIZE, 0, 0},
	{"EBL", I9300_EBL_OFFSET, 0, I9300_EBL_SIZE, 0, 0},
	{"MAIN", I9300_FIRMWARE_OFFSET, 0, I9300_FIRMWARE_SIZE, 0, 0},
	{"SECPACK", I9300_SEC_START_OFFSET, 0, I9300_SEC_START_SIZE, 0, 0},
	{"NV", I9300_NV_DATA_OFFSET, 0, I9300_NV_DATA_SIZE, 0, 0},
};

static int part_info_cmp(struct toc_part_info* toc_part_info1,
			 struct toc_part_info* toc_part_info2)
{
	if ((toc_part_info2->offset - toc_part_info1->offset) != 0) {
		printf("toc_part_info1->offset: 0x%x "
		       "toc_part_info2->offset: 0x%x\n",
		       le32toh(toc_part_info1->offset),
		       le32toh(toc_part_info2->offset));

		return toc_part_info2->offset - toc_part_info1->offset;
	}

	if ((toc_part_info2->size - toc_part_info1->size) != 0){
		printf("toc_part_info1->size: 0x%x "
		       "toc_part_info2->size: 0x%x\n",
		       le32toh(toc_part_info1->size),
		       le32toh(toc_part_info2->size));

		return toc_part_info2->size - toc_part_info1->size;
	}

	return 0;
}

static int usage(char* progname)
{
	printf("%s [path/to/RADIO.img]\n", progname);
	exit(1);
}

int main(int argc, char* argv[])
{
	struct toc_part_info* toc_part_info;
	void* buffer;
	int fd;
	int pnum;

	if (argc != 2)
		usage(argv[0]);

	fd = open(argv[1], O_RDONLY);
	if (fd == -1) {
		perror("Failed to open");
		return -1;
	}

	buffer = mmap(NULL, 0x1000, PROT_READ, MAP_SHARED, fd, 0);
	if (buffer == MAP_FAILED) {
		perror("mmap failed");
		return -1;
	}

	size_t name_max_len = 0;
	for(pnum = 0;pnum < 10;pnum++){
		char* toc_part_info_buffer = buffer +
			pnum * sizeof(struct toc_part_info);
		char name[13];

		if (toc_part_info_buffer[0] == '\0') {
			break;
		}

		toc_part_info = (struct toc_part_info*) toc_part_info_buffer;

		memcpy(&name, toc_part_info->name, sizeof(toc_part_info->name));
		name[12] = '\0';

		if (strlen(name) > name_max_len)
			name_max_len = strlen(name);
		
	}

	for(pnum = 0;pnum < 10;pnum++){
		char* toc_part_info_buffer = buffer +
			pnum * sizeof(struct toc_part_info);
		char name[13];
		size_t i = 0;

		if (toc_part_info_buffer[0] == '\0') {
			break;
		}
		toc_part_info = (struct toc_part_info*) toc_part_info_buffer;

		memcpy(&name, toc_part_info->name, sizeof(toc_part_info->name));
		name[12] = '\0';

		/* Alignement */
		printf("%s: ", name);
		for (i=0 ; i < (name_max_len - strlen(name)) ; i++)
			printf(" ");

		printf("offset: 0x%x,",le32toh(toc_part_info->offset));

		printf("loadaddr: 0x%x, size: 0x%x, crc: 0x%x, entryid: 0x%x\n",
		       le32toh(toc_part_info->loadaddr),
		       le32toh(toc_part_info->size),
		       le32toh(toc_part_info->crc),
		       le32toh(toc_part_info->entryid));


		part_info_cmp(toc_part_info, &(gt_i9300[pnum]));
	}

	return 0;
}