YAHAL
Yet Another Hardware Abstraction Library
Loading...
Searching...
No Matches
boot_blocks.h
1// ---------------------------------------------
2// This file is part of
3// _ _ __ _ _ __ __
4// ( \/ ) /__\ ( )_( ) /__\ ( )
5// \ / /(__)\ ) _ ( /(__)\ )(__
6// (__)(__)(__)(_) (_)(__)(__)(____)
7//
8// Yet Another HW Abstraction Library
9// Copyright (C) Andreas Terstegge
10// BSD Licensed (see file LICENSE)
11//
12// ---------------------------------------------
13//
14#ifndef _BOOT_BLOCKS_H_
15#define _BOOT_BLOCKS_H_
16
17#include <cstdint>
18#include <array>
19
20// These definitions might interfere with the following enums...
21#undef RP2040
22#undef RP2350
23
24namespace BLOCKS {
25
26 template<std::size_t Size>
27 using blocks = std::array<uint32_t, Size>;
28
29 constexpr auto copy(const uint32_t *start, const uint32_t *end, uint32_t *dst) {
30 while (start < end) {
31 *dst++ = *start++;
32 }
33 }
34
35 template<std::size_t N>
36 constexpr auto append(const blocks<N> &arr, const uint32_t val) {
37 blocks<N + 1> res{};
38 copy(arr.begin(), arr.end(), res.begin());
39 res[N] = val;
40 return res;
41 }
42
43 // Specific block methods and enums
44
45 template<std::size_t N>
46 constexpr auto HEADER(const blocks<N> &arr) {
47 return append(arr, 0xffffded3);
48 }
49
50 template<std::size_t N>
51 constexpr auto FOOTER(const blocks<N> &arr) {
52 return append(arr, 0xab123579);
53 }
54
55 template<std::size_t N>
56 constexpr auto LAST_ITEM(const blocks<N> &arr, uint16_t other_items_size) {
57 return append(arr, 0x000000ff + (other_items_size << 8));
58 }
59
60 template<std::size_t N>
61 constexpr auto LINK(const blocks<N> &arr, int offset) {
62 return append(arr, offset);
63 }
64
65
66 template<std::size_t N>
67 constexpr auto VERSION(const blocks<N> &arr, uint16_t major, uint16_t minor) {
68 auto tmp = append(arr, 0x00000248);
69 return append(tmp, ((major << 16) | minor));
70 }
71
72
73 enum class image_type : uint32_t {
74 INVALID = 0 << 16,
75 TYPE_EXE = 1 << 16,
76 TYPE_DATA = 2 << 16
77 };
78
79 enum class exe_security : uint32_t {
80 UNSPECIFIED = 0u << 20,
81 SEC_NS = 1u << 20,
82 SEC_S = 2u << 20
83 };
84
85 enum class exe_cpu : uint32_t {
86 CPU_ARM = 0u << 24,
87 CPU_RISCV = 1u << 24
88 };
89
90 enum class exe_chip : uint32_t {
91 CHIP_RP2040 = 0u << 28,
92 CHIP_RP2350 = 1u << 28
93 };
94
95 enum class exe_tbyb : uint32_t {
96 FALSE = 0u << 31,
97 TRUE = 1u << 31
98 };
99
100 template<std::size_t N>
101 constexpr auto IMAGE_DEF(const blocks<N> &arr, const image_type type,
102 const exe_security sec,
103 const exe_cpu cpu,
104 const exe_chip chip,
105 const exe_tbyb tbyb = exe_tbyb::FALSE) {
106 return append(arr, 0x00000142 + (uint32_t) type + (uint32_t) sec +
107 (uint32_t) cpu + (uint32_t) chip +
108 (uint32_t) tbyb);
109 }
110
111};
112
113#endif // _BOOT_BLOCKS_H_