~nabijaczleweli/klapki

ref: bfaaf3b0ee87d924825987cdd6449a38d6d9627e klapki/src/state.hpp -rw-r--r-- 5.8 KiB
bfaaf3b0наб Sort our boot entries. Handle KLAPKI_EFI_ROOT better maybe? 1 year, 26 days ago
                                                                                
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// The MIT License (MIT)

// Copyright (c) 2020 наб <nabijaczleweli@nabijaczleweli.xyz>

// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


#pragma once


#include "util.hpp"
#include <array>
#include <cstdint>
#include <fmt/format.h>
#include <map>
#include <string>
#include <variant>
#include <vector>

namespace klapki {
	struct config;
}


namespace klapki::state {
	using sha_t             = std::uint8_t[20];
	using shaa_t            = std::array<std::uint8_t, 20>;
	using nonbase_dirname_t = std::optional<std::string>;  // Use SUB to copy from previous entry

	struct boot_order_flat {
		std::shared_ptr<std::uint16_t[]> order;
		std::size_t order_cnt;
	};
	struct boot_order_structured {
		/// [chunk, ours?]
		std::vector<std::pair<std::vector<std::uint16_t>, bool>> order;
	};
	using boot_order_t = std::variant<boot_order_flat, boot_order_structured>;

	struct boot_entry {
		std::shared_ptr<std::uint8_t[]> load_option;
		std::size_t load_option_len;
		sha_t load_option_sha;

		std::uint32_t attributes;
	};

	struct stated_config_entry {
		/// Hint initially, then pointer
		std::uint16_t bootnum_hint;  // big-endian
		sha_t load_option_sha;
		std::string version;         // NUL-terminated
		std::string variant;         // NUL-terminated
		std::string kernel_dirname;  // on rootfs; NUL-terminated
		sha_t kernel_image_sha;
		std::vector<std::pair<nonbase_dirname_t, shaa_t>> initrd_dirnames;  // on rootfs; entries NUL-terminated; list empty-terminated w/o SHA


		bool operator==(const stated_config_entry & other) const noexcept;
	};

	struct stated_config {
		std::uint16_t boot_position;        // big-endian
		std::vector<std::string> variants;  // entries NUL-terminated; list empty-terminated; kept unique, order-preserving
		std::vector<stated_config_entry> wanted_entries;

		/// 0 on OK, errno on error
		static int parse(stated_config & into, const void * data, std::size_t size);

		std::vector<std::uint8_t> serialise() const;


		bool operator==(const stated_config & other) const noexcept;
		inline bool operator!=(const stated_config & other) const noexcept { return !(*this == other); }
	};

	struct state {
		boot_order_t order;
		std::map<std::uint16_t, boot_entry> entries;
		stated_config statecfg;

		std::optional<std::string> commit(std::string_view us, const state & original_state) const;

		static std::variant<state, std::string> load(const char * argv0, std::string_view us);
	};
}


template <>
struct fmt::formatter<klapki::state::boot_order_t> {
	template <class OItr>
	static OItr write(OItr out, const std::uint16_t * first, std::size_t cnt) {
		for(; cnt--; ++first) {
			out = format_to(out, "{:04X}", *first);
			if(cnt != 0)
				*out++ = ',';
		}
		return out;
	}

	constexpr auto parse(format_parse_context & ctx) { return ctx.begin(); }

	template <typename FormatContext>
	auto format(const klapki::state::boot_order_t & bord, FormatContext & ctx) {
		auto out = ctx.out();

		std::visit(klapki::overload{
		               [&](const klapki::state::boot_order_flat & bord) { out = write(out, bord.order.get(), bord.order_cnt); },
		               [&](const klapki::state::boot_order_structured & bord) {
			               bool first = true;
			               for(auto && el : bord.order) {
				               if(!first)
					               *out++ = ',';
				               else
					               first = false;

				               *out++ = el.second ? '{' : '[';
				               out    = write(out, el.first.data(), el.first.size());
				               *out++ = el.second ? '}' : ']';
			               }
		               },
		           },
		           bord);

		return out;
	}
};

template <>
struct fmt::formatter<klapki::state::stated_config_entry> {
	constexpr auto parse(format_parse_context & ctx) { return ctx.begin(); }

	template <typename FormatContext>
	auto format(const klapki::state::stated_config_entry & ent, FormatContext & ctx) {
		auto out = ctx.out();

		out = format_to(out, "{{ {:04X}, \"{}\", {}{}{}, \"{}\", [",                                                                                          //
		                ent.bootnum_hint,                                                                                                                     //
		                ent.version,                                                                                                                          //
		                ent.variant.empty() ? "" : "\"", ent.variant.empty() ? "(default)" : std::string_view{ent.variant}, ent.variant.empty() ? "" : "\"",  //
		                ent.kernel_dirname);

		bool first = true;
		for(auto && [el, _] : ent.initrd_dirnames) {
			if(!first)
				out = format_to(out, ", ");
			else
				first = false;

			if(el)
				*out++ = '\"';
			out = format_to(out, "{}", el ? std::string_view{*el} : "←");
			if(el)
				*out++ = '\"';
		}

		out = format_to(out, "] }}");

		return out;
	}
};