summaryrefslogtreecommitdiff
path: root/chip/stm32/dfu_bootmanager_shared.c
blob: 212ee0a9e99c91fb07d063c45c64fde98bb10c87 (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
/* Copyright 2022 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * DFU Boot Manager shared utilities for STM32
 */

#include "bkpdata.h"
#include "clock.h"
#include "dfu_bootmanager_shared.h"
#include "flash.h"
#include "registers.h"
#include "task.h"

/*
 * The Servo platforms do not have any free backup regions. The scratchpad
 * is only with the console command scratchpad and on some of the tests so
 * we'll use the scratchpad region.
 */
#ifdef CONFIG_CMD_SCRATCHPAD
#error "The scratchpad is used, define a backup region for the DFU fields."
#endif /* CONFIG_CMD_SCRATCHPAD */

int dfu_bootmanager_enter_dfu(void)
{
	dfu_bootmanager_backup_write(DFU_BOOTMANAGER_VALUE_DFU);
	system_reset(0);
	return 0;
}

void dfu_bootmanager_clear(void)
{
	dfu_bootmanager_backup_write(DFU_BOOTMANAGER_VALUE_CLEAR);
}

void dfu_bootmanager_backup_write(uint8_t value)
{
	uint16_t data = DFU_BOOTMANAGER_VALID_CHECK | value;

	bkpdata_write(BKPDATA_INDEX_SCRATCHPAD, data);
}

int dfu_bootmanager_backup_read(uint8_t *value)
{
	uint16_t data = bkpdata_read(BKPDATA_INDEX_SCRATCHPAD);
	uint16_t valid_check = data & DFU_BOOTMANAGER_VALID_MASK;

	*value = (uint8_t)(data & DFU_BOOTMANAGER_VALUE_MASK);
	if (valid_check != DFU_BOOTMANAGER_VALID_CHECK)
		return EC_ERROR_UNKNOWN;
	return EC_SUCCESS;
}