File: dram.c

package info (click to toggle)
crust-firmware 0.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,900 kB
  • sloc: ansic: 19,341; yacc: 596; lex: 479; makefile: 334; asm: 215; sh: 136; python: 42
file content (45 lines) | stat: -rw-r--r-- 878 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright © 2021-2022 The Crust Firmware Authors.
 * SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-only
 */

#include <debug.h>
#include <mmio.h>
#include <steps.h>
#include <util.h>

#define DRAM_BASE  0x40000000

#define MIN_OFFSET sizeof(uint32_t)
#define MAX_OFFSET BIT(26) /* 64 MiB */

static uint32_t saved_checksum;

static uint32_t
dram_calc_checksum(void)
{
	uint32_t checksum = 0;

	for (uint32_t offset = MIN_OFFSET; offset < MAX_OFFSET; offset <<= 1) {
		checksum += mmio_read_32(DRAM_BASE + 1 * offset);
		checksum += mmio_read_32(DRAM_BASE + 3 * offset);
		checksum += 1;
		checksum *= ~offset;
	}

	return checksum;
}

void
dram_save_checksum(void)
{
	saved_checksum = dram_calc_checksum();
}

void
dram_verify_checksum(void)
{
	record_step(STEP_RESUME_DRAM_CHECKSUM);
	if (dram_calc_checksum() != saved_checksum)
		panic("DRAM checksum mismatch!");
}