blob: 7507595fbd2a4b1538fad1ec3f913ba85862bb6f [file] [log] [blame]
Fabio Utzig12d59162019-11-28 10:01:59 -03001/*
2 * Copyright (c) 2019 JUUL Labs
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef H_SWAP_PRIV_
18#define H_SWAP_PRIV_
19
20#include "mcuboot_config/mcuboot_config.h"
21
22#if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
23
24/**
25 * Calculates the amount of space required to store the trailer, and erases
26 * all sectors required for this storage in the given flash_area.
27 */
28int swap_erase_trailer_sectors(const struct boot_loader_state *state,
29 const struct flash_area *fap);
30
31/**
32 * Initialize the given flash_area with the metadata required to start a new
33 * swap upgrade.
34 */
35int swap_status_init(const struct boot_loader_state *state,
36 const struct flash_area *fap,
37 const struct boot_status *bs);
38
39/**
40 * Tries to locate an interrupted swap status (metadata). If not metadata
41 * was found returns BOOT_STATUS_SOURCE_NONE.
42 *
43 * Must return one of:
44 * - BOOT_STATUS_SOURCE_NONE
45 * - BOOT_STATUS_SOURCE_SCRATCH
46 * - BOOT_STATUS_SOURCE_PRIMARY_SLOT
47 */
48int swap_status_source(struct boot_loader_state *state);
49
50/**
51 * Reads the boot status from the flash. The boot status contains
52 * the current state of an interrupted image copy operation. If the boot
53 * status is not present, or it indicates that previous copy finished,
54 * there is no operation in progress.
55 */
56int swap_read_status(struct boot_loader_state *state, struct boot_status *bs);
57
58/**
59 * Iterate over the swap status bytes in the given flash_area and populate
60 * the given boot_status with the calculated index where a swap upgrade was
61 * interrupted.
62 */
63int swap_read_status_bytes(const struct flash_area *fap,
64 struct boot_loader_state *state,
65 struct boot_status *bs);
66
67/**
68 * Marks the image in the primary slot as fully copied.
69 */
70int swap_set_copy_done(uint8_t image_index);
71
72/**
73 * Marks a reverted image in the primary slot as confirmed. This is necessary to
74 * ensure the status bytes from the image revert operation don't get processed
75 * on a subsequent boot.
76 *
77 * NOTE: image_ok is tested before writing because if there's a valid permanent
78 * image installed on the primary slot and the new image to be upgrade to has a
79 * bad sig, image_ok would be overwritten.
80 */
81int swap_set_image_ok(uint8_t image_index);
82
83/**
84 * Start a new or resume an interrupted swap according to the parameters
85 * found in the given boot_status.
86 */
87void swap_run(struct boot_loader_state *state,
88 struct boot_status *bs,
89 uint32_t copy_size);
90
91#if MCUBOOT_SWAP_USING_SCRATCH
92#define BOOT_SCRATCH_AREA(state) ((state)->scratch.area)
93
94static inline size_t boot_scratch_area_size(const struct boot_loader_state *state)
95{
96 return BOOT_SCRATCH_AREA(state)->fa_size;
97}
98#endif
99
100#endif /* defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE) */
101
102#endif /* H_SWAP_PRIV_ */