Debian Codegraph @

src/mpt/osinfo/dos_version.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_OSINFO_DOS_VERSION_HPP
4 #define MPT_OSINFO_DOS_VERSION_HPP
5
6
7
8 #include "mpt/base/detect.hpp"
9 #include "mpt/base/integer.hpp"
10 #include "mpt/base/namespace.hpp"
11 #include "mpt/string/buffer.hpp"
12
13 #include <string>
14
15 #if MPT_OS_DJGPP
16 #include <dos.h>
17 #include <dpmi.h>
18 #include <errno.h>
19 #include <go32.h>
20 #include <sys/farptr.h>
21 #endif // MPT_OS_DJGPP
22
23
24
25 namespace mpt {
26 inline namespace MPT_INLINE_NS {
27
28
29
30 namespace osinfo {
31
32 namespace dos {
33
34
35
36 class Version {
37
38 public:
39
40 enum class Host {
41 DOS,
42 Win2,
43 Win3,
44 Win95,
45 Win98,
46 WinME,
47 WinNT,
48 OS2,
49 OS2Warp,
50 DOSEmu,
51 };
52
53 struct System {
54 uint8 Major = 0;
55 uint8 Minor = 0;
56 constexpr System() noexcept
57 : Major(0)
58 , Minor(0) {
59 }
60 explicit constexpr System(uint16 number) noexcept
61 : Major(static_cast<uint8>((number >> 8) & 0xffu))
62 , Minor(static_cast<uint8>((number >> 0) & 0xffu)) {
63 }
64 explicit constexpr System(uint8 major, uint8 minor) noexcept
65 : Major(major)
66 , Minor(minor) {
67 }
68 constexpr operator uint16() const noexcept {
69 return (static_cast<uint16>(Major) << 8) | (static_cast<uint16>(Minor) << 0);
70 }
71 };
72
73 struct DPMI {
74 uint8 Major = 0;
75 uint8 Minor = 0;
76 constexpr DPMI() noexcept
77 : Major(0)
78 , Minor(0) {
79 }
80 explicit constexpr DPMI(uint8 major, uint8 minor) noexcept
81 : Major(major)
82 , Minor(minor) {
83 }
84 };
85
86 private:
87 bool m_SystemIsDos = false;
88
89 std::string m_OEM{};
90 System m_System;
91 System m_SystemEmulated;
92
93 DPMI m_DPMI;
94 std::string m_DPMIVendor{};
95 DPMI m_DPMIHost;
96
97 Host m_Host = Host::DOS;
98 int m_HostVersion = 0;
99 int m_HostRevision = 0;
100 int m_HostPatch = 0;
101 bool m_HostMultitasking = false;
102 bool m_HostFixedTimer = false;
103
104 std::string m_BIOSDate{};
105
106 private:
107 Version() {
108 return;
109 }
110
111 public:
112 static Version NoDos() {
113 return Version();
114 }
115
116 Version(std::string oem, System version, System version_emulated, DPMI dpmi, std::string dpmi_vendor, DPMI dpmi_host, Host host, int host_version, int host_revision, int host_patch, bool multitasking, bool fixedtimer, std::string bios_date)
117 : m_SystemIsDos(true)
118 , m_OEM(oem)
119 , m_System(version)
120 , m_SystemEmulated(version_emulated)
121 , m_DPMI(dpmi)
122 , m_DPMIVendor(dpmi_vendor)
123 , m_DPMIHost(dpmi_host)
124 , m_Host(host)
125 , m_HostVersion(host_version)
126 , m_HostRevision(host_revision)
127 , m_HostPatch(host_patch)
128 , m_HostMultitasking(multitasking)
129 , m_HostFixedTimer(fixedtimer)
130 , m_BIOSDate(bios_date) {
131 return;
132 }
133
134 public:
135
136 #if MPT_OS_DJGPP
137 static mpt::osinfo::dos::Version GatherDosVersion() {
138 uint16 dos_version = _get_dos_version(0);
139 uint16 dos_version_emulated = dos_version;
140 if (dos_version >= 0x0500) {
141 dos_version = _get_dos_version(1);
142 }
143 uint8 dpmi_version_major = 0;
144 uint8 dpmi_version_minor = 0;
145 __dpmi_version_ret dpmi_version{};
146 if (__dpmi_get_version(&dpmi_version) == 0) {
147 dpmi_version_major = dpmi_version.major;
148 dpmi_version_minor = dpmi_version.minor;
149 }
150 std::string dpmi_host_vendor;
151 uint8 dpmi_host_major = 0;
152 uint8 dpmi_host_minor = 0;
153 /* if (dpmi_version_major >= 1) */ {
154 int dpmi_flags = 0;
155 std::array<char, 128> dpmi_vendor = {};
156 if (__dpmi_get_capabilities(&dpmi_flags, dpmi_vendor.data()) == 0) {
157 char buf[126] = {};
158 std::memcpy(buf, dpmi_vendor.data() + 2, 126);
159 dpmi_host_vendor = mpt::ReadAutoBuf(buf);
160 dpmi_host_major = dpmi_vendor[0];
161 dpmi_host_minor = dpmi_vendor[1];
162 }
163 }
164 if (dpmi_host_vendor.empty()) {
165 dpmi_host_vendor = "unknown";
166 }
167 bool detected = false;
168 Host host = Host::DOS;
169 int host_version = 0;
170 int host_revision = 0;
171 int host_patch = 0;
172 bool host_multitasking = false;
173 bool host_fixedtimer = false;
174 if (!detected) {
175 char * os = std::getenv("OS");
176 if (os) {
177 if (std::string(os) == std::string("Windows_NT")) {
178 host = Host::WinNT;
179 host_version = 0;
180 host_revision = 0;
181 host_patch = 0;
182 host_multitasking = true;
183 host_fixedtimer = true;
184 detected = true;
185 }
186 }
187 }
188 if (!detected) {
189 __dpmi_regs r{};
190 r.x.ax = 0x1600;
191 __dpmi_int(0x2f, &r);
192 if ((r.h.al == 0x01) || (r.h.al == 0xFF)) {
193 host = Host::Win2;
194 host_version = 2;
195 host_revision = 0;
196 host_patch = 0;
197 host_multitasking = true;
198 host_fixedtimer = true;
199 detected = true;
200 } else if ((r.h.al != 0x00) && (r.h.al != 0x01) && (r.h.al != 0x80) && (r.h.al != 0xFF)) {
201 host_version = r.h.al;
202 host_revision = r.h.ah;
203 host_patch = 0;
204 if (host_version > 4) {
205 host = Host::WinME;
206 } else if ((host_version == 4) && (host_revision >= 90)) {
207 host = Host::WinME;
208 } else if ((host_version == 4) && (host_revision >= 10)) {
209 host = Host::Win98;
210 } else if (host_version == 4) {
211 host = Host::Win95;
212 } else {
213 host = Host::Win3;
214 }
215 host_multitasking = true;
216 host_fixedtimer = true;
217 detected = true;
218 }
219 }
220 if (!detected) {
221 __dpmi_regs r{};
222 r.x.ax = 0x4010;
223 __dpmi_int(0x2f, &r);
224 if (r.x.ax != 0x4010) {
225 if (r.x.ax == 0x0000u) {
226 host = Host::OS2Warp;
227 } else {
228 host = Host::OS2;
229 }
230 host_version = r.h.bh;
231 host_revision = r.h.bl;
232 host_patch = 0;
233 host_multitasking = true;
234 host_fixedtimer = true;
235 detected = true;
236 }
237 }
238 if (!detected) {
239 char buf[9] = {};
240 for (uint32 i = 0; i < 8; ++i) {
241 buf[i] = _farpeekb(_dos_ds, (0xF000u * 16) + (0xfff5u + i));
242 }
243 buf[8] = '\0';
244 if (std::string(mpt::ReadAutoBuf(buf)) == std::string("02/25/93")) {
245 __dpmi_regs r{};
246 r.x.ax = 0;
247 __dpmi_int(0xe6, &r);
248 if (r.x.ax == 0xaa55u) {
249 host = Host::DOSEmu;
250 host_version = r.h.bh;
251 host_revision = r.h.bl;
252 host_patch = r.x.cx;
253 host_multitasking = true;
254 host_fixedtimer = true;
255 detected = true;
256 }
257 }
258 }
259 if (!detected) {
260 __dpmi_regs r{};
261 r.x.ax = 0x3000;
262 __dpmi_int(0x21, &r);
263 host = Host::DOS;
264 host_version = r.h.al;
265 host_revision = r.h.ah;
266 host_patch = 0;
267 host_multitasking = false;
268 host_fixedtimer = false;
269 detected = true;
270 }
271 if (!host_multitasking) {
272 errno = 0;
273 __dpmi_yield();
274 if (errno == 0) {
275 host_multitasking = true;
276 }
277 errno = 0;
278 }
279 std::string bios_date{};
280 {
281 {
282 char buf[8 + 1] = {};
283 for (uint32 i = 0; i < 8; ++i) {
284 buf[i] = _farpeekb(_dos_ds, (0xf000u * 16) + (0xfff5u + i));
285 }
286 bios_date = mpt::ReadAutoBuf(buf);
287 }
288 }
289 return mpt::osinfo::dos::Version(
290 _os_flavor,
291 mpt::osinfo::dos::Version::System(dos_version),
292 mpt::osinfo::dos::Version::System(dos_version_emulated),
293 mpt::osinfo::dos::Version::DPMI(dpmi_version_major, dpmi_version_minor),
294 dpmi_host_vendor,
295 mpt::osinfo::dos::Version::DPMI(dpmi_host_major, dpmi_host_minor),
296 host,
297 host_version,
298 host_revision,
299 host_patch,
300 host_multitasking,
301 host_fixedtimer,
302 bios_date);
303 }
304
305 #endif // MPT_OS_DJGPP
306
307 public:
308 static inline mpt::osinfo::dos::Version Current() {
309 #if MPT_OS_DJGPP
310 static mpt::osinfo::dos::Version s_cachedVersion = GatherDosVersion();
311 return s_cachedVersion;
312 #else // !MPT_OS_DJGPP
313 return mpt::osinfo::dos::Version::NoDos();
314 #endif // MPT_OS_DJGPP
315 }
316
317 public:
318 bool IsDos() const noexcept {
319 return m_SystemIsDos;
320 }
321
322 std::string GetOEM() const {
323 return m_OEM;
324 }
325
326 mpt::osinfo::dos::Version::System GetSystem() const noexcept {
327 return m_System;
328 }
329
330 mpt::osinfo::dos::Version::System GetSystemEmulated() const noexcept {
331 return m_SystemEmulated;
332 }
333
334 mpt::osinfo::dos::Version::DPMI GetDPMI() const noexcept {
335 return m_DPMI;
336 }
337
338 std::string GetDPMIVendor() const {
339 return m_DPMIVendor;
340 }
341
342 mpt::osinfo::dos::Version::DPMI GetDPMIHost() const noexcept {
343 return m_DPMIHost;
344 }
345
346 mpt::osinfo::dos::Version::Host GetHost() const noexcept {
347 return m_Host;
348 }
349
350 std::string GetHostName() const {
351 std::string result{};
352 switch (m_Host) {
353 case Host::DOS:
354 result = "DOS";
355 break;
356 case Host::Win2:
357 result = "Windows/386 2.x";
358 break;
359 case Host::Win3:
360 result = "Windows 3.x";
361 break;
362 case Host::Win95:
363 result = "Windows 95";
364 break;
365 case Host::Win98:
366 result = "Windows 98";
367 break;
368 case Host::WinME:
369 result = "Windows ME";
370 break;
371 case Host::WinNT:
372 result = "Windows NT";
373 break;
374 case Host::OS2:
375 result = "OS/2";
376 break;
377 case Host::OS2Warp:
378 result = "OS/2 Warp";
379 break;
380 case Host::DOSEmu:
381 result = "DOSEmu";
382 break;
383 }
384 return result;
385 }
386
387 int GetHostVersion() const noexcept {
388 return m_HostVersion;
389 }
390
391 int GetHostRevision() const noexcept {
392 return m_HostRevision;
393 }
394
395 int GetHostPatch() const noexcept {
396 return m_HostPatch;
397 }
398
399 bool IsHostMultitasking() const noexcept {
400 return m_HostMultitasking;
401 }
402
403 bool HasHostFixedTimer() const noexcept {
404 return m_HostFixedTimer;
405 }
406
407 std::string GetBIOSDate() const {
408 return m_BIOSDate;
409 }
410
411 }; // class Version
412
413
414 } // namespace dos
415
416 } // namespace osinfo
417
418
419
420 } // namespace MPT_INLINE_NS
421 } // namespace mpt
422
423
424
425 #endif // MPT_OSINFO_DOS_VERSION_HPP