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 template<std::size_t N, std::size_t M>
44 constexpr auto append_N(const blocks<N> &arr, const blocks<M> &to_append) {
45 blocks<N + M> res{};
46 copy(arr.begin(), arr.end(), res.begin());
47 copy(to_append.begin(), to_append.end(), res.at(N));
48 return res;
49 }
50
51
52 // Specific block methods and enums
53
54 template<std::size_t N>
55 constexpr auto HEADER(const blocks<N> &arr) {
56 return append(arr, 0xffffded3);
57 }
58
59 template<std::size_t N>
60 constexpr auto FOOTER(const blocks<N> &arr) {
61 return append(arr, 0xab123579);
62 }
63
64 template<std::size_t N>
65 constexpr auto LAST_ITEM(const blocks<N> &arr, uint16_t other_items_size) {
66 return append(arr, 0x000000ff + (other_items_size << 8));
67 }
68
69 template<std::size_t N>
70 constexpr auto LINK(const blocks<N> &arr, int offset) {
71 return append(arr, offset);
72 }
73
74
75 template<std::size_t N>
76 constexpr auto VERSION(const blocks<N> &arr, uint16_t major, uint16_t minor) {
77 auto tmp = append(arr, 0x00000248);
78 return append(tmp, ((major << 16) | minor));
79 }
80
81
82 enum class image_type : uint32_t {
83 INVALID = 0 << 16,
84 TYPE_EXE = 1 << 16,
85 TYPE_DATA = 2 << 16
86 };
87
88 enum class exe_security : uint32_t {
89 UNSPECIFIED = 0u << 20,
90 SEC_NS = 1u << 20,
91 SEC_S = 2u << 20
92 };
93
94 enum class exe_cpu : uint32_t {
95 CPU_ARM = 0u << 24,
96 CPU_RISCV = 1u << 24
97 };
98
99 enum class exe_chip : uint32_t {
100 CHIP_RP2040 = 0u << 28,
101 CHIP_RP2350 = 1u << 28
102 };
103
104 enum class exe_tbyb : uint32_t {
105 FALSE = 0u << 31,
106 TRUE = 1u << 31
107 };
108
109 template<std::size_t N>
110 constexpr auto IMAGE_DEF(const blocks<N> &arr, const image_type type,
111 const exe_security sec,
112 const exe_cpu cpu,
113 const exe_chip chip,
114 const exe_tbyb tbyb = exe_tbyb::FALSE) {
115 return append(arr, 0x00000142 +
116 (uint32_t) type + (uint32_t) sec + (uint32_t) cpu +
117 (uint32_t) chip + (uint32_t) tbyb);
118 }
119
120 template<std::size_t N>
121 constexpr auto LOAD_MAP_ABS(const blocks<N> &arr,
122 const uint32_t storage_start,
123 const uint32_t runtime_start,
124 const uint32_t storage_end) {
125 auto a1 = append(arr, 0x81000406);
126 auto a2 = append(a1, storage_start);
127 auto a3 = append(a2, runtime_start);
128 auto a4 = append(a3, storage_end );
129 return a4;
130 }
131
132};
133
134#endif // _BOOT_BLOCKS_H_