summaryrefslogtreecommitdiffhomepage
path: root/dev/ZKAKit/src/KernelMain.cc
blob: 11147a82a7798a9432f11578c14fd2c135cbb90b (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
/* -------------------------------------------

	Copyright ZKA Technologies

	File: Main.cxx
	Purpose: Main entrypoint of kernel.

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

#include <KernelKit/PE.h>
#include <ArchKit/ArchKit.h>
#include <CompilerKit/Detail.h>
#include <FirmwareKit/Handover.h>
#include <KernelKit/FileMgr.h>
#include <KernelKit/Heap.h>
#include <KernelKit/PEF.h>
#include <KernelKit/PEFCodeMgr.h>
#include <KernelKit/UserProcessScheduler.h>
#include <KernelKit/Heap.h>
#include <NewKit/Json.h>
#include <NewKit/KString.h>
#include <NewKit/Utils.h>
#include <KernelKit/CodeMgr.h>
#include <CFKit/Property.h>
#include <Modules/FB/KWindow.h>
#include <KernelKit/Timer.h>

/***********************************************************************************/
/* Returns kernel's version. */
/***********************************************************************************/

EXTERN Kernel::Property cKernelVersion;

/***********************************************************************************/
/* This is an external C symbol. */
/***********************************************************************************/

EXTERN_C Kernel::Void _hal_init_mouse();
EXTERN_C Kernel::Boolean _hal_draw_mouse();

STATIC CG::ML_WINDOW_STRUCT* kKernelWnd = nullptr;

namespace Kernel::Detail
{
	/// @brief Filesystem auto formatter, additional checks are also done by the class.
	class FilesystemInstaller final
	{
		Kernel::NeFileSystemMgr* fNeFS{nullptr};

	public:
		/// @brief wizard constructor.
		explicit FilesystemInstaller()
		{
			if (Kernel::IFilesystemMgr::GetMounted())
			{
				CG::CGDrawStringToWnd(kKernelWnd, "MinOSKrnl: No need to allocate a NewFS filesystem here...", 10, 10, RGB(0, 0, 0));
				fNeFS = reinterpret_cast<Kernel::NeFileSystemMgr*>(Kernel::IFilesystemMgr::GetMounted());
			}
			else
			{
				CG::CGDrawStringToWnd(kKernelWnd, "MinOSKrnl: Allocating a NewFS filesystem here...", 10, 10, RGB(0, 0, 0));

				// Mounts a NewFS from main drive.
				fNeFS = new Kernel::NeFileSystemMgr();

				Kernel::IFilesystemMgr::Mount(fNeFS);
			}

			if (fNeFS->GetParser())
			{
				constexpr auto cFolderInfo		  = "META-INF";
				const auto	   cDirCount		  = 7;
				const char*	   cDirStr[cDirCount] = {
					   "/Boot/", "/System/", "/Support/", "/Applications/",
					   "/Users/", "/Library/", "/Mount/"};

				for (Kernel::SizeT dirIndx = 0UL; dirIndx < cDirCount; ++dirIndx)
				{
					auto catalogDir = fNeFS->GetParser()->GetCatalog(cDirStr[dirIndx]);

					if (catalogDir)
					{
						kcout << "newoskrnl: Already exists.\r";

						CG::CGDrawStringToWnd(kKernelWnd, "MinOSKrnl: Catalog already exists...", 10 + (10 * (dirIndx + 1)), 10, RGB(0, 0, 0));

						delete catalogDir;
						continue;
					}

					catalogDir = fNeFS->GetParser()->CreateCatalog(cDirStr[dirIndx], 0,
																   kNeFSCatalogKindDir);

					NFS_FORK_STRUCT theFork{0};

					const Kernel::Char* cSrcName = cFolderInfo;

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

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

					theFork.DataSize	 = kNeFSForkSize;
					theFork.ResourceId	 = 0;
					theFork.ResourceKind = Kernel::kNeFSRsrcForkKind;
					theFork.Kind		 = Kernel::kNeFSDataForkKind;

					Kernel::KString metadataFolder(kNeFSSectorSz);

					metadataFolder +=
						"<!properties/>\r<p>Kind: folder</p>\r<p>Created by: system</p>\r<p>Edited by: "
						"system</p>\r<p>Volume Type: Zeta</p>\r";

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

					const Kernel::SizeT metadataSz = kNeFSSectorSz;

					fNeFS->GetParser()->CreateFork(catalogDir, theFork);

					fNeFS->GetParser()->WriteCatalog(
						catalogDir, true, (Kernel::VoidPtr)(metadataFolder.CData()),
						metadataSz, cFolderInfo);

					CG::CGDrawStringToWnd(kKernelWnd, "MinOSKrnl: Catalog has been created...", 10 + (10 * (dirIndx + 1)), 10, RGB(0, 0, 0));

					delete catalogDir;
				}
			}

			const auto kSysPage = "/System/syspage.sys";

			NFS_CATALOG_STRUCT* catalogDisk =
				this->fNeFS->GetParser()->GetCatalog(kSysPage);

			const Kernel::Char* cSrcName = "8K_SYS_PAGE_KERNEL";

			if (catalogDisk)
			{
				delete catalogDisk;
			}
			else
			{
				catalogDisk =
					(NFS_CATALOG_STRUCT*)this->Leak()->CreateAlias(kSysPage);

				NFS_FORK_STRUCT theDiskFork{0};

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

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

				Kernel::Size sz_hdr = kNeFSForkSize;

				theDiskFork.DataSize	 = sz_hdr;
				theDiskFork.ResourceId	 = kNeFSCatalogKindExecutable | kNeFSCatalogKindPage;
				theDiskFork.ResourceKind = Kernel::kNeFSDataForkKind;
				theDiskFork.Kind		 = Kernel::kNeFSDataForkKind;

				fNeFS->GetParser()->CreateFork(catalogDisk, theDiskFork);

				delete catalogDisk;
			}
		}

		~FilesystemInstaller() = default;

		ZKA_COPY_DEFAULT(FilesystemInstaller);

		/// @brief Grab the disk's NewFS reference.
		/// @return NeFileSystemMgr the filesystem interface
		Kernel::NeFileSystemMgr* Leak()
		{
			return fNeFS;
		}
	};
} // namespace Kernel::Detail

/// @brief Application entrypoint.
/// @param Void
/// @return Void
EXTERN_C Kernel::Void ke_dll_entrypoint(Kernel::Void)
{
	CG::CGDrawBackground();

	kKernelWnd = nullptr;
	kKernelWnd = CG::CGCreateWindow(CG::kWndFlagWindow, "ZKA Operating System Kernel Log", "Window", 20, 20, CG::UIAccessibilty::The().Height() - 20, CG::UIAccessibilty::The().Width() - 20);

	kKernelWnd->w_sub_type = CG::kWndFlagCloseControlSelect;
	kKernelWnd->w_x		   = 10;
	kKernelWnd->w_y		   = 10;

	kKernelWnd->w_needs_repaint = Yes;

	CG::CGDrawWindowList(&kKernelWnd, 1);

	/// Now run kernel loop, until no process are running.
	Kernel::Detail::FilesystemInstaller(); // automatic filesystem creation.

	CG::CGDrawBackground();
}