summaryrefslogtreecommitdiffhomepage
path: root/Kernel/Sources/AppMain.cxx
blob: 6279589353ea30c0577797145c964f8724f545eb (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* -------------------------------------------

	Copyright SoftwareLabs

	File: AppMain.cxx
	Purpose: Kernel main loop.

------------------------------------------- */

#include <ArchKit/ArchKit.hpp>
#include <Builtins/Toolbox/Toolbox.hxx>
#include <CompilerKit/Detail.hxx>
#include <FirmwareKit/Handover.hxx>
#include <KernelKit/FileManager.hpp>
#include <KernelKit/Framebuffer.hpp>
#include <KernelKit/KernelHeap.hpp>
#include <KernelKit/PEF.hpp>
#include <KernelKit/PEFCodeManager.hxx>
#include <KernelKit/ProcessScheduler.hxx>
#include <KernelKit/UserHeap.hpp>
#include <NewKit/Json.hpp>
#include <NewKit/KernelCheck.hpp>
#include <NewKit/String.hpp>
#include <NewKit/Utils.hpp>
#include <KernelKit/CodeManager.hpp>

namespace NewOS::Detail
{
	/// @brief Filesystem auto mounter, additional checks are also done by the
	/// class.
	class FilesystemWizard final
	{
		NewOS::NewFilesystemManager* fNewFS{nullptr};

	public:
		explicit FilesystemWizard()
		{
			if (NewOS::FilesystemManagerInterface::GetMounted())
			{
				/// Mounted partition, cool!
				NewOS::kcout
					<< "New OS: No need to create for a NewFS partition here...\r";
			}
			else
			{
				/// Not mounted partition, auto-mount.
				///! Mounts a NewFS block.
				fNewFS = new NewOS::NewFilesystemManager();

				NewOS::FilesystemManagerInterface::Mount(fNewFS);

				if (fNewFS->GetImpl())
				{
					constexpr auto cFolderInfo		  = "Metadata";
					const auto	   cDirCount		  = 8;
					const char*	   cDirStr[cDirCount] = {
						   "/Boot/", "/System/", "/Support/", "/Applications/",
						   "/Users/", "/Library/", "/Mount/", "/Assistants/"};

					for (NewOS::SizeT dirIndx = 0UL; dirIndx < cDirCount; ++dirIndx)
					{
						auto catalogDir = fNewFS->GetImpl()->GetCatalog(cDirStr[dirIndx]);

						if (catalogDir)
						{
							delete catalogDir;
							continue;
						}

						catalogDir = fNewFS->GetImpl()->CreateCatalog(cDirStr[dirIndx], 0,
																	  kNewFSCatalogKindDir);

						NewFork theFork{0};

						const NewOS::Char* cSrcName = cFolderInfo;

						NewOS::rt_copy_memory((NewOS::VoidPtr)(cSrcName), theFork.ForkName,
											  NewOS::rt_string_len(cSrcName));

						NewOS::rt_copy_memory((NewOS::VoidPtr)(catalogDir->Name),
											  theFork.CatalogName,
											  NewOS::rt_string_len(catalogDir->Name));

						delete catalogDir;

						theFork.DataSize	 = kNewFSForkSize;
						theFork.ResourceId	 = 0;
						theFork.ResourceKind = NewOS::kNewFSRsrcForkKind;
						theFork.Kind		 = NewOS::kNewFSDataForkKind;

						NewOS::StringView metadataFolder(kNewFSSectorSz);

						metadataFolder +=
							"<p>Kind: folder</p>\r<p>Created by: system</p>\r<p>Edited by: "
							"system</p>\r<p>Volume Type: New OS Standard</p>\r";

						metadataFolder += "<p>File name: ";
						metadataFolder += cDirStr[dirIndx];
						metadataFolder += "</p>\r";

						const NewOS::SizeT metadataSz = kNewFSSectorSz;

						auto catalogSystem = fNewFS->GetImpl()->GetCatalog(cDirStr[dirIndx]);

						fNewFS->GetImpl()->CreateFork(catalogSystem, theFork);

						fNewFS->GetImpl()->WriteCatalog(
							catalogSystem, (NewOS::VoidPtr)(metadataFolder.CData()),
							metadataSz, cFolderInfo);

						delete catalogSystem;
					}
				}

				NewCatalog* catalogDisk =
					this->fNewFS->GetImpl()->GetCatalog("/Mount/This Disk");

				const NewOS::Char* cSrcName = "DiskInfo";

				if (catalogDisk)
				{
					auto bufferInfoDisk = (NewOS::Char*)this->fNewFS->GetImpl()->ReadCatalog(catalogDisk, kNewFSSectorSz, cSrcName);
					NewOS::kcout << bufferInfoDisk << NewOS::end_line();

					delete bufferInfoDisk;
					delete catalogDisk;
				}
				else
				{
					catalogDisk =
						(NewCatalog*)this->Leak()->CreateAlias("/Mount/This Disk");

					NewOS::StringView diskFolder(kNewFSSectorSz);

					diskFolder +=
						"<p>Kind: alias to disk</p>\r<p>Created by: system</p>\r<p>Edited "
						"by: "
						"system</p>\r<p>Volume Type: New OS Standard</p>\r";

					diskFolder += "<p>Original Path: ";
					diskFolder += NewOS::NewFilesystemHelper::Root();
					diskFolder += "</p>\r";

					NewFork theDiskFork{0};

					NewOS::rt_copy_memory((NewOS::VoidPtr)(cSrcName), theDiskFork.ForkName,
										  NewOS::rt_string_len(cSrcName));

					NewOS::rt_copy_memory((NewOS::VoidPtr)(catalogDisk->Name),
										  theDiskFork.CatalogName,
										  NewOS::rt_string_len(catalogDisk->Name));

					theDiskFork.DataSize	 = kNewFSForkSize;
					theDiskFork.ResourceId	 = 0;
					theDiskFork.ResourceKind = NewOS::kNewFSRsrcForkKind;
					theDiskFork.Kind		 = NewOS::kNewFSDataForkKind;

					fNewFS->GetImpl()->CreateFork(catalogDisk, theDiskFork);
					fNewFS->GetImpl()->WriteCatalog(catalogDisk,
													(NewOS::VoidPtr)diskFolder.CData(),
													kNewFSSectorSz, cSrcName);

					delete catalogDisk;
				}
			}
		}

		~FilesystemWizard()
		{
			delete fNewFS;
		}

		NEWOS_COPY_DEFAULT(FilesystemWizard);

		/// Grab the disk's NewFS reference.
		NewOS::NewFilesystemManager* Leak()
		{
			return fNewFS;
		}
	};

	/// @brief System loader entrypoint.
	/// @param void no parameters.
	/// @return void no return value.
	STATIC NewOS::Void AppSystem(NewOS::Void)
	{
		NewOS::PEFLoader wndServer("/System/WindowServer");

		if (!wndServer.IsLoaded())
		{
			NewOS::ke_stop(RUNTIME_CHECK_FAILED);
		}

		NewOS::Utils::execute_from_image(wndServer,
										 NewOS::ProcessHeader::kAppKind);

		NewOS::PEFLoader launchServer("/System/Launcher");

		if (!launchServer.IsLoaded())
		{
			NewOS::ke_stop(RUNTIME_CHECK_FAILED);
		}

		NewOS::Utils::execute_from_image(launchServer,
										 NewOS::ProcessHeader::kAppKind);

		NewOS::kcout << "System: done, sleeping...";

		while (true) {}
	}
} // namespace NewOS::Detail

/// @brief Application entrypoint.
/// @param Void
/// @return Void
EXTERN_C NewOS::Void AppMain(NewOS::Void)
{
	/// Now run kernel loop, until no process are running.
	NewOS::Detail::FilesystemWizard wizard; // automatic.

	auto cLoaderName = "System";
	NewOS::execute_from_image(NewOS::Detail::AppSystem, cLoaderName);

	while (NewOS::ProcessScheduler::The().Leak().Run() > 0) {}
}