~funderscore blog cgit wiki get in touch
aboutsummaryrefslogtreecommitdiff
blob: b57293250fb71f9846de190abf507b3ce6e3cac9 (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * Copyright (C) 2023, Ferass El Hafidi <vitali64pmemail@protonmail.com>
 */
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <err.h>
#include <stdlib.h>

#include "acsbaby.h"
#include "parse.h"
#include "timing.h"

acs_t parse_acs_footer(const int fildes, const uint16_t acs_offset) {
	acs_t acs_footer;
	unsigned char buffer[16];

	/* Seek to the ACS footer */
	if (lseek(fildes, acs_offset, SEEK_SET) == -1)
		err(errno, "%s", "can't seek to ACS footer");

	/* Read an entry */
	if (read(fildes, &buffer, 16) == -1) /* Each entry is 16 bytes long */
		err(errno, "%s", "can't read ACS footer");
	
	/* Check for the "acs__" magic word */
	if (strncmp((char *)buffer, "acs__", 5))
		errx(EINVAL, "%s", "invalid file");
	
	/* Parse the "acs__" entry */
	acs_footer.chip_type = (uint8_t)buffer[5];
	/*
	 * The version is actually 2 bytes long but since it's never going to 
	 * exceed 1 byte we just parse 1 byte.
	 */
	acs_footer.version = (uint8_t)buffer[6];
	acs_footer.size = (uint16_t)((buffer[8] << 2) + buffer[9]);

	/* Read all other entries */
	for (int i = 1; i <= 3; i++) {
		/* Seek to the next entry */
		if (lseek(fildes, acs_offset + i * 0x10, SEEK_SET) == -1)
			err(errno, "%s", "can't seek to next ACS entry");
		if (read(fildes, &buffer, 16) == -1)
			err(errno, "%s", "can't read ACS entry");

		/*
		 * This is where we hit into complex bitwise usage.
		 * That's normal; we're just parsing addresses and sizes as 
		 * `uint16_t`s and `uint32_t`s but the acs.bin is little 
		 * endian (which means that the bytes are stored in 
		 * reverse-byte sequence) so we need to reverse back to have 
		 * the right byte sequence.
		 */
		if (!strncmp((char *)buffer, "ddrs_", 5)) {
			acs_footer.ddrs.version = (uint8_t)buffer[5];
			acs_footer.ddrs.size    = (uint16_t)((buffer[7] << 8) 
				+ buffer[6]);
			acs_footer.ddrs.address = (uint32_t)((((buffer[11] << 8) +
				buffer[10]) << 16) + 
				((buffer[9] << 8) + buffer[8]));
		}
		else if (!strncmp((char *)buffer, "ddrt_", 5)) {
			acs_footer.ddrt.version = (uint8_t)buffer[5];
			acs_footer.ddrt.size    = (uint16_t)((buffer[7] << 8) 
				+ buffer[6]);
			acs_footer.ddrt.address = (uint32_t)(((
				(buffer[11] << 8) + buffer[10]) << 16) + 
				((buffer[9] << 8) + buffer[8]));
		}
		else if (!strncmp((char *)buffer, "pll__", 5)) {
			acs_footer.pll.version = (uint8_t)buffer[5];
			acs_footer.pll.size    = (uint16_t)((buffer[7] << 8) | 
				(buffer[6] & 0xFF));
			acs_footer.pll.address = (uint32_t)((((buffer[11] << 8) 
				+ buffer[10]) << 16) + ((buffer[9] << 8) + 
				buffer[8]));
		}
		else	errx(EINVAL, "%s", "no valid ACS entry found");
	}
	return acs_footer;
}

ddrs_t parse_ddrs(const int fildes, const acs_t acs_footer, 
		const uint16_t diff_offset) {
	uint16_t offset = acs_footer.ddrs.address - diff_offset;
	ddrs_t ddr_settings;

	/* Seek to the ddrs_ structure */
	if (lseek(fildes, offset, SEEK_SET) == -1)
		err(errno, "%s", "can't seek to ddrs_ structure");

	/* Read */
	read(fildes, &ddr_settings, acs_footer.ddrs.size);

	return ddr_settings;
}

void parse_ddrt(const int fildes, const acs_t acs_footer, 
		const uint16_t diff_offset, ddrt_t ddr_timings[7]) {
	uint16_t offset = acs_footer.ddrt.address - diff_offset;

	/* Seek to the ddrt_ structure */
	if (lseek(fildes, offset, SEEK_SET) == -1)
		err(errno, "%s", "can't seek to ddrt_ structure");

	/* Read */
	for (int i = 0; i < 7; i++) {
		read(fildes, &ddr_timings[i], acs_footer.ddrt.size / 7);
	}
}

pll_t parse_pll(const int fildes, const acs_t acs_footer, 
		const uint16_t diff_offset) {
	uint16_t offset = acs_footer.pll.address - diff_offset;
	pll_t pll_settings;

	/* Seek to the ddrt_ structure */
	if (lseek(fildes, offset, SEEK_SET) == -1)
		err(errno, "%s", "can't seek to pll__ structure");
	
	read(fildes, &pll_settings, acs_footer.pll.size);

	return pll_settings;
}