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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
* Copyright (C) 2023, Ferass El Hafidi <vitali64pmemail@protonmail.com>
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <err.h>
#include "acsbaby.h"
#include "parse.h"
#include "timing.h"
#include "util.h"
int print_usage(char *);
int main(int argc, char *argv[]) {
int argument, fildes;
uint16_t acs_offset, diff_offset;
char buffer[4096], *format = "readable", *type = "unspecified",
*argv0 = strdup(argv[0]);
while ((argument = getopt(argc, argv, "f:t:o:")) != -1) {
if (argument == '?') return print_usage(argv0);
else if (argument == 'f' && optarg != NULL)
format = strdup(optarg);
else if (argument == 't' && optarg != NULL) {
if (strcmp("u-boot", optarg) &&
strcmp("acs", optarg) &&
strcmp("bl2", optarg))
return print_usage(argv0);
type = strdup(optarg);
}
else if (argument == 'o' && optarg != NULL) {
/* Check if it's a hexadecimal (prefixed by '0x') */
if (strncmp("0x", optarg, 2))
return print_usage(argv0);
acs_offset = (uint16_t)strtol(optarg, NULL, 16);
}
} argc -= optind; argv += optind;
if (argc < 1) return print_usage(argv0);
/* Open the file */
fildes = open(argv[0], O_RDONLY);
if (fildes == -1) err(errno, "can't open file");
if (!strcmp(type, "unspecified")) {
/*
* Try figuring out the binary we have if it isn't
* explicitely set.
*/
if (read(fildes, &buffer, 32) == -1)
err(errno, "%s", "can't read");
/* Check for the "@AML" magic word (in a signed u-boot.bin) */
if (buffer[16] == '@' &&
buffer[17] == 'A' &&
buffer[18] == 'M' &&
buffer[19] == 'L')
type = "u-boot";
/* Check for the "Built :" string (in acs.bin) */
else if (buffer[16] == 'B' &&
buffer[17] == 'u' &&
buffer[18] == 'i' &&
buffer[19] == 'l' &&
buffer[20] == 't' &&
buffer[21] == ' ' &&
buffer[22] == ':')
type = "acs";
/* Assume it's a BL2 binary */
else type = "bl2";
}
/* Find the offset where we have the ACS footer */
if (!strcmp(type, "u-boot")) {
if (!acs_offset) {
warnx("type is \"u-boot\" but no offset is specified, "
"using 0xa4e0");
/*
* Nit: this offset (found in a p201 u-boot.bin) is
* different from the offset in my KII Pro eMMC backup.
* TODO: Maybe automatically detect the offset?
*/
acs_offset = 0xa4e0;
}
}
else if (!strcmp(type, "bl2") && !acs_offset)
acs_offset = 0x92e0;
else if (!strcmp(type, "acs") && !acs_offset)
acs_offset = 0x330;
/* Parse the footer */
acs_t acs_footer = parse_acs_footer(fildes, acs_offset);
if (!strcmp("acs", type)) diff_offset = 0;
else errx(EINVAL, "types other than \"acs\" aren't supported yet");
/* Check versions */
if (acs_footer.version != 1 ||
acs_footer.ddrs.version != 3 ||
acs_footer.ddrt.version != 2 ||
acs_footer.pll.version != 1)
errx(0,
"one or multiple entries have an unsupported version;"
"not parsing structures"
);
/* Parse "ddrs_" */
ddrs_t ddr_settings = parse_ddrs(fildes, acs_footer, diff_offset);
/* Parse "ddrt_" */
ddrt_t ddr_timings[7]; /* "ddrt_" is an array. */
parse_ddrt(fildes, acs_footer, diff_offset, ddr_timings);
/* Parse "pll__" */
pll_t pll_settings = parse_pll(fildes, acs_footer, diff_offset);
/* Print */
if (!strcmp(format, "readable")) {
printf(
"This is a %s.bin binary.\n",
type
);
printf(
"\"acs__\":\n"
"\tVersion: 0x%x\n"
"\tSize: 0x%x\n"
"\tChip type: 0x%x\n",
acs_footer.version, acs_footer.size,
acs_footer.chip_type
);
printf(
"\"ddrs_\":\n"
"\tVersion: 0x%x\n"
"\tSize: 0x%x\n"
"\tAddress in memory: 0x%x\n",
acs_footer.ddrs.version, acs_footer.ddrs.size,
acs_footer.ddrs.address
);
printf(
"\"ddrt_\":\n"
"\tVersion: 0x%x\n"
"\tSize: 0x%x\n"
"\tAddress in memory: 0x%x\n",
acs_footer.ddrt.version, acs_footer.ddrt.size,
acs_footer.ddrt.address
);
printf(
"\"pll__\":\n"
"\tVersion: 0x%x\n"
"\tSize: 0x%x\n"
"\tAddress in memory: 0x%x\n",
acs_footer.pll.version, acs_footer.pll.size,
acs_footer.pll.address
);
} else if (!strcmp(format, "c")) {
printf(
"/**************************************"
"****************************************\n"
" * arch/arm/cpu/armv8/gxb/firmware/acs/acs.c\n"
" * Generated by ACSBaby. PLEASE DO NOT MODIFY\n"
" * Date: %s\n" /* Date */
" **************************************"
"***************************************/\n"
"#include <asm/arch/acs.h>\n"
"#include <asm/arch/timing.h>\n"
"#include \"timing.c\"\n\n"
"struct acs_setting __acs_set = {\n"
"\t.acs_magic = \"acs__\",\n"
"\t.chip_type = 0x%x,\n"
"\t.version = %d,\n"
"\t.acs_set_length = 0x%x,\n\n"
/* "ddrs_" entry */
"\t.ddr_magic = \"ddrs_\",\n"
"\t.ddr_set_version = %d,\n"
"\t.ddr_set_length = 0x%x,\n"
"\t.ddr_set_addr = (unsigned long)"
"(&__ddr_setting),\n\n"
/* "ddrt_" entry */
"\t.ddrt_magic = \"ddrt_\",\n"
"\t.ddrt_set_version = %d,\n"
"\t.ddrt_set_length = 0x%x,\n"
"\t.ddrt_set_addr = (unsigned long)"
"(&__ddr_timming),\n\n"
/* "pll__" entry */
"\t.pll_magic = \"pll__\",\n"
"\t.pll_set_version = %d,\n"
"\t.pll_set_length = 0x%x,\n"
"\t.pll_set_addr = (unsigned long)"
"(&__pll_setting),\n};\n",
get_date(),
acs_footer.chip_type,
acs_footer.version,
acs_footer.size,
acs_footer.ddrs.version,
acs_footer.ddrs.size,
acs_footer.ddrt.version,
acs_footer.ddrt.size,
acs_footer.pll.version,
acs_footer.pll.size
);
printf(
"/**************************************"
"****************************************\n"
" * board/amlogic/<board>/firmware/timing.c\n"
" * Generated by ACSBaby. PLEASE DO NOT MODIFY\n"
" * Date: %s\n" /* Date */
" **************************************"
"***************************************/\n"
"#include <asm/arch/timing.h>\n"
"#include <asm/arch/ddr_define.h>\n", get_date()
);
} else return print_usage(argv0);
print_ddr_settings(ddr_settings, format);
print_ddr_timings(ddr_timings, format);
print_pll_settings(pll_settings, format);
return 0;
}
int print_usage(char *name) {
printf(
"ACSBaby: Parse Amlogic DDR settings.\n"
"Usage: %s [-f c|readable] [-t acs|bl2|u-boot] "
"[-o offset] file\n",
name
);
return 1;
}
|