image_toc.c
3.8 KB
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
/****************************************************************************
*
* Copyright (c) 2020 Technology Innovation Institute. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#include "hw_config.h"
#ifdef BOOTLOADER_USE_TOC
#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include "image_toc.h"
#include "bl.h"
/* Helper macros to define flash start and end addresses, based on info from
* hw_config.h
*/
#define FLASH_START_ADDRESS (APP_LOAD_ADDRESS & (~(BOARD_FLASH_SIZE - 1)))
#define FLASH_END_ADDRESS (FLASH_START_ADDRESS + BOARD_FLASH_SIZE)
bool find_toc(const image_toc_entry_t **toc_entries, uint8_t *len)
{
const uintptr_t toc_start_u32 = APP_LOAD_ADDRESS + BOOT_DELAY_ADDRESS + 8;
const image_toc_start_t *toc_start = (const image_toc_start_t *)toc_start_u32;
const image_toc_entry_t *entry = (const image_toc_entry_t *)(toc_start_u32 + sizeof(image_toc_start_t));
int i = 0;
uint8_t sig_idx;
const uint32_t toc_end_magic = TOC_END_MAGIC;
if (toc_start->magic == TOC_START_MAGIC &&
toc_start->version <= TOC_VERSION) {
/* Count the entries in TOC */
while (i < MAX_TOC_ENTRIES &&
(uintptr_t)&entry[i] <= FLASH_END_ADDRESS - sizeof(uintptr_t) &&
memcmp(&entry[i], &toc_end_magic, sizeof(toc_end_magic))) {
i++;
}
/* The number of toc entries found must be within bounds, and the
* application has to lie within the flashable area. Also ensure that
* end > start.
*/
if (i <= MAX_TOC_ENTRIES && i > 0 &&
(uintptr_t)entry[0].start == APP_LOAD_ADDRESS &&
(uintptr_t)entry[0].end <= (FLASH_END_ADDRESS - sizeof(uintptr_t)) &&
(uintptr_t)entry[0].end > (uintptr_t)entry[0].start) {
sig_idx = entry[0].signature_idx;
/* The signature idx for the first app must be within the TOC, and
* the signature must be within the flash area, not overlapping the
* app. Also ensure that end > start.
*/
if (sig_idx > 0 &&
sig_idx < MAX_TOC_ENTRIES &&
(uintptr_t)entry[sig_idx].start >= (uintptr_t)entry[0].end &&
(uintptr_t)entry[sig_idx].end <= FLASH_END_ADDRESS &&
(uintptr_t)entry[sig_idx].end > (uintptr_t)entry[sig_idx].start) {
*toc_entries = entry;
*len = i;
return true;
}
}
}
*toc_entries = NULL;
*len = 0;
return false;
}
#endif