~nabijaczleweli/klapki

ref: cb46a59c26acba4f7b96ec175bc2175034628b88 klapki/src/context_derive.cpp -rw-r--r-- 5.9 KiB
cb46a59cнаб Fold into TRY_OPT() in main(). Don't try to LTO on GCC 1 year, 30 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
// 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.


#include "context.hpp"
#include <fmt/format.h>
#include <string_view>
extern "C" {
#include <efivar/efiboot.h>
#include <efivar/efivar-dp.h>
#include <ucs2.h>
}


klapki::context::context klapki::context::context::derive(state::state & output_state) {
	context ret{};

	for(auto && went : output_state.statecfg.wanted_entries)
		if(auto bent = output_state.entries.find(went.bootnum_hint); bent != std::end(output_state.entries)) {
			our_kernel kern{};

			const auto efi_opt        = reinterpret_cast<efi_load_option *>(bent->second.load_option.get());
			const ssize_t efi_opt_len = bent->second.load_option_len;
			if(!efi_loadopt_is_valid(efi_opt, efi_opt_len)) {
				fmt::print(stderr, "Entry {:04X} not a valid EFI Load Option. Dropping.\n", went.bootnum_hint);
				// TODO: drop this from the state entirely
				continue;
			}

			const auto dp = efi_loadopt_path(efi_opt, efi_opt_len);
			if(dp->type != EFIDP_MEDIA_TYPE) {
				fmt::print(stderr, "Entry {:04X} not Media Device Path. Dropping.\n", went.bootnum_hint);
				// TODO: drop this from the state entirely
				continue;
			}
			if(dp->subtype != EFIDP_MEDIA_HD) {
				fmt::print(stderr, "Entry {:04X} not Media Device Path HD. Dropping.\n", went.bootnum_hint);
				// TODO: support this maybe?
				continue;
			}

			{
				std::string pp(256, '\0');
				efidp_format_device_path(pp.data(), pp.size(), dp, efi_opt_len);
				fmt::print("Entry {:04X}: {}\n", pp.c_str());  // verbose
			}

			kern.device = dp->hd;

			// We're assuming all we have is HD+file for now, and dog help me
			const efidp_data * file;
			switch(efidp_next_node(dp, &file)) {
				case -1:
					fmt::print(stderr, "Entry {:04X}: second (file) node: {}. Dropping.\n", went.bootnum_hint, strerror(errno));
					// TODO: drop this from the state entirely
					continue;
				case 0:
					fmt::print(stderr, "Entry {:04X}: second (file) node doesn't exist. Dropping.\n", went.bootnum_hint);
					// TODO: drop this from the state entirely
					continue;
				case 1: {
					if(dp->type != EFIDP_MEDIA_TYPE) {
						fmt::print(stderr, "Entry {:04X} file not Media Device Path. Dropping.\n", went.bootnum_hint);
						// TODO: drop this from the state entirely
						continue;
					}
					if(dp->subtype != EFIDP_MEDIA_FILE) {
						fmt::print(stderr, "Entry {:04X} file not Media Device Path File. Dropping.\n", went.bootnum_hint);
						// TODO: drop this from the state entirely
						continue;
					}

					const std::unique_ptr<char[], free_deleter> path_c{reinterpret_cast<char *>(ucs2_to_utf8(reinterpret_cast<char16_t *>(dp->file.name), dp->length))};
					std::string_view path{path_c.get()};
					if(const auto last_backslash = path.rfind('\\'); last_backslash != std::string::npos)
						kern.image_path = std::pair{path.substr(0, last_backslash + 1), path.substr(last_backslash + 1)};
					else {
						fmt::print(stderr, "Entry {:04X}: kernel image {}: no backslashes? Relating to root.\n", went.bootnum_hint, path);
						kern.image_path = std::pair{"\\", path};
					}
				} break;
				default:
					__builtin_unreachable();
			}

			// TODO: verify this is the last path entry maybe

			fmt::print("Entry {:04X}: [\"{}\", \"{}\"]\n", kern.image_path.first, kern.image_path.second);  // verbose

			kern.description = reinterpret_cast<const char *>(efi_loadopt_desc(efi_opt, efi_opt_len));
			fmt::print("Entry {:04X}: {}\n", kern.description);  // verbose

			unsigned char * opt_data;
			size_t opt_data_len;
			efi_loadopt_optional_data(efi_opt, efi_opt_len, &opt_data, &opt_data_len);
			detail::tokenise_cmdline({reinterpret_cast<char *>(opt_data), opt_data_len}, [&](auto && arg) {
				if(!arg.compare(0, std::strlen("initrd="), "initrd=")) {
					auto initrd_path = arg.substr(std::strlen("initrd="));
					if(const auto last_backslash = initrd_path.rfind('\\'); last_backslash != std::string::npos)
						kern.initrd_paths.emplace_back(initrd_path.substr(0, last_backslash + 1), initrd_path.substr(last_backslash + 1));
					else {
						fmt::print(stderr, "Entry {:04X}: initrd={}: no backslashes? Relating to root.\n", went.bootnum_hint, initrd_path);
						kern.initrd_paths.emplace_back("\\", initrd_path);
					}

				} else {
					// TODO: this might mangle ws
					kern.cmdline += arg;
					kern.cmdline += ' ';
				}

				return true;
			});
			if(!kern.cmdline.empty())
				kern.cmdline.pop_back();  // extra space

			ret.our_kernels.emplace(went.bootnum_hint, std::move(kern));
		} else
			throw __func__;  // unreachable, by this point all wanted entries match up to boot entries

	for(auto && went : output_state.statecfg.wanted_entries)
		if(auto bent = output_state.entries.find(went.bootnum_hint); bent != std::end(output_state.entries))
			ret.kernel_versions.push_back(went.version);
		else
			throw __func__;  // unreachable, by this point all wanted entries match up to boot entries

	return ret;
}